lbitset.c 32 KB

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