list.c 16 KB

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