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
// Copyright (c) 2020 - BytePlug // // This source file is part of Ingrid which is released under the MIT license. // Please refer to the LICENSE file that can be found at the root of the project // directory. // // Written by Jonathan De Wachter <dewachter.jonathan@gmail.com>, January 2020 use std::ops::Index; use crate::coordinate::Coordinate; use crate::grid::Grid; use crate::iterator_row::IteratorRow; use crate::coord; /// A view onto a row of a grid /// /// This structure is an **immutable** view into a row of a grid and its /// **lifetime is bound** to the lifetime of the grid. It's a **lightweight** /// construct that allows to operate on individual rows effectively; see it as /// an equivalent to the **slice primitive** for grids. /// /// Instead of accessing **elements** with coordinates, just an **index** is /// needed. Rows use the **left to right** direction, therefore, index zero /// corresponds to the element at the very left, also denoted the 'first' /// element of the row. Note that rows are indexable. /// /// With a row, you can easily retrieve the left and right elements of the row, /// with the `left()` and `right()` methods, but also retrieve the row above or /// below, with the `top()` and `bottom()` methods. You can also conveniently /// iterate over its elements with the `iterator()` method which returns an /// efficient iterator. /// /// Because this view is **immutable**, it's limited in terms of what it can do; /// check out the **mutable** counter-part for more operations over the rows. /// /// # Examples /// /// Iterating over the elements of a row. /// /// ``` /// # use ingrid::{Grid, GridIterator}; /// # /// let grid = Grid::from_rows(vec![vec![1, 2, 3], /// vec![4, 5, 6]]); /// /// let row = grid.row(0); /// for (coordinate, value) in row.iterator().enumerate_coordinate() { /// println!("Element at {:?} has value {}.", coordinate, *value); /// } /// ``` /// /// Indexing the row. /// /// ``` /// # use ingrid::Grid; /// # /// let grid = Grid::from_rows(vec![vec![1, 2, 3], /// vec![4, 5, 6]]); /// /// println!("First element of first row is {}", grid.row(0)[0]); /// println!("Last element of last row is {}", grid.row(1)[2]); /// ``` /// #[derive(Debug, Clone, Eq, PartialEq)] pub struct Row<'a, T> { /// A reference to its grid. pub grid: &'a Grid<T>, /// The index of the row. pub index: usize } impl<'a, T: Clone> Row<'a, T> { /// Returns the length of the row. /// /// This method returns the length of the row which is the number of /// elements. It's equivalent to the width of the grid. /// /// # Examples /// /// ``` /// # use ingrid::{Size, Grid, size}; /// # /// let grid = Grid::with_size(size!(3, 2), 42); /// /// assert_eq!(grid.row(0).length(), 3); /// assert_eq!(grid.row(1).length(), 3); /// assert_eq!(grid.size().width, 3); /// ``` /// pub fn length(&self) -> usize { self.grid.size().width } /// Returns a reference to an element of the row. /// /// This method returns a reference to an element of the row from its index. /// /// Note that index zero corresponds to the element at the very left (the /// first element of the row). If you're looking to get the first or the /// last elements of the row, check out the `left()` and `right()` methods. /// /// # Arguments /// /// * `index` - Index of the element /// /// # Panics /// /// It panics if the index is out of bounds. /// /// # Examples /// /// ```rust,should_panic /// # use ingrid::Grid; /// # /// let grid = Grid::from_rows(vec![vec![1, 2, 3], /// vec![4, 5, 6]]); /// /// let row = grid.row(1); /// assert_eq!(row.value(0), &4); /// assert_eq!(row.value(1), &5); /// assert_eq!(row.value(2), &6); /// row.value(3); // It panics here ! /// ``` /// pub fn value(&self, index: usize) -> &'a T { self.grid.value(coord!(index, self.index)) } /// Return the elements of the row. /// /// This method returns the elements of the row as a vector of reference. /// /// # Examples /// /// ``` /// # use ingrid::Grid; /// # /// let grid = Grid::from_rows(vec![vec![1, 2], /// vec![3, 4]]); /// /// assert_eq!(grid.row(0).values(), vec![&1, &2]); /// assert_eq!(grid.row(1).values(), vec![&3, &4]); /// ``` /// pub fn values(&self) -> Vec<&T> { self.iterator().collect() } /// Returns a reference to the first element of the row. /// /// This method returns a reference to the first element of the row. It's /// equivalent to retrieving the element with index `0`. /// /// Note that there is always a first element or the grid would have no /// size. /// /// # Examples /// /// ``` /// # use ingrid::Grid; /// # /// let grid = Grid::from_rows(vec![vec![1, 2, 3], /// vec![4, 5, 6]]); /// /// // The first element of the second row is 4. /// let row = grid.row(1); /// assert_eq!(row.left(), &4); /// ``` /// pub fn left(&self) -> &T { self.grid.value(coord!(0, self.index)) } /// Returns a reference to the last element of the row. /// /// This method returns a reference to the last element of the row. It's /// equivalent to retrieving the element with index `length() -1`. /// /// Note that there is always a last element or the grid would have no /// size. /// /// # Examples /// /// ``` /// # use ingrid::Grid; /// # /// let grid = Grid::from_rows(vec![vec![1, 2, 3], /// vec![4, 5, 6]]); /// /// // The last element of the second row is 6. /// let row = grid.row(1); /// assert_eq!(row.right(), &6); /// ``` /// pub fn right(&self) -> &T { self.grid.value(coord!(self.grid.size().width-1, self.index)) } /// Returns an iterator over the row. /// /// This method returns an iterator over the row. /// /// # Examples /// /// ``` /// # use ingrid::Grid; /// # /// let grid = Grid::from_rows(vec![vec![ 1, 2, 3], /// vec![42, 42, 42]]); /// /// // Check if all elements of the row have value 42. /// assert_eq!(grid.row(0).iterator().all(|item| *item == 42), false); /// assert_eq!(grid.row(1).iterator().all(|item| *item == 42), true); /// ``` /// pub fn iterator(&self) -> IteratorRow<'a, T> { IteratorRow::new(self.clone()) } /// Returns the row above. /// /// This method returns the row above this row, or `None` if this is already /// the row at the very top of the grid. /// /// # Examples /// /// ``` /// # use ingrid::Grid; /// # /// let grid = Grid::from_rows(vec![vec![1, 2], /// vec![3, 4]]); /// /// let second_row = grid.row(1); /// let first_row = second_row.top().unwrap(); /// assert!(first_row.top().is_none()); // There is no row above. /// ``` /// pub fn top(&self) -> Option<Row<'a, T>> { match self.index { 0 => None, index => Some(self.grid.row(index - 1)) } } /// Returns the row below. /// /// This method returns the row below this row, or `None` if this is already /// the row at the very bottom of the grid. /// /// # Examples /// /// ``` /// # use ingrid::Grid; /// # /// let grid = Grid::from_rows(vec![vec![1, 2], /// vec![3, 4]]); /// /// let first_row = grid.row(0); /// let second_row = first_row.bottom().unwrap(); /// assert!(second_row.bottom().is_none()); // There is no row below. /// ``` /// pub fn bottom(&self) -> Option<Row<'a, T>> { // rework this to use match syntax if self.index == self.length() -1 { None } else { Some(self.grid.row(self.index + 1)) } } } impl<'a, T: Clone> Index<usize> for Row<'a, T> { type Output = T; fn index(&self, index: usize) -> &Self::Output { self.value(index) } } #[cfg(test)] mod tests { use super::*; #[test] fn row_length() { let grid = Grid::from_rows(vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]]); assert_eq!(grid.row(0).length(), 3); assert_eq!(grid.row(1).length(), 3); assert_eq!(grid.row(2).length(), 3); } #[test] #[should_panic(expected = "index out of bounds")] fn row_value() { let grid = Grid::from_rows(vec![vec![1, 2], vec![3, 4]]); let row = grid.row(0); assert_eq!(row.value(0), &1); assert_eq!(row.value(1), &2); let row = grid.row(1); assert_eq!(row.value(0), &3); assert_eq!(row.value(1), &4); row.value(2); } #[test] #[should_panic(expected = "index out of bounds")] fn row_index() { let grid = Grid::from_rows(vec![vec![1, 2], vec![3, 4]]); let row = grid.row(0); assert_eq!(row[0], 1); assert_eq!(row[1], 2); let row = grid.row(1); assert_eq!(row[0], 3); assert_eq!(row[1], 4); row[2]; } #[test] fn row_left() { let grid = Grid::from_rows(vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]]); assert_eq!(grid.row(0).left(), &1); assert_eq!(grid.row(1).left(), &4); assert_eq!(grid.row(2).left(), &7); } #[test] fn row_right() { let grid = Grid::from_rows(vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]]); assert_eq!(grid.row(0).right(), &3); assert_eq!(grid.row(1).right(), &6); assert_eq!(grid.row(2).right(), &9); } #[test] fn row_iterator() { let grid = Grid::from_rows(vec![vec![1, 2, 3], vec![4, 5, 6]]); let mut iterator = grid.row(0).iterator(); assert_eq!(iterator.next(), Some(&1)); assert_eq!(iterator.next(), Some(&2)); assert_eq!(iterator.next(), Some(&3)); assert_eq!(iterator.next(), None); let mut iterator = grid.row(1).iterator(); assert_eq!(iterator.next(), Some(&4)); assert_eq!(iterator.next(), Some(&5)); assert_eq!(iterator.next(), Some(&6)); assert_eq!(iterator.next(), None); } #[test] fn row_top() { let grid = Grid::from_rows(vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]]); let last_row = grid.row(2); assert_eq!(last_row.values(), vec!(&7, &8, &9)); let middle_row = last_row.top().unwrap(); assert_eq!(middle_row.values(), vec!(&4, &5, &6)); let first_row = middle_row.top().unwrap(); assert_eq!(first_row.values(), vec!(&1, &2, &3)); assert!(first_row.top().is_none()); } #[test] fn row_bottom() { let grid = Grid::from_rows(vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]]); let first_row = grid.row(0); assert_eq!(first_row.values(), vec!(&1, &2, &3)); let middle_row = first_row.bottom().unwrap(); assert_eq!(middle_row.values(), vec!(&4, &5, &6)); let last_row = middle_row.bottom().unwrap(); assert_eq!(last_row.values(), vec!(&7, &8, &9)); assert!(last_row.bottom().is_none()); } }