add isl_ast_expr_access
[isl.git] / isl_hmap_templ.c
blob07f72a4b65b140c2e23308dc25b77a2800ceb306
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 int 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 0;
69 void *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 int add_key_val(__isl_take KEY *key, __isl_take VAL *val, void *user)
92 HMAP **hmap = (HMAP **) user;
94 *hmap = FN(HMAP,set)(*hmap, key, val);
96 if (!*hmap)
97 return -1;
99 return 0;
102 __isl_give HMAP *FN(HMAP,dup)(__isl_keep HMAP *hmap)
104 HMAP *dup;
106 if (!hmap)
107 return NULL;
109 dup = FN(HMAP,alloc)(hmap->ctx, hmap->table.n);
110 if (FN(HMAP,foreach)(hmap, &add_key_val, &dup) < 0)
111 return FN(HMAP,free)(dup);
113 return dup;
116 __isl_give HMAP *FN(HMAP,cow)(__isl_take HMAP *hmap)
118 if (!hmap)
119 return NULL;
121 if (hmap->ref == 1)
122 return hmap;
123 hmap->ref--;
124 return FN(HMAP,dup)(hmap);
127 __isl_give HMAP *FN(HMAP,copy)(__isl_keep HMAP *hmap)
129 if (!hmap)
130 return NULL;
132 hmap->ref++;
133 return hmap;
136 static int has_key(const void *entry, const void *c_key)
138 const S(pair) *pair = entry;
139 KEY *key = (KEY *) c_key;
141 return KEY_EQUAL(pair->key, key);
144 int FN(HMAP,has)(__isl_keep HMAP *hmap, __isl_keep KEY *key)
146 uint32_t hash;
148 if (!hmap)
149 return -1;
151 hash = FN(KEY,get_hash)(key);
152 return !!isl_hash_table_find(hmap->ctx, &hmap->table, hash,
153 &has_key, key, 0);
156 __isl_give VAL *FN(HMAP,get)(__isl_keep HMAP *hmap, __isl_take KEY *key)
158 struct isl_hash_table_entry *entry;
159 S(pair) *pair;
160 uint32_t hash;
162 if (!hmap || !key)
163 goto error;
165 hash = FN(KEY,get_hash)(key);
166 entry = isl_hash_table_find(hmap->ctx, &hmap->table, hash,
167 &has_key, key, 0);
168 FN(KEY,free)(key);
170 if (!entry)
171 return NULL;
173 pair = entry->data;
175 return FN(VAL,copy)(pair->val);
176 error:
177 FN(KEY,free)(key);
178 return NULL;
181 /* Add a mapping from "key" to "val" to "hmap".
182 * If "key" was already mapped to something else, then that mapping
183 * is replaced.
184 * If key happened to be mapped to "val" already, then we leave
185 * "hmap" untouched.
187 __isl_give HMAP *FN(HMAP,set)(__isl_take HMAP *hmap,
188 __isl_take KEY *key, __isl_take VAL *val)
190 struct isl_hash_table_entry *entry;
191 S(pair) *pair;
192 uint32_t hash;
194 if (!hmap)
195 goto error;
197 hash = FN(KEY,get_hash)(key);
198 entry = isl_hash_table_find(hmap->ctx, &hmap->table, hash,
199 &has_key, key, 0);
200 if (entry) {
201 int equal;
202 pair = entry->data;
203 equal = VAL_EQUAL(pair->val, val);
204 if (equal < 0)
205 goto error;
206 if (equal) {
207 FN(KEY,free)(key);
208 FN(VAL,free)(val);
209 return hmap;
213 hmap = FN(HMAP,cow)(hmap);
214 if (!hmap)
215 goto error;
217 entry = isl_hash_table_find(hmap->ctx, &hmap->table, hash,
218 &has_key, key, 1);
220 if (!entry)
221 goto error;
223 if (entry->data) {
224 pair = entry->data;
225 FN(VAL,free)(pair->val);
226 pair->val = val;
227 FN(KEY,free)(key);
228 return hmap;
231 pair = isl_alloc_type(hmap->ctx, S(pair));
232 if (!pair)
233 goto error;
235 entry->data = pair;
236 pair->key = key;
237 pair->val = val;
238 return hmap;
239 error:
240 FN(KEY,free)(key);
241 FN(VAL,free)(val);
242 return FN(HMAP,free)(hmap);
245 /* Internal data structure for isl_map_to_basic_set_foreach.
247 * fn is the function that should be called on each entry.
248 * user is the user-specified final argument to fn.
250 S(foreach_data) {
251 int (*fn)(__isl_take KEY *key, __isl_take VAL *val, void *user);
252 void *user;
255 /* Call data->fn on a copy of the key and value in *entry.
257 static int call_on_copy(void **entry, void *user)
259 S(pair) *pair = *entry;
260 S(foreach_data) *data = (S(foreach_data) *) user;
262 return data->fn(FN(KEY,copy)(pair->key), FN(VAL,copy)(pair->val),
263 data->user);
266 /* Call "fn" on each pair of key and value in "hmap".
268 int FN(HMAP,foreach)(__isl_keep HMAP *hmap,
269 int (*fn)(__isl_take KEY *key, __isl_take VAL *val, void *user),
270 void *user)
272 S(foreach_data) data = { fn, user };
274 if (!hmap)
275 return -1;
277 return isl_hash_table_foreach(hmap->ctx, &hmap->table,
278 &call_on_copy, &data);
281 /* Internal data structure for print_pair.
283 * p is the printer on which the associative array is being printed.
284 * first is set if the current key-value pair is the first to be printed.
286 S(print_data) {
287 isl_printer *p;
288 int first;
291 /* Print the given key-value pair to data->p.
293 static int print_pair(__isl_take KEY *key, __isl_take VAL *val, void *user)
295 S(print_data) *data = user;
297 if (!data->first)
298 data->p = isl_printer_print_str(data->p, ", ");
299 data->p = FN(isl_printer_print,KEY_BASE)(data->p, key);
300 data->p = isl_printer_print_str(data->p, ": ");
301 data->p = FN(isl_printer_print,VAL_BASE)(data->p, val);
302 data->first = 0;
304 FN(KEY,free)(key);
305 FN(VAL,free)(val);
306 return 0;
309 /* Print the associative array to "p".
311 __isl_give isl_printer *FN(isl_printer_print,HMAP_BASE)(
312 __isl_take isl_printer *p, __isl_keep HMAP *hmap)
314 S(print_data) data;
316 if (!p || !hmap)
317 return isl_printer_free(p);
319 p = isl_printer_print_str(p, "{");
320 data.p = p;
321 data.first = 1;
322 if (FN(HMAP,foreach)(hmap, &print_pair, &data) < 0)
323 data.p = isl_printer_free(data.p);
324 p = data.p;
325 p = isl_printer_print_str(p, "}");
327 return p;
330 void FN(HMAP,dump)(__isl_keep HMAP *hmap)
332 isl_printer *printer;
334 if (!hmap)
335 return;
337 printer = isl_printer_to_file(FN(HMAP,get_ctx)(hmap), stderr);
338 printer = FN(isl_printer_print,HMAP_BASE)(printer, hmap);
339 printer = isl_printer_end_line(printer);
341 isl_printer_free(printer);