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
use core::{mem, usize};
use {Bits, Ieee754};
pub struct Iter<T: Ieee754> {
from: T,
to: T,
done: bool
}
pub fn new_iter<T: Ieee754>(from: T, to: T) -> Iter<T> {
Iter { from, to, done: false }
}
impl<T: Ieee754> Iterator for Iter<T> {
type Item = T;
fn next(&mut self) -> Option<T> {
if self.done { return None }
let x = self.from;
let y = x.next();
if x.bits() == self.to.bits() {
self.done = true;
}
self.from = y;
return Some(x)
}
fn size_hint(&self) -> (usize, Option<usize>) {
if self.done {
return (0, Some(0))
}
let high_pos = 8 * mem::size_of::<T>() - 1;
let high_mask = 1 << high_pos;
let from_ = self.from.bits().as_u64();
let (from, from_sign) = (from_ & !high_mask,
from_ & high_mask != 0);
let to_ = self.to.bits().as_u64();
let (to, to_sign) = (to_ & !high_mask,
to_ & high_mask != 0);
let from = if from_sign { -(from as i64) } else { from as i64 };
let to = if to_sign { -(to as i64) } else { to as i64 };
let distance = (to - from + 1) as u64;
if distance <= usize::MAX as u64 {
let d = distance as usize;
(d, Some(d))
} else {
(usize::MAX, None)
}
}
}
impl<T: Ieee754> DoubleEndedIterator for Iter<T> {
fn next_back(&mut self) -> Option<T> {
if self.done { return None }
let x = self.to;
let y = x.prev();
if x == self.from {
self.done = true;
}
self.to = y;
return Some(x)
}
}