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;
278 inline explicit Array(size_type n,
const value_type& value = value_type());
284 template<
typename InputIterator>
285 inline Array(InputIterator first, InputIterator last);
309 inline void assign(size_type n,
const value_type& val);
311 template<
typename InputIterator>
312 inline void assign(InputIterator first, InputIterator last);
314 inline iterator begin();
316 inline iterator end();
318 inline const_iterator begin()
const;
320 inline const_iterator end()
const;
322 inline reverse_iterator rbegin();
324 inline reverse_iterator rend();
326 inline const_reverse_iterator rbegin()
const;
328 inline const_reverse_iterator rend()
const;
330 inline size_type size()
const;
332 inline size_type max_size()
const;
334 inline void resize(size_type new_size,
const value_type& x = value_type());
336 inline size_type capacity()
const;
338 inline bool empty()
const;
340 inline void reserve(size_type n);
342 inline reference operator[](size_type i);
344 inline const_reference operator[](size_type i)
const;
346 inline reference at(size_type i);
348 inline const_reference at(size_type i)
const;
350 inline reference front();
352 inline const_reference front()
const;
354 inline reference back();
356 inline const_reference back()
const;
358 inline void push_back(
const value_type& x);
360 inline void pop_back();
362 inline iterator insert(iterator position,
const value_type& x);
364 inline void insert(iterator position, size_type n,
const value_type& x);
366 template<
typename InputIterator>
367 inline void insert(iterator position, InputIterator first, InputIterator last);
369 inline iterator erase(iterator position);
371 inline iterator erase(iterator first, iterator last);
373 inline void swap(
Array& x);
385 inline Array<T>& append(
const T& x);
390 inline void remove(
int i);
396 inline int length()
const;
399 inline std::string toString()
const;
402 inline static bool hasBoundsChecking();
405 inline T* getRawPtr();
408 inline const T* getRawPtr()
const;
415 inline Array(
const std::vector<T> &v );
418 inline std::vector<T> toVector()
const;
421 inline Array& operator=(
const std::vector<T> &v );
441 inline ArrayView<T> view( size_type offset, size_type size );
461 inline ArrayView<T> operator()( size_type offset, size_type size );
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
510 raw_position( iterator position );
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 936 return reverse_iterator(end());
938 return vec().rbegin();
943 template<
typename T>
inline 947 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 948 return reverse_iterator(begin());
955 template<
typename T>
inline 959 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 960 return const_reverse_iterator(end());
962 return vec().rbegin();
967 template<
typename T>
inline 971 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 972 return const_reverse_iterator(begin());
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);
1149 const difference_type i = position - begin();
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);
1190 const difference_type i = position - begin();
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);
1213 const difference_type i = first - begin();
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 1268 return (*
this)().toString();
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>&
1438 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 1446 template<
typename T>
inline 1447 typename std::vector<T>::iterator
1450 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 1451 const iterator first = this->begin();
1452 const iterator last = this->end();
1455 "Error, this iterator is no longer valid for this Aray!" 1460 return vec_->begin() + (position - this->begin());
1467 template<
typename T>
inline 1472 "Array<T>::assertIndex(i): i="<<i<<
" out of range [0, "<< size() <<
")" 1477 template<
typename T>
inline 1482 typeName(*
this)<<
"::assertNotNull(): " 1483 "Error, the array has size zero!" 1494 template<
typename T>
inline 1496 {
return (a1.vec() == a2.vec()); }
1499 template<
typename T>
inline 1501 {
return (a1.vec() != a2.vec()); }
1504 template<
typename T>
inline 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 1521 {
return (a1.vec() > a2.vec()); }
1524 template<
typename T>
inline 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++)
1544 rtn += hashCode(array[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 )
1566 template<
typename T>
inline 1567 std::string Teuchos::toString(
const Array<T>& array)
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] !=
'}' )
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(),
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()
1638 ,
"Error, the std::string:\n" 1642 "is not a valid array represntation!" 1649 #endif // TEUCHOS_ARRAY_H Dangling reference error exception class.
Null reference error exception class.
void reserve(size_type n)
bool is_null(const boost::shared_ptr< T > &p)
Returns true if p.get()==NULL.
Partial specialization of ArrayRCP for const T.
std::vector< T > toVector() const
Explicit copy conversion to an std::vector.
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...
ArrayRCP< T > arcp(const RCP< Array< T > > &v)
Wrap an RCP<Array<T> > object as an ArrayRCP<T> object.
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.
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())
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< Teuchos::XMLObject >::const_iterator const_iterator
The type of a const forward iterator.
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< Teuchos::XMLObject >::const_reverse_iterator const_reverse_iterator
The type of a const reverse iterator.
std::string toString() const
Convert an Array to an std::string
std::vector< T >::allocator_type allocator_type
The allocator type; for compatibility with std::vector.
Smart reference counting pointer class for automatic garbage collection.
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.
iterator end() const
Return an iterator to past the end of the array of data.
Array()
Default constructor; creates an empty Array.
std::vector< Teuchos::XMLObject >::iterator iterator
The type of a forward iterator.
A utilities class for Teuchos.
Defines basic traits returning the name of a type in a portable and readable way. ...
std::vector< Teuchos::XMLObject >::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...