1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright 2019 Conflux Foundation. All rights reserved.
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/

use super::{
    config::{ConsoliableWeight, Direction, TreapMapConfig},
    update::{OpResult, TreapNodeUpdate},
};

use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use std::mem;

/// A node in a treap-map data structure.
///
/// The `Node` struct represents a node in a treap-map and contains various
/// key-value pairs and metadata required for the proper functioning and
/// maintenance of the treap-map. Direct modification of these fields is not
/// recommended outside of the `TreapMap::update` function, as this function
/// correctly maintains the integrity of the treap-map.
#[cfg_attr(test, derive(Debug, PartialEq, Eq))]
pub struct Node<C: TreapMapConfig> {
    /// The key exposed externally. Used for key-based searches within the
    /// treap-map.
    pub key: C::SearchKey,

    /// The value stored in the node.
    pub value: C::Value,

    /// The sorting key for the treap-map. If the type is `()`, the
    /// `search_key` is used for sorting.
    pub sort_key: C::SortKey,

    /// The weight of the node, used by the treap-map to maintain accumulated
    /// weights.
    pub weight: C::Weight,

    /// The sum of the weights of this node and its descendants. Maintained
    /// internally for efficient operations.
    pub(crate) sum_weight: C::Weight,

    /// A priority value used by the treap algorithm, typically a random
    /// number.
    priority: u64,

    /// The left child of the node in the treap-map structure.
    pub(crate) left: Option<Box<Node<C>>>,

    /// The right child of the node in the treap-map structure.
    pub(crate) right: Option<Box<Node<C>>>,
}

impl<C: TreapMapConfig> Node<C> {
    pub fn new(
        key: C::SearchKey, value: C::Value, sort_key: C::SortKey,
        weight: C::Weight, priority: u64,
    ) -> Node<C> {
        Node {
            key,
            value,
            sort_key,
            sum_weight: weight.clone(),
            weight,
            priority,
            left: None,
            right: None,
        }
    }

    pub(crate) fn get(
        &self, sort_key: &C::SortKey, key: &C::SearchKey,
    ) -> Option<&C::Value> {
        match C::next_node_dir((sort_key, key), (&self.sort_key, &self.key)) {
            None => Some(&self.value),
            Some(Direction::Left) => self.left.as_ref()?.get(sort_key, key),
            Some(Direction::Right) => self.right.as_ref()?.get(sort_key, key),
        }
    }

    pub(crate) fn update_inner<U: TreapNodeUpdate<C>>(
        maybe_node: &mut Option<Box<Node<C>>>, updater: U,
    ) -> (U::Ret, bool, bool) {
        // Compare the key
        let next_node_dir = if let Some(node) = maybe_node {
            C::next_node_dir(updater.treap_key(), (&node.sort_key, &node.key))
        } else {
            None
        };

        // Goto the next node or apply on the current node (if hit)
        let (ret, update_weight, mut update_priority) = match next_node_dir {
            None => {
                let update_result = updater.update_node(maybe_node.as_mut());
                Node::process_update_result::<U>(maybe_node, update_result)
            }

            Some(dir) => {
                let node = maybe_node.as_mut().unwrap();
                let next_node = match dir {
                    Direction::Left => &mut node.left,
                    Direction::Right => &mut node.right,
                };
                Node::update_inner(next_node, updater)
            }
        };

        // Update the sum_weight and priority inneeded.
        if let Some(node) = maybe_node.as_mut() {
            if update_weight {
                node.update_weight()
            }
            if let (Some(dir), true) = (next_node_dir, update_priority) {
                match dir {
                    Direction::Left
                        if node.left.as_ref().map_or(false, |left| {
                            node.priority < left.priority
                        }) =>
                    {
                        node.right_rotate();
                    }
                    Direction::Right
                        if node.right.as_ref().map_or(false, |right| {
                            node.priority < right.priority
                        }) =>
                    {
                        node.left_rotate();
                    }
                    _ => {
                        update_priority = false;
                    }
                }
            }
        }
        (ret, update_weight, update_priority)
    }

    fn process_update_result<U: TreapNodeUpdate<C>>(
        maybe_node: &mut Option<Box<Node<C>>>,
        result: OpResult<C, U::Ret, U::DeleteRet>,
    ) -> (U::Ret, bool, bool) {
        match result {
            OpResult::Noop(ret) => (ret, false, false),
            OpResult::Updated { update_weight, ret } => {
                (ret, update_weight, false)
            }
            OpResult::InsertOnVacant { insert, ret } => {
                // `maybe_node` should be empty here. So we ignore the replaced
                // value.
                let _ = mem::replace(maybe_node, Some(insert));
                (ret, true, true)
            }
            OpResult::Delete(delete_ret) => {
                let deleted_node = if maybe_node.is_some() {
                    Some(Node::delete(maybe_node))
                } else {
                    None
                };
                let ret = U::handle_delete(deleted_node, delete_ret);
                (ret, true, true)
            }
        }
    }

