Skip to main content

BstNodeId

Struct BstNodeId 

Source
pub struct BstNodeId<Spec>
where Spec: BstSpec,
{ node: NonNull<BstNode<Spec::Data, Spec::Parent>>, generation: u64, }

Fields§

§node: NonNull<BstNode<Spec::Data, Spec::Parent>>§generation: u64

Implementations§

Source§

impl<Spec> BstNodeId<Spec>
where Spec: BstSpec,

Source

pub unsafe fn reborrow<'a>( self, _root: &'a Option<BstRoot<Spec>>, ) -> BstNodeRef<Immut<'a>, Spec>

Examples found in repository?
crates/competitive/src/data_structure/treap.rs (line 316)
308    pub fn get(&mut self, node_id: BstNodeId<TreapSpec<M, L>>) -> Option<(&M::Key, &L::Key)> {
309        if !self.node_id_manager.contains(&node_id) {
310            return None;
311        }
312        unsafe {
313            WithParent::resolve_top_down::<TreapSpec<M, L>>(
314                node_id.reborrow_datamut(&mut self.root),
315            );
316            let data = node_id.reborrow(&self.root).into_data();
317            Some((&data.key.key, &data.value.key))
318        }
319    }
320
321    pub fn change(
322        &mut self,
323        node_id: BstNodeId<TreapSpec<M, L>>,
324        f: impl FnOnce(&mut L::Key),
325    ) -> bool {
326        if !self.node_id_manager.contains(&node_id) {
327            return false;
328        }
329        unsafe {
330            WithParent::resolve_top_down::<TreapSpec<M, L>>(
331                node_id.reborrow_datamut(&mut self.root),
332            );
333            let data = node_id.reborrow_datamut(&mut self.root).into_data_mut();
334            f(&mut data.value.key);
335            WithParent::resolve_bottom_up::<TreapSpec<M, L>>(
336                node_id.reborrow_datamut(&mut self.root),
337            );
338        }
339        true
340    }
341
342    pub fn change_key_value(
343        &mut self,
344        node_id: BstNodeId<TreapSpec<M, L>>,
345        f: impl FnOnce(&mut M::Key, &mut L::Key),
346    ) -> bool {
347        if !self.node_id_manager.contains(&node_id) {
348            return false;
349        }
350        unsafe {
351            WithParent::resolve_top_down::<TreapSpec<M, L>>(
352                node_id.reborrow_datamut(&mut self.root),
353            );
354            let mut node = if WithParent::is_root(node_id.reborrow(&self.root)) {
355                WithParent::remove_root(&mut self.root).unwrap_unchecked()
356            } else {
357                WithParent::remove_not_root(node_id.reborrow_mut(&mut self.root))
358            };
359            let data = node.borrow_datamut().into_data_mut();
360            f(&mut data.key.key, &mut data.value.key);
361            self.root = TreapSpec::merge_ordered(self.root.take(), Some(node));
362            true
363        }
364    }
365
366    pub fn insert(&mut self, key: M::Key, value: L::Key) -> BstNodeId<TreapSpec<M, L>> {
367        let (left, right) =
368            TreapSpec::split(self.root.take(), SeekByKey::new(&key), EqualSide::Right);
369        let data = TreapData {
370            priority: self.rng.rand64(),
371            key: MonoidActElement::from_key(key),
372            value: LazyMapElement::from_key(value),
373        };
374        let node = BstRoot::from_data(data, self.allocator.deref_mut());
375        let node_id = self.node_id_manager.register(&node);
376        self.root = TreapSpec::merge(TreapSpec::merge(left, Some(node)), right);
377        node_id
378    }
379
380    pub fn remove(&mut self, node_id: BstNodeId<TreapSpec<M, L>>) -> Option<(M::Key, L::Key)> {
381        if !self.node_id_manager.contains(&node_id) {
382            return None;
383        }
384        unsafe {
385            WithParent::resolve_top_down::<TreapSpec<M, L>>(
386                node_id.reborrow_datamut(&mut self.root),
387            );
388            let node = if WithParent::is_root(node_id.reborrow(&self.root)) {
389                WithParent::remove_root(&mut self.root).unwrap_unchecked()
390            } else {
391                WithParent::remove_not_root(node_id.reborrow_mut(&mut self.root))
392            };
393            self.node_id_manager.unregister(node_id);
394            let data = node.into_dying().into_data(self.allocator.deref_mut());
395            Some((data.key.key, data.value.key))
396        }
397    }
Source

