Clone of mesa.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

list.h 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright © 2008, 2010 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. /**
  24. * \file list.h
  25. * \brief Doubly-linked list abstract container type.
  26. *
  27. * Each doubly-linked list has a sentinal head and tail node. These nodes
  28. * contain no data. The head sentinal can be identified by its \c prev
  29. * pointer being \c NULL. The tail sentinal can be identified by its
  30. * \c next pointer being \c NULL.
  31. *
  32. * A list is empty if either the head sentinal's \c next pointer points to the
  33. * tail sentinal or the tail sentinal's \c prev poiner points to the head
  34. * sentinal.
  35. *
  36. * Instead of tracking two separate \c node structures and a \c list structure
  37. * that points to them, the sentinal nodes are in a single structure. Noting
  38. * that each sentinal node always has one \c NULL pointer, the \c NULL
  39. * pointers occupy the same memory location. In the \c list structure
  40. * contains a the following:
  41. *
  42. * - A \c head pointer that represents the \c next pointer of the
  43. * head sentinal node.
  44. * - A \c tail pointer that represents the \c prev pointer of the head
  45. * sentinal node and the \c next pointer of the tail sentinal node. This
  46. * pointer is \b always \c NULL.
  47. * - A \c tail_prev pointer that represents the \c prev pointer of the
  48. * tail sentinal node.
  49. *
  50. * Therefore, if \c head->next is \c NULL or \c tail_prev->prev is \c NULL,
  51. * the list is empty.
  52. *
  53. * To anyone familiar with "exec lists" on the Amiga, this structure should
  54. * be immediately recognizable. See the following link for the original Amiga
  55. * operating system documentation on the subject.
  56. *
  57. * http://www.natami.net/dev/Libraries_Manual_guide/node02D7.html
  58. *
  59. * \author Ian Romanick <ian.d.romanick@intel.com>
  60. */
  61. #pragma once
  62. #ifndef LIST_CONTAINER_H
  63. #define LIST_CONTAINER_H
  64. #include <assert.h>
  65. struct exec_node {
  66. struct exec_node *next;
  67. struct exec_node *prev;
  68. #ifdef __cplusplus
  69. exec_node() : next(NULL), prev(NULL)
  70. {
  71. /* empty */
  72. }
  73. const exec_node *get_next() const
  74. {
  75. return next;
  76. }
  77. exec_node *get_next()
  78. {
  79. return next;
  80. }
  81. const exec_node *get_prev() const
  82. {
  83. return prev;
  84. }
  85. exec_node *get_prev()
  86. {
  87. return prev;
  88. }
  89. void remove()
  90. {
  91. next->prev = prev;
  92. prev->next = next;
  93. next = NULL;
  94. prev = NULL;
  95. }
  96. /**
  97. * Link a node with itself
  98. *
  99. * This creates a sort of degenerate list that is occasionally useful.
  100. */
  101. void self_link()
  102. {
  103. next = this;
  104. prev = this;
  105. }
  106. /**
  107. * Insert a node in the list after the current node
  108. */
  109. void insert_after(exec_node *after)
  110. {
  111. after->next = this->next;
  112. after->prev = this;
  113. this->next->prev = after;
  114. this->next = after;
  115. }
  116. #endif
  117. };
  118. #ifdef __cplusplus
  119. struct exec_node;
  120. class iterator {
  121. public:
  122. void next()
  123. {
  124. }
  125. void *get()
  126. {
  127. return NULL;
  128. }
  129. bool has_next() const
  130. {
  131. return false;
  132. }
  133. };
  134. class exec_list_iterator : public iterator {
  135. public:
  136. exec_list_iterator(exec_node *n) : node(n), _next(n->next)
  137. {
  138. /* empty */
  139. }
  140. void next()
  141. {
  142. node = _next;
  143. _next = node->next;
  144. }
  145. void remove()
  146. {
  147. node->remove();
  148. }
  149. exec_node *get()
  150. {
  151. return node;
  152. }
  153. bool has_next() const
  154. {
  155. return _next != NULL;
  156. }
  157. private:
  158. exec_node *node;
  159. exec_node *_next;
  160. };
  161. #define foreach_iter(iter_type, iter, container) \
  162. for (iter_type iter = container . iterator(); iter.has_next(); iter.next())
  163. #endif
  164. struct exec_list {
  165. struct exec_node *head;
  166. struct exec_node *tail;
  167. struct exec_node *tail_pred;
  168. #ifdef __cplusplus
  169. exec_list()
  170. {
  171. make_empty();
  172. }
  173. void make_empty()
  174. {
  175. head = (exec_node *) & tail;
  176. tail = NULL;
  177. tail_pred = (exec_node *) & head;
  178. }
  179. bool is_empty() const
  180. {
  181. /* There are three ways to test whether a list is empty or not.
  182. *
  183. * - Check to see if the \c head points to the \c tail.
  184. * - Check to see if the \c tail_pred points to the \c head.
  185. * - Check to see if the \c head is the sentinal node by test whether its
  186. * \c next pointer is \c NULL.
  187. *
  188. * The first two methods tend to generate better code on modern systems
  189. * because they save a pointer dereference.
  190. */
  191. return head == (exec_node *) &tail;
  192. }
  193. const exec_node *get_head() const
  194. {
  195. return !is_empty() ? head : NULL;
  196. }
  197. exec_node *get_head()
  198. {
  199. return !is_empty() ? head : NULL;
  200. }
  201. const exec_node *get_tail() const
  202. {
  203. return !is_empty() ? tail_pred : NULL;
  204. }
  205. exec_node *get_tail()
  206. {
  207. return !is_empty() ? tail_pred : NULL;
  208. }
  209. void push_head(exec_node *n)
  210. {
  211. n->next = head;
  212. n->prev = (exec_node *) &head;
  213. n->next->prev = n;
  214. head = n;
  215. }
  216. void push_tail(exec_node *n)
  217. {
  218. n->next = (exec_node *) &tail;
  219. n->prev = tail_pred;
  220. n->prev->next = n;
  221. tail_pred = n;
  222. }
  223. void push_degenerate_list_at_head(exec_node *n)
  224. {
  225. assert(n->prev->next == n);
  226. n->prev->next = head;
  227. head->prev = n->prev;
  228. n->prev = (exec_node *) &head;
  229. head = n;
  230. }
  231. /**
  232. * Move all of the nodes from this list to the target list
  233. */
  234. void move_nodes_to(exec_list *target)
  235. {
  236. target->head = head;
  237. target->tail = NULL;
  238. target->tail_pred = tail_pred;
  239. target->head->prev = (exec_node *) &target->head;
  240. target->tail_pred->next = (exec_node *) &target->tail;
  241. make_empty();
  242. }
  243. exec_list_iterator iterator()
  244. {
  245. return exec_list_iterator(head);
  246. }
  247. exec_list_iterator iterator() const
  248. {
  249. return exec_list_iterator((exec_node *) head);
  250. }
  251. #endif
  252. };
  253. #endif /* LIST_CONTAINER_H */