1use crate::{generic::RangeMap, AnyRange, AsRange, IntoRange, RangePartialOrd};
2use btree_slab::generic::Node;
3use cc_traits::{SimpleCollectionMut, SimpleCollectionRef, Slab, SlabMut};
4use range_traits::{Bounded, Measure, PartialEnum};
5use std::{
6 cmp::Ordering,
7 fmt,
8 hash::{Hash, Hasher},
9};
10
11#[derive(Clone)]
15pub struct RangeSet<T, C> {
16 map: RangeMap<T, (), C>,
17}
18
19impl<T, C> RangeSet<T, C> {
20 pub fn new() -> RangeSet<T, C>
21 where
22 C: Default,
23 {
24 RangeSet {
25 map: RangeMap::new(),
26 }
27 }
28}
29
30impl<T, C: Default> Default for RangeSet<T, C> {
31 fn default() -> Self {
32 Self::new()
33 }
34}
35
36impl<T, C: Slab<Node<AnyRange<T>, ()>>> RangeSet<T, C>
37where
38 C: SimpleCollectionRef,
39{
40 pub fn range_count(&self) -> usize {
41 self.map.range_count()
42 }
43
44 pub fn len(&self) -> T::Len
45 where
46 T: Measure + PartialEnum + Bounded,
47 {
48 self.map.len()
49 }
50
51 pub fn bounded_len(&self) -> Option<T::Len>
52 where
53 T: Measure + PartialEnum,
54 {
55 self.map.bounded_len()
56 }
57
58 pub fn is_empty(&self) -> bool
59 where
60 T: Measure + PartialEnum,
61 {
62 self.map.is_empty()
63 }
64
65 pub fn intersects<R: AsRange<Item = T>>(&self, values: R) -> bool
66 where
67 T: Clone + PartialEnum + Measure,
68 {
69 self.map.intersects(values)
70 }
71
72 pub fn contains(&self, value: T) -> bool
73 where
74 T: Clone + PartialEnum + RangePartialOrd + Measure,
75 {
76 self.map.contains_key(value)
77 }
78
79 pub fn iter(&self) -> Iter<T, C> {
80 Iter {
81 inner: self.map.iter(),
82 }
83 }
84
85 pub fn gaps(&self) -> Gaps<T, C> {
87 self.map.gaps()
88 }
89}
90
91impl<T: fmt::Debug, C: Slab<Node<AnyRange<T>, ()>>> fmt::Debug for RangeSet<T, C>
92where
93 C: SimpleCollectionRef,
94{
95 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
96 write!(f, "{{")?;
97
98 for range in self {
99 write!(f, "{:?}", range)?
100 }
101
102 write!(f, "}}")
103 }
104}
105
106impl<'a, T, C: Slab<Node<AnyRange<T>, ()>>> IntoIterator for &'a RangeSet<T, C>
107where
108 C: SimpleCollectionRef,
109{
110 type Item = &'a AnyRange<T>;
111 type IntoIter = Iter<'a, T, C>;
112
113 fn into_iter(self) -> Self::IntoIter {
114 self.iter()
115 }
116}
117
118impl<T, C: SlabMut<Node<AnyRange<T>, ()>>> RangeSet<T, C>
119where
120 C: SimpleCollectionRef,
121 C: SimpleCollectionMut,
122{
123 pub fn insert<R: IntoRange<Item = T>>(&mut self, key: R)
124 where
125 T: Clone + PartialEnum + Measure,
126 {
127 self.map.insert(key, ())
128 }
129
130 pub fn remove<R: AsRange<Item = T>>(&mut self, key: R)
131 where
132 T: Clone + PartialEnum + Measure,
133 {
134 self.map.remove(key)
135 }
136
137 pub fn complement(&self) -> Self
138 where
139 T: Clone + Measure + PartialEnum,
140 C: Default,
141 {
142 self.gaps().map(AnyRange::cloned).collect()
143 }
144}
145
146impl<K, L, C: Slab<Node<AnyRange<K>, ()>>, D: Slab<Node<AnyRange<L>, ()>>> PartialEq<RangeSet<L, D>>
147 for RangeSet<K, C>
148where
149 L: Measure<K> + PartialOrd<K> + PartialEnum,
150 K: PartialEnum,
151 C: SimpleCollectionRef,
152 D: SimpleCollectionRef,
153{
154 fn eq(&self, other: &RangeSet<L, D>) -> bool {
155 self.map == other.map
156 }
157}
158
159impl<K, C: Slab<Node<AnyRange<K>, ()>>> Eq for RangeSet<K, C>
160where
161 K: Measure + PartialEnum + Ord,
162 C: SimpleCollectionRef,
163{
164}
165
166impl<K, L, C: Slab<Node<AnyRange<K>, ()>>, D: Slab<Node<AnyRange<L>, ()>>>
167 PartialOrd<RangeSet<L, D>> for RangeSet<K, C>
168where
169 L: Measure<K> + PartialOrd<K> + PartialEnum,
170 K: PartialEnum,
171 C: SimpleCollectionRef,
172 D: SimpleCollectionRef,
173{
174 fn partial_cmp(&self, other: &RangeSet<L, D>) -> Option<Ordering> {
175 self.map.partial_cmp(&other.map)
176 }
177}
178
179impl<K, C: Slab<Node<AnyRange<K>, ()>>> Ord for RangeSet<K, C>
180where
181 K: Measure + PartialEnum + Ord,
182 C: SimpleCollectionRef,
183{
184 fn cmp(&self, other: &Self) -> Ordering {
185 self.map.cmp(&other.map)
186 }
187}
188
189impl<K, C: Slab<Node<AnyRange<K>, ()>>> Hash for RangeSet<K, C>
190where
191 K: Hash + PartialEnum,
192 C: SimpleCollectionRef,
193{
194 fn hash<H: Hasher>(&self, h: &mut H) {
195 self.map.hash(h)
196 }
197}
198
199impl<T, C: SlabMut<Node<AnyRange<T>, ()>>> IntoIterator for RangeSet<T, C>
200where
201 C: SimpleCollectionRef,
202 C: SimpleCollectionMut,
203{
204 type Item = AnyRange<T>;
205 type IntoIter = IntoIter<T, C>;
206
207 fn into_iter(self) -> Self::IntoIter {
208 IntoIter {
209 inner: self.map.into_iter(),
210 }
211 }
212}
213
214pub struct Iter<'a, T, C> {
215 inner: crate::generic::map::Iter<'a, T, (), C>,
216}
217
218impl<'a, T, C: Slab<Node<AnyRange<T>, ()>>> Iterator for Iter<'a, T, C>
219where
220 C: SimpleCollectionRef,
221{
222 type Item = &'a AnyRange<T>;
223
224 fn next(&mut self) -> Option<Self::Item> {
225 match self.inner.next() {
226 Some((range, ())) => Some(range),
227 None => None,
228 }
229 }
230}
231
232pub struct IntoIter<T, C> {
233 inner: crate::generic::map::IntoIter<T, (), C>,
234}
235
236impl<T, C: SlabMut<Node<AnyRange<T>, ()>>> Iterator for IntoIter<T, C>
237where
238 C: SimpleCollectionRef,
239 C: SimpleCollectionMut,
240{
241 type Item = AnyRange<T>;
242
243 fn next(&mut self) -> Option<Self::Item> {
244 self.inner.next().map(|(range, _)| range)
245 }
246}
247
248pub type Gaps<'a, T, C> = crate::generic::map::Gaps<'a, T, (), C>;
250
251impl<R: IntoRange, C: SlabMut<Node<AnyRange<R::Item>, ()>>> std::iter::Extend<R>
252 for RangeSet<R::Item, C>
253where
254 R::Item: Clone + Measure + PartialOrd,
255 C: SimpleCollectionRef,
256 C: SimpleCollectionMut,
257{
258 fn extend<I: IntoIterator<Item = R>>(&mut self, iter: I) {
259 for range in iter {
260 self.insert(range)
261 }
262 }
263}
264
265impl<R: IntoRange, C: Default + SlabMut<Node<AnyRange<R::Item>, ()>>> FromIterator<R>
266 for RangeSet<R::Item, C>
267where
268 R::Item: Clone + Measure + PartialOrd,
269 C: SimpleCollectionRef,
270 C: SimpleCollectionMut,
271{
272 fn from_iter<I: IntoIterator<Item = R>>(iter: I) -> Self {
273 let mut result = Self::default();
274 result.extend(iter);
275 result
276 }
277}
278
279#[cfg(test)]
280mod test {
281 use crate::{AnyRange, RangeSet};
282
283 #[test]
284 fn gaps1() {
285 let mut a: RangeSet<u8> = RangeSet::new();
286 let mut b: RangeSet<u8> = RangeSet::new();
287
288 a.insert(10..20);
289
290 b.insert(0..10);
291 b.insert(20..);
292
293 assert_eq!(a.complement(), b)
294 }
295
296 #[test]
297 fn gaps2() {
298 let mut a: RangeSet<u8> = RangeSet::new();
299 let mut b: RangeSet<u8> = RangeSet::new();
300
301 a.insert(0..10);
302 b.insert(10..);
303
304 assert_eq!(a.complement(), b)
305 }
306
307 #[test]
308 fn gaps3() {
309 let mut a: RangeSet<u8> = RangeSet::new();
310 let mut b: RangeSet<u8> = RangeSet::new();
311
312 a.insert(20..);
313 b.insert(0..=19);
314
315 assert_eq!(a.complement(), b)
316 }
317
318 #[test]
319 fn gaps4() {
320 let mut a: RangeSet<u8> = RangeSet::new();
321
322 a.insert(10..20);
323
324 let mut gaps = a.gaps().map(AnyRange::cloned);
325 assert_eq!(gaps.next(), Some(AnyRange::from(..10u8)));
326 assert_eq!(gaps.next(), Some(AnyRange::from(20u8..)));
327 assert_eq!(gaps.next(), None)
328 }
329
330 #[test]
331 fn gaps5() {
332 let mut a: RangeSet<u8> = RangeSet::new();
333
334 a.insert(..10);
335 a.insert(20..);
336
337 let mut gaps = a.gaps().map(AnyRange::cloned);
338 assert_eq!(gaps.next(), Some(AnyRange::from(10..20)));
339 assert_eq!(gaps.next(), None)
340 }
341
342 #[test]
343 fn gaps6() {
344 let mut a: RangeSet<u8> = RangeSet::new();
345
346 a.insert(10..20);
347
348 assert_eq!(a.complement().complement(), a)
349 }
350}