sux 0.14.0

A pure Rust implementation of succinct and compressed data structures
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/*
 * SPDX-FileCopyrightText: 2023 Inria
 * SPDX-FileCopyrightText: 2023 Sebastiano Vigna
 *
 * SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
 */

//! Traits for indexed dictionaries.
//!
//! An indexed dictionary is a dictionary of values indexed by a `usize`. When
//! the values are monotonically increasing, such a dictionary might provide
//! additional operations such as [predecessor] and [successor].
//!
//! [predecessor]: Pred
//! [successor]: Succ
//!
//! There are seven traits:
//! - [`Types`] defines the type of the values in the dictionary. The type of
//!   input and output values may be different: for example, in a dictionary of
//!   strings (see, e.g.,
//!   [`RearCodedList`]), one
//!   usually accepts as inputs references to [`str`] but returns owned
//!   [`String`]s.
//! - [`IndexedSeq`] provides positional access to the values in the dictionary.
//! - [`IndexedDict`] provides access by value, that is, given a value, its
//!   position in the dictionary.
//! - [`SuccUnchecked`]/[`Succ`] provide the successor of a value in a sorted
//!   dictionary.
//! - [`PredUnchecked`]/[`Pred`] provide the predecessor of a value in a sorted
//!   dictionary.
//!
//! [`IndexedSeq`], [`IndexedDict`], [`SuccUnchecked`], and [`PredUnchecked`]
//! are independent. A structure may implement any combination of them, provided
//! it implements [`Types`].
//!
//! All methods accepting values have arguments of type `impl
//! Borrow<Self::Input>`. This makes it possible to pass a value both by
//! reference and by value, which is particularly convenient in the case of
//! primitive types (see, e.g.,
//! [`EliasFano`]).
//!
//! We suggest that every implementation of [`IndexedSeq`] also implements
//! [`IntoIterator`]/[`IntoIteratorFrom`] with `Item = Self::Output` on a
//! reference. This property can be tested on a type `T` with the clause `where
//! for<'a> &'a T: IntoIteratorFrom<Item = Self::Output>` (or `where for<'a> &'a
//! T: IntoIterator<Item = Self::Output>`, if you don't need to select the
//! starting position). Many implementations also offer equivalent
//! `iter`/`iter_from` convenience methods.
//!
//! [`RearCodedList`]: crate::dict::rear_coded_list::RearCodedList
//! [`EliasFano`]: crate::dict::elias_fano::EliasFano
//! [`IntoIteratorFrom`]: super::iter::IntoIteratorFrom

use ambassador::delegatable_trait;
use impl_tools::autoimpl;
use std::borrow::Borrow;

use super::iter::BidiIterator;
use crate::{debug_assert_bounds, panic_if_out_of_bounds};

/// The types of the dictionary.
#[autoimpl(for<T: trait + ?Sized> &T, &mut T, Box<T>)]
#[delegatable_trait]
pub trait Types {
    type Input: for<'a> PartialEq<Self::Output<'a>> + PartialEq + ?Sized;
    type Output<'a>: PartialEq<Self::Input> + PartialEq;
}

