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
//! Array utilities use ::{generic_array, typenum}; use typenum::consts::*; use generic_array::GenericArray; /// Create a new array from the first elements of an array of pairs of the same /// length as the first array. /// /// ``` /// # extern crate generic_array; /// # extern crate typenum; /// # extern crate rs_utils; /// # fn main() { /// # use rs_utils::array::fst; /// use generic_array::GenericArray; /// use typenum::consts::*; /// let xs : [(u8, f32); 2] = [(5, 1.0), (100, -1.0)]; /// let ys : GenericArray <u8, U2> = fst (&xs.into()); /// assert_eq!(ys.as_slice(), [5, 100]); /// # } /// ``` /// /// A compilation error will result if the lengths of the arrays do not match: /// /// ```compile_fail /// # extern crate generic_array; /// # extern crate typenum; /// # extern crate rs_utils; /// # fn main() { /// # use rs_utils::array::fst; /// use generic_array::GenericArray; /// use typenum::consts::*; /// let xs : [(u8, f32); 2] = [(5, 1.0), (100, -1.0)]; /// let ys : GenericArray <u8, U1> = fst (&xs.into()); /// # } /// ``` pub fn fst <A, B, N> (from : &GenericArray <(A,B), N>) -> GenericArray <A, N> where A : Clone, N : generic_array::ArrayLength <(A,B)> + generic_array::ArrayLength <A>, { GenericArray::generate (|i| from[i].0.clone()) } /// Create a new array from the first elements of an array of pairs or the /// default if the destination array length is longer than the input array /// length. /// /// ``` /// # extern crate generic_array; /// # extern crate typenum; /// # extern crate rs_utils; /// # fn main() { /// # use rs_utils::array::fst_default; /// use generic_array::GenericArray; /// use typenum::consts::*; /// let xs : [(u8, f32); 2] = [(5, 1.0), (100, -1.0)]; /// let ys : GenericArray <u8, U3> = fst_default (&xs.into()); /// assert_eq!(ys.as_slice(), [5, 100, 0]); /// # } /// ``` pub fn fst_default <A, B, N, M> (from : &GenericArray <(A,B), N>) -> GenericArray <A, M> where A : Clone + Default, N : generic_array::ArrayLength <(A,B)>, M : generic_array::ArrayLength <A> { GenericArray::generate (|i| if i < N::to_usize() { from[i].0.clone() } else { A::default() }) } /// Create a new array from the first elements of an array of pairs of equal /// or greater length than the first. /// /// ``` /// # extern crate generic_array; /// # extern crate typenum; /// # extern crate rs_utils; /// # fn main() { /// # use rs_utils::array::fst_initial; /// use generic_array::GenericArray; /// use typenum::consts::*; /// let xs : [(u8, f32); 2] = [(5, 1.0), (100, -1.0)]; /// let ys : GenericArray <u8, U1> = fst_initial (&xs.into()); /// assert_eq!(ys.as_slice(), [5]); /// # } /// ``` /// /// A compilation error will result if the length of the input array is /// shorter than the length of the destination array: /// /// ```compile_fail /// # extern crate generic_array; /// # extern crate typenum; /// # extern crate rs_utils; /// # fn main() { /// # use rs_utils::array::fst_initial; /// use generic_array::GenericArray; /// use typenum::consts::*; /// let xs : [(u8, f32); 2] = [(5, 1.0), (100, -1.0)]; /// let ys : GenericArray <u8, U3> = fst_initial (&xs.into()); /// # } /// ``` pub fn fst_initial <A, B, N, M> (from : &GenericArray <(A,B), N>) -> GenericArray <A, M> where A : Clone, N : generic_array::ArrayLength <(A,B)>, M : generic_array::ArrayLength <A> + typenum::IsLessOrEqual <N, Output=B1> { GenericArray::generate (|i| from[i].0.clone()) } /// Create a new array from the second elements of an array of pairs of the same /// length as the second array. /// /// ``` /// # extern crate generic_array; /// # extern crate typenum; /// # extern crate rs_utils; /// # fn main() { /// # use rs_utils::array::snd; /// use generic_array::GenericArray; /// use typenum::consts::*; /// let xs : [(u8, f32); 2] = [(5, 1.0), (100, -1.0)]; /// let ys : GenericArray <f32, U2> = snd (&xs.into()); /// assert_eq!(ys.as_slice(), [1.0, -1.0]); /// # } /// ``` /// /// A compilation error will result if the lengths of the arrays do not match: /// /// ```compile_fail /// # extern crate generic_array; /// # extern crate typenum; /// # extern crate rs_utils; /// # fn main() { /// # use rs_utils::array::snd; /// use generic_array::GenericArray; /// use typenum::consts::*; /// let xs : [(u8, f32); 2] = [(5, 1.0), (100, -1.0)]; /// let ys : GenericArray <f32, U1> = snd (&xs.into()); /// # } /// ``` pub fn snd <A, B, N> (from : &GenericArray <(A,B), N>) -> GenericArray <B, N> where B : Clone, N : generic_array::ArrayLength <(A,B)> + generic_array::ArrayLength <B>, { GenericArray::generate (|i| from[i].1.clone()) } /// Create a new array from the second elements of an array of pairs or the /// default if the destination array length is longer than the input array /// length. /// /// ``` /// # extern crate generic_array; /// # extern crate typenum; /// # extern crate rs_utils; /// # fn main() { /// # use rs_utils::array::snd_default; /// use generic_array::GenericArray; /// use typenum::consts::*; /// let xs : [(u8, f32); 2] = [(5, 1.0), (100, -1.0)]; /// let ys : GenericArray <f32, U3> = snd_default (&xs.into()); /// assert_eq!(ys.as_slice(), [1.0, -1.0, 0.0]); /// # } /// ``` pub fn snd_default <A, B, N, M> (from : &GenericArray <(A,B), N>) -> GenericArray <B, M> where B : Clone + Default, N : generic_array::ArrayLength <(A,B)>, M : generic_array::ArrayLength <B> { GenericArray::generate (|i| if i < N::to_usize() { from[i].1.clone() } else { B::default() }) } /// Create a new array from the second elements of an array of pairs of equal /// or greater length than the second. /// /// ``` /// # extern crate generic_array; /// # extern crate typenum; /// # extern crate rs_utils; /// # fn main() { /// # use rs_utils::array::snd_initial; /// use generic_array::GenericArray; /// use typenum::consts::*; /// let xs : [(u8, f32); 2] = [(5, 1.0), (100, -1.0)]; /// let ys : GenericArray <f32, U1> = snd_initial (&xs.into()); /// assert_eq!(ys.as_slice(), [1.0]); /// # } /// ``` /// /// A compilation error will result if the length of the input array is /// shorter than the length of the destination array: /// /// ```compile_fail /// # extern crate generic_array; /// # extern crate typenum; /// # extern crate rs_utils; /// # fn main() { /// # use rs_utils::array::snd_initial; /// use generic_array::GenericArray; /// use typenum::consts::*; /// let xs : [(u8, f32); 2] = [(5, 1.0), (100, -1.0)]; /// let ys : GenericArray <f32, U3> = snd_initial (&xs.into()); /// # } /// ``` pub fn snd_initial <A, B, N, M> (from : &GenericArray <(A,B), N>) -> GenericArray <B, M> where B : Clone, N : generic_array::ArrayLength <(A,B)>, M : generic_array::ArrayLength <B> + typenum::IsLessOrEqual <N, Output=B1> { GenericArray::generate (|i| from[i].1.clone()) }