compressed-intvec 0.6.0

Space-efficient integer vectors with fixed-width, variable-length, and sequence-oriented encodings.
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
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
//! Zero-copy slices of a [`SeqVec`].
//!
//! This module provides [`SeqVecSlice`], a view into a contiguous portion of a
//! [`SeqVec`]. Slices are immutable and do not own their data; they borrow it
//! from the parent [`SeqVec`].
//!
//! [`SeqVec`]: crate::seq::SeqVec

use super::{iter::SeqVecBitReader, SeqIter, SeqVec};
use crate::variable::traits::Storable;
use dsi_bitstream::prelude::{BitRead, BitSeek, CodesRead, Endianness};
use std::cmp::Ordering;
use std::fmt;
use std::ops::Range;

/// An immutable, zero-copy slice of a [`SeqVec`].
///
/// This struct provides a view into a contiguous portion of a [`SeqVec`]
/// without copying the underlying compressed data. It is created by the
/// [`slice`] or [`split_at`] methods on a [`SeqVec`].
///
/// All operations on a [`SeqVecSlice`] are relative to the start of the slice,
/// not the parent vector. The slice contains a contiguous range of sequences.
///
/// [`SeqVec`]: super::SeqVec
/// [`slice`]: super::SeqVec::slice
/// [`split_at`]: super::SeqVec::split_at
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use compressed_intvec::seq::{SeqVec, LESeqVec};
///
/// let sequences: &[&[u32]] = &[&[1, 2], &[3, 4, 5], &[6], &[7, 8, 9, 10]];
/// let vec: LESeqVec<u32> = SeqVec::from_slices(sequences)?;
///
/// // Create a slice of sequences 1 through 2 (indices 1 and 2)
/// let slice = vec.slice(1, 2).unwrap();
///
/// assert_eq!(slice.len(), 2);
///
/// // Accessing a sequence within the slice
/// // Index 0 of the slice corresponds to sequence 1 of the original vector
/// let seq0: Vec<u32> = slice.get(0).unwrap().collect();
/// assert_eq!(seq0, vec![3, 4, 5]);
///
/// // Iterating over the slice
/// let all_values: Vec<Vec<u32>> = slice.iter()
///     .map(|seq| seq.collect())
///     .collect();
/// assert_eq!(all_values, vec![vec![3, 4, 5], vec![6]]);
/// #     Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct SeqVecSlice<'a, T: Storable, E: Endianness, B: AsRef<[u64]>> {
    /// A reference to the parent vector.
    vec: &'a SeqVec<T, E, B>,
    /// The starting sequence index within the parent vector.
    start: usize,
    /// The number of sequences in the slice.
    len: usize,
}

impl<'a, T: Storable, E: Endianness, B: AsRef<[u64]>> SeqVecSlice<'a, T, E, B> {
    /// Creates a new [`SeqVecSlice`].
    pub(super) fn new(vec: &'a SeqVec<T, E, B>, range: Range<usize>) -> Self {
        debug_assert!(
            range.end <= vec.num_sequences(),
            "Slice range exceeds vector bounds"
        );
        Self {
            vec,
            start: range.start,
            len: range.len(),
        }
    }

    /// Returns the number of sequences in the slice.
    #[inline]
    pub fn len(&self) -> usize {
        self.len
    }

    /// Returns `true` if the slice contains no sequences.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Returns an iterator over the elements of the sequence at `index` within
    /// the slice, or `None` if the index is out of bounds.
    ///
    /// The index is relative to the start of the slice.
    #[inline]
    pub fn get(&self, index: usize) -> Option<SeqIter<'_, T, E>>
    where
        for<'b> SeqVecBitReader<'b, E>: BitRead<E, Error = core::convert::Infallible>
            + CodesRead<E>
            + BitSeek<Error = core::convert::Infallible>,
    {
        if index >= self.len {
            return None;
        }
        // SAFETY: The bounds check above ensures `index` is valid.
        Some(unsafe { self.get_unchecked(index) })
    }

