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
//! Strided slices. //! //! This library provides two types `Stride` and `MutStride` as //! generalised forms of `&[T]` and `&mut [T]` respectively, where the //! elements are regularly spaced in memory, but not necessarily //! immediately adjacently. //! //! For example, given an underlying array `[1, 2, 3, 4, 5]`, the //! elements `[1, 3, 5]` are a strided slice with stride 2, and //! `[1, 4]` has stride 3. Any slice can be regarded as a strided slice //! with stride 1. //! //! This provides functionality through which one can safely and //! efficiently manipulate every `n`th element of a slice (even a //! mutable one) as close as possible to it being a conventional //! slice. This releases one from worries about stride bookkeeping, //! aliasing of `&mut` or any `unsafe` code. //! //! # Quick start //! //! The work-horse functions are `.substrides(n)` and //! `.substrides_mut(n)`, which return an iterator across a series of //! `n` new strided slices (shared and mutable, respectively), each of //! which points to every `n`th element, and each of which starts at //! the next successive offset. For example, the following has //! `n = 3`. //! //! ```rust //! use strided::MutStride; //! //! let mut v = [1u8, 2, 3, 4, 5]; //! let mut all = MutStride::new(&mut v); //! //! let mut substrides = all.substrides_mut(3); //! //! let a = substrides.next().unwrap(); //! let b = substrides.next().unwrap(); //! let c = substrides.next().unwrap(); //! assert!(substrides.next().is_none()); // there was exactly 3. //! //! assert_eq!(a, MutStride::new(&mut [1, 4])); //! assert_eq!(b, MutStride::new(&mut [2, 5])); //! assert_eq!(c, MutStride::new(&mut [3])); //! ``` //! //! The common case of `n = 2` has an abbreviation `substrides2` //! (resp. `substrides2_mut`), which takes the liberty of returns a //! tuple rather than an iterator to make direct destructuring //! work. Continuing with the values above, `left` and `right` point //! to alternate elements, starting at index `0` and `1` of their //! parent slice respectively. //! //! ```rust //! # use strided::MutStride; //! # let mut v = [1u8, 2, 3, 4, 5]; //! # let mut all = MutStride::new(&mut v); //! let (left, right) = all.substrides2_mut(); //! //! assert_eq!(left, MutStride::new(&mut [1, 3, 5])); //! assert_eq!(right, MutStride::new(&mut [2, 4])); //! ``` //! //! A lot of the conventional slice functionality is available, such //! as indexing, iterators and slicing. //! //! ```rust //! # use strided::MutStride; //! # let mut v = [1u8, 2, 3, 4, 5]; //! # let mut all = MutStride::new(&mut v); //! let (mut left, right) = all.substrides2_mut(); //! assert_eq!(left[2], 5); //! assert!(right.get(10).is_none()); // out of bounds //! //! left[2] += 10; //! match left.get_mut(0) { //! Some(val) => *val -= 1, //! None => {} //! } //! //! assert_eq!(right.iter().fold(0, |sum, a| sum + *a), 2 + 4); //! for val in left.iter_mut() { //! *val /= 2 //! } //! ``` //! //! ## Ownership and `reborrow` //! //! `MutStride` has a method `reborrow` which has signature //! //! ```rust,ignore //! impl<'a, T> MutStride<'a, T> { //! pub fn reborrow<'b>(&'b mut self) -> MutStride<'b, T> { ... } //! } //! ``` //! //! That is, it allows temporarily viewing a strided slices as one //! with a shorter lifetime. This method is key because many of the //! methods on `MutStride` take `self` by-value and so consume //! ownership... which is rather unfortunate if one wants to use a //! strided slice multiple times. //! //! The temporary returned by `reborrow` can be used with the //! consuming methods, which allows the parent slice to continuing //! being used after that temporary has disappeared. For example, all //! of the splitting and slicing methods on `MutStride` consume //! ownership, and so `reborrow` is necessary there to continue using, //! in this case, `left`. //! //! ```rust //! # use strided::MutStride; //! # let mut v = [1u8, 2, 3, 4, 5]; //! # let mut all = MutStride::new(&mut v); //! let (mut left, right) = all.substrides2_mut(); //! assert_eq!(left.reborrow().slice_mut(1, 3), MutStride::new(&mut [3, 5])); //! assert_eq!(left.reborrow().slice_from_mut(2), MutStride::new(&mut [5])); //! assert_eq!(left.reborrow().slice_to_mut(2), MutStride::new(&mut [1, 3])); //! //! // no reborrow: //! assert_eq!(right.split_at_mut(1), //! (MutStride::new(&mut [2]), MutStride::new(&mut [4]))); //! // println!("{}", right); // error: use of moved value `right`. //! ``` //! //! These contortions are necessary to ensure that `&mut`s cannot //! alias, while still maintaining flexibility: leaving elements with //! the maximum possible lifetime (i.e. that of the non-strided slices //! which they lie in). Theoretically they are necessary with //! `&mut []` too, but the compiler inserts implicit reborrows and so //! one rarely needs to do them manually. //! //! In practice, one should only need to insert `reborrow`s if the //! compiler complains about the use of a moved value. //! //! The shared `Stride` is equivalent to `&[]` and only handles `&` //! references, making ownership transfer and `reborrow` unnecessary, //! so all its methods act identically to those on `&[]`. //! //! # Example //! //! The [fast Fourier transform //! (FFT)](https://en.wikipedia.org/wiki/Fast_Fourier_transform) is a //! signal processing algorithm that performs a discrete Fourier //! transform (DFT) of length `n` in `O(n log n)` time. A DFT breaks a //! waveform into the sum of sines and cosines, and is an important //! part of many other algorithms due to certain nice properties of //! the Fourier transform. //! //! The first FFT algorithm was the [Cooley-Tukey //! algorithm](https://en.wikipedia.org/wiki/Cooley-Tukey_FFT_algorithm). The //! decimation-in-time variant works by computing the FFT of //! equal-length subarrays of equally spaced elements and then //! combining these together into the desired result. This sort of //! spacing is exactly the striding provided by this library, and //! hence this library can be used to create an FFT algorithm in a //! very natural way. //! //! Below is an implementation of the radix-2 case, that is, when the //! length `n` is a power of two. In this case, only two strided //! subarrays are necessary: exactly the alternating ones provided by //! `substrides2`. Note the use of `reborrow` to allow `start` and //! `end` to be used for the recursive `fft` calls and then again //! later in the loop. //! //! ```rust //! # #![allow(unstable)] //! extern crate strided; //! extern crate num; // https://github.com/rust-lang/num //! use std::f64; //! use num::complex::{Complex, Complex64}; //! use strided::{MutStride, Stride}; //! //! /// Writes the forward DFT of `input` to `output`. //! fn fft(input: Stride<Complex64>, mut output: MutStride<Complex64>) { //! // check it's a power of two. //! assert!(input.len() == output.len() && input.len().count_ones() == 1); //! //! // base case: the DFT of a single element is itself. //! if input.len() == 1 { //! output[0] = input[0]; //! return //! } //! //! // split the input into two arrays of alternating elements ("decimate in time") //! let (evens, odds) = input.substrides2(); //! // break the output into two halves (front and back, not alternating) //! let (mut start, mut end) = output.split_at_mut(input.len() / 2); //! //! // recursively perform two FFTs on alternating elements of the input, writing the //! // results into the first and second half of the output array respectively. //! fft(evens, start.reborrow()); //! fft(odds, end.reborrow()); //! //! // exp(-2πi/N) //! let twiddle = Complex::from_polar(&1.0, &(-2.0 * f64::consts::PI / input.len() as f64)); //! //! let mut factor = Complex::new(1., 0.); //! //! // combine the subFFTs with the relations: //! // X_k = E_k + exp(-2πki/N) * O_k //! // X_{k+N/2} = E_k - exp(-2πki/N) * O_k //! for (even, odd) in start.iter_mut().zip(end.iter_mut()) { //! let twiddled = factor * *odd; //! let e = *even; //! //! *even = e + twiddled; //! *odd = e - twiddled; //! factor = factor * twiddle; //! } //! } //! //! fn main() { //! let a = [Complex::new(2., 0.), Complex::new(1., 0.), //! Complex::new(2., 0.), Complex::new(1., 0.)]; //! let mut b = [Complex::new(0., 0.); 4]; //! //! fft(Stride::new(&a), MutStride::new(&mut b)); //! println!("forward: {:?} -> {:?}", &a, &b); //! } //! ``` //! //! The above definitely has complexity `O(n log n)`, but it has a //! much larger constant factor than an optimised library like //! [FFTW](http://www.fftw.org/). (Strictly speaking `output` does not //! need to be a strided slice, since it is never split into //! alternating elements.) //#![feature(core)] #![cfg_attr(all(test, feature = "unstable"), feature(test))] #[cfg(all(test, feature = "unstable"))] extern crate test; pub use base::{Items, MutItems}; pub use mut_::Stride as MutStride; pub use mut_::Substrides as MutSubstrides; pub use imm::Stride as Stride; pub use imm::Substrides as Substrides; pub use traits::{Strided, MutStrided}; #[cfg(test)] mod common_tests; mod base; mod mut_; mod imm; mod traits; #[cfg(all(test, feature = "unstable"))] mod bench { use super::Stride; use test::Bencher as B; use test; const N: usize = 100; #[bench] fn iter_slice(b: &mut B) { let v = (0..N).collect::<Vec<_>>(); b.iter(|| { test::black_box(&v); for e in v.iter() { test::black_box(e); } }) } #[bench] fn iter_step_1(b: &mut B) { let v = (0..N).collect::<Vec<_>>(); let s = Stride::new(&*v); b.iter(|| { test::black_box(&s); for e in s.iter() { test::black_box(e); } }) } #[bench] fn iter_step_13(b: &mut B) { let v = (0..13 * N).collect::<Vec<_>>(); let s = Stride::new(&*v); let s = s.substrides(13).next().unwrap(); b.iter(|| { test::black_box(&s); for e in s.iter() { test::black_box(e); } }) } }