LinkedList.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*******************************************************************************
  2. *
  3. * Copyright (c) 2000-2003 Intel Corporation
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither name of Intel Corporation nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  26. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. ******************************************************************************/
  31. #ifndef LINKED_LIST_H
  32. #define LINKED_LIST_H
  33. /*!
  34. * \file
  35. */
  36. #include "FreeList.h"
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. #define EOUTOFMEM (-7 & 1<<29)
  41. #define FREELISTSIZE 100
  42. #define LIST_SUCCESS 1
  43. #define LIST_FAIL 0
  44. /*! Function for freeing list items. */
  45. typedef void (*free_function)(void *arg);
  46. /*! Function for comparing list items. Returns 1 if itemA==itemB */
  47. typedef int (*cmp_routine)(void *itemA,void *itemB);
  48. /*! Linked list node. Stores generic item and pointers to next and prev.
  49. * \internal
  50. */
  51. typedef struct LISTNODE
  52. {
  53. struct LISTNODE *prev;
  54. struct LISTNODE *next;
  55. void *item;
  56. } ListNode;
  57. /*!
  58. * Linked list (no protection).
  59. *
  60. * Because this is for internal use, parameters are NOT checked for validity.
  61. * The first item of the list is stored at node: head->next
  62. * The last item of the list is stored at node: tail->prev
  63. * If head->next=tail, then list is empty.
  64. * To iterate through the list:
  65. *
  66. * LinkedList g;
  67. * ListNode *temp = NULL;
  68. * for (temp = ListHead(g);temp!=NULL;temp = ListNext(g,temp)) {
  69. * }
  70. *
  71. * \internal
  72. */
  73. typedef struct LINKEDLIST
  74. {
  75. /*! head, first item is stored at: head->next */
  76. ListNode head;
  77. /*! tail, last item is stored at: tail->prev */
  78. ListNode tail;
  79. /*! size of list */
  80. long size;
  81. /*! free list to use */
  82. FreeList freeNodeList;
  83. /*! free function to use */
  84. free_function free_func;
  85. /*! compare function to use */
  86. cmp_routine cmp_func;
  87. } LinkedList;
  88. /*!
  89. * \brief Initializes LinkedList. Must be called first and only once for List.
  90. *
  91. * \return
  92. * \li \c 0 on success.
  93. * \li \c EOUTOFMEM on failure.
  94. */
  95. int ListInit(
  96. /*! Must be valid, non null, pointer to a linked list. */
  97. LinkedList *list,
  98. /*! Function used to compare items. (May be NULL). */
  99. cmp_routine cmp_func,
  100. /*! Function used to free items. (May be NULL). */
  101. free_function free_func);
  102. /*!
  103. * \brief Adds a node to the head of the list. Node gets immediately after
  104. * list head.
  105. *
  106. * Precondition:
  107. * The list has been initialized.
  108. *
  109. * \return The pointer to the ListNode on success, NULL on failure.
  110. */
  111. ListNode *ListAddHead(
  112. /*! Must be valid, non null, pointer to a linked list. */
  113. LinkedList *list,
  114. /*! Item to be added. */
  115. void *item);
  116. /*!
  117. * \brief Adds a node to the tail of the list. Node gets added immediately
  118. * before list.tail.
  119. *
  120. * Precondition: The list has been initialized.
  121. *
  122. * \return The pointer to the ListNode on success, NULL on failure.
  123. */
  124. ListNode *ListAddTail(
  125. /*! Must be valid, non null, pointer to a linked list. */
  126. LinkedList *list,
  127. /*! Item to be added. */
  128. void *item);
  129. /*!
  130. * \brief Adds a node after the specified node. Node gets added immediately
  131. * after bnode.
  132. *
  133. * Precondition: The list has been initialized.
  134. *
  135. * \return The pointer to the ListNode on success, NULL on failure.
  136. */
  137. ListNode *ListAddAfter(
  138. /*! Must be valid, non null, pointer to a linked list. */
  139. LinkedList *list,
  140. /*! Item to be added. */
  141. void *item,
  142. /*! Node to add after. */
  143. ListNode *bnode);
  144. /*!
  145. * \brief Adds a node before the specified node. Node gets added immediately
  146. * before anode.
  147. *
  148. * Precondition: The list has been initialized.
  149. *
  150. * \return The pointer to the ListNode on success, NULL on failure.
  151. */
  152. ListNode *ListAddBefore(
  153. /*! Must be valid, non null, pointer to a linked list. */
  154. LinkedList *list,
  155. /*! Item to be added. */
  156. void *item,
  157. /*! Node to add in front of. */
  158. ListNode *anode);
  159. /*!
  160. * \brief Removes a node from the list. The memory for the node is freed.
  161. *
  162. * Precondition: The list has been initialized.
  163. *
  164. * \return The pointer to the item stored in the node or NULL if the item
  165. * is freed.
  166. */
  167. void *ListDelNode(
  168. /*! Must be valid, non null, pointer to a linked list. */
  169. LinkedList *list,
  170. /*! Node to delete. */
  171. ListNode *dnode,
  172. /*! if !0 then item is freed using free function. If 0 (or free
  173. * function is NULL) then item is not freed. */
  174. int freeItem);
  175. /*!
  176. * \brief Removes all memory associated with list nodes. Does not free
  177. * LinkedList *list.
  178. *
  179. * Precondition: The list has been initialized.
  180. *
  181. * \return 0 on success, EINVAL on failure.
  182. */
  183. int ListDestroy(
  184. /*! Must be valid, non null, pointer to a linked list. */
  185. LinkedList *list,
  186. /*! if !0 then item is freed using free function. If 0 (or free
  187. * function is NULL) then item is not freed. */
  188. int freeItem);
  189. /*!
  190. * \brief Returns the head of the list.
  191. *
  192. * Precondition: The list has been initialized.
  193. *
  194. * \return The head of the list. NULL if list is empty.
  195. */
  196. ListNode *ListHead(
  197. /*! Must be valid, non null, pointer to a linked list. */
  198. LinkedList *list);
  199. /*!
  200. * \brief Returns the tail of the list.
  201. *
  202. * Precondition: The list has been initialized.
  203. *
  204. * \return The tail of the list. NULL if list is empty.
  205. */
  206. ListNode *ListTail(
  207. /*! Must be valid, non null, pointer to a linked list. */
  208. LinkedList *list);
  209. /*!
  210. * \brief Returns the next item in the list.
  211. *
  212. * Precondition: The list has been initialized.
  213. *
  214. * \return The next item in the list. NULL if there are no more items in list.
  215. */
  216. ListNode *ListNext(
  217. /*! Must be valid, non null, pointer to a linked list. */
  218. LinkedList *list,
  219. /*! Node from the list. */
  220. ListNode *node);
  221. /*!
  222. * \brief Returns the previous item in the list.
  223. *
  224. * Precondition: The list has been initialized.
  225. *
  226. * \return The previous item in the list. NULL if there are no more items in list.
  227. */
  228. ListNode *ListPrev(
  229. /*! Must be valid, non null, pointer to a linked list. */
  230. LinkedList *list,
  231. /*! Node from the list. */
  232. ListNode *node);
  233. /*!
  234. * \brief Finds the specified item in the list.
  235. *
  236. * Uses the compare function specified in ListInit. If compare function
  237. * is NULL then compares items as pointers.
  238. *
  239. * Precondition: The list has been initialized.
  240. *
  241. * \return The node containing the item. NULL if no node contains the item.
  242. */
  243. ListNode* ListFind(
  244. /*! Must be valid, non null, pointer to a linked list. */
  245. LinkedList *list,
  246. /*! The node to start from, NULL if to start from beginning. */
  247. ListNode *start,
  248. /*! The item to search for. */
  249. void *item);
  250. /*!
  251. * \brief Returns the size of the list.
  252. *
  253. * Precondition: The list has been initialized.
  254. *
  255. * \return The number of items in the list.
  256. */
  257. long ListSize(
  258. /*! Must be valid, non null, pointer to a linked list. */
  259. LinkedList* list);
  260. #ifdef __cplusplus
  261. }
  262. #endif
  263. #endif /* LINKED_LIST_H */