Decoded: OpenTTD (2003) v1.8 (2018) Source file: smallvec_type.hpp Line-by-line code walkthrough by MaiZure smallvec_type.hpp defines SmallVector container Original code: https://github.com/MaiZure/OpenTTD-1.8/blob/master/src/core/smallvec_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 Sets up a class template with two paramters: a type and a base size 29 BLOCK START - SmallVector, STL-like vector container 30 Begin protected section 31 Pointer to the container data 32 Declare the number of items in the container 33 The total number of items storable in the vector 34 * 35 Begins the public section 36 Defines the defaul constructor with a null-value initialization list 37 * 38 * 39 * 40 * 41 * 42 Declares a copy constructor with an null value initialization list 43 BLOCK START - SmallVector copy constructor 44 Return the assigned vector 45 BLOCK END - SmallVector copy constructor 46 - 47 * 48 * 49 * 50 * 51 Sets up a function template with an input base size 52 Defines a copy constructor with a null initialization list 53 BLOCK START - SmallVector copy constructor 54 Assign the input vector to the current vector. Return the new assignment 55 BLOCK END - SmallVector 56 - 57 * 58 * 59 * 60 * 61 Defines the overloaded assignment operator with an input SmallVector 62 BLOCK START - Overloaded = operator for SmallVector 63 Assigns the input SmallVector's elements to the current SmallVector 64 Returns a pointer to the current SmallVector 65 BLOCK END - Overloaded = operator for SmallVector 66 - 67 * 68 * 69 * 70 * 71 Sets up a function template with an input base size 72 Defines the overloaded assignment operator with an input SmallVector 73 BLOCK START - Overloaded = operator for SmallVector 74 Assigns the input SmallVector's elements to the current SmallVector 75 Returns a pointer to the current SmallVector 76 BLOCK END - Overloaded = operator for SmallVector 77 - 78 Defines a destructor for SmallVector 79 BLOCK START - SmallVector destructor 80 Destroys the allocated data 81 BLOCK END - SmallVector destructor 82 - 83 * 84 * 85 * 86 Sets up a function template with an input base size 87 Defines SmallVector::Assign with one input argument: The SmallVector to assign 88 BLOCK START - SmallVector::Assign, assigns another SmallVector 89 If the SmallVector to assign is already this object, do nothing 90 - 91 Clear the current object 92 If object to assign has data, copy the data to this SmallVector 93 BLOCK END - SmallVector::Assign 94 - 95 * 96 * 97 * 98 Defines SmallVector::Clear with no arguments 99 BLOCK START - SmallVector::Clear, simple vector reset 100 * 101 * 102 * 103 Clear the item counter 104 BLOCK END - SmallVector::Clear 105 - 106 * 107 * 108 * 109 Defines SmallVector::Reset with no arguments 110 BLOCK START - SmallVector::Reset, empties the vector 111 Clear the item counter 112 Clear the vector capacity counter 113 Destroy all data 114 Invalidate the data pointer 115 BLOCK END - SmallVector::Reset 116 - 117 * 118 * 119 * 120 Defines SmallVector::Compact with no arguments 121 BLOCK START - SmallVector::Compact, shrinks the vector if possible 122 Calculates the capacity required by the vector 123 If the computed capacity is the same or larger, nothing to do 124 - 125 Updates the capacity with the computed value 126 Reallocates the SmallVector with the computed capacity 127 BLOCK END - SmallVector::Compact 128 - 129 * 130 * 131 * 132 * 133 * 134 Defines SmallVector::Append with one argument, the item count to append 135 BLOCK START - SmallVector::Append, adds an item to the end 136 Store the current number of items 137 Add the count of items to add to the tracking variable 138 - 139 If the new size is larger than the current capacity... 140 Ensure that the capacity matches the desired base size 141 Reallocate memory to grow the array 142 End check for the need to grow 143 - 144 Return a pointer to the start of the new data 145 BLOCK END - SmallVector::Append 146 - 147 * 148 * 149 * 150 * 151 Defines SmallVector::Resize with one argument, the number of desired items in the vector 152 BLOCK START - SmallVector::Resize, changes the vector size 153 Updating the count of items 154 - 155 If the new size is larger than the current capacity... 156 Ensure that the capacity matches the desired base size 157 Reallocate memory to grow the array 158 End check for the need to grow 159 BLOCK END - SmallVector::Resize 160 - 161 * 162 * 163 * 164 * 165 * 166 * 167 Defines SmallVector::Find with one argument, a pointer to the item to find (as const pointer) 168 BLOCK START - SmallVector::Find, finds an item 169 Get the item at the beginning of the SmallVector 170 Get the item at the end of the SmallVector 171 Increment through the vector until the item matches input or we're at the end 172 Return the discovered item (item or End) 173 BLOCK END - SmallVector::Find 174 - 175 * 176 * 177 * 178 * 179 * 180 * 181 Defines SmallVector::Clear with one argument, a pointer to the item to find (as a const pointer) 182 BLOCK START - SmallVector::Find, finds an item 183 Get the item at the beginning of the SmallVector 184 Get the item at the end of the SmallVector 185 Increment through the vector until the item matches input or we're at the end 186 Return the discovered item (item or End) 187 BLOCK END - SmallVector::Find 188 - 189 * 190 * 191 * 192 * 193 * 194 * 195 Defines SmallVector::FindIndex with one argument, a pointer to the item to find 196 BLOCK START - SmallVector::FindIndex, gets an item's vector position 197 Initializes an index counter to 0 198 Get the item at the beginning of the SmallVector 199 Get the item at the end of the SmallVector 200 Loop through all items while the desired item hasn't been found 201 Increment to the next item 202 Increment the index counter 203 Repeat loop as needed 204 If we didn't find the item, return -1 (fail), otherwise return the index counter 205 BLOCK END - SmallVector::FindIndex 206 - 207 * 208 * 209 * 210 * 211 * 212 * 213 Defines SmallVector::Clear with one argument: a pointer to the item to verify 214 BLOCK START - SmallVector::Contains, checks for an item 215 Return the boolean result of an attempted Find 216 BLOCK END - SmallVector::Contains 217 - 218 * 219 * 220 * 221 * 222 * 223 Defines SmallVector::Erase with one argument, a pointer to the item to remove 224 BLOCK START - SmallVector::Erase, removes items 225 Checks that the input item is within the bounds of the SmallVector 226 Replaces the item with the last item in the SmallVector 227 BLOCK END - SmallVector::Erase 228 - 229 * 230 * 231 * 232 * 233 * 234 Defines SmallVector::Clear with two arguments, the item position to start, and the item count to remove 235 BLOCK START - SmallVector::ErasePreservingOrder, removes items and replaces from the back 236 If no items are requested for erasure, there's no work to do 237 Verify that the start position is valid 238 Verify that the final position is valid 239 Remove the number of requested items from the counter 240 Find the number of items to move from the back 241 Overwrite deleted items from the back of the SmallVector 242 BLOCK END - SmallVector::ErasePreservingOrder 243 - 244 * 245 * 246 * 247 * 248 * 249 * 250 Defines SmallVector::Include with one argument: a pointer to the item to test and append 251 BLOCK START - SmallVector::Include, tests an adds item to vector 252 Check if the SmallVector already contains the input item 253 If it does not, append it to the end 254 Return the boolean result if the item was already present 255 BLOCK END - SmallVector::Include 256 - 257 * 258 * 259 * 260 * 261 * 262 Defines SmallVector::Length with no arguments 263 BLOCK START - SmallVector::Length, finds the vector item length 264 Returns the number of items 265 BLOCK END - SmallVector::Length 266 - 267 * 268 * 269 * 270 * 271 * 272 Defines SmallVector::Begin with no arguments 273 BLOCK START - SmallVector::Begin, finds the first item 274 Returns a constant pointer to the start of the item data 275 BLOCK END - SmallVector::Begin 276 - 277 * 278 * 279 * 280 * 281 * 282 Defines SmallVector::Begin with no arguments 283 BLOCK START - SmallVector::Begin, finds the first item 284 Returns a pointer to the start of the item data 285 BLOCK END - SmallVector::Begin 286 - 287 * 288 * 289 * 290 * 291 * 292 Defines SmallVector::End with no arguments 293 BLOCK START - SmallVector::End, finds the last item 294 Returns a constant pointer to the last item in the SmallVector 295 BLOCK END - SmallVector::End 296 - 297 * 298 * 299 * 300 * 301 * 302 Defines SmallVector::End with no arguments 303 BLOCK START - SmallVector::End, finds the last item 304 Returns a pointer to the last item in the SmallVector 305 BLOCK END - SmallVector::End 306 - 307 * 308 * 309 * 310 * 311 * 312 * 313 Defines SmallVector::Get with one argument: the position to return 314 BLOCK START - SmallVector::Get, returns a constant pointer to an item 315 * 316 Verifies that the index exists 317 Returns a constant pointer to the item at the position 318 BLOCK END - SmallVector::Get 319 - 320 * 321 * 322 * 323 * 324 * 325 * 326 Defines SmallVector::Get with one argument: the position to return 327 BLOCK START - SmallVector::Get, returns a pointer to an item 328 * 329 Verifies that the index exists 330 Returns a pointer to the item at the position 331 BLOCK END - SmallVector::Get 332 - 333 * 334 * 335 * 336 * 337 * 338 * 339 Defines the overloaded array access operator with an input SmallVector 340 BLOCK START - SmallVector [] operator overload 341 Verify that the input index is valid 342 Return the item at the index 343 BLOCK END - SmallVector [] operator overload 344 - 345 * 346 * 347 * 348 * 349 * 350 * 351 Defines the overloaded array access operator with an input SmallVector 352 BLOCK START - SmallVector [] operator overload 353 Verify that the input index is valid 354 Return the item at the index 355 BLOCK END - SmallVector [] operator overload 356 BLOCK END - SmallVector 357 - 358 - 359 * 360 * 361 * 362 * 363 * 364 * 365 * 366 * 367 * 368 * 369 Sets up a class template with two parameters: a type and base size 370 Defines AutoFreeSmallVector as an extended SmallVector 371 Begin the public section of AutoFreeSmallVector 372 Define the AutoFreeSmallVector destructor 373 BLOCK START - AutoFreeSmallVector destructor 374 Remove all items in the vector 375 BLOCK END - AutoFreeSmallVector destructor 376 - 377 * 378 * 379 * 380 Defines AutoFreeSmallVector::Clear with no arguments 381 BLOCK START - AutoFreeSmallVector::Clear 382 Loop through all members of the vector 383 Destroy the data at the index 384 Repeat loop until all data is freed 385 - 386 Set vector size to zero 387 BLOCK END - AutoFreeSmallVector::Clear 388 BLOCK END - AutoFreeSmallVector 389 - 390 * 391 * 392 * 393 * 394 * 395 * 396 * 397 * 398 * 399 * 400 Sets up a class template with two parameters: a type and base size 401 Defines AutoDeleteSmallVector as an extended SmallVector 402 Begins the public section of AutoDeleteSmallVector 403 Defines the AutoDeleteSmallVector destructor 404 BLOCK START - AutoDeleteSmallVector destructor 405 Remove all items in the vector 406 BLOCK END - AutoDeleteSmallVector destructor 407 - 408 * 409 * 410 * 411 Defines AutoDeleteSmallVector::Clear with no arguments 412 BLOCK START - AutoDeleteSmallVector::Clear, deletes all items 413 Loop through items in the SmallVector 414 Destory the item 415 Repeat loop as needed 416 - 417 Clear the item counter 418 BLOCK END - AutoDeleteSmallVector::Clear 419 BLOCK END - AutoDeleteSmallVector 420 - 421 Adds a typedef for a string list from an AutoFreeSmallVector 422 * 423 Header guard end