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
//! Types for arrays of nibbles.
use core::ops;
use core::slice::{self as stdslice, from_raw_parts, from_raw_parts_mut};
use core::iter::FromIterator;
use arrayvec::{Array, ArrayVec, CapacityError};
use base::{u4lo, u4};
use pair::u4x2;
use slice::{self, NibSliceAligned, NibSliceAlignedMut, NibSliceFull, NibSliceNoR};
use common::{get_nib, shift_left, shift_right, set_nib};

/// An `ArrayVec` of nibbles.
#[derive(Clone)]
pub struct NibArrayVec<A: Array<Item = u4x2>>  {
    inner: ArrayVec<A>,
    has_right_lo: bool,
}
impl<A: Array<Item = u4x2>> NibArrayVec<A> {
    /// Creates an empty `NibArrayVec`.
    pub fn new() -> Self {
        NibArrayVec { inner: ArrayVec::new(), has_right_lo: true }
    }

    /// Number of nibbles in the vector.
    pub fn len(&self) -> usize {
        (self.inner.len() >> 1).saturating_sub(!self.has_right_lo as usize)
    }

    /// Whether the vector is empty.
    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    /// How many nibbles can be stored in the vector.
    pub fn capacity(&self) -> usize {
        self.inner.capacity() >> 1
    }

    /// Whether the vector is full.
    pub fn is_full(&self) -> bool {
        self.inner.is_full() && (self.inner.capacity() == 0 || self.has_right_lo)
    }

    /// Pushes a nibble onto the vector.
    ///
    /// # Panics
    ///
    /// Panics if the vector is full.
    pub fn push<T: u4>(&mut self, nib: T) {
        self.has_right_lo = !self.has_right_lo;
        if self.has_right_lo {
            self.inner.push(u4x2::from_hi(nib.to_u4hi()));
        } else {
            let i = self.inner.len() - 1;
            self.inner[i].set_lo(nib)
        }
    }

    /// Pushes a nibble onto the vector if possible.
    pub fn try_push<T: u4>(&mut self, nib: T) -> Result<(), CapacityError<T>> {
        if self.has_right_lo {
            match self.inner.try_push(u4x2::from_hi(nib.to_u4hi())) {
                Ok(()) => self.has_right_lo = false,
                Err(_) => return Err(CapacityError::new(nib)),
            }
        } else {
            let i = self.inner.len() - 1;
            self.inner[i].set_lo(nib);
        }
        Ok(())
    }

    /// Pushes a nibble onto the vector without checking if it's full.
    pub unsafe fn push_unchecked<T: u4>(&mut self, nib: T) {
        self.has_right_lo = !self.has_right_lo;
        if self.has_right_lo {
            self.inner.push_unchecked(u4x2::from_hi(nib.to_u4hi()));
        } else {
            let i = self.inner.len() - 1;
            self.inner[i].set_lo(nib)
        }
    }

    /// Inserts a nibble into the vector at the given index.
    pub fn insert<T: u4>(&mut self, index: usize, nib: T) {
        if self.has_right_lo {
            self.push(u4lo::from_lo(0));
        }
        shift_right(self.inner.as_mut_slice(), index);
        set_nib(self.inner.as_mut_slice(), index, nib);
    }

    /// Inserts a nibble into the vector at the given index.
    pub fn try_insert<T: u4>(&mut self, index: usize, nib: T) -> Result<(), CapacityError<T>> {
        let lo = nib.to_u4lo();
        if self.has_right_lo {
            self.inner.try_push(u4x2::from_byte(0)).map_err(|_| CapacityError::new(nib))?;
        }
        shift_right(self.inner.as_mut_slice(), index);
        set_nib(self.inner.as_mut_slice(), index, lo);
        Ok(())
    }

    fn discard_at(&mut self, index: usize) {
        shift_left(self.inner.as_mut_slice(), index);
        self.has_right_lo = !self.has_right_lo;
        if self.has_right_lo {
            self.inner.pop();
        }
    }

