list.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. /* Functions to support link list bitsets.
  2. Copyright (C) 2002-2004, 2006, 2009-2015, 2018-2020 Free Software
  3. Foundation, Inc.
  4. Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  15. #include <config.h>
  16. #include "bitset/list.h"
  17. #include <stddef.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "obstack.h"
  22. /* This file implements linked-list bitsets. These bitsets can be of
  23. arbitrary length and are more efficient than arrays of bits for
  24. large sparse sets.
  25. Usually if all the bits in an element are zero we remove the element
  26. from the list. However, a side effect of the bit caching is that we
  27. do not always notice when an element becomes zero. Hence the
  28. lbitset_weed function which removes zero elements. */
  29. /* Number of words to use for each element. The larger the value the
  30. greater the size of the cache and the shorter the time to find a given bit
  31. but the more memory wasted for sparse bitsets and the longer the time
  32. to search for set bits.
  33. The routines that dominate timing profiles are lbitset_elt_find
  34. and lbitset_elt_link, especially when accessing the bits randomly. */
  35. #define LBITSET_ELT_WORDS 2
  36. typedef bitset_word lbitset_word;
  37. #define LBITSET_WORD_BITS BITSET_WORD_BITS
  38. /* Number of bits stored in each element. */
  39. #define LBITSET_ELT_BITS \
  40. ((unsigned) (LBITSET_ELT_WORDS * LBITSET_WORD_BITS))
  41. /* Lbitset element. We use an array of bits for each element.
  42. These are linked together in a doubly-linked list. */
  43. typedef struct lbitset_elt_struct
  44. {
  45. struct lbitset_elt_struct *next; /* Next element. */
  46. struct lbitset_elt_struct *prev; /* Previous element. */
  47. bitset_windex index; /* bitno / BITSET_WORD_BITS. */
  48. bitset_word words[LBITSET_ELT_WORDS]; /* Bits that are set. */
  49. }
  50. lbitset_elt;
  51. enum lbitset_find_mode
  52. { LBITSET_FIND, LBITSET_CREATE, LBITSET_SUBST };
  53. static lbitset_elt lbitset_zero_elts[3]; /* Elements of all zero bits. */
  54. /* Obstack to allocate bitset elements from. */
  55. static struct obstack lbitset_obstack;
  56. static bool lbitset_obstack_init = false;
  57. static lbitset_elt *lbitset_free_list; /* Free list of bitset elements. */
  58. extern void debug_lbitset (bitset);
  59. #define LBITSET_CURRENT1(X) \
  60. ((lbitset_elt *) (void *) ((char *) (X) - offsetof (lbitset_elt, words)))
  61. #define LBITSET_CURRENT(X) LBITSET_CURRENT1((X)->b.cdata)
  62. #define LBITSET_HEAD(X) ((X)->l.head)
  63. #define LBITSET_TAIL(X) ((X)->l.tail)
  64. /* Allocate a lbitset element. The bits are not cleared. */
  65. static inline lbitset_elt *
  66. lbitset_elt_alloc (void)
  67. {
  68. lbitset_elt *elt;
  69. if (lbitset_free_list != 0)
  70. {
  71. elt = lbitset_free_list;
  72. lbitset_free_list = elt->next;
  73. }
  74. else
  75. {
  76. if (!lbitset_obstack_init)
  77. {
  78. lbitset_obstack_init = true;
  79. /* Let particular systems override the size of a chunk. */
  80. #ifndef OBSTACK_CHUNK_SIZE
  81. # define OBSTACK_CHUNK_SIZE 0
  82. #endif
  83. /* Let them override the alloc and free routines too. */
  84. #ifndef OBSTACK_CHUNK_ALLOC
  85. # define OBSTACK_CHUNK_ALLOC xmalloc
  86. #endif
  87. #ifndef OBSTACK_CHUNK_FREE
  88. # define OBSTACK_CHUNK_FREE free
  89. #endif
  90. #if !(defined __GNUC__ || defined __clang__)
  91. # define __alignof__(type) 0
  92. #endif
  93. obstack_specify_allocation (&lbitset_obstack, OBSTACK_CHUNK_SIZE,
  94. __alignof__ (lbitset_elt),
  95. OBSTACK_CHUNK_ALLOC,
  96. OBSTACK_CHUNK_FREE);
  97. }
  98. /* Perhaps we should add a number of new elements to the free
  99. list. */
  100. elt = (lbitset_elt *) obstack_alloc (&lbitset_obstack,
  101. sizeof (lbitset_elt));
  102. }
  103. return elt;
  104. }
  105. /* Allocate a lbitset element. The bits are cleared. */
  106. static inline lbitset_elt *
  107. lbitset_elt_calloc (void)
  108. {
  109. lbitset_elt *elt = lbitset_elt_alloc ();
  110. memset (elt->words, 0, sizeof (elt->words));
  111. return elt;
  112. }
  113. static inline void
  114. lbitset_elt_free (lbitset_elt *elt)
  115. {
  116. elt->next = lbitset_free_list;
  117. lbitset_free_list = elt;
  118. }
  119. /* Unlink element ELT from bitset BSET. */
  120. static inline void
  121. lbitset_elt_unlink (bitset bset, lbitset_elt *elt)
  122. {
  123. lbitset_elt *next = elt->next;
  124. lbitset_elt *prev = elt->prev;
  125. if (prev)
  126. prev->next = next;
  127. if (next)
  128. next->prev = prev;
  129. if (LBITSET_HEAD (bset) == elt)
  130. LBITSET_HEAD (bset) = next;
  131. if (LBITSET_TAIL (bset) == elt)
  132. LBITSET_TAIL (bset) = prev;
  133. /* Update cache pointer. Since the first thing we try is to insert
  134. before current, make current the next entry in preference to the
  135. previous. */
  136. if (LBITSET_CURRENT (bset) == elt)
  137. {
  138. if (next)
  139. {
  140. bset->b.cdata = next->words;
  141. bset->b.cindex = next->index;
  142. }
  143. else if (prev)
  144. {
  145. bset->b.cdata = prev->words;
  146. bset->b.cindex = prev->index;
  147. }
  148. else
  149. {
  150. bset->b.csize = 0;
  151. bset->b.cdata = 0;
  152. }
  153. }
  154. lbitset_elt_free (elt);
  155. }
  156. /* Cut the chain of bitset BSET before element ELT and free the
  157. elements. */
  158. static inline void
  159. lbitset_prune (bitset bset, lbitset_elt *elt)
  160. {
  161. if (!elt)
  162. return;
  163. if (elt->prev)
  164. {
  165. LBITSET_TAIL (bset) = elt->prev;
  166. bset->b.cdata = elt->prev->words;
  167. bset->b.cindex = elt->prev->index;
  168. elt->prev->next = 0;
  169. }
  170. else
  171. {
  172. LBITSET_HEAD (bset) = 0;
  173. LBITSET_TAIL (bset) = 0;
  174. bset->b.cdata = 0;
  175. bset->b.csize = 0;
  176. }
  177. lbitset_elt *next;
  178. for (; elt; elt = next)
  179. {
  180. next = elt->next;
  181. lbitset_elt_free (elt);
  182. }
  183. }
  184. /* Are all bits in an element zero? */
  185. static inline bool
  186. lbitset_elt_zero_p (lbitset_elt *elt)
  187. {
  188. for (int i = 0; i < LBITSET_ELT_WORDS; i++)
  189. if (elt->words[i])
  190. return false;
  191. return true;
  192. }
  193. /* Link the bitset element into the current bitset linked list. */
  194. static inline void
  195. lbitset_elt_link (bitset bset, lbitset_elt *elt)
  196. {
  197. bitset_windex windex = elt->index;
  198. lbitset_elt *current = bset->b.csize ? LBITSET_CURRENT (bset) : LBITSET_HEAD (bset);
  199. /* If this is the first and only element, add it in. */
  200. if (LBITSET_HEAD (bset) == 0)
  201. {
  202. elt->next = elt->prev = 0;
  203. LBITSET_HEAD (bset) = elt;
  204. LBITSET_TAIL (bset) = elt;
  205. }
  206. /* If this index is less than that of the current element, it goes
  207. somewhere before the current element. */
  208. else if (windex < bset->b.cindex)
  209. {
  210. lbitset_elt *ptr;
  211. for (ptr = current;
  212. ptr->prev && ptr->prev->index > windex; ptr = ptr->prev)
  213. continue;
  214. if (ptr->prev)
  215. ptr->prev->next = elt;
  216. else
  217. LBITSET_HEAD (bset) = elt;
  218. elt->prev = ptr->prev;
  219. elt->next = ptr;
  220. ptr->prev = elt;
  221. }
  222. /* Otherwise, it must go somewhere after the current element. */
  223. else
  224. {
  225. lbitset_elt *ptr;
  226. for (ptr = current;
  227. ptr->next && ptr->next->index < windex; ptr = ptr->next)
  228. continue;
  229. if (ptr->next)
  230. ptr->next->prev = elt;
  231. else
  232. LBITSET_TAIL (bset) = elt;
  233. elt->next = ptr->next;
  234. elt->prev = ptr;
  235. ptr->next = elt;
  236. }
  237. /* Set up so this is the first element searched. */
  238. bset->b.cindex = windex;
  239. bset->b.csize = LBITSET_ELT_WORDS;
  240. bset->b.cdata = elt->words;
  241. }
  242. static lbitset_elt *
  243. lbitset_elt_find (bitset bset, bitset_windex windex,
  244. enum lbitset_find_mode mode)
  245. {
  246. lbitset_elt *current;
  247. if (bset->b.csize)
  248. {
  249. current = LBITSET_CURRENT (bset);
  250. /* Check if element is the cached element. */
  251. if ((windex - bset->b.cindex) < bset->b.csize)
  252. return current;
  253. }
  254. else
  255. {
  256. current = LBITSET_HEAD (bset);
  257. }
  258. if (current)
  259. {
  260. lbitset_elt *elt;
  261. if (windex < bset->b.cindex)
  262. {
  263. for (elt = current;
  264. elt->prev && elt->index > windex; elt = elt->prev)
  265. continue;
  266. }
  267. else
  268. {
  269. for (elt = current;
  270. elt->next && (elt->index + LBITSET_ELT_WORDS - 1) < windex;
  271. elt = elt->next)
  272. continue;
  273. }
  274. /* ELT is the nearest to the one we want. If it's not the one
  275. we want, the one we want does not exist. */
  276. if (windex - elt->index < LBITSET_ELT_WORDS)
  277. {
  278. bset->b.cindex = elt->index;
  279. bset->b.csize = LBITSET_ELT_WORDS;
  280. bset->b.cdata = elt->words;
  281. return elt;
  282. }
  283. }
  284. switch (mode)
  285. {
  286. default:
  287. abort ();
  288. case LBITSET_FIND:
  289. return 0;
  290. case LBITSET_CREATE:
  291. windex -= windex % LBITSET_ELT_WORDS;
  292. lbitset_elt *elt = lbitset_elt_calloc ();
  293. elt->index = windex;
  294. lbitset_elt_link (bset, elt);
  295. return elt;
  296. case LBITSET_SUBST:
  297. return &lbitset_zero_elts[0];
  298. }
  299. }
  300. /* Weed out the zero elements from the list. */
  301. static inline void
  302. lbitset_weed (bitset bset)
  303. {
  304. lbitset_elt *next;
  305. for (lbitset_elt *elt = LBITSET_HEAD (bset); elt; elt = next)
  306. {
  307. next = elt->next;
  308. if (lbitset_elt_zero_p (elt))
  309. lbitset_elt_unlink (bset, elt);
  310. }
  311. }
  312. /* Set all bits in the bitset to zero. */
  313. static void
  314. lbitset_zero (bitset bset)
  315. {
  316. lbitset_elt *head = LBITSET_HEAD (bset);
  317. if (!head)
  318. return;
  319. /* Clear a bitset by freeing the linked list at the head element. */
  320. lbitset_prune (bset, head);
  321. }
  322. /* Is DST == SRC? */
  323. static inline bool
  324. lbitset_equal_p (bitset dst, bitset src)
  325. {
  326. if (src == dst)
  327. return true;
  328. lbitset_weed (src);
  329. lbitset_weed (dst);
  330. lbitset_elt *selt;
  331. lbitset_elt *delt;
  332. for (selt = LBITSET_HEAD (src), delt = LBITSET_HEAD (dst);
  333. selt && delt; selt = selt->next, delt = delt->next)
  334. {
  335. if (selt->index != delt->index)
  336. return false;
  337. for (int j = 0; j < LBITSET_ELT_WORDS; j++)
  338. if (delt->words[j] != selt->words[j])
  339. return false;
  340. }
  341. return !selt && !delt;
  342. }
  343. /* Copy bits from bitset SRC to bitset DST. */
  344. static inline void
  345. lbitset_copy (bitset dst, bitset src)
  346. {
  347. if (src == dst)
  348. return;
  349. lbitset_zero (dst);
  350. lbitset_elt *head = LBITSET_HEAD (src);
  351. if (!head)
  352. return;
  353. lbitset_elt *prev = 0;
  354. lbitset_elt *tmp;
  355. for (lbitset_elt *elt = head; elt; elt = elt->next)
  356. {
  357. tmp = lbitset_elt_alloc ();
  358. tmp->index = elt->index;
  359. tmp->prev = prev;
  360. tmp->next = 0;
  361. if (prev)
  362. prev->next = tmp;
  363. else
  364. LBITSET_HEAD (dst) = tmp;
  365. prev = tmp;
  366. memcpy (tmp->words, elt->words, sizeof (elt->words));
  367. }
  368. LBITSET_TAIL (dst) = tmp;
  369. dst->b.csize = LBITSET_ELT_WORDS;
  370. dst->b.cdata = LBITSET_HEAD (dst)->words;
  371. dst->b.cindex = LBITSET_HEAD (dst)->index;
  372. }
  373. /* Copy bits from bitset SRC to bitset DST. Return true if
  374. bitsets different. */
  375. static inline bool
  376. lbitset_copy_cmp (bitset dst, bitset src)
  377. {
  378. if (src == dst)
  379. return false;
  380. if (!LBITSET_HEAD (dst))
  381. {
  382. lbitset_copy (dst, src);
  383. return LBITSET_HEAD (src) != 0;
  384. }
  385. if (lbitset_equal_p (dst, src))
  386. return false;
  387. lbitset_copy (dst, src);
  388. return true;
  389. }
  390. static bitset_bindex
  391. lbitset_resize (bitset src, bitset_bindex size)
  392. {
  393. BITSET_NBITS_ (src) = size;
  394. /* Need to prune any excess bits. FIXME. */
  395. return size;
  396. }
  397. /* Set bit BITNO in bitset DST. */
  398. static void
  399. lbitset_set (bitset dst, bitset_bindex bitno)
  400. {
  401. bitset_windex windex = bitno / BITSET_WORD_BITS;
  402. lbitset_elt_find (dst, windex, LBITSET_CREATE);
  403. dst->b.cdata[windex - dst->b.cindex] |=
  404. (bitset_word) 1 << (bitno % BITSET_WORD_BITS);
  405. }
  406. /* Reset bit BITNO in bitset DST. */
  407. static void
  408. lbitset_reset (bitset dst, bitset_bindex bitno)
  409. {
  410. bitset_windex windex = bitno / BITSET_WORD_BITS;
  411. if (!lbitset_elt_find (dst, windex, LBITSET_FIND))
  412. return;
  413. dst->b.cdata[windex - dst->b.cindex] &=
  414. ~((bitset_word) 1 << (bitno % BITSET_WORD_BITS));
  415. /* If all the data is zero, perhaps we should unlink it now... */
  416. }
  417. /* Test bit BITNO in bitset SRC. */
  418. static bool
  419. lbitset_test (bitset src, bitset_bindex bitno)
  420. {
  421. bitset_windex windex = bitno / BITSET_WORD_BITS;
  422. return (lbitset_elt_find (src, windex, LBITSET_FIND)
  423. && ((src->b.cdata[windex - src->b.cindex]
  424. >> (bitno % BITSET_WORD_BITS))
  425. & 1));
  426. }
  427. static void
  428. lbitset_free (bitset bset)
  429. {
  430. lbitset_zero (bset);
  431. }
  432. /* Find list of up to NUM bits set in BSET starting from and including
  433. *NEXT and store in array LIST. Return with actual number of bits
  434. found and with *NEXT indicating where search stopped. */
  435. static bitset_bindex
  436. lbitset_list_reverse (bitset bset, bitset_bindex *list,
  437. bitset_bindex num, bitset_bindex *next)
  438. {
  439. lbitset_elt *elt = LBITSET_TAIL (bset);
  440. if (!elt)
  441. return 0;
  442. bitset_bindex n_bits = (elt->index + LBITSET_ELT_WORDS) * BITSET_WORD_BITS;
  443. bitset_bindex rbitno = *next;
  444. if (rbitno >= n_bits)
  445. return 0;
  446. bitset_bindex bitno = n_bits - (rbitno + 1);
  447. bitset_windex windex = bitno / BITSET_WORD_BITS;
  448. /* Skip back to starting element. */
  449. for (; elt && elt->index > windex; elt = elt->prev)
  450. continue;
  451. if (!elt)
  452. return 0;
  453. unsigned bcount;
  454. if (windex >= elt->index + LBITSET_ELT_WORDS)
  455. {
  456. /* We are trying to start in no-mans land so start
  457. at end of current elt. */
  458. bcount = BITSET_WORD_BITS - 1;
  459. windex = elt->index + LBITSET_ELT_WORDS - 1;
  460. }
  461. else
  462. {
  463. bcount = bitno % BITSET_WORD_BITS;
  464. }
  465. bitset_bindex count = 0;
  466. bitset_bindex boffset = windex * BITSET_WORD_BITS;
  467. /* If num is 1, we could speed things up with a binary search
  468. of the word of interest. */
  469. while (elt)
  470. {
  471. bitset_word *srcp = elt->words;
  472. for (; (windex - elt->index) < LBITSET_ELT_WORDS;
  473. windex--, boffset -= BITSET_WORD_BITS,
  474. bcount = BITSET_WORD_BITS - 1)
  475. {
  476. bitset_word word =
  477. srcp[windex - elt->index] << (BITSET_WORD_BITS - 1 - bcount);
  478. for (; word; bcount--)
  479. {
  480. if (word & BITSET_MSB)
  481. {
  482. list[count++] = boffset + bcount;
  483. if (count >= num)
  484. {
  485. *next = n_bits - (boffset + bcount);
  486. return count;
  487. }
  488. }
  489. word <<= 1;
  490. }
  491. }
  492. elt = elt->prev;
  493. if (elt)
  494. {
  495. windex = elt->index + LBITSET_ELT_WORDS - 1;
  496. boffset = windex * BITSET_WORD_BITS;
  497. }
  498. }
  499. *next = n_bits - (boffset + 1);
  500. return count;
  501. }
  502. /* Find list of up to NUM bits set in BSET starting from and including
  503. *NEXT and store in array LIST. Return with actual number of bits
  504. found and with *NEXT indicating where search stopped. */
  505. static bitset_bindex
  506. lbitset_list (bitset bset, bitset_bindex *list,
  507. bitset_bindex num, bitset_bindex *next)
  508. {
  509. lbitset_elt *head = LBITSET_HEAD (bset);
  510. if (!head)
  511. return 0;
  512. bitset_windex windex;
  513. lbitset_elt *elt;
  514. bitset_bindex bitno = *next;
  515. bitset_bindex count = 0;
  516. if (!bitno)
  517. {
  518. /* This is the most common case. */
  519. /* Start with the first element. */
  520. elt = head;
  521. windex = elt->index;
  522. bitno = windex * BITSET_WORD_BITS;
  523. }
  524. else
  525. {
  526. windex = bitno / BITSET_WORD_BITS;
  527. /* Skip to starting element. */
  528. for (elt = head;
  529. elt && (elt->index + LBITSET_ELT_WORDS - 1) < windex;
  530. elt = elt->next)
  531. continue;
  532. if (!elt)
  533. return 0;
  534. if (windex < elt->index)
  535. {
  536. windex = elt->index;
  537. bitno = windex * BITSET_WORD_BITS;
  538. }
  539. else
  540. {
  541. bitset_word *srcp = elt->words;
  542. /* We are starting within an element. */
  543. for (; (windex - elt->index) < LBITSET_ELT_WORDS; windex++)
  544. {
  545. bitset_word word = srcp[windex - elt->index] >> (bitno % BITSET_WORD_BITS);
  546. for (; word; bitno++)
  547. {
  548. if (word & 1)
  549. {
  550. list[count++] = bitno;
  551. if (count >= num)
  552. {
  553. *next = bitno + 1;
  554. return count;
  555. }
  556. }
  557. word >>= 1;
  558. }
  559. bitno = (windex + 1) * BITSET_WORD_BITS;
  560. }
  561. elt = elt->next;
  562. if (elt)
  563. {
  564. windex = elt->index;
  565. bitno = windex * BITSET_WORD_BITS;
  566. }
  567. }
  568. }
  569. /* If num is 1, we could speed things up with a binary search
  570. of the word of interest. */
  571. while (elt)
  572. {
  573. bitset_word *srcp = elt->words;
  574. if ((count + LBITSET_ELT_BITS) < num)
  575. {
  576. /* The coast is clear, plant boot! */
  577. #if LBITSET_ELT_WORDS == 2
  578. bitset_word word = srcp[0];
  579. if (word)
  580. {
  581. if (!(word & 0xffff))
  582. {
  583. word >>= 16;
  584. bitno += 16;
  585. }
  586. if (!(word & 0xff))
  587. {
  588. word >>= 8;
  589. bitno += 8;
  590. }
  591. for (; word; bitno++)
  592. {
  593. if (word & 1)
  594. list[count++] = bitno;
  595. word >>= 1;
  596. }
  597. }
  598. windex++;
  599. bitno = windex * BITSET_WORD_BITS;
  600. word = srcp[1];
  601. if (word)
  602. {
  603. if (!(word & 0xffff))
  604. {
  605. word >>= 16;
  606. bitno += 16;
  607. }
  608. for (; word; bitno++)
  609. {
  610. if (word & 1)
  611. list[count++] = bitno;
  612. word >>= 1;
  613. }
  614. }
  615. windex++;
  616. bitno = windex * BITSET_WORD_BITS;
  617. #else
  618. for (int i = 0; i < LBITSET_ELT_WORDS; i++)
  619. {
  620. bitset_word word = srcp[i];
  621. if (word)
  622. {
  623. if (!(word & 0xffff))
  624. {
  625. word >>= 16;
  626. bitno += 16;
  627. }
  628. if (!(word & 0xff))
  629. {
  630. word >>= 8;
  631. bitno += 8;
  632. }
  633. for (; word; bitno++)
  634. {
  635. if (word & 1)
  636. list[count++] = bitno;
  637. word >>= 1;
  638. }
  639. }
  640. windex++;
  641. bitno = windex * BITSET_WORD_BITS;
  642. }
  643. #endif
  644. }
  645. else
  646. {
  647. /* Tread more carefully since we need to check
  648. if array overflows. */
  649. for (int i = 0; i < LBITSET_ELT_WORDS; i++)
  650. {
  651. for (bitset_word word = srcp[i]; word; bitno++)
  652. {
  653. if (word & 1)
  654. {
  655. list[count++] = bitno;
  656. if (count >= num)
  657. {
  658. *next = bitno + 1;
  659. return count;
  660. }
  661. }
  662. word >>= 1;
  663. }
  664. windex++;
  665. bitno = windex * BITSET_WORD_BITS;
  666. }
  667. }
  668. elt = elt->next;
  669. if (elt)
  670. {
  671. windex = elt->index;
  672. bitno = windex * BITSET_WORD_BITS;
  673. }
  674. }
  675. *next = bitno;
  676. return count;
  677. }
  678. static bool
  679. lbitset_empty_p (bitset dst)
  680. {
  681. lbitset_elt *elt;
  682. lbitset_elt *next;
  683. for (elt = LBITSET_HEAD (dst); elt; elt = next)
  684. {
  685. next = elt->next;
  686. if (!lbitset_elt_zero_p (elt))
  687. return false;
  688. /* Weed as we go. */
  689. lbitset_elt_unlink (dst, elt);
  690. }
  691. return true;
  692. }
  693. /* Ensure that any unused bits within the last element are clear. */
  694. static inline void
  695. lbitset_unused_clear (bitset dst)
  696. {
  697. bitset_bindex n_bits = BITSET_SIZE_ (dst);
  698. unsigned last_bit = n_bits % LBITSET_ELT_BITS;
  699. if (last_bit)
  700. {
  701. lbitset_elt *elt = LBITSET_TAIL (dst);
  702. bitset_word *srcp = elt->words;
  703. bitset_windex windex = n_bits / BITSET_WORD_BITS;
  704. srcp[windex - elt->index]
  705. &= ((bitset_word) 1 << (last_bit % BITSET_WORD_BITS)) - 1;
  706. windex++;
  707. for (; (windex - elt->index) < LBITSET_ELT_WORDS; windex++)
  708. srcp[windex - elt->index] = 0;
  709. }
  710. }
  711. static void
  712. lbitset_ones (bitset dst)
  713. {
  714. /* This is a decidedly unfriendly operation for a linked list
  715. bitset! It makes a sparse bitset become dense. An alternative
  716. is to have a flag that indicates that the bitset stores the
  717. complement of what it indicates. */
  718. bitset_windex windex
  719. = (BITSET_SIZE_ (dst) + BITSET_WORD_BITS - 1) / BITSET_WORD_BITS;
  720. for (bitset_windex i = 0; i < windex; i += LBITSET_ELT_WORDS)
  721. {
  722. /* Create new elements if they cannot be found. */
  723. lbitset_elt *elt = lbitset_elt_find (dst, i, LBITSET_CREATE);
  724. memset (elt->words, -1, sizeof (elt->words));
  725. }
  726. lbitset_unused_clear (dst);
  727. }
  728. static void
  729. lbitset_not (bitset dst, bitset src)
  730. {
  731. bitset_windex windex
  732. = (BITSET_SIZE_ (dst) + BITSET_WORD_BITS - 1) / BITSET_WORD_BITS;
  733. for (bitset_windex i = 0; i < windex; i += LBITSET_ELT_WORDS)
  734. {
  735. /* Create new elements for dst if they cannot be found
  736. or substitute zero elements if src elements not found. */
  737. lbitset_elt *selt = lbitset_elt_find (src, i, LBITSET_SUBST);
  738. lbitset_elt *delt = lbitset_elt_find (dst, i, LBITSET_CREATE);
  739. for (unsigned j = 0; j < LBITSET_ELT_WORDS; j++)
  740. delt->words[j] = ~selt->words[j];
  741. }
  742. lbitset_unused_clear (dst);
  743. lbitset_weed (dst);
  744. }
  745. /* Is DST == DST | SRC? */
  746. static bool
  747. lbitset_subset_p (bitset dst, bitset src)
  748. {
  749. for (lbitset_elt *selt = LBITSET_HEAD (src), *delt = LBITSET_HEAD (dst);
  750. selt || delt; selt = selt->next, delt = delt->next)
  751. {
  752. if (!selt)
  753. selt = &lbitset_zero_elts[0];
  754. else if (!delt)
  755. delt = &lbitset_zero_elts[0];
  756. else if (selt->index != delt->index)
  757. {
  758. if (selt->index < delt->index)
  759. {
  760. lbitset_zero_elts[2].next = delt;
  761. delt = &lbitset_zero_elts[2];
  762. }
  763. else
  764. {
  765. lbitset_zero_elts[1].next = selt;
  766. selt = &lbitset_zero_elts[1];
  767. }
  768. }
  769. for (unsigned j = 0; j < LBITSET_ELT_WORDS; j++)
  770. if (delt->words[j] != (selt->words[j] | delt->words[j]))
  771. return false;
  772. }
  773. return true;
  774. }
  775. /* Is DST & SRC == 0? */
  776. static bool
  777. lbitset_disjoint_p (bitset dst, bitset src)
  778. {
  779. for (lbitset_elt *selt = LBITSET_HEAD (src), *delt = LBITSET_HEAD (dst);
  780. selt && delt; selt = selt->next, delt = delt->next)
  781. {
  782. if (selt->index != delt->index)
  783. {
  784. if (selt->index < delt->index)
  785. {
  786. lbitset_zero_elts[2].next = delt;
  787. delt = &lbitset_zero_elts[2];
  788. }
  789. else
  790. {
  791. lbitset_zero_elts[1].next = selt;
  792. selt = &lbitset_zero_elts[1];
  793. }
  794. /* Since the elements are different, there is no
  795. intersection of these elements. */
  796. continue;
  797. }
  798. for (unsigned j = 0; j < LBITSET_ELT_WORDS; j++)
  799. if (selt->words[j] & delt->words[j])
  800. return false;
  801. }
  802. return true;
  803. }
  804. static bool
  805. lbitset_op3_cmp (bitset dst, bitset src1, bitset src2, enum bitset_ops op)
  806. {
  807. lbitset_elt *selt1 = LBITSET_HEAD (src1);
  808. lbitset_elt *selt2 = LBITSET_HEAD (src2);
  809. lbitset_elt *delt = LBITSET_HEAD (dst);
  810. bool changed = false;
  811. LBITSET_HEAD (dst) = 0;
  812. dst->b.csize = 0;
  813. bitset_windex windex1 = (selt1) ? selt1->index : BITSET_WINDEX_MAX;
  814. bitset_windex windex2 = (selt2) ? selt2->index : BITSET_WINDEX_MAX;
  815. while (selt1 || selt2)
  816. {
  817. bitset_windex windex;
  818. lbitset_elt *stmp1;
  819. lbitset_elt *stmp2;
  820. /* Figure out whether we need to substitute zero elements for
  821. missing links. */
  822. if (windex1 == windex2)
  823. {
  824. windex = windex1;
  825. stmp1 = selt1;
  826. stmp2 = selt2;
  827. selt1 = selt1->next;
  828. windex1 = (selt1) ? selt1->index : BITSET_WINDEX_MAX;
  829. selt2 = selt2->next;
  830. windex2 = (selt2) ? selt2->index : BITSET_WINDEX_MAX;
  831. }
  832. else if (windex1 < windex2)
  833. {
  834. windex = windex1;
  835. stmp1 = selt1;
  836. stmp2 = &lbitset_zero_elts[0];
  837. selt1 = selt1->next;
  838. windex1 = (selt1) ? selt1->index : BITSET_WINDEX_MAX;
  839. }
  840. else
  841. {
  842. windex = windex2;
  843. stmp1 = &lbitset_zero_elts[0];
  844. stmp2 = selt2;
  845. selt2 = selt2->next;
  846. windex2 = (selt2) ? selt2->index : BITSET_WINDEX_MAX;
  847. }
  848. /* Find the appropriate element from DST. Begin by discarding
  849. elements that we've skipped. */
  850. lbitset_elt *dtmp;
  851. while (delt && delt->index < windex)
  852. {
  853. changed = true;
  854. dtmp = delt;
  855. delt = delt->next;
  856. lbitset_elt_free (dtmp);
  857. }
  858. if (delt && delt->index == windex)
  859. {
  860. dtmp = delt;
  861. delt = delt->next;
  862. }
  863. else
  864. dtmp = lbitset_elt_calloc ();
  865. /* Do the operation, and if any bits are set, link it into the
  866. linked list. */
  867. bitset_word *srcp1 = stmp1->words;
  868. bitset_word *srcp2 = stmp2->words;
  869. bitset_word *dstp = dtmp->words;
  870. switch (op)
  871. {
  872. default:
  873. abort ();
  874. case BITSET_OP_OR:
  875. for (unsigned i = 0; i < LBITSET_ELT_WORDS; i++, dstp++)
  876. {
  877. bitset_word tmp = *srcp1++ | *srcp2++;
  878. if (*dstp != tmp)
  879. {
  880. changed = true;
  881. *dstp = tmp;
  882. }
  883. }
  884. break;
  885. case BITSET_OP_AND:
  886. for (unsigned i = 0; i < LBITSET_ELT_WORDS; i++, dstp++)
  887. {
  888. bitset_word tmp = *srcp1++ & *srcp2++;
  889. if (*dstp != tmp)
  890. {
  891. changed = true;
  892. *dstp = tmp;
  893. }
  894. }
  895. break;
  896. case BITSET_OP_XOR:
  897. for (unsigned i = 0; i < LBITSET_ELT_WORDS; i++, dstp++)
  898. {
  899. bitset_word tmp = *srcp1++ ^ *srcp2++;
  900. if (*dstp != tmp)
  901. {
  902. changed = true;
  903. *dstp = tmp;
  904. }
  905. }
  906. break;
  907. case BITSET_OP_ANDN:
  908. for (unsigned i = 0; i < LBITSET_ELT_WORDS; i++, dstp++)
  909. {
  910. bitset_word tmp = *srcp1++ & ~(*srcp2++);
  911. if (*dstp != tmp)
  912. {
  913. changed = true;
  914. *dstp = tmp;
  915. }
  916. }
  917. break;
  918. }
  919. if (!lbitset_elt_zero_p (dtmp))
  920. {
  921. dtmp->index = windex;
  922. /* Perhaps this could be optimised... */
  923. lbitset_elt_link (dst, dtmp);
  924. }
  925. else
  926. {
  927. lbitset_elt_free (dtmp);
  928. }
  929. }
  930. /* If we have elements of DST left over, free them all. */
  931. if (delt)
  932. {
  933. changed = true;
  934. lbitset_prune (dst, delt);
  935. }
  936. return changed;
  937. }
  938. static bool
  939. lbitset_and_cmp (bitset dst, bitset src1, bitset src2)
  940. {
  941. lbitset_elt *selt1 = LBITSET_HEAD (src1);
  942. lbitset_elt *selt2 = LBITSET_HEAD (src2);
  943. if (!selt2)
  944. {
  945. lbitset_weed (dst);
  946. bool changed = !LBITSET_HEAD (dst);
  947. lbitset_zero (dst);
  948. return changed;
  949. }
  950. else if (!selt1)
  951. {
  952. lbitset_weed (dst);
  953. bool changed = !LBITSET_HEAD (dst);
  954. lbitset_zero (dst);
  955. return changed;
  956. }
  957. else
  958. return lbitset_op3_cmp (dst, src1, src2, BITSET_OP_AND);
  959. }
  960. static void
  961. lbitset_and (bitset dst, bitset src1, bitset src2)
  962. {
  963. lbitset_and_cmp (dst, src1, src2);
  964. }
  965. static bool
  966. lbitset_andn_cmp (bitset dst, bitset src1, bitset src2)
  967. {
  968. lbitset_elt *selt1 = LBITSET_HEAD (src1);
  969. lbitset_elt *selt2 = LBITSET_HEAD (src2);
  970. if (!selt2)
  971. {
  972. return lbitset_copy_cmp (dst, src1);
  973. }
  974. else if (!selt1)
  975. {
  976. lbitset_weed (dst);
  977. bool changed = !LBITSET_HEAD (dst);
  978. lbitset_zero (dst);
  979. return changed;
  980. }
  981. else
  982. return lbitset_op3_cmp (dst, src1, src2, BITSET_OP_ANDN);
  983. }
  984. static void
  985. lbitset_andn (bitset dst, bitset src1, bitset src2)
  986. {
  987. lbitset_andn_cmp (dst, src1, src2);
  988. }
  989. static bool
  990. lbitset_or_cmp (bitset dst, bitset src1, bitset src2)
  991. {
  992. lbitset_elt *selt1 = LBITSET_HEAD (src1);
  993. lbitset_elt *selt2 = LBITSET_HEAD (src2);
  994. if (!selt2)
  995. return lbitset_copy_cmp (dst, src1);
  996. else if (!selt1)
  997. return lbitset_copy_cmp (dst, src2);
  998. else
  999. return lbitset_op3_cmp (dst, src1, src2, BITSET_OP_OR);
  1000. }
  1001. static void
  1002. lbitset_or (bitset dst, bitset src1, bitset src2)
  1003. {
  1004. lbitset_or_cmp (dst, src1, src2);
  1005. }
  1006. static bool
  1007. lbitset_xor_cmp (bitset dst, bitset src1, bitset src2)
  1008. {
  1009. lbitset_elt *selt1 = LBITSET_HEAD (src1);
  1010. lbitset_elt *selt2 = LBITSET_HEAD (src2);
  1011. if (!selt2)
  1012. return lbitset_copy_cmp (dst, src1);
  1013. else if (!selt1)
  1014. return lbitset_copy_cmp (dst, src2);
  1015. else
  1016. return lbitset_op3_cmp (dst, src1, src2, BITSET_OP_XOR);
  1017. }
  1018. static void
  1019. lbitset_xor (bitset dst, bitset src1, bitset src2)
  1020. {
  1021. lbitset_xor_cmp (dst, src1, src2);
  1022. }
  1023. /* Vector of operations for linked-list bitsets. */
  1024. struct bitset_vtable lbitset_vtable = {
  1025. lbitset_set,
  1026. lbitset_reset,
  1027. bitset_toggle_,
  1028. lbitset_test,
  1029. lbitset_resize,
  1030. bitset_size_,
  1031. bitset_count_,
  1032. lbitset_empty_p,
  1033. lbitset_ones,
  1034. lbitset_zero,
  1035. lbitset_copy,
  1036. lbitset_disjoint_p,
  1037. lbitset_equal_p,
  1038. lbitset_not,
  1039. lbitset_subset_p,
  1040. lbitset_and,
  1041. lbitset_and_cmp,
  1042. lbitset_andn,
  1043. lbitset_andn_cmp,
  1044. lbitset_or,
  1045. lbitset_or_cmp,
  1046. lbitset_xor,
  1047. lbitset_xor_cmp,
  1048. bitset_and_or_,
  1049. bitset_and_or_cmp_,
  1050. bitset_andn_or_,
  1051. bitset_andn_or_cmp_,
  1052. bitset_or_and_,
  1053. bitset_or_and_cmp_,
  1054. lbitset_list,
  1055. lbitset_list_reverse,
  1056. lbitset_free,
  1057. BITSET_LIST
  1058. };
  1059. /* Return size of initial structure. */
  1060. size_t
  1061. lbitset_bytes (bitset_bindex n_bits MAYBE_UNUSED)
  1062. {
  1063. return sizeof (struct lbitset_struct);
  1064. }
  1065. /* Initialize a bitset. */
  1066. bitset
  1067. lbitset_init (bitset bset, bitset_bindex n_bits MAYBE_UNUSED)
  1068. {
  1069. BITSET_NBITS_ (bset) = n_bits;
  1070. bset->b.vtable = &lbitset_vtable;
  1071. return bset;
  1072. }
  1073. void
  1074. lbitset_release_memory (void)
  1075. {
  1076. lbitset_free_list = 0;
  1077. if (lbitset_obstack_init)
  1078. {
  1079. lbitset_obstack_init = false;
  1080. obstack_free (&lbitset_obstack, NULL);
  1081. }
  1082. }
  1083. /* Function to be called from debugger to debug lbitset. */
  1084. void
  1085. debug_lbitset (bitset bset)
  1086. {
  1087. if (!bset)
  1088. return;
  1089. for (lbitset_elt *elt = LBITSET_HEAD (bset); elt; elt = elt->next)
  1090. {
  1091. fprintf (stderr, "Elt %lu\n", (unsigned long) elt->index);
  1092. for (unsigned i = 0; i < LBITSET_ELT_WORDS; i++)
  1093. {
  1094. bitset_word word = elt->words[i];
  1095. fprintf (stderr, " Word %u:", i);
  1096. for (unsigned j = 0; j < LBITSET_WORD_BITS; j++)
  1097. if ((word & ((bitset_word) 1 << j)))
  1098. fprintf (stderr, " %u", j);
  1099. fprintf (stderr, "\n");
  1100. }
  1101. }
  1102. }