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
use std::marker::PhantomData;

use crate::prelude::*;

/// A trait indicating that a type can be referenced to a Trusted and DoubleEnded iterator.
/// A trait for types that can be iterated over with a trusted iterator.
///
/// This trait extends the `GetLen` trait and provides methods to create
/// trusted iterators over the implementing type.
///
/// # Type Parameters
///
/// * `T`: The type of items yielded by the iterator.
pub trait TIter<T>: GetLen {
    /// Creates a trusted iterator over the items of this collection.
    ///
    /// # Returns
    ///
    /// An iterator that implements the `TIterator` trait, yielding items of type `T`.
    fn titer<'a>(&'a self) -> impl TIterator<Item = T>
    where
        Self: 'a,
        T: 'a;

    /// Maps each item in the collection using the provided function.
    ///
    /// This method creates a new iterator that applies the given function to
    /// each item yielded by the original iterator.
    ///
    /// # Arguments
    ///
    /// * `f`: A function that takes an item of type `T` and returns an item of type `U`.
    ///
    /// # Returns
    ///
    /// An iterator that yields the mapped items.
    ///
    /// # Type Parameters
    ///
    /// * `'a`: The lifetime of the reference to `self`.
    /// * `U`: The type of items yielded by the new iterator.
    /// * `F`: The type of the mapping function.
    #[inline]
    fn map<'a, U, F>(&'a self, f: F) -> impl TIterator<Item = U>
    where
        F: FnMut(T) -> U,
        T: 'a,
    {
        self.titer().map(f)
    }
}

/// A trait indicating that a type can be converted into a Trusted and DoubleEnded iterator.
pub trait IntoTIter: IntoIterator
where
    Self::IntoIter: TIterator,
{
    fn into_titer(self) -> Self::IntoIter
    where
        Self: Sized;
}

impl<I: IntoIterator + GetLen> IntoTIter for I
where
    Self::IntoIter: TIterator,
{
    #[inline]
    fn into_titer(self) -> Self::IntoIter {
        self.into_iter()
    }
}
/// An iterator adapter that wraps a `Vec1View` and converts its items to `Option` types.
///
/// This struct provides a way to iterate over a `Vec1View` while converting each item
/// to an `Option` type. It's particularly useful when working with types that implement
/// the `IsNone` trait, allowing for a uniform representation of potentially absent values.
///
/// # Type Parameters
///
/// * `'a`: The lifetime of the reference to the underlying `Vec1View`.
/// * `V`: The type of the underlying `Vec1View`.
/// * `T`: The item type of the `Vec1View`.
///
/// # Fields
///
/// * `view`: A reference to the underlying `Vec1View`.
/// * `item`: A `PhantomData` to carry the item type `T`.
pub struct OptIter<'a, V: Vec1View<T>, T> {
    pub view: &'a V,
    pub item: PhantomData<T>,
}

impl<V: Vec1View<T>, T> GetLen for OptIter<'_, V, T> {
    #[inline]
    fn len(&self) -> usize {
        self.view.len()
    }
}

impl<V: Vec1View<T>, T: IsNone> TIter<Option<<T as IsNone>::Inner>> for OptIter<'_, V, T> {
    #[inline]
    fn titer<'a>(&'a self) -> impl TIterator<Item = Option<<T as IsNone>::Inner>>
    where
        Self: 'a,
    {
        self.view.titer().map(|v| v.to_opt())
    }
}

impl<'a, T: IsNone + 'a, V: Vec1View<T>> Vec1View<Option<T::Inner>> for OptIter<'a, V, T>
where
    for<'b> V::SliceOutput<'b>: TIter<T>,
{
    type SliceOutput<'b> = Vec<Option<T::Inner>> where Self: 'b;

    #[inline]
    fn slice<'b>(&'b self, start: usize, end: usize) -> TResult<Self::SliceOutput<'b>>
    where
        Self: 'b,
        T: 'b,
    {
        Ok(self
            .view
            .slice(start, end)?
            .titer()
            .map(|v| v.to_opt())
            .collect_trusted_to_vec())
    }

    #[inline]
    fn get_backend_name(&self) -> &'static str {
        self.view.get_backend_name()
    }

    #[inline]
    unsafe fn uget(&self, index: usize) -> Option<T::Inner> {
        self.view.uget(index).to_opt()
    }
}

impl<'a, 'b, V: Vec1View<T>, T: IsNone> IntoIterator for &'b OptIter<'a, V, T> {
    type Item = Option<T::Inner>;
    type IntoIter = Box<dyn TrustedLen<Item = Option<T::Inner>> + 'b>;
    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        Box::new(self.titer())
    }
}