I think this is the most common usage and greatly
simplifies the api.
Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
include/qb/qbmap.h | 35 +++------
lib/hashtable.c | 54 ++++----------
lib/map.c | 6 +-
lib/map_int.h | 8 +-
lib/skiplist.c | 39 ++++-----
tests/check_map.c | 219 +++++++++++++++++++++++-----------------------------
6 files changed, 144 insertions(+), 217 deletions(-)
diff --git a/include/qb/qbmap.h b/include/qb/qbmap.h
index 4309645..15cd981 100644
--- a/include/qb/qbmap.h
+++ b/include/qb/qbmap.h
@@ -40,49 +40,31 @@ extern "C" {
typedef struct qb_map qb_map_t;
typedef void (*qb_destroy_notifier_func)(void* data);
-typedef int32_t (*qb_compare_func)(const void* a, const void* b, void* data);
-typedef int32_t (*qb_transverse_func)(void* key, void* value, void* data);
+typedef int32_t (*qb_transverse_func)(const char* key, void* value, void* data);
-typedef uint32_t (*qb_hash_func)(const void* key, uint32_t order);
-
-uint32_t qb_hash_string(const void *key, uint32_t order);
-uint32_t qb_hash_char(const void *key, uint32_t order);
-uint32_t qb_hash_pointer(const void *key, uint32_t order);
/**
* Create an unsorted map based on a hashtable.
*
- * @param key_compare_func a user function to compare keys
- * @param key_compare_data a user pointer to be passed into the compare function
* @param key_destroy_func function to free the key
* @param value_destroy_func function to free the data
* @param max_size maximum size of the hashtable
- * @param hash_fn hash function
- *
- * @see qb_hash_pointer, qb_hash_char, qb_hash_string
*
* @return the map instance
*/
-qb_map_t* qb_hashtable_create(qb_compare_func key_compare_func,
- void *key_compare_data,
- qb_destroy_notifier_func key_destroy_func,
+qb_map_t* qb_hashtable_create(qb_destroy_notifier_func key_destroy_func,
qb_destroy_notifier_func value_destroy_func,
- size_t max_size,
- qb_hash_func hash_fn);
+ size_t max_size);
/**
* Create a sorted map using a skiplist.
*
- * @param key_compare_func a user function to compare keys
- * @param key_compare_data a user pointer to be passed into the compare function
* @param key_destroy_func function to free the key
* @param value_destroy_func function to free the data
*
* @return the map instance
*/
-qb_map_t* qb_skiplist_create(qb_compare_func key_compare_func,
- void* key_compare_data,
- qb_destroy_notifier_func key_destroy_func,
+qb_map_t* qb_skiplist_create(qb_destroy_notifier_func key_destroy_func,
qb_destroy_notifier_func value_destroy_func);
/**
@@ -94,12 +76,15 @@ qb_map_t* qb_skiplist_create(qb_compare_func key_compare_func,
* key_destroy_func when creating the qb_map_t, the old key is freed using
* that function.
*/
-void qb_map_put(qb_map_t *map, const void* key, const void* value);
+void qb_map_put(qb_map_t *map, const char* key, const void* value);
/**
* Gets the value corresponding to the given key.
+ *
+ * @retval NULL (if the key does not exist)
+ * @retval a pointer to the value
*/
-void* qb_map_get(qb_map_t *map, const void* key);
+void* qb_map_get(qb_map_t *map, const char* key);
/**
* Removes a key/value pair from a map.
@@ -109,7 +94,7 @@ void* qb_map_get(qb_map_t *map, const void* key);
* values are freed yourself. If the key does not exist in the map,
* the function does nothing.
*/
-int32_t qb_map_rm(qb_map_t *map, const void* key);
+int32_t qb_map_rm(qb_map_t *map, const char* key);
/**
* Get the number of items in the map.
diff --git a/lib/hashtable.c b/lib/hashtable.c
index 408f98f..f4fb6db 100644
--- a/lib/hashtable.c
+++ b/lib/hashtable.c
@@ -30,7 +30,7 @@
struct hash_node {
struct qb_list_head list;
void *value;
- void *key;
+ const char *key;
};
struct hash_bucket {
@@ -41,7 +41,6 @@ struct hash_table {
struct qb_map map;
size_t count;
uint32_t order;
- qb_hash_func hash_fn;
uint32_t hash_buckets_len;
struct hash_bucket hash_buckets[0];
};
@@ -63,42 +62,29 @@ hash_fnv(const void *value, uint32_t valuelen, uint32_t order)
return (res);
}
-uint32_t
+static uint32_t
qb_hash_string(const void *key, uint32_t order)
{
char* str = (char*)key;
return hash_fnv(key, strlen(str), order);
}
-uint32_t
-qb_hash_char(const void *key, uint32_t order)
-{
- return hash_fnv(key, sizeof(char), order);
-}
-
-uint32_t
-qb_hash_pointer(const void *key, uint32_t order)
-{
- return hash_fnv(key, sizeof(uint32_t), order);
-}
-
static void*
-hashtable_get(struct qb_map *map, const void* key)
+hashtable_get(struct qb_map *map, const char* key)
{
struct hash_table *hash_table = (struct hash_table *)map;
uint32_t hash_entry;
struct qb_list_head *list;
struct hash_node *hash_node;
- hash_entry = hash_table->hash_fn(key, hash_table->order);
+ hash_entry = qb_hash_string(key, hash_table->order);
for (list = hash_table->hash_buckets[hash_entry].list_head.next;
list != &hash_table->hash_buckets[hash_entry].list_head;
list = list->next) {
hash_node = qb_list_entry(list, struct hash_node, list);
- if (map->key_compare_func(hash_node->key, key,
- map->key_compare_data) == 0) {
+ if (strcmp(hash_node->key, key) == 0) {
return hash_node->value;
}
}
@@ -119,11 +105,9 @@ hashtable_rm_with_hash(struct qb_map *map, const void* key,
list = list->next) {
hash_node = qb_list_entry(list, struct hash_node, list);
- if (map->key_compare_func(hash_node->key, key,
- map->key_compare_data) == 0) {
-
+ if (strcmp(hash_node->key, key) == 0) {
if (map->key_destroy_func) {
- map->key_destroy_func(hash_node->key);
+ map->key_destroy_func((void*)hash_node->key);
}
if (map->value_destroy_func) {
map->value_destroy_func(hash_node->value);
@@ -139,23 +123,23 @@ hashtable_rm_with_hash(struct qb_map *map, const void* key,
}
static int32_t
-hashtable_rm(struct qb_map *map, const void* key)
+hashtable_rm(struct qb_map *map, const char* key)
{
struct hash_table *hash_table = (struct hash_table *)map;
uint32_t hash_entry;
- hash_entry = hash_table->hash_fn(key, hash_table->order);
+ hash_entry = qb_hash_string(key, hash_table->order);
return hashtable_rm_with_hash(map, key, hash_entry);
}
static void
-hashtable_put(struct qb_map *map, const void* key, const void* value)
+hashtable_put(struct qb_map *map, const char* key, const void* value)
{
struct hash_table *hash_table = (struct hash_table *)map;
uint32_t hash_entry;
struct hash_node *hash_node;
- hash_entry = hash_table->hash_fn(key, hash_table->order);
+ hash_entry = qb_hash_string(key, hash_table->order);
(void)hashtable_rm_with_hash(map, key, hash_entry);
hash_node = malloc(sizeof(struct hash_node));
if (hash_node == NULL) {
@@ -164,7 +148,7 @@ hashtable_put(struct qb_map *map, const void* key, const void* value)
}
hash_table->count++;
- hash_node->key = (void*)key;
+ hash_node->key = key;
hash_node->value = (void*)value;
qb_list_init(&hash_node->list);
qb_list_add_tail(&hash_node->list,
@@ -212,11 +196,9 @@ hashtable_destroy(struct qb_map *map)
}
qb_map_t *
-qb_hashtable_create(qb_compare_func key_compare_func,
- void *key_compare_data,
- qb_destroy_notifier_func key_destroy_func,
+qb_hashtable_create(qb_destroy_notifier_func key_destroy_func,
qb_destroy_notifier_func value_destroy_func,
- size_t max_size, qb_hash_func hash_fn)
+ size_t max_size)
{
int32_t i;
int32_t order;
@@ -234,8 +216,6 @@ qb_hashtable_create(qb_compare_func key_compare_func,
ht = calloc(1, size);
- ht->map.key_compare_func = key_compare_func;
- ht->map.key_compare_data = key_compare_data;
ht->map.key_destroy_func = key_destroy_func;
ht->map.value_destroy_func = value_destroy_func;
@@ -249,12 +229,6 @@ qb_hashtable_create(qb_compare_func key_compare_func,
ht->count = 0;
ht->order = order;
- if (hash_fn == NULL) {
- ht->hash_fn = qb_hash_pointer;
- } else {
- ht->hash_fn = hash_fn;
- }
-
ht->hash_buckets_len = 1 << order;
for (i = 0; i < ht->hash_buckets_len; i++) {
qb_list_init(&ht->hash_buckets[i].list_head);
diff --git a/lib/map.c b/lib/map.c
index 5487051..f8df18e 100644
--- a/lib/map.c
+++ b/lib/map.c
@@ -24,19 +24,19 @@
#include "map_int.h"
void
-qb_map_put(struct qb_map *map, const void *key, const void *value)
+qb_map_put(struct qb_map *map, const char *key, const void *value)
{
map->put(map, key, value);
}
void *
-qb_map_get(struct qb_map *map, const void *key)
+qb_map_get(struct qb_map *map, const char *key)
{
return map->get(map, key);
}
int32_t
-qb_map_rm(struct qb_map *map, const void *key)
+qb_map_rm(struct qb_map *map, const char *key)
{
return map->rm(map, key);
}
diff --git a/lib/map_int.h b/lib/map_int.h
index f97ff4e..2c21409 100644
--- a/lib/map_int.h
+++ b/lib/map_int.h
@@ -23,9 +23,9 @@
struct qb_map;
-typedef void (*qb_map_put_func)(struct qb_map *map, const void* key, const void* value);
-typedef void* (*qb_map_get_func)(struct qb_map *map, const void* key);
-typedef int32_t (*qb_map_rm_func)(struct qb_map *map, const void* key);
+typedef void (*qb_map_put_func)(struct qb_map *map, const char* key, const void* value);
+typedef void* (*qb_map_get_func)(struct qb_map *map, const char* key);
+typedef int32_t (*qb_map_rm_func)(struct qb_map *map, const char* key);
typedef size_t (*qb_map_count_get_func)(struct qb_map *map);
typedef void (*qb_map_foreach_func)(struct qb_map *map, qb_transverse_func func, void*
user_data);
typedef void (*qb_map_destroy_func)(struct qb_map *map);
@@ -33,8 +33,6 @@ typedef void (*qb_map_destroy_func)(struct qb_map *map);
struct qb_map {
/* user provided
*/
- qb_compare_func key_compare_func;
- void* key_compare_data;
qb_destroy_notifier_func key_destroy_func;
qb_destroy_notifier_func value_destroy_func;
diff --git a/lib/skiplist.c b/lib/skiplist.c
index 84fe12f..67964e1 100644
--- a/lib/skiplist.c
+++ b/lib/skiplist.c
@@ -31,7 +31,7 @@
#define SKIPLIST_LEVEL_COUNT (SKIPLIST_LEVEL_MAX - SKIPLIST_LEVEL_MIN + 1)
struct skiplist_node {
- void *key;
+ const char *key;
void *value;
int8_t level;
@@ -51,7 +51,7 @@ struct skiplist {
*/
typedef struct skiplist_node *skiplist_update_t[SKIPLIST_LEVEL_COUNT];
-static int32_t skiplist_rm(struct qb_map *list, const void *key);
+static int32_t skiplist_rm(struct qb_map *list, const char *key);
static int8_t
skiplist_level_generate(void)
@@ -90,7 +90,7 @@ skiplist_node_next(const struct skiplist_node *node)
* return: a new node on success and NULL otherwise.
*/
static struct skiplist_node *
-skiplist_node_new(const int8_t level, const void* key, const void *value)
+skiplist_node_new(const int8_t level, const char* key, const void *value)
{
struct skiplist_node *new_node = (struct skiplist_node *)
(malloc(sizeof(struct skiplist_node)));
@@ -99,7 +99,7 @@ skiplist_node_new(const int8_t level, const void* key, const void
*value)
return NULL;
new_node->value = (void*)value;
- new_node->key = (void*)key;
+ new_node->key = key;
new_node->level = level;
/* A level 0 node still needs to hold 1 forward pointer, etc. */
@@ -123,12 +123,14 @@ skiplist_header_node_new(void)
static void
skiplist_node_destroy(struct skiplist_node *node, struct skiplist *list)
{
- if (list->map.key_destroy_func && node != list->header) {
- list->map.key_destroy_func(node->key);
- }
if (list->map.value_destroy_func && node != list->header) {
list->map.value_destroy_func(node->value);
}
+
+ if (list->map.key_destroy_func && node != list->header) {
+ list->map.key_destroy_func((void*)node->key);
+ }
+
free(node->forward);
free(node);
}
@@ -165,7 +167,7 @@ op_search(const struct skiplist *list,
if (!fwd_node)
return OP_GOTO_NEXT_LEVEL;
- cmp = list->map.key_compare_func(fwd_node->key, search,
list->map.key_compare_data);
+ cmp = strcmp(fwd_node->key, search);
if (cmp < 0) {
return OP_GOTO_NEXT_NODE;
} else if (cmp == 0) {
@@ -176,15 +178,15 @@ op_search(const struct skiplist *list,
}
static void
-skiplist_put(struct qb_map * map, const void *key, const void *value)
+skiplist_put(struct qb_map * map, const char *key, const void *value)
{
struct skiplist *list = (struct skiplist *)map;
- struct skiplist_node *cur_node = list->header;
struct skiplist_node *new_node;
int8_t level = list->level;
skiplist_update_t update;
int8_t update_level;
int8_t new_node_level;
+ struct skiplist_node *cur_node = list->header;
while ((update_level = level) >= SKIPLIST_LEVEL_MIN) {
struct skiplist_node *fwd_node = cur_node->forward[level];
@@ -192,7 +194,7 @@ skiplist_put(struct qb_map * map, const void *key, const void *value)
switch (op_search(list, fwd_node, key)) {
case OP_FINISH:
if (map->key_destroy_func && fwd_node != list->header) {
- map->key_destroy_func(fwd_node->key);
+ map->key_destroy_func((void*)fwd_node->key);
}
if (map->value_destroy_func && fwd_node != list->header) {
map->value_destroy_func(fwd_node->value);
@@ -236,7 +238,7 @@ skiplist_put(struct qb_map * map, const void *key, const void *value)
}
static int32_t
-skiplist_rm(struct qb_map *map, const void *key)
+skiplist_rm(struct qb_map *map, const char *key)
{
struct skiplist *list = (struct skiplist *)map;
struct skiplist_node *found_node;
@@ -265,8 +267,7 @@ skiplist_rm(struct qb_map *map, const void *key)
found_node = skiplist_node_next(cur_node);
/* ...unless we're at the end of the list or the value doesn't exist. */
- if (!found_node
- || list->map.key_compare_func(found_node->key, key,
list->map.key_compare_data) != 0) {
+ if (!found_node || strcmp(found_node->key, key) != 0) {
return QB_FALSE;
}
@@ -294,7 +295,7 @@ skiplist_rm(struct qb_map *map, const void *key)
}
static void *
-skiplist_get(struct qb_map *map, const void *key)
+skiplist_get(struct qb_map *map, const char *key)
{
struct skiplist *list = (struct skiplist *)map;
struct skiplist_node *cur_node = list->header;
@@ -324,7 +325,7 @@ qb_skiplist_foreach(struct qb_map * map, qb_transverse_func func,
struct skiplist *list = (struct skiplist *)map;
struct skiplist_node *cur_node = skiplist_node_next(list->header);
struct skiplist_node *fwd_node;
-
+
while (cur_node) {
fwd_node = skiplist_node_next(cur_node);
if (func(cur_node->key, cur_node->value, user_data)) {
@@ -342,17 +343,13 @@ skiplist_count_get(struct qb_map * map)
}
qb_map_t *
-qb_skiplist_create(qb_compare_func key_compare_func,
- void *key_compare_data,
- qb_destroy_notifier_func key_destroy_func,
+qb_skiplist_create(qb_destroy_notifier_func key_destroy_func,
qb_destroy_notifier_func value_destroy_func)
{
struct skiplist *sl = calloc(1, sizeof(struct skiplist));
srand(time(NULL));
- sl->map.key_compare_func = key_compare_func;
- sl->map.key_compare_data = key_compare_data;
sl->map.key_destroy_func = key_destroy_func;
sl->map.value_destroy_func = value_destroy_func;
diff --git a/tests/check_map.c b/tests/check_map.c
index 47b66f6..65f5efd 100644
--- a/tests/check_map.c
+++ b/tests/check_map.c
@@ -26,32 +26,21 @@
#include <qb/qblog.h>
#include <qb/qbmap.h>
-char chars[] =
- "0123456789" "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
-
-char chars2[] = "0123456789" "abcdefghijklmnopqrstuvwxyz";
+const char *chars[] = {
+ "0","1","2","3","4","5","6","7","8","9",
+ "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
+ "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
+ NULL,
+};
+
+const char *chars2[] = {
+ "0","1","2","3","4","5","6","7","8","9",
+ "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
+ NULL,
+};
static void *destroyed_key = NULL;
static void *destroyed_value = NULL;
-static int global_data = 123;
-
-static int32_t
-my_char_compare(const void *a, const void *b, void* data)
-{
- const char *cha = a;
- const char *chb = b;
-
- return *cha - *chb;
-}
-
-static int32_t
-my_str_compare(const void *a, const void *b, void* data)
-{
- const char *cha = a;
- const char *chb = b;
-
- return strcmp(cha, chb);
-}
static void
test_map_simple(qb_map_t *m)
@@ -80,36 +69,26 @@ test_map_simple(qb_map_t *m)
}
static int32_t
-my_compare_with_data(const void *a, const void *b, void *user_data)
+my_traverse(const char *key, void *value, void *data)
{
- const char *cha = a;
- const char *chb = b;
- int * dp = (int*)user_data;
-
- /* just check that we got the right data */
- ck_assert_int_eq(*dp, 123);
-
- return *cha - *chb;
+ ck_assert((*key) > 0);
+ return QB_FALSE;
}
static int32_t
-my_traverse(void *key, void *value, void *data)
+check_order(const char *key, void *value, void *data)
{
- char *ch = key;
- ck_assert((*ch) > 0);
+ int *o = (int*)data;
+ ck_assert(chars[*o][0] == key[0]);
+ (*o)++;
return QB_FALSE;
}
-
static int32_t
-check_order(void *key, void *value, void *data)
+check_order2(const char *key, void *value, void *data)
{
- char **p = data;
- char *ch = key;
-
- ck_assert(**p == *ch);
-
- (*p)++;
-
+ int *o = (int*)data;
+ ck_assert(chars2[*o][0] == key[0]);
+ (*o)++;
return QB_FALSE;
}
@@ -118,67 +97,69 @@ test_map_search(qb_map_t* m)
{
int32_t i;
int32_t removed;
- char c;
- char *p;
+ int order;
+ char c[2];
+ const char *p;
for (i = 0; chars[i]; i++) {
- qb_map_put(m, &chars[i], &chars[i]);
+ qb_map_put(m, chars[i], chars[i]);
}
qb_map_foreach(m, my_traverse, NULL);
- ck_assert_int_eq(qb_map_count_get(m), strlen(chars));
+ ck_assert_int_eq(qb_map_count_get(m), (26*2 + 10));
- p = chars;
- qb_map_foreach(m, check_order, &p);
+ order = 0;
+ qb_map_foreach(m, check_order, &order);
for (i = 0; i < 26; i++) {
- removed = qb_map_rm(m, &chars[i + 10]);
+ removed = qb_map_rm(m, chars[i + 10]);
ck_assert(removed);
}
- c = '\0';
- removed = qb_map_rm(m, &c);
+ c[0] = '\0';
+ c[1] = '\0';
+ removed = qb_map_rm(m, c);
ck_assert(!removed);
qb_map_foreach(m, my_traverse, NULL);
- ck_assert_int_eq(qb_map_count_get(m), strlen(chars2));
+ ck_assert_int_eq(qb_map_count_get(m), 26+10);
- p = chars2;
- qb_map_foreach(m, check_order, &p);
+ order = 0;
+ qb_map_foreach(m, check_order2, &order);
for (i = 25; i >= 0; i--) {
- qb_map_put(m, &chars[i + 10], &chars[i + 10]);
+ qb_map_put(m, chars[i + 10], chars[i + 10]);
}
- p = chars;
- qb_map_foreach(m, check_order, &p);
+ order = 0;
+ qb_map_foreach(m, check_order, &order);
- c = '0';
- p = qb_map_get(m, &c);
- ck_assert(p && *p == c);
+ c[0] = '0';
+ p = qb_map_get(m, c);
+ ck_assert(p && *p == *c);
- c = 'A';
- p = qb_map_get(m, &c);
- ck_assert(p && *p == c);
+ c[0] = 'A';
+ p = qb_map_get(m, c);
+ ck_assert(p && *p == *c);
- c = 'a';
- p = qb_map_get(m, &c);
- ck_assert(p && *p == c);
+ c[0] = 'a';
+ p = qb_map_get(m, c);
+ ck_assert(p && *p == *c);
- c = 'z';
- p = qb_map_get(m, &c);
- ck_assert(p && *p == c);
+ c[0] = 'z';
+ p = qb_map_get(m, c);
+ ck_assert(p && *p == *c);
- c = '!';
- p = qb_map_get(m, &c);
+ c[0] = '!';
+ p = qb_map_get(m, c);
ck_assert(p == NULL);
- c = '=';
- p = qb_map_get(m, &c);
+ c[0] = '=';
+ p = qb_map_get(m, c);
ck_assert(p == NULL);
- c = '|';
- p = qb_map_get(m, &c);
+ c[0] = '|';
+ p = qb_map_get(m, c);
ck_assert(p == NULL);
qb_map_destroy(m);
@@ -199,48 +180,47 @@ my_value_destroy(void *value)
static void
test_map_remove(qb_map_t *tree)
{
- char a, b, c, d;
+ char * a, *b, *c, *d;
int32_t i;
int32_t removed;
- const char *remove_ch;
+ const char *remove_ch[] =
{"o","m","k","j","i","g","f","e","d","b","a", NULL};
for (i = 0; chars[i]; i++) {
- qb_map_put(tree, &chars[i], &chars[i]);
+ qb_map_put(tree, chars[i], chars[i]);
}
- a = '0';
- qb_map_put(tree, &a, &a);
- ck_assert(destroyed_key == &chars[0]);
- ck_assert(destroyed_value == &chars[0]);
+ a = "0";
+ qb_map_put(tree, a, a);
+ ck_assert(destroyed_key == chars[0]);
+ ck_assert(destroyed_value == chars[0]);
destroyed_key = NULL;
destroyed_value = NULL;
- b = '5';
- removed = qb_map_rm(tree, &b);
+ b = "5";
+ removed = qb_map_rm(tree, b);
ck_assert(removed);
- ck_assert(destroyed_key == &chars[5]);
- ck_assert(destroyed_value == &chars[5]);
+ ck_assert(destroyed_key == chars[5]);
+ ck_assert(destroyed_value == chars[5]);
destroyed_key = NULL;
destroyed_value = NULL;
- d = '1';
- qb_map_put(tree, &d, &d);
- ck_assert(destroyed_key == &chars[1]);
- ck_assert(destroyed_value == &chars[1]);
+ d = "1";
+ qb_map_put(tree, d, d);
+ ck_assert(destroyed_key == chars[1]);
+ ck_assert(destroyed_value == chars[1]);
destroyed_key = NULL;
destroyed_value = NULL;
- c = '2';
- removed = qb_map_rm(tree, &c);
+ c = "2";
+ removed = qb_map_rm(tree, c);
ck_assert(removed);
- ck_assert(destroyed_key == &chars[2]);
- ck_assert(destroyed_value == &chars[2]);
+ ck_assert(destroyed_key == chars[2]);
+ ck_assert(destroyed_value == chars[2]);
destroyed_key = NULL;
destroyed_value = NULL;
- remove_ch = "omkjigfedba";
for (i = 0; remove_ch[i]; i++) {
- removed = qb_map_rm(tree, &remove_ch[i]);
+ removed = qb_map_rm(tree, remove_ch[i]);
ck_assert(removed);
}
@@ -248,7 +228,7 @@ test_map_remove(qb_map_t *tree)
}
static int32_t
-traverse_func(void *key, void *value, void *data)
+traverse_func(const char *key, void *value, void *data)
{
char *c = value;
char **p = data;
@@ -266,9 +246,9 @@ test_map_traverse_ordered(qb_map_t *tree)
char *p, *result;
for (i = 0; chars[i]; i++) {
- qb_map_put(tree, &chars[i], &chars[i]);
+ qb_map_put(tree, chars[i], chars[i]);
}
- result = calloc(sizeof(char), strlen(chars) + 1);
+ result = calloc(sizeof(char), 26 * 2 + 10 + 1);
p = result;
qb_map_foreach(tree, traverse_func, &p);
@@ -279,11 +259,11 @@ test_map_traverse_ordered(qb_map_t *tree)
}
static int32_t
-traverse_and_remove_func(void *key, void *value, void *data)
+traverse_and_remove_func(const char *key, void *value, void *data)
{
qb_map_t *m = (qb_map_t *)data;
qb_map_rm(m, key);
- qb_map_put(m, (char*)key + 20, key);
+ qb_map_put(m, key + 20, key);
return QB_FALSE;
}
@@ -292,14 +272,14 @@ test_map_traverse_unordered(qb_map_t *tree)
{
int32_t i;
for (i = 0; i < 20; i++) {
- qb_map_put(tree, &chars[i], &chars[i]);
+ qb_map_put(tree, chars[i], chars[i]);
}
qb_map_foreach(tree, traverse_and_remove_func, tree);
}
static int32_t
-my_counter_traverse(void *key, void *value, void *data)
+my_counter_traverse(const char *key, void *value, void *data)
{
int32_t *c = (int32_t*)data;
(*c)++;
@@ -387,31 +367,28 @@ test_map_load(qb_map_t *m, const char* test_name)
START_TEST(test_skiplist_simple)
{
- qb_map_t *m = qb_skiplist_create(my_str_compare, NULL, NULL, NULL);
+ qb_map_t *m = qb_skiplist_create(NULL, NULL);
test_map_simple(m);
}
END_TEST
START_TEST(test_hashtable_simple)
{
- qb_map_t *m = qb_hashtable_create(my_str_compare, NULL, NULL, NULL,
- 32, qb_hash_string);
+ qb_map_t *m = qb_hashtable_create(NULL, NULL, 32);
test_map_simple(m);
}
END_TEST
START_TEST(test_skiplist_search)
{
- qb_map_t *m = qb_skiplist_create(my_compare_with_data,
- &global_data, NULL, NULL);
+ qb_map_t *m = qb_skiplist_create(NULL, NULL);
test_map_search(m);
}
END_TEST
START_TEST(test_skiplist_remove)
{
- qb_map_t *tree = qb_skiplist_create(my_char_compare, NULL,
- my_key_destroy,
+ qb_map_t *tree = qb_skiplist_create(my_key_destroy,
my_value_destroy);
test_map_remove(tree);
}
@@ -419,10 +396,8 @@ END_TEST
START_TEST(test_hashtable_remove)
{
- qb_map_t *tree = qb_hashtable_create(my_char_compare, NULL,
- my_key_destroy,
- my_value_destroy,
- 256, qb_hash_char);
+ qb_map_t *tree = qb_hashtable_create(my_key_destroy,
+ my_value_destroy, 256);
test_map_remove(tree);
}
END_TEST
@@ -430,18 +405,17 @@ END_TEST
START_TEST(test_skiplist_traverse)
{
qb_map_t *m;
- m = qb_skiplist_create(my_char_compare, NULL, NULL, NULL);
+ m = qb_skiplist_create(NULL, NULL);
test_map_traverse_ordered(m);
- m = qb_skiplist_create(my_char_compare, NULL, NULL, NULL);
+ m = qb_skiplist_create(NULL, NULL);
test_map_traverse_unordered(m);
}
END_TEST
START_TEST(test_hashtable_traverse)
{
- qb_map_t *m = qb_hashtable_create(my_char_compare, NULL, NULL, NULL,
- 256, qb_hash_char);
+ qb_map_t *m = qb_hashtable_create(NULL, NULL, 256);
test_map_traverse_unordered(m);
}
END_TEST
@@ -453,7 +427,7 @@ START_TEST(test_skiplist_load)
printf("no dict/words - not testing\n");
return;
}
- m = qb_skiplist_create(my_str_compare, NULL, NULL, NULL);
+ m = qb_skiplist_create(NULL, NULL);
test_map_load(m, __func__);
}
END_TEST
@@ -465,8 +439,7 @@ START_TEST(test_hashtable_load)
printf("no dict/words - not testing\n");
return;
}
- m = qb_hashtable_create(my_str_compare, NULL, NULL, NULL,
- 100000, qb_hash_string);
+ m = qb_hashtable_create(NULL, NULL, 100000);
test_map_load(m, __func__);
}
END_TEST
--
1.7.6