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 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) (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 bitset_windex 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
, bitset_windex
));
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
*, bitset_windex
));
115 static void ebitset_elt_remove
PARAMS ((bitset
, bitset_windex
));
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 bitset_windex 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 bitset_bindex 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 bitset_bindex ebitset_list
PARAMS ((bitset
, bitset_bindex
*,
140 bitset_bindex
, bitset_bindex
*));
141 static bitset_bindex 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_WINDEX_MAX, \
155 #define EBITSET_CACHE_DISABLE(BSET) ((BSET)->b.cindex = BITSET_WINDEX_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 bitset_windex oldsize
;
179 bitset_windex newsize
;
181 oldsize
= EBITSET_SIZE (bset
);
182 newsize
= oldsize
+ size
;
184 if (BITSET_SIZE_MAX
/ sizeof (ebitset_elt
*) < newsize
)
187 EBITSET_ELTS (bset
) = xrealloc (EBITSET_ELTS (bset
),
188 newsize
* sizeof (ebitset_elt
*));
189 EBITSET_SIZE (bset
) = newsize
;
191 memset (EBITSET_ELTS (bset
) + oldsize
, 0, size
* sizeof (ebitset_elt
*));
195 /* Allocate a ebitset element. The bits are not cleared. */
196 static inline ebitset_elt
*
201 if (ebitset_free_list
!= 0)
203 elt
= ebitset_free_list
;
204 ebitset_free_list
= EBITSET_NEXT (elt
);
208 if (!ebitset_obstack_init
)
210 ebitset_obstack_init
= 1;
212 /* Let particular systems override the size of a chunk. */
214 #ifndef OBSTACK_CHUNK_SIZE
215 #define OBSTACK_CHUNK_SIZE 0
218 /* Let them override the alloc and free routines too. */
220 #ifndef OBSTACK_CHUNK_ALLOC
221 #define OBSTACK_CHUNK_ALLOC xmalloc
224 #ifndef OBSTACK_CHUNK_FREE
225 #define OBSTACK_CHUNK_FREE free
228 #if !defined(__GNUC__) || (__GNUC__ < 2)
229 #define __alignof__(type) 0
232 obstack_specify_allocation (&ebitset_obstack
, OBSTACK_CHUNK_SIZE
,
233 __alignof__ (ebitset_elt
),
234 (void *(*)PARAMS ((long)))
236 (void (*)PARAMS ((void *)))
240 /* Perhaps we should add a number of new elements to the free
242 elt
= (ebitset_elt
*) obstack_alloc (&ebitset_obstack
,
243 sizeof (ebitset_elt
));
250 /* Allocate a ebitset element. The bits are cleared. */
251 static inline ebitset_elt
*
252 ebitset_elt_calloc ()
256 elt
= ebitset_elt_alloc ();
257 memset (EBITSET_WORDS (elt
), 0, sizeof (EBITSET_WORDS (elt
)));
263 ebitset_elt_free (elt
)
266 EBITSET_NEXT (elt
) = ebitset_free_list
;
267 ebitset_free_list
= elt
;
271 /* Remove element with index EINDEX from bitset BSET. */
273 ebitset_elt_remove (bset
, eindex
)
275 bitset_windex eindex
;
280 elts
= EBITSET_ELTS (bset
);
285 ebitset_elt_free (elt
);
289 /* Add ELT into elts at index EINDEX of bitset BSET. */
291 ebitset_elt_add (bset
, elt
, eindex
)
294 bitset_windex eindex
;
298 elts
= EBITSET_ELTS (bset
);
299 /* Assume that the elts entry not allocated. */
304 /* Return nonzero if all bits in an element are zero. */
306 ebitset_elt_zero_p (elt
)
311 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
312 if (EBITSET_WORDS (elt
)[i
])
320 ebitset_elt_find (bset
, windex
, mode
)
322 bitset_windex windex
;
323 enum ebitset_find_mode mode
;
327 bitset_windex eindex
;
330 eindex
= windex
/ EBITSET_ELT_WORDS
;
332 elts
= EBITSET_ELTS (bset
);
333 size
= EBITSET_SIZE (bset
);
337 if ((elt
= elts
[eindex
]))
339 if (EBITSET_WORDS (elt
) == bset
->b
.cdata
)
342 EBITSET_CACHE_SET (bset
, eindex
);
347 /* The element could not be found. */
359 extra
= eindex
- size
+ 1;
361 /* We need to expand the table by EXTRA elements. It may be
362 better with large bitsets to grow the number of
363 elements by some fraction of the current size otherwise
364 we can spend a lot of time slowly increasing the size of the
366 if (extra
< EBITSET_GROW_SIZE
)
367 extra
= EBITSET_GROW_SIZE
;
369 ebitset_elts_grow (bset
, extra
);
372 /* Create a new element. */
373 elt
= ebitset_elt_calloc ();
374 ebitset_elt_add (bset
, elt
, eindex
);
375 EBITSET_CACHE_SET (bset
, eindex
);
379 return &ebitset_zero_elts
[0];
387 static inline ebitset_elt
*
388 ebitset_elt_last (bset
)
393 elts
= EBITSET_ELTS (bset
);
395 /* Assume that have at least one element in elts. */
396 return elts
[EBITSET_SIZE (bset
) - 1];
400 /* Weed out the zero elements from the elts. */
401 static inline bitset_windex
409 if (EBITSET_ZERO_P (bset
))
412 elts
= EBITSET_ELTS (bset
);
414 for (j
= 0; j
< EBITSET_SIZE (bset
); j
++)
416 ebitset_elt
*elt
= elts
[j
];
420 if (elt
&& ebitset_elt_zero_p (elt
))
422 ebitset_elt_remove (bset
, j
);
433 /* All the bits are zero. We could shrink the elts.
434 For now just mark BSET as known to be zero. */
435 EBITSET_ZERO_SET (bset
);
438 EBITSET_NONZERO_SET (bset
);
444 /* Set all bits in the bitset to zero. */
452 if (EBITSET_ZERO_P (bset
))
455 elts
= EBITSET_ELTS (bset
);
456 for (j
= 0; j
< EBITSET_SIZE (bset
); j
++)
458 ebitset_elt
*elt
= elts
[j
];
461 ebitset_elt_remove (bset
, j
);
464 /* All the bits are zero. We could shrink the elts.
465 For now just mark BSET as known to be zero. */
466 EBITSET_ZERO_SET (bset
);
471 ebitset_equal_p (dst
, src
)
485 if (EBITSET_SIZE (src
) != EBITSET_SIZE (dst
))
488 selts
= EBITSET_ELTS (src
);
489 delts
= EBITSET_ELTS (dst
);
491 for (j
= 0; j
< EBITSET_SIZE (src
); j
++)
494 ebitset_elt
*selt
= selts
[j
];
495 ebitset_elt
*delt
= delts
[j
];
499 if ((selt
&& !delt
) || (!selt
&& delt
))
502 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
503 if (EBITSET_WORDS (selt
)[i
] != EBITSET_WORDS (delt
)[i
])
510 /* Copy bits from bitset SRC to bitset DST. */
512 ebitset_copy_ (dst
, src
)
525 if (EBITSET_SIZE (dst
) < EBITSET_SIZE (src
))
526 ebitset_elts_grow (dst
, EBITSET_SIZE (src
) - EBITSET_SIZE (dst
));
528 selts
= EBITSET_ELTS (src
);
529 delts
= EBITSET_ELTS (dst
);
530 for (j
= 0; j
< EBITSET_SIZE (src
); j
++)
532 ebitset_elt
*selt
= selts
[j
];
538 tmp
= ebitset_elt_alloc ();
540 memcpy (EBITSET_WORDS (tmp
), EBITSET_WORDS (selt
),
541 sizeof (EBITSET_WORDS (selt
)));
544 EBITSET_NONZERO_SET (dst
);
548 /* Copy bits from bitset SRC to bitset DST. Return non-zero if
549 bitsets different. */
551 ebitset_copy_cmp (dst
, src
)
558 if (EBITSET_ZERO_P (dst
))
560 ebitset_copy_ (dst
, src
);
561 return !EBITSET_ZERO_P (src
);
564 if (ebitset_equal_p (dst
, src
))
567 ebitset_copy_ (dst
, src
);
572 /* Return size in bits of bitset SRC. */
577 /* Return current size of bitset in bits. */
578 return EBITSET_SIZE (src
) * EBITSET_ELT_BITS
;
582 /* Set bit BITNO in bitset DST. */
584 ebitset_set (dst
, bitno
)
588 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
590 ebitset_elt_find (dst
, windex
, EBITSET_CREATE
);
592 dst
->b
.cdata
[windex
- dst
->b
.cindex
] |=
593 (bitset_word
) 1 << (bitno
% BITSET_WORD_BITS
);
597 /* Reset bit BITNO in bitset DST. */
599 ebitset_reset (dst
, bitno
)
603 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
605 if (!ebitset_elt_find (dst
, windex
, EBITSET_FIND
))
608 dst
->b
.cdata
[windex
- dst
->b
.cindex
] &=
609 ~((bitset_word
) 1 << (bitno
% BITSET_WORD_BITS
));
611 /* If all the data is zero, perhaps we should remove it now...
612 However, there is a good chance that the element will be needed
617 /* Test bit BITNO in bitset SRC. */
619 ebitset_test (src
, bitno
)
623 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
625 if (!ebitset_elt_find (src
, windex
, EBITSET_FIND
))
629 cdata
[windex
- src
->b
.cindex
] >> (bitno
% BITSET_WORD_BITS
)) & 1;
638 free (EBITSET_ELTS (bset
));
642 /* Find list of up to NUM bits set in BSET starting from and including
643 *NEXT and store in array LIST. Return with actual number of bits
644 found and with *NEXT indicating where search stopped. */
646 ebitset_list_reverse (bset
, list
, num
, next
)
652 bitset_bindex n_bits
;
654 bitset_bindex rbitno
;
656 bitset_bindex boffset
;
657 bitset_windex windex
;
658 bitset_windex eindex
;
659 bitset_windex woffset
;
664 if (EBITSET_ZERO_P (bset
))
667 size
= EBITSET_SIZE (bset
);
668 n_bits
= size
* EBITSET_ELT_BITS
;
671 if (rbitno
>= n_bits
)
674 elts
= EBITSET_ELTS (bset
);
676 bitno
= n_bits
- (rbitno
+ 1);
678 windex
= bitno
/ BITSET_WORD_BITS
;
679 eindex
= bitno
/ EBITSET_ELT_BITS
;
680 woffset
= windex
- eindex
* EBITSET_ELT_WORDS
;
682 /* If num is 1, we could speed things up with a binary search
683 of the word of interest. */
686 bcount
= bitno
% BITSET_WORD_BITS
;
687 boffset
= windex
* BITSET_WORD_BITS
;
697 srcp
= EBITSET_WORDS (elt
);
703 word
= srcp
[woffset
] << (BITSET_WORD_BITS
- 1 - bcount
);
705 for (; word
; bcount
--)
707 if (word
& BITSET_MSB
)
709 list
[count
++] = boffset
+ bcount
;
712 *next
= n_bits
- (boffset
+ bcount
);
718 boffset
-= BITSET_WORD_BITS
;
719 bcount
= BITSET_WORD_BITS
- 1;
724 woffset
= EBITSET_ELT_WORDS
- 1;
725 boffset
= eindex
* EBITSET_ELT_BITS
- BITSET_WORD_BITS
;
729 *next
= n_bits
- (boffset
+ 1);
734 /* Find list of up to NUM bits set in BSET starting from and including
735 *NEXT and store in array LIST. Return with actual number of bits
736 found and with *NEXT indicating where search stopped. */
738 ebitset_list (bset
, list
, num
, next
)
745 bitset_windex windex
;
746 bitset_windex eindex
;
753 if (EBITSET_ZERO_P (bset
))
759 elts
= EBITSET_ELTS (bset
);
760 size
= EBITSET_SIZE (bset
);
761 eindex
= bitno
/ EBITSET_ELT_BITS
;
763 if (bitno
% EBITSET_ELT_BITS
)
765 /* We need to start within an element. This is not very common. */
770 bitset_windex woffset
;
771 bitset_word
*srcp
= EBITSET_WORDS (elt
);
773 windex
= bitno
/ BITSET_WORD_BITS
;
774 woffset
= eindex
* EBITSET_ELT_WORDS
;
776 for (; (windex
- woffset
) < EBITSET_ELT_WORDS
; windex
++)
778 word
= srcp
[windex
- woffset
] >> (bitno
% BITSET_WORD_BITS
);
780 for (; word
; bitno
++)
784 list
[count
++] = bitno
;
793 bitno
= (windex
+ 1) * BITSET_WORD_BITS
;
797 /* Skip to next element. */
801 /* If num is 1, we could speed things up with a binary search
802 of the word of interest. */
804 for (; eindex
< size
; eindex
++)
813 srcp
= EBITSET_WORDS (elt
);
814 windex
= eindex
* EBITSET_ELT_WORDS
;
816 if ((count
+ EBITSET_ELT_BITS
) < num
)
818 /* The coast is clear, plant boot! */
820 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, windex
++)
822 bitno
= windex
* BITSET_WORD_BITS
;
827 if (!(word
& 0xffff))
837 for (; word
; bitno
++)
840 list
[count
++] = bitno
;
848 /* Tread more carefully since we need to check
849 if array overflows. */
851 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, windex
++)
853 bitno
= windex
* BITSET_WORD_BITS
;
855 for (word
= srcp
[i
]; word
; bitno
++)
859 list
[count
++] = bitno
;
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_WORDS
, EBITSET_CREATE
);
891 memset (EBITSET_WORDS (elt
), -1, sizeof (EBITSET_WORDS (elt
)));
893 EBITSET_NONZERO_SET (dst
);
898 ebitset_empty_p (dst
)
901 return !ebitset_weed (dst
);
906 ebitset_not (dst
, src
)
915 for (j
= 0; j
< EBITSET_SIZE (src
); j
++)
917 /* Create new elements for dst if they cannot be found
918 or substitute zero elements if src elements not found. */
920 ebitset_elt_find (dst
, j
* EBITSET_ELT_WORDS
, EBITSET_SUBST
);
922 ebitset_elt_find (dst
, j
* EBITSET_ELT_WORDS
, EBITSET_CREATE
);
924 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
925 EBITSET_WORDS (delt
)[i
] = ~EBITSET_WORDS (selt
)[i
];
927 EBITSET_NONZERO_SET (dst
);
931 /* Return 1 if DST == DST | SRC. */
933 ebitset_subset_p (dst
, src
)
943 selts
= EBITSET_ELTS (src
);
944 delts
= EBITSET_ELTS (dst
);
946 ssize
= EBITSET_SIZE (src
);
947 dsize
= EBITSET_SIZE (dst
);
949 for (j
= 0; j
< ssize
; j
++)
955 selt
= j
< ssize
? selts
[j
] : 0;
956 delt
= j
< dsize
? delts
[j
] : 0;
962 selt
= &ebitset_zero_elts
[0];
964 delt
= &ebitset_zero_elts
[0];
966 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
967 if (EBITSET_WORDS (delt
)[i
]
968 != (EBITSET_WORDS (selt
)[i
] | EBITSET_WORDS (delt
)[i
]))
975 /* Return 1 if DST & SRC == 0. */
977 ebitset_disjoint_p (dst
, src
)
987 selts
= EBITSET_ELTS (src
);
988 delts
= EBITSET_ELTS (dst
);
990 ssize
= EBITSET_SIZE (src
);
991 dsize
= EBITSET_SIZE (dst
);
993 for (j
= 0; j
< ssize
; j
++)
999 selt
= j
< ssize
? selts
[j
] : 0;
1000 delt
= j
< dsize
? delts
[j
] : 0;
1005 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
1006 if ((EBITSET_WORDS (selt
)[i
] & EBITSET_WORDS (delt
)[i
]))
1015 ebitset_op3_cmp (dst
, src1
, src2
, op
)
1021 bitset_windex ssize1
;
1022 bitset_windex ssize2
;
1023 bitset_windex dsize
;
1025 ebitset_elts
*selts1
;
1026 ebitset_elts
*selts2
;
1027 ebitset_elts
*delts
;
1035 ssize1
= EBITSET_SIZE (src1
);
1036 ssize2
= EBITSET_SIZE (src2
);
1037 dsize
= EBITSET_SIZE (dst
);
1043 ebitset_elts_grow (dst
, size
- dsize
);
1045 selts1
= EBITSET_ELTS (src1
);
1046 selts2
= EBITSET_ELTS (src2
);
1047 delts
= EBITSET_ELTS (dst
);
1049 for (j
= 0; j
< size
; j
++)
1055 selt1
= j
< ssize1
? selts1
[j
] : 0;
1056 selt2
= j
< ssize2
? selts2
[j
] : 0;
1057 delt
= j
< dsize
? delts
[j
] : 0;
1059 if (!selt1
&& !selt2
)
1064 ebitset_elt_remove (dst
, j
);
1070 selt1
= &ebitset_zero_elts
[0];
1072 selt2
= &ebitset_zero_elts
[0];
1074 delt
= ebitset_elt_calloc ();
1078 srcp1
= EBITSET_WORDS (selt1
);
1079 srcp2
= EBITSET_WORDS (selt2
);
1080 dstp
= EBITSET_WORDS (delt
);
1084 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, dstp
++)
1086 bitset_word tmp
= *srcp1
++ | *srcp2
++;
1097 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, dstp
++)
1099 bitset_word tmp
= *srcp1
++ & *srcp2
++;
1110 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, dstp
++)
1112 bitset_word tmp
= *srcp1
++ ^ *srcp2
++;
1122 case BITSET_OP_ANDN
:
1123 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, dstp
++)
1125 bitset_word tmp
= *srcp1
++ & ~(*srcp2
++);
1139 if (!ebitset_elt_zero_p (delt
))
1141 ebitset_elt_add (dst
, delt
, j
);
1145 ebitset_elt_free (delt
);
1149 /* If we have elements of DST left over, free them all. */
1150 for (; j
< dsize
; j
++)
1159 ebitset_elt_remove (dst
, j
);
1162 EBITSET_NONZERO_SET (dst
);
1168 ebitset_and_cmp (dst
, src1
, src2
)
1175 if (EBITSET_ZERO_P (src2
))
1178 changed
= EBITSET_ZERO_P (dst
);
1182 else if (EBITSET_ZERO_P (src1
))
1185 changed
= EBITSET_ZERO_P (dst
);
1189 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_AND
);
1194 ebitset_andn_cmp (dst
, src1
, src2
)
1201 if (EBITSET_ZERO_P (src2
))
1203 return ebitset_copy_cmp (dst
, src1
);
1205 else if (EBITSET_ZERO_P (src1
))
1208 changed
= EBITSET_ZERO_P (dst
);
1212 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_ANDN
);
1217 ebitset_or_cmp (dst
, src1
, src2
)
1222 if (EBITSET_ZERO_P (src2
))
1224 return ebitset_copy_cmp (dst
, src1
);
1226 else if (EBITSET_ZERO_P (src1
))
1228 return ebitset_copy_cmp (dst
, src2
);
1230 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_OR
);
1235 ebitset_xor_cmp (dst
, src1
, src2
)
1240 if (EBITSET_ZERO_P (src2
))
1242 return ebitset_copy_cmp (dst
, src1
);
1244 else if (EBITSET_ZERO_P (src1
))
1246 return ebitset_copy_cmp (dst
, src2
);
1248 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_XOR
);
1253 ebitset_copy (dst
, src
)
1257 if (BITSET_COMPATIBLE_ (dst
, src
))
1258 ebitset_copy_ (dst
, src
);
1260 bitset_copy_ (dst
, src
);
1264 /* Vector of operations for linked-list bitsets. */
1265 struct bitset_vtable ebitset_vtable
= {
1280 (PFV
) ebitset_and_cmp
,
1282 (PFV
) ebitset_andn_cmp
,
1284 (PFV
) ebitset_or_cmp
,
1286 (PFV
) ebitset_xor_cmp
,
1288 (PFV
) bitset_and_or_cmp_
,
1290 (PFV
) bitset_andn_or_cmp_
,
1291 bitset_andn_or_cmp_
,
1292 (PFV
) bitset_or_and_cmp_
,
1295 ebitset_list_reverse
,
1301 /* Return size of initial structure. */
1303 ebitset_bytes (n_bits
)
1304 bitset_bindex n_bits ATTRIBUTE_UNUSED
;
1306 return sizeof (struct bitset_struct
);
1310 /* Initialize a bitset. */
1313 ebitset_init (bset
, n_bits
)
1315 bitset_bindex n_bits
;
1319 bset
->b
.vtable
= &ebitset_vtable
;
1321 bset
->b
.csize
= EBITSET_ELT_WORDS
;
1323 EBITSET_ZERO_SET (bset
);
1325 size
= n_bits
? (n_bits
+ EBITSET_ELT_BITS
- 1) / EBITSET_ELT_BITS
1326 : EBITSET_INITIAL_SIZE
;
1328 EBITSET_SIZE (bset
) = 0;
1329 EBITSET_ELTS (bset
) = 0;
1330 ebitset_elts_grow (bset
, size
);
1337 ebitset_release_memory ()
1339 ebitset_free_list
= 0;
1340 if (ebitset_obstack_init
)
1342 ebitset_obstack_init
= 0;
1343 obstack_free (&ebitset_obstack
, NULL
);