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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use masked::MaybeNa;
use apply::{ElemFn, Selector, FieldFn, ApplyToElem, DataIndex, ApplyToField};
use error::Result;

/// `ElemFn` function for matching unsigned integer values.
pub struct MatchesFnUnsigned<'a> {
    value: MaybeNa<&'a u64>
}
impl<'a> ElemFn for MatchesFnUnsigned<'a> {
    type Output = bool;
    fn apply_unsigned(&mut self, value: MaybeNa<&u64>) -> bool { self.value == value }
    fn apply_signed(&mut self, _: MaybeNa<&i64>) -> bool { false }
    fn apply_text(&mut self, _: MaybeNa<&String>) -> bool { false }
    fn apply_boolean(&mut self, _: MaybeNa<&bool>) -> bool { false }
    fn apply_float(&mut self, _: MaybeNa<&f64>) -> bool { false }
}

/// `ElemFn` function for matching signed integer values.
pub struct MatchesFnSigned<'a> {
    value: MaybeNa<&'a i64>
}
impl<'a> ElemFn for MatchesFnSigned<'a> {
    type Output = bool;
    fn apply_unsigned(&mut self, _: MaybeNa<&u64>) -> bool { false }
    fn apply_signed(&mut self, value: MaybeNa<&i64>) -> bool { self.value == value }
    fn apply_text(&mut self, _: MaybeNa<&String>) -> bool { false }
    fn apply_boolean(&mut self, _: MaybeNa<&bool>) -> bool { false }
    fn apply_float(&mut self, _: MaybeNa<&f64>) -> bool { false }
}

/// `ElemFn` function for matching text values.
pub struct MatchesFnText<'a> {
    value: MaybeNa<&'a String>
}
impl<'a> ElemFn for MatchesFnText<'a> {
    type Output = bool;
    fn apply_unsigned(&mut self, _: MaybeNa<&u64>) -> bool { false }
    fn apply_signed(&mut self, _: MaybeNa<&i64>) -> bool { false }
    fn apply_text(&mut self, value: MaybeNa<&String>) -> bool { self.value == value }
    fn apply_boolean(&mut self, _: MaybeNa<&bool>) -> bool { false }
    fn apply_float(&mut self, _: MaybeNa<&f64>) -> bool { false }
}

/// `ElemFn` function for matching boolean values.
pub struct MatchesFnBoolean<'a> {
    value: MaybeNa<&'a bool>
}
impl<'a> ElemFn for MatchesFnBoolean<'a> {
    type Output = bool;
    fn apply_unsigned(&mut self, _: MaybeNa<&u64>) -> bool { false }
    fn apply_signed(&mut self, _: MaybeNa<&i64>) -> bool { false }
    fn apply_text(&mut self, _: MaybeNa<&String>) -> bool { false }
    fn apply_boolean(&mut self, value: MaybeNa<&bool>) -> bool { self.value == value }
    fn apply_float(&mut self, _: MaybeNa<&f64>) -> bool { false }
}

/// `ElemFn` function for matching floating-point values.
pub struct MatchesFnFloat<'a> {
    value: MaybeNa<&'a f64>
}
impl<'a> ElemFn for MatchesFnFloat<'a> {
    type Output = bool;
    fn apply_unsigned(&mut self, _: MaybeNa<&u64>) -> bool { false }
    fn apply_signed(&mut self, _: MaybeNa<&i64>) -> bool { false }
    fn apply_text(&mut self, _: MaybeNa<&String>) -> bool { false }
    fn apply_boolean(&mut self, _: MaybeNa<&bool>) -> bool { false }
    fn apply_float(&mut self, value: MaybeNa<&f64>) -> bool { self.value == value }
}

