aboutsummaryrefslogtreecommitdiff
path: root/engines/wintermute/coll_templ.h
blob: c9e85253e5acf0f0234bc95e354f7f08604f6902 (plain)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program 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.

 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

/*
 * This file is based on WME Lite.
 * http://dead-code.org/redir.php?target=wmelite
 * Copyright (c) 2011 Jan Nedoma
 */

#ifndef WINTERMUTE_COLL_TEMPL_H
#define WINTERMUTE_COLL_TEMPL_H


#include <new>
#include "engines/wintermute/Base/BPersistMgr.h"

namespace WinterMute {

/////////////////////////////////////////////////////////////////////////////
template<class TYPE>
inline void DCConstructElements(TYPE *pElements, int nCount) {
	// first do bit-wise zero initialization
	memset((void *)pElements, 0, nCount * sizeof(TYPE));

	// then call the constructor(s)
	for (; nCount--; pElements++)
		::new((void *)pElements) TYPE;
}

/////////////////////////////////////////////////////////////////////////////
template<class TYPE>
inline void DCDestructElements(TYPE *pElements, int nCount) {
	// call the destructor(s)
	for (; nCount--; pElements++)
		pElements->~TYPE();
}

/////////////////////////////////////////////////////////////////////////////
template<class TYPE>
inline void DCCopyElements(TYPE *pDest, const TYPE *pSrc, int nCount) {
	// default is element-copy using assignment
	while (nCount--)
		*pDest++ = *pSrc++;
}

/////////////////////////////////////////////////////////////////////////////
template<class TYPE, class ARG_TYPE>
bool DCCompareElements(const TYPE *pElement1, const ARG_TYPE *pElement2) {
	return *pElement1 == *pElement2;
}

//class CBPersistMgr;

/////////////////////////////////////////////////////////////////////////////
// CBArray<TYPE, ARG_TYPE>
/////////////////////////////////////////////////////////////////////////////
template<class TYPE, class ARG_TYPE>
class CBArray {
public:
// Construction
	CBArray();

// Attributes
	int GetSize() const;
	int GetUpperBound() const;
	void SetSize(int nNewSize, int nGrowBy = -1);

// Operations
	// Clean up
	void FreeExtra();
	void RemoveAll();
	HRESULT persist(CBPersistMgr *persistMgr);

	// Accessing elements
	TYPE GetAt(int nIndex) const;
	void SetAt(int nIndex, ARG_TYPE newElement);
	TYPE &ElementAt(int nIndex);

	// Direct Access to the element data (may return NULL)
	const TYPE *GetData() const;
	TYPE *GetData();

	// Potentially growing the array
	void SetAtGrow(int nIndex, ARG_TYPE newElement);
	int Add(ARG_TYPE newElement);
	int Append(const CBArray &src);
	void Copy(const CBArray &src);

	// overloaded operator helpers
	TYPE operator[](int nIndex) const;
	TYPE &operator[](int nIndex);

