Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : #ifndef INCLUDED_ENTITYMAP 18 : #define INCLUDED_ENTITYMAP 19 : 20 : #include "lib/debug.h" 21 : #include "simulation2/serialization/SerializeTemplates.h" 22 : #include "simulation2/system/Component.h" 23 : #include "simulation2/system/Entity.h" 24 : 25 : #include <cstddef> 26 : #include <cstdint> 27 : #include <cstdlib> 28 : #include <iterator> 29 : #include <new> 30 : 31 : /** 32 : * A fast replacement for map<entity_id_t, T>. 33 : * We make the following assumptions: 34 : * - entity id's (keys) are unique 35 : * - modifications (add / delete) are far less frequent then look-ups 36 : * - preformance for iteration is important 37 : */ 38 : template<class T> class EntityMap 39 : { 40 : private: 41 : EntityMap(const EntityMap&); // non-copyable 42 : EntityMap& operator=(const EntityMap&); // non-copyable 43 : 44 : public: 45 : typedef entity_id_t key_type; 46 : typedef T mapped_type; 47 : template<class K, class V> struct key_val { 48 : typedef K first_type; 49 : typedef V second_type; 50 : K first; 51 : V second; 52 : }; 53 : typedef key_val<entity_id_t, T> value_type; 54 : 55 : private: 56 : size_t m_BufferSize; // number of elements in the buffer 57 : size_t m_BufferCapacity; // capacity of the buffer 58 : value_type* m_Buffer; // vector with all the mapped key-value pairs 59 : 60 : size_t m_Count; // number of 'valid' entity id's 61 : 62 : public: 63 : 64 35 : inline EntityMap() : m_BufferSize(1), m_BufferCapacity(4096), m_Count(0) 65 : { 66 : // for entitymap we allocate the buffer right away 67 : // with first element in buffer being the Invalid Entity 68 35 : m_Buffer = (value_type*)malloc(sizeof(value_type) * (m_BufferCapacity + 1)); 69 35 : if (!m_Buffer) 70 0 : throw std::bad_alloc(); 71 : 72 : // create the first element: 73 35 : m_Buffer[0].first = INVALID_ENTITY; 74 35 : m_Buffer[1].first = 0xFFFFFFFF; // ensure end() always has 0xFFFFFFFF 75 35 : } 76 5 : inline ~EntityMap() 77 : { 78 5 : free(m_Buffer); 79 4 : } 80 : 81 : // Iterators 82 : template<class U> struct _iter 83 : { 84 : using iterator_category = std::forward_iterator_tag; 85 : using value_type = U; 86 : using difference_type = std::ptrdiff_t; 87 : using pointer = U*; 88 : using reference = U&; 89 : 90 : U* val; 91 175101 : inline _iter(U* init) : val(init) {} 92 : inline U& operator*() { return *val; } 93 69423 : inline U* operator->() { return val; } 94 28376 : inline _iter& operator++() // ++it 95 : { 96 28376 : ++val; 97 29000 : while (val->first == INVALID_ENTITY) ++val; // skip any invalid entities 98 : return *this; 99 : } 100 : inline _iter operator++(int) // it++ 101 : { 102 : _iter it = *this; 103 : ++val; 104 : while (val->first == INVALID_ENTITY) ++val; // skip any invalid entities 105 : return it; 106 : } 107 4 : inline bool operator==(_iter other) { return val == other.val; } 108 8478 : inline bool operator!=(_iter other) { return val != other.val; } 109 2547 : inline operator _iter<U const>() const { return _iter<U const>(val); } 110 : }; 111 : 112 : typedef _iter<value_type> iterator; 113 : typedef _iter<value_type const> const_iterator; 114 : 115 3261 : inline iterator begin() 116 : { 117 3261 : value_type* ptr = m_Buffer + 1; // skip the first INVALID_ENTITY 118 209988 : while (ptr->first == INVALID_ENTITY) ++ptr; // skip any other invalid entities 119 1086 : return ptr; 120 : } 121 143397 : inline iterator end() 122 : { 123 142335 : return iterator(m_Buffer + m_BufferSize); 124 : } 125 336 : inline const_iterator begin() const 126 : { 127 336 : value_type* ptr = m_Buffer + 1; // skip the first INVALID_ENTITY 128 7464 : while (ptr->first == INVALID_ENTITY) ++ptr; // skip any other invalid entities 129 : return ptr; 130 : } 131 30814 : inline const_iterator end() const 132 : { 133 30814 : return const_iterator(m_Buffer + m_BufferSize); 134 : } 135 : 136 : // Size 137 1 : inline bool empty() const { return m_Count == 0; } 138 59 : inline size_t size() const { return m_Count; } 139 : 140 : // Modification 141 404 : void insert(const key_type key, const mapped_type& value) 142 : { 143 404 : if (key >= m_BufferCapacity) // do we need to resize buffer? 144 : { 145 0 : size_t newCapacity = m_BufferCapacity + 4096; 146 0 : while (key >= newCapacity) newCapacity += 4096; 147 : // always allocate +1 behind the scenes, because end() must have a 0xFFFFFFFF key 148 0 : value_type* mem = (value_type*)realloc(m_Buffer, sizeof(value_type) * (newCapacity + 1)); 149 0 : if (!mem) 150 : { 151 0 : debug_warn("EntityMap::insert() realloc failed! Out of memory."); 152 0 : throw std::bad_alloc(); // fail to expand and insert 153 : } 154 0 : m_BufferCapacity = newCapacity; 155 0 : m_Buffer = mem; 156 0 : goto fill_gaps; 157 : } 158 404 : else if (key > m_BufferSize) // weird insert far beyond the end 159 : { 160 16 : fill_gaps: 161 : // set all entity id's to INVALID_ENTITY inside the new range 162 525 : for (size_t i = m_BufferSize; i <= key; ++i) 163 509 : m_Buffer[i].first = INVALID_ENTITY; 164 16 : m_BufferSize = key; // extend the new size 165 : } 166 : 167 404 : value_type& item = m_Buffer[key]; 168 404 : key_type oldKey = item.first; 169 404 : item.first = key; 170 404 : if (key == m_BufferSize) // push_back 171 : { 172 398 : ++m_BufferSize; // expand 173 398 : ++m_Count; 174 398 : new (&item.second) mapped_type(value); // copy ctor to init 175 398 : m_Buffer[m_BufferSize].first = 0xFFFFFFFF; // ensure end() always has 0xFFFFFFFF 176 : } 177 6 : else if(!item.first) // insert new to middle 178 : { 179 0 : ++m_Count; 180 0 : new (&item.second) mapped_type(value); // copy ctor to init 181 : } 182 : else // set existing value 183 : { 184 6 : if (oldKey == INVALID_ENTITY) 185 4 : m_Count++; 186 6 : item.second = value; // overwrite existing 187 : } 188 404 : } 189 : 190 61 : void erase(iterator it) 191 : { 192 61 : value_type* ptr = it.val; 193 60 : if (ptr->first != INVALID_ENTITY) 194 : { 195 61 : ptr->first = INVALID_ENTITY; 196 61 : ptr->second.~T(); // call dtor 197 61 : --m_Count; 198 : } 199 : } 200 5 : void erase(const entity_id_t key) 201 : { 202 5 : if (key < m_BufferSize) 203 : { 204 4 : value_type* ptr = m_Buffer + key; 205 4 : if (ptr->first != INVALID_ENTITY) 206 : { 207 4 : ptr->first = INVALID_ENTITY; 208 4 : ptr->second.~T(); // call dtor 209 4 : --m_Count; 210 : } 211 : } 212 : } 213 1 : inline void clear() 214 : { 215 : // orphan whole range 216 1 : value_type* ptr = m_Buffer; 217 1 : value_type* end = m_Buffer + m_BufferSize; 218 12 : for (; ptr != end; ++ptr) 219 : { 220 10 : if (ptr->first != INVALID_ENTITY) 221 : { 222 8 : ptr->first = INVALID_ENTITY; 223 8 : ptr->second.~T(); // call dtor 224 : } 225 : } 226 1 : m_Count = 0; // no more valid entities 227 : } 228 : 229 : // Operations 230 127912 : inline iterator find(const entity_id_t key) 231 : { 232 127912 : if (key < m_BufferSize) // is this key in the range of existing entitites? 233 : { 234 127874 : value_type* ptr = m_Buffer + key; 235 127874 : if (ptr->first != INVALID_ENTITY) 236 : return ptr; 237 : } 238 526 : return m_Buffer + m_BufferSize; // return iterator end() 239 : } 240 30406 : inline const_iterator find(const entity_id_t key) const 241 : { 242 30406 : if (key < m_BufferSize) // is this key in the range of existing entitites? 243 : { 244 30406 : const value_type* ptr = m_Buffer + key; 245 30406 : if (ptr->first != INVALID_ENTITY) 246 : return ptr; 247 : } 248 0 : return m_Buffer + m_BufferSize; // return iterator end() 249 : } 250 : inline size_t count(const entity_id_t key) const 251 : { 252 : if (key < m_BufferSize) 253 : { 254 : if (m_Buffer[key].first != INVALID_ENTITY) 255 : return 1; 256 : } 257 : return 0; 258 : } 259 : }; 260 : 261 : template<typename T> 262 : struct SerializeHelper<EntityMap<T>> 263 : { 264 18 : void operator()(ISerializer& serialize, const char* /*name*/, EntityMap<T>& value) 265 : { 266 18 : size_t len = value.size(); 267 18 : serialize.NumberU32_Unbounded("length", static_cast<std::uint32_t>(len)); 268 18 : size_t count = 0; 269 1050 : for (typename EntityMap<T>::iterator it = value.begin(); it != value.end(); ++it) 270 : { 271 1032 : serialize.NumberU32_Unbounded("key", it->first); 272 1032 : Serializer(serialize, "value", it->second); 273 1032 : count++; 274 : } 275 : // test to see if the entityMap count wasn't wrong 276 : // (which causes a crashing deserialisation) 277 18 : ENSURE(count == len); 278 18 : } 279 : 280 0 : void operator()(IDeserializer& deserialize, const char* /*name*/, EntityMap<T>& value) 281 : { 282 0 : value.clear(); 283 : uint32_t len; 284 0 : deserialize.NumberU32_Unbounded("length", len); 285 0 : for (size_t i = 0; i < len; ++i) 286 : { 287 : entity_id_t k; 288 0 : T v; 289 0 : deserialize.NumberU32_Unbounded("key", k); 290 0 : Serializer(deserialize, "value", v); 291 0 : value.insert(k, v); 292 : } 293 0 : } 294 : }; 295 : 296 : 297 : #endif