    /// Returns an iterator over the elements of the sequence at `index` within
    /// the slice without bounds checking.
    ///
    /// The index is relative to the start of the slice.
    ///
    /// # Safety
    ///
    /// Calling this method with an out-of-bounds index is undefined behavior.
    /// The caller must ensure that `index < self.len()`.
    #[inline(always)]
    pub unsafe fn get_unchecked(&self, index: usize) -> SeqIter<'_, T, E>
    where
        for<'b> SeqVecBitReader<'b, E>: BitRead<E, Error = core::convert::Infallible>
            + CodesRead<E>
            + BitSeek<Error = core::convert::Infallible>,
    {
        debug_assert!(index < self.len, "Index out of bounds");
        // Translate slice-relative index to parent vector index.
        let parent_index = self.start + index;
        unsafe { self.vec.get_unchecked(parent_index) }
    }

    /// Returns the sequence at `index` as a materialized `Vec<T>`, or `None`
    /// if the index is out of bounds.
    ///
    /// This method fully decodes the sequence and allocates a new vector.
    ///
    /// # Examples
    ///
    /// ```
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use compressed_intvec::seq::{SeqVec, LESeqVec};
    ///
    /// let sequences: &[&[u32]] = &[&[1, 2], &[3, 4, 5], &[6]];
    /// let vec: LESeqVec<u32> = SeqVec::from_slices(sequences)?;
    ///
    /// let slice = vec.slice(1, 2).unwrap();
    /// assert_eq!(slice.decode_vec(0), Some(vec![3, 4, 5]));
    /// assert_eq!(slice.decode_vec(1), Some(vec![6]));
    /// assert_eq!(slice.decode_vec(2), None); // Out of bounds
    /// #     Ok(())
    /// # }
    /// ```
    #[inline]
    pub fn decode_vec(&self, index: usize) -> Option<Vec<T>>
    where
        for<'b> SeqVecBitReader<'b, E>: BitRead<E, Error = core::convert::Infallible>
            + CodesRead<E>
            + BitSeek<Error = core::convert::Infallible>,
    {
        let parent_index = self.start.checked_add(index)?;
        if index >= self.len {
            return None;
        }
        self.vec.decode_vec(parent_index)
    }

    /// Returns the sequence at `index` as a materialized `Vec<T>` without bounds
    /// checking.
    ///
    /// # Safety
    ///
    /// Calling this method with an out-of-bounds index is undefined behavior.
    #[inline]
    pub unsafe fn decode_vec_unchecked(&self, index: usize) -> Vec<T>
    where
        for<'b> SeqVecBitReader<'b, E>: BitRead<E, Error = core::convert::Infallible>
            + CodesRead<E>
            + BitSeek<Error = core::convert::Infallible>,
    {
        let parent_index = self.start + index;
        unsafe { self.vec.decode_vec_unchecked(parent_index) }
    }

    /// Decodes the sequence at `index` into a reusable buffer.
    ///
    /// The buffer is cleared and then filled with the decoded sequence elements.
    /// Returns the number of elements decoded, or `None` if the index is out
    /// of bounds.
    ///
    /// # Examples
    ///
    /// ```
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use compressed_intvec::seq::{SeqVec, LESeqVec};
    ///
    /// let sequences: &[&[u32]] = &[&[1, 2], &[3, 4, 5], &[6]];
    /// let vec: LESeqVec<u32> = SeqVec::from_slices(sequences)?;
    ///
    /// let slice = vec.slice(1, 2).unwrap();
    ///
    /// let mut buf = Vec::new();
    /// assert_eq!(slice.decode_into(0, &mut buf), Some(3));
    /// assert_eq!(buf, vec![3, 4, 5]);
    ///
    /// // Buffer is reused (cleared internally).
    /// assert_eq!(slice.decode_into(1, &mut buf), Some(1));
    /// assert_eq!(buf, vec![6]);
    /// #     Ok(())
    /// # }
    /// ```
    #[inline]
    pub fn decode_into(&self, index: usize, buf: &mut Vec<T>) -> Option<usize>
    where
        for<'b> SeqVecBitReader<'b, E>: BitRead<E, Error = core::convert::Infallible>
            + CodesRead<E>
            + BitSeek<Error = core::convert::Infallible>,
    {
        let parent_index = self.start.checked_add(index)?;
        if index >= self.len {
            return None;
        }
        self.vec.decode_into(parent_index, buf)
    }

