34 static item_t *get_bucket_from_hash(
set_t *set,
size_t hash)
36 size_t index = hash % set->size;
37 return &set->items[index];
40 static void clear_items(
set_t *set)
42 for (
size_t i = 0; i < set->size; i++)
44 item_t *item = &set->items[i];
67 static item_t *impl_get_bucket(
set_t *set,
const void *key)
72 size_t hash = info.
hash(key);
73 return get_bucket_from_hash(set, hash);
76 static bool impl_keys_equal(
const set_t *set,
const void *lhs,
const void *rhs)
81 return info.
equals(lhs, rhs);
87 item_t *item = impl_get_bucket(set, key);
91 if (item->
key == NULL)
97 if (impl_keys_equal(set, item->
key, key))
102 if (item->
next != NULL)
124 if (item->
key == NULL)
129 if (impl_keys_equal(set, item->
key, key))
134 if (item->
next != NULL)
150 item_t *item = impl_get_bucket(set, key);
154 if (item->
key == NULL)
159 if (impl_keys_equal(set, item->
key, key))
165 if (item->
next != NULL)
199 for (
size_t i = 0; i < set->size; i++)
201 item_t *item = &set->items[i];
209 if (entry == NULL || entry->
key == NULL)
216 while (entry->
next != NULL)
220 if (entry->
key != NULL)
239 item_t *entry = set_next_in_chain(previous);
247 while (i < set->
size)
249 entry = &set->items[i++];
250 if (entry->
key != NULL)
256 entry = set_next_in_chain(entry);
274 item_t *current = set_find_next_item(set, &index, NULL);
275 item_t *next = set_find_next_item(set, &index, current);
#define STA_FIELD_SIZE(of)
annotate a field as being an array of of elements
#define STA_FIELD_RANGE(lo, hi)
annotate a field as being bounded by the expression of cmp and it STA_FIELD_RANGE(!...
#define STA_DECL
sal2 annotation on function implementations to copy annotations from the declaration
STA_DECL set_t * set_new(size_t size, hash_info_t info, arena_t *arena)
create a new set
STA_DECL bool set_empty(set_t *set)
check if a set is empty
STA_DECL const void * set_add(set_t *set, const void *key)
add a key to a set
STA_DECL void set_reset(set_t *set)
clear all keys from a set
STA_DECL void set_delete(set_t *set, const void *key)
remove a key from a set
STA_DECL set_iter_t set_iter(set_t *set)
acquire a set iterator for a set
STA_DECL const void * set_next(set_iter_t *iter)
get the next item from a set iterator
STA_DECL bool set_contains(const set_t *set, const void *key)
check if a set contains a key
STA_DECL bool set_has_next(set_iter_t *iter)
check if a set iterator has more items
#define ARENA_REPARENT(arena, ptr, parent)
reparent a pointer in a custom allocator
#define ARENA_MALLOC(size, name, parent, arena)
allocate memory from a custom allocator
#define CT_NEVER(...)
assert that a code path is never reached
#define CTASSERT(expr)
assert a condition, prints the condition as a message
information for using a type in a hashset or hashmap
hash_fn_t hash
the hash function for the type
equals_fn_t equals
the equality function for the type
a node in a chain of set entries
const void * key
the key to this bucket
struct item_t * next
the next bucket in the chain
item_t * next
the next item
item_t * current
the current item
size_t index
the current bucket index
set_t * set
the set to iterate over
arena_t * arena
the arena this set is allocated in