list.c 16 KB

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