2023-04-13 23:05:54 +02:00
// This file contains the definitions of the public functions used by the shash-hashtable library and is part of the shash library
// Copyright (C) 2023 theboring_XOR
// 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/>.
# ifndef _SHASH_H
# define _SHASH_H
2023-09-30 14:10:43 +02:00
# include <stdint.h>
2023-04-13 23:05:54 +02:00
# 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 ;
2023-09-30 14:10:43 +02:00
2023-04-13 23:05:54 +02:00
void * data ;
// gets set to 1 if another key, collides with this elemnts location
2023-09-30 14:10:43 +02:00
uint8_t encountered_collision ;
2023-04-13 23:05:54 +02:00
// 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.
2023-09-30 14:10:43 +02:00
Returns ptr to NULL when the key is not found */
2023-04-13 23:05:54 +02:00
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 ) ;
2023-09-30 14:10:43 +02:00
# endif