1 /* $Id: set.c,v 1.10 2005-01-15 21:45:42 adam Exp $
2 Copyright (C) 1995-2005
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 Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
33 static DFASet mk_DFASetElement (DFASetType st, int n);
35 DFASetType mk_DFASetType (int chunk)
39 assert (chunk > 8 && chunk < 8000);
41 st = (DFASetType) imalloc (sizeof(*st));
44 st->alloclist = st->freelist = NULL;
50 int inf_DFASetType (DFASetType st, long *used, long *allocated)
56 for (s = st->alloclist; s; s = s->next)
57 *allocated += st->chunk;
58 return sizeof (DFASetElement);
61 DFASetType rm_DFASetType (DFASetType st)
64 for (s = st->alloclist; (s1 = s);)
73 DFASet mk_DFASet (DFASetType st)
79 static DFASet mk_DFASetElement (DFASetType st, int n)
85 assert (st->chunk > 8);
88 s = (DFASet) imalloc (sizeof(*s) * (1+st->chunk));
90 s->next = st->alloclist;
93 for (i=st->chunk; --i > 0; s++)
99 st->freelist = s->next;
105 static void rm_DFASetElement (DFASetType st, DFASetElement *p)
108 assert (st->used > 0);
109 p->next = st->freelist;
115 DFASet rm_DFASet (DFASetType st, DFASet s)
127 s->next = st->freelist;
130 assert (st->used >= 0);
135 DFASet add_DFASet (DFASetType st, DFASet s, int n)
138 DFASet p = &dummy, snew;
140 while (p->next && p->next->value < n)
143 if (!(p->next && p->next->value == n))
145 snew = mk_DFASetElement (st, n);
146 snew->next = p->next;
152 DFASet union_DFASet (DFASetType st, DFASet s1, DFASet s2)
158 for (p = &dummy; s1 && s2;)
159 if (s1->value < s2->value)
164 else if (s1->value > s2->value)
166 p = p->next = mk_DFASetElement (st, s2->value);
181 p = p->next = mk_DFASetElement (st, s2->value);
189 DFASet cp_DFASet (DFASetType st, DFASet s)
191 return merge_DFASet (st, s, NULL);
194 DFASet merge_DFASet (DFASetType st, DFASet s1, DFASet s2)
199 for (p = &dummy; s1 && s2; p = p->next)
200 if (s1->value < s2->value)
202 p->next = mk_DFASetElement (st, s1->value);
205 else if (s1->value > s2->value)
207 p->next = mk_DFASetElement (st, s2->value);
212 p->next = mk_DFASetElement (st, s1->value);
220 p = p->next = mk_DFASetElement (st, s1->value);
227 void pr_DFASet (DFASetType st, DFASet s)
232 printf (" %d", s->value);
238 unsigned hash_DFASet (DFASetType st, DFASet s)
249 int eq_DFASet (DFASetType st, DFASet s1, DFASet s2)
251 for (; s1 && s2; s1=s1->next, s2=s2->next)
252 if (s1->value != s2->value)