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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
use std::borrow::Borrow;
use std::fmt;
use std::iter::FromIterator;
use std::ops::{Index, IndexMut};

use entry::{make_entry, Entry};
use iter::{IntoIter, Iter, IterMut, Keys, Values, ValuesMut};
use node::{Leaf, Node};
use subtrie::SubTrie;
use util::nybble_mismatch;
use wrapper::{BStr, BString};

/// A QP-trie. QP stands for - depending on who you ask - either "quelques-bits popcount" or
/// "quad-bit popcount". In any case, the fact of the matter is that this is a compressed radix
/// trie with a branching factor of 16. It acts as a key-value map where the keys are any value
/// which can be converted to a slice of bytes.
///
/// The following example uses the provided string wrapper. Unfortunately, `String`/`str` cannot be
/// used directly because they do not implement `Borrow<[u8]>` (as they do not hash the same way as
/// a byte slice.) As a stopgap, `qp_trie::wrapper::{BString, BStr}` are provided, as are the
/// `.whatever_str()` convenience methods on `qp_trie::Trie<BString, _>`.
///
/// # Example
///
/// ```rust
/// # use qp_trie::Trie;
///
/// let mut trie = Trie::new();
///
/// trie.insert_str("abbc", 1);
/// trie.insert_str("abcd", 2);
/// trie.insert_str("bcde", 3);
/// trie.insert_str("bdde", 4);
/// trie.insert_str("bddf", 5);
///
/// // This will print the following string:
/// //
/// // `{"abbc": 1, "abcd": 2, "bcde": 3, "bdde": 4, "bddf": 5}`
///
/// println!("{:?}", trie);
/// # assert_eq!(format!("{:?}", trie), "{\"abbc\": 1, \"abcd\": 2, \"bcde\": 3, \"bdde\": 4, \"bddf\": 5}");
///
/// assert_eq!(trie.get_str("abcd"), Some(&2));
/// assert_eq!(trie.get_str("bcde"), Some(&3));
///
/// // We can take subtries, removing all elements of the trie with a given prefix.
/// let mut subtrie = trie.remove_prefix_str("b");
///
/// assert_eq!(trie.get_str("abbc"), Some(&1));
/// assert_eq!(trie.get_str("abcd"), Some(&2));
/// assert_eq!(trie.get_str("bcde"), None);
/// assert_eq!(trie.get_str("bdde"), None);
/// assert_eq!(trie.get_str("bddf"), None);
///
/// assert_eq!(subtrie.get_str("abbc"), None);
/// assert_eq!(subtrie.get_str("abcd"), None);
/// assert_eq!(subtrie.get_str("bcde"), Some(&3));
/// assert_eq!(subtrie.get_str("bdde"), Some(&4));
/// assert_eq!(subtrie.get_str("bddf"), Some(&5));
///
/// // We can remove elements:
/// assert_eq!(trie.remove_str("abbc"), Some(1));
/// assert_eq!(trie.get_str("abbc"), None);
///
/// // We can mutate values:
/// *subtrie.get_mut_str("bdde").unwrap() = 0;
/// assert_eq!(subtrie.get_str("bdde"), Some(&0));
/// ```
#[derive(Clone, PartialEq, Eq)]
pub struct Trie<K, V> {
    root: Option<Node<K, V>>,
    count: usize,
}

impl<K, V> Default for Trie<K, V> {
    fn default() -> Self {
        Trie::new()
    }
}

impl<K: fmt::Debug + ToOwned, V: fmt::Debug> fmt::Debug for Trie<K, V> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.root {
            Some(ref node) => f.debug_map().entries(node.iter()).finish(),
            None => f.debug_map().finish(),
        }
    }
}

impl<K, V> IntoIterator for Trie<K, V> {
    type IntoIter = IntoIter<K, V>;
    type Item = (K, V);

    fn into_iter(self) -> Self::IntoIter {
        self.root
            .map(Node::into_iter)
            .unwrap_or_else(IntoIter::default)
    }
}

impl<K: Borrow<[u8]>, V> FromIterator<(K, V)> for Trie<K, V> {
    fn from_iter<I>(iterable: I) -> Trie<K, V>
    where
        I: IntoIterator<Item = (K, V)>,
    {
        let mut trie = Trie::new();

        for (key, val) in iterable {
            trie.insert(key, val);
        }

        trie
    }
}

impl<K: Borrow<[u8]>, V> Extend<(K, V)> for Trie<K, V> {
    fn extend<I>(&mut self, iterable: I)
    where
        I: IntoIterator<Item = (K, V)>,
    {
        for (key, val) in iterable {
            self.insert(key, val);
        }
    }
}

impl<K, V> Trie<K, V> {
    /// Create a new, empty trie.
    pub fn new() -> Trie<K, V> {
        Trie {
            root: None,
            count: 0,
        }
    }

    /// Iterate over all elements in the trie.
    pub fn iter(&self) -> Iter<K, V> {
        match self.root {
            Some(ref node) => Iter::new(node),
            None => Iter::default(),
        }
    }

