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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use core::fmt::{self, Debug};

use crate::{
    map::{Key, MaybeMap},
    Bound::{self, *},
    RangeBounds, Segment, SegmentMap,
};

pub mod iterators;
pub mod ops;

#[cfg(test)]
mod tests;

/// # SegmentSet
///
/// A set based on a [`SegmentMap`]. Like [`SegmentMap`], adjacent ranges will be
/// merged into a single range.
///
/// See [`SegmentMap`]'s documentation for more details on implementation. The
/// internal representation of this `struct` is is a `SegmentMap<T, ()>`
///
/// # Examples
///
/// ```
/// # use segmap::*;
/// let mut set = SegmentSet::new();
///
/// // Add some ranges
/// set.insert(0..5);
/// set.insert(5..10); // Note, this will be merged with 0..5!
/// set.insert(20..25);
///
/// // Check if a point is covered
/// assert!(set.contains(&7));
/// assert!(!set.contains(&12));
///
/// // Remove a range (or parts of some ranges)
/// assert!(set.contains(&5));
/// assert!(set.contains(&24));
/// set.remove(3..6);
/// set.remove(22..);
/// assert!(!set.contains(&5));
/// assert!(!set.contains(&24));
///
/// // Check which ranges are covered
/// assert!(set.into_iter().eq(vec![
///     Segment::from(0..3),
///     Segment::from(6..10),
///     Segment::from(20..22),
/// ]));
/// ```
///
#[derive(Clone)]
pub struct SegmentSet<T> {
    pub(crate) map: SegmentMap<T, ()>,
}

impl<T> SegmentSet<T> {
    /// Makes a new empty `SegmentSet`.
    pub fn new() -> Self
    where
        T: Ord,
    {
        SegmentSet {
            map: SegmentMap::new(),
        }
    }

    /// Make a new `SegmentSet` with a single range present, representing all
    /// possible values.
    ///
    /// ```
    /// # use segmap::*;
    ///
    /// // Thus, this
    /// let full = SegmentSet::<u32>::full();
    ///
    /// // Is equivalent to
    /// let mut manual = SegmentSet::new();
    /// manual.insert(..);
    ///
    /// assert_eq!(full, manual);
    /// ```
    pub fn full() -> Self
    where
        T: Ord,
    {
        let mut set = Self::new();
        set.map
            .map
            .insert(Key(Segment::new(Unbounded, Unbounded)), ());
        set
    }

    /// Clears the set, removing all elements.
    ///
    /// # Examples
    ///
    /// ```
    /// # use segmap::*;
    /// let mut set = SegmentSet::new();
    /// set.insert(0..1);
    /// set.clear();
    /// assert!(set.is_empty());
    /// ```
    pub fn clear(&mut self) {
        self.map.clear()
    }

    /// Returns `true` if any range in the set covers the specified value.
    ///
    /// # Examples
    ///
    /// ```
    /// # use segmap::*;
    /// let mut set = SegmentSet::new();
    /// set.insert(0..5);
    /// assert!(set.contains(&3));
    /// ```
    pub fn contains(&self, value: &T) -> bool
    where
        T: Clone + Ord,
    {
        self.map.contains(value)
    }

    /// Returns a reference to the range covering the given value, if any.
    ///
    /// # Examples
    ///
    /// ```
    /// # use segmap::*;
    /// let mut set = SegmentSet::new();
    /// set.insert(0..5);
    ///
    /// assert_eq!(set.get_range_for(&3), Some(&Segment::from(0..5)));
    /// assert!(set.get_range_for(&6).is_none());
    /// ```
    pub fn get_range_for(&self, value: &T) -> Option<&Segment<T>>
    where
        T: Clone + Ord,
    {
        self.map.get_range_value(value).map(|(range, _)| range)
    }

    /// Insert a range into the set.
    ///
    /// If the inserted range either overlaps or is immediately adjacent
    /// any existing range, then the ranges will be coalesced into
    /// a single contiguous range.
    ///
    /// # Examples
    ///
    /// ```
    /// # use segmap::*;
    /// let mut set = SegmentSet::new();
    /// set.insert(0..5);
    /// assert!(!set.is_empty())
    /// ```
    ///
    /// # See Also
    ///
    /// - [`SegmentMap::insert`] and [`SegmentMap::set`] for the internal map's
    /// insertion semantics. Because values are always `()` and returning
    /// overwritten values is not necessary, this method uses `set`.
    ///
    pub fn insert<R>(&mut self, range: R)
    where
        R: RangeBounds<T>,
        T: Clone + Ord,
    {
        self.map.set(range, ())
    }

    /// Removes a range from the set returning if all or any of it was present.
    ///
    /// # Examples
    ///
    /// ```
    /// # use segmap::*;
    /// let mut set = SegmentSet::new();
    /// set.insert(0..5);
    /// assert!(set.remove(0..2));
    /// ```
    ///
    /// # See Also
    ///
    /// - [`SegmentMap::remove`] and [`SegmentMap::clear_range`] for the internal map's
    /// removal semantics. However, this method will not allocate anything to
    /// return.
    /// - [`SegmentSet::take`] if you want the removed elements
    ///
    pub fn remove<R>(&mut self, range: R) -> bool
    where
        R: RangeBounds<T>,
        T: Clone + Ord,
    {
        let mut removed_ranges = MaybeMap::None;
        self.map
            .remove_internal(Segment::from(&range), &mut removed_ranges);
        removed_ranges.into()
    }

