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
use super::{Active, ActiveSet};
use std::{cmp::Ordering, fmt::Debug, ops::Index};

/// A simple ordered set implementation backed by a `Vec`.
#[derive(Debug, Clone)]
pub struct VecSet<T: Ord> {
    data: Vec<T>,
}

impl<T: Ord> Default for VecSet<T> {
    fn default() -> Self {
        Self {
            data: Default::default(),
        }
    }
}

impl<T: PartialOrd + Debug> VecSet<Active<T>> {
    pub fn index_of(&self, segment: &T) -> usize {
        self.data
            .binary_search(Active::active_ref(segment))
            .expect("segment not found in active-vec-set")
    }

    pub fn index_not_of(&self, segment: &T) -> usize {
        self.data
            .binary_search(Active::active_ref(segment))
            .expect_err("segment already found in active-vec-set")
    }
    pub fn len(&self) -> usize {
        self.data.len()
    }

    #[allow(unused)]
    pub fn insert_at(&mut self, idx: usize, segment: T) {
        self.data.insert(idx, Active(segment))
    }

    pub fn remove_at(&mut self, idx: usize) -> T {
        self.data.remove(idx).0
    }

    #[allow(unused)]
    pub fn check_swap(&mut self, idx: usize) -> bool {
        if self.data[idx].cmp(&self.data[idx + 1]) == Ordering::Greater {
            self.data.swap(idx, idx + 1);
            true
        } else {
            false
        }
    }
}

impl<T: Ord> Index<usize> for VecSet<T> {
    type Output = T;

    fn index(&self, index: usize) -> &Self::Output {
        &self.data[index]
    }
}

impl<T: PartialOrd + Debug> ActiveSet for VecSet<Active<T>> {
    type Seg = T;

    fn previous_find<F: FnMut(&Active<Self::Seg>) -> bool>(
        &self,
        segment: &Self::Seg,
        mut f: F,
    ) -> Option<&Active<Self::Seg>> {
        let segment = Active::active_ref(segment);
        let ub = match self.data.binary_search(segment) {
            Ok(i) => i,
            Err(i) => i,
        };
        self.data[..ub].iter().rev().find(|s| f(s))
    }

    fn next_find<F: FnMut(&Active<Self::Seg>) -> bool>(
        &self,
        segment: &Self::Seg,
        mut f: F,
    ) -> Option<&Active<Self::Seg>> {
        let segment = Active::active_ref(segment);
        let start = match self.data.binary_search(segment) {
            Ok(i) => i + 1,
            Err(i) => i,
        };
        self.data[start..].iter().find(|s| f(s))
    }

    fn insert_active(&mut self, segment: Self::Seg) {
        let idx = {
            let segment = Active::active_ref(&segment);
            self.data
                .binary_search(segment)
                .expect_err("element already in active-vec-set")
        };
        self.data.insert(idx, Active(segment));
    }

    fn remove_active(&mut self, segment: &Self::Seg) {
        let segment = Active::active_ref(segment);
        let idx = self
            .data
            .binary_search(segment)
            .expect("element not found in active-vec-set");
        self.data.remove(idx);
    }
}