| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: set.c,v 1.1 1997/07/21 13:47:44 mdw Exp $ |
| 4 | * |
| 5 | * Management of sets (for the use of the class expression handler) |
| 6 | * |
| 7 | * (c) 1997 EBI |
| 8 | */ |
| 9 | |
| 10 | /*----- Licencing notice --------------------------------------------------* |
| 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 |
| 25 | * along with `become'; if not, write to the Free Software |
| 26 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 27 | */ |
| 28 | |
| 29 | /*----- Revision history --------------------------------------------------* |
| 30 | * |
| 31 | * $Log: set.c,v $ |
| 32 | * Revision 1.1 1997/07/21 13:47:44 mdw |
| 33 | * Initial revision |
| 34 | * |
| 35 | */ |
| 36 | |
| 37 | /*----- Header files ------------------------------------------------------*/ |
| 38 | |
| 39 | /* --- ANSI headers --- */ |
| 40 | |
| 41 | #include <stdio.h> |
| 42 | #include <stdlib.h> |
| 43 | #include <string.h> |
| 44 | |
| 45 | /* --- Local headers --- */ |
| 46 | |
| 47 | #include "set.h" |
| 48 | #include "sym.h" |
| 49 | #include "utils.h" |
| 50 | |
| 51 | /*----- Main code ---------------------------------------------------------*/ |
| 52 | |
| 53 | /* --- @set_subtract@ --- * |
| 54 | * |
| 55 | * Arguments: @sym_table *a@ = pointer to table @A@ |
| 56 | * @sym_table *b@ = pointer to table @B@ |
| 57 | * |
| 58 | * Returns: A symbol table containing all the elements in @A@ which don't |
| 59 | * appear in @B@. |
| 60 | * |
| 61 | * Use: Subtracts a symbol table from another symbol table. Assumes |
| 62 | * that there's no data following the actual @sym_base@ block |
| 63 | * for each item in the table. |
| 64 | */ |
| 65 | |
| 66 | sym_table *set_subtract(sym_table *a, sym_table *b) |
| 67 | { |
| 68 | sym_table *c = xmalloc(sizeof(*c)); |
| 69 | sym_iter i; |
| 70 | sym_base *s; |
| 71 | unsigned f; |
| 72 | |
| 73 | sym_createTable(c); |
| 74 | for (sym_createIter(&i, a); (s = sym_next(&i)) != 0; ) { |
| 75 | if (!sym_find(b, s->name, s->len, 0, &f)) |
| 76 | sym_find(c, s->name, s->len, sizeof(sym_base), 0); |
| 77 | } |
| 78 | |
| 79 | return (c); |
| 80 | } |
| 81 | |
| 82 | /* --- @set_intersect@ --- * |
| 83 | * |
| 84 | * Arguments: @sym_table *a@ = pointer to table @A@ |
| 85 | * @sym_table *b@ = pointer to table @B@ |
| 86 | * |
| 87 | * Returns: A symbol table containing all the elements in @A@ which also |
| 88 | * appear in @B@. |
| 89 | * |
| 90 | * Use: Constructs the intersection of two symbol tables. Assumes |
| 91 | * that there's no data following the actual @sym_base@ block |
| 92 | * for each item in the table. |
| 93 | */ |
| 94 | |
| 95 | sym_table *set_intersect(sym_table *a, sym_table *b) |
| 96 | { |
| 97 | sym_table *c = xmalloc(sizeof(*c)); |
| 98 | sym_iter i; |
| 99 | sym_base *s; |
| 100 | unsigned f; |
| 101 | |
| 102 | sym_createTable(c); |
| 103 | for (sym_createIter(&i, a); (s = sym_next(&i)) != 0; ) { |
| 104 | if (sym_find(b, s->name, s->len, 0, &f)) |
| 105 | sym_find(c, s->name, s->len, sizeof(sym_base), 0); |
| 106 | } |
| 107 | |
| 108 | return (c); |
| 109 | } |
| 110 | |
| 111 | /* --- @set_union@ --- * |
| 112 | * |
| 113 | * Arguments: @sym_table *a@ = pointer to table @A@ |
| 114 | * @sym_table *b@ = pointer to table @B@ |
| 115 | * |
| 116 | * Returns: A symbol table containing all the elements in @A@ and those |
| 117 | * in @B@. |
| 118 | * |
| 119 | * Use: Constructs the union of two symbol tables. Assumes that |
| 120 | * there's no data following the actual @sym_base@ block for |
| 121 | * each item in the table. |
| 122 | */ |
| 123 | |
| 124 | sym_table *set_union(sym_table *a, sym_table *b) |
| 125 | { |
| 126 | sym_table *c = xmalloc(sizeof(*c)); |
| 127 | sym_iter i; |
| 128 | sym_base *s; |
| 129 | |
| 130 | sym_createTable(c); |
| 131 | for (sym_createIter(&i, a); (s = sym_next(&i)) != 0; ) |
| 132 | sym_find(c, s->name, s->len, sizeof(sym_base), 0); |
| 133 | |
| 134 | for (sym_createIter(&i, b); (s = sym_next(&i)) != 0; ) |
| 135 | sym_find(c, s->name, s->len, sizeof(sym_base), 0); |
| 136 | |
| 137 | return (c); |
| 138 | } |
| 139 | |
| 140 | /* --- @set_copy@ --- * |
| 141 | * |
| 142 | * Arguments: @sym_table *a@ = pointer to table |
| 143 | * |
| 144 | * Returns: A copy of the symbol table. |
| 145 | * |
| 146 | * Use: Copies a symbol table. Same assumptions again... |
| 147 | */ |
| 148 | |
| 149 | sym_table *set_copy(sym_table *a) |
| 150 | { |
| 151 | sym_table *c = xmalloc(sizeof(*c)); |
| 152 | sym_iter i; |
| 153 | sym_base *s; |
| 154 | |
| 155 | for (sym_createIter(&i, a); (s = sym_next(&i)) != 0; ) |
| 156 | sym_find(c, s->name, s->len, sizeof(sym_base), 0); |
| 157 | |
| 158 | return (c); |
| 159 | } |
| 160 | |
| 161 | /*----- That's all, folks -------------------------------------------------*/ |