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
use core::ops::{Index, IndexMut};
use core::cmp::Ordering;
use core::borrow::Borrow;
use core::ptr;

use crate::iter::*;
use crate::view::*;
use crate::flattenexact::*;

/// A (col, row) coordinate in 2D space.
pub type Coordinate = (usize, usize);

/// An iterator over each "cell" in a 2D array
pub type Cells<'a, T> = FlattenExact<Rows<'a, T>>;
/// A mutable iterator over each "cell" in a 2D array
pub type CellsMut<'a, T> = FlattenExact<RowsMut<'a, T>>;

/// Defines operations common to both `TooDee` and `TooDeeView`. Default implementations are provided
/// where possible/practical.
pub trait TooDeeOps<T> : Index<usize, Output=[T]> + Index<Coordinate, Output=T> {
    
    /// The number of columns in the area represented by this object.
    fn num_cols(&self) -> usize;
    /// The number of rows in the area represented by this object.
    fn num_rows(&self) -> usize;
    
    /// Returns the size/dimensions of the current object.
    fn size(&self) -> (usize, usize) {
        (self.num_cols(), self.num_rows())
    }

    /// Returns `true` if the array contains no elements.
    fn is_empty(&self) -> bool {
        self.num_cols() == 0 || self.num_rows() == 0
    }

    /// Returns the bounds of the object's area within the original `TooDee` area (views
    /// are not nested for now).
    fn bounds(&self) -> (Coordinate, Coordinate);
    
    /// Returns a view (or subset) of the current area based on the coordinates provided.
    fn view(&self, start: Coordinate, end: Coordinate) -> TooDeeView<'_, T>;
    
    /// Returns an iterator of slices, where each slice represents the entire row of the object's area.
    fn rows(&self) -> Rows<'_, T>;
    
    /// Returns an iterator over a single column
    fn col(&self, col: usize) -> Col<'_, T>;

    /// Returns an iterator that traverses all cells within the area.
    fn cells(&self) -> Cells<'_, T> {
        FlattenExact::new(self.rows())
    }

}

/// Defines operations common to both `TooDee` and `TooDeeViewMut`. Default implementations
/// are provided where possible/practical.
pub trait TooDeeOpsMut<T> : TooDeeOps<T> + IndexMut<usize,Output=[T]>  + IndexMut<Coordinate, Output=T> {

    /// Returns a mutable view (or subset) of the current area based on the coordinates provided.
    fn view_mut(&mut self, start: Coordinate, end: Coordinate) -> TooDeeViewMut<'_, T>;
    
    /// Returns a mutable iterator of slices, where each slice represents the entire row of the object's area.
    fn rows_mut(&mut self) -> RowsMut<'_, T>;
    
    /// Returns a mutable iterator over a single column
    fn col_mut(&mut self, col: usize) -> ColMut<'_, T>;
    
    /// Returns an iterator that traverses all cells within the area.
    fn cells_mut(&mut self) -> CellsMut<'_, T> {
        FlattenExact::new(self.rows_mut())
    }
    
    /// Fills the entire area with the specified value.
    fn fill<V>(&mut self, fill: V)
    where
        V: Borrow<T>,
        T: Clone {
        let value = fill.borrow();
        for r in self.rows_mut() {
            for v in r {
                v.clone_from(value);
            }
        }
    }
    
    /// Swap/exchange the data between two columns.
    fn swap_cols(&mut self, c1: usize, c2: usize) {
        let num_cols = self.num_cols();
        assert!(c1 < num_cols);
        assert!(c2 < num_cols);
        for r in self.rows_mut() {
            // The column indices have been checked with asserts (see above), so we can
            // safely access and swap the elements using `get_unchecked_mut`.
            unsafe {
                let pa: *mut T = r.get_unchecked_mut(c1);
                let pb: *mut T = r.get_unchecked_mut(c2);
                ptr::swap(pa, pb);
            }
        }
    }
    
    /// Swap/exchange the data between two rows.
    /// 
    /// # Panics
    /// 
    /// Panics if either row index is out of bounds.
    fn swap_rows(&mut self, mut r1: usize, mut r2: usize) {
        match r1.cmp(&r2) {
            Ordering::Less => {},
            Ordering::Greater => {
                core::mem::swap(&mut r1, &mut r2);
            },
            Ordering::Equal => {
                return;
            }
        }
        assert!(r2 < self.num_rows());
        let mut iter = self.rows_mut();
        let tmp = iter.nth(r1).unwrap();
        tmp.swap_with_slice(iter.nth(r2-r1-1).unwrap());
    }
    
    /// Return the specified rows as mutable slices.
    /// 
    /// # Panics
    /// 
    /// Will panic if `r1` and `r2` are equal, or if either row index is out of bounds.
    fn row_pair_mut(&mut self, r1: usize, r2: usize) -> (&mut [T], &mut [T]) {
        let num_rows = self.num_rows();
        assert!(r1 < num_rows);
        assert!(r2 < num_rows);
        assert!(r1 != r2);
        match r1.cmp(&r2) {
            Ordering::Less => {
                let mut iter = self.rows_mut();
                let tmp = iter.nth(r1).unwrap();
                (tmp, iter.nth(r2-r1-1).unwrap())
            },
            Ordering::Greater => {
                let mut iter = self.rows_mut();
                let tmp = iter.nth(r2).unwrap();
                (iter.nth(r1-r2-1).unwrap(), tmp)
            },
            Ordering::Equal => {
                unreachable!("r1 != r2");
            },
        }
    }
    
}