ndwt 0.1.1

High-performance discrete and lifting wavelet transforms for 1-D and N-D signals, with SIMD acceleration, adjoint operations, and 8 boundary conditions.
Documentation
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
//! N-dimensional lane iteration over flat slices and ndarray arrays.
//!
//! A *lane* is a 1-D slice through an N-dimensional array along a single axis —
//! equivalent to a row, column, or fibre depending on which axis is chosen.
//! These iterators are used internally by the wavelet drivers to apply 1-D
//! transforms independently along each axis of a multi-dimensional array.

pub mod chunk_strided_slice;
pub mod strided_slice;
#[cfg(feature = "ndarray")]
use ndarray::{ArrayRef, Dimension};
use num_traits::Zero;
use std::marker::PhantomData;
use std::ops::ControlFlow;
use std::ptr::NonNull;

use crate::{ChunkWidth, utils::stride_from_shape};
use chunk_strided_slice::{IterLaneChunks, IterLaneChunksMut};
use strided_slice::{IterLanes, IterLanesMut};

pub use chunk_strided_slice::ChunkStridedSliceRef;
pub use strided_slice::StridedSliceRef;

#[inline]
#[track_caller]
pub(crate) fn unravel(flat_index: usize, shape: &[usize]) -> Vec<usize> {
    let n_max: usize = shape.iter().product();
    assert!(
        flat_index <= n_max,
        "Flat index is beyond the end of the array."
    );

    // a special case for flat_index == n_max to return an unraveled index that points
    // one past the last item.
    // i.e. it looks like (n0-1, n1 -1, n2)
    // so it would need to be pre retreated by one **before** it is valid.
    if flat_index == n_max {
        let mut inds = shape.iter().map(|n| n - 1).collect::<Vec<_>>();
        if let Some(last) = inds.last_mut()
            && let Some(n_last) = shape.last()
        {
            *last = *n_last;
        }
        return inds;
    }
    let mut inds = vec![0; shape.len()];
    let mut flat_index = flat_index;
    inds.iter_mut()
        .zip(shape.iter())
        .rev()
        .for_each(|(i_dir, n_dir)| {
            *i_dir = flat_index % n_dir;
            flat_index /= n_dir;
        });
    inds
}

/// Marker trait for readable strided data containers.
///
/// # Safety
/// Implementors must guarantee that the underlying memory is valid for reads for the
/// declared element type.
pub unsafe trait Data: Sized {
    /// The element type stored in the container.
    type Elem;
}

/// Marker trait for writable strided data containers.
///
/// # Safety
/// Implementors must guarantee that the underlying memory is valid for reads and writes
/// for the declared element type, and that no other reference aliases the same memory.
pub unsafe trait DataMut: Data {}

/// Phantom type used to attach lifetime information to strided slice views.
pub struct SliceLifetime<T> {
    _member: PhantomData<T>,
}

unsafe impl<T> Data for SliceLifetime<&T> {
    type Elem = T;
}

unsafe impl<T> Data for SliceLifetime<&mut T> {
    type Elem = T;
}

unsafe impl<T> DataMut for SliceLifetime<&mut T> {}

#[derive(Clone, Debug)]
pub(crate) struct ArrayInfo {
    shape: Vec<usize>,
    stride: Vec<isize>,
    lane_length: usize,
    lane_stride: isize,
}

impl ArrayInfo {
    #[track_caller]
    fn new(shape: &[usize], stride: &[isize], axis: usize) -> Self {
        assert!(
            axis < shape.len(),
            "Specified axis exceeds shape dimensions"
        );
        assert_eq!(
            stride.len(),
            shape.len(),
            "Shape and stride should have the same length."
        );
        let mut stride = stride.to_owned();
        let mut shape = shape.to_owned();

        let lane_length = shape.remove(axis);
        let lane_stride = stride.remove(axis);

        Self {
            shape,
            stride,
            lane_length,
            lane_stride,
        }
    }
    #[inline(always)]
    fn n_lanes(&self) -> usize {
        self.shape.iter().product()
    }

    #[inline(always)]
    fn get_position_at(&self, i: usize) -> Vec<usize> {
        unravel(i, &self.shape)
    }

