list.c 18 KB

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