Decoded: OpenTTD (2003) v1.8 (2018) Source file: smallmatrix_type.hpp Line-by-line code walkthrough by MaiZure smallmatrix_type.hpp defines SmallMatrix container Original code: https://github.com/MaiZure/OpenTTD-1.8/blob/master/src/core/smallmatrix_type.hpp 1 COMMENT (*) 2 BLANK (-) 3 * 4 * 5 * 6 * 7 * 8 * 9 - 10 * 11 - 12 Header guard 13 Header guard 14 - 15 Include the OpenTTD type-safe memory allocation functions 16 Include the OpenTTD type-safe memory access functions 17 - 18 * 19 * 20 * 21 * 22 * 23 * 24 * 25 * 26 * 27 * 28 * 29 * 30 * 31 * 32 * 33 * 34 * 35 * 36 * 37 * 38 * 39 Sets up a class template with a type parameter 40 BLOCK START - SmallMatrix, a lightweight rectangular matrix 41 Beings the protected section of SmallMatrix 42 Data pointer to the first item 43 The width of a matrix 44 The height of the matrix 45 The total space in the matrix 46 * 47 Begins the public section of SmallMatrix 48 * 49 Default SmallMatrix constructor with init list for an empty matrix 50 * 51 * 52 * 53 * 54 * 55 Defines a copy constructor with empty initialization 56 BLOCK START - SmallMatrix copy constructor 57 Assigns the input matrix 58 BLOCK END - SmallMatrix copy constructor 59 - 60 Defines the destructor 61 BLOCK START - SmallMatrix destructor 62 Free the data 63 BLOCK END - SmallMatrix destructor 64 - 65 * 66 * 67 * 68 * 69 Defines overloaded assignment operator for SmallMatrix 70 BLOCK START - SmallMatrix = operator overload 71 Assigns the input matix to this matrix 72 Returns the pointer to self 73 BLOCK END - SmallMatrix = operator overload 74 - 75 * 76 * 77 * 78 Defines SmallMatrix::Assign with one argument, an input matrix 79 BLOCK START - SmallMatrix::Assign 80 If this is the same matrix, no need to assign so return 81 - 82 Match this matrix height to input matrix height 83 Match this matrix width to input matrix width 84 Get the number of items in the updated matrix 85 If the number of items is beyond capacity (it grew) 86 Update the capacity to the new size 87 Remove existing data 88 Allocate space for the new data 89 Copy the data from the input matrix 90 Otherwise there is enough capacity already.. 91 Copy the data from the input matrix 92 End check for matrix capacity 93 BLOCK END - SmallMatrix::Assign 94 - 95 * 96 * 97 * 98 Defines SmallMatrix::Clear with no arguments 99 BLOCK START - SmallMatrix::Clear, removes all rows 100 * 101 * 102 * 103 Sets matrix width to zero 104 BLOCK END - SmallMatrix::Clear 105 - 106 * 107 * 108 * 109 Defines SmallMatrix::Reset with no arguments 110 BLOCK START - SmallMatrix::Reset 111 Removes all rows 112 Removes all columns 113 Removes entry count 114 Destroys data 115 Removes data pointer 116 BLOCK END - SmallMatrix::Reset 117 - 118 * 119 * 120 * 121 Defines SmallMatrix::Compact with no arguments 122 BLOCK START - SmallMatrix::Compant 123 Recalculates matrix capacity 124 If the new capacity is larger than the actual capacity 125 Update the actual capacity 126 Resize matrix data 127 BLOCK END - SmallMatrix::Compact 128 - 129 * 130 * 131 * 132 * 133 Defines SmallMatrix::EraseColumn with one argument: column position 134 BLOCK START - SmallMatrix::EraseColumn 135 Decrement column count and if the column wasn't the last one... 136 Copy all the proceeding data to the replaced position... 137 ..from the original position.. 138 ...and get all the data 139 End check for data to move 140 BLOCK END - SmallMatrix::EraseColumn 141 - 142 * 143 * 144 * 145 * 146 * 147 Defines SmallMatrix::EraseColumnPreservingOrder with two arguments: position and count of columns to delete (defualt 1) 148 BLOCK START - SmallMatrix::EraseColumnPreservingOrder 149 If there are no columns to delete, do nothing 150 Check that the start column is valid 151 Check that all columns are valid 152 Remove the columns 153 Get the data count to move 154 If there is data to move 155 Copy the data to the start of the removed position 156 And get all the data 157 End of check for data to move 158 BLOCK END - SmallMatrix::EraseColumnPreservingOrder 159 - 160 * 161 * 162 * 163 * 164 Defines SmallMatrix::EraseRow with one argument: row position 165 BLOCK START - SmallMatrix::EraseRow 166 If the row to delete is valid... 167 Loop through all elements in the row 168 Replace the row with... 169 The last row of data... 170 Repeat loop for all row elements 171 End check for valid row to delete 172 Resize the matrix with the new dimensions 173 BLOCK END - SmallMatrix::EraseRow 174 - 175 * 176 * 177 * 178 * 179 * 180 Defines SmallMatrix::EraseRowPreservingOrder with two arguments: a position and count to delete (default 1) 181 BLOCK START - SmallMatrix::EraseRowPreservingOrder 182 If all of the rows are valid... 183 Loop through all of the columns 184 Move data to the beginning of the erased section.. 185 from beyond the end of the last erased row.. 186 and include all data elements that still exist 187 Repeat move loop 188 End check for valid rows 189 Resize the matrix with the rows removed 190 BLOCK END - SmallMatrix::EraseRowPreservingOrder 191 - 192 * 193 * 194 * 195 * 196 Defines SmallMatrix::AppendRow with one argument: Rows to add 197 BLOCK START - SmallMatrix::Append Row 198 Resize array with the new row count 199 BLOCK END - SmallMatrix::Append Row 200 - 201 * 202 * 203 * 204 * 205 Defines SmallMatrix::AppendColumn with one argument: Rows to add 206 BLOCK START - SmallMatrix::AppendColumn 207 Resize array with the new row count 208 BLOCK END - SmallMatrix::AppendColumn 209 - 210 * 211 * 212 * 213 * 214 * 215 * 216 Defines SmallMatrix::Resize with two arguments: the new width and height 217 BLOCK START - SmallMatrix::Resize 218 Compute the new capacity 219 Declare a new pointer for the data 220 Declare a function pointer with a memcpy/move-like prototype 221 If the matrix needs to grow... 222 * 223 Allocates the proper size to the new data pointer 224 Assigns MemCpyT to the copy function pointer 225 Otherwise the matrix is large enough so... 226 * 227 Simply copy the data pointer 228 Assign MemMoveT to the copy function 229 End check for correct capacity 230 If the height isn't correct or the data pointers don't match.. 231 If the current height is positive.. 232 and the new height needs to be larger... 233 * 234 * 235 Loop across the matrix from the back 236 If we're looking at the new space, skip the loop step 237 Copy to the back of the new array with the adjusted height 238 From the old space 239 The amount that's smaller, the old height or the new height 240 Repeat loop until all data is copied 241 Otherwise the new matrix is smaller so... 242 * 243 Loop from the front... 244 If we're already past the new matrix, end the copy 245 Copy to the new matrix using the new height 246 From the old matrix, same offset 247 Using the smaller of the two matrices 248 Repeat loop for all data to copy 249 End check for growing/shrinking 250 End check for positive height 251 Match the array heights 252 If this is a new array... 253 Delete the old data pointer 254 Update to the new data pointer 255 Update the capacity 256 End check for non-matching pointers 257 End check for non-matching height or data pointers 258 Match the new widths 259 BLOCK END - SmallMatrix::Resize 260 - 261 Defines SmallMatrix::Height with no arguments 262 BLOCK START - SmallMatrix::Height 263 Return the height of the matrix 264 BLOCK END - SmallMatrix::Height 265 - 266 Defines SmallMatrix::Width with no arguments 267 BLOCK START - SmallMatrix::Weight 268 Return the width of the matrix 269 BLOCK END - SmallMatrix::Weight 270 - 271 * 272 * 273 * 274 * 275 * 276 * 277 * 278 Defines SmallMatrix::Get with two arguments: the x and y position 279 BLOCK START - SmallMatrix::Get 280 281 282 BLOCK END - SmallMatrix::Get 283 - 284 * 285 * 286 * 287 * 288 * 289 * 290 * 291 Defines SmallMatrix::Get with two arguments: the x and y position 292 BLOCK START - SmallMatrix::Get 293 Confirm that the given coordinates are valid 294 Return the data at the coordinates (y-major) 295 BLOCK END - SmallMatrix::Get 296 - 297 * 298 * 299 * 300 * 301 * 302 * 303 Defines overloaded array access operator for SmallMatrix 304 BLOCK START - Overloading [] operator for SmallMatrix 305 Confirm that the x position is valid 306 Return the input column head 307 BLOCK END - Overloading [] operator for SmallMatrix 308 - 309 * 310 * 311 * 312 * 313 * 314 * 315 Defines overloaded array access operator for SmallMatrix 316 BLOCK START - Overloading [] operator for SmallMatrix 317 Confirm a valid column now 318 Return the input column head 319 BLOCK END - Overloading [] operator for SmallMatrix 320 BLOCK END - SmallMatrix 321 - 322 Header guard end