    /// Removes a range from the set, returning a set containing the removed
    /// elements
    ///
    /// # Examples
    ///
    /// ```
    /// # use segmap::*;
    /// let mut set = SegmentSet::new();
    /// set.insert(0..5);
    /// let removed = set.take(0..2);
    ///
    ///
    /// ```
    ///
    /// # See Also
    ///
    /// - [`SegmentMap::remove`] and [`SegmentMap::clear_range`] for the internal map's
    /// removal semantics. However, this method will not allocate anything to
    /// return.
    /// - [`SegmentSet::remove`] if you don't want the removed elements
    ///
    pub fn take<R>(&mut self, range: R) -> Self
    where
        R: RangeBounds<T>,
        T: Clone + Ord,
    {
        Self {
            map: self.map.remove(range).unwrap_or_default(),
        }
    }

    /// Retains only the elements specified by the predicate.
    ///
    /// In other words, remove all ranges `f(v)` returns `false`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use segmap::*;
    /// let mut set = SegmentSet::new();
    /// set.insert(0..4);
    /// set.insert(5..9);
    /// set.insert(10..14);
    /// set.insert(15..19);
    /// set.insert(20..24);
    ///
    /// // Keep only the ranges with even numbered starts
    /// set.retain(|r| r.start_value().unwrap() % 2 == 0);
    ///
    /// assert!(set.contains(&0));
    /// assert!(set.contains(&10));
    /// assert!(set.contains(&12));
    /// assert!(set.contains(&20));
    /// assert!(set.contains(&23));
    ///
    /// assert!(!set.contains(&15));
    /// ```
    ///
    /// # See Also
    ///
    /// - [`SegmentMap::retain`], which is called internally
    ///
    pub fn retain<F>(&mut self, mut f: F)
    where
        T: Ord,
        F: FnMut(&Segment<T>) -> bool,
    {
        self.map.retain(|r, _| f(r))
    }

    /// Moves all elements from `other` into `Self`, leaving `other` empty.
    ///
    /// # Examples
    ///
    /// ```
    /// # use segmap::*;
    /// let mut a = SegmentSet::new();
    /// a.insert(0..1);
    /// a.insert(1..2);
    /// a.insert(2..3);
    ///
    /// let mut b = SegmentSet::new();
    /// b.insert(2..3);
    /// b.insert(3..4);
    /// b.insert(4..5);
    ///
    /// a.append(&mut b);
    ///
    /// // Ranges in a should all be coalesced to 0..5
    /// assert!(a.into_iter().eq(vec![
    ///     Segment::from(0..5)
    /// ]));
    /// assert!(b.is_empty());
    /// ```
    pub fn append(&mut self, other: &mut Self)
    where
        T: Clone + Ord,
    {
        self.map.append(&mut other.map)
    }

    /// Split the set into two at the given bound. Returns everything including
    /// and after that bound.
    ///
    /// # Examples
    ///
    /// # Basic Usage
    ///
    /// ```
    /// # use segmap::*;
    /// let mut a = SegmentSet::new();
    /// a.insert(0..1);
    /// a.insert(2..3);
    /// a.insert(4..5);
    /// a.insert(6..7);
    ///
    /// let b = a.split_off(Bound::Included(4));
    ///
    /// assert!(a.into_iter().eq(vec![
    ///     Segment::from(0..1),
    ///     Segment::from(2..3),
    /// ]));
    /// assert!(b.into_iter().eq(vec![
    ///     Segment::from(4..5),
    ///     Segment::from(6..7),
    /// ]));
    /// ```
    ///
    /// ## Mixed Bounds
    ///
    /// ```
    /// # use segmap::*;
    /// let mut a = SegmentSet::new();
    /// a.insert(0..7);
    ///
    /// let c = a.split_off(Bound::Excluded(4));
    /// let b = a.split_off(Bound::Included(2));
    ///
    /// assert!(a.into_iter().eq(vec![
    ///     Segment::from(0..2)
    /// ]));
    /// assert!(b.into_iter().eq(vec![
    ///     Segment::from(2..=4)
    /// ]));
    /// assert!(c.into_iter().eq(vec![
    ///     Segment::new(Bound::Excluded(4), Bound::Excluded(7))
    /// ]));
    /// ```
    ///
    pub fn split_off(&mut self, at: Bound<T>) -> Self
    where
        T: Clone + Ord,
    {
        Self {
            map: self.map.split_off(at),
        }
    }

    // TODO: split_off_range
}

impl<T: Clone + Ord> SegmentSet<&T> {
    pub fn cloned(&self) -> SegmentSet<T> {
        SegmentSet {
            map: SegmentMap {
                map: self.map.map.iter().map(|(k, _)| (k.cloned(), ())).collect(),
                store: alloc::vec::Vec::with_capacity(self.map.store.len()),
            },
        }
    }
}

impl<T> Default for SegmentSet<T>
where
    T: Clone + Ord,
{
    fn default() -> Self {
        SegmentSet::new()
    }
}

impl<K, V> From<SegmentMap<K, V>> for SegmentSet<K>
where
    K: Ord,
{
    fn from(map: SegmentMap<K, V>) -> Self {
        let SegmentMap { map, store } = map;
        SegmentSet {
            map: SegmentMap {
                map: map.into_iter().map(|(k, _)| (k, ())).collect(),
                store,
            },
        }
    }
}

// We can't just derive this automatically, because that would
// expose irrelevant (and private) implementation details.
// Instead implement it in the same way that the underlying BTreeSet does.
impl<T: Debug> Debug for SegmentSet<T>
where
    T: Ord + Clone,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_set().entries(self.iter()).finish()
    }
}

impl<T: PartialEq> PartialEq for SegmentSet<T> {
    fn eq(&self, other: &Self) -> bool {
        self.map == other.map
    }
}