Changed source layout into a library structure

This commit is contained in:
XOR 2023-04-07 01:01:35 +02:00
parent abf81e620d
commit 0bf32ede5e
3 changed files with 97 additions and 68 deletions

View file

@ -1,51 +1,6 @@
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <stdbool.h>
#include "shash.h"
#define WORD 32
#define TRANSFORM_TABLE_MAX_RAND 4294967296
#define DELTA 1
// If set to 1, all strings will be hashed to SIMULATED_COLLISON_HASH
#define SIMULATE_COLLISIONS 0
#define SIMULATED_COLLISION_HASH 20
#ifdef __clang__
#include <builtins.h>
#define rot32_left(x, y) __builtin_rotateleft32(x, y)
#else
#define rot32_left(x, y) (x << y) | (x >> 32 - y)
#endif
typedef struct
{
char *key;
unsigned int keylen;
void *data;
// gets set to 1 if another key, collides with this elemnts location
u_int8_t encountered_collision;
// On collision, this field stores, where in the hash table array the second key (with the same hash) is located
int next_key_location;
} shash_table_element_t;
// Contains everything the functions for the hashtables need, to work with, including the hash table itself
typedef struct
{
// stores a transformation table used by the shash_hash function
unsigned int *transformation_table;
shash_table_element_t *hash_table;
unsigned int table_size;
} shash_hashtable_t;
int get_empty_hashtable_slot(shash_hashtable_t *hashtable)
static int get_empty_hashtable_slot(shash_hashtable_t *hashtable)
{
assert(hashtable != NULL);
@ -201,24 +156,3 @@ void shash_destroy_hashtable(shash_hashtable_t *hashtable)
free(hashtable->transformation_table);
free(hashtable->hash_table);
}
int main(void)
{
// Initialize an empty hashtable
shash_hashtable_t hashtable;
shash_init_hashtable(&hashtable, 100);
// Store some data
shash_set("FOO", 3, "Hello", &hashtable);
shash_set("BAR", 3, "World!", &hashtable);
// And retrieve it
char *retrieved_foo = shash_get("FOO", 3, &hashtable);
char *retrieved_bar = shash_get("BAR", 3, &hashtable);
printf("%s, %s\n", retrieved_foo, retrieved_bar);
// Destroy the hashtable
shash_destroy_hashtable(&hashtable);
return 0;
}

68
shash.h Normal file
View file

@ -0,0 +1,68 @@
#ifndef _SHASH_H
#define _SHASH_H
#include <assert.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <stdbool.h>
#define WORD 32
#define TRANSFORM_TABLE_MAX_RAND 4294967296
#define DELTA 1
// If set to 1, all strings will be hashed to SIMULATED_COLLISON_HASH
#define SIMULATE_COLLISIONS 0
#define SIMULATED_COLLISION_HASH 20
#ifdef __clang__
#include <builtins.h>
#define rot32_left(x, y) __builtin_rotateleft32(x, y)
#else
#define rot32_left(x, y) (x << y) | (x >> 32 - y)
#endif
typedef struct
{
char *key;
unsigned int keylen;
void *data;
// gets set to 1 if another key, collides with this elemnts location
u_int8_t encountered_collision;
// On collision, this field stores where in the hash table array the second key (with the same hash) is located
int next_key_location;
} shash_table_element_t;
// Contains everything the functions for the hashtables need, to work with, including the hash table itself
typedef struct
{
// stores a transformation table used by the shash_hash function
unsigned int *transformation_table;
shash_table_element_t *hash_table;
unsigned int table_size;
} shash_hashtable_t;
// Initialize an empty hastable in hashtable with a size of table_size. Returns -1 on failure
int shash_init_hashtable(shash_hashtable_t *hashtable, unsigned int table_size);
// Hash the key key of length len so that it fits in hashtable. Returns the calculated hash
unsigned int shash_hash(char *key, unsigned int len, shash_hashtable_t *hashtable);
/* Store the pointer data in hashtable at the key key of length len.
Returns EXIT_FAILURE when the hashtable is full */
int shash_set(char *key, unsigned int len, void *data, shash_hashtable_t *hashtable);
/* Returns the pointer stored at the key key of length len in hashtable.
Returns ptr to NULL when the key is not found */
void *shash_get(char *key, unsigned int len, shash_hashtable_t *hashtable);
// Free up the space allocated for hashtable. Data inside the hastable is NOT freed
void shash_destroy_hashtable(shash_hashtable_t *hashtable);
#endif

27
usage_example.c Normal file
View file

@ -0,0 +1,27 @@
#include "shash.h"
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Initialize an empty hashtable
shash_hashtable_t hashtable;
if (shash_init_hashtable(&hashtable, 100) == -1)
return EXIT_FAILURE;
// Store some data
if (
shash_set("FOO", 3, "Hello", &hashtable) == EXIT_FAILURE ||
shash_set("BAR", 3, "World!", &hashtable) == EXIT_FAILURE)
return EXIT_FAILURE;
// And retrieve it
char *retrieved_foo = shash_get("FOO", 3, &hashtable);
char *retrieved_bar = shash_get("BAR", 3, &hashtable);
printf("%s, %s\n", retrieved_foo, retrieved_bar);
// Destroy the hashtable
shash_destroy_hashtable(&hashtable);
return 0;
}