feat: improve get_args functions

This commit is contained in:
2024-04-23 03:41:28 +02:00
parent c87d4b8dda
commit 98cf2e8369
20 changed files with 270 additions and 147 deletions

View File

@ -32,7 +32,7 @@ export const NodeInputIntegerSchema = z.object({
export const NodeInputBooleanSchema = z.object({
...DefaultOptionsSchema.shape,
type: z.literal("boolean"),
vale: z.boolean().optional(),
value: z.boolean().optional(),
});
export const NodeInputSelectSchema = z.object({

View File

@ -9,11 +9,23 @@ test("it correctly concats nested arrays", () => {
const output = concatEncodedArrays([input_a, input_b, input_c]);
console.log("Output", output);
const decoded = decodeNestedArray(output);
expect(decoded[0]).toEqual([1, 2, 3]);
expect(decoded[1]).toEqual(2);
expect(decoded[2]).toEqual([4, 5, 6]);
});
test("it correctly concats nested arrays with nested arrays", () => {
const input_c = encodeNestedArray([1, 2, 3]);
const output = concatEncodedArrays([42, 12, input_c]);
const decoded = decodeNestedArray(output);
expect(decoded[0]).toEqual(42);
expect(decoded[1]).toEqual(12);
expect(decoded[2]).toEqual([1, 2, 3]);
});
// Original test case

View File

@ -14,11 +14,6 @@ export function concatEncodedArrays(input: (number | number[])[]): number[] {
const item = input[i];
if (Array.isArray(item)) {
result.push(...item);
if (item.length > 2) {
if (item[item.length - 2] !== 1 && item[item.length - 1] !== 1) {
result.push(1, 1); // add closing bracket if missing
}
}
last_closing_bracket = result.length - 1;
} else {
result[last_closing_bracket]++;
@ -46,8 +41,10 @@ export function encodeNestedArray(array: SparseArray): number[] {
encoded.push(0, 1, 1, 1);
} else {
// Recursively encode non-empty arrays
console.log("recursing", item);
const child = encodeNestedArray(item);
encoded.push(...child, 1, 0);
console.log("recursed", child);
encoded.push(...child);
}
// Update missingBracketIndex to the position of the newly added bracket
missingBracketIndex = encoded.length - 1;
@ -57,8 +54,10 @@ export function encodeNestedArray(array: SparseArray): number[] {
// Update the distance for the last opened bracket
if (missingBracketIndex) encoded[missingBracketIndex] = index + 2;
}
console.log(encoded, item);
}
return encoded;
return [...encoded, 1, 1];
};
function decode_recursive(dense: number[] | Int32Array, index = 0) {

View File

@ -1,5 +1,3 @@
use crate::log;
use super::{create_geometry_data, wrap_geometry_data};
use glam::{Mat4, Quat, Vec3};

View File

@ -15,6 +15,12 @@ pub fn create_geometry_data(vertex_amount: usize, face_amount: usize) -> Vec<i32
let mut geo = vec![0; amount];
log!(
"create_geometry_data: vertices: {} faces: {}",
vertex_amount,
face_amount
);
geo[0] = 0; // opening bracket
geo[1] = amount as i32 - 2; // opening bracket
geo[2] = 1; // type: geometry
@ -44,13 +50,6 @@ pub fn wrap_geometry_data(geometry: &mut [i32]) -> GeometryData {
let (positions_slice, rest) = rest.split_at_mut(vertices_amount * 3);
let (normals_slice, _) = rest.split_at_mut(vertices_amount * 3);
log!(
"Vertices: {}, normals: {}, Total floats: {}",
positions_slice.len(),
normals_slice.len(),
total_floats
);
assert!(
positions_slice.len() + normals_slice.len() == total_floats,
"Slices do not match the expected sizes."

View File

@ -0,0 +1,18 @@
use utils::get_args;
#[rustfmt::skip]
fn main() {
let inputs = vec![
vec![0, 1, 0, 4, 1056964608, 1065353216, 1056964608, 1, 4, 1080872141, 1054951342, 32, 1, 1 ],
vec![0, 4, 1056964608, 1065353216, 1056964608, 1, 4],
vec![0, 1, 0, 3, 0, 0, 0, 5, 0, 0, 1073741824, 1073741824, 1, 1, 1, 1, 1, 4, 1065353216, 1054615798, 5, 1, 1 ],
vec![ 0, 1, 0, 3, 0, 0, 0, 1, 4, 1073741824, 1073741824, 32, 1, 1 ],
vec![0, 1, 0, 1, 0, 14, 0, 1056964608, 1056964608, 1056964608, 1058810102, 1056964608, 1069547520, 1056964608, 1050421494, 1056964608, 1075838976, 1056964608, 0, 1, 1, 1, 2, 13, 1, 1],
vec![ 0, 1, 0, 2, 0, 0, 5, 0, 0, 1073741824, 1073741824, 1, 2, 0, 1, 4, 1088212173, 1083388723, 20, 1, 1 ]
];
for input in inputs {
println!("RESULT: {:?}", get_args(&input));
}
}

View File

@ -1,90 +1,103 @@
use crate::decode_float;
pub fn get_args(args: &[i32]) -> Vec<&[i32]> {
let mut idx: usize = 0;
let mut depth = -1;
let mut next_bracket_index = 0;
println!("-------------------");
println!("{:?}", args);
let mut out_args: Vec<&[i32]> = Vec::new();
let mut depth = 0;
let mut i = 0;
let mut start_index = 0;
let mut nbi = 0;
let len = args.len();
let length = args.len();
let mut arg_start_index = 2;
while idx < length {
let is_bracket = idx == next_bracket_index;
let value = args[idx];
// if we are at a bracket
if is_bracket {
// if we are at the end of the args
if idx >= length - 1 {
break;
}
next_bracket_index = 1 + idx + args[idx + 1] as usize;
if value == 0 {
depth += 1;
} else if value == 1 {
depth -= 1;
}
if depth == 0 {
if value == 1 {
out_args.push(&args[arg_start_index..idx]);
arg_start_index = idx + 2;
}
// skip over the bracket encoding
idx += 2;
} else {
// skip to the next bracket if we are at depth > 0
idx = next_bracket_index;
}
continue;
}
out_args.push(&args[arg_start_index..=idx]);
arg_start_index = idx + 1;
// this is at the end of args where normally multiple ] are encoded
if depth < 0 {
while i < len {
print!("{:2} ", i);
let val = args[i];
let is_bracket = i == nbi;
let is_opening_bracket = val == 0;
if i >= len - 1 {
break;
}
if is_bracket {
nbi = i + args[i + 1] as usize + 1;
if !is_opening_bracket {
depth -= 1;
}
}
idx += 1;
for _ in 0..depth {
print!("-");
}
if is_bracket {
if is_opening_bracket {
if depth == 1 {
start_index = i;
}
println!("[ {}", start_index);
depth += 1;
} else {
println!("] {}", start_index);
if depth == 1 {
out_args.push(&args[start_index..i + 2]);
println!("-> {:?}", &args[start_index..i + 2]);
start_index = i + 2;
}
}
i += 2;
continue;
} else {
if depth == 1 {
out_args.push(&args[i..i + 1]);
println!("-> {:?}", &args[i..i + 1]);
start_index = i + 1;
} else {
println!("{}", val);
}
}
i += 1;
}
out_args
}
pub fn concat_args(mut data: Vec<Vec<i32>>) -> Vec<i32> {
pub fn concat_args(mut data: Vec<&[i32]>) -> Vec<i32> {
let mut total_length = 4; // Start with 4 to account for [0, 1] at the start and [1, 1] at the end
// Calculate the total length first to avoid reallocations
for vec in &data {
total_length += vec.len(); // +4 for [0, 1] and [1, 1] per inner vec
if vec.len() == 1 {
total_length += 1;
} else {
total_length += vec.len(); // +4 for [0, 1] and [1, 1] per inner vec
}
}
let mut result = Vec::with_capacity(total_length);
// Add [0, 1] initially
// result.push(0);
// result.push(1);
result.push(0);
result.push(1);
let mut last_closing_bracket = 1;
// Process each vector
for vec in data.iter_mut() {
result.push(0);
result.push(1);
result.append(vec);
result.push(1);
result.push(1);
if vec.len() == 1 {
result.push(vec[0]);
result[last_closing_bracket] += 1;
continue;
} else {
result.extend_from_slice(vec);
last_closing_bracket = result.len() - 1;
result[last_closing_bracket] = 1;
}
}
// Add [1, 1] at the end
// result.push(1);
result.push(1);
result.push(1);
result