Compare commits
No commits in common. "6c75c3704d7dd4c7f2dfb4256e92d857793d662e" and "ff078925a1d1499369bdcfe2c6ce567df7e3930e" have entirely different histories.
6c75c3704d
...
ff078925a1
@ -129,7 +129,7 @@ pub mod macros {
|
||||
for _ in 0..$fn_arity {
|
||||
state.$in_stack.pop();
|
||||
}
|
||||
state.$out_stack.extend(result.into_iter());
|
||||
state.$out_stack.extend(result.iter());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -204,42 +204,6 @@ pub mod macros {
|
||||
};
|
||||
}
|
||||
|
||||
/// Same as make_instruction_mult but can handle one auxiliary variable.
|
||||
#[macro_export]
|
||||
macro_rules! make_instruction_mult_aux {
|
||||
($in_stack:ident, $out_stack:ident, $fn_name:ident, $fn_type:ty, $fn_arity:stmt, $aux_stack:ident, $aux_arity:stmt, $aux_type:ty) => {
|
||||
paste::item! {
|
||||
/// Runs the $fn_name function on the top $fn_arity items from
|
||||
/// the $in_stack and places the calculated value on the $out_stack.
|
||||
/// $aux_stack is also used and popped $aux_arity time(s).
|
||||
pub fn [< $in_stack $fn_name >] (state: &mut PushState) {
|
||||
let in_stack_len = state.$in_stack.len();
|
||||
let aux_stack_len = state.$aux_stack.len();
|
||||
if in_stack_len < $fn_arity || aux_stack_len < $aux_arity {
|
||||
return;
|
||||
}
|
||||
let mut inputs: Vec<$fn_type> = Vec::with_capacity($fn_arity);
|
||||
let mut aux_inputs: Vec<$aux_type> = Vec::with_capacity($aux_arity);
|
||||
for n in 1..=$aux_arity {
|
||||
aux_inputs.push(state.$aux_stack[aux_stack_len - n].clone());
|
||||
}
|
||||
for n in 1..=$fn_arity {
|
||||
inputs.push(state.$in_stack[in_stack_len - n].clone());
|
||||
}
|
||||
if let Some(result) = $fn_name(inputs, aux_inputs) {
|
||||
for _ in 0..$aux_arity {
|
||||
state.$aux_stack.pop();
|
||||
}
|
||||
for _ in 0..$fn_arity {
|
||||
state.$in_stack.pop();
|
||||
}
|
||||
state.$out_stack.extend(result.into_iter());
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Same as `make_instruction!` but can work on three stacks. Is there a way
|
||||
/// to generalize even this?
|
||||
///
|
||||
@ -413,13 +377,8 @@ pub fn string_instructions() -> Vec<fn(&mut PushState)> {
|
||||
string_index_of_vector,
|
||||
string_occurrences_of,
|
||||
string_occurrences_of_vector,
|
||||
string_parse_to_prim,
|
||||
string_set_nth,
|
||||
string_split_on,
|
||||
string_replace,
|
||||
string_remove,
|
||||
string_insert,
|
||||
string_insert_vector,
|
||||
// common.rs
|
||||
string_pop,
|
||||
]
|
||||
@ -477,16 +436,8 @@ pub fn vector_int_instructions() -> Vec<fn(&mut PushState)> {
|
||||
vector_int_index_of_vector,
|
||||
vector_int_occurrences_of,
|
||||
vector_int_occurrences_of_vector,
|
||||
vector_int_parse_to_prim,
|
||||
vector_int_set_nth,
|
||||
vector_int_split_on,
|
||||
vector_int_replace,
|
||||
vector_int_remove,
|
||||
vector_int_iterate,
|
||||
vector_int_sort,
|
||||
vector_int_sort_reverse,
|
||||
vector_int_insert,
|
||||
vector_int_insert_vector,
|
||||
// common.rs
|
||||
vector_int_pop,
|
||||
]
|
||||
@ -523,16 +474,8 @@ pub fn vector_float_instructions() -> Vec<fn(&mut PushState)> {
|
||||
vector_float_index_of_vector,
|
||||
vector_float_occurrences_of,
|
||||
vector_float_occurrences_of_vector,
|
||||
vector_float_parse_to_prim,
|
||||
vector_float_set_nth,
|
||||
vector_float_split_on,
|
||||
vector_float_replace,
|
||||
vector_float_remove,
|
||||
vector_float_iterate,
|
||||
vector_float_sort,
|
||||
vector_float_sort_reverse,
|
||||
vector_float_insert,
|
||||
vector_float_insert_vector,
|
||||
// common.rs
|
||||
vector_float_pop,
|
||||
]
|
||||
@ -568,13 +511,8 @@ pub fn vector_string_instructions() -> Vec<fn(&mut PushState)> {
|
||||
vector_string_index_of_vector,
|
||||
vector_string_occurrences_of,
|
||||
vector_string_occurrences_of_vector,
|
||||
vector_string_parse_to_prim,
|
||||
vector_string_set_nth,
|
||||
vector_string_split_on,
|
||||
vector_string_replace,
|
||||
vector_string_remove,
|
||||
vector_string_insert,
|
||||
vector_string_insert_vector,
|
||||
// common.rs
|
||||
vector_string_pop,
|
||||
]
|
||||
@ -611,14 +549,8 @@ pub fn vector_boolean_instructions() -> Vec<fn(&mut PushState)> {
|
||||
vector_boolean_index_of_vector,
|
||||
vector_boolean_occurrences_of,
|
||||
vector_boolean_occurrences_of_vector,
|
||||
vector_boolean_parse_to_prim,
|
||||
vector_boolean_set_nth,
|
||||
vector_boolean_split_on,
|
||||
vector_boolean_replace,
|
||||
vector_boolean_remove,
|
||||
vector_boolean_iterate,
|
||||
vector_boolean_insert,
|
||||
vector_boolean_insert_vector,
|
||||
// common.rs
|
||||
vector_boolean_pop,
|
||||
]
|
||||
@ -655,14 +587,8 @@ pub fn vector_char_instructions() -> Vec<fn(&mut PushState)> {
|
||||
vector_char_index_of_vector,
|
||||
vector_char_occurrences_of,
|
||||
vector_char_occurrences_of_vector,
|
||||
vector_char_parse_to_prim,
|
||||
vector_char_set_nth,
|
||||
vector_char_split_on,
|
||||
vector_char_replace,
|
||||
vector_char_remove,
|
||||
vector_char_iterate,
|
||||
vector_char_insert,
|
||||
vector_char_insert_vector,
|
||||
// common.rs
|
||||
vector_char_pop,
|
||||
]
|
||||
|
@ -1,6 +1,6 @@
|
||||
use rust_decimal::Decimal;
|
||||
use rust_decimal::prelude::*;
|
||||
use std::ops::{Add, Div};
|
||||
use std::ops::Div;
|
||||
|
||||
/// This trait houses various methods for making instructions
|
||||
/// more generic instead of declaring a separate function for each
|
||||
@ -8,7 +8,7 @@ use std::ops::{Add, Div};
|
||||
///
|
||||
/// Trig functions named safe rather than checked to not overlap
|
||||
/// with Decimal library's checked function names.
|
||||
pub trait NumericTrait: Sized + Add<Output = Self> + Div<Output = Self> + Ord {
|
||||
pub trait NumericTrait: Sized + Div<Output = Self> {
|
||||
fn checked_div(self, v: Self) -> Option<Self>;
|
||||
fn checked_mod(self, v: Self) -> Option<Self>;
|
||||
fn increment(self) -> Self;
|
||||
@ -23,8 +23,6 @@ pub trait NumericTrait: Sized + Add<Output = Self> + Div<Output = Self> + Ord {
|
||||
fn safe_sqrt(self) -> Option<Self>;
|
||||
fn sign_reverse(self) -> Self;
|
||||
fn square(self) -> Self;
|
||||
fn zero() -> Self;
|
||||
fn from_usize(num: usize) -> Self;
|
||||
}
|
||||
|
||||
impl NumericTrait for Decimal {
|
||||
@ -70,12 +68,6 @@ impl NumericTrait for Decimal {
|
||||
fn square(self) -> Self {
|
||||
self * self
|
||||
}
|
||||
fn zero() -> Self {
|
||||
dec!(0.0)
|
||||
}
|
||||
fn from_usize(num: usize) -> Self {
|
||||
Decimal::new(num as i64, 0)
|
||||
}
|
||||
}
|
||||
|
||||
impl NumericTrait for i128 {
|
||||
@ -126,12 +118,6 @@ impl NumericTrait for i128 {
|
||||
fn square(self) -> Self {
|
||||
self * self
|
||||
}
|
||||
fn zero() -> Self {
|
||||
0
|
||||
}
|
||||
fn from_usize(num: usize) -> Self {
|
||||
num as Self
|
||||
}
|
||||
}
|
||||
|
||||
/// A trait for types to implement logical functions that work
|
||||
@ -197,9 +183,9 @@ impl CastingTrait for bool {
|
||||
Some(v)
|
||||
}
|
||||
fn from_int(v: i128) -> Option<Self> {
|
||||
Some(v != 0)
|
||||
Some(if v != 0 { true } else { false })
|
||||
}
|
||||
fn from_float(v: Decimal) -> Option<Self> {
|
||||
Some(v != dec!(0.0))
|
||||
Some(if v != dec!(0.0) { true } else { false })
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
use crate::instructions::utils::NumericTrait;
|
||||
use crate::push::state::{Gene, PushState};
|
||||
use crate::push::state::PushState;
|
||||
use rust_decimal::Decimal;
|
||||
use std::collections::HashSet;
|
||||
use std::hash::Hash;
|
||||
@ -981,10 +980,9 @@ make_instruction_aux!(
|
||||
);
|
||||
make_instruction_aux!(string, int, _occurrences_of, Vec<char>, 1, char, 1, char);
|
||||
|
||||
/// Counts the amount of continuous occurrences one vector appears in another.
|
||||
pub fn _occurrences_of_vector<T>(vals: Vec<Vec<T>>) -> Option<i128>
|
||||
where
|
||||
T: Eq,
|
||||
T: Eq + Hash,
|
||||
{
|
||||
if vals[0].is_empty() {
|
||||
return Some(0);
|
||||
@ -1009,27 +1007,13 @@ make_instruction_clone!(vector_boolean, int, _occurrences_of_vector, Vec<bool>,
|
||||
make_instruction_clone!(vector_char, int, _occurrences_of_vector, Vec<char>, 2);
|
||||
make_instruction_clone!(string, int, _occurrences_of_vector, Vec<char>, 2);
|
||||
|
||||
/// Pushes the values inside a vector separated into individual vectors back to
|
||||
/// the stack.
|
||||
pub fn _parse_to_prim<T>(vals: Vec<Vec<T>>) -> Option<Vec<Vec<T>>>
|
||||
/// Pushes the values inside a vector to a primitive stack.
|
||||
pub fn _parse_to_prim<T>(vals: Vec<Vec<T>>) -> Option<Vec<T>>
|
||||
where
|
||||
T: Clone,
|
||||
Vec<T>: FromIterator<T>,
|
||||
{
|
||||
Some(vals[0].clone().into_iter().map(|x| vec![x]).collect())
|
||||
Some(vals[0].clone())
|
||||
}
|
||||
make_instruction_mult!(vector_int, vector_int, _parse_to_prim, Vec<i128>, 1);
|
||||
make_instruction_mult!(vector_float, vector_float, _parse_to_prim, Vec<Decimal>, 1);
|
||||
make_instruction_mult!(
|
||||
vector_string,
|
||||
vector_string,
|
||||
_parse_to_prim,
|
||||
Vec<Vec<char>>,
|
||||
1
|
||||
);
|
||||
make_instruction_mult!(vector_boolean, vector_boolean, _parse_to_prim, Vec<bool>, 1);
|
||||
make_instruction_mult!(vector_char, vector_char, _parse_to_prim, Vec<char>, 1);
|
||||
make_instruction_mult!(string, string, _parse_to_prim, Vec<char>, 1);
|
||||
|
||||
/// Sets the nth index in a vector. N from the int stack.
|
||||
pub fn _set_nth<T>(vals: Vec<Vec<T>>, aux0: Vec<T>, aux1: Vec<i128>) -> Option<Vec<T>>
|
||||
@ -1120,129 +1104,8 @@ make_instruction_aux2!(
|
||||
i128
|
||||
);
|
||||
|
||||
/// Splits a vector based the first occurence of a primitive
|
||||
pub fn _split_on<T>(vals: Vec<Vec<T>>, auxs: Vec<T>) -> Option<Vec<Vec<T>>>
|
||||
where
|
||||
T: Clone + Eq,
|
||||
Vec<T>: FromIterator<T>,
|
||||
{
|
||||
let mut final_vec = vec![];
|
||||
let mut temp_vec = vec![];
|
||||
for val in vals[0].iter() {
|
||||
if &auxs[0] == val {
|
||||
final_vec.push(temp_vec.clone());
|
||||
temp_vec.clear();
|
||||
continue;
|
||||
}
|
||||
temp_vec.push(val.clone());
|
||||
}
|
||||
if !temp_vec.is_empty() {
|
||||
final_vec.push(temp_vec);
|
||||
}
|
||||
Some(final_vec)
|
||||
}
|
||||
make_instruction_mult_aux!(
|
||||
vector_int,
|
||||
vector_int,
|
||||
_split_on,
|
||||
Vec<i128>,
|
||||
1,
|
||||
int,
|
||||
1,
|
||||
i128
|
||||
);
|
||||
make_instruction_mult_aux!(
|
||||
vector_float,
|
||||
vector_float,
|
||||
_split_on,
|
||||
Vec<Decimal>,
|
||||
1,
|
||||
float,
|
||||
1,
|
||||
Decimal
|
||||
);
|
||||
make_instruction_mult_aux!(
|
||||
vector_string,
|
||||
vector_string,
|
||||
_split_on,
|
||||
Vec<Vec<char>>,
|
||||
1,
|
||||
string,
|
||||
1,
|
||||
Vec<char>
|
||||
);
|
||||
make_instruction_mult_aux!(
|
||||
vector_boolean,
|
||||
vector_boolean,
|
||||
_split_on,
|
||||
Vec<bool>,
|
||||
1,
|
||||
boolean,
|
||||
1,
|
||||
bool
|
||||
);
|
||||
make_instruction_mult_aux!(
|
||||
vector_char,
|
||||
vector_char,
|
||||
_split_on,
|
||||
Vec<char>,
|
||||
1,
|
||||
char,
|
||||
1,
|
||||
char
|
||||
);
|
||||
make_instruction_mult_aux!(string, string, _split_on, Vec<char>, 1, char, 1, char);
|
||||
|
||||
/*/// Splits a vector based the first occurence of a primitive
|
||||
pub fn _split_on_vector<T>(vals: Vec<Vec<T>>) -> Option<Vec<Vec<T>>>
|
||||
where
|
||||
T: Clone + Eq,
|
||||
{
|
||||
if vals[0].is_empty() {
|
||||
return Some(vec![vals[1]]);
|
||||
}
|
||||
let mut final_vec = vec![];
|
||||
let mut temp_vec = vec![];
|
||||
for val in vals[1].windows(vals[0].len()) {
|
||||
if &auxs[0] == val {
|
||||
final_vec.push(temp_vec.clone());
|
||||
temp_vec.clear();
|
||||
continue;
|
||||
}
|
||||
temp_vec.push(val.clone());
|
||||
}
|
||||
if !temp_vec.is_empty() {
|
||||
final_vec.push(temp_vec);
|
||||
}
|
||||
Some(final_vec)
|
||||
}
|
||||
make_instruction_mult!(vector_int, vector_int, _split_on_vector, Vec<i128>, 1);
|
||||
make_instruction_mult!(
|
||||
vector_float,
|
||||
vector_float,
|
||||
_split_on_vector,
|
||||
Vec<Decimal>,
|
||||
1
|
||||
);
|
||||
make_instruction_mult!(
|
||||
vector_string,
|
||||
vector_string,
|
||||
_split_on_vector,
|
||||
Vec<Vec<char>>,
|
||||
1
|
||||
);
|
||||
make_instruction_mult!(
|
||||
vector_boolean,
|
||||
vector_boolean,
|
||||
_split_on_vector,
|
||||
Vec<bool>,
|
||||
1
|
||||
);
|
||||
make_instruction_mult!(vector_char, vector_char, _split_on_vector, Vec<char>, 1);
|
||||
make_instruction_mult!(string, string, _split_on_vector, Vec<char>, 1);*/
|
||||
|
||||
/// Replaces all values in a vector with respect to two primitives. The first primitive is
|
||||
/// the search value and the second value is the one to replace.
|
||||
/// 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,
|
||||
@ -1305,290 +1168,10 @@ make_instruction_aux!(
|
||||
);
|
||||
make_instruction_aux!(string, string, _replace, Vec<char>, 1, char, 2, char);
|
||||
|
||||
/// Removes all values in a vector with respect to a primitives. If is equal, remove it.
|
||||
pub fn _remove<T>(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 = &vals[0];
|
||||
let ret_vec = temp_vec
|
||||
.iter()
|
||||
.filter(|&x| x != &auxs[0])
|
||||
.cloned()
|
||||
.collect();
|
||||
Some(ret_vec)
|
||||
}
|
||||
make_instruction_aux!(vector_int, vector_int, _remove, Vec<i128>, 1, int, 1, i128);
|
||||
make_instruction_aux!(
|
||||
vector_float,
|
||||
vector_float,
|
||||
_remove,
|
||||
Vec<Decimal>,
|
||||
1,
|
||||
float,
|
||||
1,
|
||||
Decimal
|
||||
);
|
||||
make_instruction_aux!(
|
||||
vector_string,
|
||||
vector_string,
|
||||
_remove,
|
||||
Vec<Vec<char>>,
|
||||
1,
|
||||
string,
|
||||
1,
|
||||
Vec<char>
|
||||
);
|
||||
make_instruction_aux!(
|
||||
vector_boolean,
|
||||
vector_boolean,
|
||||
_remove,
|
||||
Vec<bool>,
|
||||
1,
|
||||
boolean,
|
||||
1,
|
||||
bool
|
||||
);
|
||||
make_instruction_aux!(
|
||||
vector_char,
|
||||
vector_char,
|
||||
_remove,
|
||||
Vec<char>,
|
||||
1,
|
||||
char,
|
||||
1,
|
||||
char
|
||||
);
|
||||
make_instruction_aux!(string, string, _remove, Vec<char>, 1, char, 1, char);
|
||||
|
||||
/// Iterates over a vector using an instruction from the exec stack.
|
||||
macro_rules! make_iterate {
|
||||
($vec_stack:ident, $prim_stack:ident, $vec_gene:ident) => {
|
||||
paste::item! {
|
||||
pub fn [< $vec_stack _iterate >] (state: &mut PushState) {
|
||||
if state.$vec_stack.is_empty() || state.exec.is_empty() {
|
||||
return;
|
||||
}
|
||||
let first_vec = state.$vec_stack.pop().unwrap();
|
||||
if first_vec.is_empty() {
|
||||
state.exec.pop();
|
||||
return;
|
||||
} else if first_vec.len() == 1 {
|
||||
state.$prim_stack.push(first_vec[0]);
|
||||
return;
|
||||
} else {
|
||||
let top_exec = state.exec[state.exec.len() - 1].clone();
|
||||
let first_prim = first_vec[0];
|
||||
state.exec.push(Gene::StateFunc([< $vec_stack _iterate >]));
|
||||
state.exec.push(Gene::$vec_gene(first_vec[1..].to_vec()));
|
||||
state.exec.push(top_exec);
|
||||
state.$prim_stack.push(first_prim);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
make_iterate!(vector_int, int, GeneVectorInt);
|
||||
make_iterate!(vector_float, float, GeneVectorFloat);
|
||||
//make_iterate!(vector_string, string, GeneVectorString);
|
||||
make_iterate!(vector_boolean, boolean, GeneVectorBoolean);
|
||||
make_iterate!(vector_char, char, GeneVectorChar);
|
||||
//make_iterate!(string, string, GeneString);
|
||||
|
||||
/// Sorts a vector
|
||||
pub fn _sort<T>(mut vals: Vec<Vec<T>>) -> Option<Vec<T>>
|
||||
where
|
||||
T: NumericTrait + Clone,
|
||||
{
|
||||
vals[0].sort();
|
||||
Some(vals[0].clone())
|
||||
}
|
||||
make_instruction_clone!(vector_int, vector_int, _sort, Vec<i128>, 1);
|
||||
make_instruction_clone!(vector_float, vector_float, _sort, Vec<Decimal>, 1);
|
||||
|
||||
/// Sorts a vector and reverses it
|
||||
pub fn _sort_reverse<T>(mut vals: Vec<Vec<T>>) -> Option<Vec<T>>
|
||||
where
|
||||
T: NumericTrait + Clone,
|
||||
{
|
||||
vals[0].sort();
|
||||
vals[0].reverse();
|
||||
Some(vals[0].clone())
|
||||
}
|
||||
make_instruction_clone!(vector_int, vector_int, _sort_reverse, Vec<i128>, 1);
|
||||
make_instruction_clone!(vector_float, vector_float, _sort_reverse, Vec<Decimal>, 1);
|
||||
|
||||
/// Inserts a primitive into a vector at a given point from the int stack
|
||||
pub fn _insert<T>(mut vals: Vec<Vec<T>>, auxs: Vec<T>, auxs2: Vec<i128>) -> Option<Vec<T>>
|
||||
where
|
||||
T: Clone,
|
||||
{
|
||||
let vec_len = vals[0].len();
|
||||
vals[0].insert(bounded_idx(auxs2[0], vec_len), auxs[0].clone());
|
||||
Some(vals[0].clone())
|
||||
}
|
||||
make_instruction_aux2!(
|
||||
vector_int,
|
||||
vector_int,
|
||||
_insert,
|
||||
Vec<i128>,
|
||||
1,
|
||||
int,
|
||||
1,
|
||||
i128,
|
||||
int,
|
||||
1,
|
||||
i128
|
||||
);
|
||||
make_instruction_aux2!(
|
||||
vector_float,
|
||||
vector_float,
|
||||
_insert,
|
||||
Vec<Decimal>,
|
||||
1,
|
||||
float,
|
||||
1,
|
||||
Decimal,
|
||||
int,
|
||||
1,
|
||||
i128
|
||||
);
|
||||
make_instruction_aux2!(
|
||||
vector_string,
|
||||
vector_string,
|
||||
_insert,
|
||||
Vec<Vec<char>>,
|
||||
1,
|
||||
string,
|
||||
1,
|
||||
Vec<char>,
|
||||
int,
|
||||
1,
|
||||
i128
|
||||
);
|
||||
make_instruction_aux2!(
|
||||
vector_boolean,
|
||||
vector_boolean,
|
||||
_insert,
|
||||
Vec<bool>,
|
||||
1,
|
||||
boolean,
|
||||
1,
|
||||
bool,
|
||||
int,
|
||||
1,
|
||||
i128
|
||||
);
|
||||
make_instruction_aux2!(
|
||||
vector_char,
|
||||
vector_char,
|
||||
_insert,
|
||||
Vec<char>,
|
||||
1,
|
||||
char,
|
||||
1,
|
||||
char,
|
||||
int,
|
||||
1,
|
||||
i128
|
||||
);
|
||||
make_instruction_aux2!(
|
||||
string,
|
||||
string,
|
||||
_insert,
|
||||
Vec<char>,
|
||||
1,
|
||||
char,
|
||||
1,
|
||||
char,
|
||||
int,
|
||||
1,
|
||||
i128
|
||||
);
|
||||
|
||||
/// Inserts one vector into another based on an index.
|
||||
pub fn _insert_vector<T>(mut vals: Vec<Vec<T>>, auxs: Vec<i128>) -> Option<Vec<T>>
|
||||
where
|
||||
T: Clone,
|
||||
{
|
||||
let vec_len = vals[0].len();
|
||||
let idx = bounded_idx(auxs[0], vec_len);
|
||||
let insert_list = vals[0].clone();
|
||||
vals[1].splice(idx..idx, insert_list);
|
||||
Some(vals[1].clone())
|
||||
}
|
||||
make_instruction_aux!(
|
||||
vector_int,
|
||||
vector_int,
|
||||
_insert_vector,
|
||||
Vec<i128>,
|
||||
2,
|
||||
int,
|
||||
1,
|
||||
i128
|
||||
);
|
||||
make_instruction_aux!(
|
||||
vector_float,
|
||||
vector_float,
|
||||
_insert_vector,
|
||||
Vec<Decimal>,
|
||||
2,
|
||||
int,
|
||||
1,
|
||||
i128
|
||||
);
|
||||
make_instruction_aux!(
|
||||
vector_string,
|
||||
vector_string,
|
||||
_insert_vector,
|
||||
Vec<Vec<char>>,
|
||||
2,
|
||||
int,
|
||||
1,
|
||||
i128
|
||||
);
|
||||
make_instruction_aux!(
|
||||
vector_boolean,
|
||||
vector_boolean,
|
||||
_insert_vector,
|
||||
Vec<bool>,
|
||||
2,
|
||||
int,
|
||||
1,
|
||||
i128
|
||||
);
|
||||
make_instruction_aux!(
|
||||
vector_char,
|
||||
vector_char,
|
||||
_insert_vector,
|
||||
Vec<char>,
|
||||
2,
|
||||
int,
|
||||
1,
|
||||
i128
|
||||
);
|
||||
make_instruction_aux!(string, string, _insert_vector, Vec<char>, 2, int, 1, i128);
|
||||
|
||||
/// Takes the mean of a vector
|
||||
pub fn _mean<T: NumericTrait + Clone>(vals: Vec<Vec<T>>) -> Option<T> {
|
||||
let mut fin_num = T::zero();
|
||||
for num in vals[0].clone().into_iter() {
|
||||
fin_num = fin_num + num;
|
||||
}
|
||||
Some(fin_num.div(T::from_usize(vals[0].len())))
|
||||
}
|
||||
make_instruction_clone!(vector_int, int, _mean, Vec<i128>, 1);
|
||||
make_instruction_clone!(vector_float, float, _mean, Vec<Decimal>, 1);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::instructions::numeric::int_inc;
|
||||
use crate::push::interpreter::interpret_program;
|
||||
use crate::push::state::EMPTY_STATE;
|
||||
use rust_decimal::dec;
|
||||
|
||||
#[test]
|
||||
fn vector_concat_test() {
|
||||
@ -2149,45 +1732,6 @@ mod tests {
|
||||
assert_eq!(vec![vec![true, false, true]], test_state.vector_boolean);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_to_prim_test() {
|
||||
let mut test_state = EMPTY_STATE;
|
||||
|
||||
test_state.vector_int = vec![vec![0, 1, 2]];
|
||||
vector_int_parse_to_prim(&mut test_state);
|
||||
assert_eq!(vec![vec![0], vec![1], vec![2]], test_state.vector_int);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_on_test() {
|
||||
let mut test_state = EMPTY_STATE;
|
||||
|
||||
test_state.vector_int = vec![vec![0, 1, 2]];
|
||||
test_state.int = vec![1];
|
||||
vector_int_split_on(&mut test_state);
|
||||
assert_eq!(vec![vec![0], vec![2]], test_state.vector_int);
|
||||
|
||||
test_state.vector_int = vec![vec![0, 1, 2, 1, 5]];
|
||||
test_state.int = vec![1];
|
||||
vector_int_split_on(&mut test_state);
|
||||
assert_eq!(vec![vec![0], vec![2], vec![5]], test_state.vector_int);
|
||||
|
||||
test_state.vector_int = vec![vec![0, 1, 2, 1]];
|
||||
test_state.int = vec![1];
|
||||
vector_int_split_on(&mut test_state);
|
||||
assert_eq!(vec![vec![0], vec![2]], test_state.vector_int);
|
||||
|
||||
test_state.vector_int = vec![vec![0, 1, 2, 1]];
|
||||
test_state.int = vec![9];
|
||||
vector_int_split_on(&mut test_state);
|
||||
assert_eq!(vec![vec![0, 1, 2, 1]], test_state.vector_int);
|
||||
|
||||
test_state.vector_int = vec![vec![0, 1, 2, 3]];
|
||||
test_state.int = vec![3];
|
||||
vector_int_split_on(&mut test_state);
|
||||
assert_eq!(vec![vec![0, 1, 2]], test_state.vector_int);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn replace_test() {
|
||||
let mut test_state = EMPTY_STATE;
|
||||
@ -2197,137 +1741,4 @@ mod tests {
|
||||
vector_int_replace(&mut test_state);
|
||||
assert_eq!(vec![vec![0, 1, 3, 3, 4, 5, 3]], test_state.vector_int);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove_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];
|
||||
vector_int_remove(&mut test_state);
|
||||
assert_eq!(vec![vec![0, 1, 2, 4, 5, 2]], test_state.vector_int);
|
||||
|
||||
test_state.vector_int = vec![vec![0, 1, 2, 3, 4, 5, 2]];
|
||||
test_state.int = vec![2];
|
||||
vector_int_remove(&mut test_state);
|
||||
assert_eq!(vec![vec![0, 1, 3, 4, 5]], test_state.vector_int);
|
||||
|
||||
test_state.vector_int = vec![vec![0, 1, 2, 3, 4, 5, 2]];
|
||||
test_state.int = vec![9];
|
||||
vector_int_remove(&mut test_state);
|
||||
assert_eq!(vec![vec![0, 1, 2, 3, 4, 5, 2]], test_state.vector_int);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn iterate_test() {
|
||||
let mut test_state = EMPTY_STATE;
|
||||
|
||||
test_state.vector_int = vec![vec![0, 1, 2, 3, 4, 5, 2]];
|
||||
test_state.exec = vec![
|
||||
Gene::StateFunc(int_inc),
|
||||
Gene::StateFunc(vector_int_iterate),
|
||||
];
|
||||
interpret_program(&mut test_state, 1000, 1000);
|
||||
assert_eq!(vec![1, 2, 3, 4, 5, 6, 3], test_state.int);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sort_test() {
|
||||
let mut test_state = EMPTY_STATE;
|
||||
|
||||
test_state.vector_int = vec![vec![0, 1, 2, 3, 4, 5, 2]];
|
||||
vector_int_sort(&mut test_state);
|
||||
assert_eq!(vec![vec![0, 1, 2, 2, 3, 4, 5]], test_state.vector_int);
|
||||
|
||||
test_state.vector_float = vec![vec![dec!(0.0), dec!(1.2), dec!(-3.4)]];
|
||||
vector_float_sort(&mut test_state);
|
||||
assert_eq!(
|
||||
vec![vec![dec!(-3.4), dec!(0.0), dec!(1.2)]],
|
||||
test_state.vector_float
|
||||
);
|
||||
|
||||
test_state.vector_int = vec![vec![]];
|
||||
vector_int_sort(&mut test_state);
|
||||
assert_eq!(vec![Vec::<i128>::new()], test_state.vector_int);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sort_reverse_test() {
|
||||
let mut test_state = EMPTY_STATE;
|
||||
|
||||
test_state.vector_int = vec![vec![0, 1, 2, 3, 4, 5, 2]];
|
||||
vector_int_sort_reverse(&mut test_state);
|
||||
assert_eq!(vec![vec![5, 4, 3, 2, 2, 1, 0]], test_state.vector_int);
|
||||
|
||||
test_state.vector_float = vec![vec![dec!(0.0), dec!(1.2), dec!(-3.4)]];
|
||||
vector_float_sort_reverse(&mut test_state);
|
||||
assert_eq!(
|
||||
vec![vec![dec!(1.2), dec!(0.0), dec!(-3.4)]],
|
||||
test_state.vector_float
|
||||
);
|
||||
|
||||
test_state.vector_int = vec![vec![]];
|
||||
vector_int_sort_reverse(&mut test_state);
|
||||
assert_eq!(vec![Vec::<i128>::new()], test_state.vector_int);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert_test() {
|
||||
let mut test_state = EMPTY_STATE;
|
||||
|
||||
test_state.vector_int = vec![vec![0, 1, 2, 3]];
|
||||
test_state.int = vec![9, 1];
|
||||
vector_int_insert(&mut test_state);
|
||||
assert_eq!(vec![vec![0, 9, 1, 2, 3]], test_state.vector_int);
|
||||
|
||||
test_state.vector_boolean = vec![vec![false, true, false]];
|
||||
test_state.int = vec![0];
|
||||
test_state.boolean = vec![false];
|
||||
vector_boolean_insert(&mut test_state);
|
||||
assert_eq!(
|
||||
vec![vec![false, false, true, false]],
|
||||
test_state.vector_boolean
|
||||
);
|
||||
|
||||
test_state.vector_int = vec![vec![0, 1, 2, 3]];
|
||||
test_state.int = vec![9, 5];
|
||||
vector_int_insert(&mut test_state);
|
||||
assert_eq!(vec![vec![0, 9, 1, 2, 3]], test_state.vector_int);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert_vector_test() {
|
||||
let mut test_state = EMPTY_STATE;
|
||||
|
||||
test_state.vector_int = vec![vec![0, 1, 2, 3], vec![69, 69]];
|
||||
test_state.int = vec![1];
|
||||
vector_int_insert_vector(&mut test_state);
|
||||
assert_eq!(vec![vec![0, 69, 69, 1, 2, 3]], test_state.vector_int);
|
||||
|
||||
test_state.vector_boolean = vec![vec![false, true, false], vec![false, true]];
|
||||
test_state.int = vec![0];
|
||||
vector_boolean_insert_vector(&mut test_state);
|
||||
assert_eq!(
|
||||
vec![vec![false, true, false, true, false]],
|
||||
test_state.vector_boolean
|
||||
);
|
||||
|
||||
test_state.vector_int = vec![vec![0, 1, 2, 3], vec![69, 69]];
|
||||
test_state.int = vec![5];
|
||||
vector_int_insert_vector(&mut test_state);
|
||||
assert_eq!(vec![vec![0, 69, 69, 1, 2, 3]], test_state.vector_int);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mean_test() {
|
||||
let mut test_state = EMPTY_STATE;
|
||||
|
||||
test_state.vector_int = vec![vec![6, 5, 4]];
|
||||
vector_int_mean(&mut test_state);
|
||||
assert_eq!(vec![5], test_state.int);
|
||||
|
||||
test_state.vector_float = vec![vec![dec!(6.0), dec!(5.0), dec!(4.0)]];
|
||||
vector_float_mean(&mut test_state);
|
||||
assert_eq!(vec![dec!(5.0)], test_state.float);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user