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
//! The implementations of vector operations.

use crate::{
    common::*,
    size::{self, Dyn, Size},
    vect::Vect,
};

typ! {
    pub fn Push<ty, size: Size>(Vect::<ty, size>: _) {
        let new_size: Size = size::IncreaseOne(size);
        Vect::<ty, new_size>
    }

    pub fn Pop<ty, size: Size>(Vect::<ty, size>: _) {
        match size::DecreaseOne(size) {
            Option::<Dyn> => Option::<Vect<ty, Dyn>>,
            UTerm => Vect::<ty, UTerm>,
            #[generics(uint: Unsigned, bit: Bit)]
            UInt::<uint, bit> => {
                let new_size = UInt::<uint, bit>;
                Vect::<ty, new_size>
            }
        }
    }
}

// VectFactory

pub trait VectFactory<T> {
    fn from_vec(vec: Vec<T>) -> Self;
}

impl<T, S> VectFactory<T> for Vect<T, S>
where
    S: Size,
{
    fn from_vec(vec: Vec<T>) -> Self {
        Vect {
            data: vec,
            _phantom: PhantomData,
        }
    }
}

// push
pub use push::{PushImpl, PushImplOp};

mod push {
    use super::*;

    /// Implements vector appending.
    pub trait PushImpl<Input, Item> {
        type Output;
        fn impl_push(input: Input, elem: Item) -> Self::Output;
    }

    impl<T, S> PushImpl<Vect<T, S>, T> for ()
    where
        S: Size,
        (): Push<Vect<T, S>>,
        PushOp<Vect<T, S>>: VectFactory<T>,
    {
        type Output = PushOp<Vect<T, S>>;

        fn impl_push(input: Vect<T, S>, item: T) -> Self::Output {
            let mut data = input.data;
            data.push(item);
            PushOp::<Vect<T, S>>::from_vec(data)
        }
    }

    pub type PushImplOp<Input, Item> = <() as PushImpl<Input, Item>>::Output;
}

// pop
pub use pop::{PopImpl, PopImplOp};

mod pop {
    use super::*;

    /// Implements dropping an element at the end of vector.
    pub trait PopImpl<Input> {
        type Output;
        fn impl_pop(input: Input) -> Self::Output;
    }

    impl<Input> PopImpl<Input> for ()
    where
        (): PopPrivate<Input, PopOp<Input>> + Pop<Input>,
    {
        type Output = <() as PopPrivate<Input, PopOp<Input>>>::Output;

        fn impl_pop(input: Input) -> Self::Output {
            <() as PopPrivate<Input, PopOp<Input>>>::impl_pop(input)
        }
    }

    pub type PopImplOp<Input> = <() as PopImpl<Input>>::Output;

    pub trait PopPrivate<Input, Out> {
        type Output;
        fn impl_pop(input: Input) -> Self::Output;
    }

    impl<T, S, Output> PopPrivate<Vect<T, S>, Option<Output>> for ()
    where
        S: Size,
        Output: VectFactory<T>,
    {
        type Output = Option<(Output, T)>;

        fn impl_pop(input: Vect<T, S>) -> Self::Output {
            let mut data = input.data;
            let elem = data.pop()?;
            Some((Output::from_vec(data), elem))
        }
    }

    impl<T, S1, S2> PopPrivate<Vect<T, S1>, Vect<T, S2>> for ()
    where
        S1: Size,
        S2: Size,
        Vect<T, S2>: VectFactory<T>,
    {
        type Output = (Vect<T, S2>, T);

        fn impl_pop(input: Vect<T, S1>) -> Self::Output {
            let mut data = input.data;
            let elem = data.pop().unwrap();
            (Vect::<T, S2>::from_vec(data), elem)
        }
    }
}

// get

pub use get::{GetImpl, GetImplOp};

mod get {
    use super::*;

