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
/**
 * Ranges can be used as collections of read-only indices that can be truncated from either end.
 * It can be useful to specify chunked ranges or a selection from a range.
 * Since our collections must know about the length, only finite ranges are supported.
 */
use super::*;
use std::ops::{Range, RangeInclusive, RangeTo, RangeToInclusive};

impl<T: IntBound> BoundedRange for Range<T> {
    type Index = T;
    fn start(&self) -> Self::Index {
        self.start.clone()
    }
    fn end(&self) -> Self::Index {
        self.end.clone()
    }
}

impl<T: IntBound> BoundedRange for RangeInclusive<T> {
    type Index = T;
    fn start(&self) -> Self::Index {
        RangeInclusive::start(self).clone()
    }
    fn end(&self) -> Self::Index {
        RangeInclusive::end(self).clone() + 1
    }
}

impl<T: IntBound> BoundedRange for RangeTo<T> {
    type Index = T;
    fn start(&self) -> Self::Index {
        0usize.into()
    }
    fn end(&self) -> Self::Index {
        self.end.clone()
    }
}

impl<T: IntBound> BoundedRange for RangeToInclusive<T> {
    type Index = T;
    fn start(&self) -> Self::Index {
        0usize.into()
    }
    fn end(&self) -> Self::Index {
        self.end.clone() + 1
    }
}

macro_rules! impls_for_range {
    ($range:ident) => {
        impl<T> ValueType for $range<T> {}
        impl<I: IntBound> Set for $range<I> {
            type Elem = <Self as BoundedRange>::Index;
            type Atom = <Self as BoundedRange>::Index;
            fn len(&self) -> usize {
                (BoundedRange::end(self) - BoundedRange::start(self)).into()
            }
        }

        impl<N, I> UniChunkable<N> for $range<I> {
            type Chunk = StaticRange<N>;
        }
        impl<'a, I: IntBound> View<'a> for $range<I> {
            type Type = Self;
            fn view(&'a self) -> Self::Type {
                self.clone()
            }
        }
    };
}

impls_for_range!(Range);
impls_for_range!(RangeInclusive);
impls_for_range!(RangeTo);
impls_for_range!(RangeToInclusive);

impl<'a, R> GetIndex<'a, R> for usize
where
    R: BoundedRange + Set,
{
    type Output = R::Index;
    fn get(self, rng: &R) -> Option<Self::Output> {
        if self >= rng.len() {
            return None;
        }
        Some(rng.start() + self)
    }
}

impl<'a, R> GetIndex<'a, R> for Range<usize>
where
    R: BoundedRange + Set,
{
    type Output = Range<R::Index>;
    fn get(self, rng: &R) -> Option<Self::Output> {
        if self.end > rng.len() {
            return None;
        }
        Some(Range {
            start: rng.start() + self.start,
            end: rng.start() + self.end,
        })
    }
}

impl<I, N> SplitPrefix<N> for Range<I>
where
    I: IntBound + Default + Copy + From<usize>,
    std::ops::RangeFrom<I>: Iterator<Item = I>,
    N: Unsigned + Array<I>,
    <N as Array<I>>::Array: Default,
{
    type Prefix = N::Array;

    fn split_prefix(self) -> Option<(Self::Prefix, Self)> {
        if self.len() < N::to_usize() {
            return None;
        }

        let std::ops::Range { start, end } = self;

        let mut prefix: N::Array = Default::default();
        for (i, item) in (start.clone()..).zip(N::iter_mut(&mut prefix)) {
            *item = i;
        }

        let start = start.clone().into();

        let rest = Range {
            start: (start + N::to_usize()).into(),
            end,
        };

        Some((prefix, rest))
    }
}

impl<I, N> IntoStaticChunkIterator<N> for Range<I>
where
    Self: Set + SplitPrefix<N> + Dummy,
    N: Unsigned,
{
    type Item = <Self as SplitPrefix<N>>::Prefix;
    type IterType = UniChunkedIter<Self, N>;
    fn into_static_chunk_iter(self) -> Self::IterType {
        self.into_generic_static_chunk_iter()
    }
}

impl<T> IntoFlat for Range<T> {
    type FlatType = Range<T>;
    fn into_flat(self) -> Self::FlatType {
        self
    }
}

