Compare commits

...

2 Commits

Author SHA1 Message Date
d1c1d28e9d doc changes 2025-04-13 18:52:21 -05:00
3c1943152d _replace done 2025-04-13 18:50:27 -05:00
3 changed files with 99 additions and 7 deletions

View File

@ -6,7 +6,7 @@ pub mod macros {
/// ///
/// The `in_stack` argument refers to which push stack should this operate on. /// The `in_stack` argument refers to which push stack should this operate on.
/// The `out_stack` argument refers to which push stack should the result be pushed to. /// The `out_stack` argument refers to which push stack should the result be pushed to.
/// The `fn_name` argement refers to the name of the function that is to operate /// The `fn_name` argument refers to the name of the function that is to operate
/// on the values popped from `in_stack`. /// on the values popped from `in_stack`.
/// The `fn_type` argument refers to the type of `in_stack`. For example, the /// The `fn_type` argument refers to the type of `in_stack`. For example, the
/// int stack is type: *Vec<i128>*. `fn_type` is *i128* in this case. /// int stack is type: *Vec<i128>*. `fn_type` is *i128* in this case.
@ -156,7 +156,7 @@ pub mod macros {
}; };
} }
/// Same as `make_instruction!` but has two extra parameters. /// Same as `make_instruction!` but can work on two stacks.
/// ///
/// `aux_stack` is an auxiliary stack to be used as input to internal function. /// `aux_stack` is an auxiliary stack to be used as input to internal function.
/// `aux_arity` is the amount of the auxiliary stack to use. /// `aux_arity` is the amount of the auxiliary stack to use.
@ -196,7 +196,7 @@ pub mod macros {
}; };
} }
/// Same as `make_instruction!` but can work on two auxiliary stacks. Is there a way /// Same as `make_instruction!` but can work on three stacks. Is there a way
/// to generalize even this? /// to generalize even this?
/// ///
/// `aux_stack` is an auxiliary stack to be used as input to internal function. /// `aux_stack` is an auxiliary stack to be used as input to internal function.
@ -230,8 +230,13 @@ pub mod macros {
aux1_inputs.push(state.$aux1_stack[aux1_stack_len - n].clone()); aux1_inputs.push(state.$aux1_stack[aux1_stack_len - n].clone());
} }
for n in 1..=$aux0_arity { for n in 1..=$aux0_arity {
if std::any::type_name::<$aux0_type>() == std::any::type_name::<$aux1_type>() {
aux0_inputs.push(state.$aux0_stack[aux0_stack_len - $aux1_arity - n].clone()); aux0_inputs.push(state.$aux0_stack[aux0_stack_len - $aux1_arity - n].clone());
} else {
aux0_inputs.push(state.$aux0_stack[aux0_stack_len - n].clone());
} }
}
// Stack shouldn't be the same for all three
for n in 1..=$fn_arity { for n in 1..=$fn_arity {
inputs.push(state.$in_stack[in_stack_len - n].clone()); inputs.push(state.$in_stack[in_stack_len - n].clone());
} }

View File

@ -886,6 +886,70 @@ make_instruction_aux2!(
i128 i128
); );
/// Replaces all values in a vector with respect to two ints. The first int is the search value
/// and the second value is the one to replace.
pub fn _replace<T>(mut vals: Vec<Vec<T>>, auxs: Vec<T>) -> Option<Vec<T>>
where
T: Clone,
for<'a> &'a T: Eq,
Vec<T>: FromIterator<T>,
{
let temp_vec = &mut vals[0];
let ret_vec: Vec<T> = temp_vec
.iter()
.map(|x| {
if x == &auxs[0] {
auxs[1].clone()
} else {
x.clone()
}
})
.collect();
Some(ret_vec)
}
make_instruction_aux!(vector_int, vector_int, _replace, Vec<i128>, 1, int, 2, i128);
make_instruction_aux!(
vector_float,
vector_float,
_replace,
Vec<Decimal>,
1,
float,
2,
Decimal
);
make_instruction_aux!(
vector_string,
vector_string,
_replace,
Vec<Vec<char>>,
1,
string,
2,
Vec<char>
);
make_instruction_aux!(
vector_boolean,
vector_boolean,
_replace,
Vec<bool>,
1,
boolean,
2,
bool
);
make_instruction_aux!(
vector_char,
vector_char,
_replace,
Vec<char>,
1,
char,
2,
char
);
make_instruction_aux!(string, string, _replace, Vec<char>, 1, char, 2, char);
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -1331,6 +1395,26 @@ mod tests {
vector_int_set_nth(&mut test_state); vector_int_set_nth(&mut test_state);
assert_eq!(vec![vec![0, 99, 1, 2, 3, 4, 5]], test_state.vector_int); assert_eq!(vec![vec![0, 99, 1, 2, 3, 4, 5]], test_state.vector_int);
// Write more tests tmo! test_state.string = vec![vec!['t', 'e', 's', 't']];
test_state.int = vec![2];
test_state.char = vec!['z'];
string_set_nth(&mut test_state);
assert_eq!(vec![vec!['t', 'e', 'z', 's', 't']], test_state.string);
test_state.vector_boolean = vec![vec![true, false, true]];
test_state.int = vec![];
test_state.boolean = vec![true];
vector_boolean_set_nth(&mut test_state);
assert_eq!(vec![vec![true, false, true]], test_state.vector_boolean);
}
#[test]
fn replace_test() {
let mut test_state = EMPTY_STATE;
test_state.vector_int = vec![vec![0, 1, 2, 3, 4, 5, 2]];
test_state.int = vec![3, 2];
vector_int_replace(&mut test_state);
assert_eq!(vec![vec![0, 1, 3, 3, 4, 5, 3]], test_state.vector_int);
} }
} }

View File

@ -1,3 +1,4 @@
use crate::instructions::vector::_replace;
use crate::push::state::{EMPTY_STATE, PushState}; use crate::push::state::{EMPTY_STATE, PushState};
use instructions::utils::NumericTrait; use instructions::utils::NumericTrait;
use rust_decimal::MathematicalOps; use rust_decimal::MathematicalOps;
@ -47,8 +48,10 @@ fn main() {
//let test_state = EMPTY_STATE; //let test_state = EMPTY_STATE;
//println!("{}", test_state.int == test_state.boolean); //println!("{}", test_state.int == test_state.boolean);
println!( /*println!(
"{}", "{}",
std::any::type_name::<PushState>() == std::any::type_name::<PushState>() std::any::type_name::<PushState>() == std::any::type_name::<PushState>()
); );*/
let temp: Option<Vec<i128>> = _replace(vec![vec![1, 2, 3]], vec![1, 2]);
} }