1 /* Functions to support link list 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.
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 #define LBITSET_ELT_WORDS 2
47 typedef bitset_word lbitset_word
;
49 #define LBITSET_WORD_BITS BITSET_WORD_BITS
51 /* Number of bits stored in each element. */
52 #define LBITSET_ELT_BITS \
53 ((unsigned) (LBITSET_ELT_WORDS * LBITSET_WORD_BITS))
55 /* Lbitset element. We use an array of bits for each element.
56 These are linked together in a doubly-linked list. */
57 typedef struct lbitset_elt_struct
59 struct lbitset_elt_struct
*next
; /* Next element. */
60 struct lbitset_elt_struct
*prev
; /* Previous element. */
61 bitset_windex index
; /* bitno / BITSET_WORD_BITS. */
62 bitset_word words
[LBITSET_ELT_WORDS
]; /* Bits that are set. */
67 enum lbitset_find_mode
68 { LBITSET_FIND
, LBITSET_CREATE
, LBITSET_SUBST
};
69 typedef int enum_lbitset_find_mode
;
71 static lbitset_elt lbitset_zero_elts
[3]; /* Elements of all zero bits. */
73 /* Obstack to allocate bitset elements from. */
74 static struct obstack lbitset_obstack
;
75 static int lbitset_obstack_init
= 0;
76 static lbitset_elt
*lbitset_free_list
; /* Free list of bitset elements. */
78 static lbitset_elt
*lbitset_elt_alloc
PARAMS ((void));
79 static lbitset_elt
*lbitset_elt_calloc
PARAMS ((void));
80 static void lbitset_elt_link
PARAMS ((bitset
, lbitset_elt
*));
81 static void lbitset_elt_unlink
PARAMS ((bitset
, lbitset_elt
*));
82 static void lbitset_elt_free
PARAMS ((lbitset_elt
*));
83 static lbitset_elt
*lbitset_elt_find
PARAMS ((bitset
, bitset_windex
,
84 enum_lbitset_find_mode
));
85 static int lbitset_elt_zero_p
PARAMS ((lbitset_elt
*));
87 static void lbitset_prune
PARAMS ((bitset
, lbitset_elt
*));
88 static void lbitset_weed
PARAMS ((bitset
));
89 static void lbitset_zero
PARAMS ((bitset
));
90 static int lbitset_equal_p
PARAMS ((bitset
, bitset
));
91 static void lbitset_copy
PARAMS ((bitset
, bitset
));
92 static int lbitset_copy_cmp
PARAMS ((bitset
, bitset
));
93 static void lbitset_set
PARAMS ((bitset
, bitset_bindex
));
94 static void lbitset_reset
PARAMS ((bitset
, bitset_bindex
));
95 static int lbitset_test
PARAMS ((bitset
, bitset_bindex
));
96 static bitset_bindex lbitset_size
PARAMS ((bitset
));
97 static int lbitset_op3_cmp
PARAMS ((bitset
, bitset
, bitset
, enum_bitset_ops
));
98 static void lbitset_and
PARAMS ((bitset
, bitset
, bitset
));
99 static int lbitset_and_cmp
PARAMS ((bitset
, bitset
, bitset
));
100 static void lbitset_andn
PARAMS ((bitset
, bitset
, bitset
));
101 static int lbitset_andn_cmp
PARAMS ((bitset
, bitset
, bitset
));
102 static void lbitset_or
PARAMS ((bitset
, bitset
, bitset
));
103 static int lbitset_or_cmp
PARAMS ((bitset
, bitset
, bitset
));
104 static void lbitset_xor
PARAMS ((bitset
, bitset
, bitset
));
105 static int lbitset_xor_cmp
PARAMS ((bitset
, bitset
, bitset
));
106 static bitset_bindex lbitset_list
PARAMS ((bitset
, bitset_bindex
*,
107 bitset_bindex
, bitset_bindex
*));
108 static bitset_bindex lbitset_list_reverse
109 PARAMS ((bitset
, bitset_bindex
*, bitset_bindex
, bitset_bindex
*));
110 static int lbitset_empty_p
PARAMS ((bitset
));
111 static void lbitset_ones
PARAMS ((bitset
));
112 static void lbitset_not
PARAMS ((bitset
, bitset
));
113 static int lbitset_subset_p
PARAMS ((bitset
, bitset
));
114 static int lbitset_disjoint_p
PARAMS ((bitset
, bitset
));
115 static void lbitset_free
PARAMS ((bitset
));
116 extern void debug_lbitset
PARAMS ((bitset
));
118 #define LBITSET_CURRENT1(X) \
119 ((lbitset_elt *) (void *) ((char *) (X) - offsetof (lbitset_elt, words)))
121 #define LBITSET_CURRENT(X) LBITSET_CURRENT1((X)->b.cdata)
123 #define LBITSET_HEAD(X) ((X)->l.head)
124 #define LBITSET_TAIL(X) ((X)->l.tail)
126 /* Allocate a lbitset element. The bits are not cleared. */
127 static inline lbitset_elt
*
132 if (lbitset_free_list
!= 0)
134 elt
= lbitset_free_list
;
135 lbitset_free_list
= elt
->next
;
139 if (!lbitset_obstack_init
)
141 lbitset_obstack_init
= 1;
143 /* Let particular systems override the size of a chunk. */
145 #ifndef OBSTACK_CHUNK_SIZE
146 #define OBSTACK_CHUNK_SIZE 0
149 /* Let them override the alloc and free routines too. */
151 #ifndef OBSTACK_CHUNK_ALLOC
152 #define OBSTACK_CHUNK_ALLOC xmalloc
155 #ifndef OBSTACK_CHUNK_FREE
156 #define OBSTACK_CHUNK_FREE free
159 #if !defined(__GNUC__) || (__GNUC__ < 2)
160 #define __alignof__(type) 0
163 obstack_specify_allocation (&lbitset_obstack
, OBSTACK_CHUNK_SIZE
,
164 __alignof__ (lbitset_elt
),
165 (void *(*)PARAMS ((long)))
167 (void (*)PARAMS ((void *)))
171 /* Perhaps we should add a number of new elements to the free
173 elt
= (lbitset_elt
*) obstack_alloc (&lbitset_obstack
,
174 sizeof (lbitset_elt
));
181 /* Allocate a lbitset element. The bits are cleared. */
182 static inline lbitset_elt
*
183 lbitset_elt_calloc ()
187 elt
= lbitset_elt_alloc ();
188 memset (elt
->words
, 0, sizeof (elt
->words
));
194 lbitset_elt_free (elt
)
197 elt
->next
= lbitset_free_list
;
198 lbitset_free_list
= elt
;
202 /* Unlink element ELT from bitset BSET. */
204 lbitset_elt_unlink (bset
, elt
)
208 lbitset_elt
*next
= elt
->next
;
209 lbitset_elt
*prev
= elt
->prev
;
217 if (LBITSET_HEAD (bset
) == elt
)
218 LBITSET_HEAD (bset
) = next
;
219 if (LBITSET_TAIL (bset
) == elt
)
220 LBITSET_TAIL (bset
) = prev
;
222 /* Update cache pointer. Since the first thing we try is to insert
223 before current, make current the next entry in preference to the
225 if (LBITSET_CURRENT (bset
) == elt
)
229 bset
->b
.cdata
= next
->words
;
230 bset
->b
.cindex
= next
->index
;
234 bset
->b
.cdata
= prev
->words
;
235 bset
->b
.cindex
= prev
->index
;
244 lbitset_elt_free (elt
);
248 /* Cut the chain of bitset BSET before element ELT and free the
251 lbitset_prune (bset
, elt
)
262 LBITSET_TAIL (bset
) = elt
->prev
;
263 bset
->b
.cdata
= elt
->prev
->words
;
264 bset
->b
.cindex
= elt
->prev
->index
;
269 LBITSET_HEAD (bset
) = 0;
270 LBITSET_TAIL (bset
) = 0;
275 for (; elt
; elt
= next
)
278 lbitset_elt_free (elt
);
283 /* Return nonzero if all bits in an element are zero. */
285 lbitset_elt_zero_p (elt
)
290 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++)
298 /* Link the bitset element into the current bitset linked list. */
300 lbitset_elt_link (bset
, elt
)
304 bitset_windex windex
= elt
->index
;
306 lbitset_elt
*current
;
309 current
= LBITSET_CURRENT (bset
);
311 current
= LBITSET_HEAD (bset
);
313 /* If this is the first and only element, add it in. */
314 if (LBITSET_HEAD (bset
) == 0)
316 elt
->next
= elt
->prev
= 0;
317 LBITSET_HEAD (bset
) = elt
;
318 LBITSET_TAIL (bset
) = elt
;
321 /* If this index is less than that of the current element, it goes
322 somewhere before the current element. */
323 else if (windex
< bset
->b
.cindex
)
326 ptr
->prev
&& ptr
->prev
->index
> windex
; ptr
= ptr
->prev
)
330 ptr
->prev
->next
= elt
;
332 LBITSET_HEAD (bset
) = elt
;
334 elt
->prev
= ptr
->prev
;
339 /* Otherwise, it must go somewhere after the current element. */
343 ptr
->next
&& ptr
->next
->index
< windex
; ptr
= ptr
->next
)
347 ptr
->next
->prev
= elt
;
349 LBITSET_TAIL (bset
) = elt
;
351 elt
->next
= ptr
->next
;
356 /* Set up so this is the first element searched. */
357 bset
->b
.cindex
= windex
;
358 bset
->b
.csize
= LBITSET_ELT_WORDS
;
359 bset
->b
.cdata
= elt
->words
;
364 lbitset_elt_find (bset
, windex
, mode
)
366 bitset_windex windex
;
367 enum_lbitset_find_mode mode
;
370 lbitset_elt
*current
;
374 current
= LBITSET_CURRENT (bset
);
375 /* Check if element is the cached element. */
376 if ((windex
- bset
->b
.cindex
) < bset
->b
.csize
)
381 current
= LBITSET_HEAD (bset
);
386 if (windex
< bset
->b
.cindex
)
389 elt
->prev
&& elt
->index
> windex
; elt
= elt
->prev
)
395 elt
->next
&& (elt
->index
+ LBITSET_ELT_WORDS
- 1) < windex
;
400 /* ELT is the nearest to the one we want. If it's not the one
401 we want, the one we want does not exist. */
402 if (elt
&& (windex
- elt
->index
) < LBITSET_ELT_WORDS
)
404 bset
->b
.cindex
= elt
->index
;
405 bset
->b
.csize
= LBITSET_ELT_WORDS
;
406 bset
->b
.cdata
= elt
->words
;
417 windex
-= windex
% LBITSET_ELT_WORDS
;
419 elt
= lbitset_elt_calloc ();
421 lbitset_elt_link (bset
, elt
);
425 return &lbitset_zero_elts
[0];
433 /* Weed out the zero elements from the list. */
441 for (elt
= LBITSET_HEAD (bset
); elt
; elt
= next
)
444 if (lbitset_elt_zero_p (elt
))
445 lbitset_elt_unlink (bset
, elt
);
450 /* Set all bits in the bitset to zero. */
457 head
= LBITSET_HEAD (bset
);
461 /* Clear a bitset by freeing the linked list at the head element. */
462 lbitset_prune (bset
, head
);
466 /* Return 1 if DST == SRC. */
468 lbitset_equal_p (dst
, src
)
481 for (selt
= LBITSET_HEAD (src
), delt
= LBITSET_HEAD (dst
);
482 selt
&& delt
; selt
= selt
->next
, delt
= delt
->next
)
484 if (selt
->index
!= delt
->index
)
487 for (j
= 0; j
< LBITSET_ELT_WORDS
; j
++)
488 if (delt
->words
[j
] != selt
->words
[j
])
491 return !selt
&& !delt
;
495 /* Copy bits from bitset SRC to bitset DST. */
497 lbitset_copy (dst
, src
)
511 head
= LBITSET_HEAD (src
);
516 for (elt
= head
; elt
; elt
= elt
->next
)
518 tmp
= lbitset_elt_alloc ();
519 tmp
->index
= elt
->index
;
525 LBITSET_HEAD (dst
) = tmp
;
528 memcpy (tmp
->words
, elt
->words
, sizeof (elt
->words
));
530 LBITSET_TAIL (dst
) = tmp
;
532 dst
->b
.csize
= LBITSET_ELT_WORDS
;
533 dst
->b
.cdata
= LBITSET_HEAD (dst
)->words
;
534 dst
->b
.cindex
= LBITSET_HEAD (dst
)->index
;
538 /* Copy bits from bitset SRC to bitset DST. Return non-zero if
539 bitsets different. */
541 lbitset_copy_cmp (dst
, src
)
548 if (!LBITSET_HEAD (dst
))
550 lbitset_copy (dst
, src
);
551 return LBITSET_HEAD (src
) != 0;
554 if (lbitset_equal_p (dst
, src
))
557 lbitset_copy (dst
, src
);
562 /* Return size in bits of bitset SRC. */
569 elt
= LBITSET_TAIL (src
);
573 /* Return current size of bitset in bits. */
574 return (elt
->index
+ LBITSET_ELT_WORDS
) * BITSET_WORD_BITS
;
578 /* Set bit BITNO in bitset DST. */
580 lbitset_set (dst
, bitno
)
584 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
586 lbitset_elt_find (dst
, windex
, LBITSET_CREATE
);
588 dst
->b
.cdata
[windex
- dst
->b
.cindex
] |=
589 (bitset_word
) 1 << (bitno
% BITSET_WORD_BITS
);
593 /* Reset bit BITNO in bitset DST. */
595 lbitset_reset (dst
, bitno
)
599 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
601 if (!lbitset_elt_find (dst
, windex
, LBITSET_FIND
))
604 dst
->b
.cdata
[windex
- dst
->b
.cindex
] &=
605 ~((bitset_word
) 1 << (bitno
% BITSET_WORD_BITS
));
607 /* If all the data is zero, perhaps we should unlink it now... */
611 /* Test bit BITNO in bitset SRC. */
613 lbitset_test (src
, bitno
)
617 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
619 if (!lbitset_elt_find (src
, windex
, LBITSET_FIND
))
622 return (src
->b
.cdata
[windex
- src
->b
.cindex
]
623 >> (bitno
% BITSET_WORD_BITS
)) & 1;
635 /* Find list of up to NUM bits set in BSET starting from and including
636 *NEXT and store in array LIST. Return with actual number of bits
637 found and with *NEXT indicating where search stopped. */
639 lbitset_list_reverse (bset
, list
, num
, next
)
645 bitset_bindex rbitno
;
648 bitset_bindex boffset
;
649 bitset_windex windex
;
653 bitset_bindex n_bits
;
655 elt
= LBITSET_TAIL (bset
);
659 n_bits
= (elt
->index
+ LBITSET_ELT_WORDS
) * BITSET_WORD_BITS
;
662 if (rbitno
>= n_bits
)
665 bitno
= n_bits
- (rbitno
+ 1);
667 windex
= bitno
/ BITSET_WORD_BITS
;
669 /* Skip back to starting element. */
670 for (; elt
&& elt
->index
> windex
; elt
= elt
->prev
)
676 if (windex
>= elt
->index
+ LBITSET_ELT_WORDS
)
678 /* We are trying to start in no-mans land so start
679 at end of current elt. */
680 bcount
= BITSET_WORD_BITS
- 1;
681 windex
= elt
->index
+ LBITSET_ELT_WORDS
- 1;
685 bcount
= bitno
% BITSET_WORD_BITS
;
689 boffset
= windex
* BITSET_WORD_BITS
;
691 /* If num is 1, we could speed things up with a binary search
692 of the word of interest. */
696 bitset_word
*srcp
= elt
->words
;
698 for (; (windex
- elt
->index
) < LBITSET_ELT_WORDS
;
699 windex
--, boffset
-= BITSET_WORD_BITS
,
700 bcount
= BITSET_WORD_BITS
- 1)
703 srcp
[windex
- elt
->index
] << (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
);
723 windex
= elt
->index
+ LBITSET_ELT_WORDS
- 1;
724 boffset
= windex
* BITSET_WORD_BITS
;
728 *next
= n_bits
- (boffset
+ 1);
733 /* Find list of up to NUM bits set in BSET starting from and including
734 *NEXT and store in array LIST. Return with actual number of bits
735 found and with *NEXT indicating where search stopped. */
737 lbitset_list (bset
, list
, num
, next
)
744 bitset_windex windex
;
750 head
= LBITSET_HEAD (bset
);
759 /* This is the most common case. */
761 /* Start with the first element. */
764 bitno
= windex
* BITSET_WORD_BITS
;
768 windex
= bitno
/ BITSET_WORD_BITS
;
770 /* Skip to starting element. */
772 elt
&& (elt
->index
+ LBITSET_ELT_WORDS
- 1) < windex
;
779 if (windex
< elt
->index
)
782 bitno
= windex
* BITSET_WORD_BITS
;
786 bitset_word
*srcp
= elt
->words
;
788 /* We are starting within an element. */
790 for (; (windex
- elt
->index
) < LBITSET_ELT_WORDS
; windex
++)
792 word
= srcp
[windex
- elt
->index
] >> (bitno
% BITSET_WORD_BITS
);
794 for (; word
; bitno
++)
798 list
[count
++] = bitno
;
807 bitno
= (windex
+ 1) * BITSET_WORD_BITS
;
814 bitno
= windex
* BITSET_WORD_BITS
;
820 /* If num is 1, we could speed things up with a binary search
821 of the word of interest. */
826 bitset_word
*srcp
= elt
->words
;
828 if ((count
+ LBITSET_ELT_BITS
) < num
)
830 /* The coast is clear, plant boot! */
832 #if LBITSET_ELT_WORDS == 2
836 if (!(word
& 0xffff))
846 for (; word
; bitno
++)
849 list
[count
++] = bitno
;
854 bitno
= windex
* BITSET_WORD_BITS
;
859 if (!(word
& 0xffff))
864 for (; word
; bitno
++)
867 list
[count
++] = bitno
;
872 bitno
= windex
* BITSET_WORD_BITS
;
874 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++)
879 if (!(word
& 0xffff))
889 for (; word
; bitno
++)
892 list
[count
++] = bitno
;
897 bitno
= windex
* BITSET_WORD_BITS
;
903 /* Tread more carefully since we need to check
904 if array overflows. */
906 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++)
908 for (word
= srcp
[i
]; word
; bitno
++)
912 list
[count
++] = bitno
;
922 bitno
= windex
* BITSET_WORD_BITS
;
930 bitno
= windex
* BITSET_WORD_BITS
;
940 lbitset_empty_p (dst
)
944 if (LBITSET_HEAD (dst
))
955 bitset_windex windex
;
958 /* This is a decidedly unfriendly operation for a linked list
959 bitset! It makes a sparse bitset become dense. An alternative
960 is to have a flag that indicates that the bitset stores the
961 complement of what it indicates. */
962 elt
= LBITSET_TAIL (dst
);
963 /* Ignore empty set. */
968 for (i
= 0; i
< windex
; i
+= LBITSET_ELT_WORDS
)
970 /* Create new elements if they cannot be found. */
971 elt
= lbitset_elt_find (dst
, i
, LBITSET_CREATE
);
972 memset (elt
->words
, -1, sizeof (elt
->words
));
978 lbitset_not (dst
, src
)
987 bitset_windex windex
;
989 /* This is another unfriendly operation for a linked list
991 elt
= LBITSET_TAIL (dst
);
992 /* Ignore empty set. */
997 for (i
= 0; i
< windex
; i
+= LBITSET_ELT_WORDS
)
999 /* Create new elements for dst if they cannot be found
1000 or substitute zero elements if src elements not found. */
1001 selt
= lbitset_elt_find (src
, i
, LBITSET_SUBST
);
1002 delt
= lbitset_elt_find (dst
, i
, LBITSET_CREATE
);
1004 for (j
= 0; j
< LBITSET_ELT_WORDS
; j
++)
1005 delt
->words
[j
] = ~selt
->words
[j
];
1012 /* Return 1 if DST == DST | SRC. */
1014 lbitset_subset_p (dst
, src
)
1022 for (selt
= LBITSET_HEAD (src
), delt
= LBITSET_HEAD (dst
);
1023 selt
|| delt
; selt
= selt
->next
, delt
= delt
->next
)
1026 selt
= &lbitset_zero_elts
[0];
1028 delt
= &lbitset_zero_elts
[0];
1029 else if (selt
->index
!= delt
->index
)
1031 if (selt
->index
< delt
->index
)
1033 lbitset_zero_elts
[2].next
= delt
;
1034 delt
= &lbitset_zero_elts
[2];
1038 lbitset_zero_elts
[1].next
= selt
;
1039 selt
= &lbitset_zero_elts
[1];
1043 for (j
= 0; j
< LBITSET_ELT_WORDS
; j
++)
1044 if (delt
->words
[j
] != (selt
->words
[j
] | delt
->words
[j
]))
1051 /* Return 1 if DST & SRC == 0. */
1053 lbitset_disjoint_p (dst
, src
)
1061 for (selt
= LBITSET_HEAD (src
), delt
= LBITSET_HEAD (dst
);
1062 selt
&& delt
; selt
= selt
->next
, delt
= delt
->next
)
1064 if (selt
->index
!= delt
->index
)
1066 if (selt
->index
< delt
->index
)
1068 lbitset_zero_elts
[2].next
= delt
;
1069 delt
= &lbitset_zero_elts
[2];
1073 lbitset_zero_elts
[1].next
= selt
;
1074 selt
= &lbitset_zero_elts
[1];
1076 /* Since the elements are different, there is no
1077 intersection of these elements. */
1081 for (j
= 0; j
< LBITSET_ELT_WORDS
; j
++)
1082 if (selt
->words
[j
] & delt
->words
[j
])
1090 lbitset_op3_cmp (dst
, src1
, src2
, op
)
1096 lbitset_elt
*selt1
= LBITSET_HEAD (src1
);
1097 lbitset_elt
*selt2
= LBITSET_HEAD (src2
);
1098 lbitset_elt
*delt
= LBITSET_HEAD (dst
);
1099 bitset_windex windex1
;
1100 bitset_windex windex2
;
1101 bitset_windex windex
;
1111 LBITSET_HEAD (dst
) = 0;
1114 windex1
= (selt1
) ? selt1
->index
: BITSET_WINDEX_MAX
;
1115 windex2
= (selt2
) ? selt2
->index
: BITSET_WINDEX_MAX
;
1117 while (selt1
|| selt2
)
1119 /* Figure out whether we need to substitute zero elements for
1121 if (windex1
== windex2
)
1126 selt1
= selt1
->next
;
1127 windex1
= (selt1
) ? selt1
->index
: BITSET_WINDEX_MAX
;
1128 selt2
= selt2
->next
;
1129 windex2
= (selt2
) ? selt2
->index
: BITSET_WINDEX_MAX
;
1131 else if (windex1
< windex2
)
1135 stmp2
= &lbitset_zero_elts
[0];
1136 selt1
= selt1
->next
;
1137 windex1
= (selt1
) ? selt1
->index
: BITSET_WINDEX_MAX
;
1142 stmp1
= &lbitset_zero_elts
[0];
1144 selt2
= selt2
->next
;
1145 windex2
= (selt2
) ? selt2
->index
: BITSET_WINDEX_MAX
;
1148 /* Find the appropriate element from DST. Begin by discarding
1149 elements that we've skipped. */
1150 while (delt
&& delt
->index
< windex
)
1155 lbitset_elt_free (dtmp
);
1157 if (delt
&& delt
->index
== windex
)
1163 dtmp
= lbitset_elt_calloc ();
1165 /* Do the operation, and if any bits are set, link it into the
1167 srcp1
= stmp1
->words
;
1168 srcp2
= stmp2
->words
;
1173 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++, dstp
++)
1175 bitset_word tmp
= *srcp1
++ | *srcp2
++;
1186 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++, dstp
++)
1188 bitset_word tmp
= *srcp1
++ & *srcp2
++;
1199 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++, dstp
++)
1201 bitset_word tmp
= *srcp1
++ ^ *srcp2
++;
1211 case BITSET_OP_ANDN
:
1212 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++, dstp
++)
1214 bitset_word tmp
= *srcp1
++ & ~(*srcp2
++);
1228 if (!lbitset_elt_zero_p (dtmp
))
1230 dtmp
->index
= windex
;
1231 /* Perhaps this could be optimised... */
1232 lbitset_elt_link (dst
, dtmp
);
1236 lbitset_elt_free (dtmp
);
1240 /* If we have elements of DST left over, free them all. */
1244 lbitset_prune (dst
, delt
);
1252 lbitset_and (dst
, src1
, src2
)
1257 lbitset_and_cmp (dst
, src1
, src2
);
1262 lbitset_and_cmp (dst
, src1
, src2
)
1267 lbitset_elt
*selt1
= LBITSET_HEAD (src1
);
1268 lbitset_elt
*selt2
= LBITSET_HEAD (src2
);
1274 changed
= !LBITSET_HEAD (dst
);
1281 changed
= !LBITSET_HEAD (dst
);
1285 return lbitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_AND
);
1290 lbitset_andn (dst
, src1
, src2
)
1295 lbitset_andn_cmp (dst
, src1
, src2
);
1300 lbitset_andn_cmp (dst
, src1
, src2
)
1305 lbitset_elt
*selt1
= LBITSET_HEAD (src1
);
1306 lbitset_elt
*selt2
= LBITSET_HEAD (src2
);
1311 return lbitset_copy_cmp (dst
, src1
);
1316 changed
= !LBITSET_HEAD (dst
);
1320 return lbitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_ANDN
);
1325 lbitset_or (dst
, src1
, src2
)
1330 lbitset_or_cmp (dst
, src1
, src2
);
1335 lbitset_or_cmp (dst
, src1
, src2
)
1340 lbitset_elt
*selt1
= LBITSET_HEAD (src1
);
1341 lbitset_elt
*selt2
= LBITSET_HEAD (src2
);
1345 return lbitset_copy_cmp (dst
, src1
);
1349 return lbitset_copy_cmp (dst
, src2
);
1351 return lbitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_OR
);
1356 lbitset_xor (dst
, src1
, src2
)
1361 lbitset_xor_cmp (dst
, src1
, src2
);
1366 lbitset_xor_cmp (dst
, src1
, src2
)
1371 lbitset_elt
*selt1
= LBITSET_HEAD (src1
);
1372 lbitset_elt
*selt2
= LBITSET_HEAD (src2
);
1376 return lbitset_copy_cmp (dst
, src1
);
1380 return lbitset_copy_cmp (dst
, src2
);
1382 return lbitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_XOR
);
1387 /* Vector of operations for linked-list bitsets. */
1388 struct bitset_vtable lbitset_vtable
= {
1414 bitset_andn_or_cmp_
,
1418 lbitset_list_reverse
,
1424 /* Return size of initial structure. */
1426 lbitset_bytes (n_bits
)
1427 bitset_bindex n_bits ATTRIBUTE_UNUSED
;
1429 return sizeof (struct lbitset_struct
);
1433 /* Initialize a bitset. */
1435 lbitset_init (bset
, n_bits
)
1437 bitset_bindex n_bits ATTRIBUTE_UNUSED
;
1439 bset
->b
.vtable
= &lbitset_vtable
;
1445 lbitset_release_memory ()
1447 lbitset_free_list
= 0;
1448 if (lbitset_obstack_init
)
1450 lbitset_obstack_init
= 0;
1451 obstack_free (&lbitset_obstack
, NULL
);
1456 /* Function to be called from debugger to debug lbitset. */
1458 debug_lbitset (bset
)
1467 for (elt
= LBITSET_HEAD (bset
); elt
; elt
= elt
->next
)
1469 fprintf (stderr
, "Elt %lu\n", (unsigned long) elt
->index
);
1470 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++)
1475 word
= elt
->words
[i
];
1477 fprintf (stderr
, " Word %u:", i
);
1478 for (j
= 0; j
< LBITSET_WORD_BITS
; j
++)
1479 if ((word
& ((bitset_word
) 1 << j
)))
1480 fprintf (stderr
, " %u", j
);
1481 fprintf (stderr
, "\n");