Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void traverse(const Vec8f& query, const BinaryTree& tree, const Result* result) {
- const Node* stack[BIG_ENOUGH];
- stack[0] = tree.root;
- int stack_ptr = 0;
- do {
- //Try to convince the compiler to load 64-byte node.
- Node node = stack[stack_ptr];
- if (node.type.is_interior) {
- //We can compute our child pointers (simple unpacking operations).
- const Node* child0 = node.calculate_child0();
- const Node* child1 = node.calculate_child1();
- //Calculate whether we need to traverse our children. Functions are math-heavy. Note:
- // they do not depend on dereferencing `child0` or `child1`.
- Temp temp;
- calculate_should_traverse0(node,query,result,&temp);
- calculate_should_traverse1(node,query,result,&temp);
- //"Recurse". The tests involve very simple logic.
- if (should_traverse_0_only(result,temp)) {
- //Replace the current node on the stack with child 0 and loop (tail recursion)
- stack[stack_ptr] = child0;
- continue;
- } else if (should_traverse_1_only(result,temp)) {
- //Replace the current node on the stack with child 1 and loop (tail recursion)
- stack[stack_ptr] = child1;
- continue;
- } else if (should_traverse_0_and_1(result,temp)) {
- if (should_traverse_0_first(result,temp)) {
- //Push child 1 onto the stack and recurse with child 0
- stack[stack_ptr++] = child1;
- stack[stack_ptr ] = child0;
- } else {
- //Push child 0 onto the stack and recurse with child 1
- stack[stack_ptr++] = child0;
- stack[stack_ptr ] = child1;
- }
- continue;
- } else {
- //We don't need to traverse either node. Fall out, decrementing `stack_ptr`.
- }
- } else { //is leaf
- calculate_math_and_memory_intensive_functions(node,result);
- }
- --stack_ptr;
- } while(stack_ptr>0)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement