1 /* Functions to support expandable bitsets.
2 Copyright (C) 2002 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.
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 some large number 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) (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
;
71 /* Head of ebitset linked list. */
72 typedef struct ebitset_struct
74 unsigned int size
; /* Number of elements. */
75 ebitset_elts
*elts
; /* Expanding array of pointers to elements. */
83 /* Number of elements to initially allocate. */
85 #ifndef EBITSET_INITIAL_SIZE
86 #define EBITSET_INITIAL_SIZE 2
89 /* Minimum number of elements to grow table. */
91 #ifndef EBITSET_GROW_SIZE
92 #define EBITSET_GROW_SIZE 4
97 struct bbitset_struct b
;
98 struct ebitset_struct e
;
101 enum ebitset_find_mode
102 { EBITSET_FIND
, EBITSET_CREATE
, EBITSET_SUBST
};
104 static ebitset_elt ebitset_zero_elts
[1]; /* Elements of all zero bits. */
106 /* Obstack to allocate bitset elements from. */
107 static struct obstack ebitset_obstack
;
108 static int ebitset_obstack_init
= 0;
109 static ebitset_elt
*ebitset_free_list
; /* Free list of bitset elements. */
111 static void ebitset_elts_grow
PARAMS ((bitset
, unsigned int));
112 static ebitset_elt
*ebitset_elt_alloc
PARAMS ((void));
113 static ebitset_elt
*ebitset_elt_calloc
PARAMS ((void));
114 static void ebitset_elt_add
PARAMS ((bitset
, ebitset_elt
*, unsigned int));
115 static void ebitset_elt_remove
PARAMS ((bitset
, unsigned int));
116 static void ebitset_elt_free
PARAMS ((ebitset_elt
*));
117 static ebitset_elt
*ebitset_elt_find
PARAMS ((bitset
, bitset_windex
,
118 enum ebitset_find_mode
));
119 static ebitset_elt
*ebitset_elt_last
PARAMS ((bitset
));
120 static int ebitset_elt_zero_p
PARAMS ((ebitset_elt
*));
122 static int ebitset_weed
PARAMS ((bitset
));
123 static void ebitset_zero
PARAMS ((bitset
));
124 static void ebitset_copy_
PARAMS ((bitset
, bitset
));
125 static int ebitset_copy_cmp
PARAMS ((bitset
, bitset
));
126 static void ebitset_set
PARAMS ((bitset
, bitset_bindex
));
127 static void ebitset_reset
PARAMS ((bitset
, bitset_bindex
));
128 static int ebitset_test
PARAMS ((bitset
, bitset_bindex
));
129 static int ebitset_size
PARAMS ((bitset
));
130 static int ebitset_disjoint_p
PARAMS ((bitset
, bitset
));
131 static int ebitset_equal_p
PARAMS ((bitset
, bitset
));
132 static void ebitset_not
PARAMS ((bitset
, bitset
));
133 static int ebitset_subset_p
PARAMS ((bitset
, bitset
));
134 static int ebitset_op3_cmp
PARAMS ((bitset
, bitset
, bitset
, enum bitset_ops
));
135 static int ebitset_and_cmp
PARAMS ((bitset
, bitset
, bitset
));
136 static int ebitset_andn_cmp
PARAMS ((bitset
, bitset
, bitset
));
137 static int ebitset_or_cmp
PARAMS ((bitset
, bitset
, bitset
));
138 static int ebitset_xor_cmp
PARAMS ((bitset
, bitset
, bitset
));
139 static int ebitset_list
PARAMS ((bitset
, bitset_bindex
*, bitset_bindex
,
141 static int ebitset_list_reverse
142 PARAMS ((bitset
, bitset_bindex
*, bitset_bindex
, bitset_bindex
*));
143 static void ebitset_free
PARAMS ((bitset
));
145 #define EBITSET_ELTS(BSET) ((BSET)->e.elts)
146 #define EBITSET_SIZE(BSET) ((BSET)->e.size)
148 #define EBITSET_NEXT(ELT) ((ELT)->u.next)
149 #define EBITSET_WORDS(ELT) ((ELT)->u.words)
151 /* Disable bitset cache and mark BSET as being zero. */
152 #define EBITSET_ZERO_SET(BSET) ((BSET)->b.cindex = BITSET_INDEX_MAX, \
155 #define EBITSET_CACHE_DISABLE(BSET) ((BSET)->b.cindex = BITSET_INDEX_MAX)
157 /* Disable bitset cache and mark BSET as being possibly non-zero. */
158 #define EBITSET_NONZERO_SET(BSET) \
159 (EBITSET_CACHE_DISABLE (BSET), (BSET)->b.cdata = (bitset_word *)~0)
161 /* A conservative estimate of whether the bitset is zero.
162 This is non-zero only if we know for sure that the bitset is zero. */
163 #define EBITSET_ZERO_P(BSET) ((BSET)->b.cdata == 0)
165 /* Enable cache to point to element with table index EINDEX.
166 The element must exist. */
167 #define EBITSET_CACHE_SET(BSET, EINDEX) \
168 ((BSET)->b.cindex = (EINDEX) * EBITSET_ELT_WORDS, \
169 (BSET)->b.cdata = EBITSET_WORDS (EBITSET_ELTS (BSET) [EINDEX]))
172 /* Grow the expandable table for BSET by SIZE elements. */
174 ebitset_elts_grow (bset
, size
)
178 unsigned int oldsize
;
179 unsigned int newsize
;
181 oldsize
= EBITSET_SIZE (bset
);
182 newsize
= oldsize
+ size
;
184 EBITSET_ELTS (bset
) = xrealloc (EBITSET_ELTS (bset
),
185 newsize
* sizeof (ebitset_elt
*));
186 EBITSET_SIZE (bset
) = newsize
;
188 memset (EBITSET_ELTS (bset
) + oldsize
, 0, size
* sizeof (ebitset_elt
*));
192 /* Allocate a ebitset element. The bits are not cleared. */
193 static inline ebitset_elt
*
198 if (ebitset_free_list
!= 0)
200 elt
= ebitset_free_list
;
201 ebitset_free_list
= EBITSET_NEXT (elt
);
205 if (!ebitset_obstack_init
)
207 ebitset_obstack_init
= 1;
209 /* Let particular systems override the size of a chunk. */
211 #ifndef OBSTACK_CHUNK_SIZE
212 #define OBSTACK_CHUNK_SIZE 0
215 /* Let them override the alloc and free routines too. */
217 #ifndef OBSTACK_CHUNK_ALLOC
218 #define OBSTACK_CHUNK_ALLOC xmalloc
221 #ifndef OBSTACK_CHUNK_FREE
222 #define OBSTACK_CHUNK_FREE free
225 #if !defined(__GNUC__) || (__GNUC__ < 2)
226 #define __alignof__(type) 0
229 obstack_specify_allocation (&ebitset_obstack
, OBSTACK_CHUNK_SIZE
,
230 __alignof__ (ebitset_elt
),
231 (void *(*)PARAMS ((long)))
233 (void (*)PARAMS ((void *)))
237 /* Perhaps we should add a number of new elements to the free
239 elt
= (ebitset_elt
*) obstack_alloc (&ebitset_obstack
,
240 sizeof (ebitset_elt
));
247 /* Allocate a ebitset element. The bits are cleared. */
248 static inline ebitset_elt
*
249 ebitset_elt_calloc ()
253 elt
= ebitset_elt_alloc ();
254 memset (EBITSET_WORDS (elt
), 0, sizeof (EBITSET_WORDS (elt
)));
260 ebitset_elt_free (elt
)
263 EBITSET_NEXT (elt
) = ebitset_free_list
;
264 ebitset_free_list
= elt
;
268 /* Remove element with index EINDEX from bitset BSET. */
270 ebitset_elt_remove (bset
, eindex
)
277 elts
= EBITSET_ELTS (bset
);
282 ebitset_elt_free (elt
);
286 /* Add ELT into elts at index EINDEX of bitset BSET. */
288 ebitset_elt_add (bset
, elt
, eindex
)
295 elts
= EBITSET_ELTS (bset
);
296 /* Assume that the elts entry not allocated. */
301 /* Return nonzero if all bits in an element are zero. */
303 ebitset_elt_zero_p (elt
)
308 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
309 if (EBITSET_WORDS (elt
)[i
])
317 ebitset_elt_find (bset
, windex
, mode
)
319 bitset_windex windex
;
320 enum ebitset_find_mode mode
;
327 eindex
= windex
/ EBITSET_ELT_WORDS
;
329 elts
= EBITSET_ELTS (bset
);
330 size
= EBITSET_SIZE (bset
);
334 if ((elt
= elts
[eindex
]))
336 if (EBITSET_WORDS (elt
) == bset
->b
.cdata
)
339 EBITSET_CACHE_SET (bset
, eindex
);
344 /* The element could not be found. */
356 extra
= eindex
- size
+ 1;
358 /* We need to expand the table by EXTRA elements. It may be
359 better with large bitsets to grow the number of
360 elements by some fraction of the current size otherwise
361 we can spend a lot of time slowly increasing the size of the
363 if (extra
< EBITSET_GROW_SIZE
)
364 extra
= EBITSET_GROW_SIZE
;
366 ebitset_elts_grow (bset
, extra
);
369 /* Create a new element. */
370 elt
= ebitset_elt_calloc ();
371 ebitset_elt_add (bset
, elt
, eindex
);
372 EBITSET_CACHE_SET (bset
, eindex
);
376 return &ebitset_zero_elts
[0];
384 static inline ebitset_elt
*
385 ebitset_elt_last (bset
)
390 elts
= EBITSET_ELTS (bset
);
392 /* Assume that have at least one element in elts. */
393 return elts
[EBITSET_SIZE (bset
) - 1];
397 /* Weed out the zero elements from the elts. */
406 if (EBITSET_ZERO_P (bset
))
409 elts
= EBITSET_ELTS (bset
);
411 for (j
= 0; j
< EBITSET_SIZE (bset
); j
++)
413 ebitset_elt
*elt
= elts
[j
];
417 if (elt
&& ebitset_elt_zero_p (elt
))
419 ebitset_elt_remove (bset
, j
);
430 /* All the bits are zero. We could shrink the elts.
431 For now just mark BSET as known to be zero. */
432 EBITSET_ZERO_SET (bset
);
435 EBITSET_NONZERO_SET (bset
);
441 /* Set all bits in the bitset to zero. */
449 if (EBITSET_ZERO_P (bset
))
452 elts
= EBITSET_ELTS (bset
);
453 for (j
= 0; j
< EBITSET_SIZE (bset
); j
++)
455 ebitset_elt
*elt
= elts
[j
];
458 ebitset_elt_remove (bset
, j
);
461 /* All the bits are zero. We could shrink the elts.
462 For now just mark BSET as known to be zero. */
463 EBITSET_ZERO_SET (bset
);
468 ebitset_equal_p (dst
, src
)
482 if (EBITSET_SIZE (src
) != EBITSET_SIZE (dst
))
485 selts
= EBITSET_ELTS (src
);
486 delts
= EBITSET_ELTS (dst
);
488 for (j
= 0; j
< EBITSET_SIZE (src
); j
++)
491 ebitset_elt
*selt
= selts
[j
];
492 ebitset_elt
*delt
= delts
[j
];
496 if ((selt
&& !delt
) || (!selt
&& delt
))
499 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
500 if (EBITSET_WORDS (selt
)[i
] != EBITSET_WORDS (delt
)[i
])
507 /* Copy bits from bitset SRC to bitset DST. */
509 ebitset_copy_ (dst
, src
)
522 if (EBITSET_SIZE (dst
) < EBITSET_SIZE (src
))
523 ebitset_elts_grow (dst
, EBITSET_SIZE (src
) - EBITSET_SIZE (dst
));
525 selts
= EBITSET_ELTS (src
);
526 delts
= EBITSET_ELTS (dst
);
527 for (j
= 0; j
< EBITSET_SIZE (src
); j
++)
529 ebitset_elt
*selt
= selts
[j
];
535 tmp
= ebitset_elt_alloc ();
537 memcpy (EBITSET_WORDS (tmp
), EBITSET_WORDS (selt
),
538 sizeof (EBITSET_WORDS (selt
)));
541 EBITSET_NONZERO_SET (dst
);
545 /* Copy bits from bitset SRC to bitset DST. Return non-zero if
546 bitsets different. */
548 ebitset_copy_cmp (dst
, src
)
555 if (EBITSET_ZERO_P (dst
))
557 ebitset_copy_ (dst
, src
);
558 return !EBITSET_ZERO_P (src
);
561 if (ebitset_equal_p (dst
, src
))
564 ebitset_copy_ (dst
, src
);
569 /* Return size in bits of bitset SRC. */
574 /* Return current size of bitset in bits. */
575 return EBITSET_SIZE (src
) * EBITSET_ELT_BITS
;
579 /* Set bit BITNO in bitset DST. */
581 ebitset_set (dst
, bitno
)
585 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
587 ebitset_elt_find (dst
, windex
, EBITSET_CREATE
);
589 dst
->b
.cdata
[windex
- dst
->b
.cindex
] |=
590 (bitset_word
) 1 << (bitno
% BITSET_WORD_BITS
);
594 /* Reset bit BITNO in bitset DST. */
596 ebitset_reset (dst
, bitno
)
600 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
602 if (!ebitset_elt_find (dst
, windex
, EBITSET_FIND
))
605 dst
->b
.cdata
[windex
- dst
->b
.cindex
] &=
606 ~((bitset_word
) 1 << (bitno
% BITSET_WORD_BITS
));
608 /* If all the data is zero, perhaps we should remove it now...
609 However, there is a good chance that the element will be needed
614 /* Test bit BITNO in bitset SRC. */
616 ebitset_test (src
, bitno
)
620 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
622 if (!ebitset_elt_find (src
, windex
, EBITSET_FIND
))
626 cdata
[windex
- src
->b
.cindex
] >> (bitno
% BITSET_WORD_BITS
)) & 1;
635 free (EBITSET_ELTS (bset
));
639 /* Find list of up to NUM bits set in BSET starting from and including
640 *NEXT and store in array LIST. Return with actual number of bits
641 found and with *NEXT indicating where search stopped. */
643 ebitset_list_reverse (bset
, list
, num
, next
)
649 bitset_bindex n_bits
;
651 bitset_bindex rbitno
;
653 bitset_bindex boffset
;
654 bitset_windex windex
;
656 bitset_windex woffset
;
661 if (EBITSET_ZERO_P (bset
))
664 size
= EBITSET_SIZE (bset
);
665 n_bits
= size
* EBITSET_ELT_BITS
;
668 if (rbitno
>= n_bits
)
671 elts
= EBITSET_ELTS (bset
);
673 bitno
= n_bits
- (rbitno
+ 1);
675 windex
= bitno
/ BITSET_WORD_BITS
;
676 eindex
= bitno
/ EBITSET_ELT_BITS
;
677 woffset
= windex
- eindex
* EBITSET_ELT_WORDS
;
679 /* If num is 1, we could speed things up with a binary search
680 of the word of interest. */
683 bcount
= bitno
% BITSET_WORD_BITS
;
684 boffset
= windex
* BITSET_WORD_BITS
;
694 srcp
= EBITSET_WORDS (elt
);
700 word
= srcp
[woffset
] << (BITSET_WORD_BITS
- 1 - bcount
);
702 for (; word
; bcount
--)
704 if (word
& BITSET_MSB
)
706 list
[count
++] = boffset
+ bcount
;
709 *next
= n_bits
- (boffset
+ bcount
);
715 boffset
-= BITSET_WORD_BITS
;
716 bcount
= BITSET_WORD_BITS
- 1;
721 woffset
= EBITSET_ELT_WORDS
- 1;
722 boffset
= eindex
* EBITSET_ELT_BITS
- BITSET_WORD_BITS
;
726 *next
= n_bits
- (boffset
+ 1);
731 /* Find list of up to NUM bits set in BSET starting from and including
732 *NEXT and store in array LIST. Return with actual number of bits
733 found and with *NEXT indicating where search stopped. */
735 ebitset_list (bset
, list
, num
, next
)
742 bitset_windex windex
;
750 if (EBITSET_ZERO_P (bset
))
756 elts
= EBITSET_ELTS (bset
);
757 size
= EBITSET_SIZE (bset
);
758 eindex
= bitno
/ EBITSET_ELT_BITS
;
760 if (bitno
% EBITSET_ELT_BITS
)
762 /* We need to start within an element. This is not very common. */
767 bitset_windex woffset
;
768 bitset_word
*srcp
= EBITSET_WORDS (elt
);
770 windex
= bitno
/ BITSET_WORD_BITS
;
771 woffset
= eindex
/ EBITSET_ELT_WORDS
;
773 for (; (windex
- woffset
) < EBITSET_ELT_WORDS
; windex
++)
775 word
= srcp
[windex
- woffset
] >> (bitno
% BITSET_WORD_BITS
);
777 for (; word
; bitno
++)
781 list
[count
++] = bitno
;
790 bitno
= (windex
+ 1) * BITSET_WORD_BITS
;
794 /* Skip to next element. */
798 /* If num is 1, we could speed things up with a binary search
799 of the word of interest. */
801 for (; eindex
< size
; eindex
++)
810 srcp
= EBITSET_WORDS (elt
);
811 windex
= eindex
* EBITSET_ELT_WORDS
;
813 if ((count
+ EBITSET_ELT_BITS
) < num
)
815 /* The coast is clear, plant boot! */
817 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, windex
++)
819 bitno
= windex
* BITSET_WORD_BITS
;
824 if (!(word
& 0xffff))
834 for (; word
; bitno
++)
837 list
[count
++] = bitno
;
845 /* Tread more carefully since we need to check
846 if array overflows. */
848 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, windex
++)
850 bitno
= windex
* BITSET_WORD_BITS
;
852 for (word
= srcp
[i
]; word
; bitno
++)
856 list
[count
++] = bitno
;
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_WORDS
, EBITSET_CREATE
);
888 memset (EBITSET_WORDS (elt
), -1, sizeof (EBITSET_WORDS (elt
)));
890 EBITSET_NONZERO_SET (dst
);
895 ebitset_empty_p (dst
)
898 return !ebitset_weed (dst
);
903 ebitset_not (dst
, src
)
912 for (j
= 0; j
< EBITSET_SIZE (src
); j
++)
914 /* Create new elements for dst if they cannot be found
915 or substitute zero elements if src elements not found. */
917 ebitset_elt_find (dst
, j
* EBITSET_ELT_WORDS
, EBITSET_SUBST
);
919 ebitset_elt_find (dst
, j
* EBITSET_ELT_WORDS
, EBITSET_CREATE
);
921 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
922 EBITSET_WORDS (delt
)[i
] = ~EBITSET_WORDS (selt
)[i
];
924 EBITSET_NONZERO_SET (dst
);
928 /* Return 1 if DST == DST | SRC. */
930 ebitset_subset_p (dst
, src
)
940 selts
= EBITSET_ELTS (src
);
941 delts
= EBITSET_ELTS (dst
);
943 ssize
= EBITSET_SIZE (src
);
944 dsize
= EBITSET_SIZE (dst
);
946 for (j
= 0; j
< ssize
; j
++)
952 selt
= j
< ssize
? selts
[j
] : 0;
953 delt
= j
< dsize
? delts
[j
] : 0;
959 selt
= &ebitset_zero_elts
[0];
961 delt
= &ebitset_zero_elts
[0];
963 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
964 if (EBITSET_WORDS (delt
)[i
]
965 != (EBITSET_WORDS (selt
)[i
] | EBITSET_WORDS (delt
)[i
]))
972 /* Return 1 if DST & SRC == 0. */
974 ebitset_disjoint_p (dst
, src
)
984 selts
= EBITSET_ELTS (src
);
985 delts
= EBITSET_ELTS (dst
);
987 ssize
= EBITSET_SIZE (src
);
988 dsize
= EBITSET_SIZE (dst
);
990 for (j
= 0; j
< ssize
; j
++)
996 selt
= j
< ssize
? selts
[j
] : 0;
997 delt
= j
< dsize
? delts
[j
] : 0;
1002 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
1003 if ((EBITSET_WORDS (selt
)[i
] & EBITSET_WORDS (delt
)[i
]))
1012 ebitset_op3_cmp (dst
, src1
, src2
, op
)
1018 bitset_windex ssize1
;
1019 bitset_windex ssize2
;
1020 bitset_windex dsize
;
1022 ebitset_elts
*selts1
;
1023 ebitset_elts
*selts2
;
1024 ebitset_elts
*delts
;
1032 ssize1
= EBITSET_SIZE (src1
);
1033 ssize2
= EBITSET_SIZE (src2
);
1034 dsize
= EBITSET_SIZE (dst
);
1040 ebitset_elts_grow (dst
, size
- dsize
);
1042 selts1
= EBITSET_ELTS (src1
);
1043 selts2
= EBITSET_ELTS (src2
);
1044 delts
= EBITSET_ELTS (dst
);
1046 for (j
= 0; j
< size
; j
++)
1052 selt1
= j
< ssize1
? selts1
[j
] : 0;
1053 selt2
= j
< ssize2
? selts2
[j
] : 0;
1054 delt
= j
< dsize
? delts
[j
] : 0;
1056 if (!selt1
&& !selt2
)
1061 ebitset_elt_remove (dst
, j
);
1067 selt1
= &ebitset_zero_elts
[0];
1069 selt2
= &ebitset_zero_elts
[0];
1071 delt
= ebitset_elt_calloc ();
1075 srcp1
= EBITSET_WORDS (selt1
);
1076 srcp2
= EBITSET_WORDS (selt2
);
1077 dstp
= EBITSET_WORDS (delt
);
1081 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, dstp
++)
1083 bitset_word tmp
= *srcp1
++ | *srcp2
++;
1094 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, dstp
++)
1096 bitset_word tmp
= *srcp1
++ & *srcp2
++;
1107 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, dstp
++)
1109 bitset_word tmp
= *srcp1
++ ^ *srcp2
++;
1119 case BITSET_OP_ANDN
:
1120 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, dstp
++)
1122 bitset_word tmp
= *srcp1
++ & ~(*srcp2
++);
1136 if (!ebitset_elt_zero_p (delt
))
1138 ebitset_elt_add (dst
, delt
, j
);
1142 ebitset_elt_free (delt
);
1146 /* If we have elements of DST left over, free them all. */
1147 for (; j
< dsize
; j
++)
1156 ebitset_elt_remove (dst
, j
);
1159 EBITSET_NONZERO_SET (dst
);
1165 ebitset_and_cmp (dst
, src1
, src2
)
1172 if (EBITSET_ZERO_P (src2
))
1175 changed
= EBITSET_ZERO_P (dst
);
1179 else if (EBITSET_ZERO_P (src1
))
1182 changed
= EBITSET_ZERO_P (dst
);
1186 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_AND
);
1191 ebitset_andn_cmp (dst
, src1
, src2
)
1198 if (EBITSET_ZERO_P (src2
))
1200 return ebitset_copy_cmp (dst
, src1
);
1202 else if (EBITSET_ZERO_P (src1
))
1205 changed
= EBITSET_ZERO_P (dst
);
1209 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_ANDN
);
1214 ebitset_or_cmp (dst
, src1
, src2
)
1219 if (EBITSET_ZERO_P (src2
))
1221 return ebitset_copy_cmp (dst
, src1
);
1223 else if (EBITSET_ZERO_P (src1
))
1225 return ebitset_copy_cmp (dst
, src2
);
1227 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_OR
);
1232 ebitset_xor_cmp (dst
, src1
, src2
)
1237 if (EBITSET_ZERO_P (src2
))
1239 return ebitset_copy_cmp (dst
, src1
);
1241 else if (EBITSET_ZERO_P (src1
))
1243 return ebitset_copy_cmp (dst
, src2
);
1245 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_XOR
);
1250 ebitset_copy (dst
, src
)
1254 if (BITSET_COMPATIBLE_ (dst
, src
))
1255 ebitset_copy_ (dst
, src
);
1257 bitset_copy_ (dst
, src
);
1261 /* Vector of operations for linked-list bitsets. */
1262 struct bitset_vtable ebitset_vtable
= {
1277 (PFV
) ebitset_and_cmp
,
1279 (PFV
) ebitset_andn_cmp
,
1281 (PFV
) ebitset_or_cmp
,
1283 (PFV
) ebitset_xor_cmp
,
1285 (PFV
) bitset_and_or_cmp_
,
1287 (PFV
) bitset_andn_or_cmp_
,
1288 bitset_andn_or_cmp_
,
1289 (PFV
) bitset_or_and_cmp_
,
1292 ebitset_list_reverse
,
1298 /* Return size of initial structure. */
1300 ebitset_bytes (n_bits
)
1301 bitset_bindex n_bits ATTRIBUTE_UNUSED
;
1303 return sizeof (struct bitset_struct
);
1307 /* Initialize a bitset. */
1310 ebitset_init (bset
, n_bits
)
1312 bitset_bindex n_bits
;
1316 bset
->b
.vtable
= &ebitset_vtable
;
1318 bset
->b
.csize
= EBITSET_ELT_WORDS
;
1320 EBITSET_ZERO_SET (bset
);
1322 size
= n_bits
? (n_bits
+ EBITSET_ELT_BITS
- 1) / EBITSET_ELT_BITS
1323 : EBITSET_INITIAL_SIZE
;
1325 EBITSET_SIZE (bset
) = 0;
1326 EBITSET_ELTS (bset
) = 0;
1327 ebitset_elts_grow (bset
, size
);
1334 ebitset_release_memory ()
1336 ebitset_free_list
= 0;
1337 if (ebitset_obstack_init
)
1339 ebitset_obstack_init
= 0;
1340 obstack_free (&ebitset_obstack
, NULL
);