impl<T> SplitAt for Range<T>
where
    T: From<usize>,
{
    fn split_at(self, mid: usize) -> (Self, Self) {
        let Range { start, end } = self;
        (
            Range {
                start,
                end: mid.into(),
            },
            Range {
                start: mid.into(),
                end,
            },
        )
    }
}

impl<T> Dummy for Range<T>
where
    T: Default,
{
    unsafe fn dummy() -> Self {
        Range {
            start: T::default(),
            end: T::default(),
        }
    }
}

impl<T> Dummy for RangeTo<T>
where
    T: Default,
{
    unsafe fn dummy() -> Self {
        RangeTo { end: T::default() }
    }
}

impl<T> RemovePrefix for Range<T>
where
    T: From<usize>,
{
    fn remove_prefix(&mut self, n: usize) {
        self.start = n.into();
    }
}

impl<'a, T: Clone> StorageView<'a> for Range<T> {
    type StorageView = Self;
    fn storage_view(&'a self) -> Self::StorageView {
        self.clone()
    }
}

impl<T> Storage for Range<T> {
    type Storage = Range<T>;
    /// A range is a type of storage, simply return an immutable reference to self.
    fn storage(&self) -> &Self::Storage {
        self
    }
}

impl<T> StorageMut for Range<T> {
    /// A range is a type of storage, simply return a mutable reference to self.
    fn storage_mut(&mut self) -> &mut Self::Storage {
        self
    }
}

impl<T: IntBound> Truncate for Range<T> {
    /// Truncate the range to a specified length.
    fn truncate(&mut self, new_len: usize) {
        self.end = self.start.clone() + new_len;
    }
}

impl<T: IntBound> IsolateIndex<Range<T>> for usize {
    type Output = T;
    fn try_isolate(self, rng: Range<T>) -> Option<Self::Output> {
        if self < rng.distance().into() {
            Some(rng.start + self)
        } else {
            None
        }
    }
}

impl<T: IntBound> IsolateIndex<Range<T>> for std::ops::Range<usize> {
    type Output = Range<T>;

    fn try_isolate(self, rng: Range<T>) -> Option<Self::Output> {
        if self.start >= rng.distance().into() || self.end > rng.distance().into() {
            return None;
        }

        Some(Range {
            start: rng.start.clone() + self.start,
            end: rng.start + self.end,
        })
    }
}

impl<T: IntBound> IsolateIndex<RangeTo<T>> for usize {
    type Output = T;
    fn try_isolate(self, rng: RangeTo<T>) -> Option<Self::Output> {
        if self < rng.distance().into() {
            Some(self.into())
        } else {
            None
        }
    }
}

impl<T: IntBound> IsolateIndex<RangeTo<T>> for std::ops::Range<usize> {
    type Output = Range<T>;

    fn try_isolate(self, rng: RangeTo<T>) -> Option<Self::Output> {
        if self.start >= rng.distance().into() || self.end > rng.distance().into() {
            return None;
        }

        Some(Range {
            start: self.start.into(),
            end: self.end.into(),
        })
    }
}

impl<T> IntoOwned for Range<T> {
    type Owned = Self;
    fn into_owned(self) -> Self::Owned {
        self
    }
}

impl<T> IntoOwnedData for Range<T> {
    type OwnedData = Self;
    fn into_owned_data(self) -> Self::OwnedData {
        self
    }
}

impl<T> IntoOwned for RangeTo<T> {
    type Owned = Self;
    fn into_owned(self) -> Self::Owned {
        self
    }
}

impl<T> IntoOwnedData for RangeTo<T> {
    type OwnedData = Self;
    fn into_owned_data(self) -> Self::OwnedData {
        self
    }
}

// Ranges are lightweight and are considered to be viewed types since they are
// cheap to operate on.
impl<T> Viewed for Range<T> {}
impl<T> Viewed for RangeInclusive<T> {}
impl<T> Viewed for RangeTo<T> {}
impl<T> Viewed for RangeToInclusive<T> {}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn range_set() {
        assert_eq!(Set::len(&(0..100)), 100);
        assert_eq!(Set::len(&(1..=100)), 100);
        assert_eq!(Set::len(&(..100)), 100);
        assert_eq!(Set::len(&(..=99)), 100);
    }
}