summaryrefslogtreecommitdiff
path: root/cloog-0.17.0/isl/isl_hmap_map_basic_set.c
blob: b303516f03109053d4e6ea529c6e03348829b750 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <isl_hmap_map_basic_set.h>

struct isl_map_basic_set_pair {
	isl_map		*key;
	isl_basic_set	*val;
};

__isl_give isl_hmap_map_basic_set *isl_hmap_map_basic_set_alloc(isl_ctx *ctx,
	int min_size)
{
	return (isl_hmap_map_basic_set *) isl_hash_table_alloc(ctx, min_size);
}

static int free_pair(void **entry, void *user)
{
	struct isl_map_basic_set_pair *pair = *entry;
	isl_map_free(pair->key);
	isl_basic_set_free(pair->val);
	free(pair);
	*entry = NULL;
	return 0;
}

void isl_hmap_map_basic_set_free(isl_ctx *ctx,
	__isl_take isl_hmap_map_basic_set *hmap)
{
	if (!hmap)
		return;
	isl_hash_table_foreach(ctx, &hmap->table, &free_pair, NULL);
	isl_hash_table_free(ctx, &hmap->table);
}

static int has_key(const void *entry, const void *key)
{
	const struct isl_map_basic_set_pair *pair = entry;
	isl_map *map = (isl_map *)key;

	return isl_map_plain_is_equal(pair->key, map);
}

int isl_hmap_map_basic_set_has(isl_ctx *ctx,
	__isl_keep isl_hmap_map_basic_set *hmap, __isl_keep isl_map *key)
{
	uint32_t hash;

	hash = isl_map_get_hash(key);
	return !!isl_hash_table_find(ctx, &hmap->table, hash, &has_key, key, 0);
}

__isl_give isl_basic_set *isl_hmap_map_basic_set_get(isl_ctx *ctx,
	__isl_keep isl_hmap_map_basic_set *hmap, __isl_take isl_map *key)
{
	struct isl_hash_table_entry *entry;
	struct isl_map_basic_set_pair *pair;
	uint32_t hash;

	hash = isl_map_get_hash(key);
	entry = isl_hash_table_find(ctx, &hmap->table, hash, &has_key, key, 0);
	isl_map_free(key);

	if (!entry)
		return NULL;

	pair = entry->data;

	return isl_basic_set_copy(pair->val);
}

int isl_hmap_map_basic_set_set(isl_ctx *ctx,
	__isl_keep isl_hmap_map_basic_set *hmap, __isl_take isl_map *key,
	__isl_take isl_basic_set *val)
{
	struct isl_hash_table_entry *entry;
	struct isl_map_basic_set_pair *pair;
	uint32_t hash;

	hash = isl_map_get_hash(key);
	entry = isl_hash_table_find(ctx, &hmap->table, hash, &has_key, key, 1);

	if (!entry)
		return -1;

	if (entry->data) {
		pair = entry->data;
		isl_basic_set_free(pair->val);
		pair->val = val;
		isl_map_free(key);
		return 0;
	}

	pair = isl_alloc_type(ctx, struct isl_map_basic_set_pair);
	if (!pair) {
		isl_map_free(key);
		isl_basic_set_free(val);
		return -1;
	}

	entry->data = pair;
	pair->key = key;
	pair->val = val;
	return 0;
}