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)
{
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,25 +166,35 @@ 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);
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);
entry->right = erase_recur(entry->right, key, error);
} else {
entry = erase_rotate_recur(entry);
}
}
return entry;
}
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,

View File

@ -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 */

View File

@ -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);