42 #ifndef VECTOR_CLASS_TMPL_H
43 #define VECTOR_CLASS_TMPL_H
47 #include "DenseLinAlgPack_Types.hpp"
48 #include "StrideIterPack_StrideIter.hpp"
50 namespace DenseLinAlgPack{
112 typedef T value_type;
113 typedef DenseLinAlgPack::size_type size_type;
114 typedef ptrdiff_t difference_type;
116 , value_type, value_type&, value_type*
119 , value_type,
const value_type&,
const value_type*
121 #if defined(_INTEL_CXX) || defined (_INTEL_CXX)
122 typedef std::reverse_iterator<
iterator, value_type
123 , value_type&, value_type*, difference_type> reverse_iterator;
125 , value_type,
const value_type&,
const value_type*
126 , difference_type> const_reverse_iterator;
128 typedef std::reverse_iterator<iterator> reverse_iterator;
129 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
131 typedef value_type& reference;
132 typedef const value_type& const_reference;
252 reverse_iterator
rbegin();
254 reverse_iterator
rend();
256 const_reverse_iterator
rbegin()
const;
258 const_reverse_iterator
rend()
const;
277 const_reference operator()(size_type i)
const;
281 const_reference
operator[](size_type i)
const;
395 size_type
dim()
const;
420 const value_type*
raw_ptr()
const;
426 difference_type
stride()
const;
433 difference_type stride_;
474 typedef T value_type;
475 typedef DenseLinAlgPack::size_type size_type;
476 typedef ptrdiff_t difference_type;
477 typedef value_type* iterator;
478 typedef const value_type* const_iterator;
480 typedef std::reverse_iterator<iterator, value_type
481 , value_type&, value_type*, difference_type> reverse_iterator;
482 typedef std::reverse_iterator<const_iterator
483 , value_type,
const value_type&,
const value_type*
484 , difference_type> const_reverse_iterator;
486 typedef std::reverse_iterator<iterator> reverse_iterator;
487 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
489 typedef value_type& reference;
490 typedef const value_type& const_reference;
491 typedef std::vector<value_type> valarray;
540 void resize(size_type n, value_type val = value_type());
550 size_type
dim()
const;
584 const_iterator
begin()
const;
586 const_iterator
end()
const;
588 reverse_iterator
rbegin();
590 reverse_iterator
rend();
592 const_reverse_iterator
rbegin()
const;
594 const_reverse_iterator
rend()
const;
613 const_reference operator()(size_type i)
const;
617 const_reference
operator[](size_type i)
const;
735 const value_type*
raw_ptr()
const;
741 difference_type
stride()
const;
764 size_type vector_validate_sized(size_type size);
766 void vector_validate_range(size_type ubound, size_type max_ubound);
768 void vector_validate_subscript(size_type size, size_type i);
773 void assert_vs_sizes(size_type size1, size_type size2);
788 const VectorSliceTmpl<T> gen_vs(
const VectorSliceTmpl<T>& vs,
size_type start,
size_type size
791 return VectorSliceTmpl<T>(
const_cast<typename VectorSliceTmpl<T>::value_type*
>(vs.start_ptr()) + vs.stride() * (start-1)
792 , size, vs.stride() * stride );
810 #ifndef LINALGPACK_CHECK_SLICE_SETUP
812 DenseLinAlgPack::size_type DenseLinAlgPack::vector_validate_sized(
size_type size)
818 #ifndef LINALGPACK_CHECK_RANGE
824 #ifndef LINALGPACK_CHECK_RANGE
830 #ifndef LINALGPACK_CHECK_RHS_SIZES
836 namespace DenseLinAlgPack {
862 : ptr_( ptr + rng.lbound() - 1 )
863 , size_( rng.full_range() ? vector_validate_sized(size) : rng.size() )
872 : ptr_( vs.start_ptr() + (rng.lbound() - 1) * vs.stride() )
873 , size_( rng.full_range() ? vector_validate_sized(vs.dim()) : rng.size() )
874 , stride_( vs.stride() )
883 stride_ = vs.stride_;
890 {
return iterator(start_ptr(), stride()); }
895 {
return iterator(start_ptr() + dim() * stride(), stride()); }
905 {
return const_iterator(start_ptr() + dim() * stride(), stride()); }
910 {
return reverse_iterator(end()); }
915 {
return reverse_iterator(begin()); }
920 {
return const_reverse_iterator(end()); }
925 {
return const_reverse_iterator(begin()); }
932 vector_validate_subscript(dim(),i);
933 return ptr_[(i-1)*stride_];
940 vector_validate_subscript(dim(),i);
941 return ptr_[(i-1)*stride_];
948 vector_validate_subscript(dim(),i+1);
949 return ptr_[(i)*stride_];
956 vector_validate_subscript(dim(),i+1);
957 return ptr_[(i)*stride_];
978 const VectorSliceTmpl<T> VectorSliceTmpl<T>::operator()(
const Range1D& rng)
const
979 {
return VectorSliceTmpl(
const_cast<VectorSliceTmpl<T>&
>(*
this), RangePack::full_range(rng,1,dim())); }
984 {
return VectorSliceTmpl(*
this, Range1D(lbound, ubound)); }
994 {
return VectorSliceTmpl( start_ptr() + stride() * (dim()-1), dim(), - stride() ); }
999 {
return VectorSliceTmpl( const_cast<value_type*>(start_ptr()) + stride() * (dim()-1), dim(), - stride() ); }
1006 std::fill(begin(),end(),alpha);
1014 assert_vs_sizes(this->dim(),rhs.
dim());
1015 std::copy(rhs.begin(),rhs.
end(),begin());
1031 {
return stride() > 0 ? start_ptr() : start_ptr() + stride() * (dim() - 1); }
1036 {
return stride() > 0 ? start_ptr() : start_ptr() + stride() * (dim() - 1); }
1081 std::copy(p,p+n,
begin());
1089 std::copy(vs.begin(),vs.
end(),
begin());
1098 std::fill(begin(),end(),val);
1112 {
return v_.size(); }
1118 {
return start_ptr(); }
1123 {
return start_ptr() + dim(); }
1128 {
return start_ptr(); }
1133 {
return start_ptr() + dim(); }
1138 {
return reverse_iterator(end()); }
1143 {
return reverse_iterator(begin()); }
1148 {
return const_reverse_iterator(end()); }
1153 {
return const_reverse_iterator(begin()); }
1160 vector_validate_subscript(dim(),i);
1161 return start_ptr()[i-1];
1168 vector_validate_subscript(dim(),i);
1169 return start_ptr()[i-1];
1176 vector_validate_subscript(dim(),i+1);
1177 return start_ptr()[i];
1184 vector_validate_subscript(dim(),i+1);
1185 return start_ptr()[i];
1227 {
return VectorSliceTmpl<T>(
const_cast<value_type*
>(start_ptr()) + dim() - 1, dim(), -1 ); }
1237 VectorTmpl<T>::operator
const VectorSliceTmpl<T>()
const
1238 {
return VectorSliceTmpl<T>(
const_cast<value_type*
>(start_ptr()), dim()); }
1245 if(!dim()) resize(1);
1246 std::fill(begin(),end(),alpha);
1255 std::copy(rhs.
begin(),rhs.
end(),begin());
1264 std::copy(rhs.begin(),rhs.
end(),begin());
1273 {
return start_ptr(); }
1278 {
return start_ptr(); }
1283 {
return dim() ? &(v_)[0] : 0; }
1288 {
return &
const_cast<valarray&
>((v_))[0]; }
1302 const typename VectorSliceTmpl<T>::value_type
1303 *raw_ptr1 = ( stride() > 0 ? start_ptr() : start_ptr() + (dim()-1)*stride() ),
1304 *raw_ptr2 = ( vs.stride() > 0 ? vs.start_ptr() : vs.start_ptr() + (vs.dim()-1)*vs.stride() );
1305 typename VectorSliceTmpl<T>::size_type
1308 typename VectorSliceTmpl<T>::difference_type
1309 stride1 = std::abs(stride()),
1310 stride2 = std::abs(vs.
stride());
1313 if(raw_ptr1 > raw_ptr2) {
1314 std::swap(raw_ptr1,raw_ptr2);
1315 std::swap(stride1,stride2);
1316 std::swap(size1,size2);
1319 if( raw_ptr1 + stride1 * (size1 - 1) < raw_ptr2 ) {
1325 start2 = raw_ptr2 - raw_ptr1;
1327 if(start1 == start2 && stride1 == stride2 && size1 == size2)
1333 else if(stride1 == stride2) {
1334 if(!((start2 - start1) % stride1))
1335 return SOME_OVERLAP;
1340 if(stride1 == 1 || stride2 == 1) {
1343 bool switch_them = (stride2 == 1);
1345 std::swap(start1,start2);
1346 std::swap(stride1,stride2);
1347 std::swap(size1,size2);
1357 size_t max_rows = (start1+size1-1)/stride2 - start1/stride2 > 0 ? stride2 - 1 : stride2;
1360 size_t vs2_col_i = start1/max_rows - start2/max_rows;
1365 size_t vs2_col_rng = start2 + vs2_col_i * stride2;
1366 if(start1 <= vs2_col_rng && vs2_col_rng <= start1+size1-1)
1367 return SOME_OVERLAP;
1374 bool switch_them = stride2 < stride1;
1376 std::swap(start1,start2);
1377 std::swap(stride1,stride2);
1378 std::swap(size1,size2);
1381 size_t max_rows = stride1;
1384 size_t sec_first_col = (start1 > start2) ? start1/max_rows : start2/max_rows,
1385 last1 = start1 + (size1 - 1) * stride1,
1386 last2 = start2 + (size2 - 1) * stride2,
1387 sec_last_col = (last1 < last2) ? last1/max_rows : last2/max_rows;
1390 size_t vs1_first_col = start1 / max_rows,
1391 vs2_first_col = start2 / max_rows;
1394 size_t vs1_first_col_i = sec_first_col - vs1_first_col,
1395 vs1_last_col_i = sec_last_col - vs1_first_col,
1396 vs2_first_col_i = sec_first_col - vs2_first_col,
1397 vs2_last_col_i = sec_last_col - vs2_first_col;
1403 ptrdiff_t diff1 = (start1 + vs1_first_col_i * stride1)
1404 - static_cast<ptrdiff_t>((start2 + vs2_first_col_i * stride2)),
1405 diff2 = (start1 + vs1_last_col_i * stride1)
1406 -
static_cast<ptrdiff_t
>((start2 + vs2_last_col_i * stride2));
1407 if(diff1 * diff2 > 0 )
1410 return SOME_OVERLAP;
1415 EOverLap VectorTmpl<T>::overlap(
const VectorSliceTmpl<value_type>& vs)
const {
1417 const typename VectorSliceTmpl<T>::value_type
1418 *raw_ptr1 = ( stride() > 0 ? start_ptr() : start_ptr() + (dim()-1)*stride() ),
1419 *raw_ptr2 = ( vs.stride() > 0 ? vs.start_ptr() : vs.start_ptr() + (vs.dim()-1)*vs.stride() );
1424 if( raw_ptr1 <= raw_ptr2 && raw_ptr2 + size2 <= raw_ptr1 + size1 ) {
1425 if( raw_ptr1 == raw_ptr2 && size1 == size2 && 1 == vs.stride() )
1427 return SOME_OVERLAP;
1434 #endif // end VECTOR_CLASS_TMPL_H
reverse_iterator rbegin()
size_type dim() const
Returns the number of elements of the VectorSliceTmpl.
reverse_iterator rbegin()
value_type * start_ptr()
Return a pointer to the conceptual first element in the underlying array.
value_type * start_ptr()
Return a pointer to the conceptual first element in the underlying array.
void resize(size_type n, value_type val=value_type())
VectorTmpl()
Constructs a vector with 0 elements (this->dim()==0).
VectorSliceTmpl< value_type > * operator&()
Allow the address to be taken of an rvalue of this object.
value_type * raw_ptr()
Return a pointer to the address of the first memory location of underlying array. ...
VectorSliceTmpl< value_type > & operator=(value_type alpha)
reference operator()(size_type i)
1-based element access (lvalue)
value_type * raw_ptr()
Return a pointer to the address of the first memory location of underlying array. ...
size_type dim() const
Returns the number of elements of the DVector.
difference_type stride() const
Return the distance (+,-) (in units of elements) between adjacent elements in the underlying array...
void bind(VectorSliceTmpl< value_type > vs)
Bind to the view of another VectorSliceTmpl.
const VectorSliceTmpl< value_type > * operator&() const
difference_type stride() const
Return the distance (+,-) (in units of elements) between adjacent elements in the underlying array...
reference operator[](size_type i)
1-based element access (lvalue)
reference operator()(size_type i)
1-based element access (lvalue)
reference operator[](size_type i)
1-based element access (lvalue)
VectorTmpl< value_type > & operator=(value_type alpha)