From f642bb98c78ad80bb82eb9b0a39bb0baccda1701 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Thu, 26 Jan 2012 02:28:58 +0900 Subject: [PATCH] Added spdylay_map_size() --- lib/spdylay_map.c | 34 ++++++++++++++++++++++++---------- lib/spdylay_map.h | 3 +++ tests/spdylay_map_test.c | 9 +++++++++ 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/lib/spdylay_map.c b/lib/spdylay_map.c index 0ada828..eaa6a42 100644 --- a/lib/spdylay_map.c +++ b/lib/spdylay_map.c @@ -27,6 +27,7 @@ int spdylay_map_init(spdylay_map *map) { map->root = NULL; + map->size = 0; return 0; } @@ -125,6 +126,9 @@ int spdylay_map_insert(spdylay_map *map, key_type key, void *val) { int error = 0; map->root = insert_recur(map->root, key, val, &error); + if(!error) { + ++map->size; + } return error; } @@ -162,16 +166,17 @@ static spdylay_map_entry* erase_rotate_recur(spdylay_map_entry *entry) } } -static spdylay_map_entry* erase_recur(spdylay_map_entry *entry, key_type key) +static spdylay_map_entry* erase_recur(spdylay_map_entry *entry, key_type key, + int *error) { - if(entry != NULL) { - if(key < entry->key) { - entry->left = erase_recur(entry->left, key); - } else if(key > entry->key) { - entry->right = erase_recur(entry->right, key); - } else { - entry = erase_rotate_recur(entry); - } + if(entry == NULL) { + *error = SPDYLAY_ERR_INVALID_ARGUMENT; + } else if(key < entry->key) { + entry->left = erase_recur(entry->left, key, error); + } else if(key > entry->key) { + entry->right = erase_recur(entry->right, key, error); + } else { + entry = erase_rotate_recur(entry); } return entry; } @@ -179,10 +184,19 @@ static spdylay_map_entry* erase_recur(spdylay_map_entry *entry, key_type key) void spdylay_map_erase(spdylay_map *map, key_type key) { if(map->root != NULL) { - map->root = erase_recur(map->root, key); + int error = 0; + map->root = erase_recur(map->root, key, &error); + if(!error) { + --map->size; + } } } +size_t spdylay_map_size(spdylay_map *map) +{ + return map->size; +} + static void for_each(spdylay_map_entry *entry, void (*func)(key_type key, void *val)) { diff --git a/lib/spdylay_map.h b/lib/spdylay_map.h index d95dceb..d78b2cd 100644 --- a/lib/spdylay_map.h +++ b/lib/spdylay_map.h @@ -46,6 +46,7 @@ typedef struct spdylay_map_entry { typedef struct { spdylay_map_entry *root; + size_t size; } spdylay_map; int spdylay_map_init(spdylay_map *map); @@ -58,6 +59,8 @@ void* spdylay_map_find(spdylay_map *map, key_type key); void spdylay_map_erase(spdylay_map *map, key_type key); +size_t spdylay_map_size(spdylay_map *map); + void spdylay_map_each(spdylay_map *map, void (*func)(key_type key, void *val)); #endif /* SPDYLAY_MAP_H */ diff --git a/tests/spdylay_map_test.c b/tests/spdylay_map_test.c index 0c67c90..c1290e8 100644 --- a/tests/spdylay_map_test.c +++ b/tests/spdylay_map_test.c @@ -35,19 +35,28 @@ void test_spdylay_map() CU_ASSERT(0 == spdylay_map_init(&map)); CU_ASSERT(0 == spdylay_map_insert(&map, 1, "foo")); CU_ASSERT(strcmp("foo", spdylay_map_find(&map, 1)) == 0); + CU_ASSERT(1 == spdylay_map_size(&map)); CU_ASSERT(SPDYLAY_ERR_INVALID_ARGUMENT == spdylay_map_insert(&map, 1, "FOO")); + CU_ASSERT(1 == spdylay_map_size(&map)); CU_ASSERT(strcmp("foo", spdylay_map_find(&map, 1)) == 0); CU_ASSERT(0 == spdylay_map_insert(&map, 2, "bar")); + CU_ASSERT(2 == spdylay_map_size(&map)); CU_ASSERT(0 == spdylay_map_insert(&map, 3, "baz")); + CU_ASSERT(3 == spdylay_map_size(&map)); CU_ASSERT(0 == spdylay_map_insert(&map, 4, "shrubbery")); + CU_ASSERT(4 == spdylay_map_size(&map)); CU_ASSERT(strcmp("baz", spdylay_map_find(&map, 3)) == 0); spdylay_map_erase(&map, 3); + CU_ASSERT(3 == spdylay_map_size(&map)); CU_ASSERT(NULL == spdylay_map_find(&map, 3)); spdylay_map_erase(&map, 1); + CU_ASSERT(2 == spdylay_map_size(&map)); CU_ASSERT(NULL == spdylay_map_find(&map, 1)); + /* Erasing non-existent entry */ spdylay_map_erase(&map, 1); + CU_ASSERT(2 == spdylay_map_size(&map)); CU_ASSERT(NULL == spdylay_map_find(&map, 1)); CU_ASSERT(strcmp("bar", spdylay_map_find(&map, 2)) == 0);