1#include <isl_hmap_map_basic_set.h>
2
3struct isl_map_basic_set_pair {
4	isl_map		*key;
5	isl_basic_set	*val;
6};
7
8__isl_give isl_hmap_map_basic_set *isl_hmap_map_basic_set_alloc(isl_ctx *ctx,
9	int min_size)
10{
11	return (isl_hmap_map_basic_set *) isl_hash_table_alloc(ctx, min_size);
12}
13
14static int free_pair(void **entry, void *user)
15{
16	struct isl_map_basic_set_pair *pair = *entry;
17	isl_map_free(pair->key);
18	isl_basic_set_free(pair->val);
19	free(pair);
20	*entry = NULL;
21	return 0;
22}
23
24void isl_hmap_map_basic_set_free(isl_ctx *ctx,
25	__isl_take isl_hmap_map_basic_set *hmap)
26{
27	if (!hmap)
28		return;
29	isl_hash_table_foreach(ctx, &hmap->table, &free_pair, NULL);
30	isl_hash_table_free(ctx, &hmap->table);
31}
32
33static int has_key(const void *entry, const void *key)
34{
35	const struct isl_map_basic_set_pair *pair = entry;
36	isl_map *map = (isl_map *)key;
37
38	return isl_map_plain_is_equal(pair->key, map);
39}
40
41int isl_hmap_map_basic_set_has(isl_ctx *ctx,
42	__isl_keep isl_hmap_map_basic_set *hmap, __isl_keep isl_map *key)
43{
44	uint32_t hash;
45
46	hash = isl_map_get_hash(key);
47	return !!isl_hash_table_find(ctx, &hmap->table, hash, &has_key, key, 0);
48}
49
50__isl_give isl_basic_set *isl_hmap_map_basic_set_get(isl_ctx *ctx,
51	__isl_keep isl_hmap_map_basic_set *hmap, __isl_take isl_map *key)
52{
53	struct isl_hash_table_entry *entry;
54	struct isl_map_basic_set_pair *pair;
55	uint32_t hash;
56
57	hash = isl_map_get_hash(key);
58	entry = isl_hash_table_find(ctx, &hmap->table, hash, &has_key, key, 0);
59	isl_map_free(key);
60
61	if (!entry)
62		return NULL;
63
64	pair = entry->data;
65
66	return isl_basic_set_copy(pair->val);
67}
68
69int isl_hmap_map_basic_set_set(isl_ctx *ctx,
70	__isl_keep isl_hmap_map_basic_set *hmap, __isl_take isl_map *key,
71	__isl_take isl_basic_set *val)
72{
73	struct isl_hash_table_entry *entry;
74	struct isl_map_basic_set_pair *pair;
75	uint32_t hash;
76
77	hash = isl_map_get_hash(key);
78	entry = isl_hash_table_find(ctx, &hmap->table, hash, &has_key, key, 1);
79
80	if (!entry)
81		goto error;
82
83	if (entry->data) {
84		pair = entry->data;
85		isl_basic_set_free(pair->val);
86		pair->val = val;
87		isl_map_free(key);
88		return 0;
89	}
90
91	pair = isl_alloc_type(ctx, struct isl_map_basic_set_pair);
92	if (!pair)
93		goto error;
94
95	entry->data = pair;
96	pair->key = key;
97	pair->val = val;
98	return 0;
99error:
100	isl_map_free(key);
101	isl_basic_set_free(val);
102	return -1;
103}
104