From: Carlos Maniero <carlos@maniero.me>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Carlos Maniero <carlos@maniero.me>,
Johnny Richard <johnny@johnnyrichard.com>
Subject: [PATCH olang 1/5] map: add function to retrieve all key-value pairs
Date: Sat, 21 Sep 2024 01:12:54 +0000 (UTC) [thread overview]
Message-ID: <20240921011234.191248-2-carlos@maniero.me> (raw)
In-Reply-To: <20240921011234.191248-1-carlos@maniero.me>
Signed-off-by: Carlos Maniero <carlos@maniero.me>
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
src/map.c | 17 +++++++++++++++++
src/map.h | 10 ++++++++++
tests/unit/map_test.c | 30 ++++++++++++++++++++++++++++++
3 files changed, 57 insertions(+)
diff --git a/src/map.c b/src/map.c
index 6665e18..a6bc3a3 100644
--- a/src/map.c
+++ b/src/map.c
@@ -80,6 +80,8 @@ bool
map_put(map_t *map, char *key, void *value)
{
assert(map && key);
+ map->size++;
+
uint32_t hash = u32_fnv1a_hash(key);
map_entry_t *entry = map->entries + map_get_index(map, hash);
@@ -126,6 +128,21 @@ map_get_index(map_t *map, uint32_t hash)
return hash & capacity_mask;
}
+void
+map_get_kvs(map_t *map, map_kv_t **kvs)
+{
+ size_t index = 0;
+
+ for (size_t j = 0; j < map->capacity; ++j) {
+ map_entry_t *entry = map->entries + j;
+
+ while (entry != NULL && entry->key != NULL) {
+ kvs[index++] = (map_kv_t *)entry;
+ entry = entry->next;
+ }
+ }
+}
+
static char *
_strdup(const char *s, arena_t *arena)
{
diff --git a/src/map.h b/src/map.h
index 123ad4d..6f8681c 100644
--- a/src/map.h
+++ b/src/map.h
@@ -37,8 +37,15 @@ typedef struct map
arena_t *arena;
map_entry_t *entries;
size_t capacity;
+ size_t size;
} map_t;
+typedef struct map_kv
+{
+ char *key;
+ void *value;
+} map_kv_t;
+
typedef struct map_entry
{
char *key;
@@ -56,4 +63,7 @@ map_put(map_t *map, char *key, void *value);
void *
map_get(map_t *map, char *key);
+void
+map_get_kvs(map_t *map, map_kv_t **kvs);
+
#endif /* MAP_H */
diff --git a/tests/unit/map_test.c b/tests/unit/map_test.c
index 449bca6..9497c7c 100644
--- a/tests/unit/map_test.c
+++ b/tests/unit/map_test.c
@@ -49,6 +49,7 @@ test_map_put_and_get(const MunitParameter params[], void *user_data_or_fixture)
map_put(map, "n1", (void *)&n1);
map_put(map, "n2", (void *)&n2);
+ assert_int(map->size, ==, 2);
assert_int(*((int *)map_get(map, "n1")), ==, n1);
assert_int(*((int *)map_get(map, "n2")), ==, n2);
@@ -61,6 +62,35 @@ test_map_put_and_get(const MunitParameter params[], void *user_data_or_fixture)
return MUNIT_OK;
}
+static MunitResult
+test_map_get_kvs(const MunitParameter params[], void *user_data_or_fixture)
+{
+ arena_t arena = arena_new(MAP_TEST_ARENA_CAPACITY);
+ map_t *map = map_new(&arena);
+
+ int n1 = 1;
+ int n2 = 2;
+
+ map_put(map, "n1", (void *)&n1);
+ map_put(map, "n2", (void *)&n2);
+
+ assert_int(map->size, ==, 2);
+
+ map_kv_t map_kvs[map->size];
+
+ map_get_kvs(map, (map_kv_t **)map_kvs);
+
+ assert_string_equal(map_kvs[0].key, "n1");
+ assert_int(*((int *)(map_kvs[0].value)), ==, 1);
+
+ assert_string_equal(map_kvs[1].key, "n2");
+ assert_int(*((int *)(map_kvs[1].value)), ==, 2);
+
+ arena_free(&arena);
+
+ return MUNIT_OK;
+}
+
static MunitTest tests[] = {
{ "/test_create_new", test_create_new, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
{ "/test_map_put_and_get", test_map_put_and_get, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
--
2.34.1
next prev parent reply other threads:[~2024-09-21 1:13 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-21 1:12 [PATCH olang 0/5] fix multiple variables Carlos Maniero
2024-09-21 1:12 ` Carlos Maniero [this message]
2024-09-21 1:12 ` [PATCH olang 2/5] scope: make scope a bi-directional tree Carlos Maniero
2024-09-21 1:13 ` [PATCH olang 3/5] ast: checker: add function scope Carlos Maniero
2024-09-21 1:13 ` [PATCH olang 4/5] codegen: reset variable offset on block leave Carlos Maniero
2024-09-21 1:13 ` [PATCH olang 5/5] codegen: preserve function's variable stack location Carlos Maniero
2024-09-21 1:13 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-09-21 1:25 ` [PATCH olang 0/5] fix multiple variables 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=20240921011234.191248-2-carlos@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