1 /* Functions to support expandable bitsets.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2009 Free Software
4 Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 /* This file implements expandable bitsets. These bitsets can be of
28 arbitrary length and are more efficient than arrays of bits for
31 Empty elements are represented by a NULL pointer in the table of
32 element pointers. An alternative is to point to a special zero
33 element. Similarly, we could represent an all 1's element with
34 another special ones element pointer.
36 Bitsets are commonly empty so we need to ensure that this special
37 case is fast. A zero bitset is indicated when cdata is 0. This is
38 conservative since cdata may be non zero and the bitset may still
41 The bitset cache can be disabled either by setting cindex to
42 BITSET_WINDEX_MAX or by setting csize to 0. Here
43 we use the former approach since cindex needs to be updated whenever
48 /* Number of words to use for each element. */
49 #define EBITSET_ELT_WORDS 2
51 /* Number of bits stored in each element. */
52 #define EBITSET_ELT_BITS \
53 ((unsigned int) (EBITSET_ELT_WORDS * BITSET_WORD_BITS))
55 /* Ebitset element. We use an array of bits. */
56 typedef struct ebitset_elt_struct
60 bitset_word words
[EBITSET_ELT_WORDS
]; /* Bits that are set. */
61 struct ebitset_elt_struct
*next
;
68 typedef ebitset_elt
*ebitset_elts
;
71 /* Number of elements to initially allocate. */
73 #ifndef EBITSET_INITIAL_SIZE
74 #define EBITSET_INITIAL_SIZE 2
78 enum ebitset_find_mode
79 { EBITSET_FIND
, EBITSET_CREATE
, EBITSET_SUBST
};
81 static ebitset_elt ebitset_zero_elts
[1]; /* Elements of all zero bits. */
83 /* Obstack to allocate bitset elements from. */
84 static struct obstack ebitset_obstack
;
85 static bool ebitset_obstack_init
= false;
86 static ebitset_elt
*ebitset_free_list
; /* Free list of bitset elements. */
88 #define EBITSET_N_ELTS(N) (((N) + EBITSET_ELT_BITS - 1) / EBITSET_ELT_BITS)
89 #define EBITSET_ELTS(BSET) ((BSET)->e.elts)
90 #define EBITSET_SIZE(BSET) EBITSET_N_ELTS (BITSET_NBITS_ (BSET))
91 #define EBITSET_ASIZE(BSET) ((BSET)->e.size)
93 #define EBITSET_NEXT(ELT) ((ELT)->u.next)
94 #define EBITSET_WORDS(ELT) ((ELT)->u.words)
96 /* Disable bitset cache and mark BSET as being zero. */
97 #define EBITSET_ZERO_SET(BSET) ((BSET)->b.cindex = BITSET_WINDEX_MAX, \
100 #define EBITSET_CACHE_DISABLE(BSET) ((BSET)->b.cindex = BITSET_WINDEX_MAX)
102 /* Disable bitset cache and mark BSET as being possibly non-zero. */
103 #define EBITSET_NONZERO_SET(BSET) \
104 (EBITSET_CACHE_DISABLE (BSET), (BSET)->b.cdata = (bitset_word *)~0)
106 /* A conservative estimate of whether the bitset is zero.
107 This is non-zero only if we know for sure that the bitset is zero. */
108 #define EBITSET_ZERO_P(BSET) ((BSET)->b.cdata == 0)
110 /* Enable cache to point to element with table index EINDEX.
111 The element must exist. */
112 #define EBITSET_CACHE_SET(BSET, EINDEX) \
113 ((BSET)->b.cindex = (EINDEX) * EBITSET_ELT_WORDS, \
114 (BSET)->b.cdata = EBITSET_WORDS (EBITSET_ELTS (BSET) [EINDEX]))
118 #define min(a, b) ((a) > (b) ? (b) : (a))
119 #define max(a, b) ((a) > (b) ? (a) : (b))
122 ebitset_resize (bitset src
, bitset_bindex n_bits
)
124 bitset_windex oldsize
;
125 bitset_windex newsize
;
127 if (n_bits
== BITSET_NBITS_ (src
))
130 oldsize
= EBITSET_SIZE (src
);
131 newsize
= EBITSET_N_ELTS (n_bits
);
133 if (oldsize
< newsize
)
137 /* The bitset needs to grow. If we already have enough memory
138 allocated, then just zero what we need. */
139 if (newsize
> EBITSET_ASIZE (src
))
141 /* We need to allocate more memory. When oldsize is
142 non-zero this means that we are changing the size, so
143 grow the bitset 25% larger than requested to reduce
144 number of reallocations. */
149 size
= newsize
+ newsize
/ 4;
152 = realloc (EBITSET_ELTS (src
), size
* sizeof (ebitset_elt
*));
153 EBITSET_ASIZE (src
) = size
;
156 memset (EBITSET_ELTS (src
) + oldsize
, 0,
157 (newsize
- oldsize
) * sizeof (ebitset_elt
*));
161 /* The bitset needs to shrink. There's no point deallocating
162 the memory unless it is shrinking by a reasonable amount. */
163 if ((oldsize
- newsize
) >= oldsize
/ 2)
166 = realloc (EBITSET_ELTS (src
), newsize
* sizeof (ebitset_elt
*));
167 EBITSET_ASIZE (src
) = newsize
;
170 /* Need to prune any excess bits. FIXME. */
173 BITSET_NBITS_ (src
) = n_bits
;
178 /* Allocate a ebitset element. The bits are not cleared. */
179 static inline ebitset_elt
*
180 ebitset_elt_alloc (void)
184 if (ebitset_free_list
!= 0)
186 elt
= ebitset_free_list
;
187 ebitset_free_list
= EBITSET_NEXT (elt
);
191 if (!ebitset_obstack_init
)
193 ebitset_obstack_init
= true;
195 /* Let particular systems override the size of a chunk. */
197 #ifndef OBSTACK_CHUNK_SIZE
198 #define OBSTACK_CHUNK_SIZE 0
201 /* Let them override the alloc and free routines too. */
203 #ifndef OBSTACK_CHUNK_ALLOC
204 #define OBSTACK_CHUNK_ALLOC xmalloc
207 #ifndef OBSTACK_CHUNK_FREE
208 #define OBSTACK_CHUNK_FREE free
211 #if ! defined __GNUC__ || __GNUC__ < 2
212 #define __alignof__(type) 0
215 obstack_specify_allocation (&ebitset_obstack
, OBSTACK_CHUNK_SIZE
,
216 __alignof__ (ebitset_elt
),
221 /* Perhaps we should add a number of new elements to the free
223 elt
= (ebitset_elt
*) obstack_alloc (&ebitset_obstack
,
224 sizeof (ebitset_elt
));
231 /* Allocate a ebitset element. The bits are cleared. */
232 static inline ebitset_elt
*
233 ebitset_elt_calloc (void)
237 elt
= ebitset_elt_alloc ();
238 memset (EBITSET_WORDS (elt
), 0, sizeof (EBITSET_WORDS (elt
)));
244 ebitset_elt_free (ebitset_elt
*elt
)
246 EBITSET_NEXT (elt
) = ebitset_free_list
;
247 ebitset_free_list
= elt
;
251 /* Remove element with index EINDEX from bitset BSET. */
253 ebitset_elt_remove (bitset bset
, bitset_windex eindex
)
258 elts
= EBITSET_ELTS (bset
);
263 ebitset_elt_free (elt
);
267 /* Add ELT into elts at index EINDEX of bitset BSET. */
269 ebitset_elt_add (bitset bset
, ebitset_elt
*elt
, bitset_windex eindex
)
273 elts
= EBITSET_ELTS (bset
);
274 /* Assume that the elts entry not allocated. */
279 /* Are all bits in an element zero? */
281 ebitset_elt_zero_p (ebitset_elt
*elt
)
285 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
286 if (EBITSET_WORDS (elt
)[i
])
294 ebitset_elt_find (bitset bset
, bitset_bindex bindex
,
295 enum ebitset_find_mode mode
)
299 bitset_windex eindex
;
302 eindex
= bindex
/ EBITSET_ELT_BITS
;
304 elts
= EBITSET_ELTS (bset
);
305 size
= EBITSET_SIZE (bset
);
309 if ((elt
= elts
[eindex
]))
311 if (EBITSET_WORDS (elt
) == bset
->b
.cdata
)
314 EBITSET_CACHE_SET (bset
, eindex
);
319 /* The element could not be found. */
331 ebitset_resize (bset
, bindex
);
333 /* Create a new element. */
334 elt
= ebitset_elt_calloc ();
335 ebitset_elt_add (bset
, elt
, eindex
);
336 EBITSET_CACHE_SET (bset
, eindex
);
340 return &ebitset_zero_elts
[0];
345 /* Weed out the zero elements from the elts. */
346 static inline bitset_windex
347 ebitset_weed (bitset bset
)
353 if (EBITSET_ZERO_P (bset
))
356 elts
= EBITSET_ELTS (bset
);
358 for (j
= 0; j
< EBITSET_SIZE (bset
); j
++)
360 ebitset_elt
*elt
= elts
[j
];
364 if (ebitset_elt_zero_p (elt
))
366 ebitset_elt_remove (bset
, j
);
377 /* All the bits are zero. We could shrink the elts.
378 For now just mark BSET as known to be zero. */
379 EBITSET_ZERO_SET (bset
);
382 EBITSET_NONZERO_SET (bset
);
388 /* Set all bits in the bitset to zero. */
390 ebitset_zero (bitset bset
)
395 if (EBITSET_ZERO_P (bset
))
398 elts
= EBITSET_ELTS (bset
);
399 for (j
= 0; j
< EBITSET_SIZE (bset
); j
++)
401 ebitset_elt
*elt
= elts
[j
];
404 ebitset_elt_remove (bset
, j
);
407 /* All the bits are zero. We could shrink the elts.
408 For now just mark BSET as known to be zero. */
409 EBITSET_ZERO_SET (bset
);
414 ebitset_equal_p (bitset dst
, bitset src
)
426 if (EBITSET_SIZE (src
) != EBITSET_SIZE (dst
))
429 selts
= EBITSET_ELTS (src
);
430 delts
= EBITSET_ELTS (dst
);
432 for (j
= 0; j
< EBITSET_SIZE (src
); j
++)
435 ebitset_elt
*selt
= selts
[j
];
436 ebitset_elt
*delt
= delts
[j
];
440 if ((selt
&& !delt
) || (!selt
&& delt
))
443 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
444 if (EBITSET_WORDS (selt
)[i
] != EBITSET_WORDS (delt
)[i
])
451 /* Copy bits from bitset SRC to bitset DST. */
453 ebitset_copy_ (bitset dst
, bitset src
)
464 if (BITSET_NBITS_ (dst
) != BITSET_NBITS_ (src
))
465 ebitset_resize (dst
, BITSET_NBITS_ (src
));
467 selts
= EBITSET_ELTS (src
);
468 delts
= EBITSET_ELTS (dst
);
469 for (j
= 0; j
< EBITSET_SIZE (src
); j
++)
471 ebitset_elt
*selt
= selts
[j
];
477 tmp
= ebitset_elt_alloc ();
479 memcpy (EBITSET_WORDS (tmp
), EBITSET_WORDS (selt
),
480 sizeof (EBITSET_WORDS (selt
)));
483 EBITSET_NONZERO_SET (dst
);
487 /* Copy bits from bitset SRC to bitset DST. Return true if
488 bitsets different. */
490 ebitset_copy_cmp (bitset dst
, bitset src
)
495 if (EBITSET_ZERO_P (dst
))
497 ebitset_copy_ (dst
, src
);
498 return !EBITSET_ZERO_P (src
);
501 if (ebitset_equal_p (dst
, src
))
504 ebitset_copy_ (dst
, src
);
509 /* Set bit BITNO in bitset DST. */
511 ebitset_set (bitset dst
, bitset_bindex bitno
)
513 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
515 ebitset_elt_find (dst
, bitno
, EBITSET_CREATE
);
517 dst
->b
.cdata
[windex
- dst
->b
.cindex
] |=
518 (bitset_word
) 1 << (bitno
% BITSET_WORD_BITS
);
522 /* Reset bit BITNO in bitset DST. */
524 ebitset_reset (bitset dst
, bitset_bindex bitno
)
526 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
528 if (!ebitset_elt_find (dst
, bitno
, EBITSET_FIND
))
531 dst
->b
.cdata
[windex
- dst
->b
.cindex
] &=
532 ~((bitset_word
) 1 << (bitno
% BITSET_WORD_BITS
));
534 /* If all the data is zero, perhaps we should remove it now...
535 However, there is a good chance that the element will be needed
540 /* Test bit BITNO in bitset SRC. */
542 ebitset_test (bitset src
, bitset_bindex bitno
)
544 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
546 return (ebitset_elt_find (src
, bitno
, EBITSET_FIND
)
547 && ((src
->b
.cdata
[windex
- src
->b
.cindex
]
548 >> (bitno
% BITSET_WORD_BITS
))
554 ebitset_free (bitset bset
)
557 free (EBITSET_ELTS (bset
));
561 /* Find list of up to NUM bits set in BSET starting from and including
562 *NEXT and store in array LIST. Return with actual number of bits
563 found and with *NEXT indicating where search stopped. */
565 ebitset_list_reverse (bitset bset
, bitset_bindex
*list
,
566 bitset_bindex num
, bitset_bindex
*next
)
568 bitset_bindex n_bits
;
570 bitset_bindex rbitno
;
572 bitset_bindex boffset
;
573 bitset_windex windex
;
574 bitset_windex eindex
;
575 bitset_windex woffset
;
580 if (EBITSET_ZERO_P (bset
))
583 size
= EBITSET_SIZE (bset
);
584 n_bits
= size
* EBITSET_ELT_BITS
;
587 if (rbitno
>= n_bits
)
590 elts
= EBITSET_ELTS (bset
);
592 bitno
= n_bits
- (rbitno
+ 1);
594 windex
= bitno
/ BITSET_WORD_BITS
;
595 eindex
= bitno
/ EBITSET_ELT_BITS
;
596 woffset
= windex
- eindex
* EBITSET_ELT_WORDS
;
598 /* If num is 1, we could speed things up with a binary search
599 of the word of interest. */
602 bcount
= bitno
% BITSET_WORD_BITS
;
603 boffset
= windex
* BITSET_WORD_BITS
;
613 srcp
= EBITSET_WORDS (elt
);
619 word
= srcp
[woffset
] << (BITSET_WORD_BITS
- 1 - bcount
);
621 for (; word
; bcount
--)
623 if (word
& BITSET_MSB
)
625 list
[count
++] = boffset
+ bcount
;
628 *next
= n_bits
- (boffset
+ bcount
);
634 boffset
-= BITSET_WORD_BITS
;
635 bcount
= BITSET_WORD_BITS
- 1;
640 woffset
= EBITSET_ELT_WORDS
- 1;
641 boffset
= eindex
* EBITSET_ELT_BITS
- BITSET_WORD_BITS
;
645 *next
= n_bits
- (boffset
+ 1);
650 /* Find list of up to NUM bits set in BSET starting from and including
651 *NEXT and store in array LIST. Return with actual number of bits
652 found and with *NEXT indicating where search stopped. */
654 ebitset_list (bitset bset
, bitset_bindex
*list
,
655 bitset_bindex num
, bitset_bindex
*next
)
658 bitset_windex windex
;
659 bitset_windex eindex
;
666 if (EBITSET_ZERO_P (bset
))
672 elts
= EBITSET_ELTS (bset
);
673 size
= EBITSET_SIZE (bset
);
674 eindex
= bitno
/ EBITSET_ELT_BITS
;
676 if (bitno
% EBITSET_ELT_BITS
)
678 /* We need to start within an element. This is not very common. */
683 bitset_windex woffset
;
684 bitset_word
*srcp
= EBITSET_WORDS (elt
);
686 windex
= bitno
/ BITSET_WORD_BITS
;
687 woffset
= eindex
* EBITSET_ELT_WORDS
;
689 for (; (windex
- woffset
) < EBITSET_ELT_WORDS
; windex
++)
691 word
= srcp
[windex
- woffset
] >> (bitno
% BITSET_WORD_BITS
);
693 for (; word
; bitno
++)
697 list
[count
++] = bitno
;
706 bitno
= (windex
+ 1) * BITSET_WORD_BITS
;
710 /* Skip to next element. */
714 /* If num is 1, we could speed things up with a binary search
715 of the word of interest. */
717 for (; eindex
< size
; eindex
++)
726 srcp
= EBITSET_WORDS (elt
);
727 windex
= eindex
* EBITSET_ELT_WORDS
;
729 if ((count
+ EBITSET_ELT_BITS
) < num
)
731 /* The coast is clear, plant boot! */
733 #if EBITSET_ELT_WORDS == 2
737 if (!(word
& 0xffff))
747 for (; word
; bitno
++)
750 list
[count
++] = bitno
;
755 bitno
= windex
* BITSET_WORD_BITS
;
760 if (!(word
& 0xffff))
765 for (; word
; bitno
++)
768 list
[count
++] = bitno
;
773 bitno
= windex
* BITSET_WORD_BITS
;
775 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, windex
++)
777 bitno
= windex
* BITSET_WORD_BITS
;
782 if (!(word
& 0xffff))
792 for (; word
; bitno
++)
795 list
[count
++] = bitno
;
804 /* Tread more carefully since we need to check
805 if array overflows. */
807 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, windex
++)
809 bitno
= windex
* BITSET_WORD_BITS
;
811 for (word
= srcp
[i
]; word
; bitno
++)
815 list
[count
++] = bitno
;
833 /* Ensure that any unused bits within the last element are clear. */
835 ebitset_unused_clear (bitset dst
)
837 unsigned int last_bit
;
838 bitset_bindex n_bits
;
840 n_bits
= BITSET_NBITS_ (dst
);
841 last_bit
= n_bits
% EBITSET_ELT_BITS
;
845 bitset_windex eindex
;
849 elts
= EBITSET_ELTS (dst
);
851 eindex
= n_bits
/ EBITSET_ELT_BITS
;
856 bitset_windex windex
;
857 bitset_windex woffset
;
858 bitset_word
*srcp
= EBITSET_WORDS (elt
);
860 windex
= n_bits
/ BITSET_WORD_BITS
;
861 woffset
= eindex
* EBITSET_ELT_WORDS
;
863 srcp
[windex
- woffset
] &= ((bitset_word
) 1 << last_bit
) - 1;
865 for (; (windex
- woffset
) < EBITSET_ELT_WORDS
; windex
++)
866 srcp
[windex
- woffset
] = 0;
873 ebitset_ones (bitset dst
)
878 for (j
= 0; j
< EBITSET_SIZE (dst
); j
++)
880 /* Create new elements if they cannot be found. Perhaps
881 we should just add pointers to a ones element? */
883 ebitset_elt_find (dst
, j
* EBITSET_ELT_BITS
, EBITSET_CREATE
);
884 memset (EBITSET_WORDS (elt
), -1, sizeof (EBITSET_WORDS (elt
)));
886 EBITSET_NONZERO_SET (dst
);
887 ebitset_unused_clear (dst
);
892 ebitset_empty_p (bitset dst
)
897 if (EBITSET_ZERO_P (dst
))
900 elts
= EBITSET_ELTS (dst
);
901 for (j
= 0; j
< EBITSET_SIZE (dst
); j
++)
903 ebitset_elt
*elt
= elts
[j
];
907 if (!ebitset_elt_zero_p (elt
))
909 /* Do some weeding as we go. */
910 ebitset_elt_remove (dst
, j
);
914 /* All the bits are zero. We could shrink the elts.
915 For now just mark DST as known to be zero. */
916 EBITSET_ZERO_SET (dst
);
922 ebitset_not (bitset dst
, bitset src
)
929 ebitset_resize (dst
, BITSET_NBITS_ (src
));
931 for (j
= 0; j
< EBITSET_SIZE (src
); j
++)
933 /* Create new elements for dst if they cannot be found
934 or substitute zero elements if src elements not found. */
936 ebitset_elt_find (dst
, j
* EBITSET_ELT_BITS
, EBITSET_SUBST
);
938 ebitset_elt_find (dst
, j
* EBITSET_ELT_BITS
, EBITSET_CREATE
);
940 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
941 EBITSET_WORDS (delt
)[i
] = ~EBITSET_WORDS (selt
)[i
];
943 EBITSET_NONZERO_SET (dst
);
944 ebitset_unused_clear (dst
);
948 /* Is DST == DST | SRC? */
950 ebitset_subset_p (bitset dst
, bitset src
)
958 selts
= EBITSET_ELTS (src
);
959 delts
= EBITSET_ELTS (dst
);
961 ssize
= EBITSET_SIZE (src
);
962 dsize
= EBITSET_SIZE (dst
);
964 for (j
= 0; j
< ssize
; j
++)
970 selt
= j
< ssize
? selts
[j
] : 0;
971 delt
= j
< dsize
? delts
[j
] : 0;
977 selt
= &ebitset_zero_elts
[0];
979 delt
= &ebitset_zero_elts
[0];
981 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
982 if (EBITSET_WORDS (delt
)[i
]
983 != (EBITSET_WORDS (selt
)[i
] | EBITSET_WORDS (delt
)[i
]))
990 /* Is DST & SRC == 0? */
992 ebitset_disjoint_p (bitset dst
, bitset src
)
1000 selts
= EBITSET_ELTS (src
);
1001 delts
= EBITSET_ELTS (dst
);
1003 ssize
= EBITSET_SIZE (src
);
1004 dsize
= EBITSET_SIZE (dst
);
1006 for (j
= 0; j
< ssize
; j
++)
1012 selt
= j
< ssize
? selts
[j
] : 0;
1013 delt
= j
< dsize
? delts
[j
] : 0;
1018 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
1019 if ((EBITSET_WORDS (selt
)[i
] & EBITSET_WORDS (delt
)[i
]))
1028 ebitset_op3_cmp (bitset dst
, bitset src1
, bitset src2
, enum bitset_ops op
)
1030 bitset_windex ssize1
;
1031 bitset_windex ssize2
;
1032 bitset_windex dsize
;
1034 ebitset_elts
*selts1
;
1035 ebitset_elts
*selts2
;
1036 ebitset_elts
*delts
;
1040 bool changed
= false;
1044 ebitset_resize (dst
, max (BITSET_NBITS_ (src1
), BITSET_NBITS_ (src2
)));
1046 ssize1
= EBITSET_SIZE (src1
);
1047 ssize2
= EBITSET_SIZE (src2
);
1048 dsize
= EBITSET_SIZE (dst
);
1053 selts1
= EBITSET_ELTS (src1
);
1054 selts2
= EBITSET_ELTS (src2
);
1055 delts
= EBITSET_ELTS (dst
);
1057 for (j
= 0; j
< size
; j
++)
1063 selt1
= j
< ssize1
? selts1
[j
] : 0;
1064 selt2
= j
< ssize2
? selts2
[j
] : 0;
1065 delt
= j
< dsize
? delts
[j
] : 0;
1067 if (!selt1
&& !selt2
)
1072 ebitset_elt_remove (dst
, j
);
1078 selt1
= &ebitset_zero_elts
[0];
1080 selt2
= &ebitset_zero_elts
[0];
1082 delt
= ebitset_elt_calloc ();
1086 srcp1
= EBITSET_WORDS (selt1
);
1087 srcp2
= EBITSET_WORDS (selt2
);
1088 dstp
= EBITSET_WORDS (delt
);
1095 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, dstp
++)
1097 bitset_word tmp
= *srcp1
++ | *srcp2
++;
1108 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, dstp
++)
1110 bitset_word tmp
= *srcp1
++ & *srcp2
++;
1121 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, dstp
++)
1123 bitset_word tmp
= *srcp1
++ ^ *srcp2
++;
1133 case BITSET_OP_ANDN
:
1134 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, dstp
++)
1136 bitset_word tmp
= *srcp1
++ & ~(*srcp2
++);
1147 if (!ebitset_elt_zero_p (delt
))
1149 ebitset_elt_add (dst
, delt
, j
);
1153 ebitset_elt_free (delt
);
1157 /* If we have elements of DST left over, free them all. */
1158 for (; j
< dsize
; j
++)
1167 ebitset_elt_remove (dst
, j
);
1170 EBITSET_NONZERO_SET (dst
);
1176 ebitset_and_cmp (bitset dst
, bitset src1
, bitset src2
)
1180 if (EBITSET_ZERO_P (src2
))
1183 changed
= EBITSET_ZERO_P (dst
);
1187 else if (EBITSET_ZERO_P (src1
))
1190 changed
= EBITSET_ZERO_P (dst
);
1194 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_AND
);
1199 ebitset_and (bitset dst
, bitset src1
, bitset src2
)
1201 ebitset_and_cmp (dst
, src1
, src2
);
1206 ebitset_andn_cmp (bitset dst
, bitset src1
, bitset src2
)
1210 if (EBITSET_ZERO_P (src2
))
1212 return ebitset_copy_cmp (dst
, src1
);
1214 else if (EBITSET_ZERO_P (src1
))
1217 changed
= EBITSET_ZERO_P (dst
);
1221 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_ANDN
);
1226 ebitset_andn (bitset dst
, bitset src1
, bitset src2
)
1228 ebitset_andn_cmp (dst
, src1
, src2
);
1233 ebitset_or_cmp (bitset dst
, bitset src1
, bitset src2
)
1235 if (EBITSET_ZERO_P (src2
))
1237 return ebitset_copy_cmp (dst
, src1
);
1239 else if (EBITSET_ZERO_P (src1
))
1241 return ebitset_copy_cmp (dst
, src2
);
1243 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_OR
);
1248 ebitset_or (bitset dst
, bitset src1
, bitset src2
)
1250 ebitset_or_cmp (dst
, src1
, src2
);
1255 ebitset_xor_cmp (bitset dst
, bitset src1
, bitset src2
)
1257 if (EBITSET_ZERO_P (src2
))
1259 return ebitset_copy_cmp (dst
, src1
);
1261 else if (EBITSET_ZERO_P (src1
))
1263 return ebitset_copy_cmp (dst
, src2
);
1265 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_XOR
);
1270 ebitset_xor (bitset dst
, bitset src1
, bitset src2
)
1272 ebitset_xor_cmp (dst
, src1
, src2
);
1277 ebitset_copy (bitset dst
, bitset src
)
1279 if (BITSET_COMPATIBLE_ (dst
, src
))
1280 ebitset_copy_ (dst
, src
);
1282 bitset_copy_ (dst
, src
);
1286 /* Vector of operations for linked-list bitsets. */
1287 struct bitset_vtable ebitset_vtable
= {
1314 bitset_andn_or_cmp_
,
1318 ebitset_list_reverse
,
1324 /* Return size of initial structure. */
1326 ebitset_bytes (bitset_bindex n_bits ATTRIBUTE_UNUSED
)
1328 return sizeof (struct ebitset_struct
);
1332 /* Initialize a bitset. */
1335 ebitset_init (bitset bset
, bitset_bindex n_bits
)
1339 bset
->b
.vtable
= &ebitset_vtable
;
1341 bset
->b
.csize
= EBITSET_ELT_WORDS
;
1343 EBITSET_ZERO_SET (bset
);
1345 size
= n_bits
? (n_bits
+ EBITSET_ELT_BITS
- 1) / EBITSET_ELT_BITS
1346 : EBITSET_INITIAL_SIZE
;
1348 EBITSET_ASIZE (bset
) = 0;
1349 EBITSET_ELTS (bset
) = 0;
1350 ebitset_resize (bset
, n_bits
);
1357 ebitset_release_memory (void)
1359 ebitset_free_list
= 0;
1360 if (ebitset_obstack_init
)
1362 ebitset_obstack_init
= false;
1363 obstack_free (&ebitset_obstack
, NULL
);