gl_anylinked_list2.h 31 KB

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