feat: finish evaluate arg allgorithm

This commit is contained in:
max_richter 2024-04-15 13:31:32 +02:00
parent d262d4b9fd
commit 9ccd76c7d9
5 changed files with 72 additions and 11 deletions

View File

@ -1,15 +1,16 @@
<script lang="ts"> <script lang="ts">
import { decode, encode } from "$lib/helpers/flat_tree"; import { decode, encode } from "$lib/helpers/flat_tree";
const input = [5, [6, 1], [7, 2, [5, 1]]]; // const input = [5, [6, 1], [7, 2, [5, 1]]];
// const input = [5, [], [6, []], []]; // const input = [5, [], [6, []], []];
// const input = [52]; // const input = [52];
const input = [0, 0, [0, 2, 0, 128, 0, 128], 0, 128];
console.log("INPUT"); console.log("INPUT");
console.log(input); console.log(input);
let encoded = encode(input); let encoded = encode(input);
encoded = [0, 3, 5, ...encoded.slice(2).slice(0, -4), 5, 5, 1, 1]; // encoded = [];
console.log("ENCODED"); console.log("ENCODED");
console.log(encoded); console.log(encoded);

View File

@ -1,7 +0,0 @@
import { describe, it, expect } from 'vitest';
describe('sum test', () => {
it('adds 1 + 2 to equal 3', () => {
expect(1 + 2).toBe(3);
});
});

View File

@ -1,4 +1,6 @@
mod encoding;
mod helpers; mod helpers;
mod nodes;
mod tree; mod tree;
pub use helpers::*; pub use helpers::*;
pub use tree::*; pub use tree::*;

View File

@ -0,0 +1,18 @@
use crate::encoding;
pub fn math_node(args: &[i32]) -> (i32, i32) {
let math_type = args[0];
let a = encoding::decode_float(args[1], args[2]);
let b = encoding::decode_float(args[3], args[4]);
let result = match math_type {
0 => a + b,
1 => a - b,
2 => a * b,
3 => a / b,
_ => 0.0,
};
encoding::encode_float(result)
}

View File

@ -1,4 +1,4 @@
pub fn get_args<'a>(args: &'a Vec<i32>) -> Vec<&[i32]> { pub fn get_args(args: &[i32]) -> Vec<&[i32]> {
let mut idx: usize = 0; let mut idx: usize = 0;
let mut depth = -1; let mut depth = -1;
@ -57,13 +57,60 @@ pub fn get_args<'a>(args: &'a Vec<i32>) -> Vec<&[i32]> {
arg_start_index = idx; arg_start_index = idx;
} }
return out_args; out_args
}
pub fn evaluate_node(input_args: &[i32]) -> (i32, i32) {
let node_type = input_args[0];
match node_type {
0 => crate::nodes::math_node(&input_args[1..]),
_ => (0, 0),
}
}
pub fn evaluate_args(input_args: &[i32]) -> (i32, i32) {
let args = get_args(input_args);
let mut resolved: Vec<i32> = Vec::new();
for arg in args {
if arg.len() == 1 {
resolved.push(arg[0]);
} else {
let res = evaluate_args(arg);
resolved.push(res.0);
resolved.push(res.1);
}
}
if resolved.len() > 1 {
evaluate_node(&resolved)
} else {
(resolved[0], resolved[1])
}
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::encoding::decode_float;
use super::*; use super::*;
#[test]
fn test_resursive_evaluation() {
let input = vec![0, 3, 0, 0, 0, 7, 0, 2, 0, 128, 0, 128, 1, 6, 0, 128];
// this is an encoded version of a math node that multiplies 2 * 2
// and another math node that adds 2 to that result
// the numbers are f32 floats encoded as two i32's
let result = evaluate_args(&input);
let decoded = decode_float(result.0, result.1);
assert_eq!(decoded, 6.0);
}
#[test] #[test]
fn test_example_input_a() { fn test_example_input_a() {
let input_a = vec![0, 4, 1, 2, 3, 0, 7, 1, 2, 4, 2, 4, 1, 1, 1, 1]; let input_a = vec![0, 4, 1, 2, 3, 0, 7, 1, 2, 4, 2, 4, 1, 1, 1, 1];