1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// SPDX-FileCopyrightText: 2022 Thomas Kramer
//
// SPDX-License-Identifier: AGPL-3.0-or-later

//! Drop-in replacement for the splay set used for the scanline.
//! Based on the BTreeSet of the standard library.
//! This might be less performant than the implementation based on a splay set, but
//! it is here for comparisons.

#![allow(unused)]

use std::borrow::Borrow;
use std::cmp::Ordering;
use std::collections::BTreeSet;
use std::ops::Bound;

/// Data structure for the scan-line based on a BTreeSet.
pub struct BTreeScanLine<K> {
    pub(crate) content: BTreeSet<K>,
}

impl<T> BTreeScanLine<T>
where
    T: Ord,
{
    pub fn new() -> BTreeScanLine<T> {
        BTreeScanLine {
            content: Default::default(),
        }
    }

    pub fn len(&self) -> usize {
        self.content.len()
    }
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
    pub fn clear(&mut self) {
        self.content.clear()
    }

    pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool
    where
        T: Borrow<Q> + Ord,
        Q: Ord,
    {
        self.content.contains(value)
    }

    pub fn next<Q: ?Sized>(&self, t: &Q) -> Option<&T>
    where
        T: Borrow<Q> + Ord,
        Q: Ord,
    {
        self.content
            .range((Bound::Excluded(t), Bound::Unbounded))
            .next()
    }

    pub fn prev<Q: ?Sized>(&self, t: &Q) -> Option<&T>
    where
        T: Borrow<Q> + Ord,
        Q: Ord,
    {
        self.content
            .range((Bound::Unbounded, Bound::Excluded(t)))
            .rev()
            .next()
    }

    pub fn insert(&mut self, t: T) -> bool {
        self.content.insert(t)
    }

    pub fn remove(&mut self, t: &T) -> bool {
        self.content.remove(t)
    }

    pub fn min(&self) -> Option<&T> {
        self.content.iter().next()
    }

    pub fn max(&self) -> Option<&T> {
        self.content.iter().rev().next()
    }
}

#[cfg(test)]
mod test_btree_scanline {
    use super::*;
    use std::i32;

    #[test]
    fn insert_simple() {
        let mut t = BTreeScanLine::new();
        t.insert(1);
        t.insert(3);
        t.insert(2);

        assert_eq!(t.min(), Some(&1));
        assert_eq!(t.max(), Some(&3));

        assert_eq!(t.next(&1), Some(&2));
        assert_eq!(t.next(&2), Some(&3));
        assert_eq!(t.next(&3), None);

        assert_eq!(t.prev(&1), None);
        assert_eq!(t.prev(&2), Some(&1));
        assert_eq!(t.prev(&3), Some(&2));
    }
}