void mp_flush_string(MP mp,mp_string s){ if(s->refs==0){ mp->strs_in_use--; mp->pool_in_use= mp->pool_in_use-(integer)s->len; (void)avl_del(s,mp->strings,NULL); } }
int ht_del( struct ht* ht, void const* cmp, struct r_set_cfg const* cfg ) { r_hash hash = cfg->hashf(cmp); size_t i = bucket_index(ht, hash); ht_dbg("Deleting element with hash %zi in bucket %zi", hash, i); return avl_del(&ht->buckets[i].avl, hash, cmp, cfg); }
Py_LOCAL(PyObject *) avl_tree_rem(avl_tree_Object * self, PyObject * val) { int rv; Py_INCREF(val); rv = avl_del((void *) val, self->tree, NULL); Py_DECREF(val); if (rv < 0) return NULL; Py_INCREF(Py_None); return Py_None; }
int main(int argc, char *argv[]) { int num = 1000000; avl_root *root = NULL; if (argc > 1) num = atoi(argv[1]); uint64_t seed = time(NULL); RAND_NR_INIT(u, v, w, seed); ulib_timer_t timer; timer_start(&timer); for (int i = 0; i < num; ++i) { avl_node *t = new avl_node; t->key = myrand(); if (&t->link != avl_map(&t->link, avl_node_cmp, &root)) delete t; } printf("Inserting 1M elems elapsed: %f\n", timer_stop(&timer)); printf("Height: %d\n", TREE_HEIGHT(root)); timer_start(&timer); for (int i = 0; i < 1000000; ++i) { avl_node t; t.key = myrand(); TREE_SEARCH(&t.link, avl_node_cmp, root); } printf("Searching 10M elems elapsed: %f\n", timer_stop(&timer)); avl_node *pos, *tmp; timer_start(&timer); avl_for_each_entry_safe(pos, tmp, root, link) { avl_del(&pos->link, &root); delete pos; }
void test_avl(void) { /* Test AVL tree functions */ avl_node *root = NULL; /* Insert strings a-z, zz-za, aa-az to hopefully excersize all combinations of rotations */ const char **ptr, *strings[] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "zz", "zy", "zx", "zw", "zv", "zu", "zt", "zs", "zr", "zq", "zp", "zo", "zn", "zm", "zl", "zk", "zj", "zi", "zh", "zg", "zf", "ze", "zd", "zc", "zb", "za", "aa", "ab", "ac", "ad", "ae", "af", "ag", "ah", "ai", "aj", "ak", "al", "am", "an", "ao", "ap", "aq", "ar", "as", "at", "au", "av", "aw", "ax", "ay", "az", NULL }; int ret, count = 0; /* Add all strings to the tree */ for (ptr = strings; *ptr; ptr++) { if (!avl_add(&root, *ptr, NULL)) fail_testf("avl_add() failed to add %s", *ptr); else count++; } /* Check that all were added correctly */ if ((ret = _avl_count(root)) != count) fail_testf("_avl_count() returned %d, expected %d", ret, count); /* Check that the binary properties of the tree hold */ if (!_avl_is_binary(root)) fail_test("_avl_is_binary() failed"); /* Check that the height of root stays within the mathematical bounds AVL trees allow */ double max_height = 1.44 * log2(count + 2) - 0.328; if ((ret = _avl_height(root)) >= max_height) fail_testf("_avl_height() returned %d, expected strictly less than %f", ret, max_height); /* Test adding a duplicate and case sensitive duplicate */ if (avl_add(&root, "aa", NULL) && count++) fail_test("avl_add() failed to detect duplicate 'aa'"); if (avl_add(&root, "aA", NULL) && count++) fail_test("avl_add() failed to detect case sensitive duplicate 'aA'"); /* Delete about half of the strings */ int num_delete = count / 2; for (ptr = strings; *ptr && num_delete > 0; ptr++, num_delete--) { if (!avl_del(&root, *ptr)) fail_testf("avl_del() failed to delete %s", *ptr); else count--; } /* Check that all were deleted correctly */ if ((ret = _avl_count(root)) != count) fail_testf("_avl_count() returned %d, expected %d", ret, count); /* Check that the binary properties of the tree still hold */ if (!_avl_is_binary(root)) fail_test("_avl_is_binary() failed"); /* Check that the height of root is still within the mathematical bounds AVL trees allow */ max_height = 1.44 * log2(count + 2) - 0.328; if ((ret = _avl_height(root)) >= max_height) fail_testf("_avl_height() returned %d, expected strictly less than %f", ret, max_height); /* Test deleting string that was previously deleted */ if (avl_del(&root, *strings)) fail_testf("_avl_del() should have failed to delete %s", *strings); }