gl_array_list.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /* Sequential list data type implemented by an array.
  2. Copyright (C) 2006-2020 Free Software Foundation, Inc.
  3. Written by Bruno Haible <bruno@clisp.org>, 2006.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include "gl_array_list.h"
  17. #include <stdint.h>
  18. #include <stdlib.h>
  19. /* Get memcpy. */
  20. #include <string.h>
  21. /* Checked size_t computations. */
  22. #include "xsize.h"
  23. /* -------------------------- gl_list_t Data Type -------------------------- */
  24. /* Concrete gl_list_impl type, valid for this file only. */
  25. struct gl_list_impl
  26. {
  27. struct gl_list_impl_base base;
  28. /* An array of ALLOCATED elements, of which the first COUNT are used.
  29. 0 <= COUNT <= ALLOCATED. */
  30. const void **elements;
  31. size_t count;
  32. size_t allocated;
  33. };
  34. /* struct gl_list_node_impl doesn't exist here. The pointers are actually
  35. indices + 1. */
  36. #define INDEX_TO_NODE(index) (gl_list_node_t)(uintptr_t)(size_t)((index) + 1)
  37. #define NODE_TO_INDEX(node) ((uintptr_t)(node) - 1)
  38. static gl_list_t
  39. gl_array_nx_create_empty (gl_list_implementation_t implementation,
  40. gl_listelement_equals_fn equals_fn,
  41. gl_listelement_hashcode_fn hashcode_fn,
  42. gl_listelement_dispose_fn dispose_fn,
  43. bool allow_duplicates)
  44. {
  45. struct gl_list_impl *list =
  46. (struct gl_list_impl *) malloc (sizeof (struct gl_list_impl));
  47. if (list == NULL)
  48. return NULL;
  49. list->base.vtable = implementation;
  50. list->base.equals_fn = equals_fn;
  51. list->base.hashcode_fn = hashcode_fn;
  52. list->base.dispose_fn = dispose_fn;
  53. list->base.allow_duplicates = allow_duplicates;
  54. list->elements = NULL;
  55. list->count = 0;
  56. list->allocated = 0;
  57. return list;
  58. }
  59. static gl_list_t
  60. gl_array_nx_create (gl_list_implementation_t implementation,
  61. gl_listelement_equals_fn equals_fn,
  62. gl_listelement_hashcode_fn hashcode_fn,
  63. gl_listelement_dispose_fn dispose_fn,
  64. bool allow_duplicates,
  65. size_t count, const void **contents)
  66. {
  67. struct gl_list_impl *list =
  68. (struct gl_list_impl *) malloc (sizeof (struct gl_list_impl));
  69. if (list == NULL)
  70. return NULL;
  71. list->base.vtable = implementation;
  72. list->base.equals_fn = equals_fn;
  73. list->base.hashcode_fn = hashcode_fn;
  74. list->base.dispose_fn = dispose_fn;
  75. list->base.allow_duplicates = allow_duplicates;
  76. if (count > 0)
  77. {
  78. if (size_overflow_p (xtimes (count, sizeof (const void *))))
  79. goto fail;
  80. list->elements = (const void **) malloc (count * sizeof (const void *));
  81. if (list->elements == NULL)
  82. goto fail;
  83. memcpy (list->elements, contents, count * sizeof (const void *));
  84. }
  85. else
  86. list->elements = NULL;
  87. list->count = count;
  88. list->allocated = count;
  89. return list;
  90. fail:
  91. free (list);
  92. return NULL;
  93. }
  94. static size_t _GL_ATTRIBUTE_PURE
  95. gl_array_size (gl_list_t list)
  96. {
  97. return list->count;
  98. }
  99. static const void * _GL_ATTRIBUTE_PURE
  100. gl_array_node_value (gl_list_t list, gl_list_node_t node)
  101. {
  102. uintptr_t index = NODE_TO_INDEX (node);
  103. if (!(index < list->count))
  104. /* Invalid argument. */
  105. abort ();
  106. return list->elements[index];
  107. }
  108. static int
  109. gl_array_node_nx_set_value (gl_list_t list, gl_list_node_t node,
  110. const void *elt)
  111. {
  112. uintptr_t index = NODE_TO_INDEX (node);
  113. if (!(index < list->count))
  114. /* Invalid argument. */
  115. abort ();
  116. list->elements[index] = elt;
  117. return 0;
  118. }
  119. static gl_list_node_t _GL_ATTRIBUTE_PURE
  120. gl_array_next_node (gl_list_t list, gl_list_node_t node)
  121. {
  122. uintptr_t index = NODE_TO_INDEX (node);
  123. if (!(index < list->count))
  124. /* Invalid argument. */
  125. abort ();
  126. index++;
  127. if (index < list->count)
  128. return INDEX_TO_NODE (index);
  129. else
  130. return NULL;
  131. }
  132. static gl_list_node_t _GL_ATTRIBUTE_PURE
  133. gl_array_previous_node (gl_list_t list, gl_list_node_t node)
  134. {
  135. uintptr_t index = NODE_TO_INDEX (node);
  136. if (!(index < list->count))
  137. /* Invalid argument. */
  138. abort ();
  139. if (index > 0)
  140. return INDEX_TO_NODE (index - 1);
  141. else
  142. return NULL;
  143. }
  144. static const void * _GL_ATTRIBUTE_PURE
  145. gl_array_get_at (gl_list_t list, size_t position)
  146. {
  147. size_t count = list->count;
  148. if (!(position < count))
  149. /* Invalid argument. */
  150. abort ();
  151. return list->elements[position];
  152. }
  153. static gl_list_node_t
  154. gl_array_nx_set_at (gl_list_t list, size_t position, const void *elt)
  155. {
  156. size_t count = list->count;
  157. if (!(position < count))
  158. /* Invalid argument. */
  159. abort ();
  160. list->elements[position] = elt;
  161. return INDEX_TO_NODE (position);
  162. }
  163. static size_t _GL_ATTRIBUTE_PURE
  164. gl_array_indexof_from_to (gl_list_t list, size_t start_index, size_t end_index,
  165. const void *elt)
  166. {
  167. size_t count = list->count;
  168. if (!(start_index <= end_index && end_index <= count))
  169. /* Invalid arguments. */
  170. abort ();
  171. if (start_index < end_index)
  172. {
  173. gl_listelement_equals_fn equals = list->base.equals_fn;
  174. if (equals != NULL)
  175. {
  176. size_t i;
  177. for (i = start_index;;)
  178. {
  179. if (equals (elt, list->elements[i]))
  180. return i;
  181. i++;
  182. if (i == end_index)
  183. break;
  184. }
  185. }
  186. else
  187. {
  188. size_t i;
  189. for (i = start_index;;)
  190. {
  191. if (elt == list->elements[i])
  192. return i;
  193. i++;
  194. if (i == end_index)
  195. break;
  196. }
  197. }
  198. }
  199. return (size_t)(-1);
  200. }
  201. static gl_list_node_t _GL_ATTRIBUTE_PURE
  202. gl_array_search_from_to (gl_list_t list, size_t start_index, size_t end_index,
  203. const void *elt)
  204. {
  205. size_t index = gl_array_indexof_from_to (list, start_index, end_index, elt);
  206. return INDEX_TO_NODE (index);
  207. }
  208. /* Ensure that list->allocated > list->count.
  209. Return 0 upon success, -1 upon out-of-memory. */
  210. static int
  211. grow (gl_list_t list)
  212. {
  213. size_t new_allocated;
  214. size_t memory_size;
  215. const void **memory;
  216. new_allocated = xtimes (list->allocated, 2);
  217. new_allocated = xsum (new_allocated, 1);
  218. memory_size = xtimes (new_allocated, sizeof (const void *));
  219. if (size_overflow_p (memory_size))
  220. /* Overflow, would lead to out of memory. */
  221. return -1;
  222. memory = (const void **) realloc (list->elements, memory_size);
  223. if (memory == NULL)
  224. /* Out of memory. */
  225. return -1;
  226. list->elements = memory;
  227. list->allocated = new_allocated;
  228. return 0;
  229. }
  230. static gl_list_node_t
  231. gl_array_nx_add_first (gl_list_t list, const void *elt)
  232. {
  233. size_t count = list->count;
  234. const void **elements;
  235. size_t i;
  236. if (count == list->allocated)
  237. if (grow (list) < 0)
  238. return NULL;
  239. elements = list->elements;
  240. for (i = count; i > 0; i--)
  241. elements[i] = elements[i - 1];
  242. elements[0] = elt;
  243. list->count = count + 1;
  244. return INDEX_TO_NODE (0);
  245. }
  246. static gl_list_node_t
  247. gl_array_nx_add_last (gl_list_t list, const void *elt)
  248. {
  249. size_t count = list->count;
  250. if (count == list->allocated)
  251. if (grow (list) < 0)
  252. return NULL;
  253. list->elements[count] = elt;
  254. list->count = count + 1;
  255. return INDEX_TO_NODE (count);
  256. }
  257. static gl_list_node_t
  258. gl_array_nx_add_before (gl_list_t list, gl_list_node_t node, const void *elt)
  259. {
  260. size_t count = list->count;
  261. uintptr_t index = NODE_TO_INDEX (node);
  262. size_t position;
  263. const void **elements;
  264. size_t i;
  265. if (!(index < count))
  266. /* Invalid argument. */
  267. abort ();
  268. position = index;
  269. if (count == list->allocated)
  270. if (grow (list) < 0)
  271. return NULL;
  272. elements = list->elements;
  273. for (i = count; i > position; i--)
  274. elements[i] = elements[i - 1];
  275. elements[position] = elt;
  276. list->count = count + 1;
  277. return INDEX_TO_NODE (position);
  278. }
  279. static gl_list_node_t
  280. gl_array_nx_add_after (gl_list_t list, gl_list_node_t node, const void *elt)
  281. {
  282. size_t count = list->count;
  283. uintptr_t index = NODE_TO_INDEX (node);
  284. size_t position;
  285. const void **elements;
  286. size_t i;
  287. if (!(index < count))
  288. /* Invalid argument. */
  289. abort ();
  290. position = index + 1;
  291. if (count == list->allocated)
  292. if (grow (list) < 0)
  293. return NULL;
  294. elements = list->elements;
  295. for (i = count; i > position; i--)
  296. elements[i] = elements[i - 1];
  297. elements[position] = elt;
  298. list->count = count + 1;
  299. return INDEX_TO_NODE (position);
  300. }
  301. static gl_list_node_t
  302. gl_array_nx_add_at (gl_list_t list, size_t position, const void *elt)
  303. {
  304. size_t count = list->count;
  305. const void **elements;
  306. size_t i;
  307. if (!(position <= count))
  308. /* Invalid argument. */
  309. abort ();
  310. if (count == list->allocated)
  311. if (grow (list) < 0)
  312. return NULL;
  313. elements = list->elements;
  314. for (i = count; i > position; i--)
  315. elements[i] = elements[i - 1];
  316. elements[position] = elt;
  317. list->count = count + 1;
  318. return INDEX_TO_NODE (position);
  319. }
  320. static bool
  321. gl_array_remove_node (gl_list_t list, gl_list_node_t node)
  322. {
  323. size_t count = list->count;
  324. uintptr_t index = NODE_TO_INDEX (node);
  325. size_t position;
  326. const void **elements;
  327. size_t i;
  328. if (!(index < count))
  329. /* Invalid argument. */
  330. abort ();
  331. position = index;
  332. elements = list->elements;
  333. if (list->base.dispose_fn != NULL)
  334. list->base.dispose_fn (elements[position]);
  335. for (i = position + 1; i < count; i++)
  336. elements[i - 1] = elements[i];
  337. list->count = count - 1;
  338. return true;
  339. }
  340. static bool
  341. gl_array_remove_at (gl_list_t list, size_t position)
  342. {
  343. size_t count = list->count;
  344. const void **elements;
  345. size_t i;
  346. if (!(position < count))
  347. /* Invalid argument. */
  348. abort ();
  349. elements = list->elements;
  350. if (list->base.dispose_fn != NULL)
  351. list->base.dispose_fn (elements[position]);
  352. for (i = position + 1; i < count; i++)
  353. elements[i - 1] = elements[i];
  354. list->count = count - 1;
  355. return true;
  356. }
  357. static bool
  358. gl_array_remove (gl_list_t list, const void *elt)
  359. {
  360. size_t position = gl_array_indexof_from_to (list, 0, list->count, elt);
  361. if (position == (size_t)(-1))
  362. return false;
  363. else
  364. return gl_array_remove_at (list, position);
  365. }
  366. static void
  367. gl_array_list_free (gl_list_t list)
  368. {
  369. if (list->elements != NULL)
  370. {
  371. if (list->base.dispose_fn != NULL)
  372. {
  373. size_t count = list->count;
  374. if (count > 0)
  375. {
  376. gl_listelement_dispose_fn dispose = list->base.dispose_fn;
  377. const void **elements = list->elements;
  378. do
  379. dispose (*elements++);
  380. while (--count > 0);
  381. }
  382. }
  383. free (list->elements);
  384. }
  385. free (list);
  386. }
  387. /* --------------------- gl_list_iterator_t Data Type --------------------- */
  388. static gl_list_iterator_t _GL_ATTRIBUTE_PURE
  389. gl_array_iterator (gl_list_t list)
  390. {
  391. gl_list_iterator_t result;
  392. result.vtable = list->base.vtable;
  393. result.list = list;
  394. result.count = list->count;
  395. result.p = list->elements + 0;
  396. result.q = list->elements + list->count;
  397. #if defined GCC_LINT || defined lint
  398. result.i = 0;
  399. result.j = 0;
  400. #endif
  401. return result;
  402. }
  403. static gl_list_iterator_t _GL_ATTRIBUTE_PURE
  404. gl_array_iterator_from_to (gl_list_t list, size_t start_index, size_t end_index)
  405. {
  406. gl_list_iterator_t result;
  407. if (!(start_index <= end_index && end_index <= list->count))
  408. /* Invalid arguments. */
  409. abort ();
  410. result.vtable = list->base.vtable;
  411. result.list = list;
  412. result.count = list->count;
  413. result.p = list->elements + start_index;
  414. result.q = list->elements + end_index;
  415. #if defined GCC_LINT || defined lint
  416. result.i = 0;
  417. result.j = 0;
  418. #endif
  419. return result;
  420. }
  421. static bool
  422. gl_array_iterator_next (gl_list_iterator_t *iterator,
  423. const void **eltp, gl_list_node_t *nodep)
  424. {
  425. gl_list_t list = iterator->list;
  426. if (iterator->count != list->count)
  427. {
  428. if (iterator->count != list->count + 1)
  429. /* Concurrent modifications were done on the list. */
  430. abort ();
  431. /* The last returned element was removed. */
  432. iterator->count--;
  433. iterator->p = (const void **) iterator->p - 1;
  434. iterator->q = (const void **) iterator->q - 1;
  435. }
  436. if (iterator->p < iterator->q)
  437. {
  438. const void **p = (const void **) iterator->p;
  439. *eltp = *p;
  440. if (nodep != NULL)
  441. *nodep = INDEX_TO_NODE (p - list->elements);
  442. iterator->p = p + 1;
  443. return true;
  444. }
  445. else
  446. return false;
  447. }
  448. static void
  449. gl_array_iterator_free (gl_list_iterator_t *iterator _GL_ATTRIBUTE_MAYBE_UNUSED)
  450. {
  451. }
  452. /* ---------------------- Sorted gl_list_t Data Type ---------------------- */
  453. static size_t _GL_ATTRIBUTE_PURE
  454. gl_array_sortedlist_indexof_from_to (gl_list_t list,
  455. gl_listelement_compar_fn compar,
  456. size_t low, size_t high,
  457. const void *elt)
  458. {
  459. if (!(low <= high && high <= list->count))
  460. /* Invalid arguments. */
  461. abort ();
  462. if (low < high)
  463. {
  464. /* At each loop iteration, low < high; for indices < low the values
  465. are smaller than ELT; for indices >= high the values are greater
  466. than ELT. So, if the element occurs in the list, it is at
  467. low <= position < high. */
  468. do
  469. {
  470. size_t mid = low + (high - low) / 2; /* low <= mid < high */
  471. int cmp = compar (list->elements[mid], elt);
  472. if (cmp < 0)
  473. low = mid + 1;
  474. else if (cmp > 0)
  475. high = mid;
  476. else /* cmp == 0 */
  477. {
  478. /* We have an element equal to ELT at index MID. But we need
  479. the minimal such index. */
  480. high = mid;
  481. /* At each loop iteration, low <= high and
  482. compar (list->elements[high], elt) == 0,
  483. and we know that the first occurrence of the element is at
  484. low <= position <= high. */
  485. while (low < high)
  486. {
  487. size_t mid2 = low + (high - low) / 2; /* low <= mid2 < high */
  488. int cmp2 = compar (list->elements[mid2], elt);
  489. if (cmp2 < 0)
  490. low = mid2 + 1;
  491. else if (cmp2 > 0)
  492. /* The list was not sorted. */
  493. abort ();
  494. else /* cmp2 == 0 */
  495. {
  496. if (mid2 == low)
  497. break;
  498. high = mid2 - 1;
  499. }
  500. }
  501. return low;
  502. }
  503. }
  504. while (low < high);
  505. /* Here low == high. */
  506. }
  507. return (size_t)(-1);
  508. }
  509. static size_t _GL_ATTRIBUTE_PURE
  510. gl_array_sortedlist_indexof (gl_list_t list, gl_listelement_compar_fn compar,
  511. const void *elt)
  512. {
  513. return gl_array_sortedlist_indexof_from_to (list, compar, 0, list->count,
  514. elt);
  515. }
  516. static gl_list_node_t _GL_ATTRIBUTE_PURE
  517. gl_array_sortedlist_search_from_to (gl_list_t list,
  518. gl_listelement_compar_fn compar,
  519. size_t low, size_t high,
  520. const void *elt)
  521. {
  522. size_t index =
  523. gl_array_sortedlist_indexof_from_to (list, compar, low, high, elt);
  524. return INDEX_TO_NODE (index);
  525. }
  526. static gl_list_node_t _GL_ATTRIBUTE_PURE
  527. gl_array_sortedlist_search (gl_list_t list, gl_listelement_compar_fn compar,
  528. const void *elt)
  529. {
  530. size_t index =
  531. gl_array_sortedlist_indexof_from_to (list, compar, 0, list->count, elt);
  532. return INDEX_TO_NODE (index);
  533. }
  534. static gl_list_node_t
  535. gl_array_sortedlist_nx_add (gl_list_t list, gl_listelement_compar_fn compar,
  536. const void *elt)
  537. {
  538. size_t count = list->count;
  539. size_t low = 0;
  540. size_t high = count;
  541. /* At each loop iteration, low <= high; for indices < low the values are
  542. smaller than ELT; for indices >= high the values are greater than ELT. */
  543. while (low < high)
  544. {
  545. size_t mid = low + (high - low) / 2; /* low <= mid < high */
  546. int cmp = compar (list->elements[mid], elt);
  547. if (cmp < 0)
  548. low = mid + 1;
  549. else if (cmp > 0)
  550. high = mid;
  551. else /* cmp == 0 */
  552. {
  553. low = mid;
  554. break;
  555. }
  556. }
  557. return gl_array_nx_add_at (list, low, elt);
  558. }
  559. static bool
  560. gl_array_sortedlist_remove (gl_list_t list, gl_listelement_compar_fn compar,
  561. const void *elt)
  562. {
  563. size_t index = gl_array_sortedlist_indexof (list, compar, elt);
  564. if (index == (size_t)(-1))
  565. return false;
  566. else
  567. return gl_array_remove_at (list, index);
  568. }
  569. const struct gl_list_implementation gl_array_list_implementation =
  570. {
  571. gl_array_nx_create_empty,
  572. gl_array_nx_create,
  573. gl_array_size,
  574. gl_array_node_value,
  575. gl_array_node_nx_set_value,
  576. gl_array_next_node,
  577. gl_array_previous_node,
  578. gl_array_get_at,
  579. gl_array_nx_set_at,
  580. gl_array_search_from_to,
  581. gl_array_indexof_from_to,
  582. gl_array_nx_add_first,
  583. gl_array_nx_add_last,
  584. gl_array_nx_add_before,
  585. gl_array_nx_add_after,
  586. gl_array_nx_add_at,
  587. gl_array_remove_node,
  588. gl_array_remove_at,
  589. gl_array_remove,
  590. gl_array_list_free,
  591. gl_array_iterator,
  592. gl_array_iterator_from_to,
  593. gl_array_iterator_next,
  594. gl_array_iterator_free,
  595. gl_array_sortedlist_search,
  596. gl_array_sortedlist_search_from_to,
  597. gl_array_sortedlist_indexof,
  598. gl_array_sortedlist_indexof_from_to,
  599. gl_array_sortedlist_nx_add,
  600. gl_array_sortedlist_remove
  601. };