    /// Decodes sequence `index` into the provided buffer without bounds checking.
    ///
    /// # Safety
    ///
    /// Calling this method with an out-of-bounds index is undefined behavior.
    #[inline]
    pub unsafe fn decode_into_unchecked(&self, index: usize, buf: &mut Vec<T>) -> usize
    where
        for<'b> SeqVecBitReader<'b, E>: BitRead<E, Error = core::convert::Infallible>
            + CodesRead<E>
            + BitSeek<Error = core::convert::Infallible>,
    {
        let parent_index = self.start + index;
        unsafe { self.vec.decode_into_unchecked(parent_index, buf) }
    }

    /// Returns an iterator over the sequences in the slice.
    pub fn iter(&self) -> SeqVecSliceIter<'_, T, E, B>
    where
        for<'b> SeqVecBitReader<'b, E>: BitRead<E, Error = core::convert::Infallible>
            + CodesRead<E>
            + BitSeek<Error = core::convert::Infallible>,
    {
        SeqVecSliceIter::new(self)
    }
}

impl<T, E, B> SeqVecSlice<'_, T, E, B>
where
    T: Storable + PartialEq,
    E: Endianness,
    B: AsRef<[u64]>,
    for<'b> SeqVecBitReader<'b, E>: BitRead<E, Error = core::convert::Infallible>
        + CodesRead<E>
        + BitSeek<Error = core::convert::Infallible>,
{
    /// Binary searches this slice for a sequence matching the given elements.
    ///
    /// If the sequence is found, returns `Ok(usize)` with the index of the
    /// matching sequence within the slice. If not found, returns `Err(usize)`
    /// with the insertion point to maintain order.
    ///
    /// The comparison is performed element-by-element with early termination:
    /// sequences with different lengths or any differing element are ordered
    /// according to the first difference encountered.
    ///
    /// # Performance Note
    ///
    /// Each binary search probe **fully decodes a compressed sequence** until a
    /// difference is found or the sequence ends. For `log(n)` probes, each
    /// decoding is O(m) where `m` is the sequence length. This is O(m * log n)
    /// overall.
    ///
    /// - If sequences are short or differ early, early termination makes this
    ///   very efficient.
    /// - If sequences are very long and similar, decoding cost dominates.
    /// - To extract a key from each sequence efficiently, use
    ///   [`binary_search_by_key`](Self::binary_search_by_key) to extract a
    ///   sortable key from the compressed data (e.g., first element) rather
    ///   than the full sequence.
    ///
    /// # Examples
    ///
    /// ```
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use compressed_intvec::seq::{SeqVec, LESeqVec};
    ///
    /// let sequences: &[&[u32]] = &[&[1, 2], &[3, 4, 5], &[6, 7], &[8]];
    /// let vec: LESeqVec<u32> = SeqVec::from_slices(sequences)?;
    ///
    /// let slice = vec.slice(0, 4).unwrap();
    ///
    /// assert_eq!(slice.binary_search(&[3, 4, 5]), Ok(1));
    /// assert_eq!(slice.binary_search(&[6, 7]), Ok(2));
    /// assert_eq!(slice.binary_search(&[5]), Err(2)); // Would be inserted at index 2
    /// #     Ok(())
    /// # }
    /// ```
    pub fn binary_search(&self, sequence: &[T]) -> Result<usize, usize>
    where
        T: Ord,
    {
        self.binary_search_by(|probe_iter| {
            // Compare element-by-element with early exit on first difference.
            let mut probe_iter = probe_iter.peekable();
            let mut seq_iter = sequence.iter().peekable();

            loop {
                match (probe_iter.next(), seq_iter.next()) {
                    (Some(a), Some(b)) => {
                        let cmp = a.cmp(b);
                        if cmp != Ordering::Equal {
                            return cmp;
                        }
                    }
                    // Probe sequence ended first → Less
                    (None, Some(_)) => return Ordering::Less,
                    // Target sequence ended first → Greater
                    (Some(_), None) => return Ordering::Greater,
                    // Both ended → Equal
                    (None, None) => return Ordering::Equal,
                }
            }
        })
    }

    /// Binary searches this slice with a custom comparison function.
    ///
    /// The comparison function receives a [`SeqIter`] for the probe sequence
    /// and should return the ordering relative to the target.
    ///
    /// # Performance Note
    ///
    /// The closure receives a [`SeqIter`] for the probe sequence, which may
    /// require decoding the compressed data. Each probe can involve up to O(m)
    /// decoding operations where `m` is the sequence length.
    ///
    /// - For fast key extraction (first element, hash, etc.), decode only what
    ///   is needed within the closure and return early.
    /// - To search by a pre-computed key, use
    ///   [`binary_search_by_key`](Self::binary_search_by_key) instead, which is
    ///   optimized for key-based comparisons.
    /// - For comparisons requiring the full sequence, full decoding is
    ///   unavoidable: O(m * log n).
    ///
    /// [`SeqIter`]: super::iter::SeqIter
    ///
    /// # Examples
    ///
    /// ```
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use compressed_intvec::seq::{SeqVec, LESeqVec};
    /// use std::cmp::Ordering;
    ///
    /// let sequences: &[&[u32]] = &[&[1], &[1, 2], &[1, 2, 3], &[1, 2, 3, 4]];
    /// let vec: LESeqVec<u32> = SeqVec::from_slices(sequences)?;
    ///
    /// let slice = vec.slice(0, 4).unwrap();
    ///
    /// // Search by sequence length (decodes all elements)
    /// let result = slice.binary_search_by(|probe| {
    ///     let probe_len = probe.count();
    ///     probe_len.cmp(&3)
    /// });
    ///
    /// assert_eq!(result, Ok(2));
    ///
    /// // Search by first element only (early termination)
    /// let result = slice.binary_search_by(|mut probe| {
    ///     match probe.next() {
    ///         Some(first) => first.cmp(&1),
    ///         None => Ordering::Less,
    ///     }
    /// });
    /// #     Ok(())
    /// # }
    /// ```
    #[inline]
    pub fn binary_search_by<F>(&self, mut f: F) -> Result<usize, usize>
    where
        F: FnMut(SeqIter<'_, T, E>) -> Ordering,
    {
        let mut low = 0;
        let mut high = self.len();

        while low < high {
            let mid = low + (high - low) / 2;
            // SAFETY: Bounds are checked by the loop invariants and the slice's
            // construction, so the index is always valid.
            let cmp = f(unsafe { self.get_unchecked(mid) });
            match cmp {
                Ordering::Less => low = mid + 1,
                Ordering::Equal => return Ok(mid),
                Ordering::Greater => high = mid,
            }
        }
        Err(low)
    }

    /// Binary searches this slice with a key extraction function.
    ///
    /// # Examples
    ///
    /// ```
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use compressed_intvec::seq::{SeqVec, LESeqVec};
    ///
    /// let sequences: &[&[u32]] = &[&[10], &[20], &[30, 40], &[50]];
    /// let vec: LESeqVec<u32> = SeqVec::from_slices(sequences)?;
    ///
    /// let slice = vec.slice(0, 4).unwrap();
    ///
    /// // Search by first element
    /// let result = slice.binary_search_by_key(&30, |mut probe| probe.next().unwrap());
    /// assert_eq!(result, Ok(2)); // Found sequence [30, 40]
    /// #     Ok(())
    /// # }
    /// ```
    #[inline]
    pub fn binary_search_by_key<K, F>(&self, b: &K, mut f: F) -> Result<usize, usize>
    where
        F: FnMut(SeqIter<'_, T, E>) -> K,
        K: Ord,
    {
        self.binary_search_by(|probe| f(probe).cmp(b))
    }
}

/// An iterator over the sequences of a [`SeqVecSlice`].
///
/// This struct is created by the [`iter`] method. Each element of this
/// iterator is a [`SeqIter`] for a sequence within the slice.
///
/// [`iter`]: SeqVecSlice::iter
/// [`SeqIter`]: super::iter::SeqIter
pub struct SeqVecSliceIter<'a, T: Storable, E: Endianness, B: AsRef<[u64]>> {
    slice: &'a SeqVecSlice<'a, T, E, B>,
    /// Current front index for forward iteration.
    front: usize,
    /// Current back index (exclusive) for backward iteration.
    back: usize,
}

impl<T: Storable, E: Endianness, B: AsRef<[u64]>> fmt::Debug for SeqVecSliceIter<'_, T, E, B> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SeqVecSliceIter")
            .field("remaining", &self.back.saturating_sub(self.front))
            .finish()
    }
}