/// Helper trait / implementations for matching a value. Returns `true` if the selected element
/// matches the provided target value.
pub trait Matches<Selector, T> {
    /// Returns `true` if the element specified with the `Selector` matches the provided target
    /// value.
    fn matches(&self, select: Selector, target: T) -> Result<bool>;
}
impl<S: Selector, T> Matches<S, u64> for T where T: ApplyToElem<S> {
    fn matches(&self, select: S, target: u64) -> Result<bool> {
        self.apply_to_elem(
            MatchesFnUnsigned { value: MaybeNa::Exists(&target) },
            select
        )
    }
}
impl<S: Selector, T> Matches<S, i64> for T where T: ApplyToElem<S> {
    fn matches(&self, select: S, target: i64) -> Result<bool> {
        self.apply_to_elem(
            MatchesFnSigned { value: MaybeNa::Exists(&target) },
            select
        )
    }
}
impl<S: Selector, T> Matches<S, String> for T where T: ApplyToElem<S> {
    fn matches(&self, select: S, target: String) -> Result<bool> {
        self.apply_to_elem(
            MatchesFnText { value: MaybeNa::Exists(&target) },
            select
        )
    }
}
impl<S: Selector, T> Matches<S, bool> for T where T: ApplyToElem<S> {
    fn matches(&self, select: S, target: bool) -> Result<bool> {
        self.apply_to_elem(
            MatchesFnBoolean { value: MaybeNa::Exists(&target) },
            select
        )
    }
}
impl<S: Selector, T> Matches<S, f64> for T where T: ApplyToElem<S> {
    fn matches(&self, select: S, target: f64) -> Result<bool> {
        self.apply_to_elem(
            MatchesFnFloat { value: MaybeNa::Exists(&target) },
            select
        )
    }
}

