rust_3d/
filter_random_accessible.rs

1/*
2Copyright 2016 Martin Buck
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"),
6to deal in the Software without restriction, including without limitation the
7rights to use, copy, modify, merge, publish, distribute, sublicense,
8and/or sell copies of the Software, and to permit persons to whom the Software
9is furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall
12be included all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
20OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21*/
22
23//! FilterRandomAccessible, a filter which can transform any IsFilter into an IsFilterRandomAccessible
24
25use std::{collections::HashSet, marker::PhantomData};
26
27use crate::*;
28
29//------------------------------------------------------------------------------
30
31/// FilterRandomAccessible, a filter which can transform any IsFilter into an IsFilterRandomAccessible
32pub struct FilterRandomAccessible<F, T>
33where
34    F: IsFilter<T>,
35{
36    filter: F,
37    _marker: PhantomData<T>,
38}
39
40impl<F, T> FilterRandomAccessible<F, T>
41where
42    F: IsFilter<T>,
43{
44    pub fn new(filter: F) -> Self {
45        FilterRandomAccessible {
46            filter,
47            _marker: PhantomData,
48        }
49    }
50}
51
52impl<F, T, RA> IsFilterRandomAccessible<RA, T> for FilterRandomAccessible<F, T>
53where
54    F: IsFilter<T>,
55    RA: IsRandomAccessible<T>,
56{
57    fn filter(&self, ra: &RA, view: &mut View) {
58        if ra.len() == 0 {
59            *view = View::Full;
60            return;
61        }
62        match view {
63            View::Full => {
64                let mut indices = HashSet::new();
65                let n = ra.len();
66                for i in 0..n {
67                    if self.filter.is_allowed(&ra[i]) {
68                        indices.insert(i);
69                    }
70                }
71                *view = View::Restricted(indices);
72            }
73            View::Restricted(indices) => {
74                let mut indices_to_remove = Vec::<usize>::new();
75                let max = ra.len() - 1;
76                for index in indices.iter() {
77                    if *index > max {
78                        indices_to_remove.push(*index);
79                        continue;
80                    }
81                    if !self.filter.is_allowed(&ra[*index]) {
82                        indices_to_remove.push(*index);
83                    }
84                }
85
86                for index_to_remove in indices_to_remove {
87                    indices.remove(&index_to_remove);
88                }
89            }
90        }
91    }
92}