forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequence.rs
More file actions
108 lines (98 loc) · 2.79 KB
/
sequence.rs
File metadata and controls
108 lines (98 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use crate::pyobject::{PyObjectRef, PyResult};
use crate::slots::PyComparisonOp;
use crate::vm::VirtualMachine;
use num_traits::cast::ToPrimitive;
pub(super) type DynPyIter<'a> = Box<dyn ExactSizeIterator<Item = &'a PyObjectRef> + 'a>;
#[allow(clippy::len_without_is_empty)]
pub(crate) trait SimpleSeq {
fn len(&self) -> usize;
fn boxed_iter(&self) -> DynPyIter;
}
impl<'a, D> SimpleSeq for D
where
D: 'a + std::ops::Deref<Target = [PyObjectRef]>,
{
fn len(&self) -> usize {
self.deref().len()
}
fn boxed_iter(&self) -> DynPyIter {
Box::new(self.deref().iter())
}
}
pub(crate) fn eq(vm: &VirtualMachine, zelf: DynPyIter, other: DynPyIter) -> PyResult<bool> {
if zelf.len() == other.len() {
for (a, b) in Iterator::zip(zelf, other) {
if !vm.identical_or_equal(a, b)? {
return Ok(false);
}
}
Ok(true)
} else {
Ok(false)
}
}
pub fn cmp(
vm: &VirtualMachine,
zelf: DynPyIter,
other: DynPyIter,
op: PyComparisonOp,
) -> PyResult<bool> {
let less = match op {
PyComparisonOp::Eq => return eq(vm, zelf, other),
PyComparisonOp::Ne => return eq(vm, zelf, other).map(|eq| !eq),
PyComparisonOp::Lt | PyComparisonOp::Le => true,
PyComparisonOp::Gt | PyComparisonOp::Ge => false,
};
let (lhs_len, rhs_len) = (zelf.len(), other.len());
for (a, b) in Iterator::zip(zelf, other) {
let ret = if less {
vm.bool_seq_lt(a, b)?
} else {
vm.bool_seq_gt(a, b)?
};
if let Some(v) = ret {
return Ok(v);
}
}
Ok(op.eval_ord(lhs_len.cmp(&rhs_len)))
}
pub(crate) struct SeqMul<'a> {
seq: &'a dyn SimpleSeq,
repetitions: usize,
iter: Option<DynPyIter<'a>>,
}
impl ExactSizeIterator for SeqMul<'_> {}
impl<'a> Iterator for SeqMul<'a> {
type Item = &'a PyObjectRef;
fn next(&mut self) -> Option<Self::Item> {
match self.iter.as_mut().and_then(Iterator::next) {
Some(item) => Some(item),
None => {
if self.repetitions == 0 {
None
} else {
self.repetitions -= 1;
self.iter = Some(self.seq.boxed_iter());
self.next()
}
}
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let size = self.iter.as_ref().map_or(0, ExactSizeIterator::len)
+ (self.repetitions * self.seq.len());
(size, Some(size))
}
}
pub(crate) fn seq_mul(seq: &impl SimpleSeq, repetitions: isize) -> SeqMul {
let repetitions = if seq.len() > 0 {
repetitions.to_usize().unwrap_or(0)
} else {
0
};
SeqMul {
seq,
repetitions,
iter: None,
}
}