Skip to main content

SplayTree

Struct SplayTree 

Source
pub struct SplayTree<K, V, A = MemoryPool<BstNode<SplayTreeData<K, V>>>>
where A: Allocator<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>

Source

pub fn new() -> Self

Source

pub fn with_capacity(capacity: usize) -> Self

Source§

impl<K, V, A> SplayTree<K, V, A>
where A: Allocator<BstNode<SplayTreeData<K, V>>>,

Source

fn splay<Seeker>(&mut self, seeker: Seeker) -> Option<Ordering>
where Seeker: BstSeeker<Spec = SplayTreeSpec<K, V>>,

Examples found in repository?
crates/competitive/src/data_structure/splay_tree.rs (line 197)
192    fn splay_by_key<Q>(&mut self, key: &Q) -> Option<Ordering>
193    where
194        K: Borrow<Q>,
195        Q: Ord + ?Sized,
196    {
197        self.splay(SeekByKey::new(key))
198    }
199
200    fn splay_by_size(&mut self, index: usize) -> Option<Ordering> {
201        self.splay(SeekBySize::new(index))
202    }
Source

fn splay_by_key<Q>(&mut self, key: &Q) -> Option<Ordering>
where K: Borrow<Q>, Q: Ord + ?Sized,

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    }
Source

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    }
Source

pub fn get<Q>(&mut self, key: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Ord + ?Sized,

Source

pub fn get_key_value<Q>(&mut self, key: &Q) -> Option<(&K, &V)>
where K: Borrow<Q>, Q: Ord + ?Sized,

Examples found in repository?
crates/competitive/src/data_structure/splay_tree.rs (line 209)
204    pub fn get<Q>(&mut self, key: &Q) -> Option<&V>
205    where
206        K: Borrow<Q>,
207        Q: Ord + ?Sized,
208    {
209        self.get_key_value(key).map(|(_, value)| value)
210    }
Source

pub fn get_key_value_at(&mut self, index: usize) -> Option<(&K, &V)>

Source

pub fn insert(&mut self, key: K, value: V) -> Option<V>
where K: Ord,

Source

pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
where K: Borrow<Q>, Q: Ord + ?Sized,

Source

pub fn remove_at(&mut self, index: usize) -> Option<(K, V)>

Source

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    }
Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn iter(&mut self) -> Iter<'_, K, V> ⓘ

Source

pub fn range<Q, R>(&mut self, range: R) -> Iter<'_, K, V> ⓘ
where K: Borrow<Q>, Q: Ord + ?Sized, R: RangeBounds<Q>,

Source

pub fn range_at<R>(&mut self, range: R) -> Iter<'_, K, V> ⓘ
where R: RangeBounds<usize>,

Trait Implementations§

Source§

impl<K, V, A> Debug for SplayTree<K, V, A>
where K: Debug, V: Debug, A: Allocator<BstNode<SplayTreeData<K, V>>>,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K, V, A> Default for SplayTree<K, V, A>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<K, V, A> Drop for SplayTree<K, V, A>
where A: Allocator<BstNode<SplayTreeData<K, V>>>,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

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>

§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToArrayVecScalar for T

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.