pub unsafe fn reborrow_datamut<'a>( self, _root: &'a mut Option<BstRoot<Spec>>, ) -> BstNodeRef<DataMut<'a>, Spec>

Examples found in repository?
crates/competitive/src/data_structure/treap.rs (line 314)
308    pub fn get(&mut self, node_id: BstNodeId<TreapSpec<M, L>>) -> Option<(&M::Key, &L::Key)> {
309        if !self.node_id_manager.contains(&node_id) {
310            return None;
311        }
312        unsafe {
313            WithParent::resolve_top_down::<TreapSpec<M, L>>(
314                node_id.reborrow_datamut(&mut self.root),
315            );
316            let data = node_id.reborrow(&self.root).into_data();
317            Some((&data.key.key, &data.value.key))
318        }
319    }
320
321    pub fn change(
322        &mut self,
323        node_id: BstNodeId<TreapSpec<M, L>>,
324        f: impl FnOnce(&mut L::Key),
325    ) -> bool {
326        if !self.node_id_manager.contains(&node_id) {
327            return false;
328        }
329        unsafe {
330            WithParent::resolve_top_down::<TreapSpec<M, L>>(
331                node_id.reborrow_datamut(&mut self.root),
332            );
333            let data = node_id.reborrow_datamut(&mut self.root).into_data_mut();
334            f(&mut data.value.key);
335            WithParent::resolve_bottom_up::<TreapSpec<M, L>>(
336                node_id.reborrow_datamut(&mut self.root),
337            );
338        }
339        true
340    }
341
342    pub fn change_key_value(
343        &mut self,
344        node_id: BstNodeId<TreapSpec<M, L>>,
345        f: impl FnOnce(&mut M::Key, &mut L::Key),
346    ) -> bool {
347        if !self.node_id_manager.contains(&node_id) {
348            return false;
349        }
350        unsafe {
351            WithParent::resolve_top_down::<TreapSpec<M, L>>(
352                node_id.reborrow_datamut(&mut self.root),
353            );
354            let mut node = if WithParent::is_root(node_id.reborrow(&self.root)) {
355                WithParent::remove_root(&mut self.root).unwrap_unchecked()
356            } else {
357                WithParent::remove_not_root(node_id.reborrow_mut(&mut self.root))
358            };
359            let data = node.borrow_datamut().into_data_mut();
360            f(&mut data.key.key, &mut data.value.key);
361            self.root = TreapSpec::merge_ordered(self.root.take(), Some(node));
362            true
363        }
364    }
365
366    pub fn insert(&mut self, key: M::Key, value: L::Key) -> BstNodeId<TreapSpec<M, L>> {
367        let (left, right) =
368            TreapSpec::split(self.root.take(), SeekByKey::new(&key), EqualSide::Right);
369        let data = TreapData {
370            priority: self.rng.rand64(),
371            key: MonoidActElement::from_key(key),
372            value: LazyMapElement::from_key(value),
373        };
374        let node = BstRoot::from_data(data, self.allocator.deref_mut());
375        let node_id = self.node_id_manager.register(&node);
376        self.root = TreapSpec::merge(TreapSpec::merge(left, Some(node)), right);
377        node_id
378    }
379
380    pub fn remove(&mut self, node_id: BstNodeId<TreapSpec<M, L>>) -> Option<(M::Key, L::Key)> {
381        if !self.node_id_manager.contains(&node_id) {
382            return None;
383        }
384        unsafe {
385            WithParent::resolve_top_down::<TreapSpec<M, L>>(
386                node_id.reborrow_datamut(&mut self.root),
387            );
388            let node = if WithParent::is_root(node_id.reborrow(&self.root)) {
389                WithParent::remove_root(&mut self.root).unwrap_unchecked()
390            } else {
391                WithParent::remove_not_root(node_id.reborrow_mut(&mut self.root))
392            };
393            self.node_id_manager.unregister(node_id);
394            let data = node.into_dying().into_data(self.allocator.deref_mut());
395            Some((data.key.key, data.value.key))
396        }
397    }
Source