    #[inline(always)]
    fn get_offset_at(&self, pos: &[usize]) -> isize {
        pos.iter()
            .zip(self.stride.iter())
            .fold(0, |acc, (i, step)| acc + *i as isize * step)
    }

    #[inline(always)]
    fn advance_position_and_offset(&self, pos: &mut [usize], offset: &mut isize) {
        let _ = self
            .stride
            .iter()
            .zip(self.shape.iter())
            .zip(pos)
            .rev()
            .try_for_each(|((str, shp), pos)| {
                *offset += *str;
                *pos += 1;
                if *pos < *shp {
                    return ControlFlow::Break(());
                };
                *pos = 0;
                *offset -= *shp as isize * str;
                ControlFlow::Continue(())
            });
    }

    #[inline(always)]
    fn retreat_position_and_offset(&self, pos: &mut [usize], offset: &mut isize) {
        let _ = self
            .stride
            .iter()
            .zip(self.shape.iter())
            .zip(pos)
            .rev()
            .try_for_each(|((str, shp), pos)| {
                if *pos == 0 {
                    *pos = *shp - 1;
                    *offset += *pos as isize * str;
                    ControlFlow::Continue(())
                } else {
                    *pos -= 1;
                    *offset -= *str;
                    ControlFlow::Break(())
                }
            });
    }
}

#[track_caller]
fn lane_parts_from_slice<T>(arr: &[T], shape: &[usize], axis: usize) -> (NonNull<T>, ArrayInfo) {
    lane_parts_from_sub_slice(arr, shape, shape, axis)
}

#[track_caller]
fn lane_parts_from_sub_slice<T>(
    arr: &[T],
    shape: &[usize],
    sub_shape: &[usize],
    axis: usize,
) -> (NonNull<T>, ArrayInfo) {
    let n = arr.len();
    assert!(
        !arr.is_empty(),
        "Attempted to create a lane iterator from an empty slice."
    );
    let n_items: usize = shape.iter().product();
    assert_eq!(
        n, n_items,
        "array length must be consistent with the shape. Shape suggests {n_items}, but slice had {n} items."
    );
    assert_eq!(
        shape.len(),
        sub_shape.len(),
        "shape length, {}, and sub_shape length, {}, must be equal",
        shape.len(),
        sub_shape.len()
    );
    assert!(
        sub_shape.iter().zip(shape.iter()).all(|(n1, n2)| n1 <= n2),
        "sub_shape: {:?}, must be equal to our smaller than shape, {:?}",
        sub_shape,
        shape,
    );
    assert!(
        axis < shape.len(),
        "axis: {axis} is out of bounds for dimension size of {}",
        shape.len()
    );

    let stride = stride_from_shape(shape)
        .into_iter()
        .map(|s| s as isize)
        .collect::<Vec<_>>();
    // SAFETY: slice length > 0 so ptr is NonNull.
    let ptr = unsafe { NonNull::new_unchecked(arr.as_ptr() as *mut T) };
    (ptr, ArrayInfo::new(sub_shape, &stride, axis))
}

#[cfg(feature = "ndarray")]
#[track_caller]
fn lane_parts_from_ndarray<T, D: Dimension>(
    arr: &ArrayRef<T, D>,
    sub_shape: &[usize],
    axis: usize,
) -> (NonNull<T>, ArrayInfo) {
    assert_ne!(
        arr.len(),
        0,
        "Cannot create a lane iterator from an empty ndarray."
    );
    let ndim = arr.ndim();
    assert!(
        axis < ndim,
        "axis: {axis} is out of bounds for dimension size of {ndim}",
    );
    assert_eq!(
        sub_shape.len(),
        arr.ndim(),
        "shape.len(), {}, is not equal to arr.ndim(), {ndim}",
        sub_shape.len(),
    );

    assert!(
        sub_shape.iter().zip(arr.shape()).all(|(n, m)| n <= m),
        "requested shape, {:?} must all be <= arr.shape(), {:?}.",
        sub_shape,
        arr.shape(),
    );

    // SAFETY: Array is not empty, so pointer to first element is gauranteed non-null.
    let ptr = unsafe { NonNull::new_unchecked(arr.as_ptr() as *mut T) };
    (ptr, ArrayInfo::new(sub_shape, arr.strides(), axis))
}

/// Iterate over 1-D lanes of an N-dimensional array along a chosen axis.
///
/// A *lane* is a 1-D slice along one axis while all other indices are held
/// fixed.  Implementations are provided for `[T]` (flat row-major slices)
/// and, when the `ndarray` feature is enabled, for `ndarray::ArrayRef<T, D>`.
pub trait LanesIterator {
    /// Element type stored in the array.
    type Item;

    /// Iterate over all lanes along `axis` of an array with the given `shape`.
    ///
    /// # Panics
    ///
    /// Panics if `axis >= shape.len()`, if the slice is empty, or if the slice length
    /// does not equal `shape.iter().product()`.
    fn iter_lanes<'a>(&'a self, shape: &[usize], axis: usize) -> IterLanes<'a, Self::Item>;

    /// Mutably iterate over all lanes along `axis`.
    ///
    /// # Panics
    ///
    /// Same conditions as [`iter_lanes`](Self::iter_lanes).
    fn iter_lanes_mut<'a>(
        &'a mut self,
        shape: &[usize],
        axis: usize,
    ) -> IterLanesMut<'a, Self::Item>;

    /// Iterate over fixed-width chunks of lanes along `axis`.
    ///
    /// Each item is a group of `N` consecutive elements within a lane.
    ///
    /// # Panics
    ///
    /// Same conditions as [`iter_lanes`](Self::iter_lanes).
    fn iter_lane_chunks<'a, const N: usize>(
        &'a self,
        shape: &[usize],
        axis: usize,
    ) -> (IterLaneChunks<'a, Self::Item, N>, IterLanes<'a, Self::Item>);

    /// Mutably iterate over fixed-width chunks of lanes along `axis`.
    ///
    /// # Panics
    ///
    /// Same conditions as [`iter_lanes`](Self::iter_lanes).
    fn iter_lane_chunks_mut<'a, const N: usize>(
        &'a mut self,
        shape: &[usize],
        axis: usize,
    ) -> (
        IterLaneChunksMut<'a, Self::Item, N>,
        IterLanesMut<'a, Self::Item>,
    );

    /// Iterate over lanes of a sub-region defined by `sub_shape`.
    ///
    /// Lanes are taken from the first `sub_shape[axis]` elements along `axis`,
    /// with the outer shape given by `shape`.
    ///
    /// # Panics
    ///
    /// Panics if `axis >= shape.len()`, if `shape.len() != sub_shape.len()`, if any
    /// `sub_shape[i] > shape[i]`, if the slice is empty, or if the slice length does
    /// not equal `shape.iter().product()`.
    fn iter_lanes_sub<'a>(
        &'a self,
        shape: &[usize],
        sub_shape: &[usize],
        axis: usize,
    ) -> IterLanes<'a, Self::Item>;

    /// Mutably iterate over lanes of a sub-region.
    ///
    /// # Panics
    ///
    /// Same conditions as [`iter_lanes_sub`](Self::iter_lanes_sub).
    fn iter_lanes_sub_mut<'a>(
        &'a mut self,
        shape: &[usize],
        sub_shape: &[usize],
        axis: usize,
    ) -> IterLanesMut<'a, Self::Item>;

    /// Iterate over fixed-width chunks of lanes within a sub-region.
    ///
    /// # Panics
    ///
    /// Same conditions as [`iter_lanes_sub`](Self::iter_lanes_sub).
    fn iter_lane_chunks_sub<'a, const N: usize>(
        &'a self,
        shape: &[usize],
        sub_shape: &[usize],
        axis: usize,
    ) -> (IterLaneChunks<'a, Self::Item, N>, IterLanes<'a, Self::Item>);

    /// Mutably iterate over fixed-width chunks of lanes within a sub-region.
    ///
    /// # Panics
    ///
    /// Same conditions as [`iter_lanes_sub`](Self::iter_lanes_sub).
    fn iter_lane_chunks_sub_mut<'a, const N: usize>(
        &'a mut self,
        shape: &[usize],
        sub_shape: &[usize],
        axis: usize,
    ) -> (
        IterLaneChunksMut<'a, Self::Item, N>,
        IterLanesMut<'a, Self::Item>,
    );

    /// Return the axis index with the smallest stride (most cache-friendly to
    /// iterate over for the given `shape`).
    fn min_stride_axis(&self, shape: &[usize]) -> usize;

    /// Return whether a lane along the request axis will be contiguous.
    fn is_ax_contiguous(&self, ax: usize, shape: &[usize]) -> bool;
}

