]> git.saurik.com Git - bison.git/blame - lib/lbitset.c
(ebitset_set, ebitset_reset): Do not assume that bitset_word is the
[bison.git] / lib / lbitset.c
CommitLineData
7086e707
AD
1/* Functions to support link list bitsets.
2 Copyright (C) 2002 Free Software Foundation, Inc.
3 Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
4
ef017502
AD
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 2 of the License, or
8 (at your option) any later version.
7086e707 9
ef017502
AD
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
7086e707 14
ef017502
AD
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
345cea78
AD
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18*/
7086e707
AD
19
20#ifdef HAVE_CONFIG_H
21#include "config.h"
22#endif
23
345cea78 24#include "lbitset.h"
7086e707
AD
25#include "obstack.h"
26#include <stdlib.h>
27
7086e707
AD
28/* This file implements linked-list bitsets. These bitsets can be of
29 arbitrary length and are more efficient than arrays of bits for
30 large sparse sets.
31
ef017502
AD
32 Usually if all the bits in an element are zero we remove the element
33 from the list. However, a side effect of the bit caching is that we
34 do not always notice when an element becomes zero. Hence the
35 lbitset_weed function which removes zero elements. */
36
37
38/* Number of words to use for each element. The larger the value the
39 greater the size of the cache and the shorter the time to find a given bit
40 but the more memory wasted for sparse bitsets and the longer the time
41 to search for set bits. */
42
ef017502 43#define LBITSET_ELT_WORDS 2
ef017502
AD
44
45typedef bitset_word lbitset_word;
46
47#define LBITSET_WORD_BITS BITSET_WORD_BITS
48
49/* Number of bits stored in each element. */
50#define LBITSET_ELT_BITS \
51 ((unsigned) (LBITSET_ELT_WORDS * LBITSET_WORD_BITS))
52
53/* Lbitset element. We use an array of bits for each element.
54 These are linked together in a doubly-linked list. */
55typedef struct lbitset_elt_struct
56{
57 struct lbitset_elt_struct *next; /* Next element. */
58 struct lbitset_elt_struct *prev; /* Previous element. */
59 bitset_windex index; /* bitno / BITSET_WORD_BITS. */
60 bitset_word words[LBITSET_ELT_WORDS]; /* Bits that are set. */
61}
62lbitset_elt;
63
64
65/* Head of lbitset linked list. */
66typedef struct lbitset_struct
67{
68 lbitset_elt *head; /* First element in linked list. */
69 lbitset_elt *tail; /* Last element in linked list. */
70}
71*lbitset;
72
7086e707 73
ef017502
AD
74struct bitset_struct
75{
76 struct bbitset_struct b;
77 struct lbitset_struct l;
78};
7086e707 79
ef017502
AD
80
81enum lbitset_find_mode
82 { LBITSET_FIND, LBITSET_CREATE, LBITSET_SUBST };
83
345cea78 84static lbitset_elt lbitset_zero_elts[3]; /* Elements of all zero bits. */
7086e707
AD
85
86/* Obstack to allocate bitset elements from. */
87static struct obstack lbitset_obstack;
88static int lbitset_obstack_init = 0;
ef017502 89static lbitset_elt *lbitset_free_list; /* Free list of bitset elements. */
7086e707
AD
90
91static lbitset_elt *lbitset_elt_alloc PARAMS ((void));
92static lbitset_elt *lbitset_elt_calloc PARAMS ((void));
93static void lbitset_elt_link PARAMS ((bitset, lbitset_elt *));
94static void lbitset_elt_unlink PARAMS ((bitset, lbitset_elt *));
95static void lbitset_elt_free PARAMS ((lbitset_elt *));
96static lbitset_elt *lbitset_elt_find PARAMS ((bitset, bitset_windex,
97 enum lbitset_find_mode));
98static int lbitset_elt_zero_p PARAMS ((lbitset_elt *));
99
100static void lbitset_prune PARAMS ((bitset, lbitset_elt *));
101static void lbitset_weed PARAMS ((bitset));
ef017502
AD
102static void lbitset_zero PARAMS ((bitset));
103static int lbitset_equal_p PARAMS ((bitset, bitset));
104static void lbitset_copy PARAMS ((bitset, bitset));
105static int lbitset_copy_compare PARAMS ((bitset, bitset));
106static void lbitset_set PARAMS ((bitset, bitset_bindex));
107static void lbitset_reset PARAMS ((bitset, bitset_bindex));
108static int lbitset_test PARAMS ((bitset, bitset_bindex));
109static int lbitset_size PARAMS ((bitset));
110static int lbitset_op1 PARAMS ((bitset, enum bitset_ops));
111static int lbitset_op2 PARAMS ((bitset, bitset, enum bitset_ops));
112static int lbitset_op3 PARAMS ((bitset, bitset, bitset, enum bitset_ops));
113static int lbitset_list PARAMS ((bitset, bitset_bindex *, bitset_bindex,
114 bitset_bindex *));
115static int lbitset_reverse_list
116PARAMS ((bitset, bitset_bindex *, bitset_bindex, bitset_bindex *));
117static void lbitset_free PARAMS ((bitset));
7086e707
AD
118
119
120#define LBITSET_CURRENT1(X) (lbitset_elt *)((char *)(X) + ((char *)&(((lbitset_elt *)(X))->next) - (char *)&(((lbitset_elt *)(X))->words)))
121
ef017502 122#define LBITSET_CURRENT(X) LBITSET_CURRENT1((X)->b.cdata)
7086e707 123
ef017502
AD
124#define LBITSET_HEAD(X) ((X)->l.head)
125#define LBITSET_TAIL(X) ((X)->l.tail)
7086e707
AD
126
127/* Allocate a lbitset element. The bits are not cleared. */
128static inline lbitset_elt *
129lbitset_elt_alloc ()
130{
131 lbitset_elt *elt;
132
133 if (lbitset_free_list != 0)
134 {
135 elt = lbitset_free_list;
136 lbitset_free_list = elt->next;
137 }
138 else
139 {
7086e707
AD
140 if (!lbitset_obstack_init)
141 {
142 lbitset_obstack_init = 1;
143
144 /* Let particular systems override the size of a chunk. */
ef017502 145
7086e707
AD
146#ifndef OBSTACK_CHUNK_SIZE
147#define OBSTACK_CHUNK_SIZE 0
148#endif
ef017502 149
7086e707 150 /* Let them override the alloc and free routines too. */
ef017502 151
7086e707
AD
152#ifndef OBSTACK_CHUNK_ALLOC
153#define OBSTACK_CHUNK_ALLOC xmalloc
154#endif
ef017502 155
7086e707
AD
156#ifndef OBSTACK_CHUNK_FREE
157#define OBSTACK_CHUNK_FREE free
158#endif
159
160#if !defined(__GNUC__) || (__GNUC__ < 2)
161#define __alignof__(type) 0
162#endif
163
164 obstack_specify_allocation (&lbitset_obstack, OBSTACK_CHUNK_SIZE,
165 __alignof__ (lbitset_elt),
ef017502
AD
166 (void *(*)PARAMS ((long)))
167 OBSTACK_CHUNK_ALLOC,
168 (void (*)PARAMS ((void *)))
169 OBSTACK_CHUNK_FREE);
7086e707
AD
170 }
171
172 /* Perhaps we should add a number of new elements to the free
ef017502 173 list. */
7086e707
AD
174 elt = (lbitset_elt *) obstack_alloc (&lbitset_obstack,
175 sizeof (lbitset_elt));
176 }
177
178 return elt;
179}
180
181
613f5e1a 182/* Allocate a lbitset element. The bits are cleared. */
7086e707
AD
183static inline lbitset_elt *
184lbitset_elt_calloc ()
185{
186 lbitset_elt *elt;
187
188 elt = lbitset_elt_alloc ();
189 memset (elt->words, 0, sizeof (elt->words));
190 return elt;
191}
192
193
194static inline void
195lbitset_elt_free (elt)
196 lbitset_elt *elt;
197{
198 elt->next = lbitset_free_list;
199 lbitset_free_list = elt;
200}
201
202
203/* Unlink element ELT from bitset BSET. */
204static inline void
205lbitset_elt_unlink (bset, elt)
206 bitset bset;
207 lbitset_elt *elt;
208{
209 lbitset_elt *next = elt->next;
210 lbitset_elt *prev = elt->prev;
211
212 if (prev)
213 prev->next = next;
214
215 if (next)
216 next->prev = prev;
217
218 if (LBITSET_HEAD (bset) == elt)
219 LBITSET_HEAD (bset) = next;
220 if (LBITSET_TAIL (bset) == elt)
221 LBITSET_TAIL (bset) = prev;
222
223 /* Update cache pointer. Since the first thing we try is to insert
224 before current, make current the next entry in preference to the
225 previous. */
226 if (LBITSET_CURRENT (bset) == elt)
227 {
228 if (next)
229 {
ef017502
AD
230 bset->b.cdata = next->words;
231 bset->b.cindex = next->index;
7086e707
AD
232 }
233 else if (prev)
234 {
ef017502
AD
235 bset->b.cdata = prev->words;
236 bset->b.cindex = prev->index;
7086e707
AD
237 }
238 else
239 {
ef017502
AD
240 bset->b.csize = 0;
241 bset->b.cdata = 0;
7086e707
AD
242 }
243 }
244
245 lbitset_elt_free (elt);
246}
247
248
249/* Cut the chain of bitset BSET before element ELT and free the
250 elements. */
251static inline void
252lbitset_prune (bset, elt)
253 bitset bset;
254 lbitset_elt *elt;
255{
256 lbitset_elt *next;
257
258 if (!elt)
ef017502 259 return;
7086e707
AD
260
261 if (elt->prev)
ef017502 262 {
7086e707 263 LBITSET_TAIL (bset) = elt->prev;
ef017502
AD
264 bset->b.cdata = elt->prev->words;
265 bset->b.cindex = elt->prev->index;
7086e707 266 elt->prev->next = 0;
ef017502 267 }
7086e707 268 else
ef017502 269 {
7086e707
AD
270 LBITSET_HEAD (bset) = 0;
271 LBITSET_TAIL (bset) = 0;
ef017502
AD
272 bset->b.cdata = 0;
273 bset->b.csize = 0;
274 }
7086e707
AD
275
276 for (; elt; elt = next)
277 {
278 next = elt->next;
279 lbitset_elt_free (elt);
280 }
281}
282
283
284/* Return nonzero if all bits in an element are zero. */
285static inline int
286lbitset_elt_zero_p (elt)
287 lbitset_elt *elt;
288{
289 int i;
290
291 for (i = 0; i < LBITSET_ELT_WORDS; i++)
292 if (elt->words[i])
293 return 0;
294
295 return 1;
296}
297
298
299/* Link the bitset element into the current bitset linked list. */
300static inline void
301lbitset_elt_link (bset, elt)
302 bitset bset;
303 lbitset_elt *elt;
304{
345cea78 305 bitset_windex windex = elt->index;
7086e707
AD
306 lbitset_elt *ptr;
307 lbitset_elt *current;
308
ef017502
AD
309 if (bset->b.csize)
310 current = LBITSET_CURRENT (bset);
7086e707 311 else
ef017502 312 current = LBITSET_HEAD (bset);
7086e707
AD
313
314 /* If this is the first and only element, add it in. */
315 if (LBITSET_HEAD (bset) == 0)
316 {
317 elt->next = elt->prev = 0;
318 LBITSET_HEAD (bset) = elt;
319 LBITSET_TAIL (bset) = elt;
320 }
321
322 /* If this index is less than that of the current element, it goes
323 somewhere before the current element. */
345cea78 324 else if (windex < bset->b.cindex)
7086e707
AD
325 {
326 for (ptr = current;
345cea78 327 ptr->prev && ptr->prev->index > windex; ptr = ptr->prev)
7086e707
AD
328 continue;
329
330 if (ptr->prev)
331 ptr->prev->next = elt;
332 else
333 LBITSET_HEAD (bset) = elt;
334
335 elt->prev = ptr->prev;
336 elt->next = ptr;
337 ptr->prev = elt;
338 }
339
340 /* Otherwise, it must go somewhere after the current element. */
341 else
342 {
343 for (ptr = current;
345cea78 344 ptr->next && ptr->next->index < windex; ptr = ptr->next)
7086e707
AD
345 continue;
346
347 if (ptr->next)
348 ptr->next->prev = elt;
349 else
350 LBITSET_TAIL (bset) = elt;
351
352 elt->next = ptr->next;
353 elt->prev = ptr;
354 ptr->next = elt;
355 }
356
357 /* Set up so this is the first element searched. */
345cea78 358 bset->b.cindex = windex;
ef017502
AD
359 bset->b.csize = LBITSET_ELT_WORDS;
360 bset->b.cdata = elt->words;
7086e707
AD
361}
362
363
364static lbitset_elt *
345cea78 365lbitset_elt_find (bset, windex, mode)
7086e707 366 bitset bset;
345cea78 367 bitset_windex windex;
7086e707
AD
368 enum lbitset_find_mode mode;
369{
370 lbitset_elt *elt;
371 lbitset_elt *current;
372
ef017502 373 if (bset->b.csize)
7086e707
AD
374 {
375 current = LBITSET_CURRENT (bset);
376 /* Check if element is the cached element. */
345cea78 377 if ((windex - bset->b.cindex) < bset->b.csize)
7086e707
AD
378 return current;
379 }
380 else
381 {
382 current = LBITSET_HEAD (bset);
383 }
384
385 if (current)
386 {
345cea78 387 if (windex < bset->b.cindex)
7086e707
AD
388 {
389 for (elt = current;
345cea78 390 elt->prev && elt->index > windex; elt = elt->prev)
7086e707
AD
391 continue;
392 }
393 else
394 {
395 for (elt = current;
345cea78 396 elt->next && (elt->index + LBITSET_ELT_WORDS - 1) < windex;
7086e707
AD
397 elt = elt->next)
398 continue;
399 }
400
401 /* `element' is the nearest to the one we want. If it's not the one
402 we want, the one we want does not exist. */
345cea78 403 if (elt && (windex - elt->index) < LBITSET_ELT_WORDS)
7086e707 404 {
ef017502
AD
405 bset->b.cindex = elt->index;
406 bset->b.csize = LBITSET_ELT_WORDS;
407 bset->b.cdata = elt->words;
7086e707
AD
408 return elt;
409 }
410 }
411
412 switch (mode)
413 {
414 case LBITSET_FIND:
415 return 0;
416
417 case LBITSET_CREATE:
345cea78 418 windex = (windex / (unsigned) LBITSET_ELT_WORDS) * LBITSET_ELT_WORDS;
7086e707
AD
419
420 elt = lbitset_elt_calloc ();
345cea78 421 elt->index = windex;
7086e707
AD
422 lbitset_elt_link (bset, elt);
423 return elt;
424
425 case LBITSET_SUBST:
426 return &lbitset_zero_elts[0];
427
428 default:
429 abort ();
430 }
431}
432
433
434/* Weed out the zero elements from the list. */
435static inline void
436lbitset_weed (bset)
437 bitset bset;
438{
439 lbitset_elt *elt;
440 lbitset_elt *next;
441
442 for (elt = LBITSET_HEAD (bset); elt; elt = next)
443 {
444 next = elt->next;
445 if (lbitset_elt_zero_p (elt))
446 lbitset_elt_unlink (bset, elt);
447 }
448}
449
450
451/* Set all bits in the bitset to zero. */
452static inline void
453lbitset_zero (bset)
454 bitset bset;
455{
456 lbitset_elt *head;
457
458 head = LBITSET_HEAD (bset);
459 if (!head)
460 return;
461
462 /* Clear a bitset by freeing the linked list at the head element. */
463 lbitset_prune (bset, head);
464}
465
466
467static inline int
468lbitset_equal_p (dst, src)
469 bitset dst;
470 bitset src;
471{
472 lbitset_elt *selt;
473 lbitset_elt *delt;
474 int j;
475
476 if (src == dst)
477 return 1;
478
479 lbitset_weed (src);
480 lbitset_weed (dst);
481 for (selt = LBITSET_HEAD (src), delt = LBITSET_HEAD (dst);
482 selt && delt; selt = selt->next, delt = delt->next)
483 {
484 if (selt->index != delt->index)
485 return 0;
486
487 for (j = 0; j < LBITSET_ELT_WORDS; j++)
488 if (delt->words[j] != selt->words[j])
489 return 0;
490 }
ef017502 491 return !selt && !delt;
7086e707
AD
492}
493
494
495/* Copy bits from bitset SRC to bitset DST. */
496static inline void
497lbitset_copy (dst, src)
498 bitset dst;
499 bitset src;
500{
501 lbitset_elt *elt;
502 lbitset_elt *head;
503 lbitset_elt *prev;
504 lbitset_elt *tmp;
505
506 if (src == dst)
507 return;
508
509 lbitset_zero (dst);
510
511 head = LBITSET_HEAD (src);
512 if (!head)
513 return;
514
515 prev = 0;
516 for (elt = head; elt; elt = elt->next)
517 {
518 tmp = lbitset_elt_alloc ();
519 tmp->index = elt->index;
520 tmp->prev = prev;
521 tmp->next = 0;
522 if (prev)
523 prev->next = tmp;
524 else
525 LBITSET_HEAD (dst) = tmp;
526 prev = tmp;
527
528 memcpy (tmp->words, elt->words, sizeof (elt->words));
529 }
530 LBITSET_TAIL (dst) = tmp;
531
ef017502
AD
532 dst->b.csize = LBITSET_ELT_WORDS;
533 dst->b.cdata = LBITSET_HEAD (dst)->words;
534 dst->b.cindex = LBITSET_HEAD (dst)->index;
7086e707
AD
535}
536
537
538/* Copy bits from bitset SRC to bitset DST. Return non-zero if
539 bitsets different. */
540static inline int
541lbitset_copy_compare (dst, src)
542 bitset dst;
543 bitset src;
544{
545 if (src == dst)
546 return 0;
547
ef017502 548 if (!LBITSET_HEAD (dst))
7086e707
AD
549 {
550 lbitset_copy (dst, src);
551 return LBITSET_HEAD (src) != 0;
552 }
553
554 if (lbitset_equal_p (dst, src))
555 return 0;
556
557 lbitset_copy (dst, src);
558 return 1;
559}
560
561
562/* Return size in bits of bitset SRC. */
563static int
564lbitset_size (src)
565 bitset src;
566{
567 lbitset_elt *elt;
568
569 elt = LBITSET_TAIL (src);
570 if (!elt)
ef017502 571 return 0;
7086e707
AD
572
573 /* Return current size of bitset in bits. */
574 return (elt->index + LBITSET_ELT_WORDS) * BITSET_WORD_BITS;
575}
576
577
578/* Set bit BITNO in bitset DST. */
579static void
580lbitset_set (dst, bitno)
581 bitset dst;
582 bitset_bindex bitno;
583{
345cea78 584 bitset_windex windex = bitno / BITSET_WORD_BITS;
7086e707 585
345cea78 586 lbitset_elt_find (dst, windex, LBITSET_CREATE);
7086e707 587
345cea78 588 dst->b.cdata[windex - dst->b.cindex] |= (1 << (bitno % BITSET_WORD_BITS));
7086e707
AD
589}
590
591
592/* Reset bit BITNO in bitset DST. */
593static void
594lbitset_reset (dst, bitno)
595 bitset dst;
596 bitset_bindex bitno;
597{
345cea78 598 bitset_windex windex = bitno / BITSET_WORD_BITS;
7086e707 599
345cea78 600 if (!lbitset_elt_find (dst, windex, LBITSET_FIND))
ef017502 601 return;
7086e707 602
345cea78 603 dst->b.cdata[windex - dst->b.cindex] &= ~(1 << (bitno % BITSET_WORD_BITS));
7086e707
AD
604
605 /* If all the data is zero, perhaps we should unlink it now... */
606}
607
608
609/* Test bit BITNO in bitset SRC. */
610static int
611lbitset_test (src, bitno)
612 bitset src;
613 bitset_bindex bitno;
614{
345cea78 615 bitset_windex windex = bitno / BITSET_WORD_BITS;
7086e707 616
345cea78 617 if (!lbitset_elt_find (src, windex, LBITSET_FIND))
7086e707
AD
618 return 0;
619
345cea78
AD
620 return (src->b.cdata[windex - src->b.cindex]
621 >> (bitno % BITSET_WORD_BITS)) & 1;
7086e707
AD
622}
623
624
625static void
626lbitset_free (bset)
627 bitset bset;
628{
629 lbitset_zero (bset);
630}
631
632
633/* Find list of up to NUM bits set in BSET starting from and including
ef017502
AD
634 *NEXT and store in array LIST. Return with actual number of bits
635 found and with *NEXT indicating where search stopped. */
7086e707
AD
636static int
637lbitset_reverse_list (bset, list, num, next)
638 bitset bset;
639 bitset_bindex *list;
640 bitset_bindex num;
641 bitset_bindex *next;
642{
643 bitset_bindex rbitno;
644 bitset_bindex bitno;
345cea78
AD
645 unsigned int bcount;
646 bitset_bindex boffset;
647 bitset_windex windex;
7086e707
AD
648 bitset_bindex count;
649 lbitset_elt *elt;
650 bitset_word word;
651 bitset_bindex n_bits;
652
653 elt = LBITSET_TAIL (bset);
654 if (!elt)
655 return 0;
656
657 n_bits = (elt->index + LBITSET_ELT_WORDS) * BITSET_WORD_BITS;
658 rbitno = *next;
659
660 if (rbitno >= n_bits)
ef017502 661 return 0;
7086e707
AD
662
663 bitno = n_bits - (rbitno + 1);
664
345cea78 665 windex = bitno / BITSET_WORD_BITS;
7086e707
AD
666
667 /* Skip back to starting element. */
345cea78 668 for (; elt && elt->index > windex; elt = elt->prev)
7086e707
AD
669 continue;
670
671 if (!elt)
672 return 0;
673
674 /* If num is 1, we could speed things up with a binary search
675 of the word of interest. */
676
677 count = 0;
345cea78
AD
678 bcount = bitno % BITSET_WORD_BITS;
679 boffset = windex * BITSET_WORD_BITS;
7086e707
AD
680
681 while (elt)
682 {
683 bitset_word *srcp = elt->words;
684
345cea78
AD
685 for (; (windex - elt->index) < LBITSET_ELT_WORDS;
686 windex--, boffset -= BITSET_WORD_BITS,
687 bcount = BITSET_WORD_BITS - 1)
7086e707 688 {
ef017502 689 word =
345cea78 690 srcp[windex - elt->index] << (BITSET_WORD_BITS - 1 - bcount);
7086e707 691
345cea78 692 for (; word; bcount--)
7086e707
AD
693 {
694 if (word & BITSET_MSB)
695 {
345cea78 696 list[count++] = boffset + bcount;
7086e707
AD
697 if (count >= num)
698 {
345cea78 699 *next = n_bits - (boffset + bcount);
7086e707
AD
700 return count;
701 }
702 }
703 word <<= 1;
704 }
705 }
706
707 elt = elt->prev;
708 if (elt)
709 {
345cea78
AD
710 windex = elt->index + LBITSET_ELT_WORDS - 1;
711 boffset = windex * BITSET_WORD_BITS;
7086e707
AD
712 }
713 }
714
345cea78 715 *next = n_bits - (boffset + 1);
7086e707
AD
716 return count;
717}
718
719
720/* Find list of up to NUM bits set in BSET starting from and including
ef017502
AD
721 *NEXT and store in array LIST. Return with actual number of bits
722 found and with *NEXT indicating where search stopped. */
7086e707
AD
723static int
724lbitset_list (bset, list, num, next)
725 bitset bset;
726 bitset_bindex *list;
727 bitset_bindex num;
728 bitset_bindex *next;
729{
730 bitset_bindex bitno;
345cea78 731 bitset_windex windex;
7086e707
AD
732 bitset_bindex count;
733 lbitset_elt *elt;
734 lbitset_elt *head;
735 bitset_word word;
736
737 head = LBITSET_HEAD (bset);
738 if (!head)
ef017502 739 return 0;
7086e707
AD
740
741 bitno = *next;
742 count = 0;
743
744 if (!bitno)
745 {
746 /* This is the most common case. */
747
748 /* Start with the first element. */
749 elt = head;
345cea78
AD
750 windex = elt->index;
751 bitno = windex * BITSET_WORD_BITS;
7086e707
AD
752 }
753 else
754 {
345cea78 755 windex = bitno / BITSET_WORD_BITS;
7086e707
AD
756
757 /* Skip to starting element. */
758 for (elt = head;
345cea78 759 elt && (elt->index + LBITSET_ELT_WORDS - 1) < windex;
7086e707
AD
760 elt = elt->next)
761 continue;
762
763 if (!elt)
764 return 0;
765
345cea78 766 if (windex < elt->index)
7086e707 767 {
345cea78
AD
768 windex = elt->index;
769 bitno = windex * BITSET_WORD_BITS;
7086e707
AD
770 }
771 else
772 {
773 bitset_word *srcp = elt->words;
774
775 /* We are starting within an element. */
776
345cea78 777 for (; (windex - elt->index) < LBITSET_ELT_WORDS; windex++)
7086e707 778 {
345cea78 779 word = srcp[windex - elt->index] >> (bitno % BITSET_WORD_BITS);
7086e707
AD
780
781 for (; word; bitno++)
782 {
783 if (word & 1)
784 {
785 list[count++] = bitno;
786 if (count >= num)
787 {
788 *next = bitno + 1;
789 return count;
790 }
791 }
792 word >>= 1;
793 }
345cea78 794 bitno = (windex + 1) * BITSET_WORD_BITS;
7086e707
AD
795 }
796
797 elt = elt->next;
798 if (elt)
799 {
345cea78
AD
800 windex = elt->index;
801 bitno = windex * BITSET_WORD_BITS;
7086e707
AD
802 }
803 }
804 }
805
806
807 /* If num is 1, we could speed things up with a binary search
808 of the word of interest. */
809
810 while (elt)
811 {
812 int i;
813 bitset_word *srcp = elt->words;
814
815 if ((count + LBITSET_ELT_BITS) < num)
816 {
817 /* The coast is clear, plant boot! */
818
819#if LBITSET_ELT_WORDS == 2
820 word = srcp[0];
821 if (word)
822 {
ef017502 823 if (!(word & 0xffff))
7086e707
AD
824 {
825 word >>= 16;
826 bitno += 16;
827 }
ef017502 828 if (!(word & 0xff))
7086e707
AD
829 {
830 word >>= 8;
831 bitno += 8;
832 }
833 for (; word; bitno++)
834 {
835 if (word & 1)
836 list[count++] = bitno;
837 word >>= 1;
838 }
839 }
345cea78
AD
840 windex++;
841 bitno = windex * BITSET_WORD_BITS;
7086e707
AD
842
843 word = srcp[1];
844 if (word)
845 {
ef017502 846 if (!(word & 0xffff))
7086e707
AD
847 {
848 word >>= 16;
849 bitno += 16;
850 }
851 for (; word; bitno++)
852 {
853 if (word & 1)
854 list[count++] = bitno;
855 word >>= 1;
856 }
857 }
345cea78
AD
858 windex++;
859 bitno = windex * BITSET_WORD_BITS;
7086e707
AD
860#else
861 for (i = 0; i < LBITSET_ELT_WORDS; i++)
862 {
863 word = srcp[i];
864 if (word)
865 {
ef017502 866 if (!(word & 0xffff))
7086e707
AD
867 {
868 word >>= 16;
869 bitno += 16;
870 }
ef017502 871 if (!(word & 0xff))
7086e707
AD
872 {
873 word >>= 8;
874 bitno += 8;
875 }
876 for (; word; bitno++)
877 {
878 if (word & 1)
879 list[count++] = bitno;
880 word >>= 1;
881 }
882 }
345cea78
AD
883 windex++;
884 bitno = windex * BITSET_WORD_BITS;
7086e707
AD
885 }
886#endif
887 }
888 else
889 {
890 /* Tread more carefully since we need to check
891 if array overflows. */
892
893 for (i = 0; i < LBITSET_ELT_WORDS; i++)
894 {
895 for (word = srcp[i]; word; bitno++)
896 {
897 if (word & 1)
898 {
899 list[count++] = bitno;
900 if (count >= num)
901 {
902 *next = bitno + 1;
903 return count;
904 }
905 }
906 word >>= 1;
907 }
345cea78
AD
908 windex++;
909 bitno = windex * BITSET_WORD_BITS;
7086e707
AD
910 }
911 }
912
913 elt = elt->next;
914 if (elt)
915 {
345cea78
AD
916 windex = elt->index;
917 bitno = windex * BITSET_WORD_BITS;
7086e707
AD
918 }
919 }
920
921 *next = bitno;
922 return count;
923}
924
925
926static int
927lbitset_op1 (dst, op)
928 bitset dst;
929 enum bitset_ops op;
930{
931 unsigned int i;
345cea78 932 bitset_windex windex;
7086e707
AD
933 lbitset_elt *elt;
934
935 switch (op)
936 {
ef017502 937 case BITSET_OP_ZERO:
7086e707
AD
938 lbitset_zero (dst);
939 break;
940
ef017502 941 case BITSET_OP_ONES:
7086e707
AD
942 /* This is a decidedly unfriendly operation for a linked list
943 bitset! */
944 elt = LBITSET_TAIL (dst);
945 /* Ignore empty set. */
946 if (!elt)
947 return 0;
948
345cea78
AD
949 windex = elt->index;
950 for (i = 0; i < windex; i += LBITSET_ELT_WORDS)
7086e707
AD
951 {
952 /* Create new elements if they cannot be found. */
953 elt = lbitset_elt_find (dst, i, LBITSET_CREATE);
954 memset (elt->words, ~0, sizeof (elt->words));
955 }
956 break;
957
ef017502 958 case BITSET_OP_EMPTY_P:
7086e707
AD
959 lbitset_weed (dst);
960 if (LBITSET_HEAD (dst))
961 return 0;
962 break;
963
964 default:
965 abort ();
966 }
967
968 return 1;
969}
970
971
972static int
973lbitset_op2 (dst, src, op)
974 bitset dst;
975 bitset src;
976 enum bitset_ops op;
977{
978 lbitset_elt *elt;
979 lbitset_elt *selt;
980 lbitset_elt *delt;
981 unsigned int i;
982 unsigned int j;
345cea78 983 bitset_windex windex;
7086e707
AD
984
985 switch (op)
986 {
ef017502 987 case BITSET_OP_COPY:
7086e707
AD
988 lbitset_copy (dst, src);
989 break;
990
ef017502 991 case BITSET_OP_NOT:
7086e707
AD
992 /* This is another unfriendly operation for a linked list
993 bitset! */
994 elt = LBITSET_TAIL (dst);
995 /* Ignore empty set. */
996 if (!elt)
997 return 0;
998
345cea78
AD
999 windex = elt->index;
1000 for (i = 0; i < windex; i += LBITSET_ELT_WORDS)
7086e707
AD
1001 {
1002 /* Create new elements for dst if they cannot be found
1003 or substitute zero elements if src elements not found. */
1004 selt = lbitset_elt_find (dst, i, LBITSET_SUBST);
1005 delt = lbitset_elt_find (dst, i, LBITSET_CREATE);
1006
1007 for (j = 0; j < LBITSET_ELT_WORDS; j++)
1008 delt->words[j] = ~selt->words[j];
1009 }
1010 lbitset_weed (dst);
1011 break;
1012
ef017502
AD
1013 /* Return 1 if DST == SRC. */
1014 case BITSET_OP_EQUAL_P:
7086e707
AD
1015 return lbitset_equal_p (dst, src);
1016 break;
1017
ef017502
AD
1018 /* Return 1 if DST == DST | SRC. */
1019 case BITSET_OP_SUBSET_P:
7086e707
AD
1020 for (selt = LBITSET_HEAD (src), delt = LBITSET_HEAD (dst);
1021 selt || delt; selt = selt->next, delt = delt->next)
1022 {
1023 if (!selt)
1024 selt = &lbitset_zero_elts[0];
1025 else if (!delt)
1026 delt = &lbitset_zero_elts[0];
1027 else if (selt->index != delt->index)
1028 {
1029 if (selt->index < delt->index)
1030 {
1031 lbitset_zero_elts[2].next = delt;
1032 delt = &lbitset_zero_elts[2];
1033 }
1034 else
1035 {
1036 lbitset_zero_elts[1].next = selt;
1037 selt = &lbitset_zero_elts[1];
1038 }
1039 }
1040
1041 for (j = 0; j < LBITSET_ELT_WORDS; j++)
1042 if (delt->words[j] != (selt->words[j] | delt->words[j]))
1043 return 0;
1044 }
1045 break;
1046
ef017502
AD
1047 /* Return 1 if DST & SRC == 0. */
1048 case BITSET_OP_DISJOINT_P:
1049 for (selt = LBITSET_HEAD (src), delt = LBITSET_HEAD (dst);
1050 selt && delt; selt = selt->next, delt = delt->next)
1051 {
1052 if (selt->index != delt->index)
1053 {
1054 if (selt->index < delt->index)
1055 {
1056 lbitset_zero_elts[2].next = delt;
1057 delt = &lbitset_zero_elts[2];
1058 }
1059 else
1060 {
1061 lbitset_zero_elts[1].next = selt;
1062 selt = &lbitset_zero_elts[1];
1063 }
345cea78
AD
1064 /* Since the elements are different, there is no
1065 intersection of these elements. */
1066 continue;
ef017502
AD
1067 }
1068
1069 for (j = 0; j < LBITSET_ELT_WORDS; j++)
1070 if (selt->words[j] & delt->words[j])
1071 return 0;
1072 }
1073 break;
1074
7086e707
AD
1075 default:
1076 abort ();
1077 }
1078
1079 return 1;
1080}
1081
1082
1083static int
1084lbitset_op3 (dst, src1, src2, op)
1085 bitset dst;
1086 bitset src1;
1087 bitset src2;
1088 enum bitset_ops op;
1089{
1090 lbitset_elt *selt1 = LBITSET_HEAD (src1);
1091 lbitset_elt *selt2 = LBITSET_HEAD (src2);
1092 lbitset_elt *delt = LBITSET_HEAD (dst);
345cea78
AD
1093 bitset_windex windex1;
1094 bitset_windex windex2;
1095 bitset_windex windex;
7086e707
AD
1096 lbitset_elt *stmp1;
1097 lbitset_elt *stmp2;
1098 lbitset_elt *dtmp;
1099 bitset_word *srcp1;
1100 bitset_word *srcp2;
1101 bitset_word *dstp;
1102 int changed = 0;
1103 unsigned int i;
1104
1105 /* Fast track common, simple cases. */
1106 if (!selt2)
1107 {
ef017502 1108 if (op == BITSET_OP_AND)
7086e707
AD
1109 {
1110 lbitset_weed (dst);
ef017502 1111 changed = !LBITSET_HEAD (dst);
7086e707
AD
1112 lbitset_zero (dst);
1113 return changed;
1114 }
ef017502
AD
1115 else if (op == BITSET_OP_ANDN || op == BITSET_OP_OR
1116 || op == BITSET_OP_XOR)
7086e707
AD
1117 {
1118 return lbitset_copy_compare (dst, src1);
1119 }
1120 }
1121 else if (!selt1)
1122 {
ef017502 1123 if (op == BITSET_OP_AND || op == BITSET_OP_ANDN)
7086e707
AD
1124 {
1125 lbitset_weed (dst);
ef017502 1126 changed = !LBITSET_HEAD (dst);
7086e707
AD
1127 lbitset_zero (dst);
1128 return changed;
1129 }
ef017502 1130 else if (op == BITSET_OP_OR || op == BITSET_OP_XOR)
7086e707
AD
1131 {
1132 return lbitset_copy_compare (dst, src2);
1133 }
1134 }
1135
7086e707 1136 LBITSET_HEAD (dst) = 0;
ef017502 1137 dst->b.csize = 0;
7086e707 1138
345cea78
AD
1139 windex1 = (selt1) ? selt1->index : BITSET_INDEX_MAX;
1140 windex2 = (selt2) ? selt2->index : BITSET_INDEX_MAX;
7086e707
AD
1141
1142 while (selt1 || selt2)
1143 {
1144 /* Figure out whether we need to substitute zero elements for
1145 missing links. */
345cea78 1146 if (windex1 == windex2)
7086e707 1147 {
345cea78 1148 windex = windex1;
7086e707
AD
1149 stmp1 = selt1;
1150 stmp2 = selt2;
1151 selt1 = selt1->next;
345cea78 1152 windex1 = (selt1) ? selt1->index : BITSET_INDEX_MAX;
7086e707 1153 selt2 = selt2->next;
345cea78 1154 windex2 = (selt2) ? selt2->index : BITSET_INDEX_MAX;
7086e707 1155 }
345cea78 1156 else if (windex1 < windex2)
7086e707 1157 {
345cea78 1158 windex = windex1;
7086e707
AD
1159 stmp1 = selt1;
1160 stmp2 = &lbitset_zero_elts[0];
1161 selt1 = selt1->next;
345cea78 1162 windex1 = (selt1) ? selt1->index : BITSET_INDEX_MAX;
7086e707
AD
1163 }
1164 else
1165 {
345cea78 1166 windex = windex2;
7086e707
AD
1167 stmp1 = &lbitset_zero_elts[0];
1168 stmp2 = selt2;
1169 selt2 = selt2->next;
345cea78 1170 windex2 = (selt2) ? selt2->index : BITSET_INDEX_MAX;
7086e707
AD
1171 }
1172
1173 /* Find the appropriate element from DST. Begin by discarding
1174 elements that we've skipped. */
345cea78 1175 while (delt && delt->index < windex)
7086e707
AD
1176 {
1177 changed = 1;
1178 dtmp = delt;
1179 delt = delt->next;
1180 lbitset_elt_free (dtmp);
1181 }
345cea78 1182 if (delt && delt->index == windex)
7086e707
AD
1183 {
1184 dtmp = delt;
1185 delt = delt->next;
1186 }
1187 else
1188 dtmp = lbitset_elt_calloc ();
1189
1190 /* Do the operation, and if any bits are set, link it into the
1191 linked list. */
1192 srcp1 = stmp1->words;
1193 srcp2 = stmp2->words;
1194 dstp = dtmp->words;
1195 switch (op)
1196 {
ef017502 1197 case BITSET_OP_OR:
7086e707
AD
1198 for (i = 0; i < LBITSET_ELT_WORDS; i++, dstp++)
1199 {
1200 bitset_word tmp = *srcp1++ | *srcp2++;
1201
1202 if (*dstp != tmp)
1203 {
1204 changed = 1;
1205 *dstp = tmp;
1206 }
1207 }
1208 break;
1209
ef017502 1210 case BITSET_OP_AND:
7086e707
AD
1211 for (i = 0; i < LBITSET_ELT_WORDS; i++, dstp++)
1212 {
1213 bitset_word tmp = *srcp1++ & *srcp2++;
1214
1215 if (*dstp != tmp)
1216 {
1217 changed = 1;
1218 *dstp = tmp;
1219 }
1220 }
1221 break;
1222
ef017502 1223 case BITSET_OP_XOR:
7086e707
AD
1224 for (i = 0; i < LBITSET_ELT_WORDS; i++, dstp++)
1225 {
1226 bitset_word tmp = *srcp1++ ^ *srcp2++;
1227
1228 if (*dstp != tmp)
1229 {
1230 changed = 1;
1231 *dstp = tmp;
1232 }
1233 }
1234 break;
1235
ef017502 1236 case BITSET_OP_ANDN:
7086e707
AD
1237 for (i = 0; i < LBITSET_ELT_WORDS; i++, dstp++)
1238 {
1239 bitset_word tmp = *srcp1++ & ~(*srcp2++);
1240
1241 if (*dstp != tmp)
1242 {
1243 changed = 1;
1244 *dstp = tmp;
1245 }
1246 }
1247 break;
1248
7086e707
AD
1249 default:
1250 abort ();
1251 }
1252
ef017502 1253 if (!lbitset_elt_zero_p (dtmp))
7086e707 1254 {
345cea78 1255 dtmp->index = windex;
7086e707
AD
1256 /* Perhaps this could be optimised... */
1257 lbitset_elt_link (dst, dtmp);
1258 }
1259 else
1260 {
1261 lbitset_elt_free (dtmp);
1262 }
1263 }
1264
1265 /* If we have elements of DST left over, free them all. */
1266 if (delt)
1267 {
1268 changed = 1;
1269 lbitset_prune (dst, delt);
1270 }
1271
1272 return changed;
1273}
1274
1275
7086e707 1276/* Vector of operations for linked-list bitsets. */
ef017502
AD
1277struct bitset_ops_struct lbitset_ops = {
1278 lbitset_set,
1279 lbitset_reset,
1280 lbitset_test,
1281 lbitset_size,
1282 lbitset_op1,
1283 lbitset_op2,
1284 lbitset_op3,
1285 bitset_op4,
1286 lbitset_list,
1287 lbitset_reverse_list,
1288 lbitset_free,
1289 BITSET_LIST
1290};
7086e707
AD
1291
1292
1293/* Return size of initial structure. */
1294int
1295lbitset_bytes (n_bits)
1296 bitset_bindex n_bits ATTRIBUTE_UNUSED;
1297{
1298 return sizeof (struct bitset_struct);
1299}
1300
1301
1302/* Initialize a bitset. */
1303
1304bitset
1305lbitset_init (bset, n_bits)
1306 bitset bset;
1307 bitset_bindex n_bits ATTRIBUTE_UNUSED;
1308{
ef017502 1309 bset->b.ops = &lbitset_ops;
7086e707
AD
1310 return bset;
1311}
1312
1313
1314void
1315lbitset_release_memory ()
1316{
1317 lbitset_free_list = 0;
1318 if (lbitset_obstack_init)
1319 {
1320 lbitset_obstack_init = 0;
1321 obstack_free (&lbitset_obstack, NULL);
1322 }
1323}