Skip to content

Commit

Permalink
add debugging support to IBSTree.h
Browse files Browse the repository at this point in the history
  • Loading branch information
jmellorcrummey authored and Sasha Nicolas committed Jul 24, 2018
1 parent 1f46d5b commit 0104847
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions common/h/IBSTree.h
Expand Up @@ -38,13 +38,11 @@
/*******************************************************/

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "dyntypes.h"

#include <set>
#include <limits>
#include <ostream>
#include <iostream>

/** Template class for Interval Binary Search Tree. The implementation is
* based on a red-black tree (derived from our codeRange implementation)
Expand Down Expand Up @@ -159,6 +157,16 @@ class IBSNode {
IBSNode<ITYPE> *parent;
};


template<class ITYPE = SimpleInterval<> >
std::ostream &operator<<(std::ostream &os, std::set<ITYPE *> &s) {
for (auto i = s.begin(); i != s.end(); i++) {
std::cerr << "[0x" << std::hex << (*i)->low()
<< ", 0x" << (*i)->high() << std::dec << ") ";
}
return os;
};

template<class ITYPE = SimpleInterval<> >
class IBSTree {
public:
Expand Down Expand Up @@ -219,7 +227,14 @@ class IBSTree {
void findIntervals(interval_type X, IBSNode<ITYPE> *R, std::set<ITYPE *> &S) const;
void findIntervals(ITYPE *I, IBSNode<ITYPE> *R, std::set<ITYPE *> &S) const;

void PrintPreorder(IBSNode<ITYPE> *n);
void PrintPreorder(IBSNode<ITYPE> *n, int indent);

std::ostream& doIndent(int n)
{
std::cerr.width(n);
std::cerr << "";
return std::cerr;
};

int height(IBSNode<ITYPE> *n);
int CountMarks(IBSNode<ITYPE> *R) const;
Expand Down Expand Up @@ -281,7 +296,7 @@ class IBSTree {
/** Delete all entries in the tree **/
void clear();

void PrintPreorder() { PrintPreorder(root); }
void PrintPreorder() { PrintPreorder(root, 0); }
};

template<class ITYPE>
Expand Down Expand Up @@ -888,22 +903,28 @@ int IBSTree<ITYPE>::height(IBSNode<ITYPE> *n)
}

template<class ITYPE>
void IBSTree<ITYPE>::PrintPreorder(IBSNode<ITYPE> *n)
void IBSTree<ITYPE>::PrintPreorder(IBSNode<ITYPE> *n, int indent)
{
if(n == nil) return;

PrintPreorder(n->left);
printf(" %d\n",n->value());
PrintPreorder(n->right);
// print self
doIndent(indent) << "node: 0x" << std::hex << n->value() << std::dec << " (" << n->value() << ")" << std::endl;
if (!n->less.empty())
doIndent(indent) << " <: " << n->less << std::endl;
if (!n->equal.empty())
doIndent(indent) << " =: " << n->equal << std::endl;
if (!n->greater.empty())
doIndent(indent) << " >: " << n->greater << std::endl;

// print children
PrintPreorder(n->left, indent + 1);
PrintPreorder(n->right, indent + 1);

if(n == root) {
int h = height(root);
printf(" tree height: %d\n", h);
std::cerr << "tree height: " << height(root) << std::endl;
}
}



template<class ITYPE>
int IBSTree<ITYPE>::CountMarks() const
{
Expand Down

0 comments on commit 0104847

Please sign in to comment.