Skip to content
Snippets Groups Projects
Commit 53d437bb authored by Carsten Gräser's avatar Carsten Gräser
Browse files

[cleanup] Implement MultiTypeBlock* assignment using Hybrid::forEach

The implementation in MultiTypeBlockMatrix would be even
simpler, if it would provide a static size() method, because
we could then use a simple Hybrid::forEach over its entries
instead of one over all indices.
parent 5d182e8f
No related branches found
No related tags found
No related merge requests found
......@@ -7,6 +7,8 @@
#include <iostream>
#include <tuple>
#include <dune/common/hybridutilities.hh>
#include "istlexception.hh"
// forward declaration
......@@ -69,45 +71,6 @@ namespace Dune {
//make MultiTypeBlockVector_Ident known (for MultiTypeBlockMatrix_Ident)
template<int count, typename T1, typename T2>
class MultiTypeBlockVector_Ident;
/**
@brief Set a MultiTypeBlockMatrix to some specific scalar value
This class is used by the MultiTypeBlockMatrix class' internal assignment operator.
Whenever a vector is assigned a scalar value, each block element
has to provide operator= for the right side. Example:
\code
typedef MultiTypeBlockVector<int,int,int> CVect;
MultiTypeBlockMatrix<CVect,CVect> CMat;
CMat = 3; //sets all 3x2 integer elements to 3
\endcode
*/
template<int rowcount, typename T1, typename T2>
class MultiTypeBlockMatrix_Ident {
public:
/**
* equalize two matrix' element
* note: uses MultiTypeBlockVector_Ident to equalize each row (which is of MultiTypeBlockVector type)
*/
static void equalize(T1& a, const T2& b) {
MultiTypeBlockVector_Ident<T1::M(),T1,T2>::equalize(a,b); //rows are cvectors
MultiTypeBlockMatrix_Ident<rowcount-1,T1,T2>::equalize(a,b); //iterate over rows
}
};
//recursion end for rowcount=0
template<typename T1, typename T2>
class MultiTypeBlockMatrix_Ident<0,T1,T2> {
public:
static void equalize (T1&, const T2&)
{}
};
/**
@brief Matrix-vector multiplication
......@@ -259,7 +222,16 @@ namespace Dune {
* assignment operator
*/
template<typename T>
void operator= (const T& newval) {MultiTypeBlockMatrix_Ident<N(),type,T>::equalize(*this, newval); }
void operator= (const T& newval) {
using namespace Dune::Hybrid;
auto size = index_constant<1+sizeof...(Args)>();
// Since Dune::Hybrid::size(MultiTypeBlockMatrix) is not implemented,
// we cannot use a plain forEach(*this, ...). This could be achived,
// e.g., by implementing a static size() method.
forEach(integralRange(size), [&](auto&& i) {
(*this)[i] = newval;
});
}
/** \brief y = A x
*/
......
......@@ -9,6 +9,7 @@
#include <dune/common/dotproduct.hh>
#include <dune/common/ftraits.hh>
#include <dune/common/hybridutilities.hh>
#include "istlexception.hh"
......@@ -64,38 +65,6 @@ namespace Dune {
/**
@brief Set a MultiTypeBlockVector to some specific value
This class is used by the MultiTypeBlockVector class' internal = operator.
Whenever a vector is assigned to a value, each vector element
has to provide the = operator for the right side. Example:
\code
MultiTypeBlockVector<int,int,int> CVect;
CVect = 3; //sets all integer elements to 3
\endcode
*/
template<int count, typename T1, typename T2>
class MultiTypeBlockVector_Ident {
public:
/**
* equalize two vectors' element (index is template parameter count)
* note: each MultiTypeBlockVector element has to provide the = operator with type T2
*/
static void equalize(T1& a, const T2& b) {
std::get<count-1>(a) = b; //equalize current elements
MultiTypeBlockVector_Ident<count-1,T1,T2>::equalize(a,b); //next elements
}
};
template<typename T1, typename T2> //recursion end (count=0)
class MultiTypeBlockVector_Ident<0,T1,T2> {public: static void equalize (T1&, const T2&) {} };
/**
@brief Add/subtract second vector to/from the first (v1 += v2)
......@@ -386,7 +355,11 @@ namespace Dune {
/** \brief Assignment operator
*/
template<typename T>
void operator= (const T& newval) {MultiTypeBlockVector_Ident<sizeof...(Args),type,T>::equalize(*this, newval); }
void operator= (const T& newval) {
Dune::Hybrid::forEach(*this, [&](auto&& entry) {
entry = newval;
});
}
/**
* operator for MultiTypeBlockVector += MultiTypeBlockVector operations
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment