File: lc_array.h

package info (click to toggle)
leocad 18.02-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 14,772 kB
  • sloc: cpp: 46,325; xml: 2,856; sh: 33; makefile: 14
file content (269 lines) | stat: -rw-r--r-- 3,771 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
#pragma once

template <class T>
class lcArray
{
public:
	typedef int (*lcArrayCompareFunc)(const T& a, const T& b);

	lcArray(int Size = 0, int Grow = 16)
	{
		mData = nullptr;
		mLength = 0;
		mAlloc = 0;
		mGrow = Grow;

		if (Size != 0)
			AllocGrow(Size);
	}

	lcArray(const lcArray<T>& Array)
	{
		mData = nullptr;
		*this = Array;
	}

	~lcArray()
	{
		delete[] mData;
	}

	lcArray<T>& operator=(const lcArray<T>& Array)
	{
		mLength = Array.mLength;
		mAlloc = Array.mAlloc;
		mGrow = Array.mGrow;

		delete[] mData;
		mData = new T[mAlloc];

		for (int i = 0; i < mLength; i++)
			mData[i] = Array.mData[i];

		return *this;
	}

	lcArray<T>& operator+=(const lcArray<T>& Array)
	{
		AllocGrow(Array.mLength);

		for (int i = 0; i < Array.mLength; i++)
			mData[mLength + i] = Array.mData[i];

		mLength += Array.mLength;
		return *this;
	}

	const T& operator[](int Index) const
	{
		return mData[Index];
	}

	T& operator[](int Index)
	{
		return mData[Index];
	}

	bool operator==(const lcArray<T>& Array) const
	{
		if (mLength != Array.mLength)
			return false;

		for (int i = 0; i < mLength; i++)
			if (mData[i] != Array.mData[i])
				return false;

		return true;
	}

	T* begin()
	{
		return &mData[0];
	}

	T* end()
	{
		return &mData[0] + mLength;
	}

	const T* begin() const
	{
		return &mData[0];
	}

	const T* end() const
	{
		return &mData[0] + mLength;
	}

	bool IsEmpty() const
	{
		return mLength == 0;
	}

	int GetSize() const
	{
		return mLength;
	}

	void SetSize(int NewSize)
	{
		if (NewSize > mAlloc)
			AllocGrow(NewSize - mLength);

		mLength = NewSize;
	}

	void SetGrow(int Grow)
	{
		if (Grow)
			mGrow = Grow;
	}

	void AllocGrow(int Grow)
	{
		if ((mLength + Grow) > mAlloc)
		{
			int NewSize = ((mLength + Grow + mGrow - 1) / mGrow) * mGrow;
			T* NewData = new T[NewSize];

			for (int i = 0; i < mLength; i++)
				NewData[i] = mData[i];

			delete[] mData;
			mData = NewData;
			mAlloc = NewSize;
		}
	}

	void Add(const T& NewItem)
	{
		AllocGrow(1);
		mData[mLength++] = NewItem;
	}

	T& Add()
	{
		AllocGrow(1);
		return mData[mLength++];
	}

	void AddSorted(const T& Obj, lcArrayCompareFunc CompareFunc)
	{
		for (int i = 0; i < mLength; i++)
		{
			if (CompareFunc(Obj, mData[i]) < 0)
			{
				InsertAt(i, Obj);
				return;
			}
		}

		Add(Obj);
	}

	T& InsertAt(int Index)
	{
		if (Index >= mLength)
			AllocGrow(Index - mLength + 1);
		else
			AllocGrow(1);

		mLength++;
		for (int i = mLength - 1; i > Index; i--)
			mData[i] = mData[i - 1];

		return mData[Index];
	}

	void InsertAt(int Index, const T& NewItem)
	{
		if (Index >= mLength)
			AllocGrow(Index - mLength + 1);
		else
			AllocGrow(1);

		mLength++;
		for (int i = mLength - 1; i > Index; i--)
			mData[i] = mData[i - 1];

		mData[Index] = NewItem;
	}

	void RemoveIndex(int Index)
	{
		mLength--;

		for (int i = Index; i < mLength; i++)
			mData[i] = mData[i + 1];
	}

	void Remove(const T& Item)
	{
		for (int i = 0; i < mLength; i++)
		{
			if (mData[i] == Item)
			{
				RemoveIndex(i);
				return;
			}
		}
	}

	void RemoveAll()
	{
		mLength = 0;
	}

	void DeleteAll()
	{
		for (int i = 0; i < mLength; i++)
			delete mData[i];

		mLength = 0;
	}

	int FindIndex(const T& Item) const
	{
		for (int i = 0; i < mLength; i++)
			if (mData[i] == Item)
				return i;

		return -1;
	}

	void Sort(lcArrayCompareFunc CompareFunc)
	{
		if (mLength <= 1)
			return;

		int i = 1;
		bool Flipped;

		do
		{
			Flipped = false;

			for (int j = mLength - 1; j >= i; --j)
			{
				T& a = mData[j];
				T& b = mData[j - 1];

				if (CompareFunc(b, a) > 0)
				{
					T Tmp = b;
					mData[j - 1] = a;
					mData[j] = Tmp;
					Flipped = true;
				}
			}
		} while ((++i < mLength) && Flipped);
	}

protected:
	T* mData;
	int mLength;
	int mAlloc;
	int mGrow;
};