zenoh_keyexpr/keyexpr_tree/impls/
mod.rs

1//
2// Copyright (c) 2023 ZettaScale Technology
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7// which is available at https://www.apache.org/licenses/LICENSE-2.0.
8//
9// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10//
11// Contributors:
12//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
13//
14
15pub use hashmap_impl::HashMapProvider;
16pub use keyed_set_impl::KeyedSetProvider;
17pub use vec_set_impl::VecSetProvider;
18
19use crate::keyexpr;
20mod hashmap_impl;
21mod keyed_set_impl;
22mod vec_set_impl;
23
24/// The advised way of storing children in KeTrees, based on benchmarks.
25pub type DefaultChildrenProvider = KeyedSetProvider;
26pub struct FilterMap<I, F> {
27    iter: I,
28    filter: F,
29}
30
31impl<I, F> FilterMap<I, F> {
32    fn new(iter: I, filter: F) -> Self {
33        Self { iter, filter }
34    }
35}
36pub trait IFilter<I> {
37    type O;
38    fn filter_map(&self, i: I) -> Option<Self::O>;
39}
40impl<I: Iterator, F: IFilter<<I as Iterator>::Item>> Iterator for FilterMap<I, F> {
41    type Item = F::O;
42
43    fn next(&mut self) -> Option<Self::Item> {
44        for next in self.iter.by_ref() {
45            if let Some(output) = self.filter.filter_map(next) {
46                return Some(output);
47            }
48        }
49        None
50    }
51}
52pub struct Intersection<'a>(pub &'a keyexpr);
53impl<K: core::ops::Deref<Target = keyexpr>, V> IFilter<(&K, V)> for Intersection<'_> {
54    type O = V;
55    fn filter_map(&self, (k, v): (&K, V)) -> Option<Self::O> {
56        self.0.intersects(k).then_some(v)
57    }
58}
59
60impl<'a, T: super::HasChunk> IFilter<&'a T> for Intersection<'_> {
61    type O = &'a T;
62    fn filter_map(&self, t: &'a T) -> Option<Self::O> {
63        self.0.intersects(t.chunk()).then_some(t)
64    }
65}
66
67impl<'a, T: super::HasChunk> IFilter<&'a mut T> for Intersection<'_> {
68    type O = &'a mut T;
69    fn filter_map(&self, t: &'a mut T) -> Option<Self::O> {
70        self.0.intersects(t.chunk()).then_some(t)
71    }
72}
73
74pub struct Inclusion<'a>(pub &'a keyexpr);
75impl<K: core::ops::Deref<Target = keyexpr>, V> IFilter<(&K, V)> for Inclusion<'_> {
76    type O = V;
77    fn filter_map(&self, (k, v): (&K, V)) -> Option<Self::O> {
78        self.0.includes(k).then_some(v)
79    }
80}
81
82impl<'a, T: super::HasChunk> IFilter<&'a T> for Inclusion<'_> {
83    type O = &'a T;
84    fn filter_map(&self, t: &'a T) -> Option<Self::O> {
85        self.0.includes(t.chunk()).then_some(t)
86    }
87}
88
89impl<'a, T: super::HasChunk> IFilter<&'a mut T> for Inclusion<'_> {
90    type O = &'a mut T;
91    fn filter_map(&self, t: &'a mut T) -> Option<Self::O> {
92        self.0.includes(t.chunk()).then_some(t)
93    }
94}