1 #ifndef SimTK_SimTKCOMMON_ARRAY_H_ 2 #define SimTK_SimTKCOMMON_ARRAY_H_ 50 template <
class T,
class X=
unsigned>
class Array_;
114 static size_type
max_size() {
return X::max_size();}
122 static size_type
max_size() {
return (
unsigned)INT_MAX;}
142 static size_type
max_size() {
return (
unsigned long)LONG_MAX;}
210 static size_type
max_size() {
return (
char)SCHAR_MAX;}
230 {
return (
unsigned long long)LLONG_MAX;}
251 template <
class Integral,
class is64Bit>
struct ArrayIndexPackTypeHelper
252 {
typedef Integral packed_size_type;};
255 template<>
struct ArrayIndexPackTypeHelper<bool,FalseType>
256 {
typedef unsigned short packed_size_type;};
257 template<>
struct ArrayIndexPackTypeHelper<char,FalseType>
258 {
typedef unsigned short packed_size_type;};
259 template<>
struct ArrayIndexPackTypeHelper<unsigned char,FalseType>
260 {
typedef unsigned short packed_size_type;};
261 template<>
struct ArrayIndexPackTypeHelper<signed char,FalseType>
262 {
typedef short packed_size_type;};
265 template<>
struct ArrayIndexPackTypeHelper<bool,TrueType>
266 {
typedef unsigned int packed_size_type;};
267 template<>
struct ArrayIndexPackTypeHelper<char,TrueType>
268 {
typedef unsigned int packed_size_type;};
269 template<>
struct ArrayIndexPackTypeHelper<unsigned char,TrueType>
270 {
typedef unsigned int packed_size_type;};
271 template<>
struct ArrayIndexPackTypeHelper<signed char,TrueType>
272 {
typedef int packed_size_type;};
273 template<>
struct ArrayIndexPackTypeHelper<unsigned short,TrueType>
274 {
typedef unsigned int packed_size_type;};
275 template<>
struct ArrayIndexPackTypeHelper<short,TrueType>
276 {
typedef int packed_size_type;};
278 template <
class Integral>
struct ArrayIndexPackType
279 {
typedef typename ArrayIndexPackTypeHelper<Integral,Is64BitPlatformType>
280 ::packed_size_type packed_size_type;};
353 typedef typename ArrayIndexPackType<size_type>::packed_size_type
378 : pData(0), nUsed(src.nUsed), nAllocated(0) {
379 if (nUsed) pData =
const_cast<T*
>(src.pData);
407 : pData(0),nUsed(0),nAllocated(0) {
408 if (last1==first)
return;
411 "ArrayViewConst_<T>(first,last1)",
412 "One of the source pointers was null (0); either both must be" 413 " non-null or both must be null.");
416 "ArrayViewConst_<T>(first,last1)",
417 "The source data's size %llu is too big for this array which" 418 " is limited to %llu elements by its index type %s.",
419 this->ull(last1-first), ullMaxSize(), indexName());
421 pData =
const_cast<T*
>(first);
422 nUsed = packed_size_type(last1-first);
455 : pData(0),nUsed(0),nAllocated(0) {
456 if (src.empty())
return;
459 "ArrayViewConst_<T>::ctor(std::vector<T>)",
460 "The source std::vector's size %llu is too big for this array which" 461 " is limited to %llu elements by its index type %s.",
462 this->ull(src.size()), ullMaxSize(), indexName());
464 pData =
const_cast<T*
>(&src.front());
465 nUsed = packed_size_type(src.size());
475 {
return *
reinterpret_cast<const Array_<T,X>*
>(
this); }
484 "ArrayViewConst_::deallocate(): called on an owner Array_");
512 bool empty()
const {
return nUsed==0;}
518 {
return size_type(nAllocated?nAllocated:nUsed); }
528 bool isOwner()
const {
return nAllocated || pData==0;}
555 const T&
at(index_type i)
const {
571 {
SimTK_ERRCHK(!empty(),
"ArrayViewConst_<T>::front()",
"Array was empty.");
579 {
SimTK_ERRCHK(!empty(),
"ArrayViewConst_<T>::back()",
"Array was empty.");
580 return pData[nUsed-1]; }
601 const size_type ix(index);
603 "For this operator, we must have 0 <= index <= size(), but" 604 " index==%llu and size==%llu.", this->ull(ix), ullSize());
606 "ArrayViewConst_<T>(index,length)",
607 "This operator requires 0 <= length <= size()-index, but" 608 " length==%llu and size()-index==%llu.",this->ull(length),this->ull(size()-ix));
615 {
return (*
this)(index,length); }
639 const T*
cend()
const {
return pData + nUsed;}
641 const T*
begin()
const {
return pData;}
643 const T*
end()
const {
return pData + nUsed;}
648 {
return const_reverse_iterator(cend()); }
652 const_reverse_iterator
crend()
const 653 {
return const_reverse_iterator(cbegin()); }
655 const_reverse_iterator
rbegin()
const {
return crbegin();}
657 const_reverse_iterator
rend()
const {
return crend();}
665 const T*
cdata()
const {
return pData;}
667 const T*
data()
const {
return pData;}
680 packed_size_type psize()
const {
return nUsed;}
681 packed_size_type pallocated()
const {
return nAllocated;}
684 void setData(
const T* p) {pData =
const_cast<T*
>(p);}
685 void setSize(size_type
n) {nUsed = packed_size_type(n);}
686 void incrSize() {++nUsed;}
687 void decrSize() {--nUsed;}
688 void setAllocated(size_type n) {nAllocated = packed_size_type(n);}
693 bool isSameSize(S sz)
const 694 {
return ull(sz) == ullSize(); }
699 bool isSizeOK(S srcSz)
const 700 {
return ull(srcSz) <= ullMaxSize(); }
707 template<
class Iter>
static 708 typename std::iterator_traits<Iter>::difference_type
709 iterDistance(
const Iter& first,
const Iter& last1) {
710 return iterDistanceImpl(first,last1,
711 typename std::iterator_traits<Iter>::iterator_category());
717 template<
class Iter>
static 718 typename std::iterator_traits<Iter>::difference_type
719 iterDistanceImpl(
const Iter& first,
const Iter& last1, std::input_iterator_tag) {
720 typename std::iterator_traits<Iter>::difference_type d = 0;
721 for (Iter src=first; src != last1; ++src, ++d)
728 template<
class Iter>
static 729 typename std::iterator_traits<Iter>::difference_type
730 iterDistanceImpl(
const Iter& first,
const Iter& last1,
731 std::random_access_iterator_tag) {
732 return last1 - first;
744 template<
class Iter>
bool 745 overlapsWithData(
const Iter& first,
const Iter& last1) {
746 return overlapsWithDataImpl(first,last1,
747 typename std::iterator_traits<Iter>::iterator_category());
752 template <
class T2>
bool 753 overlapsWithData(
const T2* first,
const T2* last1) {
759 const T* obegin =
std::max(cbegin(), (
const T*)first);
760 const T* oend1 =
std::min(cend(), (
const T*)last1);
762 return obegin < oend1;
767 template<
class Iter>
bool 768 overlapsWithDataImpl(
const Iter&,
const Iter&, std::input_iterator_tag)
773 template<
class Iter>
bool 774 overlapsWithDataImpl(
const Iter& first,
const Iter& last1,
775 std::random_access_iterator_tag) {
786 return overlapsWithData(&*first, &*(last1-1));
793 static unsigned long long ull(S sz)
794 {
return (
unsigned long long)sz; }
797 unsigned long long ullSize()
const {
return ull(size());}
798 unsigned long long ullCapacity()
const {
return ull(capacity());}
799 unsigned long long ullMaxSize()
const {
return ull(
max_size());}
813 packed_size_type nUsed;
814 packed_size_type nAllocated;
859 typedef typename ArrayIndexPackType<size_type>::packed_size_type
886 {
return *
reinterpret_cast<const Array_<T,X>*
>(
this); }
918 avAssignIteratorDispatch(src.
cbegin(), src.
cend(),
919 std::random_access_iterator_tag(),
920 "ArrayView_<T>::operator=(ArrayView_<T>)");
927 template <
class T2,
class X2>
929 if ((
const void*)&src != (
void*)
this)
930 avAssignIteratorDispatch(src.
cbegin(), src.
cend(),
931 std::random_access_iterator_tag(),
932 "ArrayView_<T>::operator=(Array_<T2>)");
941 template <
class T2,
class X2>
946 template <
class T2,
class X2>
952 template <
class T2,
class A2>
954 avAssignIteratorDispatch(src.begin(), src.end(),
955 std::random_access_iterator_tag(),
956 "ArrayView_<T>::operator=(std::vector<T2>)");
962 { fill(fillValue);
return *
this; }
970 for (T* d = begin(); d != end(); ++d)
978 void assign(size_type
n,
const T& fillValue) {
980 "Assignment to an ArrayView is permitted only if the source" 981 " is the same size. Here n==%llu element(s) but the" 982 " ArrayView has a fixed size of %llu.",
983 this->ull(n), this->ull(size()));
1006 void assign(
const T2* first,
const T2* last1) {
1007 const char* methodName =
"ArrayView_<T>::assign(T2* first, T2* last1)";
1008 SimTK_ERRCHK((first&&last1)||(first==last1), methodName,
1009 "One of the source pointers was null (0); either both must be" 1010 " non-null or both must be null.");
1012 avAssignIteratorDispatch(first, last1, std::random_access_iterator_tag(),
1047 template <
class Iter>
1048 void assign(
const Iter& first,
const Iter& last1)
1050 "ArrayView_<T>::assign(Iter first, Iter last1)"); }
1069 {
return this->CBase::operator[](i); }
1079 {
return const_cast<T&
>(this->CBase::operator[](i)); }
1085 const T&
at(index_type i)
const {
return this->CBase::at(i);}
1091 T&
at(index_type i) {
return const_cast<T&
>(this->CBase::at(i));}
1096 {
return this->CBase::getElt(i); }
1100 {
return const_cast<T&
>(this->CBase::getElt(i)); }
1149 const size_type ix(index);
1151 "For this operator, we must have 0 <= index <= size(), but" 1152 " index==%llu and size==%llu.", this->ull(ix), ullSize());
1154 "ArrayView_<T>(index,length)",
1155 "This operator requires 0 <= length <= size()-index, but" 1156 " length==%llu and size()-index==%llu.",this->ull(length),this->ull(size()-ix));
1158 return ArrayView_(data()+ix, data()+ix+length);
1163 {
return (*
this)(index,length); }
1206 {
return this->CBase::crbegin(); }
1209 {
return this->CBase::crbegin(); }
1212 reverse_iterator
rbegin() {
return reverse_iterator(end());}
1218 {
return this->CBase::crend(); }
1220 const_reverse_iterator
rend()
const 1221 {
return this->CBase::crend(); }
1225 reverse_iterator
rend() {
return reverse_iterator(begin());}
1257 size_type
max_size()
const {
return this->CBase::max_size();}
1258 bool empty()
const {
return this->CBase::empty();}
1259 size_type
capacity()
const {
return this->CBase::capacity();}
1260 size_type
allocated()
const {
return this->CBase::allocated();}
1261 bool isOwner()
const {
return this->CBase::isOwner();}
1276 template <
class IntegralType>
1277 void avAssignDispatch(IntegralType
n, IntegralType v, TrueType isIntegralType,
1279 { assign(
size_type(n), value_type(v)); }
1284 template <
class InputIterator>
1285 void avAssignDispatch(
const InputIterator& first,
const InputIterator& last1,
1286 FalseType isIntegralType,
const char* methodName)
1287 { avAssignIteratorDispatch(first, last1,
1288 typename std::iterator_traits<InputIterator>::iterator_category(),
1297 template <
class InputIterator>
1298 void avAssignIteratorDispatch(
const InputIterator& first,
1299 const InputIterator& last1,
1300 std::input_iterator_tag,
1301 const char* methodName)
1304 InputIterator src = first;
1305 while (src != last1 && p != end())
1309 const size_type nCopied =
size_type(p - begin());
1311 "The supplied input_iterator provided only %llu elements but this" 1312 " ArrayView has a fixed size of %llu elements.",
1313 this->ull(nCopied), ullSize());
1322 template <
class ForwardIterator>
1323 void avAssignIteratorDispatch(
const ForwardIterator& first,
1324 const ForwardIterator& last1,
1325 std::forward_iterator_tag,
1326 const char* methodName)
1329 ForwardIterator src = first;
1330 while (src != last1 && p != end())
1334 const size_type nCopied =
size_type(p - begin());
1336 "The supplied forward_ or bidirectional_iterator source range provided" 1337 " only %llu elements but this ArrayView has a fixed size of" 1338 " %llu elements.", this->ull(nCopied), ullSize());
1342 "The supplied forward_ or bidirectional_iterator source range" 1343 " contained too many elements; this ArrayView has a fixed size of" 1344 " %llu elements.", ullSize());
1351 template <
class RandomAccessIterator>
1352 void avAssignIteratorDispatch(
const RandomAccessIterator& first,
1353 const RandomAccessIterator& last1,
1354 std::random_access_iterator_tag,
1355 const char* methodName)
1358 "Assignment to an ArrayView is permitted only if the source" 1359 " is the same size. Here the source had %llu element(s) but the" 1360 " ArrayView has a fixed size of %llu.",
1361 this->ull(last1-first), this->ull(size()));
1364 "Source range can't overlap with the destination data.");
1367 RandomAccessIterator src = first;
1381 {
return this->CBase::psize(); }
1383 {
return this->CBase::pallocated(); }
1387 unsigned long long ullSize()
const 1388 {
return this->CBase::ullSize(); }
1389 unsigned long long ullCapacity()
const 1390 {
return this->CBase::ullCapacity(); }
1391 unsigned long long ullMaxSize()
const 1392 {
return this->CBase::ullMaxSize(); }
1395 const char* indexName()
const {
return this->CBase::indexName();}
1533 typedef typename ArrayIndexPackType<size_type>::packed_size_type
1555 allocateNoConstruct(n);
1556 defaultConstruct(data(), data()+n);
1566 allocateNoConstruct(size());
1567 fillConstruct(begin(), cend(), initVal);
1584 template <
class InputIter>
1585 Array_(
const InputIter& first,
const InputIter& last1) : Base() {
1595 Array_(
const T2* first,
const T2* last1) : Base() {
1596 SimTK_ERRCHK((first&&last1)||(first==last1),
"Array_<T>(first,last1)",
1597 "Pointers must be non-null unless they are both null.");
1598 SimTK_ERRCHK3(this->isSizeOK(last1-first),
"Array_<T>(first,last1)",
1599 "Source has %llu elements but this array is limited to %llu" 1600 " elements by its index type %s.",
1601 this->ull(last1-first), ullMaxSize(), indexName());
1604 allocateNoConstruct(size());
1605 copyConstruct(begin(), cend(), first);
1613 explicit Array_(
const std::vector<T2>& v) : Base() {
1614 if (v.empty())
return;
1616 SimTK_ERRCHK3(this->isSizeOK(v.size()),
"Array_<T>::ctor(std::vector<T2>)",
1617 "The source std::vector's size %llu is too big for this array which" 1618 " is limited to %llu elements by its index type %s.",
1619 this->ull(v.size()), ullMaxSize(), indexName());
1624 new (
this)
Array_(&v.front(), (&v.back())+1);
1632 setSize(src.
size());
1633 allocateNoConstruct(size());
1634 copyConstruct(begin(), cend(), src.
data());
1643 template <
class T2,
class X2>
1727 deallocateNoDestruct();
1729 this->Base::disconnect();
1790 SimTK_ERRCHK3(this->isSizeOK(n),
"Array_<T>::assign(n,value)",
1791 "Requested size %llu is too big for this array which is limited" 1792 " to %llu elements by its index type %s.",
1793 this->ull(n), ullMaxSize(), indexName());
1795 SimTK_ERRCHK2(isOwner() || n==size(),
"Array_<T>::assign(n,value)",
1796 "Requested size %llu is not allowed because this is a non-owner" 1797 " array of fixed size %llu.", this->ull(n), this->ull(size()));
1800 this->Base::fill(fillValue);
1803 reallocateIfAdvisable(n);
1804 fillConstruct(data(), cdata()+n, fillValue);
1823 void fill(
const T& fillValue) {this->Base::fill(fillValue);}
1848 void assign(
const T2* first,
const T2* last1) {
1849 const char* methodName =
"Array_<T>::assign(T2* first, T2* last1)";
1850 SimTK_ERRCHK((first&&last1)||(first==last1), methodName,
1851 "Pointers must be non-null unless they are both null.");
1852 SimTK_ERRCHK(!this->overlapsWithData(first,last1), methodName,
1853 "Source range can't overlap the current array contents.");
1855 assignIteratorDispatch(first,last1,std::random_access_iterator_tag(),
1895 template <
class Iter>
1896 void assign(
const Iter& first,
const Iter& last1) {
1898 "Array_<T>::assign(Iter first, Iter last1)");
1908 assignIteratorDispatch(src.
begin(), src.
end(),
1909 std::random_access_iterator_tag(),
1910 "Array_<T>::operator=(Array_<T>)");
1919 template <
class T2,
class X2>
1921 assignIteratorDispatch(src.
begin(), src.
end(),
1922 std::random_access_iterator_tag(),
1923 "Array_<T>::operator=(Array_<T2,X2>)");
1932 template <
class T2,
class A>
1934 assignIteratorDispatch(src.begin(), src.end(),
1935 std::random_access_iterator_tag(),
1936 "Array_<T>::operator=(std::vector)");
1947 T*
const pTmp=data(); setData(other.
data()); other.setData(pTmp);
1948 size_type nTmp=size(); setSize(other.
size()); other.setSize(nTmp);
1949 nTmp=allocated(); setAllocated(other.
allocated()); other.setAllocated(nTmp);
1958 size_type dataCapacity)
1961 SimTK_ERRCHK2(dataSize <= dataCapacity,
"Array_<T>::adoptData()",
1962 "Specified data size %llu was greater than the specified data" 1963 " capacity of %llu.", this->ull(dataSize), this->ull(dataCapacity));
1964 SimTK_ERRCHK(newData || dataCapacity==0,
"Array_<T>::adoptData()",
1965 "A null data pointer is allowed only if the size and capacity are" 1966 " specified as zero.");
1967 SimTK_ERRCHK(!this->overlapsWithData(newData, newData+dataSize),
1968 "Array_<T>::adoptData()",
1969 "The new data can't overlap with the old data.");
1974 setAllocated(dataCapacity);
1980 {
return adoptData(newData, dataSize, dataSize); }
1998 SimTK_ERRCHK(newData || dataSize==0,
"Array_<T>::shareData()",
1999 "A null data pointer is allowed only if the size is zero.");
2000 SimTK_ERRCHK(!this->overlapsWithData(newData, newData+dataSize),
2001 "Array_<T>::shareData()",
2002 "The new data can't overlap with the old data.");
2014 SimTK_ERRCHK3(this->isSizeOK(last1-first),
"Array_<T>::shareData(first,last1)",
2015 "Requested size %llu is too big for this array which is limited" 2016 " to %llu elements by its index type %s.",
2017 this->ull(last1-first), ullMaxSize(), indexName());
2018 return shareData(first,
size_type(last1-first));
2039 size_type
max_size()
const {
return this->CBase::max_size();}
2042 bool empty()
const {
return this->CBase::empty();}
2047 size_type
capacity()
const {
return this->CBase::capacity();}
2054 if (n == size())
return;
2057 "Requested size change to %llu is not allowed because this is a" 2058 " non-owner array of fixed size %llu.", this->ull(n), this->ull(size()));
2061 erase(data()+n, cend());
2066 defaultConstruct(data()+size(), cdata()+n);
2075 if (n == size())
return;
2078 "Requested size change to %llu is not allowed because this is a" 2079 " non-owner array of fixed size %llu.", this->ull(n), this->ull(size()));
2082 erase(data()+n, cend());
2087 fillConstruct(data()+size(), cdata()+n, initVal);
2098 if (capacity() >= n)
2102 "Requested capacity change to %llu is not allowed because this is a" 2103 " non-owner array of fixed size %llu.", this->ull(n), this->ull(size()));
2105 T* newData = allocN(n);
2106 copyConstructThenDestructSource(newData, newData+size(), data());
2135 if (capacity() - size()/4 <= size())
2137 T* newData = allocN(size());
2138 copyConstructThenDestructSource(newData, newData+size(), data());
2139 deallocateNoDestruct();
2141 setAllocated(size());
2148 {
return this->CBase::allocated(); }
2154 bool isOwner()
const {
return this->CBase::isOwner();}
2197 {
return this->CBase::crbegin(); }
2200 {
return this->CBase::crbegin(); }
2203 reverse_iterator
rbegin() {
return this->Base::rbegin();}
2209 {
return this->CBase::crend(); }
2211 const_reverse_iterator
rend()
const 2212 {
return this->CBase::crend(); }
2216 reverse_iterator
rend() {
return this->Base::rend();}
2247 {
return this->CBase::operator[](i); }
2262 const T&
at(index_type i)
const {
return this->CBase::at(i);}
2268 T&
at(index_type i) {
return const_cast<T&
>(this->Base::at(i));}
2273 {
return this->CBase::getElt(i); }
2309 {
return CBase::operator()(index,length); }
2313 {
return CBase::getSubArray(index,length); }
2318 {
return Base::operator()(index,length); }
2322 {
return Base::updSubArray(index,length); }
2360 if (pallocated() == psize())
2361 growAtEnd(1,
"Array_<T>::push_back(value)");
2362 copyConstruct(end(), value);
2380 if (pallocated() == psize())
2381 growAtEnd(1,
"Array_<T>::push_back()");
2382 defaultConstruct(end());
2402 if (pallocated() == psize())
2403 growAtEnd(1,
"Array_<T>::raw_push_back()");
2412 SimTK_ERRCHK(!empty(),
"Array_<T>::pop_back()",
"Array was empty.");
2435 SimTK_ERRCHK(begin() <= first && first <= last1 && last1 <= end(),
2436 "Array<T>::erase(first,last1)",
"Pointers out of range or out of order.");
2438 const size_type nErased =
size_type(last1-first);
2439 SimTK_ERRCHK(isOwner() || nErased==0,
"Array<T>::erase(first,last1)",
2440 "No elements can be erased from a non-owner array.");
2443 destruct(first, last1);
2444 moveElementsDown(first+nErased, nErased);
2445 setSize(size()-nErased);
2471 "Array<T>::erase(p)",
"Pointer must point to a valid element.");
2473 "No elements can be erased from a non-owner array.");
2476 moveElementsDown(p+1, 1);
2504 "Array<T>::eraseFast(p)",
"Pointer must point to a valid element.");
2506 "No elements can be erased from a non-owner array.");
2510 moveOneElement(p, &back());
2523 SimTK_ERRCHK(isOwner() || empty(),
"Array_<T>::clear()",
2524 "clear() is not allowed for a non-owner array.");
2525 destruct(begin(), end());
2557 T*
const gap = insertGapAt(p, n,
"Array<T>::insert(p,n,value)");
2559 fillConstruct(gap, gap+n, value);
2569 T*
const gap = insertGapAt(p, 1,
"Array<T>::insert(p,value)");
2571 copyConstruct(gap, value);
2606 T*
insert(T* p,
const T2* first,
const T2* last1) {
2607 const char* methodName =
"Array_<T>::insert(T* p, T2* first, T2* last1)";
2608 SimTK_ERRCHK((first&&last1) || (first==last1), methodName,
2609 "One of first or last1 was null; either both or neither must be null.");
2610 SimTK_ERRCHK(!this->overlapsWithData(first,last1), methodName,
2611 "Source range can't overlap with the current array contents.");
2613 return insertIteratorDispatch(p, first, last1,
2614 std::random_access_iterator_tag(),
2620 template <
class Iter>
2621 T*
insert(T* p,
const Iter& first,
const Iter& last1) {
2622 return insertDispatch(p, first, last1,
2624 "Array_<T>::insert(T* p, Iter first, Iter last1)");
2649 T* growWithGap(T* gapPos, size_type gapSz,
const char* methodName) {
2653 SimTK_ERRCHK(begin() <= gapPos && gapPos <= end(), methodName,
2654 "Given insertion point is not valid for this array.");
2657 setAllocated(calcNewCapacityForGrowthBy(gapSz, methodName));
2658 T* newData = allocN(allocated());
2661 const size_type nBefore = (
size_type)(gapPos-begin());
2664 T* newGap = newData + nBefore;
2665 T* newGapEnd = newGap + gapSz;
2668 copyConstructThenDestructSource(newData, newGap, data());
2670 copyConstructThenDestructSource(newGapEnd, newData+size(), gapPos);
2673 freeN(data()); setData(newData);
2678 void growAtEnd(size_type
n,
const char* methodName) {
2681 setAllocated(calcNewCapacityForGrowthBy(n, methodName));
2682 T* newData = allocN(allocated());
2684 copyConstructThenDestructSource(newData, newData+size(), data());
2686 freeN(data()); setData(newData);
2697 size_type calcNewCapacityForGrowthBy(size_type n,
const char* methodName)
const {
2699 "Can't grow this Array by %llu element(s) because it would" 2700 " then exceed the max_size of %llu set by its index type %s.",
2701 (
unsigned long long)n, ullMaxSize(), indexName());
2704 const size_type mustHave = capacity() +
n;
2708 const size_type wantToHave = capacity() <=
max_size()/2
2721 T* insertGapAt(T* p, size_type n,
const char* methodName) {
2724 "Given insertion point is not valid for this Array.");
2729 "No elements can be inserted into a non-owner array.");
2733 const size_type before = (
size_type)(p-begin()), after = (size_type)(end()-p);
2738 if (capacity() >= size()+
n) {
2739 moveElementsUp(p, n);
2741 setAllocated(calcNewCapacityForGrowthBy(n, methodName));
2742 T* newdata = allocN(allocated());
2744 copyConstructThenDestructSource(newdata, newdata+before, data());
2747 copyConstructThenDestructSource(newdata+before+n,
2748 newdata+before+n+after,
2750 p = newdata + before;
2763 template <
class IntegralType>
void 2764 ctorDispatch(IntegralType n, IntegralType v, TrueType isIntegralType) {
2776 template <
class InputIterator>
void 2777 ctorDispatch(
const InputIterator& first,
const InputIterator& last1,
2778 FalseType isIntegralType)
2779 { ctorIteratorDispatch(first, last1,
2780 typename std::iterator_traits<InputIterator>::iterator_category()); }
2788 template <
class InputIterator>
void 2789 ctorIteratorDispatch(
const InputIterator& first,
const InputIterator& last1,
2790 std::input_iterator_tag)
2792 InputIterator src = first;
2793 while (src != last1) {
2802 "Array_::ctor(InputIterator first, InputIterator last1)",
2803 "There were still source elements available when the array" 2804 " reached its maximum size of %llu as determined by its index" 2805 " type %s.", ullMaxSize(), indexName());
2816 template <
class ForwardIterator>
void 2817 ctorIteratorDispatch(
const ForwardIterator& first,
const ForwardIterator& last1,
2818 std::forward_iterator_tag)
2820 typedef typename std::iterator_traits<ForwardIterator>::difference_type
2825 const difference_type nInput = this->iterDistance(first,last1);
2828 "Array_(ForwardIterator first, ForwardIterator last1)",
2829 "Iterators were out of order.");
2832 "Array_(ForwardIterator first, ForwardIterator last1)",
2833 "Source has %llu elements but this array is limited to %llu" 2834 " elements by its index type %s.",
2835 this->ull(nInput), ullMaxSize(), indexName());
2839 allocateNoConstruct(n);
2840 copyConstruct(data(), data()+n, first);
2848 template <
class IntegralType>
2849 T* insertDispatch(T* p, IntegralType n, IntegralType v,
2850 TrueType isIntegralType,
const char*)
2851 {
return insert(p,
size_type(n), value_type(v)); }
2856 template <
class InputIterator>
2857 T* insertDispatch(T* p,
const InputIterator& first,
const InputIterator& last1,
2858 FalseType isIntegralType,
const char* methodName)
2859 {
return insertIteratorDispatch(p, first, last1,
2860 typename std::iterator_traits<InputIterator>::iterator_category(),
2866 template <
class InputIterator>
2867 T* insertIteratorDispatch(T* p, InputIterator first, InputIterator last1,
2868 std::input_iterator_tag,
const char* methodName)
2870 size_type nInserted = 0;
2871 while (first != last1) {
2874 "There were still source elements available when the array" 2875 " reached its maximum size of %llu as determined by its index" 2876 " type %s.", ullMaxSize(), indexName());
2877 p = insert(p, *first++);
2890 template <
class ForwardIterator>
2891 T* insertIteratorDispatch(T* p,
const ForwardIterator& first,
2892 const ForwardIterator& last1,
2893 std::forward_iterator_tag,
2894 const char* methodName)
2896 typedef typename std::iterator_traits<ForwardIterator>::difference_type
2901 const difference_type nInput = this->iterDistance(first,last1);
2903 SimTK_ERRCHK(nInput >= 0, methodName,
"Iterators were out of order.");
2906 "Source has %llu elements which would make this array exceed the %llu" 2907 " elements allowed by its index type %s.",
2908 this->ull(nInput), ullMaxSize(), indexName());
2911 p = insertGapAt(p, n, methodName);
2912 copyConstruct(p, p+n, first);
2922 template <
class IntegralType>
2923 void assignDispatch(IntegralType n, IntegralType v, TrueType isIntegralType,
2924 const char* methodName)
2925 { assign(
size_type(n), value_type(v)); }
2930 template <
class InputIterator>
2931 void assignDispatch(
const InputIterator& first,
const InputIterator& last1,
2932 FalseType isIntegralType,
const char* methodName)
2933 { assignIteratorDispatch(first, last1,
2934 typename std::iterator_traits<InputIterator>::iterator_category(),
2940 template <
class InputIterator>
2941 void assignIteratorDispatch(
const InputIterator& first,
2942 const InputIterator& last1,
2943 std::input_iterator_tag,
2944 const char* methodName)
2949 "Assignment to a non-owner array can only be done from a source" 2950 " designated with forward iterators or pointers because we" 2951 " must be able to verify that the source and destination sizes" 2955 InputIterator src = first;
2956 while (src != last1) {
2959 "There were still source elements available when the array" 2960 " reached its maximum size of %llu as determined by its index" 2961 " type %s.", ullMaxSize(), indexName());
2972 template <
class ForwardIterator>
2973 void assignIteratorDispatch(
const ForwardIterator& first,
2974 const ForwardIterator& last1,
2975 std::forward_iterator_tag,
2976 const char* methodName)
2978 typedef typename std::iterator_traits<ForwardIterator>::difference_type
2983 const IterDiffType nInput = this->iterDistance(first,last1);
2985 SimTK_ERRCHK(nInput >= 0, methodName,
"Iterators were out of order.");
2988 "Source has %llu elements but this Array is limited to %llu" 2989 " elements by its index type %s.",
2990 this->ull(nInput), ullMaxSize(), indexName());
2998 reallocateIfAdvisable(n);
2999 copyConstruct(data(), cdata()+n, first);
3007 "Source has %llu elements which does not match the size %llu" 3008 " of the non-owner array it is being assigned into.",
3009 this->ull(n), ullSize());
3012 ForwardIterator src = first;
3013 while (src != last1)
3029 void reallocateIfAdvisable(size_type n) {
3030 if (allocated() < n || allocated()/2 >
std::max(minAlloc(), n))
3031 reallocateNoDestructOrConstruct(n);
3035 void allocateNoConstruct(size_type n)
3036 { setData(allocN(n)); setAllocated(n); }
3037 void deallocateNoDestruct()
3038 { freeN(data()); setData(0); setAllocated(0); }
3039 void reallocateNoDestructOrConstruct(size_type n)
3040 { deallocateNoDestruct(); allocateNoConstruct(n); }
3043 size_type minAlloc()
const 3049 static T* allocN(size_type n) {
3051 unsigned char* newdata =
new unsigned char[n *
sizeof(T)];
3053 unsigned char* b=newdata;
3054 const unsigned char* e=newdata+(n*
sizeof(T));
3055 while (b != e) *b++ = 0xff;
3057 return reinterpret_cast<T*
>(newdata);
3062 static void freeN(T* p) {
3063 delete[]
reinterpret_cast<char*
>(p);
3067 static void defaultConstruct(T* p) {
new(p) T();}
3069 static void defaultConstruct(T* b,
const T* e)
3070 {
while (b!=e)
new(b++) T(); }
3073 static void fillConstruct(T* b,
const T* e,
const T& v)
3074 {
while(b!=e)
new(b++) T(v); }
3077 static void copyConstruct(T* p,
const T& v) {
new(p) T(v);}
3079 static void copyConstruct(T* b,
const T* e, T* src)
3080 {
while(b!=e)
new(b++) T(*src++); }
3083 template <
class InputIterator>
3084 static void copyConstruct(T* b,
const T* e, InputIterator src)
3085 {
while(b!=e)
new(b++) T(*src++); }
3091 static void copyConstructThenDestructSource(T* b,
const T* e, T* src)
3092 {
while(b!=e) {
new(b++) T(*src); src++->~T();} }
3098 void moveOneElement(T* to, T*
from) {
3099 assert(data() <= to && to < data()+allocated());
3100 assert(data() <= from && from < data()+allocated());
3101 copyConstruct(to, *from);
3108 void moveElementsDown(T* p, size_type n) {
3110 for (; p != end(); ++p)
3111 moveOneElement(p-n,p);
3117 void moveElementsUp(T* p, size_type n) {
3122 moveOneElement(src+n, src);;
3127 static void destruct(T* p) {p->~T();}
3129 static void destruct(T* b,
const T* e)
3130 {
while(b!=e) b++->~T(); }
3135 bool isGrowthOK(S n)
const 3136 {
return this->isSizeOK(ullCapacity() + this->ull(n)); }
3148 void setData(
const T* p) {this->CBase::setData(p);}
3149 void setSize(size_type n) {this->CBase::setSize(n);}
3150 void incrSize() {this->CBase::incrSize();}
3151 void decrSize() {this->CBase::decrSize();}
3152 void setAllocated(size_type n) {this->CBase::setAllocated(n);}
3155 unsigned long long ullSize()
const 3156 {
return this->CBase::ullSize(); }
3157 unsigned long long ullCapacity()
const 3158 {
return this->CBase::ullCapacity(); }
3159 unsigned long long ullMaxSize()
const 3160 {
return this->CBase::ullMaxSize(); }
3163 const char* indexName()
const {
return this->CBase::indexName();}
3175 template <
class T,
class X>
static inline 3181 if (!in.good()) {in.setstate(std::ios::failbit);
return in;}
3191 numRequired = isFixedSize ? out.
size() : 0;
3199 std::ws(in);
if (in.fail())
return in;
3201 if (isFixedSize && numRequired != 0)
3202 in.setstate(std::ios_base::failbit);
3210 typename std::iostream::int_type ch;
3212 #ifndef NDEBUG // avoid unused variable warnings in Release 3213 const typename std::iostream::int_type EOFch =
3214 std::iostream::traits_type::eof();
3218 bool lookForCloser =
true;
3219 char openBracket, closeBracket;
3220 ch = in.peek();
if (in.fail())
return in;
3221 assert(ch != EOFch);
3223 openBracket = (char)ch;
3224 if (openBracket==
'(') {in.get(); closeBracket =
')';}
3225 else if (openBracket==
'[') {in.get(); closeBracket =
']';}
3226 else if (openBracket==
'{') {in.get(); closeBracket =
'}';}
3227 else lookForCloser =
false;
3233 if (in.good()) std::ws(in);
3239 assert(lookForCloser);
3240 in.setstate(std::ios::failbit);
3250 bool commaOK =
true, commaRequired =
false;
3251 bool terminatorSeen =
false;
3268 if (lookForCloser) {
3270 std::ws(in);
if (!in.good())
break;
3271 ch = in.peek(); assert(ch != EOFch);
3272 if (!in.good())
break;
3274 if (c == closeBracket) {
3276 terminatorSeen =
true;
3286 if (isFixedSize && (nextIndex == numRequired))
3290 if (commaOK && nextIndex != 0) {
3292 std::ws(in);
if (!in.good())
break;
3293 ch = in.peek(); assert(ch != EOFch);
3294 if (!in.good())
break;
3298 commaRequired =
true;
3301 { in.setstate(std::ios::failbit);
break; }
3302 else commaOK =
false;
3304 if (!in.good())
break;
3315 in >> out[nextIndex];
if (in.fail())
break;
3318 if (!in.good())
break;
3332 if (lookForCloser && !terminatorSeen)
3333 in.setstate(std::ios::failbit);
3335 if (isFixedSize && nextIndex != numRequired)
3336 in.setstate(std::ios::failbit);
3368 template <
class T,
class X>
inline void 3370 for (X i(0); i < v.
size(); ++i) {
3371 if (i != 0) o <<
" ";
3380 template <
class T,
class X>
inline void 3383 for (X i(0); i < v.
size(); ++i) {
3384 if (i != 0) o <<
',';
3396 template <
class T,
class X>
inline 3404 for (
const T* p = a.
begin()+1; p != a.
end(); ++p)
3414 template <
class T,
class X>
inline bool 3428 template <
class T,
class X>
inline bool 3430 for (X i(0); i < v.
size(); ++i)
3441 template <
class T,
class X>
inline bool 3443 return !readArrayFromStream(in,v).fail();
3451 template <
class T,
class X>
inline bool 3453 return !fillArrayViewFromStream(in,v).fail();
3485 template <
class T,
class X>
static inline 3487 {
return readArrayFromStreamHelper<T,X>(in,
false , out); }
3515 template <
class T,
class X>
static inline 3517 {
return readArrayFromStreamHelper<T,X>(in,
true , out); }
3523 template <
class T,
class X>
static inline 3525 {
return readArrayFromStreamHelper<T,X>(in,
true , out); }
3539 template <
class T,
class X>
inline 3541 {
return readArrayFromStream<T,X>(in, out); }
3550 template <
class T,
class X>
inline 3552 {
return fillArrayViewFromStream<T,X>(in, out); }
3566 template <
class T1,
class X1,
class T2,
class X2>
inline bool 3569 const ptrdiff_t sz1 = a1.
end()-a1.
begin();
3570 const ptrdiff_t sz2 = a2.
end()-a2.
begin();
3571 if (sz1 != sz2)
return false;
3572 const T1* p1 = a1.
begin();
3573 const T2* p2 = a2.
begin();
3574 while (p1 != a1.
end())
3575 if (!(*p1++ == *p2++))
return false;
3580 template <
class T1,
class X1,
class T2,
class X2>
inline bool 3582 {
return !(a1 == a2); }
3588 template <
class T1,
class X1,
class T2,
class X2>
inline bool 3590 const T1* p1 = a1.
begin();
3591 const T2* p2 = a2.begin();
3592 while (p1 != a1.end() && p2 != a2.end()) {
3594 return *p1 < *p2; // otherwise p1 > p2
3599 return p1 == a1.end() && p2 != a2.end();
3603 template <
class T1,
class X1,
class T2,
class X2>
inline bool 3605 {
return !(a1 < a2); }
3609 template <
class T1,
class X1,
class T2,
class X2>
inline bool 3614 template <
class T1,
class X1,
class T2,
class X2>
inline bool 3616 {
return !(a1 > a2); }
3621 template <
class T1,
class X1,
class T2,
class A2>
inline bool 3628 template <
class T1,
class A1,
class T2,
class X2>
inline bool 3630 {
return a2 == v1; }
3634 template <
class T1,
class X1,
class T2,
class A2>
inline bool 3636 {
return !(a1 == v2); }
3639 template <
class T1,
class A1,
class T2,
class X2>
inline bool 3641 {
return !(a2 == v1); }
3648 template <
class T1,
class X1,
class T2,
class A2>
inline bool 3649 operator<(const ArrayViewConst_<T1,X1>& a1,
const std::vector<T2,A2>& v2)
3650 {
return a1 < ArrayViewConst_<T2,size_t>(v2); }
3657 template <
class T1,
class A1,
class T2,
class X2>
inline bool 3663 template <
class T1,
class X1,
class T2,
class A2>
inline bool 3665 {
return !(a1 < v2); }
3668 template <
class T1,
class A1,
class T2,
class X2>
inline bool 3670 {
return !(v1 < a2); }
3675 template <
class T1,
class X1,
class T2,
class A2>
inline bool 3681 template <
class T1,
class A1,
class T2,
class X2>
inline bool 3687 template <
class T1,
class X1,
class T2,
class A2>
inline bool 3688 operator<=(const ArrayViewConst_<T1,X1>& a1,
const std::vector<T2,A2>& v2)
3689 {
return !(a1 > v2); }
3692 template <
class T1,
class A1,
class T2,
class X2>
inline bool 3694 {
return !(v1 > a2); }
3704 template <
class T,
class X>
inline void 3711 #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:3369
T & back()
Return a writable reference to the last element in this array, which must not be empty.
Definition: Array.h:1128
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:3429
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:1920
const T & back() const
Return a const reference to the last element in this array, which must not be empty.
Definition: Array.h:2297
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:928
const T * data() const
The const version of the data() method is identical to cdata().
Definition: Array.h:2227
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:351
const T * end() const
The const version of end() is the same as cend().
Definition: Array.h:643
const T & front() const
Return a const reference to the first element in this array, which must not be empty.
Definition: Array.h:1107
bool empty() const
Definition: Array.h:1258
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:1194
#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:1823
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:3676
T value_type
The type of object stored in this container.
Definition: Array.h:328
#define SimTK_SIZECHECK(sz, maxsz, where)
Definition: ExceptionMacros.h:146
ArrayView_()
Default constructor allocates no heap space and is very fast.
Definition: Array.h:872
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:3622
const T & at(index_type i) const
Same as operator[] but always range-checked, even in a Release build.
Definition: Array.h:1085
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: Array.h:856
~ArrayViewConst_()
The destructor just disconnects the array view handle from its data; see disconnect() for more inform...
Definition: Array.h:491
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:2224
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:1644
const T & front() const
Return a const reference to the first element in this array, which must not be empty.
Definition: Array.h:2283
T * iterator
A writable iterator for this container (same as pointer here).
Definition: Array.h:340
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:652
const_reverse_iterator rbegin() const
The const version of rbegin() is the same as crbegin().
Definition: Array.h:2199
const T & at(index_type i) const
Same as operator[] but always range-checked, even in a Release build.
Definition: Array.h:555
This is a special type used for causing invocation of a particular constructor or method overload tha...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:628
#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:1789
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:2147
std::reverse_iterator< iterator > reverse_iterator
Definition: Array.h:1529
T & reference
Definition: Array.h:1525
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:1848
size_type size() const
Return the current number of elements stored in this array.
Definition: Array.h:506
bool readUnformatted(std::istream &in, T &v)
The default implementation of readUnformatted<T> reads in the next whitespace-separated token and the...
Definition: Serialize.h:188
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
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:3415
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:454
const T & operator[](index_type i) const
Select an element by its index, returning a const reference.
Definition: Array.h:547
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:2053
T * iterator
Definition: Array.h:853
Array_(size_type n)
Construct an array containing n default-constructed elements.
Definition: Array.h:1553
const T * begin() const
The const version of begin() is the same as cbegin().
Definition: Array.h:641
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:528
const T & const_reference
A const value_type reference.
Definition: Array.h:338
T * pointer
A writable pointer to a value_type.
Definition: Array.h:332
const_reverse_iterator rend() const
The const version of rend() is the same as crend().
Definition: Array.h:657
const T * end() const
The const version of end() is the same as cend().
Definition: Array.h:1196
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:1585
size_type size() const
Definition: Array.h:1256
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:3640
T * pointer
Definition: Array.h:1523
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:2203
T value_type
Definition: Array.h:847
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:2268
const_reverse_iterator rend() const
The const version of rend() is the same as crend().
Definition: Array.h:1220
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:3610
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:2568
ArrayIndexPackType< size_type >::packed_size_type packed_size_type
Definition: Array.h:860
#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:2411
ArrayIndexTraits< X >::size_type size_type
Definition: Array.h:1531
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:2401
ArrayView_(std::vector< T, A > &v)
Construct to reference memory owned by a writable std::vector.
Definition: Array.h:882
#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:2037
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 doe...
Definition: Array.h:1613
ArrayIndexTraits< X >::size_type size_type
Definition: Array.h:857
ArrayViewConst_(const ArrayViewConst_ &src)
Copy constructor is shallow; the constructed const array object will be referencing the original sour...
Definition: Array.h:377
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:1957
~Array_()
The destructor performs a deallocate() operation which may result in element destruction and freeing ...
Definition: Array.h:1712
T * begin()
Return a writable pointer to the first element of this array if any, otherwise end().
Definition: Array.h:1188
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:1162
ArrayViewConst_()
Default constructor allocates no heap space and is very fast.
Definition: Array.h:366
const T * end() const
The const version of end() is the same as cend().
Definition: Array.h:2187
T & reference
A writable value_type reference.
Definition: Array.h:336
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:2172
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:1233
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:2434
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:3486
T & operator[](index_type i)
Select an element by its index, returning a writable (lvalue) reference.
Definition: Array.h:1078
X index_type
Definition: Array.h:848
Array_ & operator=(const std::vector< T2, A > &src)
This is assignment from a source std::vector<T2>.
Definition: Array.h:1933
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:916
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:1563
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:2154
This is a compile-time equivalent of "false", used in compile-time condition checking in templatized ...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:636
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:1979
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:2469
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:2312
const T * const_iterator
Definition: Array.h:854
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:522
T & at(index_type i)
Same as operator[] but always range-checked, even in a Release build.
Definition: Array.h:1091
static const char * name()
The default implementation of name() here returns the raw result from typeid(T).name() which will be ...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:765
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: Array.h:1530
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:1181
ArrayView_ & operator=(const T &fillValue)
Fill assignment – all elements are set to fillValue.
Definition: Array.h:961
const T * begin() const
The const version of begin() is the same as cbegin().
Definition: Array.h:2174
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:2379
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation and configuration files Object form shall mean any form resulting from mechanical transformation or translation of a Source including but not limited to compiled object generated and conversions to other media types Work shall mean the work of whether in Source or Object made available under the as indicated by a copyright notice that is included in or attached to the whether in Source or Object that is based or other modifications as a an original work of authorship For the purposes of this Derivative Works shall not include works that remain separable from
Definition: LICENSE.txt:39
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:3516
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:1212
const_reverse_iterator rend() const
The const version of rend() is the same as crend().
Definition: Array.h:2211
const_reverse_iterator rbegin() const
The const version of rbegin() is the same as crbegin().
Definition: Array.h:1208
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:2556
ArrayIndexTraits< X >::difference_type difference_type
Definition: Array.h:1532
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:1006
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:1201
#define SimTK_ERRCHK_ALWAYS(cond, whereChecked, msg)
Definition: ExceptionMacros.h:281
ArrayIndexPackType< size_type >::packed_size_type packed_size_type
Definition: Array.h:1534
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:2185
ArrayIndexPackType< size_type >::packed_size_type packed_size_type
The integral type we actually use internally to store size_type values.
Definition: Array.h:354
const T * data() const
The const version of the data() method is identical to cdata().
Definition: Array.h:1236
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:1095
void disconnect()
Forward to base class disconnect() method – clears the handle without doing anything to the data...
Definition: Array.h:894
bool empty() const
Return true if there are no elements currently stored in this array.
Definition: Array.h:512
const T & operator[](index_type i) const
Select an element by its index, returning a const reference.
Definition: Array.h:2246
T & front()
Return a writable reference to the first element in this array, which must not be empty...
Definition: Array.h:1114
#define SimTK_ERRCHK3_ALWAYS(cond, whereChecked, fmt, a1, a2, a3)
Definition: ExceptionMacros.h:293
const T & const_reference
Definition: Array.h:852
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:655
std::reverse_iterator< iterator > reverse_iterator
Definition: Array.h:855
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:947
T * data()
Return a writable pointer to the first allocated element of the array, or a null pointer if no space ...
Definition: Array.h:1240
const T * const_pointer
Definition: Array.h:1524
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:978
const T * const_pointer
A const pointer to a value_type.
Definition: Array.h:334
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:3604
void reserve(size_type n)
Ensure that this array has enough allocated capacity to hold the indicated number of elements...
Definition: Array.h:2097
T value_type
Definition: Array.h:1521
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:614
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:3635
const T & const_reference
Definition: Array.h:1526
ArrayIndexTraits< X >::difference_type difference_type
Definition: Array.h:858
bool isOwner() const
Definition: Array.h:1261
size_type max_size() const
Definition: Array.h:1257
#define SimTK_FORCE_INLINE
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:282
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:2272
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:3682
std::reverse_iterator< iterator > reverse_iterator
A writable reverse iterator for this container.
Definition: Array.h:344
void swap(Array_ &other)
This is a specialized algorithm providing constant time exchange of data with another array that has ...
Definition: Array.h:1946
#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:3381
#define SimTK_ERRCHK(cond, whereChecked, msg)
Definition: ExceptionMacros.h:324
gikDdMV wfaIJt A٩t1 JcA nr S q is3 ֧ VK C 9Z D q Fxn n T Y < ['jd< K JvTMH"sw>}o_o? z'z:mV$yng͖i۸J{ Ta*dE|lzbX@!^Ooi_=O}&ŲQUVWTsh!P_7DRAVfʿbOԹɫt0Y!|'x'óݥ:/ V[,}-B֞/܂;:;;Iޘ[nK4#-='Gf\lb41۩> Os7x f pZzB I g n
Definition: SimmathUserGuide.doc:2262
T * end()
Return a writable pointer to what would be the element just after the last one in this array...
Definition: Array.h:2192
#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:1217
T & back()
Return a writable reference to the last element in this array, which must not be empty.
Definition: Array.h:2304
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:600
Array_()
Default constructor allocates no heap space and is very fast.
Definition: Array.h:1547
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:3705
const T & operator[](index_type i) const
Select an element by its index, returning a const reference.
Definition: Array.h:1068
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:1677
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:2196
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:3664
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:2216
X index_type
Definition: Array.h:1522
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:647
Array_ & deallocate()
Empty this array of its contents, returning the array to its default-constructed, all-zero state...
Definition: Array.h:1724
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:2522
const T * begin() const
The const version of begin() is the same as cbegin().
Definition: Array.h:1183
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:639
#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:2208
ArrayViewConst_(const T *first, const T *last1)
Construct an ArrayViewConst_<T> by referencing (sharing) a given range of const data [first...
Definition: Array.h:406
X index_type
The index type (an extension).
Definition: Array.h:330
size_type allocated() const
Definition: Array.h:1260
T * iterator
Definition: Array.h:1527
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:3567
ArrayIndexTraits< X >::size_type size_type
An integral type suitable for all indices and sizes for this array.
Definition: Array.h:348
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:1048
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:1631
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:1099
const T * const_pointer
Definition: Array.h:850
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:3551
const T * const_iterator
Definition: Array.h:1528
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:3581
T * begin()
Return a writable pointer to the first element of this array if any, otherwise end().
Definition: Array.h:2179
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:2621
T & reference
Definition: Array.h:851
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:570
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:665
reverse_iterator rend()
Return a writable past-the-end reverse iterator that tests equal to a reverse iterator that has been ...
Definition: Array.h:1225
bool readFormatted(std::istream &in, ArrayView_< T, X > &v)
Specialization of readFormatted() for fixed-length ArrayView_<T,X>; uses fillArrayViewFromStream() to...
Definition: Array.h:3452
const T & at(index_type i) const
Same as operator[] but always range-checked, even in a Release build.
Definition: Array.h:2262
const T * const_iterator
A const iterator for this container (same as const_pointer here).
Definition: Array.h:342
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:969
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:578
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:2502
void disconnect()
Disconnect this array handle from any data to which it refers, restoring it to the condition it would...
Definition: Array.h:482
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:2321
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:1707
size_type capacity() const
Definition: Array.h:1259
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:2074
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:3669
T & front()
Return a writable reference to the first element in this array, which must not be empty...
Definition: Array.h:2290
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:1148
size_type capacity() const
Return the number of elements this array can currently hold without requiring reallocation.
Definition: Array.h:517
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:942
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:2132
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:1906
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:2359
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:2276
size_type max_size() const
Return the maximum allowable size for this array.
Definition: Array.h:508
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:3442
std::reverse_iterator< const_iterator > const_reverse_iterator
A const reverse iterator for this container.
Definition: Array.h:346
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:2308
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:1996
T & operator[](index_type i)
Select an element by its index, returning a writable (lvalue) reference.
Definition: Array.h:2256
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:3629
~ArrayView_()
The destructor just disconnects the array view handle from its data; see ArrayViewConst_<T,X>::disconnect() for more information.
Definition: Array.h:898
T * pointer
Definition: Array.h:849
ArrayView_(T *first, const T *last1)
Construct from a range of writable memory.
Definition: Array.h:878
void assign(const Iter &first, const Iter &last1)
Assign this array from a range [first,last1) given by non-pointer iterators.
Definition: Array.h:1896
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:2013
T * data()
Return a writable pointer to the first allocated element of the array, or a null pointer if no space ...
Definition: Array.h:2231
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:561
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:2317
size_type capacity() const
Return the number of elements this array can currently hold without requiring reallocation.
Definition: Array.h:2047
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:1205
bool isSizeInRange(char sz, char mx)
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:347
static std::istream & readArrayFromStreamHelper(std::istream &in, bool isFixedSize, Array_< T, X > &out)
Definition: Array.h:3177
size_type max_size() const
Return the maximum allowable size for this array.
Definition: Array.h:2039
bool empty() const
Return true if there are no elements currently stored in this array.
Definition: Array.h:2042
const T & back() const
Return a const reference to the last element in this array, which must not be empty.
Definition: Array.h:1121
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:3524
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:3540
ArrayView_(const ArrayView_ &src)
Copy constructor is shallow.
Definition: Array.h:875
const T * data() const
The const version of the data() method is identical to cdata().
Definition: Array.h:667
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:634
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...
Definition: Array.h:1595
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:2606
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:953