    // Rotate the current node to the leaf and delete it
    fn delete(mustbe_node: &mut Option<Box<Node<C>>>) -> Box<Self> {
        use Direction::*;
        let node = mustbe_node.as_mut().unwrap();
        let next_root = match (&node.left, &node.right) {
            (None, None) => {
                // Both is None, remove current node directly
                return mem::take(mustbe_node).unwrap();
            }
            (None, Some(_)) => Right,
            (Some(_), None) => Left,
            (Some(left), Some(right)) => {
                if left.priority < right.priority {
                    Right
                } else {
                    Left
                }
            }
        };

        let res = match next_root {
            Right => {
                // node.right must be `Some` before left rotate
                node.left_rotate();
                // node.left must be `Some` after left rotate
                Node::delete(&mut node.left)
            }
            Left => {
                // node.left must be `Some` before right rotate
                node.right_rotate();
                // node.right must be `Some` after right rotate
                Node::delete(&mut node.right)
            }
        };

        node.update_weight();
        res
    }

    //    X              Y
    //   / \            / \
    //  A   Y    <=    X   C
    //     / \        / \
    //    B   C      A   B

    fn right_rotate(self: &mut Box<Node<C>>) {
        //     Y  <- self
        //    / \
        //   X   C
        //  / \
        // A   B

        let y_node = self;
        let mut x_node = mem::take(&mut y_node.left).unwrap();

        //    Y  <- self      X
        //   / \             / \
        //  .   C           A   B

        // For efficiency, must swap pointer, instead of node data
        mem::swap::<Box<Node<C>>>(y_node, &mut x_node);
        let (x_node, mut y_node) = (y_node, x_node);

        //    Y               X  <- self
        //   / \             / \
        //  .   C           A   B

        mem::swap::<Option<Box<Node<C>>>>(&mut x_node.right, &mut y_node.left);

        //    Y               X  <- self
        //   / \             / \
        //  B   C           A   .

        y_node.update_weight();
        x_node.right = Some(y_node);

        //    X
        //   / \
        //  A   Y
        //     / \
        //    B   C

        x_node.update_weight();
    }

    //    X              Y
    //   / \            / \
    //  A   Y    =>    X   C
    //     / \        / \
    //    B   C      A   B
    fn left_rotate(self: &mut Box<Node<C>>) {
        //    X   <- self
        //   / \
        //  A   Y
        //     / \
        //    B   C

        let x_node = self;
        let mut y_node = mem::take(&mut x_node.right).unwrap();

        //    X  <- self      Y
        //   / \             / \
        //  A   .           B   C

        // For efficiency, must swap pointer, instead of node data
        mem::swap::<Box<Node<C>>>(x_node, &mut y_node);
        // Also swap variable name
        let (y_node, mut x_node) = (x_node, y_node);

        //    X               Y  <- self
        //   / \             / \
        //  A   .           B   C

        mem::swap::<Option<Box<Node<C>>>>(&mut y_node.left, &mut x_node.right);

        //    X               Y  <- self
        //   / \             / \
        //  A   B           .   C

        x_node.update_weight();
        y_node.left = Some(x_node);

        //      Y  <- self
        //     / \
        //    X   C
        //   / \
        //  A   B

        y_node.update_weight();
    }

    pub(crate) fn update_weight(&mut self) {
        self.sum_weight = self.weight.clone();
        if let Some(left) = &self.left {
            self.sum_weight.accure(&left.sum_weight);
        }
        if let Some(right) = &self.right {
            self.sum_weight.accure(&right.sum_weight);
        }
    }

    pub fn sum_weight(&self) -> C::Weight { self.sum_weight.clone() }

    #[cfg(any(test, feature = "testonly_code"))]
    pub(crate) fn assert_consistency(&self)
    where C::Weight: Eq + std::fmt::Debug {
        let mut weight = self.weight.clone();

        if let Some(left) = self.left.as_ref() {
            weight.accure(&left.sum_weight);
            assert!(left.priority <= self.priority);
            left.assert_consistency();
            assert_eq!(
                C::next_node_dir(
                    (&left.sort_key, &left.key),
                    (&self.sort_key, &self.key)
                ),
                Some(Direction::Left)
            );
        }
        if let Some(right) = self.right.as_ref() {
            weight.accure(&right.sum_weight);
            assert!(right.priority <= self.priority);
            right.assert_consistency();
            assert_eq!(
                C::next_node_dir(
                    (&right.sort_key, &right.key),
                    (&self.sort_key, &self.key)
                ),
                Some(Direction::Right)
            );
        }

        assert_eq!(weight, self.sum_weight);
    }
}

impl<C: TreapMapConfig> MallocSizeOf for Node<C>
where
    C::SearchKey: MallocSizeOf,
    C::SortKey: MallocSizeOf,
    C::Value: MallocSizeOf,
    C::Weight: MallocSizeOf,
{
    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
        self.key.size_of(ops)
            + self.sort_key.size_of(ops)
            + self.value.size_of(ops)
            + self.weight.size_of(ops)
            + self.sum_weight.size_of(ops)
            + self.left.size_of(ops)
            + self.right.size_of(ops)
    }
}