2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 #include <isl_hash_private.h>
13 #include "isl_config.h"
15 uint32_t isl_hash_string(uint32_t hash
, const char *s
)
18 isl_hash_byte(hash
, *s
);
22 uint32_t isl_hash_mem(uint32_t hash
, const void *p
, size_t len
)
26 for (i
= 0; i
< len
; ++i
)
27 isl_hash_byte(hash
, s
[i
]);
31 static unsigned int round_up(unsigned int v
)
42 int isl_hash_table_init(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
52 table
->bits
= ffs(round_up(4 * (min_size
+ 1) / 3 - 1)) - 1;
55 size
= 1 << table
->bits
;
56 table
->entries
= isl_calloc_array(ctx
, struct isl_hash_table_entry
,
64 /* Dummy comparison function that always returns false.
66 static int no(const void *entry
, const void *val
)
71 /* Extend "table" to twice its size.
72 * Return 0 on success and -1 on error.
74 * We reuse isl_hash_table_find to create entries in the extended table.
75 * Since all entries in the original table are assumed to be different,
76 * there is no need to compare them against each other.
78 static int grow_table(struct isl_ctx
*ctx
, struct isl_hash_table
*table
)
81 size_t old_size
, size
;
82 struct isl_hash_table_entry
*entries
;
85 entries
= table
->entries
;
86 old_size
= 1 << table
->bits
;
88 table
->entries
= isl_calloc_array(ctx
, struct isl_hash_table_entry
,
90 if (!table
->entries
) {
91 table
->entries
= entries
;
99 for (h
= 0; h
< old_size
; ++h
) {
100 struct isl_hash_table_entry
*entry
;
102 if (!entries
[h
].data
)
105 entry
= isl_hash_table_find(ctx
, table
, entries
[h
].hash
,
109 free(table
->entries
);
110 table
->entries
= entries
;
123 struct isl_hash_table
*isl_hash_table_alloc(struct isl_ctx
*ctx
, int min_size
)
125 struct isl_hash_table
*table
= NULL
;
127 table
= isl_alloc_type(ctx
, struct isl_hash_table
);
128 if (isl_hash_table_init(ctx
, table
, min_size
))
132 isl_hash_table_free(ctx
, table
);
136 void isl_hash_table_clear(struct isl_hash_table
*table
)
140 free(table
->entries
);
143 void isl_hash_table_free(struct isl_ctx
*ctx
, struct isl_hash_table
*table
)
147 isl_hash_table_clear(table
);
151 /* A dummy entry that can be used to make a distinction between
152 * a missing entry and an error condition.
153 * It is used by isl_union_*_find_part_entry.
155 static struct isl_hash_table_entry none
= { 0, NULL
};
156 struct isl_hash_table_entry
*isl_hash_table_entry_none
= &none
;
158 struct isl_hash_table_entry
*isl_hash_table_find(struct isl_ctx
*ctx
,
159 struct isl_hash_table
*table
,
161 int (*eq
)(const void *entry
, const void *val
),
162 const void *val
, int reserve
)
165 uint32_t h
, key_bits
;
167 key_bits
= isl_hash_bits(key_hash
, table
->bits
);
168 size
= 1 << table
->bits
;
169 for (h
= key_bits
; table
->entries
[h
].data
; h
= (h
+1) % size
)
170 if (table
->entries
[h
].hash
== key_hash
&&
171 eq(table
->entries
[h
].data
, val
))
172 return &table
->entries
[h
];
177 if (4 * table
->n
>= 3 * size
) {
178 if (grow_table(ctx
, table
) < 0)
180 return isl_hash_table_find(ctx
, table
, key_hash
, eq
, val
, 1);
184 table
->entries
[h
].hash
= key_hash
;
186 return &table
->entries
[h
];
189 isl_stat
isl_hash_table_foreach(isl_ctx
*ctx
, struct isl_hash_table
*table
,
190 isl_stat (*fn
)(void **entry
, void *user
), void *user
)
196 return isl_stat_error
;
198 size
= 1 << table
->bits
;
199 for (h
= 0; h
< size
; ++ h
)
200 if (table
->entries
[h
].data
&&
201 fn(&table
->entries
[h
].data
, user
) < 0)
202 return isl_stat_error
;
207 void isl_hash_table_remove(struct isl_ctx
*ctx
,
208 struct isl_hash_table
*table
,
209 struct isl_hash_table_entry
*entry
)
214 if (!table
|| !entry
)
217 size
= 1 << table
->bits
;
218 h
= entry
- table
->entries
;
219 isl_assert(ctx
, h
>= 0 && h
< size
, return);
221 for (h2
= h
+1; table
->entries
[h2
% size
].data
; h2
++) {
222 uint32_t bits
= isl_hash_bits(table
->entries
[h2
% size
].hash
,
224 uint32_t offset
= (size
+ bits
- (h
+1)) % size
;
225 if (offset
<= h2
- (h
+1))
227 *entry
= table
->entries
[h2
% size
];
229 entry
= &table
->entries
[h
% size
];