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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use std::borrow::Borrow;
use std::cmp::Ordering;
use std::fmt::Debug;
use std::fmt::Error;
use std::fmt::Formatter;
use std::hash::Hash;
use std::hash::Hasher;
use std::ops::IndexMut;
use std::ops::{Bound, Index, Range, RangeBounds};

use crate::types::ChunkLength;

use super::{Iter, IterMut, RingBuffer};

/// An indexable representation of a subset of a `RingBuffer`.
pub struct Slice<'a, A, N: ChunkLength<A>> {
    pub(crate) buffer: &'a RingBuffer<A, N>,
    pub(crate) range: Range<usize>,
}

impl<'a, A: 'a, N: ChunkLength<A> + 'a> Slice<'a, A, N> {
    /// Get the length of the slice.
    #[inline]
    #[must_use]
    pub fn len(&self) -> usize {
        self.range.end - self.range.start
    }

    /// Test if the slice is empty.
    #[inline]
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Get a reference to the value at a given index.
    #[inline]
    #[must_use]
    pub fn get(&self, index: usize) -> Option<&'a A> {
        if index >= self.len() {
            None
        } else {
            self.buffer.get(self.range.start + index)
        }
    }

    /// Get a reference to the first value in the slice.
    #[inline]
    #[must_use]
    pub fn first(&self) -> Option<&A> {
        self.get(0)
    }

    /// Get a reference to the last value in the slice.
    #[inline]
    #[must_use]
    pub fn last(&self) -> Option<&A> {
        if self.is_empty() {
            None
        } else {
            self.get(self.len() - 1)
        }
    }

    /// Get an iterator over references to the items in the slice in order.
    #[inline]
    #[must_use]
    pub fn iter(&self) -> Iter<'_, A, N> {
        Iter {
            buffer: self.buffer,
            left_index: self.buffer.origin + self.range.start,
            right_index: self.buffer.origin + self.range.start + self.len(),
            remaining: self.len(),
        }
    }

    /// Create a subslice of this slice.
    ///
    /// This consumes the slice. To create a subslice without consuming it,
    /// clone it first: `my_slice.clone().slice(1..2)`.
    #[must_use]
    pub fn slice<R: RangeBounds<usize>>(self, range: R) -> Slice<'a, A, N> {
        let new_range = Range {
            start: match range.start_bound() {
                Bound::Unbounded => self.range.start,
                Bound::Included(index) => self.range.start + index,
                Bound::Excluded(_) => unimplemented!(),
            },
            end: match range.end_bound() {
                Bound::Unbounded => self.range.end,
                Bound::Included(index) => self.range.start + index + 1,
                Bound::Excluded(index) => self.range.start + index,
            },
        };
        if new_range.start < self.range.start
            || new_range.end > self.range.end
            || new_range.start > new_range.end
        {
            panic!("Slice::slice: index out of bounds");
        }
        Slice {
            buffer: self.buffer,
            range: new_range,
        }
    }

    /// Split the slice into two subslices at the given index.
    #[must_use]
    pub fn split_at(self, index: usize) -> (Slice<'a, A, N>, Slice<'a, A, N>) {
        if index > self.len() {
            panic!("Slice::split_at: index out of bounds");
        }
        let index = self.range.start + index;
        (
            Slice {
                buffer: self.buffer,
                range: Range {
                    start: self.range.start,
                    end: index,
                },
            },
            Slice {
                buffer: self.buffer,
                range: Range {
                    start: index,
                    end: self.range.end,
                },
            },
        )
    }

    /// Construct a new `RingBuffer` by copying the elements in this slice.
    #[inline]
    #[must_use]
    pub fn to_owned(&self) -> RingBuffer<A, N>
    where
        A: Clone,
    {
        self.iter().cloned().collect()
    }
}

impl<'a, A: 'a, N: ChunkLength<A> + 'a> From<&'a RingBuffer<A, N>> for Slice<'a, A, N> {
    #[inline]
    #[must_use]
    fn from(buffer: &'a RingBuffer<A, N>) -> Self {
        Slice {
            range: Range {
                start: 0,
                end: buffer.len(),
            },
            buffer,
        }
    }
}

