summaryrefslogtreecommitdiff
path: root/plugins/ClientChangeNotify/src/CommonLibs/TMyArray.h
blob: 68ca908fe1c7052e3fde0125146244599a3dec27 (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
/*
	TMyArray.h - TMyArray template
	Copyright (c) 2005-2008 Chervov Dmitry

    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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#pragma once

#include <stdlib.h>

#define ARRAY_GROWBY 4
// if there is more than ARRAY_FREETHRESHOLD free array elements, then we're reallocating the array
#define ARRAY_FREETHRESHOLD 16

template <class T, class ARG_T = const T&, int GrowBy = ARRAY_GROWBY, int FreeThreshold = ARRAY_FREETHRESHOLD>
class TMyArray
{
public:
	TMyArray();
	TMyArray(const TMyArray<T, ARG_T, GrowBy, FreeThreshold> &A);
	~TMyArray();

	int GetSize() const;
	T* GetData() const;
	int AddElem(ARG_T pElem);
	int Add(const T *pItems, int nItems);
	void InsertElem(ARG_T pElem, int nInsertAt);
	void MoveElem(int nIndex, int nMoveTo);
	T& SetAtGrow(int nIndex);
	int Find(ARG_T pElem); // returns an index of the specified item, or -1 if the array doesn't contain the item
	int BinarySearch(int (*CmpFunc)(ARG_T pItem, LPARAM lParam), LPARAM lParam, int *pIndex = NULL); // returns an index of the specified item, or -1 if the array doesn't contain the item;
	// also it's possible to specify pIndex so that even if the array doesn't contain the item, the function will set *pIndex to a position where the item can be inserted;
	// CmpFunc must return -1, 0 and 1 depending whether pItem is lesser, equal or greater than needed;
	// BinarySearch() requires the array to be sorted in an ascending order
	void RemoveElem(int nIndex, int nItems = 1);
	void RemoveAll();
	const T& operator[](int nIndex) const;
	T& operator[](int nIndex);
	TMyArray<T, ARG_T, GrowBy, FreeThreshold>& operator = (const TMyArray<T, ARG_T, GrowBy, FreeThreshold> &A);
	TMyArray<T, ARG_T, GrowBy, FreeThreshold>& operator += (const TMyArray<T, ARG_T, GrowBy, FreeThreshold> &A);

private:
	int SetAllocNum(int nNewAllocNum);

	T* pData;
	int nElemNum;
	int nAllocNum;
};

template <class T, class ARG_T, int GrowBy, int FreeThreshold>
TMyArray<T, ARG_T, GrowBy, FreeThreshold>::TMyArray()
{
	nElemNum = 0;
	nAllocNum = 0;
	pData = NULL;
}

template <class T, class ARG_T, int GrowBy, int FreeThreshold>
TMyArray<T, ARG_T, GrowBy, FreeThreshold>::TMyArray(const TMyArray<T, ARG_T, GrowBy, FreeThreshold> &A)//: TMyArray<T, ARG_T>()
{
	nElemNum = 0;
	nAllocNum = 0;
	pData = NULL;
	*this = A;
}

template <class T, class ARG_T, int GrowBy, int FreeThreshold>
TMyArray<T, ARG_T, GrowBy, FreeThreshold>::~TMyArray()
{
	RemoveAll();
}

template <class T, class ARG_T, int GrowBy, int FreeThreshold>
__forceinline int TMyArray<T, ARG_T, GrowBy, FreeThreshold>::GetSize() const
{
	return nElemNum;
}

template <class T, class ARG_T, int GrowBy, int FreeThreshold>
__forceinline T* TMyArray<T, ARG_T, GrowBy, FreeThreshold>::GetData() const
{
	return pData;
}

template <class T, class ARG_T, int GrowBy, int FreeThreshold>
__forceinline int TMyArray<T, ARG_T, GrowBy, FreeThreshold>::SetAllocNum(int nNewAllocNum)
{
	_ASSERT(nNewAllocNum >= nElemNum);
	T*pNewData = (nNewAllocNum) ? (T*)malloc(sizeof(T) * nNewAllocNum) : NULL;
	if (pData)
	{
		if (pNewData)
		{
			memcpy(pNewData, pData, sizeof(T) * nElemNum);
		}
		free(pData);
	}
	pData = pNewData;
	if (!pNewData)
	{
		nAllocNum = 0;
		return -1; // not enough memory?
	} else
	{
		nAllocNum = nNewAllocNum;
		return 0; // everything's ok
	}
}

template <class T, class ARG_T, int GrowBy, int FreeThreshold>
__forceinline int TMyArray<T, ARG_T, GrowBy, FreeThreshold>::AddElem(ARG_T pElem)
{
	if (nElemNum >= nAllocNum)
	{ // reallocate memory to fit new element
		SetAllocNum(nAllocNum + GrowBy);
	}
	memset(pData + nElemNum, 0, sizeof(T));
	pData[nElemNum] = pElem;
	return nElemNum++;
}

template <class T, class ARG_T, int GrowBy, int FreeThreshold>
__forceinline int TMyArray<T, ARG_T, GrowBy, FreeThreshold>::Add(const T *pItems, int nItems)
{
	if (nElemNum + nItems > nAllocNum)
	{ // reallocate memory to fit new items
		SetAllocNum(nAllocNum + nElemNum + nItems - 1 - ((nElemNum + nItems - 1) % GrowBy) + GrowBy);
	}
	memset(pData + nElemNum, 0, sizeof(T) * nItems);
	int I;
	for (I = 0; I < nItems; I++)
	{
    pData[nElemNum++] = pItems[I];
	}
	return nElemNum - nItems; // returns an index of the first item added
}

template <class T, class ARG_T, int GrowBy, int FreeThreshold>
__forceinline void TMyArray<T, ARG_T, GrowBy, FreeThreshold>::InsertElem(ARG_T pElem, int nInsertAt)
{
	_ASSERT(nInsertAt >= 0 && nInsertAt <= nElemNum);
	if (nElemNum >= nAllocNum)
	{ // reallocate memory to fit new items
		SetAllocNum(nAllocNum + GrowBy);
	}
	memmove(pData + nInsertAt + 1, pData + nInsertAt, sizeof(T) * (nElemNum - nInsertAt));
	memset(pData + nInsertAt, 0, sizeof(T));
	pData[nInsertAt] = pElem;
	nElemNum++;
}

template <class T, class ARG_T, int GrowBy, int FreeThreshold>
__forceinline void TMyArray<T, ARG_T, GrowBy, FreeThreshold>::MoveElem(int nIndex, int nMoveTo)
{
	_ASSERT(nIndex >= 0 && nIndex < nElemNum && nMoveTo >= 0 && nMoveTo < nElemNum);
	if (nIndex == nMoveTo)
	{
		return; // nothing to do
	}
	char Elem[sizeof(T)];
	memmove(Elem, pData + nIndex, sizeof(T));
	if (nIndex < nMoveTo)
	{
		memmove(pData + nIndex, pData + nIndex + 1, sizeof(T) * (nMoveTo - nIndex));
	} else
	{
		memmove(pData + nMoveTo + 1, pData + nMoveTo, sizeof(T) * (nIndex - nMoveTo));
	}
	memmove(pData + nMoveTo, Elem, sizeof(T));
}

template <class T, class ARG_T, int GrowBy, int FreeThreshold>
__forceinline T& TMyArray<T, ARG_T, GrowBy, FreeThreshold>::SetAtGrow(int nIndex)
{
	_ASSERT(nIndex >= 0);
	if (nIndex < nElemNum)
	{
		return pData[nIndex];
	}
	if (nIndex >= nAllocNum)
	{
		if (SetAllocNum(nIndex - (nIndex % GrowBy) + GrowBy))
		{ // if there was an error
			nElemNum = 0;
			return *pData;
		}
	}
	memset(pData + nElemNum, 0, sizeof(T) * (nIndex - nElemNum + 1));
	nElemNum = nIndex + 1;
	return pData[nIndex];
}

template <class T, class ARG_T, int GrowBy, int FreeThreshold>
__forceinline int TMyArray<T, ARG_T, GrowBy, FreeThreshold>::Find(ARG_T pElem)
{
	int I;
	for (I = 0; I < nElemNum; I++)
	{
		if (pData[I] == pElem)
		{
			return I;
		}
	}
	return -1;
}

template <class T, class ARG_T, int GrowBy, int FreeThreshold>
__forceinline int TMyArray<T, ARG_T, GrowBy, FreeThreshold>::BinarySearch(int (*CmpFunc)(ARG_T pItem, LPARAM lParam), LPARAM lParam, int *pIndex)
{
	int L, R;
	int CmpResult;
	if (!nElemNum)
	{
		if (pIndex)
		{
			*pIndex = -1;
		}
		return -1;
	}
	for (L = 0, R = nElemNum; R - L > 1;)
	{
		int C = (L + R) >> 1; // rounds always to a lesser index if L + R is odd
		CmpResult = CmpFunc(pData[C], lParam); // usually, CmpFunc = pData[C] - lParam; i.e. CmpFunc > 0 when pData[C] is greater than necessary, < 0 when pData[C] is less, and = 0 when pData[C] is the item we search for
		if (CmpResult < 0)
		{
			L = C;
		} else if (CmpResult > 0)
		{
			R = C;
		} else
		{ // CmpResult == 0
			if (pIndex)
			{
				*pIndex = C;
			}
			return C;
		}
	}
	if (pIndex)
	{
		*pIndex = L;
	}
	CmpResult = CmpFunc(pData[L], lParam);
	if (!CmpResult)
	{
		return L;
	}
	if (CmpResult > 0)
	{ // we don't need to check pData[R], as pData[R] > pData[L] > lParam, i.e. there are no suitable item in the array
		return -1;
	}
// CmpResult < 0, we have to check pData[R] too
	if (pIndex)
	{
		*pIndex = R;
	}
	if (R >= nElemNum)
	{
		return -1;
	}
	return CmpFunc(pData[R], lParam) ? -1 : R;
}

template <class T, class ARG_T, int GrowBy, int FreeThreshold>
__forceinline void TMyArray<T, ARG_T, GrowBy, FreeThreshold>::RemoveElem(int nIndex, int nItems)
{
	_ASSERT(nIndex >= 0 && nIndex + nItems <= nElemNum);
	if (!nItems)
	{
		return;
	}
//	delete pData[nIndex];
//	~pData[nIndex];
	int I;
	for (I = nIndex; I < nIndex + nItems; I++)
	{
		(pData + I)->~T();
	}
	memmove(pData + nIndex, pData + nIndex + nItems, sizeof(T) * (nElemNum - nIndex - nItems));
	nElemNum -= nItems;
	if (nAllocNum - nElemNum >= FreeThreshold)
	{
		SetAllocNum(nAllocNum - FreeThreshold);
	}/* else
	{
		memset(pData + nElemNum, 0, sizeof(T));
	}*/
}