    /// Removes a nibble from the vector at the given index.
    pub fn remove<T: u4>(&mut self, index: usize) -> T {
        let ret = get_nib(self.inner.as_slice(), index);
        self.discard_at(index);
        ret
    }

    /// Removes a nibble from the vector at the given index, converting it to a high-order nibble.
    pub fn pop_at<T: u4>(&mut self, index: usize) -> Option<T> {
        if index >= self.len() {
            None
        } else {
            Some(self.remove(index))
        }
    }

    /// Removes a nibble from the vector, converting it to a high-order nibble.
    pub fn pop<T: u4>(&mut self) -> Option<T> {
        self.has_right_lo = !self.has_right_lo;
        if self.has_right_lo {
            Some(T::from_lo(self.inner[self.inner.len() - 1].lo().to_lo()))
        } else {
            self.inner.pop().map(|pair| T::from_hi(pair.hi().to_hi()))
        }
    }

    /// Clears the vector, removing all nibbles.
    pub fn clear(&mut self) {
        self.inner.clear();
        self.has_right_lo = true;
    }

    /// Disposes of the vector.
    pub fn dispose(self) {
        self.inner.dispose();
    }

    /// Converts the vector into an odd array, if it's full to one less than capacity.
    pub fn into_odd_array(self) -> Result<NibArrayOdd<A>, Self> {
        if self.inner.is_full() && !self.has_right_lo {
            Ok(NibArrayOdd { inner: self.inner.into_inner().unwrap() })
        } else {
            Err(self)
        }
    }

    /// Converts the vector into an even array, if it's full to capacity.
    pub fn into_even_array(self) -> Result<NibArrayEven<A>, Self> {
        if self.inner.is_full() && self.has_right_lo {
            Ok(NibArrayEven { inner: self.inner.into_inner().unwrap() })
        } else {
            Err(self)
        }
    }

    /// Intreprets this array as a slice.
    pub fn as_slice(&self) -> NibSliceAligned {
        if self.has_right_lo {
            NibSliceAligned::Even(unsafe { &*(&self.inner[..] as *const [u4x2] as *const NibSliceFull) })
        } else {
            NibSliceAligned::Odd(unsafe { &*(&self.inner[..] as *const [u4x2] as *const NibSliceNoR) })
        }
    }

    /// Intreprets this array as a mutable slice.
    pub fn as_mut_slice(&mut self) -> NibSliceAlignedMut {
        if self.has_right_lo {
            NibSliceAlignedMut::Even(unsafe { &mut *(&mut self.inner[..] as *mut [u4x2] as *mut NibSliceFull) })
        } else {
            NibSliceAlignedMut::Odd(unsafe { &mut *(&mut self.inner[..] as *mut [u4x2] as *mut NibSliceNoR) })
        }
    }
}
impl<A: Array<Item = u4x2>> Default for NibArrayVec<A> {
    fn default() -> Self {
        NibArrayVec::new()
    }
}
impl<A: Array<Item = u4x2>, T: u4> FromIterator<T> for NibArrayVec<A> {
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        let mut vec = Self::new();
        vec.extend(iter);
        vec
    }
}
impl<'a, A: Array<Item = u4x2>> FromIterator<&'a u4> for NibArrayVec<A> {
    fn from_iter<I: IntoIterator<Item = &'a u4>>(iter: I) -> Self {
        let mut vec = Self::new();
        vec.extend(iter);
        vec
    }
}
impl<A: Array<Item = u4x2>> FromIterator<u4x2> for NibArrayVec<A> {
    fn from_iter<I: IntoIterator<Item = u4x2>>(iter: I) -> Self {
        let mut vec = Self::new();
        vec.extend(iter);
        vec
    }
}
impl<A: Array<Item = u4x2>, T: u4> Extend<T> for NibArrayVec<A> {
    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
        for nib in iter {
            self.push(nib);
        }
    }
}
impl<'a, A: Array<Item = u4x2>> Extend<&'a u4> for NibArrayVec<A> {
    fn extend<I: IntoIterator<Item = &'a u4>>(&mut self, iter: I) {
        for nib in iter {
            self.push(nib.to_u4lo());
        }
    }
}
impl<A: Array<Item = u4x2>> Extend<u4x2> for NibArrayVec<A> {
    fn extend<I: IntoIterator<Item = u4x2>>(&mut self, iter: I) {
        for nib in iter {
            self.push(*nib.hi());
            self.push(*nib.lo());
        }
    }
}
impl<A: Array<Item = u4x2>> slice::private::Sealed for NibArrayVec<A> {
    #[inline(always)]
    fn has_left_hi(&self) -> bool { true }
    #[inline(always)]
    fn has_right_lo(&self) -> bool { self.as_slice().has_right_lo() }
    #[inline(always)]
    fn iter(&self) -> stdslice::Iter<u4x2> { self.inner.iter() }
}
impl<A: Array<Item = u4x2>> slice::private::SealedMut for NibArrayVec<A> {
    #[inline(always)]
    fn iter_mut(&mut self) -> stdslice::IterMut<u4x2> { self.inner.iter_mut() }
}
impl<A: Array<Item = u4x2>> slice::NibSliceExt for NibArrayVec<A> {}
impl<A: Array<Item = u4x2>> slice::NibSliceMutExt for NibArrayVec<A> {}

