source: vis_dev/vl2mv-2.3/src/set/set.c @ 100

Last change on this file since 100 was 18, checked in by cecile, 13 years ago

vl2mv added

File size: 4.2 KB
Line 
1/*
2 *  Utilities for handling sets.
3 *
4 *  Copyright (c) 1992, 1993
5 *        Regents of the University of California
6 *  All rights reserved.
7 *
8 *  Use and copying of this software and preparation of derivative works
9 *  based upon this software are permitted.  However, any distribution of
10 *  this software or derivative works must include the above copyright
11 *  notice.
12 *
13 *  This software is made available AS IS, and neither the Electronics
14 *  Research Laboratory or the Universify of California make any
15 *  warranty about the software, its performance or its conformity to
16 *  any specification.
17 *
18 *  $Header: /projects/development/hsv/CVSRepository/vl2mv/src/set/set.c,v 1.1.1.1 2001/07/09 23:22:43 fabio Exp $
19 *
20 * Author: STCheng, stcheng@ic.berkeley.edu
21 */
22
23#include <stdio.h>
24#include "util.h"
25#include "st.h"
26#include "set.h"
27
28/*
29 * strdup
30 *
31 *     duplicate a string
32 */
33static char *SetStrdup(str)
34char *str;
35{
36    char *retval;
37
38    retval = ALLOC(char, strlen(str)+1);
39    strcpy(retval, str);
40    return retval;
41}
42
43
44/*
45 * set_empty
46 *
47 *     initialize a new empty set
48 *
49 * argument:
50 *
51 * returns: {}
52 *
53 * bugs:
54 *
55 * comments:
56 */
57set_t *set_empty()
58{
59    set_t *retval;
60
61    retval = st_init_table(strcmp, st_strhash);
62    return retval;
63}
64
65/*
66 * set_destroy
67 *
68 *     destroy a set data structure
69 *
70 * argument: s
71 *
72 * returns:
73 *
74 * bugs:
75 *
76 * comments:
77 */
78void set_destroy(s)
79set_t *s;
80{
81    st_generator *gen;
82    char *key, *data;
83
84    gen = st_init_gen(s);
85    while (st_gen(gen, &key, &data)) {
86        if (key != data)
87            FREE(key);
88        FREE(data);
89    }
90    st_free_gen(gen);
91
92    st_free_table(s);
93}
94
95/*
96 * set_add
97 *
98 *     add am element to a set
99 *
100 * argument: a, s
101 *
102 * returns: s'
103 *
104 * side effects: s will be modified
105 *
106 * bugs:
107 *
108 * comment:
109 */
110set_t *set_add(a, s)
111char *a;
112set_t *s;
113{
114    char *dummy;
115
116    if (!st_lookup(s, a, &dummy)) {
117        st_insert(s, SetStrdup(a), SetStrdup(a));
118    }
119    return s;
120}
121
122/*
123 * set_eliminate
124 *
125 *     remove am element from a set
126 *
127 * argument: a, s
128 *
129 * returns: s'
130 *
131 * side effects: s will be modified
132 *
133 * bugs:
134 *
135 * comment:
136 */
137set_t *set_eliminate(a, s)
138char *a;
139set_t *s;
140{
141    char *dummy;
142    char *key;
143
144    if (st_lookup(s, a, &dummy)) {
145        key = a;
146        st_delete(s, &key, &dummy);
147        FREE(dummy);
148        FREE(dummy);
149    }
150    return s;
151}
152
153/*
154 * set_find
155 *
156 *     determine if an element a is in s
157 *
158 * arguments: a, s
159 *
160 * returns: 1/0
161 *
162 * side effects:
163 *
164 * bugs:
165 *
166 * comments:
167 */
168int set_find(a, s)
169char *a;
170set_t *s;
171{
172    char *dummy;
173
174    return (st_lookup(s, a, &dummy));
175}
176
177/*
178 * set_union
179 *
180 *     given two sets (s1, s2) find their union
181 *
182 * argument: s1, s2
183 *
184 * returns: s1 + s2
185 *
186 * bugs:
187 *
188 * comments:
189 */
190set_t *set_union(s1, s2)
191set_t *s1;
192set_t *s2;
193{
194    st_generator *gen;
195    set_t *retval;
196    char *key, *data, *dummy;
197
198    retval = st_init_table(strcmp, st_strhash);
199    gen = st_init_gen(s1);
200    while (st_gen(gen, &key, &data)) {
201        st_insert(retval, SetStrdup(key), SetStrdup(data));
202    }
203    st_free_gen(gen);
204    gen = st_init_gen(s2);
205    while (st_gen(gen, &key, &data)) {
206        if (!st_lookup(s1, key, &dummy)) {
207            st_insert(retval, SetStrdup(key), SetStrdup(data));
208        }
209    }
210    st_free_gen(gen);
211
212    return retval;
213}
214
215/*
216 * set_intersect
217 *
218 *     given two sets (s1, s2) find their intersection
219 *
220 * argument: s1, s2
221 *
222 * returns: s1 * s2
223 *
224 * bugs:
225 *
226 * comments:
227 */
228set_t *set_intersect(s1, s2)
229set_t *s1;
230set_t *s2;
231{
232    st_generator *gen;
233    set_t *retval;
234    char *key, *data, *dummy;
235
236    retval = st_init_table(strcmp, st_strhash);
237    gen = st_init_gen(s1);
238    while (st_gen(gen, &key, &data)) {
239        if (st_lookup(s2, key, &dummy)) {
240            st_insert(retval, SetStrdup(key), SetStrdup(key));
241        }
242    }
243    st_free_gen(gen);
244
245    return retval;
246}
247
248/*
249 * set_dup
250 *
251 *     duplicate a set and all its elements
252 *
253 * arguments: s
254 *
255 * returns: s' ( = s )
256 *
257 * side effects:
258 *
259 * bugs:
260 *
261 * comments:
262 */
263set_t *set_dup(s)
264set_t *s;
265{
266    st_generator *gen;
267    set_t *retval;
268    char *key, *data;
269
270    retval = st_init_table(strcmp, st_strhash);
271    gen = st_init_gen(s);
272    while (st_gen(gen, &key, &data)) {
273        st_insert(retval, SetStrdup(key), SetStrdup(data));
274    }
275    st_free_gen(gen);
276
277    return retval;
278}
279
Note: See TracBrowser for help on using the repository browser.