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
185 template<
typename T2>
189 template<
typename T2>
193 template<
typename T2>
197 template<
typename T2>
198 friend bool Teuchos::operator<( const Array<T2> &a1,
const Array<T2> &a2 );
201 template<
typename T2>
202 friend bool Teuchos::operator<=( const Array<T2> &a1,
const Array<T2> &a2 );
205 template<
typename T2>
209 template<
typename T2>
224 typedef typename std::vector<T>::pointer
pointer;
234 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
245 typedef typename std::vector<T>::iterator
iterator;
267 template<
typename InputIterator>
268 inline Array(InputIterator first, InputIterator last);
287 template<
typename InputIterator>
288 inline void assign(InputIterator first, InputIterator last);
314 inline bool empty()
const;
342 template<
typename InputIterator>
343 inline void insert(
iterator position, InputIterator first, InputIterator last);
367 inline void remove(
int i);
373 inline int length()
const;
376 inline std::string
toString()
const;
393 inline Array(
const std::vector<T> &v );
396 inline std::vector<T>
toVector()
const;
472 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
480 inline std::vector<T>& vec(
481 bool isStructureBeingModified =
false,
482 bool activeIter =
false
485 inline const std::vector<T>& vec()
const;
487 inline typename std::vector<T>::iterator
490 inline void assertIndex(
size_type i)
const;
492 inline void assertNotNull()
const;
505 if (
is_null(v) || !v->size() )
507 return arcpWithEmbeddedObjPostDestroy<T,RCP<Array<T> > >(
508 &(*v)[0], 0, v->size(),
522 if (
is_null(v) || !v->size() )
524 return arcpWithEmbeddedObjPostDestroy<const T,RCP<const Array<T> > >(
525 &(*v)[0], 0, v->size(),
580 std::ostream& operator<<(std::ostream& os, const Array<T>& array);
587 template<
typename T>
inline
588 int hashCode(
const Array<T>& array);
597 template<
typename T>
inline
598 std::vector<T> createVector(
const Array<T> &a );
606 std::string toString(
const Array<T>& array);
661 Array<T> fromStringToArray(
const std::string& arrayStr);
669 std::istringstream& operator>> (std::istringstream& in,
Array<T>& array){
670 array = fromStringToArray<T>(in.str());
679 template<
typename T>
inline
734 static std::string name(){
735 std::string formatString = getArrayTypeNameTraitsFormat();
736 size_t starPos = formatString.find(
"*");
737 std::string prefix = formatString.substr(0,starPos);
738 std::string postFix = formatString.substr(starPos+1);
741 static std::string concreteName(
const Array<T>&)
760 template<
typename T>
inline
762 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
763 : vec_(
rcp(
new std::vector<T>()))
768 template<
typename T>
inline
770 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
771 vec_(
rcp(new std::vector<T>(n,value)))
778 template<
typename T>
inline
780 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
781 vec_(
rcp(new std::vector<T>(*x.vec_)))
788 template<
typename T>
template<
typename InputIterator>
inline
790 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
791 vec_(
rcp(new std::vector<T>(first, last)))
798 template<
typename T>
inline
803 template<
typename T>
inline
805 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
806 : vec_(
rcp(
new std::vector<T>()))
809 insert(begin(), a.begin(), a.end());
817 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
818 : vec_(
rcp(
new std::vector<T>()))
821 insert(begin(), t.
begin(), t.
end());
825 template<
typename T>
inline
836 template<
typename T>
inline
839 vec(
true).assign(n,val);
843 template<
typename T>
template<
typename InputIterator>
inline
846 vec(
true).assign(first,last);
850 template<
typename T>
inline
854 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
857 extern_arcp_ = arcp(vec_);
861 return extern_arcp_.create_weak();
863 return vec().begin();
868 template<
typename T>
inline
872 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
873 return begin() + size();
880 template<
typename T>
inline
884 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
886 extern_carcp_ =
const_cast<Array<T>*
>(
this)->begin();
890 return extern_carcp_.create_weak();
892 return vec().begin();
897 template<
typename T>
inline
901 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
902 return begin() + size();
909 template<
typename T>
inline
913 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
916 return vec().rbegin();
921 template<
typename T>
inline
925 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
933 template<
typename T>
inline
937 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
940 return vec().rbegin();
945 template<
typename T>
inline
949 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
957 template<
typename T>
inline
965 template<
typename T>
inline
969 return std::numeric_limits<size_type>::max();
973 template<
typename T>
inline
977 vec(
true).resize(new_size,x);
981 template<
typename T>
inline
985 return vec().capacity();
989 template<
typename T>
inline
992 return vec().empty();
996 template<
typename T>
inline
999 vec(
true).reserve(n);
1003 template<
typename T>
inline
1007 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1014 template<
typename T>
inline
1018 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
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
1054 return vec().front();
1058 template<
typename T>
inline
1062 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1065 return vec().front();
1069 template<
typename T>
inline
1073 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1076 return vec().back();
1080 template<
typename T>
inline
1084 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1087 return vec().back();
1091 template<
typename T>
inline
1094 vec(
true).push_back(x);
1098 template<
typename T>
inline
1101 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1104 vec(
true).pop_back();
1120 template<
typename T>
inline
1124 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1126 const typename std::vector<T>::iterator raw_poss = raw_position(position);
1128 vec(
true,
true).insert(raw_poss, x);
1131 return vec_.insert(position, x);
1136 template<
typename T>
inline
1139 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1140 const typename std::vector<T>::iterator raw_poss = raw_position(position);
1141 vec(
true,
true).insert(raw_poss, n, x);
1143 vec_.insert(position, n, x);
1148 template<
typename T>
template<
typename InputIterator>
inline
1151 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1152 const typename std::vector<T>::iterator raw_poss = raw_position(position);
1153 vec(
true,
true).insert(raw_poss, first, last);
1155 vec_.insert(position, first, last);
1160 template<
typename T>
inline
1164 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1167 const typename std::vector<T>::iterator raw_poss = raw_position(position);
1169 vec(
true,
true).erase(raw_poss);
1172 return vec_.erase(position);
1177 template<
typename T>
inline
1181 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1189 const typename std::vector<T>::iterator raw_first = raw_position(first);
1190 const typename std::vector<T>::iterator raw_last = raw_position(last);
1192 vec(
true,
true).erase(raw_first,raw_last);
1195 return vec_.erase(first,last);
1200 template<
typename T>
inline
1203 vec(
true).swap(x.vec());
1207 template<
typename T>
inline
1217 template<
typename T>
inline
1225 template<
typename T>
inline
1228 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1232 this->erase( this->begin() + i );
1236 template<
typename T>
inline
1239 return static_cast<int> (this->size ());
1243 template<
typename T>
inline
1246 return (*
this)().toString();
1250 template<
typename T>
inline
1253 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1261 template<
typename T>
inline
1264 return ( size() ? &(*
this)[0] : 0 );
1268 template<
typename T>
inline
1271 return ( size() ? &(*
this)[0] : 0 );
1278 template<
typename T>
inline
1280 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1281 vec_(new std::vector<T>(v))
1288 template<
typename T>
inline
1292 return std::vector<T>();
1293 std::vector<T> v(begin(),end());
1298 template<
typename T>
inline
1309 template<
typename T>
inline
1313 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1314 return ArrayView<T>(this->begin().persistingView(offset, size_in));
1316 return arrayView( &vec()[offset], size_in );
1319 return Teuchos::null;
1323 template<
typename T>
inline
1327 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1330 return arrayView( &vec()[offset], size_in );
1333 return Teuchos::null;
1340 template<
typename T>
inline
1343 return view(offset, size_in);
1347 template<
typename T>
inline
1350 return view(offset, size_in);
1354 template<
typename T>
inline
1359 return this->view(0, size());
1363 template<
typename T>
inline
1368 return this->view(0, size());
1372 template<
typename T>
inline
1375 return this->operator()();
1379 template<
typename T>
inline
1382 return this->operator()();
1389 template<
typename T>
1391 Array<T>::vec(
bool isStructureBeingModified,
bool activeIter )
1393 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1395 if (isStructureBeingModified) {
1399 extern_arcp_ = null;
1400 extern_carcp_ = null;
1405 (void)isStructureBeingModified;
1412 template<
typename T>
inline
1413 const std::vector<T>&
1414 Array<T>::vec()
const
1416 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1424 template<
typename T>
inline
1425 typename std::vector<T>::iterator
1426 Array<T>::raw_position( iterator position )
1428 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1429 const iterator first = this->begin();
1430 const iterator last = this->end();
1432 !(first <= position && position <= last), DanglingReferenceError,
1433 "Error, this iterator is no longer valid for this Aray!"
1438 return vec_->begin() + (position - this->begin());
1445 template<
typename T>
inline
1446 void Array<T>::assertIndex(size_type i)
const
1449 !( 0 <= i && i < size() ), RangeError,
1450 "Array<T>::assertIndex(i): i="<<i<<
" out of range [0, "<< size() <<
")"
1455 template<
typename T>
inline
1456 void Array<T>::assertNotNull()
const
1459 !size(), NullReferenceError,
1460 typeName(*
this)<<
"::assertNotNull(): "
1461 "Error, the array has size zero!"
1472 template<
typename T>
inline
1473 bool Teuchos::operator==(
const Array<T> &a1,
const Array<T> &a2 )
1474 {
return (a1.vec() == a2.vec()); }
1477 template<
typename T>
inline
1478 bool Teuchos::operator!=(
const Array<T> &a1,
const Array<T> &a2 )
1479 {
return (a1.vec() != a2.vec()); }
1482 template<
typename T>
inline
1483 void Teuchos::swap( Array<T> &a1, Array<T> &a2 )
1487 template<
typename T>
inline
1488 bool Teuchos::operator<( const Array<T> &a1,
const Array<T> &a2 )
1489 {
return (a1.vec() < a2.vec()); }
1492 template<
typename T>
inline
1493 bool Teuchos::operator<=( const Array<T> &a1,
const Array<T> &a2 )
1494 {
return (a1.vec() <= a2.vec()); }
1497 template<
typename T>
inline
1498 bool Teuchos::operator>(
const Array<T> &a1,
const Array<T> &a2 )
1499 {
return (a1.vec() > a2.vec()); }
1502 template<
typename T>
inline
1503 bool Teuchos::operator>=(
const Array<T> &a1,
const Array<T> &a2 )
1504 {
return (a1.vec() >= a2.vec()); }
1507 template<
typename T>
inline
1508 std::ostream& Teuchos::operator<<(
1509 std::ostream& os,
const Array<T>& array
1516 template<
typename T>
inline
1517 int Teuchos::hashCode(
const Array<T>& array)
1519 int rtn =
hashCode(array.length());
1520 for (
int i=0; i<array.length(); i++)
1528 template<
typename T>
inline
1529 std::vector<T> Teuchos::createVector(
const Array<T> &a )
1531 return a.toVector();
1535 template<
typename T>
inline
1538 return array.toString();
1542 template<
typename T>
1544 Teuchos::fromStringToArray(
const std::string& arrayStr)
1546 const std::string str = Utils::trimWhiteSpace(arrayStr);
1547 std::istringstream iss(str);
1549 ( str[0]!=
'{' || str[str.length()-1] !=
'}' )
1550 ,InvalidArrayStringRepresentation
1551 ,
"Error, the std::string:\n"
1555 "is not a valid array represntation!"
1562 while( !iss.eof() ) {
1564 std::string entryStr;
1565 std::getline(iss,entryStr,
',');
1574 entryStr = Utils::trimWhiteSpace(entryStr);
1576 0 == entryStr.length(),
1577 InvalidArrayStringRepresentation,
1578 "Error, the std::string:\n"
1582 "is not a valid array represntation because it has an empty array entry!"
1586 bool found_end =
false;
1587 if(entryStr[entryStr.length()-1]==
'}') {
1588 entryStr = entryStr.substr(0,entryStr.length()-1);
1590 if( entryStr.length()==0 && a.size()==0 )
1594 std::istringstream entryiss(entryStr);
1596 Teuchos::extractDataFromISS( entryiss, entry );
1605 found_end && !iss.eof()
1606 ,InvalidArrayStringRepresentation
1607 ,
"Error, the std::string:\n"
1611 "is not a valid array represntation!"
1618 #endif // TEUCHOS_ARRAY_H
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.
bool is_null(const boost::shared_ptr< T > &p)
Returns true if p.get()==NULL.
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.
ArrayView< T > view(size_type offset, size_type size)
Return non-const view of a contiguous range of elements.
#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
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)
T * getRawPtr()
Return a raw pointer to beginning of array or NULL if unsized.
std::vector< T >::const_pointer const_pointer
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.
static std::string trimWhiteSpace(const std::string &str)
Trim whitespace from beginning and end of std::string.
Statically sized simple array (tuple) class.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
std::string toString() const
Convert an Array to an std::string
ArrayRCP< const T > arcpFromArray(const Array< T > &a)
Wrap a const Array<T> object as a non-owning ArrayRCP<T> object.
TEUCHOSCORE_LIB_DLL_EXPORT std::string toString(const EVerbosityLevel verbLevel)
Return a std::string representation of the verbosity level.
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())
friend bool Teuchos::operator>(const Array< T2 > &a1, const Array< T2 > &a2)
std::vector< T >::reference reference
std::vector< T >::pointer pointer
std::vector< T >::const_iterator const_iterator
reverse_iterator rbegin()
std::vector< T >::const_reference const_reference
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
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.
friend bool Teuchos::operator>=(const Array< T2 > &a1, const Array< T2 > &a2)
std::vector< T >::allocator_type allocator_type
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.
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)
size_type max_size() const
std::vector< T >::iterator 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
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()
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.
Memory-safe templated array class that encapsulates std::vector.