pub unsafe fn reborrow_mut<'a>( self, _root: &'a mut Option<BstRoot<Spec>>, ) -> BstNodeRef<Mut<'a>, Spec>

Examples found in repository?
crates/competitive/src/data_structure/treap.rs (line 357)
342    pub fn change_key_value(
343        &mut self,
344        node_id: BstNodeId<TreapSpec<M, L>>,
345        f: impl FnOnce(&mut M::Key, &mut L::Key),
346    ) -> bool {
347        if !self.node_id_manager.contains(&node_id) {
348            return false;
349        }
350        unsafe {
351            WithParent::resolve_top_down::<TreapSpec<M, L>>(
352                node_id.reborrow_datamut(&mut self.root),
353            );
354            let mut node = if WithParent::is_root(node_id.reborrow(&self.root)) {
355                WithParent::remove_root(&mut self.root).unwrap_unchecked()
356            } else {
357                WithParent::remove_not_root(node_id.reborrow_mut(&mut self.root))
358            };
359            let data = node.borrow_datamut().into_data_mut();
360            f(&mut data.key.key, &mut data.value.key);
361            self.root = TreapSpec::merge_ordered(self.root.take(), Some(node));
362            true
363        }
364    }
365
366    pub fn insert(&mut self, key: M::Key, value: L::Key) -> BstNodeId<TreapSpec<M, L>> {
367        let (left, right) =
368            TreapSpec::split(self.root.take(), SeekByKey::new(&key), EqualSide::Right);
369        let data = TreapData {
370            priority: self.rng.rand64(),
371            key: MonoidActElement::from_key(key),
372            value: LazyMapElement::from_key(value),
373        };
374        let node = BstRoot::from_data(data, self.allocator.deref_mut());
375        let node_id = self.node_id_manager.register(&node);
376        self.root = TreapSpec::merge(TreapSpec::merge(left, Some(node)), right);
377        node_id
378    }
379
380    pub fn remove(&mut self, node_id: BstNodeId<TreapSpec<M, L>>) -> Option<(M::Key, L::Key)> {
381        if !self.node_id_manager.contains(&node_id) {
382            return None;
383        }
384        unsafe {
385            WithParent::resolve_top_down::<TreapSpec<M, L>>(
386                node_id.reborrow_datamut(&mut self.root),
387            );
388            let node = if WithParent::is_root(node_id.reborrow(&self.root)) {
389                WithParent::remove_root(&mut self.root).unwrap_unchecked()
390            } else {
391                WithParent::remove_not_root(node_id.reborrow_mut(&mut self.root))
392            };
393            self.node_id_manager.unregister(node_id);
394            let data = node.into_dying().into_data(self.allocator.deref_mut());
395            Some((data.key.key, data.value.key))
396        }
397    }

Trait Implementations§

Source§

impl<Spec> Clone for BstNodeId<Spec>
where Spec: BstSpec,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<Spec> Copy for BstNodeId<Spec>
where Spec: BstSpec,

Source§

impl<Spec> Eq for BstNodeId<Spec>
where Spec: BstSpec,

Source§

impl<Spec> Hash for BstNodeId<Spec>
where Spec: BstSpec,

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<Spec> PartialEq for BstNodeId<Spec>
where Spec: BstSpec,

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl<Spec> !Send for BstNodeId<Spec>

§

impl<Spec> !Sync for BstNodeId<Spec>

§

impl<Spec> Freeze for BstNodeId<Spec>
where NonNull<BstNode<<Spec as BstSpec>::Data, <Spec as BstSpec>::Parent>>: Freeze,

§

impl<Spec> RefUnwindSafe for BstNodeId<Spec>
where NonNull<BstNode<<Spec as BstSpec>::Data, <Spec as BstSpec>::Parent>>: RefUnwindSafe,

§

impl<Spec> Unpin for BstNodeId<Spec>
where NonNull<BstNode<<Spec as BstSpec>::Data, <Spec as BstSpec>::Parent>>: Unpin,

§

impl<Spec> UnsafeUnpin for BstNodeId<Spec>
where NonNull<BstNode<<Spec as BstSpec>::Data, <Spec as BstSpec>::Parent>>: UnsafeUnpin,

§

impl<Spec> UnwindSafe for BstNodeId<Spec>
where NonNull<BstNode<<Spec as BstSpec>::Data, <Spec as BstSpec>::Parent>>: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.