Decoded: OpenTTD (2003) v1.8 (2018) Source file: smallmap_type.hpp Line-by-line code walkthrough by MaiZure smallmap_type.hpp defines SmallMap and SmallPair containers Original code: https://github.com/MaiZure/OpenTTD-1.8/blob/master/src/core/smallmap_type.hpp 1 COMMENT (*) 2 BLANK (-) 3 * 4 * 5 * 6 * 7 * 8 * 9 - 10 * 11 - 12 Header guard 13 Header guard 14 - 15 Includes the OpenTTD SmallVector container 16 Includes OpenTTD sort functions 17 - 18 * 19 * 20 * 21 * 22 * 23 Sets up a class template with two parameters: input types 24 BLOCK START - SmallPair, a type-safe container with specific pairs 25 Declares the first pair storage 26 Declares the second pair storage 27 - 28 * 29 Defines a parameterized constructor with initialization list of the state to store 30 BLOCK END - SmallPair 31 - 32 * 33 * 34 * 35 * 36 * 37 * 38 * 39 * 40 * 41 Sets up a class template with three parameters: two types and a size with default of 16 42 BLOCK START - SmallMap, extends SmallVector and provides operations to function as a SmallVector of key/value Pairs 43 Defines a typedef for Pair using this specific type for ease of use 44 Defines a typedef of Pair to use as an interator 45 Defines a similar typedef for a constant interator 46 - 47 * 48 Defines an empty default constructor 49 * 50 Defines an empty destructor 51 - 52 * 53 * 54 * 55 * 56 * 57 Defines Pair with one argument: an input reference to a key string 58 BLOCK START - Find, returns the key or failure 59 Loop through all items in the Vector 60 Test for matching keys, then return the value 61 Loop until all keys are checked 62 If we reach here, look failed. Return end of vector as failure 63 BLOCK END - Find 64 - 65 * 66 * 67 * 68 * 69 * 70 Defines a Pair with a key argument, this time with a non-const return 71 BLOCK START - Find 72 Loop through all items in the Vector 73 Test for matching keys, then return the value 74 Loop until all keys are checked 75 If we reach here, look failed. Return end of vector as failure 76 BLOCK END - Find 77 - 78 * 79 * 80 * 81 * 82 * 83 Defines Contains with one argument: a key 84 BLOCK START - Contains 85 Returns true if Find returns a valid value 86 BLOCK END - Contains 87 - 88 * 89 * 90 * 91 * 92 * 93 Defines Erase with one argument: a key/value pair within the map 94 BLOCK START - Erase 95 Checks that the pointer could lie within the map endpoints 96 Clobbers the pointer in the map 97 BLOCK END - Erase 98 - 99 * 100 * 101 * 102 * 103 * 104 * 105 Defines Erase with one argument, a key reference to lookup 106 BLOCK START - SmallMap::Erase 107 Loop through all pairs in the map 108 If the key matches... 109 Clobber the pair 110 Return true that the erase was successful 111 End check for matching key 112 Repeat loop for all pairs 113 Didn't find the pair, return false for failure to erase 114 BLOCK END - SmallMap::Erase 115 - 116 * 117 * 118 * 119 * 120 * 121 * 122 Defines Insert with two arguments, the key and value for the new pair 123 BLOCK START - SmallMap::Insert 124 If the map already contains a value for the key, return failure 125 Create a new empty pair using the Vector Append interface 126 Set the key 127 Set the value 128 Return true for success 129 BLOCK END - SmallMap::Insert 130 - 131 * 132 * 133 * 134 * 135 * 136 * 137 Defines an overloaded array accessor for the SmallMap with an input key 138 BLOCK START - overloaded [] accessor for input keys 139 Loop through all pairs in the map 140 If the key match a pair in the map then return the value 141 Loop through all pairs 142 If we're here, then it doesn't exist so create a new empty Pair 143 Set the key 144 Return the new key value (default) 145 BLOCK END - overloaded [] accessor 146 - 147 Defines SmallMap::SortByKey with no arguments 148 BLOCK START - SmallMap::SortByKey, calls QSort on map keys 149 Calls Qsort on the map with a length and a function to use 150 BLOCK END - SmallMap::SortByKey 151 - 152 Defines KeySorter, the basic comparator function for the sorter 153 BLOCK START - SmallMap::KeySorter 154 Returns the difference between the key (pointers or hamming distance?) 155 BLOCK END - SmallMap::KeySorter 156 BLOCK END - SmallMap 157 - 158 Header guard end