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
// Copyright (c) 2018-2021 Thomas Kramer.
// SPDX-FileCopyrightText: 2022 Thomas Kramer
//
// SPDX-License-Identifier: AGPL-3.0-or-later

//! Scanline based on a splay set.

#![allow(unused)]

use libreda_splay::SplaySet;
use std::borrow::Borrow;
use std::cmp::Ordering;

/// Scanline based on a splay set.
pub struct SplayScanLine<T, C>
where
    C: Fn(&T, &T) -> Ordering,
{
    content: SplaySet<T, C>,
}

impl<T, C> SplayScanLine<T, C>
where
    C: Fn(&T, &T) -> Ordering,
{
    pub fn new(comparator: C) -> SplayScanLine<T, C> {
        SplayScanLine {
            content: SplaySet::new(comparator),
        }
    }

    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(&self, t: &T) -> bool {
        self.content.contains(t)
    }

    pub fn next(&self, t: &T) -> Option<&T> {
        self.content.next(t)
    }

    pub fn prev(&self, t: &T) -> Option<&T> {
        self.content.prev(t)
    }

    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.min()
    }

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

#[cfg(test)]
mod test_splay_scanline {
    extern crate rand;

    use super::*;
    use std::cmp::Ordering;
    use std::i32;

    fn int_comparator(a: &i32, b: &i32) -> Ordering {
        a.cmp(b)
    }

    #[test]
    fn insert_simple() {
        let mut t = SplayScanLine::new(int_comparator);
        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));
    }
}