Streamline step ranges.

This commit is contained in:
Stephen Chung 2022-06-20 18:52:07 +08:00
parent addb579f30
commit d6bfd8f617
2 changed files with 94 additions and 250 deletions

View File

@ -86,7 +86,7 @@ fn map_std_type_name(name: &str, shorthands: bool) -> &str {
} }
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
if name == type_name::<crate::packages::iter_basic::float::StepFloatRange>() { if name == type_name::<crate::packages::iter_basic::StepRange<crate::FLOAT>>() {
return if shorthands { return if shorthands {
"range" "range"
} else { } else {
@ -94,7 +94,7 @@ fn map_std_type_name(name: &str, shorthands: bool) -> &str {
}; };
} }
#[cfg(feature = "decimal")] #[cfg(feature = "decimal")]
if name == type_name::<crate::packages::iter_basic::decimal::StepDecimalRange>() { if name == type_name::<crate::packages::iter_basic::StepRange<rust_decimal::Decimal>>() {
return if shorthands { return if shorthands {
"range" "range"
} else { } else {

View File

@ -1,34 +1,59 @@
use crate::eval::calc_index; use crate::eval::calc_index;
use crate::plugin::*; use crate::plugin::*;
use crate::types::dynamic::Variant;
use crate::{def_package, ExclusiveRange, InclusiveRange, RhaiResultOf, INT, INT_BITS}; use crate::{def_package, ExclusiveRange, InclusiveRange, RhaiResultOf, INT, INT_BITS};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
use std::{ use std::{
any::type_name,
cmp::Ordering,
fmt::Debug,
iter::{ExactSizeIterator, FusedIterator}, iter::{ExactSizeIterator, FusedIterator},
ops::{Range, RangeInclusive}, ops::{Range, RangeInclusive},
}; };
#[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "unchecked"))]
use num_traits::{CheckedAdd as Add, CheckedSub as Sub}; #[inline(always)]
fn std_add<T>(x: T, y: T) -> Option<T>
#[cfg(feature = "unchecked")] where
use std::ops::{Add, Sub}; T: Debug + Copy + PartialOrd + num_traits::CheckedAdd<Output = T>,
{
x.checked_add(&y)
}
#[inline(always)]
fn regular_add<T>(x: T, y: T) -> Option<T>
where
T: Debug + Copy + PartialOrd + std::ops::Add<Output = T>,
{
Some(x + y)
}
// Range iterator with step // Range iterator with step
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)] #[derive(Clone, Copy, Hash, Eq, PartialEq)]
pub struct StepRange<T>(T, T, T) pub struct StepRange<T: Debug + Copy + PartialOrd> {
where pub from: T,
T: Variant + Copy + PartialOrd + Add<Output = T> + Sub<Output = T>; pub to: T,
pub step: T,
pub add: fn(T, T) -> Option<T>,
pub dir: i8,
}
impl<T> StepRange<T> impl<T: Debug + Copy + PartialOrd> Debug for StepRange<T> {
where fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
T: Variant + Copy + PartialOrd + Add<Output = T> + Sub<Output = T>, f.debug_tuple(&format!("StepRange<{}>", type_name::<T>()))
{ .field(&self.from)
pub fn new(from: T, to: T, step: T) -> RhaiResultOf<Self> { .field(&self.to)
#[cfg(not(feature = "unchecked"))] .field(&self.step)
if let Some(r) = from.checked_add(&step) { .finish()
if r == from { }
}
impl<T: Debug + Copy + PartialOrd> StepRange<T> {
pub fn new(from: T, to: T, step: T, add: fn(T, T) -> Option<T>) -> RhaiResultOf<Self> {
let mut dir = 0;
if let Some(n) = add(from, step) {
#[cfg(not(feature = "unchecked"))]
if n == from {
return Err(crate::ERR::ErrorInFunctionCall( return Err(crate::ERR::ErrorInFunctionCall(
"range".to_string(), "range".to_string(),
String::new(), String::new(),
@ -41,77 +66,53 @@ where
) )
.into()); .into());
} }
match from.partial_cmp(&to).unwrap_or(Ordering::Equal) {
Ordering::Less if n > from => dir = 1,
Ordering::Greater if n < from => dir = -1,
_ => (),
}
} }
Ok(Self(from, to, step)) Ok(Self {
from,
to,
step,
add,
dir,
})
} }
} }
impl<T> Iterator for StepRange<T> impl<T: Debug + Copy + PartialOrd> Iterator for StepRange<T> {
where
T: Variant + Copy + PartialOrd + Add<Output = T> + Sub<Output = T>,
{
type Item = T; type Item = T;
fn next(&mut self) -> Option<T> { fn next(&mut self) -> Option<T> {
if self.0 == self.1 { if self.dir == 0 {
None return None;
} else if self.0 < self.1 { }
#[cfg(not(feature = "unchecked"))]
let diff1 = self.1.checked_sub(&self.0)?;
#[cfg(feature = "unchecked")]
let diff1 = self.1 - self.0;
let v = self.0; let v = self.from;
#[cfg(not(feature = "unchecked"))] self.from = (self.add)(self.from, self.step)?;
let n = self.0.checked_add(&self.2)?;
#[cfg(feature = "unchecked")]
let n = self.0 + self.2;
#[cfg(not(feature = "unchecked"))] if self.dir > 0 {
let diff2 = self.1.checked_sub(&n)?; if self.from >= self.to {
#[cfg(feature = "unchecked")] self.dir = 0;
let diff2 = self.1 - n; }
} else if self.dir < 0 {
if diff2 >= diff1 { if self.from <= self.to {
None self.dir = 0;
} else {
self.0 = if n >= self.1 { self.1 } else { n };
Some(v)
} }
} else { } else {
#[cfg(not(feature = "unchecked"))] unreachable!();
let diff1 = self.0.checked_sub(&self.1)?;
#[cfg(feature = "unchecked")]
let diff1 = self.0 - self.1;
let v = self.0;
#[cfg(not(feature = "unchecked"))]
let n = self.0.checked_add(&self.2)?;
#[cfg(feature = "unchecked")]
let n = self.0 + self.2;
#[cfg(not(feature = "unchecked"))]
let diff2 = n.checked_sub(&self.1)?;
#[cfg(feature = "unchecked")]
let diff2 = n - self.1;
if diff2 >= diff1 {
None
} else {
self.0 = if n <= self.1 { self.1 } else { n };
Some(v)
}
} }
Some(v)
} }
} }
impl<T> FusedIterator for StepRange<T> where impl<T: Debug + Copy + PartialOrd> FusedIterator for StepRange<T> {}
T: Variant + Copy + PartialOrd + Add<Output = T> + Sub<Output = T>
{
}
// Bit-field iterator with step // Bit-field iterator with step
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)] #[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
@ -237,134 +238,6 @@ impl ExactSizeIterator for CharsStream {
} }
} }
#[cfg(not(feature = "no_float"))]
pub mod float {
use super::*;
use crate::FLOAT;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct StepFloatRange(FLOAT, FLOAT, FLOAT);
impl StepFloatRange {
pub fn new(from: FLOAT, to: FLOAT, step: FLOAT) -> RhaiResultOf<Self> {
#[cfg(not(feature = "unchecked"))]
if step == 0.0 {
return Err(crate::ERR::ErrorInFunctionCall(
"range".to_string(),
"".to_string(),
crate::ERR::ErrorArithmetic(
"step value cannot be zero".to_string(),
Position::NONE,
)
.into(),
Position::NONE,
)
.into());
}
Ok(Self(from, to, step))
}
}
impl Iterator for StepFloatRange {
type Item = FLOAT;
fn next(&mut self) -> Option<FLOAT> {
if self.0 == self.1 {
None
} else if self.0 < self.1 {
#[cfg(not(feature = "unchecked"))]
if self.2 < 0.0 {
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 > 0.0 {
return None;
}
let v = self.0;
let n = self.0 + self.2;
self.0 = if n <= self.1 { self.1 } else { n };
Some(v)
}
}
}
impl FusedIterator for StepFloatRange {}
}
#[cfg(feature = "decimal")]
pub mod decimal {
use super::*;
use rust_decimal::Decimal;
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
pub struct StepDecimalRange(Decimal, Decimal, Decimal);
impl StepDecimalRange {
pub fn new(from: Decimal, to: Decimal, step: Decimal) -> RhaiResultOf<Self> {
#[cfg(not(feature = "unchecked"))]
if step.is_zero() {
return Err(crate::ERR::ErrorInFunctionCall(
"range".to_string(),
"".to_string(),
crate::ERR::ErrorArithmetic(
"step value cannot be zero".to_string(),
Position::NONE,
)
.into(),
Position::NONE,
)
.into());
}
Ok(Self(from, to, step))
}
}
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)
}
}
}
impl FusedIterator for StepDecimalRange {}
}
macro_rules! reg_range { macro_rules! reg_range {
($lib:ident | $x:expr => $( $y:ty ),*) => { ($lib:ident | $x:expr => $( $y:ty ),*) => {
$( $(
@ -377,11 +250,13 @@ macro_rules! reg_range {
concat!("to: ", stringify!($y)), concat!("to: ", stringify!($y)),
concat!("Iterator<Item=", stringify!($y), ">"), concat!("Iterator<Item=", stringify!($y), ">"),
], [ ], [
"/// Return an iterator over the range of `from..to`.", "/// Return an iterator over the exclusive range of `from..to`.",
"/// The value `to` is never included.",
"///", "///",
"/// # Example", "/// # Example",
"///", "///",
"/// ```rhai", "/// ```rhai",
"/// // prints all values from 8 to 17",
"/// for n in range(8, 18) {", "/// for n in range(8, 18) {",
"/// print(n);", "/// print(n);",
"/// }", "/// }",
@ -392,9 +267,15 @@ macro_rules! reg_range {
)* )*
}; };
($lib:ident | step $x:expr => $( $y:ty ),*) => { ($lib:ident | step $x:expr => $( $y:ty ),*) => {
#[cfg(not(feature = "unchecked"))]
reg_range!($lib | step(std_add) $x => $( $y ),*);
#[cfg(feature = "unchecked")]
reg_range!($lib | step(regular_add) $x => $( $y ),*);
};
($lib:ident | step ( $add:ident ) $x:expr => $( $y:ty ),*) => {
$( $(
$lib.set_iterator::<StepRange<$y>>(); $lib.set_iterator::<StepRange<$y>>();
let _hash = $lib.set_native_fn($x, |from: $y, to: $y, step: $y| StepRange::new(from, to, step)); let _hash = $lib.set_native_fn($x, |from: $y, to: $y, step: $y| StepRange::new(from, to, step, $add));
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
$lib.update_fn_metadata_with_comments(_hash, [ $lib.update_fn_metadata_with_comments(_hash, [
@ -403,17 +284,22 @@ macro_rules! reg_range {
concat!("step: ", stringify!($y)), concat!("step: ", stringify!($y)),
concat!("Iterator<Item=", stringify!($y), ">") concat!("Iterator<Item=", stringify!($y), ">")
], [ ], [
"/// Return an iterator over the range of `from..to`, each iterator increasing by `step`.", "/// Return an iterator over the exclusive range of `from..to`, each iteration increasing by `step`.",
"/// The value `to` is never included.",
"///", "///",
"/// If `from` > `to` and `step` < 0, the iteration goes backwards.", "/// If `from` > `to` and `step` < 0, the iteration goes backwards.",
"///", "///",
"/// If `from` > `to` and `step` > 0 or `from` < `to` and `step` < 0, an empty iterator is returned.",
"///",
"/// # Example", "/// # Example",
"///", "///",
"/// ```rhai", "/// ```rhai",
"/// // prints all values from 8 to 17 in steps of 3",
"/// for n in range(8, 18, 3) {", "/// for n in range(8, 18, 3) {",
"/// print(n);", "/// print(n);",
"/// }", "/// }",
"///", "///",
"/// // prints all values down from 18 to 9 in steps of -3",
"/// for n in range(18, 8, -3) {", "/// for n in range(18, 8, -3) {",
"/// print(n);", "/// print(n);",
"/// }", "/// }",
@ -436,7 +322,6 @@ def_package! {
reg_range!(lib | "range" => i8, u8, i16, u16, i32, u32, i64, u64); reg_range!(lib | "range" => i8, u8, i16, u16, i32, u32, i64, u64);
#[cfg(not(target_family = "wasm"))] #[cfg(not(target_family = "wasm"))]
reg_range!(lib | "range" => i128, u128); reg_range!(lib | "range" => i128, u128);
} }
@ -448,63 +333,22 @@ def_package! {
reg_range!(lib | step "range" => i8, u8, i16, u16, i32, u32, i64, u64); reg_range!(lib | step "range" => i8, u8, i16, u16, i32, u32, i64, u64);
#[cfg(not(target_family = "wasm"))] #[cfg(not(target_family = "wasm"))]
reg_range!(lib | step "range" => i128, u128); reg_range!(lib | step "range" => i128, u128);
} }
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
{ reg_range!(lib | step(regular_add) "range" => crate::FLOAT);
lib.set_iterator::<float::StepFloatRange>();
let _hash = lib.set_native_fn("range", float::StepFloatRange::new);
#[cfg(feature = "metadata")]
lib.update_fn_metadata_with_comments(
_hash,
["from: FLOAT", "to: FLOAT", "step: FLOAT", "Iterator<Item=FLOAT>"],
[
"/// Return an iterator over the range of `from..to`, each iterator increasing by `step`.",
"///",
"/// If `from` > `to` and `step` < 0, the iteration goes backwards.",
"///",
"/// # Example",
"///",
"/// ```rhai",
"/// for n in range(8.0, 18.0, 3.0) {",
"/// print(n);",
"/// }",
"///",
"/// for n in range(18.0, 8.0, -3.0) {",
"/// print(n);",
"/// }",
"/// ```"
]
);
}
#[cfg(feature = "decimal")] #[cfg(feature = "decimal")]
{ reg_range!(lib | step "range" => rust_decimal::Decimal);
lib.set_iterator::<decimal::StepDecimalRange>();
let _hash = lib.set_native_fn("range", decimal::StepDecimalRange::new);
#[cfg(feature = "metadata")]
lib.update_fn_metadata_with_comments(
_hash,
["from: Decimal", "to: Decimal", "step: Decimal", "Iterator<Item=Decimal>"],
[
"/// Return an iterator over the range of `from..to`, each iterator increasing by `step`.",
"///",
"/// If `from` > `to` and `step` < 0, the iteration goes backwards.",
]
);
}
// Register string iterator // Register string iterator
lib.set_iterator::<CharsStream>(); lib.set_iterator::<CharsStream>();
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
let (range_type, range_inclusive_type) = ( let (range_type, range_inclusive_type) = (
format!("range: Range<{}>", std::any::type_name::<INT>()), format!("range: Range<{}>", type_name::<INT>()),
format!("range: RangeInclusive<{}>", std::any::type_name::<INT>()), format!("range: RangeInclusive<{}>", type_name::<INT>()),
); );
let _hash = lib.set_native_fn("chars", |string, range: ExclusiveRange| { let _hash = lib.set_native_fn("chars", |string, range: ExclusiveRange| {