46template<
class TYPE,
int STACK_SIZE>
57 TYPE*
data() {
return nullptr; }
60template<
class TYPE,
int SMALL_VECTOR_SIZE = 0>
class Array
80 Array(std::initializer_list<TYPE> list);
101 template<
typename T> T
As()
const;
107 template <
typename ...ELEM_TYPE>
189 template <
typename KEYTYPE>
IndexT FindIndex(
typename std::enable_if<true, const KEYTYPE&>::type elm,
const IndexT start = 0)
const;
232 template<typename ELEM>
234 template<typename ELEM>
236 template<typename ELEM>
239 template<class T,
bool S>
274template<class TYPE,
int SMALL_VECTOR_SIZE>
286template<
class TYPE,
int SMALL_VECTOR_SIZE>
303template<
class TYPE,
int SMALL_VECTOR_SIZE>
315 this->
GrowTo(initialSize);
316 this->
count = initialSize;
318 for (i = 0; i < initialSize; i++)
327template<
class TYPE,
int SMALL_VECTOR_SIZE>
334 static_assert(std::is_trivially_copyable<TYPE>::value,
"TYPE is not trivially copyable; Util::Array cannot be constructed from pointer of TYPE.");
337 const SizeT bytes = num *
sizeof(TYPE);
344template<
class TYPE,
int SMALL_VECTOR_SIZE>
354 for (i = 0; i < this->
count; i++)
356 this->
elements[i] = list.begin()[i];
363template<
class TYPE,
int SMALL_VECTOR_SIZE>
375template<
class TYPE,
int SMALL_VECTOR_SIZE>
388template<
class TYPE,
int SMALL_VECTOR_SIZE>
396 if (rhs.capacity <= SMALL_VECTOR_SIZE)
398 for (
IndexT i = 0; i < rhs.count; i++)
400 this->
elements[i] = std::move(rhs.elements[i]);
407 rhs.elements = rhs.stackElements.data();
410 rhs.capacity = SMALL_VECTOR_SIZE;
416template<
class TYPE,
int SMALL_VECTOR_SIZE>
420 #if NEBULA_BOUNDSCHECKS
429 for (i = 0; i < this->
count; i++)
438template<
class TYPE,
int SMALL_VECTOR_SIZE>
464template<
class TYPE,
int SMALL_VECTOR_SIZE>
void
473template<
class TYPE,
int SMALL_VECTOR_SIZE>
void
476 if constexpr (!std::is_trivially_destructible<TYPE>::value)
479 ::new ((
void*)elm) TYPE();
486template<
class TYPE,
int SMALL_VECTOR_SIZE>
495template<
class TYPE,
int SMALL_VECTOR_SIZE>
void
501 this->
count = _capacity;
515template<
class TYPE,
int SMALL_VECTOR_SIZE>
void
526 if (rhs.
count < this->count)
545template<
class TYPE,
int SMALL_VECTOR_SIZE>
void
553 if (rhs.elements != rhs.stackElements.data())
556 rhs.elements =
nullptr;
564 this->
grow = rhs.grow;
565 this->
count = rhs.count;
567 rhs.elements = rhs.stackElements.data();
569 rhs.capacity = SMALL_VECTOR_SIZE;
576template<
class TYPE,
int SMALL_VECTOR_SIZE>
void
579 if (newCapacity > SMALL_VECTOR_SIZE)
598template<
class TYPE,
int SMALL_VECTOR_SIZE>
602 #if NEBULA_BOUNDSCHECKS
609 growToSize = this->
grow;
623 growToSize = this->
capacity + growBy;
633template<
class TYPE,
int SMALL_VECTOR_SIZE>
637 #if NEBULA_BOUNDSCHECKS
643 if (fromIndex == toIndex)
649 SizeT num = this->count - fromIndex;
652 SizeT neededSize = toIndex + num;
658 if (fromIndex > toIndex)
668 for (i = num - 1; i >= 0; --i)
678 this->count = toIndex + num;
684template<
class TYPE,
int SMALL_VECTOR_SIZE>
688 if constexpr (!std::is_trivially_destructible<TYPE>::value)
690 for (
IndexT i = fromIndex; i < toIndex; i++)
704template<
class TYPE,
int SMALL_VECTOR_SIZE>
709 if constexpr (!std::is_trivially_copyable<TYPE>::value)
712 for (i = 0; i < num; i++)
726template<
class TYPE,
int SMALL_VECTOR_SIZE>
732 if constexpr (std::is_trivially_copyable<TYPE>::value)
739 for (i = 0; i < num; i++)
741 to[i] = std::move(from[i]);
749template<
class TYPE,
int SMALL_VECTOR_SIZE>
753#if NEBULA_BOUNDSCHECKS
763template<
class TYPE,
int SMALL_VECTOR_SIZE>
772 #if NEBULA_BOUNDSCHECKS
781template<
class TYPE,
int SMALL_VECTOR_SIZE>
790#if NEBULA_BOUNDSCHECKS
799template<
class TYPE,
int SMALL_VECTOR_SIZE>
804 if (neededCapacity > this->
capacity)
806 this->
GrowTo(neededCapacity);
811 for (i = 0; i < rhs.
count; i++)
821template<
class TYPE,
int SMALL_VECTOR_SIZE>
826 if (neededCapacity > this->
capacity)
828 this->
GrowTo(neededCapacity);
833 for (i = 0; i <
count; i++)
835 this->
elements[this->count + i] = arr[i];
837 this->count +=
count;
843template<
class TYPE,
int SMALL_VECTOR_SIZE>
852#if NEBULA_BOUNDSCHECKS
862template<
class TYPE,
int SMALL_VECTOR_SIZE>
867 if (neededCapacity > this->
capacity)
869 this->
GrowTo(neededCapacity);
874 for (i = 0; i <
count; i++)
876 this->
elements[this->count + i] = TYPE();
878 TYPE* first = &this->
elements[this->count];
879 this->count +=
count;
893template<
class TYPE,
int SMALL_VECTOR_SIZE>
897#if NEBULA_BOUNDSCHECKS
902 if (neededCapacity > this->
capacity)
904 this->
GrowTo(neededCapacity);
911template<
class TYPE,
int SMALL_VECTOR_SIZE>
921template<
class TYPE,
int SMALL_VECTOR_SIZE>
925 return this->
count *
sizeof(TYPE);
931template<
class TYPE,
int SMALL_VECTOR_SIZE>
943template<
class TYPE,
int SMALL_VECTOR_SIZE>
947 #if NEBULA_BOUNDSCHECKS
958template<
class TYPE,
int SMALL_VECTOR_SIZE>
962#if NEBULA_BOUNDSCHECKS
973template<
class TYPE,
int SMALL_VECTOR_SIZE>
977 if (rhs.
Size() == this->Size())
981 for (i = 0; i < num; i++)
1001template<
class TYPE,
int SMALL_VECTOR_SIZE>
1005 return !(*
this == rhs);
1011template<
class TYPE,
int SMALL_VECTOR_SIZE>
1015 #if NEBULA_BOUNDSCHECKS
1024template<
class TYPE,
int SMALL_VECTOR_SIZE>
1028 #if NEBULA_BOUNDSCHECKS
1037template<
class TYPE,
int SMALL_VECTOR_SIZE>
1047template<
class TYPE,
int SMALL_VECTOR_SIZE>
1051 return (this->
count == 0);
1057template<
class TYPE,
int SMALL_VECTOR_SIZE>
1061 return this->
elements && (index < this->
count) && (index >= 0);
1067template<
class TYPE,
int SMALL_VECTOR_SIZE>
1071 #if NEBULA_BOUNDSCHECKS
1074 if (index == (this->count - 1))
1081 this->
Move(index + 1, index);
1089template<
class TYPE,
int SMALL_VECTOR_SIZE>
1093 #if NEBULA_BOUNDSCHECKS
1098 IndexT lastElementIndex = this->count - 1;
1099 if (index < lastElementIndex)
1114template<
class TYPE,
int SMALL_VECTOR_SIZE>
1118 #if NEBULA_BOUNDSCHECKS
1129template<
class TYPE,
int SMALL_VECTOR_SIZE>
1133 #if NEBULA_BOUNDSCHECKS
1143template<
class TYPE,
int SMALL_VECTOR_SIZE>
1157 this->count -=
end - start;
1164template<
class TYPE,
int SMALL_VECTOR_SIZE>
1176template<
class TYPE,
int SMALL_VECTOR_SIZE>
1186template<
class TYPE,
int SMALL_VECTOR_SIZE>
1190#if NEBULA_BOUNDSCHECKS
1193 TYPE ret = std::move(this->
elements[0]);
1201template<
class TYPE,
int SMALL_VECTOR_SIZE>
1205#if NEBULA_BOUNDSCHECKS
1209 TYPE ret = std::move(this->
elements[index]);
1211 this->
count = index;
1218template<
class TYPE,
int SMALL_VECTOR_SIZE>
1228template<
class TYPE,
int SMALL_VECTOR_SIZE>
1238template<
class TYPE,
int SMALL_VECTOR_SIZE>
1239template<
typename ELEM>
1243 #if NEBULA_BOUNDSCHECKS
1246 if (index == this->
count)
1249 this->
Append(std::forward<ELEM>(elm));
1253 this->
Move(index, index + 1);
1254 this->
elements[index] = std::forward<ELEM>(elm);
1263template<
class TYPE,
int SMALL_VECTOR_SIZE>
1267 if (this->
count > 0)
1279template<
class TYPE,
int SMALL_VECTOR_SIZE>
1290template<
class TYPE,
int SMALL_VECTOR_SIZE>
1301template<
class TYPE,
int SMALL_VECTOR_SIZE>
1311template<
class TYPE,
int SMALL_VECTOR_SIZE>
1321template<
class TYPE,
int SMALL_VECTOR_SIZE>
1331template<
class TYPE,
int SMALL_VECTOR_SIZE>
1346template<
class TYPE,
int SMALL_VECTOR_SIZE>
1352 for (index = start; index < this->count; index++)
1370template<
class TYPE,
int SMALL_VECTOR_SIZE>
1376 for (index = start; index < this->count; index++)
1389template<
class TYPE,
int SMALL_VECTOR_SIZE>
1390template<
typename ...ELEM_TYPE>
1395 const int size =
sizeof...(elements) + 1;
1400 this->elements[this->
count++] = res[i];
1419template<
class TYPE,
int SMALL_VECTOR_SIZE>
1420template<
typename KEYTYPE>
1426 for (index = start; index < this->count; index++)
1445template<
class TYPE,
int SMALL_VECTOR_SIZE>
1449 if ((first + num) > this->
count)
1451 this->
GrowTo(first + num);
1452 this->
count = first + num;
1456 for (i = first; i < (first + num); i++)
1469template<
class TYPE,
int SMALL_VECTOR_SIZE>
1476 for (i = 0; i < num; i++)
1478 if (0 == this->
Find(rhs[i]))
1490template<
class TYPE,
int SMALL_VECTOR_SIZE>
1495 std::sort(this->
Begin(), this->
End());
1502template <
class TYPE,
int SMALL_VECTOR_SIZE>
1512 [](
const void* a,
const void* b)
1514 TYPE arg1 = *
static_cast<const TYPE*
>(a);
1515 TYPE arg2 = *
static_cast<const TYPE*
>(b);
1516 return (arg1 > arg2) - (arg1 < arg2);
1524template<
class TYPE,
int SMALL_VECTOR_SIZE>
1531 std::sort(this->
Begin(), this->
End(), func);
1537template <
class TYPE,
int SMALL_VECTOR_SIZE>
1557template<
class TYPE,
int SMALL_VECTOR_SIZE>
1570 if (0 != (
half = num/2))
1572 mid = lo + ((num & 1) ?
half : (
half - 1));
1578 else if (elm > this->
elements[mid])
1617template<
class TYPE,
int SMALL_VECTOR_SIZE>
1618template<
typename KEYTYPE>
inline IndexT
1630 if (0 != (
half = num / 2))
1632 mid = lo + ((num & 1) ?
half : (
half - 1));
1638 else if (this->
elements[mid] < elm)
1671template<
class TYPE,
int SMALL_VECTOR_SIZE>
1675 if (num < this->
count)
1690template<
class TYPE,
int SMALL_VECTOR_SIZE>
1691template<
typename ...ARGS>
1694 if (num < this->
count)
1704 for (
IndexT i = this->count; i < num; i++)
1716template<
class TYPE,
int SMALL_VECTOR_SIZE>
1729template<
class TYPE,
int SMALL_VECTOR_SIZE>
1739template<
class TYPE,
int SMALL_VECTOR_SIZE>
1758template<
class TYPE,
int SMALL_VECTOR_SIZE>
1759inline constexpr SizeT
1762 return sizeof(TYPE);
1768template<
class TYPE,
int SMALL_VECTOR_SIZE>
1779template<
class TYPE,
int SMALL_VECTOR_SIZE>
1789template<
class TYPE,
int SMALL_VECTOR_SIZE>
1799template<
class TYPE,
int SMALL_VECTOR_SIZE>
1816template<
class TYPE,
int SMALL_VECTOR_SIZE>
1820 if (this->
count > 1)
1823 for (i = 0; i < this->
count - 1; i++)
1840template<
class TYPE,
int SMALL_VECTOR_SIZE>
1853template<
class TYPE,
int SMALL_VECTOR_SIZE>
1863template<
class TYPE,
int SMALL_VECTOR_SIZE>
1864template<
typename ELEM>
1868 const TYPE& probe = elm;
1869 IndexT i = startIndex + 1;
1870 for (; i < this->
count; i++)
1880 this->
Append(std::forward<ELEM>(elm));
1881 return (this->
Size() - 1);
1889template<
class TYPE,
int SMALL_VECTOR_SIZE>
1901template<
class TYPE,
int SMALL_VECTOR_SIZE>
1911template<
class TYPE,
int SMALL_VECTOR_SIZE>
1912template<
typename ELEM>
1916 const TYPE& probe = elm;
1921 this->
Append(std::forward<ELEM>(elm));
1922 return this->
Size() - 1;
1932 if (0 != (
half = num/2))
1934 mid = lo + ((num & 1) ?
half : (
half - 1));
1940 else if (probe > this->
elements[mid])
1959 else if (probe > this->
elements[lo])
1973 #if NEBULA_BOUNDSCHECKS
1985 else if (probe > this->
elements[lo])
1996 n_error(
"Array::InsertSorted: Can't happen!");
2000template<
class TYPE,
int STACK_SIZE>
Nebula's dynamic array class.
Definition array.h:61
IndexT InsertAtEndOfIdenticalRangeInternal(IndexT startIndex, ELEM &&elm)
Array(std::nullptr_t)
construct an empty fixed array
Definition array.h:364
void Move(IndexT fromIndex, IndexT toIndex)
const TYPE * ConstIterator
Definition array.h:65
void Extend(SizeT num)
Resize to fit the provided value, but don't shrink if the new size is smaller.
Definition array.h:1717
void InsertInternal(IndexT index, ELEM &&elm)
void Insert(IndexT index, TYPE &&elm)
insert rvalue element before element at index
Definition array.h:1230
Array(SizeT initialCapacity, SizeT initialGrow)
constuctor with initial size and grow size
Definition array.h:287
void Reserve(SizeT num)
increase capacity to fit N more elements into the array.
Definition array.h:895
void Fill(IndexT first, SizeT num, const TYPE &elm)
fill array range with element
Definition array.h:1447
IndexT FindIndex(const TYPE &elm, const IndexT start=0) const
find identical element in array, return index, InvalidIndex if not found
Definition array.h:1372
Iterator End() const
return iterator to end of array
Definition array.h:1323
SizeT capacity
Definition array.h:264
SizeT count
Definition array.h:265
TYPE * EmplaceArray(const SizeT count)
Emplace range of items and return pointer to first.
Definition array.h:864
~Array()
destructor
Definition array.h:487
Iterator Find(const TYPE &elm, const IndexT start=0) const
find identical element in array, return iterator
Definition array.h:1348
IndexT FindIndex(typename std::enable_if< true, const KEYTYPE & >::type elm, const IndexT start=0) const
find identical element using a specific key type
Definition array.h:1422
bool operator!=(const Array< TYPE, SMALL_VECTOR_SIZE > &rhs) const
inequality operator
Definition array.h:1003
TYPE * Iterator
define iterator
Definition array.h:64
void EraseFront()
erase front
Definition array.h:1178
TYPE & Back() const
return reference to last element
Definition array.h:1026
IndexT InsertAtEndOfIdenticalRange(IndexT startIndex, TYPE &&elm)
insert rvalue element at the first non-identical position, return index of inclusion position
Definition array.h:1855
void Copy(const Array< TYPE, SMALL_VECTOR_SIZE > &src)
const SizeT Capacity() const
get overall allocated size of array in number of elements
Definition array.h:933
void Clear()
clear array (calls destructors)
Definition array.h:1265
TYPE * elements
Definition array.h:266
IndexT InsertAtEndOfIdenticalRange(IndexT startIndex, const TYPE &elm)
insert element at the first non-identical position, return index of inclusion position
Definition array.h:1842
ConstIterator ConstEnd() const
return const iterator to end of array
Definition array.h:1333
constexpr SizeT TypeSize() const
Returns sizeof(TYPE).
Definition array.h:1760
Array(std::initializer_list< TYPE > list)
constructor from initializer list
Definition array.h:345
void EraseRange(IndexT start, IndexT end)
erase range, excluding the element at end
Definition array.h:1145
IndexT BinarySearchIndex(typename std::enable_if< true, const KEYTYPE & >::type elm) const
do a binary search using a specific key type
Definition array.h:1619
void Sort()
sort the array
Definition array.h:1492
SizeT grow
Definition array.h:263
void operator=(Array< TYPE, SMALL_VECTOR_SIZE > &&rhs) noexcept
move operator
Definition array.h:546
void QuickSort()
quick sort the array
Definition array.h:1504
void Append(TYPE &&elm)
append an element which is being forwarded
Definition array.h:783
void DestroyRange(IndexT fromIndex, IndexT toIndex)
Array< TYPE, SMALL_VECTOR_SIZE > ArrayT
Definition array.h:67
TYPE & operator[](IndexT index) const
[] operator
Definition array.h:945
static const SizeT MinGrowSize
Definition array.h:261
Array(ArrayT &&rhs) noexcept
move constructor
Definition array.h:389
void SortWithFunc(bool(*func)(const TYPE &lhs, const TYPE &rhs))
sort with custom function
Definition array.h:1526
void Append(const TYPE &first, const ELEM_TYPE &... elements)
Append multiple elements to the end of the array.
Definition array.h:1392
TYPE & operator[](IndexT index)
[] operator
Definition array.h:960
Iterator begin() const
for range-based iteration
Definition array.h:1781
Iterator EraseSwap(Iterator iter)
erase element at iterator, fill gap by swapping in last element, destroys sorting!
Definition array.h:1131
TYPE & Emplace()
Emplace item (create new item and return reference).
Definition array.h:845
Array(const TYPE *const buf, SizeT num)
constructor from TYPE pointer and size.
Definition array.h:328
void Reset()
reset array (does NOT call destructors)
Definition array.h:1281
void Realloc(SizeT capacity, SizeT grow)
clear contents and preallocate with new attributes
Definition array.h:496
T As() const
convert to "anything"
bool IsEmpty() const
return true if array empty
Definition array.h:1049
IndexT InsertSorted(const TYPE &elm)
insert element into sorted array, return index where element was included
Definition array.h:1891
void AppendArray(const Array< TYPE, SMALL_VECTOR_SIZE > &rhs)
append the contents of an array to this array
Definition array.h:801
void EraseIndex(IndexT index)
erase element at index, keep sorting intact
Definition array.h:1069
static const SizeT MaxGrowSize
Definition array.h:262
const SizeT ByteSize() const
return the byte size of the array.
Definition array.h:923
void EraseIndexSwap(IndexT index)
erase element at index, fill gap by swapping in last element, destroys sorting!
Definition array.h:1091
TYPE PopFront()
Pop front.
Definition array.h:1188
ConstIterator ConstBegin() const
return const iterator to beginning of array
Definition array.h:1313
_smallvector< TYPE, SMALL_VECTOR_SIZE > stackElements
Definition array.h:268
void resize(size_t size)
Definition array.h:1801
Array(SizeT initialSize, SizeT initialGrow, const TYPE &initialValue)
constructor with initial size, grow size and initial values
Definition array.h:304
void operator=(const Array< TYPE, SMALL_VECTOR_SIZE > &rhs)
assignment operator
Definition array.h:516
void Append(const TYPE &elm)
append element to end of array
Definition array.h:765
IndexT InsertSortedInternal(ELEM &&elm)
void GrowTo(SizeT newCapacity)
TYPE & Get(IndexT index) const
Get element (same as operator[] but as a function).
Definition array.h:751
void Insert(IndexT index, const TYPE &elm)
insert element before element at index
Definition array.h:1220
void push_back(const TYPE &item)
size_t size() const
Definition array.h:1770
void CopyRange(TYPE *to, TYPE *from, SizeT num)
void Resize(SizeT num, ARGS... args)
Resize and fill new elements with arguments.
Definition array.h:1692
bool operator==(const Array< TYPE, SMALL_VECTOR_SIZE > &rhs) const
equality operator
Definition array.h:975
ArrayT Difference(const Array< TYPE, SMALL_VECTOR_SIZE > &rhs)
returns new array with elements which are not in rhs (slow!)
Definition array.h:1471
void EraseBack()
erase back
Definition array.h:1166
bool IsValidIndex(IndexT index) const
check if index is valid
Definition array.h:1059
IndexT InsertSorted(TYPE &&elm)
insert rvalue element into sorted array, return index where element was included
Definition array.h:1903
Iterator Begin() const
return iterator to beginning of array
Definition array.h:1303
bool IsSorted() const
test if the array is sorted, this is a slow operation!
Definition array.h:1818
IndexT BinarySearchIndex(const TYPE &elm) const
do a binary search, requires a sorted array
Definition array.h:1559
void MoveRange(TYPE *to, TYPE *from, SizeT num)
friend class FixedArray
Definition array.h:240
void Resize(SizeT num)
Set size. Grows array if num is greater than capacity. Calls destroy on all objects at index > num!
Definition array.h:1673
void QuickSortWithFunc(int(*func)(const void *lhs, const void *rhs))
quick sort the array
Definition array.h:1539
Array()
constructor with default parameters
Definition array.h:275
void Free()
free memory and reset size
Definition array.h:1292
const SizeT Size() const
get number of elements in array
Definition array.h:913
void AppendArray(const TYPE *arr, const SizeT count)
append from C array
Definition array.h:823
void clear() noexcept
Definition array.h:1731
TYPE & Front() const
return reference to first element
Definition array.h:1013
TYPE PopBack()
Pop back.
Definition array.h:1203
Iterator Erase(Iterator iter)
erase element pointed to by iterator, keep sorting intact
Definition array.h:1116
void Fit()
Fit the size of the array to the amount of elements.
Definition array.h:1741
Array(const ArrayT &rhs)
copy constructor
Definition array.h:376
void __cdecl n_error(const char *msg,...)
This function is called when a serious situation is encountered which requires abortion of the applic...
Definition debug.cc:138
#define n_assert(exp)
Definition debug.h:50
void ArrayFree(size_t size, TYPE *buffer)
Definition memory.h:188
TYPE * ArrayAlloc(size_t size)
Definition memory.h:137
__forceinline TYPE max(TYPE a, TYPE b)
Definition scalar.h:368
void Copy(const void *from, void *to, size_t numBytes)
Copy a chunk of memory (note the argument order is different from memcpy()!
Definition osxmemory.cc:213
__forceinline void CopyElements(const T *from, T *to, size_t numElements)
Copy a chunk of memory (note the argument order is different from memcpy()!
Definition posixmemory.h:221
__forceinline void MoveElements(const T *from, T *to, size_t numElements)
Move a chunk of memory, can handle overlapping regions.
Definition posixmemory.h:197
void Clear(void *ptr, size_t numBytes)
Overwrite a chunk of memory with 0's.
Definition osxmemory.cc:229
A quad tree designed to return regions of free 2D space.
Definition String.cs:6
Array< TYPE, STACK_SIZE > StackArray
Definition array.h:2001
Nebula's scalar datatype.
TYPE * data()
Definition array.h:57
TYPE stackElements[STACK_SIZE]
Definition array.h:51
TYPE * data()
Definition array.h:49
static const int InvalidIndex
Definition types.h:47
int SizeT
Definition types.h:42
int IndexT
Definition types.h:41