interavl 0.6.0

An optimised interval tree for efficient interval stabbing
Documentation
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
use core::{fmt::Debug, ops::Range};

use crate::IntervalTree;

/// A view into a single entry in an [`IntervalTree`], which may either be
/// vacant or occupied.
///
/// This `enum` is constructed from the [`entry`] method on [`IntervalTree`].
///
/// [`entry`]: IntervalTree::entry
#[derive(Debug)]
pub enum Entry<'a, R, V>
where
    R: Ord + Clone + Debug,
{
    /// A vacant entry.
    Vacant(VacantEntry<'a, R, V>),
    /// An occupied entry.
    Occupied(OccupiedEntry<'a, R, V>),
}

/// A view into a vacant entry in an [`IntervalTree`].
/// It is part of the [`Entry`] enum.
#[derive(Debug)]
pub struct VacantEntry<'a, R, V>
where
    R: Ord + Clone + Debug,
{
    key: Range<R>,
    tree: &'a mut IntervalTree<R, V>,
}

/// A view into an occupied entry in an [`IntervalTree`].
/// It is part of the [`Entry`] enum.
#[derive(Debug)]
pub struct OccupiedEntry<'a, R, V>
where
    R: Ord + Clone + Debug,
{
    key: Range<R>,
    tree: &'a mut IntervalTree<R, V>,
}

impl<'a, R, V> VacantEntry<'a, R, V>
where
    R: Ord + Clone + Debug,
{
    /// Gets a reference to the key that would be used when inserting a value
    /// through the VacantEntry.
    #[inline]
    pub fn key(&self) -> &Range<R> {
        &self.key
    }

    /// Take ownership of the key.
    #[inline]
    pub fn into_key(self) -> Range<R> {
        self.key
    }

    /// Sets the value of the entry with the VacantEntry's key,
    /// and returns a mutable reference to it.
    #[inline]
    pub fn insert(self, value: V) -> &'a mut V {
        self.tree.insert(self.key.clone(), value);
        self.tree.get_mut(&self.key).unwrap()
    }
}

impl<'a, R, V> OccupiedEntry<'a, R, V>
where
    R: Ord + Clone + Debug,
{
    /// Gets a reference to the key in the entry.
    #[inline]
    pub fn key(&self) -> &Range<R> {
        &self.key
    }

    /// Gets a reference to the value in the entry.
    #[inline]
    pub fn get(&self) -> &V {
        self.tree.get(&self.key).unwrap()
    }

    /// Gets a mutable reference to the value in the entry.
    #[inline]
    pub fn get_mut(&mut self) -> &mut V {
        self.tree.get_mut(&self.key).unwrap()
    }

    /// Converts the entry into a mutable reference to its value.
    #[inline]
    pub fn into_mut(self) -> &'a mut V {
        self.tree.get_mut(&self.key).unwrap()
    }

    /// Sets the value of the entry with the OccupiedEntry's key,
    /// and returns the entry's old value.
    #[inline]
    pub fn insert(&mut self, value: V) -> V {
        self.tree.insert(self.key.clone(), value).unwrap()
    }

    /// Takes the value of the entry out of the tree, and returns it.
    #[inline]
    pub fn remove(self) -> V {
        self.tree.remove(&self.key).unwrap()
    }
}

