cppcore
A small, portable C++ core library
Loading...
Searching...
No Matches
TArray.h
Go to the documentation of this file.
1/*-----------------------------------------------------------------------------------------------
2The MIT License (MIT)
3
4Copyright (c) 2014-2026 Kim Kulling
5
6Permission is hereby granted, free of charge, to any person obtaining a copy of
7this software and associated documentation files (the "Software"), to deal in
8the Software without restriction, including without limitation the rights to
9use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10the Software, and to permit persons to whom the Software is furnished to do so,
11subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22-----------------------------------------------------------------------------------------------*/
23#pragma once
24
27
28namespace cppcore {
29namespace Details {
30
31//-------------------------------------------------------------------------------------------------
32inline static size_t getGrowing(size_t size) {
33 if (0u == size) {
34 return 0u;
35 }
36
37 if (size < 2048u) {
38 return size * 2u;
39 }
40
41 return 4096u;
42}
43
44} // namespace Details
45
46//-------------------------------------------------------------------------------------------------
53//-------------------------------------------------------------------------------------------------
54template <class T, class TAlloc = TDefaultAllocator<T> >
55class TArray {
56public:
58 using Iterator = T*;
60 using ConstIterator = const T *;
62 using array_size_type = size_t;
63
64public:
66 TArray() = default;
67
71
75
78
81 void add(const T &newValue);
82
86 void add(const T *newValues, array_size_type numItems);
87
91
94 void remove(Iterator it);
95
97 void removeBack();
98
101 void set(const T &value);
102
106
109 T &front();
110
113 T &back();
114
117 const T &back() const;
118
122 void move(array_size_type startIdx, array_size_type endIdx);
123
128
132
137
140 size_t size() const;
141
144 size_t capacity() const;
145
148 bool isEmpty() const;
149
153 Iterator linearSearch(const T &item);
154
156 void clear();
157
162
168
171 T *data() const;
172
175
178
180 bool operator==(const TArray<T, TAlloc> &rOther) const;
181
182private:
183 TAlloc mAllocator;
184 size_t mSize{0u};
185 size_t mCapacity{0u};
186 T *mData{nullptr};
187};
188
189template <class T, class TAlloc>
191 const size_t capa = Details::getGrowing(size);
192 reserve(capa);
193 resize(size);
194}
195
196template <class T, class TAlloc>
198 resize(rhs.mSize);
199 for (size_t i = 0u; i < mSize; ++i) {
200 mData[i] = rhs.mData[i];
201 }
202}
203
204template <class T, class TAlloc>
206 clear();
207}
208
209template <class T, class TAlloc>
210inline void TArray<T, TAlloc>::add(const T &value) {
211 if (mSize + 1 > mCapacity) {
212 const size_t newcapa = Details::getGrowing(mSize + 1);
213 reserve(mCapacity + newcapa);
214 }
215 mData[mSize] = value;
216 ++mSize;
217}
218
219template <class T, class TAlloc>
220inline void TArray<T, TAlloc>::add(const T *newValues, size_t numItems) {
221 if (0 == numItems || newValues == nullptr) {
222 return;
223 }
224
225 const size_t newCapa = mSize + numItems;
226 if (newCapa > mCapacity) {
227 reserve(mCapacity + newCapa);
228 }
229 for (size_t i = 0; i < numItems; ++i) {
230 mData[mSize] = newValues[i];
231 ++mSize;
232 }
233}
234
235template <class T, class TAlloc>
236inline void TArray<T, TAlloc>::remove(size_t index) {
237 assert(index < mSize);
238
239 if (index == mSize - 1) {
240 // Destroy the last index
241 destroy(index);
242 --mSize;
243 } else {
244 // Remove an index in the middle
245 this->move(index + 1, index);
246 }
247}
248
249template <class T, class TAlloc>
251 assert(it < end());
252
253 // Look out for the index
254 long pos = -1;
255 Iterator currentIt = this->begin();
256 for (size_t i = 0; i < mSize; ++i) {
257 if (it == currentIt) {
258 pos = static_cast<long>(i);
259 break;
260 }
261 ++currentIt;
262 }
263
264 // Remove the item at the position
265 if (-1 != pos) {
266 this->remove(pos);
267 }
268}
269
270template <class T, class TAlloc>
272 assert(!isEmpty());
273
274 remove(size() - 1);
275}
276
277template <class T, class TAlloc>
278inline void TArray<T, TAlloc>::set(const T &value) {
279 if (isEmpty()) {
280 return;
281 }
282
283 for (size_t i = 0; i < size(); ++i) {
284 mData[i] = value;
285 }
286}
287
288template <class T, class TAlloc>
289inline void TArray<T, TAlloc>::destroy(size_t index) {
290 T *pElem = &(mData[index]);
291 pElem->~T();
292}
293
294template <class T, class TAlloc>
296 assert(mSize > 0);
297
298 return mData[0];
299}
300
301template <class T, class TAlloc>
303 assert(mSize > 0);
304
305 return (mData[mSize - 1]);
306}
307
308template <class T, class TAlloc>
309inline const T &TArray<T, TAlloc>::back() const {
310 assert(mSize > 0);
311
312 return (mData[mSize - 1]);
313}
314
315template <class T, class TAlloc>
316inline void TArray<T, TAlloc>::move(size_t fromIdx, size_t toIdx) {
317 if (fromIdx == toIdx) {
318 return;
319 }
320
321 const size_t numElements = mSize - fromIdx;
322 const size_t newSize = toIdx + numElements;
323 while (mCapacity < newSize) {
324 resize(mCapacity + Details::getGrowing(newSize - mCapacity));
325 }
326
327 size_t index = 0u;
328 if (fromIdx > toIdx) {
329 for (size_t i = 0; i < numElements; ++i) {
330 mData[toIdx + i] = mData[fromIdx + i];
331 }
332
333 for (size_t i = toIdx; i < fromIdx + 1; --i) {
334 index = i;
335 destroy(index);
336 }
337 } else {
338 for (size_t i = numElements - 1; i != 0; --i) {
339 mData[toIdx + i] = mData[fromIdx + i];
340 }
341
342 for (size_t i = 0; i < numElements; i++) {
343 index = fromIdx + i;
344 destroy(index);
345 }
346 }
347
348 mSize = toIdx + numElements;
349}
350
351template <class T, class TAlloc>
353 if (capacity <= mCapacity) {
354 return;
355 }
356
357 T *tmp = nullptr;
358 if (mSize > 0u) {
359 tmp = mAllocator.alloc(mSize);
360 for (size_t i = 0u; i < mSize; ++i) {
361 tmp[i] = mData[i];
362 }
363 }
364
365 if (mData) {
366 mAllocator.release(mData);
367 mData = nullptr;
368 }
369
370 mData = mAllocator.alloc(capacity);
371 mCapacity = capacity;
372
373 if (mSize > 0u) {
374 for (size_t i = 0u; i < mSize; ++i) {
375 mData[i] = tmp[i];
376 }
377 mAllocator.release(tmp);
378 }
379}
380
381template <class T, class TAlloc>
382inline void TArray<T, TAlloc>::resize(size_t size) {
383 const size_t oldSize = mSize;
384 T *tmp = nullptr;
385
386 // Store older items
387 if (mSize > 0u && mCapacity < size) {
388 tmp = mAllocator.alloc(mSize * sizeof(T));
389 for (size_t i = 0u; i < mSize; ++i) {
390 tmp[i] = mData[i];
391 }
392
393 mAllocator.release(mData);
394 mData = nullptr;
395 mCapacity = 0u;
396 }
397
398 // Realloc memory
399 if (size > mCapacity) {
400 mData = mAllocator.alloc(size * sizeof(T));
401 if (tmp) {
402 for (size_t i = 0u; i < oldSize; ++i) {
403 mData[i] = tmp[i];
404 }
405 mAllocator.release(tmp);
406 }
407 mCapacity = size;
408 }
409 mSize = size;
410}
411
412template <class T, class TAlloc>
413inline void TArray<T, TAlloc>::resize(size_t size, T val) {
414 resize(size);
415 if (0u != size) {
416 for (size_t i = 0u; i < size; ++i) {
417 mData[i] = val;
418 }
419 }
420}
421
422template <class T, class TAlloc>
423inline size_t TArray<T, TAlloc>::size() const {
424 return mSize;
425}
426
427template <class T, class TAlloc>
428inline size_t TArray<T, TAlloc>::capacity() const {
429 return mCapacity;
430}
431
432template <class T, class TAlloc>
433inline bool TArray<T, TAlloc>::isEmpty() const {
434 return (0u == mSize);
435}
436
437template <class T, class TAlloc>
439 for (Iterator it = begin(); it != end(); ++it) {
440 if (item == *it) {
441 return it;
442 }
443 }
444
445 return end();
446}
447
448template <class T, class TAlloc>
450 mAllocator.release(mData);
451 mData = nullptr;
452 mSize = 0u;
453 mCapacity = 0u;
454}
455
456template <class T, class TAlloc>
458 if (0u == size()) {
459 return end();
460 }
461
462 return &mData[0];
463}
464
465template <class T, class TAlloc>
467 T *tmp = mData;
468 tmp += mSize;
469
470 return tmp;
471}
472
473template <class T, class TAlloc>
474inline T *TArray<T, TAlloc>::data() const {
475 return mData;
476}
477
478template <class T, class TAlloc>
479inline T &TArray<T, TAlloc>::operator[](size_t idx) const {
480 assert(idx < mSize);
481 assert(nullptr != mData);
482
483 return mData[idx];
484}
485
486template <class T, class TAlloc>
488 if (other == *this) {
489 return *this;
490 }
491
492 resize(other.size());
493 if (!other.isEmpty()) {
494 assert(nullptr != other.mData);
495 if (mData) {
496 for (size_t i = 0u; i < other.size(); ++i) {
497 mData[i] = other.mData[i];
498 }
499 mSize = other.mSize;
500 }
501 }
502
503 return *this;
504}
505
506template <class T, class TAlloc>
508 if (rhs.mSize != mSize) {
509 return false;
510 }
511
512 for (size_t i = 0u; i < mSize; ++i) {
513 if (mData[i] != rhs.mData[i]) {
514 return false;
515 }
516 }
517
518 return true;
519}
520
521} // Namespace cppcore
bool operator==(const TArray< T, TAlloc > &rOther) const
The compare operator.
Definition TArray.h:507
TArray(const TArray< T, TAlloc > &rhs)
The copy constructor.
Definition TArray.h:197
void resize(array_size_type size, T val)
Resize the array, new items will be created.
Definition TArray.h:413
bool isEmpty() const
Returns true, if the array is empty.
Definition TArray.h:433
Iterator linearSearch(const T &item)
Performs a linear search for a given item in the array.
Definition TArray.h:438
T & front()
Returns the first item.
Definition TArray.h:295
void removeBack()
The last item will be removed.
Definition TArray.h:271
TArray()=default
The default class constructor.
void destroy(array_size_type index)
The item at the given index will be destroyed, the destructor will be called manually.
Definition TArray.h:289
void clear()
The array will be cleared, destructor of the items will be called.
Definition TArray.h:449
const T & back() const
Returns the last item as a const reference.
Definition TArray.h:309
void remove(array_size_type index)
Removes an item at the given index.
Definition TArray.h:236
void remove(Iterator it)
Removes the item describes by a given iterator.
Definition TArray.h:250
void reserve(array_size_type capacity)
Ensures, that the capacity of the array is big enough for the given size.
Definition TArray.h:352
void resize(array_size_type size)
Resize the array, new items will be created.
Definition TArray.h:382
AbstractLogStream ** Iterator
Definition TArray.h:58
Iterator begin()
Returns an iterator showing to the first item.
Definition TArray.h:457
T & back()
Returns the last item.
Definition TArray.h:302
T & operator[](array_size_type idx) const
The [] operator.
Definition TArray.h:479
void add(const T *newValues, array_size_type numItems)
An array of new items will be added to the array.
Definition TArray.h:220
T * data() const
Will return the data pointer.
Definition TArray.h:474
Iterator end()
Returns an iterator showing to the end of the array.
Definition TArray.h:466
const AbstractLogStream ** ConstIterator
Definition TArray.h:60
void add(const T &newValue)
A new item will be added to the array.
Definition TArray.h:210
void move(array_size_type startIdx, array_size_type endIdx)
Moves the items from the start- to the end-index.
Definition TArray.h:316
void set(const T &value)
Will set all items to the value.
Definition TArray.h:278
TArray(array_size_type size)
The class constructor with an initial size.
Definition TArray.h:190
~TArray()
The class destructor.
Definition TArray.h:205
TArray< T, TAlloc > & operator=(const TArray< T, TAlloc > &rhs)
The assignment operator.
Definition TArray.h:487
static size_t getGrowing(size_t size)
Definition TArray.h:32