summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2024-12-11 10:02:11 -0500
committerDavid Robillard <d@drobilla.net>2024-12-11 10:06:33 -0500
commit52048ee6db71982693cdeb603e725a867afd99e5 (patch)
treea98f5c1cb987d4ebeaa228ccf7481863a2bdfb02
parent09bfde1eb9b70cd32b8ca3c1cc9742d661277e3d (diff)
downloadzix-52048ee6db71982693cdeb603e725a867afd99e5.tar.gz
zix-52048ee6db71982693cdeb603e725a867afd99e5.tar.bz2
zix-52048ee6db71982693cdeb603e725a867afd99e5.zip
Remove old tree_debug.h header
-rw-r--r--src/tree.c34
-rw-r--r--src/tree_debug.h139
2 files changed, 6 insertions, 167 deletions
diff --git a/src/tree.c b/src/tree.c
index 33900cd..7ca0cb0 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -33,13 +33,12 @@ struct ZixTreeNodeImpl {
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
-// Uncomment these for debugging features
-// #define ZIX_TREE_VERIFY 1
-// #define ZIX_TREE_HYPER_VERIFY 1
-
-#if defined(ZIX_TREE_VERIFY) || defined(ZIX_TREE_HYPER_VERIFY)
-# include "tree_debug.h"
-# define ASSERT_BALANCE(n) assert(verify_balance(n))
+#ifndef NDEBUG
+# define ASSERT_BALANCE(n) \
+ assert(!(((n)->balance < -2 || (n)->balance > 2) || \
+ ((n)->balance < 0 && !(n)->left) || \
+ ((n)->balance > 0 && !(n)->right) || \
+ ((n)->balance != 0 && !(n)->left && !(n)->right)))
#else
# define ASSERT_BALANCE(n)
#endif
@@ -284,10 +283,6 @@ rotate_right_left(ZixTreeNode* p, int* height_change)
static ZixTreeNode*
zix_tree_rebalance(ZixTree* t, ZixTreeNode* node, int* height_change)
{
-#ifdef ZIX_TREE_HYPER_VERIFY
- const size_t old_height = height(node);
-#endif
-
*height_change = 0;
const bool is_root = !node->parent;
@@ -314,10 +309,6 @@ zix_tree_rebalance(ZixTree* t, ZixTreeNode* node, int* height_change)
t->root = replacement;
}
-#ifdef ZIX_TREE_HYPER_VERIFY
- assert(old_height + *height_change == height(replacement));
-#endif
-
return replacement;
}
@@ -397,12 +388,6 @@ zix_tree_insert(ZixTree* t, void* e, ZixTreeIter** ti)
++t->size;
-#ifdef ZIX_TREE_VERIFY
- if (!verify(t, t->root)) {
- return ZIX_STATUS_ERROR;
- }
-#endif
-
return ZIX_STATUS_SUCCESS;
}
@@ -550,13 +535,6 @@ zix_tree_remove(ZixTree* t, ZixTreeIter* ti)
zix_free(t->allocator, n);
--t->size;
-
-#ifdef ZIX_TREE_VERIFY
- if (!verify(t, t->root)) {
- return ZIX_STATUS_ERROR;
- }
-#endif
-
return ZIX_STATUS_SUCCESS;
}
diff --git a/src/tree_debug.h b/src/tree_debug.h
deleted file mode 100644
index b1a2d2e..0000000
--- a/src/tree_debug.h
+++ /dev/null
@@ -1,139 +0,0 @@
-// Copyright 2011-2020 David Robillard <d@drobilla.net>
-// SPDX-License-Identifier: ISC
-
-#ifndef ZIX_TREE_DEBUG_H
-#define ZIX_TREE_DEBUG_H
-
-#include <inttypes.h>
-
-#ifdef ZIX_TREE_HYPER_VERIFY
-static size_t
-height(ZixTreeNode* n)
-{
- if (!n) {
- return 0;
- } else {
- return 1 + MAX(height(n->left), height(n->right));
- }
-}
-#endif
-
-#if defined(ZIX_TREE_VERIFY) || !defined(NDEBUG)
-static bool
-verify_balance(ZixTreeNode* n)
-{
- if (!n) {
- return true;
- }
-
- if (n->balance < -2 || n->balance > 2) {
- fprintf(stderr,
- "Balance out of range : %ld (balance %d)\n",
- (intptr_t)n->data,
- n->balance);
- return false;
- }
-
- if (n->balance < 0 && !n->left) {
- fprintf(stderr,
- "Bad balance : %ld (balance %d) has no left child\n",
- (intptr_t)n->data,
- n->balance);
- return false;
- }
-
- if (n->balance > 0 && !n->right) {
- fprintf(stderr,
- "Bad balance : %ld (balance %d) has no right child\n",
- (intptr_t)n->data,
- n->balance);
- return false;
- }
-
- if (n->balance != 0 && !n->left && !n->right) {
- fprintf(stderr,
- "Bad balance : %ld (balance %d) has no children\n",
- (intptr_t)n->data,
- n->balance);
- return false;
- }
-
-# ifdef ZIX_TREE_HYPER_VERIFY
- const intptr_t left_height = (intptr_t)height(n->left);
- const intptr_t right_height = (intptr_t)height(n->right);
- if (n->balance != right_height - left_height) {
- fprintf(stderr,
- "Bad balance at %ld: h_r (%" PRIdPTR ")"
- "- l_h (%" PRIdPTR ") != %d\n",
- (intptr_t)n->data,
- right_height,
- left_height,
- n->balance);
- assert(false);
- return false;
- }
-# endif
-
- return true;
-}
-#endif
-
-#ifdef ZIX_TREE_VERIFY
-static bool
-verify(ZixTree* t, ZixTreeNode* n)
-{
- if (!n) {
- return true;
- }
-
- if (n->parent) {
- if ((n->parent->left != n) && (n->parent->right != n)) {
- fprintf(stderr, "Corrupt child/parent pointers\n");
- return false;
- }
- }
-
- if (n->left) {
- if (t->cmp(n->left->data, n->data, t->cmp_data) > 0) {
- fprintf(
- stderr, "Bad order: %" PRIdPTR " with left child\n", (intptr_t)n->data);
- fprintf(stderr, "%p ? %p\n", (void*)n, (void*)n->left);
- fprintf(stderr,
- "%" PRIdPTR " ? %" PRIdPTR "\n",
- (intptr_t)n->data,
- (intptr_t)n->left->data);
- return false;
- }
-
- if (!verify(t, n->left)) {
- return false;
- }
- }
-
- if (n->right) {
- if (t->cmp(n->right->data, n->data, t->cmp_data) < 0) {
- fprintf(stderr,
- "Bad order: %" PRIdPTR " with right child\n",
- (intptr_t)n->data);
- return false;
- }
-
- if (!verify(t, n->right)) {
- return false;
- }
- }
-
- if (!verify_balance(n)) {
- return false;
- }
-
- if (n->balance <= -2 || n->balance >= 2) {
- fprintf(stderr, "Imbalance: %p (balance %d)\n", (void*)n, n->balance);
- return false;
- }
-
- return true;
-}
-#endif
-
-#endif // ZIX_TREE_DEBUG_H