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
);
32 if (isl_hash_table_init(ctx
, &hmap
->table
, min_size
) < 0)
33 return isl_map_to_basic_set_free(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
);
48 void *isl_map_to_basic_set_free(__isl_take isl_map_to_basic_set
*hmap
)
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
);
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
)
80 hash
= isl_map_get_hash(key
);
81 return !!isl_hash_table_find(hmap
->ctx
, &hmap
->table
, hash
,
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
;
95 hash
= isl_map_get_hash(key
);
96 entry
= isl_hash_table_find(hmap
->ctx
, &hmap
->table
, hash
,
105 return isl_basic_set_copy(pair
->val
);
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
;
121 hash
= isl_map_get_hash(key
);
122 entry
= isl_hash_table_find(hmap
->ctx
, &hmap
->table
, hash
,
130 isl_basic_set_free(pair
->val
);
136 pair
= isl_alloc_type(hmap
->ctx
, struct isl_map_basic_set_pair
);
146 isl_basic_set_free(val
);
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
,
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
),
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
};
184 return isl_hash_table_foreach(hmap
->ctx
, &hmap
->table
,
185 &call_on_copy
, &data
);