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)
85 uint64_t currentIndex = (index + i) & (
map->capacity - 1);
90 if (forInsertion && firstTombstone !=
ERR)
92 return firstTombstone;
99 if (forInsertion && firstTombstone ==
ERR)
101 firstTombstone = currentIndex;
112 if (forInsertion && firstTombstone !=
ERR)
114 return firstTombstone;
127 if (newCapacity <= map->capacity)
133 if (newEntries ==
NULL)
143 map->entries = newEntries;
144 map->capacity = newCapacity;
148 for (
uint64_t i = 0; i < oldCapacity; i++)
157 map->entries = oldEntries;
158 map->capacity = oldCapacity;
160 map->tombstones = oldTombstones;
163 map->entries[newIndex] = entry;
229 map->entries[index] = entry;
286 return map->capacity;
312 if (minCapacity <= map->capacity)
uint64_t hash_object(const void *object, uint64_t length)
Hash a object.
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.
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.
uint64_t map_init(map_t *map)
Initialize a map.
void map_remove(map_t *map, const map_key_t *key)
Remove a key-value pair from the map.
bool map_is_empty(const map_t *map)
Check if the map is empty.
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 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)
_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]