2020-10-28 15:30:35 +01:00
|
|
|
use crate::dynamic::Variant;
|
2021-03-04 16:47:52 +01:00
|
|
|
use crate::stdlib::{boxed::Box, ops::Range};
|
|
|
|
use crate::{def_package, EvalAltResult, INT};
|
|
|
|
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
use crate::stdlib::string::ToString;
|
|
|
|
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
use num_traits::{CheckedAdd as Add, CheckedSub as Sub};
|
|
|
|
|
|
|
|
#[cfg(feature = "unchecked")]
|
|
|
|
use crate::stdlib::ops::{Add, Sub};
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-10-18 11:02:17 +02:00
|
|
|
fn get_range<T: Variant + Clone>(from: T, to: T) -> Result<Range<T>, Box<EvalAltResult>> {
|
2020-05-13 13:21:42 +02:00
|
|
|
Ok(from..to)
|
|
|
|
}
|
|
|
|
|
2020-04-20 18:11:25 +02:00
|
|
|
// Register range function with step
|
|
|
|
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
|
|
|
|
struct StepRange<T>(T, T, T)
|
|
|
|
where
|
2021-03-04 16:47:52 +01:00
|
|
|
T: Variant + Copy + PartialOrd + Add<Output = T> + Sub<Output = T>;
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2021-02-27 08:27:40 +01:00
|
|
|
impl<T> StepRange<T>
|
|
|
|
where
|
2021-03-04 16:47:52 +01:00
|
|
|
T: Variant + Copy + PartialOrd + Add<Output = T> + Sub<Output = T>,
|
2021-02-27 08:27:40 +01:00
|
|
|
{
|
|
|
|
pub fn new(from: T, to: T, step: T) -> Result<Self, Box<EvalAltResult>> {
|
2021-03-04 16:47:52 +01:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
if let Some(r) = from.checked_add(&step) {
|
|
|
|
if r == from {
|
|
|
|
return Err(Box::new(EvalAltResult::ErrorInFunctionCall(
|
|
|
|
"range".to_string(),
|
|
|
|
"".to_string(),
|
|
|
|
Box::new(EvalAltResult::ErrorArithmetic(
|
|
|
|
"step value cannot be zero".to_string(),
|
|
|
|
crate::Position::NONE,
|
|
|
|
)),
|
|
|
|
crate::Position::NONE,
|
|
|
|
)));
|
|
|
|
}
|
2021-02-27 08:27:40 +01:00
|
|
|
}
|
2021-03-04 16:47:52 +01:00
|
|
|
|
|
|
|
Ok(Self(from, to, step))
|
2021-02-27 08:27:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 18:11:25 +02:00
|
|
|
impl<T> Iterator for StepRange<T>
|
|
|
|
where
|
2021-03-04 16:47:52 +01:00
|
|
|
T: Variant + Copy + PartialOrd + Add<Output = T> + Sub<Output = T>,
|
2020-04-20 18:11:25 +02:00
|
|
|
{
|
|
|
|
type Item = T;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<T> {
|
2021-02-27 08:27:40 +01:00
|
|
|
if self.0 == self.1 {
|
|
|
|
None
|
|
|
|
} else if self.0 < self.1 {
|
2021-03-04 16:47:52 +01:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
let diff1 = if let Some(diff) = self.1.checked_sub(&self.0) {
|
|
|
|
diff
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
#[cfg(feature = "unchecked")]
|
|
|
|
let diff1 = self.1 - self.0;
|
2021-03-04 14:43:00 +01:00
|
|
|
|
2021-03-04 16:47:52 +01:00
|
|
|
let v = self.0;
|
2021-03-04 14:43:00 +01:00
|
|
|
|
2021-03-04 16:47:52 +01:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
let n = if let Some(num) = self.0.checked_add(&self.2) {
|
|
|
|
num
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
#[cfg(feature = "unchecked")]
|
|
|
|
let n = self.0 + self.2;
|
|
|
|
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
let diff2 = if let Some(diff) = self.1.checked_sub(&n) {
|
|
|
|
diff
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
#[cfg(feature = "unchecked")]
|
|
|
|
let diff2 = self.1 - n;
|
2021-03-04 14:43:00 +01:00
|
|
|
|
|
|
|
if diff2 >= diff1 {
|
|
|
|
None
|
|
|
|
} else {
|
2021-03-04 16:47:52 +01:00
|
|
|
self.0 = if n >= self.1 { self.1 } else { n };
|
2021-03-04 14:43:00 +01:00
|
|
|
Some(v)
|
|
|
|
}
|
2020-04-20 18:11:25 +02:00
|
|
|
} else {
|
2021-03-04 16:47:52 +01:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
let diff1 = if let Some(diff) = self.0.checked_sub(&self.1) {
|
|
|
|
diff
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
#[cfg(feature = "unchecked")]
|
|
|
|
let diff1 = self.0 - self.1;
|
2021-03-04 14:43:00 +01:00
|
|
|
|
2021-03-04 16:47:52 +01:00
|
|
|
let v = self.0;
|
2021-03-04 14:43:00 +01:00
|
|
|
|
2021-03-04 16:47:52 +01:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
let n = if let Some(num) = self.0.checked_add(&self.2) {
|
|
|
|
num
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
#[cfg(feature = "unchecked")]
|
|
|
|
let n = self.0 + self.2;
|
|
|
|
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
let diff2 = if let Some(diff) = n.checked_sub(&self.1) {
|
|
|
|
diff
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
#[cfg(feature = "unchecked")]
|
|
|
|
let diff2 = n - self.1;
|
2021-03-04 14:43:00 +01:00
|
|
|
|
|
|
|
if diff2 >= diff1 {
|
|
|
|
None
|
|
|
|
} else {
|
2021-03-04 16:47:52 +01:00
|
|
|
self.0 = if n <= self.1 { self.1 } else { n };
|
2021-03-04 14:43:00 +01:00
|
|
|
Some(v)
|
|
|
|
}
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-18 11:02:17 +02:00
|
|
|
fn get_step_range<T>(from: T, to: T, step: T) -> Result<StepRange<T>, Box<EvalAltResult>>
|
2020-05-13 13:21:42 +02:00
|
|
|
where
|
2021-03-04 16:47:52 +01:00
|
|
|
T: Variant + Copy + PartialOrd + Add<Output = T> + Sub<Output = T>,
|
2020-05-13 13:21:42 +02:00
|
|
|
{
|
2021-02-27 08:27:40 +01:00
|
|
|
StepRange::<T>::new(from, to, step)
|
2020-05-13 13:21:42 +02:00
|
|
|
}
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-10-19 17:49:01 +02:00
|
|
|
macro_rules! reg_range {
|
2021-03-04 16:47:52 +01:00
|
|
|
($lib:ident | $x:expr => $( $y:ty ),*) => {
|
2020-10-19 17:49:01 +02:00
|
|
|
$(
|
|
|
|
$lib.set_iterator::<Range<$y>>();
|
2020-11-22 10:21:34 +01:00
|
|
|
let hash = $lib.set_fn_2($x, get_range::<$y>);
|
2021-03-01 08:58:11 +01:00
|
|
|
$lib.update_fn_metadata(hash, &[
|
2020-11-22 15:15:17 +01:00
|
|
|
concat!("from: ", stringify!($y)),
|
|
|
|
concat!("to: ", stringify!($y)),
|
|
|
|
concat!("Iterator<Item=", stringify!($y), ">")
|
|
|
|
]);
|
2020-10-19 17:49:01 +02:00
|
|
|
)*
|
2021-03-04 16:47:52 +01:00
|
|
|
};
|
|
|
|
($lib:ident | step $x:expr => $( $y:ty ),*) => {
|
2020-10-19 17:49:01 +02:00
|
|
|
$(
|
|
|
|
$lib.set_iterator::<StepRange<$y>>();
|
2020-11-22 10:21:34 +01:00
|
|
|
let hash = $lib.set_fn_3($x, get_step_range::<$y>);
|
2021-03-01 08:58:11 +01:00
|
|
|
$lib.update_fn_metadata(hash, &[
|
2020-11-22 15:15:17 +01:00
|
|
|
concat!("from: ", stringify!($y)),
|
|
|
|
concat!("to: ", stringify!($y)),
|
2021-02-19 16:13:53 +01:00
|
|
|
concat!("step: ", stringify!($y)),
|
|
|
|
concat!("Iterator<Item=", stringify!($y), ">")
|
2020-11-22 15:15:17 +01:00
|
|
|
]);
|
2020-10-19 17:49:01 +02:00
|
|
|
)*
|
2021-03-04 16:47:52 +01:00
|
|
|
};
|
2020-10-19 17:49:01 +02:00
|
|
|
}
|
|
|
|
|
2020-05-13 13:21:42 +02:00
|
|
|
def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, {
|
2021-03-04 16:47:52 +01:00
|
|
|
reg_range!(lib | "range" => INT);
|
2020-04-21 17:01:10 +02:00
|
|
|
|
2020-10-19 17:49:01 +02:00
|
|
|
#[cfg(not(feature = "only_i32"))]
|
|
|
|
#[cfg(not(feature = "only_i64"))]
|
|
|
|
{
|
2021-03-04 16:47:52 +01:00
|
|
|
reg_range!(lib | "range" => i8, u8, i16, u16, i32, u32, i64, u64);
|
2020-06-17 10:50:46 +02:00
|
|
|
|
2020-07-31 16:30:23 +02:00
|
|
|
if cfg!(not(target_arch = "wasm32")) {
|
2021-03-04 16:47:52 +01:00
|
|
|
reg_range!(lib | "range" => i128, u128);
|
2020-07-31 16:30:23 +02:00
|
|
|
}
|
2020-04-21 17:01:10 +02:00
|
|
|
}
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2021-03-04 16:47:52 +01:00
|
|
|
reg_range!(lib | step "range" => INT);
|
2020-04-21 17:01:10 +02:00
|
|
|
|
2020-10-19 17:49:01 +02:00
|
|
|
#[cfg(not(feature = "only_i32"))]
|
|
|
|
#[cfg(not(feature = "only_i64"))]
|
|
|
|
{
|
2021-03-04 16:47:52 +01:00
|
|
|
reg_range!(lib | step "range" => i8, u8, i16, u16, i32, u32, i64, u64);
|
2020-06-17 10:50:46 +02:00
|
|
|
|
2020-07-31 16:30:23 +02:00
|
|
|
if cfg!(not(target_arch = "wasm32")) {
|
2021-03-04 16:47:52 +01:00
|
|
|
reg_range!(lib | step "range" => i128, u128);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "decimal")]
|
|
|
|
{
|
2021-03-05 05:58:27 +01:00
|
|
|
use rust_decimal::{
|
|
|
|
prelude::{One, Zero},
|
|
|
|
Decimal,
|
|
|
|
};
|
|
|
|
|
2021-03-04 16:47:52 +01:00
|
|
|
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
|
|
|
|
struct StepDecimalRange(Decimal, Decimal, Decimal);
|
|
|
|
|
|
|
|
impl StepDecimalRange {
|
|
|
|
pub fn new(from: Decimal, to: Decimal, step: Decimal) -> Result<Self, Box<EvalAltResult>> {
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
if step.is_zero() {
|
|
|
|
use crate::stdlib::string::ToString;
|
|
|
|
|
|
|
|
return Err(Box::new(EvalAltResult::ErrorInFunctionCall("range".to_string(), "".to_string(),
|
|
|
|
Box::new(EvalAltResult::ErrorArithmetic("step value cannot be zero".to_string(), crate::Position::NONE)),
|
|
|
|
crate::Position::NONE,
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Self(from, to, step))
|
|
|
|
}
|
2020-07-31 16:30:23 +02:00
|
|
|
}
|
2021-03-04 16:47:52 +01:00
|
|
|
|
|
|
|
impl Iterator for StepDecimalRange {
|
|
|
|
type Item = Decimal;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Decimal> {
|
|
|
|
if self.0 == self.1 {
|
|
|
|
None
|
|
|
|
} else if self.0 < self.1 {
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
if self.2.is_sign_negative() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let v = self.0;
|
|
|
|
let n = self.0 + self.2;
|
|
|
|
|
|
|
|
self.0 = if n >= self.1 { self.1 } else { n };
|
|
|
|
Some(v)
|
|
|
|
} else {
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
if self.2.is_sign_positive() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let v = self.0;
|
|
|
|
let n = self.0 + self.2;
|
|
|
|
|
|
|
|
self.0 = if n <= self.1 { self.1 } else { n };
|
|
|
|
Some(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lib.set_iterator::<StepDecimalRange>();
|
|
|
|
|
|
|
|
let hash = lib.set_fn_2("range", |from, to| StepDecimalRange::new(from, to, Decimal::one()));
|
|
|
|
lib.update_fn_metadata(hash, &["from: Decimal", "to: Decimal", "Iterator<Item=Decimal>"]);
|
|
|
|
|
|
|
|
let hash = lib.set_fn_3("range", |from, to, step| StepDecimalRange::new(from, to, step));
|
|
|
|
lib.update_fn_metadata(hash, &["from: Decimal", "to: Decimal", "step: Decimal", "Iterator<Item=Decimal>"]);
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
2020-04-21 17:01:10 +02:00
|
|
|
});
|