Skip to main content

TreapSpec

Struct TreapSpec 

Source
pub struct TreapSpec<M, L> {
    _marker: PhantomData<(M, L)>,
}

Fields§

§_marker: PhantomData<(M, L)>

Implementations§

Source§

impl<M, L> TreapSpec<M, L>
where M: MonoidAct<Key: Ord>, L: LazyMapMonoid,

Source

pub fn merge_ordered( left: Option<BstRoot<TreapSpec<M, L>>>, right: Option<BstRoot<TreapSpec<M, L>>>, ) -> Option<BstRoot<TreapSpec<M, L>>>

Examples found in repository?
crates/competitive/src/data_structure/treap.rs (line 201)
187    pub fn merge_ordered(
188        left: Option<TreapRoot<M, L>>,
189        right: Option<TreapRoot<M, L>>,
190    ) -> Option<TreapRoot<M, L>> {
191        match (left, right) {
192            (None, None) => None,
193            (None, Some(node)) | (Some(node), None) => Some(node),
194            (Some(mut left), Some(mut right)) => unsafe {
195                if left.reborrow().into_data().priority > right.reborrow().into_data().priority {
196                    Self::top_down(left.borrow_datamut());
197                    let key = &left.reborrow().into_data().key.key;
198                    let (rl, rr) = Self::split(Some(right), SeekByKey::new(key), EqualSide::Right);
199                    let ll = left.borrow_mut().left().take();
200                    let lr = left.borrow_mut().right().take();
201                    if let Some(l) = Self::merge_ordered(ll, rl) {
202                        left.borrow_mut().left().set(l);
203                    }
204                    if let Some(r) = Self::merge_ordered(lr, rr) {
205                        left.borrow_mut().right().set(r);
206                    }
207                    Self::bottom_up(left.borrow_datamut());
208                    Some(left)
209                } else {
210                    Self::top_down(right.borrow_datamut());
211                    let key = &right.reborrow().into_data().key.key;
212                    let (ll, lr) = Self::split(Some(left), SeekByKey::new(key), EqualSide::Right);
213                    let rl = right.borrow_mut().left().take();
214                    let rr = right.borrow_mut().right().take();
215                    if let Some(l) = Self::merge_ordered(ll, rl) {
216                        right.borrow_mut().left().set(l);
217                    }
218                    if let Some(r) = Self::merge_ordered(lr, rr) {
219                        right.borrow_mut().right().set(r);
220                    }
221                    Self::bottom_up(right.borrow_datamut());
222                    Some(right)
223                }
224            },
225        }
226    }
227}
228
229pub struct Treap<M, L, A = BoxAllocator<TreapNode<M, L>>>
230where
231    M: MonoidAct<Key: Ord>,
232    L: LazyMapMonoid,
233    A: Allocator<TreapNode<M, L>>,
234{
235    root: Option<TreapRoot<M, L>>,
236    node_id_manager: BstNodeIdManager<TreapSpec<M, L>>,
237    rng: Xorshift,
238    allocator: ManuallyDrop<A>,
239    _marker: PhantomData<(M, L)>,
240}
241
242impl<M, L, A> Default for Treap<M, L, A>
243where
244    M: MonoidAct<Key: Ord>,
245    L: LazyMapMonoid,
246    A: Allocator<TreapNode<M, L>> + Default,
247{
248    fn default() -> Self {
249        Self {
250            root: None,
251            node_id_manager: Default::default(),
252            rng: Xorshift::new(),
253            allocator: ManuallyDrop::new(A::default()),
254            _marker: PhantomData,
255        }
256    }
257}
258
259impl<M, L, A> Drop for Treap<M, L, A>
260where
261    M: MonoidAct<Key: Ord>,
262    L: LazyMapMonoid,
263    A: Allocator<TreapNode<M, L>>,
264{
265    fn drop(&mut self) {
266        unsafe {
267            if let Some(root) = self.root.take() {
268                root.into_dying().drop_all(self.allocator.deref_mut());
269            }
270            ManuallyDrop::drop(&mut self.allocator);
271        }
272    }
273}
274
275impl<M, L> Treap<M, L>
276where
277    M: MonoidAct<Key: Ord>,
278    L: LazyMapMonoid,
279{
280    pub fn new() -> Self {
281        Self::default()
282    }
283}
284
285impl<M, L, A> Treap<M, L, A>
286where
287    M: MonoidAct<Key: Ord>,
288    L: LazyMapMonoid,
289    A: Allocator<TreapNode<M, L>>,
290{
291    pub fn len(&self) -> usize {
292        self.node_id_manager.len()
293    }
294
295    pub fn is_empty(&self) -> bool {
296        self.node_id_manager.is_empty()
297    }
298
299    pub fn clear(&mut self) {
300        unsafe {
301            if let Some(root) = self.root.take() {
302                root.into_dying().drop_all(self.allocator.deref_mut());
303            }
304            self.node_id_manager.clear();
305        }
306    }
307
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    }

Trait Implementations§

Source§

impl<M, L> BstSpec for TreapSpec<M, L>
where M: MonoidAct<Key: Ord>, L: LazyMapMonoid,

Source§

type Parent = WithParent<<TreapSpec<M, L> as BstSpec>::Data>

Source§

type Data = TreapData<M, L>

Source§

fn top_down(node: BstDataMutRef<'_, Self>)

Source§

fn bottom_up(node: BstDataMutRef<'_, Self>)

Source§

fn merge( left: Option<BstRoot<TreapSpec<M, L>>>, right: Option<BstRoot<TreapSpec<M, L>>>, ) -> Option<BstRoot<TreapSpec<M, L>>>

Source§

fn split<Seeker>( node: Option<BstRoot<TreapSpec<M, L>>>, seeker: Seeker, equal_side: EqualSide, ) -> (Option<BstRoot<TreapSpec<M, L>>>, Option<BstRoot<TreapSpec<M, L>>>)
where Seeker: BstSeeker<Spec = Self>,

Auto Trait Implementations§

§

impl<M, L> Freeze for TreapSpec<M, L>

§

impl<M, L> RefUnwindSafe for TreapSpec<M, L>

§

impl<M, L> Send for TreapSpec<M, L>

§

impl<M, L> Sync for TreapSpec<M, L>

§

impl<M, L> Unpin for TreapSpec<M, L>

§

impl<M, L> UnsafeUnpin for TreapSpec<M, L>

§

impl<M, L> UnwindSafe for TreapSpec<M, L>

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.