summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Anholt <eric@anholt.net>2009-11-24 02:35:52 +0100
committerEric Anholt <eric@anholt.net>2009-11-23 18:05:54 -0800
commit8a4c8f14d40e88239d5dcd9eabb803b8459a4833 (patch)
tree55be7e7cb4cb70d61af3c3d45d1c47f7bff56643
parentef05536d15f07c27b5b862fffe5839f42a2a6742 (diff)
Add an interface for choosing a random hash table entry with a predicate.
-rw-r--r--hash_table.c27
-rw-r--r--hash_table.h3
-rw-r--r--tests/.gitignore2
-rw-r--r--tests/Makefile.am1
-rw-r--r--tests/random_entry.c99
5 files changed, 132 insertions, 0 deletions
diff --git a/hash_table.c b/hash_table.c
index 3a22155..991f02b 100644
--- a/hash_table.c
+++ b/hash_table.c
@@ -271,3 +271,30 @@ hash_table_next_entry(struct hash_table *ht, struct hash_entry *entry)
return NULL;
}
+
+struct hash_entry *
+hash_table_random_entry(struct hash_table *ht,
+ int (*predicate)(struct hash_entry *entry))
+{
+ struct hash_entry *entry;
+ uint32_t i = random() % ht->size;
+
+ if (ht->entries == 0)
+ return NULL;
+
+ for (entry = ht->table + i; entry != ht->table + ht->size; entry++) {
+ if (entry_is_present(entry) &&
+ (!predicate || predicate(entry))) {
+ return entry;
+ }
+ }
+
+ for (entry = ht->table; entry != ht->table + i; entry++) {
+ if (entry_is_present(entry) &&
+ (!predicate || predicate(entry))) {
+ return entry;
+ }
+ }
+
+ return NULL;
+}
diff --git a/hash_table.h b/hash_table.h
index c7044c3..e013458 100644
--- a/hash_table.h
+++ b/hash_table.h
@@ -57,3 +57,6 @@ void hash_table_remove(struct hash_table *ht, struct hash_entry *entry);
struct hash_entry *hash_table_next_entry(struct hash_table *ht,
struct hash_entry *entry);
+struct hash_entry *
+hash_table_random_entry(struct hash_table *ht,
+ int (*predicate)(struct hash_entry *entry));
diff --git a/tests/.gitignore b/tests/.gitignore
index be475a3..89dc805 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -1,4 +1,6 @@
delete_and_lookup
+destroy_callback
insert_and_lookup
insert_many
null_destroy
+random_entry
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 20c0ef8..d941c86 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -27,6 +27,7 @@ TESTS = \
insert_and_lookup \
insert_many \
null_destroy \
+ random_entry \
$()
EXTRA_PROGRAMS = $(TESTS)
diff --git a/tests/random_entry.c b/tests/random_entry.c
new file mode 100644
index 0000000..3f51088
--- /dev/null
+++ b/tests/random_entry.c
@@ -0,0 +1,99 @@
+/*
+ * Copyright © 2009 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ * Eric Anholt <eric@anholt.net>
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include "hash_table.h"
+#include "fnv_hash.h"
+
+static uint32_t
+key_value(const void *key)
+{
+ return *(uint32_t *)key;
+}
+
+static uint32_t
+uint32_t_key_hash(const void *key)
+{
+ /* Note that we're using the address of the key instead of the
+ * perfectly-hashed 32-bit value in the key, in order to trigger
+ * collisions.
+ */
+ return (uint32_t)(uintptr_t)key;
+}
+
+static int
+uint32_t_key_equals(const void *a, const void *b)
+{
+ return key_value(a) == key_value(b);
+}
+
+static int
+uint32_t_key_is_even(struct hash_entry *entry)
+{
+ return (key_value(entry->key) & 1) == 0;
+}
+
+int
+main(int argc, char **argv)
+{
+ struct hash_table *ht;
+ struct hash_entry *entry;
+ int size = 10000;
+ uint32_t keys[size];
+ uint32_t i, random_value;
+
+ ht = hash_table_create(uint32_t_key_hash, uint32_t_key_equals);
+
+ for (i = 0; i < size; i++) {
+ keys[i] = i;
+
+ hash_table_insert(ht, keys + i, NULL);
+ }
+
+ /* Test the no-predicate case. */
+ entry = hash_table_random_entry(ht, NULL);
+ assert(entry);
+
+ /* Check that we're getting different entries and that the predicate
+ * works.
+ */
+ for (i = 0; i < 100; i++) {
+ entry = hash_table_random_entry(ht, uint32_t_key_is_even);
+ assert(entry);
+ assert((key_value(entry->key) & 1) == 0);
+ if (i == 0 || key_value(entry->key) != random_value)
+ break;
+ random_value = key_value(entry->key);
+ }
+ assert(i != 100);
+
+ hash_table_destroy(NULL, NULL);
+
+ return 0;
+}