isl_map_to_basic_set_free: return NULL
[isl.git] / isl_map_to_basic_set.c
blob253b9520b953467272b1f7d16d5609143c5427d7
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 return (isl_map_to_basic_set *) isl_hash_table_alloc(ctx, min_size);
24 static int free_pair(void **entry, void *user)
26 struct isl_map_basic_set_pair *pair = *entry;
27 isl_map_free(pair->key);
28 isl_basic_set_free(pair->val);
29 free(pair);
30 *entry = NULL;
31 return 0;
34 void *isl_map_to_basic_set_free(isl_ctx *ctx,
35 __isl_take isl_map_to_basic_set *hmap)
37 if (!hmap)
38 return NULL;
39 isl_hash_table_foreach(ctx, &hmap->table, &free_pair, NULL);
40 isl_hash_table_free(ctx, &hmap->table);
41 return NULL;
44 static int has_key(const void *entry, const void *key)
46 const struct isl_map_basic_set_pair *pair = entry;
47 isl_map *map = (isl_map *)key;
49 return isl_map_plain_is_equal(pair->key, map);
52 int isl_map_to_basic_set_has(isl_ctx *ctx,
53 __isl_keep isl_map_to_basic_set *hmap, __isl_keep isl_map *key)
55 uint32_t hash;
57 hash = isl_map_get_hash(key);
58 return !!isl_hash_table_find(ctx, &hmap->table, hash, &has_key, key, 0);
61 __isl_give isl_basic_set *isl_map_to_basic_set_get(isl_ctx *ctx,
62 __isl_keep isl_map_to_basic_set *hmap, __isl_take isl_map *key)
64 struct isl_hash_table_entry *entry;
65 struct isl_map_basic_set_pair *pair;
66 uint32_t hash;
68 hash = isl_map_get_hash(key);
69 entry = isl_hash_table_find(ctx, &hmap->table, hash, &has_key, key, 0);
70 isl_map_free(key);
72 if (!entry)
73 return NULL;
75 pair = entry->data;
77 return isl_basic_set_copy(pair->val);
80 int isl_map_to_basic_set_set(isl_ctx *ctx,
81 __isl_keep isl_map_to_basic_set *hmap, __isl_take isl_map *key,
82 __isl_take isl_basic_set *val)
84 struct isl_hash_table_entry *entry;
85 struct isl_map_basic_set_pair *pair;
86 uint32_t hash;
88 hash = isl_map_get_hash(key);
89 entry = isl_hash_table_find(ctx, &hmap->table, hash, &has_key, key, 1);
91 if (!entry)
92 goto error;
94 if (entry->data) {
95 pair = entry->data;
96 isl_basic_set_free(pair->val);
97 pair->val = val;
98 isl_map_free(key);
99 return 0;
102 pair = isl_alloc_type(ctx, struct isl_map_basic_set_pair);
103 if (!pair)
104 goto error;
106 entry->data = pair;
107 pair->key = key;
108 pair->val = val;
109 return 0;
110 error:
111 isl_map_free(key);
112 isl_basic_set_free(val);
113 return -1;