Permalink
Browse files

Fixes size issue with HklHash

Uses the new size implemented in HklTree to properly update the size of
the hash when a value is attempted to be removed from a tree in the
hash.
  • Loading branch information...
1 parent 642bbb3 commit a52b489fb62d67479081a3da6dd6632cd8c03d5f @cknadler cknadler committed Jan 9, 2013
Showing with 18 additions and 1 deletion.
  1. +14 −1 src/hkl_hash.c
  2. +4 −0 src/hkl_hash.h
View
@@ -160,6 +160,8 @@ void hkl_hash_insert(HklHash* hash, HklString* key, void* value)
hkl_tree_move_pair((HklTree*) element->data, pair);
hkl_tree_insert((HklTree*) element->data, key, value);
+
+ ++hash->length;
// Mark the element as a tree
element->is_tree = true;
@@ -239,7 +241,18 @@ void hkl_hash_remove(HklHash* hash, HklString *key)
if (element->is_tree)
{
- hkl_tree_remove((HklTree*) element->data, key);
+ HklTree* tree = element->data;
+
+ // Keep track of the old tree size
+ size_t old_size = tree->size;
+
+ // Attempt to remove the element from the tree
+ hkl_tree_remove(tree, key);
+
+ // Subtract the difference between the old and new
+ // tree sizes from the hash length
+ assert((old_size - tree->size) <= 1);
+ hash->length -= old_size - tree->size;
}
else
{
View
@@ -17,7 +17,11 @@ typedef struct HklHash
{
struct HklHashElement* buckets;
+
+ // Total number of elements (not buckets)
size_t length;
+
+ // Total number of buckets
size_t size;
} HklHash;

0 comments on commit a52b489

Please sign in to comment.