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. */
29 /* This file implements linked-list bitsets. These bitsets can be of
30 arbitrary length and are more efficient than arrays of bits for
33 Usually if all the bits in an element are zero we remove the element
34 from the list. However, a side effect of the bit caching is that we
35 do not always notice when an element becomes zero. Hence the
36 lbitset_weed function which removes zero elements. */
39 /* Number of words to use for each element. The larger the value the
40 greater the size of the cache and the shorter the time to find a given bit
41 but the more memory wasted for sparse bitsets and the longer the time
42 to search for set bits.
44 The routines that dominate timing profiles are lbitset_elt_find
45 and lbitset_elt_link, especially when accessing the bits randomly. */
47 #define LBITSET_ELT_WORDS 2
49 typedef bitset_word lbitset_word
;
51 #define LBITSET_WORD_BITS BITSET_WORD_BITS
53 /* Number of bits stored in each element. */
54 #define LBITSET_ELT_BITS \
55 ((unsigned int) (LBITSET_ELT_WORDS * LBITSET_WORD_BITS))
57 /* Lbitset element. We use an array of bits for each element.
58 These are linked together in a doubly-linked list. */
59 typedef struct lbitset_elt_struct
61 struct lbitset_elt_struct
*next
; /* Next element. */
62 struct lbitset_elt_struct
*prev
; /* Previous element. */
63 bitset_windex index
; /* bitno / BITSET_WORD_BITS. */
64 bitset_word words
[LBITSET_ELT_WORDS
]; /* Bits that are set. */
69 enum lbitset_find_mode
70 { LBITSET_FIND
, LBITSET_CREATE
, LBITSET_SUBST
};
72 static lbitset_elt lbitset_zero_elts
[3]; /* Elements of all zero bits. */
74 /* Obstack to allocate bitset elements from. */
75 static struct obstack lbitset_obstack
;
76 static bool lbitset_obstack_init
= false;
77 static lbitset_elt
*lbitset_free_list
; /* Free list of bitset elements. */
79 extern void debug_lbitset (bitset
);
81 #define LBITSET_CURRENT1(X) \
82 ((lbitset_elt *) (void *) ((char *) (X) - offsetof (lbitset_elt, words)))
84 #define LBITSET_CURRENT(X) LBITSET_CURRENT1((X)->b.cdata)
86 #define LBITSET_HEAD(X) ((X)->l.head)
87 #define LBITSET_TAIL(X) ((X)->l.tail)
89 /* Allocate a lbitset element. The bits are not cleared. */
90 static inline lbitset_elt
*
91 lbitset_elt_alloc (void)
95 if (lbitset_free_list
!= 0)
97 elt
= lbitset_free_list
;
98 lbitset_free_list
= elt
->next
;
102 if (!lbitset_obstack_init
)
104 lbitset_obstack_init
= true;
106 /* Let particular systems override the size of a chunk. */
108 #ifndef OBSTACK_CHUNK_SIZE
109 #define OBSTACK_CHUNK_SIZE 0
112 /* Let them override the alloc and free routines too. */
114 #ifndef OBSTACK_CHUNK_ALLOC
115 #define OBSTACK_CHUNK_ALLOC xmalloc
118 #ifndef OBSTACK_CHUNK_FREE
119 #define OBSTACK_CHUNK_FREE free
122 #if ! defined __GNUC__ || __GNUC__ < 2
123 #define __alignof__(type) 0
126 obstack_specify_allocation (&lbitset_obstack
, OBSTACK_CHUNK_SIZE
,
127 __alignof__ (lbitset_elt
),
132 /* Perhaps we should add a number of new elements to the free
134 elt
= (lbitset_elt
*) obstack_alloc (&lbitset_obstack
,
135 sizeof (lbitset_elt
));
142 /* Allocate a lbitset element. The bits are cleared. */
143 static inline lbitset_elt
*
144 lbitset_elt_calloc (void)
148 elt
= lbitset_elt_alloc ();
149 memset (elt
->words
, 0, sizeof (elt
->words
));
155 lbitset_elt_free (lbitset_elt
*elt
)
157 elt
->next
= lbitset_free_list
;
158 lbitset_free_list
= elt
;
162 /* Unlink element ELT from bitset BSET. */
164 lbitset_elt_unlink (bitset bset
, lbitset_elt
*elt
)
166 lbitset_elt
*next
= elt
->next
;
167 lbitset_elt
*prev
= elt
->prev
;
175 if (LBITSET_HEAD (bset
) == elt
)
176 LBITSET_HEAD (bset
) = next
;
177 if (LBITSET_TAIL (bset
) == elt
)
178 LBITSET_TAIL (bset
) = prev
;
180 /* Update cache pointer. Since the first thing we try is to insert
181 before current, make current the next entry in preference to the
183 if (LBITSET_CURRENT (bset
) == elt
)
187 bset
->b
.cdata
= next
->words
;
188 bset
->b
.cindex
= next
->index
;
192 bset
->b
.cdata
= prev
->words
;
193 bset
->b
.cindex
= prev
->index
;
202 lbitset_elt_free (elt
);
206 /* Cut the chain of bitset BSET before element ELT and free the
209 lbitset_prune (bitset bset
, lbitset_elt
*elt
)
218 LBITSET_TAIL (bset
) = elt
->prev
;
219 bset
->b
.cdata
= elt
->prev
->words
;
220 bset
->b
.cindex
= elt
->prev
->index
;
225 LBITSET_HEAD (bset
) = 0;
226 LBITSET_TAIL (bset
) = 0;
231 for (; elt
; elt
= next
)
234 lbitset_elt_free (elt
);
239 /* Are all bits in an element zero? */
241 lbitset_elt_zero_p (lbitset_elt
*elt
)
245 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++)
253 /* Link the bitset element into the current bitset linked list. */
255 lbitset_elt_link (bitset bset
, lbitset_elt
*elt
)
257 bitset_windex windex
= elt
->index
;
259 lbitset_elt
*current
;
262 current
= LBITSET_CURRENT (bset
);
264 current
= LBITSET_HEAD (bset
);
266 /* If this is the first and only element, add it in. */
267 if (LBITSET_HEAD (bset
) == 0)
269 elt
->next
= elt
->prev
= 0;
270 LBITSET_HEAD (bset
) = elt
;
271 LBITSET_TAIL (bset
) = elt
;
274 /* If this index is less than that of the current element, it goes
275 somewhere before the current element. */
276 else if (windex
< bset
->b
.cindex
)
279 ptr
->prev
&& ptr
->prev
->index
> windex
; ptr
= ptr
->prev
)
283 ptr
->prev
->next
= elt
;
285 LBITSET_HEAD (bset
) = elt
;
287 elt
->prev
= ptr
->prev
;
292 /* Otherwise, it must go somewhere after the current element. */
296 ptr
->next
&& ptr
->next
->index
< windex
; ptr
= ptr
->next
)
300 ptr
->next
->prev
= elt
;
302 LBITSET_TAIL (bset
) = elt
;
304 elt
->next
= ptr
->next
;
309 /* Set up so this is the first element searched. */
310 bset
->b
.cindex
= windex
;
311 bset
->b
.csize
= LBITSET_ELT_WORDS
;
312 bset
->b
.cdata
= elt
->words
;
317 lbitset_elt_find (bitset bset
, bitset_windex windex
,
318 enum lbitset_find_mode mode
)
321 lbitset_elt
*current
;
325 current
= LBITSET_CURRENT (bset
);
326 /* Check if element is the cached element. */
327 if ((windex
- bset
->b
.cindex
) < bset
->b
.csize
)
332 current
= LBITSET_HEAD (bset
);
337 if (windex
< bset
->b
.cindex
)
340 elt
->prev
&& elt
->index
> windex
; elt
= elt
->prev
)
346 elt
->next
&& (elt
->index
+ LBITSET_ELT_WORDS
- 1) < windex
;
351 /* ELT is the nearest to the one we want. If it's not the one
352 we want, the one we want does not exist. */
353 if (elt
&& (windex
- elt
->index
) < LBITSET_ELT_WORDS
)
355 bset
->b
.cindex
= elt
->index
;
356 bset
->b
.csize
= LBITSET_ELT_WORDS
;
357 bset
->b
.cdata
= elt
->words
;
371 windex
-= windex
% LBITSET_ELT_WORDS
;
373 elt
= lbitset_elt_calloc ();
375 lbitset_elt_link (bset
, elt
);
379 return &lbitset_zero_elts
[0];
384 /* Weed out the zero elements from the list. */
386 lbitset_weed (bitset bset
)
391 for (elt
= LBITSET_HEAD (bset
); elt
; elt
= next
)
394 if (lbitset_elt_zero_p (elt
))
395 lbitset_elt_unlink (bset
, elt
);
400 /* Set all bits in the bitset to zero. */
402 lbitset_zero (bitset bset
)
406 head
= LBITSET_HEAD (bset
);
410 /* Clear a bitset by freeing the linked list at the head element. */
411 lbitset_prune (bset
, head
);
417 lbitset_equal_p (bitset dst
, bitset src
)
428 for (selt
= LBITSET_HEAD (src
), delt
= LBITSET_HEAD (dst
);
429 selt
&& delt
; selt
= selt
->next
, delt
= delt
->next
)
431 if (selt
->index
!= delt
->index
)
434 for (j
= 0; j
< LBITSET_ELT_WORDS
; j
++)
435 if (delt
->words
[j
] != selt
->words
[j
])
438 return !selt
&& !delt
;
442 /* Copy bits from bitset SRC to bitset DST. */
444 lbitset_copy (bitset dst
, bitset src
)
456 head
= LBITSET_HEAD (src
);
461 for (elt
= head
; elt
; elt
= elt
->next
)
463 tmp
= lbitset_elt_alloc ();
464 tmp
->index
= elt
->index
;
470 LBITSET_HEAD (dst
) = tmp
;
473 memcpy (tmp
->words
, elt
->words
, sizeof (elt
->words
));
475 LBITSET_TAIL (dst
) = tmp
;
477 dst
->b
.csize
= LBITSET_ELT_WORDS
;
478 dst
->b
.cdata
= LBITSET_HEAD (dst
)->words
;
479 dst
->b
.cindex
= LBITSET_HEAD (dst
)->index
;
483 /* Copy bits from bitset SRC to bitset DST. Return true if
484 bitsets different. */
486 lbitset_copy_cmp (bitset dst
, bitset src
)
491 if (!LBITSET_HEAD (dst
))
493 lbitset_copy (dst
, src
);
494 return LBITSET_HEAD (src
) != 0;
497 if (lbitset_equal_p (dst
, src
))
500 lbitset_copy (dst
, src
);
506 lbitset_resize (bitset src
, bitset_bindex size
)
508 BITSET_NBITS_ (src
) = size
;
510 /* Need to prune any excess bits. FIXME. */
514 /* Set bit BITNO in bitset DST. */
516 lbitset_set (bitset dst
, bitset_bindex bitno
)
518 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
520 lbitset_elt_find (dst
, windex
, LBITSET_CREATE
);
522 dst
->b
.cdata
[windex
- dst
->b
.cindex
] |=
523 (bitset_word
) 1 << (bitno
% BITSET_WORD_BITS
);
527 /* Reset bit BITNO in bitset DST. */
529 lbitset_reset (bitset dst
, bitset_bindex bitno
)
531 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
533 if (!lbitset_elt_find (dst
, windex
, LBITSET_FIND
))
536 dst
->b
.cdata
[windex
- dst
->b
.cindex
] &=
537 ~((bitset_word
) 1 << (bitno
% BITSET_WORD_BITS
));
539 /* If all the data is zero, perhaps we should unlink it now... */
543 /* Test bit BITNO in bitset SRC. */
545 lbitset_test (bitset src
, bitset_bindex bitno
)
547 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
549 return (lbitset_elt_find (src
, windex
, LBITSET_FIND
)
550 && ((src
->b
.cdata
[windex
- src
->b
.cindex
]
551 >> (bitno
% BITSET_WORD_BITS
))
557 lbitset_free (bitset bset
)
563 /* Find list of up to NUM bits set in BSET starting from and including
564 *NEXT and store in array LIST. Return with actual number of bits
565 found and with *NEXT indicating where search stopped. */
567 lbitset_list_reverse (bitset bset
, bitset_bindex
*list
,
568 bitset_bindex num
, bitset_bindex
*next
)
570 bitset_bindex rbitno
;
573 bitset_bindex boffset
;
574 bitset_windex windex
;
578 bitset_bindex n_bits
;
580 elt
= LBITSET_TAIL (bset
);
584 n_bits
= (elt
->index
+ LBITSET_ELT_WORDS
) * BITSET_WORD_BITS
;
587 if (rbitno
>= n_bits
)
590 bitno
= n_bits
- (rbitno
+ 1);
592 windex
= bitno
/ BITSET_WORD_BITS
;
594 /* Skip back to starting element. */
595 for (; elt
&& elt
->index
> windex
; elt
= elt
->prev
)
601 if (windex
>= elt
->index
+ LBITSET_ELT_WORDS
)
603 /* We are trying to start in no-mans land so start
604 at end of current elt. */
605 bcount
= BITSET_WORD_BITS
- 1;
606 windex
= elt
->index
+ LBITSET_ELT_WORDS
- 1;
610 bcount
= bitno
% BITSET_WORD_BITS
;
614 boffset
= windex
* BITSET_WORD_BITS
;
616 /* If num is 1, we could speed things up with a binary search
617 of the word of interest. */
621 bitset_word
*srcp
= elt
->words
;
623 for (; (windex
- elt
->index
) < LBITSET_ELT_WORDS
;
624 windex
--, boffset
-= BITSET_WORD_BITS
,
625 bcount
= BITSET_WORD_BITS
- 1)
628 srcp
[windex
- elt
->index
] << (BITSET_WORD_BITS
- 1 - bcount
);
630 for (; word
; bcount
--)
632 if (word
& BITSET_MSB
)
634 list
[count
++] = boffset
+ bcount
;
637 *next
= n_bits
- (boffset
+ bcount
);
648 windex
= elt
->index
+ LBITSET_ELT_WORDS
- 1;
649 boffset
= windex
* BITSET_WORD_BITS
;
653 *next
= n_bits
- (boffset
+ 1);
658 /* Find list of up to NUM bits set in BSET starting from and including
659 *NEXT and store in array LIST. Return with actual number of bits
660 found and with *NEXT indicating where search stopped. */
662 lbitset_list (bitset bset
, bitset_bindex
*list
,
663 bitset_bindex num
, bitset_bindex
*next
)
666 bitset_windex windex
;
672 head
= LBITSET_HEAD (bset
);
681 /* This is the most common case. */
683 /* Start with the first element. */
686 bitno
= windex
* BITSET_WORD_BITS
;
690 windex
= bitno
/ BITSET_WORD_BITS
;
692 /* Skip to starting element. */
694 elt
&& (elt
->index
+ LBITSET_ELT_WORDS
- 1) < windex
;
701 if (windex
< elt
->index
)
704 bitno
= windex
* BITSET_WORD_BITS
;
708 bitset_word
*srcp
= elt
->words
;
710 /* We are starting within an element. */
712 for (; (windex
- elt
->index
) < LBITSET_ELT_WORDS
; windex
++)
714 word
= srcp
[windex
- elt
->index
] >> (bitno
% BITSET_WORD_BITS
);
716 for (; word
; bitno
++)
720 list
[count
++] = bitno
;
729 bitno
= (windex
+ 1) * BITSET_WORD_BITS
;
736 bitno
= windex
* BITSET_WORD_BITS
;
742 /* If num is 1, we could speed things up with a binary search
743 of the word of interest. */
748 bitset_word
*srcp
= elt
->words
;
750 if ((count
+ LBITSET_ELT_BITS
) < num
)
752 /* The coast is clear, plant boot! */
754 #if LBITSET_ELT_WORDS == 2
758 if (!(word
& 0xffff))
768 for (; word
; bitno
++)
771 list
[count
++] = bitno
;
776 bitno
= windex
* BITSET_WORD_BITS
;
781 if (!(word
& 0xffff))
786 for (; word
; bitno
++)
789 list
[count
++] = bitno
;
794 bitno
= windex
* BITSET_WORD_BITS
;
796 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++)
801 if (!(word
& 0xffff))
811 for (; word
; bitno
++)
814 list
[count
++] = bitno
;
819 bitno
= windex
* BITSET_WORD_BITS
;
825 /* Tread more carefully since we need to check
826 if array overflows. */
828 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++)
830 for (word
= srcp
[i
]; word
; bitno
++)
834 list
[count
++] = bitno
;
844 bitno
= windex
* BITSET_WORD_BITS
;
852 bitno
= windex
* BITSET_WORD_BITS
;
862 lbitset_empty_p (bitset dst
)
867 for (elt
= LBITSET_HEAD (dst
); elt
; elt
= next
)
870 if (!lbitset_elt_zero_p (elt
))
873 lbitset_elt_unlink (dst
, elt
);
880 /* Ensure that any unused bits within the last element are clear. */
882 lbitset_unused_clear (bitset dst
)
884 unsigned int last_bit
;
885 bitset_bindex n_bits
;
887 n_bits
= BITSET_SIZE_ (dst
);
888 last_bit
= n_bits
% LBITSET_ELT_BITS
;
893 bitset_windex windex
;
896 elt
= LBITSET_TAIL (dst
);
898 windex
= n_bits
/ BITSET_WORD_BITS
;
900 srcp
[windex
- elt
->index
] &= ((bitset_word
) 1 << last_bit
) - 1;
903 for (; (windex
- elt
->index
) < LBITSET_ELT_WORDS
; windex
++)
904 srcp
[windex
- elt
->index
] = 0;
910 lbitset_ones (bitset dst
)
913 bitset_windex windex
;
916 /* This is a decidedly unfriendly operation for a linked list
917 bitset! It makes a sparse bitset become dense. An alternative
918 is to have a flag that indicates that the bitset stores the
919 complement of what it indicates. */
921 windex
= (BITSET_SIZE_ (dst
) + BITSET_WORD_BITS
- 1) / BITSET_WORD_BITS
;
923 for (i
= 0; i
< windex
; i
+= LBITSET_ELT_WORDS
)
925 /* Create new elements if they cannot be found. */
926 elt
= lbitset_elt_find (dst
, i
, LBITSET_CREATE
);
927 memset (elt
->words
, -1, sizeof (elt
->words
));
930 lbitset_unused_clear (dst
);
935 lbitset_not (bitset dst
, bitset src
)
942 bitset_windex windex
;
944 /* This is another unfriendly operation for a linked list
946 elt
= LBITSET_TAIL (dst
);
948 windex
= (BITSET_SIZE_ (dst
) + BITSET_WORD_BITS
- 1) / BITSET_WORD_BITS
;
950 for (i
= 0; i
< windex
; i
+= LBITSET_ELT_WORDS
)
952 /* Create new elements for dst if they cannot be found
953 or substitute zero elements if src elements not found. */
954 selt
= lbitset_elt_find (src
, i
, LBITSET_SUBST
);
955 delt
= lbitset_elt_find (dst
, i
, LBITSET_CREATE
);
957 for (j
= 0; j
< LBITSET_ELT_WORDS
; j
++)
958 delt
->words
[j
] = ~selt
->words
[j
];
960 lbitset_unused_clear (dst
);
966 /* Is DST == DST | SRC? */
968 lbitset_subset_p (bitset dst
, bitset src
)
974 for (selt
= LBITSET_HEAD (src
), delt
= LBITSET_HEAD (dst
);
975 selt
|| delt
; selt
= selt
->next
, delt
= delt
->next
)
978 selt
= &lbitset_zero_elts
[0];
980 delt
= &lbitset_zero_elts
[0];
981 else if (selt
->index
!= delt
->index
)
983 if (selt
->index
< delt
->index
)
985 lbitset_zero_elts
[2].next
= delt
;
986 delt
= &lbitset_zero_elts
[2];
990 lbitset_zero_elts
[1].next
= selt
;
991 selt
= &lbitset_zero_elts
[1];
995 for (j
= 0; j
< LBITSET_ELT_WORDS
; j
++)
996 if (delt
->words
[j
] != (selt
->words
[j
] | delt
->words
[j
]))
1003 /* Is DST & SRC == 0? */
1005 lbitset_disjoint_p (bitset dst
, bitset src
)
1011 for (selt
= LBITSET_HEAD (src
), delt
= LBITSET_HEAD (dst
);
1012 selt
&& delt
; selt
= selt
->next
, delt
= delt
->next
)
1014 if (selt
->index
!= delt
->index
)
1016 if (selt
->index
< delt
->index
)
1018 lbitset_zero_elts
[2].next
= delt
;
1019 delt
= &lbitset_zero_elts
[2];
1023 lbitset_zero_elts
[1].next
= selt
;
1024 selt
= &lbitset_zero_elts
[1];
1026 /* Since the elements are different, there is no
1027 intersection of these elements. */
1031 for (j
= 0; j
< LBITSET_ELT_WORDS
; j
++)
1032 if (selt
->words
[j
] & delt
->words
[j
])
1040 lbitset_op3_cmp (bitset dst
, bitset src1
, bitset src2
, enum bitset_ops op
)
1042 lbitset_elt
*selt1
= LBITSET_HEAD (src1
);
1043 lbitset_elt
*selt2
= LBITSET_HEAD (src2
);
1044 lbitset_elt
*delt
= LBITSET_HEAD (dst
);
1045 bitset_windex windex1
;
1046 bitset_windex windex2
;
1047 bitset_windex windex
;
1054 bool changed
= false;
1057 LBITSET_HEAD (dst
) = 0;
1060 windex1
= (selt1
) ? selt1
->index
: BITSET_WINDEX_MAX
;
1061 windex2
= (selt2
) ? selt2
->index
: BITSET_WINDEX_MAX
;
1063 while (selt1
|| selt2
)
1065 /* Figure out whether we need to substitute zero elements for
1067 if (windex1
== windex2
)
1072 selt1
= selt1
->next
;
1073 windex1
= (selt1
) ? selt1
->index
: BITSET_WINDEX_MAX
;
1074 selt2
= selt2
->next
;
1075 windex2
= (selt2
) ? selt2
->index
: BITSET_WINDEX_MAX
;
1077 else if (windex1
< windex2
)
1081 stmp2
= &lbitset_zero_elts
[0];
1082 selt1
= selt1
->next
;
1083 windex1
= (selt1
) ? selt1
->index
: BITSET_WINDEX_MAX
;
1088 stmp1
= &lbitset_zero_elts
[0];
1090 selt2
= selt2
->next
;
1091 windex2
= (selt2
) ? selt2
->index
: BITSET_WINDEX_MAX
;
1094 /* Find the appropriate element from DST. Begin by discarding
1095 elements that we've skipped. */
1096 while (delt
&& delt
->index
< windex
)
1101 lbitset_elt_free (dtmp
);
1103 if (delt
&& delt
->index
== windex
)
1109 dtmp
= lbitset_elt_calloc ();
1111 /* Do the operation, and if any bits are set, link it into the
1113 srcp1
= stmp1
->words
;
1114 srcp2
= stmp2
->words
;
1122 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++, dstp
++)
1124 bitset_word tmp
= *srcp1
++ | *srcp2
++;
1135 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++, dstp
++)
1137 bitset_word tmp
= *srcp1
++ & *srcp2
++;
1148 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++, dstp
++)
1150 bitset_word tmp
= *srcp1
++ ^ *srcp2
++;
1160 case BITSET_OP_ANDN
:
1161 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++, dstp
++)
1163 bitset_word tmp
= *srcp1
++ & ~(*srcp2
++);
1174 if (!lbitset_elt_zero_p (dtmp
))
1176 dtmp
->index
= windex
;
1177 /* Perhaps this could be optimised... */
1178 lbitset_elt_link (dst
, dtmp
);
1182 lbitset_elt_free (dtmp
);
1186 /* If we have elements of DST left over, free them all. */
1190 lbitset_prune (dst
, delt
);
1198 lbitset_and_cmp (bitset dst
, bitset src1
, bitset src2
)
1200 lbitset_elt
*selt1
= LBITSET_HEAD (src1
);
1201 lbitset_elt
*selt2
= LBITSET_HEAD (src2
);
1207 changed
= !LBITSET_HEAD (dst
);
1214 changed
= !LBITSET_HEAD (dst
);
1218 return lbitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_AND
);
1223 lbitset_and (bitset dst
, bitset src1
, bitset src2
)
1225 lbitset_and_cmp (dst
, src1
, src2
);
1230 lbitset_andn_cmp (bitset dst
, bitset src1
, bitset src2
)
1232 lbitset_elt
*selt1
= LBITSET_HEAD (src1
);
1233 lbitset_elt
*selt2
= LBITSET_HEAD (src2
);
1238 return lbitset_copy_cmp (dst
, src1
);
1243 changed
= !LBITSET_HEAD (dst
);
1247 return lbitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_ANDN
);
1252 lbitset_andn (bitset dst
, bitset src1
, bitset src2
)
1254 lbitset_andn_cmp (dst
, src1
, src2
);
1259 lbitset_or_cmp (bitset dst
, bitset src1
, bitset src2
)
1261 lbitset_elt
*selt1
= LBITSET_HEAD (src1
);
1262 lbitset_elt
*selt2
= LBITSET_HEAD (src2
);
1266 return lbitset_copy_cmp (dst
, src1
);
1270 return lbitset_copy_cmp (dst
, src2
);
1272 return lbitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_OR
);
1277 lbitset_or (bitset dst
, bitset src1
, bitset src2
)
1279 lbitset_or_cmp (dst
, src1
, src2
);
1284 lbitset_xor_cmp (bitset dst
, bitset src1
, bitset src2
)
1286 lbitset_elt
*selt1
= LBITSET_HEAD (src1
);
1287 lbitset_elt
*selt2
= LBITSET_HEAD (src2
);
1291 return lbitset_copy_cmp (dst
, src1
);
1295 return lbitset_copy_cmp (dst
, src2
);
1297 return lbitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_XOR
);
1302 lbitset_xor (bitset dst
, bitset src1
, bitset src2
)
1304 lbitset_xor_cmp (dst
, src1
, src2
);
1309 /* Vector of operations for linked-list bitsets. */
1310 struct bitset_vtable lbitset_vtable
= {
1337 bitset_andn_or_cmp_
,
1341 lbitset_list_reverse
,
1347 /* Return size of initial structure. */
1349 lbitset_bytes (bitset_bindex n_bits ATTRIBUTE_UNUSED
)
1351 return sizeof (struct lbitset_struct
);
1355 /* Initialize a bitset. */
1357 lbitset_init (bitset bset
, bitset_bindex n_bits ATTRIBUTE_UNUSED
)
1359 BITSET_NBITS_ (bset
) = n_bits
;
1360 bset
->b
.vtable
= &lbitset_vtable
;
1366 lbitset_release_memory (void)
1368 lbitset_free_list
= 0;
1369 if (lbitset_obstack_init
)
1371 lbitset_obstack_init
= false;
1372 obstack_free (&lbitset_obstack
, NULL
);
1377 /* Function to be called from debugger to debug lbitset. */
1379 debug_lbitset (bitset bset
)
1387 for (elt
= LBITSET_HEAD (bset
); elt
; elt
= elt
->next
)
1389 fprintf (stderr
, "Elt %lu\n", (unsigned long int) elt
->index
);
1390 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++)
1395 word
= elt
->words
[i
];
1397 fprintf (stderr
, " Word %u:", i
);
1398 for (j
= 0; j
< LBITSET_WORD_BITS
; j
++)
1399 if ((word
& ((bitset_word
) 1 << j
)))
1400 fprintf (stderr
, " %u", j
);
1401 fprintf (stderr
, "\n");