Skip to main content

Node

Struct Node 

Source
struct Node<M>
where M: LazyMapMonoid,
{ child: [usize; 2], parent: usize, agg: M::Agg, lazy: M::Act, }

Fields§

§child: [usize; 2]§parent: usize§agg: M::Agg§lazy: M::Act

Implementations§

Source§

impl<M> Node<M>
where M: LazyMapMonoid,

Source

fn new(parent: usize) -> Self

Examples found in repository?
crates/competitive/src/data_structure/binary_trie.rs (line 62)
50    pub fn with_capacity(bit_len: usize, capacity: usize) -> Self {
51        assert!(bit_len <= 64);
52        let max_key = if bit_len == 64 {
53            u64::MAX
54        } else {
55            (1u64 << bit_len) - 1
56        };
57        let mut nodes = Vec::with_capacity(
58            capacity
59                .saturating_mul(bit_len.saturating_add(1))
60                .saturating_add(1),
61        );
62        nodes.push(Node::new(usize::MAX));
63        Self {
64            bit_len,
65            max_key,
66            len: 0,
67            xor_mask: 0,
68            nodes,
69        }
70    }
71
72    pub fn len(&self) -> usize {
73        self.len
74    }
75
76    pub fn is_empty(&self) -> bool {
77        self.len() == 0
78    }
79
80    pub fn clear(&mut self) {
81        self.len = 0;
82        self.xor_mask = 0;
83        self.nodes.clear();
84        self.nodes.push(Node::new(usize::MAX));
85    }
86
87    pub fn set(&mut self, key: u64, value: M::Agg) {
88        self.modify_or_insert(key, |x| *x = value);
89    }
90
91    pub fn modify_or_insert(&mut self, key: u64, f: impl FnOnce(&mut M::Agg)) {
92        assert!(key <= self.max_key);
93        if self.bit_len == 0 {
94            if self.is_empty() {
95                self.len = 1;
96            }
97            f(&mut self.nodes[0].agg);
98            return;
99        }
100
101        let key = key ^ self.xor_mask;
102        let mut inserted = false;
103        let mut node = 0;
104        for d in (0..self.bit_len).rev() {
105            self.push_at(node, d + 1);
106            let bit = ((key >> d) & 1) as usize;
107            if self.nodes[node].child[bit] == usize::MAX {
108                inserted = true;
109                let next = self.nodes.len();
110                self.nodes[node].child[bit] = next;
111                self.nodes.push(Node::new(node));
112            }
113            node = self.nodes[node].child[bit];
114        }
115
116        if inserted {
117            self.len += 1;
118        }
119        self.nodes[node].lazy = M::act_unit();
120        f(&mut self.nodes[node].agg);
121        self.recalc_up(node);
122    }

Auto Trait Implementations§

§

impl<M> Freeze for Node<M>
where <M as LazyMapMonoid>::Agg: Freeze, <M as LazyMapMonoid>::Act: Freeze,

§

impl<M> RefUnwindSafe for Node<M>

§

impl<M> Send for Node<M>
where <M as LazyMapMonoid>::Agg: Send, <M as LazyMapMonoid>::Act: Send,

§

impl<M> Sync for Node<M>
where <M as LazyMapMonoid>::Agg: Sync, <M as LazyMapMonoid>::Act: Sync,

§

impl<M> Unpin for Node<M>
where <M as LazyMapMonoid>::Agg: Unpin, <M as LazyMapMonoid>::Act: Unpin,

§

impl<M> UnsafeUnpin for Node<M>

§

impl<M> UnwindSafe for Node<M>

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.