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
use std::{collections::HashSet, marker::PhantomData};
use crate::*;
pub struct FilterRandomAccessible<F, T>
where
F: IsFilter<T>,
{
filter: F,
_marker: PhantomData<T>,
}
impl<F, T> FilterRandomAccessible<F, T>
where
F: IsFilter<T>,
{
pub fn new(filter: F) -> Self {
FilterRandomAccessible {
filter,
_marker: PhantomData,
}
}
}
impl<F, T, RA> IsFilterRandomAccessible<RA, T> for FilterRandomAccessible<F, T>
where
F: IsFilter<T>,
RA: IsRandomAccessible<T>,
{
fn filter(&self, ra: &RA, view: &mut View) {
if ra.len() == 0 {
*view = View::Full;
return;
}
match view {
View::Full => {
let mut indices = HashSet::new();
let n = ra.len();
for i in 0..n {
if self.filter.is_allowed(&ra[i]) {
indices.insert(i);
}
}
*view = View::Restricted(indices);
}
View::Restricted(indices) => {
let mut indices_to_remove = Vec::<usize>::new();
let max = ra.len() - 1;
for index in indices.iter() {
if *index > max {
indices_to_remove.push(*index);
continue;
}
if !self.filter.is_allowed(&ra[*index]) {
indices_to_remove.push(*index);
}
}
for index_to_remove in indices_to_remove {
indices.remove(&index_to_remove);
}
}
}
}
}