10#ifndef TEUCHOS_ARRAY_H
11#define TEUCHOS_ARRAY_H
18#include "Teuchos_Assert.hpp"
20#include "Teuchos_ArrayRCP.hpp"
21#include "Teuchos_Tuple.hpp"
23#include "Teuchos_Assert.hpp"
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
40template<
typename T>
class Array;
56template<
typename T>
inline
64template<
typename T>
inline
72template<
typename T>
inline
80template<
typename T>
inline
88template<
typename T>
inline
96template<
typename T>
inline
104template<
typename T>
inline
172 template<
typename T2>
176 template<
typename T2>
180 template<
typename T2>
184 template<
typename T2>
188 template<
typename T2>
192 template<
typename T2>
196 template<
typename T2>
211 typedef typename std::vector<T>::pointer
pointer;
221#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
232 typedef typename std::vector<T>::iterator
iterator;
255 template<
typename InputIterator>
266 inline Array(std::initializer_list<T> list);
285 template<
typename InputIterator>
340 template<
typename InputIterator>
478#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
482#ifdef USE_MUTEX_LOCK_FOR_ARRAY
489 inline std::vector<T>& vec(
494 inline const std::vector<T>& vec()
const;
496 inline typename std::vector<T>::iterator
501 inline void assertNotNull()
const;
517 &(*v)[0], 0,
v->size(),
534 &(*v)[0], 0,
v->size(),
596template<
typename T>
inline
606template<
typename T>
inline
688template<
typename T>
inline
743 static std::string name(){
744 std::string
formatString = getArrayTypeNameTraitsFormat();
750 static std::string concreteName(
const Array<T>&)
769template<
typename T>
inline
771#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
772 : vec_(
rcp(
new std::vector<T>()))
777template<
typename T>
inline
787template<
typename T>
inline
797template<
typename T>
template<
typename InputIterator>
inline
807template<
typename T>
inline
812template<
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());
833template<
typename T>
inline
835#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
836 : vec_(
rcp(
new std::vector<T>(
a)))
842template<
typename T>
inline
845#ifdef USE_MUTEX_LOCK_FOR_ARRAY
856template<
typename T>
inline
859#ifdef USE_MUTEX_LOCK_FOR_ARRAY
862 vec(
true).assign(n,
val);
866template<
typename T>
template<
typename InputIterator>
inline
869#ifdef USE_MUTEX_LOCK_FOR_ARRAY
872 vec(
true).assign(first,
last);
876template<
typename T>
inline
880#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
882#ifdef USE_MUTEX_LOCK_FOR_ARRAY
895 return vec().begin();
900template<
typename T>
inline
904#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
905 return begin() + size();
911template<
typename T>
inline
915#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
917#ifdef USE_MUTEX_LOCK_FOR_ARRAY
939 return vec().begin();
944template<
typename T>
inline
948#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
949 return begin() + size();
956template<
typename T>
inline
960#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
963 return vec().rbegin();
968template<
typename T>
inline
972#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
980template<
typename T>
inline
984#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
987 return vec().rbegin();
992template<
typename T>
inline
996#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1004template<
typename T>
inline
1008 return vec().size();
1012template<
typename T>
inline
1016 return std::numeric_limits<size_type>::max();
1020template<
typename T>
inline
1024#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1031template<
typename T>
inline
1035 return vec().capacity();
1039template<
typename T>
inline
1042 return vec().empty();
1046template<
typename T>
inline
1049#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1052 vec(
true).reserve(n);
1056template<
typename T>
inline
1060#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1067template<
typename T>
inline
1071#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1078template<
typename T>
inline
1082#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1089template<
typename T>
inline
1093#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1100template<
typename T>
inline
1104#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1107 return vec().front();
1111template<
typename T>
inline
1115#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1118 return vec().front();
1122template<
typename T>
inline
1126#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1129 return vec().back();
1133template<
typename T>
inline
1137#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1140 return vec().back();
1144template<
typename T>
inline
1147#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1150 vec(
true).push_back(x);
1154template<
typename T>
inline
1157#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1160#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1163 vec(
true).pop_back();
1179template<
typename T>
inline
1183#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1187#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1191 vec(
true,
true).insert(
raw_poss, x);
1192#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1202template<
typename T>
inline
1205#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1207#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1210 vec(
true,
true).insert(
raw_poss, n, x);
1217template<
typename T>
template<
typename InputIterator>
inline
1220#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1222#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1232template<
typename T>
inline
1236#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1241#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1246#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1256template<
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
1276#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1281 return vec_.erase(first,
last);
1286template<
typename T>
inline
1289#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1292 vec(
true).swap(x.vec());
1296template<
typename T>
inline
1299#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1309template<
typename T>
inline
1317template<
typename T>
inline
1320#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1324 this->erase( this->begin() +
i );
1328template<
typename T>
inline
1331 return static_cast<int> (this->size ());
1335template<
typename T>
inline
1338 return (*
this)().toString();
1342template<
typename T>
inline
1345#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1353template<
typename T>
inline
1356 return ( size() ? &(*
this)[0] :
nullptr );
1359template<
typename T>
inline
1362 return ( size() ? &(*
this)[0] :
nullptr );
1365template<
typename T>
inline
1368 return ( size() ? &(*
this)[0] :
nullptr );
1371template<
typename T>
inline
1374 return ( size() ? &(*
this)[0] :
nullptr );
1380template<
typename T>
inline
1390template<
typename T>
inline
1394 return std::vector<T>();
1395 std::vector<T>
v(begin(),end());
1400template<
typename T>
inline
1403#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1414template<
typename T>
inline
1418#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1424 return Teuchos::null;
1428template<
typename T>
inline
1432#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1438 return Teuchos::null;
1445template<
typename T>
inline
1452template<
typename T>
inline
1459template<
typename T>
inline
1464 return this->view(0, size());
1468template<
typename T>
inline
1473 return this->view(0, size());
1477template<
typename T>
inline
1480 return this->operator()();
1484template<
typename T>
inline
1487 return this->operator()();
1498#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1514 (void)isStructureBeingModified;
1521template<
typename T>
inline
1522const std::vector<T>&
1523Array<T>::vec()
const
1525#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1533template<
typename T>
inline
1534typename std::vector<T>::iterator
1535Array<T>::raw_position( iterator position )
1537#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1538 const iterator first = this->begin();
1539 const iterator last = this->end();
1541 !(first <= position && position <= last), DanglingReferenceError,
1542 "Error, this iterator is no longer valid for this Aray!"
1547 return vec_->begin() + (position - this->begin());
1554template<
typename T>
inline
1555void Array<T>::assertIndex(size_type i)
const
1558 !( 0 <= i && i < size() ), RangeError,
1559 "Array<T>::assertIndex(i): i="<<i<<
" out of range [0, "<< size() <<
")"
1564template<
typename T>
inline
1565void Array<T>::assertNotNull()
const
1568 !size(), NullReferenceError,
1569 typeName(*
this)<<
"::assertNotNull(): "
1570 "Error, the array has size zero!"
1581template<
typename T>
inline
1582bool Teuchos::operator==(
const Array<T> &a1,
const Array<T> &a2 )
1583{
return (a1.vec() == a2.vec()); }
1586template<
typename T>
inline
1587bool Teuchos::operator!=(
const Array<T> &a1,
const Array<T> &a2 )
1588{
return (a1.vec() != a2.vec()); }
1591template<
typename T>
inline
1592void Teuchos::swap( Array<T> &a1, Array<T> &a2 )
1596template<
typename T>
inline
1598{
return (a1.vec() < a2.vec()); }
1601template<
typename T>
inline
1603{
return (a1.vec() <= a2.vec()); }
1606template<
typename T>
inline
1607bool Teuchos::operator>(
const Array<T> &a1,
const Array<T> &a2 )
1608{
return (a1.vec() > a2.vec()); }
1611template<
typename T>
inline
1612bool Teuchos::operator>=(
const Array<T> &a1,
const Array<T> &a2 )
1613{
return (a1.vec() >= a2.vec()); }
1616template<
typename T>
inline
1618 std::ostream& os,
const Array<T>& array
1621 return os << Teuchos::toString(array);
1625template<
typename T>
inline
1626int Teuchos::hashCode(
const Array<T>& array)
1628 int rtn = hashCode(array.length());
1629 for (
int i=0; i<array.length(); i++)
1631 rtn += hashCode(array[i]);
1638 size_t maxIntBeforeWrap = std::numeric_limits<int>::max();
1639 maxIntBeforeWrap ++;
1640 rtn += maxIntBeforeWrap;
1646template<
typename T>
inline
1647std::vector<T> Teuchos::createVector(
const Array<T> &a )
1649 return a.toVector();
1653template<
typename T>
inline
1654std::string Teuchos::toString(
const Array<T>& array)
1656 return array.toString();
1662Teuchos::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!"
Teuchos header file which uses auto-configuration information to include necessary C++ headers.
Defines basic traits returning the name of a type in a portable and readable way.
A utilities class for Teuchos.
ArrayRCP< T > arcpFromArray(Array< T > &a)
Wrap an Array<T> object as a non-owning ArrayRCP<T> object.
ArrayRCP< T > arcp(const RCP< Array< T > > &v)
Wrap an RCP<Array<T> > object as an ArrayRCP<T> object.
ArrayRCP< const T > arcpFromArray(const Array< T > &a)
Wrap a const 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.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes.
const_reference back() const
void reserve(size_type n)
iterator insert(iterator position, const value_type &x)
Array(const ArrayView< const T > &a)
Create an Array which is a deep copy of the given ArrayView.
const_iterator end() const
Array(const std::vector< T > &v)
Copy constructor from an std::vector (does a deep copy).
friend void swap(Array< T2 > &a1, Array< T2 > &a2)
T * getRawPtr()
Return a raw pointer to beginning of array or NULL if unsized.
Ordinal difference_type
The type of the difference between two size_type values.
int length() const
Return number of elements in the array.
const_iterator begin() const
ArrayView< const T > operator()() const
Return an const ArrayView of *this.
Array(const Array< T > &x)
Copy constructor (does a deep copy).
Array & operator=(const Array< T > &a)
Assignment operator (does a deep copy).
bool operator<(const Array< T > &a1, const Array< T > &a2)
Less-than operator.
Array()
Default constructor; creates an empty Array.
void assign(InputIterator first, InputIterator last)
void insert(iterator position, size_type n, const value_type &x)
std::string toString(const Array< T > &array)
Convert an array to a string representation.
friend bool Teuchos::operator!=(const Array< T2 > &a1, const Array< T2 > &a2)
std::vector< T >::iterator iterator
The type of a forward iterator.
std::vector< T >::allocator_type allocator_type
The allocator type; for compatibility with std::vector.
std::vector< T >::const_pointer const_pointer
The type of a const pointer to T; for compatibility with std::vector.
std::vector< T >::const_reference const_reference
The type of a const reference to T; for compatibility with std::vector.
const_reverse_iterator rend() const
std::ostream & operator<<(std::ostream &os, const Array< T > &array)
Write an Array to an ostream.
Ordinal size_type
The type of Array sizes and capacities.
std::vector< T >::reverse_iterator reverse_iterator
The type of a reverse iterator.
iterator erase(iterator first, iterator last)
void assign(size_type n, const value_type &val)
std::vector< T >::reference reference
The type of a reference to T; for compatibility with std::vector.
reference at(size_type i)
ArrayView< const T > view(size_type offset, size_type size) const
Return const view of a contiguous range of elements.
size_type capacity() const
const_reference at(size_type i) const
bool operator>=(const Array< T > &a1, const Array< T > &a2)
Greater-than-or-equal operator.
Array & operator=(const std::vector< T > &v)
Assignment operator for std::vector.
Array(size_type n, const value_type &value=value_type())
Create an array of length n, and fill it with the given value.
Array< T > fromStringToArray(const std::string &arrayStr)
Converts from std::string representation (as created by toString()) back into the array object.
ArrayView< T > view(size_type offset, size_type size)
Return non-const view of a contiguous range of elements.
void remove(int i)
Remove the i-th element from the array, with optional boundschecking.
ArrayView< const T > operator()(size_type offset, size_type size) const
Return a const view of a contiguous range of elements (calls view(offset,size)).
const_reference operator[](size_type i) const
void push_back(const value_type &x)
Array(std::initializer_list< T > list)
Create an array with braced initialization.
const_reference front() const
std::vector< T >::const_reverse_iterator const_reverse_iterator
The type of a const reverse iterator.
void extractDataFromISS(std::istringstream &iss, T &data)
Extracts data from an istringstream object.
const_reverse_iterator rbegin() const
bool operator>(const Array< T > &a1, const Array< T > &a2)
Greater-than operator.
ArrayView< T > operator()(size_type offset, size_type size)
Return a non-const view of a contiguous range of elements (calls view(offset,size)).
static bool hasBoundsChecking()
Return true if Array has been compiled with boundschecking on.
int hashCode(const Array< T > &array)
Return the hash code.
std::vector< T > toVector() const
Explicit copy conversion to an std::vector.
const T * data() const
Return a const raw pointer to beginning of array.
std::string toString() const
Convert an Array to an std::string
bool operator!=(const Array< T > &a1, const Array< T > &a2)
Non-equality operator.
void insert(iterator position, InputIterator first, InputIterator last)
bool operator<=(const Array< T > &a1, const Array< T > &a2)
Less-than-or-equal operator.
std::vector< T > createVector(const Array< T > &a)
Copy conversion to an std::vector.
const T * getRawPtr() const
Return a const raw pointer to beginning of array or NULL if unsized.
reverse_iterator rbegin()
bool operator==(const Array< T > &a1, const Array< T > &a2)
Equality operator.
size_type max_size() const
T * data()
Return a raw pointer to beginning of array.
ArrayView< T > operator()()
Return an non-const ArrayView of *this.
Array(InputIterator first, InputIterator last)
Create an array, and fill it with values from the given iterator range.
std::vector< T >::pointer pointer
The type of a pointer to T; for compatibility with std::vector.
void resize(size_type new_size, const value_type &x=value_type())
std::vector< T >::value_type value_type
The type of an entry of the Array; for compatibility with std::vector.
Teuchos_Ordinal Ordinal
The type of indices.
std::vector< T >::const_iterator const_iterator
The type of a const forward iterator.
reference operator[](size_type i)
void extractDataFromISS(std::istringstream &iss, std::string &data)
Extracts std::string data from an istringstream object.
std::string getArrayTypeNameTraitsFormat()
Get the format that is used for the specialization of the TypeName traits class for Array.
Array(const Tuple< T, N > &t)
Copy constructor from the given Tuple.
Array< T > & append(const T &x)
Add a new entry at the end of the array.
void swap(Array< T > &a1, Array< T > &a2)
Non-member swap (specializes default std version).
iterator erase(iterator position)
Smart reference counting pointer class for automatic garbage collection.
RCP< T > create_weak() const
Create a new weak RCP object from another (strong) RCP object.
void swap(RCP< T > &r_ptr)
Swap the contents with some other RCP object.
bool is_null() const
Returns true if the underlying pointer is null.
T * getRawPtr() const
Get the raw C++ pointer to the underlying object.
Default traits class that just returns typeid(T).name().
static std::string trimWhiteSpace(const std::string &str)
Trim whitespace from beginning and end of std::string.
#define TEUCHOS_ASSERT(assertion_test)
This macro is throws when an assert fails.
#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.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
std::string typeName(const T &t)
Template function for returning the concrete type name of a passed-in object.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.