impl<T> LanesIterator for [T] {
    type Item = T;
    #[track_caller]
    fn iter_lanes<'a>(&'a self, shape: &[usize], axis: usize) -> IterLanes<'a, Self::Item> {
        IterLanes::from_slice(self, shape, axis)
    }
    #[track_caller]
    fn iter_lanes_mut<'a>(
        &'a mut self,
        shape: &[usize],
        axis: usize,
    ) -> IterLanesMut<'a, Self::Item> {
        IterLanesMut::from_slice(self, shape, axis)
    }

    #[track_caller]
    fn iter_lane_chunks<'a, const N: usize>(
        &'a self,
        shape: &[usize],
        axis: usize,
    ) -> (IterLaneChunks<'a, Self::Item, N>, IterLanes<'a, Self::Item>) {
        IterLaneChunks::from_slice(self, shape, axis)
    }

    #[track_caller]
    fn iter_lane_chunks_mut<'a, const N: usize>(
        &'a mut self,
        shape: &[usize],
        axis: usize,
    ) -> (
        IterLaneChunksMut<'a, Self::Item, N>,
        IterLanesMut<'a, Self::Item>,
    ) {
        IterLaneChunksMut::from_slice(self, shape, axis)
    }

    #[track_caller]
    fn iter_lanes_sub<'a>(
        &'a self,
        shape: &[usize],
        sub_shape: &[usize],
        axis: usize,
    ) -> IterLanes<'a, Self::Item> {
        IterLanes::from_sub_slice(self, shape, sub_shape, axis)
    }
    #[track_caller]
    fn iter_lanes_sub_mut<'a>(
        &'a mut self,
        shape: &[usize],
        sub_shape: &[usize],
        axis: usize,
    ) -> IterLanesMut<'a, Self::Item> {
        IterLanesMut::from_sub_slice(self, shape, sub_shape, axis)
    }

    #[track_caller]
    fn iter_lane_chunks_sub<'a, const N: usize>(
        &'a self,
        shape: &[usize],
        sub_shape: &[usize],
        axis: usize,
    ) -> (IterLaneChunks<'a, Self::Item, N>, IterLanes<'a, Self::Item>) {
        IterLaneChunks::from_sub_slice(self, shape, sub_shape, axis)
    }

    #[track_caller]
    fn iter_lane_chunks_sub_mut<'a, const N: usize>(
        &'a mut self,
        shape: &[usize],
        sub_shape: &[usize],
        axis: usize,
    ) -> (
        IterLaneChunksMut<'a, Self::Item, N>,
        IterLanesMut<'a, Self::Item>,
    ) {
        IterLaneChunksMut::from_sub_slice(self, shape, sub_shape, axis)
    }

    fn min_stride_axis(&self, shape: &[usize]) -> usize {
        if !shape.is_empty() {
            shape.len() - 1
        } else {
            0
        }
    }

    #[inline]
    fn is_ax_contiguous(&self, ax: usize, shape: &[usize]) -> bool {
        ax + 1 == shape.len()
    }
}

