pub struct SplayMap<K, V, A = MemoryPool<Node<((K, V), usize)>>>{
root: Root<SizedSplay<(K, V)>>,
length: usize,
alloc: ManuallyDrop<A>,
}Fields§
§root: Root<SizedSplay<(K, V)>>§length: usize§alloc: ManuallyDrop<A>Implementations§
Source§impl<K, V> SplayMap<K, V>
impl<K, V> SplayMap<K, V>
pub fn new() -> Self
pub fn with_capacity(capacity: usize) -> Self
Source§impl<K, V, A> SplayMap<K, V, A>
impl<K, V, A> SplayMap<K, V, A>
pub fn get<Q>(&mut self, key: &Q) -> Option<&V>
Sourcefn splay_by_key<Q>(&mut self, key: &Q) -> Option<Ordering>
fn splay_by_key<Q>(&mut self, key: &Q) -> Option<Ordering>
Examples found in repository?
crates/competitive/src/data_structure/splay_tree/sized_map.rs (line 168)
163 pub fn get_key_value<Q>(&mut self, key: &Q) -> Option<(&K, &V)>
164 where
165 K: Borrow<Q>,
166 Q: Ord + ?Sized,
167 {
168 if !matches!(self.splay_by_key(key)?, Ordering::Equal) {
169 return None;
170 }
171 self.root.root().map(|node| {
172 let ((k, v), _) = node.data();
173 (k, v)
174 })
175 }
176 fn splay_at(&mut self, index: usize) -> Option<Ordering> {
177 self.root.splay_by(SeekBySize::new(index))
178 }
179 pub fn get_key_value_at(&mut self, index: usize) -> Option<(&K, &V)> {
180 if index >= self.length {
181 return None;
182 }
183 self.splay_at(index);
184 self.root.root().map(|node| {
185 let ((k, v), _) = node.data();
186 (k, v)
187 })
188 }
189 pub fn insert(&mut self, key: K, value: V) -> Option<V>
190 where
191 K: Ord,
192 {
193 let ord = self.splay_by_key(&key);
194 self.length += (ord != Some(Ordering::Equal)) as usize;
195 match ord {
196 Some(Ordering::Equal) => {
197 return Some(replace(
198 &mut (self.root.root_data_mut().unwrap().data_mut().0).1,
199 value,
200 ));
201 }
202 Some(Ordering::Less) => unsafe {
203 self.root.insert_left(NodeRef::from_data(
204 ((key, value), 1),
205 self.alloc.deref_mut(),
206 ));
207 },
208 _ => unsafe {
209 self.root.insert_right(NodeRef::from_data(
210 ((key, value), 1),
211 self.alloc.deref_mut(),
212 ));
213 },
214 }
215 None
216 }
217 pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
218 where
219 K: Borrow<Q>,
220 Q: Ord + ?Sized,
221 {
222 if !matches!(self.splay_by_key(key)?, Ordering::Equal) {
223 return None;
224 }
225 self.length -= 1;
226 let node = self.root.take_root().unwrap().into_dying();
227 unsafe { Some((node.into_data(self.alloc.deref_mut()).0).1) }
228 }Sourcefn splay_at(&mut self, index: usize) -> Option<Ordering>
fn splay_at(&mut self, index: usize) -> Option<Ordering>
Examples found in repository?
crates/competitive/src/data_structure/splay_tree/sized_map.rs (line 183)
179 pub fn get_key_value_at(&mut self, index: usize) -> Option<(&K, &V)> {
180 if index >= self.length {
181 return None;
182 }
183 self.splay_at(index);
184 self.root.root().map(|node| {
185 let ((k, v), _) = node.data();
186 (k, v)
187 })
188 }
189 pub fn insert(&mut self, key: K, value: V) -> Option<V>
190 where
191 K: Ord,
192 {
193 let ord = self.splay_by_key(&key);
194 self.length += (ord != Some(Ordering::Equal)) as usize;
195 match ord {
196 Some(Ordering::Equal) => {
197 return Some(replace(
198 &mut (self.root.root_data_mut().unwrap().data_mut().0).1,
199 value,
200 ));
201 }
202 Some(Ordering::Less) => unsafe {
203 self.root.insert_left(NodeRef::from_data(
204 ((key, value), 1),
205 self.alloc.deref_mut(),
206 ));
207 },
208 _ => unsafe {
209 self.root.insert_right(NodeRef::from_data(
210 ((key, value), 1),
211 self.alloc.deref_mut(),
212 ));
213 },
214 }
215 None
216 }
217 pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
218 where
219 K: Borrow<Q>,
220 Q: Ord + ?Sized,
221 {
222 if !matches!(self.splay_by_key(key)?, Ordering::Equal) {
223 return None;
224 }
225 self.length -= 1;
226 let node = self.root.take_root().unwrap().into_dying();
227 unsafe { Some((node.into_data(self.alloc.deref_mut()).0).1) }
228 }
229 pub fn remove_at(&mut self, index: usize) -> Option<(K, V)> {
230 if index >= self.length {
231 return None;
232 }
233 self.splay_at(index);
234 self.length -= 1;
235 let node = self.root.take_root().unwrap().into_dying();
236 unsafe { Some(node.into_data(self.alloc.deref_mut()).0) }
237 }pub fn get_key_value_at(&mut self, index: usize) -> Option<(&K, &V)>
pub fn insert(&mut self, key: K, value: V) -> Option<V>where
K: Ord,
pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
pub fn remove_at(&mut self, index: usize) -> Option<(K, V)>
pub fn is_empty(&self) -> bool
pub fn iter(&mut self) -> Iter<'_, K, V> ⓘ
pub fn range<Q, R>(&mut self, range: R) -> Iter<'_, K, V> ⓘ
pub fn range_at<R>(&mut self, range: R) -> Iter<'_, K, V> ⓘwhere
R: RangeBounds<usize>,
Trait Implementations§
Auto Trait Implementations§
impl<K, V, A> Freeze for SplayMap<K, V, A>where
A: Freeze,
impl<K, V, A> RefUnwindSafe for SplayMap<K, V, A>
impl<K, V, A = MemoryPool<Node<((K, V), usize)>>> !Send for SplayMap<K, V, A>
impl<K, V, A = MemoryPool<Node<((K, V), usize)>>> !Sync for SplayMap<K, V, A>
impl<K, V, A> Unpin for SplayMap<K, V, A>where
A: Unpin,
impl<K, V, A> UnwindSafe for SplayMap<K, V, A>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more