1 /* Functions to support link list bitsets.
2 Copyright (C) 2002, 2003, 2004, 2006 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 Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
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 (elt
&& (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
)
943 bitset_windex windex
;
945 /* This is another unfriendly operation for a linked list
947 elt
= LBITSET_TAIL (dst
);
949 windex
= (BITSET_SIZE_ (dst
) + BITSET_WORD_BITS
- 1) / BITSET_WORD_BITS
;
951 for (i
= 0; i
< windex
; i
+= LBITSET_ELT_WORDS
)
953 /* Create new elements for dst if they cannot be found
954 or substitute zero elements if src elements not found. */
955 selt
= lbitset_elt_find (src
, i
, LBITSET_SUBST
);
956 delt
= lbitset_elt_find (dst
, i
, LBITSET_CREATE
);
958 for (j
= 0; j
< LBITSET_ELT_WORDS
; j
++)
959 delt
->words
[j
] = ~selt
->words
[j
];
961 lbitset_unused_clear (dst
);
967 /* Is DST == DST | SRC? */
969 lbitset_subset_p (bitset dst
, bitset src
)
975 for (selt
= LBITSET_HEAD (src
), delt
= LBITSET_HEAD (dst
);
976 selt
|| delt
; selt
= selt
->next
, delt
= delt
->next
)
979 selt
= &lbitset_zero_elts
[0];
981 delt
= &lbitset_zero_elts
[0];
982 else if (selt
->index
!= delt
->index
)
984 if (selt
->index
< delt
->index
)
986 lbitset_zero_elts
[2].next
= delt
;
987 delt
= &lbitset_zero_elts
[2];
991 lbitset_zero_elts
[1].next
= selt
;
992 selt
= &lbitset_zero_elts
[1];
996 for (j
= 0; j
< LBITSET_ELT_WORDS
; j
++)
997 if (delt
->words
[j
] != (selt
->words
[j
] | delt
->words
[j
]))
1004 /* Is DST & SRC == 0? */
1006 lbitset_disjoint_p (bitset dst
, bitset src
)
1012 for (selt
= LBITSET_HEAD (src
), delt
= LBITSET_HEAD (dst
);
1013 selt
&& delt
; selt
= selt
->next
, delt
= delt
->next
)
1015 if (selt
->index
!= delt
->index
)
1017 if (selt
->index
< delt
->index
)
1019 lbitset_zero_elts
[2].next
= delt
;
1020 delt
= &lbitset_zero_elts
[2];
1024 lbitset_zero_elts
[1].next
= selt
;
1025 selt
= &lbitset_zero_elts
[1];
1027 /* Since the elements are different, there is no
1028 intersection of these elements. */
1032 for (j
= 0; j
< LBITSET_ELT_WORDS
; j
++)
1033 if (selt
->words
[j
] & delt
->words
[j
])
1041 lbitset_op3_cmp (bitset dst
, bitset src1
, bitset src2
, enum bitset_ops op
)
1043 lbitset_elt
*selt1
= LBITSET_HEAD (src1
);
1044 lbitset_elt
*selt2
= LBITSET_HEAD (src2
);
1045 lbitset_elt
*delt
= LBITSET_HEAD (dst
);
1046 bitset_windex windex1
;
1047 bitset_windex windex2
;
1048 bitset_windex windex
;
1055 bool changed
= false;
1058 LBITSET_HEAD (dst
) = 0;
1061 windex1
= (selt1
) ? selt1
->index
: BITSET_WINDEX_MAX
;
1062 windex2
= (selt2
) ? selt2
->index
: BITSET_WINDEX_MAX
;
1064 while (selt1
|| selt2
)
1066 /* Figure out whether we need to substitute zero elements for
1068 if (windex1
== windex2
)
1073 selt1
= selt1
->next
;
1074 windex1
= (selt1
) ? selt1
->index
: BITSET_WINDEX_MAX
;
1075 selt2
= selt2
->next
;
1076 windex2
= (selt2
) ? selt2
->index
: BITSET_WINDEX_MAX
;
1078 else if (windex1
< windex2
)
1082 stmp2
= &lbitset_zero_elts
[0];
1083 selt1
= selt1
->next
;
1084 windex1
= (selt1
) ? selt1
->index
: BITSET_WINDEX_MAX
;
1089 stmp1
= &lbitset_zero_elts
[0];
1091 selt2
= selt2
->next
;
1092 windex2
= (selt2
) ? selt2
->index
: BITSET_WINDEX_MAX
;
1095 /* Find the appropriate element from DST. Begin by discarding
1096 elements that we've skipped. */
1097 while (delt
&& delt
->index
< windex
)
1102 lbitset_elt_free (dtmp
);
1104 if (delt
&& delt
->index
== windex
)
1110 dtmp
= lbitset_elt_calloc ();
1112 /* Do the operation, and if any bits are set, link it into the
1114 srcp1
= stmp1
->words
;
1115 srcp2
= stmp2
->words
;
1123 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++, dstp
++)
1125 bitset_word tmp
= *srcp1
++ | *srcp2
++;
1136 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++, dstp
++)
1138 bitset_word tmp
= *srcp1
++ & *srcp2
++;
1149 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++, dstp
++)
1151 bitset_word tmp
= *srcp1
++ ^ *srcp2
++;
1161 case BITSET_OP_ANDN
:
1162 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++, dstp
++)
1164 bitset_word tmp
= *srcp1
++ & ~(*srcp2
++);
1175 if (!lbitset_elt_zero_p (dtmp
))
1177 dtmp
->index
= windex
;
1178 /* Perhaps this could be optimised... */
1179 lbitset_elt_link (dst
, dtmp
);
1183 lbitset_elt_free (dtmp
);
1187 /* If we have elements of DST left over, free them all. */
1191 lbitset_prune (dst
, delt
);
1199 lbitset_and_cmp (bitset dst
, bitset src1
, bitset src2
)
1201 lbitset_elt
*selt1
= LBITSET_HEAD (src1
);
1202 lbitset_elt
*selt2
= LBITSET_HEAD (src2
);
1208 changed
= !LBITSET_HEAD (dst
);
1215 changed
= !LBITSET_HEAD (dst
);
1219 return lbitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_AND
);
1224 lbitset_and (bitset dst
, bitset src1
, bitset src2
)
1226 lbitset_and_cmp (dst
, src1
, src2
);
1231 lbitset_andn_cmp (bitset dst
, bitset src1
, bitset src2
)
1233 lbitset_elt
*selt1
= LBITSET_HEAD (src1
);
1234 lbitset_elt
*selt2
= LBITSET_HEAD (src2
);
1239 return lbitset_copy_cmp (dst
, src1
);
1244 changed
= !LBITSET_HEAD (dst
);
1248 return lbitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_ANDN
);
1253 lbitset_andn (bitset dst
, bitset src1
, bitset src2
)
1255 lbitset_andn_cmp (dst
, src1
, src2
);
1260 lbitset_or_cmp (bitset dst
, bitset src1
, bitset src2
)
1262 lbitset_elt
*selt1
= LBITSET_HEAD (src1
);
1263 lbitset_elt
*selt2
= LBITSET_HEAD (src2
);
1267 return lbitset_copy_cmp (dst
, src1
);
1271 return lbitset_copy_cmp (dst
, src2
);
1273 return lbitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_OR
);
1278 lbitset_or (bitset dst
, bitset src1
, bitset src2
)
1280 lbitset_or_cmp (dst
, src1
, src2
);
1285 lbitset_xor_cmp (bitset dst
, bitset src1
, bitset src2
)
1287 lbitset_elt
*selt1
= LBITSET_HEAD (src1
);
1288 lbitset_elt
*selt2
= LBITSET_HEAD (src2
);
1292 return lbitset_copy_cmp (dst
, src1
);
1296 return lbitset_copy_cmp (dst
, src2
);
1298 return lbitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_XOR
);
1303 lbitset_xor (bitset dst
, bitset src1
, bitset src2
)
1305 lbitset_xor_cmp (dst
, src1
, src2
);
1310 /* Vector of operations for linked-list bitsets. */
1311 struct bitset_vtable lbitset_vtable
= {
1338 bitset_andn_or_cmp_
,
1342 lbitset_list_reverse
,
1348 /* Return size of initial structure. */
1350 lbitset_bytes (bitset_bindex n_bits ATTRIBUTE_UNUSED
)
1352 return sizeof (struct lbitset_struct
);
1356 /* Initialize a bitset. */
1358 lbitset_init (bitset bset
, bitset_bindex n_bits ATTRIBUTE_UNUSED
)
1360 BITSET_NBITS_ (bset
) = n_bits
;
1361 bset
->b
.vtable
= &lbitset_vtable
;
1367 lbitset_release_memory (void)
1369 lbitset_free_list
= 0;
1370 if (lbitset_obstack_init
)
1372 lbitset_obstack_init
= false;
1373 obstack_free (&lbitset_obstack
, NULL
);
1378 /* Function to be called from debugger to debug lbitset. */
1380 debug_lbitset (bitset bset
)
1388 for (elt
= LBITSET_HEAD (bset
); elt
; elt
= elt
->next
)
1390 fprintf (stderr
, "Elt %lu\n", (unsigned long int) elt
->index
);
1391 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++)
1396 word
= elt
->words
[i
];
1398 fprintf (stderr
, " Word %u:", i
);
1399 for (j
= 0; j
< LBITSET_WORD_BITS
; j
++)
1400 if ((word
& ((bitset_word
) 1 << j
)))
1401 fprintf (stderr
, " %u", j
);
1402 fprintf (stderr
, "\n");