impl<'a, A: 'a, N: ChunkLength<A> + 'a> Clone for Slice<'a, A, N> {
    #[inline]
    #[must_use]
    fn clone(&self) -> Self {
        Slice {
            buffer: self.buffer,
            range: self.range.clone(),
        }
    }
}

impl<'a, A: 'a, N: ChunkLength<A> + 'a> Index<usize> for Slice<'a, A, N> {
    type Output = A;

    #[inline]
    #[must_use]
    fn index(&self, index: usize) -> &Self::Output {
        self.buffer.index(self.range.start + index)
    }
}

impl<'a, A: PartialEq + 'a, N: ChunkLength<A> + 'a> PartialEq for Slice<'a, A, N> {
    #[inline]
    #[must_use]
    fn eq(&self, other: &Self) -> bool {
        self.len() == other.len() && self.iter().eq(other.iter())
    }
}

impl<'a, A: PartialEq + 'a, N: ChunkLength<A> + 'a, S> PartialEq<S> for Slice<'a, A, N>
where
    S: Borrow<[A]>,
{
    #[inline]
    #[must_use]
    fn eq(&self, other: &S) -> bool {
        let other = other.borrow();
        self.len() == other.len() && self.iter().eq(other.iter())
    }
}

impl<'a, A: Eq + 'a, N: ChunkLength<A> + 'a> Eq for Slice<'a, A, N> {}

impl<'a, A: PartialOrd + 'a, N: ChunkLength<A> + 'a> PartialOrd for Slice<'a, A, N> {
    #[inline]
    #[must_use]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.iter().partial_cmp(other.iter())
    }
}

impl<'a, A: Ord + 'a, N: ChunkLength<A> + 'a> Ord for Slice<'a, A, N> {
    #[inline]
    #[must_use]
    fn cmp(&self, other: &Self) -> Ordering {
        self.iter().cmp(other.iter())
    }
}

impl<'a, A: Debug + 'a, N: ChunkLength<A> + 'a> Debug for Slice<'a, A, N> {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
        f.write_str("RingBuffer")?;
        f.debug_list().entries(self.iter()).finish()
    }
}

impl<'a, A: Hash + 'a, N: ChunkLength<A> + 'a> Hash for Slice<'a, A, N> {
    #[inline]
    fn hash<H: Hasher>(&self, hasher: &mut H) {
        for item in self {
            item.hash(hasher)
        }
    }
}

impl<'a, A: 'a, N: ChunkLength<A> + 'a> IntoIterator for &'a Slice<'a, A, N> {
    type Item = &'a A;
    type IntoIter = Iter<'a, A, N>;

    #[inline]
    #[must_use]
    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

// Mutable slice

/// An indexable representation of a mutable subset of a `RingBuffer`.
pub struct SliceMut<'a, A, N: ChunkLength<A>> {
    pub(crate) buffer: &'a mut RingBuffer<A, N>,
    pub(crate) range: Range<usize>,
}