/// `FieldFn` function for finding an index set of unsigned integer values of a field that match
/// a predicate.
pub struct FilterFnUnsigned<F: Fn(&u64) -> bool> {
    f: F,
}
impl<F: Fn(&u64) -> bool> FieldFn for FilterFnUnsigned<F> {
    type Output = Vec<usize>;
    fn apply_unsigned<T: DataIndex<u64>>(&mut self, field: &T) -> Vec<usize> {
        (0..field.len()).filter(|&idx| {
            match field.get_data(idx).unwrap() {
                MaybeNa::Exists(&ref val) => (self.f)(val),
                MaybeNa::Na => false,
            }
        }).collect()
    }
    fn apply_signed<T: DataIndex<i64>>(&mut self, _: &T) -> Vec<usize> { vec![] }
    fn apply_text<T: DataIndex<String>>(&mut self, _: &T) -> Vec<usize> { vec![] }
    fn apply_boolean<T: DataIndex<bool>>(&mut self, _: &T) -> Vec<usize> { vec![] }
    fn apply_float<T: DataIndex<f64>>(&mut self, _: &T) -> Vec<usize> { vec![] }
}
/// `FieldFn` function for finding an index set of signed integer values of a field that match
/// a predicate.
pub struct FilterFnSigned<F: Fn(&i64) -> bool> {
    f: F,
}
impl<F: Fn(&i64) -> bool> FieldFn for FilterFnSigned<F> {
    type Output = Vec<usize>;
    fn apply_unsigned<T: DataIndex<u64>>(&mut self, _: &T) -> Vec<usize> { vec![] }
    fn apply_signed<T: DataIndex<i64>>(&mut self, field: &T) -> Vec<usize> {
        (0..field.len()).filter(|&idx| {
            match field.get_data(idx).unwrap() {
                MaybeNa::Exists(&ref val) => (self.f)(val),
                MaybeNa::Na => false,
            }
        }).collect()
    }
    fn apply_text<T: DataIndex<String>>(&mut self, _: &T) -> Vec<usize> { vec![] }
    fn apply_boolean<T: DataIndex<bool>>(&mut self, _: &T) -> Vec<usize> { vec![] }
    fn apply_float<T: DataIndex<f64>>(&mut self, _: &T) -> Vec<usize> { vec![] }
}
/// `FieldFn` function for finding an index set of text values of a field that match
/// a predicate.
pub struct FilterFnText<F: Fn(&String) -> bool> {
    f: F,
}
impl<F: Fn(&String) -> bool> FieldFn for FilterFnText<F> {
    type Output = Vec<usize>;
    fn apply_unsigned<T: DataIndex<u64>>(&mut self, _: &T) -> Vec<usize> { vec![] }
    fn apply_signed<T: DataIndex<i64>>(&mut self, _: &T) -> Vec<usize> { vec![] }
    fn apply_text<T: DataIndex<String>>(&mut self, field: &T) -> Vec<usize> {
        (0..field.len()).filter(|&idx| {
            match field.get_data(idx).unwrap() {
                MaybeNa::Exists(&ref val) => (self.f)(val),
                MaybeNa::Na => false,
            }
        }).collect()
    }
    fn apply_boolean<T: DataIndex<bool>>(&mut self, _: &T) -> Vec<usize> { vec![] }
    fn apply_float<T: DataIndex<f64>>(&mut self, _: &T) -> Vec<usize> { vec![] }
}
/// `FieldFn` function for finding an index set of boolean values of a field that match
/// a predicate.
pub struct FilterFnBoolean<F: Fn(&bool) -> bool> {
    f: F,
}
impl<F: Fn(&bool) -> bool> FieldFn for FilterFnBoolean<F> {
    type Output = Vec<usize>;
    fn apply_unsigned<T: DataIndex<u64>>(&mut self, _: &T) -> Vec<usize> { vec![] }
    fn apply_signed<T: DataIndex<i64>>(&mut self, _: &T) -> Vec<usize> { vec![] }
    fn apply_text<T: DataIndex<String>>(&mut self, _: &T) -> Vec<usize> { vec![] }
    fn apply_boolean<T: DataIndex<bool>>(&mut self, field: &T) -> Vec<usize> {
        (0..field.len()).filter(|&idx| {
            match field.get_data(idx).unwrap() {
                MaybeNa::Exists(&ref val) => (self.f)(val),
                MaybeNa::Na => false,
            }
        }).collect()
    }
    fn apply_float<T: DataIndex<f64>>(&mut self, _: &T) -> Vec<usize> { vec![] }
}
/// `FieldFn` function for finding an index set of floating-point values of a field that match
/// a predicate.
pub struct FilterFnFloat<F: Fn(&f64) -> bool> {
    f: F,
}
impl<F: Fn(&f64) -> bool> FieldFn for FilterFnFloat<F> {
    type Output = Vec<usize>;
    fn apply_unsigned<T: DataIndex<u64>>(&mut self, _: &T) -> Vec<usize> { vec![] }
    fn apply_signed<T: DataIndex<i64>>(&mut self, _: &T) -> Vec<usize> { vec![] }
    fn apply_text<T: DataIndex<String>>(&mut self, _: &T) -> Vec<usize> { vec![] }
    fn apply_boolean<T: DataIndex<bool>>(&mut self, _: &T) -> Vec<usize> { vec![] }
    fn apply_float<T: DataIndex<f64>>(&mut self, field: &T) -> Vec<usize> {
        (0..field.len()).filter(|&idx| {
            match field.get_data(idx).unwrap() {
                MaybeNa::Exists(&ref val) => (self.f)(val),
                MaybeNa::Na => false,
            }
        }).collect()
    }
}

