Teuchos_ArrayView.hpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef TEUCHOS_ARRAY_VIEW_HPP
00030 #define TEUCHOS_ARRAY_VIEW_HPP
00031
00032
00033 #include "Teuchos_ArrayViewDecl.hpp"
00034 #include "Teuchos_ArrayRCP.hpp"
00035 #include "Teuchos_as.hpp"
00036
00037
00038 namespace Teuchos {
00039
00040
00041
00042
00043
00044 template<class T> inline
00045 ArrayView<T>::ArrayView( ENull )
00046 :ptr_(0), size_(0)
00047 {
00048 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00049 setUpIterators();
00050 #endif
00051 }
00052
00053
00054 template<class T> inline
00055 ArrayView<T>::ArrayView( T* p, Ordinal size_in )
00056 :ptr_(p), size_(size_in)
00057 {
00058 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00059 TEST_FOR_EXCEPT( p != 0 && size_in <= 0 );
00060 TEST_FOR_EXCEPT( p == 0 && size_in != 0 );
00061 setUpIterators();
00062 #endif
00063 }
00064
00065
00066 template<class T> inline
00067 ArrayView<T>::ArrayView(const ArrayView<T>& array)
00068 :ptr_(array.ptr_), size_(array.size_)
00069 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00070 ,arcp_(array.arcp_)
00071 #endif
00072 {}
00073
00074
00075 template<class T> inline
00076 ArrayView<T>::ArrayView(
00077 std::vector<typename ConstTypeTraits<T>::NonConstType>& vec
00078 )
00079 : ptr_( vec.empty() ? 0 : &vec[0] ), size_(vec.size())
00080 {
00081 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00082 setUpIterators();
00083 #endif
00084 }
00085
00086
00087 template<class T> inline
00088 ArrayView<T>::ArrayView(
00089 const std::vector<typename ConstTypeTraits<T>::NonConstType>& vec
00090 )
00091 : ptr_( vec.empty() ? 0 : &vec[0] ), size_(vec.size())
00092 {
00093 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00094 setUpIterators();
00095 #endif
00096 }
00097
00098
00099 template<class T> inline
00100 ArrayView<T>::~ArrayView()
00101 {}
00102
00103
00104
00105
00106
00107 template<class T> inline
00108 typename ArrayView<T>::Ordinal ArrayView<T>::size() const
00109 {
00110 return size_;
00111 }
00112
00113
00114 template<typename T>
00115 std::string ArrayView<T>::toString() const
00116 {
00117 using Teuchos::as;
00118 std::ostringstream ss;
00119 ss << "{";
00120
00121 for (int i=0; i < as<int>(size()); ++i)
00122 {
00123 ss << operator[](i);
00124 if (i < size()-1) ss << ", ";
00125 }
00126 ss << "}";
00127
00128 return ss.str();
00129 }
00130
00131
00132
00133
00134
00135 template<class T> inline
00136 T* ArrayView<T>::getRawPtr() const
00137 {
00138 return ptr_;
00139 }
00140
00141
00142 template<class T> inline
00143 T& ArrayView<T>::operator[](Ordinal i) const
00144 {
00145 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00146 assert_in_range(i,1);
00147 #endif
00148 return ptr_[i];
00149 }
00150
00151
00152 template<class T> inline
00153 T& ArrayView<T>::front() const
00154 {
00155 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00156 assert_not_null();
00157 #endif
00158 return *ptr_;
00159 }
00160
00161
00162 template<class T> inline
00163 T& ArrayView<T>::back() const
00164 {
00165 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00166 assert_not_null();
00167 #endif
00168 return *(ptr_+size_-1);
00169 }
00170
00171
00172
00173
00174
00175 template<class T> inline
00176 ArrayView<T> ArrayView<T>::view(Ordinal offset, Ordinal size_in) const
00177 {
00178 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00179 assert_in_range(offset,size_in);
00180 #endif
00181 return ArrayView<T>(ptr_+offset,size_in);
00182
00183
00184 }
00185
00186
00187 template<class T> inline
00188 ArrayView<T> ArrayView<T>::operator()( Ordinal offset, Ordinal size_in ) const
00189 {
00190 return view(offset,size_in);
00191 }
00192
00193
00194 template<class T> inline
00195 const ArrayView<T>& ArrayView<T>::operator()() const
00196 {
00197 return *this;
00198 }
00199
00200
00201 template<class T> inline
00202 ArrayView<const T> ArrayView<T>::getConst() const
00203 {
00204 return ArrayView<const T>(ptr_,size_);
00205 }
00206
00207
00208 template<class T> inline
00209 ArrayView<T>::operator ArrayView<const T>() const
00210 {
00211 return getConst();
00212 }
00213
00214
00215
00216
00217
00218 template<class T>
00219 void ArrayView<T>::assign(const ArrayView<const T>& array) const
00220 {
00221 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00222 assert_not_null();
00223 #endif
00224 if (this->getRawPtr()==array.getRawPtr() && this->size()==array.size())
00225 return;
00226 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00227 assert_in_range(0,array.size());
00228 #endif
00229 std::copy( array.begin(), array.end(), this->begin() );
00230
00231
00232 }
00233
00234 template<class T>
00235 ArrayView<T>& ArrayView<T>::operator=(const ArrayView<T>& array)
00236 {
00237 ptr_ = array.ptr_;
00238 size_ = array.size_;
00239 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00240 arcp_ = array.arcp_;
00241 #endif
00242 return *this;
00243 }
00244
00245
00246
00247
00248 template<class T>
00249 typename ArrayView<T>::iterator ArrayView<T>::begin() const
00250 {
00251 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00252 return arcp_;
00253 #else
00254 return ptr_;
00255 #endif
00256 }
00257
00258
00259 template<class T>
00260 typename ArrayView<T>::iterator ArrayView<T>::end() const
00261 {
00262 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00263 return arcp_ + size_;
00264 #else
00265 return ptr_ + size_;
00266 #endif
00267 }
00268
00269
00270
00271
00272
00273 template<class T>
00274 const ArrayView<T>& ArrayView<T>::assert_not_null() const
00275 {
00276 if(!ptr_)
00277 throw_null_ptr_error(typeName(*this));
00278 return *this;
00279 }
00280
00281
00282 template<class T>
00283 const ArrayView<T>&
00284 ArrayView<T>::assert_in_range( Ordinal offset, Ordinal size_in ) const
00285 {
00286 assert_not_null();
00287 TEST_FOR_EXCEPTION(
00288 !( 0 <= offset && offset+size_in <= this->size() ), Teuchos::RangeError,
00289 typeName(*this)<<"::assert_in_range():"
00290 " Error, [offset,offset+size) = ["<<offset<<","<<(offset+size_in)<<")"
00291 " does not lie in the range [0,"<<this->size()<<")!"
00292 );
00293 return*this;
00294 }
00295
00296
00297
00298
00299
00300 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00301
00302 template<class T>
00303 void ArrayView<T>::setUpIterators()
00304 {
00305 if (ptr_)
00306 arcp_ = arcp(ptr_,0,size_,false);
00307 }
00308
00309 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00310
00311
00312 template<class T> inline
00313 void* ArrayView<T>::operator new(size_t)
00314 {
00315
00316 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00317 TEST_FOR_EXCEPT(true);
00318 #endif
00319 return 0;
00320 }
00321
00322
00323 #ifndef TEUCHOS_PRIVIATE_DELETE_NOT_SUPPORTED
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334 #endif // TEUCHOS_PRIVIATE_DELETE_NOT_SUPPORTED
00335
00336
00337 }
00338
00339
00340
00341
00342
00343
00344
00345 template<class T> inline
00346 Teuchos::ArrayView<T>
00347 Teuchos::arrayView( T* p, typename ArrayView<T>::Ordinal size )
00348 {
00349 return ArrayView<T>(p,size);
00350 }
00351
00352
00353 template<class T> inline
00354 Teuchos::ArrayView<T> Teuchos::arrayViewFromVector( std::vector<T>& vec )
00355 {
00356 return ArrayView<T>(vec);
00357 }
00358
00359
00360 template<class T> inline
00361 Teuchos::ArrayView<const T> Teuchos::arrayViewFromVector( const std::vector<T>& vec )
00362 {
00363 return ArrayView<const T>(vec);
00364 }
00365
00366
00367 #ifndef __sun
00368
00369 template<class T> inline
00370 std::vector<T> Teuchos::createVector( const ArrayView<T> &ptr )
00371 {
00372 std::vector<T> v(ptr.begin(),ptr.end());
00373 return v;
00374 }
00375
00376 #endif // __sun
00377
00378
00379 template<class T> inline
00380 std::vector<T> Teuchos::createVector( const ArrayView<const T> &ptr )
00381 {
00382 std::vector<T> v(ptr.begin(),ptr.end());
00383 return v;
00384 }
00385
00386
00387 template<class T>
00388 std::ostream& Teuchos::operator<<( std::ostream& out, const ArrayView<T>& p )
00389 {
00390 return out << p.toString();
00391 }
00392
00393
00394 #endif // TEUCHOS_ARRAY_VIEW_HPP