mirror of
https://github.com/nushell/nushell
synced 2025-01-15 14:44:14 +00:00
Implement RangeIterator::contains
This commit is contained in:
parent
7f06d6144f
commit
29cbcb8459
1 changed files with 31 additions and 13 deletions
|
@ -152,6 +152,36 @@ impl RangeIterator {
|
||||||
incr: range.incr,
|
incr: range.incr,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn contains(&self, x: &Value) -> bool {
|
||||||
|
let ordering_against_curr = compare_numbers(x, &self.curr);
|
||||||
|
let ordering_against_end = compare_numbers(x, &self.end);
|
||||||
|
|
||||||
|
match (ordering_against_curr, ordering_against_end) {
|
||||||
|
(Some(Ordering::Greater | Ordering::Equal), Some(Ordering::Less)) if self.moves_up => {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
(Some(Ordering::Less | Ordering::Equal), Some(Ordering::Greater)) if !self.moves_up => {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
(Some(_), Some(Ordering::Equal)) if self.is_end_inclusive => true,
|
||||||
|
(_, _) => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn compare_numbers(val: &Value, other: &Value) -> Option<Ordering> {
|
||||||
|
match (val, other) {
|
||||||
|
(Value::Int { val, .. }, Value::Int { val: other, .. }) => Some(val.cmp(other)),
|
||||||
|
(Value::Float { val, .. }, Value::Float { val: other, .. }) => compare_floats(*val, *other),
|
||||||
|
(Value::Float { val, .. }, Value::Int { val: other, .. }) => {
|
||||||
|
compare_floats(*val, *other as f64)
|
||||||
|
}
|
||||||
|
(Value::Int { val, .. }, Value::Float { val: other, .. }) => {
|
||||||
|
compare_floats(*val as f64, *other)
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compare two floating point numbers. The decision interval for equality is dynamically scaled
|
// Compare two floating point numbers. The decision interval for equality is dynamically scaled
|
||||||
|
@ -176,19 +206,7 @@ impl Iterator for RangeIterator {
|
||||||
let ordering = if matches!(self.end, Value::Nothing { .. }) {
|
let ordering = if matches!(self.end, Value::Nothing { .. }) {
|
||||||
Some(Ordering::Less)
|
Some(Ordering::Less)
|
||||||
} else {
|
} else {
|
||||||
match (&self.curr, &self.end) {
|
compare_numbers(&self.curr, &self.end)
|
||||||
(Value::Int { val: curr, .. }, Value::Int { val: end, .. }) => Some(curr.cmp(end)),
|
|
||||||
(Value::Float { val: curr, .. }, Value::Float { val: end, .. }) => {
|
|
||||||
compare_floats(*curr, *end)
|
|
||||||
}
|
|
||||||
(Value::Float { val: curr, .. }, Value::Int { val: end, .. }) => {
|
|
||||||
compare_floats(*curr, *end as f64)
|
|
||||||
}
|
|
||||||
(Value::Int { val: curr, .. }, Value::Float { val: end, .. }) => {
|
|
||||||
compare_floats(*curr as f64, *end)
|
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let ordering = if let Some(ord) = ordering {
|
let ordering = if let Some(ord) = ordering {
|
||||||
|
|
Loading…
Reference in a new issue