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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
   Copyright 2023 James Forster

   This file is part of gap_query_interval_tree.

   gap_query_interval_tree is free software: you can redistribute it
   and/or modify it under the terms of the GNU Affero General Public
   License as published by the Free Software Foundation, either
   version 3 of the License, or (at your option) any later version.

   gap_query_interval_tree is distributed in the hope that it will be
   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public
   License along with gap_query_interval_tree. If not, see
   <https://www.gnu.org/licenses/>.
*/

use alloc::collections::{BTreeMap, BTreeSet};
use alloc::vec::Vec;

use discrete_range_map::discrete_range_map::{PointType, RangeType};
use discrete_range_map::{DiscreteRangeSet, InclusiveInterval};

use crate::{interface::GapQueryIntervalTree, IdType};

#[derive(Debug, Clone)]
pub struct NaiveGapQueryIntervalTree<I, K, D> {
    pub(crate) inner: BTreeMap<D, DiscreteRangeSet<I, K>>,
}

impl<I, K, D> PartialEq for NaiveGapQueryIntervalTree<I, K, D>
where
    I: PartialEq,
    K: PartialEq,
    D: IdType,
{
    fn eq(&self, other: &Self) -> bool {
        self.inner == other.inner
    }
}

impl<I, K, D> GapQueryIntervalTree<I, K, D> for NaiveGapQueryIntervalTree<I, K, D>
where
    I: PointType,
    K: RangeType<I>,
    D: IdType,
{
    fn gap_query<Q>(&self, with_identifier: Option<D>, interval: Q) -> Vec<K>
    where
        Q: RangeType<I>,
    {
        let gaps = self.get_gaps(with_identifier);

        gaps.overlapping(interval).copied().collect()
    }

    fn insert(&mut self, identifiers: BTreeSet<D>, interval: K) {
        for identifier in identifiers {
            self.inner
                .entry(identifier)
                .or_default()
                .insert_merge_touching_or_overlapping(interval);
        }
    }
    fn cut<Q>(&mut self, with_identifiers: Option<BTreeSet<D>>, interval: Q)
    where
        Q: RangeType<I>,
    {
        match with_identifiers {
            Some(identifiers) => {
                for identifier in identifiers {
                    if let Some(set) = self.inner.get_mut(&identifier) {
                        let _ = set.cut(interval);
                    }
                }
            }
            None => {
                for set in self.inner.values_mut() {
                    let _ = set.cut(interval);
                }
            }
        }
    }

    fn append(&mut self, other: &mut Self) {
        for (identifier, intervals) in other.inner.extract_if(|_, _| true) {
            if !intervals.is_empty() {
                let store = self.inner.entry(identifier).or_default();
                for interval in intervals {
                    store.insert_merge_touching_or_overlapping(interval);
                }
            }
        }
    }

    fn identifiers_at_point(&self, at_point: I) -> BTreeSet<D> {
        self.inner
            .iter()
            .filter_map(|(identifier, intervals)| {
                if intervals.contains_point(at_point) {
                    Some(identifier)
                } else {
                    None
                }
            })
            .copied()
            .collect()
    }
}

impl<I, K, D> Default for NaiveGapQueryIntervalTree<I, K, D> {
    fn default() -> Self {
        Self {
            inner: BTreeMap::new(),
        }
    }
}

impl<I, K, D> NaiveGapQueryIntervalTree<I, K, D> {
    pub fn new() -> Self {
        Self::default()
    }
}

impl<I, K, D> NaiveGapQueryIntervalTree<I, K, D>
where
    I: PointType,
    K: RangeType<I>,
    D: IdType,
{
    fn get_gaps(&self, with_identifier: Option<D>) -> DiscreteRangeSet<I, K> {
        let mut total_intervals = DiscreteRangeSet::new();
        for other_identifier_intervals in
            self.inner
                .iter()
                .filter_map(|(other_identifier, intervals)| {
                    if let Some(identifier) = with_identifier.as_ref()
                        && identifier == other_identifier
                    {
                        None
                    } else {
                        Some(intervals)
                    }
                })
        {
            for interval in other_identifier_intervals.iter() {
                total_intervals.insert_merge_touching_or_overlapping(*interval);
            }
        }

        let gaps = total_intervals.gaps(InclusiveInterval {
            start: I::MIN,
            end: I::MAX,
        });

        let mut set = DiscreteRangeSet::new();
        for gap in gaps {
            set.insert_strict(gap).unwrap();
        }

        set
    }
}