10 #ifndef TEUCHOS_ARRAY_H
11 #define TEUCHOS_ARRAY_H
25 #if defined(HAVE_TEUCHOSCORE_CXX11) && defined(HAVE_TEUCHOS_ARRAY_BOUNDSCHECK) && defined(HAVE_TEUCHOS_THREAD_SAFE) && !defined(REMOVE_THREAD_PROTECTION_FOR_ARRAY)
27 #define USE_MUTEX_LOCK_FOR_ARRAY
40 template<
typename T>
class Array;
56 template<
typename T>
inline
64 template<
typename T>
inline
72 template<
typename T>
inline
80 template<
typename T>
inline
81 bool operator<( const Array<T> &a1,
const Array<T> &a2 );
88 template<
typename T>
inline
89 bool operator<=( const Array<T> &a1,
const Array<T> &a2 );
96 template<
typename T>
inline
104 template<
typename T>
inline
172 template<
typename T2>
176 template<
typename T2>
180 template<
typename T2>
184 template<
typename T2>
185 friend bool Teuchos::operator<( const Array<T2> &a1,
const Array<T2> &a2 );
188 template<
typename T2>
189 friend bool Teuchos::operator<=( const Array<T2> &a1,
const Array<T2> &a2 );
192 template<
typename T2>
196 template<
typename T2>
211 typedef typename std::vector<T>::pointer
pointer;
221 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
231 typedef typename std::vector<T>::iterator
iterator;
255 template<
typename InputIterator>
256 inline Array(InputIterator first, InputIterator last);
266 inline Array(std::initializer_list<T> list);
285 template<
typename InputIterator>
286 inline void assign(InputIterator first, InputIterator last);
312 inline bool empty()
const;
340 template<
typename InputIterator>
341 inline void insert(
iterator position, InputIterator first, InputIterator last);
364 inline void remove(
int i);
370 inline int length()
const;
373 inline std::string
toString()
const;
392 inline const T*
data()
const;
399 inline Array(
const std::vector<T> &v );
402 inline std::vector<T>
toVector()
const;
478 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
482 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
483 mutable std::mutex mutex_lock;
489 inline std::vector<T>&
vec(
490 bool isStructureBeingModified =
false,
491 bool activeIter =
false
494 inline const std::vector<T>&
vec()
const;
496 inline typename std::vector<T>::iterator
514 if (
is_null(v) || !v->size() )
516 return arcpWithEmbeddedObjPostDestroy<T,RCP<Array<T> > >(
517 &(*v)[0], 0, v->size(),
531 if (
is_null(v) || !v->size() )
533 return arcpWithEmbeddedObjPostDestroy<const T,RCP<const Array<T> > >(
534 &(*v)[0], 0, v->size(),
589 std::ostream& operator<<(std::ostream& os, const Array<T>& array);
596 template<
typename T>
inline
597 int hashCode(
const Array<T>& array);
606 template<
typename T>
inline
607 std::vector<T> createVector(
const Array<T> &a );
670 Array<T> fromStringToArray(
const std::string& arrayStr);
679 array = fromStringToArray<T>(in.str());
688 template<
typename T>
inline
744 std::string formatString = getArrayTypeNameTraitsFormat();
745 size_t starPos = formatString.find(
"*");
746 std::string prefix = formatString.substr(0,starPos);
747 std::string postFix = formatString.substr(starPos+1);
769 template<
typename T>
inline
771 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
772 : vec_(
rcp(
new std::vector<T>()))
777 template<
typename T>
inline
779 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
780 vec_(
rcp(new std::vector<T>(n,value)))
787 template<
typename T>
inline
789 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
790 vec_(
rcp(new std::vector<T>(*x.vec_)))
797 template<
typename T>
template<
typename InputIterator>
inline
799 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
800 vec_(
rcp(new std::vector<T>(first, last)))
807 template<
typename T>
inline
812 template<
typename T>
inline
814 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
815 : vec_(
rcp(
new std::vector<T>()))
818 insert(begin(), a.
begin(), a.
end());
826 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
827 : vec_(
rcp(
new std::vector<T>()))
830 insert(begin(), t.
begin(), t.
end());
833 template<
typename T>
inline
835 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
836 : vec_(
rcp(
new std::vector<T>(a)))
842 template<
typename T>
inline
845 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
846 std::lock_guard<std::mutex> lockGuard(mutex_lock);
856 template<
typename T>
inline
859 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
860 std::lock_guard<std::mutex> lockGuard(mutex_lock);
862 vec(
true).assign(n,val);
866 template<
typename T>
template<
typename InputIterator>
inline
869 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
870 std::lock_guard<std::mutex> lockGuard(mutex_lock);
872 vec(
true).assign(first,last);
876 template<
typename T>
inline
880 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
882 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
883 std::lock_guard<std::mutex> lockGuard(mutex_lock);
888 extern_arcp_ = arcp(vec_);
893 return extern_arcp_.create_weak();
895 return vec().begin();
900 template<
typename T>
inline
904 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
905 return begin() + size();
911 template<
typename T>
inline
915 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
917 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
918 std::lock_guard<std::mutex> lockGuard(mutex_lock);
928 extern_arcp_ = arcp(vec_);
932 extern_carcp_ = extern_arcp_.create_weak();
937 return extern_carcp_.create_weak();
939 return vec().begin();
944 template<
typename T>
inline
948 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
949 return begin() + size();
956 template<
typename T>
inline
960 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
963 return vec().rbegin();
968 template<
typename T>
inline
972 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
980 template<
typename T>
inline
984 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
987 return vec().rbegin();
992 template<
typename T>
inline
996 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1004 template<
typename T>
inline
1008 return vec().size();
1012 template<
typename T>
inline
1016 return std::numeric_limits<size_type>::max();
1020 template<
typename T>
inline
1024 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1025 std::lock_guard<std::mutex> lockGuard(mutex_lock);
1027 vec(
true).resize(new_size,x);
1031 template<
typename T>
inline
1035 return vec().capacity();
1039 template<
typename T>
inline
1042 return vec().empty();
1046 template<
typename T>
inline
1049 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1050 std::lock_guard<std::mutex> lockGuard(mutex_lock);
1052 vec(
true).reserve(n);
1056 template<
typename T>
inline
1060 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1067 template<
typename T>
inline
1071 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1078 template<
typename T>
inline
1082 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1089 template<
typename T>
inline
1093 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1100 template<
typename T>
inline
1104 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1107 return vec().front();
1111 template<
typename T>
inline
1115 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1118 return vec().front();
1122 template<
typename T>
inline
1126 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1129 return vec().back();
1133 template<
typename T>
inline
1137 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1140 return vec().back();
1144 template<
typename T>
inline
1147 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1148 std::lock_guard<std::mutex> lockGuard(mutex_lock);
1150 vec(
true).push_back(x);
1154 template<
typename T>
inline
1157 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1160 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1161 std::lock_guard<std::mutex> lockGuard(mutex_lock);
1163 vec(
true).pop_back();
1179 template<
typename T>
inline
1183 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1185 const typename std::vector<T>::iterator raw_poss = raw_position(position);
1187 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1189 std::lock_guard<std::mutex> lockGuard(mutex_lock);
1191 vec(
true,
true).insert(raw_poss, x);
1192 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1197 return vec_.insert(position, x);
1202 template<
typename T>
inline
1205 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1206 const typename std::vector<T>::iterator raw_poss = raw_position(position);
1207 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1208 std::lock_guard<std::mutex> lockGuard(mutex_lock);
1210 vec(
true,
true).insert(raw_poss, n, x);
1212 vec_.insert(position, n, x);
1217 template<
typename T>
template<
typename InputIterator>
inline
1220 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1221 const typename std::vector<T>::iterator raw_poss = raw_position(position);
1222 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1223 std::lock_guard<std::mutex> lockGuard(mutex_lock);
1225 vec(
true,
true).insert(raw_poss, first, last);
1227 vec_.insert(position, first, last);
1232 template<
typename T>
inline
1236 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1239 const typename std::vector<T>::iterator raw_poss = raw_position(position);
1241 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1243 std::lock_guard<std::mutex> lockGuard(mutex_lock);
1245 vec(
true,
true).erase(raw_poss);
1246 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1251 return vec_.erase(position);
1256 template<
typename T>
inline
1260 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1268 const typename std::vector<T>::iterator raw_first = raw_position(first);
1269 const typename std::vector<T>::iterator raw_last = raw_position(last);
1271 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1273 std::lock_guard<std::mutex> lockGuard(mutex_lock);
1275 vec(
true,
true).erase(raw_first,raw_last);
1276 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1281 return vec_.erase(first,last);
1286 template<
typename T>
inline
1289 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1290 std::lock_guard<std::mutex> lockGuard(mutex_lock);
1292 vec(
true).swap(x.
vec());
1296 template<
typename T>
inline
1299 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1300 std::lock_guard<std::mutex> lockGuard(mutex_lock);
1309 template<
typename T>
inline
1317 template<
typename T>
inline
1320 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1324 this->erase( this->begin() + i );
1328 template<
typename T>
inline
1331 return static_cast<int> (this->size ());
1335 template<
typename T>
inline
1342 template<
typename T>
inline
1345 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1353 template<
typename T>
inline
1356 return ( size() ? &(*
this)[0] :
nullptr );
1359 template<
typename T>
inline
1362 return ( size() ? &(*
this)[0] :
nullptr );
1365 template<
typename T>
inline
1368 return ( size() ? &(*
this)[0] :
nullptr );
1371 template<
typename T>
inline
1374 return ( size() ? &(*
this)[0] :
nullptr );
1380 template<
typename T>
inline
1382 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1383 vec_(new std::vector<T>(v))
1390 template<
typename T>
inline
1394 return std::vector<T>();
1395 std::vector<T> v(begin(),end());
1400 template<
typename T>
inline
1403 #ifdef USE_MUTEX_LOCK_FOR_ARRAY
1404 std::lock_guard<std::mutex> lockGuard(mutex_lock);
1414 template<
typename T>
inline
1418 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1419 return ArrayView<T>(this->begin().persistingView(offset, size_in));
1421 return arrayView( &vec()[offset], size_in );
1428 template<
typename T>
inline
1432 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1435 return arrayView( &vec()[offset], size_in );
1445 template<
typename T>
inline
1448 return view(offset, size_in);
1452 template<
typename T>
inline
1455 return view(offset, size_in);
1459 template<
typename T>
inline
1464 return this->view(0, size());
1468 template<
typename T>
inline
1473 return this->view(0, size());
1477 template<
typename T>
inline
1480 return this->operator()();
1484 template<
typename T>
inline
1487 return this->operator()();
1494 template<
typename T>
1498 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1500 if (isStructureBeingModified) {
1508 extern_arcp_ =
null;
1509 extern_carcp_ =
null;
1514 (void)isStructureBeingModified;
1521 template<
typename T>
inline
1522 const std::vector<T>&
1525 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1533 template<
typename T>
inline
1534 typename std::vector<T>::iterator
1537 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1538 const iterator first = this->begin();
1542 "Error, this iterator is no longer valid for this Aray!"
1547 return vec_->begin() + (position - this->begin());
1554 template<
typename T>
inline
1559 "Array<T>::assertIndex(i): i="<<i<<
" out of range [0, "<< size() <<
")"
1564 template<
typename T>
inline
1569 typeName(*
this)<<
"::assertNotNull(): "
1570 "Error, the array has size zero!"
1581 template<
typename T>
inline
1583 {
return (a1.vec() == a2.vec()); }
1586 template<
typename T>
inline
1588 {
return (a1.vec() != a2.vec()); }
1591 template<
typename T>
inline
1592 void Teuchos::swap( Array<T> &a1, Array<T> &a2 )
1596 template<
typename T>
inline
1597 bool Teuchos::operator<( const Array<T> &a1,
const Array<T> &a2 )
1598 {
return (a1.vec() < a2.vec()); }
1601 template<
typename T>
inline
1602 bool Teuchos::operator<=( const Array<T> &a1,
const Array<T> &a2 )
1603 {
return (a1.vec() <= a2.vec()); }
1606 template<
typename T>
inline
1608 {
return (a1.vec() > a2.vec()); }
1611 template<
typename T>
inline
1613 {
return (a1.vec() >= a2.vec()); }
1616 template<
typename T>
inline
1618 std::ostream& os,
const Array<T>& array
1625 template<
typename T>
inline
1626 int Teuchos::hashCode(
const Array<T>& array)
1628 int rtn =
hashCode(array.length());
1629 for (
int i=0; i<array.length(); i++)
1638 size_t maxIntBeforeWrap = std::numeric_limits<int>::max();
1639 maxIntBeforeWrap ++;
1640 rtn += maxIntBeforeWrap;
1646 template<
typename T>
inline
1647 std::vector<T> Teuchos::createVector(
const Array<T> &a )
1649 return a.toVector();
1653 template<
typename T>
inline
1656 return array.toString();
1660 template<
typename T>
1662 Teuchos::fromStringToArray(
const std::string& arrayStr)
1664 const std::string str = Utils::trimWhiteSpace(arrayStr);
1665 std::istringstream iss(str);
1667 ( str[0]!=
'{' || str[str.length()-1] !=
'}' )
1668 ,InvalidArrayStringRepresentation
1669 ,
"Error, the std::string:\n"
1673 "is not a valid array represntation!"
1680 while( !iss.eof() ) {
1682 std::string entryStr;
1683 std::getline(iss,entryStr,
',');
1692 entryStr = Utils::trimWhiteSpace(entryStr);
1694 0 == entryStr.length(),
1695 InvalidArrayStringRepresentation,
1696 "Error, the std::string:\n"
1700 "is not a valid array represntation because it has an empty array entry!"
1704 bool found_end =
false;
1705 if(entryStr[entryStr.length()-1]==
'}') {
1706 entryStr = entryStr.substr(0,entryStr.length()-1);
1708 if( entryStr.length()==0 && a.size()==0 )
1712 std::istringstream entryiss(entryStr);
1714 Teuchos::extractDataFromISS( entryiss, entry );
1723 found_end && !iss.eof()
1724 ,InvalidArrayStringRepresentation
1725 ,
"Error, the std::string:\n"
1729 "is not a valid array represntation!"
1736 #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...