1 /* Functions to support expandable bitsets.
2 Copyright (C) 2002, 2003 Free Software Foundation, Inc.
3 Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
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.
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
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /* This file implements expandable bitsets. These bitsets can be of
30 arbitrary length and are more efficient than arrays of bits for
33 Empty elements are represented by a NULL pointer in the table of
34 element pointers. An alternative is to point to a special zero
35 element. Similarly, we could represent an all 1's element with
36 another special ones element pointer.
38 Bitsets are commonly empty so we need to ensure that this special
39 case is fast. A zero bitset is indicated when cdata is 0. This is
40 conservative since cdata may be non zero and the bitset may still
43 The bitset cache can be disabled either by setting cindex to
44 BITSET_WINDEX_MAX or by setting csize to 0. Here
45 we use the former approach since cindex needs to be updated whenever
50 /* Number of words to use for each element. */
51 #define EBITSET_ELT_WORDS 2
53 /* Number of bits stored in each element. */
54 #define EBITSET_ELT_BITS \
55 ((unsigned) (EBITSET_ELT_WORDS * BITSET_WORD_BITS))
57 /* Ebitset element. We use an array of bits. */
58 typedef struct ebitset_elt_struct
62 bitset_word words
[EBITSET_ELT_WORDS
]; /* Bits that are set. */
63 struct ebitset_elt_struct
*next
;
70 typedef ebitset_elt
*ebitset_elts
;
73 /* Number of elements to initially allocate. */
75 #ifndef EBITSET_INITIAL_SIZE
76 #define EBITSET_INITIAL_SIZE 2
80 enum ebitset_find_mode
81 { EBITSET_FIND
, EBITSET_CREATE
, EBITSET_SUBST
};
83 static ebitset_elt ebitset_zero_elts
[1]; /* Elements of all zero bits. */
85 /* Obstack to allocate bitset elements from. */
86 static struct obstack ebitset_obstack
;
87 static bool ebitset_obstack_init
= false;
88 static ebitset_elt
*ebitset_free_list
; /* Free list of bitset elements. */
90 #define EBITSET_N_ELTS(N) (((N) + EBITSET_ELT_BITS - 1) / EBITSET_ELT_BITS)
91 #define EBITSET_ELTS(BSET) ((BSET)->e.elts)
92 #define EBITSET_SIZE(BSET) EBITSET_N_ELTS (BITSET_NBITS_ (BSET))
93 #define EBITSET_ASIZE(BSET) ((BSET)->e.size)
95 #define EBITSET_NEXT(ELT) ((ELT)->u.next)
96 #define EBITSET_WORDS(ELT) ((ELT)->u.words)
98 /* Disable bitset cache and mark BSET as being zero. */
99 #define EBITSET_ZERO_SET(BSET) ((BSET)->b.cindex = BITSET_WINDEX_MAX, \
102 #define EBITSET_CACHE_DISABLE(BSET) ((BSET)->b.cindex = BITSET_WINDEX_MAX)
104 /* Disable bitset cache and mark BSET as being possibly non-zero. */
105 #define EBITSET_NONZERO_SET(BSET) \
106 (EBITSET_CACHE_DISABLE (BSET), (BSET)->b.cdata = (bitset_word *)~0)
108 /* A conservative estimate of whether the bitset is zero.
109 This is non-zero only if we know for sure that the bitset is zero. */
110 #define EBITSET_ZERO_P(BSET) ((BSET)->b.cdata == 0)
112 /* Enable cache to point to element with table index EINDEX.
113 The element must exist. */
114 #define EBITSET_CACHE_SET(BSET, EINDEX) \
115 ((BSET)->b.cindex = (EINDEX) * EBITSET_ELT_WORDS, \
116 (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))
124 ebitset_resize (src
, n_bits
)
126 bitset_bindex n_bits
;
128 bitset_windex oldsize
;
129 bitset_windex newsize
;
131 if (n_bits
== BITSET_NBITS_ (src
))
134 oldsize
= EBITSET_SIZE (src
);
135 newsize
= EBITSET_N_ELTS (n_bits
);
137 if (oldsize
< newsize
)
141 /* The bitset needs to grow. If we already have enough memory
142 allocated, then just zero what we need. */
143 if (newsize
> EBITSET_ASIZE (src
))
145 /* We need to allocate more memory. When oldsize is
146 non-zero this means that we are changing the size, so
147 grow the bitset 25% larger than requested to reduce
148 number of reallocations. */
153 size
= newsize
+ newsize
/ 4;
156 = realloc (EBITSET_ELTS (src
), size
* sizeof (ebitset_elt
*));
157 EBITSET_ASIZE (src
) = size
;
160 memset (EBITSET_ELTS (src
) + oldsize
, 0,
161 (newsize
- oldsize
) * sizeof (ebitset_elt
*));
165 /* The bitset needs to shrink. There's no point deallocating
166 the memory unless it is shrinking by a reasonable amount. */
167 if ((oldsize
- newsize
) >= oldsize
/ 2)
170 = realloc (EBITSET_ELTS (src
), newsize
* sizeof (ebitset_elt
*));
171 EBITSET_ASIZE (src
) = newsize
;
174 /* Need to prune any excess bits. FIXME. */
177 BITSET_NBITS_ (src
) = n_bits
;
182 /* Allocate a ebitset element. The bits are not cleared. */
183 static inline ebitset_elt
*
184 ebitset_elt_alloc (void)
188 if (ebitset_free_list
!= 0)
190 elt
= ebitset_free_list
;
191 ebitset_free_list
= EBITSET_NEXT (elt
);
195 if (!ebitset_obstack_init
)
197 ebitset_obstack_init
= true;
199 /* Let particular systems override the size of a chunk. */
201 #ifndef OBSTACK_CHUNK_SIZE
202 #define OBSTACK_CHUNK_SIZE 0
205 /* Let them override the alloc and free routines too. */
207 #ifndef OBSTACK_CHUNK_ALLOC
208 #define OBSTACK_CHUNK_ALLOC xmalloc
211 #ifndef OBSTACK_CHUNK_FREE
212 #define OBSTACK_CHUNK_FREE free
215 #if !defined(__GNUC__) || (__GNUC__ < 2)
216 #define __alignof__(type) 0
219 obstack_specify_allocation (&ebitset_obstack
, OBSTACK_CHUNK_SIZE
,
220 __alignof__ (ebitset_elt
),
221 (void *(*)PARAMS ((long)))
223 (void (*)PARAMS ((void *)))
227 /* Perhaps we should add a number of new elements to the free
229 elt
= (ebitset_elt
*) obstack_alloc (&ebitset_obstack
,
230 sizeof (ebitset_elt
));
237 /* Allocate a ebitset element. The bits are cleared. */
238 static inline ebitset_elt
*
239 ebitset_elt_calloc (void)
243 elt
= ebitset_elt_alloc ();
244 memset (EBITSET_WORDS (elt
), 0, sizeof (EBITSET_WORDS (elt
)));
250 ebitset_elt_free (ebitset_elt
*elt
)
252 EBITSET_NEXT (elt
) = ebitset_free_list
;
253 ebitset_free_list
= elt
;
257 /* Remove element with index EINDEX from bitset BSET. */
259 ebitset_elt_remove (bitset bset
, bitset_windex eindex
)
264 elts
= EBITSET_ELTS (bset
);
269 ebitset_elt_free (elt
);
273 /* Add ELT into elts at index EINDEX of bitset BSET. */
275 ebitset_elt_add (bitset bset
, ebitset_elt
*elt
, bitset_windex eindex
)
279 elts
= EBITSET_ELTS (bset
);
280 /* Assume that the elts entry not allocated. */
285 /* Are all bits in an element zero? */
287 ebitset_elt_zero_p (ebitset_elt
*elt
)
291 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
292 if (EBITSET_WORDS (elt
)[i
])
300 ebitset_elt_find (bitset bset
, bitset_bindex bindex
,
301 enum ebitset_find_mode mode
)
305 bitset_windex eindex
;
308 eindex
= bindex
/ EBITSET_ELT_BITS
;
310 elts
= EBITSET_ELTS (bset
);
311 size
= EBITSET_SIZE (bset
);
315 if ((elt
= elts
[eindex
]))
317 if (EBITSET_WORDS (elt
) == bset
->b
.cdata
)
320 EBITSET_CACHE_SET (bset
, eindex
);
325 /* The element could not be found. */
334 ebitset_resize (bset
, bindex
);
336 /* Create a new element. */
337 elt
= ebitset_elt_calloc ();
338 ebitset_elt_add (bset
, elt
, eindex
);
339 EBITSET_CACHE_SET (bset
, eindex
);
343 return &ebitset_zero_elts
[0];
351 /* Weed out the zero elements from the elts. */
352 static inline bitset_windex
353 ebitset_weed (bitset bset
)
359 if (EBITSET_ZERO_P (bset
))
362 elts
= EBITSET_ELTS (bset
);
364 for (j
= 0; j
< EBITSET_SIZE (bset
); j
++)
366 ebitset_elt
*elt
= elts
[j
];
370 if (ebitset_elt_zero_p (elt
))
372 ebitset_elt_remove (bset
, j
);
383 /* All the bits are zero. We could shrink the elts.
384 For now just mark BSET as known to be zero. */
385 EBITSET_ZERO_SET (bset
);
388 EBITSET_NONZERO_SET (bset
);
394 /* Set all bits in the bitset to zero. */
396 ebitset_zero (bitset bset
)
401 if (EBITSET_ZERO_P (bset
))
404 elts
= EBITSET_ELTS (bset
);
405 for (j
= 0; j
< EBITSET_SIZE (bset
); j
++)
407 ebitset_elt
*elt
= elts
[j
];
410 ebitset_elt_remove (bset
, j
);
413 /* All the bits are zero. We could shrink the elts.
414 For now just mark BSET as known to be zero. */
415 EBITSET_ZERO_SET (bset
);
420 ebitset_equal_p (bitset dst
, bitset src
)
432 if (EBITSET_SIZE (src
) != EBITSET_SIZE (dst
))
435 selts
= EBITSET_ELTS (src
);
436 delts
= EBITSET_ELTS (dst
);
438 for (j
= 0; j
< EBITSET_SIZE (src
); j
++)
441 ebitset_elt
*selt
= selts
[j
];
442 ebitset_elt
*delt
= delts
[j
];
446 if ((selt
&& !delt
) || (!selt
&& delt
))
449 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
450 if (EBITSET_WORDS (selt
)[i
] != EBITSET_WORDS (delt
)[i
])
457 /* Copy bits from bitset SRC to bitset DST. */
459 ebitset_copy_ (bitset dst
, bitset src
)
470 if (BITSET_NBITS_ (dst
) != BITSET_NBITS_ (src
))
471 ebitset_resize (dst
, BITSET_NBITS_ (src
));
473 selts
= EBITSET_ELTS (src
);
474 delts
= EBITSET_ELTS (dst
);
475 for (j
= 0; j
< EBITSET_SIZE (src
); j
++)
477 ebitset_elt
*selt
= selts
[j
];
483 tmp
= ebitset_elt_alloc ();
485 memcpy (EBITSET_WORDS (tmp
), EBITSET_WORDS (selt
),
486 sizeof (EBITSET_WORDS (selt
)));
489 EBITSET_NONZERO_SET (dst
);
493 /* Copy bits from bitset SRC to bitset DST. Return true if
494 bitsets different. */
496 ebitset_copy_cmp (bitset dst
, bitset src
)
501 if (EBITSET_ZERO_P (dst
))
503 ebitset_copy_ (dst
, src
);
504 return !EBITSET_ZERO_P (src
);
507 if (ebitset_equal_p (dst
, src
))
510 ebitset_copy_ (dst
, src
);
515 /* Set bit BITNO in bitset DST. */
517 ebitset_set (bitset dst
, bitset_bindex bitno
)
519 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
521 ebitset_elt_find (dst
, bitno
, EBITSET_CREATE
);
523 dst
->b
.cdata
[windex
- dst
->b
.cindex
] |=
524 (bitset_word
) 1 << (bitno
% BITSET_WORD_BITS
);
528 /* Reset bit BITNO in bitset DST. */
530 ebitset_reset (bitset dst
, bitset_bindex bitno
)
532 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
534 if (!ebitset_elt_find (dst
, bitno
, EBITSET_FIND
))
537 dst
->b
.cdata
[windex
- dst
->b
.cindex
] &=
538 ~((bitset_word
) 1 << (bitno
% BITSET_WORD_BITS
));
540 /* If all the data is zero, perhaps we should remove it now...
541 However, there is a good chance that the element will be needed
546 /* Test bit BITNO in bitset SRC. */
548 ebitset_test (bitset src
, bitset_bindex bitno
)
550 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
552 return (ebitset_elt_find (src
, bitno
, EBITSET_FIND
)
553 && ((src
->b
.cdata
[windex
- src
->b
.cindex
]
554 >> (bitno
% BITSET_WORD_BITS
))
560 ebitset_free (bitset bset
)
563 free (EBITSET_ELTS (bset
));
567 /* Find list of up to NUM bits set in BSET starting from and including
568 *NEXT and store in array LIST. Return with actual number of bits
569 found and with *NEXT indicating where search stopped. */
571 ebitset_list_reverse (bitset bset
, bitset_bindex
*list
,
572 bitset_bindex num
, bitset_bindex
*next
)
574 bitset_bindex n_bits
;
576 bitset_bindex rbitno
;
578 bitset_bindex boffset
;
579 bitset_windex windex
;
580 bitset_windex eindex
;
581 bitset_windex woffset
;
586 if (EBITSET_ZERO_P (bset
))
589 size
= EBITSET_SIZE (bset
);
590 n_bits
= size
* EBITSET_ELT_BITS
;
593 if (rbitno
>= n_bits
)
596 elts
= EBITSET_ELTS (bset
);
598 bitno
= n_bits
- (rbitno
+ 1);
600 windex
= bitno
/ BITSET_WORD_BITS
;
601 eindex
= bitno
/ EBITSET_ELT_BITS
;
602 woffset
= windex
- eindex
* EBITSET_ELT_WORDS
;
604 /* If num is 1, we could speed things up with a binary search
605 of the word of interest. */
608 bcount
= bitno
% BITSET_WORD_BITS
;
609 boffset
= windex
* BITSET_WORD_BITS
;
619 srcp
= EBITSET_WORDS (elt
);
625 word
= srcp
[woffset
] << (BITSET_WORD_BITS
- 1 - bcount
);
627 for (; word
; bcount
--)
629 if (word
& BITSET_MSB
)
631 list
[count
++] = boffset
+ bcount
;
634 *next
= n_bits
- (boffset
+ bcount
);
640 boffset
-= BITSET_WORD_BITS
;
641 bcount
= BITSET_WORD_BITS
- 1;
646 woffset
= EBITSET_ELT_WORDS
- 1;
647 boffset
= eindex
* EBITSET_ELT_BITS
- BITSET_WORD_BITS
;
651 *next
= n_bits
- (boffset
+ 1);
656 /* Find list of up to NUM bits set in BSET starting from and including
657 *NEXT and store in array LIST. Return with actual number of bits
658 found and with *NEXT indicating where search stopped. */
660 ebitset_list (bitset bset
, bitset_bindex
*list
,
661 bitset_bindex num
, bitset_bindex
*next
)
664 bitset_windex windex
;
665 bitset_windex eindex
;
672 if (EBITSET_ZERO_P (bset
))
678 elts
= EBITSET_ELTS (bset
);
679 size
= EBITSET_SIZE (bset
);
680 eindex
= bitno
/ EBITSET_ELT_BITS
;
682 if (bitno
% EBITSET_ELT_BITS
)
684 /* We need to start within an element. This is not very common. */
689 bitset_windex woffset
;
690 bitset_word
*srcp
= EBITSET_WORDS (elt
);
692 windex
= bitno
/ BITSET_WORD_BITS
;
693 woffset
= eindex
* EBITSET_ELT_WORDS
;
695 for (; (windex
- woffset
) < EBITSET_ELT_WORDS
; windex
++)
697 word
= srcp
[windex
- woffset
] >> (bitno
% BITSET_WORD_BITS
);
699 for (; word
; bitno
++)
703 list
[count
++] = bitno
;
712 bitno
= (windex
+ 1) * BITSET_WORD_BITS
;
716 /* Skip to next element. */
720 /* If num is 1, we could speed things up with a binary search
721 of the word of interest. */
723 for (; eindex
< size
; eindex
++)
732 srcp
= EBITSET_WORDS (elt
);
733 windex
= eindex
* EBITSET_ELT_WORDS
;
735 if ((count
+ EBITSET_ELT_BITS
) < num
)
737 /* The coast is clear, plant boot! */
739 #if EBITSET_ELT_WORDS == 2
743 if (!(word
& 0xffff))
753 for (; word
; bitno
++)
756 list
[count
++] = bitno
;
761 bitno
= windex
* BITSET_WORD_BITS
;
766 if (!(word
& 0xffff))
771 for (; word
; bitno
++)
774 list
[count
++] = bitno
;
779 bitno
= windex
* BITSET_WORD_BITS
;
781 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, windex
++)
783 bitno
= windex
* BITSET_WORD_BITS
;
788 if (!(word
& 0xffff))
798 for (; word
; bitno
++)
801 list
[count
++] = bitno
;
810 /* Tread more carefully since we need to check
811 if array overflows. */
813 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, windex
++)
815 bitno
= windex
* BITSET_WORD_BITS
;
817 for (word
= srcp
[i
]; word
; bitno
++)
821 list
[count
++] = bitno
;
839 /* Ensure that any unused bits within the last element are clear. */
841 ebitset_unused_clear (dst
)
844 unsigned int last_bit
;
845 bitset_bindex n_bits
;
847 n_bits
= BITSET_NBITS_ (dst
);
848 last_bit
= n_bits
% EBITSET_ELT_BITS
;
852 bitset_windex eindex
;
856 elts
= EBITSET_ELTS (dst
);
858 eindex
= n_bits
/ EBITSET_ELT_BITS
;
863 bitset_windex windex
;
864 bitset_windex woffset
;
865 bitset_word
*srcp
= EBITSET_WORDS (elt
);
867 windex
= n_bits
/ BITSET_WORD_BITS
;
868 woffset
= eindex
* EBITSET_ELT_WORDS
;
870 srcp
[windex
- woffset
] &= ((bitset_word
) 1 << last_bit
) - 1;
872 for (; (windex
- woffset
) < EBITSET_ELT_WORDS
; windex
++)
873 srcp
[windex
- woffset
] = 0;
880 ebitset_ones (bitset dst
)
885 for (j
= 0; j
< EBITSET_SIZE (dst
); j
++)
887 /* Create new elements if they cannot be found. Perhaps
888 we should just add pointers to a ones element? */
890 ebitset_elt_find (dst
, j
* EBITSET_ELT_BITS
, EBITSET_CREATE
);
891 memset (EBITSET_WORDS (elt
), -1, sizeof (EBITSET_WORDS (elt
)));
893 EBITSET_NONZERO_SET (dst
);
894 ebitset_unused_clear (dst
);
899 ebitset_empty_p (bitset dst
)
904 if (EBITSET_ZERO_P (dst
))
907 elts
= EBITSET_ELTS (dst
);
908 for (j
= 0; j
< EBITSET_SIZE (dst
); j
++)
910 ebitset_elt
*elt
= elts
[j
];
914 if (!ebitset_elt_zero_p (elt
))
916 /* Do some weeding as we go. */
917 ebitset_elt_remove (dst
, j
);
921 /* All the bits are zero. We could shrink the elts.
922 For now just mark DST as known to be zero. */
923 EBITSET_ZERO_SET (dst
);
929 ebitset_not (bitset dst
, bitset src
)
936 ebitset_resize (dst
, BITSET_NBITS_ (src
));
938 for (j
= 0; j
< EBITSET_SIZE (src
); j
++)
940 /* Create new elements for dst if they cannot be found
941 or substitute zero elements if src elements not found. */
943 ebitset_elt_find (dst
, j
* EBITSET_ELT_BITS
, EBITSET_SUBST
);
945 ebitset_elt_find (dst
, j
* EBITSET_ELT_BITS
, EBITSET_CREATE
);
947 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
948 EBITSET_WORDS (delt
)[i
] = ~EBITSET_WORDS (selt
)[i
];
950 EBITSET_NONZERO_SET (dst
);
951 ebitset_unused_clear (dst
);
955 /* Is DST == DST | SRC? */
957 ebitset_subset_p (bitset dst
, bitset src
)
965 selts
= EBITSET_ELTS (src
);
966 delts
= EBITSET_ELTS (dst
);
968 ssize
= EBITSET_SIZE (src
);
969 dsize
= EBITSET_SIZE (dst
);
971 for (j
= 0; j
< ssize
; j
++)
977 selt
= j
< ssize
? selts
[j
] : 0;
978 delt
= j
< dsize
? delts
[j
] : 0;
984 selt
= &ebitset_zero_elts
[0];
986 delt
= &ebitset_zero_elts
[0];
988 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
989 if (EBITSET_WORDS (delt
)[i
]
990 != (EBITSET_WORDS (selt
)[i
] | EBITSET_WORDS (delt
)[i
]))
997 /* Is DST & SRC == 0? */
999 ebitset_disjoint_p (bitset dst
, bitset src
)
1002 ebitset_elts
*selts
;
1003 ebitset_elts
*delts
;
1004 bitset_windex ssize
;
1005 bitset_windex dsize
;
1007 selts
= EBITSET_ELTS (src
);
1008 delts
= EBITSET_ELTS (dst
);
1010 ssize
= EBITSET_SIZE (src
);
1011 dsize
= EBITSET_SIZE (dst
);
1013 for (j
= 0; j
< ssize
; j
++)
1019 selt
= j
< ssize
? selts
[j
] : 0;
1020 delt
= j
< dsize
? delts
[j
] : 0;
1025 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
1026 if ((EBITSET_WORDS (selt
)[i
] & EBITSET_WORDS (delt
)[i
]))
1035 ebitset_op3_cmp (bitset dst
, bitset src1
, bitset src2
, enum bitset_ops op
)
1037 bitset_windex ssize1
;
1038 bitset_windex ssize2
;
1039 bitset_windex dsize
;
1041 ebitset_elts
*selts1
;
1042 ebitset_elts
*selts2
;
1043 ebitset_elts
*delts
;
1047 bool changed
= false;
1051 ebitset_resize (dst
, max (BITSET_NBITS_ (src1
), BITSET_NBITS_ (src2
)));
1053 ssize1
= EBITSET_SIZE (src1
);
1054 ssize2
= EBITSET_SIZE (src2
);
1055 dsize
= EBITSET_SIZE (dst
);
1060 selts1
= EBITSET_ELTS (src1
);
1061 selts2
= EBITSET_ELTS (src2
);
1062 delts
= EBITSET_ELTS (dst
);
1064 for (j
= 0; j
< size
; j
++)
1070 selt1
= j
< ssize1
? selts1
[j
] : 0;
1071 selt2
= j
< ssize2
? selts2
[j
] : 0;
1072 delt
= j
< dsize
? delts
[j
] : 0;
1074 if (!selt1
&& !selt2
)
1079 ebitset_elt_remove (dst
, j
);
1085 selt1
= &ebitset_zero_elts
[0];
1087 selt2
= &ebitset_zero_elts
[0];
1089 delt
= ebitset_elt_calloc ();
1093 srcp1
= EBITSET_WORDS (selt1
);
1094 srcp2
= EBITSET_WORDS (selt2
);
1095 dstp
= EBITSET_WORDS (delt
);
1099 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, dstp
++)
1101 bitset_word tmp
= *srcp1
++ | *srcp2
++;
1112 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, dstp
++)
1114 bitset_word tmp
= *srcp1
++ & *srcp2
++;
1125 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, dstp
++)
1127 bitset_word tmp
= *srcp1
++ ^ *srcp2
++;
1137 case BITSET_OP_ANDN
:
1138 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, dstp
++)
1140 bitset_word tmp
= *srcp1
++ & ~(*srcp2
++);
1154 if (!ebitset_elt_zero_p (delt
))
1156 ebitset_elt_add (dst
, delt
, j
);
1160 ebitset_elt_free (delt
);
1164 /* If we have elements of DST left over, free them all. */
1165 for (; j
< dsize
; j
++)
1174 ebitset_elt_remove (dst
, j
);
1177 EBITSET_NONZERO_SET (dst
);
1183 ebitset_and_cmp (bitset dst
, bitset src1
, bitset src2
)
1187 if (EBITSET_ZERO_P (src2
))
1190 changed
= EBITSET_ZERO_P (dst
);
1194 else if (EBITSET_ZERO_P (src1
))
1197 changed
= EBITSET_ZERO_P (dst
);
1201 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_AND
);
1206 ebitset_and (bitset dst
, bitset src1
, bitset src2
)
1208 ebitset_and_cmp (dst
, src1
, src2
);
1213 ebitset_andn_cmp (bitset dst
, bitset src1
, bitset src2
)
1217 if (EBITSET_ZERO_P (src2
))
1219 return ebitset_copy_cmp (dst
, src1
);
1221 else if (EBITSET_ZERO_P (src1
))
1224 changed
= EBITSET_ZERO_P (dst
);
1228 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_ANDN
);
1233 ebitset_andn (bitset dst
, bitset src1
, bitset src2
)
1235 ebitset_andn_cmp (dst
, src1
, src2
);
1240 ebitset_or_cmp (bitset dst
, bitset src1
, bitset src2
)
1242 if (EBITSET_ZERO_P (src2
))
1244 return ebitset_copy_cmp (dst
, src1
);
1246 else if (EBITSET_ZERO_P (src1
))
1248 return ebitset_copy_cmp (dst
, src2
);
1250 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_OR
);
1255 ebitset_or (bitset dst
, bitset src1
, bitset src2
)
1257 ebitset_or_cmp (dst
, src1
, src2
);
1262 ebitset_xor_cmp (bitset dst
, bitset src1
, bitset src2
)
1264 if (EBITSET_ZERO_P (src2
))
1266 return ebitset_copy_cmp (dst
, src1
);
1268 else if (EBITSET_ZERO_P (src1
))
1270 return ebitset_copy_cmp (dst
, src2
);
1272 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_XOR
);
1277 ebitset_xor (bitset dst
, bitset src1
, bitset src2
)
1279 ebitset_xor_cmp (dst
, src1
, src2
);
1284 ebitset_copy (bitset dst
, bitset src
)
1286 if (BITSET_COMPATIBLE_ (dst
, src
))
1287 ebitset_copy_ (dst
, src
);
1289 bitset_copy_ (dst
, src
);
1293 /* Vector of operations for linked-list bitsets. */
1294 struct bitset_vtable ebitset_vtable
= {
1321 bitset_andn_or_cmp_
,
1325 ebitset_list_reverse
,
1331 /* Return size of initial structure. */
1333 ebitset_bytes (bitset_bindex n_bits ATTRIBUTE_UNUSED
)
1335 return sizeof (struct ebitset_struct
);
1339 /* Initialize a bitset. */
1342 ebitset_init (bitset bset
, bitset_bindex n_bits
)
1346 bset
->b
.vtable
= &ebitset_vtable
;
1348 bset
->b
.csize
= EBITSET_ELT_WORDS
;
1350 EBITSET_ZERO_SET (bset
);
1352 size
= n_bits
? (n_bits
+ EBITSET_ELT_BITS
- 1) / EBITSET_ELT_BITS
1353 : EBITSET_INITIAL_SIZE
;
1355 EBITSET_ASIZE (bset
) = 0;
1356 EBITSET_ELTS (bset
) = 0;
1357 ebitset_resize (bset
, n_bits
);
1364 ebitset_release_memory (void)
1366 ebitset_free_list
= 0;
1367 if (ebitset_obstack_init
)
1369 ebitset_obstack_init
= false;
1370 obstack_free (&ebitset_obstack
, NULL
);