codebased/include/common/stringmap.h
2023-04-25 02:13:20 +02:00

36 lines
1.1 KiB
C

#ifndef COMMON_STRINGMAP_H
#define COMMON_STRINGMAP_H
#include "common/maybe_unused.h"
#include <stdbool.h>
#include <stddef.h>
// https://stackoverflow.com/questions/7666509/hash-function-for-string
// http://www.cse.yorku.ca/~oz/hash.html
MAYBE_UNUSED static inline size_t
common_string_hash_djb2(const unsigned char* value, size_t length)
{
size_t hash = 5381;
for (size_t i = 0; i < length && value[i] != '\0'; ++i)
hash = ((hash << 5) + hash) + value[i];
return hash;
}
typedef struct StringMap StringMap;
StringMap* stringmap_new(void);
void stringmap_delete(StringMap* map);
size_t* stringmap_get(const StringMap* map, const char* key, size_t key_length);
bool stringmap_has(const StringMap* map, const char* key, size_t key_length);
void stringmap_set(
StringMap* map, const char* key, size_t key_length, size_t value
);
void stringmap_reserve(StringMap* map, size_t minimum_size);
void stringmap_remove(StringMap* map, const char* key, size_t key_length);
void stringmap_clean(StringMap* map);
void stringmap_shrink(StringMap* map);
void stringmap_clean_and_shrink(StringMap* map);
size_t stringmap_length(StringMap* map);
#endif