5
5
//! mostly want to avoid dropping or improving summary performance when data
6
6
//! points are missing as that's misleading and noisy, and this works well for
7
7
//! that.
8
+ //!
9
+ //! As an example:
10
+ //! Given a series with some missing data `[1, 2, ?, 4]`,
11
+ //! this iterator yields `[1, 2, 2, 4]`.
8
12
9
13
use crate :: db:: Point ;
10
14
15
+ /// Whether a point has been interpolated or not
11
16
#[ derive( Debug , Copy , Clone , PartialEq , Eq , Hash ) ]
12
- pub enum Interpolated {
17
+ pub enum IsInterpolated {
13
18
No ,
14
19
Yes ,
15
20
}
16
21
17
- impl Interpolated {
18
- pub fn is_interpolated ( self ) -> bool {
19
- self == Interpolated :: Yes
22
+ impl IsInterpolated {
23
+ pub fn as_bool ( self ) -> bool {
24
+ self == IsInterpolated :: Yes
20
25
}
21
26
}
22
27
23
- impl < P > Point for ( P , Interpolated )
28
+ impl < P > Point for ( P , IsInterpolated )
24
29
where
25
30
P : Point ,
26
31
{
@@ -39,21 +44,23 @@ where
39
44
self . 0 . set_value ( value)
40
45
}
41
46
fn interpolated ( & self ) -> bool {
42
- self . 1 . is_interpolated ( )
47
+ self . 1 . as_bool ( )
43
48
}
44
49
fn set_interpolated ( & mut self ) {
45
- self . 1 = Interpolated :: Yes ;
50
+ self . 1 = IsInterpolated :: Yes ;
46
51
}
47
52
}
48
53
54
+ /// The interpolated iterator
49
55
pub struct Interpolate < I >
50
56
where
51
57
I : Iterator ,
52
58
{
59
+ /// The base iterator we're interpolating
53
60
iterator : I ,
61
+ /// The last seen point which will be used for interpolation
54
62
last_seen : Option < f64 > ,
55
-
56
- // When we need to seek forward at the start, we store things in here.
63
+ /// When we need to seek forward at the start, we store things in here.
57
64
consumed : Vec < I :: Item > ,
58
65
}
59
66
@@ -76,15 +83,15 @@ where
76
83
I : Iterator ,
77
84
I :: Item : Point ,
78
85
{
79
- type Item = ( I :: Item , Interpolated ) ;
86
+ type Item = ( I :: Item , IsInterpolated ) ;
80
87
81
88
fn next ( & mut self ) -> Option < Self :: Item > {
82
89
if let Some ( mut item) = self . consumed . pop ( ) {
83
90
item. set_value ( self . last_seen . unwrap ( ) ) ;
84
91
let interpolation = if self . consumed . is_empty ( ) {
85
- Interpolated :: No
92
+ IsInterpolated :: No
86
93
} else {
87
- Interpolated :: Yes
94
+ IsInterpolated :: Yes
88
95
} ;
89
96
return Some ( ( item, interpolation) ) ;
90
97
}
@@ -94,12 +101,12 @@ where
94
101
match item. value ( ) {
95
102
Some ( pt) => {
96
103
self . last_seen = Some ( pt) ;
97
- return Some ( ( item, Interpolated :: No ) ) ;
104
+ return Some ( ( item, IsInterpolated :: No ) ) ;
98
105
}
99
106
None => {
100
107
if let Some ( last) = self . last_seen {
101
108
item. set_value ( last) ;
102
- return Some ( ( item, Interpolated :: Yes ) ) ;
109
+ return Some ( ( item, IsInterpolated :: Yes ) ) ;
103
110
}
104
111
105
112
self . consumed . push ( item) ;
@@ -122,7 +129,7 @@ where
122
129
123
130
let mut item = self . consumed . pop ( ) . unwrap ( ) ;
124
131
item. set_value ( self . last_seen . unwrap ( ) ) ;
125
- return Some ( ( item, Interpolated :: Yes ) ) ;
132
+ return Some ( ( item, IsInterpolated :: Yes ) ) ;
126
133
}
127
134
}
128
135
}
@@ -139,15 +146,15 @@ where
139
146
140
147
#[ cfg( test) ]
141
148
mod tests {
142
- use super :: { Interpolate , Interpolated } ;
149
+ use super :: { Interpolate , IsInterpolated } ;
143
150
144
151
#[ test]
145
152
fn test_no_interpolation ( ) {
146
153
let v = vec ! [ ( "a" , 1.0 ) , ( "b" , 2.0 ) ] ;
147
154
let mut iter = Interpolate :: new ( v. into_iter ( ) ) ;
148
155
149
- assert_eq ! ( iter. next( ) . unwrap( ) , ( ( "a" , 1.0 ) , Interpolated :: No ) ) ;
150
- assert_eq ! ( iter. next( ) . unwrap( ) , ( ( "b" , 2.0 ) , Interpolated :: No ) ) ;
156
+ assert_eq ! ( iter. next( ) . unwrap( ) , ( ( "a" , 1.0 ) , IsInterpolated :: No ) ) ;
157
+ assert_eq ! ( iter. next( ) . unwrap( ) , ( ( "b" , 2.0 ) , IsInterpolated :: No ) ) ;
151
158
assert ! ( iter. next( ) . is_none( ) ) ;
152
159
}
153
160
@@ -156,10 +163,16 @@ mod tests {
156
163
let v = vec ! [ ( "a" , None ) , ( "b" , None ) , ( "c" , Some ( 3.0 ) ) , ( "d" , Some ( 4.0 ) ) ] ;
157
164
let mut iter = Interpolate :: new ( v. into_iter ( ) ) ;
158
165
159
- assert_eq ! ( iter. next( ) . unwrap( ) , ( ( "a" , Some ( 3.0 ) ) , Interpolated :: Yes ) ) ;
160
- assert_eq ! ( iter. next( ) . unwrap( ) , ( ( "b" , Some ( 3.0 ) ) , Interpolated :: Yes ) ) ;
161
- assert_eq ! ( iter. next( ) . unwrap( ) , ( ( "c" , Some ( 3.0 ) ) , Interpolated :: No ) ) ;
162
- assert_eq ! ( iter. next( ) . unwrap( ) , ( ( "d" , Some ( 4.0 ) ) , Interpolated :: No ) ) ;
166
+ assert_eq ! (
167
+ iter. next( ) . unwrap( ) ,
168
+ ( ( "a" , Some ( 3.0 ) ) , IsInterpolated :: Yes )
169
+ ) ;
170
+ assert_eq ! (
171
+ iter. next( ) . unwrap( ) ,
172
+ ( ( "b" , Some ( 3.0 ) ) , IsInterpolated :: Yes )
173
+ ) ;
174
+ assert_eq ! ( iter. next( ) . unwrap( ) , ( ( "c" , Some ( 3.0 ) ) , IsInterpolated :: No ) ) ;
175
+ assert_eq ! ( iter. next( ) . unwrap( ) , ( ( "d" , Some ( 4.0 ) ) , IsInterpolated :: No ) ) ;
163
176
assert ! ( iter. next( ) . is_none( ) ) ;
164
177
}
165
178
@@ -175,12 +188,18 @@ mod tests {
175
188
] ;
176
189
let mut iter = Interpolate :: new ( v. into_iter ( ) ) ;
177
190
178
- assert_eq ! ( iter. next( ) . unwrap( ) , ( ( "a" , Some ( 1.0 ) ) , Interpolated :: No ) ) ;
179
- assert_eq ! ( iter. next( ) . unwrap( ) , ( ( "b" , Some ( 2.0 ) ) , Interpolated :: No ) ) ;
180
- assert_eq ! ( iter. next( ) . unwrap( ) , ( ( "c" , Some ( 2.0 ) ) , Interpolated :: Yes ) ) ;
181
- assert_eq ! ( iter. next( ) . unwrap( ) , ( ( "d" , Some ( 2.0 ) ) , Interpolated :: Yes ) ) ;
182
- assert_eq ! ( iter. next( ) . unwrap( ) , ( ( "e" , Some ( 5.0 ) ) , Interpolated :: No ) ) ;
183
- assert_eq ! ( iter. next( ) . unwrap( ) , ( ( "f" , Some ( 6.0 ) ) , Interpolated :: No ) ) ;
191
+ assert_eq ! ( iter. next( ) . unwrap( ) , ( ( "a" , Some ( 1.0 ) ) , IsInterpolated :: No ) ) ;
192
+ assert_eq ! ( iter. next( ) . unwrap( ) , ( ( "b" , Some ( 2.0 ) ) , IsInterpolated :: No ) ) ;
193
+ assert_eq ! (
194
+ iter. next( ) . unwrap( ) ,
195
+ ( ( "c" , Some ( 2.0 ) ) , IsInterpolated :: Yes )
196
+ ) ;
197
+ assert_eq ! (
198
+ iter. next( ) . unwrap( ) ,
199
+ ( ( "d" , Some ( 2.0 ) ) , IsInterpolated :: Yes )
200
+ ) ;
201
+ assert_eq ! ( iter. next( ) . unwrap( ) , ( ( "e" , Some ( 5.0 ) ) , IsInterpolated :: No ) ) ;
202
+ assert_eq ! ( iter. next( ) . unwrap( ) , ( ( "f" , Some ( 6.0 ) ) , IsInterpolated :: No ) ) ;
184
203
assert ! ( iter. next( ) . is_none( ) ) ;
185
204
}
186
205
@@ -189,10 +208,16 @@ mod tests {
189
208
let v = vec ! [ ( "a" , Some ( 1.0 ) ) , ( "b" , Some ( 2.0 ) ) , ( "c" , None ) , ( "d" , None ) ] ;
190
209
let mut iter = Interpolate :: new ( v. into_iter ( ) ) ;
191
210
192
- assert_eq ! ( iter. next( ) . unwrap( ) , ( ( "a" , Some ( 1.0 ) ) , Interpolated :: No ) ) ;
193
- assert_eq ! ( iter. next( ) . unwrap( ) , ( ( "b" , Some ( 2.0 ) ) , Interpolated :: No ) ) ;
194
- assert_eq ! ( iter. next( ) . unwrap( ) , ( ( "c" , Some ( 2.0 ) ) , Interpolated :: Yes ) ) ;
195
- assert_eq ! ( iter. next( ) . unwrap( ) , ( ( "d" , Some ( 2.0 ) ) , Interpolated :: Yes ) ) ;
211
+ assert_eq ! ( iter. next( ) . unwrap( ) , ( ( "a" , Some ( 1.0 ) ) , IsInterpolated :: No ) ) ;
212
+ assert_eq ! ( iter. next( ) . unwrap( ) , ( ( "b" , Some ( 2.0 ) ) , IsInterpolated :: No ) ) ;
213
+ assert_eq ! (
214
+ iter. next( ) . unwrap( ) ,
215
+ ( ( "c" , Some ( 2.0 ) ) , IsInterpolated :: Yes )
216
+ ) ;
217
+ assert_eq ! (
218
+ iter. next( ) . unwrap( ) ,
219
+ ( ( "d" , Some ( 2.0 ) ) , IsInterpolated :: Yes )
220
+ ) ;
196
221
assert ! ( iter. next( ) . is_none( ) ) ;
197
222
}
198
223
}
0 commit comments