// --- IntoIterator Implementation ---

impl<'a, T, E, B> IntoIterator for &'a SeqVecSlice<'a, T, E, B>
where
    T: Storable,
    E: Endianness,
    B: AsRef<[u64]>,
    for<'b> SeqVecBitReader<'b, E>: BitRead<E, Error = core::convert::Infallible>
        + CodesRead<E>
        + BitSeek<Error = core::convert::Infallible>,
{
    type Item = SeqIter<'a, T, E>;
    type IntoIter = SeqVecSliceIter<'a, T, E, B>;

    /// Returns an iterator over the sequences in the slice.
    ///
    /// This implementation allows slices to be used in `for` loops:
    ///
    /// ```no_run
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # use compressed_intvec::seq::{SeqVec, LESeqVec};
    /// # let vec: LESeqVec<u32> = SeqVec::from_slices(&[&[1, 2][..], &[3, 4, 5][..]])?;
    /// # let slice = vec.slice(0, 2).unwrap();
    /// for seq in &slice {
    ///     // Process each sequence
    /// }
    /// #     Ok(())
    /// # }
    /// ```
    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'a, T: Storable, E: Endianness, B: AsRef<[u64]>> SeqVecSliceIter<'a, T, E, B> {
    /// Creates a new iterator for a given [`SeqVecSlice`].
    fn new(slice: &'a SeqVecSlice<'a, T, E, B>) -> Self {
        Self {
            slice,
            front: 0,
            back: slice.len(),
        }
    }
}

impl<'a, T, E, B> Iterator for SeqVecSliceIter<'a, T, E, B>
where
    T: Storable,
    E: Endianness,
    B: AsRef<[u64]>,
    for<'b> SeqVecBitReader<'b, E>: BitRead<E, Error = core::convert::Infallible>
        + CodesRead<E>
        + BitSeek<Error = core::convert::Infallible>,
{
    type Item = SeqIter<'a, T, E>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if self.front >= self.back {
            return None;
        }
        let index = self.front;
        self.front += 1;
        // SAFETY: The iterator's invariant ensures `index` is in bounds.
        Some(unsafe { self.slice.get_unchecked(index) })
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = self.back.saturating_sub(self.front);
        (remaining, Some(remaining))
    }

    #[inline]
    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        let remaining = self.back.saturating_sub(self.front);
        if n >= remaining {
            self.front = self.back;
            return None;
        }
        self.front += n;
        self.next()
    }

    #[inline]
    fn count(self) -> usize {
        self.back.saturating_sub(self.front)
    }
}