#[cfg(feature = "ndarray")]
impl<T, D: ::ndarray::Dimension> LanesIterator for ArrayRef<T, D> {
    type Item = T;
    #[track_caller]
    fn iter_lanes<'a>(&'a self, shape: &[usize], axis: usize) -> IterLanes<'a, Self::Item> {
        IterLanes::from_ndarray(self, shape, axis)
    }
    #[track_caller]
    fn iter_lanes_mut<'a>(
        &'a mut self,
        shape: &[usize],
        axis: usize,
    ) -> IterLanesMut<'a, Self::Item> {
        IterLanesMut::from_ndarray(self, shape, axis)
    }

    #[track_caller]
    fn iter_lane_chunks<'a, const N: usize>(
        &'a self,
        shape: &[usize],
        axis: usize,
    ) -> (IterLaneChunks<'a, Self::Item, N>, IterLanes<'a, Self::Item>) {
        IterLaneChunks::from_ndarray(self, shape, axis)
    }

    #[track_caller]
    fn iter_lane_chunks_mut<'a, const N: usize>(
        &'a mut self,
        shape: &[usize],
        axis: usize,
    ) -> (
        IterLaneChunksMut<'a, Self::Item, N>,
        IterLanesMut<'a, Self::Item>,
    ) {
        IterLaneChunksMut::from_ndarray(self, shape, axis)
    }

    #[track_caller]
    fn iter_lanes_sub<'a>(
        &'a self,
        _shape: &[usize],
        sub_shape: &[usize],
        axis: usize,
    ) -> IterLanes<'a, Self::Item> {
        IterLanes::from_ndarray(self, sub_shape, axis)
    }

    #[track_caller]
    fn iter_lanes_sub_mut<'a>(
        &'a mut self,
        _shape: &[usize],
        sub_shape: &[usize],
        axis: usize,
    ) -> IterLanesMut<'a, Self::Item> {
        IterLanesMut::from_ndarray(self, sub_shape, axis)
    }

    #[track_caller]
    fn iter_lane_chunks_sub<'a, const N: usize>(
        &'a self,
        _shape: &[usize],
        sub_shape: &[usize],
        axis: usize,
    ) -> (IterLaneChunks<'a, Self::Item, N>, IterLanes<'a, Self::Item>) {
        IterLaneChunks::from_ndarray(self, sub_shape, axis)
    }

    #[track_caller]
    fn iter_lane_chunks_sub_mut<'a, const N: usize>(
        &'a mut self,
        _shape: &[usize],
        sub_shape: &[usize],
        axis: usize,
    ) -> (
        IterLaneChunksMut<'a, Self::Item, N>,
        IterLanesMut<'a, Self::Item>,
    ) {
        IterLaneChunksMut::from_ndarray(self, sub_shape, axis)
    }

    fn min_stride_axis(&self, _shape: &[usize]) -> usize {
        // copy input into the output
        let (min_axis, _) = self
            .strides()
            .iter()
            .cloned()
            .enumerate()
            .reduce(|acc, v| if v.1.abs() < acc.1.abs() { v } else { acc })
            .unwrap_or((0, 0));

        min_axis
    }

    #[inline]
    fn is_ax_contiguous(&self, ax: usize, _shape: &[usize]) -> bool {
        self.strides().get(ax).map(|v| *v == 1).unwrap_or(false)
    }
}

pub(crate) fn copy_over<T, L, const N: usize>(
    input: &L,
    output: &mut L,
    in_shape: &[usize],
    out_shape: &[usize],
) where
    L: LanesIterator<Item = T> + ?Sized,
    T: Clone + Zero + ChunkWidth<T, N>,
{
    // copy input into output
    let min_axis = output.min_stride_axis(out_shape);
    let (in_lanes, out_lanes) = if input.is_ax_contiguous(min_axis, in_shape)
        || output.is_ax_contiguous(min_axis, out_shape)
    {
        (
            input.iter_lanes_sub(in_shape, out_shape, min_axis),
            output.iter_lanes_mut(out_shape, min_axis),
        )
    } else {
        let (in_chunks, in_rem) = input.iter_lane_chunks_sub::<N>(in_shape, out_shape, min_axis);
        let (out_chunks, out_rem) = output.iter_lane_chunks_mut::<N>(out_shape, min_axis);

        out_chunks.zip(in_chunks).for_each(|(mut o, i)| {
            o.iter_mut().zip(i.iter()).for_each(|(o, i)| {
                o.into_iter()
                    .zip(i.into_iter().cloned())
                    .for_each(|(o, i)| {
                        *o = i;
                    });
            });
        });
        (in_rem, out_rem)
    };
    out_lanes.zip(in_lanes).for_each(|(mut o, i)| {
        o.iter_mut()
            .zip(i.iter().cloned())
            .for_each(|(o, i)| *o = i);
    });
}

