42 #ifndef TEUCHOS_ARRAY_H
43 #define TEUCHOS_ARRAY_H
57 #if defined(HAVE_TEUCHOSCORE_CXX11) && defined(HAVE_TEUCHOS_ARRAY_BOUNDSCHECK) && defined(HAVE_TEUCHOS_THREAD_SAFE) && !defined(REMOVE_THREAD_PROTECTION_FOR_ARRAY)
59 #define USE_MUTEX_LOCK_FOR_ARRAY
72 template<
typename T>
class Array;
88 template<
typename T>
inline
96 template<
typename T>
inline
104 template<
typename T>
inline
112 template<
typename T>
inline
113 bool operator<( const Array<T> &a1,
const Array<T> &a2 );
120 template<
typename T>
inline
121 bool operator<=( const Array<T> &a1,
const Array<T> &a2 );
128 template<
typename T>
inline
136 template<
typename T>
inline
204 template<
typename T2>
208 template<
typename T2>
212 template<
typename T2>
216 template<
typename T2>
217 friend bool Teuchos::operator<( const Array<T2> &a1,
const Array<T2> &a2 );
220 template<
typename T2>
221 friend bool Teuchos::operator<=( const Array<T2> &a1,
const Array<T2> &a2 );
224 template<
typename T2>
228 template<
typename T2>
243 typedef typename std::vector<T>::pointer
pointer;
253 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
263 typedef typename std::vector<T>::iterator
iterator;
287 template<
typename InputIterator>
288 inline Array(InputIterator first, InputIterator last);
314 template<
typename InputIterator>
315 inline void assign(InputIterator first, InputIterator last);
341 inline bool empty()
const;
369 template<
typename InputIterator>
370 inline void insert(
iterator position, InputIterator first, InputIterator last);
393 inline void remove(
int i);
399 inline int length()
const;
402 inline std::string
toString()
const;
421 inline const T*
data()
const;
428 inline Array(
const std::vector<T> &v );
431 inline std::vector<T>
toVector()
const;
507 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
511 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
512 mutable std::mutex mutex_lock;
518 inline std::vector<T>&
vec(
519 bool isStructureBeingModified =
false,
520 bool activeIter =
false
523 inline const std::vector<T>&
vec()
const;
525 inline typename std::vector<T>::iterator
543 if (
is_null(v) || !v->size() )
545 return arcpWithEmbeddedObjPostDestroy<T,RCP<Array<T> > >(
546 &(*v)[0], 0, v->size(),
560 if (
is_null(v) || !v->size() )
562 return arcpWithEmbeddedObjPostDestroy<const T,RCP<const Array<T> > >(
563 &(*v)[0], 0, v->size(),
618 std::ostream& operator<<(std::ostream& os, const Array<T>& array);
625 template<
typename T>
inline
626 int hashCode(
const Array<T>& array);
635 template<
typename T>
inline
636 std::vector<T> createVector(
const Array<T> &a );
699 Array<T> fromStringToArray(
const std::string& arrayStr);
708 array = fromStringToArray<T>(in.str());
717 template<
typename T>
inline
773 std::string formatString = getArrayTypeNameTraitsFormat();
774 size_t starPos = formatString.find(
"*");
775 std::string prefix = formatString.substr(0,starPos);
776 std::string postFix = formatString.substr(starPos+1);
798 template<
typename T>
inline
800 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
801 : vec_(
rcp(
new std::vector<T>()))
806 template<
typename T>
inline
808 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
809 vec_(
rcp(new std::vector<T>(n,value)))
816 template<
typename T>
inline
818 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
819 vec_(
rcp(new std::vector<T>(*x.vec_)))
826 template<
typename T>
template<
typename InputIterator>
inline
828 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
829 vec_(
rcp(new std::vector<T>(first, last)))
836 template<
typename T>
inline
841 template<
typename T>
inline
843 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
844 : vec_(
rcp(
new std::vector<T>()))
847 insert(begin(), a.
begin(), a.
end());
855 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
856 : vec_(
rcp(
new std::vector<T>()))
859 insert(begin(), t.
begin(), t.
end());
863 template<
typename T>
inline
866 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
867 std::lock_guard<std::mutex> lockGuard(mutex_lock);
877 template<
typename T>
inline
880 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
881 std::lock_guard<std::mutex> lockGuard(mutex_lock);
883 vec(
true).assign(n,val);
887 template<
typename T>
template<
typename InputIterator>
inline
890 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
891 std::lock_guard<std::mutex> lockGuard(mutex_lock);
893 vec(
true).assign(first,last);
897 template<
typename T>
inline
901 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
903 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
904 std::lock_guard<std::mutex> lockGuard(mutex_lock);
909 extern_arcp_ = arcp(vec_);
914 return extern_arcp_.create_weak();
916 return vec().begin();
921 template<
typename T>
inline
925 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
926 return begin() + size();
932 template<
typename T>
inline
936 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
938 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
939 std::lock_guard<std::mutex> lockGuard(mutex_lock);
949 extern_arcp_ = arcp(vec_);
953 extern_carcp_ = extern_arcp_.create_weak();
958 return extern_carcp_.create_weak();
960 return vec().begin();
965 template<
typename T>
inline
969 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
970 return begin() + size();
977 template<
typename T>
inline
981 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
984 return vec().rbegin();
989 template<
typename T>
inline
993 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1001 template<
typename T>
inline
1005 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1008 return vec().rbegin();
1013 template<
typename T>
inline
1017 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1020 return vec().rend();
1025 template<
typename T>
inline
1029 return vec().size();
1033 template<
typename T>
inline
1037 return std::numeric_limits<size_type>::max();
1041 template<
typename T>
inline
1045 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1046 std::lock_guard<std::mutex> lockGuard(mutex_lock);
1048 vec(
true).resize(new_size,x);
1052 template<
typename T>
inline
1056 return vec().capacity();
1060 template<
typename T>
inline
1063 return vec().empty();
1067 template<
typename T>
inline
1070 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1071 std::lock_guard<std::mutex> lockGuard(mutex_lock);
1073 vec(
true).reserve(n);
1077 template<
typename T>
inline
1081 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1088 template<
typename T>
inline
1092 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1099 template<
typename T>
inline
1103 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1110 template<
typename T>
inline
1114 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1121 template<
typename T>
inline
1125 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1128 return vec().front();
1132 template<
typename T>
inline
1136 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1139 return vec().front();
1143 template<
typename T>
inline
1147 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1150 return vec().back();
1154 template<
typename T>
inline
1158 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1161 return vec().back();
1165 template<
typename T>
inline
1168 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1169 std::lock_guard<std::mutex> lockGuard(mutex_lock);
1171 vec(
true).push_back(x);
1175 template<
typename T>
inline
1178 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1181 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1182 std::lock_guard<std::mutex> lockGuard(mutex_lock);
1184 vec(
true).pop_back();
1200 template<
typename T>
inline
1204 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1206 const typename std::vector<T>::iterator raw_poss = raw_position(position);
1208 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1210 std::lock_guard<std::mutex> lockGuard(mutex_lock);
1212 vec(
true,
true).insert(raw_poss, x);
1213 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1218 return vec_.insert(position, x);
1223 template<
typename T>
inline
1226 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1227 const typename std::vector<T>::iterator raw_poss = raw_position(position);
1228 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1229 std::lock_guard<std::mutex> lockGuard(mutex_lock);
1231 vec(
true,
true).insert(raw_poss, n, x);
1233 vec_.insert(position, n, x);
1238 template<
typename T>
template<
typename InputIterator>
inline
1241 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1242 const typename std::vector<T>::iterator raw_poss = raw_position(position);
1243 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1244 std::lock_guard<std::mutex> lockGuard(mutex_lock);
1246 vec(
true,
true).insert(raw_poss, first, last);
1248 vec_.insert(position, first, last);
1253 template<
typename T>
inline
1257 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1260 const typename std::vector<T>::iterator raw_poss = raw_position(position);
1262 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1264 std::lock_guard<std::mutex> lockGuard(mutex_lock);
1266 vec(
true,
true).erase(raw_poss);
1267 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1272 return vec_.erase(position);
1277 template<
typename T>
inline
1281 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1289 const typename std::vector<T>::iterator raw_first = raw_position(first);
1290 const typename std::vector<T>::iterator raw_last = raw_position(last);
1292 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1294 std::lock_guard<std::mutex> lockGuard(mutex_lock);
1296 vec(
true,
true).erase(raw_first,raw_last);
1297 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1302 return vec_.erase(first,last);
1307 template<
typename T>
inline
1310 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1311 std::lock_guard<std::mutex> lockGuard(mutex_lock);
1313 vec(
true).swap(x.
vec());
1317 template<
typename T>
inline
1320 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1321 std::lock_guard<std::mutex> lockGuard(mutex_lock);
1330 template<
typename T>
inline
1338 template<
typename T>
inline
1341 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1345 this->erase( this->begin() + i );
1349 template<
typename T>
inline
1352 return static_cast<int> (this->size ());
1356 template<
typename T>
inline
1363 template<
typename T>
inline
1366 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1374 template<
typename T>
inline
1377 return ( size() ? &(*
this)[0] :
nullptr );
1380 template<
typename T>
inline
1383 return ( size() ? &(*
this)[0] :
nullptr );
1386 template<
typename T>
inline
1389 return ( size() ? &(*
this)[0] :
nullptr );
1392 template<
typename T>
inline
1395 return ( size() ? &(*
this)[0] :
nullptr );
1401 template<
typename T>
inline
1403 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1404 vec_(new std::vector<T>(v))
1411 template<
typename T>
inline
1415 return std::vector<T>();
1416 std::vector<T> v(begin(),end());
1421 template<
typename T>
inline
1424 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1425 std::lock_guard<std::mutex> lockGuard(mutex_lock);
1435 template<
typename T>
inline
1439 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1440 return ArrayView<T>(this->begin().persistingView(offset, size_in));
1442 return arrayView( &vec()[offset], size_in );
1449 template<
typename T>
inline
1453 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1456 return arrayView( &vec()[offset], size_in );
1466 template<
typename T>
inline
1469 return view(offset, size_in);
1473 template<
typename T>
inline
1476 return view(offset, size_in);
1480 template<
typename T>
inline
1485 return this->view(0, size());
1489 template<
typename T>
inline
1494 return this->view(0, size());
1498 template<
typename T>
inline
1501 return this->operator()();
1505 template<
typename T>
inline
1508 return this->operator()();
1515 template<
typename T>
1519 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1521 if (isStructureBeingModified) {
1529 extern_arcp_ =
null;
1530 extern_carcp_ =
null;
1535 (void)isStructureBeingModified;
1542 template<
typename T>
inline
1543 const std::vector<T>&
1546 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1554 template<
typename T>
inline
1555 typename std::vector<T>::iterator
1558 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1559 const iterator first = this->begin();
1563 "Error, this iterator is no longer valid for this Aray!"
1568 return vec_->begin() + (position - this->begin());
1575 template<
typename T>
inline
1580 "Array<T>::assertIndex(i): i="<<i<<
" out of range [0, "<< size() <<
")"
1585 template<
typename T>
inline
1590 typeName(*
this)<<
"::assertNotNull(): "
1591 "Error, the array has size zero!"
1602 template<
typename T>
inline
1604 {
return (a1.vec() == a2.vec()); }
1607 template<
typename T>
inline
1609 {
return (a1.vec() != a2.vec()); }
1612 template<
typename T>
inline
1613 void Teuchos::swap( Array<T> &a1, Array<T> &a2 )
1617 template<
typename T>
inline
1618 bool Teuchos::operator<( const Array<T> &a1,
const Array<T> &a2 )
1619 {
return (a1.vec() < a2.vec()); }
1622 template<
typename T>
inline
1623 bool Teuchos::operator<=( const Array<T> &a1,
const Array<T> &a2 )
1624 {
return (a1.vec() <= a2.vec()); }
1627 template<
typename T>
inline
1629 {
return (a1.vec() > a2.vec()); }
1632 template<
typename T>
inline
1634 {
return (a1.vec() >= a2.vec()); }
1637 template<
typename T>
inline
1639 std::ostream& os,
const Array<T>& array
1646 template<
typename T>
inline
1647 int Teuchos::hashCode(
const Array<T>& array)
1649 int rtn =
hashCode(array.length());
1650 for (
int i=0; i<array.length(); i++)
1659 size_t maxIntBeforeWrap = std::numeric_limits<int>::max();
1660 maxIntBeforeWrap ++;
1661 rtn += maxIntBeforeWrap;
1667 template<
typename T>
inline
1668 std::vector<T> Teuchos::createVector(
const Array<T> &a )
1670 return a.toVector();
1674 template<
typename T>
inline
1677 return array.toString();
1681 template<
typename T>
1683 Teuchos::fromStringToArray(
const std::string& arrayStr)
1685 const std::string str = Utils::trimWhiteSpace(arrayStr);
1686 std::istringstream iss(str);
1688 ( str[0]!=
'{' || str[str.length()-1] !=
'}' )
1689 ,InvalidArrayStringRepresentation
1690 ,
"Error, the std::string:\n"
1694 "is not a valid array represntation!"
1701 while( !iss.eof() ) {
1703 std::string entryStr;
1704 std::getline(iss,entryStr,
',');
1713 entryStr = Utils::trimWhiteSpace(entryStr);
1715 0 == entryStr.length(),
1716 InvalidArrayStringRepresentation,
1717 "Error, the std::string:\n"
1721 "is not a valid array represntation because it has an empty array entry!"
1725 bool found_end =
false;
1726 if(entryStr[entryStr.length()-1]==
'}') {
1727 entryStr = entryStr.substr(0,entryStr.length()-1);
1729 if( entryStr.length()==0 && a.size()==0 )
1733 std::istringstream entryiss(entryStr);
1735 Teuchos::extractDataFromISS( entryiss, entry );
1744 found_end && !iss.eof()
1745 ,InvalidArrayStringRepresentation
1746 ,
"Error, the std::string:\n"
1750 "is not a valid array represntation!"
1757 #endif // TEUCHOS_ARRAY_H
static std::string concreteName(const Array< T > &)
Dangling reference error exception class.
Null reference error exception class.
void remove(int i)
Remove the i-th element from the array, with optional boundschecking.
void reserve(size_type n)
int hashCode(const Array< T > &array)
Return the hash code.
Array< T > & append(const T &x)
Add a new entry at the end of the array.
std::string typeName(const T &t)
Template function for returning the concrete type name of a passed-in object.
Partial specialization of ArrayRCP for const T.
static bool hasBoundsChecking()
Return true if Array has been compiled with boundschecking on.
iterator begin() const
Return an iterator to beginning of the array of data.
static std::string name()
ArrayView< T > view(size_type offset, size_type size)
Return non-const view of a contiguous range of elements.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
std::vector< T >::value_type value_type
The type of an entry of the Array; for compatibility with std::vector.
std::vector< T >::iterator raw_position(iterator position)
void extractDataFromISS(std::istringstream &iss, std::string &data)
Extracts std::string data from an istringstream object.
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
iterator erase(iterator position)
ArrayRCP< T > arcp(const RCP< Array< T > > &v)
Wrap an RCP<Array<T> > object as an ArrayRCP<T> object.
Array & operator=(const Array< T > &a)
Assignment operator (does a deep copy).
T * getRawPtr()
Return a raw pointer to beginning of array or NULL if unsized.
std::vector< T >::const_pointer const_pointer
The type of a const pointer to T; for compatibility with std::vector.
ArrayRCP< T > arcpFromArray(Array< T > &a)
Wrap an Array<T> object as a non-owning ArrayRCP<T> object.
ArrayRCP< const T > arcp(const RCP< const Array< T > > &v)
Wrap a RCP<const Array<T> > object as an ArrayRCP<const T> object.
void assertNotNull() const
bool operator!=(const Allocator< T > &a_t, const Allocator< U > &a_u)
Return ! (a_t == a_u) (see above).
std::istringstream & operator>>(std::istringstream &in, TwoDArray< T > &array)
static std::string trimWhiteSpace(const std::string &str)
Trim whitespace from beginning and end of std::string.
const std::vector< T > & vec() const
std::ostream & operator<<(std::ostream &os, BigUInt< n > a)
Statically sized simple array (tuple) class.
TEUCHOS_ORDINAL_TYPE Teuchos_Ordinal
Ordinal size_type
The type of Array sizes and capacities.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
bool operator>(BigUInt< n > const &a, BigUInt< n > const &b)
Ordinal difference_type
The type of the difference between two size_type values.
bool operator>=(BigUInt< n > const &a, BigUInt< n > const &b)
#define TEUCHOSCORE_LIB_DLL_EXPORT
std::string toString(const HashSet< Key > &h)
std::string toString() const
Convert an Array to an std::string
std::vector< T > & vec(bool isStructureBeingModified=false, bool activeIter=false)
T * data()
Return a raw pointer to beginning of array.
ArrayRCP< const T > arcpFromArray(const Array< T > &a)
Wrap a const Array<T> object as a non-owning ArrayRCP<T> object.
std::string getArrayTypeNameTraitsFormat()
Get the format that is used for the specialization of the TypeName traits class for Array...
friend void swap(Array< T2 > &a1, Array< T2 > &a2)
void resize(size_type new_size, const value_type &x=value_type())
Teuchos_Ordinal Ordinal
The type of indices.
std::vector< T >::reference reference
The type of a reference to T; for compatibility with std::vector.
std::vector< T >::pointer pointer
The type of a pointer to T; for compatibility with std::vector.
std::vector< T >::const_iterator const_iterator
The type of a const forward iterator.
reverse_iterator rbegin()
std::vector< T >::const_reference const_reference
The type of a const reference to T; for compatibility with std::vector.
void push_back(const value_type &x)
Default traits class that just returns typeid(T).name().
void extractDataFromISS(std::istringstream &iss, T &data)
Extracts data from an istringstream object.
std::vector< T >::const_reverse_iterator const_reverse_iterator
The type of a const reverse iterator.
iterator end() const
Return an iterator to past the end of the array of data.
int length() const
Return number of elements in the array.
InvalidArrayStringRepresentation(const std::string &what_arg)
std::vector< T >::allocator_type allocator_type
The allocator type; for compatibility with std::vector.
std::vector< T > toVector() const
Explicit copy conversion to an std::vector.
ArrayView< T > operator()()
Return an non-const ArrayView of *this.
reference at(size_type i)
Smart reference counting pointer class for automatic garbage collection.
reference operator[](size_type i)
Partial specialization of ArrayView for const T.
#define TEUCHOS_ASSERT(assertion_test)
This macro is throws when an assert fails.
void assign(size_type n, const value_type &val)
Range error exception class.
bool operator==(BigUInt< n > const &a, BigUInt< n > const &b)
Array()
Default constructor; creates an empty Array.
size_type max_size() const
std::vector< T >::iterator iterator
The type of a forward iterator.
A utilities class for Teuchos.
iterator insert(iterator position, const value_type &x)
Defines basic traits returning the name of a type in a portable and readable way. ...
std::vector< T >::reverse_iterator reverse_iterator
The type of a reverse iterator.
size_type capacity() const
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
This macro is designed to be a short version of TEUCHOS_TEST_FOR_EXCEPTION() that is easier to call...
static std::string name()
void assertIndex(size_type i) const
Reference-counted smart pointer for managing arrays.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes...