list.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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. {
  25. faux_list_node_t *node = NULL;
  26. node = faux_zmalloc(sizeof(*node));
  27. assert(node);
  28. if (!node)
  29. return NULL;
  30. // Initialize
  31. node->prev = NULL;
  32. node->next = NULL;
  33. node->data = data;
  34. return node;
  35. }
  36. /** @brief Free list node instance.
  37. *
  38. * @param [in] node List node instance.
  39. */
  40. static void faux_list_free_node(faux_list_node_t *node)
  41. {
  42. faux_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. {
  51. assert(node);
  52. if (!node)
  53. return NULL;
  54. return node->prev;
  55. }
  56. /** @brief Gets next list node.
  57. *
  58. * @param [in] this List node instance.
  59. * @return List node next in list.
  60. */
  61. faux_list_node_t *faux_list_next_node(const faux_list_node_t *node)
  62. {
  63. assert(node);
  64. if (!node)
  65. return NULL;
  66. return node->next;
  67. }
  68. /** @brief Gets user data from list node.
  69. *
  70. * @param [in] this List node instance.
  71. * @return User data stored within specified list node.
  72. */
  73. void *faux_list_data(const faux_list_node_t *node)
  74. {
  75. assert(node);
  76. if (!node)
  77. return NULL;
  78. return node->data;
  79. }
  80. /** @brief Iterate through each list node.
  81. *
  82. * On each call to this function the iterator will change its value.
  83. * Before function using the iterator must be initialised by list head node.
  84. *
  85. * @param [in,out] iter List node ptr used as an iterator.
  86. * @return List node or NULL if list elements are over.
  87. */
  88. faux_list_node_t *faux_list_each_node(faux_list_node_t **iter)
  89. {
  90. faux_list_node_t *current_node = *iter;
  91. // No assert() on current_node. NULL iterator is normal
  92. if (!current_node)
  93. return NULL;
  94. *iter = faux_list_next_node(current_node);
  95. return current_node;
  96. }
  97. /** @brief Iterate through each list node. Reverse order.
  98. *
  99. * On each call to this function the iterator will change its value.
  100. * Before function using the iterator must be initialised by list tail node.
  101. *
  102. * @param [in,out] iter List node ptr used as an iterator.
  103. * @return List node or NULL if list elements are over.
  104. */
  105. faux_list_node_t *faux_list_eachr_node(faux_list_node_t **iter)
  106. {
  107. faux_list_node_t *current_node = *iter;
  108. // No assert() on current_node. NULL iterator is normal
  109. if (!current_node)
  110. return NULL;
  111. *iter = faux_list_prev_node(current_node);
  112. return current_node;
  113. }
  114. /** @brief Iterate through each list node and returns user data.
  115. *
  116. * On each call to this function the iterator will change its value.
  117. * Before function using the iterator must be initialised by list head node.
  118. *
  119. * @param [in,out] iter List node ptr used as an iterator.
  120. * @return User data or NULL if list elements are over.
  121. */
  122. void *faux_list_each(faux_list_node_t **iter)
  123. {
  124. faux_list_node_t *current_node = NULL;
  125. // No assert() on current_node. NULL iterator is normal
  126. if (!*iter)
  127. return NULL;
  128. current_node = faux_list_each_node(iter);
  129. return faux_list_data(current_node);
  130. }
  131. /** @brief Iterate (reverse order) through each list node and returns user data.
  132. *
  133. * On each call to this function the iterator will change its value.
  134. * Before function using the iterator must be initialised by list head node.
  135. *
  136. * @param [in,out] iter List node ptr used as an iterator.
  137. * @return User data or NULL if list elements are over.
  138. */
  139. void *faux_list_eachr(faux_list_node_t **iter)
  140. {
  141. faux_list_node_t *current_node = NULL;
  142. // No assert() on current_node. NULL iterator is normal
  143. if (!*iter)
  144. return NULL;
  145. current_node = faux_list_eachr_node(iter);
  146. return faux_list_data(current_node);
  147. }
  148. /** @brief Allocate and initialize bidirectional list.
  149. *
  150. * Prototypes for callback functions:
  151. * @code
  152. * int (*faux_list_cmp_fn)(const void *new_item, const void *list_item);
  153. * void faux_list_free_fn(void *data);
  154. * @endcode
  155. *
  156. * @param [in] sorted If list is sorted - FAUX_LIST_SORTED, unsorted - FAUX_LIST_UNSORTED.
  157. * @param [in] unique If list entry is unique - FAUX_LIST_UNIQUE, else - FAUX_LIST_NONUNIQUE.
  158. * @param [in] compareFn Callback function to compare two user data instances
  159. * to sort list.
  160. * @param [in] freeFn Callback function to free user data.
  161. * @return Newly created bidirectional list or NULL on error.
  162. */
  163. faux_list_t *faux_list_new(faux_list_sorted_t sorted, faux_list_unique_t unique,
  164. faux_list_cmp_fn cmpFn, faux_list_kcmp_fn kcmpFn,
  165. faux_list_free_fn freeFn)
  166. {
  167. faux_list_t *list = NULL;
  168. // Sorted list must have cmpFn
  169. if (sorted && !cmpFn)
  170. return NULL;
  171. // Unique list must have cmpFn
  172. if (unique && !cmpFn)
  173. return NULL;
  174. list = faux_zmalloc(sizeof(*list));
  175. assert(list);
  176. if (!list)
  177. return NULL;
  178. // Initialize
  179. list->head = NULL;
  180. list->tail = NULL;
  181. list->sorted = sorted;
  182. list->unique = unique;
  183. list->cmpFn = cmpFn;
  184. list->kcmpFn = kcmpFn;
  185. list->freeFn = freeFn;
  186. list->len = 0;
  187. return list;
  188. }
  189. /** @brief Empty list
  190. *
  191. * Removes and frees all list entries.
  192. *
  193. * @param [in] list List to empty.
  194. */
  195. void faux_list_empty(faux_list_t *list)
  196. {
  197. faux_list_node_t *iter = NULL;
  198. if (!list)
  199. return;
  200. while ((iter = faux_list_head(list))) {
  201. faux_list_del(list, iter);
  202. }
  203. }
  204. /** @brief Free bidirectional list
  205. *
  206. * Free all nodes and user data from list and finally
  207. * free the list itself. It uses special callback
  208. * function specified by user (while faux_list_new()) to free the abstract
  209. * user data.
  210. *
  211. * @param [in] list List to free.
  212. */
  213. void faux_list_free(faux_list_t *list)
  214. {
  215. faux_list_empty(list);
  216. faux_free(list);
  217. }
  218. /** @brief Gets head of list.
  219. *
  220. * @param [in] list List.
  221. * @return List node first in list.
  222. */
  223. faux_list_node_t *faux_list_head(const faux_list_t *list)
  224. {
  225. assert(list);
  226. if (!list)
  227. return NULL;
  228. return list->head;
  229. }
  230. /** @brief Gets tail of list.
  231. *
  232. * @param [in] list List.
  233. * @return List node last in list.
  234. */
  235. faux_list_node_t *faux_list_tail(const faux_list_t *list)
  236. {
  237. assert(list);
  238. if (!list)
  239. return NULL;
  240. return list->tail;
  241. }
  242. /** @brief Gets current length of list.
  243. *
  244. * @param [in] list List.
  245. * @return Current length of list.
  246. */
  247. size_t faux_list_len(const faux_list_t *list)
  248. {
  249. assert(list);
  250. if (!list)
  251. return 0;
  252. return list->len;
  253. }
  254. /** @brief Generic static function for adding new list nodes.
  255. *
  256. * @param [in] list List to add node to.
  257. * @param [in] data User data for new list node.
  258. * key (when the cmpFn() returns 0)
  259. * @param [in] find - true/false Function returns list node if there is
  260. * identical entry. Or NULL if find is false.
  261. * @return Newly added list node.
  262. */
  263. static faux_list_node_t *faux_list_add_generic(
  264. faux_list_t *list, void *data, bool_t find)
  265. {
  266. faux_list_node_t *node = NULL;
  267. faux_list_node_t *iter = NULL;
  268. assert(list);
  269. assert(data);
  270. if (!list || !data)
  271. return NULL;
  272. node = faux_list_new_node(data);
  273. if (!node)
  274. return NULL;
  275. // Empty list
  276. if (!list->head) {
  277. list->head = node;
  278. list->tail = node;
  279. list->len++;
  280. return node;
  281. }
  282. // Non-sorted: Insert to tail
  283. if (!list->sorted) {
  284. // Unique: Search through whole list
  285. if (list->unique) {
  286. iter = list->tail;
  287. while (iter) {
  288. int res = list->cmpFn(node->data, iter->data);
  289. if (0 == res) { // Already in list
  290. faux_list_free_node(node);
  291. return (find ? iter : NULL);
  292. }
  293. iter = iter->prev;
  294. }
  295. }
  296. // Add entry to the tail
  297. node->prev = list->tail;
  298. node->next = NULL;
  299. if (list->tail)
  300. list->tail->next = node;
  301. list->tail = node;
  302. list->len++;
  303. return node;
  304. }
  305. // Sorted: Insert from tail
  306. iter = list->tail;
  307. while (iter) {
  308. int res = list->cmpFn(node->data, iter->data);
  309. // Unique: Already exists
  310. if (list->unique && (0 == res)) {
  311. faux_list_free_node(node);
  312. return (find ? iter : NULL);
  313. }
  314. // Non-unique: Entry will be inserted after existent one
  315. if (res >= 0) {
  316. node->next = iter->next;
  317. node->prev = iter;
  318. iter->next = node;
  319. if (node->next)
  320. node->next->prev = node;
  321. break;
  322. }
  323. iter = iter->prev;
  324. }
  325. // Insert node into the list head
  326. if (!iter) {
  327. node->next = list->head;
  328. node->prev = NULL;
  329. list->head->prev = node;
  330. list->head = node;
  331. }
  332. if (!node->next)
  333. list->tail = node;
  334. list->len++;
  335. return node;
  336. }
  337. /** @brief Adds user data to the list.
  338. *
  339. * The user data is not unique. It means that two equal user data instances
  340. * can be added to the list.
  341. *
  342. * @param [in] list List to add entry to.
  343. * @param [in] data User data.
  344. * @return Newly created list node or NULL on error.
  345. */
  346. faux_list_node_t *faux_list_add(faux_list_t *list, void *data)
  347. {
  348. return faux_list_add_generic(list, data, BOOL_FALSE);
  349. }
  350. /** @brief Adds user data (unique) to the list or return equal existent node.
  351. *
  352. * The user data must be unique in this case. Function compares list nodes
  353. * with the new one. If equal node is already in the list then function
  354. * returns this node. Else new unique node will be added to the list.
  355. *
  356. * @param [in] list List to add entry to.
  357. * @param [in] data User data.
  358. * @return Newly created list node, existent equal node or NULL on error.
  359. */
  360. faux_list_node_t *faux_list_add_find(faux_list_t *list, void *data)
  361. {
  362. assert(list);
  363. if (!list)
  364. return NULL;
  365. // Function add_find has no meaning for non-unique list. What is the
  366. // function behaviour? It found entry. Must it return existent entry or
  367. // add new non-unique entry?
  368. if (!list->unique)
  369. return NULL;
  370. return faux_list_add_generic(list, data, BOOL_TRUE);
  371. }
  372. /** Takes away list node from the list.
  373. *
  374. * Function removes list node from the list and returns user data
  375. * stored in this node.
  376. *
  377. * @param [in] list List to take away node from.
  378. * @param [in] node List node to take away.
  379. * @return User data from removed node or NULL on error.
  380. */
  381. void *faux_list_takeaway(faux_list_t *list, faux_list_node_t *node)
  382. {
  383. void *data = NULL;
  384. assert(list);
  385. assert(node);
  386. if (!list || !node)
  387. return NULL;
  388. if (node->prev)
  389. node->prev->next = node->next;
  390. else
  391. list->head = node->next;
  392. if (node->next)
  393. node->next->prev = node->prev;
  394. else
  395. list->tail = node->prev;
  396. list->len--;
  397. data = faux_list_data(node);
  398. faux_list_free_node(node);
  399. return data;
  400. }
  401. /** @brief Deletes list node from the list.
  402. *
  403. * Functions removes node from the list and free user data memory if
  404. * freeFn callback was defined while list creation. If freeFn callback
  405. * is not defined then function is the same as faux_list_takeaway().
  406. *
  407. * @param [in] list List to delete node from.
  408. * @param [in] node List node to delete.
  409. * @return 0 on success, < 0 on error.
  410. */
  411. int faux_list_del(faux_list_t *list, faux_list_node_t *node)
  412. {
  413. void *data = NULL;
  414. assert(list);
  415. assert(node);
  416. if (!list || !node)
  417. return -1;
  418. data = faux_list_takeaway(list, node);
  419. assert(data);
  420. if (!data) // Illegal case
  421. return -1;
  422. if (list->freeFn)
  423. list->freeFn(data);
  424. return 0;
  425. }
  426. /** @brief Deletes list node from the list by user key.
  427. *
  428. * @sa faux_list_del()
  429. * @param [in] list List to delete node from.
  430. * @param [in] node User key to find node to delete.
  431. * @return 0 on success, < 0 on error.
  432. */
  433. int faux_list_kdel(faux_list_t *list, const void *userkey)
  434. {
  435. faux_list_node_t *node = NULL;
  436. assert(list);
  437. assert(userkey);
  438. if (!list || !userkey)
  439. return -1;
  440. node = faux_list_kfind_node(list, userkey);
  441. if (!node)
  442. return -1; // Not found
  443. return faux_list_del(list, node);
  444. }
  445. /** @brief Search list for matching (match function).
  446. *
  447. * Function iterates through the list and executes special matching user defined
  448. * callback function matchFn for every list node. User can provide "userkey" -
  449. * the data that matchFn can use how it wants. The matchFn is arbitrary
  450. * argument. The userkey argument can be NULL. The function will immediately
  451. * return matched list node. To continue searching the saveptr argument contains
  452. * current iterator. So user can call to faux_list_match_node() for several
  453. * times and gets all matched nodes from list.
  454. *
  455. * Prototype for matchFn callback function:
  456. * @code
  457. * int (*faux_list_kcmp_fn)(const void *key, const void *list_item);
  458. * @endcode
  459. *
  460. * @param [in] list List.
  461. * @param [in] matchFn User defined matching callback function.
  462. * @param [in] userkey User defined data to use in matchFn function.
  463. * @param [in,out] saveptr Ptr to save iterator.
  464. * @return Matched list node.
  465. */
  466. faux_list_node_t *faux_list_match_node(const faux_list_t *list,
  467. faux_list_kcmp_fn matchFn, const void *userkey,
  468. faux_list_node_t **saveptr)
  469. {
  470. faux_list_node_t *iter = NULL;
  471. assert(list);
  472. assert(matchFn);
  473. if (!list || !matchFn || !list->head)
  474. return NULL;
  475. if (saveptr)
  476. iter = *saveptr;
  477. if (!iter)
  478. iter = list->head;
  479. while (iter) {
  480. int res = 0;
  481. faux_list_node_t *node = iter;
  482. iter = faux_list_next_node(node);
  483. if (saveptr)
  484. *saveptr = iter;
  485. res = matchFn(userkey, faux_list_data(node));
  486. if (0 == res)
  487. return node;
  488. if (list->sorted && (res < 0)) // No chances to find match
  489. return NULL;
  490. }
  491. return NULL;
  492. }
  493. /** @brief Search list for matching (key cmp function).
  494. *
  495. * Same as faux_list_match_node() but uses userkey compare function defined
  496. * while faux_list_new() function call.
  497. *
  498. * @sa faux_list_match_node()
  499. */
  500. faux_list_node_t *faux_list_kmatch_node(const faux_list_t *list,
  501. const void *userkey, faux_list_node_t **saveptr)
  502. {
  503. assert(list);
  504. if (!list)
  505. return NULL;
  506. return faux_list_match_node(list, list->kcmpFn, userkey, saveptr);
  507. }
  508. /** @brief Search list for matching (match function) and returns user data.
  509. *
  510. * Same as faux_list_match_node() but returns user data structure.
  511. *
  512. * @sa faux_list_match_node()
  513. */
  514. void *faux_list_match(const faux_list_t *list, faux_list_kcmp_fn matchFn,
  515. const void *userkey, faux_list_node_t **saveptr)
  516. {
  517. faux_list_node_t *res =
  518. faux_list_match_node(list, matchFn, userkey, saveptr);
  519. if (!res)
  520. return NULL;
  521. return faux_list_data(res);
  522. }
  523. /** @brief Search list for matching (key cmp function) and returns user data.
  524. *
  525. * Same as faux_list_match() but uses userkey compare function defined
  526. * while faux_list_new() function call.
  527. *
  528. * @sa faux_list_match_node()
  529. */
  530. void *faux_list_kmatch(const faux_list_t *list, const void *userkey,
  531. faux_list_node_t **saveptr)
  532. {
  533. assert(list);
  534. if (!list)
  535. return NULL;
  536. return faux_list_match(list, list->kcmpFn, userkey, saveptr);
  537. }
  538. /** @brief Search list for first matching (match function).
  539. *
  540. * Same as faux_list_match_node() but search for the fisrt matching.
  541. * Doesn't use saveptr iterator.
  542. *
  543. * @sa faux_list_match_node()
  544. */
  545. faux_list_node_t *faux_list_find_node(const faux_list_t *list,
  546. faux_list_kcmp_fn matchFn, const void *userkey)
  547. {
  548. return faux_list_match_node(list, matchFn, userkey, NULL);
  549. }
  550. /** @brief Search list for first matching (key cmp function).
  551. *
  552. * Same as faux_list_find_node() but uses userkey compare function defined
  553. * while faux_list_new() function call.
  554. *
  555. * @sa faux_list_match_node()
  556. */
  557. faux_list_node_t *faux_list_kfind_node(const faux_list_t *list,
  558. const void *userkey)
  559. {
  560. return faux_list_find_node(list, list->kcmpFn, userkey);
  561. }
  562. /** @brief Search list for first matching (match function) and returns user data.
  563. *
  564. * Same as faux_list_match_node() but returns user data structure and search
  565. * only for the first matching. Doesn't use saveptr iterator.
  566. *
  567. * @sa faux_list_match_node()
  568. */
  569. void *faux_list_find(const faux_list_t *list, faux_list_kcmp_fn matchFn,
  570. const void *userkey)
  571. {
  572. return faux_list_match(list, matchFn, userkey, NULL);
  573. }
  574. /** @brief Search list for first matching (key cmp function). Returns user data.
  575. *
  576. * Same as faux_list_find() but uses userkey compare function defined
  577. * while faux_list_new() function call.
  578. *
  579. * @sa faux_list_match_node()
  580. */
  581. void *faux_list_kfind(const faux_list_t *list,
  582. const void *userkey)
  583. {
  584. return faux_list_find(list, list->kcmpFn, userkey);
  585. }