Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
Teuchos_Array.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef TEUCHOS_ARRAY_H
43 #define TEUCHOS_ARRAY_H
44 
49 #include "Teuchos_ConfigDefs.hpp"
50 #include "Teuchos_Assert.hpp"
52 #include "Teuchos_ArrayRCP.hpp"
53 #include "Teuchos_Tuple.hpp"
54 #include "Teuchos_Utils.hpp"
55 #include "Teuchos_Assert.hpp"
56 
57 
58 namespace Teuchos {
59 
60 
65 class InvalidArrayStringRepresentation : public std::logic_error
66 {public:InvalidArrayStringRepresentation(const std::string& what_arg) : std::logic_error(what_arg) {}};
67 
68 
69 template<typename T> class Array;
70 
71 
72 // 2007/11/30: rabartl: Below, I had to move the initial declaration of these
73 // non-member template functions outside of the Array class since the Sun
74 // compiler on sass9000 would not accept this. However, this did work on a
75 // number of other compilers such a g++, Intel C++ etc. The old in-class
76 // non-member friend definition is clearly ISO 98 C++ as shown in Item 46 of
77 // "Effective C++: Third Edition". This is not the end of the world but this
78 // is something to remember for this platform.
79 
80 
85 template<typename T> inline
86 bool operator==( const Array<T> &a1, const Array<T> &a2 );
87 
88 
93 template<typename T> inline
94 bool operator!=( const Array<T> &a1, const Array<T> &a2 );
95 
96 
101 template<typename T> inline
102 void swap( Array<T> &a1, Array<T> &a2 );
103 
104 
109 template<typename T> inline
110 bool operator<( const Array<T> &a1, const Array<T> &a2 );
111 
112 
117 template<typename T> inline
118 bool operator<=( const Array<T> &a1, const Array<T> &a2 );
119 
120 
125 template<typename T> inline
126 bool operator>( const Array<T> &a1, const Array<T> &a2 );
127 
128 
133 template<typename T> inline
134 bool operator>=( const Array<T> &a1, const Array<T> &a2 );
135 
136 
174 template<typename T>
175 class Array
176 {
177 public:
178 
179  // 2007/11/30: rabartl: Below, note that the only reason that these
180  // functions are declared as friends is so that the compiler will do
181  // automatic type conversions as described in "Effective C++: Third Edition"
182  // Item 46.
183 
185  template<typename T2>
186  friend bool Teuchos::operator==( const Array<T2> &a1, const Array<T2> &a2 );
187 
189  template<typename T2>
190  friend bool Teuchos::operator!=( const Array<T2> &a1, const Array<T2> &a2 );
191 
193  template<typename T2>
194  friend void swap( Array<T2> &a1, Array<T2> &a2 );
195 
197  template<typename T2>
198  friend bool Teuchos::operator<( const Array<T2> &a1, const Array<T2> &a2 );
199 
201  template<typename T2>
202  friend bool Teuchos::operator<=( const Array<T2> &a1, const Array<T2> &a2 );
203 
205  template<typename T2>
206  friend bool Teuchos::operator>( const Array<T2> &a1, const Array<T2> &a2 );
207 
209  template<typename T2>
210  friend bool Teuchos::operator>=( const Array<T2> &a1, const Array<T2> &a2 );
211 
214 
218  typedef Ordinal size_type;
220  typedef Ordinal difference_type;
222  typedef typename std::vector<T>::value_type value_type;
224  typedef typename std::vector<T>::pointer pointer;
226  typedef typename std::vector<T>::const_pointer const_pointer;
228  typedef typename std::vector<T>::reference reference;
230  typedef typename std::vector<T>::const_reference const_reference;
232  typedef typename std::vector<T>::allocator_type allocator_type;
233 
234 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
235 
236  typedef ArrayRCP<T> iterator;
240  typedef std::reverse_iterator<iterator> reverse_iterator;
242  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
243 #else
244 
245  typedef typename std::vector<T>::iterator iterator;
247  typedef typename std::vector<T>::const_iterator const_iterator;
249  typedef typename std::vector<T>::reverse_iterator reverse_iterator;
251  typedef typename std::vector<T>::const_reverse_iterator const_reverse_iterator;
252 #endif
253 
254 
256 
259 
261  inline Array();
263  inline explicit Array(size_type n, const value_type& value = value_type());
265  inline Array(const Array<T>& x);
267  template<typename InputIterator>
268  inline Array(InputIterator first, InputIterator last);
270  inline Array(const ArrayView<const T>& a);
272  template<int N>
273  inline Array(const Tuple<T,N>& t);
275  inline ~Array();
277  inline Array& operator=(const Array<T>& a);
278 
280 
283 
285  inline void assign(size_type n, const value_type& val);
287  template<typename InputIterator>
288  inline void assign(InputIterator first, InputIterator last);
290  inline iterator begin();
292  inline iterator end();
294  inline const_iterator begin() const;
296  inline const_iterator end() const;
298  inline reverse_iterator rbegin();
300  inline reverse_iterator rend();
302  inline const_reverse_iterator rbegin() const;
304  inline const_reverse_iterator rend() const;
306  inline size_type size() const;
308  inline size_type max_size() const;
310  inline void resize(size_type new_size, const value_type& x = value_type());
312  inline size_type capacity() const;
314  inline bool empty() const;
316  inline void reserve(size_type n);
318  inline reference operator[](size_type i);
320  inline const_reference operator[](size_type i) const;
322  inline reference at(size_type i);
324  inline const_reference at(size_type i) const;
326  inline reference front();
328  inline const_reference front() const;
330  inline reference back();
332  inline const_reference back() const;
334  inline void push_back(const value_type& x);
336  inline void pop_back();
338  inline iterator insert(iterator position, const value_type& x);
340  inline void insert(iterator position, size_type n, const value_type& x);
342  template<typename InputIterator>
343  inline void insert(iterator position, InputIterator first, InputIterator last);
345  inline iterator erase(iterator position);
347  inline iterator erase(iterator first, iterator last);
349  inline void swap(Array& x);
351  inline void clear();
352 
354 
357 
362  inline Array<T>& append(const T& x);
363 
367  inline void remove(int i);
368 
373  inline int length() const;
374 
376  inline std::string toString() const;
377 
379  inline static bool hasBoundsChecking();
380 
382  inline T* getRawPtr();
383 
385  inline const T* getRawPtr() const;
386 
388 
391 
393  inline Array( const std::vector<T> &v );
394 
396  inline std::vector<T> toVector() const;
397 
399  inline Array& operator=( const std::vector<T> &v );
400 
402 
404 
405 
419  inline ArrayView<T> view( size_type offset, size_type size );
420 
434  inline ArrayView<const T> view( size_type offset, size_type size ) const;
435 
439  inline ArrayView<T> operator()( size_type offset, size_type size );
440 
444  inline ArrayView<const T> operator()( size_type offset, size_type size ) const;
445 
450  inline ArrayView<T> operator()();
451 
456  inline ArrayView<const T> operator()() const;
457 
461  inline operator ArrayView<T>();
462 
466  inline operator ArrayView<const T>() const;
467 
469 
470 private:
471 
472 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
473  RCP<std::vector<T> > vec_;
474  mutable ArrayRCP<T> extern_arcp_;
475  mutable ArrayRCP<const T> extern_carcp_;
476 #else
477  std::vector<T> vec_;
478 #endif
479 
480  inline std::vector<T>& vec(
481  bool isStructureBeingModified = false,
482  bool activeIter = false
483  );
484 
485  inline const std::vector<T>& vec() const;
486 
487  inline typename std::vector<T>::iterator
488  raw_position( iterator position );
489 
490  inline void assertIndex(size_type i) const;
491 
492  inline void assertNotNull() const;
493 
494 };
495 
496 
502 template<class T>
504 {
505  if ( is_null(v) || !v->size() )
506  return null;
507  return arcpWithEmbeddedObjPostDestroy<T,RCP<Array<T> > >(
508  &(*v)[0], 0, v->size(),
509  v, false
510  );
511 }
512 
513 
519 template<class T>
520 ArrayRCP<const T> arcp( const RCP<const Array<T> > &v )
521 {
522  if ( is_null(v) || !v->size() )
523  return null;
524  return arcpWithEmbeddedObjPostDestroy<const T,RCP<const Array<T> > >(
525  &(*v)[0], 0, v->size(),
526  v, false
527  );
528 }
529 
530 
536 template<class T>
538 {
539  if (a.size() == 0)
540  return null;
541 #ifdef TEUCHOS_DEBUG
542  return a.begin(); // Catch dangling reference!
543 #else
544  return arcp(a.getRawPtr(), 0, a.size(), false);
545 #endif
546 }
547 
548 
554 template<class T>
556 {
557  if (a.size() == 0)
558  return null;
559 #ifdef TEUCHOS_DEBUG
560  return a.begin(); // Catch dangling reference!
561 #else
562  return arcp(a.getRawPtr(), 0, a.size(), false);
563 #endif
564 }
565 
566 
579 template<typename T>
580 std::ostream& operator<<(std::ostream& os, const Array<T>& array);
581 
582 
587 template<typename T> inline
588 int hashCode(const Array<T>& array);
589 
590 
597 template<typename T> inline
598 std::vector<T> createVector( const Array<T> &a );
599 
600 
605 template<typename T>
606 std::string toString(const Array<T>& array);
607 
608 
660 template<typename T>
661 Array<T> fromStringToArray(const std::string& arrayStr);
662 
668 template<typename T>
669 std::istringstream& operator>> (std::istringstream& in, Array<T>& array){
670  array = fromStringToArray<T>(in.str());
671  return in;
672 }
673 
679 template<typename T> inline
680 void extractDataFromISS( std::istringstream& iss, T& data )
681 {
682  iss >> data; // Assumes type has operator>>(...) defined!
683 }
684 
691 inline
692 void extractDataFromISS( std::istringstream& iss, std::string& data )
693 {
694  // grab unformatted string.
695  data = iss.str();
696  // remove white space from beginning and end of string.
697  data = Utils::trimWhiteSpace(data);
698 }
699 
709 inline
711  return "Array(*)";
712 }
713 
714 
715 
731 template<typename T>
733 public:
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);
739  return prefix+TypeNameTraits<T>::name()+postFix;
740  }
741  static std::string concreteName(const Array<T>&)
742  { return name(); }
743 };
744 
745 
746 } // namespace Teuchos
747 
748 
749 //
750 // Implementation
751 //
752 
753 
754 namespace Teuchos {
755 
756 
757 // All constructors
758 
759 
760 template<typename T> inline
762 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
763  : vec_(rcp(new std::vector<T>()))
764 #endif
765 {}
766 
767 
768 template<typename T> inline
769 Array<T>::Array(size_type n, const value_type& value) :
770 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
771  vec_(rcp(new std::vector<T>(n,value)))
772 #else
773  vec_(n, value)
774 #endif
775 {}
776 
777 
778 template<typename T> inline
780 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
781  vec_(rcp(new std::vector<T>(*x.vec_)))
782 #else
783  vec_(x.vec_)
784 #endif
785 {}
786 
787 
788 template<typename T> template<typename InputIterator> inline
789 Array<T>::Array(InputIterator first, InputIterator last) :
790 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
791  vec_(rcp(new std::vector<T>(first, last)))
792 #else
793  vec_(first, last)
794 #endif
795 {}
796 
797 
798 template<typename T> inline
800 {}
801 
802 
803 template<typename T> inline
805 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
806  : vec_(rcp(new std::vector<T>()))
807 #endif
808 {
809  insert(begin(), a.begin(), a.end());
810 }
811 
812 
813 template<typename T>
814 template<int N>
815 inline
817 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
818  : vec_(rcp(new std::vector<T>()))
819 #endif
820 {
821  insert(begin(), t.begin(), t.end());
822 }
823 
824 
825 template<typename T> inline
827 {
828  vec(true) = a.vec();
829  return *this;
830 }
831 
832 
833 // Other std::vector functions
834 
835 
836 template<typename T> inline
837 void Array<T>::assign(size_type n, const value_type& val)
838 {
839  vec(true).assign(n,val);
840 }
841 
842 
843 template<typename T> template<typename InputIterator> inline
844 void Array<T>::assign(InputIterator first, InputIterator last)
845 {
846  vec(true).assign(first,last);
847 }
848 
849 
850 template<typename T> inline
851 typename Array<T>::iterator
853 {
854 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
855  if (is_null(extern_arcp_)) {
856  // Here we must use the same RCP to avoid creating two unrelated RCPNodes!
857  extern_arcp_ = arcp(vec_); // Will be null if vec_ is sized!
858  }
859  // Returning a weak pointer will help to catch dangling references but still
860  // keep the same behavior as optimized code.
861  return extern_arcp_.create_weak();
862 #else
863  return vec().begin();
864 #endif
865 }
866 
867 
868 template<typename T> inline
869 typename Array<T>::iterator
871 {
872 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
873  return begin() + size();
874 #else
875  return vec().end();
876 #endif
877 }
878 
879 
880 template<typename T> inline
883 {
884 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
885  if (is_null(extern_carcp_)) {
886  extern_carcp_ = const_cast<Array<T>*>(this)->begin();
887  }
888  // Returning a weak pointer will help to catch dangling references but still
889  // keep the same behavior as optimized code.
890  return extern_carcp_.create_weak();
891 #else
892  return vec().begin();
893 #endif
894 }
895 
896 
897 template<typename T> inline
900 {
901 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
902  return begin() + size();
903 #else
904  return vec().end();
905 #endif
906 }
907 
908 
909 template<typename T> inline
912 {
913 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
914  return reverse_iterator(end());
915 #else
916  return vec().rbegin();
917 #endif
918 }
919 
920 
921 template<typename T> inline
924 {
925 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
926  return reverse_iterator(begin());
927 #else
928  return vec().rend();
929 #endif
930 }
931 
932 
933 template<typename T> inline
936 {
937 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
938  return const_reverse_iterator(end());
939 #else
940  return vec().rbegin();
941 #endif
942 }
943 
944 
945 template<typename T> inline
948 {
949 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
950  return const_reverse_iterator(begin());
951 #else
952  return vec().rend();
953 #endif
954 }
955 
956 
957 template<typename T> inline
958 typename Array<T>::size_type
960 {
961  return vec().size();
962 }
963 
964 
965 template<typename T> inline
966 typename Array<T>::size_type
968 {
969  return std::numeric_limits<size_type>::max();
970 }
971 
972 
973 template<typename T> inline
974 void
975 Array<T>::resize(size_type new_size, const value_type& x)
976 {
977  vec(true).resize(new_size,x);
978 }
979 
980 
981 template<typename T> inline
982 typename Array<T>::size_type
984 {
985  return vec().capacity();
986 }
987 
988 
989 template<typename T> inline
990 bool Array<T>::empty() const
991 {
992  return vec().empty();
993 }
994 
995 
996 template<typename T> inline
997 void Array<T>::reserve(size_type n)
998 {
999  vec(true).reserve(n);
1000 }
1001 
1002 
1003 template<typename T> inline
1004 typename Array<T>::reference
1006 {
1007 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1008  assertIndex(i);
1009 #endif
1010  return vec()[i];
1011 }
1012 
1013 
1014 template<typename T> inline
1016 Array<T>::operator[](size_type i) const
1017 {
1018 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1019  assertIndex(i);
1020 #endif
1021  return vec()[i];
1022 }
1023 
1024 
1025 template<typename T> inline
1026 typename Array<T>::reference
1027 Array<T>::at(size_type i)
1028 {
1029 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1030  assertIndex(i);
1031 #endif
1032  return vec().at(i);
1033 }
1034 
1035 
1036 template<typename T> inline
1038 Array<T>::at(size_type i) const
1039 {
1040 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1041  assertIndex(i);
1042 #endif
1043  return vec().at(i);
1044 }
1045 
1046 
1047 template<typename T> inline
1048 typename Array<T>::reference
1050 {
1051 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1052  assertNotNull();
1053 #endif
1054  return vec().front();
1055 }
1056 
1057 
1058 template<typename T> inline
1061 {
1062 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1063  assertNotNull();
1064 #endif
1065  return vec().front();
1066 }
1067 
1068 
1069 template<typename T> inline
1070 typename Array<T>::reference
1072 {
1073 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1074  assertNotNull();
1075 #endif
1076  return vec().back();
1077 }
1078 
1079 
1080 template<typename T> inline
1083 {
1084 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1085  assertNotNull();
1086 #endif
1087  return vec().back();
1088 }
1089 
1090 
1091 template<typename T> inline
1092 void Array<T>::push_back(const value_type& x)
1093 {
1094  vec(true).push_back(x);
1095 }
1096 
1097 
1098 template<typename T> inline
1100 {
1101 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1102  assertNotNull();
1103 #endif
1104  vec(true).pop_back();
1105 }
1106 
1107 
1108 // 2009/11/13:: rabartl: After moving to a full RCPNode tracing and lookup
1109 // model, I had to how modifying functions like insert(...) and erase(...)
1110 // work which have active iterators controled by the client and yet need to
1111 // allow the structure of the container change. The way these troublesome
1112 // functions work is that first the raw std::vector iterator is extracted.
1113 // The function vec(true, true) then deletes the strong iterators but there is
1114 // still a weak ArrayRCP object that is owned by the client which is being
1115 // passed into this function. The issue is that the design of ArrayRCP is
1116 // such that the RCPNode object is not removed but instead remains in order to
1117 // perform runtime checking.
1118 
1119 
1120 template<typename T> inline
1121 typename Array<T>::iterator
1122 Array<T>::insert(iterator position, const value_type& x)
1123 {
1124 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1125  // Assert a valid iterator and get vector iterator
1126  const typename std::vector<T>::iterator raw_poss = raw_position(position);
1127  const difference_type i = position - begin();
1128  vec(true, true).insert(raw_poss, x);
1129  return begin() + i;
1130 #else
1131  return vec_.insert(position, x);
1132 #endif
1133 }
1134 
1135 
1136 template<typename T> inline
1137 void Array<T>::insert(iterator position, size_type n, const value_type& x)
1138 {
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);
1142 #else
1143  vec_.insert(position, n, x);
1144 #endif
1145 }
1146 
1147 
1148 template<typename T> template<typename InputIterator> inline
1149 void Array<T>::insert(iterator position, InputIterator first, InputIterator last)
1150 {
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);
1154 #else
1155  vec_.insert(position, first, last);
1156 #endif
1157 }
1158 
1159 
1160 template<typename T> inline
1161 typename Array<T>::iterator
1162 Array<T>::erase(iterator position)
1163 {
1164 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1165  assertNotNull();
1166  // Assert a valid iterator and get vector iterator
1167  const typename std::vector<T>::iterator raw_poss = raw_position(position);
1168  const difference_type i = position - begin();
1169  vec(true, true).erase(raw_poss);
1170  return begin() + i;
1171 #else
1172  return vec_.erase(position);
1173 #endif
1174 }
1175 
1176 
1177 template<typename T> inline
1178 typename Array<T>::iterator
1179 Array<T>::erase(iterator first, iterator last)
1180 {
1181 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1182  if (empty()) {
1183  TEUCHOS_ASSERT(first == begin());
1184  TEUCHOS_ASSERT(last == end());
1185  return end();
1186  }
1187  assertNotNull();
1188  // Assert a valid iterator and get vector iterator
1189  const typename std::vector<T>::iterator raw_first = raw_position(first);
1190  const typename std::vector<T>::iterator raw_last = raw_position(last);
1191  const difference_type i = first - begin();
1192  vec(true,true).erase(raw_first,raw_last);
1193  return begin() + i;
1194 #else
1195  return vec_.erase(first,last);
1196 #endif
1197 }
1198 
1199 
1200 template<typename T> inline
1202 {
1203  vec(true).swap(x.vec());
1204 }
1205 
1206 
1207 template<typename T> inline
1209 {
1210  vec(true).clear();
1211 }
1212 
1213 
1214 // Non-standard functions
1215 
1216 
1217 template<typename T> inline
1219 {
1220  this->push_back(x);
1221  return *this;
1222 }
1223 
1224 
1225 template<typename T> inline
1226 void Array<T>::remove(int i)
1227 {
1228 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1229  assertIndex(i);
1230 #endif
1231  // Erase the i-th element of this array.
1232  this->erase( this->begin() + i );
1233 }
1234 
1235 
1236 template<typename T> inline
1237 int Array<T>::length() const
1238 {
1239  return static_cast<int> (this->size ());
1240 }
1241 
1242 
1243 template<typename T> inline
1244 std::string Array<T>::toString() const
1245 {
1246  return (*this)().toString(); // Use ArrayView<T>::toString()
1247 }
1248 
1249 
1250 template<typename T> inline
1252 {
1253 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1254  return true;
1255 #else
1256  return false;
1257 #endif
1258 }
1259 
1260 
1261 template<typename T> inline
1263 {
1264  return ( size() ? &(*this)[0] : 0 );
1265 }
1266 
1267 
1268 template<typename T> inline
1269 const T* Array<T>::getRawPtr() const
1270 {
1271  return ( size() ? &(*this)[0] : 0 );
1272 }
1273 
1274 
1275 // Conversions to and from std::vector
1276 
1277 
1278 template<typename T> inline
1279 Array<T>::Array( const std::vector<T> &v ) :
1280 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1281  vec_(new std::vector<T>(v))
1282 #else
1283  vec_(v)
1284 #endif
1285 {}
1286 
1287 
1288 template<typename T> inline
1289 std::vector<T> Array<T>::toVector() const
1290 {
1291  if (!size())
1292  return std::vector<T>();
1293  std::vector<T> v(begin(),end());
1294  return v;
1295 }
1296 
1297 
1298 template<typename T> inline
1299 Array<T>& Array<T>::operator=( const std::vector<T> &v )
1300 {
1301  vec(true) = v;
1302  return *this;
1303 }
1304 
1305 
1306 // Views
1307 
1308 
1309 template<typename T> inline
1310 ArrayView<T> Array<T>::view( size_type offset, size_type size_in )
1311 {
1312  if (size_in) {
1313 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1314  return ArrayView<T>(this->begin().persistingView(offset, size_in));
1315 #else
1316  return arrayView( &vec()[offset], size_in );
1317 #endif
1318  }
1319  return Teuchos::null;
1320 }
1321 
1322 
1323 template<typename T> inline
1324 ArrayView<const T> Array<T>::view( size_type offset, size_type size_in ) const
1325 {
1326  if (size_in) {
1327 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1328  return ArrayView<const T>(this->begin().persistingView(offset, size_in));
1329 #else
1330  return arrayView( &vec()[offset], size_in );
1331 #endif
1332  }
1333  return Teuchos::null;
1334  // NOTE: Above, we use a different implementation to call the const version
1335  // of begin() instead of the non-const version. This sets up a different
1336  // ArrayRCP object that gets checked.
1337 }
1338 
1339 
1340 template<typename T> inline
1341 ArrayView<T> Array<T>::operator()( size_type offset, size_type size_in )
1342 {
1343  return view(offset, size_in);
1344 }
1345 
1346 
1347 template<typename T> inline
1348 ArrayView<const T> Array<T>::operator()( size_type offset, size_type size_in ) const
1349 {
1350  return view(offset, size_in);
1351 }
1352 
1353 
1354 template<typename T> inline
1356 {
1357  if (!size())
1358  return null;
1359  return this->view(0, size());
1360 }
1361 
1362 
1363 template<typename T> inline
1365 {
1366  if (!size())
1367  return null;
1368  return this->view(0, size());
1369 }
1370 
1371 
1372 template<typename T> inline
1374 {
1375  return this->operator()();
1376 }
1377 
1378 
1379 template<typename T> inline
1381 {
1382  return this->operator()();
1383 }
1384 
1385 
1386 // private
1387 
1388 
1389 template<typename T>
1390 std::vector<T>&
1391 Array<T>::vec( bool isStructureBeingModified, bool activeIter )
1392 {
1393 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1394  (void)activeIter;
1395  if (isStructureBeingModified) {
1396  // Give up my ArrayRCPs used for iterator access since the array we be
1397  // getting modifed! Any clients that have views through weak pointers
1398  // better not touch them!
1399  extern_arcp_ = null;
1400  extern_carcp_ = null;
1401  }
1402  return *vec_;
1403 #else
1404  // get rid of "unused parameter" warnings
1405  (void)isStructureBeingModified;
1406  (void)activeIter;
1407  return vec_;
1408 #endif
1409 }
1410 
1411 
1412 template<typename T> inline
1413 const std::vector<T>&
1415 {
1416 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
1417  return *vec_;
1418 #else
1419  return vec_;
1420 #endif
1421 }
1422 
1423 
1424 template<typename T> inline
1425 typename std::vector<T>::iterator
1426 Array<T>::raw_position( iterator position )
1427 {
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!"
1434  );
1435  // Note, above operator<=(...) functions will throw
1436  // IncompatibleIteratorsError if the iterators do not share the same
1437  // RCP_node object!
1438  return vec_->begin() + (position - this->begin());
1439 #else
1440  return position;
1441 #endif
1442 }
1443 
1444 
1445 template<typename T> inline
1446 void Array<T>::assertIndex(size_type i) const
1447 {
1449  !( 0 <= i && i < size() ), RangeError,
1450  "Array<T>::assertIndex(i): i="<<i<<" out of range [0, "<< size() << ")"
1451  );
1452 }
1453 
1454 
1455 template<typename T> inline
1457 {
1460  typeName(*this)<<"::assertNotNull(): "
1461  "Error, the array has size zero!"
1462  );
1463 }
1464 
1465 
1466 } // namespace Teuchos
1467 
1468 
1469 // Nonmember functions
1470 
1471 
1472 template<typename T> inline
1473 bool Teuchos::operator==( const Array<T> &a1, const Array<T> &a2 )
1474 { return (a1.vec() == a2.vec()); }
1475 
1476 
1477 template<typename T> inline
1478 bool Teuchos::operator!=( const Array<T> &a1, const Array<T> &a2 )
1479 { return (a1.vec() != a2.vec()); }
1480 
1481 
1482 template<typename T> inline
1483 void Teuchos::swap( Array<T> &a1, Array<T> &a2 )
1484 { a1.swap(a2); }
1485 
1486 
1487 template<typename T> inline
1488 bool Teuchos::operator<( const Array<T> &a1, const Array<T> &a2 )
1489 { return (a1.vec() < a2.vec()); }
1490 
1491 
1492 template<typename T> inline
1493 bool Teuchos::operator<=( const Array<T> &a1, const Array<T> &a2 )
1494 { return (a1.vec() <= a2.vec()); }
1495 
1496 
1497 template<typename T> inline
1498 bool Teuchos::operator>( const Array<T> &a1, const Array<T> &a2 )
1499 { return (a1.vec() > a2.vec()); }
1500 
1501 
1502 template<typename T> inline
1503 bool Teuchos::operator>=( const Array<T> &a1, const Array<T> &a2 )
1504 { return (a1.vec() >= a2.vec()); }
1505 
1506 
1507 template<typename T> inline
1508 std::ostream& Teuchos::operator<<(
1509  std::ostream& os, const Array<T>& array
1510  )
1511 {
1512  return os << Teuchos::toString(array);
1513 }
1514 
1515 
1516 template<typename T> inline
1517 int Teuchos::hashCode(const Array<T>& array)
1518 {
1519  int rtn = hashCode(array.length());
1520  for (int i=0; i<array.length(); i++)
1521  {
1522  rtn += hashCode(array[i]);
1523  }
1524  return rtn;
1525 }
1526 
1527 
1528 template<typename T> inline
1529 std::vector<T> Teuchos::createVector( const Array<T> &a )
1530 {
1531  return a.toVector();
1532 }
1533 
1534 
1535 template<typename T> inline
1536 std::string Teuchos::toString(const Array<T>& array)
1537 {
1538  return array.toString();
1539 }
1540 
1541 
1542 template<typename T>
1544 Teuchos::fromStringToArray(const std::string& arrayStr)
1545 {
1546  const std::string str = Utils::trimWhiteSpace(arrayStr);
1547  std::istringstream iss(str);
1549  ( str[0]!='{' || str[str.length()-1] != '}' )
1551  ,"Error, the std::string:\n"
1552  "----------\n"
1553  <<str<<
1554  "\n----------\n"
1555  "is not a valid array represntation!"
1556  );
1557  char c;
1558  c = iss.get(); // Read initial '{'
1559  TEUCHOS_TEST_FOR_EXCEPT(c!='{'); // Should not throw!
1560  // Now we are ready to begin reading the entries of the array!
1561  Array<T> a;
1562  while( !iss.eof() ) {
1563  // Get the basic entry std::string
1564  std::string entryStr;
1565  std::getline(iss,entryStr,','); // Get next entry up to ,!
1566  // ToDo: Above, we might have to be careful to look for the opening and
1567  // closing of parentheses in order not to pick up an internal ',' in the
1568  // middle of an entry (for a std::complex number for instance). The above
1569  // implementation assumes that there will be no commas in the middle of
1570  // the std::string representation of an entry. This is certainly true for
1571  // the types bool, int, float, and double.
1572  //
1573  // Trim whitespace from beginning and end
1574  entryStr = Utils::trimWhiteSpace(entryStr);
1576  0 == entryStr.length(),
1578  "Error, the std::string:\n"
1579  "----------\n"
1580  <<str<<
1581  "\n----------\n"
1582  "is not a valid array represntation because it has an empty array entry!"
1583  );
1584  // Remove the final '}' if this is the last entry and we did not
1585  // actually terminate the above getline(...) on ','
1586  bool found_end = false;
1587  if(entryStr[entryStr.length()-1]=='}') {
1588  entryStr = entryStr.substr(0,entryStr.length()-1);
1589  found_end = true;
1590  if( entryStr.length()==0 && a.size()==0 )
1591  return a; // This is the empty array "{}" (with any spaces in it!)
1592  }
1593  // Finally we can convert the entry and add it to the array!
1594  std::istringstream entryiss(entryStr);
1595  T entry;
1596  Teuchos::extractDataFromISS( entryiss, entry );
1597  // ToDo: We may need to define a traits class to allow us to specialized
1598  // how conversion from a std::string to a object is done!
1599  a.push_back(entry);
1600  // At the end of the loop body here, if we have reached the last '}'
1601  // then the input stream iss should be empty and iss.eof() should be
1602  // true, so the loop should terminate. We put an std::exception test here
1603  // just in case something has gone wrong.
1605  found_end && !iss.eof()
1607  ,"Error, the std::string:\n"
1608  "----------\n"
1609  <<str<<
1610  "\n----------\n"
1611  "is not a valid array represntation!"
1612  );
1613  }
1614  return a;
1615 }
1616 
1617 
1618 #endif // TEUCHOS_ARRAY_H
static std::string concreteName(const Array< T > &)
Dangling reference error exception class.
Null reference error exception class.
reverse_iterator rend()
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.
std::string typeName(const T &t)
Template function for returning the concrete type name of a passed-in object.
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.
bool empty() const
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
std::vector< T >::iterator raw_position(iterator position)
void extractDataFromISS(std::istringstream &iss, std::string &data)
Extracts std::string data from an istringstream object.
RawPointerConversionTraits< Container >::Ptr_t getRawPtr(const Container &c)
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
std::vector< T > vec_
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.
void assertNotNull() const
std::istringstream & operator>>(std::istringstream &in, TwoDArray< T > &array)
static std::string trimWhiteSpace(const std::string &str)
Trim whitespace from beginning and end of std::string.
const std::vector< T > & vec() const
Statically sized simple array (tuple) class.
TEUCHOS_ORDINAL_TYPE Teuchos_Ordinal
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
reference front()
Ordinal difference_type
#define TEUCHOSCORE_LIB_DLL_EXPORT
std::string toString(const HashSet< Key > &h)
std::string toString() const
Convert an Array to an std::string
std::vector< T > & vec(bool isStructureBeingModified=false, bool activeIter=false)
ArrayRCP< const T > arcpFromArray(const Array< T > &a)
Wrap a const Array<T> object as a non-owning ArrayRCP<T> object.
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)
Teuchos_Ordinal Ordinal
std::vector< T >::reference reference
std::vector< T >::pointer pointer
std::vector< Teuchos::XMLObject >::const_iterator const_iterator
reverse_iterator rbegin()
std::vector< T >::const_reference const_reference
std::ostream & operator<<(std::ostream &os, const HashSet< Key > &h)
reference back()
void push_back(const value_type &x)
bool operator==(const TwoDArray< T > &a1, const TwoDArray< T > &a2)
Nonowning array view.
Default traits class that just returns typeid(T).name().
void extractDataFromISS(std::istringstream &iss, T &data)
Extracts data from an istringstream object.
std::vector< Teuchos::XMLObject >::const_reverse_iterator const_reverse_iterator
dependentList insert(myDepList.getEntryRCP(dependent1))
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.
size_type size() const
friend bool Teuchos::operator>=(const Array< T2 > &a1, const Array< T2 > &a2)
InvalidArrayStringRepresentation(const std::string &what_arg)
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)
Range error exception class.
size_type max_size() const
std::vector< Teuchos::XMLObject >::iterator iterator
A utilities class for Teuchos.
iterator insert(iterator position, const value_type &x)
iterator begin()
Defines basic traits returning the name of a type in a portable and readable way. ...
std::vector< Teuchos::XMLObject >::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...
void assertIndex(size_type i) const
Reference-counted smart pointer for managing arrays.
Memory-safe templated array class that encapsulates std::vector.