c4f2d992 |
1 | /* -*-c-*- |
2 | * |
03f996bd |
3 | * $Id: set.c,v 1.2 1997/08/04 10:24:25 mdw Exp $ |
c4f2d992 |
4 | * |
5 | * Management of sets (for the use of the class expression handler) |
6 | * |
7 | * (c) 1997 EBI |
8 | */ |
9 | |
03f996bd |
10 | /*----- Licensing notice --------------------------------------------------* |
c4f2d992 |
11 | * |
12 | * This file is part of `become' |
13 | * |
14 | * `Become' is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU General Public License as published by |
16 | * the Free Software Foundation; either version 2 of the License, or |
17 | * (at your option) any later version. |
18 | * |
19 | * `Become' is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU General Public License |
03f996bd |
25 | * along with `become'; if not, write to the Free Software Foundation, |
26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
c4f2d992 |
27 | */ |
28 | |
29 | /*----- Revision history --------------------------------------------------* |
30 | * |
31 | * $Log: set.c,v $ |
03f996bd |
32 | * Revision 1.2 1997/08/04 10:24:25 mdw |
33 | * Sources placed under CVS control. |
34 | * |
35 | * Revision 1.1 1997/07/21 13:47:44 mdw |
c4f2d992 |
36 | * Initial revision |
37 | * |
38 | */ |
39 | |
40 | /*----- Header files ------------------------------------------------------*/ |
41 | |
42 | /* --- ANSI headers --- */ |
43 | |
44 | #include <stdio.h> |
45 | #include <stdlib.h> |
46 | #include <string.h> |
47 | |
48 | /* --- Local headers --- */ |
49 | |
50 | #include "set.h" |
51 | #include "sym.h" |
52 | #include "utils.h" |
53 | |
54 | /*----- Main code ---------------------------------------------------------*/ |
55 | |
56 | /* --- @set_subtract@ --- * |
57 | * |
58 | * Arguments: @sym_table *a@ = pointer to table @A@ |
59 | * @sym_table *b@ = pointer to table @B@ |
60 | * |
61 | * Returns: A symbol table containing all the elements in @A@ which don't |
62 | * appear in @B@. |
63 | * |
64 | * Use: Subtracts a symbol table from another symbol table. Assumes |
65 | * that there's no data following the actual @sym_base@ block |
66 | * for each item in the table. |
67 | */ |
68 | |
69 | sym_table *set_subtract(sym_table *a, sym_table *b) |
70 | { |
71 | sym_table *c = xmalloc(sizeof(*c)); |
72 | sym_iter i; |
73 | sym_base *s; |
74 | unsigned f; |
75 | |
76 | sym_createTable(c); |
77 | for (sym_createIter(&i, a); (s = sym_next(&i)) != 0; ) { |
78 | if (!sym_find(b, s->name, s->len, 0, &f)) |
79 | sym_find(c, s->name, s->len, sizeof(sym_base), 0); |
80 | } |
81 | |
82 | return (c); |
83 | } |
84 | |
85 | /* --- @set_intersect@ --- * |
86 | * |
87 | * Arguments: @sym_table *a@ = pointer to table @A@ |
88 | * @sym_table *b@ = pointer to table @B@ |
89 | * |
90 | * Returns: A symbol table containing all the elements in @A@ which also |
91 | * appear in @B@. |
92 | * |
93 | * Use: Constructs the intersection of two symbol tables. Assumes |
94 | * that there's no data following the actual @sym_base@ block |
95 | * for each item in the table. |
96 | */ |
97 | |
98 | sym_table *set_intersect(sym_table *a, sym_table *b) |
99 | { |
100 | sym_table *c = xmalloc(sizeof(*c)); |
101 | sym_iter i; |
102 | sym_base *s; |
103 | unsigned f; |
104 | |
105 | sym_createTable(c); |
106 | for (sym_createIter(&i, a); (s = sym_next(&i)) != 0; ) { |
107 | if (sym_find(b, s->name, s->len, 0, &f)) |
108 | sym_find(c, s->name, s->len, sizeof(sym_base), 0); |
109 | } |
110 | |
111 | return (c); |
112 | } |
113 | |
114 | /* --- @set_union@ --- * |
115 | * |
116 | * Arguments: @sym_table *a@ = pointer to table @A@ |
117 | * @sym_table *b@ = pointer to table @B@ |
118 | * |
119 | * Returns: A symbol table containing all the elements in @A@ and those |
120 | * in @B@. |
121 | * |
122 | * Use: Constructs the union of two symbol tables. Assumes that |
123 | * there's no data following the actual @sym_base@ block for |
124 | * each item in the table. |
125 | */ |
126 | |
127 | sym_table *set_union(sym_table *a, sym_table *b) |
128 | { |
129 | sym_table *c = xmalloc(sizeof(*c)); |
130 | sym_iter i; |
131 | sym_base *s; |
132 | |
133 | sym_createTable(c); |
134 | for (sym_createIter(&i, a); (s = sym_next(&i)) != 0; ) |
135 | sym_find(c, s->name, s->len, sizeof(sym_base), 0); |
136 | |
137 | for (sym_createIter(&i, b); (s = sym_next(&i)) != 0; ) |
138 | sym_find(c, s->name, s->len, sizeof(sym_base), 0); |
139 | |
140 | return (c); |
141 | } |
142 | |
143 | /* --- @set_copy@ --- * |
144 | * |
145 | * Arguments: @sym_table *a@ = pointer to table |
146 | * |
147 | * Returns: A copy of the symbol table. |
148 | * |
149 | * Use: Copies a symbol table. Same assumptions again... |
150 | */ |
151 | |
152 | sym_table *set_copy(sym_table *a) |
153 | { |
154 | sym_table *c = xmalloc(sizeof(*c)); |
155 | sym_iter i; |
156 | sym_base *s; |
157 | |
158 | for (sym_createIter(&i, a); (s = sym_next(&i)) != 0; ) |
159 | sym_find(c, s->name, s->len, sizeof(sym_base), 0); |
160 | |
161 | return (c); |
162 | } |
163 | |
164 | /*----- That's all, folks -------------------------------------------------*/ |