/// An array with an even number of nibbles.
pub struct NibArrayEven<A: Array<Item = u4x2>> {
    inner: A,
}
impl<A: Array<Item = u4x2>> From<A> for NibArrayEven<A> {
    fn from(inner: A) -> Self {
        NibArrayEven { inner }
    }
}
impl<A: Array<Item = u4x2>> ops::Deref for NibArrayEven<A> {
    type Target = NibSliceFull;
    fn deref(&self) -> &NibSliceFull {
        let slice = unsafe { from_raw_parts(A::as_ptr(&self.inner), A::capacity()) };
        unsafe { &*(slice as *const [u4x2] as *const NibSliceFull) }
    }
}
impl<A: Array<Item = u4x2>> ops::DerefMut for NibArrayEven<A> {
    fn deref_mut(&mut self) -> &mut NibSliceFull {
        let slice = unsafe { from_raw_parts_mut(A::as_mut_ptr(&mut self.inner), A::capacity()) };
        unsafe { &mut *(slice as *mut [u4x2] as *mut NibSliceFull) }
    }
}
impl<A: Array<Item = u4x2>> slice::private::Sealed for NibArrayEven<A> {
    #[inline(always)]
    fn has_left_hi(&self) -> bool { true }
    #[inline(always)]
    fn has_right_lo(&self) -> bool { true }
    #[inline(always)]
    fn iter(&self) -> stdslice::Iter<u4x2> {
        unsafe { stdslice::from_raw_parts(A::as_ptr(&self.inner), A::capacity()) }.iter()
    }
}
impl<A: Array<Item = u4x2>> slice::private::SealedMut for NibArrayEven<A> {
    #[inline(always)]
    fn iter_mut(&mut self) -> stdslice::IterMut<u4x2> {
        unsafe { stdslice::from_raw_parts_mut(A::as_mut_ptr(&mut self.inner), A::capacity()) }.iter_mut()
    }
}
impl<A: Array<Item = u4x2>> slice::NibSliceExt for NibArrayEven<A> {}
impl<A: Array<Item = u4x2>> slice::NibSliceMutExt for NibArrayEven<A> {}

