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

/// A two-dimensional array. Unlike a standard vector, `Vec2d` must maintain a constant
/// number of elements equal to its number of rows times its number of columns.
#[derive (Clone)]
pub struct Vec2d<T> {
    data: Vec<T>,
    width: usize,
}


impl<T: Default> Vec2d<T> {
    /// Creates a new `Vec2d<T>` and initializes all the values to `T::default()`.
    pub fn new_with_default(rows: usize, cols: usize) -> Vec2d<T> {
        Vec2d::new_empty(rows, cols).initialize_to_default()
    }

    /// Adds a new row to the vector and sets all elements of that row to `T::default()`.
    pub fn add_row_of_default(&mut self) {
        // pushes a number of elements to data equal to the width
        // width does not change
        let size = self.count() + self.width;
        while self.data.len() < size {
            self.data.push(T::default());
        }
    }

    // private methods
    fn initialize_to_default(mut self) -> Vec2d<T> {
        let size = self.data.capacity();
        while self.data.len() < size {
            self.data.push(T::default());
        }
        self
    }
}

impl<T: Copy> Vec2d<T> {
    /// Creates a new `Vec2d<T>` and initializes all the values to a copy of `val`.
    pub fn new_with_value(rows: usize, cols: usize, val:T) -> Vec2d<T> {
        Vec2d::new_empty(rows, cols).initialize_to_value(val)
    }

    /// Creates a new `Vec2d<T>` from an array slice.
    /// The slice must have a length that is divisible by `cols` in order to fill the new array.
    /// The array is filled left to right, top to bottom.
    pub fn from_slice(cols: usize, arr: &[T]) -> Vec2d<T> {
        let size = arr.len();
        assert!(size % cols == 0);
        let mut data: Vec<T> = Vec::with_capacity(size);
        for idx in 0..size {
            data.push(arr[idx]);
        }
        Vec2d {
            data: data,
            width: cols,
        }
    }

    /// Creates a new `Vec2d<T>` from an indexable type by copying values up to `size`.
    /// Note that `size` must be divisible by `cols` or the data will not fill the array.
    /// The array is filled from left to right, top to bottom.
    pub fn from_index<U: std::ops::Index<usize, Output=T>>(size: usize, cols: usize, arr: &U) -> Vec2d<T> {
        // must be rectangular
        assert!(size % cols == 0);
        let mut data: Vec<T> = Vec::with_capacity(size);
        for idx in 0..size {
            data.push(arr[idx]);
        }
        Vec2d {
            data: data,
            width: cols,
        }
    }

    // private
    fn initialize_to_value(mut self, val: T) -> Vec2d<T> {
        let size = self.data.capacity();
        while self.data.len() < size {
            self.data.push(val);
        }
        self
    }
}

impl<T: PartialEq> Vec2d<T> {

    /// Checks if `val` is equivalent to any of the elements in the array.
    /// Returns `true` if there is a match, `false` otherwise.
    pub fn contains(&self, val: &T) -> bool {
        for idx in 0..self.count() {
            if *val == self.data[idx] { return true; }
        }
        false
    }
}


impl<T> Vec2d<T> {
    /// Creates a new `Vec2d<T>` with no rows, columns, or elements.
    pub fn new() -> Vec2d<T> {
        // a vector without any data has width 0
        Vec2d {
            data: Vec::new(),
            width: 0,
        }
    }

    /// Creates a new `Vec2d<T>` and initializes its values from `initializer`,
    /// a passed-in function that takes the current cell index (row and column)
    /// and returns a `T`.
    pub fn from_fn(rows: usize, cols: usize, initializer: &dyn Fn(usize, usize) -> T) -> Vec2d<T> {
        Vec2d::new_empty(rows, cols).initialize_from_fn(initializer)
    }

    /// Returns the number of elements in the array.
    /// Equal to the number of rows times the number of columns.
    pub fn count(&self) -> usize {
        self.width * self.count_rows()
    }

    /// Returns the number of columns of the array.
    pub fn count_cols(&self) -> usize {
        self.width
    }

    /// Returns the number of rows of the array.
    pub fn count_rows(&self) -> usize {
        if self.width == 0 { 0 } else { self.data.len() / self.width }
    }

    /// Returns the dimensions of the array as a tuple of (row, col).
    pub fn size(&self) -> (usize, usize) {
        (self.count_rows(), self.count_cols())
    }

    // private
    fn new_empty(rows: usize, cols: usize) -> Vec2d<T> {
        Vec2d {
            data: Vec::with_capacity(rows*cols),
            width: cols,
        }
    }

    fn initialize_from_fn(mut self, initializer: &dyn Fn(usize, usize) -> T) -> Vec2d<T> {
        let size = self.data.capacity();
        for idx in 0..size {
            let row = idx / self.width;
            let col = idx - (row * self.width);
            self.data.push(initializer(row, col));
        }
        self
    }
}

impl<T> std::ops::Index<usize> for Vec2d<T> {
    type Output = [T];

    fn index(&self, row: usize) -> &[T] {
        let start = row * self.width;
        let end = start + self.width;
        &self.data[start..end]
    }
}

impl<T> std::ops::IndexMut<usize> for Vec2d<T> {
    fn index_mut<'a>(&'a mut self, row: usize) -> &'a mut Self::Output {
        let start = row * self.width;
        let end = start + self.width;
        &mut self.data[start..end]
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn check_initializer() {
        let v = Vec2d::from_fn(3, 3, &|row, col| row + col);
        for idx in 0..v.count() {
            let row = idx / v.count_cols();
            let col = idx - (row * v.count_cols());
            assert_eq!(v[row][col], row+col);
        }
    }

    #[test]
    #[should_panic]
    fn panics_on_mismatched_arr_size() {
        let arr: [i32;4] = [1,2,3,4];
        let slc: &[i32] = &arr[0..3];
        let _v: Vec2d<i32> = Vec2d::from_slice(2, slc);
        let arr: [i32;5] = [1,2,3,4,5];
        let _v = Vec2d::from_slice(2, &arr);
    }

    #[test]
    fn build_from_data() {
        let data: [i32;12] = [1,2,3,4,5,6,7,8,9,10,11,12];
        for divs in 1..13 {
            if 12 % divs == 0 {
                let _v = Vec2d::from_slice(divs, &data);
                for row in 0.._v.count_rows() {
                    for col in 0.._v.count_cols() {
                        let idx = row * _v.count_cols() + col;
                        assert_eq!(data[idx], _v[row][col]);
                    }
                }
            }
        }
    }



}