File size: 2,162 Bytes
d5bfab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use loda_rust_core::execute::{NodeLoopLimit, ProgramCache, ProgramRunner, RegisterValue, RunMode};
use loda_rust_core::execute::NodeRegisterLimit;
use loda_rust_core::util::BigIntVec;

pub struct TermComputer {
    pub terms: BigIntVec,
    pub steps: Vec<u64>,
    pub step_count: u64,
}

impl TermComputer {
    pub fn new() -> Self {
        Self {
            terms: Vec::with_capacity(40),
            steps: Vec::with_capacity(40),
            step_count: 0,
        }
    }

    pub fn compute(&mut self, cache: &mut ProgramCache, runner: &ProgramRunner, count: usize) -> anyhow::Result<()> {
        let node_register_limit = NodeRegisterLimit::LimitBits(32);
        loop {
            let length: usize = self.terms.len();
            if length >= count {
                break;
            }
            let node_loop_limit: NodeLoopLimit;
            if length <= 10 {
                node_loop_limit = NodeLoopLimit::LimitCount(4000);
            } else {
                if length <= 20 {
                    node_loop_limit = NodeLoopLimit::LimitCount(8000);
                } else {
                    node_loop_limit = NodeLoopLimit::LimitCount(32000);
                }
            }
            let step_count_limit: u64;
            if length <= 10 {
                step_count_limit = 40000;
            } else {
                if length <= 20 {
                    step_count_limit = 80000;
                } else {
                    step_count_limit = 320000;
                }
            }
            let index = length as i64;
            let input = RegisterValue::from_i64(index);
            let output: RegisterValue = runner.run(
                input, 
                RunMode::Silent, 
                &mut self.step_count, 
                step_count_limit, 
                node_register_limit.clone(),
                node_loop_limit.clone(),
                cache
            )?;
            self.terms.push(output.0);
            self.steps.push(self.step_count);
        }
        Ok(())
    }

    pub fn reset(&mut self) {
        self.terms.clear();
        self.steps.clear();
        self.step_count = 0;
    }
}