list.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /** @file list.c
  2. * @brief Implementation of a bidirectional list.
  3. *
  4. * Bidirectional List stores special structures (nodes) as its elements.
  5. * Nodes are linked to each other. Node stores abstract user data (i.e. void *).
  6. *
  7. * List can be sorted or unsorted. To sort list user provides special callback
  8. * function to compare two nodes. The list will be sorted
  9. * due to this function return value that indicates "less than",
  10. * "equal", "greater than". Additionally user may provide another callback
  11. * function to free user defined data on list freeing.
  12. */
  13. #include <stdlib.h>
  14. #include <assert.h>
  15. #include <string.h>
  16. #include "private.h"
  17. #include "faux/list.h"
  18. /** @brief Allocates and initializes new list node instance.
  19. *
  20. * @param [in] data User defined data to store within node.
  21. * @return Newly created list node instance or NULL on error.
  22. */
  23. static faux_list_node_t *faux_list_new_node(void *data) {
  24. faux_list_node_t *node = NULL;
  25. node = malloc(sizeof(*node));
  26. assert(node);
  27. if (!node)
  28. return NULL;
  29. // Initialize
  30. node->prev = NULL;
  31. node->next = NULL;
  32. node->data = data;
  33. return node;
  34. }
  35. /** @brief Free list node instance.
  36. *
  37. * @param [in] node List node instance.
  38. */
  39. static void faux_list_free_node(faux_list_node_t *node) {
  40. assert(node);
  41. if (node)
  42. free(node);
  43. }
  44. /** @brief Gets previous list node.
  45. *
  46. * @param [in] this List node instance.
  47. * @return List node previous in list.
  48. */
  49. faux_list_node_t *faux_list_prev_node(const faux_list_node_t *node) {
  50. assert(node);
  51. if (!node)
  52. return NULL;
  53. return node->prev;
  54. }
  55. /** @brief Gets next list node.
  56. *
  57. * @param [in] this List node instance.
  58. * @return List node next in list.
  59. */
  60. faux_list_node_t *faux_list_next_node(const faux_list_node_t *node) {
  61. assert(node);
  62. if (!node)
  63. return NULL;
  64. return node->next;
  65. }
  66. /** @brief Gets user data from list node.
  67. *
  68. * @param [in] this List node instance.
  69. * @return User data stored within specified list node.
  70. */
  71. void *faux_list_data(const faux_list_node_t *node) {
  72. assert(node);
  73. if (!node)
  74. return NULL;
  75. return node->data;
  76. }
  77. /** @brief Iterate through each list node.
  78. *
  79. * On each call to this function the iterator will change its value.
  80. * Before function using the iterator must be initialised by list head node.
  81. *
  82. * @param [in,out] iter List node ptr used as an iterator.
  83. * @return List node or NULL if list elements are over.
  84. */
  85. faux_list_node_t *faux_list_each_node(faux_list_node_t **iter) {
  86. faux_list_node_t *current_node = *iter;
  87. if (!current_node)
  88. return NULL;
  89. *iter = faux_list_next_node(current_node);
  90. return current_node;
  91. }
  92. /** @brief Iterate through each list node and returns user data.
  93. *
  94. * On each call to this function the iterator will change its value.
  95. * Before function using the iterator must be initialised by list head node.
  96. *
  97. * @param [in,out] iter List node ptr used as an iterator.
  98. * @return User data or NULL if list elements are over.
  99. */
  100. void *faux_list_each(faux_list_node_t **iter) {
  101. faux_list_node_t *current_node = NULL;
  102. if (!*iter)
  103. return NULL;
  104. current_node = faux_list_each_node(iter);
  105. return faux_list_data(current_node);
  106. }
  107. /** @brief Allocate and initialize bidirectional list.
  108. *
  109. * @param [in] compareFn Callback function to compare two user data instances
  110. * to sort list.
  111. * @param [in] freeFn Callback function to free user data.
  112. * @return Newly created bidirectional list or NULL on error.
  113. */
  114. faux_list_t *faux_list_new(faux_list_compare_fn compareFn,
  115. faux_list_free_fn freeFn) {
  116. faux_list_t *list;
  117. list = malloc(sizeof(*list));
  118. assert(list);
  119. if (!list)
  120. return NULL;
  121. // Initialize
  122. list->head = NULL;
  123. list->tail = NULL;
  124. list->compareFn = compareFn;
  125. list->freeFn = freeFn;
  126. list->len = 0;
  127. return list;
  128. }
  129. /** @brief Free bidirectional list
  130. *
  131. * Free all nodes and user data from list and finally
  132. * free the list itself. It uses special callback
  133. * function specified by user (while faux_list_new()) to free the abstract
  134. * user data.
  135. *
  136. * @param [in] list List to free.
  137. */
  138. void faux_list_free(faux_list_t *list) {
  139. faux_list_node_t *iter;
  140. while ((iter = faux_list_head(list))) {
  141. faux_list_del(list, iter);
  142. }
  143. free(list);
  144. }
  145. /*--------------------------------------------------------- */
  146. faux_list_node_t *faux_list_head(faux_list_t *this) {
  147. return this->head;
  148. }
  149. /*--------------------------------------------------------- */
  150. faux_list_node_t *faux_list_tail(faux_list_t *this) {
  151. return this->tail;
  152. }
  153. /*--------------------------------------------------------- */
  154. size_t faux_list_len(faux_list_t *this) {
  155. return this->len;
  156. }
  157. /*--------------------------------------------------------- */
  158. /* uniq - true/false Don't add entry with identical order
  159. * key (when the compareFn() returns 0)
  160. * find - true/false Function returns list_node if there is
  161. * identical entry. Or NULL if find is false.
  162. */
  163. static faux_list_node_t *faux_list_add_generic(faux_list_t *this, void *data,
  164. bool_t uniq, bool_t find) {
  165. faux_list_node_t *node = faux_list_new_node(data);
  166. faux_list_node_t *iter;
  167. this->len++;
  168. /* Empty list */
  169. if (!this->head) {
  170. this->head = node;
  171. this->tail = node;
  172. return node;
  173. }
  174. /* Not sorted list. Add to the tail. */
  175. if (!this->compareFn) {
  176. node->prev = this->tail;
  177. node->next = NULL;
  178. this->tail->next = node;
  179. this->tail = node;
  180. return node;
  181. }
  182. /* Sorted list */
  183. iter = this->tail;
  184. while (iter) {
  185. int res = this->compareFn(node->data, iter->data);
  186. if (uniq && (res == 0)) {
  187. this->len--; // Revert previous increment
  188. return (find ? iter : NULL);
  189. }
  190. if (res >= 0) {
  191. node->next = iter->next;
  192. node->prev = iter;
  193. iter->next = node;
  194. if (node->next)
  195. node->next->prev = node;
  196. break;
  197. }
  198. iter = iter->prev;
  199. }
  200. /* Insert node into the list head */
  201. if (!iter) {
  202. node->next = this->head;
  203. node->prev = NULL;
  204. this->head->prev = node;
  205. this->head = node;
  206. }
  207. if (!node->next)
  208. this->tail = node;
  209. return node;
  210. }
  211. /*--------------------------------------------------------- */
  212. faux_list_node_t *faux_list_add(faux_list_t *this, void *data) {
  213. return faux_list_add_generic(this, data, BOOL_FALSE, BOOL_FALSE);
  214. }
  215. /*--------------------------------------------------------- */
  216. faux_list_node_t *faux_list_add_uniq(faux_list_t *this, void *data) {
  217. return faux_list_add_generic(this, data, BOOL_TRUE, BOOL_FALSE);
  218. }
  219. /*--------------------------------------------------------- */
  220. faux_list_node_t *faux_list_find_add(faux_list_t *this, void *data) {
  221. return faux_list_add_generic(this, data, BOOL_TRUE, BOOL_TRUE);
  222. }
  223. void *faux_list_takeaway(faux_list_t *list, faux_list_node_t *node) {
  224. void *data = NULL;
  225. assert(list);
  226. assert(node);
  227. if (!list || !node)
  228. return NULL;
  229. if (node->prev)
  230. node->prev->next = node->next;
  231. else
  232. list->head = node->next;
  233. if (node->next)
  234. node->next->prev = node->prev;
  235. else
  236. list->tail = node->prev;
  237. list->len--;
  238. data = faux_list_data(node);
  239. faux_list_free_node(node);
  240. return data;
  241. }
  242. /*--------------------------------------------------------- */
  243. void faux_list_del(faux_list_t *list, faux_list_node_t *node) {
  244. void *data = NULL;
  245. assert(list);
  246. assert(node);
  247. if (!list || !node)
  248. return;
  249. data = faux_list_takeaway(list, node);
  250. if (list->freeFn)
  251. list->freeFn(data);
  252. }
  253. /*--------------------------------------------------------- */
  254. faux_list_node_t *faux_list_match_node(faux_list_t *this,
  255. faux_list_match_fn matchFn, const void *userkey,
  256. faux_list_node_t **saveptr) {
  257. faux_list_node_t *iter = NULL;
  258. if (!this || !matchFn || !this->head)
  259. return NULL;
  260. if (saveptr)
  261. iter = *saveptr;
  262. if (!iter)
  263. iter = this->head;
  264. while (iter) {
  265. int res;
  266. faux_list_node_t *node = iter;
  267. iter = faux_list_next_node(iter);
  268. if (saveptr)
  269. *saveptr = iter;
  270. res = matchFn(userkey, faux_list_data(node));
  271. if (!res)
  272. return node;
  273. if (res < 0) // No chances to find match
  274. return NULL;
  275. }
  276. return NULL;
  277. }
  278. /*--------------------------------------------------------- */
  279. faux_list_node_t *faux_list_find_node(faux_list_t *this,
  280. faux_list_match_fn matchFn, const void *userkey) {
  281. return faux_list_match_node(this, matchFn, userkey, NULL);
  282. }
  283. /*--------------------------------------------------------- */
  284. void *faux_list_match(faux_list_t *this,
  285. faux_list_match_fn matchFn, const void *userkey,
  286. faux_list_node_t **saveptr) {
  287. faux_list_node_t *res =
  288. faux_list_match_node(this, matchFn, userkey, saveptr);
  289. if (!res)
  290. return NULL;
  291. return faux_list_data(res);
  292. }
  293. /*--------------------------------------------------------- */
  294. void *faux_list_find(faux_list_t *this,
  295. faux_list_match_fn matchFn, const void *userkey) {
  296. return faux_list_match(this, matchFn, userkey, NULL);
  297. }