|
@@ -0,0 +1,117 @@
|
|
|
|
|
+#include "../headers/keylime.h"
|
|
|
|
|
+
|
|
|
|
|
+#define KL_HASH_TABLE_DEFAULT_SIZE 32
|
|
|
|
|
+
|
|
|
|
|
+KL_hash_table KL_new_table(){
|
|
|
|
|
+ KL_hash_table table = malloc(sizeof(KL_hash_element*) * KL_HASH_TABLE_DEFAULT_SIZE);
|
|
|
|
|
+
|
|
|
|
|
+ for (int i = 0; i < KL_HASH_TABLE_DEFAULT_SIZE; i++)
|
|
|
|
|
+ table[i] = NULL;
|
|
|
|
|
+
|
|
|
|
|
+ return table;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+int seek(void* operand, void* seek_data){
|
|
|
|
|
+ KL_hash_element* elem = (KL_hash_element*)operand;
|
|
|
|
|
+
|
|
|
|
|
+ iterator_data* data = (iterator_data*)seek_data;
|
|
|
|
|
+
|
|
|
|
|
+ if (strcmp(elem->key, data->key) == 0){
|
|
|
|
|
+ data->exists = 1;
|
|
|
|
|
+ data->value = elem->value;
|
|
|
|
|
+
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ } else
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+int set(void* operand, void* seek_data){
|
|
|
|
|
+ KL_hash_element* elem = (KL_hash_element*)operand;
|
|
|
|
|
+
|
|
|
|
|
+ iterator_data* data = (iterator_data*)seek_data;
|
|
|
|
|
+
|
|
|
|
|
+ if (strcmp(elem->key, data->key) == 0){
|
|
|
|
|
+ data->exists = 1;
|
|
|
|
|
+ elem->value = data->value;
|
|
|
|
|
+
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ } else
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+int del(void* operand, void* seek_data){
|
|
|
|
|
+ KL_hash_element* elem = (KL_hash_element*)operand;
|
|
|
|
|
+
|
|
|
|
|
+ iterator_data* data = (iterator_data*)seek_data;
|
|
|
|
|
+
|
|
|
|
|
+ if (strcmp(elem->key, data->key) == 0){
|
|
|
|
|
+ if (OT_drop(operand) == -1){
|
|
|
|
|
+ data->exists = 1;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ } else
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+int hash_func(char* key){
|
|
|
|
|
+ int summed_key = 0;
|
|
|
|
|
+
|
|
|
|
|
+ for (long unsigned int i = 0; i < strlen(key); i++)
|
|
|
|
|
+ summed_key += key[i];
|
|
|
|
|
+
|
|
|
|
|
+ return summed_key % KL_HASH_TABLE_DEFAULT_SIZE;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void KL_set(KL_hash_table table, char* key, char* value){
|
|
|
|
|
+ KL_hash_element* elem = table[hash_func(key)];
|
|
|
|
|
+
|
|
|
|
|
+ KL_hash_element* new_elem = malloc(sizeof(KL_hash_element));
|
|
|
|
|
+
|
|
|
|
|
+ new_elem->key = key;
|
|
|
|
|
+ new_elem->value = value;
|
|
|
|
|
+
|
|
|
|
|
+ iterator_data data = (iterator_data){0, key, value};
|
|
|
|
|
+
|
|
|
|
|
+ if (elem) {
|
|
|
|
|
+ OT_iterate(elem, &data, set);
|
|
|
|
|
+ } else
|
|
|
|
|
+ table[hash_func(key)] = new_elem;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void KL_add(KL_hash_table table, char* key, char* value){
|
|
|
|
|
+ KL_hash_element* elem = table[hash_func(key)];
|
|
|
|
|
+
|
|
|
|
|
+ iterator_data data = (iterator_data){0, key, NULL};
|
|
|
|
|
+
|
|
|
|
|
+ OT_iterate(elem, &data, seek);
|
|
|
|
|
+
|
|
|
|
|
+ if (elem && data.exists){
|
|
|
|
|
+ return;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ KL_set(table, key, value);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void* KL_get(KL_hash_table table, char* key){
|
|
|
|
|
+ KL_hash_element* elem = table[hash_func(key)];
|
|
|
|
|
+
|
|
|
|
|
+ if (!elem) return NULL;
|
|
|
|
|
+
|
|
|
|
|
+ iterator_data data = (iterator_data){0, key, NULL};
|
|
|
|
|
+
|
|
|
|
|
+ OT_iterate(elem, &data, seek);
|
|
|
|
|
+
|
|
|
|
|
+ return data.value;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void KL_del(KL_hash_table table, char* key){
|
|
|
|
|
+ KL_hash_element* elem = table[hash_func(key)];
|
|
|
|
|
+
|
|
|
|
|
+ iterator_data data = (iterator_data){0, key, NULL};
|
|
|
|
|
+
|
|
|
|
|
+ OT_iterate(elem, &data, del);
|
|
|
|
|
+
|
|
|
|
|
+ if (data.exists == 1)
|
|
|
|
|
+ table[hash_func(key)] = NULL;
|
|
|
|
|
+}
|