Skip to content

Commit

Permalink
✨ (delete): Added a hashmap delete function
Browse files Browse the repository at this point in the history
  • Loading branch information
theobori committed Aug 30, 2024
1 parent ab71757 commit 42f60e0
Show file tree
Hide file tree
Showing 17 changed files with 250 additions and 107 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
push:

jobs:
tests:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ dkms.conf

build
.direnv
result
result
/debug.c
32 changes: 25 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,31 +68,49 @@ uint32_t custom_hash_function(uint8_t *bytes, size_t size)
int main(int argc, const char* argv[])
{
th_table_t table;
bool success;

Person person = { "James", true };

// Initialize the table
th_table_init(&table);

// Insert a new key value pair
th_table_put(&table, "key_1", strlen("key_1"), &person);
success = th_table_put(&table, "key_1", strlen("key_1"), &person);
if (success == false) {
fprintf(stderr, "Unable to insert\n");
return 1;
}

// Get the last inserted value
Person *james;
james = th_table_get(&table, "key_1", strlen("key_1"));
if (james == NULL) {
fprintf(stderr, "Unable to get value from key_1");

success = james != NULL;
if (success == false) {
fprintf(stderr, "It does not exist\n");
return 1;
}

printf("name -> %s, is_hydrated -> %d\n", james->name, james->is_hydrated);

// Feel free to change the hash function
table.options.hash_func = custom_hash_function;
// Delete the entry
success = th_table_delete(&table, "key_1", strlen("key_1"));
if (success == false) {
fprintf(stderr, "Unable to delete\n");
return 1;
}

// Verify that it doesnt exist anymore
james = th_table_get(&table, "key_1", strlen("key_1"));
if (james != NULL) {
fprintf(stderr, "The entry still exists\n");
return 1;
}

// Free the allocated memory
th_table_destroy(&table);
th_table_free(&table);

return 0;
}

```
4 changes: 3 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ set (
hash.c
entry.c
table.c
key.c
)

set (
TINYHASH_HEADERS
entry.h
table.h
hash.h
options.h
types.h
key.h
)

add_library (${TINYHASH_NAME} ${TINYHASH_LIB_TYPE} ${TINYHASH_SRC})
Expand Down
28 changes: 12 additions & 16 deletions src/entry.c
Original file line number Diff line number Diff line change
@@ -1,40 +1,36 @@
#include "entry.h"
#include "hash.h"

#include <string.h>

static th_entry_t *th_entry_new(th_key_t *key, th_any_t value)
{
th_entry_t *entry = malloc(sizeof(th_entry_t));

if (entry == NULL) return NULL;

entry->key = *key;
entry->value = value;
entry->previous = NULL;
entry->next = NULL;

return entry;
}

static void th_entry_raw_add(th_entry_t **root,
th_entry_t *entry)
static void th_entry_raw_add(th_entry_t **root, th_entry_t *entry)
{
if (*root != NULL) {
(*root)->previous = entry;
}

entry->next = *root;
*root = entry;
}

void th_entry_add(th_entry_t **root, th_key_t *key,
th_any_t value)
bool th_entry_add(th_entry_t **root, th_key_t *key, th_any_t value)
{
th_entry_t *entry = th_entry_new(key, value);

if (entry == NULL) return false;

th_entry_raw_add(root, entry);
}

th_key_t th_key_new(th_any_t data, size_t size,
th_hash_func_t hash_func)
{
return (th_key_t) {
.hash = hash_func(data, size),
.size = size,
.data = data,
};
return true;
}
17 changes: 4 additions & 13 deletions src/entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,19 @@

#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>

#include "hash.h"

typedef void *th_any_t;

typedef struct {
uint32_t hash;
int size;
th_any_t data;
} th_key_t;
#include "key.h"

typedef struct th_entry_s {
th_key_t key;
th_any_t value;
struct th_entry_s *previous;
struct th_entry_s *next;
} th_entry_t;


void th_entry_add(th_entry_t **root, th_key_t *key,
th_any_t value);

th_key_t th_key_new(th_any_t data, size_t size,
th_hash_func_t hash_func);
bool th_entry_add(th_entry_t **root, th_key_t *key, th_any_t value);

#endif
8 changes: 4 additions & 4 deletions src/hash.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#include <stdint.h>
#include <stdlib.h>

#define HASH_INITIAL_VALUE 2166136261u
#define HASH_MUL_VALUE 16777619
#define TH_HASH_INITIAL_VALUE 2166136261u
#define TH_HASH_MUL_VALUE 16777619

uint32_t th_hash(uint8_t *bytes, size_t size)
{
if (bytes == NULL) return 0;

uint32_t hash = HASH_INITIAL_VALUE;
uint32_t hash = TH_HASH_INITIAL_VALUE;

for (int i = 0; i < size; i++) {
hash ^= bytes[i];
hash *= HASH_MUL_VALUE;
hash *= TH_HASH_MUL_VALUE;
}

return hash;
Expand Down
2 changes: 0 additions & 2 deletions src/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include <stdint.h>
#include <stdio.h>

typedef uint32_t (*th_hash_func_t)(uint8_t *bytes, size_t size);

uint32_t th_hash(uint8_t *bytes, size_t size);

#endif
20 changes: 20 additions & 0 deletions src/key.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <string.h>

#include "key.h"
#include "hash.h"

th_key_t th_key_new(th_any_t data, size_t size)
{
return (th_key_t) {
.hash = th_hash(data, size),
.size = size,
.data = data,
};
}

bool th_key_is_equal(th_key_t *first, th_key_t *second)
{
if (first->size != second->size) return false;

return memcmp(first->data, second->data, first->size) == 0;
}
20 changes: 20 additions & 0 deletions src/key.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef __TINYHASH_KEY_H__
#define __TINYHASH_KEY_H__

#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>

#include "types.h"

typedef struct {
uint32_t hash;
int size;
th_any_t data;
} th_key_t;

th_key_t th_key_new(th_any_t data, size_t size);

bool th_key_is_equal(th_key_t *first, th_key_t *second);

#endif
12 changes: 0 additions & 12 deletions src/options.h

This file was deleted.

Loading

0 comments on commit 42f60e0

Please sign in to comment.