lset 0.3.0

Data types describing linear sets
Documentation
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
418
419
420
421
422
423
424
425
426
427
// SPDX-License-Identifier: Apache-2.0

use super::*;
use core::{cmp::Ordering, ops::*};

/// Expresses a linear set by its start element and number of elements.
///
/// This type is fully isomorphic with `core::ops::Range` and `Line`. However,
/// unlike `core::ops::Range`, this type is not an iterator and therefore can
/// implement `Copy`.
#[repr(C)]
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq)]
pub struct Span<T, U = T> {
    /// The start element
    pub start: T,

    /// The number of elments
    pub count: U,
}

impl<T, U> Span<T, U> {
    /// Create a new span
    ///
    /// # Example
    ///
    /// ```
    /// let span = lset::Span::new(5, 10);
    /// assert_eq!(span.start, 5);
    /// assert_eq!(span.count, 10);
    /// ```
    #[inline(always)]
    pub const fn new(start: T, count: U) -> Self {
        Self { start, count }
    }

    /// Indicates whether the span is empty
    ///
    /// # Example
    ///
    /// ```
    /// use lset::*;
    /// assert!(Span::from(2..2).is_empty());
    /// assert!(!Span::from(2..3).is_empty());
    /// ```
    #[inline(always)]
    pub fn is_empty(&self) -> bool
    where
        T: Copy + PartialEq + Add<U, Output = T>,
        U: Copy,
    {
        self.start + self.count == self.start
    }
}

impl<T, U> Span<T, U>
where
    Span<T, U>: Into<Line<T>> + From<Line<T>>,
    T: PartialOrd,
{
    /// Returns the intersection between the sets, if any.
    ///
    /// ```
    /// use lset::*;
    ///
    /// let a = Span::new(0, 5);
    /// let b = Span::new(2, 5);
    /// let c = Span::new(5, 5);
    ///
    /// assert_eq!(a.intersection(b), Some(Span::new(2, 3)));
    /// assert_eq!(b.intersection(c), Some(Span::new(5, 2)));
    /// assert_eq!(a.intersection(a), Some(a));
    /// assert_eq!(b.intersection(b), Some(b));
    /// assert_eq!(c.intersection(c), Some(c));
    /// assert_eq!(a.intersection(c), None);
    /// ```
    pub fn intersection(self, other: Self) -> Option<Self> {
        self.into().intersection(other.into()).map(|x| x.into())
    }
}

impl<T: Add<T, Output = T>> Add<T> for Span<T> {
    type Output = Self;

    /// Grows the line by the size of the operand
    ///
    /// # Example
    ///
    /// ```
    /// let before = lset::Span::new(5, 10);
    /// let after = before + 5;
    /// assert_eq!(after.start, 5);
    /// assert_eq!(after.count, 15);
    /// ```
    #[inline(always)]
    fn add(self, rhs: T) -> Self::Output {
        Self {
            start: self.start,
            count: self.count + rhs,
        }
    }
}

impl<T: AddAssign<T>> AddAssign<T> for Span<T> {
    /// Grows the line by the size of the operand
    ///
    /// # Example
    ///
    /// ```
    /// let mut span = lset::Span::new(5, 10);
    /// span += 5;
    /// assert_eq!(span.start, 5);
    /// assert_eq!(span.count, 15);
    /// ```
    #[inline(always)]
    fn add_assign(&mut self, rhs: T) {
        self.count += rhs;
    }
}

impl<T: Sub<T, Output = T>> Sub<T> for Span<T> {
    type Output = Self;

    /// Shrinks the line by the size of the operand
    ///
    /// # Example
    ///
    /// ```
    /// let before = lset::Span::new(5, 10);
    /// let after = before - 5;
    /// assert_eq!(after.start, 5);
    /// assert_eq!(after.count, 5);
    /// ```
    #[inline(always)]
    fn sub(self, rhs: T) -> Self::Output {
        Self {
            start: self.start,
            count: self.count - rhs,
        }
    }
}

impl<T: SubAssign<T>> SubAssign<T> for Span<T> {
    /// Shrinks the line by the size of the operand
    ///
    /// # Example
    ///
    /// ```
    /// let mut span = lset::Span::new(5, 10);
    /// span -= 5;
    /// assert_eq!(span.start, 5);
    /// assert_eq!(span.count, 5);
    /// ```
    #[inline(always)]
    fn sub_assign(&mut self, rhs: T) {
        self.count -= rhs;
    }
}

