gl_anylinked_list2.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  1. /* Sequential list data type implemented by a linked list.
  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. /* Common code of gl_linked_list.c and gl_linkedhash_list.c. */
  15. /* If the symbol SIGNAL_SAFE_LIST is defined, the code is compiled in such
  16. a way that a gl_list_t data structure may be used from within a signal
  17. handler. The operations allowed in the signal handler are:
  18. gl_list_iterator, gl_list_iterator_next, gl_list_iterator_free.
  19. The list and node fields that are therefore accessed from the signal handler
  20. are:
  21. list->root, node->next, node->value.
  22. We are careful to make modifications to these fields only in an order
  23. that maintains the consistency of the list data structure at any moment,
  24. and we use 'volatile' assignments to prevent the compiler from reordering
  25. such assignments. */
  26. #ifdef SIGNAL_SAFE_LIST
  27. # define ASYNCSAFE(type) *(type volatile *)&
  28. #else
  29. # define ASYNCSAFE(type)
  30. #endif
  31. /* -------------------------- gl_list_t Data Type -------------------------- */
  32. static gl_list_t
  33. gl_linked_nx_create_empty (gl_list_implementation_t implementation,
  34. gl_listelement_equals_fn equals_fn,
  35. gl_listelement_hashcode_fn hashcode_fn,
  36. gl_listelement_dispose_fn dispose_fn,
  37. bool allow_duplicates)
  38. {
  39. struct gl_list_impl *list =
  40. (struct gl_list_impl *) malloc (sizeof (struct gl_list_impl));
  41. if (list == NULL)
  42. return NULL;
  43. list->base.vtable = implementation;
  44. list->base.equals_fn = equals_fn;
  45. list->base.hashcode_fn = hashcode_fn;
  46. list->base.dispose_fn = dispose_fn;
  47. list->base.allow_duplicates = allow_duplicates;
  48. #if WITH_HASHTABLE
  49. list->table_size = 11;
  50. list->table =
  51. (gl_hash_entry_t *) calloc (list->table_size, sizeof (gl_hash_entry_t));
  52. if (list->table == NULL)
  53. goto fail;
  54. #endif
  55. list->root.next = &list->root;
  56. list->root.prev = &list->root;
  57. list->count = 0;
  58. return list;
  59. #if WITH_HASHTABLE
  60. fail:
  61. free (list);
  62. return NULL;
  63. #endif
  64. }
  65. static gl_list_t
  66. gl_linked_nx_create (gl_list_implementation_t implementation,
  67. gl_listelement_equals_fn equals_fn,
  68. gl_listelement_hashcode_fn hashcode_fn,
  69. gl_listelement_dispose_fn dispose_fn,
  70. bool allow_duplicates,
  71. size_t count, const void **contents)
  72. {
  73. struct gl_list_impl *list =
  74. (struct gl_list_impl *) malloc (sizeof (struct gl_list_impl));
  75. gl_list_node_t tail;
  76. if (list == NULL)
  77. return NULL;
  78. list->base.vtable = implementation;
  79. list->base.equals_fn = equals_fn;
  80. list->base.hashcode_fn = hashcode_fn;
  81. list->base.dispose_fn = dispose_fn;
  82. list->base.allow_duplicates = allow_duplicates;
  83. #if WITH_HASHTABLE
  84. {
  85. size_t estimate = xsum (count, count / 2); /* 1.5 * count */
  86. if (estimate < 10)
  87. estimate = 10;
  88. list->table_size = next_prime (estimate);
  89. if (size_overflow_p (xtimes (list->table_size, sizeof (gl_hash_entry_t))))
  90. goto fail1;
  91. list->table =
  92. (gl_hash_entry_t *) calloc (list->table_size, sizeof (gl_hash_entry_t));
  93. if (list->table == NULL)
  94. goto fail1;
  95. }
  96. #endif
  97. list->count = count;
  98. tail = &list->root;
  99. for (; count > 0; contents++, count--)
  100. {
  101. gl_list_node_t node =
  102. (struct gl_list_node_impl *) malloc (sizeof (struct gl_list_node_impl));
  103. if (node == NULL)
  104. goto fail2;
  105. node->value = *contents;
  106. #if WITH_HASHTABLE
  107. node->h.hashcode =
  108. (list->base.hashcode_fn != NULL
  109. ? list->base.hashcode_fn (node->value)
  110. : (size_t)(uintptr_t) node->value);
  111. /* Add node to the hash table. */
  112. if (add_to_bucket (list, node) < 0)
  113. {
  114. free (node);
  115. goto fail2;
  116. }
  117. #endif
  118. /* Add node to the list. */
  119. node->prev = tail;
  120. tail->next = node;
  121. tail = node;
  122. }
  123. tail->next = &list->root;
  124. list->root.prev = tail;
  125. return list;
  126. fail2:
  127. {
  128. gl_list_node_t node;
  129. for (node = tail; node != &list->root; )
  130. {
  131. gl_list_node_t prev = node->prev;
  132. free (node);
  133. node = prev;
  134. }
  135. }
  136. #if WITH_HASHTABLE
  137. free (list->table);
  138. fail1:
  139. #endif
  140. free (list);
  141. return NULL;
  142. }
  143. static size_t _GL_ATTRIBUTE_PURE
  144. gl_linked_size (gl_list_t list)
  145. {
  146. return list->count;
  147. }
  148. static const void * _GL_ATTRIBUTE_PURE
  149. gl_linked_node_value (gl_list_t list _GL_ATTRIBUTE_MAYBE_UNUSED,
  150. gl_list_node_t node)
  151. {
  152. return node->value;
  153. }
  154. static int
  155. gl_linked_node_nx_set_value (gl_list_t list _GL_ATTRIBUTE_MAYBE_UNUSED,
  156. gl_list_node_t node,
  157. const void *elt)
  158. {
  159. #if WITH_HASHTABLE
  160. if (elt != node->value)
  161. {
  162. size_t new_hashcode =
  163. (list->base.hashcode_fn != NULL
  164. ? list->base.hashcode_fn (elt)
  165. : (size_t)(uintptr_t) elt);
  166. if (new_hashcode != node->h.hashcode)
  167. {
  168. remove_from_bucket (list, node);
  169. node->value = elt;
  170. node->h.hashcode = new_hashcode;
  171. if (add_to_bucket (list, node) < 0)
  172. {
  173. /* Out of memory. We removed node from a bucket but cannot add
  174. it to another bucket. In order to avoid inconsistencies, we
  175. must remove node entirely from the list. */
  176. gl_list_node_t before_removed = node->prev;
  177. gl_list_node_t after_removed = node->next;
  178. ASYNCSAFE(gl_list_node_t) before_removed->next = after_removed;
  179. after_removed->prev = before_removed;
  180. list->count--;
  181. free (node);
  182. return -1;
  183. }
  184. }
  185. else
  186. node->value = elt;
  187. }
  188. #else
  189. node->value = elt;
  190. #endif
  191. return 0;
  192. }
  193. static gl_list_node_t _GL_ATTRIBUTE_PURE
  194. gl_linked_next_node (gl_list_t list, gl_list_node_t node)
  195. {
  196. return (node->next != &list->root ? node->next : NULL);
  197. }
  198. static gl_list_node_t _GL_ATTRIBUTE_PURE
  199. gl_linked_previous_node (gl_list_t list, gl_list_node_t node)
  200. {
  201. return (node->prev != &list->root ? node->prev : NULL);
  202. }
  203. static const void * _GL_ATTRIBUTE_PURE
  204. gl_linked_get_at (gl_list_t list, size_t position)
  205. {
  206. size_t count = list->count;
  207. gl_list_node_t node;
  208. if (!(position < count))
  209. /* Invalid argument. */
  210. abort ();
  211. /* Here we know count > 0. */
  212. if (position <= ((count - 1) / 2))
  213. {
  214. node = list->root.next;
  215. for (; position > 0; position--)
  216. node = node->next;
  217. }
  218. else
  219. {
  220. position = count - 1 - position;
  221. node = list->root.prev;
  222. for (; position > 0; position--)
  223. node = node->prev;
  224. }
  225. return node->value;
  226. }
  227. static gl_list_node_t
  228. gl_linked_nx_set_at (gl_list_t list, size_t position, const void *elt)
  229. {
  230. size_t count = list->count;
  231. gl_list_node_t node;
  232. if (!(position < count))
  233. /* Invalid argument. */
  234. abort ();
  235. /* Here we know count > 0. */
  236. if (position <= ((count - 1) / 2))
  237. {
  238. node = list->root.next;
  239. for (; position > 0; position--)
  240. node = node->next;
  241. }
  242. else
  243. {
  244. position = count - 1 - position;
  245. node = list->root.prev;
  246. for (; position > 0; position--)
  247. node = node->prev;
  248. }
  249. #if WITH_HASHTABLE
  250. if (elt != node->value)
  251. {
  252. size_t new_hashcode =
  253. (list->base.hashcode_fn != NULL
  254. ? list->base.hashcode_fn (elt)
  255. : (size_t)(uintptr_t) elt);
  256. if (new_hashcode != node->h.hashcode)
  257. {
  258. remove_from_bucket (list, node);
  259. node->value = elt;
  260. node->h.hashcode = new_hashcode;
  261. if (add_to_bucket (list, node) < 0)
  262. {
  263. /* Out of memory. We removed node from a bucket but cannot add
  264. it to another bucket. In order to avoid inconsistencies, we
  265. must remove node entirely from the list. */
  266. gl_list_node_t before_removed = node->prev;
  267. gl_list_node_t after_removed = node->next;
  268. ASYNCSAFE(gl_list_node_t) before_removed->next = after_removed;
  269. after_removed->prev = before_removed;
  270. list->count--;
  271. free (node);
  272. return NULL;
  273. }
  274. }
  275. else
  276. node->value = elt;
  277. }
  278. #else
  279. node->value = elt;
  280. #endif
  281. return node;
  282. }
  283. static gl_list_node_t _GL_ATTRIBUTE_PURE
  284. gl_linked_search_from_to (gl_list_t list, size_t start_index, size_t end_index,
  285. const void *elt)
  286. {
  287. size_t count = list->count;
  288. if (!(start_index <= end_index && end_index <= count))
  289. /* Invalid arguments. */
  290. abort ();
  291. {
  292. #if WITH_HASHTABLE
  293. size_t hashcode =
  294. (list->base.hashcode_fn != NULL
  295. ? list->base.hashcode_fn (elt)
  296. : (size_t)(uintptr_t) elt);
  297. size_t bucket = hashcode % list->table_size;
  298. gl_listelement_equals_fn equals = list->base.equals_fn;
  299. if (!list->base.allow_duplicates)
  300. {
  301. /* Look for the first match in the hash bucket. */
  302. gl_list_node_t found = NULL;
  303. gl_list_node_t node;
  304. for (node = (gl_list_node_t) list->table[bucket];
  305. node != NULL;
  306. node = (gl_list_node_t) node->h.hash_next)
  307. if (node->h.hashcode == hashcode
  308. && (equals != NULL
  309. ? equals (elt, node->value)
  310. : elt == node->value))
  311. {
  312. found = node;
  313. break;
  314. }
  315. if (start_index > 0)
  316. /* Look whether found's index is < start_index. */
  317. for (node = list->root.next; ; node = node->next)
  318. {
  319. if (node == found)
  320. return NULL;
  321. if (--start_index == 0)
  322. break;
  323. }
  324. if (end_index < count)
  325. /* Look whether found's index is >= end_index. */
  326. {
  327. end_index = count - end_index;
  328. for (node = list->root.prev; ; node = node->prev)
  329. {
  330. if (node == found)
  331. return NULL;
  332. if (--end_index == 0)
  333. break;
  334. }
  335. }
  336. return found;
  337. }
  338. else
  339. {
  340. /* Look whether there is more than one match in the hash bucket. */
  341. bool multiple_matches = false;
  342. gl_list_node_t first_match = NULL;
  343. gl_list_node_t node;
  344. for (node = (gl_list_node_t) list->table[bucket];
  345. node != NULL;
  346. node = (gl_list_node_t) node->h.hash_next)
  347. if (node->h.hashcode == hashcode
  348. && (equals != NULL
  349. ? equals (elt, node->value)
  350. : elt == node->value))
  351. {
  352. if (first_match == NULL)
  353. first_match = node;
  354. else
  355. {
  356. multiple_matches = true;
  357. break;
  358. }
  359. }
  360. if (multiple_matches)
  361. {
  362. /* We need the match with the smallest index. But we don't have
  363. a fast mapping node -> index. So we have to walk the list. */
  364. end_index -= start_index;
  365. node = list->root.next;
  366. for (; start_index > 0; start_index--)
  367. node = node->next;
  368. for (;
  369. end_index > 0;
  370. node = node->next, end_index--)
  371. if (node->h.hashcode == hashcode
  372. && (equals != NULL
  373. ? equals (elt, node->value)
  374. : elt == node->value))
  375. return node;
  376. /* The matches must have all been at indices < start_index or
  377. >= end_index. */
  378. return NULL;
  379. }
  380. else
  381. {
  382. if (start_index > 0)
  383. /* Look whether first_match's index is < start_index. */
  384. for (node = list->root.next; node != &list->root; node = node->next)
  385. {
  386. if (node == first_match)
  387. return NULL;
  388. if (--start_index == 0)
  389. break;
  390. }
  391. if (end_index < list->count)
  392. /* Look whether first_match's index is >= end_index. */
  393. {
  394. end_index = list->count - end_index;
  395. for (node = list->root.prev; ; node = node->prev)
  396. {
  397. if (node == first_match)
  398. return NULL;
  399. if (--end_index == 0)
  400. break;
  401. }
  402. }
  403. return first_match;
  404. }
  405. }
  406. #else
  407. gl_listelement_equals_fn equals = list->base.equals_fn;
  408. gl_list_node_t node = list->root.next;
  409. end_index -= start_index;
  410. for (; start_index > 0; start_index--)
  411. node = node->next;
  412. if (equals != NULL)
  413. {
  414. for (; end_index > 0; node = node->next, end_index--)
  415. if (equals (elt, node->value))
  416. return node;
  417. }
  418. else
  419. {
  420. for (; end_index > 0; node = node->next, end_index--)
  421. if (elt == node->value)
  422. return node;
  423. }
  424. return NULL;
  425. #endif
  426. }
  427. }
  428. static size_t _GL_ATTRIBUTE_PURE
  429. gl_linked_indexof_from_to (gl_list_t list, size_t start_index, size_t end_index,
  430. const void *elt)
  431. {
  432. size_t count = list->count;
  433. if (!(start_index <= end_index && end_index <= count))
  434. /* Invalid arguments. */
  435. abort ();
  436. {
  437. #if WITH_HASHTABLE
  438. /* Here the hash table doesn't help much. It only allows us to minimize
  439. the number of equals() calls, by looking up first the node and then
  440. its index. */
  441. size_t hashcode =
  442. (list->base.hashcode_fn != NULL
  443. ? list->base.hashcode_fn (elt)
  444. : (size_t)(uintptr_t) elt);
  445. size_t bucket = hashcode % list->table_size;
  446. gl_listelement_equals_fn equals = list->base.equals_fn;
  447. gl_list_node_t node;
  448. /* First step: Look up the node. */
  449. if (!list->base.allow_duplicates)
  450. {
  451. /* Look for the first match in the hash bucket. */
  452. for (node = (gl_list_node_t) list->table[bucket];
  453. node != NULL;
  454. node = (gl_list_node_t) node->h.hash_next)
  455. if (node->h.hashcode == hashcode
  456. && (equals != NULL
  457. ? equals (elt, node->value)
  458. : elt == node->value))
  459. break;
  460. }
  461. else
  462. {
  463. /* Look whether there is more than one match in the hash bucket. */
  464. bool multiple_matches = false;
  465. gl_list_node_t first_match = NULL;
  466. for (node = (gl_list_node_t) list->table[bucket];
  467. node != NULL;
  468. node = (gl_list_node_t) node->h.hash_next)
  469. if (node->h.hashcode == hashcode
  470. && (equals != NULL
  471. ? equals (elt, node->value)
  472. : elt == node->value))
  473. {
  474. if (first_match == NULL)
  475. first_match = node;
  476. else
  477. {
  478. multiple_matches = true;
  479. break;
  480. }
  481. }
  482. if (multiple_matches)
  483. {
  484. /* We need the match with the smallest index. But we don't have
  485. a fast mapping node -> index. So we have to walk the list. */
  486. size_t index;
  487. index = start_index;
  488. node = list->root.next;
  489. for (; start_index > 0; start_index--)
  490. node = node->next;
  491. for (;
  492. index < end_index;
  493. node = node->next, index++)
  494. if (node->h.hashcode == hashcode
  495. && (equals != NULL
  496. ? equals (elt, node->value)
  497. : elt == node->value))
  498. return index;
  499. /* The matches must have all been at indices < start_index or
  500. >= end_index. */
  501. return (size_t)(-1);
  502. }
  503. node = first_match;
  504. }
  505. /* Second step: Look up the index of the node. */
  506. if (node == NULL)
  507. return (size_t)(-1);
  508. else
  509. {
  510. size_t index = 0;
  511. for (; node->prev != &list->root; node = node->prev)
  512. index++;
  513. if (index >= start_index && index < end_index)
  514. return index;
  515. else
  516. return (size_t)(-1);
  517. }
  518. #else
  519. gl_listelement_equals_fn equals = list->base.equals_fn;
  520. size_t index = start_index;
  521. gl_list_node_t node = list->root.next;
  522. for (; start_index > 0; start_index--)
  523. node = node->next;
  524. if (equals != NULL)
  525. {
  526. for (;
  527. index < end_index;
  528. node = node->next, index++)
  529. if (equals (elt, node->value))
  530. return index;
  531. }
  532. else
  533. {
  534. for (;
  535. index < end_index;
  536. node = node->next, index++)
  537. if (elt == node->value)
  538. return index;
  539. }
  540. return (size_t)(-1);
  541. #endif
  542. }
  543. }
  544. static gl_list_node_t
  545. gl_linked_nx_add_first (gl_list_t list, const void *elt)
  546. {
  547. gl_list_node_t node =
  548. (struct gl_list_node_impl *) malloc (sizeof (struct gl_list_node_impl));
  549. if (node == NULL)
  550. return NULL;
  551. ASYNCSAFE(const void *) node->value = elt;
  552. #if WITH_HASHTABLE
  553. node->h.hashcode =
  554. (list->base.hashcode_fn != NULL
  555. ? list->base.hashcode_fn (node->value)
  556. : (size_t)(uintptr_t) node->value);
  557. /* Add node to the hash table. */
  558. if (add_to_bucket (list, node) < 0)
  559. {
  560. free (node);
  561. return NULL;
  562. }
  563. #endif
  564. /* Add node to the list. */
  565. node->prev = &list->root;
  566. ASYNCSAFE(gl_list_node_t) node->next = list->root.next;
  567. node->next->prev = node;
  568. ASYNCSAFE(gl_list_node_t) list->root.next = node;
  569. list->count++;
  570. #if WITH_HASHTABLE
  571. hash_resize_after_add (list);
  572. #endif
  573. return node;
  574. }
  575. static gl_list_node_t
  576. gl_linked_nx_add_last (gl_list_t list, const void *elt)
  577. {
  578. gl_list_node_t node =
  579. (struct gl_list_node_impl *) malloc (sizeof (struct gl_list_node_impl));
  580. if (node == NULL)
  581. return NULL;
  582. ASYNCSAFE(const void *) node->value = elt;
  583. #if WITH_HASHTABLE
  584. node->h.hashcode =
  585. (list->base.hashcode_fn != NULL
  586. ? list->base.hashcode_fn (node->value)
  587. : (size_t)(uintptr_t) node->value);
  588. /* Add node to the hash table. */
  589. if (add_to_bucket (list, node) < 0)
  590. {
  591. free (node);
  592. return NULL;
  593. }
  594. #endif
  595. /* Add node to the list. */
  596. ASYNCSAFE(gl_list_node_t) node->next = &list->root;
  597. node->prev = list->root.prev;
  598. ASYNCSAFE(gl_list_node_t) node->prev->next = node;
  599. list->root.prev = node;
  600. list->count++;
  601. #if WITH_HASHTABLE
  602. hash_resize_after_add (list);
  603. #endif
  604. return node;
  605. }
  606. static gl_list_node_t
  607. gl_linked_nx_add_before (gl_list_t list, gl_list_node_t node, const void *elt)
  608. {
  609. gl_list_node_t new_node =
  610. (struct gl_list_node_impl *) malloc (sizeof (struct gl_list_node_impl));
  611. if (new_node == NULL)
  612. return NULL;
  613. ASYNCSAFE(const void *) new_node->value = elt;
  614. #if WITH_HASHTABLE
  615. new_node->h.hashcode =
  616. (list->base.hashcode_fn != NULL
  617. ? list->base.hashcode_fn (new_node->value)
  618. : (size_t)(uintptr_t) new_node->value);
  619. /* Add new_node to the hash table. */
  620. if (add_to_bucket (list, new_node) < 0)
  621. {
  622. free (new_node);
  623. return NULL;
  624. }
  625. #endif
  626. /* Add new_node to the list. */
  627. ASYNCSAFE(gl_list_node_t) new_node->next = node;
  628. new_node->prev = node->prev;
  629. ASYNCSAFE(gl_list_node_t) new_node->prev->next = new_node;
  630. node->prev = new_node;
  631. list->count++;
  632. #if WITH_HASHTABLE
  633. hash_resize_after_add (list);
  634. #endif
  635. return new_node;
  636. }
  637. static gl_list_node_t
  638. gl_linked_nx_add_after (gl_list_t list, gl_list_node_t node, const void *elt)
  639. {
  640. gl_list_node_t new_node =
  641. (struct gl_list_node_impl *) malloc (sizeof (struct gl_list_node_impl));
  642. if (new_node == NULL)
  643. return NULL;
  644. ASYNCSAFE(const void *) new_node->value = elt;
  645. #if WITH_HASHTABLE
  646. new_node->h.hashcode =
  647. (list->base.hashcode_fn != NULL
  648. ? list->base.hashcode_fn (new_node->value)
  649. : (size_t)(uintptr_t) new_node->value);
  650. /* Add new_node to the hash table. */
  651. if (add_to_bucket (list, new_node) < 0)
  652. {
  653. free (new_node);
  654. return NULL;
  655. }
  656. #endif
  657. /* Add new_node to the list. */
  658. new_node->prev = node;
  659. ASYNCSAFE(gl_list_node_t) new_node->next = node->next;
  660. new_node->next->prev = new_node;
  661. ASYNCSAFE(gl_list_node_t) node->next = new_node;
  662. list->count++;
  663. #if WITH_HASHTABLE
  664. hash_resize_after_add (list);
  665. #endif
  666. return new_node;
  667. }
  668. static gl_list_node_t
  669. gl_linked_nx_add_at (gl_list_t list, size_t position, const void *elt)
  670. {
  671. size_t count = list->count;
  672. gl_list_node_t new_node;
  673. if (!(position <= count))
  674. /* Invalid argument. */
  675. abort ();
  676. new_node = (struct gl_list_node_impl *) malloc (sizeof (struct gl_list_node_impl));
  677. if (new_node == NULL)
  678. return NULL;
  679. ASYNCSAFE(const void *) new_node->value = elt;
  680. #if WITH_HASHTABLE
  681. new_node->h.hashcode =
  682. (list->base.hashcode_fn != NULL
  683. ? list->base.hashcode_fn (new_node->value)
  684. : (size_t)(uintptr_t) new_node->value);
  685. /* Add new_node to the hash table. */
  686. if (add_to_bucket (list, new_node) < 0)
  687. {
  688. free (new_node);
  689. return NULL;
  690. }
  691. #endif
  692. /* Add new_node to the list. */
  693. if (position <= (count / 2))
  694. {
  695. gl_list_node_t node;
  696. node = &list->root;
  697. for (; position > 0; position--)
  698. node = node->next;
  699. new_node->prev = node;
  700. ASYNCSAFE(gl_list_node_t) new_node->next = node->next;
  701. new_node->next->prev = new_node;
  702. ASYNCSAFE(gl_list_node_t) node->next = new_node;
  703. }
  704. else
  705. {
  706. gl_list_node_t node;
  707. position = count - position;
  708. node = &list->root;
  709. for (; position > 0; position--)
  710. node = node->prev;
  711. ASYNCSAFE(gl_list_node_t) new_node->next = node;
  712. new_node->prev = node->prev;
  713. ASYNCSAFE(gl_list_node_t) new_node->prev->next = new_node;
  714. node->prev = new_node;
  715. }
  716. list->count++;
  717. #if WITH_HASHTABLE
  718. hash_resize_after_add (list);
  719. #endif
  720. return new_node;
  721. }
  722. static bool
  723. gl_linked_remove_node (gl_list_t list, gl_list_node_t node)
  724. {
  725. gl_list_node_t prev;
  726. gl_list_node_t next;
  727. #if WITH_HASHTABLE
  728. /* Remove node from the hash table. */
  729. remove_from_bucket (list, node);
  730. #endif
  731. /* Remove node from the list. */
  732. prev = node->prev;
  733. next = node->next;
  734. ASYNCSAFE(gl_list_node_t) prev->next = next;
  735. next->prev = prev;
  736. list->count--;
  737. if (list->base.dispose_fn != NULL)
  738. list->base.dispose_fn (node->value);
  739. free (node);
  740. return true;
  741. }
  742. static bool
  743. gl_linked_remove_at (gl_list_t list, size_t position)
  744. {
  745. size_t count = list->count;
  746. gl_list_node_t removed_node;
  747. if (!(position < count))
  748. /* Invalid argument. */
  749. abort ();
  750. /* Here we know count > 0. */
  751. if (position <= ((count - 1) / 2))
  752. {
  753. gl_list_node_t node;
  754. gl_list_node_t after_removed;
  755. node = &list->root;
  756. for (; position > 0; position--)
  757. node = node->next;
  758. removed_node = node->next;
  759. after_removed = node->next->next;
  760. ASYNCSAFE(gl_list_node_t) node->next = after_removed;
  761. after_removed->prev = node;
  762. }
  763. else
  764. {
  765. gl_list_node_t node;
  766. gl_list_node_t before_removed;
  767. position = count - 1 - position;
  768. node = &list->root;
  769. for (; position > 0; position--)
  770. node = node->prev;
  771. removed_node = node->prev;
  772. before_removed = node->prev->prev;
  773. node->prev = before_removed;
  774. ASYNCSAFE(gl_list_node_t) before_removed->next = node;
  775. }
  776. #if WITH_HASHTABLE
  777. remove_from_bucket (list, removed_node);
  778. #endif
  779. list->count--;
  780. if (list->base.dispose_fn != NULL)
  781. list->base.dispose_fn (removed_node->value);
  782. free (removed_node);
  783. return true;
  784. }
  785. static bool
  786. gl_linked_remove (gl_list_t list, const void *elt)
  787. {
  788. gl_list_node_t node = gl_linked_search_from_to (list, 0, list->count, elt);
  789. if (node != NULL)
  790. return gl_linked_remove_node (list, node);
  791. else
  792. return false;
  793. }
  794. static void
  795. gl_linked_list_free (gl_list_t list)
  796. {
  797. gl_listelement_dispose_fn dispose = list->base.dispose_fn;
  798. gl_list_node_t node;
  799. for (node = list->root.next; node != &list->root; )
  800. {
  801. gl_list_node_t next = node->next;
  802. if (dispose != NULL)
  803. dispose (node->value);
  804. free (node);
  805. node = next;
  806. }
  807. #if WITH_HASHTABLE
  808. free (list->table);
  809. #endif
  810. free (list);
  811. }
  812. /* --------------------- gl_list_iterator_t Data Type --------------------- */
  813. static gl_list_iterator_t _GL_ATTRIBUTE_PURE
  814. gl_linked_iterator (gl_list_t list)
  815. {
  816. gl_list_iterator_t result;
  817. result.vtable = list->base.vtable;
  818. result.list = list;
  819. result.p = list->root.next;
  820. result.q = &list->root;
  821. #if defined GCC_LINT || defined lint
  822. result.i = 0;
  823. result.j = 0;
  824. result.count = 0;
  825. #endif
  826. return result;
  827. }
  828. static gl_list_iterator_t _GL_ATTRIBUTE_PURE
  829. gl_linked_iterator_from_to (gl_list_t list,
  830. size_t start_index, size_t end_index)
  831. {
  832. gl_list_iterator_t result;
  833. size_t n1, n2, n3;
  834. if (!(start_index <= end_index && end_index <= list->count))
  835. /* Invalid arguments. */
  836. abort ();
  837. result.vtable = list->base.vtable;
  838. result.list = list;
  839. n1 = start_index;
  840. n2 = end_index - start_index;
  841. n3 = list->count - end_index;
  842. /* Find the maximum among n1, n2, n3, so as to reduce the number of
  843. loop iterations to n1 + n2 + n3 - max(n1,n2,n3). */
  844. if (n1 > n2 && n1 > n3)
  845. {
  846. /* n1 is the maximum, use n2 and n3. */
  847. gl_list_node_t node;
  848. size_t i;
  849. node = &list->root;
  850. for (i = n3; i > 0; i--)
  851. node = node->prev;
  852. result.q = node;
  853. for (i = n2; i > 0; i--)
  854. node = node->prev;
  855. result.p = node;
  856. }
  857. else if (n2 > n3)
  858. {
  859. /* n2 is the maximum, use n1 and n3. */
  860. gl_list_node_t node;
  861. size_t i;
  862. node = list->root.next;
  863. for (i = n1; i > 0; i--)
  864. node = node->next;
  865. result.p = node;
  866. node = &list->root;
  867. for (i = n3; i > 0; i--)
  868. node = node->prev;
  869. result.q = node;
  870. }
  871. else
  872. {
  873. /* n3 is the maximum, use n1 and n2. */
  874. gl_list_node_t node;
  875. size_t i;
  876. node = list->root.next;
  877. for (i = n1; i > 0; i--)
  878. node = node->next;
  879. result.p = node;
  880. for (i = n2; i > 0; i--)
  881. node = node->next;
  882. result.q = node;
  883. }
  884. #if defined GCC_LINT || defined lint
  885. result.i = 0;
  886. result.j = 0;
  887. result.count = 0;
  888. #endif
  889. return result;
  890. }
  891. static bool
  892. gl_linked_iterator_next (gl_list_iterator_t *iterator,
  893. const void **eltp, gl_list_node_t *nodep)
  894. {
  895. if (iterator->p != iterator->q)
  896. {
  897. gl_list_node_t node = (gl_list_node_t) iterator->p;
  898. *eltp = node->value;
  899. if (nodep != NULL)
  900. *nodep = node;
  901. iterator->p = node->next;
  902. return true;
  903. }
  904. else
  905. return false;
  906. }
  907. static void
  908. gl_linked_iterator_free (gl_list_iterator_t *iterator _GL_ATTRIBUTE_MAYBE_UNUSED)
  909. {
  910. }
  911. /* ---------------------- Sorted gl_list_t Data Type ---------------------- */
  912. static gl_list_node_t _GL_ATTRIBUTE_PURE
  913. gl_linked_sortedlist_search (gl_list_t list, gl_listelement_compar_fn compar,
  914. const void *elt)
  915. {
  916. gl_list_node_t node;
  917. for (node = list->root.next; node != &list->root; node = node->next)
  918. {
  919. int cmp = compar (node->value, elt);
  920. if (cmp > 0)
  921. break;
  922. if (cmp == 0)
  923. return node;
  924. }
  925. return NULL;
  926. }
  927. static gl_list_node_t _GL_ATTRIBUTE_PURE
  928. gl_linked_sortedlist_search_from_to (gl_list_t list,
  929. gl_listelement_compar_fn compar,
  930. size_t low, size_t high,
  931. const void *elt)
  932. {
  933. size_t count = list->count;
  934. if (!(low <= high && high <= list->count))
  935. /* Invalid arguments. */
  936. abort ();
  937. high -= low;
  938. if (high > 0)
  939. {
  940. /* Here we know low < count. */
  941. size_t position = low;
  942. gl_list_node_t node;
  943. if (position <= ((count - 1) / 2))
  944. {
  945. node = list->root.next;
  946. for (; position > 0; position--)
  947. node = node->next;
  948. }
  949. else
  950. {
  951. position = count - 1 - position;
  952. node = list->root.prev;
  953. for (; position > 0; position--)
  954. node = node->prev;
  955. }
  956. do
  957. {
  958. int cmp = compar (node->value, elt);
  959. if (cmp > 0)
  960. break;
  961. if (cmp == 0)
  962. return node;
  963. node = node->next;
  964. }
  965. while (--high > 0);
  966. }
  967. return NULL;
  968. }
  969. static size_t _GL_ATTRIBUTE_PURE
  970. gl_linked_sortedlist_indexof (gl_list_t list, gl_listelement_compar_fn compar,
  971. const void *elt)
  972. {
  973. gl_list_node_t node;
  974. size_t index;
  975. for (node = list->root.next, index = 0;
  976. node != &list->root;
  977. node = node->next, index++)
  978. {
  979. int cmp = compar (node->value, elt);
  980. if (cmp > 0)
  981. break;
  982. if (cmp == 0)
  983. return index;
  984. }
  985. return (size_t)(-1);
  986. }
  987. static size_t _GL_ATTRIBUTE_PURE
  988. gl_linked_sortedlist_indexof_from_to (gl_list_t list,
  989. gl_listelement_compar_fn compar,
  990. size_t low, size_t high,
  991. const void *elt)
  992. {
  993. size_t count = list->count;
  994. if (!(low <= high && high <= list->count))
  995. /* Invalid arguments. */
  996. abort ();
  997. high -= low;
  998. if (high > 0)
  999. {
  1000. /* Here we know low < count. */
  1001. size_t index = low;
  1002. size_t position = low;
  1003. gl_list_node_t node;
  1004. if (position <= ((count - 1) / 2))
  1005. {
  1006. node = list->root.next;
  1007. for (; position > 0; position--)
  1008. node = node->next;
  1009. }
  1010. else
  1011. {
  1012. position = count - 1 - position;
  1013. node = list->root.prev;
  1014. for (; position > 0; position--)
  1015. node = node->prev;
  1016. }
  1017. do
  1018. {
  1019. int cmp = compar (node->value, elt);
  1020. if (cmp > 0)
  1021. break;
  1022. if (cmp == 0)
  1023. return index;
  1024. node = node->next;
  1025. index++;
  1026. }
  1027. while (--high > 0);
  1028. }
  1029. return (size_t)(-1);
  1030. }
  1031. static gl_list_node_t
  1032. gl_linked_sortedlist_nx_add (gl_list_t list, gl_listelement_compar_fn compar,
  1033. const void *elt)
  1034. {
  1035. gl_list_node_t node;
  1036. for (node = list->root.next; node != &list->root; node = node->next)
  1037. if (compar (node->value, elt) >= 0)
  1038. return gl_linked_nx_add_before (list, node, elt);
  1039. return gl_linked_nx_add_last (list, elt);
  1040. }
  1041. static bool
  1042. gl_linked_sortedlist_remove (gl_list_t list, gl_listelement_compar_fn compar,
  1043. const void *elt)
  1044. {
  1045. gl_list_node_t node;
  1046. for (node = list->root.next; node != &list->root; node = node->next)
  1047. {
  1048. int cmp = compar (node->value, elt);
  1049. if (cmp > 0)
  1050. break;
  1051. if (cmp == 0)
  1052. return gl_linked_remove_node (list, node);
  1053. }
  1054. return false;
  1055. }