2020-05-06 13:45:17 +02:00
|
|
|
#![cfg(not(feature = "no_index"))]
|
|
|
|
|
2020-04-20 18:11:25 +02:00
|
|
|
use crate::any::{Dynamic, Variant};
|
2020-04-21 17:25:12 +02:00
|
|
|
use crate::def_package;
|
2020-04-20 18:11:25 +02:00
|
|
|
use crate::engine::Array;
|
2020-05-13 13:21:42 +02:00
|
|
|
use crate::module::FuncReturn;
|
2020-04-20 18:11:25 +02:00
|
|
|
use crate::parser::INT;
|
|
|
|
|
2020-04-24 06:39:24 +02:00
|
|
|
use crate::stdlib::{any::TypeId, boxed::Box, string::String};
|
2020-04-20 18:11:25 +02:00
|
|
|
|
|
|
|
// Register array utility functions
|
2020-05-13 13:21:42 +02:00
|
|
|
fn push<T: Variant + Clone>(list: &mut Array, item: T) -> FuncReturn<()> {
|
2020-04-20 18:11:25 +02:00
|
|
|
list.push(Dynamic::from(item));
|
2020-05-13 13:21:42 +02:00
|
|
|
Ok(())
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
2020-05-13 13:21:42 +02:00
|
|
|
fn ins<T: Variant + Clone>(list: &mut Array, position: INT, item: T) -> FuncReturn<()> {
|
2020-04-20 18:11:25 +02:00
|
|
|
if position <= 0 {
|
|
|
|
list.insert(0, Dynamic::from(item));
|
|
|
|
} else if (position as usize) >= list.len() - 1 {
|
2020-05-13 13:39:34 +02:00
|
|
|
push(list, item)?;
|
2020-04-20 18:11:25 +02:00
|
|
|
} else {
|
|
|
|
list.insert(position as usize, Dynamic::from(item));
|
|
|
|
}
|
2020-05-13 13:21:42 +02:00
|
|
|
Ok(())
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
2020-05-13 13:21:42 +02:00
|
|
|
fn pad<T: Variant + Clone>(list: &mut Array, len: INT, item: T) -> FuncReturn<()> {
|
2020-04-20 18:11:25 +02:00
|
|
|
if len >= 0 {
|
|
|
|
while list.len() < len as usize {
|
2020-05-13 13:39:34 +02:00
|
|
|
push(list, item.clone())?;
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-13 13:21:42 +02:00
|
|
|
Ok(())
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
|
|
|
|
2020-05-13 13:21:42 +02:00
|
|
|
macro_rules! reg_op {
|
|
|
|
($lib:expr, $op:expr, $func:ident, $($par:ty),*) => {
|
|
|
|
$( $lib.set_fn_2_mut($op, $func::<$par>); )*
|
|
|
|
};
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
2020-05-13 13:21:42 +02:00
|
|
|
macro_rules! reg_tri {
|
|
|
|
($lib:expr, $op:expr, $func:ident, $($par:ty),*) => {
|
|
|
|
$( $lib.set_fn_3_mut($op, $func::<$par>); )*
|
|
|
|
};
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
|
|
|
|
2020-04-21 17:01:10 +02:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2020-04-22 08:55:40 +02:00
|
|
|
def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, {
|
2020-04-21 17:01:10 +02:00
|
|
|
reg_op!(lib, "push", push, INT, bool, char, String, Array, ());
|
|
|
|
reg_tri!(lib, "pad", pad, INT, bool, char, String, Array, ());
|
|
|
|
reg_tri!(lib, "insert", ins, INT, bool, char, String, Array, ());
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-05-13 13:21:42 +02:00
|
|
|
lib.set_fn_2_mut("append", |x: &mut Array, y: Array| {
|
|
|
|
x.extend(y);
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
lib.set_fn_2(
|
2020-04-21 17:01:10 +02:00
|
|
|
"+",
|
|
|
|
|mut x: Array, y: Array| {
|
|
|
|
x.extend(y);
|
2020-05-13 13:21:42 +02:00
|
|
|
Ok(x)
|
2020-04-21 17:01:10 +02:00
|
|
|
},
|
|
|
|
);
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-04-21 17:01:10 +02:00
|
|
|
#[cfg(not(feature = "only_i32"))]
|
|
|
|
#[cfg(not(feature = "only_i64"))]
|
|
|
|
{
|
|
|
|
reg_op!(lib, "push", push, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128);
|
|
|
|
reg_tri!(lib, "pad", pad, i8, u8, i16, u16, i32, u32, i64, u64, i128, u128);
|
|
|
|
reg_tri!(lib, "insert", ins, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128);
|
|
|
|
}
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-04-21 17:01:10 +02:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
{
|
|
|
|
reg_op!(lib, "push", push, f32, f64);
|
|
|
|
reg_tri!(lib, "pad", pad, f32, f64);
|
|
|
|
reg_tri!(lib, "insert", ins, f32, f64);
|
|
|
|
}
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-05-13 13:21:42 +02:00
|
|
|
lib.set_fn_1_mut(
|
2020-04-21 17:01:10 +02:00
|
|
|
"pop",
|
2020-05-13 13:21:42 +02:00
|
|
|
|list: &mut Array| Ok(list.pop().unwrap_or_else(|| ().into())),
|
2020-04-21 17:01:10 +02:00
|
|
|
);
|
2020-05-13 13:21:42 +02:00
|
|
|
lib.set_fn_1_mut(
|
2020-04-21 17:01:10 +02:00
|
|
|
"shift",
|
|
|
|
|list: &mut Array| {
|
2020-05-13 13:21:42 +02:00
|
|
|
Ok(if list.is_empty() {
|
2020-04-22 08:55:40 +02:00
|
|
|
().into()
|
2020-04-21 17:01:10 +02:00
|
|
|
} else {
|
|
|
|
list.remove(0)
|
2020-05-13 13:21:42 +02:00
|
|
|
})
|
2020-04-21 17:01:10 +02:00
|
|
|
},
|
|
|
|
);
|
2020-05-13 13:21:42 +02:00
|
|
|
lib.set_fn_2_mut(
|
2020-04-21 17:01:10 +02:00
|
|
|
"remove",
|
|
|
|
|list: &mut Array, len: INT| {
|
2020-05-13 13:21:42 +02:00
|
|
|
Ok(if len < 0 || (len as usize) >= list.len() {
|
2020-04-22 08:55:40 +02:00
|
|
|
().into()
|
2020-04-21 17:01:10 +02:00
|
|
|
} else {
|
|
|
|
list.remove(len as usize)
|
2020-05-13 13:21:42 +02:00
|
|
|
})
|
2020-04-21 17:01:10 +02:00
|
|
|
},
|
|
|
|
);
|
2020-05-13 13:21:42 +02:00
|
|
|
lib.set_fn_1_mut("len", |list: &mut Array| Ok(list.len() as INT));
|
|
|
|
lib.set_fn_1_mut("clear", |list: &mut Array| {
|
|
|
|
list.clear();
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
lib.set_fn_2_mut(
|
2020-04-21 17:01:10 +02:00
|
|
|
"truncate",
|
|
|
|
|list: &mut Array, len: INT| {
|
|
|
|
if len >= 0 {
|
|
|
|
list.truncate(len as usize);
|
2020-04-24 14:05:34 +02:00
|
|
|
} else {
|
|
|
|
list.clear();
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
2020-05-13 13:21:42 +02:00
|
|
|
Ok(())
|
2020-04-21 17:01:10 +02:00
|
|
|
},
|
|
|
|
);
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-04-21 17:01:10 +02:00
|
|
|
// Register array iterator
|
2020-05-13 14:22:05 +02:00
|
|
|
lib.set_iter(
|
2020-04-21 17:01:10 +02:00
|
|
|
TypeId::of::<Array>(),
|
2020-05-11 12:55:58 +02:00
|
|
|
Box::new(|arr| Box::new(
|
|
|
|
arr.cast::<Array>().into_iter()) as Box<dyn Iterator<Item = Dynamic>>
|
|
|
|
),
|
2020-04-21 17:01:10 +02:00
|
|
|
);
|
|
|
|
});
|