    /// Iterate over all elements in the trie, given a mutable reference to the associated value.
    pub fn iter_mut(&mut self) -> IterMut<K, V> {
        match self.root {
            Some(ref mut node) => IterMut::new(node),
            None => IterMut::default(),
        }
    }

    /// Iterate over all keys in the trie.
    pub fn keys(&self) -> Keys<K, V> {
        match self.root {
            Some(ref node) => Keys::new(node),
            None => Keys::default(),
        }
    }

    /// Iterate over all values in the trie.
    pub fn values(&self) -> Values<K, V> {
        match self.root {
            Some(ref node) => Values::new(node),
            None => Values::default(),
        }
    }

    /// Iterate over all values in the trie, mutably.
    pub fn values_mut(&mut self) -> ValuesMut<K, V> {
        match self.root {
            Some(ref mut node) => ValuesMut::new(node),
            None => ValuesMut::default(),
        }
    }

    /// Remove all entries from the trie, leaving it empty.
    pub fn clear(&mut self) {
        self.root = None;
    }

    /// Returns true if the trie has no entries.
    pub fn is_empty(&self) -> bool {
        self.root.is_none()
    }
}

impl<K: Borrow<[u8]>, V> Trie<K, V> {
    /// Iterate over all elements with a given prefix.
    pub fn iter_prefix<'a, Q: ?Sized>(&self, prefix: &'a Q) -> Iter<K, V>
    where
        K: Borrow<Q>,
        Q: Borrow<[u8]>,
    {
        match self
            .root
            .as_ref()
            .and_then(|node| node.get_prefix(prefix.borrow()))
        {
            Some(node) => Iter::new(node),
            None => Iter::default(),
        }
    }

    /// Iterate over all elements with a given prefix, but given a mutable reference to the
    /// associated value.
    pub fn iter_prefix_mut<'a, Q: ?Sized>(&mut self, prefix: &'a Q) -> IterMut<K, V>
    where
        K: Borrow<Q>,
        Q: Borrow<[u8]>,
    {
        match self
            .root
            .as_mut()
            .and_then(|node| node.get_prefix_mut(prefix.borrow()))
        {
            Some(node) => IterMut::new(node),
            None => IterMut::default(),
        }
    }

    /// Get an immutable view into the trie, providing only values keyed with the given prefix.
    pub fn subtrie<'a, Q: ?Sized>(&self, prefix: &'a Q) -> SubTrie<K, V>
    where
        K: Borrow<Q>,
        Q: Borrow<[u8]>,
    {
        SubTrie {
            root: self
                .root
                .as_ref()
                .and_then(|node| node.get_prefix(prefix.borrow())),
        }
    }

    /// Get the longest common prefix of all the nodes in the trie and the given key.
    pub fn longest_common_prefix<'a, Q: ?Sized>(&self, key: &'a Q) -> &K::Split
    where
        K: Borrow<Q> + Break,
        Q: Borrow<[u8]>,
    {
        match self.root.as_ref() {
            Some(root) => {
                let exemplar = root.get_exemplar(key.borrow());

                match nybble_mismatch(exemplar.key_slice(), key.borrow()) {
                    Some(i) => exemplar.key.find_break(i / 2),
                    None => exemplar.key.borrow(),
                }
            }
            None => K::empty(),
        }
    }

    /// Count the number of entries in the tree.
    pub fn count(&self) -> usize {
        self.count
    }

    /// Returns true if there is an entry for the given key.
    pub fn contains_key<'a, Q: ?Sized>(&self, key: &'a Q) -> bool
    where
        K: Borrow<Q>,
        Q: Borrow<[u8]>,
    {
        self.root
            .as_ref()
            .and_then(|node| node.get(key.borrow()))
            .is_some()
    }

    /// Get an immutable reference to the value associated with a given key, if it is in the tree.
    pub fn get<'a, Q: ?Sized>(&self, key: &'a Q) -> Option<&V>
    where
        K: Borrow<Q>,
        Q: Borrow<[u8]>,
    {
        self.root
            .as_ref()
            .and_then(|node| node.get(key.borrow()))
            .map(|leaf| &leaf.val)
    }

    /// Get a mutable reference to the value associated with a given key, if it is in the tree.
    pub fn get_mut<'a, Q: ?Sized>(&mut self, key: &'a Q) -> Option<&mut V>
    where
        K: Borrow<Q>,
        Q: Borrow<[u8]>,
    {
        self.root
            .as_mut()
            .and_then(|node| node.get_mut(key.borrow()))
            .map(|leaf| &mut leaf.val)
    }

    /// Insert a key/value pair into the trie, returning the old value if an entry already existed.
    pub fn insert(&mut self, key: K, val: V) -> Option<V> {
        match self.root {
            Some(ref mut root) => {
                let old = root.insert(key, val);
                if old.is_none() {
                    self.count += 1;
                }
                old
            }
            None => {
                self.root = Some(Node::Leaf(Leaf::new(key, val)));
                self.count += 1;
                None
            }
        }
    }

    /// Remove the key/value pair associated with a given key from the trie, returning
    /// `Some(val)` if a corresponding key/value pair was found.
    pub fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V>
    where
        K: Borrow<Q>,
        Q: Borrow<[u8]>,
    {
        let node = Node::remove(&mut self.root, key.borrow()).map(|leaf| leaf.val);
        if node.is_some() {
            self.count -= 1;
        }
        node
    }

    /// Remove all elements beginning with a given prefix from the trie, producing a subtrie.
    pub fn remove_prefix<'a, Q: ?Sized>(&mut self, prefix: &'a Q) -> Trie<K, V>
    where
        K: Borrow<Q>,
        Q: Borrow<[u8]>,
    {
        let root = Node::remove_prefix(&mut self.root, prefix.borrow());
        let count = root.as_ref().map(Node::count).unwrap_or(0);
        self.count -= count;
        Trie { root, count }
    }

    /// Get the corresponding entry for the given key.
    pub fn entry(&mut self, key: K) -> Entry<K, V> {
        make_entry(key, &mut self.root)
    }
}

impl<'a, K: Borrow<[u8]>, V, Q: ?Sized> Index<&'a Q> for Trie<K, V>
where
    K: Borrow<Q>,
    Q: Borrow<[u8]>,
{
    type Output = V;

    fn index(&self, key: &Q) -> &V {
        self.get(key).unwrap()
    }
}

impl<'a, K: Borrow<[u8]>, V, Q: ?Sized> IndexMut<&'a Q> for Trie<K, V>
where
    K: Borrow<Q>,
    Q: Borrow<[u8]>,
{
    fn index_mut(&mut self, key: &Q) -> &mut V {
        self.get_mut(key).unwrap()
    }
}

pub trait Break: Borrow<<Self as Break>::Split> {
    type Split: ?Sized;

    fn empty<'a>() -> &'a Self::Split;
    fn find_break(&self, loc: usize) -> &Self::Split;
}

impl Break for [u8] {
    type Split = [u8];

    #[inline]
    fn empty<'a>() -> &'a [u8] {
        <&'a [u8]>::default()
    }

    #[inline]
    fn find_break(&self, loc: usize) -> &[u8] {
        &self[..loc]
    }
}

impl<'b> Break for &'b [u8] {
    type Split = [u8];

    #[inline]
    fn empty<'a>() -> &'a [u8] {
        <&'a [u8]>::default()
    }

    #[inline]
    fn find_break(&self, loc: usize) -> &[u8] {
        &self[..loc]
    }
}

impl<V> Trie<BString, V> {
    /// Convenience function for iterating over suffixes with a string.
    pub fn iter_prefix_str<'a, Q: ?Sized>(&self, key: &'a Q) -> Iter<BString, V>
    where
        Q: Borrow<str>,
    {
        self.iter_prefix(AsRef::<BStr>::as_ref(key.borrow()))
    }

    /// Convenience function for iterating over suffixes with a string.
    pub fn iter_prefix_mut_str<'a, Q: ?Sized>(&mut self, key: &'a Q) -> IterMut<BString, V>
    where
        Q: Borrow<str>,
    {
        self.iter_prefix_mut(AsRef::<BStr>::as_ref(key.borrow()))
    }

    /// Convenience function for viewing subtries wit a string prefix.
    pub fn subtrie_str<'a, Q: ?Sized>(&self, prefix: &'a Q) -> SubTrie<BString, V>
    where
        Q: Borrow<str>,
    {
        self.subtrie(AsRef::<BStr>::as_ref(prefix.borrow()))
    }

    /// Returns true if there is an entry for the given string key.
    pub fn contains_key_str<'a, Q: ?Sized>(&self, key: &'a Q) -> bool
    where
        Q: Borrow<str>,
    {
        self.contains_key(AsRef::<BStr>::as_ref(key.borrow()))
    }

    /// Convenience function for getting with a string.
    pub fn get_str<'a, Q: ?Sized>(&self, key: &'a Q) -> Option<&V>
    where
        Q: Borrow<str>,
    {
        self.get(AsRef::<BStr>::as_ref(key.borrow()))
    }

    /// Convenience function for getting mutably with a string.
    pub fn get_mut_str<'a, Q: ?Sized>(&mut self, key: &'a Q) -> Option<&mut V>
    where
        Q: Borrow<str>,
    {
        self.get_mut(AsRef::<BStr>::as_ref(key.borrow()))
    }

    /// Convenience function for inserting with a string.
    pub fn insert_str<'a, Q: ?Sized>(&mut self, key: &'a Q, val: V) -> Option<V>
    where
        Q: Borrow<str>,
    {
        self.insert(key.borrow().into(), val)
    }

    /// Convenience function for removing with a string.
    pub fn remove_str<'a, Q: ?Sized>(&mut self, key: &'a Q) -> Option<V>
    where
        Q: Borrow<str>,
    {
        self.remove(AsRef::<BStr>::as_ref(key.borrow()))
    }

    /// Convenience function for removing a prefix with a string.
    pub fn remove_prefix_str<'a, Q: ?Sized>(&mut self, prefix: &'a Q) -> Trie<BString, V>
    where
        Q: Borrow<str>,
    {
        self.remove_prefix(AsRef::<BStr>::as_ref(prefix.borrow()))
    }
}