2018-02-16 18:11:26 +01:00
|
|
|
/*
|
|
|
|
* BreakHack - A dungeone crawler RPG
|
|
|
|
* Copyright (C) 2018 Linus Probert <linus.probert@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2017-12-12 13:06:01 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include "hashtable.h"
|
2017-12-19 21:00:02 +01:00
|
|
|
#include "defines.h"
|
2018-01-23 22:54:02 +01:00
|
|
|
#include "util.h"
|
2017-12-12 13:06:01 +01:00
|
|
|
|
|
|
|
Hashtable*
|
|
|
|
ht_create(unsigned int size)
|
|
|
|
{
|
|
|
|
Hashtable *table;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (size < 1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
table = ec_malloc(sizeof(Hashtable));
|
|
|
|
table->size = size;
|
2018-02-22 09:44:27 +01:00
|
|
|
table->entries = ec_malloc((unsigned int) sizeof(Entry) * size);
|
2017-12-12 13:06:01 +01:00
|
|
|
|
|
|
|
for (i = 0; i < size; ++i) {
|
|
|
|
table->entries[i] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
2018-02-01 09:55:12 +01:00
|
|
|
/* D. J. Bernstein hash function */
|
2017-12-12 13:06:01 +01:00
|
|
|
static unsigned int
|
2017-12-13 23:20:54 +01:00
|
|
|
hash(Hashtable *table, const char *key)
|
2017-12-12 13:06:01 +01:00
|
|
|
{
|
2018-02-01 09:55:12 +01:00
|
|
|
unsigned int hash = 5381;
|
|
|
|
|
|
|
|
while (*key) {
|
|
|
|
hash = 33 * hash ^ (unsigned char) *key++;
|
2017-12-12 13:06:01 +01:00
|
|
|
}
|
|
|
|
|
2018-02-01 09:55:12 +01:00
|
|
|
return hash % table->size;
|
2017-12-12 13:06:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Entry*
|
2017-12-13 23:20:54 +01:00
|
|
|
entry_create(const char *key, void *value)
|
2017-12-12 13:06:01 +01:00
|
|
|
{
|
|
|
|
Entry *entry;
|
|
|
|
|
|
|
|
entry = ec_malloc(sizeof(Entry));
|
2017-12-19 21:00:02 +01:00
|
|
|
entry->key = strdup(key);
|
2017-12-12 13:06:01 +01:00
|
|
|
entry->value = value;
|
|
|
|
entry->next = NULL;
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-12-13 23:20:54 +01:00
|
|
|
ht_set(Hashtable *table, const char *key, void *val)
|
2017-12-12 13:06:01 +01:00
|
|
|
{
|
2018-02-22 09:44:27 +01:00
|
|
|
unsigned int hashkey = 0;
|
2017-12-19 19:42:05 +01:00
|
|
|
Entry *newEntry = NULL;
|
2017-12-19 23:02:24 +01:00
|
|
|
Entry *next;
|
2017-12-19 19:42:05 +01:00
|
|
|
Entry *last = NULL;
|
2017-12-12 13:06:01 +01:00
|
|
|
|
2018-02-22 09:44:27 +01:00
|
|
|
hashkey = hash(table, key);
|
2017-12-12 13:06:01 +01:00
|
|
|
|
|
|
|
next = table->entries[hashkey];
|
|
|
|
|
|
|
|
/* Find a position */
|
|
|
|
while (next != NULL
|
|
|
|
&& next->key != NULL
|
|
|
|
&& strcmp(key, next->key) > 0)
|
|
|
|
{
|
|
|
|
last = next;
|
|
|
|
next = next->next;
|
|
|
|
}
|
|
|
|
|
2017-12-19 23:02:24 +01:00
|
|
|
if (last == NULL)
|
|
|
|
last = table->entries[hashkey];
|
|
|
|
|
2017-12-12 13:06:01 +01:00
|
|
|
if (next && next->key && strcmp(key, next->key) == 0) {
|
|
|
|
/* Collision */
|
|
|
|
free(next->value);
|
|
|
|
next->value = val;
|
|
|
|
} else {
|
|
|
|
/* New entry */
|
|
|
|
newEntry = entry_create(key, val);
|
|
|
|
|
2017-12-19 22:51:00 +01:00
|
|
|
if (next == table->entries[hashkey]) {
|
2017-12-12 13:06:01 +01:00
|
|
|
table->entries[hashkey] = newEntry;
|
|
|
|
newEntry->next = next;
|
|
|
|
} else if(next == NULL) {
|
|
|
|
last->next = newEntry;
|
|
|
|
} else {
|
|
|
|
newEntry->next = next;
|
|
|
|
last->next = newEntry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
2017-12-13 23:20:54 +01:00
|
|
|
ht_get(Hashtable *table, const char *key)
|
2017-12-12 13:06:01 +01:00
|
|
|
{
|
2018-02-22 09:44:27 +01:00
|
|
|
unsigned int hashkey = 0;
|
2017-12-12 13:06:01 +01:00
|
|
|
Entry *entry;
|
|
|
|
|
|
|
|
hashkey = hash(table, key);
|
|
|
|
|
|
|
|
entry = table->entries[hashkey];
|
|
|
|
|
2017-12-20 18:56:28 +01:00
|
|
|
while (entry && entry->key && strcmp(entry->key, key) < 0) {
|
2017-12-12 13:06:01 +01:00
|
|
|
entry = entry->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entry || !entry->key || strcmp(entry->key, key) != 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return entry->value;
|
|
|
|
}
|
|
|
|
|
2018-05-06 13:27:29 +02:00
|
|
|
void*
|
|
|
|
ht_remove(Hashtable *table, const char *key)
|
|
|
|
{
|
|
|
|
unsigned int hashkey = 0;
|
|
|
|
Entry *entry, *last;
|
|
|
|
|
|
|
|
hashkey = hash(table, key);
|
|
|
|
|
|
|
|
entry = table->entries[hashkey];
|
|
|
|
last = NULL;
|
|
|
|
|
|
|
|
while (entry && entry->key && strcmp(entry->key, key) < 0) {
|
|
|
|
last = entry;
|
|
|
|
entry = entry->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entry || !entry->key || strcmp(entry->key, key) != 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last == NULL) {
|
|
|
|
table->entries[hashkey] = entry->next;
|
|
|
|
} else {
|
|
|
|
last->next = entry->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *value = entry->value;
|
|
|
|
free(entry->key);
|
|
|
|
free(entry);
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2017-12-12 13:06:01 +01:00
|
|
|
void
|
|
|
|
ht_destroy(Hashtable *table)
|
2017-12-13 14:26:30 +01:00
|
|
|
{
|
|
|
|
ht_destroy_custom(table, free);
|
|
|
|
}
|
|
|
|
|
2018-05-06 13:27:29 +02:00
|
|
|
void
|
|
|
|
ht_foreach(Hashtable *table, void (*func)(void*))
|
|
|
|
{
|
|
|
|
Entry *entry;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (table == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < table->size; ++i) {
|
|
|
|
entry = table->entries[i];
|
|
|
|
if (entry == NULL)
|
|
|
|
continue;
|
|
|
|
while (entry) {
|
|
|
|
func(entry->value);
|
|
|
|
entry = entry->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 14:26:30 +01:00
|
|
|
void
|
2018-01-31 16:55:48 +01:00
|
|
|
ht_destroy_custom(Hashtable *table, void (*destroy_value)(void *))
|
2017-12-12 13:06:01 +01:00
|
|
|
{
|
|
|
|
Entry *entry, *next;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (table == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < table->size; ++i) {
|
|
|
|
entry = table->entries[i];
|
|
|
|
if (entry == NULL)
|
|
|
|
continue;
|
|
|
|
while (entry) {
|
|
|
|
next = entry->next;
|
2017-12-13 14:26:30 +01:00
|
|
|
destroy_value(entry->value);
|
2017-12-12 13:06:01 +01:00
|
|
|
entry->value = NULL;
|
2017-12-13 23:20:54 +01:00
|
|
|
free(entry->key);
|
2017-12-12 13:06:01 +01:00
|
|
|
free(entry);
|
|
|
|
entry = next;
|
|
|
|
}
|
|
|
|
}
|
2017-12-13 14:26:30 +01:00
|
|
|
free(table->entries);
|
2017-12-12 13:06:01 +01:00
|
|
|
free(table);
|
|
|
|
}
|