From 3db84f0b0a9852159af6343c81e6a5c16bb555d9 Mon Sep 17 00:00:00 2001 From: tidwall Date: Sat, 25 Jul 2020 05:36:27 -0700 Subject: [PATCH] Added probe function --- hashmap.c | 13 +++++++++++++ hashmap.h | 1 + 2 files changed, 14 insertions(+) diff --git a/hashmap.c b/hashmap.c index e159e10..17b5dfd 100644 --- a/hashmap.c +++ b/hashmap.c @@ -238,6 +238,19 @@ void *hashmap_get(struct hashmap *map, void *key) { } } +// hashmap_probe returns the item in the bucket at position or NULL if an item +// is not set for that bucket. The position is 'moduloed' by the number of +// buckets in the hashmap. +void *hashmap_probe(struct hashmap *map, uint64_t position) { + size_t i = position & map->mask; + struct bucket *bucket = bucket_at(map, i); + if (!bucket->dib) { + return NULL; + } + return bucket_item(bucket); +} + + // hashmap_delete removes an item from the hash map and returns it. If the // item is not found then NULL is returned. void *hashmap_delete(struct hashmap *map, void *key) { diff --git a/hashmap.h b/hashmap.h index 2c0457a..5be32d2 100644 --- a/hashmap.h +++ b/hashmap.h @@ -24,6 +24,7 @@ bool hashmap_oom(struct hashmap *map); void *hashmap_get(struct hashmap *map, void *item); void *hashmap_set(struct hashmap *map, void *item); void *hashmap_delete(struct hashmap *map, void *item); +void *hashmap_probe(struct hashmap *map, uint64_t position); bool hashmap_scan(struct hashmap *map, bool (*iter)(const void *item, void *udata), void *udata); void hashmap_set_allocator(void *(*malloc)(size_t), void (*free)(void*));