	// Operations that move elements around
	void InsertAt(int nIndex, ARG_TYPE newElement, int nCount = 1);
	void RemoveAt(int nIndex, int nCount = 1);
	void InsertAt(int nStartIndex, CBArray *pNewArray);

// Implementation
protected:
	TYPE *_pData;   // the actual array of data
	int _nSize;     // # of elements (upperBound - 1)
	int _nMaxSize;  // max allocated
	int _nGrowBy;   // grow amount

public:
	~CBArray();
};

/////////////////////////////////////////////////////////////////////////////
// CBArray<TYPE, ARG_TYPE> inline functions
/////////////////////////////////////////////////////////////////////////////
template<class TYPE, class ARG_TYPE>
inline int CBArray<TYPE, ARG_TYPE>::GetSize() const {
	return _nSize;
}
template<class TYPE, class ARG_TYPE>
inline int CBArray<TYPE, ARG_TYPE>::GetUpperBound() const {
	return _nSize - 1;
}
template<class TYPE, class ARG_TYPE>
inline void CBArray<TYPE, ARG_TYPE>::RemoveAll() {
	SetSize(0, -1);
}
template<class TYPE, class ARG_TYPE>
inline TYPE CBArray<TYPE, ARG_TYPE>::GetAt(int nIndex) const {
	return _pData[nIndex];
}
template<class TYPE, class ARG_TYPE>
inline void CBArray<TYPE, ARG_TYPE>::SetAt(int nIndex, ARG_TYPE newElement) {
	_pData[nIndex] = newElement;
}
template<class TYPE, class ARG_TYPE>
inline TYPE &CBArray<TYPE, ARG_TYPE>::ElementAt(int nIndex) {
	return _pData[nIndex];
}
template<class TYPE, class ARG_TYPE>
inline const TYPE *CBArray<TYPE, ARG_TYPE>::GetData() const {
	return (const TYPE *)_pData;
}
template<class TYPE, class ARG_TYPE>
inline TYPE *CBArray<TYPE, ARG_TYPE>::GetData() {
	return (TYPE *)_pData;
}
template<class TYPE, class ARG_TYPE>
inline int CBArray<TYPE, ARG_TYPE>::Add(ARG_TYPE newElement) {
	int nIndex = _nSize;
	SetAtGrow(nIndex, newElement);
	return nIndex;
}
template<class TYPE, class ARG_TYPE>
inline TYPE CBArray<TYPE, ARG_TYPE>::operator[](int nIndex) const {
	return GetAt(nIndex);
}
template<class TYPE, class ARG_TYPE>
inline TYPE &CBArray<TYPE, ARG_TYPE>::operator[](int nIndex) {
	return ElementAt(nIndex);
}

/////////////////////////////////////////////////////////////////////////////
// CBArray<TYPE, ARG_TYPE> out-of-line functions
/////////////////////////////////////////////////////////////////////////////
template<class TYPE, class ARG_TYPE>
CBArray<TYPE, ARG_TYPE>::CBArray() {
	_pData = NULL;
	_nSize = _nMaxSize = _nGrowBy = 0;
}

/////////////////////////////////////////////////////////////////////////////
template<class TYPE, class ARG_TYPE>
CBArray<TYPE, ARG_TYPE>::~CBArray() {
	if (_pData != NULL) {
		DCDestructElements<TYPE>(_pData, _nSize);
		delete[](byte *)_pData;
	}
}

/////////////////////////////////////////////////////////////////////////////
template<class TYPE, class ARG_TYPE>
void CBArray<TYPE, ARG_TYPE>::SetSize(int nNewSize, int nGrowBy) {
	if (nGrowBy != -1)
		_nGrowBy = nGrowBy;  // set new size

	if (nNewSize == 0) {
		// shrink to nothing
		if (_pData != NULL) {
			DCDestructElements<TYPE>(_pData, _nSize);
			delete[](byte *)_pData;
			_pData = NULL;
		}
		_nSize = _nMaxSize = 0;
	} else if (_pData == NULL) {
		// create one with exact size
		_pData = (TYPE *) new byte[nNewSize * sizeof(TYPE)];
		DCConstructElements<TYPE>(_pData, nNewSize);
		_nSize = _nMaxSize = nNewSize;
	} else if (nNewSize <= _nMaxSize) {
		// it fits
		if (nNewSize > _nSize) {
			// initialize the new elements
			DCConstructElements<TYPE>(&_pData[_nSize], nNewSize - _nSize);
		} else if (_nSize > nNewSize) {
			// destroy the old elements
			DCDestructElements<TYPE>(&_pData[nNewSize], _nSize - nNewSize);
		}
		_nSize = nNewSize;
	} else {
		// otherwise, grow array
		nGrowBy = _nGrowBy;
		if (nGrowBy == 0) {
			// heuristically determine growth when nGrowBy == 0
			//  (this avoids heap fragmentation in many situations)
			nGrowBy = _nSize / 8;
			nGrowBy = (nGrowBy < 4) ? 4 : ((nGrowBy > 1024) ? 1024 : nGrowBy);
		}
		int nNewMax;
		if (nNewSize < _nMaxSize + nGrowBy)
			nNewMax = _nMaxSize + nGrowBy;  // granularity
		else
			nNewMax = nNewSize;  // no slush

		TYPE *pNewData = (TYPE *) new byte[nNewMax * sizeof(TYPE)];

		// copy new data from old
		memcpy(pNewData, _pData, _nSize * sizeof(TYPE));

		// construct remaining elements
		DCConstructElements<TYPE>(&pNewData[_nSize], nNewSize - _nSize);

		// get rid of old stuff (note: no destructors called)
		delete[](byte *)_pData;
		_pData = pNewData;
		_nSize = nNewSize;
		_nMaxSize = nNewMax;
	}
}

/////////////////////////////////////////////////////////////////////////////
template<class TYPE, class ARG_TYPE>
int CBArray<TYPE, ARG_TYPE>::Append(const CBArray &src) {
	int nOldSize = _nSize;
	SetSize(_nSize + src._nSize);
	DCCopyElements<TYPE>(_pData + nOldSize, src._pData, src._nSize);
	return nOldSize;
}

/////////////////////////////////////////////////////////////////////////////
template<class TYPE, class ARG_TYPE>
void CBArray<TYPE, ARG_TYPE>::Copy(const CBArray &src) {
	SetSize(src._nSize);
	DCCopyElements<TYPE>(_pData, src._pData, src._nSize);
}

/////////////////////////////////////////////////////////////////////////////
template<class TYPE, class ARG_TYPE>
void CBArray<TYPE, ARG_TYPE>::FreeExtra() {
	if (_nSize != _nMaxSize) {
		// shrink to desired size
		TYPE *pNewData = NULL;
		if (_nSize != 0) {
			pNewData = (TYPE *) new byte[_nSize * sizeof(TYPE)];
			// copy new data from old
			memcpy(pNewData, _pData, _nSize * sizeof(TYPE));
		}

		// get rid of old stuff (note: no destructors called)
		delete[](byte *)_pData;
		_pData = pNewData;
		_nMaxSize = _nSize;
	}
}

/////////////////////////////////////////////////////////////////////////////
template<class TYPE, class ARG_TYPE>
void CBArray<TYPE, ARG_TYPE>::SetAtGrow(int nIndex, ARG_TYPE newElement) {
	if (nIndex >= _nSize)
		SetSize(nIndex + 1, -1);
	_pData[nIndex] = newElement;
}

/////////////////////////////////////////////////////////////////////////////
template<class TYPE, class ARG_TYPE>
void CBArray<TYPE, ARG_TYPE>::InsertAt(int nIndex, ARG_TYPE newElement, int nCount /*=1*/) {
	if (nIndex >= _nSize) {
		// adding after the end of the array
		SetSize(nIndex + nCount, -1);   // grow so nIndex is valid
	} else {
		// inserting in the middle of the array
		int nOldSize = _nSize;
		SetSize(_nSize + nCount, -1);  // grow it to new size
		// destroy intial data before copying over it
		DCDestructElements<TYPE>(&_pData[nOldSize], nCount);
		// shift old data up to fill gap
		memmove(&_pData[nIndex + nCount], &_pData[nIndex],
		        (nOldSize - nIndex) * sizeof(TYPE));

		// re-init slots we copied from
		DCConstructElements<TYPE>(&_pData[nIndex], nCount);
	}

	// insert new value in the gap
	while (nCount--)
		_pData[nIndex++] = newElement;
}

/////////////////////////////////////////////////////////////////////////////
template<class TYPE, class ARG_TYPE>
void CBArray<TYPE, ARG_TYPE>::RemoveAt(int nIndex, int nCount) {
	// just remove a range
	int nMoveCount = _nSize - (nIndex + nCount);
	DCDestructElements<TYPE>(&_pData[nIndex], nCount);
	if (nMoveCount)
		memcpy(&_pData[nIndex], &_pData[nIndex + nCount],
		       nMoveCount * sizeof(TYPE));
	_nSize -= nCount;
}

/////////////////////////////////////////////////////////////////////////////
template<class TYPE, class ARG_TYPE>
void CBArray<TYPE, ARG_TYPE>::InsertAt(int nStartIndex, CBArray *pNewArray) {
	if (pNewArray->GetSize() > 0) {
		InsertAt(nStartIndex, pNewArray->GetAt(0), pNewArray->GetSize());
		for (int i = 0; i < pNewArray->GetSize(); i++)
			SetAt(nStartIndex + i, pNewArray->GetAt(i));
	}
}


/////////////////////////////////////////////////////////////////////////////
template<class TYPE, class ARG_TYPE>
HRESULT CBArray<TYPE, ARG_TYPE>::persist(CBPersistMgr *persistMgr) {
	int i, j;
	if (persistMgr->_saving) {
		j = GetSize();
		persistMgr->transfer("ArraySize", &j);
		for (i = 0; i < j; i++) {
			ARG_TYPE obj = GetAt(i);
			persistMgr->transfer("", &obj);
		}
	} else {
		SetSize(0, -1);
		persistMgr->transfer("ArraySize", &j);
		for (i = 0; i < j; i++) {
			ARG_TYPE obj;
			persistMgr->transfer("", &obj);
			Add(obj);
		}
	}
	return S_OK;
}

} // end of namespace WinterMute

#endif // COLL_TEMPL_H