From: "Carlos Maniero" <carlos@maniero.me>
To: "Johnny Richard" <johnny@johnnyrichard.com>,
<~johnnyrichard/olang-devel@lists.sr.ht>
Subject: Re: [PATCH olang 2/2] utils: create hash map data structure
Date: Tue, 27 Feb 2024 15:44:56 -0300 [thread overview]
Message-ID: <CZG311Z668RN.1IQM621M5S8HU@maniero.me> (raw)
In-Reply-To: <20240221222226.67254-3-johnny@johnnyrichard.com>
Fixing the suggestions based on Johnny's comments:
- on map_test I was asserting after freeing the memory
- on map.c there was a bug where I was valdating that the entry was NULL
not the entry->key which was leading to always having an empty first
entry (not a SEGFAULT).
- It does not makes sense to have strdup on arena.
- Instead I just kept the convention to have a single _.
If you think we are good to go, please send a v2. So it will trigger the
pipeline.
--->8---
diff --git a/src/map.c b/src/map.c
index 532ba3b..6665e18 100644
--- a/src/map.c
+++ b/src/map.c
@@ -32,7 +32,10 @@ static void
map_init(map_t *map);
static char *
-__strdup(const char *s, arena_t *arena);
+_strdup(const char *s, arena_t *arena);
+
+static uint32_t
+map_get_index(map_t *map, uint32_t hash);
map_t *
map_new(arena_t *arena)
@@ -78,24 +81,26 @@ map_put(map_t *map, char *key, void *value)
{
assert(map && key);
uint32_t hash = u32_fnv1a_hash(key);
- map_entry_t *entry = &(map->entries[hash & (map->capacity - 1)]);
+ map_entry_t *entry = map->entries + map_get_index(map, hash);
- while (entry != NULL) {
+ if (entry->key == NULL) {
+ *entry = (map_entry_t){ .key = _strdup(key, map->arena), .hash = hash, .value = value, .next = NULL };
+ return true;
+ }
+
+ do {
if (entry->hash == hash && strcmp(entry->key, key) == 0) {
entry->value = value;
- return true;
+ break;
}
- if (entry->next == NULL)
+ if (entry->next == NULL) {
+ entry->next = (map_entry_t *)arena_alloc(map->arena, sizeof(map_entry_t));
+ *entry->next = (map_entry_t){ .key = _strdup(key, map->arena), .hash = hash, .value = value, .next = NULL };
+
break;
+ }
entry = entry->next;
- }
-
- if (entry->key == NULL) {
- *entry = (map_entry_t){ .key = __strdup(key, map->arena), .hash = hash, .value = value, .next = NULL };
- } else {
- entry->next = (map_entry_t *)arena_alloc(map->arena, sizeof(map_entry_t));
- *entry->next = (map_entry_t){ .key = __strdup(key, map->arena), .hash = hash, .value = value, .next = NULL };
- }
+ } while (entry != NULL);
return true;
}
@@ -104,7 +109,7 @@ void *
map_get(map_t *map, char *key)
{
uint32_t hash = u32_fnv1a_hash(key);
- map_entry_t *entry = &map->entries[hash & (map->capacity - 1)];
+ map_entry_t *entry = map->entries + map_get_index(map, hash);
while (entry != NULL) {
if (entry->hash == hash && strcmp(entry->key, key) == 0) {
return entry->value;
@@ -114,8 +119,15 @@ map_get(map_t *map, char *key)
return NULL;
}
+static uint32_t
+map_get_index(map_t *map, uint32_t hash)
+{
+ uint32_t capacity_mask = map->capacity - 1;
+ return hash & capacity_mask;
+}
+
static char *
-__strdup(const char *s, arena_t *arena)
+_strdup(const char *s, arena_t *arena)
{
size_t slen = strlen(s);
char *result = arena_alloc(arena, slen + 1);
diff --git a/tests/unit/map_test.c b/tests/unit/map_test.c
index 3eb9acd..449bca6 100644
--- a/tests/unit/map_test.c
+++ b/tests/unit/map_test.c
@@ -52,6 +52,10 @@ test_map_put_and_get(const MunitParameter params[], void *user_data_or_fixture)
assert_int(*((int *)map_get(map, "n1")), ==, n1);
assert_int(*((int *)map_get(map, "n2")), ==, n2);
+ map_put(map, "n1", (void *)&n2);
+
+ assert_int(*((int *)map_get(map, "n1")), ==, n2);
+
arena_free(&arena);
return MUNIT_OK;
next prev parent reply other threads:[~2024-02-27 18:45 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-21 22:20 [PATCH olang 0/2] introduce " Johnny Richard
2024-02-21 22:20 ` [PATCH olang 1/2] build: add clean target on root Makefile Johnny Richard
2024-02-21 22:20 ` [PATCH olang 2/2] utils: create hash map data structure Johnny Richard
2024-02-21 21:24 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-02-27 17:26 ` [PATCH olang 2/2] utils: create hash map data structure Carlos Maniero
2024-02-27 19:30 ` Johnny Richard
2024-02-27 18:44 ` Carlos Maniero [this message]
2024-02-27 20:05 ` Johnny Richard
2024-02-27 20:03 ` Johnny Richard
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CZG311Z668RN.1IQM621M5S8HU@maniero.me \
--to=carlos@maniero.me \
--cc=johnny@johnnyrichard.com \
--cc=~johnnyrichard/olang-devel@lists.sr.ht \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.johnnyrichard.com/olang.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox