1 #ifndef SimTK_SimTKCOMMON_ARRAY_H_
2 #define SimTK_SimTKCOMMON_ARRAY_H_
50 template <
class T,
class X=
unsigned>
class Array_;
225 {
return (
unsigned long long)LLONG_MAX;}
246 template <
class Integral,
class is64Bit>
struct ArrayIndexPackTypeHelper
247 {
typedef Integral packed_size_type;};
250 template<>
struct ArrayIndexPackTypeHelper<bool,FalseType>
251 {
typedef unsigned short packed_size_type;};
252 template<>
struct ArrayIndexPackTypeHelper<char,FalseType>
253 {
typedef unsigned short packed_size_type;};
254 template<>
struct ArrayIndexPackTypeHelper<unsigned char,FalseType>
255 {
typedef unsigned short packed_size_type;};
256 template<>
struct ArrayIndexPackTypeHelper<signed char,FalseType>
257 {
typedef short packed_size_type;};
260 template<>
struct ArrayIndexPackTypeHelper<bool,TrueType>
261 {
typedef unsigned int packed_size_type;};
262 template<>
struct ArrayIndexPackTypeHelper<char,TrueType>
263 {
typedef unsigned int packed_size_type;};
264 template<>
struct ArrayIndexPackTypeHelper<unsigned char,TrueType>
265 {
typedef unsigned int packed_size_type;};
266 template<>
struct ArrayIndexPackTypeHelper<signed char,TrueType>
267 {
typedef int packed_size_type;};
268 template<>
struct ArrayIndexPackTypeHelper<unsigned short,TrueType>
269 {
typedef unsigned int packed_size_type;};
270 template<>
struct ArrayIndexPackTypeHelper<short,TrueType>
271 {
typedef int packed_size_type;};
273 template <
class Integral>
struct ArrayIndexPackType
274 {
typedef typename ArrayIndexPackTypeHelper<Integral,Is64BitPlatformType>
275 ::packed_size_type packed_size_type;};
312 template <
class T,
class X>
class ArrayViewConst_ {
348 typedef typename ArrayIndexPackType<size_type>::packed_size_type
373 : pData(0), nUsed(src.nUsed), nAllocated(0) {
374 if (nUsed) pData =
const_cast<T*
>(src.pData);
402 : pData(0),nUsed(0),nAllocated(0) {
403 if (last1==first)
return;
406 "ArrayViewConst_<T>(first,last1)",
407 "One of the source pointers was null (0); either both must be"
408 " non-null or both must be null.");
411 "ArrayViewConst_<T>(first,last1)",
412 "The source data's size %llu is too big for this array which"
413 " is limited to %llu elements by its index type %s.",
414 this->ull(last1-first), ullMaxSize(), indexName());
416 pData =
const_cast<T*
>(first);
450 : pData(0),nUsed(0),nAllocated(0) {
451 if (src.empty())
return;
454 "ArrayViewConst_<T>::ctor(std::vector<T>)",
455 "The source std::vector's size %llu is too big for this array which"
456 " is limited to %llu elements by its index type %s.",
457 this->ull(src.size()), ullMaxSize(), indexName());
459 pData =
const_cast<T*
>(&src.front());
470 {
return *
reinterpret_cast<const Array_<T,X>*
>(
this); }
479 "ArrayViewConst_::deallocate(): called on an owner Array_");
506 bool empty()
const {
return nUsed==0;}
521 bool isOwner()
const {
return nAllocated || pData==0;}
572 return pData[nUsed-1]; }
595 "For this operator, we must have 0 <= index <= size(), but"
596 " index==%llu and size==%llu.", this->ull(ix), ullSize());
598 "ArrayViewConst_<T>(index,length)",
599 "This operator requires 0 <= length <= size()-index, but"
600 " length==%llu and size()-index==%llu.",this->ull(length),this->ull(
size()-ix));
607 {
return (*
this)(index,length); }
631 const T*
cend()
const {
return pData + nUsed;}
633 const T*
begin()
const {
return pData;}
635 const T*
end()
const {
return pData + nUsed;}
655 const T*
cdata()
const {
return pData;}
657 const T*
data()
const {
return pData;}
674 void setData(
const T* p) {pData =
const_cast<T*
>(p);}
676 void incrSize() {++nUsed;}
677 void decrSize() {--nUsed;}
683 bool isSameSize(S sz)
const
684 {
return ull(sz) == ullSize(); }
689 bool isSizeOK(S srcSz)
const
690 {
return ull(srcSz) <= ullMaxSize(); }
697 template<
class Iter>
static
698 typename std::iterator_traits<Iter>::difference_type
699 iterDistance(
const Iter& first,
const Iter& last1) {
700 return iterDistanceImpl(first,last1,
701 typename std::iterator_traits<Iter>::iterator_category());
707 template<
class Iter>
static
708 typename std::iterator_traits<Iter>::difference_type
709 iterDistanceImpl(
const Iter& first,
const Iter& last1, std::input_iterator_tag) {
710 typename std::iterator_traits<Iter>::difference_type d = 0;
711 for (Iter src=first; src != last1; ++src, ++d)
718 template<
class Iter>
static
719 typename std::iterator_traits<Iter>::difference_type
720 iterDistanceImpl(
const Iter& first,
const Iter& last1,
721 std::random_access_iterator_tag) {
722 return last1 - first;
734 template<
class Iter>
bool
735 overlapsWithData(
const Iter& first,
const Iter& last1) {
736 return overlapsWithDataImpl(first,last1,
737 typename std::iterator_traits<Iter>::iterator_category());
742 template <
class T2>
bool
743 overlapsWithData(
const T2* first,
const T2* last1) {
752 return obegin < oend1;
757 template<
class Iter>
bool
758 overlapsWithDataImpl(
const Iter&,
const Iter&, std::input_iterator_tag)
763 template<
class Iter>
bool
764 overlapsWithDataImpl(
const Iter& first,
const Iter& last1,
765 std::random_access_iterator_tag) {
776 return overlapsWithData(&*first, &*(last1-1));
783 static unsigned long long ull(S sz)
784 {
return (
unsigned long long)sz; }
787 unsigned long long ullSize()
const {
return ull(
size());}
788 unsigned long long ullCapacity()
const {
return ull(
capacity());}
789 unsigned long long ullMaxSize()
const {
return ull(
max_size());}
792 const char* indexName()
const {
return NiceTypeName<X>::name();}
828 template <
class T,
class X>
class ArrayView_ :
public ArrayViewConst_<T,X> {
829 typedef ArrayViewConst_<T,X> CBase;
849 typedef typename ArrayIndexPackType<size_type>::packed_size_type
876 {
return *
reinterpret_cast<const Array_<T,X>*
>(
this); }
908 avAssignIteratorDispatch(src.cbegin(), src.cend(),
909 std::random_access_iterator_tag(),
910 "ArrayView_<T>::operator=(ArrayView_<T>)");
917 template <
class T2,
class X2>
919 if ((
const void*)&src != (
void*)
this)
920 avAssignIteratorDispatch(src.cbegin(), src.cend(),
921 std::random_access_iterator_tag(),
922 "ArrayView_<T>::operator=(Array_<T2>)");
931 template <
class T2,
class X2>
936 template <
class T2,
class X2>
942 template <
class T2,
class A2>
944 avAssignIteratorDispatch(src.begin(), src.end(),
945 std::random_access_iterator_tag(),
946 "ArrayView_<T>::operator=(std::vector<T2>)");
952 {
fill(fillValue);
return *
this; }
960 for (T* d =
begin(); d !=
end(); ++d)
970 "Assignment to an ArrayView is permitted only if the source"
971 " is the same size. Here n==%llu element(s) but the"
972 " ArrayView has a fixed size of %llu.",
973 this->ull(n), this->ull(
size()));
996 void assign(
const T2* first,
const T2* last1) {
997 const char* methodName =
"ArrayView_<T>::assign(T2* first, T2* last1)";
998 SimTK_ERRCHK((first&&last1)||(first==last1), methodName,
999 "One of the source pointers was null (0); either both must be"
1000 " non-null or both must be null.");
1002 avAssignIteratorDispatch(first, last1, std::random_access_iterator_tag(),
1037 template <
class Iter>
1038 void assign(
const Iter& first,
const Iter& last1)
1039 { avAssignDispatch(first,last1,
typename IsIntegralType<Iter>::Result(),
1040 "ArrayView_<T>::assign(Iter first, Iter last1)"); }
1137 "For this operator, we must have 0 <= index <= size(), but"
1138 " index==%llu and size==%llu.", this->ull(ix), ullSize());
1140 "ArrayView_<T>(index,length)",
1141 "This operator requires 0 <= length <= size()-index, but"
1142 " length==%llu and size()-index==%llu.",this->ull(length),this->ull(
size()-ix));
1149 {
return (*
this)(index,length); }
1258 template <
class IntegralType>
1259 void avAssignDispatch(IntegralType n, IntegralType v, TrueType isIntegralType,
1266 template <
class InputIterator>
1267 void avAssignDispatch(
const InputIterator& first,
const InputIterator& last1,
1268 FalseType isIntegralType,
const char* methodName)
1269 { avAssignIteratorDispatch(first, last1,
1270 typename std::iterator_traits<InputIterator>::iterator_category(),
1279 template <
class InputIterator>
1280 void avAssignIteratorDispatch(
const InputIterator& first,
1281 const InputIterator& last1,
1282 std::input_iterator_tag,
1283 const char* methodName)
1286 InputIterator src = first;
1287 while (src != last1 && p !=
end())
1293 "The supplied input_iterator provided only %llu elements but this"
1294 " ArrayView has a fixed size of %llu elements.",
1295 this->ull(nCopied), ullSize());
1304 template <
class ForwardIterator>
1305 void avAssignIteratorDispatch(
const ForwardIterator& first,
1306 const ForwardIterator& last1,
1307 std::forward_iterator_tag,
1308 const char* methodName)
1311 ForwardIterator src = first;
1312 while (src != last1 && p !=
end())
1318 "The supplied forward_ or bidirectional_iterator source range provided"
1319 " only %llu elements but this ArrayView has a fixed size of"
1320 " %llu elements.", this->ull(nCopied), ullSize());
1324 "The supplied forward_ or bidirectional_iterator source range"
1325 " contained too many elements; this ArrayView has a fixed size of"
1326 " %llu elements.", ullSize());
1333 template <
class RandomAccessIterator>
1334 void avAssignIteratorDispatch(
const RandomAccessIterator& first,
1335 const RandomAccessIterator& last1,
1336 std::random_access_iterator_tag,
1337 const char* methodName)
1340 "Assignment to an ArrayView is permitted only if the source"
1341 " is the same size. Here the source had %llu element(s) but the"
1342 " ArrayView has a fixed size of %llu.",
1343 this->ull(last1-first), this->ull(
size()));
1346 "Source range can't overlap with the destination data.");
1349 RandomAccessIterator src = first;
1367 unsigned long long ullSize()
const {
return this->CBase::ullSize();}
1368 unsigned long long ullCapacity()
const {
return this->CBase::ullCapacity();}
1369 unsigned long long ullMaxSize()
const {
return this->CBase::ullMaxSize();}
1372 const char* indexName()
const {
return this->CBase::indexName();}
1484 template <
class T,
class X>
class Array_ :
public ArrayView_<T,X> {
1485 typedef ArrayView_<T,X> Base;
1486 typedef ArrayViewConst_<T,X> CBase;
1510 typedef typename ArrayIndexPackType<size_type>::packed_size_type
1532 allocateNoConstruct(n);
1533 defaultConstruct(
data(),
data()+n);
1543 allocateNoConstruct(
size());
1544 fillConstruct(
begin(),
cend(), initVal);
1561 template <
class InputIter>
1562 Array_(
const InputIter& first,
const InputIter& last1) :
Base() {
1563 ctorDispatch(first,last1,
typename IsIntegralType<InputIter>::Result());
1573 SimTK_ERRCHK((first&&last1)||(first==last1),
"Array_<T>(first,last1)",
1574 "Pointers must be non-null unless they are both null.");
1575 SimTK_ERRCHK3(this->isSizeOK(last1-first),
"Array_<T>(first,last1)",
1576 "Source has %llu elements but this array is limited to %llu"
1577 " elements by its index type %s.",
1578 this->ull(last1-first), ullMaxSize(), indexName());
1581 allocateNoConstruct(
size());
1591 if (v.empty())
return;
1593 SimTK_ERRCHK3(this->isSizeOK(v.size()),
"Array_<T>::ctor(std::vector<T2>)",
1594 "The source std::vector's size %llu is too big for this array which"
1595 " is limited to %llu elements by its index type %s.",
1596 this->ull(v.size()), ullMaxSize(), indexName());
1601 new (
this)
Array_(&v.front(), (&v.back())+1);
1609 setSize(src.size());
1610 allocateNoConstruct(
size());
1611 copyConstruct(
begin(),
cend(), src.data());
1620 template <
class T2,
class X2>
1622 new (
this)
Array_(src.begin(), src.cend());
1654 Array_(T* first,
const T* last1,
const DontCopy&) :
Base(first,last1) {}
1704 deallocateNoDestruct();
1767 SimTK_ERRCHK3(this->isSizeOK(n),
"Array_<T>::assign(n,value)",
1768 "Requested size %llu is too big for this array which is limited"
1769 " to %llu elements by its index type %s.",
1770 this->ull(n), ullMaxSize(), indexName());
1773 "Requested size %llu is not allowed because this is a non-owner"
1774 " array of fixed size %llu.", this->ull(n), this->ull(
size()));
1780 reallocateIfAdvisable(n);
1781 fillConstruct(
data(),
cdata()+n, fillValue);
1825 void assign(
const T2* first,
const T2* last1) {
1826 const char* methodName =
"Array_<T>::assign(T2* first, T2* last1)";
1827 SimTK_ERRCHK((first&&last1)||(first==last1), methodName,
1828 "Pointers must be non-null unless they are both null.");
1829 SimTK_ERRCHK(!this->overlapsWithData(first,last1), methodName,
1830 "Source range can't overlap the current array contents.");
1832 assignIteratorDispatch(first,last1,std::random_access_iterator_tag(),
1872 template <
class Iter>
1873 void assign(
const Iter& first,
const Iter& last1) {
1874 assignDispatch(first,last1,
typename IsIntegralType<Iter>::Result(),
1875 "Array_<T>::assign(Iter first, Iter last1)");
1885 assignIteratorDispatch(src.begin(), src.end(),
1886 std::random_access_iterator_tag(),
1887 "Array_<T>::operator=(Array_<T>)");
1896 template <
class T2,
class X2>
1898 assignIteratorDispatch(src.begin(), src.end(),
1899 std::random_access_iterator_tag(),
1900 "Array_<T>::operator=(Array_<T2,X2>)");
1909 template <
class T2,
class A>
1911 assignIteratorDispatch(src.begin(), src.end(),
1912 std::random_access_iterator_tag(),
1913 "Array_<T>::operator=(std::vector)");
1924 T*
const pTmp=
data(); setData(other.data()); other.setData(pTmp);
1925 size_type nTmp=
size(); setSize(other.size()); other.setSize(nTmp);
1926 nTmp=
allocated(); setAllocated(other.allocated()); other.setAllocated(nTmp);
1938 SimTK_ERRCHK2(dataSize <= dataCapacity,
"Array_<T>::adoptData()",
1939 "Specified data size %llu was greater than the specified data"
1940 " capacity of %llu.", this->ull(dataSize), this->ull(dataCapacity));
1941 SimTK_ERRCHK(newData || dataCapacity==0,
"Array_<T>::adoptData()",
1942 "A null data pointer is allowed only if the size and capacity are"
1943 " specified as zero.");
1944 SimTK_ERRCHK(!this->overlapsWithData(newData, newData+dataSize),
1945 "Array_<T>::adoptData()",
1946 "The new data can't overlap with the old data.");
1951 setAllocated(dataCapacity);
1957 {
return adoptData(newData, dataSize, dataSize); }
1975 SimTK_ERRCHK(newData || dataSize==0,
"Array_<T>::shareData()",
1976 "A null data pointer is allowed only if the size is zero.");
1977 SimTK_ERRCHK(!this->overlapsWithData(newData, newData+dataSize),
1978 "Array_<T>::shareData()",
1979 "The new data can't overlap with the old data.");
1991 SimTK_ERRCHK3(this->isSizeOK(last1-first),
"Array_<T>::shareData(first,last1)",
1992 "Requested size %llu is too big for this array which is limited"
1993 " to %llu elements by its index type %s.",
1994 this->ull(last1-first), ullMaxSize(), indexName());
2031 if (n ==
size())
return;
2034 "Requested size change to %llu is not allowed because this is a"
2035 " non-owner array of fixed size %llu.", this->ull(n), this->ull(
size()));
2052 if (n ==
size())
return;
2055 "Requested size change to %llu is not allowed because this is a"
2056 " non-owner array of fixed size %llu.", this->ull(n), this->ull(
size()));
2079 "Requested capacity change to %llu is not allowed because this is a"
2080 " non-owner array of fixed size %llu.", this->ull(n), this->ull(
size()));
2082 T* newData = allocN(n);
2083 copyConstructThenDestructSource(newData, newData+
size(),
data());
2114 T* newData = allocN(
size());
2115 copyConstructThenDestructSource(newData, newData+
size(),
data());
2116 deallocateNoDestruct();
2118 setAllocated(
size());
2330 if (pallocated() == psize())
2331 growAtEnd(1,
"Array_<T>::push_back(value)");
2332 copyConstruct(
end(), value);
2350 if (pallocated() == psize())
2351 growAtEnd(1,
"Array_<T>::push_back()");
2352 defaultConstruct(
end());
2372 if (pallocated() == psize())
2373 growAtEnd(1,
"Array_<T>::raw_push_back()");
2406 "Array<T>::erase(first,last1)",
"Pointers out of range or out of order.");
2410 "No elements can be erased from a non-owner array.");
2413 destruct(first, last1);
2414 moveElementsDown(first+nErased, nErased);
2415 setSize(
size()-nErased);
2441 "Array<T>::erase(p)",
"Pointer must point to a valid element.");
2443 "No elements can be erased from a non-owner array.");
2446 moveElementsDown(p+1, 1);
2474 "Array<T>::eraseFast(p)",
"Pointer must point to a valid element.");
2476 "No elements can be erased from a non-owner array.");
2480 moveOneElement(p, &
back());
2494 "clear() is not allowed for a non-owner array.");
2527 T*
const gap = insertGapAt(p, n,
"Array<T>::insert(p,n,value)");
2529 fillConstruct(gap, gap+n, value);
2539 T*
const gap = insertGapAt(p, 1,
"Array<T>::insert(p,value)");
2541 copyConstruct(gap, value);
2576 T*
insert(T* p,
const T2* first,
const T2* last1) {
2577 const char* methodName =
"Array_<T>::insert(T* p, T2* first, T2* last1)";
2578 SimTK_ERRCHK((first&&last1) || (first==last1), methodName,
2579 "One of first or last1 was null; either both or neither must be null.");
2580 SimTK_ERRCHK(!this->overlapsWithData(first,last1), methodName,
2581 "Source range can't overlap with the current array contents.");
2583 return insertIteratorDispatch(p, first, last1,
2584 std::random_access_iterator_tag(),
2590 template <
class Iter>
2591 T*
insert(T* p,
const Iter& first,
const Iter& last1) {
2592 return insertDispatch(p, first, last1,
2593 typename IsIntegralType<Iter>::Result(),
2594 "Array_<T>::insert(T* p, Iter first, Iter last1)");
2619 T* growWithGap(T* gapPos,
size_type gapSz,
const char* methodName) {
2624 "Given insertion point is not valid for this array.");
2627 setAllocated(calcNewCapacityForGrowthBy(gapSz, methodName));
2634 T* newGap = newData + nBefore;
2635 T* newGapEnd = newGap + gapSz;
2638 copyConstructThenDestructSource(newData, newGap,
data());
2640 copyConstructThenDestructSource(newGapEnd, newData+
size(), gapPos);
2643 freeN(
data()); setData(newData);
2648 void growAtEnd(
size_type n,
const char* methodName) {
2651 setAllocated(calcNewCapacityForGrowthBy(n, methodName));
2654 copyConstructThenDestructSource(newData, newData+
size(),
data());
2656 freeN(
data()); setData(newData);
2669 "Can't grow this Array by %llu element(s) because it would"
2670 " then exceed the max_size of %llu set by its index type %s.",
2671 (
unsigned long long)n, ullMaxSize(), indexName());
2691 T* insertGapAt(T* p,
size_type n,
const char* methodName) {
2694 "Given insertion point is not valid for this Array.");
2699 "No elements can be inserted into a non-owner array.");
2709 moveElementsUp(p, n);
2711 setAllocated(calcNewCapacityForGrowthBy(n, methodName));
2714 copyConstructThenDestructSource(newdata, newdata+before,
data());
2717 copyConstructThenDestructSource(newdata+before+n,
2718 newdata+before+n+after,
2720 p = newdata + before;
2733 template <
class IntegralType>
void
2734 ctorDispatch(IntegralType n, IntegralType v, TrueType isIntegralType) {
2746 template <
class InputIterator>
void
2747 ctorDispatch(
const InputIterator& first,
const InputIterator& last1,
2748 FalseType isIntegralType)
2749 { ctorIteratorDispatch(first, last1,
2750 typename std::iterator_traits<InputIterator>::iterator_category()); }
2758 template <
class InputIterator>
void
2759 ctorIteratorDispatch(
const InputIterator& first,
const InputIterator& last1,
2760 std::input_iterator_tag)
2762 InputIterator src = first;
2763 while (src != last1) {
2772 "Array_::ctor(InputIterator first, InputIterator last1)",
2773 "There were still source elements available when the array"
2774 " reached its maximum size of %llu as determined by its index"
2775 " type %s.", ullMaxSize(), indexName());
2786 template <
class ForwardIterator>
void
2787 ctorIteratorDispatch(
const ForwardIterator& first,
const ForwardIterator& last1,
2788 std::forward_iterator_tag)
2790 typedef typename std::iterator_traits<ForwardIterator>::difference_type
2795 const difference_type nInput = this->iterDistance(first,last1);
2798 "Array_(ForwardIterator first, ForwardIterator last1)",
2799 "Iterators were out of order.");
2802 "Array_(ForwardIterator first, ForwardIterator last1)",
2803 "Source has %llu elements but this array is limited to %llu"
2804 " elements by its index type %s.",
2805 this->ull(nInput), ullMaxSize(), indexName());
2809 allocateNoConstruct(n);
2810 copyConstruct(
data(),
data()+n, first);
2818 template <
class IntegralType>
2819 T* insertDispatch(T* p, IntegralType n, IntegralType v,
2820 TrueType isIntegralType,
const char*)
2826 template <
class InputIterator>
2827 T* insertDispatch(T* p,
const InputIterator& first,
const InputIterator& last1,
2828 FalseType isIntegralType,
const char* methodName)
2829 {
return insertIteratorDispatch(p, first, last1,
2830 typename std::iterator_traits<InputIterator>::iterator_category(),
2836 template <
class InputIterator>
2837 T* insertIteratorDispatch(T* p, InputIterator first, InputIterator last1,
2838 std::input_iterator_tag,
const char* methodName)
2841 while (first != last1) {
2844 "There were still source elements available when the array"
2845 " reached its maximum size of %llu as determined by its index"
2846 " type %s.", ullMaxSize(), indexName());
2860 template <
class ForwardIterator>
2861 T* insertIteratorDispatch(T* p,
const ForwardIterator& first,
2862 const ForwardIterator& last1,
2863 std::forward_iterator_tag,
2864 const char* methodName)
2866 typedef typename std::iterator_traits<ForwardIterator>::difference_type
2871 const difference_type nInput = this->iterDistance(first,last1);
2873 SimTK_ERRCHK(nInput >= 0, methodName,
"Iterators were out of order.");
2876 "Source has %llu elements which would make this array exceed the %llu"
2877 " elements allowed by its index type %s.",
2878 this->ull(nInput), ullMaxSize(), indexName());
2881 p = insertGapAt(p, n, methodName);
2882 copyConstruct(p, p+n, first);
2892 template <
class IntegralType>
2893 void assignDispatch(IntegralType n, IntegralType v, TrueType isIntegralType,
2894 const char* methodName)
2900 template <
class InputIterator>
2901 void assignDispatch(
const InputIterator& first,
const InputIterator& last1,
2902 FalseType isIntegralType,
const char* methodName)
2903 { assignIteratorDispatch(first, last1,
2904 typename std::iterator_traits<InputIterator>::iterator_category(),
2910 template <
class InputIterator>
2911 void assignIteratorDispatch(
const InputIterator& first,
2912 const InputIterator& last1,
2913 std::input_iterator_tag,
2914 const char* methodName)
2919 "Assignment to a non-owner array can only be done from a source"
2920 " designated with forward iterators or pointers because we"
2921 " must be able to verify that the source and destination sizes"
2925 InputIterator src = first;
2926 while (src != last1) {
2929 "There were still source elements available when the array"
2930 " reached its maximum size of %llu as determined by its index"
2931 " type %s.", ullMaxSize(), indexName());
2942 template <
class ForwardIterator>
2943 void assignIteratorDispatch(
const ForwardIterator& first,
2944 const ForwardIterator& last1,
2945 std::forward_iterator_tag,
2946 const char* methodName)
2948 typedef typename std::iterator_traits<ForwardIterator>::difference_type
2953 const IterDiffType nInput = this->iterDistance(first,last1);
2955 SimTK_ERRCHK(nInput >= 0, methodName,
"Iterators were out of order.");
2958 "Source has %llu elements but this Array is limited to %llu"
2959 " elements by its index type %s.",
2960 this->ull(nInput), ullMaxSize(), indexName());
2968 reallocateIfAdvisable(n);
2969 copyConstruct(
data(),
cdata()+n, first);
2977 "Source has %llu elements which does not match the size %llu"
2978 " of the non-owner array it is being assigned into.",
2979 this->ull(n), ullSize());
2982 ForwardIterator src = first;
2983 while (src != last1)
2999 void reallocateIfAdvisable(
size_type n) {
3001 reallocateNoDestructOrConstruct(n);
3006 { setData(allocN(n)); setAllocated(n); }
3007 void deallocateNoDestruct()
3008 { freeN(
data()); setData(0); setAllocated(0); }
3009 void reallocateNoDestructOrConstruct(
size_type n)
3010 { deallocateNoDestruct(); allocateNoConstruct(n); }
3021 unsigned char* newdata =
new unsigned char[n *
sizeof(T)];
3023 unsigned char* b=newdata;
3024 const unsigned char* e=newdata+(n*
sizeof(T));
3025 while (b != e) *b++ = 0xff;
3027 return reinterpret_cast<T*
>(newdata);
3032 static void freeN(T* p) {
3033 delete[]
reinterpret_cast<char*
>(p);
3037 static void defaultConstruct(T* p) {
new(p) T();}
3039 static void defaultConstruct(T* b,
const T* e)
3040 {
while (b!=e)
new(b++) T(); }
3043 static void fillConstruct(T* b,
const T* e,
const T& v)
3044 {
while(b!=e)
new(b++) T(v); }
3047 static void copyConstruct(T* p,
const T& v) {
new(p) T(v);}
3049 static void copyConstruct(T* b,
const T* e, T* src)
3050 {
while(b!=e)
new(b++) T(*src++); }
3053 template <
class InputIterator>
3054 static void copyConstruct(T* b,
const T* e, InputIterator src)
3055 {
while(b!=e)
new(b++) T(*src++); }
3061 static void copyConstructThenDestructSource(T* b,
const T* e, T* src)
3062 {
while(b!=e) {
new(b++) T(*src); src++->~T();} }
3068 void moveOneElement(T* to, T* from) {
3071 copyConstruct(to, *from);
3078 void moveElementsDown(T* p,
size_type n) {
3080 for (; p !=
end(); ++p)
3081 moveOneElement(p-n,p);
3087 void moveElementsUp(T* p,
size_type n) {
3092 moveOneElement(src+n, src);;
3097 static void destruct(T* p) {p->~T();}
3099 static void destruct(T* b,
const T* e)
3100 {
while(b!=e) b++->~T(); }
3105 bool isGrowthOK(S n)
const
3106 {
return this->isSizeOK(ullCapacity() + this->ull(n)); }
3118 void setData(
const T* p) {this->CBase::setData(p);}
3119 void setSize(
size_type n) {this->CBase::setSize(n);}
3120 void incrSize() {this->CBase::incrSize();}
3121 void decrSize() {this->CBase::decrSize();}
3122 void setAllocated(
size_type n) {this->CBase::setAllocated(n);}
3125 unsigned long long ullSize()
const {
return this->CBase::ullSize();}
3126 unsigned long long ullCapacity()
const {
return this->CBase::ullCapacity();}
3127 unsigned long long ullMaxSize()
const {
return this->CBase::ullMaxSize();}
3130 const char* indexName()
const {
return this->CBase::indexName();}
3142 template <
class T,
class X>
static inline
3148 if (!in.good()) {in.setstate(std::ios::failbit);
return in;}
3158 numRequired = isFixedSize ? out.size() : 0;
3166 std::ws(in);
if (in.fail())
return in;
3168 if (isFixedSize && numRequired != 0)
3169 in.setstate(std::ios_base::failbit);
3177 typename std::iostream::int_type ch;
3178 const typename std::iostream::int_type EOFch =
3179 std::iostream::traits_type::eof();
3182 bool lookForCloser =
true;
3183 char openBracket, closeBracket;
3184 ch = in.peek();
if (in.fail())
return in;
3185 assert(ch != EOFch);
3187 openBracket = (char)ch;
3188 if (openBracket==
'(') {in.get(); closeBracket =
')';}
3189 else if (openBracket==
'[') {in.get(); closeBracket =
']';}
3190 else if (openBracket==
'{') {in.get(); closeBracket =
'}';}
3191 else lookForCloser =
false;
3197 if (in.good()) std::ws(in);
3203 assert(lookForCloser);
3204 in.setstate(std::ios::failbit);
3214 bool commaOK =
true, commaRequired =
false;
3215 bool terminatorSeen =
false;
3232 if (lookForCloser) {
3234 std::ws(in);
if (!in.good())
break;
3235 ch = in.peek(); assert(ch != EOFch);
3236 if (!in.good())
break;
3238 if (c == closeBracket) {
3240 terminatorSeen =
true;
3250 if (isFixedSize && (nextIndex == numRequired))
3254 if (commaOK && nextIndex != 0) {
3256 std::ws(in);
if (!in.good())
break;
3257 ch = in.peek(); assert(ch != EOFch);
3258 if (!in.good())
break;
3262 commaRequired =
true;
3265 { in.setstate(std::ios::failbit);
break; }
3266 else commaOK =
false;
3268 if (!in.good())
break;
3279 in >> out[nextIndex];
if (in.fail())
break;
3282 if (!in.good())
break;
3296 if (lookForCloser && !terminatorSeen)
3297 in.setstate(std::ios::failbit);
3299 if (isFixedSize && nextIndex != numRequired)
3300 in.setstate(std::ios::failbit);
3332 template <
class T,
class X>
inline void
3334 for (X i(0); i < v.size(); ++i) {
3335 if (i != 0) o <<
" ";
3344 template <
class T,
class X>
inline void
3347 for (X i(0); i < v.size(); ++i) {
3348 if (i != 0) o <<
',';
3360 template <
class T,
class X>
inline
3368 for (
const T* p = a.begin()+1; p != a.end(); ++p)
3378 template <
class T,
class X>
inline bool
3384 v.push_back(element);
3392 template <
class T,
class X>
inline bool
3394 for (X i(0); i < v.size(); ++i)
3405 template <
class T,
class X>
inline bool
3407 return !readArrayFromStream(in,v).fail();
3415 template <
class T,
class X>
inline bool
3417 return !fillArrayViewFromStream(in,v).fail();
3449 template <
class T,
class X>
static inline
3451 {
return readArrayFromStreamHelper<T,X>(in,
false , out); }
3479 template <
class T,
class X>
static inline
3481 {
return readArrayFromStreamHelper<T,X>(in,
true , out); }
3487 template <
class T,
class X>
static inline
3489 {
return readArrayFromStreamHelper<T,X>(in,
true , out); }
3503 template <
class T,
class X>
inline
3505 {
return readArrayFromStream<T,X>(in, out); }
3514 template <
class T,
class X>
inline
3516 {
return fillArrayViewFromStream<T,X>(in, out); }
3530 template <
class T1,
class X1,
class T2,
class X2>
inline bool
3533 const ptrdiff_t sz1 = a1.end()-a1.begin();
3534 const ptrdiff_t sz2 = a2.end()-a2.begin();
3535 if (sz1 != sz2)
return false;
3536 const T1* p1 = a1.begin();
3537 const T2* p2 = a2.begin();
3538 while (p1 != a1.end())
3539 if (!(*p1++ == *p2++))
return false;
3544 template <
class T1,
class X1,
class T2,
class X2>
inline bool
3546 {
return !(a1 == a2); }
3552 template <
class T1,
class X1,
class T2,
class X2>
inline bool
3554 const T1* p1 = a1.
begin();
3555 const T2* p2 = a2.begin();
3556 while (p1 != a1.end() && p2 != a2.end()) {
3558 return *p1 < *p2; // otherwise p1 > p2
3563 return p1 == a1.end() && p2 != a2.end();
3567 template <
class T1,
class X1,
class T2,
class X2>
inline bool
3569 {
return !(a1 < a2); }
3573 template <
class T1,
class X1,
class T2,
class X2>
inline bool
3578 template <
class T1,
class X1,
class T2,
class X2>
inline bool
3580 {
return !(a1 > a2); }
3585 template <
class T1,
class X1,
class T2,
class A2>
inline bool
3592 template <
class T1,
class A1,
class T2,
class X2>
inline bool
3594 {
return a2 == v1; }
3598 template <
class T1,
class X1,
class T2,
class A2>
inline bool
3600 {
return !(a1 == v2); }
3603 template <
class T1,
class A1,
class T2,
class X2>
inline bool
3605 {
return !(a2 == v1); }
3612 template <
class T1,
class X1,
class T2,
class A2>
inline bool
3613 operator<(const ArrayViewConst_<T1,X1>& a1,
const std::vector<T2,A2>& v2)
3614 {
return a1 < ArrayViewConst_<T2,size_t>(v2); }
3621 template <
class T1,
class A1,
class T2,
class X2>
inline bool
3627 template <
class T1,
class X1,
class T2,
class A2>
inline bool
3629 {
return !(a1 < v2); }
3632 template <
class T1,
class A1,
class T2,
class X2>
inline bool
3634 {
return !(v1 < a2); }
3639 template <
class T1,
class X1,
class T2,
class A2>
inline bool
3645 template <
class T1,
class A1,
class T2,
class X2>
inline bool
3651 template <
class T1,
class X1,
class T2,
class A2>
inline bool
3652 operator<=(const ArrayViewConst_<T1,X1>& a1,
const std::vector<T2,A2>& v2)
3653 {
return !(a1 > v2); }
3656 template <
class T1,
class A1,
class T2,
class X2>
inline bool
3658 {
return !(v1 > a2); }
3668 template <
class T,
class X>
inline void
3675 #endif // SimTK_SimTKCOMMON_ARRAY_H_
void writeUnformatted(std::ostream &o, const Array_< T, X > &v)
Specialize writeUnformatted() for Array_<E,X> to delegate to element type E, with spaces separating t...
Definition: Array.h:3333
T & back()
Return a writable reference to the last element in this array, which must not be empty.
Definition: Array.h:1114
This Array_ helper class is the base class for Array_, extending ArrayViewConst_ to add the ability t...
Definition: Array.h:49
bool readUnformatted(std::istream &in, ArrayView_< T, X > &v)
Specialization of readUnformatted() for fixed-length ArrayView_<T,X>; reads whitespace-separated toke...
Definition: Array.h:3393
Array_ & operator=(const Array_< T2, X2 > &src)
This is assignment from a source array whose element type T2 and/or index type X2 are different from ...
Definition: Array.h:1897
const T & back() const
Return a const reference to the last element in this array, which must not be empty.
Definition: Array.h:2267
ArrayView_ & operator=(const ArrayViewConst_< T2, X2 > &src)
Assignment from any other array object is allowed as long as the number of elements matches and the t...
Definition: Array.h:918
const T * data() const
The const version of the data() method is identical to cdata().
Definition: Array.h:2199
ArrayIndexTraits< X >::difference_type difference_type
A signed integral type that can represent the difference between any two legitimate index values for ...
Definition: Array.h:346
const T * end() const
The const version of end() is the same as cend().
Definition: Array.h:635
const T & front() const
Return a const reference to the first element in this array, which must not be empty.
Definition: Array.h:1093
bool empty() const
Definition: Array.h:1240
const T * cend() const
Return a const pointer to what would be the element just after the last one in the array; this may be...
Definition: Array.h:1180
#define SimTK_ERRCHK2_ALWAYS(cond, whereChecked, fmt, a1, a2)
Definition: ExceptionMacros.h:289
void fill(const T &fillValue)
Assign all current elements of the array to the same fillValue.
Definition: Array.h:1800
bool operator>(const ArrayViewConst_< T1, X1 > &a1, const std::vector< T2, A2 > &v2)
The greater than operator is implemented by using less than with the arguments reversed, meaning the elements must have working comparison operators of the form T2==T1 and T2<T1.
Definition: Array.h:3640
T value_type
The type of object stored in this container.
Definition: Array.h:323
#define SimTK_SIZECHECK(sz, maxsz, where)
Definition: ExceptionMacros.h:146
ArrayView_()
Default constructor allocates no heap space and is very fast.
Definition: Array.h:862
bool operator==(const ArrayViewConst_< T1, X1 > &a1, const std::vector< T2, A2 > &v2)
An Array_<T1> and an std::vector<T2> are equal if and only if they are the same size() and each eleme...
Definition: Array.h:3586
const T & at(index_type i) const
Same as operator[] but always range-checked, even in a Release build.
Definition: Array.h:1073
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: Array.h:846
~ArrayViewConst_()
The destructor just disconnects the array view handle from its data; see disconnect() for more inform...
Definition: Array.h:486
const T * cdata() const
Return a const pointer to the first element of the array, or possibly (but not necessarily) null (0) ...
Definition: Array.h:2196
Array_(const Array_< T2, X2 > &src)
Construct this Array_<T,X> as a copy of another Array_<T2,X2> where T2!=T or X2!=X.
Definition: Array.h:1621
const T & front() const
Return a const reference to the first element in this array, which must not be empty.
Definition: Array.h:2253
T * iterator
A writable iterator for this container (same as pointer here).
Definition: Array.h:335
const_reverse_iterator crend() const
Return the past-the-end reverse iterator that tests equal to a reverse iterator that has been increme...
Definition: Array.h:643
const_reverse_iterator rbegin() const
The const version of rbegin() is the same as crbegin().
Definition: Array.h:2174
const T & at(index_type i) const
Same as operator[] but always range-checked, even in a Release build.
Definition: Array.h:547
#define SimTK_ERRCHK1_ALWAYS(cond, whereChecked, fmt, a1)
Definition: ExceptionMacros.h:285
void assign(size_type n, const T &fillValue)
Set this array to be n copies of the supplied fillValue.
Definition: Array.h:1766
size_type allocated() const
Return the amount of heap space owned by this array; this is the same as capacity() for owner arrays ...
Definition: Array.h:2124
std::reverse_iterator< iterator > reverse_iterator
Definition: Array.h:1506
T & reference
Definition: Array.h:1502
void assign(const T2 *first, const T2 *last1)
Assign to this array to to make it a copy of the elements in range [first,last1) given by ordinary po...
Definition: Array.h:1825
size_type size() const
Return the current number of elements stored in this array.
Definition: Array.h:501
bool readUnformatted(std::istream &in, Array_< T, X > &v)
Specialization of readUnformatted() for variable-length Array_<T,X>; continues reading whitespace-sep...
Definition: Array.h:3379
ArrayViewConst_(const std::vector< T, A > &src)
Construct a ArrayViewConst_<T> by referencing (sharing) the data in a const std::vector<T>, without copying the data; this is also an implicit conversion.
Definition: Array.h:449
const T & operator[](index_type i) const
Select an element by its index, returning a const reference.
Definition: Array.h:539
void resize(size_type n)
Change the size of this Array, preserving all the elements that will still fit, and default construct...
Definition: Array.h:2030
T * iterator
Definition: Array.h:843
Array_(size_type n)
Construct an array containing n default-constructed elements.
Definition: Array.h:1530
const T * begin() const
The const version of begin() is the same as cbegin().
Definition: Array.h:633
bool isOwner() const
Does this array own the data to which it refers? If not, it can't be resized, and the destructor will...
Definition: Array.h:521
const T & const_reference
A const value_type reference.
Definition: Array.h:333
T * pointer
A writable pointer to a value_type.
Definition: Array.h:327
const_reverse_iterator rend() const
The const version of rend() is the same as crend().
Definition: Array.h:647
const T * end() const
The const version of end() is the same as cend().
Definition: Array.h:1182
Array_(const InputIter &first, const InputIter &last1)
Construct an Array_<T> from a range [first,last1) of values identified by a pair of iterators...
Definition: Array.h:1562
size_type size() const
Definition: Array.h:1238
bool operator!=(const std::vector< T1, A1 > &v1, const ArrayViewConst_< T2, X2 > &a2)
The not equal operator is implemented using the equal operator.
Definition: Array.h:3604
T * pointer
Definition: Array.h:1500
reverse_iterator rbegin()
Return a writable reverse iterator pointing to the last element in the array or rend() if the array i...
Definition: Array.h:2177
T value_type
Definition: Array.h:837
ELEM min(const VectorBase< ELEM > &v)
Definition: VectorMath.h:178
T & at(index_type i)
Same as operator[] but always range-checked, even in a Release build.
Definition: Array.h:2239
const_reverse_iterator rend() const
The const version of rend() is the same as crend().
Definition: Array.h:1203
bool operator>(const ArrayViewConst_< T1, X1 > &a1, const ArrayViewConst_< T2, X2 > &a2)
The greater than operator is implemented by using less than with the arguments reversed, meaning the elements must have working comparison operators of the form T2==T1 and T2<T1.
Definition: Array.h:3574
T * insert(T *p, const T &value)
Insert a new element at a given location within this array, initializing it to a copy of a given valu...
Definition: Array.h:2538
ArrayIndexPackType< size_type >::packed_size_type packed_size_type
Definition: Array.h:850
#define SimTK_ASSERT(cond, msg)
Definition: ExceptionMacros.h:374
void pop_back()
Remove the last element from this array, which must not be empty.
Definition: Array.h:2381
ArrayIndexTraits< X >::size_type size_type
Definition: Array.h:1508
T * raw_push_back()
This dangerous method increases the Array's size by one element at the end but doesn't perform any co...
Definition: Array.h:2371
ArrayView_(std::vector< T, A > &v)
Construct to reference memory owned by a writable std::vector.
Definition: Array.h:872
#define SimTK_ERRCHK3(cond, whereChecked, fmt, a1, a2, a3)
Definition: ExceptionMacros.h:330
size_type size() const
Return the current number of elements stored in this array.
Definition: Array.h:2014
Array_(const std::vector< T2 > &v)
Construct an Array_<T> by copying from an std::vector<T2>, where T2 may be the same type as T but doesn't...
Definition: Array.h:1590
ArrayIndexTraits< X >::size_type size_type
Definition: Array.h:847
ArrayViewConst_(const ArrayViewConst_ &src)
Copy constructor is shallow; the constructed const array object will be referencing the original sour...
Definition: Array.h:372
Array_ & adoptData(T *newData, size_type dataSize, size_type dataCapacity)
This dangerous extension allows you to supply your own already-allocated heap space for use by this a...
Definition: Array.h:1934
~Array_()
The destructor performs a deallocate() operation which may result in element destruction and freeing ...
Definition: Array.h:1689
T * begin()
Return a writable pointer to the first element of this array if any, otherwise end().
Definition: Array.h:1174
ArrayView_ updSubArray(index_type index, size_type length)
Same as non-const operator()(index,length); exists to provide non-operator access to that functionali...
Definition: Array.h:1148
ArrayViewConst_()
Default constructor allocates no heap space and is very fast.
Definition: Array.h:361
const T * end() const
The const version of end() is the same as cend().
Definition: Array.h:2163
T & reference
A writable value_type reference.
Definition: Array.h:331
const T * cbegin() const
Return a const pointer to the first element of this array if any, otherwise cend(), which may be null (0) in that case but does not have to be.
Definition: Array.h:2148
const T * cdata() const
Return a const pointer to the first element of the array, or possibly (but not necessarily) null (0) ...
Definition: Array.h:1215
T * erase(T *first, const T *last1)
Erase elements in range [first,last1), packing in any later elements into the newly-available space a...
Definition: Array.h:2404
static std::istream & readArrayFromStream(std::istream &in, Array_< T, X > &out)
Read in an Array_<T> from a stream, as a sequence of space-separated or comma-separated values option...
Definition: Array.h:3450
T & operator[](index_type i)
Select an element by its index, returning a writable (lvalue) reference.
Definition: Array.h:1067
X index_type
Definition: Array.h:838
Array_ & operator=(const std::vector< T2, A > &src)
This is assignment from a source std::vector<T2>.
Definition: Array.h:1910
This file contains macros which are convenient to use for sprinkling error checking around liberally ...
ArrayView_ & operator=(const ArrayView_ &src)
Copy assignment; source must be the same size as this array.
Definition: Array.h:906
Array_(size_type n, const T &initVal)
Construct an array containing n elements each set to a copy of the given initial value.
Definition: Array.h:1540
bool isOwner() const
Does this array own the data to which it refers? If not, it can't be resized, and the destructor will...
Definition: Array.h:2130
Array_ & adoptData(T *newData, size_type dataSize)
A variant of adoptData() that assumes the capacity is the same as the current size.
Definition: Array.h:1956
T * erase(T *p)
Erase just one element, moving all subsequent elements down one slot and reducing the array's size by...
Definition: Array.h:2439
bool readUnformatted(std::istream &in, VectorView_< E > &v)
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3325
ArrayViewConst_< T, X > getSubArray(index_type index, size_type length) const
Same as const form of operator()(index,length); exists to provide non-operator access to that functio...
Definition: Array.h:2282
const T * const_iterator
Definition: Array.h:844
size_type allocated() const
Return the amount of heap space owned by this array; this is the same as capacity() for owner arrays ...
Definition: Array.h:515
T & at(index_type i)
Same as operator[] but always range-checked, even in a Release build.
Definition: Array.h:1079
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: Array.h:1507
const T * cbegin() const
Return a const pointer to the first element of this array if any, otherwise end(), which may be null (0) in that case but does not have to be.
Definition: Array.h:1167
ArrayView_ & operator=(const T &fillValue)
Fill assignment – all elements are set to fillValue.
Definition: Array.h:951
const T * begin() const
The const version of begin() is the same as cbegin().
Definition: Array.h:2150
void push_back()
This is a non-standard version of push_back() that increases the size of the array by one default-con...
Definition: Array.h:2349
static std::istream & fillArrayFromStream(std::istream &in, Array_< T, X > &out)
Read in a fixed number of elements from a stream into an Array.
Definition: Array.h:3480
reverse_iterator rbegin()
Return a writable reverse iterator pointing to the last element in the array or rend() if the array i...
Definition: Array.h:1196
const_reverse_iterator rend() const
The const version of rend() is the same as crend().
Definition: Array.h:2184
const_reverse_iterator rbegin() const
The const version of rbegin() is the same as crbegin().
Definition: Array.h:1193
T * insert(T *p, size_type n, const T &value)
Insert n copies of a given value at a particular location within this array, moving all following ele...
Definition: Array.h:2526
ArrayIndexTraits< X >::difference_type difference_type
Definition: Array.h:1509
void assign(const T2 *first, const T2 *last1)
Assign to this array to make it a copy of the elements in range [first,last1) given by ordinary point...
Definition: Array.h:996
The SimTK::Array_<T> container class is a plug-compatible replacement for the C++ standard template l...
Definition: Array.h:50
T * end()
Return a writable pointer to what would be the element just after the last one in this array...
Definition: Array.h:1187
#define SimTK_ERRCHK_ALWAYS(cond, whereChecked, msg)
Definition: ExceptionMacros.h:281
ArrayIndexPackType< size_type >::packed_size_type packed_size_type
Definition: Array.h:1511
const T * cend() const
Return a const pointer to what would be the element just after the last one in the array; this may be...
Definition: Array.h:2161
ArrayIndexPackType< size_type >::packed_size_type packed_size_type
The integral type we actually use internally to store size_type values.
Definition: Array.h:349
const T * data() const
The const version of the data() method is identical to cdata().
Definition: Array.h:1218
const T & getElt(index_type i) const
Same as the const form of operator[]; exists to provide a non-operator method for element access in c...
Definition: Array.h:1083
void disconnect()
Forward to base class disconnect() method – clears the handle without doing anything to the data...
Definition: Array.h:884
bool empty() const
Return true if there are no elements currently stored in this array.
Definition: Array.h:506
const T & operator[](index_type i) const
Select an element by its index, returning a const reference.
Definition: Array.h:2218
T & front()
Return a writable reference to the first element in this array, which must not be empty...
Definition: Array.h:1100
#define SimTK_ERRCHK3_ALWAYS(cond, whereChecked, fmt, a1, a2, a3)
Definition: ExceptionMacros.h:293
const T & const_reference
Definition: Array.h:842
ELEM max(const VectorBase< ELEM > &v)
Definition: VectorMath.h:251
const_reverse_iterator rbegin() const
The const version of rbegin() is the same as crbegin().
Definition: Array.h:645
std::reverse_iterator< iterator > reverse_iterator
Definition: Array.h:845
ArrayView_ & operator=(const Array_< T2, X2 > &src)
Assignment from any other array object is allowed as long as the number of elements matches and the t...
Definition: Array.h:937
T * data()
Return a writable pointer to the first allocated element of the array, or a null pointer if no space ...
Definition: Array.h:1222
const T * const_pointer
Definition: Array.h:1501
void assign(size_type n, const T &fillValue)
This is the same as fill() but has the usual std::vector signature for compatibility; it will only wo...
Definition: Array.h:968
const T * const_pointer
A const pointer to a value_type.
Definition: Array.h:329
bool operator>=(const ArrayViewConst_< T1, X1 > &a1, const ArrayViewConst_< T2, X2 > &a2)
The greater than or equal operator is implemented using the less than operator.
Definition: Array.h:3568
void reserve(size_type n)
Ensure that this array has enough allocated capacity to hold the indicated number of elements...
Definition: Array.h:2074
T value_type
Definition: Array.h:1498
ArrayViewConst_ getSubArray(index_type index, size_type length) const
Same as const form of operator()(index,length); exists to provide non-operator access to that functio...
Definition: Array.h:606
bool operator!=(const ArrayViewConst_< T1, X1 > &a1, const std::vector< T2, A2 > &v2)
The not equal operator is implemented using the equal operator.
Definition: Array.h:3599
const T & const_reference
Definition: Array.h:1503
ArrayIndexTraits< X >::difference_type difference_type
Definition: Array.h:848
bool isOwner() const
Definition: Array.h:1243
size_type max_size() const
Definition: Array.h:1239
const T & getElt(index_type i) const
Same as the const form of operator[]; exists to provide a non-operator method for element access in c...
Definition: Array.h:2243
std::ostream & operator<<(std::ostream &o, const ContactForce &f)
Definition: CompliantContactSubsystem.h:387
bool operator>(const std::vector< T1, A1 > &v1, const ArrayViewConst_< T2, X2 > &a2)
The greater than operator is implemented by using less than with the arguments reversed, meaning the elements must have working comparison operators of the form T2==T1 and T2<T1.
Definition: Array.h:3646
std::reverse_iterator< iterator > reverse_iterator
A writable reverse iterator for this container.
Definition: Array.h:339
void swap(Array_ &other)
This is a specialized algorithm providing constant time exchange of data with another array that has ...
Definition: Array.h:1923
#define SimTK_INDEXCHECK_ALWAYS(ix, ub, where)
Definition: ExceptionMacros.h:106
void writeFormatted(std::ostream &o, const Array_< T, X > &v)
Specialize writeFormatted() for Array_<E,X> to delegate to element type E, with surrounding parenthes...
Definition: Array.h:3345
#define SimTK_ERRCHK(cond, whereChecked, msg)
Definition: ExceptionMacros.h:324
T * end()
Return a writable pointer to what would be the element just after the last one in this array...
Definition: Array.h:2168
#define SimTK_ERRCHK2(cond, whereChecked, fmt, a1, a2)
Definition: ExceptionMacros.h:328
This Array_ helper class is the base class for ArrayView_ which is the base class for Array_; here we...
Definition: Array.h:48
const_reverse_iterator crend() const
Return the past-the-end reverse iterator that tests equal to a reverse iterator that has been increme...
Definition: Array.h:1201
T & back()
Return a writable reference to the last element in this array, which must not be empty.
Definition: Array.h:2274
ArrayViewConst_ operator()(index_type index, size_type length) const
Select a contiguous subarray of the elements of this array and create another ArrayViewConst_ that re...
Definition: Array.h:592
Array_()
Default constructor allocates no heap space and is very fast.
Definition: Array.h:1524
void swap(SimTK::Array_< T, X > &a1, SimTK::Array_< T, X > &a2)
This is a specialization of the STL std::swap() algorithm which uses the constant time built-in swap(...
Definition: Array.h:3669
const T & operator[](index_type i) const
Select an element by its index, returning a const reference.
Definition: Array.h:1058
Array_(T *first, const T *last1, const DontCopy &)
Construct an Array_<T> by referencing (sharing) a given range of data [first,last1), without copying that data; better to use the corresponding ArrayView_<T> constructor if you can.
Definition: Array.h:1654
const_reverse_iterator crbegin() const
Return a const reverse iterator pointing to the last element in the array or crend() if the array is ...
Definition: Array.h:2172
bool operator>=(const ArrayViewConst_< T1, X1 > &a1, const std::vector< T2, A2 > &v2)
The greater than or equal operator is implemented using the less than operator.
Definition: Array.h:3628
Mandatory first inclusion for any Simbody source or header file.
reverse_iterator rend()
Return a writable past-the-end reverse iterator that tests equal to a reverse iterator that has been ...
Definition: Array.h:2188
X index_type
Definition: Array.h:1499
const_reverse_iterator crbegin() const
Return a const reverse iterator pointing to the last element in the array or crend() if the array is ...
Definition: Array.h:639
Array_ & deallocate()
Empty this array of its contents, returning the array to its default-constructed, all-zero state...
Definition: Array.h:1701
This file contains definitions of templatized serialize-to-stream methods specialized for the built-i...
void clear()
Erase all the elements currently in this array without changing the capacity; equivalent to erase(beg...
Definition: Array.h:2492
const T * begin() const
The const version of begin() is the same as cbegin().
Definition: Array.h:1169
const T * cend() const
Return a const pointer to what would be the element just after the last one in the array; this may be...
Definition: Array.h:631
#define SimTK_INDEXCHECK(ix, ub, where)
Definition: ExceptionMacros.h:145
const_reverse_iterator crend() const
Return the past-the-end reverse iterator that tests equal to a reverse iterator that has been increme...
Definition: Array.h:2182
ArrayViewConst_(const T *first, const T *last1)
Construct an ArrayViewConst_<T> by referencing (sharing) a given range of const data [first...
Definition: Array.h:401
X index_type
The index type (an extension).
Definition: Array.h:325
size_type allocated() const
Definition: Array.h:1242
T * iterator
Definition: Array.h:1504
bool operator==(const ArrayViewConst_< T1, X1 > &a1, const ArrayViewConst_< T2, X2 > &a2)
Two Array_ objects are equal if and only if they are the same size() and each element compares equal ...
Definition: Array.h:3531
ArrayIndexTraits< X >::size_type size_type
An integral type suitable for all indices and sizes for this array.
Definition: Array.h:343
void assign(const Iter &first, const Iter &last1)
Assign to this array to make it a copy of the elements in range [first,last1) given by non-pointer it...
Definition: Array.h:1038
Array_(const Array_ &src)
Copy constructor allocates exactly as much memory as is in use in the source (not its capacity) and c...
Definition: Array.h:1608
T & updElt(index_type i)
Same as the non-const form of operator[]; exists to provide a non-operator method for element access ...
Definition: Array.h:1086
const T * const_pointer
Definition: Array.h:840
std::istream & operator>>(std::istream &in, ArrayView_< T, X > &out)
Read a (fixed size n) ArrayView_<T> from a stream as a sequence of space- or comma-separated values o...
Definition: Array.h:3515
const T * const_iterator
Definition: Array.h:1505
bool operator!=(const ArrayViewConst_< T1, X1 > &a1, const ArrayViewConst_< T2, X2 > &a2)
The not equal operator is implemented using the equal operator.
Definition: Array.h:3545
T * begin()
Return a writable pointer to the first element of this array if any, otherwise end().
Definition: Array.h:2155
T * insert(T *p, const Iter &first, const Iter &last1)
Insert elements in a range [first,last1) where the range is given by non-pointer iterators.
Definition: Array.h:2591
T & reference
Definition: Array.h:841
const T & front() const
Return a const reference to the first element in this array, which must not be empty (we'll check in ...
Definition: Array.h:562
const T * cdata() const
Return a const pointer to the first element of the array, or possibly (but not necessarily) null (0) ...
Definition: Array.h:655
reverse_iterator rend()
Return a writable past-the-end reverse iterator that tests equal to a reverse iterator that has been ...
Definition: Array.h:1207
bool readFormatted(std::istream &in, ArrayView_< T, X > &v)
Specialization of readFormatted() for fixed-length ArrayView_<T,X>; uses fillArrayViewFromStream() to...
Definition: Array.h:3416
const T & at(index_type i) const
Same as operator[] but always range-checked, even in a Release build.
Definition: Array.h:2233
const T * const_iterator
A const iterator for this container (same as const_pointer here).
Definition: Array.h:337
ArrayView_ & fill(const T &fillValue)
Assign the supplied fill value to each element of this array, using T's copy assignment operator for ...
Definition: Array.h:959
const T & back() const
Return a const reference to the last element in this array, which must not be empty (we'll check in a...
Definition: Array.h:570
T * eraseFast(T *p)
Be careful with this non-standard extension; it erases one element and then moves the last one in its...
Definition: Array.h:2472
void disconnect()
Disconnect this array handle from any data to which it refers, restoring it to the condition it would...
Definition: Array.h:477
ArrayView_< T, X > updSubArray(index_type index, size_type length)
Same as non-const operator()(index,length); exists to provide non-operator access to that functionali...
Definition: Array.h:2291
Array_(std::vector< T, A > &v, const DontCopy &)
Construct an Array_<T> by referencing (sharing) the data in an std::vector<T>, without copying the da...
Definition: Array.h:1684
size_type capacity() const
Definition: Array.h:1241
void resize(size_type n, const T &initVal)
Change the size of this array, preserving all the elements that will still fit, and initializing any ...
Definition: Array.h:2051
bool operator>=(const std::vector< T1, A1 > &v1, const ArrayViewConst_< T2, X2 > &a2)
The greater than or equal operator is implemented using the less than operator.
Definition: Array.h:3633
T & front()
Return a writable reference to the first element in this array, which must not be empty...
Definition: Array.h:2260
ArrayView_ operator()(index_type index, size_type length)
Select a contiguous subarray of the elements of this array and create another ArrayView_ that refers ...
Definition: Array.h:1134
size_type capacity() const
Return the number of elements this array can currently hold without requiring reallocation.
Definition: Array.h:511
ArrayView_ & operator=(const ArrayView_< T2, X2 > &src)
Assignment from any other array object is allowed as long as the number of elements matches and the t...
Definition: Array.h:932
void shrink_to_fit()
Request that the capacity of this array be reduced to the minimum necessary to hold the number of ele...
Definition: Array.h:2109
Array_ & operator=(const Array_ &src)
Copy assignment operator destructs the current contents of this array and then makes it a copy of the...
Definition: Array.h:1883
void push_back(const T &value)
This method increases the size of the Array by one element at the end and initializes that element by...
Definition: Array.h:2329
T & updElt(index_type i)
Same as the non-const form of operator[]; exists to provide a non-operator method for element access ...
Definition: Array.h:2246
size_type max_size() const
Return the maximum allowable size for this array.
Definition: Array.h:503
bool readFormatted(std::istream &in, Array_< T, X > &v)
Specialization of readFormatted() for variable-length Array_<T,X>; uses readArrayFromStream() to cons...
Definition: Array.h:3406
std::reverse_iterator< const_iterator > const_reverse_iterator
A const reverse iterator for this container.
Definition: Array.h:341
ArrayViewConst_< T, X > operator()(index_type index, size_type length) const
Select a subrange of this const array by starting index and length, and return a ArrayViewConst_ refe...
Definition: Array.h:2278
Array_ & shareData(T *newData, size_type dataSize)
This dangerous extension allows you to make this array handle refer to someone else's data without co...
Definition: Array.h:1973
T & operator[](index_type i)
Select an element by its index, returning a writable (lvalue) reference.
Definition: Array.h:2227
bool operator==(const std::vector< T1, A1 > &v1, const ArrayViewConst_< T2, X2 > &a2)
An std::vector<T1> and an Array_<T2> are equal if and only if they are the same size() and each eleme...
Definition: Array.h:3593
~ArrayView_()
The destructor just disconnects the array view handle from its data; see ArrayViewConst_<T,X>::disconnect() for more information.
Definition: Array.h:888
T * pointer
Definition: Array.h:839
ArrayView_(T *first, const T *last1)
Construct from a range of writable memory.
Definition: Array.h:868
void assign(const Iter &first, const Iter &last1)
Assign this array from a range [first,last1) given by non-pointer iterators.
Definition: Array.h:1873
Array_ & shareData(T *first, const T *last1)
Same as shareData(data,size) but uses a pointer range [first,last1) to identify the data to be refere...
Definition: Array.h:1990
T * data()
Return a writable pointer to the first allocated element of the array, or a null pointer if no space ...
Definition: Array.h:2203
const T & getElt(index_type i) const
Same as the const form of operator[]; exists to provide a non-operator method for element access in c...
Definition: Array.h:553
ArrayView_< T, X > operator()(index_type index, size_type length)
Select a subrange of this array by starting index and length, and return an ArrayView_ referencing th...
Definition: Array.h:2287
size_type capacity() const
Return the number of elements this array can currently hold without requiring reallocation.
Definition: Array.h:2024
const_reverse_iterator crbegin() const
Return a const reverse iterator pointing to the last element in the array or crend() if the array is ...
Definition: Array.h:1191
static std::istream & readArrayFromStreamHelper(std::istream &in, bool isFixedSize, Array_< T, X > &out)
Definition: Array.h:3144
size_type max_size() const
Return the maximum allowable size for this array.
Definition: Array.h:2016
bool empty() const
Return true if there are no elements currently stored in this array.
Definition: Array.h:2019
const T & back() const
Return a const reference to the last element in this array, which must not be empty.
Definition: Array.h:1107
static std::istream & fillArrayViewFromStream(std::istream &in, ArrayView_< T, X > &out)
Read in a fixed number of elements from a stream into an ArrayView.
Definition: Array.h:3488
std::istream & operator>>(std::istream &in, Array_< T, X > &out)
Read an Array_<T> from a stream as a sequence of space- or comma-separated values of type T...
Definition: Array.h:3504
ArrayView_(const ArrayView_ &src)
Copy constructor is shallow.
Definition: Array.h:865
const T * data() const
The const version of the data() method is identical to cdata().
Definition: Array.h:657
const T * cbegin() const
Return a const pointer to the first element of this array if any, otherwise cend(), which may be null (0) in that case but does not have to be.
Definition: Array.h:626
Array_(const T2 *first, const T2 *last1)
Construct an Array_<T> from a range [first,last1) of values identified by a pair of ordinary pointers t...
Definition: Array.h:1572
T * insert(T *p, const T2 *first, const T2 *last1)
Insert elements in a range [first,last1) into this array at a given position p, moving all following ...
Definition: Array.h:2576
ArrayView_ & operator=(const std::vector< T2, A2 > &src)
Assignment from any std::vector object is allowed as long as the number of elements matches and the t...
Definition: Array.h:943