isl_int_sioimath.c: directly include stdlib.h (for malloc)
[isl.git] / isl_hmap_templ.c
blobc6a67473bce09fca39fdff5ace673e51ef32c654
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2013 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
9 * 91893 Orsay, France
10 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 #include <isl/ctx.h>
14 #include <isl/hash.h>
16 #define xCAT(A,B) A ## B
17 #define CAT(A,B) xCAT(A,B)
18 #define KEY CAT(isl_,KEY_BASE)
19 #define VAL CAT(isl_,VAL_BASE)
20 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
21 #define FN(TYPE,NAME) xFN(TYPE,NAME)
22 #define xHMAP(KEY,VAL_BASE) KEY ## _to_ ## VAL_BASE
23 #define yHMAP(KEY,VAL_BASE) xHMAP(KEY,VAL_BASE)
24 #define HMAP yHMAP(KEY,VAL_BASE)
25 #define HMAP_BASE yHMAP(KEY_BASE,VAL_BASE)
26 #define xS(TYPE1,TYPE2,NAME) struct isl_ ## TYPE1 ## _ ## TYPE2 ## _ ## NAME
27 #define yS(TYPE1,TYPE2,NAME) xS(TYPE1,TYPE2,NAME)
28 #define S(NAME) yS(KEY_BASE,VAL_BASE,NAME)
30 struct HMAP {
31 int ref;
32 isl_ctx *ctx;
33 struct isl_hash_table table;
36 S(pair) {
37 KEY *key;
38 VAL *val;
41 __isl_give HMAP *FN(HMAP,alloc)(isl_ctx *ctx, int min_size)
43 HMAP *hmap;
45 hmap = isl_calloc_type(ctx, HMAP);
46 if (!hmap)
47 return NULL;
49 hmap->ctx = ctx;
50 isl_ctx_ref(ctx);
51 hmap->ref = 1;
53 if (isl_hash_table_init(ctx, &hmap->table, min_size) < 0)
54 return FN(HMAP,free)(hmap);
56 return hmap;
59 static isl_stat free_pair(void **entry, void *user)
61 S(pair) *pair = *entry;
62 FN(KEY,free)(pair->key);
63 FN(VAL,free)(pair->val);
64 free(pair);
65 *entry = NULL;
66 return isl_stat_ok;
69 __isl_null HMAP *FN(HMAP,free)(__isl_take HMAP *hmap)
71 if (!hmap)
72 return NULL;
73 if (--hmap->ref > 0)
74 return NULL;
75 isl_hash_table_foreach(hmap->ctx, &hmap->table, &free_pair, NULL);
76 isl_hash_table_clear(&hmap->table);
77 isl_ctx_deref(hmap->ctx);
78 free(hmap);
79 return NULL;
82 isl_ctx *FN(HMAP,get_ctx)(__isl_keep HMAP *hmap)
84 return hmap ? hmap->ctx : NULL;
87 /* Add a mapping from "key" to "val" to the associative array
88 * pointed to by user.
90 static isl_stat add_key_val(__isl_take KEY *key, __isl_take VAL *val,
91 void *user)
93 HMAP **hmap = (HMAP **) user;
95 *hmap = FN(HMAP,set)(*hmap, key, val);
97 if (!*hmap)
98 return isl_stat_error;
100 return isl_stat_ok;
103 __isl_give HMAP *FN(HMAP,dup)(__isl_keep HMAP *hmap)
105 HMAP *dup;
107 if (!hmap)
108 return NULL;
110 dup = FN(HMAP,alloc)(hmap->ctx, hmap->table.n);
111 if (FN(HMAP,foreach)(hmap, &add_key_val, &dup) < 0)
112 return FN(HMAP,free)(dup);
114 return dup;
117 __isl_give HMAP *FN(HMAP,cow)(__isl_take HMAP *hmap)
119 if (!hmap)
120 return NULL;
122 if (hmap->ref == 1)
123 return hmap;
124 hmap->ref--;
125 return FN(HMAP,dup)(hmap);
128 __isl_give HMAP *FN(HMAP,copy)(__isl_keep HMAP *hmap)
130 if (!hmap)
131 return NULL;
133 hmap->ref++;
134 return hmap;
137 static int has_key(const void *entry, const void *c_key)
139 const S(pair) *pair = entry;
140 KEY *key = (KEY *) c_key;
142 return KEY_EQUAL(pair->key, key);
145 isl_bool FN(HMAP,has)(__isl_keep HMAP *hmap, __isl_keep KEY *key)
147 uint32_t hash;
149 if (!hmap)
150 return isl_bool_error;
152 hash = FN(KEY,get_hash)(key);
153 return !!isl_hash_table_find(hmap->ctx, &hmap->table, hash,
154 &has_key, key, 0);
157 __isl_give VAL *FN(HMAP,get)(__isl_keep HMAP *hmap, __isl_take KEY *key)
159 struct isl_hash_table_entry *entry;
160 S(pair) *pair;
161 uint32_t hash;
163 if (!hmap || !key)
164 goto error;
166 hash = FN(KEY,get_hash)(key);
167 entry = isl_hash_table_find(hmap->ctx, &hmap->table, hash,
168 &has_key, key, 0);
169 FN(KEY,free)(key);
171 if (!entry)
172 return NULL;
174 pair = entry->data;
176 return FN(VAL,copy)(pair->val);
177 error:
178 FN(KEY,free)(key);
179 return NULL;
182 /* Remove the mapping between "key" and its associated value (if any)
183 * from "hmap".
185 * If "key" is not mapped to anything, then we leave "hmap" untouched"
187 __isl_give HMAP *FN(HMAP,drop)(__isl_take HMAP *hmap, __isl_take KEY *key)
189 struct isl_hash_table_entry *entry;
190 S(pair) *pair;
191 uint32_t hash;
193 if (!hmap || !key)
194 goto error;
196 hash = FN(KEY,get_hash)(key);
197 entry = isl_hash_table_find(hmap->ctx, &hmap->table, hash,
198 &has_key, key, 0);
199 if (!entry) {
200 FN(KEY,free)(key);
201 return hmap;
204 hmap = FN(HMAP,cow)(hmap);
205 if (!hmap)
206 goto error;
207 entry = isl_hash_table_find(hmap->ctx, &hmap->table, hash,
208 &has_key, key, 0);
209 FN(KEY,free)(key);
211 if (!entry)
212 isl_die(hmap->ctx, isl_error_internal,
213 "missing entry" , goto error);
215 pair = entry->data;
216 isl_hash_table_remove(hmap->ctx, &hmap->table, entry);
217 FN(KEY,free)(pair->key);
218 FN(VAL,free)(pair->val);
219 free(pair);
221 return hmap;
222 error:
223 FN(KEY,free)(key);
224 FN(HMAP,free)(hmap);
225 return NULL;
228 /* Add a mapping from "key" to "val" to "hmap".
229 * If "key" was already mapped to something else, then that mapping
230 * is replaced.
231 * If key happened to be mapped to "val" already, then we leave
232 * "hmap" untouched.
234 __isl_give HMAP *FN(HMAP,set)(__isl_take HMAP *hmap,
235 __isl_take KEY *key, __isl_take VAL *val)
237 struct isl_hash_table_entry *entry;
238 S(pair) *pair;
239 uint32_t hash;
241 if (!hmap || !key || !val)
242 goto error;
244 hash = FN(KEY,get_hash)(key);
245 entry = isl_hash_table_find(hmap->ctx, &hmap->table, hash,
246 &has_key, key, 0);
247 if (entry) {
248 int equal;
249 pair = entry->data;
250 equal = VAL_EQUAL(pair->val, val);
251 if (equal < 0)
252 goto error;
253 if (equal) {
254 FN(KEY,free)(key);
255 FN(VAL,free)(val);
256 return hmap;
260 hmap = FN(HMAP,cow)(hmap);
261 if (!hmap)
262 goto error;
264 entry = isl_hash_table_find(hmap->ctx, &hmap->table, hash,
265 &has_key, key, 1);
267 if (!entry)
268 goto error;
270 if (entry->data) {
271 pair = entry->data;
272 FN(VAL,free)(pair->val);
273 pair->val = val;
274 FN(KEY,free)(key);
275 return hmap;
278 pair = isl_alloc_type(hmap->ctx, S(pair));
279 if (!pair)
280 goto error;
282 entry->data = pair;
283 pair->key = key;
284 pair->val = val;
285 return hmap;
286 error:
287 FN(KEY,free)(key);
288 FN(VAL,free)(val);
289 return FN(HMAP,free)(hmap);
292 /* Internal data structure for isl_map_to_basic_set_foreach.
294 * fn is the function that should be called on each entry.
295 * user is the user-specified final argument to fn.
297 S(foreach_data) {
298 isl_stat (*fn)(__isl_take KEY *key, __isl_take VAL *val, void *user);
299 void *user;
302 /* Call data->fn on a copy of the key and value in *entry.
304 static isl_stat call_on_copy(void **entry, void *user)
306 S(pair) *pair = *entry;
307 S(foreach_data) *data = (S(foreach_data) *) user;
309 return data->fn(FN(KEY,copy)(pair->key), FN(VAL,copy)(pair->val),
310 data->user);
313 /* Call "fn" on each pair of key and value in "hmap".
315 isl_stat FN(HMAP,foreach)(__isl_keep HMAP *hmap,
316 isl_stat (*fn)(__isl_take KEY *key, __isl_take VAL *val, void *user),
317 void *user)
319 S(foreach_data) data = { fn, user };
321 if (!hmap)
322 return isl_stat_error;
324 return isl_hash_table_foreach(hmap->ctx, &hmap->table,
325 &call_on_copy, &data);
328 /* Internal data structure for print_pair.
330 * p is the printer on which the associative array is being printed.
331 * first is set if the current key-value pair is the first to be printed.
333 S(print_data) {
334 isl_printer *p;
335 int first;
338 /* Print the given key-value pair to data->p.
340 static isl_stat print_pair(__isl_take KEY *key, __isl_take VAL *val, void *user)
342 S(print_data) *data = user;
344 if (!data->first)
345 data->p = isl_printer_print_str(data->p, ", ");
346 data->p = FN(isl_printer_print,KEY_BASE)(data->p, key);
347 data->p = isl_printer_print_str(data->p, ": ");
348 data->p = FN(isl_printer_print,VAL_BASE)(data->p, val);
349 data->first = 0;
351 FN(KEY,free)(key);
352 FN(VAL,free)(val);
353 return isl_stat_ok;
356 /* Print the associative array to "p".
358 __isl_give isl_printer *FN(isl_printer_print,HMAP_BASE)(
359 __isl_take isl_printer *p, __isl_keep HMAP *hmap)
361 S(print_data) data;
363 if (!p || !hmap)
364 return isl_printer_free(p);
366 p = isl_printer_print_str(p, "{");
367 data.p = p;
368 data.first = 1;
369 if (FN(HMAP,foreach)(hmap, &print_pair, &data) < 0)
370 data.p = isl_printer_free(data.p);
371 p = data.p;
372 p = isl_printer_print_str(p, "}");
374 return p;
377 void FN(HMAP,dump)(__isl_keep HMAP *hmap)
379 isl_printer *printer;
381 if (!hmap)
382 return;
384 printer = isl_printer_to_file(FN(HMAP,get_ctx)(hmap), stderr);
385 printer = FN(isl_printer_print,HMAP_BASE)(printer, hmap);
386 printer = isl_printer_end_line(printer);
388 isl_printer_free(printer);