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