1cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Functions to support expandable bitsets.
205436638acc7c010349a69c3395f1a57c642dc62Ying Wang
305436638acc7c010349a69c3395f1a57c642dc62Ying Wang   Copyright (C) 2002-2006, 2009-2012 Free Software Foundation, Inc.
405436638acc7c010349a69c3395f1a57c642dc62Ying Wang
5cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
6cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
705436638acc7c010349a69c3395f1a57c642dc62Ying Wang   This program is free software: you can redistribute it and/or modify
8cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   it under the terms of the GNU General Public License as published by
905436638acc7c010349a69c3395f1a57c642dc62Ying Wang   the Free Software Foundation, either version 3 of the License, or
10cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   (at your option) any later version.
11cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
12cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   This program is distributed in the hope that it will be useful,
13cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   but WITHOUT ANY WARRANTY; without even the implied warranty of
14cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   GNU General Public License for more details.
16cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
17cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   You should have received a copy of the GNU General Public License
1805436638acc7c010349a69c3395f1a57c642dc62Ying Wang   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
2005436638acc7c010349a69c3395f1a57c642dc62Ying Wang#include <config.h>
21cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
22cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#include "ebitset.h"
2305436638acc7c010349a69c3395f1a57c642dc62Ying Wang
24cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#include "obstack.h"
25cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#include <stdlib.h>
26cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#include <string.h>
27cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
28cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* This file implements expandable bitsets.  These bitsets can be of
29cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   arbitrary length and are more efficient than arrays of bits for
30cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   large sparse sets.
31cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
32cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   Empty elements are represented by a NULL pointer in the table of
33cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   element pointers.  An alternative is to point to a special zero
34cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   element.  Similarly, we could represent an all 1's element with
35cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   another special ones element pointer.
36cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
37cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   Bitsets are commonly empty so we need to ensure that this special
38cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   case is fast.  A zero bitset is indicated when cdata is 0.  This is
39cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   conservative since cdata may be non zero and the bitset may still
40cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   be zero.
41cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
42cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   The bitset cache can be disabled either by setting cindex to
43cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   BITSET_WINDEX_MAX or by setting csize to 0.  Here
44cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   we use the former approach since cindex needs to be updated whenever
45cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   cdata is changed.
46cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project*/
47cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
48cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
49cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Number of words to use for each element.  */
50cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#define EBITSET_ELT_WORDS 2
51cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
52cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Number of bits stored in each element.  */
53cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#define EBITSET_ELT_BITS \
54cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ((unsigned int) (EBITSET_ELT_WORDS * BITSET_WORD_BITS))
55cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
56cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Ebitset element.  We use an array of bits.  */
57cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projecttypedef struct ebitset_elt_struct
58cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
59cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  union
60cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  {
61cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    bitset_word words[EBITSET_ELT_WORDS];	/* Bits that are set.  */
62cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    struct ebitset_elt_struct *next;
63cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  }
64cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  u;
65cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
66cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_elt;
67cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
68cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
69cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projecttypedef ebitset_elt *ebitset_elts;
70cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
71cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
72cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Number of elements to initially allocate.  */
73cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
74cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#ifndef EBITSET_INITIAL_SIZE
75cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#define EBITSET_INITIAL_SIZE 2
76cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#endif
77cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
78cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
79cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectenum ebitset_find_mode
80cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  { EBITSET_FIND, EBITSET_CREATE, EBITSET_SUBST };
81cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
82cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic ebitset_elt ebitset_zero_elts[1]; /* Elements of all zero bits.  */
83cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
84cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Obstack to allocate bitset elements from.  */
85cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic struct obstack ebitset_obstack;
86cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic bool ebitset_obstack_init = false;
87cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic ebitset_elt *ebitset_free_list;	/* Free list of bitset elements.  */
88cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
89cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#define EBITSET_N_ELTS(N) (((N) + EBITSET_ELT_BITS - 1) / EBITSET_ELT_BITS)
90cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#define EBITSET_ELTS(BSET) ((BSET)->e.elts)
91cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#define EBITSET_SIZE(BSET) EBITSET_N_ELTS (BITSET_NBITS_ (BSET))
92cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#define EBITSET_ASIZE(BSET) ((BSET)->e.size)
93cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
94cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#define EBITSET_NEXT(ELT) ((ELT)->u.next)
95cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#define EBITSET_WORDS(ELT) ((ELT)->u.words)
96cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
97cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Disable bitset cache and mark BSET as being zero.  */
98cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#define EBITSET_ZERO_SET(BSET) ((BSET)->b.cindex = BITSET_WINDEX_MAX, \
99cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	(BSET)->b.cdata = 0)
100cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
101cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#define EBITSET_CACHE_DISABLE(BSET)  ((BSET)->b.cindex = BITSET_WINDEX_MAX)
102cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
103cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Disable bitset cache and mark BSET as being possibly non-zero.  */
104cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#define EBITSET_NONZERO_SET(BSET) \
105cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project (EBITSET_CACHE_DISABLE (BSET), (BSET)->b.cdata = (bitset_word *)~0)
106cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
107cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* A conservative estimate of whether the bitset is zero.
108cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   This is non-zero only if we know for sure that the bitset is zero.  */
109cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#define EBITSET_ZERO_P(BSET) ((BSET)->b.cdata == 0)
110cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
111cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Enable cache to point to element with table index EINDEX.
112cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   The element must exist.  */
113cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#define EBITSET_CACHE_SET(BSET, EINDEX) \
114cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project ((BSET)->b.cindex = (EINDEX) * EBITSET_ELT_WORDS, \
115cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  (BSET)->b.cdata = EBITSET_WORDS (EBITSET_ELTS (BSET) [EINDEX]))
116cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
117cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#undef min
118cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#undef max
119cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#define min(a, b) ((a) > (b) ? (b) : (a))
120cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#define max(a, b) ((a) > (b) ? (a) : (b))
121cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
122cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic bitset_bindex
123cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_resize (bitset src, bitset_bindex n_bits)
124cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
125cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex oldsize;
126cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex newsize;
127cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
128cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (n_bits == BITSET_NBITS_ (src))
129cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    return n_bits;
130cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
131cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  oldsize = EBITSET_SIZE (src);
132cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  newsize = EBITSET_N_ELTS (n_bits);
133cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
134cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (oldsize < newsize)
135cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
136cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      bitset_windex size;
137cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
138cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      /* The bitset needs to grow.  If we already have enough memory
139cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	 allocated, then just zero what we need.  */
140cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      if (newsize > EBITSET_ASIZE (src))
141cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	{
142cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  /* We need to allocate more memory.  When oldsize is
143cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	     non-zero this means that we are changing the size, so
144cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	     grow the bitset 25% larger than requested to reduce
145cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	     number of reallocations.  */
146cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
147cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  if (oldsize == 0)
148cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    size = newsize;
149cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  else
150cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    size = newsize + newsize / 4;
151cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
152cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  EBITSET_ELTS (src)
153cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    = realloc (EBITSET_ELTS (src), size * sizeof (ebitset_elt *));
154cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  EBITSET_ASIZE (src) = size;
155cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	}
156cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
157cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      memset (EBITSET_ELTS (src) + oldsize, 0,
158cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      (newsize - oldsize) * sizeof (ebitset_elt *));
159cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
160cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  else
161cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
162cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      /* The bitset needs to shrink.  There's no point deallocating
163cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	 the memory unless it is shrinking by a reasonable amount.  */
164cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      if ((oldsize - newsize) >= oldsize / 2)
165cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	{
166cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  EBITSET_ELTS (src)
167cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    = realloc (EBITSET_ELTS (src), newsize * sizeof (ebitset_elt *));
168cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  EBITSET_ASIZE (src) = newsize;
169cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	}
170cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
171cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      /* Need to prune any excess bits.  FIXME.  */
172cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
173cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
174cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  BITSET_NBITS_ (src) = n_bits;
175cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  return n_bits;
176cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
177cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
178cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
179cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Allocate a ebitset element.  The bits are not cleared.  */
180cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic inline ebitset_elt *
181cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_elt_alloc (void)
182cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
183cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elt *elt;
184cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
185cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (ebitset_free_list != 0)
186cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
187cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      elt = ebitset_free_list;
188cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_free_list = EBITSET_NEXT (elt);
189cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
190cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  else
191cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
192cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      if (!ebitset_obstack_init)
193cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	{
194cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  ebitset_obstack_init = true;
195cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
196cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  /* Let particular systems override the size of a chunk.  */
197cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
198cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#ifndef OBSTACK_CHUNK_SIZE
199cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#define OBSTACK_CHUNK_SIZE 0
200cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#endif
201cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
202cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  /* Let them override the alloc and free routines too.  */
203cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
204cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#ifndef OBSTACK_CHUNK_ALLOC
205cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#define OBSTACK_CHUNK_ALLOC xmalloc
206cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#endif
207cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
208cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#ifndef OBSTACK_CHUNK_FREE
209cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#define OBSTACK_CHUNK_FREE free
210cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#endif
211cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
212cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#if ! defined __GNUC__ || __GNUC__ < 2
213cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#define __alignof__(type) 0
214cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#endif
215cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
216cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  obstack_specify_allocation (&ebitset_obstack, OBSTACK_CHUNK_SIZE,
217cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project				      __alignof__ (ebitset_elt),
218cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project				      OBSTACK_CHUNK_ALLOC,
219cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project				      OBSTACK_CHUNK_FREE);
220cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	}
221cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
222cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      /* Perhaps we should add a number of new elements to the free
223cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	 list.  */
224cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      elt = (ebitset_elt *) obstack_alloc (&ebitset_obstack,
225cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project					   sizeof (ebitset_elt));
226cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
227cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
228cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  return elt;
229cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
230cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
231cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
232cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Allocate a ebitset element.  The bits are cleared.  */
233cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic inline ebitset_elt *
234cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_elt_calloc (void)
235cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
236cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elt *elt;
237cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
238cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  elt = ebitset_elt_alloc ();
239cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  memset (EBITSET_WORDS (elt), 0, sizeof (EBITSET_WORDS (elt)));
240cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  return elt;
241cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
242cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
243cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
244cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic inline void
245cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_elt_free (ebitset_elt *elt)
246cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
247cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  EBITSET_NEXT (elt) = ebitset_free_list;
248cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_free_list = elt;
249cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
250cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
251cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
252cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Remove element with index EINDEX from bitset BSET.  */
253cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic inline void
254cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_elt_remove (bitset bset, bitset_windex eindex)
255cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
256cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elts *elts;
257cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elt *elt;
258cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
259cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  elts = EBITSET_ELTS (bset);
260cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
261cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  elt = elts[eindex];
262cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
263cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  elts[eindex] = 0;
264cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elt_free (elt);
265cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
266cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
267cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
268cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Add ELT into elts at index EINDEX of bitset BSET.  */
269cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic inline void
270cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_elt_add (bitset bset, ebitset_elt *elt, bitset_windex eindex)
271cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
272cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elts *elts;
273cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
274cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  elts = EBITSET_ELTS (bset);
275cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  /* Assume that the elts entry not allocated.  */
276cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  elts[eindex] = elt;
277cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
278cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
279cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
280cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Are all bits in an element zero?  */
281cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic inline bool
282cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_elt_zero_p (ebitset_elt *elt)
283cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
284cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  int i;
285cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
286cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  for (i = 0; i < EBITSET_ELT_WORDS; i++)
287cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    if (EBITSET_WORDS (elt)[i])
288cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      return false;
289cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
290cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  return true;
291cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
292cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
293cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
294cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic ebitset_elt *
295cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_elt_find (bitset bset, bitset_bindex bindex,
296cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  enum ebitset_find_mode mode)
297cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
298cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elt *elt;
299cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex size;
300cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex eindex;
301cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elts *elts;
302cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
303cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  eindex = bindex / EBITSET_ELT_BITS;
304cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
305cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  elts = EBITSET_ELTS (bset);
306cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  size = EBITSET_SIZE (bset);
307cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
308cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (eindex < size)
309cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
310cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      if ((elt = elts[eindex]))
311cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	{
312cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  if (EBITSET_WORDS (elt) == bset->b.cdata)
313cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    return elt;
314cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
315cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  EBITSET_CACHE_SET (bset, eindex);
316cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  return elt;
317cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	}
318cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
319cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
320cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  /* The element could not be found.  */
321cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
322cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  switch (mode)
323cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
324cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    default:
325cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      abort ();
326cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
327cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    case EBITSET_FIND:
328cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      return 0;
329cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
330cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    case EBITSET_CREATE:
331cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      if (eindex >= size)
332cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	ebitset_resize (bset, bindex);
333cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
334cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      /* Create a new element.  */
335cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      elt = ebitset_elt_calloc ();
336cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_elt_add (bset, elt, eindex);
337cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      EBITSET_CACHE_SET (bset, eindex);
338cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      return elt;
339cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
340cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    case EBITSET_SUBST:
341cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      return &ebitset_zero_elts[0];
342cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
343cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
344cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
345cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
346cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Weed out the zero elements from the elts.  */
347cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic inline bitset_windex
348cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_weed (bitset bset)
349cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
350cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elts *elts;
351cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex j;
352cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex count;
353cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
354cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (EBITSET_ZERO_P (bset))
355cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    return 0;
356cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
357cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  elts = EBITSET_ELTS (bset);
358cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  count = 0;
359cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  for (j = 0; j < EBITSET_SIZE (bset); j++)
360cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
361cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_elt *elt = elts[j];
362cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
363cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      if (elt)
364cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	{
365cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  if (ebitset_elt_zero_p (elt))
366cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    {
367cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      ebitset_elt_remove (bset, j);
368cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      count++;
369cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    }
370cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	}
371cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      else
372cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	count++;
373cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
374cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
375cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  count = j - count;
376cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (!count)
377cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
378cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      /* All the bits are zero.  We could shrink the elts.
379cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	 For now just mark BSET as known to be zero.  */
380cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      EBITSET_ZERO_SET (bset);
381cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
382cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  else
383cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    EBITSET_NONZERO_SET (bset);
384cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
385cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  return count;
386cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
387cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
388cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
389cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Set all bits in the bitset to zero.  */
390cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic inline void
391cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_zero (bitset bset)
392cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
393cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elts *elts;
394cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex j;
395cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
396cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (EBITSET_ZERO_P (bset))
397cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    return;
398cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
399cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  elts = EBITSET_ELTS (bset);
400cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  for (j = 0; j < EBITSET_SIZE (bset); j++)
401cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
402cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_elt *elt = elts[j];
403cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
404cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      if (elt)
405cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	ebitset_elt_remove (bset, j);
406cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
407cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
408cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  /* All the bits are zero.  We could shrink the elts.
409cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project     For now just mark BSET as known to be zero.  */
410cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  EBITSET_ZERO_SET (bset);
411cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
412cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
413cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
414cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic inline bool
415cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_equal_p (bitset dst, bitset src)
416cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
417cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elts *selts;
418cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elts *delts;
419cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex j;
420cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
421cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (src == dst)
422cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    return true;
423cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
424cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_weed (dst);
425cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_weed (src);
426cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
427cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (EBITSET_SIZE (src) != EBITSET_SIZE (dst))
428cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    return false;
429cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
430cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  selts = EBITSET_ELTS (src);
431cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  delts = EBITSET_ELTS (dst);
432cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
433cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  for (j = 0; j < EBITSET_SIZE (src); j++)
434cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
435cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      unsigned int i;
436cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_elt *selt = selts[j];
437cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_elt *delt = delts[j];
438cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
439cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      if (!selt && !delt)
440cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	continue;
441cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      if ((selt && !delt) || (!selt && delt))
442cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	return false;
443cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
444cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      for (i = 0; i < EBITSET_ELT_WORDS; i++)
445cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	if (EBITSET_WORDS (selt)[i] != EBITSET_WORDS (delt)[i])
446cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  return false;
447cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
448cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  return true;
449cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
450cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
451cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
452cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Copy bits from bitset SRC to bitset DST.  */
453cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic inline void
454cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_copy_ (bitset dst, bitset src)
455cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
456cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elts *selts;
457cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elts *delts;
458cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex j;
459cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
460cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (src == dst)
461cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    return;
462cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
463cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_zero (dst);
464cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
465cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (BITSET_NBITS_ (dst) != BITSET_NBITS_ (src))
466cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    ebitset_resize (dst, BITSET_NBITS_ (src));
467cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
468cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  selts = EBITSET_ELTS (src);
469cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  delts = EBITSET_ELTS (dst);
470cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  for (j = 0; j < EBITSET_SIZE (src); j++)
471cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
472cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_elt *selt = selts[j];
473cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
474cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      if (selt)
475cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	{
476cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  ebitset_elt *tmp;
477cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
478cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  tmp = ebitset_elt_alloc ();
479cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  delts[j] = tmp;
480cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  memcpy (EBITSET_WORDS (tmp), EBITSET_WORDS (selt),
481cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  sizeof (EBITSET_WORDS (selt)));
482cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	}
483cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
484cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  EBITSET_NONZERO_SET (dst);
485cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
486cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
487cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
488cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Copy bits from bitset SRC to bitset DST.  Return true if
489cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   bitsets different.  */
490cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic inline bool
491cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_copy_cmp (bitset dst, bitset src)
492cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
493cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (src == dst)
494cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    return false;
495cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
496cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (EBITSET_ZERO_P (dst))
497cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
498cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_copy_ (dst, src);
499cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      return !EBITSET_ZERO_P (src);
500cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
501cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
502cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (ebitset_equal_p (dst, src))
503cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    return false;
504cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
505cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_copy_ (dst, src);
506cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  return true;
507cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
508cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
509cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
510cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Set bit BITNO in bitset DST.  */
511cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic void
512cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_set (bitset dst, bitset_bindex bitno)
513cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
514cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex windex = bitno / BITSET_WORD_BITS;
515cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
516cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elt_find (dst, bitno, EBITSET_CREATE);
517cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
518cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  dst->b.cdata[windex - dst->b.cindex] |=
519cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    (bitset_word) 1 << (bitno % BITSET_WORD_BITS);
520cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
521cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
522cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
523cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Reset bit BITNO in bitset DST.  */
524cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic void
525cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_reset (bitset dst, bitset_bindex bitno)
526cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
527cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex windex = bitno / BITSET_WORD_BITS;
528cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
529cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (!ebitset_elt_find (dst, bitno, EBITSET_FIND))
530cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    return;
531cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
532cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  dst->b.cdata[windex - dst->b.cindex] &=
533cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    ~((bitset_word) 1 << (bitno % BITSET_WORD_BITS));
534cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
535cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  /* If all the data is zero, perhaps we should remove it now...
536cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project     However, there is a good chance that the element will be needed
537cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project     again soon.  */
538cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
539cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
540cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
541cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Test bit BITNO in bitset SRC.  */
542cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic bool
543cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_test (bitset src, bitset_bindex bitno)
544cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
545cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex windex = bitno / BITSET_WORD_BITS;
546cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
547cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  return (ebitset_elt_find (src, bitno, EBITSET_FIND)
548cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  && ((src->b.cdata[windex - src->b.cindex]
549cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	       >> (bitno % BITSET_WORD_BITS))
550cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      & 1));
551cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
552cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
553cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
554cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic void
555cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_free (bitset bset)
556cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
557cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_zero (bset);
558cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  free (EBITSET_ELTS (bset));
559cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
560cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
561cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
562cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Find list of up to NUM bits set in BSET starting from and including
563cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project *NEXT and store in array LIST.  Return with actual number of bits
564cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project found and with *NEXT indicating where search stopped.  */
565cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic bitset_bindex
566cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_list_reverse (bitset bset, bitset_bindex *list,
567cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		      bitset_bindex num, bitset_bindex *next)
568cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
569cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_bindex n_bits;
570cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_bindex bitno;
571cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_bindex rbitno;
572cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  unsigned int bcount;
573cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_bindex boffset;
574cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex windex;
575cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex eindex;
576cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex woffset;
577cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_bindex count;
578cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex size;
579cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elts *elts;
580cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
581cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (EBITSET_ZERO_P (bset))
582cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    return 0;
583cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
584cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  size = EBITSET_SIZE (bset);
585cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  n_bits = size * EBITSET_ELT_BITS;
586cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  rbitno = *next;
587cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
588cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (rbitno >= n_bits)
589cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    return 0;
590cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
591cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  elts = EBITSET_ELTS (bset);
592cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
593cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitno = n_bits - (rbitno + 1);
594cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
595cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  windex = bitno / BITSET_WORD_BITS;
596cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  eindex = bitno / EBITSET_ELT_BITS;
597cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  woffset = windex - eindex * EBITSET_ELT_WORDS;
598cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
599cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  /* If num is 1, we could speed things up with a binary search
600cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project     of the word of interest.  */
601cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
602cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  count = 0;
603cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bcount = bitno % BITSET_WORD_BITS;
604cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  boffset = windex * BITSET_WORD_BITS;
605cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
606cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  do
607cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
608cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_elt *elt;
609cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      bitset_word *srcp;
610cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
611cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      elt = elts[eindex];
612cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      if (elt)
613cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	{
614cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  srcp = EBITSET_WORDS (elt);
615cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
616cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  do
617cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    {
618cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      bitset_word word;
619cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
620cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      word = srcp[woffset] << (BITSET_WORD_BITS - 1 - bcount);
621cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
622cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      for (; word; bcount--)
623cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		{
624cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  if (word & BITSET_MSB)
625cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		    {
626cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		      list[count++] = boffset + bcount;
627cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		      if (count >= num)
628cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project			{
629cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project			  *next = n_bits - (boffset + bcount);
630cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project			  return count;
631cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project			}
632cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		    }
633cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  word <<= 1;
634cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		}
635cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      boffset -= BITSET_WORD_BITS;
636cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      bcount = BITSET_WORD_BITS - 1;
637cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    }
638cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  while (woffset--);
639cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	}
640cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
641cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      woffset = EBITSET_ELT_WORDS - 1;
642cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      boffset = eindex * EBITSET_ELT_BITS - BITSET_WORD_BITS;
643cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
644cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  while (eindex--);
645cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
646cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  *next = n_bits - (boffset + 1);
647cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  return count;
648cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
649cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
650cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
651cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Find list of up to NUM bits set in BSET starting from and including
652cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project *NEXT and store in array LIST.  Return with actual number of bits
653cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project found and with *NEXT indicating where search stopped.  */
654cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic bitset_bindex
655cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_list (bitset bset, bitset_bindex *list,
656cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      bitset_bindex num, bitset_bindex *next)
657cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
658cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_bindex bitno;
659cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex windex;
660cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex eindex;
661cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_bindex count;
662cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex size;
663cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elt *elt;
664cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_word word;
665cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elts *elts;
666cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
667cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (EBITSET_ZERO_P (bset))
668cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    return 0;
669cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
670cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitno = *next;
671cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  count = 0;
672cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
673cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  elts = EBITSET_ELTS (bset);
674cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  size = EBITSET_SIZE (bset);
675cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  eindex = bitno / EBITSET_ELT_BITS;
676cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
677cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (bitno % EBITSET_ELT_BITS)
678cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
679cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      /* We need to start within an element.  This is not very common.  */
680cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
681cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      elt = elts[eindex];
682cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      if (elt)
683cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	{
684cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  bitset_windex woffset;
685cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  bitset_word *srcp = EBITSET_WORDS (elt);
686cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
687cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  windex = bitno / BITSET_WORD_BITS;
688cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  woffset = eindex * EBITSET_ELT_WORDS;
689cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
690cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  for (; (windex - woffset) < EBITSET_ELT_WORDS; windex++)
691cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    {
692cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      word = srcp[windex - woffset] >> (bitno % BITSET_WORD_BITS);
693cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
694cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      for (; word; bitno++)
695cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		{
696cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  if (word & 1)
697cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		    {
698cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		      list[count++] = bitno;
699cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		      if (count >= num)
700cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project			{
701cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project			  *next = bitno + 1;
702cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project			  return count;
703cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project			}
704cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		    }
705cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  word >>= 1;
706cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		}
707cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      bitno = (windex + 1) * BITSET_WORD_BITS;
708cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    }
709cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	}
710cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
711cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      /* Skip to next element.  */
712cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      eindex++;
713cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
714cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
715cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  /* If num is 1, we could speed things up with a binary search
716cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project     of the word of interest.  */
717cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
718cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  for (; eindex < size; eindex++)
719cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
720cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      int i;
721cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      bitset_word *srcp;
722cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
723cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      elt = elts[eindex];
724cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      if (!elt)
725cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	continue;
726cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
727cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      srcp = EBITSET_WORDS (elt);
728cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      windex = eindex * EBITSET_ELT_WORDS;
729cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
730cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      if ((count + EBITSET_ELT_BITS) < num)
731cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	{
732cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  /* The coast is clear, plant boot!  */
733cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
734cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#if EBITSET_ELT_WORDS == 2
735cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  word = srcp[0];
736cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  if (word)
737cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    {
738cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      if (!(word & 0xffff))
739cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		{
740cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  word >>= 16;
741cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  bitno += 16;
742cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		}
743cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      if (!(word & 0xff))
744cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		{
745cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  word >>= 8;
746cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  bitno += 8;
747cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		}
748cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      for (; word; bitno++)
749cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		{
750cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  if (word & 1)
751cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		    list[count++] = bitno;
752cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  word >>= 1;
753cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		}
754cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    }
755cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  windex++;
756cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  bitno = windex * BITSET_WORD_BITS;
757cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
758cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  word = srcp[1];
759cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  if (word)
760cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    {
761cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      if (!(word & 0xffff))
762cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		{
763cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  word >>= 16;
764cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  bitno += 16;
765cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		}
766cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      for (; word; bitno++)
767cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		{
768cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  if (word & 1)
769cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		    list[count++] = bitno;
770cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  word >>= 1;
771cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		}
772cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    }
773cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  windex++;
774cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  bitno = windex * BITSET_WORD_BITS;
775cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#else
776cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  for (i = 0; i < EBITSET_ELT_WORDS; i++, windex++)
777cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    {
778cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      bitno = windex * BITSET_WORD_BITS;
779cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
780cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      word = srcp[i];
781cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      if (word)
782cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		{
783cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  if (!(word & 0xffff))
784cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		    {
785cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		      word >>= 16;
786cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		      bitno += 16;
787cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		    }
788cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  if (!(word & 0xff))
789cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		    {
790cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		      word >>= 8;
791cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		      bitno += 8;
792cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		    }
793cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  for (; word; bitno++)
794cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		    {
795cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		      if (word & 1)
796cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project			list[count++] = bitno;
797cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		      word >>= 1;
798cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		    }
799cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		}
800cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    }
801cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#endif
802cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	}
803cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      else
804cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	{
805cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  /* Tread more carefully since we need to check
806cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	     if array overflows.  */
807cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
808cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  for (i = 0; i < EBITSET_ELT_WORDS; i++, windex++)
809cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    {
810cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      bitno = windex * BITSET_WORD_BITS;
811cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
812cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      for (word = srcp[i]; word; bitno++)
813cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		{
814cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  if (word & 1)
815cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		    {
816cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		      list[count++] = bitno;
817cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		      if (count >= num)
818cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project			{
819cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project			  *next = bitno + 1;
820cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project			  return count;
821cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project			}
822cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		    }
823cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  word >>= 1;
824cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		}
825cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    }
826cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	}
827cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
828cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
829cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  *next = bitno;
830cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  return count;
831cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
832cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
833cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
834cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Ensure that any unused bits within the last element are clear.  */
835cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic inline void
836cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_unused_clear (bitset dst)
837cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
838cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  unsigned int last_bit;
839cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_bindex n_bits;
840cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
841cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  n_bits = BITSET_NBITS_ (dst);
842cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  last_bit = n_bits % EBITSET_ELT_BITS;
843cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
844cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (last_bit)
845cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
846cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      bitset_windex eindex;
847cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_elts *elts;
848cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_elt *elt;
849cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
850cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      elts = EBITSET_ELTS (dst);
851cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
852cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      eindex = n_bits / EBITSET_ELT_BITS;
853cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
854cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      elt = elts[eindex];
855cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      if (elt)
856cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	{
857cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  bitset_windex windex;
858cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  bitset_windex woffset;
859cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  bitset_word *srcp = EBITSET_WORDS (elt);
860cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
861cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  windex = n_bits / BITSET_WORD_BITS;
862cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  woffset = eindex * EBITSET_ELT_WORDS;
863cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
864cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  srcp[windex - woffset] &= ((bitset_word) 1 << last_bit) - 1;
865cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  windex++;
866cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  for (; (windex - woffset) < EBITSET_ELT_WORDS; windex++)
867cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    srcp[windex - woffset] = 0;
868cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	}
869cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
870cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
871cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
872cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
873cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic void
874cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_ones (bitset dst)
875cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
876cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex j;
877cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elt *elt;
878cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
879cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  for (j = 0; j < EBITSET_SIZE (dst); j++)
880cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
881cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      /* Create new elements if they cannot be found.  Perhaps
882cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	 we should just add pointers to a ones element?  */
883cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      elt =
884cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	ebitset_elt_find (dst, j * EBITSET_ELT_BITS, EBITSET_CREATE);
885cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      memset (EBITSET_WORDS (elt), -1, sizeof (EBITSET_WORDS (elt)));
886cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
887cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  EBITSET_NONZERO_SET (dst);
888cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_unused_clear (dst);
889cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
890cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
891cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
892cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic bool
893cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_empty_p (bitset dst)
894cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
895cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elts *elts;
896cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex j;
897cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
898cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (EBITSET_ZERO_P (dst))
899cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    return 1;
900cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
901cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  elts = EBITSET_ELTS (dst);
902cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  for (j = 0; j < EBITSET_SIZE (dst); j++)
903cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
904cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_elt *elt = elts[j];
905cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
906cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      if (elt)
907cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	{
908cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  if (!ebitset_elt_zero_p (elt))
909cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    return 0;
910cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  /* Do some weeding as we go.  */
911cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  ebitset_elt_remove (dst, j);
912cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	}
913cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
914cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
915cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  /* All the bits are zero.  We could shrink the elts.
916cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project     For now just mark DST as known to be zero.  */
917cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  EBITSET_ZERO_SET (dst);
918cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  return 1;
919cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
920cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
921cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
922cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic void
923cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_not (bitset dst, bitset src)
924cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
925cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  unsigned int i;
926cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elt *selt;
927cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elt *delt;
928cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex j;
929cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
930cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_resize (dst, BITSET_NBITS_ (src));
931cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
932cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  for (j = 0; j < EBITSET_SIZE (src); j++)
933cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
934cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      /* Create new elements for dst if they cannot be found
935cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	 or substitute zero elements if src elements not found.  */
936cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      selt =
937cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	ebitset_elt_find (dst, j * EBITSET_ELT_BITS, EBITSET_SUBST);
938cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      delt =
939cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	ebitset_elt_find (dst, j * EBITSET_ELT_BITS, EBITSET_CREATE);
940cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
941cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      for (i = 0; i < EBITSET_ELT_WORDS; i++)
942cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	EBITSET_WORDS (delt)[i] = ~EBITSET_WORDS (selt)[i];
943cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
944cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  EBITSET_NONZERO_SET (dst);
945cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_unused_clear (dst);
946cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
947cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
948cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
949cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Is DST == DST | SRC?  */
950cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic bool
951cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_subset_p (bitset dst, bitset src)
952cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
953cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex j;
954cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elts *selts;
955cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elts *delts;
956cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex ssize;
957cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex dsize;
958cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
959cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  selts = EBITSET_ELTS (src);
960cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  delts = EBITSET_ELTS (dst);
961cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
962cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ssize = EBITSET_SIZE (src);
963cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  dsize = EBITSET_SIZE (dst);
964cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
965cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  for (j = 0; j < ssize; j++)
966cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
967cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      unsigned int i;
968cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_elt *selt;
969cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_elt *delt;
970cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
971cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      selt = j < ssize ? selts[j] : 0;
972cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      delt = j < dsize ? delts[j] : 0;
973cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
974cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      if (!selt && !delt)
975cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	continue;
976cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
977cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      if (!selt)
978cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	selt = &ebitset_zero_elts[0];
979cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      if (!delt)
980cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	delt = &ebitset_zero_elts[0];
981cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
982cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      for (i = 0; i < EBITSET_ELT_WORDS; i++)
983cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	if (EBITSET_WORDS (delt)[i]
984cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    != (EBITSET_WORDS (selt)[i] | EBITSET_WORDS (delt)[i]))
985cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  return false;
986cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
987cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  return true;
988cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
989cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
990cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
991cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Is DST & SRC == 0?  */
992cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic bool
993cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_disjoint_p (bitset dst, bitset src)
994cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
995cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex j;
996cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elts *selts;
997cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elts *delts;
998cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex ssize;
999cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex dsize;
1000cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1001cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  selts = EBITSET_ELTS (src);
1002cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  delts = EBITSET_ELTS (dst);
1003cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1004cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ssize = EBITSET_SIZE (src);
1005cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  dsize = EBITSET_SIZE (dst);
1006cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1007cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  for (j = 0; j < ssize; j++)
1008cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
1009cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      unsigned int i;
1010cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_elt *selt;
1011cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_elt *delt;
1012cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1013cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      selt = j < ssize ? selts[j] : 0;
1014cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      delt = j < dsize ? delts[j] : 0;
1015cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1016cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      if (!selt || !delt)
1017cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	continue;
1018cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1019cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      for (i = 0; i < EBITSET_ELT_WORDS; i++)
1020cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	if ((EBITSET_WORDS (selt)[i] & EBITSET_WORDS (delt)[i]))
1021cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  return false;
1022cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
1023cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  return true;
1024cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
1025cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1026cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1027cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1028cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic bool
1029cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_op3_cmp (bitset dst, bitset src1, bitset src2, enum bitset_ops op)
1030cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
1031cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex ssize1;
1032cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex ssize2;
1033cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex dsize;
1034cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex size;
1035cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elts *selts1;
1036cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elts *selts2;
1037cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_elts *delts;
1038cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_word *srcp1;
1039cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_word *srcp2;
1040cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_word *dstp;
1041cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bool changed = false;
1042cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  unsigned int i;
1043cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_windex j;
1044cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1045cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_resize (dst, max (BITSET_NBITS_ (src1), BITSET_NBITS_ (src2)));
1046cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1047cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ssize1 = EBITSET_SIZE (src1);
1048cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ssize2 = EBITSET_SIZE (src2);
1049cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  dsize = EBITSET_SIZE (dst);
1050cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  size = ssize1;
1051cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (size < ssize2)
1052cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    size = ssize2;
1053cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1054cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  selts1 = EBITSET_ELTS (src1);
1055cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  selts2 = EBITSET_ELTS (src2);
1056cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  delts = EBITSET_ELTS (dst);
1057cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1058cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  for (j = 0; j < size; j++)
1059cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
1060cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_elt *selt1;
1061cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_elt *selt2;
1062cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_elt *delt;
1063cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1064cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      selt1 = j < ssize1 ? selts1[j] : 0;
1065cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      selt2 = j < ssize2 ? selts2[j] : 0;
1066cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      delt = j < dsize ? delts[j] : 0;
1067cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1068cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      if (!selt1 && !selt2)
1069cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	{
1070cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  if (delt)
1071cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    {
1072cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      changed = true;
1073cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      ebitset_elt_remove (dst, j);
1074cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    }
1075cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  continue;
1076cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	}
1077cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1078cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      if (!selt1)
1079cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	selt1 = &ebitset_zero_elts[0];
1080cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      if (!selt2)
1081cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	selt2 = &ebitset_zero_elts[0];
1082cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      if (!delt)
1083cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	delt = ebitset_elt_calloc ();
1084cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      else
1085cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	delts[j] = 0;
1086cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1087cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      srcp1 = EBITSET_WORDS (selt1);
1088cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      srcp2 = EBITSET_WORDS (selt2);
1089cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      dstp = EBITSET_WORDS (delt);
1090cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      switch (op)
1091cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	{
1092cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	default:
1093cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  abort ();
1094cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1095cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	case BITSET_OP_OR:
1096cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  for (i = 0; i < EBITSET_ELT_WORDS; i++, dstp++)
1097cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    {
1098cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      bitset_word tmp = *srcp1++ | *srcp2++;
1099cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1100cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      if (*dstp != tmp)
1101cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		{
1102cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  changed = true;
1103cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  *dstp = tmp;
1104cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		}
1105cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    }
1106cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  break;
1107cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1108cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	case BITSET_OP_AND:
1109cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  for (i = 0; i < EBITSET_ELT_WORDS; i++, dstp++)
1110cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    {
1111cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      bitset_word tmp = *srcp1++ & *srcp2++;
1112cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1113cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      if (*dstp != tmp)
1114cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		{
1115cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  changed = true;
1116cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  *dstp = tmp;
1117cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		}
1118cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    }
1119cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  break;
1120cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1121cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	case BITSET_OP_XOR:
1122cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  for (i = 0; i < EBITSET_ELT_WORDS; i++, dstp++)
1123cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    {
1124cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      bitset_word tmp = *srcp1++ ^ *srcp2++;
1125cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1126cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      if (*dstp != tmp)
1127cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		{
1128cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  changed = true;
1129cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  *dstp = tmp;
1130cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		}
1131cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    }
1132cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  break;
1133cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1134cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	case BITSET_OP_ANDN:
1135cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  for (i = 0; i < EBITSET_ELT_WORDS; i++, dstp++)
1136cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    {
1137cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      bitset_word tmp = *srcp1++ & ~(*srcp2++);
1138cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1139cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	      if (*dstp != tmp)
1140cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		{
1141cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  changed = true;
1142cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		  *dstp = tmp;
1143cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project		}
1144cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	    }
1145cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  break;
1146cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	}
1147cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1148cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      if (!ebitset_elt_zero_p (delt))
1149cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	{
1150cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  ebitset_elt_add (dst, delt, j);
1151cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	}
1152cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      else
1153cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	{
1154cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	  ebitset_elt_free (delt);
1155cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	}
1156cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
1157cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1158cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  /* If we have elements of DST left over, free them all.  */
1159cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  for (; j < dsize; j++)
1160cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
1161cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_elt *delt;
1162cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1163cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      changed = true;
1164cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1165cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      delt = delts[j];
1166cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1167cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      if (delt)
1168cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project	ebitset_elt_remove (dst, j);
1169cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
1170cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1171cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  EBITSET_NONZERO_SET (dst);
1172cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  return changed;
1173cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
1174cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1175cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1176cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic bool
1177cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_and_cmp (bitset dst, bitset src1, bitset src2)
1178cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
1179cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bool changed;
1180cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1181cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (EBITSET_ZERO_P (src2))
1182cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
1183cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_weed (dst);
1184cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      changed = EBITSET_ZERO_P (dst);
1185cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_zero (dst);
1186cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      return changed;
1187cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
1188cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  else if (EBITSET_ZERO_P (src1))
1189cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
1190cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_weed (dst);
1191cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      changed = EBITSET_ZERO_P (dst);
1192cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_zero (dst);
1193cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      return changed;
1194cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
1195cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  return ebitset_op3_cmp (dst, src1, src2, BITSET_OP_AND);
1196cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
1197cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1198cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1199cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic void
1200cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_and (bitset dst, bitset src1, bitset src2)
1201cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
1202cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_and_cmp (dst, src1, src2);
1203cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
1204cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1205cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1206cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic bool
1207cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_andn_cmp (bitset dst, bitset src1, bitset src2)
1208cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
1209cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bool changed;
1210cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1211cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (EBITSET_ZERO_P (src2))
1212cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
1213cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      return ebitset_copy_cmp (dst, src1);
1214cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
1215cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  else if (EBITSET_ZERO_P (src1))
1216cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
1217cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_weed (dst);
1218cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      changed = EBITSET_ZERO_P (dst);
1219cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_zero (dst);
1220cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      return changed;
1221cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
1222cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  return ebitset_op3_cmp (dst, src1, src2, BITSET_OP_ANDN);
1223cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
1224cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1225cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1226cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic void
1227cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_andn (bitset dst, bitset src1, bitset src2)
1228cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
1229cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_andn_cmp (dst, src1, src2);
1230cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
1231cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1232cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1233cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic bool
1234cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_or_cmp (bitset dst, bitset src1, bitset src2)
1235cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
1236cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (EBITSET_ZERO_P (src2))
1237cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
1238cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      return ebitset_copy_cmp (dst, src1);
1239cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
1240cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  else if (EBITSET_ZERO_P (src1))
1241cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
1242cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      return ebitset_copy_cmp (dst, src2);
1243cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
1244cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  return ebitset_op3_cmp (dst, src1, src2, BITSET_OP_OR);
1245cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
1246cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1247cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1248cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic void
1249cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_or (bitset dst, bitset src1, bitset src2)
1250cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
1251cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_or_cmp (dst, src1, src2);
1252cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
1253cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1254cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1255cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic bool
1256cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_xor_cmp (bitset dst, bitset src1, bitset src2)
1257cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
1258cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (EBITSET_ZERO_P (src2))
1259cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
1260cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      return ebitset_copy_cmp (dst, src1);
1261cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
1262cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  else if (EBITSET_ZERO_P (src1))
1263cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
1264cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      return ebitset_copy_cmp (dst, src2);
1265cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
1266cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  return ebitset_op3_cmp (dst, src1, src2, BITSET_OP_XOR);
1267cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
1268cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1269cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1270cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic void
1271cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_xor (bitset dst, bitset src1, bitset src2)
1272cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
1273cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_xor_cmp (dst, src1, src2);
1274cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
1275cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1276cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1277cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstatic void
1278cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_copy (bitset dst, bitset src)
1279cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
1280cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (BITSET_COMPATIBLE_ (dst, src))
1281cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_copy_ (dst, src);
1282cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  else
1283cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      bitset_copy_ (dst, src);
1284cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
1285cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1286cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1287cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Vector of operations for linked-list bitsets.  */
1288cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectstruct bitset_vtable ebitset_vtable = {
1289cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_set,
1290cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_reset,
1291cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_toggle_,
1292cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_test,
1293cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_resize,
1294cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_size_,
1295cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_count_,
1296cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_empty_p,
1297cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_ones,
1298cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_zero,
1299cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_copy,
1300cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_disjoint_p,
1301cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_equal_p,
1302cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_not,
1303cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_subset_p,
1304cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_and,
1305cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_and_cmp,
1306cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_andn,
1307cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_andn_cmp,
1308cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_or,
1309cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_or_cmp,
1310cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_xor,
1311cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_xor_cmp,
1312cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_and_or_,
1313cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_and_or_cmp_,
1314cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_andn_or_,
1315cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_andn_or_cmp_,
1316cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_or_and_,
1317cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bitset_or_and_cmp_,
1318cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_list,
1319cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_list_reverse,
1320cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_free,
1321cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  BITSET_TABLE
1322cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project};
1323cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1324cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1325cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Return size of initial structure.  */
1326cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectsize_t
1327cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_bytes (bitset_bindex n_bits ATTRIBUTE_UNUSED)
1328cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
1329cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  return sizeof (struct ebitset_struct);
1330cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
1331cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1332cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1333cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Initialize a bitset.  */
1334cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1335cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectbitset
1336cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_init (bitset bset, bitset_bindex n_bits)
1337cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
1338cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bset->b.vtable = &ebitset_vtable;
1339cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1340cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  bset->b.csize = EBITSET_ELT_WORDS;
1341cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1342cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  EBITSET_ZERO_SET (bset);
1343cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1344cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  EBITSET_ASIZE (bset) = 0;
1345cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  EBITSET_ELTS (bset) = 0;
1346cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_resize (bset, n_bits);
1347cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1348cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  return bset;
1349cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
1350cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1351cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1352cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectvoid
1353cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectebitset_release_memory (void)
1354cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
1355cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  ebitset_free_list = 0;
1356cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (ebitset_obstack_init)
1357cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
1358cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      ebitset_obstack_init = false;
1359cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      obstack_free (&ebitset_obstack, NULL);
1360cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
1361cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
1362