template <class T, class ARG_T, int GrowBy, int FreeThreshold>
__forceinline void TMyArray<T, ARG_T, GrowBy, FreeThreshold>::RemoveAll()
{
	int I;
	for (I = 0; I < nElemNum; I++)
	{
		//delete pData[I];
		(pData + I)->~T();
	}
	nElemNum = 0;
	SetAllocNum(0);
}

template <class T, class ARG_T, int GrowBy, int FreeThreshold>
__forceinline const T& TMyArray<T, ARG_T, GrowBy, FreeThreshold>::operator[](int nIndex) const
{
	_ASSERT(nIndex >= 0 && nIndex < nElemNum);
	return pData[nIndex];
}

template <class T, class ARG_T, int GrowBy, int FreeThreshold>
__forceinline T& TMyArray<T, ARG_T, GrowBy, FreeThreshold>::operator[](int nIndex)
{
	_ASSERT(nIndex >= 0 && nIndex < nElemNum);
	return pData[nIndex];
}

template <class T, class ARG_T, int GrowBy, int FreeThreshold>
__forceinline TMyArray<T, ARG_T, GrowBy, FreeThreshold>& TMyArray<T, ARG_T, GrowBy, FreeThreshold>::operator = (const TMyArray<T, ARG_T, GrowBy, FreeThreshold> &A)
{
	RemoveAll();
	int I;
	for (I = 0; I < A.GetSize(); I++)
	{
		AddElem(A[I]);
	}
	return *this;
}

template <class T, class ARG_T, int GrowBy, int FreeThreshold>
__forceinline TMyArray<T, ARG_T, GrowBy, FreeThreshold>& TMyArray<T, ARG_T, GrowBy, FreeThreshold>::operator += (const TMyArray<T, ARG_T, GrowBy, FreeThreshold> &A)
{
	int I;
	for (I = 0; I < A.GetSize(); I++)
	{
		AddElem(A[I]);
	}
	return *this;
}

typedef TMyArray<char, const char&, 1024, 4096> CHARARRAY;