4 #ifndef DUNE_ISTL_BVECTOR_HH 5 #define DUNE_ISTL_BVECTOR_HH 12 #include <dune/common/promotiontraits.hh> 13 #include <dune/common/dotproduct.hh> 14 #include <dune/common/ftraits.hh> 28 template<
class B,
class A=std::allocator<B> >
47 template<
class B,
class A=std::allocator<B> >
48 class block_vector_unmanaged :
public base_array_unmanaged<B,A>
55 typedef typename B::field_type field_type;
61 typedef A allocator_type;
64 typedef typename A::size_type size_type;
67 typedef typename base_array_unmanaged<B,A>::iterator Iterator;
70 typedef typename base_array_unmanaged<B,A>::const_iterator ConstIterator;
79 typedef const B& const_reference;
84 block_vector_unmanaged& operator= (
const field_type& k)
86 for (size_type i=0; i<this->n; i++)
93 block_vector_unmanaged& operator+= (
const block_vector_unmanaged& y)
95 #ifdef DUNE_ISTL_WITH_CHECKING 96 if (this->n!=y.N()) DUNE_THROW(
ISTLError,
"vector size mismatch");
98 for (size_type i=0; i<this->n; ++i) (*
this)[i] += y[i];
103 block_vector_unmanaged& operator-= (
const block_vector_unmanaged& y)
105 #ifdef DUNE_ISTL_WITH_CHECKING 106 if (this->n!=y.N()) DUNE_THROW(
ISTLError,
"vector size mismatch");
108 for (size_type i=0; i<this->n; ++i) (*
this)[i] -= y[i];
113 block_vector_unmanaged& operator*= (
const field_type& k)
115 for (size_type i=0; i<this->n; ++i) (*
this)[i] *= k;
120 block_vector_unmanaged& operator/= (
const field_type& k)
122 for (size_type i=0; i<this->n; ++i) (*
this)[i] /= k;
127 block_vector_unmanaged& axpy (
const field_type& a,
const block_vector_unmanaged& y)
129 #ifdef DUNE_ISTL_WITH_CHECKING 130 if (this->n!=y.N()) DUNE_THROW(
ISTLError,
"vector size mismatch");
132 for (size_type i=0; i<this->n; ++i) (*
this)[i].axpy(a,y[i]);
144 template<
class OtherB,
class OtherA>
145 typename PromotionTraits<field_type,typename OtherB::field_type>::PromotedType operator* (
const block_vector_unmanaged<OtherB,OtherA>& y)
const 147 typedef typename PromotionTraits<field_type,typename OtherB::field_type>::PromotedType PromotedType;
149 #ifdef DUNE_ISTL_WITH_CHECKING 150 if (this->n!=y.N()) DUNE_THROW(
ISTLError,
"vector size mismatch");
152 for (size_type i=0; i<this->n; ++i) {
153 sum += PromotedType(((*
this)[i])*y[i]);
165 template<
class OtherB,
class OtherA>
166 typename PromotionTraits<field_type,typename OtherB::field_type>::PromotedType dot(
const block_vector_unmanaged<OtherB,OtherA>& y)
const 168 typedef typename PromotionTraits<field_type,typename OtherB::field_type>::PromotedType PromotedType;
170 #ifdef DUNE_ISTL_WITH_CHECKING 171 if (this->n!=y.N()) DUNE_THROW(
ISTLError,
"vector size mismatch");
173 for (size_type i=0; i<this->n; ++i) sum += ((*
this)[i]).dot(y[i]);
180 typename FieldTraits<field_type>::real_type one_norm ()
const 182 typename FieldTraits<field_type>::real_type sum=0;
183 for (size_type i=0; i<this->n; ++i) sum += (*
this)[i].one_norm();
188 typename FieldTraits<field_type>::real_type one_norm_real ()
const 190 typename FieldTraits<field_type>::real_type sum=0;
191 for (size_type i=0; i<this->n; ++i) sum += (*
this)[i].one_norm_real();
196 typename FieldTraits<field_type>::real_type two_norm ()
const 198 typename FieldTraits<field_type>::real_type sum=0;
199 for (size_type i=0; i<this->n; ++i) sum += (*
this)[i].two_norm2();
204 typename FieldTraits<field_type>::real_type two_norm2 ()
const 206 typename FieldTraits<field_type>::real_type sum=0;
207 for (size_type i=0; i<this->n; ++i) sum += (*
this)[i].two_norm2();
212 template <
typename ft = field_type,
213 typename std::enable_if<!has_nan<ft>::value,
int>::type = 0>
214 typename FieldTraits<ft>::real_type infinity_norm()
const {
215 using real_type =
typename FieldTraits<ft>::real_type;
219 for (
auto const &x : *
this) {
220 real_type
const a = x.infinity_norm();
227 template <
typename ft = field_type,
228 typename std::enable_if<!has_nan<ft>::value,
int>::type = 0>
229 typename FieldTraits<ft>::real_type infinity_norm_real()
const {
230 using real_type =
typename FieldTraits<ft>::real_type;
234 for (
auto const &x : *
this) {
235 real_type
const a = x.infinity_norm_real();
242 template <
typename ft = field_type,
243 typename std::enable_if<has_nan<ft>::value,
int>::type = 0>
244 typename FieldTraits<ft>::real_type infinity_norm()
const {
245 using real_type =
typename FieldTraits<ft>::real_type;
250 for (
auto const &x : *
this) {
251 real_type
const a = x.infinity_norm();
260 template <
typename ft = field_type,
261 typename std::enable_if<has_nan<ft>::value,
int>::type = 0>
262 typename FieldTraits<ft>::real_type infinity_norm_real()
const {
263 using real_type =
typename FieldTraits<ft>::real_type;
268 for (
auto const &x : *
this) {
269 real_type
const a = x.infinity_norm_real();
286 size_type dim ()
const 289 for (size_type i=0; i<this->n; i++)
290 d += (*
this)[i].dim();
296 block_vector_unmanaged () : base_array_unmanaged<B,A>()
315 template<
class B,
class A=std::allocator<B> >
329 typedef A allocator_type;
337 blocklevel = B::blocklevel+1
341 typedef typename Imp::block_vector_unmanaged<B,A>::Iterator
Iterator;
344 typedef typename Imp::block_vector_unmanaged<B,A>::ConstIterator
ConstIterator;
359 this->p = this->allocator_.allocate(capacity_);
361 new(this->p)B[capacity_];
374 capacity_ = l.size();
376 this->p = this->allocator_.allocate(capacity_);
378 new(this->p)B[capacity_];
380 std::copy_n(l.begin(), l.size(), this->p);
404 static_assert(std::numeric_limits<S>::is_integer,
405 "capacity must be an unsigned integral type (be aware, that this constructor does not set the default value!)" );
406 size_type capacity = _capacity;
408 if(this->n > capacity)
411 capacity_ = capacity;
414 this->p = this->allocator_.allocate(capacity_);
415 new (this->p)B[capacity_];
441 void reserve(size_type capacity,
bool copyOldValues=
true)
443 if(capacity >= Imp::block_vector_unmanaged<B,A>::N() && capacity != capacity_) {
449 this->p = this->allocator_.allocate(capacity);
450 new (this->p)B[capacity];
457 for(size_type i=0; i < Imp::block_vector_unmanaged<B,A>::N(); ++i, ++from, ++to)
465 this->allocator_.deallocate(pold,capacity_);
474 capacity_ = capacity;
503 void resize(size_type size,
bool copyOldValues=
true)
505 if (size > Imp::block_vector_unmanaged<B,A>::N())
507 this->reserve(size, copyOldValues);
516 Imp::block_vector_unmanaged<B,A>(a)
523 this->p = this->allocator_.allocate(capacity_);
524 new (this->p)B[capacity_];
532 for (size_type i=0; i<this->n; i++) this->p[i]=a.p[i];
542 this->allocator_.deallocate(this->p,capacity_);
558 this->allocator_.deallocate(this->p,capacity_);
562 this->p = this->allocator_.allocate(capacity_);
563 new (this->p)B[capacity_];
572 for (size_type i=0; i<this->n; i++)
582 (
static_cast<Imp::block_vector_unmanaged<B,A>&
>(*this)) = k;
587 template<
class OtherAlloc>
590 resize(other.size());
591 for(std::size_t i=0; i<other.size(); ++i)
592 (*
this)[i] = other[i];
608 template<
class B,
class A>
618 template<
class K,
class A>
620 std::ostream& operator<< (std::ostream& s, const BlockVector<K, A>& v)
624 for (size_type i=0; i<v.size(); i++)
625 s << v[i] << std::endl;
652 template<
class B,
class A>
654 template<
class B,
class A=std::allocator<B> >
663 typedef typename B::field_type field_type;
666 typedef B block_type;
669 typedef A allocator_type;
672 typedef typename A::size_type size_type;
677 blocklevel = B::blocklevel+1
681 typedef typename Imp::block_vector_unmanaged<B,A>::Iterator Iterator;
684 typedef typename Imp::block_vector_unmanaged<B,A>::ConstIterator ConstIterator;
710 #ifdef DUNE_ISTL_WITH_CHECKING 711 if (this->n!=a.N()) DUNE_THROW(
ISTLError,
"vector size mismatch");
717 for (size_type i=0; i<this->n; i++) this->p[i]=a.p[i];
725 (
static_cast<Imp::block_vector_unmanaged<B,A>&
>(*this)) = k;
733 void set (size_type _n, B* _p)
740 void setsize (size_type _n)
776 template<
class B,
class A=std::allocator<B> >
777 class compressed_block_vector_unmanaged :
public compressed_base_array_unmanaged<B,A>
784 typedef typename B::field_type field_type;
787 typedef B block_type;
790 typedef A allocator_type;
793 typedef typename compressed_base_array_unmanaged<B,A>::iterator Iterator;
796 typedef typename compressed_base_array_unmanaged<B,A>::const_iterator ConstIterator;
799 typedef typename A::size_type size_type;
803 compressed_block_vector_unmanaged& operator= (
const field_type& k)
805 for (size_type i=0; i<this->n; i++)
815 compressed_block_vector_unmanaged& operator+= (
const V& y)
817 #ifdef DUNE_ISTL_WITH_CHECKING 818 if (!includesindexset(y)) DUNE_THROW(
ISTLError,
"index set mismatch");
820 for (size_type i=0; i<y.n; ++i) this->
operator[](y.j[i]) += y.p[i];
826 compressed_block_vector_unmanaged& operator-= (
const V& y)
828 #ifdef DUNE_ISTL_WITH_CHECKING 829 if (!includesindexset(y)) DUNE_THROW(
ISTLError,
"index set mismatch");
831 for (size_type i=0; i<y.n; ++i) this->
operator[](y.j[i]) -= y.p[i];
837 compressed_block_vector_unmanaged& axpy (
const field_type& a,
const V& y)
839 #ifdef DUNE_ISTL_WITH_CHECKING 840 if (!includesindexset(y)) DUNE_THROW(
ISTLError,
"index set mismatch");
842 for (size_type i=0; i<y.n; ++i) (this->
operator[](y.j[i])).axpy(a,y.p[i]);
847 compressed_block_vector_unmanaged& operator*= (
const field_type& k)
849 for (size_type i=0; i<this->n; ++i) (this->p)[i] *= k;
854 compressed_block_vector_unmanaged& operator/= (
const field_type& k)
856 for (size_type i=0; i<this->n; ++i) (this->p)[i] /= k;
864 field_type operator* (
const compressed_block_vector_unmanaged& y)
const 866 #ifdef DUNE_ISTL_WITH_CHECKING 867 if (!includesindexset(y) || !y.includesindexset(*
this) )
868 DUNE_THROW(
ISTLError,
"index set mismatch");
871 for (size_type i=0; i<this->n; ++i)
872 sum += (this->p)[i] * y[(this->j)[i]];
880 typename FieldTraits<field_type>::real_type one_norm ()
const 882 typename FieldTraits<field_type>::real_type sum=0;
883 for (size_type i=0; i<this->n; ++i) sum += (this->p)[i].one_norm();
888 typename FieldTraits<field_type>::real_type one_norm_real ()
const 890 typename FieldTraits<field_type>::real_type sum=0;
891 for (size_type i=0; i<this->n; ++i) sum += (this->p)[i].one_norm_real();
896 typename FieldTraits<field_type>::real_type two_norm ()
const 898 typename FieldTraits<field_type>::real_type sum=0;
899 for (size_type i=0; i<this->n; ++i) sum += (this->p)[i].two_norm2();
904 typename FieldTraits<field_type>::real_type two_norm2 ()
const 906 typename FieldTraits<field_type>::real_type sum=0;
907 for (size_type i=0; i<this->n; ++i) sum += (this->p)[i].two_norm2();
912 template <
typename ft = field_type,
913 typename std::enable_if<!has_nan<ft>::value,
int>::type = 0>
914 typename FieldTraits<ft>::real_type infinity_norm()
const {
915 using real_type =
typename FieldTraits<ft>::real_type;
919 for (
auto const &x : *
this) {
920 real_type
const a = x.infinity_norm();
927 template <
typename ft = field_type,
928 typename std::enable_if<!has_nan<ft>::value,
int>::type = 0>
929 typename FieldTraits<ft>::real_type infinity_norm_real()
const {
930 using real_type =
typename FieldTraits<ft>::real_type;
934 for (
auto const &x : *
this) {
935 real_type
const a = x.infinity_norm_real();
942 template <
typename ft = field_type,
943 typename std::enable_if<has_nan<ft>::value,
int>::type = 0>
944 typename FieldTraits<ft>::real_type infinity_norm()
const {
945 using real_type =
typename FieldTraits<ft>::real_type;
950 for (
auto const &x : *
this) {
951 real_type
const a = x.infinity_norm();
960 template <
typename ft = field_type,
961 typename std::enable_if<has_nan<ft>::value,
int>::type = 0>
962 typename FieldTraits<ft>::real_type infinity_norm_real()
const {
963 using real_type =
typename FieldTraits<ft>::real_type;
968 for (
auto const &x : *
this) {
969 real_type
const a = x.infinity_norm_real();
986 size_type dim ()
const 989 for (size_type i=0; i<this->n; i++)
990 d += (this->p)[i].dim();
996 compressed_block_vector_unmanaged () : compressed_base_array_unmanaged<B,A>()
1001 bool includesindexset (
const V& y)
1003 typename V::ConstIterator e=this->end();
1004 for (size_type i=0; i<y.n; i++)
1005 if (this->find(y.j[i])==e)
1030 template<
class B,
class A=std::allocator<B> >
1031 class CompressedBlockVectorWindow :
public compressed_block_vector_unmanaged<B,A>
1038 typedef typename B::field_type field_type;
1041 typedef B block_type;
1044 typedef A allocator_type;
1047 typedef typename A::size_type size_type;
1052 blocklevel = B::blocklevel+1
1056 typedef typename compressed_block_vector_unmanaged<B,A>::Iterator Iterator;
1059 typedef typename compressed_block_vector_unmanaged<B,A>::ConstIterator ConstIterator;
1064 CompressedBlockVectorWindow () : compressed_block_vector_unmanaged<B,A>()
1068 CompressedBlockVectorWindow (B* _p, size_type* _j, size_type _n)
1076 CompressedBlockVectorWindow (
const CompressedBlockVectorWindow& a)
1084 CompressedBlockVectorWindow& operator= (
const CompressedBlockVectorWindow& a)
1087 #ifdef DUNE_ISTL_WITH_CHECKING 1088 if (this->n!=a.N()) DUNE_THROW(
ISTLError,
"vector size mismatch");
1094 for (size_type i=0; i<this->n; i++) this->p[i]=a.p[i];
1095 for (size_type i=0; i<this->n; i++) this->j[i]=a.j[i];
1101 CompressedBlockVectorWindow& operator= (
const field_type& k)
1103 (
static_cast<compressed_block_vector_unmanaged<B,A>&
>(*this)) = k;
1111 void set (size_type _n, B* _p, size_type* _j)
1119 void setsize (size_type _n)
1131 void setindexptr (size_type* _j)
1143 size_type* getindexptr ()
1149 const B* getptr ()
const 1155 const size_type* getindexptr ()
const 1160 size_type getsize ()
const A::size_type size_type
The type for the index access.
Definition: bvector.hh:332
Imp::block_vector_unmanaged< B, A >::Iterator Iterator
make iterators available as types
Definition: bvector.hh:341
BlockVector(size_type _n, S _capacity)
Make vector with _n components but preallocating capacity components.
Definition: bvector.hh:402
Definition: allocator.hh:7
BlockVector()
makes empty vector
Definition: bvector.hh:349
void reserve(size_type capacity, bool copyOldValues=true)
Reserve space.
Definition: bvector.hh:441
B::field_type field_type
export the type representing the field
Definition: bvector.hh:323
BlockVector(size_type _n)
make vector with _n components
Definition: bvector.hh:354
size_type capacity_
Definition: bvector.hh:597
size_type capacity() const
Get the capacity of the vector.
Definition: bvector.hh:484
~BlockVector()
free dynamic memory
Definition: bvector.hh:536
FieldTraits< B >::field_type field_type
Definition: bvector.hh:611
BlockVector(std::initializer_list< B > const &l)
Construct from a std::initializer_list.
Definition: bvector.hh:371
derive error class from the base class in common
Definition: istlexception.hh:16
Imp::block_vector_unmanaged< B, A >::ConstIterator ConstIterator
make iterators available as types
Definition: bvector.hh:344
Definition: bvector.hh:29
Implements several basic array containers.
FieldTraits< B >::real_type real_type
Definition: bvector.hh:612
A vector of blocks with memory management.
Definition: bvector.hh:316
BlockVector(const BlockVector &a)
copy constructor
Definition: bvector.hh:515
B block_type
export the type representing the components
Definition: bvector.hh:326
void resize(size_type size, bool copyOldValues=true)
Resize the vector.
Definition: bvector.hh:503