1
0

list.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 = malloc(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. if (node)
  42. 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. assert(node);
  51. if (!node)
  52. return NULL;
  53. return node->prev;
  54. }
  55. /** @brief Gets next list node.
  56. *
  57. * @param [in] this List node instance.
  58. * @return List node next in list.
  59. */
  60. faux_list_node_t *faux_list_next_node(const faux_list_node_t *node) {
  61. assert(node);
  62. if (!node)
  63. return NULL;
  64. return node->next;
  65. }
  66. /** @brief Gets user data from list node.
  67. *
  68. * @param [in] this List node instance.
  69. * @return User data stored within specified list node.
  70. */
  71. void *faux_list_data(const faux_list_node_t *node) {
  72. assert(node);
  73. if (!node)
  74. return NULL;
  75. return node->data;
  76. }
  77. /** @brief Iterate through each list node.
  78. *
  79. * On each call to this function the iterator will change its value.
  80. * Before function using the iterator must be initialised by list head node.
  81. *
  82. * @param [in,out] iter List node ptr used as an iterator.
  83. * @return List node or NULL if list elements are over.
  84. */
  85. faux_list_node_t *faux_list_each_node(faux_list_node_t **iter) {
  86. faux_list_node_t *current_node = *iter;
  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 and returns user data.
  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 head node.
  96. *
  97. * @param [in,out] iter List node ptr used as an iterator.
  98. * @return User data or NULL if list elements are over.
  99. */
  100. void *faux_list_each(faux_list_node_t **iter) {
  101. faux_list_node_t *current_node = NULL;
  102. if (!*iter)
  103. return NULL;
  104. current_node = faux_list_each_node(iter);
  105. return faux_list_data(current_node);
  106. }
  107. /** @brief Allocate and initialize bidirectional list.
  108. *
  109. * Prototypes for callback functions:
  110. * @code
  111. * int faux_list_compare_fn(const void *first, const void *second);
  112. * void faux_list_free_fn(void *data);
  113. * @endcode
  114. *
  115. * @param [in] compareFn Callback function to compare two user data instances
  116. * to sort list.
  117. * @param [in] freeFn Callback function to free user data.
  118. * @return Newly created bidirectional list or NULL on error.
  119. */
  120. faux_list_t *faux_list_new(faux_list_compare_fn compareFn,
  121. faux_list_free_fn freeFn) {
  122. faux_list_t *list;
  123. list = malloc(sizeof(*list));
  124. assert(list);
  125. if (!list)
  126. return NULL;
  127. // Initialize
  128. list->head = NULL;
  129. list->tail = NULL;
  130. list->compareFn = compareFn;
  131. list->freeFn = freeFn;
  132. list->len = 0;
  133. return list;
  134. }
  135. /** @brief Free bidirectional list
  136. *
  137. * Free all nodes and user data from list and finally
  138. * free the list itself. It uses special callback
  139. * function specified by user (while faux_list_new()) to free the abstract
  140. * user data.
  141. *
  142. * @param [in] list List to free.
  143. */
  144. void faux_list_free(faux_list_t *list) {
  145. faux_list_node_t *iter = NULL;
  146. assert(list);
  147. if (!list)
  148. return;
  149. while ((iter = faux_list_head(list))) {
  150. faux_list_del(list, iter);
  151. }
  152. free(list);
  153. }
  154. /** @brief Gets head of list.
  155. *
  156. * @param [in] list List.
  157. * @return List node first in list.
  158. */
  159. faux_list_node_t *faux_list_head(const faux_list_t *list) {
  160. assert(list);
  161. if (!list)
  162. return NULL;
  163. return list->head;
  164. }
  165. /** @brief Gets tail of list.
  166. *
  167. * @param [in] list List.
  168. * @return List node last in list.
  169. */
  170. faux_list_node_t *faux_list_tail(const faux_list_t *list) {
  171. assert(list);
  172. if (!list)
  173. return NULL;
  174. return list->tail;
  175. }
  176. /** @brief Gets current length of list.
  177. *
  178. * @param [in] list List.
  179. * @return Current length of list.
  180. */
  181. size_t faux_list_len(const faux_list_t *list) {
  182. assert(list);
  183. if (!list)
  184. return 0;
  185. return list->len;
  186. }
  187. /** @brief Generic static function for adding new list nodes.
  188. *
  189. * @param [in] list List to add node to.
  190. * @param [in] data User data for new list node.
  191. * @param [in] uniq - true/false Don't add entry with identical order
  192. * key (when the compareFn() returns 0)
  193. * @param [in] find - true/false Function returns list node if there is
  194. * identical entry. Or NULL if find is false.
  195. * @return Newly added list node.
  196. */
  197. static faux_list_node_t *faux_list_add_generic(faux_list_t *list, void *data,
  198. bool_t uniq, bool_t find) {
  199. faux_list_node_t *node = NULL;
  200. faux_list_node_t *iter = NULL;
  201. assert(list);
  202. assert(data);
  203. if (!list || !data)
  204. return NULL;
  205. node = faux_list_new_node(data);
  206. if (!node)
  207. return NULL;
  208. // Empty list.
  209. if (!list->head) {
  210. list->head = node;
  211. list->tail = node;
  212. list->len++;
  213. return node;
  214. }
  215. // Not sorted list. Add to the tail.
  216. if (!list->compareFn) {
  217. node->prev = list->tail;
  218. node->next = NULL;
  219. list->tail->next = node;
  220. list->tail = node;
  221. list->len++;
  222. return node;
  223. }
  224. // Sorted list.
  225. iter = list->tail;
  226. while (iter) {
  227. int res = list->compareFn(node->data, iter->data);
  228. if (uniq && (res == 0)) {
  229. faux_list_free_node(node);
  230. return (find ? iter : NULL);
  231. }
  232. if (res >= 0) {
  233. node->next = iter->next;
  234. node->prev = iter;
  235. iter->next = node;
  236. if (node->next)
  237. node->next->prev = node;
  238. break;
  239. }
  240. iter = iter->prev;
  241. }
  242. // Insert node into the list head
  243. if (!iter) {
  244. node->next = list->head;
  245. node->prev = NULL;
  246. list->head->prev = node;
  247. list->head = node;
  248. }
  249. if (!node->next)
  250. list->tail = node;
  251. list->len++;
  252. return node;
  253. }
  254. /** @brief Adds user data (not unique) to the list.
  255. *
  256. * The user data is not unique. It means that two equal user data instances
  257. * can be added to the list.
  258. *
  259. * @param [in] list List to add entry to.
  260. * @param [in] data User data.
  261. * @return Newly created list node or NULL on error.
  262. */
  263. faux_list_node_t *faux_list_add(faux_list_t *list, void *data) {
  264. return faux_list_add_generic(list, data, BOOL_FALSE, BOOL_FALSE);
  265. }
  266. /** @brief Adds user data (unique) to the list.
  267. *
  268. * The user data must be unique. It means that two equal user data instances
  269. * can not be added to the list. Function will return NULL if equal user
  270. * data is already in the list.
  271. *
  272. * @param [in] list List to add entry to.
  273. * @param [in] data User data.
  274. * @return Newly created list node or NULL on error.
  275. */
  276. faux_list_node_t *faux_list_add_uniq(faux_list_t *list, void *data) {
  277. return faux_list_add_generic(list, data, BOOL_TRUE, BOOL_FALSE);
  278. }
  279. /** @brief Adds user data (unique) to the list or return equal existent node.
  280. *
  281. * The user data must be unique in this case. Function compares list nodes
  282. * with the new one. If equal node is already in the list then function
  283. * returns this node. Else new unique node will be added to the list.
  284. *
  285. * @param [in] list List to add entry to.
  286. * @param [in] data User data.
  287. * @return Newly created list node, existent equal node or NULL on error.
  288. */
  289. faux_list_node_t *faux_list_add_find(faux_list_t *list, void *data) {
  290. return faux_list_add_generic(list, data, BOOL_TRUE, BOOL_TRUE);
  291. }
  292. /** Takes away list node from the list.
  293. *
  294. * Function removes list node from the list and returns user data
  295. * stored in this node.
  296. *
  297. * @param [in] list List to take away node from.
  298. * @param [in] node List node to take away.
  299. * @return User data from removed node or NULL on error.
  300. */
  301. void *faux_list_takeaway(faux_list_t *list, faux_list_node_t *node) {
  302. void *data = NULL;
  303. assert(list);
  304. assert(node);
  305. if (!list || !node)
  306. return NULL;
  307. if (node->prev)
  308. node->prev->next = node->next;
  309. else
  310. list->head = node->next;
  311. if (node->next)
  312. node->next->prev = node->prev;
  313. else
  314. list->tail = node->prev;
  315. list->len--;
  316. data = faux_list_data(node);
  317. faux_list_free_node(node);
  318. return data;
  319. }
  320. /** @brief Deletes list node from the list.
  321. *
  322. * Functions removes node from the list and free user data memory if
  323. * freeFn callback was defined while list creation. If freeFn callback
  324. * is not defined then function is the same as faux_list_takeaway().
  325. *
  326. * @param [in] list List to delete node from.
  327. * @param [in] node List node to delete.
  328. * @return 0 on success, < 0 on error.
  329. */
  330. int faux_list_del(faux_list_t *list, faux_list_node_t *node) {
  331. void *data = NULL;
  332. assert(list);
  333. assert(node);
  334. if (!list || !node)
  335. return -1;
  336. data = faux_list_takeaway(list, node);
  337. assert(data);
  338. if (!data) // Illegal case
  339. return -1;
  340. if (list->freeFn)
  341. list->freeFn(data);
  342. return 0;
  343. }
  344. /** @brief Search list for matching (match function).
  345. *
  346. * Function iterates through the list and executes special matching user defined
  347. * callback function matchFn for every list node. User can provide "userkey" -
  348. * the data that matchFn can use how it wants. The matchFn is arbitrary
  349. * argument. The userkey argument can be NULL. The function will immediately
  350. * return matched list node. To continue searching the saveptr argument contains
  351. * current iterator. So user can call to faux_list_match_node() for several
  352. * times and gets all matched nodes from list.
  353. *
  354. * Prototype for matchFn callback function:
  355. * @code
  356. * int faux_list_match_fn(const void *key, const void *data);
  357. * @endcode
  358. *
  359. * @param [in] list List.
  360. * @param [in] matchFn User defined matching callback function.
  361. * @param [in] userkey User defined data to use in matchFn function.
  362. * @param [in,out] saveptr Ptr to save iterator.
  363. * @return Matched list node.
  364. */
  365. faux_list_node_t *faux_list_match_node(const faux_list_t *list,
  366. faux_list_match_fn matchFn, const void *userkey,
  367. faux_list_node_t **saveptr) {
  368. faux_list_node_t *iter = NULL;
  369. assert(list);
  370. assert(matchFn);
  371. if (!list || !matchFn || !list->head)
  372. return NULL;
  373. if (saveptr)
  374. iter = *saveptr;
  375. if (!iter)
  376. iter = list->head;
  377. while (iter) {
  378. int res;
  379. faux_list_node_t *node = iter;
  380. iter = faux_list_next_node(iter);
  381. if (saveptr)
  382. *saveptr = iter;
  383. res = matchFn(userkey, faux_list_data(node));
  384. if (!res)
  385. return node;
  386. if (res < 0) // No chances to find match
  387. return NULL;
  388. }
  389. return NULL;
  390. }
  391. /** @brief Search list for matching (match function) and returns user data.
  392. *
  393. * Same as faux_list_match_node() but returns user data structure.
  394. *
  395. * @sa faux_list_match_node()
  396. */
  397. void *faux_list_match(const faux_list_t *list, faux_list_match_fn matchFn,
  398. const void *userkey, faux_list_node_t **saveptr) {
  399. faux_list_node_t *res =
  400. faux_list_match_node(list, matchFn, userkey, saveptr);
  401. if (!res)
  402. return NULL;
  403. return faux_list_data(res);
  404. }
  405. /** @brief Search list for first matching (match function).
  406. *
  407. * Same as faux_list_match_node() but search for the fisrt matching.
  408. * Doesn't use saveptr iterator.
  409. *
  410. * @sa faux_list_match_node()
  411. */
  412. faux_list_node_t *faux_list_find_node(const faux_list_t *list,
  413. faux_list_match_fn matchFn, const void *userkey) {
  414. return faux_list_match_node(list, matchFn, userkey, NULL);
  415. }
  416. /** @brief Search list for first matching (match function) and returns user data.
  417. *
  418. * Same as faux_list_match_node() but returns user data structure and search
  419. * only for the first matching. Doesn't use saveptr iterator.
  420. *
  421. * @sa faux_list_match_node()
  422. */
  423. void *faux_list_find(const faux_list_t *list, faux_list_match_fn matchFn,
  424. const void *userkey) {
  425. return faux_list_match(list, matchFn, userkey, NULL);
  426. }