Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

simple_list.h 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**
  2. * \file simple_list.h
  3. * Simple macros for type-safe, intrusive lists.
  4. *
  5. * Intended to work with a list sentinal which is created as an empty
  6. * list. Insert & delete are O(1).
  7. *
  8. * \author
  9. * (C) 1997, Keith Whitwell
  10. */
  11. /*
  12. * Mesa 3-D graphics library
  13. * Version: 3.5
  14. *
  15. * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
  16. *
  17. * Permission is hereby granted, free of charge, to any person obtaining a
  18. * copy of this software and associated documentation files (the "Software"),
  19. * to deal in the Software without restriction, including without limitation
  20. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  21. * and/or sell copies of the Software, and to permit persons to whom the
  22. * Software is furnished to do so, subject to the following conditions:
  23. *
  24. * The above copyright notice and this permission notice shall be included
  25. * in all copies or substantial portions of the Software.
  26. *
  27. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  28. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  30. * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  31. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. */
  34. #ifndef _SIMPLE_LIST_H
  35. #define _SIMPLE_LIST_H
  36. struct simple_node {
  37. struct simple_node *next;
  38. struct simple_node *prev;
  39. };
  40. /**
  41. * Remove an element from list.
  42. *
  43. * \param elem element to remove.
  44. */
  45. #define remove_from_list(elem) \
  46. do { \
  47. (elem)->next->prev = (elem)->prev; \
  48. (elem)->prev->next = (elem)->next; \
  49. } while (0)
  50. /**
  51. * Insert an element to the list head.
  52. *
  53. * \param list list.
  54. * \param elem element to insert.
  55. */
  56. #define insert_at_head(list, elem) \
  57. do { \
  58. (elem)->prev = list; \
  59. (elem)->next = (list)->next; \
  60. (list)->next->prev = elem; \
  61. (list)->next = elem; \
  62. } while(0)
  63. /**
  64. * Insert an element to the list tail.
  65. *
  66. * \param list list.
  67. * \param elem element to insert.
  68. */
  69. #define insert_at_tail(list, elem) \
  70. do { \
  71. (elem)->next = list; \
  72. (elem)->prev = (list)->prev; \
  73. (list)->prev->next = elem; \
  74. (list)->prev = elem; \
  75. } while(0)
  76. /**
  77. * Move an element to the list head.
  78. *
  79. * \param list list.
  80. * \param elem element to move.
  81. */
  82. #define move_to_head(list, elem) \
  83. do { \
  84. remove_from_list(elem); \
  85. insert_at_head(list, elem); \
  86. } while (0)
  87. /**
  88. * Move an element to the list tail.
  89. *
  90. * \param list list.
  91. * \param elem element to move.
  92. */
  93. #define move_to_tail(list, elem) \
  94. do { \
  95. remove_from_list(elem); \
  96. insert_at_tail(list, elem); \
  97. } while (0)
  98. /**
  99. * Consatinate a cyclic list to a list
  100. *
  101. * Appends the sequence of nodes starting with \c tail to the list \c head.
  102. * A "cyclic list" is a list that does not have a sentinal node. This means
  103. * that the data pointed to by \c tail is an actual node, not a dataless
  104. * sentinal. Note that if \c tail constist of a single node, this macro
  105. * behaves identically to \c insert_at_tail
  106. *
  107. * \param head Head of the list to be appended to. This may or may not
  108. * be a cyclic list.
  109. * \param tail Head of the cyclic list to be appended to \c head.
  110. * \param temp Temporary \c simple_list used by the macro
  111. *
  112. * \sa insert_at_tail
  113. */
  114. #define concat_list_and_cycle(head, tail, temp) \
  115. do { \
  116. (head)->prev->next = (tail); \
  117. (tail)->prev->next = (head); \
  118. (temp) = (head)->prev; \
  119. (head)->prev = (tail)->prev; \
  120. (tail)->prev = (temp); \
  121. } while (0)
  122. #define concat_list(head, next_list) \
  123. do { \
  124. (next_list)->next->prev = (head)->prev; \
  125. (next_list)->prev->next = (head); \
  126. (head)->prev->next = (next_list)->next; \
  127. (head)->prev = (next_list)->prev; \
  128. } while (0)
  129. /**
  130. * Make a empty list empty.
  131. *
  132. * \param sentinal list (sentinal element).
  133. */
  134. #define make_empty_list(sentinal) \
  135. do { \
  136. (sentinal)->next = sentinal; \
  137. (sentinal)->prev = sentinal; \
  138. } while (0)
  139. /**
  140. * Get list first element.
  141. *
  142. * \param list list.
  143. *
  144. * \return pointer to first element.
  145. */
  146. #define first_elem(list) ((list)->next)
  147. /**
  148. * Get list last element.
  149. *
  150. * \param list list.
  151. *
  152. * \return pointer to last element.
  153. */
  154. #define last_elem(list) ((list)->prev)
  155. /**
  156. * Get next element.
  157. *
  158. * \param elem element.
  159. *
  160. * \return pointer to next element.
  161. */
  162. #define next_elem(elem) ((elem)->next)
  163. /**
  164. * Get previous element.
  165. *
  166. * \param elem element.
  167. *
  168. * \return pointer to previous element.
  169. */
  170. #define prev_elem(elem) ((elem)->prev)
  171. /**
  172. * Test whether element is at end of the list.
  173. *
  174. * \param list list.
  175. * \param elem element.
  176. *
  177. * \return non-zero if element is at end of list, or zero otherwise.
  178. */
  179. #define at_end(list, elem) ((elem) == (list))
  180. /**
  181. * Test if a list is empty.
  182. *
  183. * \param list list.
  184. *
  185. * \return non-zero if list empty, or zero otherwise.
  186. */
  187. #define is_empty_list(list) ((list)->next == (list))
  188. /**
  189. * Walk through the elements of a list.
  190. *
  191. * \param ptr pointer to the current element.
  192. * \param list list.
  193. *
  194. * \note It should be followed by a { } block or a single statement, as in a \c
  195. * for loop.
  196. */
  197. #define foreach(ptr, list) \
  198. for( ptr=(list)->next ; ptr!=list ; ptr=(ptr)->next )
  199. /**
  200. * Walk through the elements of a list.
  201. *
  202. * Same as #foreach but lets you unlink the current value during a list
  203. * traversal. Useful for freeing a list, element by element.
  204. *
  205. * \param ptr pointer to the current element.
  206. * \param t temporary pointer.
  207. * \param list list.
  208. *
  209. * \note It should be followed by a { } block or a single statement, as in a \c
  210. * for loop.
  211. */
  212. #define foreach_s(ptr, t, list) \
  213. for(ptr=(list)->next,t=(ptr)->next; list != ptr; ptr=t, t=(t)->next)
  214. #endif