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
// 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 crate::{
    config::{ConsoliableWeight, KeyMngTrait},
    search::{accumulate_weight_search, SearchDirection},
    update::{ApplyOp, ApplyOpOutcome, InsertOp, RemoveOp},
    Direction, NoWeight, SearchResult,
};

use super::{config::TreapMapConfig, node::Node};
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use rand::{RngCore, SeedableRng};
use rand_xorshift::XorShiftRng;

/// A treap map data structure.
///
/// See [`TreapMapConfig`][crate::TreapMapConfig] for more details.
pub struct TreapMap<C: TreapMapConfig> {
    /// The root node of the treap.
    #[cfg(test)]
    pub(crate) root: Option<Box<Node<C>>>,
    #[cfg(not(test))]
    root: Option<Box<Node<C>>>,

    /// A map for recovering the `sort_key` from the `search_key`.
    /// This is useful when the `sort_key` is derived from `search_key` and
    /// `value`.
    ext_map: C::ExtMap,

    /// A random number generator used for generating priority values for new
    /// nodes.
    rng: XorShiftRng,
}

impl<C: TreapMapConfig> MallocSizeOf for TreapMap<C>
where
    Node<C>: MallocSizeOf,
    C::ExtMap: MallocSizeOf,
{
    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
        self.root.size_of(ops) + self.ext_map.size_of(ops)
    }
}

impl<C: TreapMapConfig> TreapMap<C> {
    pub fn new() -> TreapMap<C> {
        TreapMap {
            root: None,
            rng: XorShiftRng::from_entropy(),
            ext_map: Default::default(),
        }
    }

    pub fn new_with_rng(rng: XorShiftRng) -> TreapMap<C> {
        TreapMap {
            root: None,
            rng,
            ext_map: Default::default(),
        }
    }

    pub fn len(&self) -> usize { self.ext_map.len() }

    pub fn is_empty(&self) -> bool { self.ext_map.len() == 0 }

    pub fn contains_key(&self, key: &C::SearchKey) -> bool {
        self.get(key).is_some()
    }

    pub fn insert(
        &mut self, key: C::SearchKey, value: C::Value, weight: C::Weight,
    ) -> Option<C::Value> {
        let sort_key = self.ext_map.make_sort_key(&key, &value);

        let node = Node::new(key, value, sort_key, weight, self.rng.next_u64());

        let (result, _, _) = Node::update_inner(
            &mut self.root,
            InsertOp {
                node: Box::new(node),
                ext_map: &mut self.ext_map,
            },
        );

        result
    }

    pub fn remove(&mut self, key: &C::SearchKey) -> Option<C::Value> {
        let sort_key = self.ext_map.get_sort_key(&key)?;

        let (result, _, _) = Node::update_inner(
            &mut self.root,
            RemoveOp {
                key: (&sort_key, key),
                ext_map: &mut self.ext_map,
            },
        );

        result
    }

    /// Updates the value of a node with the given key in the treap map.
    ///
    /// # Parameters
    /// - `key`: The search key of the node to be updated.
    /// - `update`: A function that is called if a node with the given key
    ///   already exists. It takes a mutable reference to the node and returns
    ///   an `ApplyOpOutcome<T>` or a custom error `E`. See
    ///   [`ApplyOpOutcome`][crate::ApplyOpOutcome] for more details.
    /// - `insert`: A function that is called if a node with the given key does
    ///   not exist. It takes a mutable reference to a random number generator
    ///   (for computing priority for a [`Node`][crate::Node]) and should return
    ///   a tuple containing a new `Node<C>` and a value of type `T`, or an
    ///   error of type `E`.
    ///   - WARNING: The key of the new node must match the key provided to the
    ///     function.
    pub fn update<U, I, T, E>(
        &mut self, key: &C::SearchKey, update: U, insert: I,
    ) -> Result<T, E>
    where
        U: FnOnce(&mut Node<C>) -> Result<ApplyOpOutcome<T>, E>,
        I: FnOnce(&mut dyn RngCore) -> Result<(Node<C>, T), E>,
    {
        let sort_key = if let Some(sort_key) = self.ext_map.get_sort_key(key) {
            sort_key
        } else {
            return match insert(&mut self.rng) {
                Ok((node, ret)) => {
                    self.insert(node.key, node.value, node.weight);
                    Ok(ret)
                }
                Err(err) => Err(err),
            };
        };
        let rng = &mut self.rng;
        let (res, _, _) = Node::update_inner(
            &mut self.root,
            ApplyOp {
                key: (&sort_key, key),
                update,
                insert: || insert(rng),
                ext_map: &mut self.ext_map,
            },
        );
        let (ret, maybe_node) = res?;
        if let Some(node) = maybe_node {
            self.insert(node.key, node.value, node.weight);
        }
        Ok(ret)
    }

