list.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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_e sorted, faux_list_unique_e 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 Delete all entries from list
  190. *
  191. * Removes and frees all list entries.
  192. *
  193. * @param [in] list List to empty.
  194. * @return Number of deleted entries or < 0 on error.
  195. */
  196. ssize_t faux_list_del_all(faux_list_t *list)
  197. {
  198. faux_list_node_t *iter = NULL;
  199. ssize_t num = 0;
  200. if (!list)
  201. return -1;
  202. while ((iter = faux_list_head(list))) {
  203. faux_list_del(list, iter);
  204. num++;
  205. }
  206. return num;
  207. }
  208. /** @brief Free bidirectional list
  209. *
  210. * Free all nodes and user data from list and finally
  211. * free the list itself. It uses special callback
  212. * function specified by user (while faux_list_new()) to free the abstract
  213. * user data.
  214. *
  215. * @param [in] list List to free.
  216. */
  217. void faux_list_free(faux_list_t *list)
  218. {
  219. faux_list_del_all(list);
  220. faux_free(list);
  221. }
  222. /** @brief Gets head of list.
  223. *
  224. * @param [in] list List.
  225. * @return List node first in list.
  226. */
  227. faux_list_node_t *faux_list_head(const faux_list_t *list)
  228. {
  229. assert(list);
  230. if (!list)
  231. return NULL;
  232. return list->head;
  233. }
  234. /** @brief Gets tail of list.
  235. *
  236. * @param [in] list List.
  237. * @return List node last in list.
  238. */
  239. faux_list_node_t *faux_list_tail(const faux_list_t *list)
  240. {
  241. assert(list);
  242. if (!list)
  243. return NULL;
  244. return list->tail;
  245. }
  246. /** @brief Gets current length of list.
  247. *
  248. * @param [in] list List.
  249. * @return Current length of list.
  250. */
  251. size_t faux_list_len(const faux_list_t *list)
  252. {
  253. assert(list);
  254. if (!list)
  255. return 0;
  256. return list->len;
  257. }
  258. /** @brief Checks is list empty.
  259. *
  260. * @param [in] list Allocated list.
  261. * @return BOOL_TRUE - empty, BOOL_FALSE - not empty.
  262. */
  263. bool_t faux_list_is_empty(const faux_list_t *list)
  264. {
  265. assert(list);
  266. if (!list)
  267. return BOOL_TRUE;
  268. if (faux_list_len(list) == 0)
  269. return BOOL_TRUE;
  270. return BOOL_FALSE;
  271. }
  272. /** @brief Generic static function for adding new list nodes.
  273. *
  274. * @param [in] list List to add node to.
  275. * @param [in] data User data for new list node.
  276. * key (when the cmpFn() returns 0)
  277. * @param [in] find - true/false Function returns list node if there is
  278. * identical entry. Or NULL if find is false.
  279. * @return Newly added list node.
  280. */
  281. static faux_list_node_t *faux_list_add_generic(
  282. faux_list_t *list, void *data, bool_t find)
  283. {
  284. faux_list_node_t *node = NULL;
  285. faux_list_node_t *iter = NULL;
  286. assert(list);
  287. assert(data);
  288. if (!list || !data)
  289. return NULL;
  290. node = faux_list_new_node(data);
  291. if (!node)
  292. return NULL;
  293. // Empty list
  294. if (!list->head) {
  295. list->head = node;
  296. list->tail = node;
  297. list->len++;
  298. return node;
  299. }
  300. // Non-sorted: Insert to tail
  301. if (!list->sorted) {
  302. // Unique: Search through whole list
  303. if (list->unique) {
  304. iter = list->tail;
  305. while (iter) {
  306. int res = list->cmpFn(node->data, iter->data);
  307. if (0 == res) { // Already in list
  308. faux_list_free_node(node);
  309. return (find ? iter : NULL);
  310. }
  311. iter = iter->prev;
  312. }
  313. }
  314. // Add entry to the tail
  315. node->prev = list->tail;
  316. node->next = NULL;
  317. if (list->tail)
  318. list->tail->next = node;
  319. list->tail = node;
  320. list->len++;
  321. return node;
  322. }
  323. // Sorted: Insert from tail
  324. iter = list->tail;
  325. while (iter) {
  326. int res = list->cmpFn(node->data, iter->data);
  327. // Unique: Already exists
  328. if (list->unique && (0 == res)) {
  329. faux_list_free_node(node);
  330. return (find ? iter : NULL);
  331. }
  332. // Non-unique: Entry will be inserted after existent one
  333. if (res >= 0) {
  334. node->next = iter->next;
  335. node->prev = iter;
  336. iter->next = node;
  337. if (node->next)
  338. node->next->prev = node;
  339. break;
  340. }
  341. iter = iter->prev;
  342. }
  343. // Insert node into the list head
  344. if (!iter) {
  345. node->next = list->head;
  346. node->prev = NULL;
  347. list->head->prev = node;
  348. list->head = node;
  349. }
  350. if (!node->next)
  351. list->tail = node;
  352. list->len++;
  353. return node;
  354. }
  355. /** @brief Adds user data to the list.
  356. *
  357. * The user data is not unique. It means that two equal user data instances
  358. * can be added to the list.
  359. *
  360. * @param [in] list List to add entry to.
  361. * @param [in] data User data.
  362. * @return Newly created list node or NULL on error.
  363. */
  364. faux_list_node_t *faux_list_add(faux_list_t *list, void *data)
  365. {
  366. return faux_list_add_generic(list, data, BOOL_FALSE);
  367. }
  368. /** @brief Adds user data (unique) to the list or return equal existent node.
  369. *
  370. * The user data must be unique in this case. Function compares list nodes
  371. * with the new one. If equal node is already in the list then function
  372. * returns this node. Else new unique node will be added to the list.
  373. *
  374. * @param [in] list List to add entry to.
  375. * @param [in] data User data.
  376. * @return Newly created list node, existent equal node or NULL on error.
  377. */
  378. faux_list_node_t *faux_list_add_find(faux_list_t *list, void *data)
  379. {
  380. assert(list);
  381. if (!list)
  382. return NULL;
  383. // Function add_find has no meaning for non-unique list. What is the
  384. // function behaviour? It found entry. Must it return existent entry or
  385. // add new non-unique entry?
  386. if (!list->unique)
  387. return NULL;
  388. return faux_list_add_generic(list, data, BOOL_TRUE);
  389. }
  390. /** Takes away list node from the list.
  391. *
  392. * Function removes list node from the list and returns user data
  393. * stored in this node.
  394. *
  395. * @param [in] list List to take away node from.
  396. * @param [in] node List node to take away.
  397. * @return User data from removed node or NULL on error.
  398. */
  399. void *faux_list_takeaway(faux_list_t *list, faux_list_node_t *node)
  400. {
  401. void *data = NULL;
  402. assert(list);
  403. assert(node);
  404. if (!list || !node)
  405. return NULL;
  406. if (node->prev)
  407. node->prev->next = node->next;
  408. else
  409. list->head = node->next;
  410. if (node->next)
  411. node->next->prev = node->prev;
  412. else
  413. list->tail = node->prev;
  414. list->len--;
  415. data = faux_list_data(node);
  416. faux_list_free_node(node);
  417. return data;
  418. }
  419. /** @brief Deletes list node from the list.
  420. *
  421. * Functions removes node from the list and free user data memory if
  422. * freeFn callback was defined while list creation. If freeFn callback
  423. * is not defined then function is the same as faux_list_takeaway().
  424. *
  425. * @param [in] list List to delete node from.
  426. * @param [in] node List node to delete.
  427. * @return BOOL_TRUE - success, BOOL_FALSE on error.
  428. */
  429. bool_t faux_list_del(faux_list_t *list, faux_list_node_t *node)
  430. {
  431. void *data = NULL;
  432. assert(list);
  433. assert(node);
  434. if (!list || !node)
  435. return BOOL_FALSE;
  436. data = faux_list_takeaway(list, node);
  437. assert(data);
  438. if (!data) // Illegal case
  439. return BOOL_FALSE;
  440. if (list->freeFn)
  441. list->freeFn(data);
  442. return BOOL_TRUE;
  443. }
  444. /** @brief Deletes list node from the list by user key.
  445. *
  446. * @sa faux_list_del()
  447. * @param [in] list List to delete node from.
  448. * @param [in] node User key to find node to delete.
  449. * @return BOOL_TRUE - success, BOOL_FALSE on error.
  450. */
  451. bool_t faux_list_kdel(faux_list_t *list, const void *userkey)
  452. {
  453. faux_list_node_t *node = NULL;
  454. assert(list);
  455. assert(userkey);
  456. if (!list || !userkey)
  457. return BOOL_FALSE;
  458. node = faux_list_kfind_node(list, userkey);
  459. if (!node)
  460. return BOOL_FALSE; // Not found
  461. return faux_list_del(list, node);
  462. }
  463. /** @brief Search list for matching (match function).
  464. *
  465. * Function iterates through the list and executes special matching user defined
  466. * callback function matchFn for every list node. User can provide "userkey" -
  467. * the data that matchFn can use how it wants. The matchFn is arbitrary
  468. * argument. The userkey argument can be NULL. The function will immediately
  469. * return matched list node. To continue searching the saveptr argument contains
  470. * current iterator. So user can call to faux_list_match_node() for several
  471. * times and gets all matched nodes from list.
  472. *
  473. * Prototype for matchFn callback function:
  474. * @code
  475. * int (*faux_list_kcmp_fn)(const void *key, const void *list_item);
  476. * @endcode
  477. *
  478. * @param [in] list List.
  479. * @param [in] matchFn User defined matching callback function.
  480. * @param [in] userkey User defined data to use in matchFn function.
  481. * @param [in,out] saveptr Ptr to save iterator.
  482. * @return Matched list node.
  483. */
  484. faux_list_node_t *faux_list_match_node(const faux_list_t *list,
  485. faux_list_kcmp_fn matchFn, const void *userkey,
  486. faux_list_node_t **saveptr)
  487. {
  488. faux_list_node_t *iter = NULL;
  489. assert(list);
  490. assert(matchFn);
  491. if (!list || !matchFn || !list->head)
  492. return NULL;
  493. if (saveptr)
  494. iter = *saveptr;
  495. if (!iter)
  496. iter = list->head;
  497. while (iter) {
  498. int res = 0;
  499. faux_list_node_t *node = iter;
  500. iter = faux_list_next_node(node);
  501. if (saveptr)
  502. *saveptr = iter;
  503. res = matchFn(userkey, faux_list_data(node));
  504. if (0 == res)
  505. return node;
  506. if (list->sorted && (res < 0)) // No chances to find match
  507. return NULL;
  508. }
  509. return NULL;
  510. }
  511. /** @brief Search list for matching (key cmp function).
  512. *
  513. * Same as faux_list_match_node() but uses userkey compare function defined
  514. * while faux_list_new() function call.
  515. *
  516. * @sa faux_list_match_node()
  517. */
  518. faux_list_node_t *faux_list_kmatch_node(const faux_list_t *list,
  519. const void *userkey, faux_list_node_t **saveptr)
  520. {
  521. assert(list);
  522. if (!list)
  523. return NULL;
  524. return faux_list_match_node(list, list->kcmpFn, userkey, saveptr);
  525. }
  526. /** @brief Search list for matching (match function) and returns user data.
  527. *
  528. * Same as faux_list_match_node() but returns user data structure.
  529. *
  530. * @sa faux_list_match_node()
  531. */
  532. void *faux_list_match(const faux_list_t *list, faux_list_kcmp_fn matchFn,
  533. const void *userkey, faux_list_node_t **saveptr)
  534. {
  535. faux_list_node_t *res =
  536. faux_list_match_node(list, matchFn, userkey, saveptr);
  537. if (!res)
  538. return NULL;
  539. return faux_list_data(res);
  540. }
  541. /** @brief Search list for matching (key cmp function) and returns user data.
  542. *
  543. * Same as faux_list_match() but uses userkey compare function defined
  544. * while faux_list_new() function call.
  545. *
  546. * @sa faux_list_match_node()
  547. */
  548. void *faux_list_kmatch(const faux_list_t *list, const void *userkey,
  549. faux_list_node_t **saveptr)
  550. {
  551. assert(list);
  552. if (!list)
  553. return NULL;
  554. return faux_list_match(list, list->kcmpFn, userkey, saveptr);
  555. }
  556. /** @brief Search list for first matching (match function).
  557. *
  558. * Same as faux_list_match_node() but search for the first matching.
  559. * Doesn't use saveptr iterator.
  560. *
  561. * @sa faux_list_match_node()
  562. */
  563. faux_list_node_t *faux_list_find_node(const faux_list_t *list,
  564. faux_list_kcmp_fn matchFn, const void *userkey)
  565. {
  566. return faux_list_match_node(list, matchFn, userkey, NULL);
  567. }
  568. /** @brief Search list for first matching (key cmp function).
  569. *
  570. * Same as faux_list_find_node() but uses userkey compare function defined
  571. * while faux_list_new() function call.
  572. *
  573. * @sa faux_list_match_node()
  574. */
  575. faux_list_node_t *faux_list_kfind_node(const faux_list_t *list,
  576. const void *userkey)
  577. {
  578. return faux_list_find_node(list, list->kcmpFn, userkey);
  579. }
  580. /** @brief Search list for first matching (match function) and returns user data.
  581. *
  582. * Same as faux_list_match_node() but returns user data structure and search
  583. * only for the first matching. Doesn't use saveptr iterator.
  584. *
  585. * @sa faux_list_match_node()
  586. */
  587. void *faux_list_find(const faux_list_t *list, faux_list_kcmp_fn matchFn,
  588. const void *userkey)
  589. {
  590. return faux_list_match(list, matchFn, userkey, NULL);
  591. }
  592. /** @brief Search list for first matching (key cmp function). Returns user data.
  593. *
  594. * Same as faux_list_find() but uses userkey compare function defined
  595. * while faux_list_new() function call.
  596. *
  597. * @sa faux_list_match_node()
  598. */
  599. void *faux_list_kfind(const faux_list_t *list,
  600. const void *userkey)
  601. {
  602. return faux_list_find(list, list->kcmpFn, userkey);
  603. }