Skip to main content

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