2022-07-28 15:15:04 +00:00
|
|
|
/*
|
2022-12-26 15:52:52 +00:00
|
|
|
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
2022-07-28 15:15:04 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person
|
|
|
|
* obtaining a copy of this software and associated documentation files
|
|
|
|
* (the "Software"), to deal in the Software without restriction,
|
|
|
|
* including without limitation the rights to use, copy, modify, merge,
|
|
|
|
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
* and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
2022-07-28 16:00:52 +00:00
|
|
|
* included in all copies or portions of the Software.
|
2022-07-28 15:15:04 +00:00
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
2022-07-23 00:19:12 +00:00
|
|
|
#include <HashMap.h>
|
|
|
|
|
2022-10-13 16:18:42 +00:00
|
|
|
#include <Memory.h>
|
|
|
|
|
2022-07-23 00:19:12 +00:00
|
|
|
#include <stddef.h>
|
2022-10-13 16:18:42 +00:00
|
|
|
#include <string.h>
|
2022-07-23 00:19:12 +00:00
|
|
|
|
2022-07-25 19:18:35 +00:00
|
|
|
typedef struct HashMapBucket
|
|
|
|
{
|
2022-07-27 13:47:22 +00:00
|
|
|
unsigned long hash;
|
2022-07-25 20:26:30 +00:00
|
|
|
char *key;
|
2022-07-23 00:19:12 +00:00
|
|
|
void *value;
|
|
|
|
} HashMapBucket;
|
|
|
|
|
2022-07-25 19:18:35 +00:00
|
|
|
struct HashMap
|
|
|
|
{
|
2022-07-23 00:19:12 +00:00
|
|
|
size_t count;
|
|
|
|
size_t capacity;
|
|
|
|
HashMapBucket **entries;
|
|
|
|
|
2022-10-13 13:09:26 +00:00
|
|
|
unsigned long (*hashFunc) (const char *);
|
2022-10-11 13:27:34 +00:00
|
|
|
|
2022-07-23 00:19:12 +00:00
|
|
|
float maxLoad;
|
2022-07-26 01:31:09 +00:00
|
|
|
size_t iterator;
|
2022-07-23 00:19:12 +00:00
|
|
|
};
|
|
|
|
|
2022-07-27 13:47:22 +00:00
|
|
|
static unsigned long
|
2022-07-23 00:19:12 +00:00
|
|
|
HashMapHashKey(const char *key)
|
|
|
|
{
|
2022-07-27 13:47:22 +00:00
|
|
|
unsigned long hash = 2166136261u;
|
2022-07-23 00:19:12 +00:00
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
while (key[i])
|
|
|
|
{
|
2022-07-27 13:47:22 +00:00
|
|
|
hash ^= (unsigned char) key[i];
|
2022-07-23 00:19:12 +00:00
|
|
|
hash *= 16777619;
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2022-07-25 19:18:35 +00:00
|
|
|
HashMapGrow(HashMap * map)
|
2022-07-23 00:19:12 +00:00
|
|
|
{
|
|
|
|
size_t oldCapacity;
|
|
|
|
size_t i;
|
|
|
|
HashMapBucket **newEntries;
|
|
|
|
|
|
|
|
if (!map)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
oldCapacity = map->capacity;
|
|
|
|
map->capacity *= 2;
|
|
|
|
|
2022-10-13 16:18:42 +00:00
|
|
|
newEntries = Malloc(map->capacity * sizeof(HashMapBucket *));
|
2022-07-23 00:19:12 +00:00
|
|
|
if (!newEntries)
|
|
|
|
{
|
2022-10-31 15:07:32 +00:00
|
|
|
map->capacity /= 2;
|
2022-07-23 00:19:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-11-08 01:05:28 +00:00
|
|
|
memset(newEntries, 0, map->capacity * sizeof(HashMapBucket *));
|
2022-10-13 16:18:42 +00:00
|
|
|
|
2022-07-23 00:19:12 +00:00
|
|
|
for (i = 0; i < oldCapacity; i++)
|
|
|
|
{
|
|
|
|
/* If there is a value here, and it isn't a tombstone */
|
|
|
|
if (map->entries[i] && map->entries[i]->hash)
|
|
|
|
{
|
|
|
|
/* Copy it to the new entries array */
|
|
|
|
size_t index = map->entries[i]->hash % map->capacity;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (newEntries[index])
|
|
|
|
{
|
|
|
|
if (!newEntries[index]->hash)
|
|
|
|
{
|
2022-10-13 16:18:42 +00:00
|
|
|
Free(newEntries[index]);
|
2022-07-23 00:19:12 +00:00
|
|
|
newEntries[index] = map->entries[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newEntries[index] = map->entries[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
index = (index + 1) % map->capacity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Either NULL or a tombstone */
|
2022-10-13 16:18:42 +00:00
|
|
|
Free(map->entries[i]);
|
2022-07-23 00:19:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-13 16:18:42 +00:00
|
|
|
Free(map->entries);
|
2022-07-23 00:19:12 +00:00
|
|
|
map->entries = newEntries;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
HashMap *
|
|
|
|
HashMapCreate(void)
|
|
|
|
{
|
2022-10-13 16:18:42 +00:00
|
|
|
HashMap *map = Malloc(sizeof(HashMap));
|
2022-07-25 19:18:35 +00:00
|
|
|
|
2022-07-23 00:19:12 +00:00
|
|
|
if (!map)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
map->maxLoad = 0.75;
|
|
|
|
map->count = 0;
|
|
|
|
map->capacity = 16;
|
2022-07-26 01:31:09 +00:00
|
|
|
map->iterator = 0;
|
2022-10-13 13:09:26 +00:00
|
|
|
map->hashFunc = HashMapHashKey;
|
2022-07-23 00:19:12 +00:00
|
|
|
|
2022-10-13 16:18:42 +00:00
|
|
|
map->entries = Malloc(map->capacity * sizeof(HashMapBucket *));
|
2022-07-23 00:19:12 +00:00
|
|
|
if (!map->entries)
|
|
|
|
{
|
2022-10-13 16:18:42 +00:00
|
|
|
Free(map);
|
2022-07-23 00:19:12 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-10-13 16:18:42 +00:00
|
|
|
memset(map->entries, 0, map->capacity * sizeof(HashMapBucket *));
|
|
|
|
|
2022-07-23 00:19:12 +00:00
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2022-07-25 19:18:35 +00:00
|
|
|
HashMapDelete(HashMap * map, const char *key)
|
2022-07-23 00:19:12 +00:00
|
|
|
{
|
2022-07-27 13:47:22 +00:00
|
|
|
unsigned long hash;
|
2022-07-23 00:19:12 +00:00
|
|
|
size_t index;
|
|
|
|
|
|
|
|
if (!map || !key)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-10-11 13:27:34 +00:00
|
|
|
hash = map->hashFunc(key);
|
2022-07-23 00:19:12 +00:00
|
|
|
index = hash % map->capacity;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
HashMapBucket *bucket = map->entries[index];
|
|
|
|
|
|
|
|
if (!bucket)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bucket->hash == hash)
|
|
|
|
{
|
|
|
|
bucket->hash = 0;
|
|
|
|
return bucket->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
index = (index + 1) % map->capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-07-25 19:18:35 +00:00
|
|
|
HashMapFree(HashMap * map)
|
2022-07-23 00:19:12 +00:00
|
|
|
{
|
|
|
|
if (map)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < map->capacity; i++)
|
|
|
|
{
|
|
|
|
if (map->entries[i])
|
|
|
|
{
|
2022-10-13 16:18:42 +00:00
|
|
|
Free(map->entries[i]);
|
2022-07-23 00:19:12 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-13 16:18:42 +00:00
|
|
|
Free(map->entries);
|
|
|
|
Free(map);
|
2022-07-23 00:19:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2022-07-25 19:18:35 +00:00
|
|
|
HashMapGet(HashMap * map, const char *key)
|
2022-07-23 00:19:12 +00:00
|
|
|
{
|
2022-07-27 13:47:22 +00:00
|
|
|
unsigned long hash;
|
2022-07-23 00:19:12 +00:00
|
|
|
size_t index;
|
|
|
|
|
|
|
|
if (!map || !key)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-10-11 13:27:34 +00:00
|
|
|
hash = map->hashFunc(key);
|
2022-07-23 00:19:12 +00:00
|
|
|
index = hash % map->capacity;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
HashMapBucket *bucket = map->entries[index];
|
|
|
|
|
|
|
|
if (!bucket)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bucket->hash == hash)
|
|
|
|
{
|
|
|
|
return bucket->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
index = (index + 1) % map->capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-02-17 03:14:43 +00:00
|
|
|
void *
|
|
|
|
HashMapGetKey(HashMap * map, const char *key)
|
|
|
|
{
|
|
|
|
unsigned long hash;
|
|
|
|
size_t index;
|
|
|
|
|
|
|
|
if (!map || !key)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
hash = map->hashFunc(key);
|
|
|
|
index = hash % map->capacity;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
HashMapBucket *bucket = map->entries[index];
|
|
|
|
|
|
|
|
if (!bucket)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bucket->hash == hash)
|
|
|
|
{
|
|
|
|
return bucket->key;
|
|
|
|
}
|
|
|
|
|
|
|
|
index = (index + 1) % map->capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-07-26 01:31:09 +00:00
|
|
|
int
|
|
|
|
HashMapIterate(HashMap * map, char **key, void **value)
|
2022-07-23 00:19:12 +00:00
|
|
|
{
|
|
|
|
if (!map)
|
|
|
|
{
|
2022-07-26 01:31:09 +00:00
|
|
|
return 0;
|
2022-07-23 00:19:12 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 01:31:09 +00:00
|
|
|
if (map->iterator >= map->capacity)
|
2022-07-23 00:19:12 +00:00
|
|
|
{
|
2022-07-26 01:31:09 +00:00
|
|
|
map->iterator = 0;
|
|
|
|
*key = NULL;
|
|
|
|
*value = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (map->iterator < map->capacity)
|
|
|
|
{
|
|
|
|
HashMapBucket *bucket = map->entries[map->iterator];
|
|
|
|
|
|
|
|
map->iterator++;
|
2022-07-23 00:19:12 +00:00
|
|
|
|
2023-02-11 00:15:49 +00:00
|
|
|
if (bucket && bucket->hash)
|
2022-07-23 00:19:12 +00:00
|
|
|
{
|
2022-07-26 01:31:09 +00:00
|
|
|
*key = bucket->key;
|
|
|
|
*value = bucket->value;
|
|
|
|
return 1;
|
2022-07-23 00:19:12 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-26 01:31:09 +00:00
|
|
|
|
2022-07-26 02:55:22 +00:00
|
|
|
map->iterator = 0;
|
2022-07-26 01:31:09 +00:00
|
|
|
return 0;
|
2022-07-23 00:19:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-07-25 19:18:35 +00:00
|
|
|
HashMapMaxLoadSet(HashMap * map, float load)
|
2022-07-23 00:19:12 +00:00
|
|
|
{
|
2022-07-29 16:32:46 +00:00
|
|
|
if (!map || (load > 1.0 || load <= 0))
|
2022-07-23 00:19:12 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
map->maxLoad = load;
|
|
|
|
}
|
|
|
|
|
2022-10-11 13:27:34 +00:00
|
|
|
void
|
|
|
|
HashMapFunctionSet(HashMap * map, unsigned long (*hashFunc) (const char *))
|
|
|
|
{
|
2022-10-13 13:09:26 +00:00
|
|
|
if (!map || !hashFunc)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2022-10-11 13:27:34 +00:00
|
|
|
|
2022-10-13 13:09:26 +00:00
|
|
|
map->hashFunc = hashFunc;
|
2022-10-11 13:27:34 +00:00
|
|
|
}
|
2022-07-23 00:19:12 +00:00
|
|
|
|
|
|
|
void *
|
2022-07-25 20:26:30 +00:00
|
|
|
HashMapSet(HashMap * map, char *key, void *value)
|
2022-07-23 00:19:12 +00:00
|
|
|
{
|
2022-07-27 13:47:22 +00:00
|
|
|
unsigned long hash;
|
2022-07-23 00:19:12 +00:00
|
|
|
size_t index;
|
|
|
|
|
|
|
|
if (!map || !key || !value)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (map->count + 1 > map->capacity * map->maxLoad)
|
|
|
|
{
|
|
|
|
HashMapGrow(map);
|
|
|
|
}
|
|
|
|
|
2022-10-11 13:27:34 +00:00
|
|
|
hash = map->hashFunc(key);
|
2022-07-23 00:19:12 +00:00
|
|
|
index = hash % map->capacity;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
HashMapBucket *bucket = map->entries[index];
|
|
|
|
|
|
|
|
if (!bucket)
|
|
|
|
{
|
2022-10-13 16:18:42 +00:00
|
|
|
bucket = Malloc(sizeof(HashMapBucket));
|
2022-07-23 00:19:12 +00:00
|
|
|
if (!bucket)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket->hash = hash;
|
2022-07-25 20:26:30 +00:00
|
|
|
bucket->key = key;
|
2022-07-23 00:19:12 +00:00
|
|
|
bucket->value = value;
|
|
|
|
map->entries[index] = bucket;
|
|
|
|
map->count++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bucket->hash)
|
|
|
|
{
|
|
|
|
bucket->hash = hash;
|
2022-07-25 20:26:30 +00:00
|
|
|
bucket->key = key;
|
2022-07-23 00:19:12 +00:00
|
|
|
bucket->value = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bucket->hash == hash)
|
|
|
|
{
|
|
|
|
void *oldValue = bucket->value;
|
2022-07-25 19:18:35 +00:00
|
|
|
|
2022-07-23 00:19:12 +00:00
|
|
|
bucket->value = value;
|
|
|
|
return oldValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
index = (index + 1) % map->capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-07-26 01:31:09 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
HashMapIterateFree(char *key, void *value)
|
|
|
|
{
|
|
|
|
if (key)
|
|
|
|
{
|
2022-10-13 16:18:42 +00:00
|
|
|
Free(key);
|
2022-07-26 01:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
{
|
2022-10-13 16:18:42 +00:00
|
|
|
Free(value);
|
2022-07-26 01:31:09 +00:00
|
|
|
}
|
|
|
|
}
|