rust_lapper/
lib.rs

1//! This module provides a simple data-structure for fast interval searches.
2//! ## Features
3//! - Extremely fast on most genomic datasets. (3-4x faster than other methods)
4//! - Extremely fast on in order queries. (10x faster than other methods)
5//! - Extremely fast intersections count method based on the
6//! [BITS](https://arxiv.org/pdf/1208.3407.pdf) algorithm
7//! - Parallel friendly. Queries are on an immutable structure, even for seek
8//! - Consumer / Adapter paradigm, Iterators are returned and serve as the main API for interacting
9//! with the lapper
10//!
11//! ## Details:
12//!
13//! ```text
14//!       0  1  2  3  4  5  6  7  8  9  10 11
15//! (0,10]X  X  X  X  X  X  X  X  X  X
16//! (2,5]       X  X  X
17//! (3,8]          X  X  X  X  X
18//! (3,8]          X  X  X  X  X
19//! (3,8]          X  X  X  X  X
20//! (3,8]          X  X  X  X  X
21//! (5,9]                X  X  X  X
22//! (8,11]                        X  X  X
23//!
24//! Query: (8, 11]
25//! Answer: ((0,10], (5,9], (8,11])
26//! ```
27//!
28//! Most interaction with this crate will be through the [`Lapper`](struct.Lapper.html) struct
29//! The main methods are [`find`](struct.Lapper.html#method.find),
30//! [`seek`](struct.Lapper.html#method.seek), and [`count`](struct.Lapper.html#method.count)
31//! where both `seek` and `count` are special cases allowing for very fast queries in certain scenarios.
32//!
33//! The overlap function for this assumes a zero based genomic coordinate system. So [start, stop)
34//! is not inclusive of the stop position for neither the queries, nor the Intervals.
35//!
36//! Lapper does not use an interval tree, instead, it operates on the assumtion that most intervals are
37//! of similar length; or, more exactly, that the longest interval in the set is not long compred to
38//! the average distance between intervals.
39//!
40//! For cases where this holds true (as it often does with genomic data), we can sort by start and
41//! use binary search on the starts, accounting for the length of the longest interval. The advantage
42//! of this approach is simplicity of implementation and speed. In realistic tests queries returning
43//! the overlapping intervals are 1000 times faster than brute force and queries that merely check
44//! for the overlaps are > 5000 times faster.
45//!
46//! When this is not the case, if possible in your scenario, use merge_overlaps first, and then use
47//! `find` or `seek`. The `count` method will be fast in all scenarios.
48//!
49//! # Examples
50//!
51//! ```rust
52//!    use rust_lapper::{Interval, Lapper};
53//!    use std::cmp;
54//!    type Iv = Interval<usize, u32>;
55//!
56//!    // create some fake data
57//!    let data: Vec<Iv> = (0..20).step_by(5).map(|x| Iv{start: x, stop: x + 2, val: 0}).collect();
58//!    println!("{:#?}", data);
59//!
60//!    // make lapper structure
61//!    let laps = Lapper::new(data);
62//!
63//!    assert_eq!(laps.find(6, 11).next(), Some(&Iv{start: 5, stop: 7, val: 0}));
64//!
65//!    // Demonstration of seek function. By passing in the &mut cursor, seek can have thread local
66//!    // cursors going
67//!    let mut sim: usize = 0;
68//!    let mut cursor = 0;
69//!    // Calculate the overlap between the query and the found intervals, sum total overlap
70//!    for i in (0..10).step_by(3) {
71//!        sim += laps
72//!            .seek(i, i + 2, &mut cursor)
73//!            .map(|iv| cmp::min(i + 2, iv.stop) - cmp::max(i, iv.start))
74//!            .sum::<usize>();
75//!    }
76//!    assert_eq!(sim, 4);
77//! ```
78use num_traits::{
79    identities::{one, zero},
80    PrimInt, Unsigned,
81};
82use std::cmp::Ordering::{self};
83use std::collections::VecDeque;
84
85#[cfg(feature = "with_serde")]
86use serde::{Deserialize, Serialize};
87
88/// Represent a range from [start, stop)
89/// Inclusive start, exclusive of stop
90#[cfg_attr(feature = "with_serde", derive(Serialize, Deserialize))]
91#[derive(Eq, Debug, Clone)]
92pub struct Interval<I, T>
93where
94    I: PrimInt + Unsigned + Ord + Clone + Send + Sync,
95    T: Eq + Clone + Send + Sync,
96{
97    pub start: I,
98    pub stop: I,
99    pub val: T,
100}
101
102/// Primary object of the library. The public intervals holds all the intervals and can be used for
103/// iterating / pulling values out of the tree.
104#[cfg_attr(feature = "with_serde", derive(Serialize, Deserialize))]
105#[derive(Debug, Clone)]
106pub struct Lapper<I, T>
107where
108    I: PrimInt + Unsigned + Ord + Clone + Send + Sync,
109    T: Eq + Clone + Send + Sync,
110{
111    /// List of intervals
112    pub intervals: Vec<Interval<I, T>>,
113    /// Sorted list of start positions,
114    starts: Vec<I>,
115    /// Sorted list of end positions,
116    stops: Vec<I>,
117    /// The length of the longest interval
118    max_len: I,
119    /// The calculated number of positions covered by the intervals
120    cov: Option<I>,
121    /// Whether or not overlaps have been merged
122    pub overlaps_merged: bool,
123}
124
125impl<I, T> Interval<I, T>
126where
127    I: PrimInt + Unsigned + Ord + Clone + Send + Sync,
128    T: Eq + Clone + Send + Sync,
129{
130    /// Compute the intsect between two intervals
131    #[inline]
132    pub fn intersect(&self, other: &Interval<I, T>) -> I {
133        std::cmp::min(self.stop, other.stop)
134            .checked_sub(std::cmp::max(&self.start, &other.start))
135            .unwrap_or_else(zero::<I>)
136    }
137
138    /// Check if two intervals overlap
139    #[inline]
140    pub fn overlap(&self, start: I, stop: I) -> bool {
141        self.start < stop && self.stop > start
142    }
143}
144
145impl<I, T> Ord for Interval<I, T>
146where
147    I: PrimInt + Unsigned + Ord + Clone + Send + Sync,
148    T: Eq + Clone + Send + Sync,
149{
150    #[inline]
151    fn cmp(&self, other: &Interval<I, T>) -> Ordering {
152        match self.start.cmp(&other.start) {
153            Ordering::Less => Ordering::Less,
154            Ordering::Greater => Ordering::Greater,
155            Ordering::Equal => self.stop.cmp(&other.stop),
156        }
157    }
158}
159
160impl<I, T> PartialOrd for Interval<I, T>
161where
162    I: PrimInt + Unsigned + Ord + Clone + Send + Sync,
163    T: Eq + Clone + Send + Sync,
164{
165    #[inline]
166    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
167        Some(self.cmp(other))
168    }
169}
170
171impl<I, T> PartialEq for Interval<I, T>
172where
173    I: PrimInt + Unsigned + Ord + Clone + Send + Sync,
174    T: Eq + Clone + Send + Sync,
175{
176    #[inline]
177    fn eq(&self, other: &Interval<I, T>) -> bool {
178        self.start == other.start && self.stop == other.stop
179    }
180}
181
182impl<I, T> Lapper<I, T>
183where
184    I: PrimInt + Unsigned + Ord + Clone + Send + Sync,
185    T: Eq + Clone + Send + Sync,
186{
187    /// Create a new instance of Lapper by passing in a vector of Intervals. This vector will
188    /// immediately be sorted by start order.
189    /// ```
190    /// use rust_lapper::{Lapper, Interval};
191    /// let data = (0..20).step_by(5)
192    ///                   .map(|x| Interval{start: x, stop: x + 10, val: true})
193    ///                   .collect::<Vec<Interval<usize, bool>>>();
194    /// let lapper = Lapper::new(data);
195    /// ```
196    pub fn new(mut intervals: Vec<Interval<I, T>>) -> Self {
197        intervals.sort();
198        let (mut starts, mut stops): (Vec<_>, Vec<_>) =
199            intervals.iter().map(|x| (x.start, x.stop)).unzip();
200        starts.sort();
201        stops.sort();
202        let mut max_len = zero::<I>();
203        for interval in intervals.iter() {
204            let i_len = interval
205                .stop
206                .checked_sub(&interval.start)
207                .unwrap_or_else(zero::<I>);
208            if i_len > max_len {
209                max_len = i_len;
210            }
211        }
212        Lapper {
213            intervals,
214            starts,
215            stops,
216            max_len,
217            cov: None,
218            overlaps_merged: false,
219        }
220    }
221
222    /// Insert a new interval after the Lapper has been created. This is very
223    /// inefficient and should be avoided if possible.
224    ///
225    /// SIDE EFFECTS: This clears cov() and overlaps_merged
226    /// meaning that those will have to be recomputed after a insert
227    /// ```
228    /// use rust_lapper::{Lapper, Interval};
229    /// let data : Vec<Interval<usize, usize>>= vec!{
230    ///     Interval{start:0,  stop:5,  val:1},
231    ///     Interval{start:6,  stop:10, val:2},
232    /// };
233    /// let mut lapper = Lapper::new(data);
234    /// lapper.insert(Interval{start:0, stop:20, val:5});
235    /// assert_eq!(lapper.len(), 3);
236    /// assert_eq!(lapper.find(1,3).collect::<Vec<&Interval<usize,usize>>>(),
237    ///     vec![
238    ///         &Interval{start:0, stop:5, val:1},
239    ///         &Interval{start:0, stop:20, val:5},
240    ///     ]
241    /// );
242    ///
243    /// ```
244    pub fn insert(&mut self, elem: Interval<I, T>) {
245        let starts_insert_index = Self::bsearch_seq(elem.start, &self.starts);
246        let stops_insert_index = Self::bsearch_seq(elem.stop, &self.stops);
247        let intervals_insert_index = Self::bsearch_seq_ref(&elem, &self.intervals);
248        let i_len = elem.stop.checked_sub(&elem.start).unwrap_or_else(zero::<I>);
249        if i_len > self.max_len {
250            self.max_len = i_len;
251        }
252        self.starts.insert(starts_insert_index, elem.start);
253        self.stops.insert(stops_insert_index, elem.stop);
254        self.intervals.insert(intervals_insert_index, elem);
255        self.cov = None;
256        self.overlaps_merged = false;
257    }
258
259    /// Get the number over intervals in Lapper
260    /// ```
261    /// use rust_lapper::{Lapper, Interval};
262    /// let data = (0..20).step_by(5)
263    ///                   .map(|x| Interval{start: x, stop: x + 10, val: true})
264    ///                   .collect::<Vec<Interval<usize, bool>>>();
265    /// let lapper = Lapper::new(data);
266    /// assert_eq!(lapper.len(), 4);
267    /// ```
268    #[inline]
269    pub fn len(&self) -> usize {
270        self.intervals.len()
271    }
272
273    /// Check if lapper is empty
274    /// ```
275    /// use rust_lapper::{Lapper, Interval};
276    /// let data: Vec<Interval<usize, bool>> = vec![];
277    /// let lapper = Lapper::new(data);
278    /// assert_eq!(lapper.is_empty(), true);
279    /// ```
280    #[inline]
281    pub fn is_empty(&self) -> bool {
282        self.intervals.is_empty()
283    }
284
285    /// Get the number of positions covered by the intervals in Lapper. This provides immutable
286    /// access if it has already been set, or on the fly calculation.
287    /// ```
288    /// use rust_lapper::{Lapper, Interval};
289    /// let data = (0..20).step_by(5)
290    ///                   .map(|x| Interval{start: x, stop: x + 10, val: true})
291    ///                   .collect::<Vec<Interval<usize, bool>>>();
292    /// let lapper = Lapper::new(data);
293    /// assert_eq!(lapper.cov(), 25);
294    #[inline]
295    pub fn cov(&self) -> I {
296        match self.cov {
297            None => self.calculate_coverage(),
298            Some(cov) => cov,
299        }
300    }
301
302    /// Get the number fo positions covered by the intervals in Lapper and store it. If you are
303    /// going to be using the coverage, you should set it to avoid calculating it over and over.
304    pub fn set_cov(&mut self) -> I {
305        let cov = self.calculate_coverage();
306        self.cov = Some(cov);
307        cov
308    }
309
310    /// Calculate the actual coverage behind the scenes.
311    fn calculate_coverage(&self) -> I {
312        let mut moving_interval = Interval {
313            start: zero::<I>(),
314            stop: zero::<I>(),
315            val: zero::<I>(),
316        };
317        let mut cov = zero::<I>();
318
319        for interval in self.intervals.iter() {
320            // If it overlaps, embrace, extend, extinguish
321            if moving_interval.overlap(interval.start, interval.stop) {
322                moving_interval.start = std::cmp::min(moving_interval.start, interval.start);
323                moving_interval.stop = std::cmp::max(moving_interval.stop, interval.stop);
324            } else {
325                // add the set and move on
326                cov = cov + (moving_interval.stop - moving_interval.start);
327                moving_interval.start = interval.start;
328                moving_interval.stop = interval.stop;
329            }
330        }
331        // add in the last bit
332        cov = cov + (moving_interval.stop - moving_interval.start);
333        cov
334    }
335
336    /// Return an iterator over the intervals in Lapper
337    #[inline]
338    pub fn iter(&self) -> IterLapper<I, T> {
339        IterLapper {
340            inner: self,
341            pos: 0,
342        }
343    }
344
345    /// Merge any intervals that overlap with eachother within the Lapper. This is an easy way to
346    /// speed up queries.
347    pub fn merge_overlaps(&mut self) {
348        let mut stack: VecDeque<&mut Interval<I, T>> = VecDeque::new();
349        let mut ivs = self.intervals.iter_mut();
350        if let Some(first) = ivs.next() {
351            stack.push_back(first);
352            for interval in ivs {
353                let mut top = stack.pop_back().unwrap();
354                if top.stop < interval.start {
355                    stack.push_back(top);
356                    stack.push_back(interval);
357                } else if top.stop < interval.stop {
358                    top.stop = interval.stop;
359                    //stack.pop_back();
360                    stack.push_back(top);
361                } else {
362                    // they were equal
363                    stack.push_back(top);
364                }
365            }
366            self.overlaps_merged = true;
367            self.intervals = stack
368                .into_iter()
369                .map(|x| Interval {
370                    start: x.start,
371                    stop: x.stop,
372                    val: x.val.clone(),
373                })
374                .collect();
375        }
376        // Fix the starts and stops used by counts
377        let (mut starts, mut stops): (Vec<_>, Vec<_>) =
378            self.intervals.iter().map(|x| (x.start, x.stop)).unzip();
379        starts.sort();
380        stops.sort();
381        self.starts = starts;
382        self.stops = stops;
383        self.max_len = self
384            .intervals
385            .iter()
386            .map(|x| x.stop.checked_sub(&x.start).unwrap_or_else(zero::<I>))
387            .max()
388            .unwrap_or_else(zero::<I>);
389    }
390
391    /// Determine the first index that we should start checking for overlaps for via a binary
392    /// search.
393    /// Assumes that the maximum interval length in `intervals` has been subtracted from
394    /// `start`, otherwise the result is undefined
395    #[inline]
396    pub fn lower_bound(start: I, intervals: &[Interval<I, T>]) -> usize {
397        let mut size = intervals.len();
398        let mut low = 0;
399
400        while size > 0 {
401            let half = size / 2;
402            let other_half = size - half;
403            let probe = low + half;
404            let other_low = low + other_half;
405            let v = &intervals[probe];
406            size = half;
407            low = if v.start < start { other_low } else { low }
408        }
409        low
410    }
411
412    #[inline]
413    pub fn bsearch_seq<K>(key: K, elems: &[K]) -> usize
414    where
415        K: PartialEq + PartialOrd,
416    {
417        Self::bsearch_seq_ref(&key, elems)
418    }
419
420    #[inline]
421    pub fn bsearch_seq_ref<K>(key: &K, elems: &[K]) -> usize
422    where
423        K: PartialEq + PartialOrd,
424    {
425        if elems.is_empty() {
426            return 0;
427        }
428        if elems[0] > *key {
429            return 0;
430        }
431        let mut high = elems.len();
432        let mut low = 0;
433
434        while high - low > 1 {
435            let mid = (high + low) / 2;
436            if elems[mid] < *key {
437                low = mid;
438            } else {
439                high = mid;
440            }
441        }
442        high
443    }
444
445    /// Find the union and the intersect of two lapper objects.
446    /// Union: The set of positions found in both lappers
447    /// Intersect: The number of positions where both lappers intersect. Note that a position only
448    /// counts one time, multiple Intervals covering the same position don't add up.
449    /// ``` rust
450    /// use rust_lapper::{Lapper, Interval};
451    /// type Iv = Interval<u32, u32>;
452    /// let data1: Vec<Iv> = vec![
453    ///     Iv{start: 70, stop: 120, val: 0}, // max_len = 50
454    ///     Iv{start: 10, stop: 15, val: 0}, // exact overlap
455    ///     Iv{start: 12, stop: 15, val: 0}, // inner overlap
456    ///     Iv{start: 14, stop: 16, val: 0}, // overlap end
457    ///     Iv{start: 68, stop: 71, val: 0}, // overlap start
458    /// ];
459    /// let data2: Vec<Iv> = vec![
460    ///
461    ///     Iv{start: 10, stop: 15, val: 0},
462    ///     Iv{start: 40, stop: 45, val: 0},
463    ///     Iv{start: 50, stop: 55, val: 0},
464    ///     Iv{start: 60, stop: 65, val: 0},
465    ///     Iv{start: 70, stop: 75, val: 0},
466    /// ];
467    ///
468    /// let (mut lapper1, mut lapper2) = (Lapper::new(data1), Lapper::new(data2)) ;
469    /// // Should be the same either way it's calculated
470    /// let (union, intersect) = lapper1.union_and_intersect(&lapper2);
471    /// assert_eq!(intersect, 10);
472    /// assert_eq!(union, 73);
473    /// let (union, intersect) = lapper2.union_and_intersect(&lapper1);
474    /// assert_eq!(intersect, 10);
475    /// assert_eq!(union, 73);
476    /// lapper1.merge_overlaps();
477    /// lapper1.set_cov();
478    /// lapper2.merge_overlaps();
479    /// lapper2.set_cov();
480    ///
481    /// // Should be the same either way it's calculated
482    /// let (union, intersect) = lapper1.union_and_intersect(&lapper2);
483    /// assert_eq!(intersect, 10);
484    /// assert_eq!(union, 73);
485    /// let (union, intersect) = lapper2.union_and_intersect(&lapper1);
486    /// assert_eq!(intersect, 10);
487    /// assert_eq!(union, 73);
488    /// ```
489    #[inline]
490    pub fn union_and_intersect(&self, other: &Self) -> (I, I) {
491        let mut cursor: usize = 0;
492
493        if !self.overlaps_merged || !other.overlaps_merged {
494            let mut intersections: Vec<Interval<I, bool>> = vec![];
495            for self_iv in self.iter() {
496                for other_iv in other.seek(self_iv.start, self_iv.stop, &mut cursor) {
497                    let start = std::cmp::max(self_iv.start, other_iv.start);
498                    let stop = std::cmp::min(self_iv.stop, other_iv.stop);
499                    intersections.push(Interval {
500                        start,
501                        stop,
502                        val: true,
503                    });
504                }
505            }
506            let mut temp_lapper = Lapper::new(intersections);
507            temp_lapper.merge_overlaps();
508            temp_lapper.set_cov();
509            let union = self.cov() + other.cov() - temp_lapper.cov();
510            (union, temp_lapper.cov())
511        } else {
512            let mut intersect = zero::<I>();
513            for c1_iv in self.iter() {
514                for c2_iv in other.seek(c1_iv.start, c1_iv.stop, &mut cursor) {
515                    let local_intersect = c1_iv.intersect(c2_iv);
516                    intersect = intersect + local_intersect;
517                }
518            }
519            let union = self.cov() + other.cov() - intersect;
520            (union, intersect)
521        }
522    }
523
524    /// Find the intersect of two lapper objects.
525    /// Intersect: The number of positions where both lappers intersect. Note that a position only
526    /// counts one time, multiple Intervals covering the same position don't add up
527    #[inline]
528    pub fn intersect(&self, other: &Self) -> I {
529        self.union_and_intersect(other).1
530    }
531
532    /// Find the union of two lapper objects.
533    #[inline]
534    pub fn union(&self, other: &Self) -> I {
535        self.union_and_intersect(other).0
536    }
537
538    /// Return the contiguous intervals of coverage, `val` represents the number of intervals
539    /// covering the returned interval.
540    ///
541    /// # Examples
542    /// ```
543    /// use rust_lapper::{Lapper, Interval};
544    /// let data = (0..20).step_by(5)
545    ///                   .map(|x| Interval{start: x, stop: x + 10, val: true})
546    ///                   .collect::<Vec<Interval<usize, bool>>>();
547    /// let lapper = Lapper::new(data);
548    /// assert_eq!(lapper.depth().collect::<Vec<Interval<usize, usize>>>(), vec![
549    ///             Interval { start: 0, stop: 5, val: 1 },
550    ///             Interval { start: 5, stop: 20, val: 2 },
551    ///             Interval { start: 20, stop: 25, val: 1 }]);
552    /// ```
553    #[inline]
554    pub fn depth(&self) -> IterDepth<I, T> {
555        let mut merged_lapper = Lapper::new(
556            self.intervals
557                .iter()
558                .map(|i| Interval {
559                    start: i.start,
560                    stop: i.stop,
561                    val: true,
562                })
563                .collect::<Vec<Interval<I, bool>>>(),
564        );
565        merged_lapper.merge_overlaps();
566        let merged_len = merged_lapper.intervals.len();
567        IterDepth {
568            inner: self,
569            merged: merged_lapper,
570            curr_merged_pos: zero::<I>(),
571            curr_pos: 0,
572            cursor: 0,
573            end: merged_len,
574        }
575    }
576
577    /// Count all intervals that overlap start .. stop. This performs two binary search in order to
578    /// find all the excluded elements, and then deduces the intersection from there. See
579    /// [BITS](https://arxiv.org/pdf/1208.3407.pdf) for more details.
580    /// ```
581    /// use rust_lapper::{Lapper, Interval};
582    /// let lapper = Lapper::new((0..100).step_by(5)
583    ///                                 .map(|x| Interval{start: x, stop: x+2 , val: true})
584    ///                                 .collect::<Vec<Interval<usize, bool>>>());
585    /// assert_eq!(lapper.count(5, 11), 2);
586    /// ```
587    #[inline]
588    pub fn count(&self, start: I, stop: I) -> usize {
589        let len = self.intervals.len();
590        let mut first = Self::bsearch_seq(start, &self.stops);
591        let last = Self::bsearch_seq(stop, &self.starts);
592        //println!("{}/{}", start, stop);
593        //println!("pre start found in stops: {}: {}", first, self.stops[first]);
594        //println!("pre stop found in starts: {}", last);
595        //while last < len && self.starts[last] == stop {
596        //last += 1;
597        //}
598        while first < len && self.stops[first] == start {
599            first += 1;
600        }
601        let num_cant_after = len - last;
602        len - first - num_cant_after
603        //println!("{:#?}", self.starts);
604        //println!("{:#?}", self.stops);
605        //println!("start found in stops: {}", first);
606        //println!("stop found in starts: {}", last);
607    }
608
609    /// Find all intervals that overlap start .. stop
610    /// ```
611    /// use rust_lapper::{Lapper, Interval};
612    /// let lapper = Lapper::new((0..100).step_by(5)
613    ///                                 .map(|x| Interval{start: x, stop: x+2 , val: true})
614    ///                                 .collect::<Vec<Interval<usize, bool>>>());
615    /// assert_eq!(lapper.find(5, 11).count(), 2);
616    /// ```
617    #[inline]
618    pub fn find(&self, start: I, stop: I) -> IterFind<I, T> {
619        IterFind {
620            inner: self,
621            off: Self::lower_bound(
622                start.checked_sub(&self.max_len).unwrap_or_else(zero::<I>),
623                &self.intervals,
624            ),
625            start,
626            stop,
627        }
628    }
629
630    /// Find all intevals that overlap start .. stop. This method will work when queries
631    /// to this lapper are in sorted (start) order. It uses a linear search from the last query
632    /// instead of a binary search. A reference to a cursor must be passed in. This reference will
633    /// be modified and should be reused in the next query. This allows seek to not need to make
634    /// the lapper object mutable, and thus use the same lapper accross threads.
635    /// ```
636    /// use rust_lapper::{Lapper, Interval};
637    /// let lapper = Lapper::new((0..100).step_by(5)
638    ///                                 .map(|x| Interval{start: x, stop: x+2 , val: true})
639    ///                                 .collect::<Vec<Interval<usize, bool>>>());
640    /// let mut cursor = 0;
641    /// for i in lapper.iter() {
642    ///    assert_eq!(lapper.seek(i.start, i.stop, &mut cursor).count(), 1);
643    /// }
644    /// ```
645    #[inline]
646    pub fn seek<'a>(&'a self, start: I, stop: I, cursor: &mut usize) -> IterFind<'a, I, T> {
647        if *cursor == 0 || (*cursor < self.intervals.len() && self.intervals[*cursor].start > start)
648        {
649            *cursor = Self::lower_bound(
650                start.checked_sub(&self.max_len).unwrap_or_else(zero::<I>),
651                &self.intervals,
652            );
653        }
654
655        while *cursor + 1 < self.intervals.len()
656            && self.intervals[*cursor + 1].start
657                < start.checked_sub(&self.max_len).unwrap_or_else(zero::<I>)
658        {
659            *cursor += 1;
660        }
661
662        IterFind {
663            inner: self,
664            off: *cursor,
665            start,
666            stop,
667        }
668    }
669}
670
671/// Find Iterator
672#[derive(Debug)]
673pub struct IterFind<'a, I, T>
674where
675    T: Eq + Clone + Send + Sync + 'a,
676    I: PrimInt + Unsigned + Ord + Clone + Send + Sync,
677{
678    inner: &'a Lapper<I, T>,
679    off: usize,
680    start: I,
681    stop: I,
682}
683
684impl<'a, I, T> Iterator for IterFind<'a, I, T>
685where
686    T: Eq + Clone + Send + Sync + 'a,
687    I: PrimInt + Unsigned + Ord + Clone + Send + Sync,
688{
689    type Item = &'a Interval<I, T>;
690
691    #[inline]
692    // interval.start < stop && interval.stop > start
693    fn next(&mut self) -> Option<Self::Item> {
694        while self.off < self.inner.intervals.len() {
695            //let mut generator = self.inner.intervals[self.off..].iter();
696            //while let Some(interval) = generator.next() {
697            let interval = &self.inner.intervals[self.off];
698            self.off += 1;
699            if interval.overlap(self.start, self.stop) {
700                return Some(interval);
701            } else if interval.start >= self.stop {
702                break;
703            }
704        }
705        None
706    }
707}
708
709/// Depth Iterator
710#[derive(Debug)]
711pub struct IterDepth<'a, I, T>
712where
713    T: Eq + Clone + Send + Sync + 'a,
714    I: PrimInt + Unsigned + Ord + Clone + Send + Sync,
715{
716    inner: &'a Lapper<I, T>,
717    merged: Lapper<I, bool>, // A lapper that is the merged_lapper of inner
718    curr_merged_pos: I,      // Current start position in current interval
719    curr_pos: usize,         // In merged list of non-overlapping intervals
720    cursor: usize,           // cursor for seek over inner lapper
721    end: usize,              // len of merged
722}
723
724impl<'a, I, T> Iterator for IterDepth<'a, I, T>
725where
726    T: Eq + Clone + Send + Sync + 'a,
727    I: PrimInt + Unsigned + Ord + Clone + Send + Sync,
728{
729    type Item = Interval<I, I>;
730
731    #[inline]
732    fn next(&mut self) -> Option<Self::Item> {
733        let mut interval: &Interval<I, bool> = &self.merged.intervals[self.curr_pos];
734        if self.curr_merged_pos == zero::<I>() {
735            self.curr_merged_pos = interval.start;
736        }
737        if interval.stop == self.curr_merged_pos {
738            if self.curr_pos + 1 != self.end {
739                self.curr_pos += 1;
740                interval = &self.merged.intervals[self.curr_pos];
741                self.curr_merged_pos = interval.start;
742            } else {
743                return None;
744            }
745        }
746        let start = self.curr_merged_pos;
747        let depth_at_point = self
748            .inner
749            .seek(
750                self.curr_merged_pos,
751                self.curr_merged_pos + one::<I>(),
752                &mut self.cursor,
753            )
754            .count();
755        let mut new_depth_at_point = depth_at_point;
756        while new_depth_at_point == depth_at_point && self.curr_merged_pos < interval.stop {
757            self.curr_merged_pos = self.curr_merged_pos + one::<I>();
758            new_depth_at_point = self
759                .inner
760                .seek(
761                    self.curr_merged_pos,
762                    self.curr_merged_pos + one::<I>(),
763                    &mut self.cursor,
764                )
765                .count();
766        }
767        Some(Interval {
768            start,
769            stop: self.curr_merged_pos,
770            val: I::from(depth_at_point).unwrap(), // from usize should always work
771        })
772    }
773}
774/// Lapper Iterator
775pub struct IterLapper<'a, I, T>
776where
777    T: Eq + Clone + Send + Sync + 'a,
778    I: PrimInt + Unsigned + Ord + Clone + Send + Sync,
779{
780    inner: &'a Lapper<I, T>,
781    pos: usize,
782}
783
784impl<'a, I, T> Iterator for IterLapper<'a, I, T>
785where
786    T: Eq + Clone + Send + Sync + 'a,
787    I: PrimInt + Unsigned + Ord + Clone + Send + Sync,
788{
789    type Item = &'a Interval<I, T>;
790
791    fn next(&mut self) -> Option<Self::Item> {
792        if self.pos >= self.inner.intervals.len() {
793            None
794        } else {
795            self.pos += 1;
796            self.inner.intervals.get(self.pos - 1)
797        }
798    }
799}
800
801impl<I, T> IntoIterator for Lapper<I, T>
802where
803    T: Eq + Clone + Send + Sync,
804    I: PrimInt + Unsigned + Ord + Clone + Send + Sync,
805{
806    type Item = Interval<I, T>;
807    type IntoIter = ::std::vec::IntoIter<Self::Item>;
808
809    fn into_iter(self) -> Self::IntoIter {
810        self.intervals.into_iter()
811    }
812}
813
814impl<'a, I, T> IntoIterator for &'a Lapper<I, T>
815where
816    T: Eq + Clone + Send + Sync + 'a,
817    I: PrimInt + Unsigned + Ord + Clone + Send + Sync,
818{
819    type Item = &'a Interval<I, T>;
820    type IntoIter = std::slice::Iter<'a, Interval<I, T>>;
821
822    fn into_iter(self) -> std::slice::Iter<'a, Interval<I, T>> {
823        self.intervals.iter()
824    }
825}
826
827impl<'a, I, T> IntoIterator for &'a mut Lapper<I, T>
828where
829    T: Eq + Clone + Send + Sync + 'a,
830    I: PrimInt + Unsigned + Ord + Clone + Send + Sync,
831{
832    type Item = &'a mut Interval<I, T>;
833    type IntoIter = std::slice::IterMut<'a, Interval<I, T>>;
834
835    fn into_iter(self) -> std::slice::IterMut<'a, Interval<I, T>> {
836        self.intervals.iter_mut()
837    }
838}
839
840#[cfg(test)]
841#[rustfmt::skip]
842mod tests {
843    use super::*;
844
845    type Iv = Interval<usize, u32>;
846    fn setup_nonoverlapping() -> Lapper<usize, u32> {
847        let data: Vec<Iv> = (0..100)
848            .step_by(20)
849            .map(|x| Iv {
850                start: x,
851                stop: x + 10,
852                val: 0,
853            })
854            .collect();
855        Lapper::new(data)
856    }
857
858    fn setup_overlapping() -> Lapper<usize, u32> {
859        let data: Vec<Iv> = (0..100)
860            .step_by(10)
861            .map(|x| Iv {
862                start: x,
863                stop: x + 15,
864                val: 0,
865            })
866            .collect();
867        Lapper::new(data)
868    }
869
870    fn setup_badlapper() -> Lapper<usize, u32> {
871        let data: Vec<Iv> = vec![
872            Iv{start: 70, stop: 120, val: 0}, // max_len = 50
873            Iv{start: 10, stop: 15, val: 0},
874            Iv{start: 10, stop: 15, val: 0}, // exact overlap
875            Iv{start: 12, stop: 15, val: 0}, // inner overlap
876            Iv{start: 14, stop: 16, val: 0}, // overlap end
877            Iv{start: 40, stop: 45, val: 0},
878            Iv{start: 50, stop: 55, val: 0},
879            Iv{start: 60, stop: 65, val: 0},
880            Iv{start: 68, stop: 71, val: 0}, // overlap start
881            Iv{start: 70, stop: 75, val: 0},
882        ];
883        Lapper::new(data)
884    }
885
886    fn setup_single() -> Lapper<usize, u32> {
887        let data: Vec<Iv> = vec![Iv {
888            start: 10,
889            stop: 35,
890            val: 0,
891        }];
892        Lapper::new(data)
893    }
894
895    // Test that inserting data ends up with the same lapper (nonoverlapping)
896    #[test]
897    fn insert_equality_nonoverlapping() {
898        let data: Vec<Iv> = (0..100)
899            .step_by(20)
900            .map(|x| Iv {
901                start: x,
902                stop: x + 10,
903                val: 0,
904            })
905            .collect();
906        let new_lapper = Lapper::new(data.clone());
907        let mut insert_lapper = Lapper::new(vec![]);
908        for elem in data {
909            insert_lapper.insert(elem);
910        }
911        assert_eq!(new_lapper.starts, insert_lapper.starts);
912        assert_eq!(new_lapper.stops, insert_lapper.stops);
913        assert_eq!(new_lapper.intervals, insert_lapper.intervals);
914        assert_eq!(new_lapper.max_len, insert_lapper.max_len);
915    }
916
917    // Test that inserting data ends up with the same lapper (overlapping)
918    #[test]
919    fn insert_equality_overlapping() {
920        let data: Vec<Iv> = (0..100)
921            .step_by(10)
922            .map(|x| Iv {
923                start: x,
924                stop: x + 15,
925                val: 0,
926            })
927            .collect();
928        let new_lapper = Lapper::new(data.clone());
929        let mut insert_lapper = Lapper::new(vec![]);
930        for elem in data {
931            insert_lapper.insert(elem);
932        }
933        assert_eq!(new_lapper.starts, insert_lapper.starts);
934        assert_eq!(new_lapper.stops, insert_lapper.stops);
935        assert_eq!(new_lapper.intervals, insert_lapper.intervals);
936        assert_eq!(new_lapper.max_len, insert_lapper.max_len);
937    }
938
939    // Test that inserting data half with new and half with insert
940    // ends up with the same lapper
941    #[test]
942    fn insert_equality_half_and_half() {
943        let data: Vec<Iv> = (0..100)
944            .step_by(1)
945            .map(|x| Iv {
946                start: x,
947                stop: x + 15,
948                val: 0,
949            })
950            .collect();
951        let new_lapper = Lapper::new(data.clone());
952        let (new_data, insert_data) = data.split_at(50);
953        let mut insert_lapper = Lapper::new(new_data.to_vec());
954        let mut insert_data = insert_data.to_vec();
955        insert_data.reverse();
956        for elem in insert_data {
957            insert_lapper.insert(elem);
958        }
959        assert_eq!(new_lapper.starts, insert_lapper.starts);
960        assert_eq!(new_lapper.stops, insert_lapper.stops);
961        assert_eq!(new_lapper.intervals, insert_lapper.intervals);
962        assert_eq!(new_lapper.max_len, insert_lapper.max_len);
963    }
964
965    // Test that inserting data ends up with the same lapper (badlapper)
966    #[test]
967    fn insert_equality_badlapper() {
968        let data: Vec<Iv> = vec![
969            Iv{start: 70, stop: 120, val: 0}, // max_len = 50
970            Iv{start: 10, stop: 15, val: 0},
971            Iv{start: 10, stop: 15, val: 0}, // exact overlap
972            Iv{start: 12, stop: 15, val: 0}, // inner overlap
973            Iv{start: 14, stop: 16, val: 0}, // overlap end
974            Iv{start: 40, stop: 45, val: 0},
975            Iv{start: 50, stop: 55, val: 0},
976            Iv{start: 60, stop: 65, val: 0},
977            Iv{start: 68, stop: 71, val: 0}, // overlap start
978            Iv{start: 70, stop: 75, val: 0},
979        ];
980        let new_lapper = Lapper::new(data.clone());
981        let mut insert_lapper = Lapper::new(vec![]);
982        for elem in data {
983            insert_lapper.insert(elem);
984        }
985        assert_eq!(new_lapper.starts, insert_lapper.starts);
986        assert_eq!(new_lapper.stops, insert_lapper.stops);
987        assert_eq!(new_lapper.intervals, insert_lapper.intervals);
988        assert_eq!(new_lapper.max_len, insert_lapper.max_len);
989    }
990
991    // Test that inserting data ends up with the same lapper (single)
992    #[test]
993    fn insert_equality_single() {
994        let data: Vec<Iv> = vec![Iv {
995            start: 10,
996            stop: 35,
997            val: 0,
998        }];
999        let new_lapper = Lapper::new(data.clone());
1000        let mut insert_lapper = Lapper::new(vec![]);
1001        for elem in data {
1002            insert_lapper.insert(elem);
1003        }
1004        assert_eq!(new_lapper.starts, insert_lapper.starts);
1005        assert_eq!(new_lapper.stops, insert_lapper.stops);
1006        assert_eq!(new_lapper.intervals, insert_lapper.intervals);
1007        assert_eq!(new_lapper.max_len, insert_lapper.max_len);
1008    }
1009
1010    // Test that a query stop that hits an interval start returns no interval
1011    #[test]
1012    fn test_query_stop_interval_start() {
1013        let lapper = setup_nonoverlapping();
1014        let mut cursor = 0;
1015        assert_eq!(None, lapper.find(15, 20).next());
1016        assert_eq!(None, lapper.seek(15, 20, &mut cursor).next());
1017        assert_eq!(lapper.find(15, 20).count(), lapper.count(15, 20));
1018    }
1019
1020    // Test that a query start that hits an interval end returns no interval
1021    #[test]
1022    fn test_query_start_interval_stop() {
1023        let lapper = setup_nonoverlapping();
1024        let mut cursor = 0;
1025        assert_eq!(None, lapper.find(30, 35).next());
1026        assert_eq!(None, lapper.seek(30, 35, &mut cursor).next());
1027        assert_eq!(lapper.find(30, 35).count(), lapper.count(30, 35));
1028    }
1029
1030    // Test that a query that overlaps the start of an interval returns that interval
1031    #[test]
1032    fn test_query_overlaps_interval_start() {
1033        let lapper = setup_nonoverlapping();
1034        let mut cursor = 0;
1035        let expected = Iv {
1036            start: 20,
1037            stop: 30,
1038            val: 0,
1039        };
1040        assert_eq!(Some(&expected), lapper.find(15, 25).next());
1041        assert_eq!(Some(&expected), lapper.seek(15, 25, &mut cursor).next());
1042        assert_eq!(lapper.find(15, 25).count(), lapper.count(15, 25));
1043    }
1044
1045    // Test that a query that overlaps the stop of an interval returns that interval
1046    #[test]
1047    fn test_query_overlaps_interval_stop() {
1048        let lapper = setup_nonoverlapping();
1049        let mut cursor = 0;
1050        let expected = Iv {
1051            start: 20,
1052            stop: 30,
1053            val: 0,
1054        };
1055        assert_eq!(Some(&expected), lapper.find(25, 35).next());
1056        assert_eq!(Some(&expected), lapper.seek(25, 35, &mut cursor).next());
1057        assert_eq!(lapper.find(25, 35).count(), lapper.count(25, 35));
1058    }
1059
1060    // Test that a query that is enveloped by interval returns interval
1061    #[test]
1062    fn test_interval_envelops_query() {
1063        let lapper = setup_nonoverlapping();
1064        let mut cursor = 0;
1065        let expected = Iv {
1066            start: 20,
1067            stop: 30,
1068            val: 0,
1069        };
1070        assert_eq!(Some(&expected), lapper.find(22, 27).next());
1071        assert_eq!(Some(&expected), lapper.seek(22, 27, &mut cursor).next());
1072        assert_eq!(lapper.find(22, 27).count(), lapper.count(22, 27));
1073    }
1074
1075    // Test that a query that envolops an interval returns that interval
1076    #[test]
1077    fn test_query_envolops_interval() {
1078        let lapper = setup_nonoverlapping();
1079        let mut cursor = 0;
1080        let expected = Iv {
1081            start: 20,
1082            stop: 30,
1083            val: 0,
1084        };
1085        assert_eq!(Some(&expected), lapper.find(15, 35).next());
1086        assert_eq!(Some(&expected), lapper.seek(15, 35, &mut cursor).next());
1087        assert_eq!(lapper.find(15, 35).count(), lapper.count(15, 35));
1088    }
1089
1090    #[test]
1091    fn test_overlapping_intervals() {
1092        let lapper = setup_overlapping();
1093        let mut cursor = 0;
1094        let e1 = Iv {
1095            start: 0,
1096            stop: 15,
1097            val: 0,
1098        };
1099        let e2 = Iv {
1100            start: 10,
1101            stop: 25,
1102            val: 0,
1103        };
1104        assert_eq!(vec![&e1, &e2], lapper.find(8, 20).collect::<Vec<&Iv>>());
1105        assert_eq!(
1106            vec![&e1, &e2],
1107            lapper.seek(8, 20, &mut cursor).collect::<Vec<&Iv>>()
1108        );
1109        assert_eq!(lapper.count(8, 20), 2);
1110    }
1111
1112    #[test]
1113    fn test_merge_overlaps() {
1114        let mut lapper = setup_badlapper();
1115        let expected: Vec<&Iv> = vec![
1116            &Iv{start: 10, stop: 16, val: 0},
1117            &Iv{start: 40, stop: 45, val: 0},
1118            &Iv{start: 50, stop: 55, val: 0},
1119            &Iv{start: 60, stop: 65, val: 0},
1120            &Iv{start: 68, stop: 120, val: 0}, // max_len = 50
1121        ];
1122        assert_eq!(lapper.intervals.len(), lapper.starts.len());
1123        lapper.merge_overlaps();
1124        assert_eq!(expected, lapper.iter().collect::<Vec<&Iv>>());
1125        assert_eq!(lapper.intervals.len(), lapper.starts.len())
1126
1127    }
1128
1129    // This test was added because this breakage was found in a library user's code, where after
1130    // calling merge_overlaps(), the find() call returned an empty iterator.
1131    #[test]
1132    fn test_merge_overlaps_find() {
1133        let data = vec![
1134                Iv{start: 2, stop: 3, val: 0},
1135                Iv{start: 3, stop: 4, val: 0},
1136                Iv{start: 4, stop: 6, val: 0},
1137                Iv{start: 6, stop: 7, val: 0},
1138                Iv{start: 7, stop: 8, val: 0},
1139        ];
1140        let mut lapper = Lapper::new(data);
1141
1142        let found = lapper.find(7, 9).collect::<Vec<&Interval<_,_>>>();
1143        assert_eq!(found, vec![
1144            &Iv{start:7, stop: 8, val: 0},
1145        ]);
1146
1147        // merge_overlaps should merge all intervals to one, which should be returned in the find call.
1148        lapper.merge_overlaps();
1149
1150        let found = lapper.find(7, 9).collect::<Vec<&Interval<_,_>>>();
1151        assert_eq!(found, vec![
1152            &Iv{start:2, stop: 8, val: 0},
1153        ]);
1154    }
1155
1156    #[test]
1157    fn test_lapper_cov() {
1158        let mut lapper = setup_badlapper();
1159        let before = lapper.cov();
1160        lapper.merge_overlaps();
1161        let after = lapper.cov();
1162        assert_eq!(before, after);
1163
1164        let mut lapper = setup_nonoverlapping();
1165        lapper.set_cov();
1166        assert_eq!(lapper.cov(), 50);
1167    }
1168
1169    #[test]
1170    fn test_interval_intersects() {
1171        let i1 = Iv{start: 70, stop: 120, val: 0}; // max_len = 50
1172        let i2 = Iv{start: 10, stop: 15, val: 0};
1173        let i3 = Iv{start: 10, stop: 15, val: 0}; // exact overlap
1174        let i4 = Iv{start: 12, stop: 15, val: 0}; // inner overlap
1175        let i5 = Iv{start: 14, stop: 16, val: 0}; // overlap end
1176        let i6 = Iv{start: 40, stop: 50, val: 0};
1177        let i7 = Iv{start: 50, stop: 55, val: 0};
1178        let i_8 = Iv{start: 60, stop: 65, val: 0};
1179        let i9 = Iv{start: 68, stop: 71, val: 0}; // overlap start
1180        let i10 = Iv{start: 70, stop: 75, val: 0};
1181
1182        assert_eq!(i2.intersect(&i3), 5); // exact match
1183        assert_eq!(i2.intersect(&i4), 3); // inner intersect
1184        assert_eq!(i2.intersect(&i5), 1); // end intersect
1185        assert_eq!(i9.intersect(&i10), 1); // start intersect
1186        assert_eq!(i7.intersect(&i_8), 0); // no intersect
1187        assert_eq!(i6.intersect(&i7), 0); // no intersect stop = start
1188        assert_eq!(i1.intersect(&i10), 5); // inner intersect at start
1189    }
1190
1191    #[test]
1192    fn test_union_and_intersect() {
1193        let data1: Vec<Iv> = vec![
1194            Iv{start: 70, stop: 120, val: 0}, // max_len = 50
1195            Iv{start: 10, stop: 15, val: 0}, // exact overlap
1196            Iv{start: 12, stop: 15, val: 0}, // inner overlap
1197            Iv{start: 14, stop: 16, val: 0}, // overlap end
1198            Iv{start: 68, stop: 71, val: 0}, // overlap start
1199        ];
1200        let data2: Vec<Iv> = vec![
1201
1202            Iv{start: 10, stop: 15, val: 0},
1203            Iv{start: 40, stop: 45, val: 0},
1204            Iv{start: 50, stop: 55, val: 0},
1205            Iv{start: 60, stop: 65, val: 0},
1206            Iv{start: 70, stop: 75, val: 0},
1207        ];
1208
1209        let (mut lapper1, mut lapper2) = (Lapper::new(data1), Lapper::new(data2)) ;
1210        // Should be the same either way it's calculated
1211        let (union, intersect) = lapper1.union_and_intersect(&lapper2);
1212        assert_eq!(intersect, 10);
1213        assert_eq!(union, 73);
1214        let (union, intersect) = lapper2.union_and_intersect(&lapper1);
1215        assert_eq!(intersect, 10);
1216        assert_eq!(union, 73);
1217        lapper1.merge_overlaps();
1218        lapper1.set_cov();
1219        lapper2.merge_overlaps();
1220        lapper2.set_cov();
1221
1222        // Should be the same either way it's calculated
1223        let (union, intersect) = lapper1.union_and_intersect(&lapper2);
1224        assert_eq!(intersect, 10);
1225        assert_eq!(union, 73);
1226        let (union, intersect) = lapper2.union_and_intersect(&lapper1);
1227        assert_eq!(intersect, 10);
1228        assert_eq!(union, 73);
1229    }
1230
1231    #[test]
1232    fn test_find_overlaps_in_large_intervals() {
1233        let data1: Vec<Iv> = vec![
1234            Iv{start: 0, stop: 8, val: 0},
1235            Iv{start: 1, stop: 10, val: 0},
1236            Iv{start: 2, stop: 5, val: 0},
1237            Iv{start: 3, stop: 8, val: 0},
1238            Iv{start: 4, stop: 7, val: 0},
1239            Iv{start: 5, stop: 8, val: 0},
1240            Iv{start: 8, stop: 8, val: 0},
1241            Iv{start: 9, stop: 11, val: 0},
1242            Iv{start: 10, stop: 13, val: 0},
1243            Iv{start: 100, stop: 200, val: 0},
1244            Iv{start: 110, stop: 120, val: 0},
1245            Iv{start: 110, stop: 124, val: 0},
1246            Iv{start: 111, stop: 160, val: 0},
1247            Iv{start: 150, stop: 200, val: 0},
1248        ];
1249        let lapper = Lapper::new(data1);
1250        let found = lapper.find(8, 11).collect::<Vec<&Iv>>();
1251        assert_eq!(found, vec![
1252            &Iv{start: 1, stop: 10, val: 0},
1253            &Iv{start: 9, stop: 11, val: 0},
1254            &Iv{start: 10, stop: 13, val: 0},
1255        ]);
1256        assert_eq!(lapper.count(8, 11), 3);
1257        let found = lapper.find(145, 151).collect::<Vec<&Iv>>();
1258        assert_eq!(found, vec![
1259            &Iv{start: 100, stop: 200, val: 0},
1260            &Iv{start: 111, stop: 160, val: 0},
1261            &Iv{start: 150, stop: 200, val: 0},
1262        ]);
1263
1264        assert_eq!(lapper.count(145, 151), 3);
1265    }
1266
1267    #[test]
1268    fn test_depth_sanity() {
1269        let data1: Vec<Iv> = vec![
1270            Iv{start: 0, stop: 10, val: 0},
1271            Iv{start: 5, stop: 10, val: 0}
1272        ];
1273        let lapper = Lapper::new(data1);
1274        let found = lapper.depth().collect::<Vec<Interval<usize, usize>>>();
1275        assert_eq!(found, vec![
1276                   Interval{start: 0, stop: 5, val: 1},
1277                   Interval{start: 5, stop: 10, val: 2}
1278        ]);
1279    }
1280
1281    #[test]
1282    fn test_depth_hard() {
1283        let data1: Vec<Iv> = vec![
1284            Iv{start: 1, stop: 10, val: 0},
1285            Iv{start: 2, stop: 5, val: 0},
1286            Iv{start: 3, stop: 8, val: 0},
1287            Iv{start: 3, stop: 8, val: 0},
1288            Iv{start: 3, stop: 8, val: 0},
1289            Iv{start: 5, stop: 8, val: 0},
1290            Iv{start: 9, stop: 11, val: 0},
1291        ];
1292        let lapper = Lapper::new(data1);
1293        let found = lapper.depth().collect::<Vec<Interval<usize, usize>>>();
1294        assert_eq!(found, vec![
1295                   Interval{start: 1, stop: 2, val: 1},
1296                   Interval{start: 2, stop: 3, val: 2},
1297                   Interval{start: 3, stop: 8, val: 5},
1298                   Interval{start: 8, stop: 9, val: 1},
1299                   Interval{start: 9, stop: 10, val: 2},
1300                   Interval{start: 10, stop: 11, val: 1},
1301        ]);
1302    }
1303    #[test]
1304    fn test_depth_harder() {
1305        let data1: Vec<Iv> = vec![
1306            Iv{start: 1, stop: 10, val: 0},
1307            Iv{start: 2, stop: 5, val: 0},
1308            Iv{start: 3, stop: 8, val: 0},
1309            Iv{start: 3, stop: 8, val: 0},
1310            Iv{start: 3, stop: 8, val: 0},
1311            Iv{start: 5, stop: 8, val: 0},
1312            Iv{start: 9, stop: 11, val: 0},
1313            Iv{start: 15, stop: 20, val: 0},
1314        ];
1315        let lapper = Lapper::new(data1);
1316        let found = lapper.depth().collect::<Vec<Interval<usize, usize>>>();
1317        assert_eq!(found, vec![
1318                   Interval{start: 1, stop: 2, val: 1},
1319                   Interval{start: 2, stop: 3, val: 2},
1320                   Interval{start: 3, stop: 8, val: 5},
1321                   Interval{start: 8, stop: 9, val: 1},
1322                   Interval{start: 9, stop: 10, val: 2},
1323                   Interval{start: 10, stop: 11, val: 1},
1324                   Interval{start: 15, stop: 20, val: 1},
1325        ]);
1326    }
1327    // BUG TESTS - these are tests that came from real life
1328
1329    // Test that it's not possible to induce index out of bounds by pushing the cursor past the end
1330    // of the lapper.
1331    #[test]
1332    fn test_seek_over_len() {
1333        let lapper = setup_nonoverlapping();
1334        let single = setup_single();
1335        let mut cursor: usize = 0;
1336
1337        for interval in lapper.iter() {
1338            for o_interval in single.seek(interval.start, interval.stop, &mut cursor) {
1339                println!("{:#?}", o_interval);
1340            }
1341        }
1342    }
1343
1344    // Test that if lower_bound puts us before the first match, we still return a match
1345    #[test]
1346    fn test_find_over_behind_first_match() {
1347        let lapper = setup_badlapper();
1348        let e1 = Iv {start: 50, stop: 55, val: 0};
1349        let found = lapper.find(50, 55).next();
1350        assert_eq!(found, Some(&e1));
1351        assert_eq!(lapper.find(50, 55).count(), lapper.count(50,55));
1352    }
1353
1354    // When there is a very long interval that spans many little intervals, test that the little
1355    // intevals still get returne properly
1356    #[test]
1357    fn test_bad_skips() {
1358        let data = vec![
1359            Iv{start:25264912, stop: 25264986, val: 0},
1360            Iv{start:27273024, stop: 27273065	, val: 0},
1361            Iv{start:27440273, stop: 27440318	, val: 0},
1362            Iv{start:27488033, stop: 27488125	, val: 0},
1363            Iv{start:27938410, stop: 27938470	, val: 0},
1364            Iv{start:27959118, stop: 27959171	, val: 0},
1365            Iv{start:28866309, stop: 33141404	, val: 0},
1366        ];
1367        let lapper = Lapper::new(data);
1368
1369        let found = lapper.find(28974798, 33141355).collect::<Vec<&Iv>>();
1370        assert_eq!(found, vec![
1371            &Iv{start:28866309, stop: 33141404	, val: 0},
1372        ]);
1373        assert_eq!(lapper.count(28974798, 33141355), 1);
1374    }
1375
1376    #[test]
1377    fn serde_test() {
1378        let data = vec![
1379            Iv{start:25264912, stop: 25264986, val: 0},
1380            Iv{start:27273024, stop: 27273065	, val: 0},
1381            Iv{start:27440273, stop: 27440318	, val: 0},
1382            Iv{start:27488033, stop: 27488125	, val: 0},
1383            Iv{start:27938410, stop: 27938470	, val: 0},
1384            Iv{start:27959118, stop: 27959171	, val: 0},
1385            Iv{start:28866309, stop: 33141404	, val: 0},
1386        ];
1387        let lapper = Lapper::new(data);
1388
1389        let serialized = bincode::serialize(&lapper).unwrap();
1390        let deserialzed: Lapper<usize, u32> = bincode::deserialize(&serialized).unwrap();
1391
1392        let found = deserialzed.find(28974798, 33141355).collect::<Vec<&Iv>>();
1393        assert_eq!(found, vec![
1394            &Iv{start:28866309, stop: 33141404	, val: 0},
1395        ]);
1396        assert_eq!(deserialzed.count(28974798, 33141355), 1);
1397    }
1398
1399}