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" 69 template<
typename T>
class Array;
85 template<
typename T>
inline 93 template<
typename T>
inline 101 template<
typename T>
inline 109 template<
typename T>
inline 110 bool operator<( const Array<T> &a1,
const Array<T> &a2 );
117 template<
typename T>
inline 118 bool operator<=( const Array<T> &a1,
const Array<T> &a2 );
125 template<
typename T>
inline 133 template<
typename T>
inline 201 template<
typename T2>
205 template<
typename T2>
209 template<
typename T2>
213 template<
typename T2>
214 friend bool Teuchos::operator<( const Array<T2> &a1,
const Array<T2> &a2 );
217 template<
typename T2>
218 friend bool Teuchos::operator<=( const Array<T2> &a1,
const Array<T2> &a2 );
221 template<
typename T2>
225 template<
typename T2>
240 typedef typename std::vector<T>::pointer
pointer;
250 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 260 typedef typename std::vector<T>::iterator
iterator;
284 template<
typename InputIterator>
285 inline Array(InputIterator first, InputIterator last);
311 template<
typename InputIterator>
312 inline void assign(InputIterator first, InputIterator last);
338 inline bool empty()
const;
366 template<
typename InputIterator>
367 inline void insert(
iterator position, InputIterator first, InputIterator last);
390 inline void remove(
int i);
396 inline int length()
const;
399 inline std::string
toString()
const;
415 inline Array(
const std::vector<T> &v );
418 inline std::vector<T>
toVector()
const;
494 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 502 inline std::vector<T>& vec(
503 bool isStructureBeingModified =
false,
504 bool activeIter =
false 507 inline const std::vector<T>& vec()
const;
509 inline typename std::vector<T>::iterator
512 inline void assertIndex(
size_type i)
const;
514 inline void assertNotNull()
const;
527 if (
is_null(v) || !v->size() )
529 return arcpWithEmbeddedObjPostDestroy<T,RCP<Array<T> > >(
530 &(*v)[0], 0, v->size(),
544 if (
is_null(v) || !v->size() )
546 return arcpWithEmbeddedObjPostDestroy<const T,RCP<const Array<T> > >(
547 &(*v)[0], 0, v->size(),
602 std::ostream& operator<<(std::ostream& os, const Array<T>& array);
609 template<
typename T>
inline 610 int hashCode(
const Array<T>& array);
619 template<
typename T>
inline 620 std::vector<T> createVector(
const Array<T> &a );
628 std::string toString(
const Array<T>& array);
683 Array<T> fromStringToArray(
const std::string& arrayStr);
691 std::istringstream& operator>> (std::istringstream& in,
Array<T>& array){
692 array = fromStringToArray<T>(in.str());
701 template<
typename T>
inline 756 static std::string name(){
757 std::string formatString = getArrayTypeNameTraitsFormat();
758 size_t starPos = formatString.find(
"*");
759 std::string prefix = formatString.substr(0,starPos);
760 std::string postFix = formatString.substr(starPos+1);
763 static std::string concreteName(
const Array<T>&)
782 template<
typename T>
inline 784 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 785 : vec_(
rcp(
new std::vector<T>()))
790 template<
typename T>
inline 792 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
793 vec_(
rcp(new std::vector<T>(n,value)))
800 template<
typename T>
inline 802 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
803 vec_(
rcp(new std::vector<T>(*x.vec_)))
810 template<
typename T>
template<
typename InputIterator>
inline 812 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
813 vec_(
rcp(new std::vector<T>(first, last)))
820 template<
typename T>
inline 825 template<
typename T>
inline 827 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 828 : vec_(
rcp(
new std::vector<T>()))
831 insert(begin(), a.begin(), a.end());
839 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 840 : vec_(
rcp(
new std::vector<T>()))
843 insert(begin(), t.
begin(), t.
end());
847 template<
typename T>
inline 858 template<
typename T>
inline 865 template<
typename T>
template<
typename InputIterator>
inline 868 vec(
true).
assign(first,last);
872 template<
typename T>
inline 876 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 879 extern_arcp_ =
arcp(vec_);
883 return extern_arcp_.create_weak();
885 return vec().begin();
890 template<
typename T>
inline 894 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 895 return begin() +
size();
902 template<
typename T>
inline 906 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 908 extern_carcp_ =
const_cast<Array<T>*
>(
this)->begin();
912 return extern_carcp_.create_weak();
914 return vec().begin();
919 template<
typename T>
inline 923 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 924 return begin() +
size();
931 template<
typename T>
inline 935 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 938 return vec().rbegin();
943 template<
typename T>
inline 947 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 955 template<
typename T>
inline 959 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 962 return vec().rbegin();
967 template<
typename T>
inline 971 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 979 template<
typename T>
inline 987 template<
typename T>
inline 991 return std::numeric_limits<size_type>::max();
995 template<
typename T>
inline 999 vec(
true).
resize(new_size,x);
1003 template<
typename T>
inline 1011 template<
typename T>
inline 1014 return vec().
empty();
1018 template<
typename T>
inline 1025 template<
typename T>
inline 1029 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 1036 template<
typename T>
inline 1040 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 1047 template<
typename T>
inline 1051 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 1058 template<
typename T>
inline 1062 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 1069 template<
typename T>
inline 1073 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 1076 return vec().front();
1080 template<
typename T>
inline 1084 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 1087 return vec().front();
1091 template<
typename T>
inline 1095 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 1098 return vec().back();
1102 template<
typename T>
inline 1106 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 1109 return vec().back();
1113 template<
typename T>
inline 1120 template<
typename T>
inline 1123 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 1126 vec(
true).pop_back();
1142 template<
typename T>
inline 1146 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 1148 const typename std::vector<T>::iterator raw_poss = raw_position(position);
1150 vec(
true,
true).insert(raw_poss, x);
1153 return vec_.insert(position, x);
1158 template<
typename T>
inline 1161 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 1162 const typename std::vector<T>::iterator raw_poss = raw_position(position);
1163 vec(
true,
true).insert(raw_poss, n, x);
1165 vec_.insert(position, n, x);
1170 template<
typename T>
template<
typename InputIterator>
inline 1173 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 1174 const typename std::vector<T>::iterator raw_poss = raw_position(position);
1175 vec(
true,
true).insert(raw_poss, first, last);
1177 vec_.insert(position, first, last);
1182 template<
typename T>
inline 1186 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 1189 const typename std::vector<T>::iterator raw_poss = raw_position(position);
1191 vec(
true,
true).erase(raw_poss);
1194 return vec_.erase(position);
1199 template<
typename T>
inline 1203 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 1211 const typename std::vector<T>::iterator raw_first = raw_position(first);
1212 const typename std::vector<T>::iterator raw_last = raw_position(last);
1214 vec(
true,
true).erase(raw_first,raw_last);
1217 return vec_.erase(first,last);
1222 template<
typename T>
inline 1225 vec(
true).
swap(x.vec());
1229 template<
typename T>
inline 1239 template<
typename T>
inline 1247 template<
typename T>
inline 1250 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 1254 this->erase( this->begin() + i );
1258 template<
typename T>
inline 1261 return static_cast<int> (this->
size ());
1265 template<
typename T>
inline 1272 template<
typename T>
inline 1275 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 1283 template<
typename T>
inline 1286 return (
size() ? &(*
this)[0] : 0 );
1290 template<
typename T>
inline 1293 return (
size() ? &(*
this)[0] : 0 );
1300 template<
typename T>
inline 1302 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1303 vec_(new std::vector<T>(v))
1310 template<
typename T>
inline 1314 return std::vector<T>();
1315 std::vector<T> v(begin(),end());
1320 template<
typename T>
inline 1331 template<
typename T>
inline 1335 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 1336 return ArrayView<T>(this->begin().persistingView(offset, size_in));
1338 return arrayView( &vec()[offset], size_in );
1341 return Teuchos::null;
1345 template<
typename T>
inline 1349 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 1352 return arrayView( &vec()[offset], size_in );
1355 return Teuchos::null;
1362 template<
typename T>
inline 1365 return view(offset, size_in);
1369 template<
typename T>
inline 1372 return view(offset, size_in);
1376 template<
typename T>
inline 1381 return this->view(0,
size());
1385 template<
typename T>
inline 1390 return this->view(0,
size());
1394 template<
typename T>
inline 1397 return this->operator()();
1401 template<
typename T>
inline 1404 return this->operator()();
1411 template<
typename T>
1413 Array<T>::vec(
bool isStructureBeingModified,
bool activeIter )
1415 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 1417 if (isStructureBeingModified) {
1421 extern_arcp_ = null;
1422 extern_carcp_ = null;
1427 (void)isStructureBeingModified;
1434 template<
typename T>
inline 1435 const std::vector<T>&
1436 Array<T>::vec()
const 1438 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 1446 template<
typename T>
inline 1447 typename std::vector<T>::iterator
1448 Array<T>::raw_position( iterator position )
1450 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 1451 const iterator first = this->begin();
1452 const iterator last = this->end();
1454 !(first <= position && position <= last), DanglingReferenceError,
1455 "Error, this iterator is no longer valid for this Aray!" 1460 return vec_->begin() + (position - this->begin());
1467 template<
typename T>
inline 1468 void Array<T>::assertIndex(size_type i)
const 1471 !( 0 <= i && i <
size() ), RangeError,
1472 "Array<T>::assertIndex(i): i="<<i<<
" out of range [0, "<<
size() <<
")" 1477 template<
typename T>
inline 1478 void Array<T>::assertNotNull()
const 1481 !
size(), NullReferenceError,
1482 typeName(*
this)<<
"::assertNotNull(): " 1483 "Error, the array has size zero!" 1494 template<
typename T>
inline 1495 bool Teuchos::operator==(
const Array<T> &a1,
const Array<T> &a2 )
1496 {
return (a1.vec() == a2.vec()); }
1499 template<
typename T>
inline 1500 bool Teuchos::operator!=(
const Array<T> &a1,
const Array<T> &a2 )
1501 {
return (a1.vec() != a2.vec()); }
1504 template<
typename T>
inline 1505 void Teuchos::swap( Array<T> &a1, Array<T> &a2 )
1509 template<
typename T>
inline 1510 bool Teuchos::operator<( const Array<T> &a1,
const Array<T> &a2 )
1511 {
return (a1.vec() < a2.vec()); }
1514 template<
typename T>
inline 1515 bool Teuchos::operator<=( const Array<T> &a1,
const Array<T> &a2 )
1516 {
return (a1.vec() <= a2.vec()); }
1519 template<
typename T>
inline 1520 bool Teuchos::operator>(
const Array<T> &a1,
const Array<T> &a2 )
1521 {
return (a1.vec() > a2.vec()); }
1524 template<
typename T>
inline 1525 bool Teuchos::operator>=(
const Array<T> &a1,
const Array<T> &a2 )
1526 {
return (a1.vec() >= a2.vec()); }
1529 template<
typename T>
inline 1530 std::ostream& Teuchos::operator<<(
1531 std::ostream& os,
const Array<T>& array
1534 return os << Teuchos::toString(array);
1538 template<
typename T>
inline 1539 int Teuchos::hashCode(
const Array<T>& array)
1541 int rtn =
hashCode(array.length());
1542 for (
int i=0; i<array.length(); i++)
1551 size_t maxIntBeforeWrap = std::numeric_limits<int>::max();
1552 maxIntBeforeWrap ++;
1553 rtn += maxIntBeforeWrap;
1559 template<
typename T>
inline 1560 std::vector<T> Teuchos::createVector(
const Array<T> &a )
1562 return a.toVector();
1566 template<
typename T>
inline 1567 std::string Teuchos::toString(
const Array<T>& array)
1569 return array.toString();
1573 template<
typename T>
1575 Teuchos::fromStringToArray(
const std::string& arrayStr)
1577 const std::string str = Utils::trimWhiteSpace(arrayStr);
1578 std::istringstream iss(str);
1580 ( str[0]!=
'{' || str[str.length()-1] !=
'}' )
1581 ,InvalidArrayStringRepresentation
1582 ,
"Error, the std::string:\n" 1586 "is not a valid array represntation!" 1593 while( !iss.eof() ) {
1595 std::string entryStr;
1596 std::getline(iss,entryStr,
',');
1605 entryStr = Utils::trimWhiteSpace(entryStr);
1607 0 == entryStr.length(),
1608 InvalidArrayStringRepresentation,
1609 "Error, the std::string:\n" 1613 "is not a valid array represntation because it has an empty array entry!" 1617 bool found_end =
false;
1618 if(entryStr[entryStr.length()-1]==
'}') {
1619 entryStr = entryStr.substr(0,entryStr.length()-1);
1621 if( entryStr.length()==0 && a.size()==0 )
1625 std::istringstream entryiss(entryStr);
1627 Teuchos::extractDataFromISS( entryiss, entry );
1636 found_end && !iss.eof()
1637 ,InvalidArrayStringRepresentation
1638 ,
"Error, the std::string:\n" 1642 "is not a valid array represntation!" 1649 #endif // TEUCHOS_ARRAY_H
RCP< T > rcp(const boost::shared_ptr< T > &sptr)
Conversion function that takes in a boost::shared_ptr object and spits out a Teuchos::RCP object...
ArrayView< T > arrayView(T *p, typename ArrayView< T >::size_type size)
Construct a const or non-const view to const or non-const data.
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.
Partial specialization of ArrayRCP for const T.
static bool hasBoundsChecking()
Return true if Array has been compiled with boundschecking on.
std::vector< T > toVector() const
Explicit copy conversion to an std::vector.
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.
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.
bool is_null(const ArrayRCP< T > &p)
Returns true if p.get()==NULL.
ArrayRCP< const T > arcp(const RCP< const Array< T > > &v)
Wrap a RCP<const Array<T> > object as an ArrayRCP<const T> object.
std::string toString(const any &rhs)
Converts the value in any to a std::string.
iterator begin() const
Return an iterator to beginning of the array of data.
static std::string trimWhiteSpace(const std::string &str)
Trim whitespace from beginning and end of std::string.
Statically sized simple array (tuple) class.
Ordinal size_type
The type of Array sizes and capacities.
int length() const
Return number of elements in the array.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Ordinal difference_type
The type of the difference between two size_type values.
ArrayRCP< const T > arcpFromArray(const Array< T > &a)
Wrap a const Array<T> object as a non-owning ArrayRCP<T> object.
size_type capacity() const
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())
int size(const Comm< Ordinal > &comm)
Get the number of processes in the communicator.
friend bool Teuchos::operator>(const Array< T2 > &a1, const Array< T2 > &a2)
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.
size_type max_size() const
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.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
std::vector< T >::const_reverse_iterator const_reverse_iterator
The type of a const reverse iterator.
std::string toString() const
Convert an Array to an std::string
friend bool Teuchos::operator>=(const Array< T2 > &a1, const Array< T2 > &a2)
std::vector< T >::allocator_type allocator_type
The allocator type; for compatibility with 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.
friend bool Teuchos::operator!=(const Array< T2 > &a1, const Array< T2 > &a2)
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)
iterator end() const
Return an iterator to past the end of the array of data.
Array()
Default constructor; creates an empty Array.
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.
#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()
std::string typeName(const T &t)
Template function for returning the concrete type name of a passed-in object.
Reference-counted smart pointer for managing arrays.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes...