1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (c) Cole Frederick. All rights reserved.
// The use and distribution terms for this software are covered by the
// Eclipse Public License 1.0 (https://opensource.org/licenses/eclipse-1.0.php)
// which can be found in the file epl-v10.html at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by the terms of this license.
// You must not remove this notice, or any other, from this software.

use super::*;
use vector::tear_down::{NodeRecord, NodeRecordStack, BLANK};
use transduce::{inges, last_call, Process};

pub fn reduce(prism: AnchoredLine, process_stack: &mut [Box<dyn Process>]) -> Value {
    let guide = Guide::hydrate(prism);
    let count = guide.count;
    if count <= TAIL_CAP {
        for i in (0..count).rev() {
            let x = guide.root.offset(i as i32).line().star() as *const Value;
            let y = unsafe { &* x };
            if let Some(ret) = inges(process_stack, y) {
                return ret;
            }
        }
        last_call(process_stack)
    } else {
        let tailoff = tailoff(guide.count);
        if tailoff == TAIL_CAP {
            let tail = guide.root[-1].segment();
            for i in (0..(count - tailoff)).rev() {
                let x = tail.line_at(i).line().star() as *const Value;
                let y = unsafe { &* x };
                if let Some(ret) = inges(process_stack, y) {
                    return ret;
                }
            }
            for i in (0..TAIL_CAP).rev() {
                let x = guide.root.offset(i as i32).line().star() as *const Value;
                let y = unsafe { &* x };
                if let Some(ret) = inges(process_stack, y) {
                    return ret;
                }
            }
            last_call(process_stack)
        } else {
            reduce_tree(guide, tailoff, process_stack)
        }
    }
}

pub fn reduce_tree(guide: Guide, tailoff: u32, process_stack: &mut [Box<dyn Process>]) -> Value {
    let tail = guide.root[-1].segment();
    for i in (0..(guide.count - tailoff)).rev() {
        let x = tail.line_at(i).line().star() as *const Value;
        let y = unsafe { &* x };
        if let Some(ret) = inges(process_stack, y) {
            return ret;
        }
    }
    let last_tree_index = tailoff - 1;
    let stack_space = [BLANK; 8];
    let mut stack = NodeRecordStack::new(stack_space.as_ptr());
    stack.push(NodeRecord {
        first_child: guide.root,
        child_count: root_content_count(tailoff),
        height: digit_count(last_tree_index),
        on_boundary: true,
        current_child: None,
    });
    while !stack.is_empty() {
        if let Some(ret) = step(&mut stack, process_stack, last_tree_index) {
            return ret;
        }
    }
    last_call(process_stack)
}

pub fn base_case(node: &NodeRecord, process_stack: &mut [Box<dyn Process>]) -> Option<Value> {
    for i in (0..node.child_count).rev() {
        let a_tail = node.first_child[i as i32].segment();
        for j in (0..TAIL_CAP).rev() {
            let x = a_tail.line_at(j).line().star() as *const Value;
            let y = unsafe { &* x };
            if let Some(ret) = inges(process_stack, y) {
                return Some(ret);
            }
        }
    }
    None
}

pub fn step(stack: &mut NodeRecordStack, process_stack: &mut [Box<dyn Process>],
            last_tree_index: u32) -> Option<Value> {
    let top = stack.top();
    if top.height == 2 {
        if let Some(ret) = base_case(top, process_stack) {
            return Some(ret);
        }
        stack.pop();
        return None;
    }
    let next = match top.current_child {
        Some(i) => { i + 1 },
        None    => { 0 },
    };
    let cap = top.child_count;
    if next < cap {
        let idx = cap - next - 1;
        let s = top.first_child[idx as i32].segment();
        let r = child_record(top, last_tree_index, s, idx == cap - 1);
        top.current_child = Some(next);
        stack.push(r);
    } else {
        stack.pop();
    }
    None
}

pub fn child_record(top: &mut NodeRecord, last_tree_index: u32, s: Segment, last_child: bool)
                    -> NodeRecord {
    let mut r = NodeRecord {
        first_child: s.line_at(0),
        child_count: TAIL_CAP,
        height: top.height - 1,
        on_boundary: false,
        current_child: None,
    };
    if top.on_boundary && last_child {
        let child_idx = (last_tree_index >> (BITS * (r.height - 1))) & MASK;
        r.child_count = child_idx + 1;
        r.on_boundary = true;
    }
    r
}