1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2011 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <idzebra/util.h>
31 #define GET_BIT(s,m) (s[(m)/(sizeof(BSetWord)*8)]&(1<<(m&(sizeof(BSetWord)*8-1))))
32 #define SET_BIT(s,m) (s[(m)/(sizeof(BSetWord)*8)]|=(1<<(m&(sizeof(BSetWord)*8-1))))
34 BSetHandle *mk_BSetHandle (int size, int chunk)
36 int wsize = 1+size/(sizeof(BSetWord)*8);
41 sh = (BSetHandle *) imalloc (sizeof(BSetHandle) +
42 chunk*sizeof(BSetWord)*wsize);
46 sh->chunk = chunk * wsize;
52 void rm_BSetHandle (BSetHandle **shp)
67 int inf_BSetHandle (BSetHandle *sh, long *used, long *allocated)
78 *allocated += sh->chunk;
79 } while ((sh = sh->setchain));
83 BSet mk_BSet (BSetHandle **shp)
92 if ((off + sh->wsize) > sh->chunk)
94 sh1 = (BSetHandle *) imalloc (sizeof(BSetHandle) +
95 sh->chunk*sizeof(BSetWord));
97 sh1->wsize = sh->wsize;
98 sh1->chunk = sh->chunk;
99 off = sh1->offset = 0;
103 sh->offset = off + sh->wsize;
104 return sh->setarray + off;
107 void add_BSet (BSetHandle *sh, BSet dst, unsigned member)
111 assert (member <= sh->size);
112 SET_BIT(dst, member);
115 unsigned test_BSet (BSetHandle *sh, BSet src, unsigned member)
119 assert (member <= sh->size);
120 return GET_BIT (src , member) != 0;
123 BSet cp_BSet (BSetHandle *sh, BSet dst, BSet src)
128 memcpy (dst, src, sh->wsize * sizeof(BSetWord));
132 void res_BSet (BSetHandle *sh, BSet dst)
135 memset (dst, 0, sh->wsize * sizeof(BSetWord));
138 void union_BSet (BSetHandle *sh, BSet dst, BSet src)
144 for (i=sh->wsize; --i >= 0;)
148 unsigned hash_BSet (BSetHandle *sh, BSet src)
154 for (i=sh->wsize; --i >= 0;)
159 void com_BSet (BSetHandle *sh, BSet dst)
164 for (i=sh->wsize; --i >= 0; dst++)
168 int eq_BSet (BSetHandle *sh, BSet dst, BSet src)
174 for (i=sh->wsize; --i >= 0;)
175 if (*dst++ != *src++)
180 int trav_BSet (BSetHandle *sh, BSet src, unsigned member)
182 int i = sh->size - member;
183 BSetWord *sw = src+member/(sizeof(BSetWord)*8);
184 unsigned b = member & (sizeof(BSetWord)*8-1);
186 if (b == 0 && *sw == 0)
188 member += sizeof(BSetWord)*8;
190 i -= sizeof(BSetWord)*8;
192 else if (*sw & (1<<b))
198 if (++b == sizeof(BSetWord)*8)
207 int travi_BSet (BSetHandle *sh, BSet src, unsigned member)
209 int i = sh->size - member;
210 BSetWord *sw = src+member/(sizeof(BSetWord)*8);
211 unsigned b = member & (sizeof(BSetWord)*8-1);
213 if (b == 0 && *sw == (BSetWord) ~ 0)
215 member += sizeof(BSetWord)*8;
217 i -= sizeof(BSetWord)*8;
219 else if ((*sw & (1<<b)) == 0)
225 if (++b == sizeof(BSetWord)*8)
235 void pr_BSet (BSetHandle *sh, BSet src)
240 for (i=0; (i=trav_BSet(sh,src,i)) != -1; i++)
245 void pr_charBSet (BSetHandle *sh, BSet src, void (*f) (int))
251 i = trav_BSet (sh, src, 0);
258 i1 = trav_BSet (sh, src, ++i);
263 while ((i1=trav_BSet (sh, src, i)) == i);
278 * c-file-style: "Stroustrup"
279 * indent-tabs-mode: nil
281 * vim: shiftwidth=4 tabstop=8 expandtab