#[cfg(feature = "rayon")]
/// Parallel lane iteration using Rayon.
///
/// Provides the same interface as [`LanesIterator`] but returns Rayon parallel
/// iterators so that the caller can process independent lanes on multiple threads.
pub mod parallel {
    use super::chunk_strided_slice::parallel::{ParIterLaneChunks, ParIterLaneChunksMut};
    use super::strided_slice::parallel::{ParIterLanes, ParIterLanesMut};
    use super::*;

    /// Iterate over 1-D lanes of an N-dimensional array along a chosen axis in parallel.
    ///
    /// A *lane* is a 1-D slice along one axis while all other indices are held
    /// fixed.  Implementations are provided for `[T]` (flat row-major slices)
    /// and, when the `ndarray` feature is enabled, for `ndarray::ArrayRef<T, D>`.
    pub trait LanesParallelIterator: LanesIterator {
        /// Iterate over all lanes along `axis` of an array with the given `shape`.
        ///
        /// # Panics
        ///
        /// Panics if `axis >= shape.len()`, if the slice is empty, or if the slice length
        /// does not equal `shape.iter().product()`.
        fn par_iter_lanes<'a>(
            &'a self,
            shape: &[usize],
            axis: usize,
        ) -> ParIterLanes<'a, Self::Item>;

