Browse Source

first commit

Iver 1 month ago
commit
146c77f7ca

+ 16 - 0
Makefile

@@ -0,0 +1,16 @@
+COMPILER=gcc
+FLAGS_ALL=-g -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter
+FLAGS_EXAMPLE=-Lbuilds/ -lkeylime -lorangetree -Wl,-rpath=builds/ -lm
+FLAGS_LIB=-fPIC -shared -lc -lm 
+
+main.bin: libkeylime.so liborangetree.so
+	$(COMPILER) $(FLAGS_ALL) src/launch\ program/main.c -o builds/main.bin $(FLAGS_EXAMPLE) 
+
+libkeylime.so:
+	$(COMPILER) $(FLAGS_ALL) src/main/main.c -o builds/libkeylime.so $(FLAGS_LIB) 
+
+liborangetree.so:
+	cp src/dependencies/liborangetree.so builds/
+
+clean:
+	rm builds/*

+ 1 - 0
builds/.txt

@@ -0,0 +1 @@
+this is a file so that git commits this folder

BIN
builds/NAMEHERE.so


BIN
builds/libkeylime.so


BIN
builds/liborangetree.so


BIN
builds/main.bin


+ 4 - 0
changelog.txt

@@ -0,0 +1,4 @@
+-made file tree
+-made functions
+-fixed makefile
+-made example

+ 1 - 0
dbg

@@ -0,0 +1 @@
+gdb -ex run builds/main.bin

+ 0 - 0
issues.txt


+ 7 - 0
readme.md

@@ -0,0 +1,7 @@
+# KeyLime, A Hash Map Manager
+
+## Etymology
+
+ * I think this one is pretty obvious
+ * Key Lime, like how a hash map has keys
+

+ 1 - 0
run

@@ -0,0 +1 @@
+./builds/main.bin

BIN
src/dependencies/liborangetree.so


+ 14 - 0
src/headers/KL_functions.h

@@ -0,0 +1,14 @@
+#ifndef KL_FUNCTIONS_H
+#define KL_FUNCTIONS_H
+
+KL_hash_table KL_new_table();
+
+void KL_set(KL_hash_table table, char* key, char* value);
+
+void KL_add(KL_hash_table table, char* key, char* value);
+
+void* KL_get(KL_hash_table table, char* key);
+
+void KL_del(KL_hash_table table, char* key);
+
+#endif

+ 19 - 0
src/headers/KL_types.h

@@ -0,0 +1,19 @@
+#ifndef KL_TYPES_H
+#define KL_TYPES_H
+
+typedef struct {
+    void* OT_next;
+    void* OT_prev;
+    char* key;
+    void* value;
+} KL_hash_element; 
+
+typedef struct {
+    unsigned char exists;
+    char* key;
+    void* value;
+} iterator_data;
+
+typedef KL_hash_element** KL_hash_table;
+
+#endif

+ 48 - 0
src/headers/OT_functions.h

@@ -0,0 +1,48 @@
+#ifndef OT_FUNCTIONS_H
+#define OT_FUNCTIONS_H
+
+// - Returns the last element of a linked list
+// - Takes in any element of the list
+void* OT_get_last(void* operand);
+
+// - Returns the first element of a linked list
+// - Takes in any element of the list
+void* OT_get_first(void* operand);
+
+// - Removes an element from the linked list
+// - Takes in specific element of the list
+// - Returns -1 if the element was the last in the linked list
+int OT_drop(void* operand);
+
+// - Removes an element from the linked list and frees it
+// - Takes in specific element of the list
+// - Returns -1 if the element was the last in the linked list
+int OT_erase(void* operand);
+
+// - Adds an element to the end of a linked list
+// - Takes in any element of the list and an element to add to the list
+void OT_append(void* operand, void* appendee);
+
+// - Adds and element to the beginning of a linked list
+// - Takes in any element of the list and an element to add to the list
+void OT_prepend(void* operand, void* prependee);
+
+// - Adds an element to the left of an element in a linked list
+// - Takes in specific element of the list and an element to add to the list
+void OT_insert_left(void* operand, void* insertee);
+
+// - Adds an element to the right of an element in a linked list
+// - Takes in specific element of the list and an element to add to the list
+void OT_insert_right(void* operand, void* insertee);
+
+// - Frees entire list
+// - Takes in any element of the list
+void OT_free(void* operand);
+
+// - Runs `(*user_function)` for every element in the linked list
+// - Takes in any element of the list, a pointer that will be passed into the `user_function`, and `user_function`
+// - `user_function` must be in the form `int func(void* operand, void* user_pointer)`
+// - if 'user_function' returns -1, the loop will break
+void OT_iterate(void* operand, void* user_pointer, int (*user_function)(void*, void*));
+
+#endif

+ 9 - 0
src/headers/OT_types.h

@@ -0,0 +1,9 @@
+#ifndef OT_TYPES_H
+#define OT_TYPES_H
+
+struct OT_operand {
+    void* OT_next;
+    void* OT_prev;
+};
+
+#endif

+ 10 - 0
src/headers/keylime.h

@@ -0,0 +1,10 @@
+#ifndef KEYLIME_H
+#define KEYLIME_H
+
+#include "KL_types.h"
+#include "KL_functions.h"
+#include "orangetree.h"
+#include <stdio.h>
+#include <string.h>
+
+#endif

+ 8 - 0
src/headers/orangetree.h

@@ -0,0 +1,8 @@
+#ifndef ORANGETREE_H
+#define ORANGETREE_H
+
+#include <stdlib.h>
+#include "OT_types.h"
+#include "OT_functions.h"
+
+#endif

+ 36 - 0
src/launch program/main.c

@@ -0,0 +1,36 @@
+#include "../headers/keylime.h"
+
+int main(){
+    KL_hash_table table = KL_new_table();
+
+    char* hi = "hi";
+    char* hello = "hello";
+    char* howdy = "howdy";
+
+    KL_add(table, "hi", hi);
+    KL_add(table, "hello", hello);
+    KL_add(table, "howdy", howdy);
+
+    printf("hi: %s     hello: %s     howdy: %s\n", (char*)KL_get(table, "hi"), (char*)KL_get(table, "hello"), (char*)KL_get(table, "howdy"));
+    
+    KL_set(table, "hi", hello);
+    KL_add(table, "hi", hi);
+    KL_set(table, "hello", hello);
+    KL_set(table, "howdy", hello);
+
+    printf("hi: %s     hello: %s     howdy: %s\n", (char*)KL_get(table, "hi"), (char*)KL_get(table, "hello"), (char*)KL_get(table, "howdy"));
+
+    KL_del(table, "hi");
+
+    printf("hi: %s     hello: %s     howdy: %s\n", (char*)KL_get(table, "hi"), (char*)KL_get(table, "hello"), (char*)KL_get(table, "howdy"));
+    
+    KL_del(table, "hello");
+
+    printf("hi: %s     hello: %s     howdy: %s\n", (char*)KL_get(table, "hi"), (char*)KL_get(table, "hello"), (char*)KL_get(table, "howdy"));
+    
+    KL_del(table, "howdy");
+
+    printf("hi: %s     hello: %s     howdy: %s\n", (char*)KL_get(table, "hi"), (char*)KL_get(table, "hello"), (char*)KL_get(table, "howdy"));
+
+    return 0;
+}

+ 117 - 0
src/main/main.c

@@ -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;
+}

+ 1 - 0
val

@@ -0,0 +1 @@
+valgrind --leak-check=full --track-origins=yes ./builds/main.bin