Browse files

Change signature of insert method.

  • Loading branch information...
1 parent 106a67c commit b27c1edd84b39f8c1cfaa288d77c0479c3036124 @fmela committed Jan 3, 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
10 anagram.c
@@ -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
3 include/dict.h
@@ -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
3 include/hashtable.h
@@ -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
3 include/hb_tree.h
@@ -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
3 include/pr_tree.h
@@ -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
3 include/rb_tree.h
@@ -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
3 include/skiplist.h
@@ -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
3 include/sp_tree.h
@@ -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
3 include/tr_tree.h
@@ -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
3 include/wb_tree.h
@@ -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
20 src/hashtable.c
@@ -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
23 src/hb_tree.c
@@ -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
55 src/pr_tree.c
@@ -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
20 src/rb_tree.c
@@ -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
View
37 src/skiplist.c
@@ -96,8 +96,7 @@ static itor_vtable skiplist_itor_vtable = {
};
static skip_node* node_new(void* key, unsigned link_count);
-static bool node_insert(skiplist* list, void* key,
- void*** datum_location, skip_node** update);
+static void** node_insert(skiplist* list, void* key, skip_node** update);
static unsigned rand_link_count(skiplist* list);
skiplist*
@@ -161,8 +160,9 @@ skiplist_clone(skiplist* list, dict_key_datum_clone_func clone_func)
if (clone) {
skip_node* node = list->head->link[0];
while (node) {
- void** datum = NULL;
- if (!skiplist_insert(clone, node->key, &datum) || !datum) {
+ bool inserted = false;
+ void** datum = skiplist_insert(clone, node->key, &inserted);
+ if (!datum || !inserted) {
skiplist_free(clone);
return NULL;
}
@@ -180,20 +180,15 @@ skiplist_clone(skiplist* list, dict_key_datum_clone_func clone_func)
return clone;
}
-static bool
-node_insert(skiplist* list, void* key, void*** datum_location,
- skip_node** update)
+static void**
+node_insert(skiplist* list, void* key, skip_node** update)
{
const unsigned nlinks = rand_link_count(list);
ASSERT(nlinks < list->max_link);
skip_node* x = node_new(key, nlinks);
if (!x) {
- if (datum_location)
- *datum_location = NULL;
- return false;
+ return NULL;
}
- if (datum_location)
- *datum_location = &x->datum;
if (list->top_link < nlinks) {
for (unsigned k = list->top_link+1; k <= nlinks; k++) {
@@ -212,11 +207,11 @@ node_insert(skiplist* list, void* key, void*** datum_location,
update[k]->link[k] = x;
}
++list->count;
- return true;
+ return &x->datum;
}
-bool
-skiplist_insert(skiplist* list, void* key, void*** datum_location)
+void**
+skiplist_insert(skiplist* list, void* key, bool* inserted)
{
ASSERT(list != NULL);
@@ -230,11 +225,15 @@ skiplist_insert(skiplist* list, void* key, void*** datum_location)
}
x = x->link[0];
if (x && list->cmp_func(key, x->key) == 0) {
- if (datum_location)
- *datum_location = &x->datum;
- return false;
+ if (inserted)
+ *inserted = false;
+ return &x->datum;
+ }
+ void **datum = node_insert(list, key, update);
+ if (datum) {
+ *inserted = true;
}
- return node_insert(list, key, datum_location, update);
+ return datum;
}
void*
View
23 src/sp_tree.c
@@ -287,8 +287,8 @@ splay(sp_tree* t, sp_node* n)
t->rotation_count += rotations;
}
-bool
-sp_tree_insert(sp_tree* tree, void* key, void*** datum_location)
+void**
+sp_tree_insert(sp_tree* tree, void* key, bool* inserted)
{
ASSERT(tree != NULL);
@@ -302,19 +302,16 @@ sp_tree_insert(sp_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 false;
- }
- if (datum_location)
- *datum_location = &node->datum;
+ if (!(node = node_new(key)))
+ return NULL;
+ if (inserted)
+ *inserted = true;
if (!(node->parent = parent)) {
ASSERT(tree->count == 0);
ASSERT(tree->root == NULL);
@@ -329,7 +326,7 @@ sp_tree_insert(sp_tree* tree, void* key, void*** datum_location)
++tree->count;
}
ASSERT(tree->root == node);
- return true;
+ return &node->datum;
}
void*
View
23 src/tr_tree.c
@@ -163,8 +163,8 @@ tr_tree_clear(tr_tree* tree)
return tree_clear(tree);
}
-bool
-tr_tree_insert(tr_tree* tree, void* key, void*** datum_location)
+void**
+tr_tree_insert(tr_tree* tree, void* key, bool* inserted)
{
ASSERT(tree != NULL);
@@ -178,19 +178,16 @@ tr_tree_insert(tr_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 false;
- }
- if (datum_location)
- *datum_location = &node->datum;
+ if (!(node = node_new(key)))
+ return NULL;
+ if (inserted)
+ *inserted = true;
node->prio = tree->prio_func ? tree->prio_func(key) :
(tree->randgen = tree->randgen * RGEN_A + RGEN_M);
@@ -216,7 +213,7 @@ tr_tree_insert(tr_tree* tree, void* key, void*** datum_location)
tree->rotation_count += rotations;
}
++tree->count;
- return true;
+ return &node->datum;
}
bool
View
24 src/wb_tree.c
@@ -253,8 +253,8 @@ fixup(wb_tree* tree, wb_node* n) {
return rotations;
}
-bool
-wb_tree_insert(wb_tree* tree, void* key, void*** datum_location)
+void**
+wb_tree_insert(wb_tree* tree, void* key, bool* inserted)
{
int cmp = 0;
@@ -269,19 +269,17 @@ wb_tree_insert(wb_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 false;
- }
- if (datum_location)
- *datum_location = &node->datum;
+ wb_node *add = node = node_new(key);
+ if (!add)
+ return NULL;
+ if (inserted)
+ *inserted = true;
if (!(node->parent = parent)) {
ASSERT(tree->count == 0);
ASSERT(tree->root == NULL);
@@ -301,7 +299,7 @@ wb_tree_insert(wb_tree* tree, void* key, void*** datum_location)
tree->rotation_count += rotations;
}
++tree->count;
- return true;
+ return &add->datum;
}
bool
View
5 test.c
@@ -150,8 +150,9 @@ main(int argc, char **argv)
timer_start(&start);
for (unsigned i = 0; i < nwords; i++) {
- void **datum_location = NULL;
- if (!dict_insert(dct, words[i], &datum_location))
+ bool inserted = false;
+ void **datum_location = dict_insert(dct, words[i], &inserted);
+ if (!inserted)
quit("insert #%d failed for '%s'", i, words[i]);
ASSERT(datum_location != NULL);
ASSERT(*datum_location == NULL);
View
31 unit_tests.c
@@ -162,9 +162,11 @@ void test_basic(dict *dct, const struct key_info *keys, const unsigned nkeys) {
CU_ASSERT_TRUE(dict_verify(dct));
for (unsigned i = 0; i < nkeys; ++i) {
- void **datum_location = NULL;
- CU_ASSERT_TRUE(dict_insert(dct, keys[i].key, &datum_location));
+ bool inserted = false;
+ void **datum_location = dict_insert(dct, keys[i].key, &inserted);
+ CU_ASSERT_TRUE(inserted);
CU_ASSERT_PTR_NOT_NULL(datum_location);
+ CU_ASSERT_PTR_NULL(*datum_location);
*datum_location = keys[i].value;
CU_ASSERT_TRUE(dict_verify(dct));
@@ -205,8 +207,9 @@ void test_basic(dict *dct, const struct key_info *keys, const unsigned nkeys) {
CU_ASSERT_EQUAL(dict_search(dct, keys[i].key), keys[i].value);
for (unsigned i = 0; i < nkeys; ++i) {
- void **datum_location = NULL;
- CU_ASSERT_FALSE(dict_insert(dct, keys[i].key, &datum_location));
+ bool inserted = false;
+ void **datum_location = dict_insert(dct, keys[i].key, &inserted);
+ CU_ASSERT_FALSE(inserted);
CU_ASSERT_PTR_NOT_NULL(datum_location);
CU_ASSERT_EQUAL(*datum_location, keys[i].value);
@@ -221,6 +224,7 @@ void test_basic(dict *dct, const struct key_info *keys, const unsigned nkeys) {
for (dict_itor_first(itor); dict_itor_valid(itor); dict_itor_next(itor)) {
CU_ASSERT_PTR_NOT_NULL(dict_itor_key(itor));
CU_ASSERT_PTR_NOT_NULL(dict_itor_data(itor));
+ CU_ASSERT_PTR_NOT_NULL(*dict_itor_data(itor));
char *key = dict_itor_key(itor);
bool key_matched = false;
@@ -248,6 +252,7 @@ void test_basic(dict *dct, const struct key_info *keys, const unsigned nkeys) {
for (dict_itor_last(itor); dict_itor_valid(itor); dict_itor_prev(itor)) {
CU_ASSERT_PTR_NOT_NULL(dict_itor_key(itor));
CU_ASSERT_PTR_NOT_NULL(dict_itor_data(itor));
+ CU_ASSERT_PTR_NOT_NULL(*dict_itor_data(itor));
char *key = dict_itor_key(itor);
bool key_matched = false;
@@ -273,9 +278,11 @@ void test_basic(dict *dct, const struct key_info *keys, const unsigned nkeys) {
dict_itor_free(itor);
for (unsigned i = 0; i < nkeys; ++i) {
- void **datum_location = NULL;
- CU_ASSERT_FALSE(dict_insert(dct, keys[i].key, &datum_location));
+ bool inserted = false;
+ void **datum_location = dict_insert(dct, keys[i].key, &inserted);
+ CU_ASSERT_FALSE(inserted);
CU_ASSERT_PTR_NOT_NULL(datum_location);
+ CU_ASSERT_PTR_NOT_NULL(*datum_location);
*datum_location = keys[i].alt;
CU_ASSERT_TRUE(dict_verify(dct));
@@ -300,9 +307,11 @@ void test_basic(dict *dct, const struct key_info *keys, const unsigned nkeys) {
}
for (unsigned i = 0; i < nkeys; ++i) {
- void **datum_location = NULL;
- CU_ASSERT_TRUE(dict_insert(dct, keys[i].key, &datum_location));
+ bool inserted = false;
+ void **datum_location = dict_insert(dct, keys[i].key, &inserted);
+ CU_ASSERT_TRUE(inserted);
CU_ASSERT_PTR_NOT_NULL(datum_location);
+ CU_ASSERT_PTR_NULL(*datum_location);
*datum_location = keys[i].value;
CU_ASSERT_TRUE(dict_verify(dct));
@@ -311,9 +320,11 @@ void test_basic(dict *dct, const struct key_info *keys, const unsigned nkeys) {
CU_ASSERT_EQUAL(dict_clear(dct), nkeys);
for (unsigned i = 0; i < nkeys; ++i) {
- void **datum_location = NULL;
- CU_ASSERT_TRUE(dict_insert(dct, keys[i].key, &datum_location));
+ bool inserted = false;
+ void **datum_location = dict_insert(dct, keys[i].key, &inserted);
+ CU_ASSERT_TRUE(inserted);
CU_ASSERT_PTR_NOT_NULL(datum_location);
+ CU_ASSERT_PTR_NULL(*datum_location);
*datum_location = keys[i].value;
CU_ASSERT_TRUE(dict_verify(dct));

0 comments on commit b27c1ed

Please sign in to comment.