aboutsummaryrefslogtreecommitdiff
path: root/src/core/idhash.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2024-08-11 22:12:30 -0700
committerGarrett D'Amore <garrett@damore.org>2024-08-11 22:12:38 -0700
commitd3652db599cb3bf4101cf2e6cf42c764d65b6fb8 (patch)
tree1ca0bd8f2ed991abb96b02b56d7010f8680703e3 /src/core/idhash.c
parent6e5cf2967bc8717ab712a6cdda9d297d18bdeef0 (diff)
downloadnng-d3652db599cb3bf4101cf2e6cf42c764d65b6fb8.tar.gz
nng-d3652db599cb3bf4101cf2e6cf42c764d65b6fb8.tar.bz2
nng-d3652db599cb3bf4101cf2e6cf42c764d65b6fb8.zip
idhash: add nng_id_visit API
This allows an efficient way to iterate over the entries stored in an ID hash. The iteration is fast, and requires no additional storage. The order of iteration is not guaranteed.
Diffstat (limited to 'src/core/idhash.c')
-rw-r--r--src/core/idhash.c26
1 files changed, 24 insertions, 2 deletions
diff --git a/src/core/idhash.c b/src/core/idhash.c
index 67ae67ea..306c751f 100644
--- a/src/core/idhash.c
+++ b/src/core/idhash.c
@@ -1,5 +1,5 @@
//
-// Copyright 2023 Staysail Systems, Inc. <info@staysail.tech>
+// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
@@ -366,4 +366,26 @@ nni_id_alloc32(nni_id_map *m, uint32_t *idp, void *val)
NNI_ASSERT(id < (1ULL << 32));
*idp = (uint32_t) id;
return (rv);
-} \ No newline at end of file
+}
+
+bool
+nni_id_visit(nni_id_map *m, uint64_t *keyp, void **valp, uint32_t *cursor)
+{
+ // cursor is just a cursor into the table
+ uint32_t index = *cursor;
+ while (index < m->id_cap) {
+ if (m->id_entries[index].val != NULL) {
+ if (valp != NULL) {
+ *valp = m->id_entries[index].val;
+ }
+ if (keyp != NULL) {
+ *keyp = m->id_entries[index].key;
+ }
+ *cursor = index + 1;
+ return true;
+ }
+ index++;
+ }
+ *cursor = index;
+ return (false);
+}