/// Helper trait / implementations for finding an index set of values in a field that match a
/// predicate. Returns a vector of indices of all elements in the field that pass the predicate.
pub trait GetFilter<S: Selector, T> {
    /// Returns vector of indices of all elements in the field specified with the `Selector` that
    /// pass the predicate.
    fn get_filter<F: Fn(&T) -> bool>(&self, select: S, pred: F) -> Result<Vec<usize>>;
}
impl<S: Selector, T> GetFilter<S, u64> for T where T: ApplyToField<S> {
    fn get_filter<F: Fn(&u64) -> bool>(&self, select: S, pred: F) -> Result<Vec<usize>> {
        self.apply_to_field(
            FilterFnUnsigned { f: pred },
            select
        )
    }
}
impl<S: Selector, T> GetFilter<S, i64> for T where T: ApplyToField<S> {
    fn get_filter<F: Fn(&i64) -> bool>(&self, select: S, pred: F) -> Result<Vec<usize>> {
        self.apply_to_field(
            FilterFnSigned { f: pred },
            select
        )
    }
}
impl<S: Selector, T> GetFilter<S, String> for T where T: ApplyToField<S> {
    fn get_filter<F: Fn(&String) -> bool>(&self, select: S, pred: F) -> Result<Vec<usize>> {
        self.apply_to_field(
            FilterFnText { f: pred },
            select
        )
    }
}
impl<S: Selector, T> GetFilter<S, bool> for T where T: ApplyToField<S> {
    fn get_filter<F: Fn(&bool) -> bool>(&self, select: S, pred: F) -> Result<Vec<usize>> {
        self.apply_to_field(
            FilterFnBoolean { f: pred },
            select
        )
    }
}
impl<S: Selector, T> GetFilter<S, f64> for T where T: ApplyToField<S> {
    fn get_filter<F: Fn(&f64) -> bool>(&self, select: S, pred: F) -> Result<Vec<usize>> {
        self.apply_to_field(
            FilterFnFloat { f: pred },
            select
        )
    }
}


/// `FieldFn` function for matching all unsigned integer values of a field against a predicate.
pub struct MatchesAllFnUnsigned<F: Fn(&u64) -> bool> {
    f: F,
}
impl<F: Fn(&u64) -> bool> FieldFn for MatchesAllFnUnsigned<F> {
    type Output = bool;
    fn apply_unsigned<T: DataIndex<u64>>(&mut self, field: &T) -> bool {
        (0..field.len()).all(|idx| match field.get_data(idx).unwrap() {
            MaybeNa::Exists(&ref val) => (self.f)(val),
            MaybeNa::Na => false
        })
    }
    fn apply_signed<T: DataIndex<i64>>(&mut self, _: &T) -> bool { false }
    fn apply_text<T: DataIndex<String>>(&mut self, _: &T) -> bool { false }
    fn apply_boolean<T: DataIndex<bool>>(&mut self, _: &T) -> bool { false }
    fn apply_float<T: DataIndex<f64>>(&mut self, _: &T) -> bool { false }
}

/// `FieldFn` function for matching all signed integer values of a field against a predicate.
pub struct MatchesAllFnSigned<F: Fn(&i64) -> bool> {
    f: F,
}
impl<F: Fn(&i64) -> bool> FieldFn for MatchesAllFnSigned<F> {
    type Output = bool;
    fn apply_unsigned<T: DataIndex<u64>>(&mut self, _: &T) -> bool { false }
    fn apply_signed<T: DataIndex<i64>>(&mut self, field: &T) -> bool {
        (0..field.len()).all(|idx| match field.get_data(idx).unwrap() {
            MaybeNa::Exists(&ref val) => (self.f)(val),
            MaybeNa::Na => false
        })
    }
    fn apply_text<T: DataIndex<String>>(&mut self, _: &T) -> bool { false }
    fn apply_boolean<T: DataIndex<bool>>(&mut self, _: &T) -> bool { false }
    fn apply_float<T: DataIndex<f64>>(&mut self, _: &T) -> bool { false }
}

/// `FieldFn` function for matching all text values of a field against a predicate.
pub struct MatchesAllFnText<F: Fn(&String) -> bool> {
    f: F,
}
impl<F: Fn(&String) -> bool> FieldFn for MatchesAllFnText<F> {
    type Output = bool;
    fn apply_unsigned<T: DataIndex<u64>>(&mut self, _: &T) -> bool { false }
    fn apply_signed<T: DataIndex<i64>>(&mut self, _: &T) -> bool { false }
    fn apply_text<T: DataIndex<String>>(&mut self, field: &T) -> bool {
        (0..field.len()).all(|idx| match field.get_data(idx).unwrap() {
            MaybeNa::Exists(&ref val) => (self.f)(val),
            MaybeNa::Na => false
        })
    }
    fn apply_boolean<T: DataIndex<bool>>(&mut self, _: &T) -> bool { false }
    fn apply_float<T: DataIndex<f64>>(&mut self, _: &T) -> bool { false }
}

