1 /* Functions to support expandable bitsets.
2 Copyright (C) 2002, 2003, 2004 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 int) (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 (bitset src
, bitset_bindex n_bits
)
126 bitset_windex oldsize
;
127 bitset_windex newsize
;
129 if (n_bits
== BITSET_NBITS_ (src
))
132 oldsize
= EBITSET_SIZE (src
);
133 newsize
= EBITSET_N_ELTS (n_bits
);
135 if (oldsize
< newsize
)
139 /* The bitset needs to grow. If we already have enough memory
140 allocated, then just zero what we need. */
141 if (newsize
> EBITSET_ASIZE (src
))
143 /* We need to allocate more memory. When oldsize is
144 non-zero this means that we are changing the size, so
145 grow the bitset 25% larger than requested to reduce
146 number of reallocations. */
151 size
= newsize
+ newsize
/ 4;
154 = realloc (EBITSET_ELTS (src
), size
* sizeof (ebitset_elt
*));
155 EBITSET_ASIZE (src
) = size
;
158 memset (EBITSET_ELTS (src
) + oldsize
, 0,
159 (newsize
- oldsize
) * sizeof (ebitset_elt
*));
163 /* The bitset needs to shrink. There's no point deallocating
164 the memory unless it is shrinking by a reasonable amount. */
165 if ((oldsize
- newsize
) >= oldsize
/ 2)
168 = realloc (EBITSET_ELTS (src
), newsize
* sizeof (ebitset_elt
*));
169 EBITSET_ASIZE (src
) = newsize
;
172 /* Need to prune any excess bits. FIXME. */
175 BITSET_NBITS_ (src
) = n_bits
;
180 /* Allocate a ebitset element. The bits are not cleared. */
181 static inline ebitset_elt
*
182 ebitset_elt_alloc (void)
186 if (ebitset_free_list
!= 0)
188 elt
= ebitset_free_list
;
189 ebitset_free_list
= EBITSET_NEXT (elt
);
193 if (!ebitset_obstack_init
)
195 ebitset_obstack_init
= true;
197 /* Let particular systems override the size of a chunk. */
199 #ifndef OBSTACK_CHUNK_SIZE
200 #define OBSTACK_CHUNK_SIZE 0
203 /* Let them override the alloc and free routines too. */
205 #ifndef OBSTACK_CHUNK_ALLOC
206 #define OBSTACK_CHUNK_ALLOC xmalloc
209 #ifndef OBSTACK_CHUNK_FREE
210 #define OBSTACK_CHUNK_FREE free
213 #if !defined(__GNUC__) || (__GNUC__ < 2)
214 #define __alignof__(type) 0
217 obstack_specify_allocation (&ebitset_obstack
, OBSTACK_CHUNK_SIZE
,
218 __alignof__ (ebitset_elt
),
219 (void *(*)PARAMS ((long int)))
221 (void (*)PARAMS ((void *)))
225 /* Perhaps we should add a number of new elements to the free
227 elt
= (ebitset_elt
*) obstack_alloc (&ebitset_obstack
,
228 sizeof (ebitset_elt
));
235 /* Allocate a ebitset element. The bits are cleared. */
236 static inline ebitset_elt
*
237 ebitset_elt_calloc (void)
241 elt
= ebitset_elt_alloc ();
242 memset (EBITSET_WORDS (elt
), 0, sizeof (EBITSET_WORDS (elt
)));
248 ebitset_elt_free (ebitset_elt
*elt
)
250 EBITSET_NEXT (elt
) = ebitset_free_list
;
251 ebitset_free_list
= elt
;
255 /* Remove element with index EINDEX from bitset BSET. */
257 ebitset_elt_remove (bitset bset
, bitset_windex eindex
)
262 elts
= EBITSET_ELTS (bset
);
267 ebitset_elt_free (elt
);
271 /* Add ELT into elts at index EINDEX of bitset BSET. */
273 ebitset_elt_add (bitset bset
, ebitset_elt
*elt
, bitset_windex eindex
)
277 elts
= EBITSET_ELTS (bset
);
278 /* Assume that the elts entry not allocated. */
283 /* Are all bits in an element zero? */
285 ebitset_elt_zero_p (ebitset_elt
*elt
)
289 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
290 if (EBITSET_WORDS (elt
)[i
])
298 ebitset_elt_find (bitset bset
, bitset_bindex bindex
,
299 enum ebitset_find_mode mode
)
303 bitset_windex eindex
;
306 eindex
= bindex
/ EBITSET_ELT_BITS
;
308 elts
= EBITSET_ELTS (bset
);
309 size
= EBITSET_SIZE (bset
);
313 if ((elt
= elts
[eindex
]))
315 if (EBITSET_WORDS (elt
) == bset
->b
.cdata
)
318 EBITSET_CACHE_SET (bset
, eindex
);
323 /* 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];
349 /* Weed out the zero elements from the elts. */
350 static inline bitset_windex
351 ebitset_weed (bitset bset
)
357 if (EBITSET_ZERO_P (bset
))
360 elts
= EBITSET_ELTS (bset
);
362 for (j
= 0; j
< EBITSET_SIZE (bset
); j
++)
364 ebitset_elt
*elt
= elts
[j
];
368 if (ebitset_elt_zero_p (elt
))
370 ebitset_elt_remove (bset
, j
);
381 /* All the bits are zero. We could shrink the elts.
382 For now just mark BSET as known to be zero. */
383 EBITSET_ZERO_SET (bset
);
386 EBITSET_NONZERO_SET (bset
);
392 /* Set all bits in the bitset to zero. */
394 ebitset_zero (bitset bset
)
399 if (EBITSET_ZERO_P (bset
))
402 elts
= EBITSET_ELTS (bset
);
403 for (j
= 0; j
< EBITSET_SIZE (bset
); j
++)
405 ebitset_elt
*elt
= elts
[j
];
408 ebitset_elt_remove (bset
, j
);
411 /* All the bits are zero. We could shrink the elts.
412 For now just mark BSET as known to be zero. */
413 EBITSET_ZERO_SET (bset
);
418 ebitset_equal_p (bitset dst
, bitset src
)
430 if (EBITSET_SIZE (src
) != EBITSET_SIZE (dst
))
433 selts
= EBITSET_ELTS (src
);
434 delts
= EBITSET_ELTS (dst
);
436 for (j
= 0; j
< EBITSET_SIZE (src
); j
++)
439 ebitset_elt
*selt
= selts
[j
];
440 ebitset_elt
*delt
= delts
[j
];
444 if ((selt
&& !delt
) || (!selt
&& delt
))
447 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
448 if (EBITSET_WORDS (selt
)[i
] != EBITSET_WORDS (delt
)[i
])
455 /* Copy bits from bitset SRC to bitset DST. */
457 ebitset_copy_ (bitset dst
, bitset src
)
468 if (BITSET_NBITS_ (dst
) != BITSET_NBITS_ (src
))
469 ebitset_resize (dst
, BITSET_NBITS_ (src
));
471 selts
= EBITSET_ELTS (src
);
472 delts
= EBITSET_ELTS (dst
);
473 for (j
= 0; j
< EBITSET_SIZE (src
); j
++)
475 ebitset_elt
*selt
= selts
[j
];
481 tmp
= ebitset_elt_alloc ();
483 memcpy (EBITSET_WORDS (tmp
), EBITSET_WORDS (selt
),
484 sizeof (EBITSET_WORDS (selt
)));
487 EBITSET_NONZERO_SET (dst
);
491 /* Copy bits from bitset SRC to bitset DST. Return true if
492 bitsets different. */
494 ebitset_copy_cmp (bitset dst
, bitset src
)
499 if (EBITSET_ZERO_P (dst
))
501 ebitset_copy_ (dst
, src
);
502 return !EBITSET_ZERO_P (src
);
505 if (ebitset_equal_p (dst
, src
))
508 ebitset_copy_ (dst
, src
);
513 /* Set bit BITNO in bitset DST. */
515 ebitset_set (bitset dst
, bitset_bindex bitno
)
517 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
519 ebitset_elt_find (dst
, bitno
, EBITSET_CREATE
);
521 dst
->b
.cdata
[windex
- dst
->b
.cindex
] |=
522 (bitset_word
) 1 << (bitno
% BITSET_WORD_BITS
);
526 /* Reset bit BITNO in bitset DST. */
528 ebitset_reset (bitset dst
, bitset_bindex bitno
)
530 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
532 if (!ebitset_elt_find (dst
, bitno
, EBITSET_FIND
))
535 dst
->b
.cdata
[windex
- dst
->b
.cindex
] &=
536 ~((bitset_word
) 1 << (bitno
% BITSET_WORD_BITS
));
538 /* If all the data is zero, perhaps we should remove it now...
539 However, there is a good chance that the element will be needed
544 /* Test bit BITNO in bitset SRC. */
546 ebitset_test (bitset src
, bitset_bindex bitno
)
548 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
550 return (ebitset_elt_find (src
, bitno
, EBITSET_FIND
)
551 && ((src
->b
.cdata
[windex
- src
->b
.cindex
]
552 >> (bitno
% BITSET_WORD_BITS
))
558 ebitset_free (bitset bset
)
561 free (EBITSET_ELTS (bset
));
565 /* Find list of up to NUM bits set in BSET starting from and including
566 *NEXT and store in array LIST. Return with actual number of bits
567 found and with *NEXT indicating where search stopped. */
569 ebitset_list_reverse (bitset bset
, bitset_bindex
*list
,
570 bitset_bindex num
, bitset_bindex
*next
)
572 bitset_bindex n_bits
;
574 bitset_bindex rbitno
;
576 bitset_bindex boffset
;
577 bitset_windex windex
;
578 bitset_windex eindex
;
579 bitset_windex woffset
;
584 if (EBITSET_ZERO_P (bset
))
587 size
= EBITSET_SIZE (bset
);
588 n_bits
= size
* EBITSET_ELT_BITS
;
591 if (rbitno
>= n_bits
)
594 elts
= EBITSET_ELTS (bset
);
596 bitno
= n_bits
- (rbitno
+ 1);
598 windex
= bitno
/ BITSET_WORD_BITS
;
599 eindex
= bitno
/ EBITSET_ELT_BITS
;
600 woffset
= windex
- eindex
* EBITSET_ELT_WORDS
;
602 /* If num is 1, we could speed things up with a binary search
603 of the word of interest. */
606 bcount
= bitno
% BITSET_WORD_BITS
;
607 boffset
= windex
* BITSET_WORD_BITS
;
617 srcp
= EBITSET_WORDS (elt
);
623 word
= srcp
[woffset
] << (BITSET_WORD_BITS
- 1 - bcount
);
625 for (; word
; bcount
--)
627 if (word
& BITSET_MSB
)
629 list
[count
++] = boffset
+ bcount
;
632 *next
= n_bits
- (boffset
+ bcount
);
638 boffset
-= BITSET_WORD_BITS
;
639 bcount
= BITSET_WORD_BITS
- 1;
644 woffset
= EBITSET_ELT_WORDS
- 1;
645 boffset
= eindex
* EBITSET_ELT_BITS
- BITSET_WORD_BITS
;
649 *next
= n_bits
- (boffset
+ 1);
654 /* Find list of up to NUM bits set in BSET starting from and including
655 *NEXT and store in array LIST. Return with actual number of bits
656 found and with *NEXT indicating where search stopped. */
658 ebitset_list (bitset bset
, bitset_bindex
*list
,
659 bitset_bindex num
, bitset_bindex
*next
)
662 bitset_windex windex
;
663 bitset_windex eindex
;
670 if (EBITSET_ZERO_P (bset
))
676 elts
= EBITSET_ELTS (bset
);
677 size
= EBITSET_SIZE (bset
);
678 eindex
= bitno
/ EBITSET_ELT_BITS
;
680 if (bitno
% EBITSET_ELT_BITS
)
682 /* We need to start within an element. This is not very common. */
687 bitset_windex woffset
;
688 bitset_word
*srcp
= EBITSET_WORDS (elt
);
690 windex
= bitno
/ BITSET_WORD_BITS
;
691 woffset
= eindex
* EBITSET_ELT_WORDS
;
693 for (; (windex
- woffset
) < EBITSET_ELT_WORDS
; windex
++)
695 word
= srcp
[windex
- woffset
] >> (bitno
% BITSET_WORD_BITS
);
697 for (; word
; bitno
++)
701 list
[count
++] = bitno
;
710 bitno
= (windex
+ 1) * BITSET_WORD_BITS
;
714 /* Skip to next element. */
718 /* If num is 1, we could speed things up with a binary search
719 of the word of interest. */
721 for (; eindex
< size
; eindex
++)
730 srcp
= EBITSET_WORDS (elt
);
731 windex
= eindex
* EBITSET_ELT_WORDS
;
733 if ((count
+ EBITSET_ELT_BITS
) < num
)
735 /* The coast is clear, plant boot! */
737 #if EBITSET_ELT_WORDS == 2
741 if (!(word
& 0xffff))
751 for (; word
; bitno
++)
754 list
[count
++] = bitno
;
759 bitno
= windex
* BITSET_WORD_BITS
;
764 if (!(word
& 0xffff))
769 for (; word
; bitno
++)
772 list
[count
++] = bitno
;
777 bitno
= windex
* BITSET_WORD_BITS
;
779 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, windex
++)
781 bitno
= windex
* BITSET_WORD_BITS
;
786 if (!(word
& 0xffff))
796 for (; word
; bitno
++)
799 list
[count
++] = bitno
;
808 /* Tread more carefully since we need to check
809 if array overflows. */
811 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, windex
++)
813 bitno
= windex
* BITSET_WORD_BITS
;
815 for (word
= srcp
[i
]; word
; bitno
++)
819 list
[count
++] = bitno
;
837 /* Ensure that any unused bits within the last element are clear. */
839 ebitset_unused_clear (bitset dst
)
841 unsigned int last_bit
;
842 bitset_bindex n_bits
;
844 n_bits
= BITSET_NBITS_ (dst
);
845 last_bit
= n_bits
% EBITSET_ELT_BITS
;
849 bitset_windex eindex
;
853 elts
= EBITSET_ELTS (dst
);
855 eindex
= n_bits
/ EBITSET_ELT_BITS
;
860 bitset_windex windex
;
861 bitset_windex woffset
;
862 bitset_word
*srcp
= EBITSET_WORDS (elt
);
864 windex
= n_bits
/ BITSET_WORD_BITS
;
865 woffset
= eindex
* EBITSET_ELT_WORDS
;
867 srcp
[windex
- woffset
] &= ((bitset_word
) 1 << last_bit
) - 1;
869 for (; (windex
- woffset
) < EBITSET_ELT_WORDS
; windex
++)
870 srcp
[windex
- woffset
] = 0;
877 ebitset_ones (bitset dst
)
882 for (j
= 0; j
< EBITSET_SIZE (dst
); j
++)
884 /* Create new elements if they cannot be found. Perhaps
885 we should just add pointers to a ones element? */
887 ebitset_elt_find (dst
, j
* EBITSET_ELT_BITS
, EBITSET_CREATE
);
888 memset (EBITSET_WORDS (elt
), -1, sizeof (EBITSET_WORDS (elt
)));
890 EBITSET_NONZERO_SET (dst
);
891 ebitset_unused_clear (dst
);
896 ebitset_empty_p (bitset dst
)
901 if (EBITSET_ZERO_P (dst
))
904 elts
= EBITSET_ELTS (dst
);
905 for (j
= 0; j
< EBITSET_SIZE (dst
); j
++)
907 ebitset_elt
*elt
= elts
[j
];
911 if (!ebitset_elt_zero_p (elt
))
913 /* Do some weeding as we go. */
914 ebitset_elt_remove (dst
, j
);
918 /* All the bits are zero. We could shrink the elts.
919 For now just mark DST as known to be zero. */
920 EBITSET_ZERO_SET (dst
);
926 ebitset_not (bitset dst
, bitset src
)
933 ebitset_resize (dst
, BITSET_NBITS_ (src
));
935 for (j
= 0; j
< EBITSET_SIZE (src
); j
++)
937 /* Create new elements for dst if they cannot be found
938 or substitute zero elements if src elements not found. */
940 ebitset_elt_find (dst
, j
* EBITSET_ELT_BITS
, EBITSET_SUBST
);
942 ebitset_elt_find (dst
, j
* EBITSET_ELT_BITS
, EBITSET_CREATE
);
944 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
945 EBITSET_WORDS (delt
)[i
] = ~EBITSET_WORDS (selt
)[i
];
947 EBITSET_NONZERO_SET (dst
);
948 ebitset_unused_clear (dst
);
952 /* Is DST == DST | SRC? */
954 ebitset_subset_p (bitset dst
, bitset src
)
962 selts
= EBITSET_ELTS (src
);
963 delts
= EBITSET_ELTS (dst
);
965 ssize
= EBITSET_SIZE (src
);
966 dsize
= EBITSET_SIZE (dst
);
968 for (j
= 0; j
< ssize
; j
++)
974 selt
= j
< ssize
? selts
[j
] : 0;
975 delt
= j
< dsize
? delts
[j
] : 0;
981 selt
= &ebitset_zero_elts
[0];
983 delt
= &ebitset_zero_elts
[0];
985 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
986 if (EBITSET_WORDS (delt
)[i
]
987 != (EBITSET_WORDS (selt
)[i
] | EBITSET_WORDS (delt
)[i
]))
994 /* Is DST & SRC == 0? */
996 ebitset_disjoint_p (bitset dst
, bitset src
)
1000 ebitset_elts
*delts
;
1001 bitset_windex ssize
;
1002 bitset_windex dsize
;
1004 selts
= EBITSET_ELTS (src
);
1005 delts
= EBITSET_ELTS (dst
);
1007 ssize
= EBITSET_SIZE (src
);
1008 dsize
= EBITSET_SIZE (dst
);
1010 for (j
= 0; j
< ssize
; j
++)
1016 selt
= j
< ssize
? selts
[j
] : 0;
1017 delt
= j
< dsize
? delts
[j
] : 0;
1022 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
1023 if ((EBITSET_WORDS (selt
)[i
] & EBITSET_WORDS (delt
)[i
]))
1032 ebitset_op3_cmp (bitset dst
, bitset src1
, bitset src2
, enum bitset_ops op
)
1034 bitset_windex ssize1
;
1035 bitset_windex ssize2
;
1036 bitset_windex dsize
;
1038 ebitset_elts
*selts1
;
1039 ebitset_elts
*selts2
;
1040 ebitset_elts
*delts
;
1044 bool changed
= false;
1048 ebitset_resize (dst
, max (BITSET_NBITS_ (src1
), BITSET_NBITS_ (src2
)));
1050 ssize1
= EBITSET_SIZE (src1
);
1051 ssize2
= EBITSET_SIZE (src2
);
1052 dsize
= EBITSET_SIZE (dst
);
1057 selts1
= EBITSET_ELTS (src1
);
1058 selts2
= EBITSET_ELTS (src2
);
1059 delts
= EBITSET_ELTS (dst
);
1061 for (j
= 0; j
< size
; j
++)
1067 selt1
= j
< ssize1
? selts1
[j
] : 0;
1068 selt2
= j
< ssize2
? selts2
[j
] : 0;
1069 delt
= j
< dsize
? delts
[j
] : 0;
1071 if (!selt1
&& !selt2
)
1076 ebitset_elt_remove (dst
, j
);
1082 selt1
= &ebitset_zero_elts
[0];
1084 selt2
= &ebitset_zero_elts
[0];
1086 delt
= ebitset_elt_calloc ();
1090 srcp1
= EBITSET_WORDS (selt1
);
1091 srcp2
= EBITSET_WORDS (selt2
);
1092 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
++);
1151 if (!ebitset_elt_zero_p (delt
))
1153 ebitset_elt_add (dst
, delt
, j
);
1157 ebitset_elt_free (delt
);
1161 /* If we have elements of DST left over, free them all. */
1162 for (; j
< dsize
; j
++)
1171 ebitset_elt_remove (dst
, j
);
1174 EBITSET_NONZERO_SET (dst
);
1180 ebitset_and_cmp (bitset dst
, bitset src1
, bitset src2
)
1184 if (EBITSET_ZERO_P (src2
))
1187 changed
= EBITSET_ZERO_P (dst
);
1191 else if (EBITSET_ZERO_P (src1
))
1194 changed
= EBITSET_ZERO_P (dst
);
1198 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_AND
);
1203 ebitset_and (bitset dst
, bitset src1
, bitset src2
)
1205 ebitset_and_cmp (dst
, src1
, src2
);
1210 ebitset_andn_cmp (bitset dst
, bitset src1
, bitset src2
)
1214 if (EBITSET_ZERO_P (src2
))
1216 return ebitset_copy_cmp (dst
, src1
);
1218 else if (EBITSET_ZERO_P (src1
))
1221 changed
= EBITSET_ZERO_P (dst
);
1225 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_ANDN
);
1230 ebitset_andn (bitset dst
, bitset src1
, bitset src2
)
1232 ebitset_andn_cmp (dst
, src1
, src2
);
1237 ebitset_or_cmp (bitset dst
, bitset src1
, bitset src2
)
1239 if (EBITSET_ZERO_P (src2
))
1241 return ebitset_copy_cmp (dst
, src1
);
1243 else if (EBITSET_ZERO_P (src1
))
1245 return ebitset_copy_cmp (dst
, src2
);
1247 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_OR
);
1252 ebitset_or (bitset dst
, bitset src1
, bitset src2
)
1254 ebitset_or_cmp (dst
, src1
, src2
);
1259 ebitset_xor_cmp (bitset dst
, bitset src1
, bitset src2
)
1261 if (EBITSET_ZERO_P (src2
))
1263 return ebitset_copy_cmp (dst
, src1
);
1265 else if (EBITSET_ZERO_P (src1
))
1267 return ebitset_copy_cmp (dst
, src2
);
1269 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_XOR
);
1274 ebitset_xor (bitset dst
, bitset src1
, bitset src2
)
1276 ebitset_xor_cmp (dst
, src1
, src2
);
1281 ebitset_copy (bitset dst
, bitset src
)
1283 if (BITSET_COMPATIBLE_ (dst
, src
))
1284 ebitset_copy_ (dst
, src
);
1286 bitset_copy_ (dst
, src
);
1290 /* Vector of operations for linked-list bitsets. */
1291 struct bitset_vtable ebitset_vtable
= {
1318 bitset_andn_or_cmp_
,
1322 ebitset_list_reverse
,
1328 /* Return size of initial structure. */
1330 ebitset_bytes (bitset_bindex n_bits ATTRIBUTE_UNUSED
)
1332 return sizeof (struct ebitset_struct
);
1336 /* Initialize a bitset. */
1339 ebitset_init (bitset bset
, bitset_bindex n_bits
)
1343 bset
->b
.vtable
= &ebitset_vtable
;
1345 bset
->b
.csize
= EBITSET_ELT_WORDS
;
1347 EBITSET_ZERO_SET (bset
);
1349 size
= n_bits
? (n_bits
+ EBITSET_ELT_BITS
- 1) / EBITSET_ELT_BITS
1350 : EBITSET_INITIAL_SIZE
;
1352 EBITSET_ASIZE (bset
) = 0;
1353 EBITSET_ELTS (bset
) = 0;
1354 ebitset_resize (bset
, n_bits
);
1361 ebitset_release_memory (void)
1363 ebitset_free_list
= 0;
1364 if (ebitset_obstack_init
)
1366 ebitset_obstack_init
= false;
1367 obstack_free (&ebitset_obstack
, NULL
);