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) };
77 match self.child_at_mut(chunk) {
78 Some(entry) => Entry::Occupied(entry),
79 None => Entry::Vacant(this, chunk),
80 }
81 }
82
83 type Iter<'a>
84 = core::slice::Iter<'a, T>
85 where
86 Self: 'a;
87 fn children<'a>(&'a self) -> Self::Iter<'a>
88 where
89 Self: 'a,
90 {
91 self.iter()
92 }
93
94 type IterMut<'a>
95 = core::slice::IterMut<'a, T>
96 where
97 Self: 'a;
98
99 fn children_mut<'a>(&'a mut self) -> Self::IterMut<'a>
100 where
101 Self: 'a,
102 {
103 self.iter_mut()
104 }
105
106 fn filter_out<F: FnMut(&mut T) -> bool>(&mut self, predicate: &mut F) {
107 for i in (0..self.len()).rev() {
108 if predicate(&mut self[i]) {
109 self.swap_remove(i);
110 }
111 }
112 }
113
114 type Intersection<'a>
115 = super::FilterMap<core::slice::Iter<'a, T>, super::Intersection<'a>>
116 where
117 Self: 'a,
118 Self::Node: 'a;
119 fn intersection<'a>(&'a self, key: &'a keyexpr) -> Self::Intersection<'a> {
120 super::FilterMap::new(self.iter(), super::Intersection(key))
121 }
122 type IntersectionMut<'a>
123 = super::FilterMap<core::slice::IterMut<'a, T>, super::Intersection<'a>>
124 where
125 Self: 'a,
126 Self::Node: 'a;
127 fn intersection_mut<'a>(&'a mut self, key: &'a keyexpr) -> Self::IntersectionMut<'a> {
128 super::FilterMap::new(self.iter_mut(), super::Intersection(key))
129 }
130 type Inclusion<'a>
131 = super::FilterMap<core::slice::Iter<'a, T>, super::Inclusion<'a>>
132 where
133 Self: 'a,
134 Self::Node: 'a;
135 fn inclusion<'a>(&'a self, key: &'a keyexpr) -> Self::Inclusion<'a> {
136 super::FilterMap::new(self.iter(), super::Inclusion(key))
137 }
138 type InclusionMut<'a>
139 = super::FilterMap<core::slice::IterMut<'a, T>, super::Inclusion<'a>>
140 where
141 Self: 'a,
142 Self::Node: 'a;
143 fn inclusion_mut<'a>(&'a mut self, key: &'a keyexpr) -> Self::InclusionMut<'a> {
144 super::FilterMap::new(self.iter_mut(), super::Inclusion(key))
145 }
146}