1 /* Functions to support expandable bitsets.
3 Copyright (C) 2002-2006, 2009-2011 Free Software Foundation, Inc.
5 Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 /* This file implements expandable bitsets. These bitsets can be of
29 arbitrary length and are more efficient than arrays of bits for
32 Empty elements are represented by a NULL pointer in the table of
33 element pointers. An alternative is to point to a special zero
34 element. Similarly, we could represent an all 1's element with
35 another special ones element pointer.
37 Bitsets are commonly empty so we need to ensure that this special
38 case is fast. A zero bitset is indicated when cdata is 0. This is
39 conservative since cdata may be non zero and the bitset may still
42 The bitset cache can be disabled either by setting cindex to
43 BITSET_WINDEX_MAX or by setting csize to 0. Here
44 we use the former approach since cindex needs to be updated whenever
49 /* Number of words to use for each element. */
50 #define EBITSET_ELT_WORDS 2
52 /* Number of bits stored in each element. */
53 #define EBITSET_ELT_BITS \
54 ((unsigned int) (EBITSET_ELT_WORDS * BITSET_WORD_BITS))
56 /* Ebitset element. We use an array of bits. */
57 typedef struct ebitset_elt_struct
61 bitset_word words
[EBITSET_ELT_WORDS
]; /* Bits that are set. */
62 struct ebitset_elt_struct
*next
;
69 typedef ebitset_elt
*ebitset_elts
;
72 /* Number of elements to initially allocate. */
74 #ifndef EBITSET_INITIAL_SIZE
75 #define EBITSET_INITIAL_SIZE 2
79 enum ebitset_find_mode
80 { EBITSET_FIND
, EBITSET_CREATE
, EBITSET_SUBST
};
82 static ebitset_elt ebitset_zero_elts
[1]; /* Elements of all zero bits. */
84 /* Obstack to allocate bitset elements from. */
85 static struct obstack ebitset_obstack
;
86 static bool ebitset_obstack_init
= false;
87 static ebitset_elt
*ebitset_free_list
; /* Free list of bitset elements. */
89 #define EBITSET_N_ELTS(N) (((N) + EBITSET_ELT_BITS - 1) / EBITSET_ELT_BITS)
90 #define EBITSET_ELTS(BSET) ((BSET)->e.elts)
91 #define EBITSET_SIZE(BSET) EBITSET_N_ELTS (BITSET_NBITS_ (BSET))
92 #define EBITSET_ASIZE(BSET) ((BSET)->e.size)
94 #define EBITSET_NEXT(ELT) ((ELT)->u.next)
95 #define EBITSET_WORDS(ELT) ((ELT)->u.words)
97 /* Disable bitset cache and mark BSET as being zero. */
98 #define EBITSET_ZERO_SET(BSET) ((BSET)->b.cindex = BITSET_WINDEX_MAX, \
101 #define EBITSET_CACHE_DISABLE(BSET) ((BSET)->b.cindex = BITSET_WINDEX_MAX)
103 /* Disable bitset cache and mark BSET as being possibly non-zero. */
104 #define EBITSET_NONZERO_SET(BSET) \
105 (EBITSET_CACHE_DISABLE (BSET), (BSET)->b.cdata = (bitset_word *)~0)
107 /* A conservative estimate of whether the bitset is zero.
108 This is non-zero only if we know for sure that the bitset is zero. */
109 #define EBITSET_ZERO_P(BSET) ((BSET)->b.cdata == 0)
111 /* Enable cache to point to element with table index EINDEX.
112 The element must exist. */
113 #define EBITSET_CACHE_SET(BSET, EINDEX) \
114 ((BSET)->b.cindex = (EINDEX) * EBITSET_ELT_WORDS, \
115 (BSET)->b.cdata = EBITSET_WORDS (EBITSET_ELTS (BSET) [EINDEX]))
119 #define min(a, b) ((a) > (b) ? (b) : (a))
120 #define max(a, b) ((a) > (b) ? (a) : (b))
123 ebitset_resize (bitset src
, bitset_bindex n_bits
)
125 bitset_windex oldsize
;
126 bitset_windex newsize
;
128 if (n_bits
== BITSET_NBITS_ (src
))
131 oldsize
= EBITSET_SIZE (src
);
132 newsize
= EBITSET_N_ELTS (n_bits
);
134 if (oldsize
< newsize
)
138 /* The bitset needs to grow. If we already have enough memory
139 allocated, then just zero what we need. */
140 if (newsize
> EBITSET_ASIZE (src
))
142 /* We need to allocate more memory. When oldsize is
143 non-zero this means that we are changing the size, so
144 grow the bitset 25% larger than requested to reduce
145 number of reallocations. */
150 size
= newsize
+ newsize
/ 4;
153 = realloc (EBITSET_ELTS (src
), size
* sizeof (ebitset_elt
*));
154 EBITSET_ASIZE (src
) = size
;
157 memset (EBITSET_ELTS (src
) + oldsize
, 0,
158 (newsize
- oldsize
) * sizeof (ebitset_elt
*));
162 /* The bitset needs to shrink. There's no point deallocating
163 the memory unless it is shrinking by a reasonable amount. */
164 if ((oldsize
- newsize
) >= oldsize
/ 2)
167 = realloc (EBITSET_ELTS (src
), newsize
* sizeof (ebitset_elt
*));
168 EBITSET_ASIZE (src
) = newsize
;
171 /* Need to prune any excess bits. FIXME. */
174 BITSET_NBITS_ (src
) = n_bits
;
179 /* Allocate a ebitset element. The bits are not cleared. */
180 static inline ebitset_elt
*
181 ebitset_elt_alloc (void)
185 if (ebitset_free_list
!= 0)
187 elt
= ebitset_free_list
;
188 ebitset_free_list
= EBITSET_NEXT (elt
);
192 if (!ebitset_obstack_init
)
194 ebitset_obstack_init
= true;
196 /* Let particular systems override the size of a chunk. */
198 #ifndef OBSTACK_CHUNK_SIZE
199 #define OBSTACK_CHUNK_SIZE 0
202 /* Let them override the alloc and free routines too. */
204 #ifndef OBSTACK_CHUNK_ALLOC
205 #define OBSTACK_CHUNK_ALLOC xmalloc
208 #ifndef OBSTACK_CHUNK_FREE
209 #define OBSTACK_CHUNK_FREE free
212 #if ! defined __GNUC__ || __GNUC__ < 2
213 #define __alignof__(type) 0
216 obstack_specify_allocation (&ebitset_obstack
, OBSTACK_CHUNK_SIZE
,
217 __alignof__ (ebitset_elt
),
222 /* Perhaps we should add a number of new elements to the free
224 elt
= (ebitset_elt
*) obstack_alloc (&ebitset_obstack
,
225 sizeof (ebitset_elt
));
232 /* Allocate a ebitset element. The bits are cleared. */
233 static inline ebitset_elt
*
234 ebitset_elt_calloc (void)
238 elt
= ebitset_elt_alloc ();
239 memset (EBITSET_WORDS (elt
), 0, sizeof (EBITSET_WORDS (elt
)));
245 ebitset_elt_free (ebitset_elt
*elt
)
247 EBITSET_NEXT (elt
) = ebitset_free_list
;
248 ebitset_free_list
= elt
;
252 /* Remove element with index EINDEX from bitset BSET. */
254 ebitset_elt_remove (bitset bset
, bitset_windex eindex
)
259 elts
= EBITSET_ELTS (bset
);
264 ebitset_elt_free (elt
);
268 /* Add ELT into elts at index EINDEX of bitset BSET. */
270 ebitset_elt_add (bitset bset
, ebitset_elt
*elt
, bitset_windex eindex
)
274 elts
= EBITSET_ELTS (bset
);
275 /* Assume that the elts entry not allocated. */
280 /* Are all bits in an element zero? */
282 ebitset_elt_zero_p (ebitset_elt
*elt
)
286 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
287 if (EBITSET_WORDS (elt
)[i
])
295 ebitset_elt_find (bitset bset
, bitset_bindex bindex
,
296 enum ebitset_find_mode mode
)
300 bitset_windex eindex
;
303 eindex
= bindex
/ EBITSET_ELT_BITS
;
305 elts
= EBITSET_ELTS (bset
);
306 size
= EBITSET_SIZE (bset
);
310 if ((elt
= elts
[eindex
]))
312 if (EBITSET_WORDS (elt
) == bset
->b
.cdata
)
315 EBITSET_CACHE_SET (bset
, eindex
);
320 /* The element could not be found. */
332 ebitset_resize (bset
, bindex
);
334 /* Create a new element. */
335 elt
= ebitset_elt_calloc ();
336 ebitset_elt_add (bset
, elt
, eindex
);
337 EBITSET_CACHE_SET (bset
, eindex
);
341 return &ebitset_zero_elts
[0];
346 /* Weed out the zero elements from the elts. */
347 static inline bitset_windex
348 ebitset_weed (bitset bset
)
354 if (EBITSET_ZERO_P (bset
))
357 elts
= EBITSET_ELTS (bset
);
359 for (j
= 0; j
< EBITSET_SIZE (bset
); j
++)
361 ebitset_elt
*elt
= elts
[j
];
365 if (ebitset_elt_zero_p (elt
))
367 ebitset_elt_remove (bset
, j
);
378 /* All the bits are zero. We could shrink the elts.
379 For now just mark BSET as known to be zero. */
380 EBITSET_ZERO_SET (bset
);
383 EBITSET_NONZERO_SET (bset
);
389 /* Set all bits in the bitset to zero. */
391 ebitset_zero (bitset bset
)
396 if (EBITSET_ZERO_P (bset
))
399 elts
= EBITSET_ELTS (bset
);
400 for (j
= 0; j
< EBITSET_SIZE (bset
); j
++)
402 ebitset_elt
*elt
= elts
[j
];
405 ebitset_elt_remove (bset
, j
);
408 /* All the bits are zero. We could shrink the elts.
409 For now just mark BSET as known to be zero. */
410 EBITSET_ZERO_SET (bset
);
415 ebitset_equal_p (bitset dst
, bitset src
)
427 if (EBITSET_SIZE (src
) != EBITSET_SIZE (dst
))
430 selts
= EBITSET_ELTS (src
);
431 delts
= EBITSET_ELTS (dst
);
433 for (j
= 0; j
< EBITSET_SIZE (src
); j
++)
436 ebitset_elt
*selt
= selts
[j
];
437 ebitset_elt
*delt
= delts
[j
];
441 if ((selt
&& !delt
) || (!selt
&& delt
))
444 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
445 if (EBITSET_WORDS (selt
)[i
] != EBITSET_WORDS (delt
)[i
])
452 /* Copy bits from bitset SRC to bitset DST. */
454 ebitset_copy_ (bitset dst
, bitset src
)
465 if (BITSET_NBITS_ (dst
) != BITSET_NBITS_ (src
))
466 ebitset_resize (dst
, BITSET_NBITS_ (src
));
468 selts
= EBITSET_ELTS (src
);
469 delts
= EBITSET_ELTS (dst
);
470 for (j
= 0; j
< EBITSET_SIZE (src
); j
++)
472 ebitset_elt
*selt
= selts
[j
];
478 tmp
= ebitset_elt_alloc ();
480 memcpy (EBITSET_WORDS (tmp
), EBITSET_WORDS (selt
),
481 sizeof (EBITSET_WORDS (selt
)));
484 EBITSET_NONZERO_SET (dst
);
488 /* Copy bits from bitset SRC to bitset DST. Return true if
489 bitsets different. */
491 ebitset_copy_cmp (bitset dst
, bitset src
)
496 if (EBITSET_ZERO_P (dst
))
498 ebitset_copy_ (dst
, src
);
499 return !EBITSET_ZERO_P (src
);
502 if (ebitset_equal_p (dst
, src
))
505 ebitset_copy_ (dst
, src
);
510 /* Set bit BITNO in bitset DST. */
512 ebitset_set (bitset dst
, bitset_bindex bitno
)
514 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
516 ebitset_elt_find (dst
, bitno
, EBITSET_CREATE
);
518 dst
->b
.cdata
[windex
- dst
->b
.cindex
] |=
519 (bitset_word
) 1 << (bitno
% BITSET_WORD_BITS
);
523 /* Reset bit BITNO in bitset DST. */
525 ebitset_reset (bitset dst
, bitset_bindex bitno
)
527 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
529 if (!ebitset_elt_find (dst
, bitno
, EBITSET_FIND
))
532 dst
->b
.cdata
[windex
- dst
->b
.cindex
] &=
533 ~((bitset_word
) 1 << (bitno
% BITSET_WORD_BITS
));
535 /* If all the data is zero, perhaps we should remove it now...
536 However, there is a good chance that the element will be needed
541 /* Test bit BITNO in bitset SRC. */
543 ebitset_test (bitset src
, bitset_bindex bitno
)
545 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
547 return (ebitset_elt_find (src
, bitno
, EBITSET_FIND
)
548 && ((src
->b
.cdata
[windex
- src
->b
.cindex
]
549 >> (bitno
% BITSET_WORD_BITS
))
555 ebitset_free (bitset bset
)
558 free (EBITSET_ELTS (bset
));
562 /* Find list of up to NUM bits set in BSET starting from and including
563 *NEXT and store in array LIST. Return with actual number of bits
564 found and with *NEXT indicating where search stopped. */
566 ebitset_list_reverse (bitset bset
, bitset_bindex
*list
,
567 bitset_bindex num
, bitset_bindex
*next
)
569 bitset_bindex n_bits
;
571 bitset_bindex rbitno
;
573 bitset_bindex boffset
;
574 bitset_windex windex
;
575 bitset_windex eindex
;
576 bitset_windex woffset
;
581 if (EBITSET_ZERO_P (bset
))
584 size
= EBITSET_SIZE (bset
);
585 n_bits
= size
* EBITSET_ELT_BITS
;
588 if (rbitno
>= n_bits
)
591 elts
= EBITSET_ELTS (bset
);
593 bitno
= n_bits
- (rbitno
+ 1);
595 windex
= bitno
/ BITSET_WORD_BITS
;
596 eindex
= bitno
/ EBITSET_ELT_BITS
;
597 woffset
= windex
- eindex
* EBITSET_ELT_WORDS
;
599 /* If num is 1, we could speed things up with a binary search
600 of the word of interest. */
603 bcount
= bitno
% BITSET_WORD_BITS
;
604 boffset
= windex
* BITSET_WORD_BITS
;
614 srcp
= EBITSET_WORDS (elt
);
620 word
= srcp
[woffset
] << (BITSET_WORD_BITS
- 1 - bcount
);
622 for (; word
; bcount
--)
624 if (word
& BITSET_MSB
)
626 list
[count
++] = boffset
+ bcount
;
629 *next
= n_bits
- (boffset
+ bcount
);
635 boffset
-= BITSET_WORD_BITS
;
636 bcount
= BITSET_WORD_BITS
- 1;
641 woffset
= EBITSET_ELT_WORDS
- 1;
642 boffset
= eindex
* EBITSET_ELT_BITS
- BITSET_WORD_BITS
;
646 *next
= n_bits
- (boffset
+ 1);
651 /* Find list of up to NUM bits set in BSET starting from and including
652 *NEXT and store in array LIST. Return with actual number of bits
653 found and with *NEXT indicating where search stopped. */
655 ebitset_list (bitset bset
, bitset_bindex
*list
,
656 bitset_bindex num
, bitset_bindex
*next
)
659 bitset_windex windex
;
660 bitset_windex eindex
;
667 if (EBITSET_ZERO_P (bset
))
673 elts
= EBITSET_ELTS (bset
);
674 size
= EBITSET_SIZE (bset
);
675 eindex
= bitno
/ EBITSET_ELT_BITS
;
677 if (bitno
% EBITSET_ELT_BITS
)
679 /* We need to start within an element. This is not very common. */
684 bitset_windex woffset
;
685 bitset_word
*srcp
= EBITSET_WORDS (elt
);
687 windex
= bitno
/ BITSET_WORD_BITS
;
688 woffset
= eindex
* EBITSET_ELT_WORDS
;
690 for (; (windex
- woffset
) < EBITSET_ELT_WORDS
; windex
++)
692 word
= srcp
[windex
- woffset
] >> (bitno
% BITSET_WORD_BITS
);
694 for (; word
; bitno
++)
698 list
[count
++] = bitno
;
707 bitno
= (windex
+ 1) * BITSET_WORD_BITS
;
711 /* Skip to next element. */
715 /* If num is 1, we could speed things up with a binary search
716 of the word of interest. */
718 for (; eindex
< size
; eindex
++)
727 srcp
= EBITSET_WORDS (elt
);
728 windex
= eindex
* EBITSET_ELT_WORDS
;
730 if ((count
+ EBITSET_ELT_BITS
) < num
)
732 /* The coast is clear, plant boot! */
734 #if EBITSET_ELT_WORDS == 2
738 if (!(word
& 0xffff))
748 for (; word
; bitno
++)
751 list
[count
++] = bitno
;
756 bitno
= windex
* BITSET_WORD_BITS
;
761 if (!(word
& 0xffff))
766 for (; word
; bitno
++)
769 list
[count
++] = bitno
;
774 bitno
= windex
* BITSET_WORD_BITS
;
776 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, windex
++)
778 bitno
= windex
* BITSET_WORD_BITS
;
783 if (!(word
& 0xffff))
793 for (; word
; bitno
++)
796 list
[count
++] = bitno
;
805 /* Tread more carefully since we need to check
806 if array overflows. */
808 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, windex
++)
810 bitno
= windex
* BITSET_WORD_BITS
;
812 for (word
= srcp
[i
]; word
; bitno
++)
816 list
[count
++] = bitno
;
834 /* Ensure that any unused bits within the last element are clear. */
836 ebitset_unused_clear (bitset dst
)
838 unsigned int last_bit
;
839 bitset_bindex n_bits
;
841 n_bits
= BITSET_NBITS_ (dst
);
842 last_bit
= n_bits
% EBITSET_ELT_BITS
;
846 bitset_windex eindex
;
850 elts
= EBITSET_ELTS (dst
);
852 eindex
= n_bits
/ EBITSET_ELT_BITS
;
857 bitset_windex windex
;
858 bitset_windex woffset
;
859 bitset_word
*srcp
= EBITSET_WORDS (elt
);
861 windex
= n_bits
/ BITSET_WORD_BITS
;
862 woffset
= eindex
* EBITSET_ELT_WORDS
;
864 srcp
[windex
- woffset
] &= ((bitset_word
) 1 << last_bit
) - 1;
866 for (; (windex
- woffset
) < EBITSET_ELT_WORDS
; windex
++)
867 srcp
[windex
- woffset
] = 0;
874 ebitset_ones (bitset dst
)
879 for (j
= 0; j
< EBITSET_SIZE (dst
); j
++)
881 /* Create new elements if they cannot be found. Perhaps
882 we should just add pointers to a ones element? */
884 ebitset_elt_find (dst
, j
* EBITSET_ELT_BITS
, EBITSET_CREATE
);
885 memset (EBITSET_WORDS (elt
), -1, sizeof (EBITSET_WORDS (elt
)));
887 EBITSET_NONZERO_SET (dst
);
888 ebitset_unused_clear (dst
);
893 ebitset_empty_p (bitset dst
)
898 if (EBITSET_ZERO_P (dst
))
901 elts
= EBITSET_ELTS (dst
);
902 for (j
= 0; j
< EBITSET_SIZE (dst
); j
++)
904 ebitset_elt
*elt
= elts
[j
];
908 if (!ebitset_elt_zero_p (elt
))
910 /* Do some weeding as we go. */
911 ebitset_elt_remove (dst
, j
);
915 /* All the bits are zero. We could shrink the elts.
916 For now just mark DST as known to be zero. */
917 EBITSET_ZERO_SET (dst
);
923 ebitset_not (bitset dst
, bitset src
)
930 ebitset_resize (dst
, BITSET_NBITS_ (src
));
932 for (j
= 0; j
< EBITSET_SIZE (src
); j
++)
934 /* Create new elements for dst if they cannot be found
935 or substitute zero elements if src elements not found. */
937 ebitset_elt_find (dst
, j
* EBITSET_ELT_BITS
, EBITSET_SUBST
);
939 ebitset_elt_find (dst
, j
* EBITSET_ELT_BITS
, EBITSET_CREATE
);
941 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
942 EBITSET_WORDS (delt
)[i
] = ~EBITSET_WORDS (selt
)[i
];
944 EBITSET_NONZERO_SET (dst
);
945 ebitset_unused_clear (dst
);
949 /* Is DST == DST | SRC? */
951 ebitset_subset_p (bitset dst
, bitset src
)
959 selts
= EBITSET_ELTS (src
);
960 delts
= EBITSET_ELTS (dst
);
962 ssize
= EBITSET_SIZE (src
);
963 dsize
= EBITSET_SIZE (dst
);
965 for (j
= 0; j
< ssize
; j
++)
971 selt
= j
< ssize
? selts
[j
] : 0;
972 delt
= j
< dsize
? delts
[j
] : 0;
978 selt
= &ebitset_zero_elts
[0];
980 delt
= &ebitset_zero_elts
[0];
982 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
983 if (EBITSET_WORDS (delt
)[i
]
984 != (EBITSET_WORDS (selt
)[i
] | EBITSET_WORDS (delt
)[i
]))
991 /* Is DST & SRC == 0? */
993 ebitset_disjoint_p (bitset dst
, bitset src
)
1001 selts
= EBITSET_ELTS (src
);
1002 delts
= EBITSET_ELTS (dst
);
1004 ssize
= EBITSET_SIZE (src
);
1005 dsize
= EBITSET_SIZE (dst
);
1007 for (j
= 0; j
< ssize
; j
++)
1013 selt
= j
< ssize
? selts
[j
] : 0;
1014 delt
= j
< dsize
? delts
[j
] : 0;
1019 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
1020 if ((EBITSET_WORDS (selt
)[i
] & EBITSET_WORDS (delt
)[i
]))
1029 ebitset_op3_cmp (bitset dst
, bitset src1
, bitset src2
, enum bitset_ops op
)
1031 bitset_windex ssize1
;
1032 bitset_windex ssize2
;
1033 bitset_windex dsize
;
1035 ebitset_elts
*selts1
;
1036 ebitset_elts
*selts2
;
1037 ebitset_elts
*delts
;
1041 bool changed
= false;
1045 ebitset_resize (dst
, max (BITSET_NBITS_ (src1
), BITSET_NBITS_ (src2
)));
1047 ssize1
= EBITSET_SIZE (src1
);
1048 ssize2
= EBITSET_SIZE (src2
);
1049 dsize
= EBITSET_SIZE (dst
);
1054 selts1
= EBITSET_ELTS (src1
);
1055 selts2
= EBITSET_ELTS (src2
);
1056 delts
= EBITSET_ELTS (dst
);
1058 for (j
= 0; j
< size
; j
++)
1064 selt1
= j
< ssize1
? selts1
[j
] : 0;
1065 selt2
= j
< ssize2
? selts2
[j
] : 0;
1066 delt
= j
< dsize
? delts
[j
] : 0;
1068 if (!selt1
&& !selt2
)
1073 ebitset_elt_remove (dst
, j
);
1079 selt1
= &ebitset_zero_elts
[0];
1081 selt2
= &ebitset_zero_elts
[0];
1083 delt
= ebitset_elt_calloc ();
1087 srcp1
= EBITSET_WORDS (selt1
);
1088 srcp2
= EBITSET_WORDS (selt2
);
1089 dstp
= EBITSET_WORDS (delt
);
1096 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, dstp
++)
1098 bitset_word tmp
= *srcp1
++ | *srcp2
++;
1109 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, dstp
++)
1111 bitset_word tmp
= *srcp1
++ & *srcp2
++;
1122 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, dstp
++)
1124 bitset_word tmp
= *srcp1
++ ^ *srcp2
++;
1134 case BITSET_OP_ANDN
:
1135 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, dstp
++)
1137 bitset_word tmp
= *srcp1
++ & ~(*srcp2
++);
1148 if (!ebitset_elt_zero_p (delt
))
1150 ebitset_elt_add (dst
, delt
, j
);
1154 ebitset_elt_free (delt
);
1158 /* If we have elements of DST left over, free them all. */
1159 for (; j
< dsize
; j
++)
1168 ebitset_elt_remove (dst
, j
);
1171 EBITSET_NONZERO_SET (dst
);
1177 ebitset_and_cmp (bitset dst
, bitset src1
, bitset src2
)
1181 if (EBITSET_ZERO_P (src2
))
1184 changed
= EBITSET_ZERO_P (dst
);
1188 else if (EBITSET_ZERO_P (src1
))
1191 changed
= EBITSET_ZERO_P (dst
);
1195 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_AND
);
1200 ebitset_and (bitset dst
, bitset src1
, bitset src2
)
1202 ebitset_and_cmp (dst
, src1
, src2
);
1207 ebitset_andn_cmp (bitset dst
, bitset src1
, bitset src2
)
1211 if (EBITSET_ZERO_P (src2
))
1213 return ebitset_copy_cmp (dst
, src1
);
1215 else if (EBITSET_ZERO_P (src1
))
1218 changed
= EBITSET_ZERO_P (dst
);
1222 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_ANDN
);
1227 ebitset_andn (bitset dst
, bitset src1
, bitset src2
)
1229 ebitset_andn_cmp (dst
, src1
, src2
);
1234 ebitset_or_cmp (bitset dst
, bitset src1
, bitset src2
)
1236 if (EBITSET_ZERO_P (src2
))
1238 return ebitset_copy_cmp (dst
, src1
);
1240 else if (EBITSET_ZERO_P (src1
))
1242 return ebitset_copy_cmp (dst
, src2
);
1244 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_OR
);
1249 ebitset_or (bitset dst
, bitset src1
, bitset src2
)
1251 ebitset_or_cmp (dst
, src1
, src2
);
1256 ebitset_xor_cmp (bitset dst
, bitset src1
, bitset src2
)
1258 if (EBITSET_ZERO_P (src2
))
1260 return ebitset_copy_cmp (dst
, src1
);
1262 else if (EBITSET_ZERO_P (src1
))
1264 return ebitset_copy_cmp (dst
, src2
);
1266 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_XOR
);
1271 ebitset_xor (bitset dst
, bitset src1
, bitset src2
)
1273 ebitset_xor_cmp (dst
, src1
, src2
);
1278 ebitset_copy (bitset dst
, bitset src
)
1280 if (BITSET_COMPATIBLE_ (dst
, src
))
1281 ebitset_copy_ (dst
, src
);
1283 bitset_copy_ (dst
, src
);
1287 /* Vector of operations for linked-list bitsets. */
1288 struct bitset_vtable ebitset_vtable
= {
1315 bitset_andn_or_cmp_
,
1319 ebitset_list_reverse
,
1325 /* Return size of initial structure. */
1327 ebitset_bytes (bitset_bindex n_bits ATTRIBUTE_UNUSED
)
1329 return sizeof (struct ebitset_struct
);
1333 /* Initialize a bitset. */
1336 ebitset_init (bitset bset
, bitset_bindex n_bits
)
1340 bset
->b
.vtable
= &ebitset_vtable
;
1342 bset
->b
.csize
= EBITSET_ELT_WORDS
;
1344 EBITSET_ZERO_SET (bset
);
1346 size
= n_bits
? (n_bits
+ EBITSET_ELT_BITS
- 1) / EBITSET_ELT_BITS
1347 : EBITSET_INITIAL_SIZE
;
1349 EBITSET_ASIZE (bset
) = 0;
1350 EBITSET_ELTS (bset
) = 0;
1351 ebitset_resize (bset
, n_bits
);
1358 ebitset_release_memory (void)
1360 ebitset_free_list
= 0;
1361 if (ebitset_obstack_init
)
1363 ebitset_obstack_init
= false;
1364 obstack_free (&ebitset_obstack
, NULL
);