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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use std::ops::{
RangeFull,
Range,
RangeTo,
RangeFrom
};
pub trait AppendTuple<X> {
type Result;
fn append(self, x: X) -> Self::Result;
}
macro_rules! impl_append_tuple(
() => (
impl<T> AppendTuple<T> for () {
type Result = (T, );
fn append(self, x: T) -> (T, ) {
(x, )
}
}
);
($A:ident, $($B:ident,)*) => (
impl_append_tuple!($($B,)*);
#[allow(non_snake_case)]
impl<$A, $($B,)* T> AppendTuple<T> for ($A, $($B),*) {
type Result = ($A, $($B, )* T);
fn append(self, x: T) -> ($A, $($B,)* T) {
let ($A, $($B),*) = self;
($A, $($B,)* x)
}
}
);
);
impl_append_tuple!(A, B, C, D, E, F, G, H, I, J, K, L,);
#[derive(Clone)]
pub struct FlatTuples<I> {
iter: I,
}
impl<I> FlatTuples<I>
{
pub fn new(iter: I) -> Self
{
FlatTuples{iter: iter}
}
}
impl<X, T, I> Iterator for FlatTuples<I> where
I: Iterator<Item=(T, X)>,
T: AppendTuple<X>,
{
type Item = T::Result;
#[inline]
fn next(&mut self) -> Option<<Self as Iterator>::Item>
{
self.iter.next().map(|(t, x)| t.append(x))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl<X, T, I> DoubleEndedIterator for FlatTuples<I> where
I: DoubleEndedIterator<Item=(T, X)>,
T: AppendTuple<X>,
{
#[inline]
fn next_back(&mut self) -> Option<<Self as Iterator>::Item>
{
self.iter.next_back().map(|(t, x)| t.append(x))
}
}
pub trait GenericRange {
fn start(&self) -> Option<usize> { None }
fn end(&self) -> Option<usize> { None }
}
impl GenericRange for RangeFull {}
impl GenericRange for RangeFrom<usize> {
fn start(&self) -> Option<usize> { Some(self.start) }
}
impl GenericRange for RangeTo<usize> {
fn end(&self) -> Option<usize> { Some(self.end) }
}
impl GenericRange for Range<usize> {
fn start(&self) -> Option<usize> { Some(self.start) }
fn end(&self) -> Option<usize> { Some(self.end) }
}
pub trait ToFloat<F> : Copy {
fn to_float(self) -> F;
}
impl ToFloat<f32> for usize {
fn to_float(self) -> f32 { self as f32 }
}
impl ToFloat<f64> for usize {
fn to_float(self) -> f64 { self as f64 }
}