10 return n > 0 && (n & (n - 1)) == 0;
20 if ((n > 0 && (n & (n - 1)) == 0))
44 uint64_t hash = 0xcbf29ce484222325ULL;
45 const uint64_t prime = 0x100000001b3ULL;
47 for (
uint64_t i = 0; i < length; ++i)
86 uint64_t currentIndex = (index + i) & (
map->capacity - 1);
91 if (forInsertion && firstTombstone !=
ERR)
93 return firstTombstone;
100 if (forInsertion && firstTombstone ==
ERR)
102 firstTombstone = currentIndex;
113 if (forInsertion && firstTombstone !=
ERR)
115 return firstTombstone;
128 if (newCapacity <= map->capacity)
134 if (newEntries ==
NULL)
144 map->entries = newEntries;
145 map->capacity = newCapacity;
149 for (
uint64_t i = 0; i < oldCapacity; i++)
158 map->entries = oldEntries;
159 map->capacity = oldCapacity;
161 map->tombstones = oldTombstones;
165 entry->
index = newIndex;
167 map->entries[newIndex] = entry;
238 entry->
index = index;
241 map->entries[index] = entry;
273 else if (
map->entries[index] ==
NULL)
279 entry->
index = index;
282 map->entries[index] = entry;
388 return map->capacity;
420 if (minCapacity < map->length)
#define assert(expression)
uint64_t hash_object(const void *object, uint64_t length)
Hash a object.
void map_init(map_t *map)
Initialize a map.
uint64_t map_capacity(const map_t *map)
Get the capacity of the map.
bool map_contains(map_t *map, const map_key_t *key)
Check if the map contains a key.
void map_clear(map_t *map)
Clear all entries from the map.
void map_deinit(map_t *map)
Deinitialize a map.
#define MAP_MIN_CAPACITY
The minimum capacity of a map.
uint64_t map_replace(map_t *map, const map_key_t *key, map_entry_t *entry)
Replace a key-value pair in the map.
void map_entry_init(map_entry_t *entry)
Initialize a map entry.
#define MAP_MAX_LOAD_PERCENTAGE
The maximum load percentage of a map before it resizes.
#define MAP_TOMBSTONE
The value used to indicate a tombstone (removed entry).
uint64_t map_size(const map_t *map)
Get the number of entries in the map.
uint64_t map_insert(map_t *map, const map_key_t *key, map_entry_t *entry)
Insert a key-value pair into the map.
map_entry_t * map_get_and_remove(map_t *map, const map_key_t *key)
Get and remove a key-value pair from the map.
void map_remove(map_t *map, map_entry_t *entry)
Remove a entry from the map.
void map_remove_key(map_t *map, const map_key_t *key)
Remove a key-value pair from the map by key.
bool map_is_empty(const map_t *map)
Check if the map is empty.
#define MAP_MIN_LOAD_PERCENTAGE
The minimum load percentage of a map before it resizes down.
map_entry_t * map_get(map_t *map, const map_key_t *key)
Get a value from the map by key.
uint64_t map_reserve(map_t *map, uint64_t minCapacity)
Reserve space in the map for at least minCapacity entries.
#define EEXIST
File exists.
#define EINVAL
Invalid argument.
#define ENOMEM
Out of memory.
#define errno
Error number variable.
#define NULL
Pointer error value.
#define ERR
Integer error value.
static uint64_t next_power_of_two(uint64_t n)
static uint64_t map_resize(map_t *map, uint64_t newCapacity)
static void map_remove_index(map_t *map, uint64_t index)
static uint64_t map_find_slot(const map_t *map, const map_key_t *key, bool forInsertion)
static bool is_power_of_two(uint64_t n)
bool map_key_is_equal(const map_key_t *a, const map_key_t *b)
static uint64_t map_resize_check(map_t *map)
_PUBLIC void * calloc(size_t nmemb, size_t size)
_PUBLIC void free(void *ptr)
_PUBLIC int memcmp(const void *s1, const void *s2, size_t n)
_PUBLIC void * memset(void *s, int c, size_t n)
uint8_t key[MAP_KEY_MAX_LENGTH]