impl<T: Copy + Sub<T, Output = T>> Shl<T> for Span<T> {
    type Output = Self;

    /// Shifts the line downwards without changing size
    ///
    /// # Example
    ///
    /// ```
    /// let before = lset::Span::new(5, 10);
    /// let after = before << 5;
    /// assert_eq!(after.start, 0);
    /// assert_eq!(after.count, 10);
    /// ```
    #[inline(always)]
    #[allow(clippy::suspicious_arithmetic_impl)]
    fn shl(self, rhs: T) -> Self::Output {
        Self {
            start: self.start - rhs,
            count: self.count,
        }
    }
}

impl<T: Copy + SubAssign<T>> ShlAssign<T> for Span<T> {
    /// Shifts the line downwards without changing size
    ///
    /// # Example
    ///
    /// ```
    /// let mut span = lset::Span::new(5, 10);
    /// span <<= 5;
    /// assert_eq!(span.start, 0);
    /// assert_eq!(span.count, 10);
    /// ```
    #[inline(always)]
    #[allow(clippy::suspicious_op_assign_impl)]
    fn shl_assign(&mut self, rhs: T) {
        self.start -= rhs;
    }
}

impl<T: Copy + Add<T, Output = T>> Shr<T> for Span<T> {
    type Output = Self;

    /// Shifts the line upwards without changing size
    ///
    /// # Example
    ///
    /// ```
    /// let before = lset::Span::new(5, 10);
    /// let after = before >> 5;
    /// assert_eq!(after.start, 10);
    /// assert_eq!(after.count, 10);
    /// ```
    #[inline(always)]
    #[allow(clippy::suspicious_arithmetic_impl)]
    fn shr(self, rhs: T) -> Self::Output {
        Self {
            start: self.start + rhs,
            count: self.count,
        }
    }
}

impl<T: Copy + AddAssign<T>> ShrAssign<T> for Span<T> {
    /// Shifts the line upwards without changing size
    ///
    /// # Example
    ///
    /// ```
    /// let mut span = lset::Span::new(5, 10);
    /// span >>= 5;
    /// assert_eq!(span.start, 10);
    /// assert_eq!(span.count, 10);
    /// ```
    #[inline(always)]
    #[allow(clippy::suspicious_op_assign_impl)]
    fn shr_assign(&mut self, rhs: T) {
        self.start += rhs;
    }
}

impl<T: PartialEq, U: PartialEq> PartialOrd for Span<T, U>
where
    Span<T, U>: Copy + Into<Line<T>>,
    Line<T>: PartialOrd,
{
    /// Compares two `Span` types.
    ///
    /// # Example
    ///
    /// ```
    /// use lset::*;
    /// assert!(Span::new(5, 5) <= Span::new(5, 5));
    /// assert!(Span::new(5, 5) >= Span::new(5, 5));
    /// assert!(Span::new(5, 5) < Span::new(10, 5));
    /// assert!(Span::new(10, 5) > Span::new(5, 5));
    /// ```
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        (*self).into().partial_cmp(&(*other).into())
    }
}

impl<T: Copy + Sub<T, Output = U>, U> From<Range<T>> for Span<T, U> {
    /// Converts a `Range` into a `Span`
    ///
    /// # Example
    ///
    /// ```
    /// use lset::*;
    /// assert_eq!(Span::new(5, 10), Span::from(5..15));
    /// assert_eq!(Span::new(5, 10), (5..15).into());
    /// ```
    #[inline(always)]
    fn from(value: Range<T>) -> Self {
        Self {
            start: value.start,
            count: value.end - value.start,
        }
    }
}

impl<T: Clone + Add<U, Output = T>, U> From<Span<T, U>> for Range<T> {
    /// Converts a `Span` into a `Range`
    ///
    /// # Example
    ///
    /// ```
    /// use lset::*;
    /// assert_eq!(5..15, std::ops::Range::from(Span::new(5, 10)));
    /// assert_eq!(5..15, Span::new(5, 10).into());
    /// ```
    #[inline(always)]
    fn from(value: Span<T, U>) -> Self {
        Self {
            start: value.start.clone(),
            end: value.start + value.count,
        }
    }
}

impl<T: Clone + Sub<T, Output = U>, U> From<Line<T>> for Span<T, U> {
    /// Converts a `Line` into a `Span`
    ///
    /// # Example
    ///
    /// ```
    /// use lset::*;
    /// assert_eq!(Span::new(5, 10), Span::from(Line::new(5, 15)));
    /// assert_eq!(Span::new(5, 10), (Line::new(5, 15)).into());
    /// ```
    #[inline(always)]
    fn from(value: Line<T>) -> Self {
        Self {
            start: value.start.clone(),
            count: value.end - value.start,
        }
    }
}

