File: EntityMap.h

package info (click to toggle)
0ad 0.0.26-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 130,460 kB
  • sloc: cpp: 261,824; ansic: 198,392; javascript: 19,067; python: 14,557; sh: 7,629; perl: 4,072; xml: 849; makefile: 741; java: 533; ruby: 229; php: 190; pascal: 30; sql: 21; tcl: 4
file content (282 lines) | stat: -rw-r--r-- 7,693 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* Copyright (C) 2013 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef INCLUDED_ENTITYMAP
#define INCLUDED_ENTITYMAP

#include "Entity.h"

#include "simulation2/serialization/SerializeTemplates.h"

/**
 * A fast replacement for map<entity_id_t, T>.
 * We make the following assumptions:
 *  - entity id's (keys) are unique
 *  - modifications (add / delete) are far less frequent then look-ups
 *  - preformance for iteration is important
 */
template<class T> class EntityMap
{
private:
	EntityMap(const EntityMap&);			// non-copyable
	EntityMap& operator=(const EntityMap&); // non-copyable

public:
	typedef entity_id_t key_type;
	typedef T mapped_type;
	template<class K, class V> struct key_val {
		typedef K first_type;
		typedef V second_type;
		K first;
		V second;
	};
	typedef key_val<entity_id_t, T> value_type;

private:
	size_t m_BufferSize;		// number of elements in the buffer
	size_t m_BufferCapacity;	// capacity of the buffer
	value_type* m_Buffer;		// vector with all the mapped key-value pairs

	size_t m_Count;				// number of 'valid' entity id's

public:

	inline EntityMap() : m_BufferSize(1), m_BufferCapacity(4096), m_Count(0)
	{
		// for entitymap we allocate the buffer right away
		// with first element in buffer being the Invalid Entity
		m_Buffer = (value_type*)malloc(sizeof(value_type) * (m_BufferCapacity + 1));

		// create the first element:
		m_Buffer[0].first = INVALID_ENTITY;
		m_Buffer[1].first = 0xFFFFFFFF; // ensure end() always has 0xFFFFFFFF
	}
	inline ~EntityMap()
	{
		free(m_Buffer);
	}

	// Iterators
	template<class U> struct _iter : public std::iterator<std::forward_iterator_tag, U>
	{
		U* val;
		inline _iter(U* init) : val(init) {}
		inline U& operator*() { return *val; }
		inline U* operator->() { return val; }
		inline _iter& operator++() // ++it
		{
			++val;
			while (val->first == INVALID_ENTITY) ++val; // skip any invalid entities
			return *this;
		}
		inline _iter& operator++(int) // it++
		{
			U* ptr = val;
			++val;
			while (val->first == INVALID_ENTITY) ++val; // skip any invalid entities
			return ptr;
		}
		inline bool operator==(_iter other) { return val == other.val; }
		inline bool operator!=(_iter other) { return val != other.val; }
		inline operator _iter<U const>() const { return _iter<U const>(val); }
	};

	typedef _iter<value_type> iterator;
	typedef _iter<value_type const> const_iterator;

	inline iterator begin()
	{
		value_type* ptr = m_Buffer + 1; // skip the first INVALID_ENTITY
		while (ptr->first == INVALID_ENTITY) ++ptr; // skip any other invalid entities
		return ptr;
	}
	inline iterator end()
	{
		return iterator(m_Buffer + m_BufferSize);
	}
	inline const_iterator begin() const
	{
		value_type* ptr = m_Buffer + 1; // skip the first INVALID_ENTITY
		while (ptr->first == INVALID_ENTITY) ++ptr; // skip any other invalid entities
		return ptr;
	}
	inline const_iterator end() const
	{
		return const_iterator(m_Buffer + m_BufferSize);
	}

	// Size
	inline bool empty() const { return m_Count == 0; }
	inline size_t size() const { return m_Count; }

	// Modification
	void insert(const key_type key, const mapped_type& value)
	{
		if (key >= m_BufferCapacity) // do we need to resize buffer?
		{
			size_t newCapacity = m_BufferCapacity + 4096;
			while (key >= newCapacity) newCapacity += 4096;
			// always allocate +1 behind the scenes, because end() must have a 0xFFFFFFFF key
			value_type* mem = (value_type*)realloc(m_Buffer, sizeof(value_type) * (newCapacity + 1));
			if (!mem)
			{
				debug_warn("EntityMap::insert() realloc failed! Out of memory.");
				throw std::bad_alloc(); // fail to expand and insert
			}
			m_BufferCapacity = newCapacity;
			m_Buffer = mem;
			goto fill_gaps;
		}
		else if (key > m_BufferSize) // weird insert far beyond the end
		{
fill_gaps:
			// set all entity id's to INVALID_ENTITY inside the new range
			for (size_t i = m_BufferSize; i <= key; ++i)
				m_Buffer[i].first = INVALID_ENTITY;
			m_BufferSize = key; // extend the new size
		}

		value_type& item = m_Buffer[key];
		key_type oldKey = item.first;
		item.first = key;
		if (key == m_BufferSize) // push_back
		{
			++m_BufferSize; // expand
			++m_Count;
			new (&item.second) mapped_type(value); // copy ctor to init
			m_Buffer[m_BufferSize].first = 0xFFFFFFFF; // ensure end() always has 0xFFFFFFFF
		}
		else if(!item.first) // insert new to middle
		{
			++m_Count;
			new (&item.second) mapped_type(value); // copy ctor to init
		}
		else // set existing value
		{
			if (oldKey == INVALID_ENTITY)
				m_Count++;
			item.second = value; // overwrite existing
		}
	}

	void erase(iterator it)
	{
		value_type* ptr = it.val;
		if (ptr->first != INVALID_ENTITY)
		{
			ptr->first = INVALID_ENTITY;
			ptr->second.~T(); // call dtor
			--m_Count;
		}
	}
	void erase(const entity_id_t key)
	{
		if (key < m_BufferSize)
		{
			value_type* ptr = m_Buffer + key;
			if (ptr->first != INVALID_ENTITY)
			{
				ptr->first = INVALID_ENTITY;
				ptr->second.~T(); // call dtor
				--m_Count;
			}
		}
	}
	inline void clear()
	{
		// orphan whole range
		value_type* ptr = m_Buffer;
		value_type* end = m_Buffer + m_BufferSize;
		for (; ptr != end; ++ptr)
		{
			if (ptr->first != INVALID_ENTITY)
			{
				ptr->first = INVALID_ENTITY;
				ptr->second.~T(); // call dtor
			}
		}
		m_Count = 0; // no more valid entities
	}

	// Operations
	inline iterator find(const entity_id_t key)
	{
		if (key < m_BufferSize) // is this key in the range of existing entitites?
		{
			value_type* ptr = m_Buffer + key;
			if (ptr->first != INVALID_ENTITY)
				return ptr;
		}
		return m_Buffer + m_BufferSize; // return iterator end()
	}
	inline const_iterator find(const entity_id_t key) const
	{
		if (key < m_BufferSize) // is this key in the range of existing entitites?
		{
			const value_type* ptr = m_Buffer + key;
			if (ptr->first != INVALID_ENTITY)
				return ptr;
		}
		return m_Buffer + m_BufferSize; // return iterator end()
	}
	inline size_t count(const entity_id_t key) const
	{
		if (key < m_BufferSize)
		{
			if (m_Buffer[key].first != INVALID_ENTITY)
				return 1;
		}
		return 0;
	}
};

template<typename T>
struct SerializeHelper<EntityMap<T>>
{
	void operator()(ISerializer& serialize, const char* UNUSED(name), EntityMap<T>& value)
	{
		size_t len = value.size();
		serialize.NumberU32_Unbounded("length", (u32)len);
		size_t count = 0;
		for (typename EntityMap<T>::iterator it = value.begin(); it != value.end(); ++it)
		{
			serialize.NumberU32_Unbounded("key", it->first);
			Serializer(serialize, "value", it->second);
			count++;
		}
		// test to see if the entityMap count wasn't wrong
		// (which causes a crashing deserialisation)
		ENSURE(count == len);
	}

	void operator()(IDeserializer& deserialize, const char* UNUSED(name), EntityMap<T>& value)
	{
		value.clear();
		uint32_t len;
		deserialize.NumberU32_Unbounded("length", len);
		for (size_t i = 0; i < len; ++i)
		{
			entity_id_t k;
			T v;
			deserialize.NumberU32_Unbounded("key", k);
			Serializer(deserialize, "value", v);
			value.insert(k, v);
		}
	}
};


#endif