42#ifndef TEUCHOS_ARRAY_H
43#define TEUCHOS_ARRAY_H
50#include "Teuchos_Assert.hpp"
52#include "Teuchos_ArrayRCP.hpp"
53#include "Teuchos_Tuple.hpp"
55#include "Teuchos_Assert.hpp"
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
72template<
typename T>
class Array;
88template<
typename T>
inline
96template<
typename T>
inline
104template<
typename T>
inline
112template<
typename T>
inline
120template<
typename T>
inline
128template<
typename T>
inline
136template<
typename T>
inline
204 template<
typename T2>
208 template<
typename T2>
212 template<
typename T2>
216 template<
typename T2>
220 template<
typename T2>
224 template<
typename T2>
228 template<
typename T2>
243 typedef typename std::vector<T>::pointer
pointer;
253#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
264 typedef typename std::vector<T>::iterator
iterator;
287 template<
typename InputIterator>
314 template<
typename InputIterator>
369 template<
typename InputIterator>
507#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
511#ifdef USE_MUTEX_LOCK_FOR_ARRAY
518 inline std::vector<T>& vec(
523 inline const std::vector<T>& vec()
const;
525 inline typename std::vector<T>::iterator
530 inline void assertNotNull()
const;
546 &(*v)[0], 0,
v->size(),
563 &(*v)[0], 0,
v->size(),
625template<
typename T>
inline
635template<
typename T>
inline
717template<
typename T>
inline
772 static std::string name(){
773 std::string
formatString = getArrayTypeNameTraitsFormat();
779 static std::string concreteName(
const Array<T>&)
798template<
typename T>
inline
800#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
801 : vec_(
rcp(
new std::vector<T>()))
806template<
typename T>
inline
816template<
typename T>
inline
826template<
typename T>
template<
typename InputIterator>
inline
836template<
typename T>
inline
841template<
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());
863template<
typename T>
inline
866#ifdef USE_MUTEX_LOCK_FOR_ARRAY
877template<
typename T>
inline
880#ifdef USE_MUTEX_LOCK_FOR_ARRAY
883 vec(
true).assign(n,
val);
887template<
typename T>
template<
typename InputIterator>
inline
890#ifdef USE_MUTEX_LOCK_FOR_ARRAY
893 vec(
true).assign(first,
last);
897template<
typename T>
inline
901#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
903#ifdef USE_MUTEX_LOCK_FOR_ARRAY
916 return vec().begin();
921template<
typename T>
inline
925#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
926 return begin() + size();
932template<
typename T>
inline
936#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
938#ifdef USE_MUTEX_LOCK_FOR_ARRAY
960 return vec().begin();
965template<
typename T>
inline
969#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
970 return begin() + size();
977template<
typename T>
inline
981#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
984 return vec().rbegin();
989template<
typename T>
inline
993#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1001template<
typename T>
inline
1005#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1008 return vec().rbegin();
1013template<
typename T>
inline
1017#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1020 return vec().rend();
1025template<
typename T>
inline
1029 return vec().size();
1033template<
typename T>
inline
1037 return std::numeric_limits<size_type>::max();
1041template<
typename T>
inline
1045#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1052template<
typename T>
inline
1056 return vec().capacity();
1060template<
typename T>
inline
1063 return vec().empty();
1067template<
typename T>
inline
1070#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1073 vec(
true).reserve(n);
1077template<
typename T>
inline
1081#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1088template<
typename T>
inline
1092#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1099template<
typename T>
inline
1103#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1110template<
typename T>
inline
1114#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1121template<
typename T>
inline
1125#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1128 return vec().front();
1132template<
typename T>
inline
1136#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1139 return vec().front();
1143template<
typename T>
inline
1147#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1150 return vec().back();
1154template<
typename T>
inline
1158#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1161 return vec().back();
1165template<
typename T>
inline
1168#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1171 vec(
true).push_back(x);
1175template<
typename T>
inline
1178#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1181#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1184 vec(
true).pop_back();
1200template<
typename T>
inline
1204#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1208#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1212 vec(
true,
true).insert(
raw_poss, x);
1213#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1223template<
typename T>
inline
1226#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1228#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1231 vec(
true,
true).insert(
raw_poss, n, x);
1238template<
typename T>
template<
typename InputIterator>
inline
1241#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1243#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1253template<
typename T>
inline
1257#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1262#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1267#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1277template<
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
1297#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1302 return vec_.erase(first,
last);
1307template<
typename T>
inline
1310#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1313 vec(
true).swap(x.vec());
1317template<
typename T>
inline
1320#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1330template<
typename T>
inline
1338template<
typename T>
inline
1341#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1345 this->erase( this->begin() +
i );
1349template<
typename T>
inline
1352 return static_cast<int> (this->size ());
1356template<
typename T>
inline
1359 return (*
this)().toString();
1363template<
typename T>
inline
1366#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1374template<
typename T>
inline
1377 return ( size() ? &(*
this)[0] :
nullptr );
1380template<
typename T>
inline
1383 return ( size() ? &(*
this)[0] :
nullptr );
1386template<
typename T>
inline
1389 return ( size() ? &(*
this)[0] :
nullptr );
1392template<
typename T>
inline
1395 return ( size() ? &(*
this)[0] :
nullptr );
1401template<
typename T>
inline
1411template<
typename T>
inline
1415 return std::vector<T>();
1416 std::vector<T>
v(begin(),end());
1421template<
typename T>
inline
1424#ifdef USE_MUTEX_LOCK_FOR_ARRAY
1435template<
typename T>
inline
1439#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1445 return Teuchos::null;
1449template<
typename T>
inline
1453#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1459 return Teuchos::null;
1466template<
typename T>
inline
1473template<
typename T>
inline
1480template<
typename T>
inline
1485 return this->view(0, size());
1489template<
typename T>
inline
1494 return this->view(0, size());
1498template<
typename T>
inline
1501 return this->operator()();
1505template<
typename T>
inline
1508 return this->operator()();
1519#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1542template<
typename T>
inline
1543const std::vector<T>&
1544Array<T>::vec()
const
1546#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1554template<
typename T>
inline
1555typename std::vector<T>::iterator
1556Array<T>::raw_position( iterator position )
1558#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1559 const iterator first = this->begin();
1560 const iterator
last = this->end();
1563 "Error, this iterator is no longer valid for this Aray!"
1568 return vec_->begin() + (
position - this->begin());
1575template<
typename T>
inline
1576void Array<T>::assertIndex(size_type i)
const
1579 !( 0 <=
i &&
i < size() ), RangeError,
1580 "Array<T>::assertIndex(i): i="<<
i<<
" out of range [0, "<< size() <<
")"
1585template<
typename T>
inline
1586void Array<T>::assertNotNull()
const
1589 !size(), NullReferenceError,
1590 typeName(*
this)<<
"::assertNotNull(): "
1591 "Error, the array has size zero!"
1602template<
typename T>
inline
1603bool Teuchos::operator==(
const Array<T> &a1,
const Array<T> &a2 )
1604{
return (a1.vec() == a2.vec()); }
1607template<
typename T>
inline
1608bool Teuchos::operator!=(
const Array<T> &a1,
const Array<T> &a2 )
1609{
return (a1.vec() != a2.vec()); }
1612template<
typename T>
inline
1613void Teuchos::swap( Array<T> &a1, Array<T> &a2 )
1617template<
typename T>
inline
1619{
return (a1.vec() < a2.vec()); }
1622template<
typename T>
inline
1624{
return (a1.vec() <= a2.vec()); }
1627template<
typename T>
inline
1628bool Teuchos::operator>(
const Array<T> &a1,
const Array<T> &a2 )
1629{
return (a1.vec() > a2.vec()); }
1632template<
typename T>
inline
1633bool Teuchos::operator>=(
const Array<T> &a1,
const Array<T> &a2 )
1634{
return (a1.vec() >= a2.vec()); }
1637template<
typename T>
inline
1639 std::ostream& os,
const Array<T>& array
1642 return os << Teuchos::toString(array);
1646template<
typename T>
inline
1647int Teuchos::hashCode(
const Array<T>& array)
1649 int rtn = hashCode(array.length());
1650 for (
int i=0; i<array.length(); i++)
1652 rtn += hashCode(array[i]);
1659 size_t maxIntBeforeWrap = std::numeric_limits<int>::max();
1660 maxIntBeforeWrap ++;
1661 rtn += maxIntBeforeWrap;
1667template<
typename T>
inline
1668std::vector<T> Teuchos::createVector(
const Array<T> &a )
1670 return a.toVector();
1674template<
typename T>
inline
1675std::string Teuchos::toString(
const Array<T>& array)
1677 return array.toString();
1683Teuchos::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!"
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)
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.
RCP(ENull null_arg=null)
Initialize RCP<T> to NULL.
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.