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::ActImplementations§
Source§impl<M> Node<M>where
M: LazyMapMonoid,
impl<M> Node<M>where
M: LazyMapMonoid,
Sourcefn new(parent: usize) -> Self
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>
impl<M> RefUnwindSafe for Node<M>
impl<M> Send for Node<M>
impl<M> Sync for Node<M>
impl<M> Unpin for Node<M>
impl<M> UnsafeUnpin for Node<M>
impl<M> UnwindSafe for Node<M>
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