table.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. /* Functions to support expandable bitsets.
  2. Copyright (C) 2002-2006, 2009-2015, 2018-2020 Free Software Foundation, Inc.
  3. Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. #include "bitset/table.h"
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "obstack.h"
  19. #include "xalloc.h"
  20. /* This file implements expandable bitsets. These bitsets can be of
  21. arbitrary length and are more efficient than arrays of bits for
  22. large sparse sets.
  23. Empty elements are represented by a NULL pointer in the table of
  24. element pointers. An alternative is to point to a special zero
  25. element. Similarly, we could represent an all 1's element with
  26. another special ones element pointer.
  27. Bitsets are commonly empty so we need to ensure that this special
  28. case is fast. A zero bitset is indicated when cdata is 0. This is
  29. conservative since cdata may be non zero and the bitset may still
  30. be zero.
  31. The bitset cache can be disabled either by setting cindex to
  32. BITSET_WINDEX_MAX or by setting csize to 0. Here
  33. we use the former approach since cindex needs to be updated whenever
  34. cdata is changed.
  35. */
  36. /* Number of words to use for each element. */
  37. #define EBITSET_ELT_WORDS 2
  38. /* Number of bits stored in each element. */
  39. #define EBITSET_ELT_BITS \
  40. ((unsigned) (EBITSET_ELT_WORDS * BITSET_WORD_BITS))
  41. /* Ebitset element. We use an array of bits. */
  42. typedef struct tbitset_elt_struct
  43. {
  44. union
  45. {
  46. bitset_word words[EBITSET_ELT_WORDS]; /* Bits that are set. */
  47. struct tbitset_elt_struct *next;
  48. }
  49. u;
  50. }
  51. tbitset_elt;
  52. typedef tbitset_elt *tbitset_elts;
  53. /* Number of elements to initially allocate. */
  54. #ifndef EBITSET_INITIAL_SIZE
  55. # define EBITSET_INITIAL_SIZE 2
  56. #endif
  57. enum tbitset_find_mode
  58. { EBITSET_FIND, EBITSET_CREATE, EBITSET_SUBST };
  59. static tbitset_elt tbitset_zero_elts[1]; /* Elements of all zero bits. */
  60. /* Obstack to allocate bitset elements from. */
  61. static struct obstack tbitset_obstack;
  62. static bool tbitset_obstack_init = false;
  63. static tbitset_elt *tbitset_free_list; /* Free list of bitset elements. */
  64. #define EBITSET_N_ELTS(N) (((N) + EBITSET_ELT_BITS - 1) / EBITSET_ELT_BITS)
  65. #define EBITSET_ELTS(BSET) ((BSET)->e.elts)
  66. #define EBITSET_SIZE(BSET) EBITSET_N_ELTS (BITSET_NBITS_ (BSET))
  67. #define EBITSET_ASIZE(BSET) ((BSET)->e.size)
  68. #define EBITSET_NEXT(ELT) ((ELT)->u.next)
  69. #define EBITSET_WORDS(ELT) ((ELT)->u.words)
  70. /* Disable bitset cache and mark BSET as being zero. */
  71. #define EBITSET_ZERO_SET(BSET) ((BSET)->b.cindex = BITSET_WINDEX_MAX, \
  72. (BSET)->b.cdata = 0)
  73. #define EBITSET_CACHE_DISABLE(BSET) ((BSET)->b.cindex = BITSET_WINDEX_MAX)
  74. /* Disable bitset cache and mark BSET as being possibly non-zero. */
  75. #define EBITSET_NONZERO_SET(BSET) \
  76. (EBITSET_CACHE_DISABLE (BSET), (BSET)->b.cdata = (bitset_word *)~0)
  77. /* A conservative estimate of whether the bitset is zero.
  78. This is non-zero only if we know for sure that the bitset is zero. */
  79. #define EBITSET_ZERO_P(BSET) ((BSET)->b.cdata == 0)
  80. /* Enable cache to point to element with table index EINDEX.
  81. The element must exist. */
  82. #define EBITSET_CACHE_SET(BSET, EINDEX) \
  83. ((BSET)->b.cindex = (EINDEX) * EBITSET_ELT_WORDS, \
  84. (BSET)->b.cdata = EBITSET_WORDS (EBITSET_ELTS (BSET) [EINDEX]))
  85. #undef min
  86. #undef max
  87. #define min(a, b) ((a) > (b) ? (b) : (a))
  88. #define max(a, b) ((a) > (b) ? (a) : (b))
  89. static bitset_bindex
  90. tbitset_resize (bitset src, bitset_bindex n_bits)
  91. {
  92. if (n_bits == BITSET_NBITS_ (src))
  93. return n_bits;
  94. bitset_windex oldsize = EBITSET_SIZE (src);
  95. bitset_windex newsize = EBITSET_N_ELTS (n_bits);
  96. if (oldsize < newsize)
  97. {
  98. /* The bitset needs to grow. If we already have enough memory
  99. allocated, then just zero what we need. */
  100. if (newsize > EBITSET_ASIZE (src))
  101. {
  102. /* We need to allocate more memory. When oldsize is
  103. non-zero this means that we are changing the size, so
  104. grow the bitset 25% larger than requested to reduce
  105. number of reallocations. */
  106. bitset_windex size = oldsize == 0 ? newsize : newsize + newsize / 4;
  107. EBITSET_ELTS (src)
  108. = xrealloc (EBITSET_ELTS (src), size * sizeof (tbitset_elt *));
  109. EBITSET_ASIZE (src) = size;
  110. }
  111. memset (EBITSET_ELTS (src) + oldsize, 0,
  112. (newsize - oldsize) * sizeof (tbitset_elt *));
  113. }
  114. else
  115. {
  116. /* The bitset needs to shrink. There's no point deallocating
  117. the memory unless it is shrinking by a reasonable amount. */
  118. if ((oldsize - newsize) >= oldsize / 2)
  119. {
  120. void *p
  121. = realloc (EBITSET_ELTS (src), newsize * sizeof (tbitset_elt *));
  122. if (p)
  123. {
  124. EBITSET_ELTS (src) = p;
  125. EBITSET_ASIZE (src) = newsize;
  126. }
  127. }
  128. /* Need to prune any excess bits. FIXME. */
  129. }
  130. BITSET_NBITS_ (src) = n_bits;
  131. return n_bits;
  132. }
  133. /* Allocate a tbitset element. The bits are not cleared. */
  134. static inline tbitset_elt *
  135. tbitset_elt_alloc (void)
  136. {
  137. tbitset_elt *elt;
  138. if (tbitset_free_list != 0)
  139. {
  140. elt = tbitset_free_list;
  141. tbitset_free_list = EBITSET_NEXT (elt);
  142. }
  143. else
  144. {
  145. if (!tbitset_obstack_init)
  146. {
  147. tbitset_obstack_init = true;
  148. /* Let particular systems override the size of a chunk. */
  149. #ifndef OBSTACK_CHUNK_SIZE
  150. # define OBSTACK_CHUNK_SIZE 0
  151. #endif
  152. /* Let them override the alloc and free routines too. */
  153. #ifndef OBSTACK_CHUNK_ALLOC
  154. # define OBSTACK_CHUNK_ALLOC xmalloc
  155. #endif
  156. #ifndef OBSTACK_CHUNK_FREE
  157. # define OBSTACK_CHUNK_FREE free
  158. #endif
  159. #if !(defined __GNUC__ || defined __clang__)
  160. # define __alignof__(type) 0
  161. #endif
  162. obstack_specify_allocation (&tbitset_obstack, OBSTACK_CHUNK_SIZE,
  163. __alignof__ (tbitset_elt),
  164. OBSTACK_CHUNK_ALLOC,
  165. OBSTACK_CHUNK_FREE);
  166. }
  167. /* Perhaps we should add a number of new elements to the free
  168. list. */
  169. elt = (tbitset_elt *) obstack_alloc (&tbitset_obstack,
  170. sizeof (tbitset_elt));
  171. }
  172. return elt;
  173. }
  174. /* Allocate a tbitset element. The bits are cleared. */
  175. static inline tbitset_elt *
  176. tbitset_elt_calloc (void)
  177. {
  178. tbitset_elt *elt = tbitset_elt_alloc ();
  179. memset (EBITSET_WORDS (elt), 0, sizeof (EBITSET_WORDS (elt)));
  180. return elt;
  181. }
  182. static inline void
  183. tbitset_elt_free (tbitset_elt *elt)
  184. {
  185. EBITSET_NEXT (elt) = tbitset_free_list;
  186. tbitset_free_list = elt;
  187. }
  188. /* Remove element with index EINDEX from bitset BSET. */
  189. static inline void
  190. tbitset_elt_remove (bitset bset, bitset_windex eindex)
  191. {
  192. tbitset_elts *elts = EBITSET_ELTS (bset);
  193. tbitset_elt *elt = elts[eindex];
  194. elts[eindex] = 0;
  195. tbitset_elt_free (elt);
  196. }
  197. /* Add ELT into elts at index EINDEX of bitset BSET. */
  198. static inline void
  199. tbitset_elt_add (bitset bset, tbitset_elt *elt, bitset_windex eindex)
  200. {
  201. tbitset_elts *elts = EBITSET_ELTS (bset);
  202. /* Assume that the elts entry not allocated. */
  203. elts[eindex] = elt;
  204. }
  205. /* Are all bits in an element zero? */
  206. static inline bool
  207. tbitset_elt_zero_p (tbitset_elt *elt)
  208. {
  209. for (int i = 0; i < EBITSET_ELT_WORDS; i++)
  210. if (EBITSET_WORDS (elt)[i])
  211. return false;
  212. return true;
  213. }
  214. static tbitset_elt *
  215. tbitset_elt_find (bitset bset, bitset_bindex bindex,
  216. enum tbitset_find_mode mode)
  217. {
  218. bitset_windex eindex = bindex / EBITSET_ELT_BITS;
  219. tbitset_elts *elts = EBITSET_ELTS (bset);
  220. bitset_windex size = EBITSET_SIZE (bset);
  221. if (eindex < size)
  222. {
  223. tbitset_elt *elt = elts[eindex];
  224. if (elt)
  225. {
  226. if (EBITSET_WORDS (elt) != bset->b.cdata)
  227. EBITSET_CACHE_SET (bset, eindex);
  228. return elt;
  229. }
  230. }
  231. /* The element could not be found. */
  232. switch (mode)
  233. {
  234. default:
  235. abort ();
  236. case EBITSET_FIND:
  237. return NULL;
  238. case EBITSET_CREATE:
  239. if (eindex >= size)
  240. tbitset_resize (bset, bindex);
  241. /* Create a new element. */
  242. {
  243. tbitset_elt *elt = tbitset_elt_calloc ();
  244. tbitset_elt_add (bset, elt, eindex);
  245. EBITSET_CACHE_SET (bset, eindex);
  246. return elt;
  247. }
  248. case EBITSET_SUBST:
  249. return &tbitset_zero_elts[0];
  250. }
  251. }
  252. /* Weed out the zero elements from the elts. */
  253. static inline bitset_windex
  254. tbitset_weed (bitset bset)
  255. {
  256. if (EBITSET_ZERO_P (bset))
  257. return 0;
  258. tbitset_elts *elts = EBITSET_ELTS (bset);
  259. bitset_windex count = 0;
  260. bitset_windex j;
  261. for (j = 0; j < EBITSET_SIZE (bset); j++)
  262. {
  263. tbitset_elt *elt = elts[j];
  264. if (elt)
  265. {
  266. if (tbitset_elt_zero_p (elt))
  267. {
  268. tbitset_elt_remove (bset, j);
  269. count++;
  270. }
  271. }
  272. else
  273. count++;
  274. }
  275. count = j - count;
  276. if (!count)
  277. {
  278. /* All the bits are zero. We could shrink the elts.
  279. For now just mark BSET as known to be zero. */
  280. EBITSET_ZERO_SET (bset);
  281. }
  282. else
  283. EBITSET_NONZERO_SET (bset);
  284. return count;
  285. }
  286. /* Set all bits in the bitset to zero. */
  287. static inline void
  288. tbitset_zero (bitset bset)
  289. {
  290. if (EBITSET_ZERO_P (bset))
  291. return;
  292. tbitset_elts *elts = EBITSET_ELTS (bset);
  293. for (bitset_windex j = 0; j < EBITSET_SIZE (bset); j++)
  294. {
  295. tbitset_elt *elt = elts[j];
  296. if (elt)
  297. tbitset_elt_remove (bset, j);
  298. }
  299. /* All the bits are zero. We could shrink the elts.
  300. For now just mark BSET as known to be zero. */
  301. EBITSET_ZERO_SET (bset);
  302. }
  303. static inline bool
  304. tbitset_equal_p (bitset dst, bitset src)
  305. {
  306. if (src == dst)
  307. return true;
  308. tbitset_weed (dst);
  309. tbitset_weed (src);
  310. if (EBITSET_SIZE (src) != EBITSET_SIZE (dst))
  311. return false;
  312. tbitset_elts *selts = EBITSET_ELTS (src);
  313. tbitset_elts *delts = EBITSET_ELTS (dst);
  314. for (bitset_windex j = 0; j < EBITSET_SIZE (src); j++)
  315. {
  316. tbitset_elt *selt = selts[j];
  317. tbitset_elt *delt = delts[j];
  318. if (!selt && !delt)
  319. continue;
  320. if ((selt && !delt) || (!selt && delt))
  321. return false;
  322. for (unsigned i = 0; i < EBITSET_ELT_WORDS; i++)
  323. if (EBITSET_WORDS (selt)[i] != EBITSET_WORDS (delt)[i])
  324. return false;
  325. }
  326. return true;
  327. }
  328. /* Copy bits from bitset SRC to bitset DST. */
  329. static inline void
  330. tbitset_copy_ (bitset dst, bitset src)
  331. {
  332. if (src == dst)
  333. return;
  334. tbitset_zero (dst);
  335. if (BITSET_NBITS_ (dst) != BITSET_NBITS_ (src))
  336. tbitset_resize (dst, BITSET_NBITS_ (src));
  337. tbitset_elts *selts = EBITSET_ELTS (src);
  338. tbitset_elts *delts = EBITSET_ELTS (dst);
  339. for (bitset_windex j = 0; j < EBITSET_SIZE (src); j++)
  340. {
  341. tbitset_elt *selt = selts[j];
  342. if (selt)
  343. {
  344. tbitset_elt *tmp = tbitset_elt_alloc ();
  345. delts[j] = tmp;
  346. memcpy (EBITSET_WORDS (tmp), EBITSET_WORDS (selt),
  347. sizeof (EBITSET_WORDS (selt)));
  348. }
  349. }
  350. EBITSET_NONZERO_SET (dst);
  351. }
  352. /* Copy bits from bitset SRC to bitset DST. Return true if
  353. bitsets different. */
  354. static inline bool
  355. tbitset_copy_cmp (bitset dst, bitset src)
  356. {
  357. if (src == dst)
  358. return false;
  359. if (EBITSET_ZERO_P (dst))
  360. {
  361. tbitset_copy_ (dst, src);
  362. return !EBITSET_ZERO_P (src);
  363. }
  364. if (tbitset_equal_p (dst, src))
  365. return false;
  366. tbitset_copy_ (dst, src);
  367. return true;
  368. }
  369. /* Set bit BITNO in bitset DST. */
  370. static void
  371. tbitset_set (bitset dst, bitset_bindex bitno)
  372. {
  373. bitset_windex windex = bitno / BITSET_WORD_BITS;
  374. tbitset_elt_find (dst, bitno, EBITSET_CREATE);
  375. dst->b.cdata[windex - dst->b.cindex] |=
  376. (bitset_word) 1 << (bitno % BITSET_WORD_BITS);
  377. }
  378. /* Reset bit BITNO in bitset DST. */
  379. static void
  380. tbitset_reset (bitset dst, bitset_bindex bitno)
  381. {
  382. bitset_windex windex = bitno / BITSET_WORD_BITS;
  383. if (!tbitset_elt_find (dst, bitno, EBITSET_FIND))
  384. return;
  385. dst->b.cdata[windex - dst->b.cindex] &=
  386. ~((bitset_word) 1 << (bitno % BITSET_WORD_BITS));
  387. /* If all the data is zero, perhaps we should remove it now...
  388. However, there is a good chance that the element will be needed
  389. again soon. */
  390. }
  391. /* Test bit BITNO in bitset SRC. */
  392. static bool
  393. tbitset_test (bitset src, bitset_bindex bitno)
  394. {
  395. bitset_windex windex = bitno / BITSET_WORD_BITS;
  396. return (tbitset_elt_find (src, bitno, EBITSET_FIND)
  397. && ((src->b.cdata[windex - src->b.cindex]
  398. >> (bitno % BITSET_WORD_BITS))
  399. & 1));
  400. }
  401. static void
  402. tbitset_free (bitset bset)
  403. {
  404. tbitset_zero (bset);
  405. free (EBITSET_ELTS (bset));
  406. }
  407. /* Find list of up to NUM bits set in BSET starting from and including
  408. *NEXT and store in array LIST. Return with actual number of bits
  409. found and with *NEXT indicating where search stopped. */
  410. static bitset_bindex
  411. tbitset_list_reverse (bitset bset, bitset_bindex *list,
  412. bitset_bindex num, bitset_bindex *next)
  413. {
  414. if (EBITSET_ZERO_P (bset))
  415. return 0;
  416. bitset_windex size = EBITSET_SIZE (bset);
  417. bitset_bindex n_bits = size * EBITSET_ELT_BITS;
  418. bitset_bindex rbitno = *next;
  419. if (rbitno >= n_bits)
  420. return 0;
  421. tbitset_elts *elts = EBITSET_ELTS (bset);
  422. bitset_bindex bitno = n_bits - (rbitno + 1);
  423. bitset_windex windex = bitno / BITSET_WORD_BITS;
  424. bitset_windex eindex = bitno / EBITSET_ELT_BITS;
  425. bitset_windex woffset = windex - eindex * EBITSET_ELT_WORDS;
  426. /* If num is 1, we could speed things up with a binary search
  427. of the word of interest. */
  428. bitset_bindex count = 0;
  429. unsigned bcount = bitno % BITSET_WORD_BITS;
  430. bitset_bindex boffset = windex * BITSET_WORD_BITS;
  431. do
  432. {
  433. tbitset_elt *elt = elts[eindex];
  434. if (elt)
  435. {
  436. bitset_word *srcp = EBITSET_WORDS (elt);
  437. do
  438. {
  439. for (bitset_word word = srcp[woffset] << (BITSET_WORD_BITS - 1 - bcount);
  440. word; bcount--)
  441. {
  442. if (word & BITSET_MSB)
  443. {
  444. list[count++] = boffset + bcount;
  445. if (count >= num)
  446. {
  447. *next = n_bits - (boffset + bcount);
  448. return count;
  449. }
  450. }
  451. word <<= 1;
  452. }
  453. boffset -= BITSET_WORD_BITS;
  454. bcount = BITSET_WORD_BITS - 1;
  455. }
  456. while (woffset--);
  457. }
  458. woffset = EBITSET_ELT_WORDS - 1;
  459. boffset = eindex * EBITSET_ELT_BITS - BITSET_WORD_BITS;
  460. }
  461. while (eindex--);
  462. *next = n_bits - (boffset + 1);
  463. return count;
  464. }
  465. /* Find list of up to NUM bits set in BSET starting from and including
  466. *NEXT and store in array LIST. Return with actual number of bits
  467. found and with *NEXT indicating where search stopped. */
  468. static bitset_bindex
  469. tbitset_list (bitset bset, bitset_bindex *list,
  470. bitset_bindex num, bitset_bindex *next)
  471. {
  472. if (EBITSET_ZERO_P (bset))
  473. return 0;
  474. bitset_bindex bitno = *next;
  475. bitset_bindex count = 0;
  476. tbitset_elts *elts = EBITSET_ELTS (bset);
  477. bitset_windex size = EBITSET_SIZE (bset);
  478. bitset_windex eindex = bitno / EBITSET_ELT_BITS;
  479. if (bitno % EBITSET_ELT_BITS)
  480. {
  481. /* We need to start within an element. This is not very common. */
  482. tbitset_elt *elt = elts[eindex];
  483. if (elt)
  484. {
  485. bitset_word *srcp = EBITSET_WORDS (elt);
  486. bitset_windex woffset = eindex * EBITSET_ELT_WORDS;
  487. for (bitset_windex windex = bitno / BITSET_WORD_BITS;
  488. (windex - woffset) < EBITSET_ELT_WORDS; windex++)
  489. {
  490. bitset_word word = srcp[windex - woffset] >> (bitno % BITSET_WORD_BITS);
  491. for (; word; bitno++)
  492. {
  493. if (word & 1)
  494. {
  495. list[count++] = bitno;
  496. if (count >= num)
  497. {
  498. *next = bitno + 1;
  499. return count;
  500. }
  501. }
  502. word >>= 1;
  503. }
  504. bitno = (windex + 1) * BITSET_WORD_BITS;
  505. }
  506. }
  507. /* Skip to next element. */
  508. eindex++;
  509. }
  510. /* If num is 1, we could speed things up with a binary search
  511. of the word of interest. */
  512. for (; eindex < size; eindex++)
  513. {
  514. bitset_word *srcp;
  515. tbitset_elt *elt = elts[eindex];
  516. if (!elt)
  517. continue;
  518. srcp = EBITSET_WORDS (elt);
  519. bitset_windex windex = eindex * EBITSET_ELT_WORDS;
  520. if ((count + EBITSET_ELT_BITS) < num)
  521. {
  522. /* The coast is clear, plant boot! */
  523. #if EBITSET_ELT_WORDS == 2
  524. bitset_word word = srcp[0];
  525. if (word)
  526. {
  527. if (!(word & 0xffff))
  528. {
  529. word >>= 16;
  530. bitno += 16;
  531. }
  532. if (!(word & 0xff))
  533. {
  534. word >>= 8;
  535. bitno += 8;
  536. }
  537. for (; word; bitno++)
  538. {
  539. if (word & 1)
  540. list[count++] = bitno;
  541. word >>= 1;
  542. }
  543. }
  544. windex++;
  545. bitno = windex * BITSET_WORD_BITS;
  546. word = srcp[1];
  547. if (word)
  548. {
  549. if (!(word & 0xffff))
  550. {
  551. word >>= 16;
  552. bitno += 16;
  553. }
  554. for (; word; bitno++)
  555. {
  556. if (word & 1)
  557. list[count++] = bitno;
  558. word >>= 1;
  559. }
  560. }
  561. windex++;
  562. bitno = windex * BITSET_WORD_BITS;
  563. #else
  564. for (int i = 0; i < EBITSET_ELT_WORDS; i++, windex++)
  565. {
  566. bitno = windex * BITSET_WORD_BITS;
  567. word = srcp[i];
  568. if (word)
  569. {
  570. if (!(word & 0xffff))
  571. {
  572. word >>= 16;
  573. bitno += 16;
  574. }
  575. if (!(word & 0xff))
  576. {
  577. word >>= 8;
  578. bitno += 8;
  579. }
  580. for (; word; bitno++)
  581. {
  582. if (word & 1)
  583. list[count++] = bitno;
  584. word >>= 1;
  585. }
  586. }
  587. }
  588. #endif
  589. }
  590. else
  591. {
  592. /* Tread more carefully since we need to check
  593. if array overflows. */
  594. for (int i = 0; i < EBITSET_ELT_WORDS; i++, windex++)
  595. {
  596. bitno = windex * BITSET_WORD_BITS;
  597. for (bitset_word word = srcp[i]; word; bitno++)
  598. {
  599. if (word & 1)
  600. {
  601. list[count++] = bitno;
  602. if (count >= num)
  603. {
  604. *next = bitno + 1;
  605. return count;
  606. }
  607. }
  608. word >>= 1;
  609. }
  610. }
  611. }
  612. }
  613. *next = bitno;
  614. return count;
  615. }
  616. /* Ensure that any unused bits within the last element are clear. */
  617. static inline void
  618. tbitset_unused_clear (bitset dst)
  619. {
  620. bitset_bindex n_bits = BITSET_NBITS_ (dst);
  621. unsigned last_bit = n_bits % EBITSET_ELT_BITS;
  622. if (last_bit)
  623. {
  624. tbitset_elts *elts = EBITSET_ELTS (dst);
  625. bitset_windex eindex = n_bits / EBITSET_ELT_BITS;
  626. tbitset_elt *elt = elts[eindex];
  627. if (elt)
  628. {
  629. bitset_word *srcp = EBITSET_WORDS (elt);
  630. bitset_windex windex = n_bits / BITSET_WORD_BITS;
  631. bitset_windex woffset = eindex * EBITSET_ELT_WORDS;
  632. srcp[windex - woffset]
  633. &= ((bitset_word) 1 << (last_bit % BITSET_WORD_BITS)) - 1;
  634. windex++;
  635. for (; (windex - woffset) < EBITSET_ELT_WORDS; windex++)
  636. srcp[windex - woffset] = 0;
  637. }
  638. }
  639. }
  640. static void
  641. tbitset_ones (bitset dst)
  642. {
  643. for (bitset_windex j = 0; j < EBITSET_SIZE (dst); j++)
  644. {
  645. /* Create new elements if they cannot be found. Perhaps
  646. we should just add pointers to a ones element? */
  647. tbitset_elt *elt =
  648. tbitset_elt_find (dst, j * EBITSET_ELT_BITS, EBITSET_CREATE);
  649. memset (EBITSET_WORDS (elt), -1, sizeof (EBITSET_WORDS (elt)));
  650. }
  651. EBITSET_NONZERO_SET (dst);
  652. tbitset_unused_clear (dst);
  653. }
  654. static bool
  655. tbitset_empty_p (bitset dst)
  656. {
  657. if (EBITSET_ZERO_P (dst))
  658. return true;
  659. tbitset_elts *elts = EBITSET_ELTS (dst);
  660. for (bitset_windex j = 0; j < EBITSET_SIZE (dst); j++)
  661. {
  662. tbitset_elt *elt = elts[j];
  663. if (elt)
  664. {
  665. if (!tbitset_elt_zero_p (elt))
  666. return false;
  667. /* Do some weeding as we go. */
  668. tbitset_elt_remove (dst, j);
  669. }
  670. }
  671. /* All the bits are zero. We could shrink the elts.
  672. For now just mark DST as known to be zero. */
  673. EBITSET_ZERO_SET (dst);
  674. return true;
  675. }
  676. static void
  677. tbitset_not (bitset dst, bitset src)
  678. {
  679. tbitset_resize (dst, BITSET_NBITS_ (src));
  680. for (bitset_windex j = 0; j < EBITSET_SIZE (src); j++)
  681. {
  682. /* Create new elements for dst if they cannot be found
  683. or substitute zero elements if src elements not found. */
  684. tbitset_elt *selt =
  685. tbitset_elt_find (src, j * EBITSET_ELT_BITS, EBITSET_SUBST);
  686. tbitset_elt *delt =
  687. tbitset_elt_find (dst, j * EBITSET_ELT_BITS, EBITSET_CREATE);
  688. for (unsigned i = 0; i < EBITSET_ELT_WORDS; i++)
  689. EBITSET_WORDS (delt)[i] = ~EBITSET_WORDS (selt)[i];
  690. }
  691. EBITSET_NONZERO_SET (dst);
  692. tbitset_unused_clear (dst);
  693. }
  694. /* Is DST == DST | SRC? */
  695. static bool
  696. tbitset_subset_p (bitset dst, bitset src)
  697. {
  698. tbitset_elts *selts = EBITSET_ELTS (src);
  699. tbitset_elts *delts = EBITSET_ELTS (dst);
  700. bitset_windex ssize = EBITSET_SIZE (src);
  701. bitset_windex dsize = EBITSET_SIZE (dst);
  702. for (bitset_windex j = 0; j < ssize; j++)
  703. {
  704. tbitset_elt *selt = j < ssize ? selts[j] : 0;
  705. tbitset_elt *delt = j < dsize ? delts[j] : 0;
  706. if (!selt && !delt)
  707. continue;
  708. if (!selt)
  709. selt = &tbitset_zero_elts[0];
  710. if (!delt)
  711. delt = &tbitset_zero_elts[0];
  712. for (unsigned i = 0; i < EBITSET_ELT_WORDS; i++)
  713. if (EBITSET_WORDS (delt)[i]
  714. != (EBITSET_WORDS (selt)[i] | EBITSET_WORDS (delt)[i]))
  715. return false;
  716. }
  717. return true;
  718. }
  719. /* Is DST & SRC == 0? */
  720. static bool
  721. tbitset_disjoint_p (bitset dst, bitset src)
  722. {
  723. tbitset_elts *selts = EBITSET_ELTS (src);
  724. tbitset_elts *delts = EBITSET_ELTS (dst);
  725. bitset_windex ssize = EBITSET_SIZE (src);
  726. bitset_windex dsize = EBITSET_SIZE (dst);
  727. for (bitset_windex j = 0; j < ssize; j++)
  728. {
  729. tbitset_elt *selt = j < ssize ? selts[j] : 0;
  730. tbitset_elt *delt = j < dsize ? delts[j] : 0;
  731. if (!selt || !delt)
  732. continue;
  733. for (unsigned i = 0; i < EBITSET_ELT_WORDS; i++)
  734. if ((EBITSET_WORDS (selt)[i] & EBITSET_WORDS (delt)[i]))
  735. return false;
  736. }
  737. return true;
  738. }
  739. static bool
  740. tbitset_op3_cmp (bitset dst, bitset src1, bitset src2, enum bitset_ops op)
  741. {
  742. bool changed = false;
  743. tbitset_resize (dst, max (BITSET_NBITS_ (src1), BITSET_NBITS_ (src2)));
  744. bitset_windex ssize1 = EBITSET_SIZE (src1);
  745. bitset_windex ssize2 = EBITSET_SIZE (src2);
  746. bitset_windex dsize = EBITSET_SIZE (dst);
  747. bitset_windex size = ssize1;
  748. if (size < ssize2)
  749. size = ssize2;
  750. tbitset_elts *selts1 = EBITSET_ELTS (src1);
  751. tbitset_elts *selts2 = EBITSET_ELTS (src2);
  752. tbitset_elts *delts = EBITSET_ELTS (dst);
  753. bitset_windex j = 0;
  754. for (j = 0; j < size; j++)
  755. {
  756. tbitset_elt *selt1 = j < ssize1 ? selts1[j] : 0;
  757. tbitset_elt *selt2 = j < ssize2 ? selts2[j] : 0;
  758. tbitset_elt *delt = j < dsize ? delts[j] : 0;
  759. if (!selt1 && !selt2)
  760. {
  761. if (delt)
  762. {
  763. changed = true;
  764. tbitset_elt_remove (dst, j);
  765. }
  766. continue;
  767. }
  768. if (!selt1)
  769. selt1 = &tbitset_zero_elts[0];
  770. if (!selt2)
  771. selt2 = &tbitset_zero_elts[0];
  772. if (!delt)
  773. delt = tbitset_elt_calloc ();
  774. else
  775. delts[j] = 0;
  776. bitset_word *srcp1 = EBITSET_WORDS (selt1);
  777. bitset_word *srcp2 = EBITSET_WORDS (selt2);
  778. bitset_word *dstp = EBITSET_WORDS (delt);
  779. switch (op)
  780. {
  781. default:
  782. abort ();
  783. case BITSET_OP_OR:
  784. for (unsigned i = 0; i < EBITSET_ELT_WORDS; i++, dstp++)
  785. {
  786. bitset_word tmp = *srcp1++ | *srcp2++;
  787. if (*dstp != tmp)
  788. {
  789. changed = true;
  790. *dstp = tmp;
  791. }
  792. }
  793. break;
  794. case BITSET_OP_AND:
  795. for (unsigned i = 0; i < EBITSET_ELT_WORDS; i++, dstp++)
  796. {
  797. bitset_word tmp = *srcp1++ & *srcp2++;
  798. if (*dstp != tmp)
  799. {
  800. changed = true;
  801. *dstp = tmp;
  802. }
  803. }
  804. break;
  805. case BITSET_OP_XOR:
  806. for (unsigned i = 0; i < EBITSET_ELT_WORDS; i++, dstp++)
  807. {
  808. bitset_word tmp = *srcp1++ ^ *srcp2++;
  809. if (*dstp != tmp)
  810. {
  811. changed = true;
  812. *dstp = tmp;
  813. }
  814. }
  815. break;
  816. case BITSET_OP_ANDN:
  817. for (unsigned i = 0; i < EBITSET_ELT_WORDS; i++, dstp++)
  818. {
  819. bitset_word tmp = *srcp1++ & ~(*srcp2++);
  820. if (*dstp != tmp)
  821. {
  822. changed = true;
  823. *dstp = tmp;
  824. }
  825. }
  826. break;
  827. }
  828. if (!tbitset_elt_zero_p (delt))
  829. tbitset_elt_add (dst, delt, j);
  830. else
  831. tbitset_elt_free (delt);
  832. }
  833. /* If we have elements of DST left over, free them all. */
  834. for (; j < dsize; j++)
  835. {
  836. changed = true;
  837. tbitset_elt *delt = delts[j];
  838. if (delt)
  839. tbitset_elt_remove (dst, j);
  840. }
  841. EBITSET_NONZERO_SET (dst);
  842. return changed;
  843. }
  844. static bool
  845. tbitset_and_cmp (bitset dst, bitset src1, bitset src2)
  846. {
  847. if (EBITSET_ZERO_P (src2))
  848. {
  849. tbitset_weed (dst);
  850. bool changed = EBITSET_ZERO_P (dst);
  851. tbitset_zero (dst);
  852. return changed;
  853. }
  854. else if (EBITSET_ZERO_P (src1))
  855. {
  856. tbitset_weed (dst);
  857. bool changed = EBITSET_ZERO_P (dst);
  858. tbitset_zero (dst);
  859. return changed;
  860. }
  861. else
  862. return tbitset_op3_cmp (dst, src1, src2, BITSET_OP_AND);
  863. }
  864. static void
  865. tbitset_and (bitset dst, bitset src1, bitset src2)
  866. {
  867. tbitset_and_cmp (dst, src1, src2);
  868. }
  869. static bool
  870. tbitset_andn_cmp (bitset dst, bitset src1, bitset src2)
  871. {
  872. if (EBITSET_ZERO_P (src2))
  873. return tbitset_copy_cmp (dst, src1);
  874. else if (EBITSET_ZERO_P (src1))
  875. {
  876. tbitset_weed (dst);
  877. bool changed = EBITSET_ZERO_P (dst);
  878. tbitset_zero (dst);
  879. return changed;
  880. }
  881. else
  882. return tbitset_op3_cmp (dst, src1, src2, BITSET_OP_ANDN);
  883. }
  884. static void
  885. tbitset_andn (bitset dst, bitset src1, bitset src2)
  886. {
  887. tbitset_andn_cmp (dst, src1, src2);
  888. }
  889. static bool
  890. tbitset_or_cmp (bitset dst, bitset src1, bitset src2)
  891. {
  892. if (EBITSET_ZERO_P (src2))
  893. return tbitset_copy_cmp (dst, src1);
  894. else if (EBITSET_ZERO_P (src1))
  895. return tbitset_copy_cmp (dst, src2);
  896. else
  897. return tbitset_op3_cmp (dst, src1, src2, BITSET_OP_OR);
  898. }
  899. static void
  900. tbitset_or (bitset dst, bitset src1, bitset src2)
  901. {
  902. tbitset_or_cmp (dst, src1, src2);
  903. }
  904. static bool
  905. tbitset_xor_cmp (bitset dst, bitset src1, bitset src2)
  906. {
  907. if (EBITSET_ZERO_P (src2))
  908. return tbitset_copy_cmp (dst, src1);
  909. else if (EBITSET_ZERO_P (src1))
  910. return tbitset_copy_cmp (dst, src2);
  911. else
  912. return tbitset_op3_cmp (dst, src1, src2, BITSET_OP_XOR);
  913. }
  914. static void
  915. tbitset_xor (bitset dst, bitset src1, bitset src2)
  916. {
  917. tbitset_xor_cmp (dst, src1, src2);
  918. }
  919. static void
  920. tbitset_copy (bitset dst, bitset src)
  921. {
  922. if (BITSET_COMPATIBLE_ (dst, src))
  923. tbitset_copy_ (dst, src);
  924. else
  925. bitset_copy_ (dst, src);
  926. }
  927. /* Vector of operations for linked-list bitsets. */
  928. struct bitset_vtable tbitset_vtable = {
  929. tbitset_set,
  930. tbitset_reset,
  931. bitset_toggle_,
  932. tbitset_test,
  933. tbitset_resize,
  934. bitset_size_,
  935. bitset_count_,
  936. tbitset_empty_p,
  937. tbitset_ones,
  938. tbitset_zero,
  939. tbitset_copy,
  940. tbitset_disjoint_p,
  941. tbitset_equal_p,
  942. tbitset_not,
  943. tbitset_subset_p,
  944. tbitset_and,
  945. tbitset_and_cmp,
  946. tbitset_andn,
  947. tbitset_andn_cmp,
  948. tbitset_or,
  949. tbitset_or_cmp,
  950. tbitset_xor,
  951. tbitset_xor_cmp,
  952. bitset_and_or_,
  953. bitset_and_or_cmp_,
  954. bitset_andn_or_,
  955. bitset_andn_or_cmp_,
  956. bitset_or_and_,
  957. bitset_or_and_cmp_,
  958. tbitset_list,
  959. tbitset_list_reverse,
  960. tbitset_free,
  961. BITSET_TABLE
  962. };
  963. /* Return size of initial structure. */
  964. size_t
  965. tbitset_bytes (bitset_bindex n_bits MAYBE_UNUSED)
  966. {
  967. return sizeof (struct tbitset_struct);
  968. }
  969. /* Initialize a bitset. */
  970. bitset
  971. tbitset_init (bitset bset, bitset_bindex n_bits)
  972. {
  973. bset->b.vtable = &tbitset_vtable;
  974. bset->b.csize = EBITSET_ELT_WORDS;
  975. EBITSET_ZERO_SET (bset);
  976. EBITSET_ASIZE (bset) = 0;
  977. EBITSET_ELTS (bset) = 0;
  978. tbitset_resize (bset, n_bits);
  979. return bset;
  980. }
  981. void
  982. tbitset_release_memory (void)
  983. {
  984. tbitset_free_list = 0;
  985. if (tbitset_obstack_init)
  986. {
  987. tbitset_obstack_init = false;
  988. obstack_free (&tbitset_obstack, NULL);
  989. }
  990. }