zenoh_keyexpr/keyexpr_tree/impls/
vec_set_impl.rs1use alloc::vec::Vec;
16
17use zenoh_result::unlikely;
18
19use crate::keyexpr_tree::*;
20
21pub struct VecSetProvider;
22impl<T: 'static> IChildrenProvider<T> for VecSetProvider {
23 type Assoc = Vec<T>;
24}
25
26impl<'a, 'b, T: HasChunk> IEntry<'a, 'b, T> for Entry<'a, 'b, T> {
27 fn get_or_insert_with<F: FnOnce(&'b keyexpr) -> T>(self, f: F) -> &'a mut T {
28 match self {
29 Entry::Vacant(vec, key) => {
30 vec.push(f(key));
31 vec.last_mut().unwrap()
32 }
33 Entry::Occupied(v) => v,
34 }
35 }
36}
37
38pub enum Entry<'a, 'b, T> {
39 Vacant(&'a mut Vec<T>, &'b keyexpr),
40 Occupied(&'a mut T),
41}
42impl<T: HasChunk + AsNode<T> + AsNodeMut<T> + 'static> IChildren<T> for Vec<T> {
43 type Node = T;
44 fn child_at(&self, chunk: &keyexpr) -> Option<&T> {
45 self.iter().find(|t| unlikely(t.chunk() == chunk))
46 }
47 fn child_at_mut(&mut self, chunk: &keyexpr) -> Option<&mut T> {
48 self.iter_mut().find(|t| unlikely(t.chunk() == chunk))
49 }
50 fn remove(&mut self, chunk: &keyexpr) -> Option<Self::Node> {
51 for (i, t) in self.iter().enumerate() {
52 if unlikely(t.chunk() == chunk) {
53 return Some(self.swap_remove(i));
54 }
55 }
56 None
57 }
58 fn len(&self) -> usize {
59 self.len()
60 }
61 fn is_empty(&self) -> bool {
62 self.is_empty()
63 }
64 type Entry<'a, 'b>
65 = Entry<'a, 'b, T>
66 where
67 Self: 'a,
68 'a: 'b,
69 T: 'b;
70 fn entry<'a, 'b>(&'a mut self, chunk: &'b keyexpr) -> Self::Entry<'a, 'b>
71 where
72 Self: 'a,
73 'a: 'b,
74 T: 'b,
75 {
76 let this = unsafe { &mut *(self as *mut Self) };
78 match self.child_at_mut(chunk) {
79 Some(entry) => Entry::Occupied(entry),
80 None => Entry::Vacant(this, chunk),
81 }
82 }
83
84 type Iter<'a>
85 = core::slice::Iter<'a, T>
86 where
87 Self: 'a;
88 fn children<'a>(&'a self) -> Self::Iter<'a>
89 where
90 Self: 'a,
91 {
92 self.iter()
93 }
94
95 type IterMut<'a>
96 = core::slice::IterMut<'a, T>
97 where
98 Self: 'a;
99
100 fn children_mut<'a>(&'a mut self) -> Self::IterMut<'a>
101 where
102 Self: 'a,
103 {
104 self.iter_mut()
105 }
106
107 fn filter_out<F: FnMut(&mut T) -> bool>(&mut self, predicate: &mut F) {
108 for i in (0..self.len()).rev() {
109 if predicate(&mut self[i]) {
110 self.swap_remove(i);
111 }
112 }
113 }
114
115 type Intersection<'a>
116 = super::FilterMap<core::slice::Iter<'a, T>, super::Intersection<'a>>
117 where
118 Self: 'a,
119 Self::Node: 'a;
120 fn intersection<'a>(&'a self, key: &'a keyexpr) -> Self::Intersection<'a> {
121 super::FilterMap::new(self.iter(), super::Intersection(key))
122 }
123 type IntersectionMut<'a>
124 = super::FilterMap<core::slice::IterMut<'a, T>, super::Intersection<'a>>
125 where
126 Self: 'a,
127 Self::Node: 'a;
128 fn intersection_mut<'a>(&'a mut self, key: &'a keyexpr) -> Self::IntersectionMut<'a> {
129 super::FilterMap::new(self.iter_mut(), super::Intersection(key))
130 }
131 type Inclusion<'a>
132 = super::FilterMap<core::slice::Iter<'a, T>, super::Inclusion<'a>>
133 where
134 Self: 'a,
135 Self::Node: 'a;
136 fn inclusion<'a>(&'a self, key: &'a keyexpr) -> Self::Inclusion<'a> {
137 super::FilterMap::new(self.iter(), super::Inclusion(key))
138 }
139 type InclusionMut<'a>
140 = super::FilterMap<core::slice::IterMut<'a, T>, super::Inclusion<'a>>
141 where
142 Self: 'a,
143 Self::Node: 'a;
144 fn inclusion_mut<'a>(&'a mut self, key: &'a keyexpr) -> Self::InclusionMut<'a> {
145 super::FilterMap::new(self.iter_mut(), super::Inclusion(key))
146 }
147}