Add hashing function, hashtable struct, and hashtable init function

This commit is contained in:
XOR 2023-03-29 15:37:57 +02:00
parent 2661d781da
commit 21dcb50d79
3 changed files with 93 additions and 1 deletions

15
.clang-format Executable file
View file

@ -0,0 +1,15 @@
#BasedOnStyle: None
AlignTrailingComments: true
BreakBeforeBraces: Allman
ColumnLimit: 0
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: false
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PointerBindsToType: false
SpacesBeforeTrailingComments: 1
TabWidth: 4
UseTab: Never
AlignArrayOfStructures: Right
SpaceBeforeSquareBrackets: false

15
.gitignore vendored
View file

@ -36,7 +36,8 @@
*.i*86
*.x86_64
*.hex
eipaasm
eipaasm_debug
# Debug files
*.dSYM/
*.su
@ -52,3 +53,15 @@ Module.symvers
Mkfile.old
dkms.conf
#EIPA specific
*.eipa
!test*.eipa
*.eipaimg
#Code editor
.vscode/*
compile_commands.json
.cache/*
#Code analysis
softwipe*.*

64
main.c Normal file
View file

@ -0,0 +1,64 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
#include <builtins.h>
#define WORD 32
#define RAND_MAX 4294967296
#define DELTA 1
#define rot32_left(x, y) __builtin_rotateleft32(x, y)
// Contains everything the functions for the hashtables need, to work with, including the hash table itself
typedef struct
{
unsigned int *transformation_table;
void *hash_table;
unsigned int table_size;
}shash_hashtable_t;
void shash_init_hashtable(shash_hashtable_t *hashtable, unsigned int table_size)
{
// Create a random transformation table
srand(time(NULL));
unsigned int *table = malloc((CHAR_MAX-CHAR_MIN)*sizeof(int));
for(int i = 0; i < CHAR_MAX-CHAR_MIN; i++){
table[i] = rand();
}
hashtable->transformation_table = table;
hashtable->hash_table = malloc(table_size * sizeof(void *));
hashtable->table_size = table_size;
}
unsigned int shash_hash(char *key, unsigned int len, shash_hashtable_t *hashtable)
{
//Slight variation of cyclic polynomial hasing, as described in the Paper: "Recursive Hashing functions for n-Grams" by J. D. Cohen
unsigned int hash_word = 0;
for(int i = 0; i < len; i++)
{
hash_word = rot32_left(hash_word, DELTA);
hash_word = hash_word ^ hashtable->transformation_table[key[i]];
}
return hash_word % hashtable->table_size;
}
void shash_set(char *key, unsigned int len, shash_hashtable_t *hashtable)
{
//unsigned int hash = shash_hash(key, len, 1000,[1,]);
//printf("%u\n", hash);
}
int main(void)
{
// Initialize an empty hashtable
shash_hashtable_t hashtable;
shash_init_hashtable(&hashtable, 100);
printf("%d\n", shash_hash("INC",3,&hashtable));
return 0;
}