    pub fn sum_weight(&self) -> C::Weight {
        match &self.root {
            Some(node) => node.sum_weight(),
            None => C::Weight::empty(),
        }
    }

    pub fn get(&self, key: &C::SearchKey) -> Option<&C::Value> {
        let sort_key = self.ext_map.get_sort_key(key)?;
        self.root.as_ref().and_then(|x| x.get(&sort_key, key))
    }

    #[inline]
    pub fn get_by_weight(&self, weight: C::Weight) -> Option<&C::Value>
    where C::Weight: Ord {
        use SearchDirection::*;
        self.search(|base, mid| {
            if &weight < base {
                Left
            } else {
                let right_base = C::Weight::consolidate(base, &mid.weight);
                if weight < right_base {
                    Stop
                } else {
                    Right(right_base)
                }
            }
        })?
        .maybe_value()
    }

    /// See details in [`crate::accumulate_weight_search`]
    pub fn search<F>(&self, f: F) -> Option<SearchResult<C, C::Weight>>
    where F: FnMut(&C::Weight, &Node<C>) -> SearchDirection<C::Weight> {
        Some(accumulate_weight_search(self.root.as_ref()?, f, |weight| {
            weight
        }))
    }

    /// See details in [`crate::accumulate_weight_search`]
    /// If the search process does not require accessing 'weight', this function
    /// can outperform `search` by eliminating the maintenance of the 'weight'
    /// dimension.
    pub fn search_no_weight<F>(
        &self, mut f: F,
    ) -> Option<SearchResult<C, NoWeight>>
    where F: FnMut(&Node<C>) -> SearchDirection<()> {
        static NW: NoWeight = NoWeight;
        Some(accumulate_weight_search(
            self.root.as_ref()?,
            |_, node| f(node).map_into(|_| NoWeight),
            |_| &NW,
        ))
    }

    pub fn iter(&self) -> Iter<C> {
        let mut iter = Iter { nodes: vec![] };
        if let Some(ref n) = self.root {
            iter.nodes.push(&**n);
            iter.extend_path();
        }
        iter
    }

    pub fn iter_range(&self, key: &C::SearchKey) -> Iter<C>
    where C: TreapMapConfig<SortKey = ()> {
        let mut iter = Iter { nodes: vec![] };
        if let Some(ref n) = self.root {
            iter.nodes.push(&**n);
            iter.extend_path_with_key((&(), key));
        }
        iter
    }

    pub fn values(&self) -> impl Iterator<Item = &C::Value> {
        self.iter().map(|node| &node.value)
    }

    pub fn key_values(
        &self,
    ) -> impl Iterator<Item = (&C::SearchKey, &C::Value)> {
        self.iter().map(|node| (&node.key, &node.value))
    }

    #[cfg(any(test, feature = "testonly_code"))]
    pub fn assert_consistency(&self)
    where C::Weight: std::fmt::Debug {
        if let Some(node) = self.root.as_ref() {
            node.assert_consistency()
        }
    }
}

pub struct Iter<'a, C: TreapMapConfig> {
    nodes: Vec<&'a Node<C>>,
}

impl<'a, C: TreapMapConfig> Clone for Iter<'a, C> {
    fn clone(&self) -> Self {
        Self {
            nodes: self.nodes.clone(),
        }
    }
}

impl<'a, C: TreapMapConfig> Iter<'a, C> {
    fn extend_path(&mut self) {
        loop {
            let node = *self.nodes.last().unwrap();
            match node.left {
                None => return,
                Some(ref n) => self.nodes.push(&**n),
            }
        }
    }

    fn extend_path_with_key(&mut self, key: (&C::SortKey, &C::SearchKey)) {
        loop {
            let node = *self.nodes.last().unwrap();
            match C::next_node_dir(key, (&node.sort_key, &node.key)) {
                Some(Direction::Left) => {
                    if let Some(left) = &node.left {
                        self.nodes.push(left);
                    } else {
                        return;
                    }
                }
                None => {
                    return;
                }
                Some(Direction::Right) => {
                    let node = self.nodes.pop().unwrap();
                    if let Some(right) = &node.right {
                        self.nodes.push(right);
                    } else {
                        return;
                    }
                }
            }
        }
    }
}

impl<'a, C: TreapMapConfig> Iterator for Iter<'a, C> {
    type Item = &'a Node<C>;

    fn next(&mut self) -> Option<Self::Item> {
        match self.nodes.pop() {
            None => None,
            Some(node) => {
                if let Some(ref n) = node.right {
                    self.nodes.push(&**n);
                    self.extend_path();
                }
                Some(&node)
            }
        }
    }
}