1 /* $Id: bset.c,v 1.10 2006-08-14 10:40:08 adam Exp $
2 Copyright (C) 1995-2006
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <idzebra/util.h>
34 #define GET_BIT(s,m) (s[(m)/(sizeof(BSetWord)*8)]&(1<<(m&(sizeof(BSetWord)*8-1))))
35 #define SET_BIT(s,m) (s[(m)/(sizeof(BSetWord)*8)]|=(1<<(m&(sizeof(BSetWord)*8-1))))
37 BSetHandle *mk_BSetHandle (int size, int chunk)
39 int wsize = 1+size/(sizeof(BSetWord)*8);
44 sh = (BSetHandle *) imalloc (sizeof(BSetHandle) +
45 chunk*sizeof(BSetWord)*wsize);
49 sh->chunk = chunk * wsize;
55 void rm_BSetHandle (BSetHandle **shp)
70 int inf_BSetHandle (BSetHandle *sh, long *used, long *allocated)
81 *allocated += sh->chunk;
82 } while ((sh = sh->setchain));
86 BSet mk_BSet (BSetHandle **shp)
95 if ((off + sh->wsize) > sh->chunk)
97 sh1 = (BSetHandle *) imalloc (sizeof(BSetHandle) +
98 sh->chunk*sizeof(BSetWord));
100 sh1->wsize = sh->wsize;
101 sh1->chunk = sh->chunk;
102 off = sh1->offset = 0;
106 sh->offset = off + sh->wsize;
107 return sh->setarray + off;
110 void add_BSet (BSetHandle *sh, BSet dst, unsigned member)
114 assert (member <= sh->size);
115 SET_BIT(dst, member);
118 unsigned test_BSet (BSetHandle *sh, BSet src, unsigned member)
122 assert (member <= sh->size);
123 return GET_BIT (src , member) != 0;
126 BSet cp_BSet (BSetHandle *sh, BSet dst, BSet src)
131 memcpy (dst, src, sh->wsize * sizeof(BSetWord));
135 void res_BSet (BSetHandle *sh, BSet dst)
138 memset (dst, 0, sh->wsize * sizeof(BSetWord));
141 void union_BSet (BSetHandle *sh, BSet dst, BSet src)
147 for (i=sh->wsize; --i >= 0;)
151 unsigned hash_BSet (BSetHandle *sh, BSet src)
157 for (i=sh->wsize; --i >= 0;)
162 void com_BSet (BSetHandle *sh, BSet dst)
167 for (i=sh->wsize; --i >= 0; dst++)
171 int eq_BSet (BSetHandle *sh, BSet dst, BSet src)
177 for (i=sh->wsize; --i >= 0;)
178 if (*dst++ != *src++)
183 int trav_BSet (BSetHandle *sh, BSet src, unsigned member)
185 int i = sh->size - member;
186 BSetWord *sw = src+member/(sizeof(BSetWord)*8);
187 unsigned b = member & (sizeof(BSetWord)*8-1);
189 if (b == 0 && *sw == 0)
191 member += sizeof(BSetWord)*8;
193 i -= sizeof(BSetWord)*8;
195 else if (*sw & (1<<b))
201 if (++b == sizeof(BSetWord)*8)
210 int travi_BSet (BSetHandle *sh, BSet src, unsigned member)
212 int i = sh->size - member;
213 BSetWord *sw = src+member/(sizeof(BSetWord)*8);
214 unsigned b = member & (sizeof(BSetWord)*8-1);
216 if (b == 0 && *sw == (BSetWord) ~ 0)
218 member += sizeof(BSetWord)*8;
220 i -= sizeof(BSetWord)*8;
222 else if ((*sw & (1<<b)) == 0)
228 if (++b == sizeof(BSetWord)*8)
238 void pr_BSet (BSetHandle *sh, BSet src)
243 for (i=0; (i=trav_BSet(sh,src,i)) != -1; i++)
248 void pr_charBSet (BSetHandle *sh, BSet src, void (*f) (int))
254 i = trav_BSet (sh, src, 0);
261 i1 = trav_BSet (sh, src, ++i);
264 while ((i1=trav_BSet (sh, src, ++i)) == i)
280 * indent-tabs-mode: nil
282 * vim: shiftwidth=4 tabstop=8 expandtab