|
| 1 | + |
| 2 | +use std::ops::{ |
| 3 | + Index, |
| 4 | + IndexMut, |
| 5 | + Deref, |
| 6 | + DerefMut, |
| 7 | +}; |
| 8 | +use imp_prelude::*; |
| 9 | +use dimension::DimPrivate; |
| 10 | + |
| 11 | +const CAP: usize = 4; |
| 12 | + |
| 13 | +/// T is usize or isize |
| 14 | +#[derive(Debug)] |
| 15 | +enum IxDynRepr<T> { |
| 16 | + Inline(u32, [T; CAP]), |
| 17 | + Alloc(Box<[T]>), |
| 18 | +} |
| 19 | + |
| 20 | +impl<T> Deref for IxDynRepr<T> { |
| 21 | + type Target = [T]; |
| 22 | + fn deref(&self) -> &[T] { |
| 23 | + match *self { |
| 24 | + IxDynRepr::Inline(len, ref ar) => { |
| 25 | + unsafe { |
| 26 | + ar.get_unchecked(..len as usize) |
| 27 | + } |
| 28 | + } |
| 29 | + IxDynRepr::Alloc(ref ar) => &*ar, |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl<T> DerefMut for IxDynRepr<T> { |
| 35 | + fn deref_mut(&mut self) -> &mut [T] { |
| 36 | + match *self { |
| 37 | + IxDynRepr::Inline(len, ref mut ar) => { |
| 38 | + unsafe { |
| 39 | + ar.get_unchecked_mut(..len as usize) |
| 40 | + } |
| 41 | + } |
| 42 | + IxDynRepr::Alloc(ref mut ar) => &mut *ar, |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/// The default is equivalent to `Self::from(&[0])`. |
| 48 | +impl Default for IxDynRepr<Ix> { |
| 49 | + fn default() -> Self { |
| 50 | + Self::copy_from(&[0]) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +use ::libnum::Zero; |
| 56 | + |
| 57 | +impl<T: Copy + Zero> IxDynRepr<T> { |
| 58 | + pub fn copy_from(x: &[T]) -> Self { |
| 59 | + if x.len() <= CAP { |
| 60 | + let mut arr = [T::zero(); CAP]; |
| 61 | + for i in 0..x.len() { |
| 62 | + arr[i] = x[i]; |
| 63 | + } |
| 64 | + IxDynRepr::Inline(x.len() as _, arr) |
| 65 | + } else { |
| 66 | + Self::from(x) |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl<T: Copy + Zero> IxDynRepr<T> { |
| 72 | + // make an Inline or Alloc version as appropriate |
| 73 | + fn from_vec_auto(v: Vec<T>) -> Self { |
| 74 | + if v.len() <= CAP { |
| 75 | + Self::copy_from(&v) |
| 76 | + } else { |
| 77 | + Self::from_vec(v) |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl<T: Copy> IxDynRepr<T> { |
| 83 | + fn from_vec(v: Vec<T>) -> Self { |
| 84 | + IxDynRepr::Alloc(v.into_boxed_slice()) |
| 85 | + } |
| 86 | + |
| 87 | + fn from(x: &[T]) -> Self { |
| 88 | + Self::from_vec(x.to_vec()) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl<T: Copy> Clone for IxDynRepr<T> { |
| 93 | + fn clone(&self) -> Self { |
| 94 | + match *self { |
| 95 | + IxDynRepr::Inline(len, arr) => { |
| 96 | + IxDynRepr::Inline(len, arr) |
| 97 | + } |
| 98 | + _ => Self::from(&self[..]) |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +impl<T: Eq> Eq for IxDynRepr<T> { } |
| 104 | + |
| 105 | +impl<T: PartialEq> PartialEq for IxDynRepr<T> { |
| 106 | + fn eq(&self, rhs: &Self) -> bool { |
| 107 | + match (self, rhs) { |
| 108 | + (&IxDynRepr::Inline(slen, ref sarr), &IxDynRepr::Inline(rlen, ref rarr)) => { |
| 109 | + slen == rlen && |
| 110 | + (0..CAP as usize).filter(|&i| i < slen as usize) |
| 111 | + .all(|i| sarr[i] == rarr[i]) |
| 112 | + } |
| 113 | + _ => self[..] == rhs[..] |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +/// Dynamic dimension or index type. |
| 119 | +/// |
| 120 | +/// Use `IxDyn` directly. This type implements a dynamic number of |
| 121 | +/// dimensions or indices. Short dimensions are stored inline and don't need |
| 122 | +/// any dynamic memory allocation. |
| 123 | +#[derive(Debug, Clone, PartialEq, Eq, Default)] |
| 124 | +pub struct IxDynImpl(IxDynRepr<Ix>); |
| 125 | +unsafe impl Send for IxDynImpl {} |
| 126 | +unsafe impl Sync for IxDynImpl {} |
| 127 | + |
| 128 | +impl IxDynImpl { |
| 129 | + fn remove(&self, i: usize) -> Self { |
| 130 | + IxDynImpl(match self.0 { |
| 131 | + IxDynRepr::Inline(0, _) => IxDynRepr::Inline(0, [0; CAP]), |
| 132 | + IxDynRepr::Inline(1, _) => IxDynRepr::Inline(0, [0; CAP]), |
| 133 | + IxDynRepr::Inline(2, ref arr) => { |
| 134 | + let mut out = [0; CAP]; |
| 135 | + out[0] = arr[1 - i]; |
| 136 | + IxDynRepr::Inline(1, out) |
| 137 | + } |
| 138 | + ref ixdyn => { |
| 139 | + let len = ixdyn.len(); |
| 140 | + let mut result = IxDynRepr::copy_from(&ixdyn[..len - 1]); |
| 141 | + for j in i..len - 1 { |
| 142 | + result[j] = ixdyn[j + 1] |
| 143 | + } |
| 144 | + result |
| 145 | + } |
| 146 | + }) |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +impl<'a> From<&'a [Ix]> for IxDynImpl { |
| 151 | + #[inline] |
| 152 | + fn from(ix: &'a [Ix]) -> Self { |
| 153 | + IxDynImpl(IxDynRepr::copy_from(ix)) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +impl From<Vec<Ix>> for IxDynImpl { |
| 158 | + #[inline] |
| 159 | + fn from(ix: Vec<Ix>) -> Self { |
| 160 | + IxDynImpl(IxDynRepr::from_vec_auto(ix)) |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +impl<J> Index<J> for IxDynImpl |
| 165 | + where [Ix]: Index<J>, |
| 166 | +{ |
| 167 | + type Output = <[Ix] as Index<J>>::Output; |
| 168 | + fn index(&self, index: J) -> &Self::Output { |
| 169 | + &self.0[index] |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +impl<J> IndexMut<J> for IxDynImpl |
| 174 | + where [Ix]: IndexMut<J>, |
| 175 | +{ |
| 176 | + fn index_mut(&mut self, index: J) -> &mut Self::Output { |
| 177 | + &mut self.0[index] |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +impl Deref for IxDynImpl { |
| 182 | + type Target = [Ix]; |
| 183 | + #[inline] |
| 184 | + fn deref(&self) -> &[Ix] { |
| 185 | + &self.0 |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +impl DerefMut for IxDynImpl { |
| 190 | + #[inline] |
| 191 | + fn deref_mut(&mut self) -> &mut [Ix] { |
| 192 | + &mut self.0 |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +impl<'a> IntoIterator for &'a IxDynImpl { |
| 197 | + type Item = &'a Ix; |
| 198 | + type IntoIter = <&'a [Ix] as IntoIterator>::IntoIter; |
| 199 | + #[inline] |
| 200 | + fn into_iter(self) -> Self::IntoIter { |
| 201 | + self[..].into_iter() |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +impl RemoveAxis for Dim<IxDynImpl> { |
| 206 | + type Smaller = Self; |
| 207 | + fn remove_axis(&self, axis: Axis) -> Self { |
| 208 | + debug_assert!(axis.index() < self.ndim()); |
| 209 | + Dim::new(self.ix().remove(axis.index())) |
| 210 | + } |
| 211 | +} |
0 commit comments