list.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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 = NULL;
  140. assert(list);
  141. if (!list)
  142. return;
  143. while ((iter = faux_list_head(list))) {
  144. faux_list_del(list, iter);
  145. }
  146. free(list);
  147. }
  148. /** @brief Gets head of list.
  149. *
  150. * @param [in] list List.
  151. * @return List node first in list.
  152. */
  153. faux_list_node_t *faux_list_head(const faux_list_t *list) {
  154. assert(list);
  155. if (!list)
  156. return NULL;
  157. return list->head;
  158. }
  159. /** @brief Gets tail of list.
  160. *
  161. * @param [in] list List.
  162. * @return List node last in list.
  163. */
  164. faux_list_node_t *faux_list_tail(const faux_list_t *list) {
  165. assert(list);
  166. if (!list)
  167. return NULL;
  168. return list->tail;
  169. }
  170. /** @brief Gets current length of list.
  171. *
  172. * @param [in] list List.
  173. * @return Current length of list.
  174. */
  175. size_t faux_list_len(const faux_list_t *list) {
  176. assert(list);
  177. if (!list)
  178. return 0;
  179. return list->len;
  180. }
  181. /** @brief Generic static function for adding new list nodes.
  182. *
  183. * @param [in] list List to add node to.
  184. * @param [in] data User data for new list node.
  185. * @param [in] uniq - true/false Don't add entry with identical order
  186. * key (when the compareFn() returns 0)
  187. * @param [in] find - true/false Function returns list node if there is
  188. * identical entry. Or NULL if find is false.
  189. * @return Newly added list node.
  190. */
  191. static faux_list_node_t *faux_list_add_generic(faux_list_t *list, void *data,
  192. bool_t uniq, bool_t find) {
  193. faux_list_node_t *node = NULL;
  194. faux_list_node_t *iter = NULL;
  195. assert(list);
  196. assert(data);
  197. if (!list || !data)
  198. return NULL;
  199. node = faux_list_new_node(data);
  200. if (!node)
  201. return NULL;
  202. // Empty list.
  203. if (!list->head) {
  204. list->head = node;
  205. list->tail = node;
  206. list->len++;
  207. return node;
  208. }
  209. // Not sorted list. Add to the tail.
  210. if (!list->compareFn) {
  211. node->prev = list->tail;
  212. node->next = NULL;
  213. list->tail->next = node;
  214. list->tail = node;
  215. list->len++;
  216. return node;
  217. }
  218. // Sorted list.
  219. iter = list->tail;
  220. while (iter) {
  221. int res = list->compareFn(node->data, iter->data);
  222. if (uniq && (res == 0)) {
  223. faux_list_free_node(node);
  224. return (find ? iter : NULL);
  225. }
  226. if (res >= 0) {
  227. node->next = iter->next;
  228. node->prev = iter;
  229. iter->next = node;
  230. if (node->next)
  231. node->next->prev = node;
  232. break;
  233. }
  234. iter = iter->prev;
  235. }
  236. // Insert node into the list head
  237. if (!iter) {
  238. node->next = list->head;
  239. node->prev = NULL;
  240. list->head->prev = node;
  241. list->head = node;
  242. }
  243. if (!node->next)
  244. list->tail = node;
  245. list->len++;
  246. return node;
  247. }
  248. /** @brief Adds user data (not unique) to the list.
  249. *
  250. * The user data is not unique. It means that two equal user data instances
  251. * can be added to the list.
  252. *
  253. * @param [in] list List to add entry to.
  254. * @param [in] data User data.
  255. * @return Newly created list node or NULL on error.
  256. */
  257. faux_list_node_t *faux_list_add(faux_list_t *list, void *data) {
  258. return faux_list_add_generic(list, data, BOOL_FALSE, BOOL_FALSE);
  259. }
  260. /** @brief Adds user data (unique) to the list.
  261. *
  262. * The user data must be unique. It means that two equal user data instances
  263. * can not be added to the list. Function will return NULL if equal user
  264. * data is already in the list.
  265. *
  266. * @param [in] list List to add entry to.
  267. * @param [in] data User data.
  268. * @return Newly created list node or NULL on error.
  269. */
  270. faux_list_node_t *faux_list_add_uniq(faux_list_t *list, void *data) {
  271. return faux_list_add_generic(list, data, BOOL_TRUE, BOOL_FALSE);
  272. }
  273. /** @brief Adds user data (unique) to the list or return equal existent node.
  274. *
  275. * The user data must be unique in this case. Function compares list nodes
  276. * with the new one. If equal node is already in the list then function
  277. * returns this node. Else new unique node will be added to the list.
  278. *
  279. * @param [in] list List to add entry to.
  280. * @param [in] data User data.
  281. * @return Newly created list node, existent equal node or NULL on error.
  282. */
  283. faux_list_node_t *faux_list_find_add(faux_list_t *list, void *data) {
  284. return faux_list_add_generic(list, data, BOOL_TRUE, BOOL_TRUE);
  285. }
  286. /** Takes away list node from the list.
  287. *
  288. * Function removes list node from the list and returns user data
  289. * stored in this node.
  290. *
  291. * @param [in] list List to take away node from.
  292. * @param [in] node List node to take away.
  293. * @return User data from removed node or NULL on error.
  294. */
  295. void *faux_list_takeaway(faux_list_t *list, faux_list_node_t *node) {
  296. void *data = NULL;
  297. assert(list);
  298. assert(node);
  299. if (!list || !node)
  300. return NULL;
  301. if (node->prev)
  302. node->prev->next = node->next;
  303. else
  304. list->head = node->next;
  305. if (node->next)
  306. node->next->prev = node->prev;
  307. else
  308. list->tail = node->prev;
  309. list->len--;
  310. data = faux_list_data(node);
  311. faux_list_free_node(node);
  312. return data;
  313. }
  314. /** @brief Deletes list node from the list.
  315. *
  316. * Functions removes node from the list and free user data memory if
  317. * freeFn callback was defined while list creation. If freeFn callback
  318. * is not defined then function is the same as faux_list_takeaway().
  319. *
  320. * @param [in] list List to delete node from.
  321. * @param [in] node List node to delete.
  322. * @return 0 on success, < 0 on error.
  323. */
  324. int faux_list_del(faux_list_t *list, faux_list_node_t *node) {
  325. void *data = NULL;
  326. assert(list);
  327. assert(node);
  328. if (!list || !node)
  329. return -1;
  330. data = faux_list_takeaway(list, node);
  331. assert(data);
  332. if (!data) // Illegal case
  333. return -1;
  334. if (list->freeFn)
  335. list->freeFn(data);
  336. return 0;
  337. }
  338. /** @brief Search list for matching (match function).
  339. *
  340. * Function iterates through the list and executes special matching user defined
  341. * callback function matchFn for every list node. User can provide "userkey" -
  342. * the data that matchFn can use how it wants. The matchFn is arbitrary
  343. * argument. The userkey argument can be NULL. The function will immediately
  344. * return matched list node. To continue searching the saveptr argument contains
  345. * current iterator. So user can call to faux_list_match_node() for several
  346. * times and gets all matched nodes from list.
  347. *
  348. * @param [in] list List.
  349. * @param [in] matchFn User defined matching callback function.
  350. * @param [in] userkey User defined data to use in matchFn function.
  351. * @param [in,out] saveptr Ptr to save iterator.
  352. * @return Matched list node.
  353. */
  354. faux_list_node_t *faux_list_match_node(const faux_list_t *list,
  355. faux_list_match_fn matchFn, const void *userkey,
  356. faux_list_node_t **saveptr) {
  357. faux_list_node_t *iter = NULL;
  358. assert(list);
  359. assert(matchFn);
  360. if (!list || !matchFn || !list->head)
  361. return NULL;
  362. if (saveptr)
  363. iter = *saveptr;
  364. if (!iter)
  365. iter = list->head;
  366. while (iter) {
  367. int res;
  368. faux_list_node_t *node = iter;
  369. iter = faux_list_next_node(iter);
  370. if (saveptr)
  371. *saveptr = iter;
  372. res = matchFn(userkey, faux_list_data(node));
  373. if (!res)
  374. return node;
  375. if (res < 0) // No chances to find match
  376. return NULL;
  377. }
  378. return NULL;
  379. }
  380. /** @brief Search list for matching (match function) and returns user data.
  381. *
  382. * Same as faux_list_match_node() but returns user data structure.
  383. *
  384. * @sa faux_list_match_node()
  385. */
  386. void *faux_list_match(const faux_list_t *list, faux_list_match_fn matchFn,
  387. const void *userkey, faux_list_node_t **saveptr) {
  388. faux_list_node_t *res =
  389. faux_list_match_node(list, matchFn, userkey, saveptr);
  390. if (!res)
  391. return NULL;
  392. return faux_list_data(res);
  393. }
  394. /** @brief Search list for first matching (match function).
  395. *
  396. * Same as faux_list_match_node() but search for the fisrt matching.
  397. * Doesn't use saveptr iterator.
  398. *
  399. * @sa faux_list_match_node()
  400. */
  401. faux_list_node_t *faux_list_find_node(const faux_list_t *list,
  402. faux_list_match_fn matchFn, const void *userkey) {
  403. return faux_list_match_node(list, matchFn, userkey, NULL);
  404. }
  405. /** @brief Search list for first matching (match function) and returns user data.
  406. *
  407. * Same as faux_list_match_node() but returns user data structure and search
  408. * only for the first matching. Doesn't use saveptr iterator.
  409. *
  410. * @sa faux_list_match_node()
  411. */
  412. void *faux_list_find(const faux_list_t *list, faux_list_match_fn matchFn,
  413. const void *userkey) {
  414. return faux_list_match(list, matchFn, userkey, NULL);
  415. }