/// Access by position to the dictionary.
///
/// # Notes
///
/// This trait does not include an `iter` iteration method with a default
/// implementation, although it would be convenient, because it would cause
/// significant problems with structures that have their own implementation of
/// the method, and in which the implementation is dependent on additional trait
/// bounds (see, e.g., [`EliasFano`]).
///
/// More precisely, the inherent implementation could not be used to override
/// the default implementation, due to the additional trait bounds, and thus the
/// selection of the inherent vs. default trait implementation would depend on
/// the type of the variable, which might lead to efficiency bugs difficult to
/// diagnose. Having a different name for the trait and inherent iteration
/// method would make the call predictable, but it would be less ergonomic.
///
/// The (pretty standard) strategy outlined in the [module documentation] is
/// more flexible, as it makes it possible to write methods that use the
/// inherent implementation only if available.
///
/// [`EliasFano`]: crate::dict::elias_fano::EliasFano
/// [module documentation]: crate::traits::indexed_dict
#[autoimpl(for<T: trait + ?Sized> &T, &mut T, Box<T>)]
#[delegatable_trait]
pub trait IndexedSeq: Types {
    /// Returns the value at the specified index.
    ///
    /// # Panics
    ///
    /// May panic if the index is not in [0..[len]).
    ///
    /// [len]: IndexedSeq::len
    fn get(&self, index: usize) -> Self::Output<'_> {
        panic_if_out_of_bounds!(index, self.len());
        unsafe { self.get_unchecked(index) }
    }

    /// Returns the value at the specified index.
    ///
    /// # Safety
    ///
    /// `index` must be in [0..[len]). No bounds checking
    /// is performed.
    ///
    /// [len]: IndexedSeq::len
    unsafe fn get_unchecked(&self, index: usize) -> Self::Output<'_>;

    /// Returns the length (number of items) of the dictionary.
    fn len(&self) -> usize;

    /// Returns true if [`len`] is zero.
    ///
    /// [`len`]: IndexedSeq::len
    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns the first value, or `None` if the dictionary is empty.
    ///
    /// Equivalent to [`get(0)`], but implementations may provide a more
    /// efficient method.
    ///
    /// [`get(0)`]: IndexedSeq::get
    fn first_value(&self) -> Option<Self::Output<'_>> {
        if self.is_empty() {
            None
        } else {
            Some(unsafe { self.get_unchecked(0) })
        }
    }

    /// Returns the last value, or `None` if the dictionary is empty.
    ///
    /// Equivalent to [`get(len - 1)`], but implementations may provide a
    /// more efficient method.
    ///
    /// [`get(len - 1)`]: IndexedSeq::get
    fn last_value(&self) -> Option<Self::Output<'_>> {
        if self.is_empty() {
            None
        } else {
            Some(unsafe { self.get_unchecked(self.len() - 1) })
        }
    }
}

/// Access by value to the dictionary.
#[autoimpl(for<T: trait + ?Sized> &T, &mut T, Box<T>)]
#[delegatable_trait]
pub trait IndexedDict: Types {
    /// Returns the index of the given value if the dictionary contains it and
    /// `None` otherwise.
    fn index_of(&self, value: impl Borrow<Self::Input>) -> Option<usize>;

    /// Returns true if the dictionary contains the given value.
    ///
    /// The default implementation just calls [`index_of`].
    ///
    /// [`index_of`]: IndexedDict::index_of
    fn contains(&self, value: impl Borrow<Self::Input>) -> bool {
        self.index_of(value).is_some()
    }
}