impl<'a, T, E, B> ExactSizeIterator for SeqVecSliceIter<'a, T, E, B>
where
    T: Storable,
    E: Endianness,
    B: AsRef<[u64]>,
    for<'b> SeqVecBitReader<'b, E>: BitRead<E, Error = core::convert::Infallible>
        + CodesRead<E>
        + BitSeek<Error = core::convert::Infallible>,
{
    fn len(&self) -> usize {
        self.back.saturating_sub(self.front)
    }
}

impl<'a, T, E, B> std::iter::FusedIterator for SeqVecSliceIter<'a, T, E, B>
where
    T: Storable,
    E: Endianness,
    B: AsRef<[u64]>,
    for<'b> SeqVecBitReader<'b, E>: BitRead<E, Error = core::convert::Infallible>
        + CodesRead<E>
        + BitSeek<Error = core::convert::Infallible>,
{
}

impl<'a, T, E, B> DoubleEndedIterator for SeqVecSliceIter<'a, T, E, B>
where
    T: Storable,
    E: Endianness,
    B: AsRef<[u64]>,
    for<'b> SeqVecBitReader<'b, E>: BitRead<E, Error = core::convert::Infallible>
        + CodesRead<E>
        + BitSeek<Error = core::convert::Infallible>,
{
    #[inline]
    fn next_back(&mut self) -> Option<Self::Item> {
        if self.front >= self.back {
            return None;
        }
        self.back -= 1;
        // SAFETY: The iterator's invariant ensures `back` is in bounds.
        Some(unsafe { self.slice.get_unchecked(self.back) })
    }

    #[inline]
    fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
        let remaining = self.back.saturating_sub(self.front);
        if n >= remaining {
            self.back = self.front;
            return None;
        }
        self.back -= n;
        self.next_back()
    }
}

