list.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJ_LIST_H__
  20. #define __PJ_LIST_H__
  21. /**
  22. * @file list.h
  23. * @brief Linked List data structure.
  24. */
  25. #include <pj/types.h>
  26. PJ_BEGIN_DECL
  27. /*
  28. * @defgroup PJ_DS Data Structure.
  29. */
  30. /**
  31. * @defgroup PJ_LIST Linked List
  32. * @ingroup PJ_DS
  33. * @{
  34. *
  35. * List in PJLIB is implemented as doubly-linked list, and it won't require
  36. * dynamic memory allocation (just as all PJLIB data structures). The list here
  37. * should be viewed more like a low level C list instead of high level C++ list
  38. * (which normally are easier to use but require dynamic memory allocations),
  39. * therefore all caveats with C list apply here too (such as you can NOT put
  40. * a node in more than one lists).
  41. *
  42. * \section pj_list_example_sec Examples
  43. *
  44. * See below for examples on how to manipulate linked list:
  45. * - List sample: \src{pjlib/src/pjlib-samples/list.c}
  46. * - List test: \src{pjlib/src/pjlib-test/list.c}
  47. */
  48. /**
  49. * Use this macro in the start of the structure declaration to declare that
  50. * the structure can be used in the linked list operation. This macro simply
  51. * declares additional member @a prev and @a next to the structure.
  52. * @hideinitializer
  53. */
  54. #define PJ_DECL_LIST_MEMBER(type) \
  55. /** List @a prev. */ \
  56. type *prev; \
  57. /** List @a next. */ \
  58. type *next
  59. /**
  60. * This structure describes generic list node and list. The owner of this list
  61. * must initialize the 'value' member to an appropriate value (typically the
  62. * owner itself).
  63. */
  64. struct pj_list
  65. {
  66. PJ_DECL_LIST_MEMBER(void);
  67. } PJ_ATTR_MAY_ALIAS; /* may_alias avoids warning with gcc-4.4 -Wall -O2 */
  68. /**
  69. * Initialize the list.
  70. * Initially, the list will have no member, and function pj_list_empty() will
  71. * always return nonzero (which indicates TRUE) for the newly initialized
  72. * list.
  73. *
  74. * @param node The list head.
  75. */
  76. PJ_INLINE(void) pj_list_init(pj_list_type * node)
  77. {
  78. ((pj_list*)node)->next = ((pj_list*)node)->prev = node;
  79. }
  80. /**
  81. * Check that the list is empty.
  82. *
  83. * @param node The list head.
  84. *
  85. * @return Non-zero if the list is empty, or zero if it is not empty.
  86. *
  87. */
  88. PJ_INLINE(int) pj_list_empty(const pj_list_type * node)
  89. {
  90. return ((pj_list*)node)->next == node;
  91. }
  92. /**
  93. * Insert the node to the list before the specified element position.
  94. *
  95. * @param pos The element to which the node will be inserted before.
  96. * @param node The element to be inserted.
  97. *
  98. */
  99. PJ_IDECL(void) pj_list_insert_before(pj_list_type *pos, pj_list_type *node);
  100. /**
  101. * Insert the node to the back of the list. This is just an alias for
  102. * #pj_list_insert_before().
  103. *
  104. * @param list The list.
  105. * @param node The element to be inserted.
  106. */
  107. PJ_INLINE(void) pj_list_push_back(pj_list_type *list, pj_list_type *node)
  108. {
  109. pj_list_insert_before(list, node);
  110. }
  111. /**
  112. * Inserts all nodes in \a nodes to the target list.
  113. *
  114. * @param lst The target list.
  115. * @param nodes Nodes list.
  116. */
  117. PJ_IDECL(void) pj_list_insert_nodes_before(pj_list_type *lst,
  118. pj_list_type *nodes);
  119. /**
  120. * Insert a node to the list after the specified element position.
  121. *
  122. * @param pos The element in the list which will precede the inserted
  123. * element.
  124. * @param node The element to be inserted after the position element.
  125. *
  126. */
  127. PJ_IDECL(void) pj_list_insert_after(pj_list_type *pos, pj_list_type *node);
  128. /**
  129. * Insert the node to the front of the list. This is just an alias for
  130. * #pj_list_insert_after().
  131. *
  132. * @param list The list.
  133. * @param node The element to be inserted.
  134. */
  135. PJ_INLINE(void) pj_list_push_front(pj_list_type *list, pj_list_type *node)
  136. {
  137. pj_list_insert_after(list, node);
  138. }
  139. /**
  140. * Insert all nodes in \a nodes to the target list.
  141. *
  142. * @param lst The target list.
  143. * @param nodes Nodes list.
  144. */
  145. PJ_IDECL(void) pj_list_insert_nodes_after(pj_list_type *lst,
  146. pj_list_type *nodes);
  147. /**
  148. * Insert a list to another list before the specified element position.
  149. *
  150. * @param pos The element to which the node will be inserted before.
  151. * @param lst The list to be inserted.
  152. */
  153. PJ_IDECL(void) pj_list_insert_list_before(pj_list_type *pos,
  154. pj_list_type *lst);
  155. /**
  156. * Insert a list to another list after the specified element position.
  157. *
  158. * @param pos The element in the list which will precede the inserted
  159. * list.
  160. * @param lst The list to be inserted.
  161. */
  162. PJ_IDECL(void) pj_list_insert_list_after(pj_list_type *pos,
  163. pj_list_type *lst);
  164. /**
  165. * Remove elements from the source list, and insert them to the destination
  166. * list. The elements of the source list will occupy the
  167. * front elements of the target list. Note that the node pointed by \a list2
  168. * itself is not considered as a node, but rather as the list descriptor, so
  169. * it will not be inserted to the \a list1. The elements to be inserted starts
  170. * at \a list2->next. If \a list2 is to be included in the operation, use
  171. * \a pj_list_insert_nodes_before.
  172. *
  173. * @param list1 The destination list.
  174. * @param list2 The source list.
  175. *
  176. */
  177. PJ_IDECL(void) pj_list_merge_first(pj_list_type *list1, pj_list_type *list2);
  178. /**
  179. * Remove elements from the second list argument, and insert them to the list
  180. * in the first argument. The elements from the second list will be appended
  181. * to the first list. Note that the node pointed by \a list2
  182. * itself is not considered as a node, but rather as the list descriptor, so
  183. * it will not be inserted to the \a list1. The elements to be inserted starts
  184. * at \a list2->next. If \a list2 is to be included in the operation, use
  185. * \a pj_list_insert_nodes_before.
  186. *
  187. * @param list1 The element in the list which will precede the inserted
  188. * element.
  189. * @param list2 The element in the list to be inserted.
  190. *
  191. */
  192. PJ_IDECL(void) pj_list_merge_last( pj_list_type *list1, pj_list_type *list2);
  193. /**
  194. * Erase the node from the list it currently belongs.
  195. *
  196. * @param node The element to be erased.
  197. */
  198. PJ_IDECL(void) pj_list_erase(pj_list_type *node);
  199. /**
  200. * Find node in the list.
  201. *
  202. * @param list The list head.
  203. * @param node The node element to be searched.
  204. *
  205. * @return The node itself if it is found in the list, or NULL if it is not
  206. * found in the list.
  207. */
  208. PJ_IDECL(pj_list_type*) pj_list_find_node(pj_list_type *list,
  209. pj_list_type *node);
  210. /**
  211. * Search the list for the specified value, using the specified comparison
  212. * function. This function iterates on nodes in the list, started with the
  213. * first node, and call the user supplied comparison function until the
  214. * comparison function returns ZERO.
  215. *
  216. * @param list The list head.
  217. * @param value The user defined value to be passed in the comparison
  218. * function
  219. * @param comp The comparison function, which should return ZERO to
  220. * indicate that the searched value is found.
  221. *
  222. * @return The first node that matched, or NULL if it is not found.
  223. */
  224. PJ_IDECL(pj_list_type*) pj_list_search(pj_list_type *list, void *value,
  225. int (*comp)(void *value,
  226. const pj_list_type *node)
  227. );
  228. /**
  229. * Traverse the list to get the number of elements in the list.
  230. *
  231. * @param list The list head.
  232. *
  233. * @return Number of elements.
  234. */
  235. PJ_IDECL(pj_size_t) pj_list_size(const pj_list_type *list);
  236. /**
  237. * @}
  238. */
  239. #if PJ_FUNCTIONS_ARE_INLINED
  240. # include "list_i.h"
  241. #endif
  242. PJ_END_DECL
  243. #endif /* __PJ_LIST_H__ */