impl<'a, R, V> Entry<'a, R, V>
where
    R: Ord + Clone + Debug,
{
    /// Create a new Entry for the given key and tree.
    pub(crate) fn new(key: Range<R>, tree: &'a mut IntervalTree<R, V>) -> Self {
        if tree.contains_key(&key) {
            Entry::Occupied(OccupiedEntry { key, tree })
        } else {
            Entry::Vacant(VacantEntry { key, tree })
        }
    }

    /// Returns a reference to this entry's key.
    ///
    /// # Examples
    ///
    /// ```
    /// use interavl::IntervalTree;
    ///
    /// let mut tree: IntervalTree<i32, &str> = IntervalTree::default();
    /// assert_eq!(tree.entry(0..10).key(), &(0..10));
    /// ```
    #[inline]
    pub fn key(&self) -> &Range<R> {
        match self {
            Entry::Vacant(entry) => entry.key(),
            Entry::Occupied(entry) => entry.key(),
        }
    }

    /// Ensures a value is in the entry by inserting the default if empty,
    /// and returns a mutable reference to the value in the entry.
    ///
    /// # Examples
    ///
    /// ```
    /// use interavl::IntervalTree;
    ///
    /// let mut tree: IntervalTree<i32, u32> = IntervalTree::default();
    ///
    /// tree.entry(0..10).or_insert(42);
    /// assert_eq!(tree.get(&(0..10)), Some(&42));
    ///
    /// *tree.entry(0..10).or_insert(100) += 1;
    /// assert_eq!(tree.get(&(0..10)), Some(&43));
    /// ```
    #[inline]
    pub fn or_insert(self, default: V) -> &'a mut V {
        match self {
            Entry::Occupied(entry) => entry.into_mut(),
            Entry::Vacant(entry) => entry.insert(default),
        }
    }

    /// Ensures a value is in the entry by inserting the result of the default
    /// function if empty, and returns a mutable reference to the value in the
    /// entry.
    ///
    /// # Examples
    ///
    /// ```
    /// use interavl::IntervalTree;
    ///
    /// let mut tree: IntervalTree<i32, String> = IntervalTree::default();
    /// let s = "hello".to_string();
    ///
    /// tree.entry(0..10).or_insert_with(|| s);
    ///
    /// assert_eq!(tree.get(&(0..10)), Some(&"hello".to_string()));
    /// ```
    #[inline]
    pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
        match self {
            Entry::Occupied(entry) => entry.into_mut(),
            Entry::Vacant(entry) => entry.insert(default()),
        }
    }

    /// Ensures a value is in the entry by inserting, if empty, the result of
    /// the default function. This method allows for generating key-derived
    /// values for insertion by providing the default function a reference to
    /// the key that was moved during the `.entry(key)` method call.
    ///
    /// # Examples
    ///
    /// ```
    /// use interavl::IntervalTree;
    ///
    /// let mut tree: IntervalTree<i32, i32> = IntervalTree::default();
    ///
    /// tree.entry(0..10).or_insert_with_key(|key| key.start + key.end);
    ///
    /// assert_eq!(tree.get(&(0..10)), Some(&10));
    /// ```
    #[inline]
    pub fn or_insert_with_key<F: FnOnce(&Range<R>) -> V>(self, default: F) -> &'a mut V {
        match self {
            Entry::Occupied(entry) => entry.into_mut(),
            Entry::Vacant(entry) => {
                let value = default(entry.key());
                entry.insert(value)
            }
        }
    }

    /// Provides in-place mutable access to an occupied entry before any
    /// potential inserts into the tree.
    ///
    /// # Examples
    ///
    /// ```
    /// use interavl::IntervalTree;
    ///
    /// let mut tree: IntervalTree<i32, u32> = IntervalTree::default();
    ///
    /// tree.entry(0..10)
    ///     .and_modify(|v| *v += 1)
    ///     .or_insert(42);
    /// assert_eq!(tree.get(&(0..10)), Some(&42));
    ///
    /// tree.entry(0..10)
    ///     .and_modify(|v| *v += 1)
    ///     .or_insert(42);
    /// assert_eq!(tree.get(&(0..10)), Some(&43));
    /// ```
    #[inline]
    pub fn and_modify<F: FnOnce(&mut V)>(mut self, f: F) -> Self {
        match &mut self {
            Entry::Occupied(entry) => {
                f(entry.get_mut());
            }
            Entry::Vacant(_) => {}
        }
        self
    }

    /// Sets the value of the entry, and returns an OccupiedEntry.
    ///
    /// # Examples
    ///
    /// ```
    /// use interavl::IntervalTree;
    ///
    /// let mut tree: IntervalTree<i32, &str> = IntervalTree::default();
    /// let entry = tree.entry(0..10).insert_entry("hello");
    ///
    /// assert_eq!(entry.key(), &(0..10));
    /// ```
    #[inline]
    pub fn insert_entry(self, value: V) -> OccupiedEntry<'a, R, V> {
        match self {
            Entry::Occupied(mut entry) => {
                entry.insert(value);
                entry
            }
            Entry::Vacant(entry) => {
                let key = entry.key.clone();
                entry.tree.insert(key.clone(), value);
                OccupiedEntry {
                    key,
                    tree: entry.tree,
                }
            }
        }
    }
}