    /// Implements accessing an vector on vector by type level index.
    pub trait GetImpl<'a, Input, Index> {
        type Output;
        fn impl_get(input: &'a Input, index: Index) -> Self::Output;
    }

    impl<'a, T, S, Index> GetImpl<'a, Vect<T, S>, Index> for ()
    where
        S: Size,
        Index: Size,
        (): GetPrivate<'a, Vect<T, S>, Index, size::CheckIndexOp<S, Index>>
            + size::CheckIndex<S, Index>,
    {
        type Output =
            <() as GetPrivate<'a, Vect<T, S>, Index, size::CheckIndexOp<S, Index>>>::Output;

        fn impl_get(input: &'a Vect<T, S>, index: Index) -> Self::Output {
            <() as GetPrivate<'a, Vect<T, S>, Index, size::CheckIndexOp<S, Index>>>::impl_get(
                input, index,
            )
        }
    }

    pub type GetImplOp<'a, Input, Index> = <() as GetImpl<'a, Input, Index>>::Output;

    pub trait GetPrivate<'a, Input, Index, Out> {
        type Output;
        fn impl_get(input: &'a Input, index: Index) -> Self::Output;
    }

    impl<'a, T, S, Index> GetPrivate<'a, Vect<T, S>, Index, ()> for ()
    where
        T: 'a,
        S: Size,
        Index: Unsigned + Size,
    {
        type Output = &'a T;

        fn impl_get(input: &'a Vect<T, S>, _index: Index) -> Self::Output {
            unsafe { input.data.get_unchecked(Index::USIZE) }
        }
    }

    impl<'a, T, S, Index> GetPrivate<'a, Vect<T, S>, Index, Option<()>> for ()
    where
        T: 'a,
        S: Size,
        Index: Size,
    {
        type Output = Option<&'a T>;

        fn impl_get(input: &'a Vect<T, S>, index: Index) -> Self::Output {
            input.data.get(index.to_usize())
        }
    }
}

// insert

pub use insert::{InsertImpl, InsertImplOp};

mod insert {
    use super::*;

    /// Implements element insertion to a vector.
    pub trait InsertImpl<Input, Index, Item> {
        type Output;
        fn impl_insert(input: Input, index: Index, item: Item) -> Self::Output;
    }
    pub type InsertImplOp<Input, Index, Item> = <() as InsertImpl<Input, Index, Item>>::Output;

    impl<S, Index, Item> InsertImpl<Vect<Item, S>, Index, Item> for ()
    where
        S: Size,
        Index: Size,
        (): size::IncreaseOne<S> + size::CheckIndexInclusive<S, Index>,
    {
        type Output = Vect<Item, size::IncreaseOneOp<S>>;

        fn impl_insert(input: Vect<Item, S>, index: Index, item: Item) -> Self::Output {
            let mut data = input.data;
            data.insert(index.to_usize(), item);
            <Self::Output as VectFactory<Item>>::from_vec(data)
        }
    }
}

// remove

pub use remove::{RemoveImpl, RemoveImplOp};

mod remove {
    use super::*;

    /// Implements element removal from a vector.
    pub trait RemoveImpl<Input, Index> {
        type Output;
        fn impl_remove(input: Input, index: Index) -> Self::Output;
    }
    pub type RemoveImplOp<Input, Index> = <() as RemoveImpl<Input, Index>>::Output;

    impl<S, Index, Item> RemoveImpl<Vect<Item, S>, Index> for ()
    where
        S: Size,
        Index: Size,
        (): size::DecreaseOne<S> + size::CheckIndex<S, Index>,
    {
        type Output = (Vect<Item, size::DecreaseOneOp<S>>, Item);

        fn impl_remove(input: Vect<Item, S>, index: Index) -> Self::Output {
            let mut data = input.data;
            let item = data.remove(index.to_usize());
            (
                <Vect<Item, size::DecreaseOneOp<S>> as VectFactory<Item>>::from_vec(data),
                item,
            )
        }
    }
}