20 #include <type_traits> 34 #define AGX_DECLARE_VECTOR_TYPES(type) \ 35 typedef agx::Vector<type ## Ref> type ## RefVector; \ 36 typedef agx::Vector<type ## Observer> type ## ObserverVector; \ 37 typedef agx::VectorPOD<type *> type ## PtrVector 40 #pragma warning (push) 41 # pragma warning( disable: 4345) // behavior change: an object of POD type constructed with an initializer of the form () will be default-initialized 51 template<
typename T,
typename Allocator = ByteAllocator >
92 void resize(
size_t size,
const T& value);
121 const T*
ptr()
const;
124 T&
at(
size_t index)
const;
128 template <
typename T2>
142 const_iterator
begin()
const;
143 const_iterator
end()
const;
145 reverse_iterator
rbegin();
146 reverse_iterator
rend();
147 const_reverse_iterator
rbegin()
const;
148 const_reverse_iterator
rend()
const;
153 iterator
erase(iterator position);
154 iterator
erase(iterator start, iterator end);
159 void erase(
size_t index);
160 void erase(
size_t start,
size_t end);
165 iterator
eraseFast(const_iterator position);
176 template <
typename T2>
177 bool contains(
const T2& element)
const;
179 bool contains(
const T& element)
const;
185 template <
typename T2>
186 size_t find(
const T2& element)
const;
194 template <
typename T2>
195 bool findAndErase(
const T2& element,
bool searchMultiple =
false);
197 iterator
insert(iterator position,
const T& value);
198 void insert(
size_t index,
const T& value);
200 template <
typename InputIterator>
201 void insert(const_iterator it, InputIterator first, InputIterator last);
203 void insert(const_iterator it, std::initializer_list<T> ilist);
212 size_t getNumActiveElements() const;
216 void reallocate(
size_t size);
217 void constructElements(
size_t startIndex,
size_t endIndex);
218 void constructElements(
size_t startIndex,
size_t endIndex, const T& value);
219 void destroyElements(
size_t startIndex,
size_t endIndex);
222 Real32 m_smoothingAverage;
225 template<typename T, typename A>
228 template<typename T, typename A>
231 template<typename T, typename A>
234 template<typename T, typename A>
235 typename
Vector<T, A>::const_iterator end(const
Vector<T, A>& v);
285 void resize(
size_t size,
const T& value);
311 const T*
ptr()
const;
314 T&
at(
size_t index)
const;
327 const_iterator
begin()
const;
328 const_iterator
end()
const;
330 reverse_iterator
rbegin();
331 reverse_iterator
rend();
332 const_reverse_iterator
rbegin()
const;
333 const_reverse_iterator
rend()
const;
338 iterator
erase(iterator position);
339 iterator
erase(iterator start, iterator end);
344 void erase(
size_t index);
345 void erase(
size_t start,
size_t end);
350 iterator
eraseFast(const_iterator position);
362 template <
typename T2>
363 bool contains(
const T2& element)
const;
365 bool contains(
const T& element)
const;
370 template <
typename T2>
371 size_t find(
const T2& element)
const;
379 bool findAndErase(
const T& element,
bool searchMultiple =
false);
381 iterator
insert(iterator position,
const T& value);
382 void insert(
size_t index,
const T& value);
384 template <
typename InputIterator>
385 void insert(const_iterator it, InputIterator first, InputIterator last);
387 void insert(const_iterator it, std::initializer_list<T> ilist);
395 size_t getNumActiveElements() const;
399 void reallocate(
size_t size);
400 void constructElements(
size_t startIndex,
size_t endIndex);
401 void constructElements(
size_t startIndex,
size_t endIndex, const T& value);
403 static T s_initializer;
406 Real32 m_smoothingAverage;
409 template<typename T, typename A>
412 template<typename T, typename A>
415 template<typename T, typename A>
418 template<typename T, typename A>
423 #define AGX_VECTOR_RESIZE_FACTOR 2.0f 424 #define AGX_VECTOR_SMOOTHING_FACTOR 0.8f 425 #define AGX_VECTOR_SHRINK_THRESHOLD 0.25f 426 #define AGX_VECTOR_MIN_SIZE 4 428 #define m_elements static_cast<T *>(this->m_buffer) 429 #define m_size this->m_size 430 #define m_capacity this->m_capacity 440 template <
typename T,
typename A>
446 template <
typename T,
typename A>
447 Vector<T, A>::Vector(
size_t size,
const T& value,
const A& allocator) : m_allocator(allocator), m_smoothingAverage(0.0)
450 this->resize(size, value);
453 template <
typename T,
typename A>
459 template <
typename T,
typename Allocator>
461 :Container(
std::move(other)), m_smoothingAverage(0.0)
464 m_allocator = other.m_allocator;
466 m_smoothingAverage = other.m_smoothingAverage;
470 other.m_smoothingAverage =
Real32(0.0);
473 template <
typename T,
typename A>
474 Vector<T, A>::Vector(const_iterator first, const_iterator end,
const A& allocator) : m_allocator(allocator), m_smoothingAverage(0.0)
477 this->insert(this->
begin(), first, end);
480 template <
typename T,
typename A>
482 : m_allocator(allocator), m_smoothingAverage(0.0)
490 template <
typename T,
typename A>
496 this->destroyElements(0,
m_size);
498 if (policy == MAINTAIN_BUFFER)
502 else if (policy == SHRINK_BUFFER)
508 m_smoothingAverage = 0.0;
524 this->reallocate((
size_t)m_smoothingAverage);
530 template <
typename T,
typename A>
539 template <
typename T,
typename A>
545 this->reallocate(size);
547 this->constructElements(
m_size, size);
552 this->reallocate(size);
554 this->destroyElements(size,
m_size);
561 template <
typename T,
typename A>
567 this->reallocate(size);
569 this->constructElements(
m_size, size, value);
574 this->reallocate(size);
576 this->destroyElements(size,
m_size);
582 template <
typename T,
typename A>
586 this->reallocate(size);
591 template <
typename T,
typename A>
596 while (size > newCapacity)
598 this->reallocate(newCapacity);
604 template <
typename T,
typename A>
607 size_t newSize =
m_size + numElements;
611 this->constructElements(
m_size, newSize);
618 template <
typename T,
typename A>
624 for (
size_t i = 0; i <
m_size; i++)
631 template <
typename T,
typename A>
637 for (
size_t i = 0; i <
m_size; i++)
645 template <
typename T,
typename A>
651 this->
clear(MAINTAIN_BUFFER);
652 m_allocator = other.m_allocator;
660 for (
size_t i = 0; i < other.
size(); i++)
668 template <
typename T,
typename A>
671 this->destroyElements(0,
m_size);
676 template <
typename T,
typename A>
679 template <
typename T,
typename A>
683 template <
typename T,
typename A>
686 agxAssertN(i <
m_size,
"Array index %llu is out of bounds, current size is %llu", (
long long unsigned)i, (
long long unsigned)
m_size);
690 template <
typename T,
typename A>
693 agxVerifyN(index <
m_size,
"Array index %llu is out of bounds, current size is %llu", (
long long unsigned)index, (
long long unsigned)
m_size);
698 template <
typename T,
typename A>
706 template <
typename T,
typename A>
714 template <
typename T,
typename A>
template <
typename T2>
726 template <
typename T,
typename A>
740 template <
typename T,
typename A>
753 template <
typename T,
typename A>
764 template <
typename T,
typename A>
767 size_t index = position - this->
begin();
769 return this->
begin() + index;
772 template <
typename T,
typename A>
775 size_t startIndex = start - this->
begin();
776 size_t endIndex = end - this->
begin();
777 this->erase(startIndex, endIndex);
779 return this->
begin() + endIndex;
783 template <
typename T,
typename A>
791 for (
size_t i = index + 1; i <
m_size; i++)
805 template <
typename T,
typename A>
808 agxAssert1(start < end && end <=
m_size,
"Erase bounds are not with array bounds!");
809 size_t numElements = end - start;
812 for (
size_t i = end; i <
m_size; i++)
816 this->destroyElements(m_size-numElements, m_size);
818 m_size -= numElements;
824 template <
typename T,
typename A>
827 size_t index = position - this->
begin();
828 this->eraseFast(index);
829 return this->
begin() + index;
832 template <
typename T,
typename A>
835 agxAssertN(index <
m_size,
"Array index %llu is out of bounds, current size is %llu", (
long long unsigned)index, (
long long unsigned)
m_size);
842 template <
typename T,
typename A>
template <
typename T2>
845 return this->find(element) !=
m_size;
848 template <
typename T,
typename A>
851 return this->find(element) !=
m_size;
855 template <
typename T,
typename A>
template <
typename T2>
858 for (
size_t i = 0; i <
m_size; i++)
867 template <
typename T,
typename A>
template <
typename T2>
872 for (
size_t i = 0; i <
m_size; i++)
890 template <
typename T,
typename A>
893 size_t index = position - this->
begin();
894 this->insert(index, value);
895 return this->
begin() + index;
898 template <
typename T,
typename A>
template <
typename InputIterator>
902 size_t numElements = std::distance( first, last );
904 if (numElements == 0)
907 size_t index = it - this->
begin();
912 for (
size_t i =
m_size; i > index; i--) {
917 for (; first != last; first++)
918 ::
new((
void *)&
m_elements[index++]) T(*first);
923 template <
typename T,
typename A>
926 this->insert(it, ilist.begin(), ilist.end());
929 template <
typename T,
typename A>
932 agxAssertN(index <=
m_size,
"Insert with invalid index %llu, size is %llu", (
long long unsigned)index, (
long long unsigned)
m_size);
937 for (
size_t i = m_size; i > index; i--) {
946 template <
typename T,
typename A>
952 this->destroyElements(size,
m_size);
959 T *newBuf =
reinterpret_cast<T*
>(m_allocator.
allocateBytes(size *
sizeof(T)));
964 for (
size_t i = 0; i <
m_size; i++)
969 this->destroyElements(0, m_size);
972 m_buffer = (
void *)newBuf;
977 template <
typename T,
typename A>
980 for (
size_t i = startIndex; i < endIndex; i++)
984 template <
typename T,
typename A>
987 for (
size_t i = startIndex; i < endIndex; i++)
991 template <
typename T,
typename A>
994 for (
size_t i = startIndex; i < endIndex; i++)
1000 template <
typename T,
typename A>
1003 template <
typename T,
typename A>
1007 template <
typename T,
typename A>
1011 template <
typename VectorT>
1014 for (
size_t i = 0; i < vec.size(); i++)
1018 if (i < vec.size()-1)
1025 template <
typename T,
typename A>
1026 std::ostream& operator << ( std::ostream& output, const Vector<T, A>& vec)
1032 template <
typename T,
typename A>
1033 std::ostream& operator << ( std::ostream& output, const VectorPOD<T, A>& vec)
1040 template <
typename T,
typename A>
1043 template <
typename T,
typename A>
1046 template <
typename T,
typename A>
1049 template <
typename T,
typename A>
1052 template <
typename T,
typename A>
1055 template <
typename T,
typename A>
1058 template <
typename T,
typename A>
1061 template <
typename T,
typename A>
1067 template<
typename T,
typename A>
1070 template<
typename T,
typename A>
1074 template <
typename T,
typename A>
1077 template <
typename T,
typename A>
1092 template <
typename T,
typename A>
1095 static_assert( std::is_trivially_destructible<T>::value,
"Non-POD type used in VectorPOD");
1099 template <
typename T,
typename A>
1103 this->resize(size, value);
1106 template <
typename T,
typename A>
1114 template <
typename T,
typename A>
1116 : Container(
std::move(other)), m_smoothingAverage(0.0)
1119 m_allocator = std::move(other.m_allocator);
1121 m_smoothingAverage = other.m_smoothingAverage;
1124 other.m_allocator = A();
1125 other.m_smoothingAverage =
Real32(0);
1130 template <
typename T,
typename A>
1134 this->insert(this->
begin(), first, end);
1137 template <
typename T,
typename A>
1139 : m_allocator(allocator), m_smoothingAverage(0.0)
1148 template <
typename T,
typename A>
1154 if (policy == MAINTAIN_BUFFER)
1158 else if (policy == SHRINK_BUFFER)
1164 m_smoothingAverage = 0.0;
1180 this->reallocate((
size_t)m_smoothingAverage);
1185 template <
typename T,
typename A>
1190 this->reallocate(
m_size);
1195 template <
typename T,
typename A>
1201 this->reallocate(size);
1203 this->constructElements(
m_size, size);
1208 this->reallocate(size);
1215 template <
typename T,
typename A>
1221 this->reallocate(size);
1223 this->constructElements(
m_size, size, value);
1228 this->reallocate(size);
1234 template <
typename T,
typename A>
1238 this->reallocate(size);
1242 template <
typename T,
typename A>
1247 while (size > newCapacity)
1249 this->reallocate(newCapacity);
1254 template <
typename T,
typename A>
1257 size_t newSize =
m_size + numElements;
1261 this->constructElements(
m_size, newSize);
1268 template <
typename T,
typename A>
1274 for (
size_t i = 0; i <
m_size; i++)
1281 template <
typename T,
typename A>
1287 for (
size_t i = 0; i <
m_size; i++)
1295 template <
typename T,
typename A>
1301 m_allocator = other.m_allocator;
1305 this->reallocate(other.
size());
1307 if (other.
size() > 0) {
1316 template <
typename T,
typename A>
1323 template <
typename T,
typename A>
1326 template <
typename T,
typename A>
1329 template <
typename T,
typename A>
1332 agxAssertN(i <
m_size,
"Array index %llu is out of bounds, current size is %llu", (
long long unsigned)i, (
long long unsigned)
m_size);
1337 template <
typename T,
typename A>
1340 agxVerifyN(index <
m_size,
"Array index %llu is out of bounds, current size is %llu", (
long long unsigned)index, (
long long unsigned)
m_size);
1345 template <
typename T,
typename A>
1353 template <
typename T,
typename A>
1361 template <
typename T,
typename A>
1373 template <
typename T,
typename A>
1385 template <
typename T,
typename A>
1394 template <
typename T,
typename A>
1397 size_t index = position - this->
begin();
1399 return this->
begin() + index;
1402 template <
typename T,
typename A>
1405 size_t startIndex = start - this->
begin();
1406 size_t endIndex = end - this->
begin();
1407 this->erase(startIndex, endIndex);
1409 return this->
begin() + endIndex;
1413 template <
typename T,
typename A>
1416 this->erase(index, index+1);
1419 template <
typename T,
typename A>
1422 agxAssert1(start < end && end <=
m_size,
"Erase bounds are not with array bounds!");
1423 size_t numElements = end - start;
1426 for (
size_t i = end; i <
m_size; i++)
1429 m_size -= numElements;
1435 template <
typename T,
typename A>
1438 size_t index = position - this->
begin();
1439 this->eraseFast(index);
1440 return this->
begin() + index;
1443 template <
typename T,
typename A>
1446 agxAssertN(index <
m_size,
"Array index %llu is out of bounds, current size is %llu", (
long long unsigned)index, (
long long unsigned)
m_size);
1451 template <
typename T,
typename A>
template <
typename T2>
1454 return this->find(element) !=
m_size;
1457 template <
typename T,
typename A>
1460 return this->find(element) !=
m_size;
1464 template <
typename T,
typename A>
template <
typename T2>
1467 for (
size_t i = 0; i <
m_size; i++)
1477 template <
typename T,
typename A>
1482 for (
size_t i = 0; i <
m_size; i++)
1489 if (!searchMultiple)
1500 template <
typename T,
typename A>
1504 size_t index = position - this->
begin();
1505 this->insert(index, value);
1506 return this->
begin() + index;
1510 template <
typename T,
typename A>
1513 this->insert(it, ilist.begin(), ilist.end());
1516 template <
typename T,
typename A>
template <
typename InputIterator>
1520 size_t numElements = std::distance( first, last );
1521 if (numElements == 0)
1524 size_t index = it - this->
begin();
1529 for (
size_t i =
m_size; i > index; i--)
1532 for (; first != last; first++)
1533 memcpy(&
m_elements[index++], &(*first),
sizeof(T));
1538 template <
typename T,
typename A>
1544 for (
size_t i =
m_size; i > index; i--)
1547 memcpy(&
m_elements[index], &value,
sizeof(T));
1553 template <
typename T,
typename A>
1566 memcpy(newBuf, oldBuf,
m_size *
sizeof(T));
1577 template <
typename T,
typename A>
1580 for (
size_t i = startIndex; i < endIndex; i++)
1581 memcpy(&
m_elements[i], &s_initializer,
sizeof(T));
1584 template <
typename T,
typename A>
1587 for (
size_t i = startIndex; i < endIndex; i++)
1588 memcpy((
void*)&
m_elements[i], (
void*)&value,
sizeof(T));
1592 template <
typename T,
typename A>
1595 template <
typename T,
typename A>
1599 template <
typename T,
typename A>
1604 template <
typename T,
typename A>
1608 template <
typename T,
typename A>
1611 template <
typename T,
typename A>
1614 template <
typename T,
typename A>
1617 template <
typename T,
typename A>
1620 template <
typename T,
typename A>
1623 template <
typename T,
typename A>
1626 template <
typename T,
typename A>
1629 template <
typename T,
typename A>
1633 template<
typename T,
typename A>
1636 template<
typename T,
typename A>
1639 template <
typename T,
typename A>
1642 template <
typename T,
typename A>
1650 template <
typename T,
typename A>
1656 std::swap(this->m_smoothingAverage, other.m_smoothingAverage);
1659 template <
typename T,
typename A>
1665 std::swap(this->m_smoothingAverage, other.m_smoothingAverage);
1671 #pragma warning (pop) #define AGX_VECTOR_RESIZE_FACTOR
iterator insert(iterator position, const T &value)
AGXTERRAIN_EXPORT void clear()
The Container is the base class for several of the container classes proided by AGX, such as agx::Vector and agx::HashSet.
T & operator[](size_t i) const
size_t find(const T2 &element) const
Find the index to a matching element, return size() if not found.
Vec3T< T > max(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
#define agxVerifyN(expr, format,...)
#define agxAssertN(expr, format,...)
VectorPOD< T, A >::const_iterator end(const VectorPOD< T, A > &v)
void swap(VectorPOD &other)
T * increment(size_t numElements=1)
Resize using a increment.
bool operator==(T val, InvalidIndexStruct)
iterator erase(iterator position)
STL erase functionality.
bool contains(const T2 &element) const
Test if the vector contains a certain element.
const T & const_reference
ClearPolicy
agxData::Values from this enumeration is passed to the subclasses' 'clear' method in order to control...
std::reverse_iterator< const_iterator > const_reverse_iterator
VectorPOD< T, A >::const_iterator begin(const VectorPOD< T, A > &v)
void reserve(size_t size)
Reserve capacity in the vector.
const T & const_reference
Vector< T, Allocator > & operator=(const Vector< T, Allocator > &other)
ptrdiff_t difference_type
reverse_iterator rbegin()
Vector containing 'raw' data.
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
bool operator!=(const Vector< T, Allocator > &other) const
void deallocateBytes(void *buffer, size_t numBytes)
Deallocate a section previously allocated using allocateBytes method.
size_t capacity() const
Returns the size of the memory are used by the container to store its elements.
T & at(size_t index) const
bool findAndErase(const T2 &element, bool searchMultiple=false)
Find and erase an element.
std::ostream & printVector(std::ostream &output, const VectorT &vec)
void resize(size_t size)
Resize the vector, which then enables direct addressing using the bracket '[]' operator.
void clear(ClearPolicy policy=SHRINK_BUFFER_AVERAGED)
Remove all elements, optionally with maintained buffer allocation.
void * allocateBytes(size_t numBytes, size_t alignment=16)
Allocate a memory section of specified alignment.
void reserveAtLeast(size_t size)
Reserve capacity in the vector.
void setContainer(class Container *)
Vector(const Allocator &allocator=Allocator())
std::reverse_iterator< iterator > reverse_iterator
#define AGX_VECTOR_MIN_SIZE
std::reverse_iterator< const_iterator > const_reverse_iterator
#define AGX_VECTOR_SMOOTHING_FACTOR
#define agxAssert1(expr, msg)
T * ptr()
Get access to the internal data buffer.
Buffer is shrunk if a smoothing average (which is updated each clear call) goes below a threshold...
bool operator==(const Vector< T, Allocator > &other) const
Compare with other vector, return true if same size and all pairs are equal.
iterator eraseFast(const_iterator position)
Fast erase, replacing the erased element with the last element.
void shrink_to_fit()
Reduce the capacity of the vector to the actual size (number of elements)
#define AGX_VECTOR_SHRINK_THRESHOLD
bool operator!=(T val, InvalidIndexStruct)
std::reverse_iterator< iterator > reverse_iterator
std::random_access_iterator_tag iterator_category
ptrdiff_t difference_type
void swap(VectorPOD< T > &x, VectorPOD< T > &y)
void push_back(const T2 &value)
#define DOXYGEN_END_INTERNAL_BLOCK()
#define DOXYGEN_START_INTERNAL_BLOCK()
T * ptr()
Get access to the internal data buffer.