1 #ifndef SimTK_SimTKCOMMON_ARRAY_H_
2 #define SimTK_SimTKCOMMON_ARRAY_H_
47 template <
class X>
struct ArrayIndexTraits;
48 template <
class T,
class X=
unsigned>
class ArrayViewConst_;
49 template <
class T,
class X=
unsigned>
class ArrayView_;
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;};
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();}
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)
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();}
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() {
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());
1620 template <
class T2,
class X2>
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) {
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);
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,
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
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_