I\'m trying to improve performance in our app. I\'ve got performance information in the form of a tree of calls, with the following node class:
public class
C code here:
void printVLine(wchar_t token, unsigned short height, unsigned short y, unsigned short x);
const static wchar_t TREE_VLINE = L'┃';
const static wchar_t TREE_INBRANCH[] = L"┣╾⟶ ";
const static wchar_t TREE_OUTBRANCH[] = L"┗╾⟶ ";
typedef void (*Printer)(void * whateverYouWant);
const static unsigned int INBRANCH_SIZE = sizeof(TREE_INBRANCH) / sizeof(TREE_INBRANCH[0]);
const static unsigned int OUTBRANCH_SIZE = sizeof(TREE_OUTBRANCH) / sizeof(TREE_OUTBRANCH[0]);
size_t Tree_printFancy(Tree * self, int y, int x, Printer print){
if (self == NULL) return 0L;
//
size_t descendants = y;
move(y, x);
print(Tree_at(self));
if (!Tree_isLeaf(self)){ // in order not to experience unsigned value overflow in while()
move(++y, x);
size_t i = 0;
while(i < Tree_childrenSize(self) - 1){
wprintf(TREE_INBRANCH);
size_t curChildren = Tree_printFancy(
Tree_childAt(self, i), y, x + INBRANCH_SIZE, print
);
printVLine(TREE_VLINE, curChildren , y + 1, x);
move((y += curChildren), x);
++i;
}
wprintf(TREE_OUTBRANCH);
y += Tree_printFancy( // printing outermost child
Tree_childAt(self, i), y, x + OUTBRANCH_SIZE, print
) - 1;
}
return y - descendants + 1;
}
It is applicable rather for console printing. Function move(y, x) moves cursor to (y, x) location on the screen. The best part is, you may change style of output by changing variables TREE_VLINE, TREE_INBRANCH, TREE_OUTBRANCH, length of two last strings doesn't matter. And you can print whatever you like, by passing Printer function pointer, which will print the value of the current tree node. Output looks like this