_replace done
This commit is contained in:
parent
a17bdbbf88
commit
3c1943152d
@ -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());
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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]);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user