isl_map_to_basic_set: keep track of isl_ctx
[isl.git] / isl_map_to_basic_set.c
blob3975fe4b7721ed5c1445f1bc92f3751d853a75a1
1 /*
2 * Copyright 2011 INRIA Saclay
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8 * 91893 Orsay, France
9 */
11 #include <isl_map_to_basic_set.h>
13 struct isl_map_basic_set_pair {
14 isl_map *key;
15 isl_basic_set *val;
18 __isl_give isl_map_to_basic_set *isl_map_to_basic_set_alloc(isl_ctx *ctx,
19 int min_size)
21 isl_map_to_basic_set *hmap;
23 hmap = isl_calloc_type(ctx, isl_map_to_basic_set);
24 if (!hmap)
25 return NULL;
27 hmap->ctx = ctx;
28 isl_ctx_ref(ctx);
30 if (isl_hash_table_init(ctx, &hmap->table, min_size) < 0)
31 return isl_map_to_basic_set_free(hmap);
33 return hmap;
36 static int free_pair(void **entry, void *user)
38 struct isl_map_basic_set_pair *pair = *entry;
39 isl_map_free(pair->key);
40 isl_basic_set_free(pair->val);
41 free(pair);
42 *entry = NULL;
43 return 0;
46 void *isl_map_to_basic_set_free(__isl_take isl_map_to_basic_set *hmap)
48 if (!hmap)
49 return NULL;
50 isl_hash_table_foreach(hmap->ctx, &hmap->table, &free_pair, NULL);
51 isl_hash_table_clear(&hmap->table);
52 isl_ctx_deref(hmap->ctx);
53 free(hmap);
54 return NULL;
57 isl_ctx *isl_map_to_basic_set_get_ctx(__isl_keep isl_map_to_basic_set *hmap)
59 return hmap ? hmap->ctx : NULL;
62 static int has_key(const void *entry, const void *key)
64 const struct isl_map_basic_set_pair *pair = entry;
65 isl_map *map = (isl_map *)key;
67 return isl_map_plain_is_equal(pair->key, map);
70 int isl_map_to_basic_set_has(__isl_keep isl_map_to_basic_set *hmap,
71 __isl_keep isl_map *key)
73 uint32_t hash;
75 if (!hmap)
76 return -1;
78 hash = isl_map_get_hash(key);
79 return !!isl_hash_table_find(hmap->ctx, &hmap->table, hash,
80 &has_key, key, 0);
83 __isl_give isl_basic_set *isl_map_to_basic_set_get(
84 __isl_keep isl_map_to_basic_set *hmap, __isl_take isl_map *key)
86 struct isl_hash_table_entry *entry;
87 struct isl_map_basic_set_pair *pair;
88 uint32_t hash;
90 if (!hmap || !key)
91 goto error;
93 hash = isl_map_get_hash(key);
94 entry = isl_hash_table_find(hmap->ctx, &hmap->table, hash,
95 &has_key, key, 0);
96 isl_map_free(key);
98 if (!entry)
99 return NULL;
101 pair = entry->data;
103 return isl_basic_set_copy(pair->val);
104 error:
105 isl_map_free(key);
106 return NULL;
109 int isl_map_to_basic_set_set(__isl_keep isl_map_to_basic_set *hmap,
110 __isl_take isl_map *key, __isl_take isl_basic_set *val)
112 struct isl_hash_table_entry *entry;
113 struct isl_map_basic_set_pair *pair;
114 uint32_t hash;
116 if (!hmap)
117 goto error;
119 hash = isl_map_get_hash(key);
120 entry = isl_hash_table_find(hmap->ctx, &hmap->table, hash,
121 &has_key, key, 1);
123 if (!entry)
124 goto error;
126 if (entry->data) {
127 pair = entry->data;
128 isl_basic_set_free(pair->val);
129 pair->val = val;
130 isl_map_free(key);
131 return 0;
134 pair = isl_alloc_type(hmap->ctx, struct isl_map_basic_set_pair);
135 if (!pair)
136 goto error;
138 entry->data = pair;
139 pair->key = key;
140 pair->val = val;
141 return 0;
142 error:
143 isl_map_free(key);
144 isl_basic_set_free(val);
145 return -1;