/// Unchecked successor computation for dictionaries whose values are monotonically increasing.
pub trait SuccUnchecked: Types
where
    Self::Input: for<'a> PartialOrd<Self::Output<'a>> + PartialOrd,
    for<'a> Self::Output<'a>: PartialOrd<Self::Input> + PartialOrd,
{
    /// The forward iterator type returned by
    /// [`iter_from_succ_unchecked`].
    ///
    /// [`iter_from_succ_unchecked`]: SuccUnchecked::iter_from_succ_unchecked
    type Iter<'a>: Iterator<Item = Self::Output<'a>>
    where
        Self: 'a;
    /// The bidirectional iterator type returned by
    /// [`iter_bidi_from_succ_unchecked`].
    ///
    /// [`iter_bidi_from_succ_unchecked`]: SuccUnchecked::iter_bidi_from_succ_unchecked
    type BidiIter<'a>: BidiIterator<Item = Self::Output<'a>>
    where
        Self: 'a;

    /// Returns the index of the successor and the successor of the given value.
    ///
    /// The successor is the least value in the dictionary that is greater than
    /// or equal to the given value, if `STRICT` is `false`, or the least value
    /// in the dictionary that is greater than the given value, if `STRICT` is
    /// `true`.
    ///
    /// If there are repeated values, the index of the one returned depends on
    /// the implementation.
    ///
    /// # Safety
    ///
    /// The successor must exist.
    unsafe fn succ_unchecked<const STRICT: bool>(
        &self,
        value: impl Borrow<Self::Input>,
    ) -> (usize, Self::Output<'_>);

    /// Returns the index of the successor and an iterator starting at
    /// the successor position.
    ///
    /// The iterator's first [`next()`] call returns the
    /// successor value itself. The index returned is the position of the
    /// successor in the sequence.
    ///
    /// [`next()`]: Iterator::next
    ///
    /// # Safety
    ///
    /// The successor must exist.
    unsafe fn iter_from_succ_unchecked<const STRICT: bool>(
        &self,
        value: impl Borrow<Self::Input>,
    ) -> (usize, Self::Iter<'_>);

    /// Returns the index of the successor and a bidirectional iterator
    /// positioned at the successor.
    ///
    /// The iterator's first [`next()`] call returns the
    /// successor value itself; the first
    /// [`prev()`] call returns the element
    /// before the successor.
    ///
    /// [`next()`]: Iterator::next
    /// [`prev()`]: crate::traits::BidiIterator::prev
    ///
    /// # Safety
    ///
    /// The successor must exist.
    unsafe fn iter_bidi_from_succ_unchecked<const STRICT: bool>(
        &self,
        value: impl Borrow<Self::Input>,
    ) -> (usize, Self::BidiIter<'_>);
}

impl<T: SuccUnchecked + ?Sized> SuccUnchecked for &T
where
    T::Input: for<'a> PartialOrd<T::Output<'a>> + PartialOrd,
    for<'a> T::Output<'a>: PartialOrd<T::Input> + PartialOrd,
{
    type Iter<'a>
        = T::Iter<'a>
    where
        Self: 'a;
    type BidiIter<'a>
        = T::BidiIter<'a>
    where
        Self: 'a;

    #[inline(always)]
    unsafe fn succ_unchecked<const STRICT: bool>(
        &self,
        value: impl Borrow<Self::Input>,
    ) -> (usize, Self::Output<'_>) {
        unsafe { (*self).succ_unchecked::<STRICT>(value) }
    }

    #[inline(always)]
    unsafe fn iter_from_succ_unchecked<const STRICT: bool>(
        &self,
        value: impl Borrow<Self::Input>,
    ) -> (usize, Self::Iter<'_>) {
        unsafe { (*self).iter_from_succ_unchecked::<STRICT>(value) }
    }

    #[inline(always)]
    unsafe fn iter_bidi_from_succ_unchecked<const STRICT: bool>(
        &self,
        value: impl Borrow<Self::Input>,
    ) -> (usize, Self::BidiIter<'_>) {
        unsafe { (*self).iter_bidi_from_succ_unchecked::<STRICT>(value) }
    }
}

/// Successor computation for dictionaries whose values are monotonically increasing.
#[delegatable_trait]
pub trait Succ: SuccUnchecked
where
    Self::Input: for<'a> PartialOrd<Self::Output<'a>> + PartialOrd,
    for<'a> Self::Output<'a>: PartialOrd<Self::Input> + PartialOrd,
{
    /// Returns the index of the successor and the successor
    /// of the given value, or `None` if there is no successor.
    ///
    /// The successor is the least value in the dictionary
    /// that is greater than or equal to the given value.
    ///
    /// If there are repeated values, the index of the one returned
    /// depends on the implementation.
    fn succ(&self, value: impl Borrow<Self::Input>) -> Option<(usize, Self::Output<'_>)>;

    /// Returns the index of the strict successor and the strict successor
    /// of the given value, or `None` if there is no strict successor.
    ///
    /// The strict successor is the least value in the dictionary
    /// that is greater than the given value.
    ///
    /// If there are repeated values, the index of the one returned
    /// depends on the implementation.
    fn succ_strict(&self, value: impl Borrow<Self::Input>) -> Option<(usize, Self::Output<'_>)>;

    /// Returns the index of the successor and an iterator starting at
    /// the successor position, or `None` if there is no successor.
    fn iter_from_succ(&self, value: impl Borrow<Self::Input>) -> Option<(usize, Self::Iter<'_>)>;

    /// Returns the index of the strict successor and an iterator starting
    /// at the strict successor position, or `None` if there is no strict
    /// successor.
    fn iter_from_succ_strict(
        &self,
        value: impl Borrow<Self::Input>,
    ) -> Option<(usize, Self::Iter<'_>)>;

    /// Returns the index of the successor and a bidirectional iterator
    /// positioned at the successor, or `None` if there is no successor.
    fn iter_bidi_from_succ(
        &self,
        value: impl Borrow<Self::Input>,
    ) -> Option<(usize, Self::BidiIter<'_>)>;

    /// Returns the index of the strict successor and a bidirectional iterator
    /// positioned at the strict successor, or `None` if there is no strict
    /// successor.
    fn iter_bidi_from_succ_strict(
        &self,
        value: impl Borrow<Self::Input>,
    ) -> Option<(usize, Self::BidiIter<'_>)>;
}

impl<T: Succ + ?Sized> Succ for &T
where
    T::Input: for<'a> PartialOrd<T::Output<'a>> + PartialOrd,
    for<'a> T::Output<'a>: PartialOrd<T::Input> + PartialOrd,
{
    #[inline(always)]
    fn succ(&self, value: impl Borrow<Self::Input>) -> Option<(usize, Self::Output<'_>)> {
        (*self).succ(value)
    }

    #[inline(always)]
    fn succ_strict(&self, value: impl Borrow<Self::Input>) -> Option<(usize, Self::Output<'_>)> {
        (*self).succ_strict(value)
    }

    #[inline(always)]
    fn iter_from_succ(&self, value: impl Borrow<Self::Input>) -> Option<(usize, Self::Iter<'_>)> {
        (*self).iter_from_succ(value)
    }

    #[inline(always)]
    fn iter_from_succ_strict(
        &self,
        value: impl Borrow<Self::Input>,
    ) -> Option<(usize, Self::Iter<'_>)> {
        (*self).iter_from_succ_strict(value)
    }

    #[inline(always)]
    fn iter_bidi_from_succ(
        &self,
        value: impl Borrow<Self::Input>,
    ) -> Option<(usize, Self::BidiIter<'_>)> {
        (*self).iter_bidi_from_succ(value)
    }

    #[inline(always)]
    fn iter_bidi_from_succ_strict(
        &self,
        value: impl Borrow<Self::Input>,
    ) -> Option<(usize, Self::BidiIter<'_>)> {
        (*self).iter_bidi_from_succ_strict(value)
    }
}

/// Unchecked predecessor computation for dictionaries whose values are monotonically increasing.
pub trait PredUnchecked: Types
where
    Self::Input: for<'a> PartialOrd<Self::Output<'a>> + PartialOrd,
    for<'a> Self::Output<'a>: PartialOrd<Self::Input> + PartialOrd,
{
    /// The backward iterator type returned by
    /// [`iter_back_from_pred_unchecked`].
    ///
    /// [`iter_back_from_pred_unchecked`]: PredUnchecked::iter_back_from_pred_unchecked
    type BackIter<'a>: Iterator<Item = Self::Output<'a>>
    where
        Self: 'a;
    /// The bidirectional iterator type returned by
    /// [`iter_bidi_from_pred_unchecked`].
    ///
    /// [`iter_bidi_from_pred_unchecked`]: PredUnchecked::iter_bidi_from_pred_unchecked
    type BidiIter<'a>: BidiIterator<Item = Self::Output<'a>>
    where
        Self: 'a;

    /// Returns the index of the predecessor and the predecessor of the given
    /// value.
    ///
    /// The predecessor is the greatest value in the dictionary that is less
    /// than or equal to the given value, if `STRICT` is `false`, or the
    /// greatest value in the dictionary that is less than the given value, if
    /// `STRICT` is `true`.
    ///
    /// If there are repeated values, the index of the one returned depends on
    /// the implementation.
    ///
    /// # Safety
    ///
    /// The predecessor must exist.
    unsafe fn pred_unchecked<const STRICT: bool>(
        &self,
        value: impl Borrow<Self::Input>,
    ) -> (usize, Self::Output<'_>);

    /// Returns the index of the predecessor and a backward iterator starting
    /// at the predecessor position.
    ///
    /// The iterator's first [`next()`] call returns the
    /// predecessor value itself, and subsequent calls return preceding elements
    /// in decreasing order.
    ///
    /// [`next()`]: Iterator::next
    ///
    /// # Safety
    ///
    /// The predecessor must exist.
    unsafe fn iter_back_from_pred_unchecked<const STRICT: bool>(
        &self,
        value: impl Borrow<Self::Input>,
    ) -> (usize, Self::BackIter<'_>);

    /// Returns the index of the predecessor and a bidirectional iterator
    /// positioned at the predecessor.
    ///
    /// The iterator's first [`next()`] call returns the
    /// predecessor value itself; the first
    /// [`prev()`] call returns the element
    /// after the predecessor.
    ///
    /// [`next()`]: Iterator::next
    /// [`prev()`]: crate::traits::BidiIterator::prev
    ///
    /// # Safety
    ///
    /// The predecessor must exist.
    unsafe fn iter_bidi_from_pred_unchecked<const STRICT: bool>(
        &self,
        value: impl Borrow<Self::Input>,
    ) -> (usize, Self::BidiIter<'_>);
}

impl<T: PredUnchecked + ?Sized> PredUnchecked for &T
where
    T::Input: for<'a> PartialOrd<T::Output<'a>> + PartialOrd,
    for<'a> T::Output<'a>: PartialOrd<T::Input> + PartialOrd,
{
    type BackIter<'a>
        = T::BackIter<'a>
    where
        Self: 'a;
    type BidiIter<'a>
        = T::BidiIter<'a>
    where
        Self: 'a;

    #[inline(always)]
    unsafe fn pred_unchecked<const STRICT: bool>(
        &self,
        value: impl Borrow<Self::Input>,
    ) -> (usize, Self::Output<'_>) {
        unsafe { (*self).pred_unchecked::<STRICT>(value) }
    }

    #[inline(always)]
    unsafe fn iter_back_from_pred_unchecked<const STRICT: bool>(
        &self,
        value: impl Borrow<Self::Input>,
    ) -> (usize, Self::BackIter<'_>) {
        unsafe { (*self).iter_back_from_pred_unchecked::<STRICT>(value) }
    }

    #[inline(always)]
    unsafe fn iter_bidi_from_pred_unchecked<const STRICT: bool>(
        &self,
        value: impl Borrow<Self::Input>,
    ) -> (usize, Self::BidiIter<'_>) {
        unsafe { (*self).iter_bidi_from_pred_unchecked::<STRICT>(value) }
    }
}

/// Predecessor computation for dictionaries whose values are monotonically increasing.
#[delegatable_trait]
pub trait Pred: PredUnchecked
where
    Self::Input: for<'a> PartialOrd<Self::Output<'a>> + PartialOrd,
    for<'a> Self::Output<'a>: PartialOrd<Self::Input> + PartialOrd,
{
    /// Returns the index of the predecessor and the predecessor
    /// of the given value, or `None` if there is no predecessor.
    ///
    /// The predecessor is the greatest value in the dictionary
    /// that is less than or equal to the given value.
    ///
    /// If there are repeated values, the index of the one returned
    /// depends on the implementation.
    fn pred(&self, value: impl Borrow<Self::Input>) -> Option<(usize, Self::Output<'_>)>;

    /// Returns the index of the strict predecessor and the strict predecessor
    /// of the given value, or `None` if there is no strict predecessor.
    ///
    /// The strict predecessor is the greatest value in the dictionary
    /// that is less than the given value.
    ///
    /// If there are repeated values, the index of the one returned
    /// depends on the implementation.
    fn pred_strict(&self, value: impl Borrow<Self::Input>) -> Option<(usize, Self::Output<'_>)>;

    /// Returns the index of the predecessor and a backward iterator starting
    /// at the predecessor, or [`None`] if there is no predecessor.
    fn iter_back_from_pred(
        &self,
        value: impl Borrow<Self::Input>,
    ) -> Option<(usize, Self::BackIter<'_>)>;

    /// Returns the index of the strict predecessor and a backward iterator
    /// starting at the strict predecessor, or [`None`] if there is no strict
    /// predecessor.
    fn iter_back_from_pred_strict(
        &self,
        value: impl Borrow<Self::Input>,
    ) -> Option<(usize, Self::BackIter<'_>)>;

    /// Returns the index of the predecessor and a bidirectional iterator
    /// positioned at the predecessor, or [`None`] if there is no predecessor.
    fn iter_bidi_from_pred(
        &self,
        value: impl Borrow<Self::Input>,
    ) -> Option<(usize, Self::BidiIter<'_>)>;

    /// Returns the index of the strict predecessor and a bidirectional
    /// iterator positioned at the strict predecessor, or [`None`] if there is
    /// no strict predecessor.
    fn iter_bidi_from_pred_strict(
        &self,
        value: impl Borrow<Self::Input>,
    ) -> Option<(usize, Self::BidiIter<'_>)>;
}

impl<T: Pred + ?Sized> Pred for &T
where
    T::Input: for<'a> PartialOrd<T::Output<'a>> + PartialOrd,
    for<'a> T::Output<'a>: PartialOrd<T::Input> + PartialOrd,
{
    #[inline(always)]
    fn pred(&self, value: impl Borrow<Self::Input>) -> Option<(usize, Self::Output<'_>)> {
        (*self).pred(value)
    }

    #[inline(always)]
    fn pred_strict(&self, value: impl Borrow<Self::Input>) -> Option<(usize, Self::Output<'_>)> {
        (*self).pred_strict(value)
    }

    #[inline(always)]
    fn iter_back_from_pred(
        &self,
        value: impl Borrow<Self::Input>,
    ) -> Option<(usize, Self::BackIter<'_>)> {
        (*self).iter_back_from_pred(value)
    }

    #[inline(always)]
    fn iter_back_from_pred_strict(
        &self,
        value: impl Borrow<Self::Input>,
    ) -> Option<(usize, Self::BackIter<'_>)> {
        (*self).iter_back_from_pred_strict(value)
    }

    #[inline(always)]
    fn iter_bidi_from_pred(
        &self,
        value: impl Borrow<Self::Input>,
    ) -> Option<(usize, Self::BidiIter<'_>)> {
        (*self).iter_bidi_from_pred(value)
    }

    #[inline(always)]
    fn iter_bidi_from_pred_strict(
        &self,
        value: impl Borrow<Self::Input>,
    ) -> Option<(usize, Self::BidiIter<'_>)> {
        (*self).iter_bidi_from_pred_strict(value)
    }
}

// Delegations for slices, vectors, and boxed slices

macro_rules! impl_types {
    ($($ty:ty),*) => {$(
        impl Types for [$ty] {
            type Input = $ty;
            type Output<'a> = $ty;
        }

        impl Types for Vec<$ty> {
            type Input = $ty;
            type Output<'a> = $ty;
        }

        impl<const N: usize> Types for [$ty; N] {
            type Input = $ty;
            type Output<'a> = $ty;
        }
    )*};
}

impl_types!(u8, u16, u32, u64, u128, usize);
impl_types!(i8, i16, i32, i64, i128, isize);

macro_rules! impl_indexed_seq {
    ($($ty:ty),*) => {$(
        impl IndexedSeq for [$ty] {
            fn get(&self, index: usize) -> Self::Output<'_> {
                self[index]
            }

            unsafe fn get_unchecked(&self, index: usize) -> Self::Output<'_> {
                debug_assert_bounds!(index, self.len());
                // SAFETY: the caller must ensure index < self.len()
                unsafe { *self.get_unchecked(index) }
            }

            fn len(&self) -> usize {
                self.len()
            }

            fn is_empty(&self) -> bool {
                self.is_empty()
            }
        }

        impl IndexedSeq for Vec<$ty> {
            fn get(&self, index: usize) -> Self::Output<'_> {
                self[index]
            }

            unsafe fn get_unchecked(&self, index: usize) -> Self::Output<'_> {
                use std::ops::Deref;
                debug_assert_bounds!(index, self.len());
                // SAFETY: the caller must ensure index < self.len()
                unsafe { *self.deref().get_unchecked(index) }
            }

            fn len(&self) -> usize {
                self.len()
            }

            fn is_empty(&self) -> bool {
                self.is_empty()
            }
        }

        impl<const N: usize> IndexedSeq for [$ty; N] {
            fn get(&self, index: usize) -> Self::Output<'_> {
                self[index]
            }

            unsafe fn get_unchecked(&self, index: usize) -> Self::Output<'_> {
                debug_assert_bounds!(index, self.len());
                // SAFETY: the caller must ensure index < self.len()
                unsafe { *self.as_slice().get_unchecked(index) }
            }

            fn len(&self) -> usize {
                self.as_slice().len()
            }

            fn is_empty(&self) -> bool {
                self.as_slice().is_empty()
            }
        }
    )*};
}

impl_indexed_seq!(u8, u16, u32, u64, u128, usize);
impl_indexed_seq!(i8, i16, i32, i64, i128, isize);