        /// Mutably iterate over all lanes along `axis`.
        ///
        /// # Panics
        ///
        /// Same conditions as [`par_iter_lanes`](Self::par_iter_lanes).
        fn par_iter_lanes_mut<'a>(
            &'a mut self,
            shape: &[usize],
            axis: usize,
        ) -> ParIterLanesMut<'a, Self::Item>;

        /// Iterate over SIMD-width chunks of lanes along `axis`.
        ///
        /// Each item is a group of `N` consecutive elements within a lane.
        ///
        /// # Panics
        ///
        /// Same conditions as [`par_iter_lanes`](Self::par_iter_lanes).
        fn par_iter_lane_chunks<'a, const N: usize>(
            &'a self,
            shape: &[usize],
            axis: usize,
        ) -> (
            ParIterLaneChunks<'a, Self::Item, N>,
            ParIterLanes<'a, Self::Item>,
        );

        /// Mutably iterate over SIMD-width chunks of lanes along `axis`.
        ///
        /// # Panics
        ///
        /// Same conditions as [`par_iter_lanes`](Self::par_iter_lanes).
        fn par_iter_lane_chunks_mut<'a, const N: usize>(
            &'a mut self,
            shape: &[usize],
            axis: usize,
        ) -> (
            ParIterLaneChunksMut<'a, Self::Item, N>,
            ParIterLanesMut<'a, Self::Item>,
        );

        /// Iterate over lanes of a sub-region defined by `sub_shape`.
        ///
        /// Lanes are taken from the first `sub_shape[axis]` elements along `axis`,
        /// with the outer shape given by `shape`.
        ///
        /// # Panics
        ///
        /// Panics if `axis >= shape.len()`, if `shape.len() != sub_shape.len()`, if any
        /// `sub_shape[i] > shape[i]`, if the slice is empty, or if the slice length does
        /// not equal `shape.iter().product()`.
        fn par_iter_lanes_sub<'a>(
            &'a self,
            shape: &[usize],
            sub_shape: &[usize],
            axis: usize,
        ) -> ParIterLanes<'a, Self::Item>;

        /// Mutably iterate over lanes of a sub-region.
        ///
        /// # Panics
        ///
        /// Same conditions as [`par_iter_lanes_sub`](Self::par_iter_lanes_sub).
        fn par_iter_lanes_sub_mut<'a>(
            &'a mut self,
            shape: &[usize],
            sub_shape: &[usize],
            axis: usize,
        ) -> ParIterLanesMut<'a, Self::Item>;

        /// Iterate over SIMD-width chunks of lanes within a sub-region.
        ///
        /// # Panics
        ///
        /// Same conditions as [`par_iter_lanes_sub`](Self::par_iter_lanes_sub).
        fn par_iter_lane_chunks_sub<'a, const N: usize>(
            &'a self,
            shape: &[usize],
            sub_shape: &[usize],
            axis: usize,
        ) -> (
            ParIterLaneChunks<'a, Self::Item, N>,
            ParIterLanes<'a, Self::Item>,
        );

        /// Mutably iterate over SIMD-width chunks of lanes within a sub-region.
        ///
        /// # Panics
        ///
        /// Same conditions as [`par_iter_lanes_sub`](Self::par_iter_lanes_sub).
        fn par_iter_lane_chunks_sub_mut<'a, const N: usize>(
            &'a mut self,
            shape: &[usize],
            sub_shape: &[usize],
            axis: usize,
        ) -> (
            ParIterLaneChunksMut<'a, Self::Item, N>,
            ParIterLanesMut<'a, Self::Item>,
        );
    }

    impl<T> LanesParallelIterator for [T] {
        #[track_caller]
        fn par_iter_lanes<'a>(
            &'a self,
            shape: &[usize],
            axis: usize,
        ) -> ParIterLanes<'a, Self::Item> {
            ParIterLanes::from_slice(self, shape, axis)
        }
        #[track_caller]
        fn par_iter_lanes_mut<'a>(
            &'a mut self,
            shape: &[usize],
            axis: usize,
        ) -> ParIterLanesMut<'a, Self::Item> {
            ParIterLanesMut::from_slice(self, shape, axis)
        }

        #[track_caller]
        fn par_iter_lane_chunks<'a, const N: usize>(
            &'a self,
            shape: &[usize],
            axis: usize,
        ) -> (
            ParIterLaneChunks<'a, Self::Item, N>,
            ParIterLanes<'a, Self::Item>,
        ) {
            ParIterLaneChunks::from_slice(self, shape, axis)
        }

        #[track_caller]
        fn par_iter_lane_chunks_mut<'a, const N: usize>(
            &'a mut self,
            shape: &[usize],
            axis: usize,
        ) -> (
            ParIterLaneChunksMut<'a, Self::Item, N>,
            ParIterLanesMut<'a, Self::Item>,
        ) {
            ParIterLaneChunksMut::from_slice(self, shape, axis)
        }

        #[track_caller]
        fn par_iter_lanes_sub<'a>(
            &'a self,
            shape: &[usize],
            sub_shape: &[usize],
            axis: usize,
        ) -> ParIterLanes<'a, Self::Item> {
            ParIterLanes::from_sub_slice(self, shape, sub_shape, axis)
        }
        #[track_caller]
        fn par_iter_lanes_sub_mut<'a>(
            &'a mut self,
            shape: &[usize],
            sub_shape: &[usize],
            axis: usize,
        ) -> ParIterLanesMut<'a, Self::Item> {
            ParIterLanesMut::from_sub_slice(self, shape, sub_shape, axis)
        }

        #[track_caller]
        fn par_iter_lane_chunks_sub<'a, const N: usize>(
            &'a self,
            shape: &[usize],
            sub_shape: &[usize],
            axis: usize,
        ) -> (
            ParIterLaneChunks<'a, Self::Item, N>,
            ParIterLanes<'a, Self::Item>,
        ) {
            ParIterLaneChunks::from_sub_slice(self, shape, sub_shape, axis)
        }

        #[track_caller]
        fn par_iter_lane_chunks_sub_mut<'a, const N: usize>(
            &'a mut self,
            shape: &[usize],
            sub_shape: &[usize],
            axis: usize,
        ) -> (
            ParIterLaneChunksMut<'a, Self::Item, N>,
            ParIterLanesMut<'a, Self::Item>,
        ) {
            ParIterLaneChunksMut::from_sub_slice(self, shape, sub_shape, axis)
        }
    }

    #[cfg(feature = "ndarray")]
    impl<T, D: ::ndarray::Dimension> LanesParallelIterator for ArrayRef<T, D> {
        #[track_caller]
        fn par_iter_lanes<'a>(
            &'a self,
            shape: &[usize],
            axis: usize,
        ) -> ParIterLanes<'a, Self::Item> {
            ParIterLanes::from_ndarray(self, shape, axis)
        }
        #[track_caller]
        fn par_iter_lanes_mut<'a>(
            &'a mut self,
            shape: &[usize],
            axis: usize,
        ) -> ParIterLanesMut<'a, Self::Item> {
            ParIterLanesMut::from_ndarray(self, shape, axis)
        }

        #[track_caller]
        fn par_iter_lane_chunks<'a, const N: usize>(
            &'a self,
            shape: &[usize],
            axis: usize,
        ) -> (
            ParIterLaneChunks<'a, Self::Item, N>,
            ParIterLanes<'a, Self::Item>,
        ) {
            ParIterLaneChunks::from_ndarray(self, shape, axis)
        }

        #[track_caller]
        fn par_iter_lane_chunks_mut<'a, const N: usize>(
            &'a mut self,
            shape: &[usize],
            axis: usize,
        ) -> (
            ParIterLaneChunksMut<'a, Self::Item, N>,
            ParIterLanesMut<'a, Self::Item>,
        ) {
            ParIterLaneChunksMut::from_ndarray(self, shape, axis)
        }

        #[track_caller]
        fn par_iter_lanes_sub<'a>(
            &'a self,
            _shape: &[usize],
            sub_shape: &[usize],
            axis: usize,
        ) -> ParIterLanes<'a, Self::Item> {
            ParIterLanes::from_ndarray(self, sub_shape, axis)
        }
        #[track_caller]
        fn par_iter_lanes_sub_mut<'a>(
            &'a mut self,
            _shape: &[usize],
            sub_shape: &[usize],
            axis: usize,
        ) -> ParIterLanesMut<'a, Self::Item> {
            ParIterLanesMut::from_ndarray(self, sub_shape, axis)
        }

        #[track_caller]
        fn par_iter_lane_chunks_sub<'a, const N: usize>(
            &'a self,
            _shape: &[usize],
            sub_shape: &[usize],
            axis: usize,
        ) -> (
            ParIterLaneChunks<'a, Self::Item, N>,
            ParIterLanes<'a, Self::Item>,
        ) {
            ParIterLaneChunks::from_ndarray(self, sub_shape, axis)
        }

        #[track_caller]
        fn par_iter_lane_chunks_sub_mut<'a, const N: usize>(
            &'a mut self,
            _shape: &[usize],
            sub_shape: &[usize],
            axis: usize,
        ) -> (
            ParIterLaneChunksMut<'a, Self::Item, N>,
            ParIterLanesMut<'a, Self::Item>,
        ) {
            ParIterLaneChunksMut::from_ndarray(self, sub_shape, axis)
        }
    }

    pub(crate) fn copy_over<T, L, const N: usize>(
        input: &L,
        output: &mut L,
        in_shape: &[usize],
        out_shape: &[usize],
    ) where
        L: LanesParallelIterator<Item = T> + ?Sized,
        T: Clone + Zero + ChunkWidth<T, N> + Send + Sync,
    {
        use rayon::iter::{IndexedParallelIterator, ParallelIterator};
        // copy input into output
        let min_axis = output.min_stride_axis(out_shape);
        let (in_chunks, in_rem) =
            input.par_iter_lane_chunks_sub::<N>(in_shape, out_shape, min_axis);
        let (out_chunks, out_rem) = output.par_iter_lane_chunks_mut::<N>(out_shape, min_axis);

        out_chunks.zip(in_chunks).for_each(|(mut o, i)| {
            o.iter_mut().zip(i.iter()).for_each(|(o, i)| {
                o.into_iter()
                    .zip(i.into_iter().cloned())
                    .for_each(|(o, i)| {
                        *o = i;
                    });
            });
        });
        out_rem.zip(in_rem).for_each(|(mut o, i)| {
            o.iter_mut()
                .zip(i.iter().cloned())
                .for_each(|(o, i)| *o = i);
        });
    }
}