1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

remove hash_table

This commit is contained in:
Steffen Vogel 2019-04-07 15:44:53 +02:00
parent 73cb4f8512
commit dc22ec9035
5 changed files with 0 additions and 366 deletions

View file

@ -1,85 +0,0 @@
/** A generic hash table
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLAScommon
*
* 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
* 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/>.
*********************************************************************************/
#pragma once
#include <pthread.h>
#include <stdbool.h>
#include <villas/common.h>
#ifdef __cplusplus
extern "C" {
#endif
struct hash_table_entry {
const void *key;
void *data;
struct hash_table_entry *next;
};
/** A thread-safe hash table using separate chaing with linked lists. */
struct hash_table {
enum state state;
struct hash_table_entry **table;
size_t size;
pthread_mutex_t lock;
};
/** Initialize a new hash table.
*
*/
int hash_table_init(struct hash_table *ht, size_t size);
/** Destroy a hash table.
*
*
*/
int hash_table_destroy(struct hash_table *ht, dtor_cb_t dtor, bool release);
/** Insert a new key/value pair into the hash table.
*
*/
int hash_table_insert(struct hash_table *ht, const void *key, void *data);
/** Delete a key from the hash table.
*
*
*/
int hash_table_delete(struct hash_table *ht, const void *key);
/** Perform a lookup in the hash table.
*
* @retval != NULL The value for the given key.
* @retval NULL The given key is not stored in the hash table.
*/
void * hash_table_lookup(struct hash_table *ht, const void *key);
/** Dump the contents of the hash table in a human readable format to stdout. */
void hash_table_dump(struct hash_table *ht);
#ifdef __cplusplus
}
#endif

View file

