diff --git a/src/instructions/mod.rs b/src/instructions/mod.rs index 1a42239..dcd08f2 100644 --- a/src/instructions/mod.rs +++ b/src/instructions/mod.rs @@ -230,8 +230,13 @@ pub mod macros { aux1_inputs.push(state.$aux1_stack[aux1_stack_len - n].clone()); } for n in 1..=$aux0_arity { - aux0_inputs.push(state.$aux0_stack[aux0_stack_len - $aux1_arity - n].clone()); + 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()); + } 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 { inputs.push(state.$in_stack[in_stack_len - n].clone()); } diff --git a/src/instructions/vector.rs b/src/instructions/vector.rs index 860e54f..6dd80c8 100644 --- a/src/instructions/vector.rs +++ b/src/instructions/vector.rs @@ -886,6 +886,70 @@ make_instruction_aux2!( 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(mut vals: Vec>, auxs: Vec) -> Option> +where + T: Clone, + for<'a> &'a T: Eq, + Vec: FromIterator, +{ + let temp_vec = &mut vals[0]; + let ret_vec: Vec = 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, 1, int, 2, i128); +make_instruction_aux!( + vector_float, + vector_float, + _replace, + Vec, + 1, + float, + 2, + Decimal +); +make_instruction_aux!( + vector_string, + vector_string, + _replace, + Vec>, + 1, + string, + 2, + Vec +); +make_instruction_aux!( + vector_boolean, + vector_boolean, + _replace, + Vec, + 1, + boolean, + 2, + bool +); +make_instruction_aux!( + vector_char, + vector_char, + _replace, + Vec, + 1, + char, + 2, + char +); +make_instruction_aux!(string, string, _replace, Vec, 1, char, 2, char); + #[cfg(test)] mod tests { use super::*; @@ -1331,6 +1395,26 @@ mod tests { vector_int_set_nth(&mut test_state); 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); } } diff --git a/src/main.rs b/src/main.rs index ab3a002..70f8924 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use crate::instructions::vector::_replace; use crate::push::state::{EMPTY_STATE, PushState}; use instructions::utils::NumericTrait; use rust_decimal::MathematicalOps; @@ -47,8 +48,10 @@ fn main() { //let test_state = EMPTY_STATE; //println!("{}", test_state.int == test_state.boolean); - println!( + /*println!( "{}", std::any::type_name::() == std::any::type_name::() - ); + );*/ + + let temp: Option> = _replace(vec![vec![1, 2, 3]], vec![1, 2]); }