Permalink
Browse files

Change signature of insert method.

  • Loading branch information...
1 parent 106a67c commit b27c1edd84b39f8c1cfaa288d77c0479c3036124 @fmela committed Jan 4, 2013
Showing with 139 additions and 159 deletions.
  1. +2 −8 anagram.c
  2. +1 −2 include/dict.h
  3. +1 −2 include/hashtable.h
  4. +1 −2 include/hb_tree.h
  5. +1 −2 include/pr_tree.h
  6. +1 −2 include/rb_tree.h
  7. +1 −2 include/skiplist.h
  8. +1 −2 include/sp_tree.h
  9. +1 −2 include/tr_tree.h
  10. +1 −2 include/wb_tree.h
  11. +9 −11 src/hashtable.c
  12. +11 −12 src/hb_tree.c
  13. +26 −29 src/pr_tree.c
  14. +9 −11 src/rb_tree.c
  15. +18 −19 src/skiplist.c
  16. +10 −13 src/sp_tree.c
  17. +10 −13 src/tr_tree.c
  18. +11 −13 src/wb_tree.c
  19. +3 −2 test.c
  20. +21 −10 unit_tests.c
View
@@ -57,15 +57,9 @@ main(int argc, char *argv[])
}
*p = 0;
- WordList *word = xmalloc(sizeof(*word));
+ WordList* word = xmalloc(sizeof(*word));
word->word = xstrdup(buf);
- WordList **wordp = NULL;
- if (rb_tree_insert(tree, xstrdup(name), (void ***)&wordp)) {
- ASSERT(*wordp == NULL);
- } else {
- /* Key already exists. */
- ASSERT(wordp != NULL);
- }
+ WordList** wordp = (WordList**)rb_tree_insert(tree, xstrdup(name), NULL);
word->next = *wordp;
*wordp = word;
}
View
@@ -85,8 +85,7 @@ typedef struct dict_itor dict_itor;
typedef dict_itor* (*dict_inew_func)(void* obj);
typedef size_t (*dict_dfree_func)(void* obj);
-typedef bool (*dict_insert_func)(void* obj, void* key,
- void*** datum_location);
+typedef void** (*dict_insert_func)(void* obj, void* key, bool* inserted);
typedef void* (*dict_search_func)(void* obj, const void* key);
typedef bool (*dict_remove_func)(void* obj, const void* key);
typedef size_t (*dict_clear_func)(void* obj);
View
@@ -49,8 +49,7 @@ size_t hashtable_free(hashtable* table);
hashtable* hashtable_clone(hashtable* table,
dict_key_datum_clone_func clone_func);
-bool hashtable_insert(hashtable* table, void* key,
- void*** datum_location);
+void** hashtable_insert(hashtable* table, void* key, bool* inserted);
void* hashtable_search(hashtable* table, const void* key);
bool hashtable_remove(hashtable* table, const void* key);
size_t hashtable_clear(hashtable* table);
View
@@ -47,8 +47,7 @@ size_t hb_tree_free(hb_tree* tree);
hb_tree* hb_tree_clone(hb_tree* tree,
dict_key_datum_clone_func clone_func);
-bool hb_tree_insert(hb_tree* tree, void* key,
- void*** datum_location);
+void** hb_tree_insert(hb_tree* tree, void* key, bool* inserted);
void* hb_tree_search(hb_tree* tree, const void* key);
bool hb_tree_remove(hb_tree* tree, const void* key);
size_t hb_tree_clear(hb_tree* tree);
View
@@ -47,8 +47,7 @@ size_t pr_tree_free(pr_tree* tree);
pr_tree* pr_tree_clone(pr_tree* tree,
dict_key_datum_clone_func clone_func);
-bool pr_tree_insert(pr_tree* tree, void* key,
- void*** datum_location);
+void** pr_tree_insert(pr_tree* tree, void* key, bool* inserted);
void* pr_tree_search(pr_tree* tree, const void* key);
bool pr_tree_remove(pr_tree* tree, const void* key);
size_t pr_tree_clear(pr_tree* tree);
View
@@ -47,8 +47,7 @@ size_t rb_tree_free(rb_tree* tree);
rb_tree* rb_tree_clone(rb_tree* tree,
dict_key_datum_clone_func clone_func);
-bool rb_tree_insert(rb_tree* tree, void* key,
- void*** datum_location);
+void** rb_tree_insert(rb_tree* tree, void* key, bool* inserted);
void* rb_tree_search(rb_tree* tree, const void* key);
bool rb_tree_remove(rb_tree* tree, const void* key);
size_t rb_tree_clear(rb_tree* tree);
View
@@ -49,8 +49,7 @@ size_t skiplist_free(skiplist* list);
skiplist* skiplist_clone(skiplist* list,
dict_key_datum_clone_func clone_func);
-bool skiplist_insert(skiplist* list, void* key,
- void*** datum_location);
+void** skiplist_insert(skiplist* list, void* key, bool* inserted);
void* skiplist_search(skiplist* list, const void* key);
bool skiplist_remove(skiplist* list, const void* key);
size_t skiplist_clear(skiplist* list);
View
@@ -47,8 +47,7 @@ size_t sp_tree_free(sp_tree* tree);
sp_tree* sp_tree_clone(sp_tree* tree,
dict_key_datum_clone_func clone_func);
-bool sp_tree_insert(sp_tree* tree, void* key,
- void*** datum_location);
+void** sp_tree_insert(sp_tree* tree, void* key, bool* inserted);
void* sp_tree_search(sp_tree* tree, const void* key);
bool sp_tree_remove(sp_tree* tree, const void* key);
size_t sp_tree_clear(sp_tree* tree);
View
@@ -49,8 +49,7 @@ size_t tr_tree_free(tr_tree* tree);
tr_tree* tr_tree_clone(tr_tree* tree,
dict_key_datum_clone_func clone_func);
-bool tr_tree_insert(tr_tree* tree, void* key,
- void*** datum_location);
+void** tr_tree_insert(tr_tree* tree, void* key, bool* inserted);
void* tr_tree_search(tr_tree* tree, const void* key);
bool tr_tree_remove(tr_tree* tree, const void* key);
size_t tr_tree_clear(tr_tree* tree);
View
@@ -47,8 +47,7 @@ size_t wb_tree_free(wb_tree* tree);
wb_tree* wb_tree_clone(wb_tree* tree,
dict_key_datum_clone_func clone_func);
-bool wb_tree_insert(wb_tree* tree, void* key,
- void*** datum_location);
+void** wb_tree_insert(wb_tree* tree, void* key, bool* inserted);
void* wb_tree_search(wb_tree* tree, const void* key);
bool wb_tree_remove(wb_tree* tree, const void* key);
size_t wb_tree_clear(wb_tree* tree);
View
@@ -182,8 +182,8 @@ hashtable_free(hashtable* table)
return count;
}
-bool
-hashtable_insert(hashtable* table, void* key, void*** datum_location)
+void**
+hashtable_insert(hashtable* table, void* key, bool* inserted)
{
ASSERT(table != NULL);
@@ -193,25 +193,23 @@ hashtable_insert(hashtable* table, void* key, void*** datum_location)
hash_node* prev = NULL;
while (node && hash >= node->hash) {
if (hash == node->hash && table->cmp_func(key, node->key) == 0) {
- if (datum_location)
- *datum_location = &node->datum;
- return false;
+ if (inserted)
+ *inserted = false;
+ return &node->datum;
}
prev = node;
node = node->next;
}
hash_node* add = MALLOC(sizeof(*add));
if (!add) {
- if (datum_location)
- *datum_location = NULL;
- return false;
+ return NULL;
}
+ if (inserted)
+ *inserted = true;
add->key = key;
add->datum = NULL;
- if (datum_location)
- *datum_location = &add->datum;
add->hash = hash;
add->prev = prev;
if (prev)
@@ -223,7 +221,7 @@ hashtable_insert(hashtable* table, void* key, void*** datum_location)
node->prev = add;
table->count++;
- return true;
+ return &add->datum;
}
void*
View
@@ -179,8 +179,8 @@ hb_tree_search(hb_tree* tree, const void* key)
return tree_search(tree, key);
}
-bool
-hb_tree_insert(hb_tree* tree, void* key, void*** datum_location)
+void**
+hb_tree_insert(hb_tree* tree, void* key, bool* inserted)
{
ASSERT(tree != NULL);
@@ -195,21 +195,20 @@ hb_tree_insert(hb_tree* tree, void* key, void*** datum_location)
else if (cmp)
parent = node, node = node->rlink;
else {
- if (datum_location)
- *datum_location = &node->datum;
- return false;
+ if (inserted)
+ *inserted = false;
+ return &node->datum;
}
if (parent->bal)
q = parent;
}
- if (!(node = node_new(key))) {
- if (datum_location)
- *datum_location = NULL;
- return false;
+ hb_node *add = node = node_new(key);
+ if (!node) {
+ return NULL;
}
- if (datum_location)
- *datum_location = &node->datum;
+ if (inserted)
+ *inserted = true;
if (!(node->parent = parent)) {
tree->root = node;
ASSERT(tree->count == 0);
@@ -249,7 +248,7 @@ hb_tree_insert(hb_tree* tree, void* key, void*** datum_location)
tree->rotation_count += rotations;
}
++tree->count;
- return true;
+ return &add->datum;
}
bool
View
@@ -287,8 +287,8 @@ fixup(pr_tree* tree, pr_node* node)
return rotations;
}
-bool
-pr_tree_insert(pr_tree* tree, void* key, void*** datum_location)
+void**
+pr_tree_insert(pr_tree* tree, void* key, bool* inserted)
{
ASSERT(tree != NULL);
@@ -302,40 +302,37 @@ pr_tree_insert(pr_tree* tree, void* key, void*** datum_location)
else if (cmp)
parent = node, node = node->rlink;
else {
- if (datum_location)
- *datum_location = &node->datum;
- return false;
+ if (inserted)
+ *inserted = false;
+ return &node->datum;
}
}
- if (!(node = node_new(key))) {
- if (datum_location)
- *datum_location = NULL;
- return -1;
- }
- if (datum_location)
- *datum_location = &node->datum;
- if (!(node->parent = parent)) {
+ pr_node *add = node_new(key);
+ if (!add)
+ return NULL;
+ if (inserted)
+ *inserted = true;
+ if (!(add->parent = parent)) {
ASSERT(tree->count == 0);
- tree->root = node;
- tree->count = 1;
- return true;
- }
- if (cmp < 0)
- parent->llink = node;
- else
- parent->rlink = node;
+ ASSERT(tree->root == NULL);
+ tree->root = add;
+ } else {
+ if (cmp < 0)
+ parent->llink = add;
+ else
+ parent->rlink = add;
- unsigned rotations = 0;
- while ((node = parent) != NULL) {
- parent = parent->parent;
- ++node->weight;
- rotations += fixup(tree, node);
+ unsigned rotations = 0;
+ while ((node = parent) != NULL) {
+ parent = parent->parent;
+ ++node->weight;
+ rotations += fixup(tree, node);
+ }
+ tree->rotation_count += rotations;
}
-
++tree->count;
- tree->rotation_count += rotations;
- return true;
+ return &add->datum;
}
bool
View
@@ -201,8 +201,8 @@ rb_tree_search(rb_tree* tree, const void* key)
return NULL;
}
-bool
-rb_tree_insert(rb_tree* tree, void* key, void*** datum_location)
+void**
+rb_tree_insert(rb_tree* tree, void* key, bool* inserted)
{
ASSERT(tree != NULL);
@@ -216,19 +216,17 @@ rb_tree_insert(rb_tree* tree, void* key, void*** datum_location)
else if (cmp)
parent = node, node = RLINK(node);
else {
- if (datum_location)
- *datum_location = &node->datum;
- return false;
+ if (inserted)
+ *inserted = false;
+ return &node->datum;
}
}
if (!(node = node_new(key))) {
- if (datum_location)
- *datum_location = NULL;
- return false;
+ return NULL;
}
- if (datum_location)
- *datum_location = &node->datum;
+ if (inserted)
+ *inserted = true;
if ((node->parent = parent) == RB_NULL) {
tree->root = node;
ASSERT(tree->count == 0);
@@ -242,7 +240,7 @@ rb_tree_insert(rb_tree* tree, void* key, void*** datum_location)
tree->rotation_count += insert_fixup(tree, node);
}
++tree->count;
- return true;
+ return &node->datum;
}
static unsigned
Oops, something went wrong.

0 comments on commit b27c1ed

Please sign in to comment.