31 # pragma warning (push)
33 # pragma warning( disable : 4312)
34 # pragma warning( disable : 4786)
94 template <
class T,
size_t MIN_ELEMENTS = 10>
115 this->numAllocated = 0;
126 for (
size_t i = 0; i <
num; ++i) {
127 data[i] = other.
data[i];
136 return (address >= data) && (address < data +
num);
159 data = (T*)m_memoryManager->alloc(
sizeof(T) * numAllocated);
160 alwaysAssertM(data,
"Memory manager returned NULL: out of memory?");
163 {
const size_t N =
G3D::min(oldNum, numAllocated);
164 const T*
end = data + N;
166 for (T* ptr = data; ptr <
end; ++ptr, ++oldPtr) {
170 const T* constructed =
new (ptr) T(*oldPtr);
174 "new returned a different address than the one provided by Array.");
178 {
const T*
end = oldData + oldNum;
179 for (T* ptr = oldData; ptr <
end; ++ptr) {
183 m_memoryManager->free(oldData);
193 for (
int i = 0; i < (int)num; ++i) {
201 for (
size_t i = 0; i <
num; ++i) {
244 ConstIterator
end()
const {
300 Array(
const T& v0,
const T& v1,
const T& v2) {
308 Array(
const T& v0,
const T& v1,
const T& v2,
const T& v3) {
317 Array(
const T& v0,
const T& v1,
const T& v2,
const T& v3,
const T& v4) {
335 explicit Array(
const std::vector<T>& other) : num(0), data(
NULL) {
349 if (numAllocated < other.
num) {
350 m_memoryManager->free(data);
353 data = (T*)m_memoryManager->alloc(
sizeof(T) * other.
num);
355 numAllocated = other.
num;
359 if (other.
data && (num > 0)) {
367 const size_t oldSize =
num;
369 if (numAllocated < num) {
372 data = (T*)m_memoryManager->alloc(
sizeof(T) * num);
374 m_memoryManager->free(old);
391 for (
size_t i = 0; i <
num; ++i) {
395 m_memoryManager->free(data);
448 data[index] = data[num - 1];
460 for (
size_t i = (
size_t)(num - 1); i > (size_t)n; --i) {
461 data[i] = data[i - 1];
468 for (
size_t i = 0; i <
num; ++i) {
479 numAllocated =
size();
490 void resize(
size_t n,
bool shrinkIfNecessary =
true) {
491 alwaysAssertM(n < 0xFFFFFFFF,
"This implementation does not support arrays with more than 2^32 elements, although the size in memory may be larger.");
500 for (
size_t i = num; i < oldNum; ++i) {
505 const size_t minSize =
G3D::max(MIN_ELEMENTS, (
size_t)(MIN_BYTES /
sizeof(T)));
507 if ((MIN_ELEMENTS == 0) && (MIN_BYTES == 0) && (n == 0) && shrinkIfNecessary) {
510 m_memoryManager->free(data);
515 if (num > numAllocated) {
518 if (numAllocated == 0) {
527 numAllocated = minSize;
537 double growFactor = 3.0f;
539 size_t oldSizeBytes = numAllocated *
sizeof(T);
540 if (oldSizeBytes > 10000000) {
543 }
else if (oldSizeBytes > 400000) {
546 }
else if (oldSizeBytes > 64000) {
551 numAllocated = (num -
numAllocated) + (
size_t)(numAllocated * growFactor);
553 if (numAllocated < minSize) {
554 numAllocated = minSize;
561 }
else if ((num <= numAllocated / 3) && shrinkIfNecessary && (num > minSize)) {
573 for (
size_t i = oldNum; i <
num; ++i) {
585 if (num < numAllocated) {
588 new (data +
num) T(value);
599 resize(num + 1, DONT_SHRINK_UNDERLYING_ARRAY);
600 data[num - 1] =
value;
605 inline void append(
const T& v1,
const T& v2) {
612 }
else if (num + 1 < numAllocated) {
615 new (data +
num) T(v1);
616 new (data + num + 1) T(v2);
620 resize(num + 2, DONT_SHRINK_UNDERLYING_ARRAY);
627 inline void append(
const T& v1,
const T& v2,
const T& v3) {
633 }
else if (num + 2 < numAllocated) {
636 new (data +
num) T(v1);
637 new (data + num + 1) T(v2);
638 new (data + num + 2) T(v3);
641 resize(num + 3, DONT_SHRINK_UNDERLYING_ARRAY);
649 inline void append(
const T& v1,
const T& v2,
const T& v3,
const T& v4) {
656 }
else if (num + 3 < numAllocated) {
659 new (data +
num) T(v1);
660 new (data + num + 1) T(v2);
661 new (data + num + 2) T(v3);
662 new (data + num + 3) T(v4);
665 resize(num + 4, DONT_SHRINK_UNDERLYING_ARRAY);
673 void append(
const T& v1,
const T& v2,
const T& v3,
const T& v4,
const T& v5) {
680 append(t1, t2, t3, t4, t5);
681 }
else if (num + 4 < numAllocated) {
684 new (data +
num) T(v1);
685 new (data + num + 1) T(v2);
686 new (data + num + 2) T(v3);
687 new (data + num + 3) T(v4);
688 new (data + num + 4) T(v5);
691 resize(num + 5, DONT_SHRINK_UNDERLYING_ARRAY);
700 void append(
const T& v1,
const T& v2,
const T& v3,
const T& v4,
const T& v5,
const T& v6) {
708 append(t1, t2, t3, t4, t5, t6);
709 }
else if (num + 5 < numAllocated) {
712 new (data +
num) T(v1);
713 new (data + num + 1) T(v2);
714 new (data + num + 2) T(v3);
715 new (data + num + 3) T(v4);
716 new (data + num + 4) T(v5);
717 new (data + num + 5) T(v6);
720 resize(num + 6, DONT_SHRINK_UNDERLYING_ARRAY);
733 for (
int i = 0; i <
size(); ++i) {
734 if ((*
this)[i] == e) {
749 size_t arrayLength = array.
length();
751 resize(num + arrayLength,
false);
753 for (
size_t i = 0; i < arrayLength; ++i) {
754 data[oldNum + i] = array.
data[i];
822 return (*
this)[
size()-1];
831 return (*
this)[
size()-1];
837 inline T
pop(
bool shrinkUnderlyingArrayIfNecessary =
true) {
839 T temp = data[num - 1];
840 resize(num - 1, shrinkUnderlyingArrayIfNecessary);
846 inline void popDiscard(
bool shrinkUnderlyingArrayIfNecessary =
false) {
848 resize(num - 1, shrinkUnderlyingArrayIfNecessary);
870 format(
"Array index out of bounds. n = %d, size() = %d", (
int)n, (
int)num));
910 return data[
iRandom(0, (
int)num - 1)];
916 return data[
iRandom(0, num - 1)];
926 return data[num - 1];
933 return data[num - 1];
967 return data[num >> 1];
973 return data[num >> 1];
981 for (
size_t i = 0; i <
num; ++i) {
997 for (
int i = num -1 ; i >= 0; --i) {
998 if (data[i] == value) {
1010 for (
size_t i = 0; i <
num; ++i) {
1011 if (data[i] == value) {
1023 for (
int i = 0; i <
num; ++i) {
1024 if (data[i] == value) {
1032 for (
int i = 0; i <
num; ++i) {
1033 if (data[i] == value) {
1044 void remove(Iterator element,
int count = 1) {
1047 Iterator
last =
end() - count;
1049 while(element < last) {
1050 element[0] = element[count];
1057 void remove(
int index,
int count = 1) {
1059 debugAssert((count > 0) && (index + count <= (
int)num));
1061 remove(
begin() + index, count);
1070 size_t n2 = num / 2;
1071 for (
size_t i = 0; i < n2; ++i) {
1072 temp = data[num - 1 - i];
1073 data[num - 1 - i] = data[i];
1108 template<
class LessThan>
1109 void sort(
const LessThan& lessThan) {
1112 std::sort(data, data + num, lessThan);
1128 void sort(
int direction = SORT_INCREASING) {
1129 if (direction == SORT_INCREASING) {
1130 std::sort(data, data + num);
1139 void sortSubArray(
int beginIndex,
int endIndex,
int direction = SORT_INCREASING) {
1140 if (direction == SORT_INCREASING) {
1141 std::sort(data + beginIndex, data + endIndex + 1);
1143 std::sort(data + beginIndex, data + endIndex + 1,
compareGT);
1147 void sortSubArray(
int beginIndex,
int endIndex,
bool (__cdecl *lessThan)(
const T& elem1,
const T& elem2)) {
1148 std::sort(data + beginIndex, data + endIndex + 1, lessThan);
1155 template<
typename StrictWeakOrdering>
1156 void sortSubArray(
int beginIndex,
int endIndex, StrictWeakOrdering& lessThan) {
1157 std::sort(data + beginIndex, data + endIndex + 1, lessThan);
1166 }
else if (A == B) {
1193 template<
typename Comparator>
1195 const T& partitionElement,
1215 Array<T>* bucket[3] = {<Array, &eqArray, >Array};
1217 for (
size_t i = 0; i <
num; ++i) {
1218 int c =
comparator(partitionElement, data[i]);
1219 debugAssertM(c >= -1 && c <= 1,
"Comparator returned an illegal value.");
1222 bucket[c + 1]->
append(data[i]);
1230 const T& partitionElement,
1246 template<
typename Comparator>
1308 int lowerHalfSize, upperHalfSize;
1310 lowerHalfSize =
size() / 2;
1311 upperHalfSize = lowerHalfSize + 1;
1313 lowerHalfSize = upperHalfSize = (
size() + 1) / 2;
1315 const T* xPtr =
NULL;
1329 xPtr = &(source->
middle());
1330 if (source->
size() == 1) {
1337 source->
partition(x, *lt, *eq, *gt, comparator);
1339 int L = lt->
size() + ltBoost + eq->
size();
1340 int U = gt->
size() + gtBoost + eq->
size();
1341 if ((L >= lowerHalfSize) &&
1342 (U >= upperHalfSize)) {
1347 }
else if (L < lowerHalfSize) {
1350 ltBoost += lt->
size() + eq->
size();
1355 Array<T>* newGt = (source ==
this) ? extra :
const_cast<Array<T>*
>(source);
1364 gtBoost += gt->
size() + eq->
size();
1369 Array<T>* newLt = (source ==
this) ? extra :
const_cast<Array<T>*
>(source);
1383 partition(median, ltMedian, eqMedian, gtMedian, comparator);
1397 medianPartition(ltMedian, eqMedian, gtMedian, temp, DefaultComparator());
1408 for (
int i =
size() - 1; i >= 0; --i) {
1421 const int oldSize =
size();
1435 for (
int i = 0; i <
size(); ++i) {
1439 data[nextNull] = data[i];
1451 template<
class T>
bool contains(
const T* array,
int len,
const T& e) {
1452 for (
int i = len - 1; i >= 0; --i) {
1453 if (array[i] == e) {
1463 # pragma warning (pop)
T * Iterator
Definition: Array.h:214
Iterator end()
Definition: Array.h:248
const T & randomElement() const
Definition: Array.h:913
void insert(int n, const T &value)
Definition: Array.h:456
void randomize()
Definition: Array.h:1405
void push(const T &value)
Definition: Array.h:770
T pop(bool shrinkUnderlyingArrayIfNecessary=true)
Definition: Array.h:837
int size_type
Definition: Array.h:225
bool notNull(const Pointer< T > &p)
Definition: Pointer.h:350
static MemoryManager::Ref create()
Definition: MemoryManager.cpp:35
void push_back(const T &v)
Definition: Array.h:779
void fastClear()
Definition: Array.h:419
void copyFrom(const Array< T > &other)
Definition: Array.h:341
void resize(size_t n, bool shrinkIfNecessary=true)
Definition: Array.h:490
static void memcpy(void *dst, const void *src, size_t numBytes)
Definition: System.cpp:643
T & operator[](int n)
Definition: Array.h:868
void reverse()
Definition: Array.h:1067
ConstIterator end() const
Definition: Array.h:244
T & randomElement()
Definition: Array.h:907
Array(const T &v0)
Definition: Array.h:287
T & middle()
Definition: Array.h:971
bool inArray(const T *address)
Definition: Array.h:135
void append(const T &v1, const T &v2, const T &v3, const T &v4)
Definition: Array.h:649
void push(const Array< T > &array)
Definition: Array.h:774
Array(const Array &other)
Definition: Array.h:331
ConstIterator const_iterator
Definition: Array.h:221
void sort(const LessThan &lessThan)
Definition: Array.h:1109
T value_type
Definition: Array.h:223
int capacity() const
Definition: Array.h:794
void pop_back()
Definition: Array.h:785
const T & operator[](uint32 n) const
Definition: Array.h:895
T * getCArray()
Definition: Array.h:256
Dynamic 1D array tuned for performance.
Definition: Array.h:95
const T & last() const
Definition: Array.h:923
arena_t NULL
Definition: jemalloc_internal.h:624
int length() const
Definition: Array.h:438
uint64_t uint64
Definition: g3dmath.h:170
shared_ptr< class MemoryManager > Ref
Definition: MemoryManager.h:31
void append(const T &v1, const T &v2, const T &v3)
Definition: Array.h:627
int middleIndex() const
Definition: Array.h:959
void popDiscard(bool shrinkUnderlyingArrayIfNecessary=false)
Definition: Array.h:846
#define debugAssertM(exp, message)
Definition: debugAssert.h:161
T max(const T &x, const T &y)
Definition: g3dmath.h:320
bool contains(const T *array, int len, const T &e)
Definition: Array.h:1451
void reserve(int n)
Definition: Array.h:1419
T & next()
Definition: Array.h:762
static void swap(Array< T, MIN_ELEMENTS > &a, Array< T, MIN_ELEMENTS > &b)
Definition: Array.h:265
void trimToSize()
Definition: Array.h:476
const T & operator[](uint64 n) const
Definition: Array.h:901
int comparator(const void *ke1, const void *ke2)
Definition: WatcherKqueue.cpp:28
void medianPartition(Array< T > <Median, Array< T > &eqMedian, Array< T > >Median, Array< T > &tempArray, const Comparator &comparator) const
Definition: Array.h:1247
const T & operator[](int n) const
Definition: Array.h:889
void append(const Array< T > &array)
Definition: Array.h:746
void sort(int direction=SORT_INCREASING)
Definition: Array.h:1128
const T * ConstIterator
Definition: Array.h:216
ConstIterator find(const T &value) const
Definition: Array.h:1031
size_t sizeInMemory() const
Definition: Array.h:1428
Iterator find(const T &value)
Definition: Array.h:1022
void append(const T &v1, const T &v2, const T &v3, const T &v4, const T &v5)
Definition: Array.h:673
T min(const T &x, const T &y)
Definition: g3dmath.h:305
void setAll(const T &value)
Definition: Array.h:467
void fastRemove(int index, bool shrinkIfNecessary=false)
Definition: Array.h:446
ConstIterator begin() const
Definition: Array.h:237
Array(const T &v0, const T &v1)
Definition: Array.h:293
const T * getCArray() const
Definition: Array.h:276
Iterator begin()
Definition: Array.h:233
const bool DONT_SHRINK_UNDERLYING_ARRAY
Definition: Array.h:43
int operator()(const T &A, const T &B) const
Definition: Array.h:1163
MemoryManager::Ref m_memoryManager
Definition: Array.h:108
T & front()
Definition: Array.h:803
#define debugAssert(exp)
Definition: debugAssert.h:160
void G3D_DEPRECATED deleteAll()
Definition: Array.h:988
Array & operator=(const std::vector< T > &other)
Definition: Array.h:199
void partition(const T &partitionElement, Array< T > <Array, Array< T > &eqArray, Array< T > >Array) const
Definition: Array.h:1229
size_t numAllocated
Definition: Array.h:106
const int SORT_DECREASING
Definition: Array.h:48
Array(const std::vector< T > &other)
Definition: Array.h:335
~Array()
Definition: Array.h:389
void copyPOD(const Array< T > &other)
Definition: Array.h:348
void init(size_t n, const MemoryManager::Ref &m)
Definition: Array.h:112
int difference_type
Definition: Array.h:227
T * data
Definition: Array.h:103
void clear(bool shrink=true)
Definition: Array.h:407
bool isEven(int num)
Definition: g3dmath.h:794
std::string __cdecl format(const char *fmt...) G3D_CHECK_PRINTF_ARGS
void clearAndSetMemoryManager(const MemoryManager::Ref &m)
Definition: Array.h:411
T & back()
Definition: Array.h:821
int size() const
Definition: Array.h:430
void medianPartition(Array< T > <Median, Array< T > &eqMedian, Array< T > >Median) const
Definition: Array.h:1391
int lastIndex() const
Definition: Array.h:937
const T & first() const
Definition: Array.h:953
Array(const T &v0, const T &v1, const T &v2, const T &v3, const T &v4)
Definition: Array.h:317
T & operator[](uint32 n)
Definition: Array.h:875
bool contains(const T &e) const
Definition: Array.h:732
void swap(Array< T > &str)
Definition: Array.h:858
MemoryManager::Ref memoryManager() const
Definition: Array.h:423
Array()
Definition: Array.h:281
int findIndex(const T &value) const
Definition: Array.h:1009
static const size_t MIN_BYTES
Definition: Array.h:100
void sortSubArray(int beginIndex, int endIndex, int direction=SORT_INCREASING)
Definition: Array.h:1139
size_t num
Definition: Array.h:105
const int SORT_INCREASING
Definition: Array.h:46
void append(const T &v1, const T &v2)
Definition: Array.h:605
void append(const T &v1, const T &v2, const T &v3, const T &v4, const T &v5, const T &v6)
Definition: Array.h:700
void _copy(const Array &other)
Definition: Array.h:124
const T & middle() const
Definition: Array.h:965
const T & front() const
Definition: Array.h:812
Array(const T &v0, const T &v1, const T &v2, const T &v3)
Definition: Array.h:308
int rfindIndex(const T &value) const
Definition: Array.h:996
int firstIndex() const
Definition: Array.h:942
void appendPOD(const Array< T > &other)
Definition: Array.h:366
Array & operator=(const Array &other)
Definition: Array.h:191
void partition(const T &partitionElement, Array< T > <Array, Array< T > &eqArray, Array< T > >Array, const Comparator &comparator) const
Definition: Array.h:1194
void realloc(size_t oldNum)
Definition: Array.h:151
void sortSubArray(int beginIndex, int endIndex, StrictWeakOrdering &lessThan)
Definition: Array.h:1156
const T & back() const
Definition: Array.h:830
T & first()
Definition: Array.h:948
const FieldDescriptor value
Definition: descriptor.h:1522
uint32_t uint32
Definition: g3dmath.h:168
void invokeDeleteOnAllElements()
Definition: Array.h:980
G3D::int16 x
Definition: Vector2int16.h:37
void removeNulls()
Definition: Array.h:1433
void append(const T &value)
Definition: Array.h:583
#define alwaysAssertM(exp, message)
Definition: debugAssert.h:165
int iRandom(int low, int hi)
Definition: g3dmath.cpp:110
T & last()
Definition: Array.h:930
Array(const T &v0, const T &v1, const T &v2)
Definition: Array.h:300
void sortSubArray(int beginIndex, int endIndex, bool(__cdecl *lessThan)(const T &elem1, const T &elem2))
Definition: Array.h:1147
static bool __cdecl compareGT(const T &a, const T &b)
Definition: Array.h:141
T & operator[](uint64 n)
Definition: Array.h:881
Iterator iterator
Definition: Array.h:219