impl<T, U> Contains<T> for Span<T, U>
where
    Self: Into<Line<T>> + Clone,
    Line<T>: Contains<T>,
{
    /// Indicates whether the span contains a point
    ///
    /// # Example
    ///
    /// ```
    /// use lset::*;
    /// assert!(!Span::from(2..3).contains(&1));
    /// assert!(Span::from(2..3).contains(&2));
    /// assert!(!Span::from(2..3).contains(&3));

    /// assert!(!Span::from(3..2).contains(&1));
    /// assert!(!Span::from(3..2).contains(&2));
    /// assert!(!Span::from(3..2).contains(&3));
    /// ```
    #[inline(always)]
    fn contains(&self, value: &T) -> bool {
        self.clone().into().contains(value)
    }
}

impl<T, U> Contains<Self> for Span<T, U>
where
    Self: Into<Line<T>> + Clone,
    T: PartialOrd,
{
    /// Indicates whether the span contains another span
    ///
    /// # Example
    ///
    /// ```
    /// use lset::*;
    /// assert!(Span::from(4..8).contains(&Span::from(5..7)));
    /// assert!(Span::from(4..8).contains(&Span::from(4..7)));
    /// assert!(Span::from(4..8).contains(&Span::from(5..8)));
    /// assert!(Span::from(4..8).contains(&Span::from(4..8)));
    /// assert!(!Span::from(4..8).contains(&Span::from(3..8)));
    /// assert!(!Span::from(4..8).contains(&Span::from(4..9)));
    /// assert!(!Span::from(4..8).contains(&Span::from(3..9)));
    /// assert!(!Span::from(4..8).contains(&Span::from(2..10)));
    /// assert!(!Span::from(4..8).contains(&Span::from(6..5)));
    /// assert!(!Span::from(7..3).contains(&Span::from(5..6)));
    /// ```
    #[inline(always)]
    fn contains(&self, value: &Self) -> bool {
        self.clone().into().contains(&value.clone().into())
    }
}

impl<T, U> Split<Self> for Span<T, U>
where
    Self: Into<Line<T>>,
    Line<T>: Into<Self>,
    Line<T>: Split<Line<T>>,
{
    /// Splits a span by another span
    ///
    /// # Example
    ///
    /// ```
    /// use lset::*;
    /// let span = Span::from(2..5);
    /// assert_eq!(span.split(Span::from(1..4)), None);
    /// assert_eq!(span.split(Span::from(3..6)), None);
    /// assert_eq!(span.split(Span::from(2..2)), None);
    /// assert_eq!(span.split(Span::from(2..3)), Some((Span::from(2..2), Span::from(3..5))));
    /// assert_eq!(span.split(Span::from(3..3)), None);
    /// assert_eq!(span.split(Span::from(3..4)), Some((Span::from(2..3), Span::from(4..5))));
    /// assert_eq!(span.split(Span::from(4..4)), None);
    /// assert_eq!(span.split(Span::from(4..5)), Some((Span::from(2..4), Span::from(5..5))));
    /// assert_eq!(span.split(Span::from(5..5)), None);
    /// assert_eq!(span.split(span), Some((Span::from(2..2), Span::from(5..5))));
    /// ```
    #[inline(always)]
    fn split(self, at: Self) -> Option<(Self, Self)> {
        let (l, r) = self.into().split(at.into())?;
        Some((l.into(), r.into()))
    }
}

impl<T, U> Split<U> for Span<T, U>
where
    T: Add<U, Output = T> + Clone,
    Line<T>: Split<T> + Into<Self>,
    Self: Into<Line<T>>,
{
    /// Splits a span at a offset
    ///
    /// # Example
    ///
    /// ```
    /// use lset::*;
    /// let span = Span::from(2..4);
    /// assert_eq!(span.split(0), Some((Span::from(2..2), span)));
    /// assert_eq!(span.split(1), Some((Span::from(2..3), Span::from(3..4))));
    /// assert_eq!(span.split(2), Some((span, Span::from(4..4))));
    /// assert_eq!(span.split(3), None);
    /// ```
    #[inline(always)]
    fn split(self, at: U) -> Option<(Self, Self)> {
        let e = self.start.clone() + at;
        let (l, r) = self.into().split(e)?;
        Some((l.into(), r.into()))
    }
}