1 /* Functions to support link list bitsets.
3 Copyright (C) 2002-2004, 2006, 2009-2012 Free Software Foundation, Inc.
5 Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 /* This file implements linked-list bitsets. These bitsets can be of
31 arbitrary length and are more efficient than arrays of bits for
34 Usually if all the bits in an element are zero we remove the element
35 from the list. However, a side effect of the bit caching is that we
36 do not always notice when an element becomes zero. Hence the
37 lbitset_weed function which removes zero elements. */
40 /* Number of words to use for each element. The larger the value the
41 greater the size of the cache and the shorter the time to find a given bit
42 but the more memory wasted for sparse bitsets and the longer the time
43 to search for set bits.
45 The routines that dominate timing profiles are lbitset_elt_find
46 and lbitset_elt_link, especially when accessing the bits randomly. */
48 #define LBITSET_ELT_WORDS 2
50 typedef bitset_word lbitset_word
;
52 #define LBITSET_WORD_BITS BITSET_WORD_BITS
54 /* Number of bits stored in each element. */
55 #define LBITSET_ELT_BITS \
56 ((unsigned int) (LBITSET_ELT_WORDS * LBITSET_WORD_BITS))
58 /* Lbitset element. We use an array of bits for each element.
59 These are linked together in a doubly-linked list. */
60 typedef struct lbitset_elt_struct
62 struct lbitset_elt_struct
*next
; /* Next element. */
63 struct lbitset_elt_struct
*prev
; /* Previous element. */
64 bitset_windex index
; /* bitno / BITSET_WORD_BITS. */
65 bitset_word words
[LBITSET_ELT_WORDS
]; /* Bits that are set. */
70 enum lbitset_find_mode
71 { LBITSET_FIND
, LBITSET_CREATE
, LBITSET_SUBST
};
73 static lbitset_elt lbitset_zero_elts
[3]; /* Elements of all zero bits. */
75 /* Obstack to allocate bitset elements from. */
76 static struct obstack lbitset_obstack
;
77 static bool lbitset_obstack_init
= false;
78 static lbitset_elt
*lbitset_free_list
; /* Free list of bitset elements. */
80 extern void debug_lbitset (bitset
);
82 #define LBITSET_CURRENT1(X) \
83 ((lbitset_elt *) (void *) ((char *) (X) - offsetof (lbitset_elt, words)))
85 #define LBITSET_CURRENT(X) LBITSET_CURRENT1((X)->b.cdata)
87 #define LBITSET_HEAD(X) ((X)->l.head)
88 #define LBITSET_TAIL(X) ((X)->l.tail)
90 /* Allocate a lbitset element. The bits are not cleared. */
91 static inline lbitset_elt
*
92 lbitset_elt_alloc (void)
96 if (lbitset_free_list
!= 0)
98 elt
= lbitset_free_list
;
99 lbitset_free_list
= elt
->next
;
103 if (!lbitset_obstack_init
)
105 lbitset_obstack_init
= true;
107 /* Let particular systems override the size of a chunk. */
109 #ifndef OBSTACK_CHUNK_SIZE
110 #define OBSTACK_CHUNK_SIZE 0
113 /* Let them override the alloc and free routines too. */
115 #ifndef OBSTACK_CHUNK_ALLOC
116 #define OBSTACK_CHUNK_ALLOC xmalloc
119 #ifndef OBSTACK_CHUNK_FREE
120 #define OBSTACK_CHUNK_FREE free
123 #if ! defined __GNUC__ || __GNUC__ < 2
124 #define __alignof__(type) 0
127 obstack_specify_allocation (&lbitset_obstack
, OBSTACK_CHUNK_SIZE
,
128 __alignof__ (lbitset_elt
),
133 /* Perhaps we should add a number of new elements to the free
135 elt
= (lbitset_elt
*) obstack_alloc (&lbitset_obstack
,
136 sizeof (lbitset_elt
));
143 /* Allocate a lbitset element. The bits are cleared. */
144 static inline lbitset_elt
*
145 lbitset_elt_calloc (void)
149 elt
= lbitset_elt_alloc ();
150 memset (elt
->words
, 0, sizeof (elt
->words
));
156 lbitset_elt_free (lbitset_elt
*elt
)
158 elt
->next
= lbitset_free_list
;
159 lbitset_free_list
= elt
;
163 /* Unlink element ELT from bitset BSET. */
165 lbitset_elt_unlink (bitset bset
, lbitset_elt
*elt
)
167 lbitset_elt
*next
= elt
->next
;
168 lbitset_elt
*prev
= elt
->prev
;
176 if (LBITSET_HEAD (bset
) == elt
)
177 LBITSET_HEAD (bset
) = next
;
178 if (LBITSET_TAIL (bset
) == elt
)
179 LBITSET_TAIL (bset
) = prev
;
181 /* Update cache pointer. Since the first thing we try is to insert
182 before current, make current the next entry in preference to the
184 if (LBITSET_CURRENT (bset
) == elt
)
188 bset
->b
.cdata
= next
->words
;
189 bset
->b
.cindex
= next
->index
;
193 bset
->b
.cdata
= prev
->words
;
194 bset
->b
.cindex
= prev
->index
;
203 lbitset_elt_free (elt
);
207 /* Cut the chain of bitset BSET before element ELT and free the
210 lbitset_prune (bitset bset
, lbitset_elt
*elt
)
219 LBITSET_TAIL (bset
) = elt
->prev
;
220 bset
->b
.cdata
= elt
->prev
->words
;
221 bset
->b
.cindex
= elt
->prev
->index
;
226 LBITSET_HEAD (bset
) = 0;
227 LBITSET_TAIL (bset
) = 0;
232 for (; elt
; elt
= next
)
235 lbitset_elt_free (elt
);
240 /* Are all bits in an element zero? */
242 lbitset_elt_zero_p (lbitset_elt
*elt
)
246 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++)
254 /* Link the bitset element into the current bitset linked list. */
256 lbitset_elt_link (bitset bset
, lbitset_elt
*elt
)
258 bitset_windex windex
= elt
->index
;
260 lbitset_elt
*current
;
263 current
= LBITSET_CURRENT (bset
);
265 current
= LBITSET_HEAD (bset
);
267 /* If this is the first and only element, add it in. */
268 if (LBITSET_HEAD (bset
) == 0)
270 elt
->next
= elt
->prev
= 0;
271 LBITSET_HEAD (bset
) = elt
;
272 LBITSET_TAIL (bset
) = elt
;
275 /* If this index is less than that of the current element, it goes
276 somewhere before the current element. */
277 else if (windex
< bset
->b
.cindex
)
280 ptr
->prev
&& ptr
->prev
->index
> windex
; ptr
= ptr
->prev
)
284 ptr
->prev
->next
= elt
;
286 LBITSET_HEAD (bset
) = elt
;
288 elt
->prev
= ptr
->prev
;
293 /* Otherwise, it must go somewhere after the current element. */
297 ptr
->next
&& ptr
->next
->index
< windex
; ptr
= ptr
->next
)
301 ptr
->next
->prev
= elt
;
303 LBITSET_TAIL (bset
) = elt
;
305 elt
->next
= ptr
->next
;
310 /* Set up so this is the first element searched. */
311 bset
->b
.cindex
= windex
;
312 bset
->b
.csize
= LBITSET_ELT_WORDS
;
313 bset
->b
.cdata
= elt
->words
;
318 lbitset_elt_find (bitset bset
, bitset_windex windex
,
319 enum lbitset_find_mode mode
)
322 lbitset_elt
*current
;
326 current
= LBITSET_CURRENT (bset
);
327 /* Check if element is the cached element. */
328 if ((windex
- bset
->b
.cindex
) < bset
->b
.csize
)
333 current
= LBITSET_HEAD (bset
);
338 if (windex
< bset
->b
.cindex
)
341 elt
->prev
&& elt
->index
> windex
; elt
= elt
->prev
)
347 elt
->next
&& (elt
->index
+ LBITSET_ELT_WORDS
- 1) < windex
;
352 /* ELT is the nearest to the one we want. If it's not the one
353 we want, the one we want does not exist. */
354 if (windex
- elt
->index
< LBITSET_ELT_WORDS
)
356 bset
->b
.cindex
= elt
->index
;
357 bset
->b
.csize
= LBITSET_ELT_WORDS
;
358 bset
->b
.cdata
= elt
->words
;
372 windex
-= windex
% LBITSET_ELT_WORDS
;
374 elt
= lbitset_elt_calloc ();
376 lbitset_elt_link (bset
, elt
);
380 return &lbitset_zero_elts
[0];
385 /* Weed out the zero elements from the list. */
387 lbitset_weed (bitset bset
)
392 for (elt
= LBITSET_HEAD (bset
); elt
; elt
= next
)
395 if (lbitset_elt_zero_p (elt
))
396 lbitset_elt_unlink (bset
, elt
);
401 /* Set all bits in the bitset to zero. */
403 lbitset_zero (bitset bset
)
407 head
= LBITSET_HEAD (bset
);
411 /* Clear a bitset by freeing the linked list at the head element. */
412 lbitset_prune (bset
, head
);
418 lbitset_equal_p (bitset dst
, bitset src
)
429 for (selt
= LBITSET_HEAD (src
), delt
= LBITSET_HEAD (dst
);
430 selt
&& delt
; selt
= selt
->next
, delt
= delt
->next
)
432 if (selt
->index
!= delt
->index
)
435 for (j
= 0; j
< LBITSET_ELT_WORDS
; j
++)
436 if (delt
->words
[j
] != selt
->words
[j
])
439 return !selt
&& !delt
;
443 /* Copy bits from bitset SRC to bitset DST. */
445 lbitset_copy (bitset dst
, bitset src
)
457 head
= LBITSET_HEAD (src
);
462 for (elt
= head
; elt
; elt
= elt
->next
)
464 tmp
= lbitset_elt_alloc ();
465 tmp
->index
= elt
->index
;
471 LBITSET_HEAD (dst
) = tmp
;
474 memcpy (tmp
->words
, elt
->words
, sizeof (elt
->words
));
476 LBITSET_TAIL (dst
) = tmp
;
478 dst
->b
.csize
= LBITSET_ELT_WORDS
;
479 dst
->b
.cdata
= LBITSET_HEAD (dst
)->words
;
480 dst
->b
.cindex
= LBITSET_HEAD (dst
)->index
;
484 /* Copy bits from bitset SRC to bitset DST. Return true if
485 bitsets different. */
487 lbitset_copy_cmp (bitset dst
, bitset src
)
492 if (!LBITSET_HEAD (dst
))
494 lbitset_copy (dst
, src
);
495 return LBITSET_HEAD (src
) != 0;
498 if (lbitset_equal_p (dst
, src
))
501 lbitset_copy (dst
, src
);
507 lbitset_resize (bitset src
, bitset_bindex size
)
509 BITSET_NBITS_ (src
) = size
;
511 /* Need to prune any excess bits. FIXME. */
515 /* Set bit BITNO in bitset DST. */
517 lbitset_set (bitset dst
, bitset_bindex bitno
)
519 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
521 lbitset_elt_find (dst
, windex
, LBITSET_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 lbitset_reset (bitset dst
, bitset_bindex bitno
)
532 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
534 if (!lbitset_elt_find (dst
, windex
, LBITSET_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 unlink it now... */
544 /* Test bit BITNO in bitset SRC. */
546 lbitset_test (bitset src
, bitset_bindex bitno
)
548 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
550 return (lbitset_elt_find (src
, windex
, LBITSET_FIND
)
551 && ((src
->b
.cdata
[windex
- src
->b
.cindex
]
552 >> (bitno
% BITSET_WORD_BITS
))
558 lbitset_free (bitset bset
)
564 /* Find list of up to NUM bits set in BSET starting from and including
565 *NEXT and store in array LIST. Return with actual number of bits
566 found and with *NEXT indicating where search stopped. */
568 lbitset_list_reverse (bitset bset
, bitset_bindex
*list
,
569 bitset_bindex num
, bitset_bindex
*next
)
571 bitset_bindex rbitno
;
574 bitset_bindex boffset
;
575 bitset_windex windex
;
579 bitset_bindex n_bits
;
581 elt
= LBITSET_TAIL (bset
);
585 n_bits
= (elt
->index
+ LBITSET_ELT_WORDS
) * BITSET_WORD_BITS
;
588 if (rbitno
>= n_bits
)
591 bitno
= n_bits
- (rbitno
+ 1);
593 windex
= bitno
/ BITSET_WORD_BITS
;
595 /* Skip back to starting element. */
596 for (; elt
&& elt
->index
> windex
; elt
= elt
->prev
)
602 if (windex
>= elt
->index
+ LBITSET_ELT_WORDS
)
604 /* We are trying to start in no-mans land so start
605 at end of current elt. */
606 bcount
= BITSET_WORD_BITS
- 1;
607 windex
= elt
->index
+ LBITSET_ELT_WORDS
- 1;
611 bcount
= bitno
% BITSET_WORD_BITS
;
615 boffset
= windex
* BITSET_WORD_BITS
;
617 /* If num is 1, we could speed things up with a binary search
618 of the word of interest. */
622 bitset_word
*srcp
= elt
->words
;
624 for (; (windex
- elt
->index
) < LBITSET_ELT_WORDS
;
625 windex
--, boffset
-= BITSET_WORD_BITS
,
626 bcount
= BITSET_WORD_BITS
- 1)
629 srcp
[windex
- elt
->index
] << (BITSET_WORD_BITS
- 1 - bcount
);
631 for (; word
; bcount
--)
633 if (word
& BITSET_MSB
)
635 list
[count
++] = boffset
+ bcount
;
638 *next
= n_bits
- (boffset
+ bcount
);
649 windex
= elt
->index
+ LBITSET_ELT_WORDS
- 1;
650 boffset
= windex
* BITSET_WORD_BITS
;
654 *next
= n_bits
- (boffset
+ 1);
659 /* Find list of up to NUM bits set in BSET starting from and including
660 *NEXT and store in array LIST. Return with actual number of bits
661 found and with *NEXT indicating where search stopped. */
663 lbitset_list (bitset bset
, bitset_bindex
*list
,
664 bitset_bindex num
, bitset_bindex
*next
)
667 bitset_windex windex
;
673 head
= LBITSET_HEAD (bset
);
682 /* This is the most common case. */
684 /* Start with the first element. */
687 bitno
= windex
* BITSET_WORD_BITS
;
691 windex
= bitno
/ BITSET_WORD_BITS
;
693 /* Skip to starting element. */
695 elt
&& (elt
->index
+ LBITSET_ELT_WORDS
- 1) < windex
;
702 if (windex
< elt
->index
)
705 bitno
= windex
* BITSET_WORD_BITS
;
709 bitset_word
*srcp
= elt
->words
;
711 /* We are starting within an element. */
713 for (; (windex
- elt
->index
) < LBITSET_ELT_WORDS
; windex
++)
715 word
= srcp
[windex
- elt
->index
] >> (bitno
% BITSET_WORD_BITS
);
717 for (; word
; bitno
++)
721 list
[count
++] = bitno
;
730 bitno
= (windex
+ 1) * BITSET_WORD_BITS
;
737 bitno
= windex
* BITSET_WORD_BITS
;
743 /* If num is 1, we could speed things up with a binary search
744 of the word of interest. */
749 bitset_word
*srcp
= elt
->words
;
751 if ((count
+ LBITSET_ELT_BITS
) < num
)
753 /* The coast is clear, plant boot! */
755 #if LBITSET_ELT_WORDS == 2
759 if (!(word
& 0xffff))
769 for (; word
; bitno
++)
772 list
[count
++] = bitno
;
777 bitno
= windex
* BITSET_WORD_BITS
;
782 if (!(word
& 0xffff))
787 for (; word
; bitno
++)
790 list
[count
++] = bitno
;
795 bitno
= windex
* BITSET_WORD_BITS
;
797 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++)
802 if (!(word
& 0xffff))
812 for (; word
; bitno
++)
815 list
[count
++] = bitno
;
820 bitno
= windex
* BITSET_WORD_BITS
;
826 /* Tread more carefully since we need to check
827 if array overflows. */
829 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++)
831 for (word
= srcp
[i
]; word
; bitno
++)
835 list
[count
++] = bitno
;
845 bitno
= windex
* BITSET_WORD_BITS
;
853 bitno
= windex
* BITSET_WORD_BITS
;
863 lbitset_empty_p (bitset dst
)
868 for (elt
= LBITSET_HEAD (dst
); elt
; elt
= next
)
871 if (!lbitset_elt_zero_p (elt
))
874 lbitset_elt_unlink (dst
, elt
);
881 /* Ensure that any unused bits within the last element are clear. */
883 lbitset_unused_clear (bitset dst
)
885 unsigned int last_bit
;
886 bitset_bindex n_bits
;
888 n_bits
= BITSET_SIZE_ (dst
);
889 last_bit
= n_bits
% LBITSET_ELT_BITS
;
894 bitset_windex windex
;
897 elt
= LBITSET_TAIL (dst
);
899 windex
= n_bits
/ BITSET_WORD_BITS
;
901 srcp
[windex
- elt
->index
] &= ((bitset_word
) 1 << last_bit
) - 1;
904 for (; (windex
- elt
->index
) < LBITSET_ELT_WORDS
; windex
++)
905 srcp
[windex
- elt
->index
] = 0;
911 lbitset_ones (bitset dst
)
914 bitset_windex windex
;
917 /* This is a decidedly unfriendly operation for a linked list
918 bitset! It makes a sparse bitset become dense. An alternative
919 is to have a flag that indicates that the bitset stores the
920 complement of what it indicates. */
922 windex
= (BITSET_SIZE_ (dst
) + BITSET_WORD_BITS
- 1) / BITSET_WORD_BITS
;
924 for (i
= 0; i
< windex
; i
+= LBITSET_ELT_WORDS
)
926 /* Create new elements if they cannot be found. */
927 elt
= lbitset_elt_find (dst
, i
, LBITSET_CREATE
);
928 memset (elt
->words
, -1, sizeof (elt
->words
));
931 lbitset_unused_clear (dst
);
936 lbitset_not (bitset dst
, bitset src
)
942 bitset_windex windex
;
944 windex
= (BITSET_SIZE_ (dst
) + BITSET_WORD_BITS
- 1) / BITSET_WORD_BITS
;
946 for (i
= 0; i
< windex
; i
+= LBITSET_ELT_WORDS
)
948 /* Create new elements for dst if they cannot be found
949 or substitute zero elements if src elements not found. */
950 selt
= lbitset_elt_find (src
, i
, LBITSET_SUBST
);
951 delt
= lbitset_elt_find (dst
, i
, LBITSET_CREATE
);
953 for (j
= 0; j
< LBITSET_ELT_WORDS
; j
++)
954 delt
->words
[j
] = ~selt
->words
[j
];
956 lbitset_unused_clear (dst
);
962 /* Is DST == DST | SRC? */
964 lbitset_subset_p (bitset dst
, bitset src
)
970 for (selt
= LBITSET_HEAD (src
), delt
= LBITSET_HEAD (dst
);
971 selt
|| delt
; selt
= selt
->next
, delt
= delt
->next
)
974 selt
= &lbitset_zero_elts
[0];
976 delt
= &lbitset_zero_elts
[0];
977 else if (selt
->index
!= delt
->index
)
979 if (selt
->index
< delt
->index
)
981 lbitset_zero_elts
[2].next
= delt
;
982 delt
= &lbitset_zero_elts
[2];
986 lbitset_zero_elts
[1].next
= selt
;
987 selt
= &lbitset_zero_elts
[1];
991 for (j
= 0; j
< LBITSET_ELT_WORDS
; j
++)
992 if (delt
->words
[j
] != (selt
->words
[j
] | delt
->words
[j
]))
999 /* Is DST & SRC == 0? */
1001 lbitset_disjoint_p (bitset dst
, bitset src
)
1007 for (selt
= LBITSET_HEAD (src
), delt
= LBITSET_HEAD (dst
);
1008 selt
&& delt
; selt
= selt
->next
, delt
= delt
->next
)
1010 if (selt
->index
!= delt
->index
)
1012 if (selt
->index
< delt
->index
)
1014 lbitset_zero_elts
[2].next
= delt
;
1015 delt
= &lbitset_zero_elts
[2];
1019 lbitset_zero_elts
[1].next
= selt
;
1020 selt
= &lbitset_zero_elts
[1];
1022 /* Since the elements are different, there is no
1023 intersection of these elements. */
1027 for (j
= 0; j
< LBITSET_ELT_WORDS
; j
++)
1028 if (selt
->words
[j
] & delt
->words
[j
])
1036 lbitset_op3_cmp (bitset dst
, bitset src1
, bitset src2
, enum bitset_ops op
)
1038 lbitset_elt
*selt1
= LBITSET_HEAD (src1
);
1039 lbitset_elt
*selt2
= LBITSET_HEAD (src2
);
1040 lbitset_elt
*delt
= LBITSET_HEAD (dst
);
1041 bitset_windex windex1
;
1042 bitset_windex windex2
;
1043 bitset_windex windex
;
1050 bool changed
= false;
1053 LBITSET_HEAD (dst
) = 0;
1056 windex1
= (selt1
) ? selt1
->index
: BITSET_WINDEX_MAX
;
1057 windex2
= (selt2
) ? selt2
->index
: BITSET_WINDEX_MAX
;
1059 while (selt1
|| selt2
)
1061 /* Figure out whether we need to substitute zero elements for
1063 if (windex1
== windex2
)
1068 selt1
= selt1
->next
;
1069 windex1
= (selt1
) ? selt1
->index
: BITSET_WINDEX_MAX
;
1070 selt2
= selt2
->next
;
1071 windex2
= (selt2
) ? selt2
->index
: BITSET_WINDEX_MAX
;
1073 else if (windex1
< windex2
)
1077 stmp2
= &lbitset_zero_elts
[0];
1078 selt1
= selt1
->next
;
1079 windex1
= (selt1
) ? selt1
->index
: BITSET_WINDEX_MAX
;
1084 stmp1
= &lbitset_zero_elts
[0];
1086 selt2
= selt2
->next
;
1087 windex2
= (selt2
) ? selt2
->index
: BITSET_WINDEX_MAX
;
1090 /* Find the appropriate element from DST. Begin by discarding
1091 elements that we've skipped. */
1092 while (delt
&& delt
->index
< windex
)
1097 lbitset_elt_free (dtmp
);
1099 if (delt
&& delt
->index
== windex
)
1105 dtmp
= lbitset_elt_calloc ();
1107 /* Do the operation, and if any bits are set, link it into the
1109 srcp1
= stmp1
->words
;
1110 srcp2
= stmp2
->words
;
1118 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++, dstp
++)
1120 bitset_word tmp
= *srcp1
++ | *srcp2
++;
1131 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++, dstp
++)
1133 bitset_word tmp
= *srcp1
++ & *srcp2
++;
1144 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++, dstp
++)
1146 bitset_word tmp
= *srcp1
++ ^ *srcp2
++;
1156 case BITSET_OP_ANDN
:
1157 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++, dstp
++)
1159 bitset_word tmp
= *srcp1
++ & ~(*srcp2
++);
1170 if (!lbitset_elt_zero_p (dtmp
))
1172 dtmp
->index
= windex
;
1173 /* Perhaps this could be optimised... */
1174 lbitset_elt_link (dst
, dtmp
);
1178 lbitset_elt_free (dtmp
);
1182 /* If we have elements of DST left over, free them all. */
1186 lbitset_prune (dst
, delt
);
1194 lbitset_and_cmp (bitset dst
, bitset src1
, bitset src2
)
1196 lbitset_elt
*selt1
= LBITSET_HEAD (src1
);
1197 lbitset_elt
*selt2
= LBITSET_HEAD (src2
);
1203 changed
= !LBITSET_HEAD (dst
);
1210 changed
= !LBITSET_HEAD (dst
);
1214 return lbitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_AND
);
1219 lbitset_and (bitset dst
, bitset src1
, bitset src2
)
1221 lbitset_and_cmp (dst
, src1
, src2
);
1226 lbitset_andn_cmp (bitset dst
, bitset src1
, bitset src2
)
1228 lbitset_elt
*selt1
= LBITSET_HEAD (src1
);
1229 lbitset_elt
*selt2
= LBITSET_HEAD (src2
);
1234 return lbitset_copy_cmp (dst
, src1
);
1239 changed
= !LBITSET_HEAD (dst
);
1243 return lbitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_ANDN
);
1248 lbitset_andn (bitset dst
, bitset src1
, bitset src2
)
1250 lbitset_andn_cmp (dst
, src1
, src2
);
1255 lbitset_or_cmp (bitset dst
, bitset src1
, bitset src2
)
1257 lbitset_elt
*selt1
= LBITSET_HEAD (src1
);
1258 lbitset_elt
*selt2
= LBITSET_HEAD (src2
);
1262 return lbitset_copy_cmp (dst
, src1
);
1266 return lbitset_copy_cmp (dst
, src2
);
1268 return lbitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_OR
);
1273 lbitset_or (bitset dst
, bitset src1
, bitset src2
)
1275 lbitset_or_cmp (dst
, src1
, src2
);
1280 lbitset_xor_cmp (bitset dst
, bitset src1
, bitset src2
)
1282 lbitset_elt
*selt1
= LBITSET_HEAD (src1
);
1283 lbitset_elt
*selt2
= LBITSET_HEAD (src2
);
1287 return lbitset_copy_cmp (dst
, src1
);
1291 return lbitset_copy_cmp (dst
, src2
);
1293 return lbitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_XOR
);
1298 lbitset_xor (bitset dst
, bitset src1
, bitset src2
)
1300 lbitset_xor_cmp (dst
, src1
, src2
);
1305 /* Vector of operations for linked-list bitsets. */
1306 struct bitset_vtable lbitset_vtable
= {
1333 bitset_andn_or_cmp_
,
1337 lbitset_list_reverse
,
1343 /* Return size of initial structure. */
1345 lbitset_bytes (bitset_bindex n_bits ATTRIBUTE_UNUSED
)
1347 return sizeof (struct lbitset_struct
);
1351 /* Initialize a bitset. */
1353 lbitset_init (bitset bset
, bitset_bindex n_bits ATTRIBUTE_UNUSED
)
1355 BITSET_NBITS_ (bset
) = n_bits
;
1356 bset
->b
.vtable
= &lbitset_vtable
;
1362 lbitset_release_memory (void)
1364 lbitset_free_list
= 0;
1365 if (lbitset_obstack_init
)
1367 lbitset_obstack_init
= false;
1368 obstack_free (&lbitset_obstack
, NULL
);
1373 /* Function to be called from debugger to debug lbitset. */
1375 debug_lbitset (bitset bset
)
1383 for (elt
= LBITSET_HEAD (bset
); elt
; elt
= elt
->next
)
1385 fprintf (stderr
, "Elt %lu\n", (unsigned long int) elt
->index
);
1386 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++)
1391 word
= elt
->words
[i
];
1393 fprintf (stderr
, " Word %u:", i
);
1394 for (j
= 0; j
< LBITSET_WORD_BITS
; j
++)
1395 if ((word
& ((bitset_word
) 1 << j
)))
1396 fprintf (stderr
, " %u", j
);
1397 fprintf (stderr
, "\n");