impl<'a, A: 'a, N: ChunkLength<A> + 'a> SliceMut<'a, A, N> {
    /// Downgrade this slice into a non-mutable slice.
    #[inline]
    #[must_use]
    pub fn unmut(self) -> Slice<'a, A, N> {
        Slice {
            buffer: self.buffer,
            range: self.range,
        }
    }

    /// Get the length of the slice.
    #[inline]
    #[must_use]
    pub fn len(&self) -> usize {
        self.range.end - self.range.start
    }

    /// Test if the slice is empty.
    #[inline]
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Get a reference to the value at a given index.
    #[inline]
    #[must_use]
    pub fn get(&self, index: usize) -> Option<&'a A> {
        if index >= self.len() {
            None
        } else {
            self.buffer
                .get(self.range.start + index)
                .map(|r| unsafe { &*(r as *const _) })
        }
    }

    /// Get a mutable reference to the value at a given index.
    #[inline]
    #[must_use]
    pub fn get_mut(&mut self, index: usize) -> Option<&'a mut A> {
        if index >= self.len() {
            None
        } else {
            self.buffer
                .get_mut(self.range.start + index)
                .map(|r| unsafe { &mut *(r as *mut _) })
        }
    }

    /// Get a reference to the first value in the slice.
    #[inline]
    #[must_use]
    pub fn first(&self) -> Option<&A> {
        self.get(0)
    }

    /// Get a mutable reference to the first value in the slice.
    #[inline]
    #[must_use]
    pub fn first_mut(&mut self) -> Option<&mut A> {
        self.get_mut(0)
    }

    /// Get a reference to the last value in the slice.
    #[inline]
    #[must_use]
    pub fn last(&self) -> Option<&A> {
        if self.is_empty() {
            None
        } else {
            self.get(self.len() - 1)
        }
    }

    /// Get a mutable reference to the last value in the slice.
    #[inline]
    #[must_use]
    pub fn last_mut(&mut self) -> Option<&mut A> {
        if self.is_empty() {
            None
        } else {
            self.get_mut(self.len() - 1)
        }
    }

    /// Get an iterator over references to the items in the slice in order.
    #[inline]
    #[must_use]
    pub fn iter(&self) -> Iter<'_, A, N> {
        Iter {
            buffer: self.buffer,
            left_index: self.buffer.origin + self.range.start,
            right_index: self.buffer.origin + self.range.start + self.len(),
            remaining: self.len(),
        }
    }

    /// Get an iterator over mutable references to the items in the slice in
    /// order.
    #[inline]
    #[must_use]
    pub fn iter_mut(&mut self) -> IterMut<'_, A, N> {
        let origin = self.buffer.origin;
        let len = self.len();
        IterMut {
            buffer: self.buffer,
            left_index: origin + self.range.start,
            right_index: origin + self.range.start + len,
            remaining: len,
        }
    }

    /// Create a subslice of this slice.
    ///
    /// This consumes the slice. Because the slice works like a mutable
    /// reference, you can only have one slice over a given subset of a
    /// `RingBuffer` at any one time, so that's just how it's got to be.
    #[must_use]
    pub fn slice<R: RangeBounds<usize>>(self, range: R) -> SliceMut<'a, A, N> {
        let new_range = Range {
            start: match range.start_bound() {
                Bound::Unbounded => self.range.start,
                Bound::Included(index) => self.range.start + index,
                Bound::Excluded(_) => unimplemented!(),
            },
            end: match range.end_bound() {
                Bound::Unbounded => self.range.end,
                Bound::Included(index) => self.range.start + index + 1,
                Bound::Excluded(index) => self.range.start + index,
            },
        };
        if new_range.start < self.range.start
            || new_range.end > self.range.end
            || new_range.start > new_range.end
        {
            panic!("Slice::slice: index out of bounds");
        }
        SliceMut {
            buffer: self.buffer,
            range: new_range,
        }
    }

    /// Split the slice into two subslices at the given index.
    #[must_use]
    pub fn split_at(self, index: usize) -> (SliceMut<'a, A, N>, SliceMut<'a, A, N>) {
        if index > self.len() {
            panic!("SliceMut::split_at: index out of bounds");
        }
        let index = self.range.start + index;
        let ptr: *mut RingBuffer<A, N> = self.buffer;
        (
            SliceMut {
                buffer: unsafe { &mut *ptr },
                range: Range {
                    start: self.range.start,
                    end: index,
                },
            },
            SliceMut {
                buffer: unsafe { &mut *ptr },
                range: Range {
                    start: index,
                    end: self.range.end,
                },
            },
        )
    }

    /// Update the value at index `index`, returning the old value.
    ///
    /// Panics if `index` is out of bounds.
    #[inline]
    #[must_use]
    pub fn set(&mut self, index: usize, value: A) -> A {
        if index >= self.len() {
            panic!("SliceMut::set: index out of bounds");
        } else {
            self.buffer.set(self.range.start + index, value)
        }
    }

    /// Construct a new `RingBuffer` by copying the elements in this slice.
    #[inline]
    #[must_use]
    pub fn to_owned(&self) -> RingBuffer<A, N>
    where
        A: Clone,
    {
        self.iter().cloned().collect()
    }
}