@ -27,7 +27,6 @@ add_library(villas-common SHARED
json_buffer.cpp
compat.cpp
crypt.cpp
hash_table.cpp
hist.cpp
kernel/kernel.cpp
kernel/kernel.cpp

View file

@ -1,205 +0,0 @@
/** A generic hash table
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLAScommon
*
* 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
* 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/>.
*********************************************************************************/
#include <string.h>
#include <villas/utils.h>
#include <villas/hash_table.h>
static int hash_table_hash(struct hash_table *ht, const void *key)
{
uintptr_t ptr = (uintptr_t) key;
return ptr % ht->size;
}
int hash_table_init(struct hash_table *ht, size_t size)
{
int ret;
size_t len = sizeof(struct hash_table_entry *) * size;
assert(ht->state == STATE_DESTROYED);
ret = pthread_mutex_init(&ht->lock, NULL);
if (ret)
return ret;
ht->table = (struct hash_table_entry **) alloc(len);
memset(ht->table, 0, len);
ht->size = size;
ht->state = STATE_INITIALIZED;
return 0;
}
int hash_table_destroy(struct hash_table *ht, dtor_cb_t dtor, bool release)
{
int ret;
struct hash_table_entry *cur, *next;
assert(ht->state == STATE_INITIALIZED);
pthread_mutex_lock(&ht->lock);
for (unsigned i = 0; i < ht->size; i++) {
for (cur = ht->table[i]; cur; cur = next) {
if (dtor)
dtor(cur->data);
if (release)
free(cur->data);
next = cur->next;
free(cur);
}
}
pthread_mutex_unlock(&ht->lock);
ret = pthread_mutex_destroy(&ht->lock);
if (ret)
return ret;
free(ht->table);
ht->state = STATE_DESTROYED;
return 0;
}
int hash_table_insert(struct hash_table *ht, const void *key, void *data)
{
int ret, ikey = hash_table_hash(ht, key);
struct hash_table_entry *hte, *cur;
assert(ht->state == STATE_INITIALIZED);
pthread_mutex_lock(&ht->lock);
/* Check that the key is not already in the table */
for (cur = ht->table[ikey];
cur && cur->key != key;
cur = cur->next);
if (cur)
ret = -1;
else {
hte = (struct hash_table_entry *) alloc(sizeof(struct hash_table_entry));
if (hte) {
hte->key = key;
hte->data = data;
hte->next = NULL;
if ((cur = ht->table[ikey]))
hte->next = ht->table[ikey];
ht->table[ikey] = hte;
ret = 0;
}
else
ret = -1;
}
pthread_mutex_unlock(&ht->lock);
return ret;
}
#include <stdio.h>
int hash_table_delete(struct hash_table *ht, const void *key)
{
int ret, ikey = hash_table_hash(ht, key);
struct hash_table_entry *cur, *prev;
assert(ht->state == STATE_INITIALIZED);
pthread_mutex_lock(&ht->lock);
for (prev = NULL,
cur = ht->table[ikey];
cur && cur->key != key;
prev = cur,
cur = cur->next);
if (cur) {
if (prev)
prev->next = cur->next;
else
ht->table[ikey] = cur->next;
free(cur);
ret = 0;
}
else
ret = -1; /* not found */
pthread_mutex_unlock(&ht->lock);
return ret;
}
void * hash_table_lookup(struct hash_table *ht, const void *key)
{
int ikey = hash_table_hash(ht, key);
struct hash_table_entry *hte;
assert(ht->state == STATE_INITIALIZED);
pthread_mutex_lock(&ht->lock);
for (hte = ht->table[ikey];
hte && hte->key != key;
hte = hte->next);
void *data = hte ? hte->data : NULL;
pthread_mutex_unlock(&ht->lock);
return data;
}
void hash_table_dump(struct hash_table *ht)
{
struct hash_table_entry *hte;
assert(ht->state == STATE_INITIALIZED);
pthread_mutex_lock(&ht->lock);
for (unsigned i = 0; i < ht->size; i++) {
char *strlst = NULL;
for (hte = ht->table[i]; hte; hte = hte->next)
strcatf(&strlst, "%p->%p ", hte->key, hte->data);
info("%i: %s", i, strlst);
free(strlst);
}
pthread_mutex_unlock(&ht->lock);
}

View file

@ -25,7 +25,6 @@ add_executable(unit-tests-common
json_buffer.cpp
bitset.cpp
graph.cpp
hash_table.cpp
hist.cpp
kernel.cpp
list.cpp

View file

@ -1,74 +0,0 @@
/** Unit tests for hash table
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLAScommon
*
* 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
* 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/>.
*********************************************************************************/
#include <criterion/criterion.h>
#include <villas/utils.h>
#include <villas/hash_table.h>
static const char *keys[] = { "able", "achieve", "acoustics", "action", "activity", "aftermath", "afternoon", "afterthought", "apparel", "appliance", "beginner", "believe", "bomb", "border", "boundary", "breakfast", "cabbage", "cable", "calculator", "calendar", "caption", "carpenter", "cemetery", "channel", "circle", "creator", "creature", "education", "faucet", "feather", "friction", "fruit", "fuel", "galley", "guide", "guitar", "health", "heart", "idea", "kitten", "laborer", "language" };
static const char *values[] = { "lawyer", "linen", "locket", "lumber", "magic", "minister", "mitten", "money", "mountain", "music", "partner", "passenger", "pickle", "picture", "plantation", "plastic", "pleasure", "pocket", "police", "pollution", "railway", "recess", "reward", "route", "scene", "scent", "squirrel", "stranger", "suit", "sweater", "temper", "territory", "texture", "thread", "treatment", "veil", "vein", "volcano", "wealth", "weather", "wilderness", "wren" };
TestSuite(hash_table, .description = "Hash table datastructure");
Test(hash_table, hash_table_lookup)
{
int ret;
struct hash_table ht;
ht.state = STATE_DESTROYED;
ret = hash_table_init(&ht, 20);
cr_assert(!ret);
/* Insert */
for (unsigned i = 0; i < ARRAY_LEN(keys); i++) {
ret = hash_table_insert(&ht, keys[i], (void *) values[i]);
cr_assert(!ret);
}
/* Lookup */
for (unsigned i = 0; i < ARRAY_LEN(keys); i++) {
char *value = (char *) hash_table_lookup(&ht, keys[i]);
cr_assert_eq(values[i], value);
}
/* Inserting the same key twice should fail */
ret = hash_table_insert(&ht, keys[0], (void *) values[0]);
cr_assert(ret);
hash_table_dump(&ht);
/* Removing an entry */
ret = hash_table_delete(&ht, keys[0]);
cr_assert(!ret);
/* Removing the same entry twice should fail */
ret = hash_table_delete(&ht, keys[0]);
cr_assert(ret);
/* After removing, we should be able to insert it again */
ret = hash_table_insert(&ht, keys[0], (void *) values[0]);
cr_assert(!ret);
ret = hash_table_destroy(&ht, nullptr, false);
cr_assert(!ret);
}