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