/// `FieldFn` function for matching all boolean values of a field against a predicate.
pub struct MatchesAllFnBoolean<F: Fn(&bool) -> bool> {
    f: F,
}
impl<F: Fn(&bool) -> bool> FieldFn for MatchesAllFnBoolean<F> {
    type Output = bool;
    fn apply_unsigned<T: DataIndex<u64>>(&mut self, _: &T) -> bool { false }
    fn apply_signed<T: DataIndex<i64>>(&mut self, _: &T) -> bool { false }
    fn apply_text<T: DataIndex<String>>(&mut self, _: &T) -> bool { false }
    fn apply_boolean<T: DataIndex<bool>>(&mut self, field: &T) -> bool {
        (0..field.len()).all(|idx| match field.get_data(idx).unwrap() {
            MaybeNa::Exists(&ref val) => (self.f)(val),
            MaybeNa::Na => false
        })
    }
    fn apply_float<T: DataIndex<f64>>(&mut self, _: &T) -> bool { false }
}

/// `FieldFn` function for matching all floating-point values of a field against a predicate.
pub struct MatchesAllFnFloat<F: Fn(&f64) -> bool> {
    f: F,
}
impl<F: Fn(&f64) -> bool> FieldFn for MatchesAllFnFloat<F> {
    type Output = bool;
    fn apply_unsigned<T: DataIndex<u64>>(&mut self, _: &T) -> bool { false }
    fn apply_signed<T: DataIndex<i64>>(&mut self, _: &T) -> bool { false }
    fn apply_text<T: DataIndex<String>>(&mut self, _: &T) -> bool { false }
    fn apply_boolean<T: DataIndex<bool>>(&mut self, _: &T) -> bool { false }
    fn apply_float<T: DataIndex<f64>>(&mut self, field: &T) -> bool {
        (0..field.len()).all(|idx| match field.get_data(idx).unwrap() {
            MaybeNa::Exists(&ref val) => (self.f)(val),
            MaybeNa::Na => false
        })
    }
}

/// Helper trait / implementations for matching a predicate to a field. Returns `true` if the
/// provided predicate returns true for all elements in the field.
pub trait MatchesAll<S: Selector, T> {
    /// Returns `true` if the all elements in the field specified with the `Selector` pass the
    /// predicate.
    fn matches_all<F: Fn(&T) -> bool>(&self, select: S, pred: F) -> Result<bool>;
}
impl<S: Selector, T> MatchesAll<S, u64> for T where T: ApplyToField<S> {
    fn matches_all<F: Fn(&u64) -> bool>(&self, select: S, pred: F) -> Result<bool> {
        self.apply_to_field(
            MatchesAllFnUnsigned { f: pred },
            select
        )
    }
}
impl<S: Selector, T> MatchesAll<S, i64> for T where T: ApplyToField<S> {
    fn matches_all<F: Fn(&i64) -> bool>(&self, select: S, pred: F) -> Result<bool> {
        self.apply_to_field(
            MatchesAllFnSigned { f: pred },
            select
        )
    }
}
impl<S: Selector, T> MatchesAll<S, String> for T where T: ApplyToField<S> {
    fn matches_all<F: Fn(&String) -> bool>(&self, select: S, pred: F) -> Result<bool> {
        self.apply_to_field(
            MatchesAllFnText { f: pred },
            select
        )
    }
}
impl<S: Selector, T> MatchesAll<S, bool> for T where T: ApplyToField<S> {
    fn matches_all<F: Fn(&bool) -> bool>(&self, select: S, pred: F) -> Result<bool> {
        self.apply_to_field(
            MatchesAllFnBoolean { f: pred },
            select
        )
    }
}
impl<S: Selector, T> MatchesAll<S, f64> for T where T: ApplyToField<S> {
    fn matches_all<F: Fn(&f64) -> bool>(&self, select: S, pred: F) -> Result<bool> {
        self.apply_to_field(
            MatchesAllFnFloat { f: pred },
            select
        )
    }
}