/// An array with an odd number of nibbles.
pub struct NibArrayOdd<A: Array<Item = u4x2>> {
    inner: A
}
impl<A: Array<Item = u4x2>> From<A> for NibArrayOdd<A> {
    fn from(inner: A) -> Self {
        NibArrayOdd { inner }
    }
}
impl<A: Array<Item = u4x2>> ops::Deref for NibArrayOdd<A> {
    type Target = NibSliceNoR;
    fn deref(&self) -> &NibSliceNoR {
        let slice = unsafe { from_raw_parts(A::as_ptr(&self.inner), A::capacity()) };
        unsafe { &*(slice as *const [u4x2] as *const NibSliceNoR) }
    }
}
impl<A: Array<Item = u4x2>> ops::DerefMut for NibArrayOdd<A> {
    fn deref_mut(&mut self) -> &mut NibSliceNoR {
        let slice = unsafe { from_raw_parts_mut(A::as_mut_ptr(&mut self.inner), A::capacity()) };
        unsafe { &mut *(slice as *mut [u4x2] as *mut NibSliceNoR) }
    }
}
impl<A: Array<Item = u4x2>> slice::private::Sealed for NibArrayOdd<A> {
    #[inline(always)]
    fn has_left_hi(&self) -> bool { true }
    #[inline(always)]
    fn has_right_lo(&self) -> bool { false }
    #[inline(always)]
    fn iter(&self) -> stdslice::Iter<u4x2> {
        unsafe { stdslice::from_raw_parts(A::as_ptr(&self.inner), A::capacity()) }.iter()
    }
}
impl<A: Array<Item = u4x2>> slice::private::SealedMut for NibArrayOdd<A> {
    #[inline(always)]
    fn iter_mut(&mut self) -> stdslice::IterMut<u4x2> {
        unsafe { stdslice::from_raw_parts_mut(A::as_mut_ptr(&mut self.inner), A::capacity()) }.iter_mut()
    }
}
impl<A: Array<Item = u4x2>> slice::NibSliceExt for NibArrayOdd<A> {}
impl<A: Array<Item = u4x2>> slice::NibSliceMutExt for NibArrayOdd<A> {}

/// An array of nibbles.
pub enum NibArray<A: Array<Item = u4x2>> {
    Even(NibArrayEven<A>),
    Odd(NibArrayOdd<A>),
}
impl<A: Array<Item = u4x2>> NibArray<A> {
    /// Intreprets this array as a slice.
    pub fn as_slice(&self) -> NibSliceAligned {
        match *self {
            NibArray::Even(ref e) => NibSliceAligned::Even(e),
            NibArray::Odd(ref e) => NibSliceAligned::Odd(e),
        }
    }

    /// Intreprets this array as a mutable slice.
    pub fn as_mut_slice(&mut self) -> NibSliceAlignedMut {
        match *self {
            NibArray::Even(ref mut e) => NibSliceAlignedMut::Even(e),
            NibArray::Odd(ref mut e) => NibSliceAlignedMut::Odd(e),
        }
    }
}
impl<A: Array<Item = u4x2>> slice::private::Sealed for NibArray<A> {
    #[inline(always)]
    fn has_left_hi(&self) -> bool { true }

    fn has_right_lo(&self) -> bool {
        match *self {
            NibArray::Even(_) => true,
            NibArray::Odd(_) => false,
        }
    }

    fn iter(&self) -> stdslice::Iter<u4x2> {
        match *self {
            NibArray::Even(ref s) => unsafe { stdslice::from_raw_parts(A::as_ptr(&s.inner), A::capacity()) }.iter(),
            NibArray::Odd(ref s) => unsafe { stdslice::from_raw_parts(A::as_ptr(&s.inner), A::capacity()) }.iter(),
        }
    }
}
impl<A: Array<Item = u4x2>> slice::private::SealedMut for NibArray<A> {
    fn iter_mut(&mut self) -> stdslice::IterMut<u4x2> {
        match *self {
            NibArray::Even(ref mut s) => unsafe { stdslice::from_raw_parts_mut(A::as_mut_ptr(&mut s.inner), A::capacity()) }.iter_mut(),
            NibArray::Odd(ref mut s) => unsafe { stdslice::from_raw_parts_mut(A::as_mut_ptr(&mut s.inner), A::capacity()) }.iter_mut(),
        }
    }
}
impl<A: Array<Item = u4x2>> slice::NibSliceExt for NibArray<A> {}
impl<A: Array<Item = u4x2>> slice::NibSliceMutExt for NibArray<A> {}