list.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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. list->tail->next = node;
  276. list->tail = node;
  277. list->len++;
  278. return node;
  279. }
  280. // Sorted: Insert from tail
  281. iter = list->tail;
  282. while (iter) {
  283. int res = list->cmpFn(node->data, iter->data);
  284. // Unique: Already exists
  285. if (list->unique && (0 == res)) {
  286. faux_list_free_node(node);
  287. return (find ? iter : NULL);
  288. }
  289. // Non-unique: Entry will be inserted after existent one
  290. if (res >= 0) {
  291. node->next = iter->next;
  292. node->prev = iter;
  293. iter->next = node;
  294. if (node->next)
  295. node->next->prev = node;
  296. break;
  297. }
  298. iter = iter->prev;
  299. }
  300. // Insert node into the list head
  301. if (!iter) {
  302. node->next = list->head;
  303. node->prev = NULL;
  304. list->head->prev = node;
  305. list->head = node;
  306. }
  307. if (!node->next)
  308. list->tail = node;
  309. list->len++;
  310. return node;
  311. }
  312. /** @brief Adds user data to the list.
  313. *
  314. * The user data is not unique. It means that two equal user data instances
  315. * can be added to the list.
  316. *
  317. * @param [in] list List to add entry to.
  318. * @param [in] data User data.
  319. * @return Newly created list node or NULL on error.
  320. */
  321. faux_list_node_t *faux_list_add(faux_list_t *list, void *data) {
  322. return faux_list_add_generic(list, data, BOOL_FALSE);
  323. }
  324. /** @brief Adds user data (unique) to the list or return equal existent node.
  325. *
  326. * The user data must be unique in this case. Function compares list nodes
  327. * with the new one. If equal node is already in the list then function
  328. * returns this node. Else new unique node will be added to the list.
  329. *
  330. * @param [in] list List to add entry to.
  331. * @param [in] data User data.
  332. * @return Newly created list node, existent equal node or NULL on error.
  333. */
  334. faux_list_node_t *faux_list_add_find(faux_list_t *list, void *data) {
  335. assert(list);
  336. if (!list)
  337. return NULL;
  338. // Function add_find has no meaning for non-unique list. What is the
  339. // function behaviour? It found entry. Must it return existent entry or
  340. // add new non-unique entry?
  341. if (!list->unique)
  342. return NULL;
  343. return faux_list_add_generic(list, data, BOOL_TRUE);
  344. }
  345. /** Takes away list node from the list.
  346. *
  347. * Function removes list node from the list and returns user data
  348. * stored in this node.
  349. *
  350. * @param [in] list List to take away node from.
  351. * @param [in] node List node to take away.
  352. * @return User data from removed node or NULL on error.
  353. */
  354. void *faux_list_takeaway(faux_list_t *list, faux_list_node_t *node) {
  355. void *data = NULL;
  356. assert(list);
  357. assert(node);
  358. if (!list || !node)
  359. return NULL;
  360. if (node->prev)
  361. node->prev->next = node->next;
  362. else
  363. list->head = node->next;
  364. if (node->next)
  365. node->next->prev = node->prev;
  366. else
  367. list->tail = node->prev;
  368. list->len--;
  369. data = faux_list_data(node);
  370. faux_list_free_node(node);
  371. return data;
  372. }
  373. /** @brief Deletes list node from the list.
  374. *
  375. * Functions removes node from the list and free user data memory if
  376. * freeFn callback was defined while list creation. If freeFn callback
  377. * is not defined then function is the same as faux_list_takeaway().
  378. *
  379. * @param [in] list List to delete node from.
  380. * @param [in] node List node to delete.
  381. * @return 0 on success, < 0 on error.
  382. */
  383. int faux_list_del(faux_list_t *list, faux_list_node_t *node) {
  384. void *data = NULL;
  385. assert(list);
  386. assert(node);
  387. if (!list || !node)
  388. return -1;
  389. data = faux_list_takeaway(list, node);
  390. assert(data);
  391. if (!data) // Illegal case
  392. return -1;
  393. if (list->freeFn)
  394. list->freeFn(data);
  395. return 0;
  396. }
  397. /** @brief Search list for matching (match function).
  398. *
  399. * Function iterates through the list and executes special matching user defined
  400. * callback function matchFn for every list node. User can provide "userkey" -
  401. * the data that matchFn can use how it wants. The matchFn is arbitrary
  402. * argument. The userkey argument can be NULL. The function will immediately
  403. * return matched list node. To continue searching the saveptr argument contains
  404. * current iterator. So user can call to faux_list_match_node() for several
  405. * times and gets all matched nodes from list.
  406. *
  407. * Prototype for matchFn callback function:
  408. * @code
  409. * int faux_list_kcmp_fn(const void *key, const void *list_item);
  410. * @endcode
  411. *
  412. * @param [in] list List.
  413. * @param [in] matchFn User defined matching callback function.
  414. * @param [in] userkey User defined data to use in matchFn function.
  415. * @param [in,out] saveptr Ptr to save iterator.
  416. * @return Matched list node.
  417. */
  418. faux_list_node_t *faux_list_match_node(const faux_list_t *list,
  419. faux_list_kcmp_fn matchFn, const void *userkey,
  420. faux_list_node_t **saveptr) {
  421. faux_list_node_t *iter = NULL;
  422. assert(list);
  423. assert(matchFn);
  424. if (!list || !matchFn || !list->head)
  425. return NULL;
  426. if (saveptr)
  427. iter = *saveptr;
  428. if (!iter)
  429. iter = list->head;
  430. while (iter) {
  431. int res = 0;
  432. faux_list_node_t *node = iter;
  433. iter = faux_list_next_node(iter);
  434. if (saveptr)
  435. *saveptr = iter;
  436. res = matchFn(userkey, faux_list_data(node));
  437. if (0 == res)
  438. return node;
  439. if (res < 0) // No chances to find match
  440. return NULL;
  441. }
  442. return NULL;
  443. }
  444. /** @brief Search list for matching (match function) and returns user data.
  445. *
  446. * Same as faux_list_match_node() but returns user data structure.
  447. *
  448. * @sa faux_list_match_node()
  449. */
  450. void *faux_list_match(const faux_list_t *list, faux_list_kcmp_fn matchFn,
  451. const void *userkey, faux_list_node_t **saveptr) {
  452. faux_list_node_t *res =
  453. faux_list_match_node(list, matchFn, userkey, saveptr);
  454. if (!res)
  455. return NULL;
  456. return faux_list_data(res);
  457. }
  458. /** @brief Search list for first matching (match function).
  459. *
  460. * Same as faux_list_match_node() but search for the fisrt matching.
  461. * Doesn't use saveptr iterator.
  462. *
  463. * @sa faux_list_match_node()
  464. */
  465. faux_list_node_t *faux_list_find_node(const faux_list_t *list,
  466. faux_list_kcmp_fn matchFn, const void *userkey) {
  467. return faux_list_match_node(list, matchFn, userkey, NULL);
  468. }
  469. /** @brief Search list for first matching (match function) and returns user data.
  470. *
  471. * Same as faux_list_match_node() but returns user data structure and search
  472. * only for the first matching. Doesn't use saveptr iterator.
  473. *
  474. * @sa faux_list_match_node()
  475. */
  476. void *faux_list_find(const faux_list_t *list, faux_list_kcmp_fn matchFn,
  477. const void *userkey) {
  478. return faux_list_match(list, matchFn, userkey, NULL);
  479. }