Skip to content

Commit

Permalink
Implemented the sk_hashmap DS
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed Jun 9, 2024
1 parent 6e36cb6 commit 53b6913
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
38 changes: 38 additions & 0 deletions include/sk_hashmap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* GNU Sparky --- A 5v5 character-based libre tactical shooter
* Copyright (C) 2024 Wasym A. Alonso
*
* This file is part of Sparky.
*
* Sparky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sparky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sparky. If not, see <http://www.gnu.org/licenses/>.
*/


#pragma once

#include <sk_defines.h>

typedef struct {
u32 capacity;
usz element_size;
void *data;
} sk_hashmap;

sk_hashmap sk_hashmap_create(u32 capacity, usz element_size);

void sk_hashmap_destroy(sk_hashmap *hm);

u8 sk_hashmap_set(sk_hashmap *hm, const char *name, void *value);

u8 sk_hashmap_get(sk_hashmap *hm, const char *name, void *out_value);
69 changes: 69 additions & 0 deletions src/sk_hashmap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* GNU Sparky --- A 5v5 character-based libre tactical shooter
* Copyright (C) 2024 Wasym A. Alonso
*
* This file is part of Sparky.
*
* Sparky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sparky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sparky. If not, see <http://www.gnu.org/licenses/>.
*/


#include <stdlib.h>
#include <string.h>
#include <sk_log.h>
#include <sk_hashmap.h>

#define HASH_MULTIPLIER 97

static u64 hash(const char *name, u32 n) {
u64 hash = 0;
for (const u8 *us = (const u8 *) name; *us; ++us) hash = (hash * HASH_MULTIPLIER) + *us;
return hash % n;
}

sk_hashmap sk_hashmap_create(u32 capacity, usz element_size) {
return (sk_hashmap) {
.capacity = capacity,
.element_size = element_size,
.data = malloc((usz) capacity * element_size)
};
}

void sk_hashmap_destroy(sk_hashmap *hm) {
if (!hm) {
SK_LOG_WARN("sk_hashmap_destroy :: `hm` is not a valid pointer, skipping destruction");
return;
}
free(hm->data);
*hm = (sk_hashmap) {0};
hm = 0;
}

u8 sk_hashmap_set(sk_hashmap *hm, const char *name, void *value) {
if (!hm || !name || !value) {
SK_LOG_ERROR("sk_hashmap_set :: `hm`, `name` and `value` need to be valid pointers");
return 0;
}
memcpy((void *) ((u64 *) hm->data + (hash(name, hm->capacity) * hm->element_size)), value, hm->element_size);
return 1;
}

u8 sk_hashmap_get(sk_hashmap *hm, const char *name, void *out_value) {
if (!hm || !name || !out_value) {
SK_LOG_ERROR("sk_hashmap_set :: `hm`, `name` and `out_value` need to be valid pointers");
return 0;
}
memcpy(out_value, (void *) ((u64 *) hm->data + (hash(name, hm->capacity) * hm->element_size)), hm->element_size);
return 1;
}

0 comments on commit 53b6913

Please sign in to comment.