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,
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
{
20 __isl_give isl_map_to_basic_set
*isl_map_to_basic_set_alloc(isl_ctx
*ctx
,
23 isl_map_to_basic_set
*hmap
;
25 hmap
= isl_calloc_type(ctx
, isl_map_to_basic_set
);
33 if (isl_hash_table_init(ctx
, &hmap
->table
, min_size
) < 0)
34 return isl_map_to_basic_set_free(hmap
);
39 static int free_pair(void **entry
, void *user
)
41 struct isl_map_basic_set_pair
*pair
= *entry
;
42 isl_map_free(pair
->key
);
43 isl_basic_set_free(pair
->val
);
49 void *isl_map_to_basic_set_free(__isl_take isl_map_to_basic_set
*hmap
)
55 isl_hash_table_foreach(hmap
->ctx
, &hmap
->table
, &free_pair
, NULL
);
56 isl_hash_table_clear(&hmap
->table
);
57 isl_ctx_deref(hmap
->ctx
);
62 isl_ctx
*isl_map_to_basic_set_get_ctx(__isl_keep isl_map_to_basic_set
*hmap
)
64 return hmap
? hmap
->ctx
: NULL
;
67 /* Add a mapping from "key" to "val" to the associative array
70 static int add_key_val(__isl_take isl_map
*key
, __isl_take isl_basic_set
*val
,
73 isl_map_to_basic_set
**hmap
= (isl_map_to_basic_set
**) user
;
75 *hmap
= isl_map_to_basic_set_set(*hmap
, key
, val
);
83 __isl_give isl_map_to_basic_set
*isl_map_to_basic_set_dup(
84 __isl_keep isl_map_to_basic_set
*hmap
)
86 isl_map_to_basic_set
*dup
;
91 dup
= isl_map_to_basic_set_alloc(hmap
->ctx
, hmap
->table
.n
);
92 if (isl_map_to_basic_set_foreach(hmap
, &add_key_val
, &dup
) < 0)
93 return isl_map_to_basic_set_free(dup
);
98 __isl_give isl_map_to_basic_set
*isl_map_to_basic_set_cow(
99 __isl_take isl_map_to_basic_set
*hmap
)
107 return isl_map_to_basic_set_dup(hmap
);
110 __isl_give isl_map_to_basic_set
*isl_map_to_basic_set_copy(
111 __isl_keep isl_map_to_basic_set
*hmap
)
120 static int has_key(const void *entry
, const void *key
)
122 const struct isl_map_basic_set_pair
*pair
= entry
;
123 isl_map
*map
= (isl_map
*)key
;
125 return isl_map_plain_is_equal(pair
->key
, map
);
128 int isl_map_to_basic_set_has(__isl_keep isl_map_to_basic_set
*hmap
,
129 __isl_keep isl_map
*key
)
136 hash
= isl_map_get_hash(key
);
137 return !!isl_hash_table_find(hmap
->ctx
, &hmap
->table
, hash
,
141 __isl_give isl_basic_set
*isl_map_to_basic_set_get(
142 __isl_keep isl_map_to_basic_set
*hmap
, __isl_take isl_map
*key
)
144 struct isl_hash_table_entry
*entry
;
145 struct isl_map_basic_set_pair
*pair
;
151 hash
= isl_map_get_hash(key
);
152 entry
= isl_hash_table_find(hmap
->ctx
, &hmap
->table
, hash
,
161 return isl_basic_set_copy(pair
->val
);
167 /* Add a mapping from "key" to "val" to "hmap".
168 * If "key" was already mapped to something else, then that mapping
170 * If key happened to be mapped to "val" already, then we leave
173 __isl_give isl_map_to_basic_set
*isl_map_to_basic_set_set(
174 __isl_take isl_map_to_basic_set
*hmap
,
175 __isl_take isl_map
*key
, __isl_take isl_basic_set
*val
)
177 struct isl_hash_table_entry
*entry
;
178 struct isl_map_basic_set_pair
*pair
;
184 hash
= isl_map_get_hash(key
);
185 entry
= isl_hash_table_find(hmap
->ctx
, &hmap
->table
, hash
,
190 equal
= isl_basic_set_plain_is_equal(pair
->val
, val
);
195 isl_basic_set_free(val
);
200 hmap
= isl_map_to_basic_set_cow(hmap
);
204 entry
= isl_hash_table_find(hmap
->ctx
, &hmap
->table
, hash
,
212 isl_basic_set_free(pair
->val
);
218 pair
= isl_alloc_type(hmap
->ctx
, struct isl_map_basic_set_pair
);
228 isl_basic_set_free(val
);
229 return isl_map_to_basic_set_free(hmap
);
232 /* Internal data structure for isl_map_to_basic_set_foreach.
234 * fn is the function that should be called on each entry.
235 * user is the user-specified final argument to fn.
237 struct isl_map_to_basic_set_foreach_data
{
238 int (*fn
)(__isl_take isl_map
*key
, __isl_take isl_basic_set
*val
,
243 /* Call data->fn on a copy of the key and value in *entry.
245 static int call_on_copy(void **entry
, void *user
)
247 struct isl_map_basic_set_pair
*pair
= *entry
;
248 struct isl_map_to_basic_set_foreach_data
*data
;
249 data
= (struct isl_map_to_basic_set_foreach_data
*) user
;
251 return data
->fn(isl_map_copy(pair
->key
), isl_basic_set_copy(pair
->val
),
255 /* Call "fn" on each pair of key and value in "hmap".
257 int isl_map_to_basic_set_foreach(__isl_keep isl_map_to_basic_set
*hmap
,
258 int (*fn
)(__isl_take isl_map
*key
, __isl_take isl_basic_set
*val
,
259 void *user
), void *user
)
261 struct isl_map_to_basic_set_foreach_data data
= { fn
, user
};
266 return isl_hash_table_foreach(hmap
->ctx
, &hmap
->table
,
267 &call_on_copy
, &data
);
270 /* Internal data structure for print_pair.
272 * p is the printer on which the associative array is being printed.
273 * first is set if the current key-value pair is the first to be printed.
275 struct isl_map_to_basic_set_print_data
{
280 /* Print the given key-value pair to data->p.
282 static int print_pair(__isl_take isl_map
*key
, __isl_take isl_basic_set
*val
,
285 struct isl_map_to_basic_set_print_data
*data
= user
;
288 data
->p
= isl_printer_print_str(data
->p
, ", ");
289 data
->p
= isl_printer_print_map(data
->p
, key
);
290 data
->p
= isl_printer_print_str(data
->p
, ": ");
291 data
->p
= isl_printer_print_basic_set(data
->p
, val
);
295 isl_basic_set_free(val
);
299 /* Print the associative array to "p".
301 __isl_give isl_printer
*isl_printer_print_map_to_basic_set(
302 __isl_take isl_printer
*p
, __isl_keep isl_map_to_basic_set
*hmap
)
304 struct isl_map_to_basic_set_print_data data
;
307 return isl_printer_free(p
);
309 p
= isl_printer_print_str(p
, "{");
312 if (isl_map_to_basic_set_foreach(hmap
, &print_pair
, &data
) < 0)
313 data
.p
= isl_printer_free(data
.p
);
315 p
= isl_printer_print_str(p
, "}");
320 void isl_map_to_basic_set_dump(__isl_keep isl_map_to_basic_set
*hmap
)
322 isl_printer
*printer
;
327 printer
= isl_printer_to_file(isl_map_to_basic_set_get_ctx(hmap
),
329 printer
= isl_printer_print_map_to_basic_set(printer
, hmap
);
330 printer
= isl_printer_end_line(printer
);
332 isl_printer_free(printer
);