diff options
| author | Baitinq <[email protected]> | 2025-01-02 15:28:06 +0100 |
|---|---|---|
| committer | Baitinq <[email protected]> | 2025-01-03 01:03:24 +0100 |
| commit | e37092c768ed6820641c0884e73e0c38dd00e9e8 (patch) | |
| tree | 7ab40736601961da194c9f3629312fcc6b71e626 | |
| download | c-hashtable-e37092c768ed6820641c0884e73e0c38dd00e9e8.tar.gz c-hashtable-e37092c768ed6820641c0884e73e0c38dd00e9e8.tar.bz2 c-hashtable-e37092c768ed6820641c0884e73e0c38dd00e9e8.zip | |
Initial commit
| -rw-r--r-- | .envrc | 1 | ||||
| -rw-r--r-- | .gitignore | 5 | ||||
| -rw-r--r-- | Makefile | 14 | ||||
| -rw-r--r-- | flake.lock | 27 | ||||
| -rw-r--r-- | flake.nix | 30 | ||||
| -rw-r--r-- | src/hashtable.c | 52 | ||||
| -rw-r--r-- | src/hashtable.h | 14 | ||||
| -rw-r--r-- | src/main.c | 28 |
8 files changed, 171 insertions, 0 deletions
diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f480dc4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.direnv/ + +*.o + +main diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e28fd46 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +CC = gcc +CFLAGS = -Wall -g +SRC = src/main.c src/hashtable.c +OBJ = $(SRC:.c=.o) +TARGET = main + +$(TARGET): $(OBJ) + $(CC) $(OBJ) -o $(TARGET) + +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +clean: + rm -f $(OBJ) $(TARGET) diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..b18cf2a --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1735617354, + "narHash": "sha256-5zJyv66q68QZJZsXtmjDBazGnF0id593VSy+8eSckoo=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "69b9a8c860bdbb977adfa9c5e817ccb717884182", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..67cfaed --- /dev/null +++ b/flake.nix @@ -0,0 +1,30 @@ +{ + description = "Renfe flake"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + }; + + outputs = { + self, + nixpkgs, + }: let + systems = ["x86_64-darwin" "aarch64-darwin" "x86_64-linux"]; + createDevShell = system: let + pkgs = import nixpkgs { + system = "${system}"; + config.allowUnfree = true; + }; + in + pkgs.mkShell { + buildInputs = with pkgs; [ + gcc + gnumake + valgrind + gdb + ]; + }; + in { + devShell = nixpkgs.lib.genAttrs systems createDevShell; + }; +} diff --git a/src/hashtable.c b/src/hashtable.c new file mode 100644 index 0000000..14ab58a --- /dev/null +++ b/src/hashtable.c @@ -0,0 +1,52 @@ +#include <stddef.h> +#include <stdlib.h> +#include <stdio.h> + +#include "hashtable.h" + +struct { + void** data; + size_t size; +} typedef HashTableImpl; + +static int hash(char key) { + return key % 7;//TODO +} + +HashTable hashtable_init() { + HashTableImpl* ht = (HashTableImpl*) malloc(sizeof(HashTableImpl)); + + int len = 8; + ht->data = (void**) malloc(sizeof(void*) * len); + ht->size = len; + + return (HashTable) ht; +} + +int hashtable_deinit(HashTable* ht) { + HashTableImpl* ht_impl = (HashTableImpl*) *ht; + free(ht_impl->data); + free(ht_impl); + ht = NULL; + return 0; +} + +void* hashtable_get(HashTable ht, char key) { + HashTableImpl* ht_impl = (HashTableImpl*) ht; + + int index = hash(key); + + void* res = ht_impl->data[index]; + + return res; +} + +int hashtable_put(HashTable ht, char key, void* val) { + HashTableImpl* ht_impl = (HashTableImpl*) ht; + + int index = hash(key); + + ht_impl->data[index] = val; + + return 0; +} diff --git a/src/hashtable.h b/src/hashtable.h new file mode 100644 index 0000000..2a31204 --- /dev/null +++ b/src/hashtable.h @@ -0,0 +1,14 @@ +#ifndef __HASHTABLE_H__ +#define __HASHTABLE_H__ + +void typedef *HashTable; + +HashTable hashtable_init(); + +int hashtable_deinit(HashTable*); + +int hashtable_put(HashTable, char, void*); + +void* hashtable_get(HashTable, char); + +#endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..d1d84bf --- /dev/null +++ b/src/main.c @@ -0,0 +1,28 @@ +#include <stdio.h> +#include "hashtable.h" + +int main(int argc, char** argv) { + printf("Testing hashing:\n"); + + HashTable ht = hashtable_init(); + + char res = hashtable_get(ht, 'a'); + + printf("Result: %c\n", res); + + hashtable_put(ht, 'a', 'x'); + + res = hashtable_get(ht, 'a'); + + printf("Result: %c\n", res); + + hashtable_put(ht, 'h', '1'); + + res = hashtable_get(ht, 'a'); + + printf("Result: %c\n", res); + + hashtable_deinit(&ht); + + return 0; +} |