19template<
class TYPE,
bool StackAlloc = false>
class FixedArray
71 void Fill(
const TYPE& val);
96 template<
class T,
bool S>
114template<
class TYPE,
bool StackAlloc>
125template<
class TYPE,
bool StackAlloc>
void
139 this->elements =
nullptr;
148template<
class TYPE,
bool StackAlloc>
void
151 #if NEBULA_BOUNDSCHECKS
172template<
class TYPE,
bool StackAlloc>
void
177 this->Alloc(rhs.
count);
178 if constexpr (!std::is_trivially_copyable<TYPE>::value)
181 for (i = 0; i < this->count; i++)
183 this->elements[i] = rhs.
elements[i];
187 memcpy(this->elements, rhs.
elements, this->count *
sizeof(TYPE));
194template<
class TYPE,
bool StackAlloc>
205template<
class TYPE,
bool StackAlloc>
211 this->
Fill(initialValue);
217template<
class TYPE,
bool StackAlloc>
224 this->Alloc(rhs.count);
225 if constexpr (!std::is_trivially_copyable<TYPE>::value)
228 for (i = 0; i < this->count; i++)
230 this->elements[i] = rhs.elements[i];
241template<
class TYPE,
bool StackAlloc>
248 this->Alloc(rhs.count);
249 if constexpr (!std::is_trivially_copyable<TYPE>::value)
252 for (i = 0; i < this->count; i++)
254 this->elements[i] = rhs.elements[i];
265template<
class TYPE,
bool StackAlloc>
273 if constexpr (!std::is_trivially_copyable<TYPE>::value)
276 for (i = 0; i < this->
count; i++)
282 memcpy(this->
elements, rhs.
Begin(), this->count *
sizeof(TYPE));
289template<
class TYPE,
bool StackAlloc>
297 if (rhs.stackElements.data() == rhs.elements || StackAlloc)
299 this->Alloc(rhs.count);
300 if constexpr (!std::is_trivially_copyable<TYPE>::value)
303 for (i = 0; i < this->count; i++)
305 this->elements[i] = rhs.Begin()[i];
309 memcpy(this->elements, rhs.Begin(), this->count * sizeof(TYPE));
313 this->elements = rhs.elements;
314 rhs.elements = nullptr;
315 this->count = rhs.count;
325template<
class TYPE,
bool StackAlloc>
328 elements(rhs.elements)
332 this->
Alloc(rhs.count);
333 if constexpr (!std::is_trivially_copyable<TYPE>::value)
336 for (i = 0; i < this->
count; i++)
342 memcpy(this->
elements, rhs.Begin(), this->count *
sizeof(TYPE));
347 rhs.elements =
nullptr;
354template<
class TYPE,
bool StackAlloc>
360 if constexpr (!std::is_trivially_copyable<TYPE>::value)
363 for (i = 0; i < this->
count; i++)
365 this->
elements[i] = list.begin()[i];
369 memcpy(this->
elements, list.begin(), this->count *
sizeof(TYPE));
375template<
class TYPE,
bool StackAlloc>
385template<
class TYPE,
bool StackAlloc>
394template<
class TYPE,
bool StackAlloc>
void
407template<
class TYPE,
bool StackAlloc>
void
413 this->elements = rhs.elements;
414 this->count = rhs.count;
415 rhs.elements =
nullptr;
423template<
class TYPE,
bool StackAlloc> TYPE&
426 #if NEBULA_BOUNDSCHECKS
427 n_assert(this->elements && (index < this->count));
429 return this->elements[index];
435template<
class TYPE,
bool StackAlloc>
bool
438 if (this->count != rhs.
count)
444 #if NEBULA_BOUNDSCHECKS
448 SizeT num = this->count;
449 for (i = 0; i < num; i++)
451 if (this->elements[i] != rhs.
elements[i])
463template<
class TYPE,
bool StackAlloc>
bool
466 return !(*
this == rhs);
472template<
class TYPE,
bool StackAlloc>
void
482template<
class TYPE,
bool StackAlloc>
void
486 if (newSize == this->count)
489 TYPE* newElements = 0;
500 SizeT numCopy = this->count;
503 if (numCopy > newSize)
505 if constexpr (!std::is_trivially_move_assignable<TYPE>::value && std::is_move_assignable<TYPE>::value)
508 for (i = 0; i < numCopy; i++)
510 newElements[i] = std::move(this->elements[i]);
524 this->elements = newElements;
525 this->count = newSize;
531template<
class TYPE,
bool StackAlloc>
const SizeT
540template<
class TYPE,
bool StackAlloc>
const SizeT
543 return this->count *
sizeof(TYPE);
549template<
class TYPE,
bool StackAlloc>
bool
552 return 0 == this->count;
558template<
class TYPE,
bool StackAlloc>
void
567template<
class TYPE,
bool StackAlloc>
void
571 for (i = 0; i < this->count; i++)
573 this->elements[i] = val;
580template<
class TYPE,
bool StackAlloc>
void
583 #if NEBULA_BOUNDSCHECKS
584 n_assert((first + num) <= this->count);
588 for (i = first; i < (first + num); i++)
590 this->elements[i] = val;
600 return this->elements;
609 return this->elements + this->count;
619 for (i = 0; i < this->count; i++)
621 if (elm == this->elements[i])
623 return &(this->elements[i]);
632template<
class TYPE,
bool StackAlloc>
IndexT
636 for (i = 0; i < this->count; i++)
638 if (elm == this->elements[i])
649template<
class TYPE,
bool StackAlloc>
void
652 std::sort(this->Begin(), this->End());
659template<
class TYPE,
bool StackAlloc>
IndexT
662 SizeT num = this->Size();
671 if (0 != (
half = num/2))
673 mid = lo + ((num & 1) ?
half : (
half - 1));
674 if (elm < this->elements[mid])
679 else if (elm > this->elements[mid])
691 if (elm != this->elements[lo])
718 for (i = 0; i < this->count; i++)
720 result.
Append(this->elements[i]);
731 return this->elements;
740 return this->elements + this->count;
746template<
class TYPE,
bool StackAlloc>
void
749 if (s > this->capacity)
759template<
class TYPE,
bool StackAlloc>
size_t
Nebula's dynamic array class.
Definition array.h:60
void Reserve(SizeT num)
increase capacity to fit N more elements into the array.
Definition array.h:862
void Append(const TYPE &first, const ELEM_TYPE &... elements)
Append multiple elements to the end of the array.
Definition array.h:1334
Iterator Begin() const
return iterator to beginning of array
Definition array.h:1245
Implements a fixed size one-dimensional array.
Definition fixedarray.h:20
void Delete()
delete content
Definition fixedarray.h:126
void operator=(const FixedArray< TYPE, StackAlloc > &rhs)
assignment operator
Definition fixedarray.h:395
Iterator End() const
get iterator past last element
Definition fixedarray.h:607
IndexT BinarySearchIndex(const TYPE &val) const
do a binary search, requires a sorted array
Definition fixedarray.h:660
FixedArray(FixedArray< TYPE, StackAlloc > &&rhs)
move constructor
Definition fixedarray.h:326
TYPE & operator[](IndexT index) const
write [] operator
Definition fixedarray.h:424
Iterator Find(const TYPE &val) const
find identical element in unsorted array (slow)
Definition fixedarray.h:616
Array< TYPE > AsArray() const
return content as Array (slow!)
Definition fixedarray.h:713
void Fill(const TYPE &val)
fill the entire array with a value
Definition fixedarray.h:568
bool operator==(const FixedArray< TYPE, StackAlloc > &rhs) const
equality operator
Definition fixedarray.h:436
Iterator end() const
Definition fixedarray.h:738
FixedArray(const FixedArray< TYPE, false > &rhs)
copy constructor
Definition fixedarray.h:242
Iterator Begin() const
get iterator to first element
Definition fixedarray.h:598
FixedArray(const SizeT s)
constructor with size
Definition fixedarray.h:195
const SizeT ByteSize() const
get total byte size
Definition fixedarray.h:541
size_t size() const
Definition fixedarray.h:760
TYPE * Iterator
define element iterator
Definition fixedarray.h:23
const SizeT Size() const
get number of elements
Definition fixedarray.h:532
void resize(size_t size)
Definition fixedarray.h:747
FixedArray(const FixedArray< TYPE, true > &rhs)
copy constructor
Definition fixedarray.h:218
IndexT FindIndex(const TYPE &val) const
find index of identical element in unsorted array (slow)
Definition fixedarray.h:633
void operator=(FixedArray< TYPE, StackAlloc > &&rhs) noexcept
move assignment operator
Definition fixedarray.h:408
bool operator!=(const FixedArray< TYPE, StackAlloc > &rhs) const
inequality operator
Definition fixedarray.h:464
void Clear()
clear the array, free elements
Definition fixedarray.h:559
FixedArray()
default constructor
Definition fixedarray.h:115
FixedArray(std::initializer_list< TYPE > list)
constructor from initializer list
Definition fixedarray.h:355
bool IsEmpty() const
return true if array if empty (has no elements)
Definition fixedarray.h:550
void Fill(IndexT first, SizeT num, const TYPE &val)
fill array range with element
Definition fixedarray.h:581
FixedArray(const Array< TYPE > &rhs)
construct from array
Definition fixedarray.h:266
void Resize(SizeT newSize)
resize array without deleting existing content
Definition fixedarray.h:483
void SetSize(SizeT s)
set number of elements (clears existing content)
Definition fixedarray.h:473
~FixedArray()
destructor
Definition fixedarray.h:386
TYPE * elements
Definition fixedarray.h:107
void Copy(const FixedArray< TYPE, StackAlloc > &src)
copy content
Definition fixedarray.h:173
friend class FixedArray
Definition fixedarray.h:97
void Alloc(SizeT s)
allocate array for given size
Definition fixedarray.h:149
FixedArray(const SizeT s, const TYPE &initialValue)
constructor with size and initial value
Definition fixedarray.h:206
FixedArray(std::nullptr_t)
construct an empty fixed array
Definition fixedarray.h:376
SizeT count
Definition fixedarray.h:106
FixedArray(Array< TYPE > &&rhs)
move from array
Definition fixedarray.h:290
Iterator begin() const
for range-based iteration (C++11)
Definition fixedarray.h:729
void Sort()
sort the array
Definition fixedarray.h:650
#define n_assert(exp)
Definition debug.h:50
void ArrayFreeStack(size_t size, TYPE *buffer)
Definition memory.h:111
TYPE * ArrayAllocStack(size_t size)
Definition memory.h:75
void ArrayFree(size_t size, TYPE *buffer)
Definition memory.h:94
TYPE * ArrayAlloc(size_t size)
Definition memory.h:57
__forceinline void MoveElements(const T *from, T *to, size_t numElements)
Move a chunk of memory, can handle overlapping regions.
Definition posixmemory.h:192
A pinned array is an array which manages its own virtual memory.
Definition String.cs:6
#define StackAlloc(size)
Definition posixmemory.h:30
static const int InvalidIndex
Definition types.h:54
int SizeT
Definition types.h:49
int IndexT
Definition types.h:48