Skip to main content

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;
26#[derive(Debug)]
27pub struct FilterMap<I, F> {
28    iter: I,
29    filter: F,
30}
31
32impl<I, F> FilterMap<I, F> {
33    fn new(iter: I, filter: F) -> Self {
34        Self { iter, filter }
35    }
36}
37pub trait IFilter<I> {
38    type O;
39    fn filter_map(&self, i: I) -> Option<Self::O>;
40}
41impl<I: Iterator, F: IFilter<<I as Iterator>::Item>> Iterator for FilterMap<I, F> {
42    type Item = F::O;
43
44    fn next(&mut self) -> Option<Self::Item> {
45        for next in self.iter.by_ref() {
46            if let Some(output) = self.filter.filter_map(next) {
47                return Some(output);
48            }
49        }
50        None
51    }
52}
53#[derive(Debug)]
54pub struct Intersection<'a>(pub &'a keyexpr);
55impl<K: core::ops::Deref<Target = keyexpr>, V> IFilter<(&K, V)> for Intersection<'_> {
56    type O = V;
57    fn filter_map(&self, (k, v): (&K, V)) -> Option<Self::O> {
58        self.0.intersects(k).then_some(v)
59    }
60}
61
62impl<'a, T: super::HasChunk> IFilter<&'a T> for Intersection<'_> {
63    type O = &'a T;
64    fn filter_map(&self, t: &'a T) -> Option<Self::O> {
65        self.0.intersects(t.chunk()).then_some(t)
66    }
67}
68
69impl<'a, T: super::HasChunk> IFilter<&'a mut T> for Intersection<'_> {
70    type O = &'a mut T;
71    fn filter_map(&self, t: &'a mut T) -> Option<Self::O> {
72        self.0.intersects(t.chunk()).then_some(t)
73    }
74}
75
76#[derive(Debug)]
77pub struct Inclusion<'a>(pub &'a keyexpr);
78impl<K: core::ops::Deref<Target = keyexpr>, V> IFilter<(&K, V)> for Inclusion<'_> {
79    type O = V;
80    fn filter_map(&self, (k, v): (&K, V)) -> Option<Self::O> {
81        self.0.includes(k).then_some(v)
82    }
83}
84
85impl<'a, T: super::HasChunk> IFilter<&'a T> for Inclusion<'_> {
86    type O = &'a T;
87    fn filter_map(&self, t: &'a T) -> Option<Self::O> {
88        self.0.includes(t.chunk()).then_some(t)
89    }
90}
91
92impl<'a, T: super::HasChunk> IFilter<&'a mut T> for Inclusion<'_> {
93    type O = &'a mut T;
94    fn filter_map(&self, t: &'a mut T) -> Option<Self::O> {
95        self.0.includes(t.chunk()).then_some(t)
96    }
97}