pub struct SplayTree<K, V, A = MemoryPool<BstNode<SplayTreeData<K, V>>>>{
root: Option<BstRoot<SplayTreeSpec<K, V>>>,
length: usize,
allocator: ManuallyDrop<A>,
}Fields§
§root: Option<BstRoot<SplayTreeSpec<K, V>>>§length: usize§allocator: ManuallyDrop<A>Implementations§
Source§impl<K, V> SplayTree<K, V>
impl<K, V> SplayTree<K, V>
pub fn new() -> Self
pub fn with_capacity(capacity: usize) -> Self
Source§impl<K, V, A> SplayTree<K, V, A>
impl<K, V, A> SplayTree<K, V, A>
Sourcefn splay<Seeker>(&mut self, seeker: Seeker) -> Option<Ordering>where
Seeker: BstSeeker<Spec = SplayTreeSpec<K, V>>,
fn splay<Seeker>(&mut self, seeker: Seeker) -> Option<Ordering>where
Seeker: BstSeeker<Spec = SplayTreeSpec<K, 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.rs (line 217)
212 pub fn get_key_value<Q>(&mut self, key: &Q) -> Option<(&K, &V)>
213 where
214 K: Borrow<Q>,
215 Q: Ord + ?Sized,
216 {
217 matches!(self.splay_by_key(key)?, Ordering::Equal).then(|| {
218 let data = self.root.as_ref().unwrap().reborrow().into_data();
219 (&data.key, &data.value)
220 })
221 }
222
223 pub fn get_key_value_at(&mut self, index: usize) -> Option<(&K, &V)> {
224 if index >= self.length {
225 return None;
226 }
227 self.splay_by_size(index);
228 let data = self.root.as_ref()?.reborrow().into_data();
229 Some((&data.key, &data.value))
230 }
231
232 pub fn insert(&mut self, key: K, value: V) -> Option<V>
233 where
234 K: Ord,
235 {
236 let ordering = self.splay_by_key(&key);
237 if matches!(ordering, Some(Ordering::Equal)) {
238 return Some(replace(
239 &mut self
240 .root
241 .as_mut()
242 .unwrap()
243 .borrow_datamut()
244 .data_mut()
245 .value,
246 value,
247 ));
248 }
249 let mut node = BstRoot::from_data(
250 SplayTreeData {
251 key,
252 value,
253 size: 1,
254 },
255 self.allocator.deref_mut(),
256 );
257 if let Some(mut root) = self.root.take() {
258 match ordering.unwrap() {
259 Ordering::Greater => {
260 let left = unsafe { root.borrow_mut().left_mut().take() };
261 if let Some(left) = left {
262 unsafe { node.borrow_mut().left_mut().set(left) };
263 }
264 SplayTreeSpec::bottom_up(root.borrow_datamut());
265 unsafe { node.borrow_mut().right_mut().set(root) };
266 }
267 Ordering::Less => {
268 let right = unsafe { root.borrow_mut().right_mut().take() };
269 if let Some(right) = right {
270 unsafe { node.borrow_mut().right_mut().set(right) };
271 }
272 SplayTreeSpec::bottom_up(root.borrow_datamut());
273 unsafe { node.borrow_mut().left_mut().set(root) };
274 }
275 Ordering::Equal => unreachable!(),
276 }
277 SplayTreeSpec::bottom_up(node.borrow_datamut());
278 }
279 self.root = Some(node);
280 self.length += 1;
281 None
282 }
283
284 pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
285 where
286 K: Borrow<Q>,
287 Q: Ord + ?Sized,
288 {
289 if !matches!(self.splay_by_key(key)?, Ordering::Equal) {
290 return None;
291 }
292 Some(self.remove_root().1)
293 }Sourcefn splay_by_size(&mut self, index: usize) -> Option<Ordering>
fn splay_by_size(&mut self, index: usize) -> Option<Ordering>
Examples found in repository?
crates/competitive/src/data_structure/splay_tree.rs (line 227)
223 pub fn get_key_value_at(&mut self, index: usize) -> Option<(&K, &V)> {
224 if index >= self.length {
225 return None;
226 }
227 self.splay_by_size(index);
228 let data = self.root.as_ref()?.reborrow().into_data();
229 Some((&data.key, &data.value))
230 }
231
232 pub fn insert(&mut self, key: K, value: V) -> Option<V>
233 where
234 K: Ord,
235 {
236 let ordering = self.splay_by_key(&key);
237 if matches!(ordering, Some(Ordering::Equal)) {
238 return Some(replace(
239 &mut self
240 .root
241 .as_mut()
242 .unwrap()
243 .borrow_datamut()
244 .data_mut()
245 .value,
246 value,
247 ));
248 }
249 let mut node = BstRoot::from_data(
250 SplayTreeData {
251 key,
252 value,
253 size: 1,
254 },
255 self.allocator.deref_mut(),
256 );
257 if let Some(mut root) = self.root.take() {
258 match ordering.unwrap() {
259 Ordering::Greater => {
260 let left = unsafe { root.borrow_mut().left_mut().take() };
261 if let Some(left) = left {
262 unsafe { node.borrow_mut().left_mut().set(left) };
263 }
264 SplayTreeSpec::bottom_up(root.borrow_datamut());
265 unsafe { node.borrow_mut().right_mut().set(root) };
266 }
267 Ordering::Less => {
268 let right = unsafe { root.borrow_mut().right_mut().take() };
269 if let Some(right) = right {
270 unsafe { node.borrow_mut().right_mut().set(right) };
271 }
272 SplayTreeSpec::bottom_up(root.borrow_datamut());
273 unsafe { node.borrow_mut().left_mut().set(root) };
274 }
275 Ordering::Equal => unreachable!(),
276 }
277 SplayTreeSpec::bottom_up(node.borrow_datamut());
278 }
279 self.root = Some(node);
280 self.length += 1;
281 None
282 }
283
284 pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
285 where
286 K: Borrow<Q>,
287 Q: Ord + ?Sized,
288 {
289 if !matches!(self.splay_by_key(key)?, Ordering::Equal) {
290 return None;
291 }
292 Some(self.remove_root().1)
293 }
294
295 pub fn remove_at(&mut self, index: usize) -> Option<(K, V)> {
296 if index >= self.length {
297 return None;
298 }
299 self.splay_by_size(index);
300 Some(self.remove_root())
301 }pub fn get<Q>(&mut self, key: &Q) -> Option<&V>
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)>
Sourcefn remove_root(&mut self) -> (K, V)
fn remove_root(&mut self) -> (K, V)
Examples found in repository?
crates/competitive/src/data_structure/splay_tree.rs (line 292)
284 pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
285 where
286 K: Borrow<Q>,
287 Q: Ord + ?Sized,
288 {
289 if !matches!(self.splay_by_key(key)?, Ordering::Equal) {
290 return None;
291 }
292 Some(self.remove_root().1)
293 }
294
295 pub fn remove_at(&mut self, index: usize) -> Option<(K, V)> {
296 if index >= self.length {
297 return None;
298 }
299 self.splay_by_size(index);
300 Some(self.remove_root())
301 }pub fn len(&self) -> usize
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 = MemoryPool<BstNode<SplayTreeData<K, V>>>> !Send for SplayTree<K, V, A>
impl<K, V, A = MemoryPool<BstNode<SplayTreeData<K, V>>>> !Sync for SplayTree<K, V, A>
impl<K, V, A> Freeze for SplayTree<K, V, A>
impl<K, V, A> RefUnwindSafe for SplayTree<K, V, A>where
Option<BstNodeRef<Owned, SplayTreeSpec<K, V>>>: RefUnwindSafe,
ManuallyDrop<A>: RefUnwindSafe,
impl<K, V, A> Unpin for SplayTree<K, V, A>
impl<K, V, A> UnsafeUnpin for SplayTree<K, V, A>
impl<K, V, A> UnwindSafe for SplayTree<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