#define RED 1
#define BLACK 0
-#define ASSERT_INVARIANTS
-
struct redblack_node;
typedef struct redblack_node Node;
_rbNodeSetColor(tree->root, BLACK);
}
-static void
-_rbNodeAssertNodeInvariants(Node *node, void *data)
-{
- PARCTreeRedBlack *tree = (PARCTreeRedBlack *) data;
- assertNotNull(node->parent, "Node has NULL parent");
- assertNotNull(node->left_child, "Left child NULL");
- assertNotNull(node->right_child, "Richt child NULL");
- if (node != tree->root) {
- assertTrue(node->parent != tree->nil, "Paren't can't be nill for node!");
- // Don't need to compare to parent, they compared to us
- }
- assertNotNull(node->key, "We have a null key!!");
- assertNotNull(node->value, "We have a null value!!");
- if (node->left_child != tree->nil) {
- assertTrue(tree->keyCompare(node->key, node->left_child->key) > 0, "Left child not smaller?");
- }
- if (node->right_child != tree->nil) {
- assertTrue(tree->keyCompare(node->key, node->right_child->key) < 0, "Right child not bigger?");
- }
-}
-
-static
-void
-_rbNodeAssertTreeInvariants(const PARCTreeRedBlack *tree)
-{
- assertNotNull(tree, "Tree is null!");
- assertTrue(tree->size >= 0, "Tree has negative size");
- assertNotNull(tree->keyCompare, "No key compare function");
- if (tree->size != 0) {
- assertTrue(tree->root != tree->nil, "Tree size = %d > 0 but root is nil", tree->size);
- assertNotNull(tree->root, "Tree size > 0 but root is NULL");
-#ifdef ASSERT_INVARIANTS
- _rbNodeRecursiveRun((PARCTreeRedBlack *) tree, tree->root, _rbNodeAssertNodeInvariants, (void *) tree);
-#endif
- }
-}
-
static void
_rbNodeFixDelete(PARCTreeRedBlack *tree, Node *node)
{
Node *fixNode;
while ((node != tree->root) && (_rbNodeColor(node) == BLACK)) {
- _rbNodeAssertTreeInvariants(tree);
if (node == node->parent->left_child) {
fixNode = node->parent->right_child;
if (_rbNodeColor(fixNode) == RED) {
static void
_rbNodeRemove(PARCTreeRedBlack *tree, Node *node)
{
- _rbNodeAssertTreeInvariants(tree);
Node *fixupNode;
int deleteNodeColor = _rbNodeColor(node);
if (node->left_child == tree->nil) {
tree->size--;
// Fix the red-blackness
- _rbNodeAssertTreeInvariants(tree);
if (deleteNodeColor == BLACK) {
_rbNodeFixDelete(tree, fixupNode);
}
- _rbNodeAssertTreeInvariants(tree);
}
{
assertNotNull(treePointer, "pointer to pointer to tree can't be null");
assertNotNull(*treePointer, "pointer to tree can't be null");
- _rbNodeAssertTreeInvariants(*treePointer);
if ((*treePointer)->size > 0) {
// If we have any elements in the tree, free them
// We have a correct tree. But we need to regain the red-black property.
_rbNodeFix(tree, newNode);
- _rbNodeAssertTreeInvariants(tree);
}
// Return value
parcTreeRedBlack_Get(PARCTreeRedBlack *tree, const void *key)
{
assertNotNull(tree, "Tree can't be NULL");
- _rbNodeAssertTreeInvariants(tree);
Node *node = tree->root;
{
assertNotNull(tree, "Tree can't be NULL");
assertNotNull(key, "Key can't be NULL");
- _rbNodeAssertTreeInvariants(tree);
Node *node = tree->root;
tree->keyFree(&node->key);
}
parcMemory_Deallocate((void **) &node);
- _rbNodeAssertTreeInvariants(tree);
return value;
} else {
if (_rbNodeIsGreaterThan(tree, node, key)) {
// We didn't find the node
- _rbNodeAssertTreeInvariants(tree);
return NULL;
}
if (_rbNodeIsEqual(tree, node, key)) {
_rbNodeRemove(tree, node);
_rbNodeFree(tree, node);
- _rbNodeAssertTreeInvariants(tree);
return;
} else {
if (_rbNodeIsGreaterThan(tree, node, key)) {
}
}
}
- _rbNodeAssertTreeInvariants(tree);
}
void *
parcTreeRedBlack_LastKey(const PARCTreeRedBlack *tree)
{
assertNotNull(tree, "Tree can't be NULL");
- _rbNodeAssertTreeInvariants(tree);
Node *node = tree->root;
if (tree->size == 0) {
parcTreeRedBlack_FirstKey(const PARCTreeRedBlack *tree)
{
assertNotNull(tree, "Tree can't be NULL");
- _rbNodeAssertTreeInvariants(tree);
Node *node = tree->root;
parcTreeRedBlack_Size(const PARCTreeRedBlack *tree)
{
assertNotNull(tree, "Tree can't be NULL");
- _rbNodeAssertTreeInvariants(tree);
return tree->size;
}
parcTreeRedBlack_Keys(const PARCTreeRedBlack *tree)
{
assertNotNull(tree, "Tree can't be NULL");
- _rbNodeAssertTreeInvariants(tree);
PARCArrayList *keys = parcArrayList_Create(NULL);
parcTreeRedBlack_Values(const PARCTreeRedBlack *tree)
{
assertNotNull(tree, "Tree can't be NULL");
- _rbNodeAssertTreeInvariants(tree);
PARCArrayList *values = parcArrayList_Create(NULL);
int
parcTreeRedBlack_Equals(const PARCTreeRedBlack *tree1, const PARCTreeRedBlack *tree2)
{
- _rbNodeAssertTreeInvariants(tree1);
- _rbNodeAssertTreeInvariants(tree2);
assertNotNull(tree1, "Tree can't be NULL");
assertNotNull(tree2, "Tree can't be NULL");
PARCTreeRedBlack *
parcTreeRedBlack_Copy(const PARCTreeRedBlack *source_tree)
{
- _rbNodeAssertTreeInvariants(source_tree);
assertNotNull(source_tree, "Tree can't be NULL");
void *key_source;