add isl_map_to_basic_set_foreach
[isl.git] / isl_map_to_basic_set.c
blob157e04b20bbab524611e80332e41f6bf7857448a
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_map_to_basic_set.h>
15 struct isl_map_basic_set_pair {
16 isl_map *key;
17 isl_basic_set *val;
20 __isl_give isl_map_to_basic_set *isl_map_to_basic_set_alloc(isl_ctx *ctx,
21 int min_size)
23 isl_map_to_basic_set *hmap;
25 hmap = isl_calloc_type(ctx, isl_map_to_basic_set);
26 if (!hmap)
27 return NULL;
29 hmap->ctx = ctx;
30 isl_ctx_ref(ctx);
32 if (isl_hash_table_init(ctx, &hmap->table, min_size) < 0)
33 return isl_map_to_basic_set_free(hmap);
35 return hmap;
38 static int free_pair(void **entry, void *user)
40 struct isl_map_basic_set_pair *pair = *entry;
41 isl_map_free(pair->key);
42 isl_basic_set_free(pair->val);
43 free(pair);
44 *entry = NULL;
45 return 0;
48 void *isl_map_to_basic_set_free(__isl_take isl_map_to_basic_set *hmap)
50 if (!hmap)
51 return NULL;
52 isl_hash_table_foreach(hmap->ctx, &hmap->table, &free_pair, NULL);
53 isl_hash_table_clear(&hmap->table);
54 isl_ctx_deref(hmap->ctx);
55 free(hmap);
56 return NULL;
59 isl_ctx *isl_map_to_basic_set_get_ctx(__isl_keep isl_map_to_basic_set *hmap)
61 return hmap ? hmap->ctx : NULL;
64 static int has_key(const void *entry, const void *key)
66 const struct isl_map_basic_set_pair *pair = entry;
67 isl_map *map = (isl_map *)key;
69 return isl_map_plain_is_equal(pair->key, map);
72 int isl_map_to_basic_set_has(__isl_keep isl_map_to_basic_set *hmap,
73 __isl_keep isl_map *key)
75 uint32_t hash;
77 if (!hmap)
78 return -1;
80 hash = isl_map_get_hash(key);
81 return !!isl_hash_table_find(hmap->ctx, &hmap->table, hash,
82 &has_key, key, 0);
85 __isl_give isl_basic_set *isl_map_to_basic_set_get(
86 __isl_keep isl_map_to_basic_set *hmap, __isl_take isl_map *key)
88 struct isl_hash_table_entry *entry;
89 struct isl_map_basic_set_pair *pair;
90 uint32_t hash;
92 if (!hmap || !key)
93 goto error;
95 hash = isl_map_get_hash(key);
96 entry = isl_hash_table_find(hmap->ctx, &hmap->table, hash,
97 &has_key, key, 0);
98 isl_map_free(key);
100 if (!entry)
101 return NULL;
103 pair = entry->data;
105 return isl_basic_set_copy(pair->val);
106 error:
107 isl_map_free(key);
108 return NULL;
111 int isl_map_to_basic_set_set(__isl_keep isl_map_to_basic_set *hmap,
112 __isl_take isl_map *key, __isl_take isl_basic_set *val)
114 struct isl_hash_table_entry *entry;
115 struct isl_map_basic_set_pair *pair;
116 uint32_t hash;
118 if (!hmap)
119 goto error;
121 hash = isl_map_get_hash(key);
122 entry = isl_hash_table_find(hmap->ctx, &hmap->table, hash,
123 &has_key, key, 1);
125 if (!entry)
126 goto error;
128 if (entry->data) {
129 pair = entry->data;
130 isl_basic_set_free(pair->val);
131 pair->val = val;
132 isl_map_free(key);
133 return 0;
136 pair = isl_alloc_type(hmap->ctx, struct isl_map_basic_set_pair);
137 if (!pair)
138 goto error;
140 entry->data = pair;
141 pair->key = key;
142 pair->val = val;
143 return 0;
144 error:
145 isl_map_free(key);
146 isl_basic_set_free(val);
147 return -1;
150 /* Internal data structure for isl_map_to_basic_set_foreach.
152 * fn is the function that should be called on each entry.
153 * user is the user-specified final argument to fn.
155 struct isl_map_to_basic_set_foreach_data {
156 int (*fn)(__isl_take isl_map *key, __isl_take isl_basic_set *val,
157 void *user);
158 void *user;
161 /* Call data->fn on a copy of the key and value in *entry.
163 static int call_on_copy(void **entry, void *user)
165 struct isl_map_basic_set_pair *pair = *entry;
166 struct isl_map_to_basic_set_foreach_data *data;
167 data = (struct isl_map_to_basic_set_foreach_data *) user;
169 return data->fn(isl_map_copy(pair->key), isl_basic_set_copy(pair->val),
170 data->user);
173 /* Call "fn" on each pair of key and value in "hmap".
175 int isl_map_to_basic_set_foreach(__isl_keep isl_map_to_basic_set *hmap,
176 int (*fn)(__isl_take isl_map *key, __isl_take isl_basic_set *val,
177 void *user), void *user)
179 struct isl_map_to_basic_set_foreach_data data = { fn, user };
181 if (!hmap)
182 return -1;
184 return isl_hash_table_foreach(hmap->ctx, &hmap->table,
185 &call_on_copy, &data);