// --- PartialEq Implementations ---

impl<'a, T, E, B> PartialEq<SeqVecSlice<'a, T, E, B>> for SeqVecSlice<'a, T, E, B>
where
    T: Storable + PartialEq,
    E: Endianness,
    B: AsRef<[u64]>,
    for<'b> SeqVecBitReader<'b, E>: BitRead<E, Error = core::convert::Infallible>
        + CodesRead<E>
        + BitSeek<Error = core::convert::Infallible>,
{
    fn eq(&self, other: &SeqVecSlice<'a, T, E, B>) -> bool {
        if self.len() != other.len() {
            return false;
        }
        self.iter()
            .zip(other.iter())
            .all(|(seq_a, seq_b)| seq_a.eq(seq_b))
    }
}

impl<'a, T, E, B> Eq for SeqVecSlice<'a, T, E, B>
where
    T: Storable + Eq,
    E: Endianness,
    B: AsRef<[u64]>,
    for<'b> SeqVecBitReader<'b, E>: BitRead<E, Error = core::convert::Infallible>
        + CodesRead<E>
        + BitSeek<Error = core::convert::Infallible>,
{
}

impl<'a, T, E, B> PartialEq<SeqVec<T, E, B>> for SeqVecSlice<'a, T, E, B>
where
    T: Storable + PartialEq,
    E: Endianness,
    B: AsRef<[u64]>,
    for<'b> SeqVecBitReader<'b, E>: BitRead<E, Error = core::convert::Infallible>
        + CodesRead<E>
        + BitSeek<Error = core::convert::Infallible>,
{
    fn eq(&self, other: &SeqVec<T, E, B>) -> bool {
        if self.len() != other.num_sequences() {
            return false;
        }
        self.iter().zip(other.iter()).all(|(a, b)| a.eq(b))
    }
}

impl<'a, T, E, B> PartialEq<SeqVecSlice<'a, T, E, B>> for SeqVec<T, E, B>
where
    T: Storable + PartialEq,
    E: Endianness,
    B: AsRef<[u64]>,
    for<'b> SeqVecBitReader<'b, E>: BitRead<E, Error = core::convert::Infallible>
        + CodesRead<E>
        + BitSeek<Error = core::convert::Infallible>,
{
    fn eq(&self, other: &SeqVecSlice<'a, T, E, B>) -> bool {
        other.eq(self)
    }
}

impl<'a, T, E, B> PartialEq<&SeqVec<T, E, B>> for SeqVecSlice<'a, T, E, B>
where
    T: Storable + PartialEq,
    E: Endianness,
    B: AsRef<[u64]>,
    for<'b> SeqVecBitReader<'b, E>: BitRead<E, Error = core::convert::Infallible>
        + CodesRead<E>
        + BitSeek<Error = core::convert::Infallible>,
{
    fn eq(&self, other: &&SeqVec<T, E, B>) -> bool {
        self.eq(*other)
    }
}