1 /* $Id: set.c,v 1.8 2002-08-02 19:26:55 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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
34 static Set mk_SetElement (SetType st, int n);
36 SetType mk_SetType (int chunk)
40 assert (chunk > 8 && chunk < 8000);
42 st = (SetType) imalloc (sizeof(*st));
45 st->alloclist = st->freelist = NULL;
51 int inf_SetType (SetType st, long *used, long *allocated)
57 for (s = st->alloclist; s; s = s->next)
58 *allocated += st->chunk;
59 return sizeof (SetElement);
62 SetType rm_SetType (SetType st)
65 for (s = st->alloclist; (s1 = s);)
74 Set mk_Set (SetType st)
80 static Set mk_SetElement (SetType st, int n)
86 assert (st->chunk > 8);
89 s = (Set) imalloc (sizeof(*s) * (1+st->chunk));
91 s->next = st->alloclist;
94 for (i=st->chunk; --i > 0; s++)
100 st->freelist = s->next;
106 static void rm_SetElement (SetType st, SetElement *p)
109 assert (st->used > 0);
110 p->next = st->freelist;
116 Set rm_Set (SetType st, Set s)
128 s->next = st->freelist;
131 assert (st->used >= 0);
136 Set add_Set (SetType st, Set s, int n)
139 Set p = &dummy, snew;
141 while (p->next && p->next->value < n)
144 if (!(p->next && p->next->value == n))
146 snew = mk_SetElement (st, n);
147 snew->next = p->next;
153 Set union_Set (SetType st, Set s1, Set s2)
159 for (p = &dummy; s1 && s2;)
160 if (s1->value < s2->value)
165 else if (s1->value > s2->value)
167 p = p->next = mk_SetElement (st, s2->value);
182 p = p->next = mk_SetElement (st, s2->value);
190 Set cp_Set (SetType st, Set s)
192 return merge_Set (st, s, NULL);
195 Set merge_Set (SetType st, Set s1, Set s2)
200 for (p = &dummy; s1 && s2; p = p->next)
201 if (s1->value < s2->value)
203 p->next = mk_SetElement (st, s1->value);
206 else if (s1->value > s2->value)
208 p->next = mk_SetElement (st, s2->value);
213 p->next = mk_SetElement (st, s1->value);
221 p = p->next = mk_SetElement (st, s1->value);
228 void pr_Set (SetType st, Set s)
233 printf (" %d", s->value);
239 unsigned hash_Set (SetType st, Set s)
250 int eq_Set (SetType st, Set s1, Set s2)
252 for (; s1 && s2; s1=s1->next, s2=s2->next)
253 if (s1->value != s2->value)