|
| 1 | +use std::fmt::Display; |
| 2 | + |
| 3 | +use arrayvec::ArrayVec; |
| 4 | + |
| 5 | +/// A generic 2D table for general use in chess engines. |
| 6 | +#[derive(Debug, Clone)] |
| 7 | +pub(crate) struct Table<T, const CAP: usize> { |
| 8 | + data: ArrayVec<T, CAP>, |
| 9 | + width: usize, |
| 10 | + height: usize, |
| 11 | +} |
| 12 | + |
| 13 | +impl<T, const CAP: usize> Table<T, CAP> { |
| 14 | + /// Creates a new table with the specified width and height. |
| 15 | + /// The table is initialized with a capacity of `CAP` elements. |
| 16 | + /// The width and height are the number of columns and rows respectively. |
| 17 | + /// |
| 18 | + /// # Arguments |
| 19 | + /// |
| 20 | + /// - `width` - The number of columns in the table. |
| 21 | + /// - `height` - The number of rows in the table. |
| 22 | + /// |
| 23 | + /// # Returns |
| 24 | + /// |
| 25 | + /// A new instance of `Table<T, CAP>`. |
| 26 | + /// |
| 27 | + /// # Panics |
| 28 | + /// |
| 29 | + /// This function will panic if width*height > CAP. |
| 30 | + pub fn new(width: usize, height: usize) -> Self { |
| 31 | + assert!( |
| 32 | + width * height <= CAP, |
| 33 | + "Table capacity exceeded: {} > {}", |
| 34 | + width * height, |
| 35 | + CAP |
| 36 | + ); |
| 37 | + |
| 38 | + Self { |
| 39 | + data: ArrayVec::<T, CAP>::new(), |
| 40 | + width, |
| 41 | + height, |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /// Insert an element at the given row and column. |
| 46 | + /// |
| 47 | + /// # Arguments |
| 48 | + /// |
| 49 | + /// - `value` - The value to insert. |
| 50 | + /// - `row` - The row index. |
| 51 | + /// - `col` - The column index. |
| 52 | + pub fn insert(&mut self, value: T, row: usize, col: usize) { |
| 53 | + assert!( |
| 54 | + row < self.rows() && col < self.cols(), |
| 55 | + "Invalid row or column index" |
| 56 | + ); |
| 57 | + self.data.insert(self.index(row, col), value); |
| 58 | + } |
| 59 | + |
| 60 | + /// Get a mutable reference to the element at the given row and column, if it exists. |
| 61 | + /// |
| 62 | + /// # Arguments |
| 63 | + /// |
| 64 | + /// - `row` - The row index. |
| 65 | + /// - `col` - The column index. |
| 66 | + /// |
| 67 | + /// # Returns |
| 68 | + /// |
| 69 | + /// An `Option<&T>` that contains a reference to the element at the specified row and column, or `None` if it does not exist. |
| 70 | + pub fn at(&self, row: usize, col: usize) -> Option<&T> { |
| 71 | + self.data.get(self.index(row, col)) |
| 72 | + } |
| 73 | + |
| 74 | + /// Get a slice of the given row. |
| 75 | + /// |
| 76 | + /// # Arguments |
| 77 | + /// |
| 78 | + /// - `row` - The row index. |
| 79 | + /// |
| 80 | + /// # Returns |
| 81 | + /// |
| 82 | + /// A slice of the row at the specified index. |
| 83 | + pub fn row(&self, row: usize) -> &[T] { |
| 84 | + let start_idx = self.index(row, 0); |
| 85 | + let end_idx = self.index(row, self.cols()); |
| 86 | + // note, this is an inclusive range |
| 87 | + self.data.get(start_idx..end_idx).expect("Invalid range") |
| 88 | + } |
| 89 | + |
| 90 | + /// Returns the number of rows in the table; a.k.a, the height. |
| 91 | + pub fn rows(&self) -> usize { |
| 92 | + self.height |
| 93 | + } |
| 94 | + |
| 95 | + /// Returns the number of columns in the table; a.k.a, the width. |
| 96 | + pub fn cols(&self) -> usize { |
| 97 | + self.width |
| 98 | + } |
| 99 | + |
| 100 | + /// Fills the table with values generated by the provided function. |
| 101 | + /// |
| 102 | + /// # Arguments |
| 103 | + /// |
| 104 | + /// - `value_producer` - A function that takes the row and column indices and returns a value of type `T`. |
| 105 | + pub fn fill<ValueProducer: Fn(usize, usize) -> T>(&mut self, value_producer: ValueProducer) { |
| 106 | + for row in 0..self.rows() { |
| 107 | + for col in 0..self.cols() { |
| 108 | + self.insert(value_producer(row, col), row, col); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /// Helper to calculate the index with a given row and column. |
| 114 | + fn index(&self, row: usize, col: usize) -> usize { |
| 115 | + row * self.width + col |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +impl<T: Display, const CAP: usize> Display for Table<T, CAP> { |
| 120 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 121 | + let mut output = "".to_string(); |
| 122 | + |
| 123 | + for row in 0..self.rows() { |
| 124 | + let row_data = self.row(row); |
| 125 | + for (col, item) in row_data.iter().enumerate() { |
| 126 | + output.push_str(format!("{}", item).as_str()); |
| 127 | + if col < self.cols() - 1 { |
| 128 | + output.push(','); |
| 129 | + output.push(' '); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + if row < self.rows() - 1 { |
| 134 | + output.push('\n'); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + write!(f, "{}", output) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +#[cfg(test)] |
| 143 | +mod tests { |
| 144 | + use crate::table::Table; |
| 145 | + |
| 146 | + #[test] |
| 147 | + fn initialize_and_fill() { |
| 148 | + const SIZE: usize = 8; |
| 149 | + let iota = |row: usize, col: usize| -> usize { row * SIZE + col }; |
| 150 | + |
| 151 | + let mut table = Table::<usize, 64>::new(8, 8); |
| 152 | + table.fill(iota); |
| 153 | + |
| 154 | + for row in 0..table.rows() { |
| 155 | + for col in 0..table.cols() { |
| 156 | + let val = table.at(row, col); |
| 157 | + assert!(val.is_some()); |
| 158 | + assert_eq!(*val.unwrap(), iota(row, col)); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + #[test] |
| 164 | + #[should_panic] |
| 165 | + fn expect_panic_with_cap_mismatch() { |
| 166 | + const SIZE: usize = 8; |
| 167 | + let _ = Table::<usize, SIZE>::new(8, 8); |
| 168 | + } |
| 169 | + |
| 170 | + #[test] |
| 171 | + #[should_panic] |
| 172 | + fn insert_invalid_index() { |
| 173 | + const SIZE: usize = 8; |
| 174 | + let mut table = Table::<usize, SIZE>::new(8, 8); |
| 175 | + table.insert(0, 0, 10); |
| 176 | + } |
| 177 | + |
| 178 | + #[test] |
| 179 | + fn index_calculation() { |
| 180 | + const SIZE: usize = 64; |
| 181 | + let table = Table::<usize, SIZE>::new(8, 8); |
| 182 | + let mut i = 0; |
| 183 | + for row in 0..table.rows() { |
| 184 | + for col in 0..table.cols() { |
| 185 | + assert_eq!(table.index(row, col), i); |
| 186 | + i += 1; |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + #[test] |
| 192 | + fn read_row() { |
| 193 | + const SIZE: usize = 64; |
| 194 | + let mut table = Table::<usize, SIZE>::new(8, 8); |
| 195 | + table.fill(|_, col| col); |
| 196 | + |
| 197 | + for row in 0..table.rows() { |
| 198 | + let row_data = table.row(row); |
| 199 | + assert!(row_data.len() == table.cols()); |
| 200 | + for (i, item) in row_data.iter().enumerate() { |
| 201 | + assert_eq!(*item, i); |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + #[test] |
| 207 | + fn verify_formatting() { |
| 208 | + const SIZE: usize = 64; |
| 209 | + let mut table = Table::<usize, SIZE>::new(8, 8); |
| 210 | + table.fill(|row, col| row * 8 + col); |
| 211 | + let formatted = format!("{}", table); |
| 212 | + println!("{}", formatted); |
| 213 | + let expected = "0, 1, 2, 3, 4, 5, 6, 7\n\ |
| 214 | + 8, 9, 10, 11, 12, 13, 14, 15\n\ |
| 215 | + 16, 17, 18, 19, 20, 21, 22, 23\n\ |
| 216 | + 24, 25, 26, 27, 28, 29, 30, 31\n\ |
| 217 | + 32, 33, 34, 35, 36, 37, 38, 39\n\ |
| 218 | + 40, 41, 42, 43, 44, 45, 46, 47\n\ |
| 219 | + 48, 49, 50, 51, 52, 53, 54, 55\n\ |
| 220 | + 56, 57, 58, 59, 60, 61, 62, 63"; |
| 221 | + |
| 222 | + assert_eq!(formatted, expected); |
| 223 | + } |
| 224 | +} |
0 commit comments