Added spdylay_map_size()

This commit is contained in:
Tatsuhiro Tsujikawa 2012-01-26 02:28:58 +09:00
parent 6e627548be
commit f642bb98c7
3 changed files with 36 additions and 10 deletions

View File

@ -27,6 +27,7 @@
int spdylay_map_init(spdylay_map *map) int spdylay_map_init(spdylay_map *map)
{ {
map->root = NULL; map->root = NULL;
map->size = 0;
return 0; return 0;
} }
@ -125,6 +126,9 @@ int spdylay_map_insert(spdylay_map *map, key_type key, void *val)
{ {
int error = 0; int error = 0;
map->root = insert_recur(map->root, key, val, &error); map->root = insert_recur(map->root, key, val, &error);
if(!error) {
++map->size;
}
return error; 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(entry == NULL) {
if(key < entry->key) { *error = SPDYLAY_ERR_INVALID_ARGUMENT;
entry->left = erase_recur(entry->left, key); } else if(key < entry->key) {
} else if(key > entry->key) { entry->left = erase_recur(entry->left, key, error);
entry->right = erase_recur(entry->right, key); } else if(key > entry->key) {
} else { entry->right = erase_recur(entry->right, key, error);
entry = erase_rotate_recur(entry); } else {
} entry = erase_rotate_recur(entry);
} }
return 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) void spdylay_map_erase(spdylay_map *map, key_type key)
{ {
if(map->root != NULL) { 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, static void for_each(spdylay_map_entry *entry,
void (*func)(key_type key, void *val)) void (*func)(key_type key, void *val))
{ {

View File

@ -46,6 +46,7 @@ typedef struct spdylay_map_entry {
typedef struct { typedef struct {
spdylay_map_entry *root; spdylay_map_entry *root;
size_t size;
} spdylay_map; } spdylay_map;
int spdylay_map_init(spdylay_map *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); 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)); void spdylay_map_each(spdylay_map *map, void (*func)(key_type key, void *val));
#endif /* SPDYLAY_MAP_H */ #endif /* SPDYLAY_MAP_H */

View File

@ -35,19 +35,28 @@ void test_spdylay_map()
CU_ASSERT(0 == spdylay_map_init(&map)); CU_ASSERT(0 == spdylay_map_init(&map));
CU_ASSERT(0 == spdylay_map_insert(&map, 1, "foo")); CU_ASSERT(0 == spdylay_map_insert(&map, 1, "foo"));
CU_ASSERT(strcmp("foo", spdylay_map_find(&map, 1)) == 0); 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(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(strcmp("foo", spdylay_map_find(&map, 1)) == 0);
CU_ASSERT(0 == spdylay_map_insert(&map, 2, "bar")); 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(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(0 == spdylay_map_insert(&map, 4, "shrubbery"));
CU_ASSERT(4 == spdylay_map_size(&map));
CU_ASSERT(strcmp("baz", spdylay_map_find(&map, 3)) == 0); CU_ASSERT(strcmp("baz", spdylay_map_find(&map, 3)) == 0);
spdylay_map_erase(&map, 3); spdylay_map_erase(&map, 3);
CU_ASSERT(3 == spdylay_map_size(&map));
CU_ASSERT(NULL == spdylay_map_find(&map, 3)); CU_ASSERT(NULL == spdylay_map_find(&map, 3));
spdylay_map_erase(&map, 1); spdylay_map_erase(&map, 1);
CU_ASSERT(2 == spdylay_map_size(&map));
CU_ASSERT(NULL == spdylay_map_find(&map, 1)); CU_ASSERT(NULL == spdylay_map_find(&map, 1));
/* Erasing non-existent entry */
spdylay_map_erase(&map, 1); spdylay_map_erase(&map, 1);
CU_ASSERT(2 == spdylay_map_size(&map));
CU_ASSERT(NULL == spdylay_map_find(&map, 1)); CU_ASSERT(NULL == spdylay_map_find(&map, 1));
CU_ASSERT(strcmp("bar", spdylay_map_find(&map, 2)) == 0); CU_ASSERT(strcmp("bar", spdylay_map_find(&map, 2)) == 0);