impl<'a, A: 'a, N: ChunkLength<A> + 'a> From<&'a mut RingBuffer<A, N>> for SliceMut<'a, A, N> {
    #[must_use]
    fn from(buffer: &'a mut RingBuffer<A, N>) -> Self {
        SliceMut {
            range: Range {
                start: 0,
                end: buffer.len(),
            },
            buffer,
        }
    }
}

impl<'a, A: 'a, N: ChunkLength<A> + 'a> Into<Slice<'a, A, N>> for SliceMut<'a, A, N> {
    #[inline]
    #[must_use]
    fn into(self) -> Slice<'a, A, N> {
        self.unmut()
    }
}

impl<'a, A: 'a, N: ChunkLength<A> + 'a> Index<usize> for SliceMut<'a, A, N> {
    type Output = A;

    #[inline]
    #[must_use]
    fn index(&self, index: usize) -> &Self::Output {
        self.buffer.index(self.range.start + index)
    }
}

impl<'a, A: 'a, N: ChunkLength<A> + 'a> IndexMut<usize> for SliceMut<'a, A, N> {
    #[inline]
    #[must_use]
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        self.buffer.index_mut(self.range.start + index)
    }
}

impl<'a, A: PartialEq + 'a, N: ChunkLength<A> + 'a> PartialEq for SliceMut<'a, A, N> {
    #[inline]
    #[must_use]
    fn eq(&self, other: &Self) -> bool {
        self.len() == other.len() && self.iter().eq(other.iter())
    }
}

impl<'a, A: PartialEq + 'a, N: ChunkLength<A> + 'a, S> PartialEq<S> for SliceMut<'a, A, N>
where
    S: Borrow<[A]>,
{
    #[inline]
    #[must_use]
    fn eq(&self, other: &S) -> bool {
        let other = other.borrow();
        self.len() == other.len() && self.iter().eq(other.iter())
    }
}

impl<'a, A: Eq + 'a, N: ChunkLength<A> + 'a> Eq for SliceMut<'a, A, N> {}

impl<'a, A: PartialOrd + 'a, N: ChunkLength<A> + 'a> PartialOrd for SliceMut<'a, A, N> {
    #[inline]
    #[must_use]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.iter().partial_cmp(other.iter())
    }
}

impl<'a, A: Ord + 'a, N: ChunkLength<A> + 'a> Ord for SliceMut<'a, A, N> {
    #[inline]
    #[must_use]
    fn cmp(&self, other: &Self) -> Ordering {
        self.iter().cmp(other.iter())
    }
}

impl<'a, A: Debug + 'a, N: ChunkLength<A> + 'a> Debug for SliceMut<'a, A, N> {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
        f.write_str("RingBuffer")?;
        f.debug_list().entries(self.iter()).finish()
    }
}

impl<'a, A: Hash + 'a, N: ChunkLength<A> + 'a> Hash for SliceMut<'a, A, N> {
    #[inline]
    fn hash<H: Hasher>(&self, hasher: &mut H) {
        for item in self {
            item.hash(hasher)
        }
    }
}

impl<'a, 'b, A: 'a, N: ChunkLength<A> + 'a> IntoIterator for &'a SliceMut<'a, A, N> {
    type Item = &'a A;
    type IntoIter = Iter<'a, A, N>;

    #[inline]
    #[must_use]
    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'a, 'b, A: 'a, N: ChunkLength<A> + 'a> IntoIterator for &'a mut SliceMut<'a, A, N> {
    type Item = &'a mut A;
    type IntoIter = IterMut<'a, A, N>;

    #[inline]
    #[must_use]
    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}