impl<'a, R, V> Entry<'a, R, V>
where
    R: Ord + Clone + Debug,
    V: Default,
{
    /// Ensures a value is in the entry by inserting the default value if empty,
    /// and returns a mutable reference to the value in the entry.
    ///
    /// # Examples
    ///
    /// ```
    /// use interavl::IntervalTree;
    ///
    /// let mut tree: IntervalTree<i32, Option<u32>> = IntervalTree::default();
    /// tree.entry(0..10).or_default();
    ///
    /// assert_eq!(tree.get(&(0..10)), Some(&None));
    /// ```
    #[inline]
    pub fn or_default(self) -> &'a mut V {
        match self {
            Entry::Occupied(entry) => entry.into_mut(),
            Entry::Vacant(entry) => entry.insert(V::default()),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::{
        assert, assert_eq,
        prelude::v1::{test, Default, None, Option, Some, String, ToString},
    };

    #[test]
    fn test_entry_or_insert() {
        let mut tree: IntervalTree<i32, i32> = IntervalTree::default();

        // Insert via vacant entry
        tree.entry(0..10).or_insert(42);
        assert_eq!(tree.get(&(0..10)), Some(&42));

        // Entry is now occupied, should not change
        tree.entry(0..10).or_insert(100);
        assert_eq!(tree.get(&(0..10)), Some(&42));
    }

    #[test]
    fn test_entry_or_insert_with() {
        let mut tree: IntervalTree<i32, String> = IntervalTree::default();

        tree.entry(0..10).or_insert_with(|| "hello".to_string());
        assert_eq!(tree.get(&(0..10)), Some(&"hello".to_string()));

        // Should not call the closure again
        tree.entry(0..10).or_insert_with(|| "world".to_string());
        assert_eq!(tree.get(&(0..10)), Some(&"hello".to_string()));
    }

    #[test]
    fn test_entry_or_insert_with_key() {
        let mut tree: IntervalTree<i32, i32> = IntervalTree::default();

        tree.entry(0..10)
            .or_insert_with_key(|key| key.start + key.end);
        assert_eq!(tree.get(&(0..10)), Some(&10));
    }

    #[test]
    fn test_entry_and_modify() {
        let mut tree: IntervalTree<i32, u32> = IntervalTree::default();

        // On vacant, and_modify should not do anything
        tree.entry(0..10).and_modify(|v| *v += 1).or_insert(42);
        assert_eq!(tree.get(&(0..10)), Some(&42));

        // On occupied, and_modify should modify the value
        tree.entry(0..10).and_modify(|v| *v += 1).or_insert(100);
        assert_eq!(tree.get(&(0..10)), Some(&43));
    }

    #[test]
    fn test_entry_insert_entry() {
        let mut tree: IntervalTree<i32, &str> = IntervalTree::default();

        // Insert on vacant
        let entry = tree.entry(0..10).insert_entry("hello");
        assert_eq!(entry.key(), &(0..10));
        assert_eq!(entry.get(), &"hello");

        // Insert on occupied - should replace
        let entry = tree.entry(0..10).insert_entry("world");
        assert_eq!(entry.key(), &(0..10));
        assert_eq!(entry.get(), &"world");
    }

    #[test]
    fn test_entry_or_default() {
        let mut tree: IntervalTree<i32, Option<u32>> = IntervalTree::default();

        tree.entry(0..10).or_default();
        assert_eq!(tree.get(&(0..10)), Some(&None));
    }

    #[test]
    fn test_entry_key() {
        let mut tree: IntervalTree<i32, i32> = IntervalTree::default();

        let entry = tree.entry(0..10);
        assert_eq!(entry.key(), &(0..10));
    }

    #[test]
    fn test_vacant_entry_into_key() {
        let mut tree: IntervalTree<i32, i32> = IntervalTree::default();

        let entry = tree.entry(0..10);
        match entry {
            Entry::Vacant(vacant) => {
                let key = vacant.into_key();
                assert_eq!(key, 0..10);
            }
            Entry::Occupied(_) => panic!("Expected vacant entry"),
        }
    }

    #[test]
    fn test_occupied_entry_remove() {
        let mut tree: IntervalTree<i32, i32> = IntervalTree::default();
        tree.insert(0..10, 42);

        let entry = tree.entry(0..10);
        match entry {
            Entry::Occupied(occupied) => {
                let value = occupied.remove();
                assert_eq!(value, 42);
            }
            Entry::Vacant(_) => panic!("Expected occupied entry"),
        }

        assert!(!tree.contains_key(&(0..10)));
    }

    #[test]
    fn test_occupied_entry_insert() {
        let mut tree: IntervalTree<i32, i32> = IntervalTree::default();
        tree.insert(0..10, 42);

        let entry = tree.entry(0..10);
        match entry {
            Entry::Occupied(mut occupied) => {
                let old = occupied.insert(100);
                assert_eq!(old, 42);
                assert_eq!(occupied.get(), &100);
            }
            Entry::Vacant(_) => panic!("Expected occupied entry"),
        }
    }
}