KASKADE 7 development version
functor.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2002-2012 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12/*
13 * functor.hh
14 *
15 * Created on: 01.10.2012
16 * Author: lars lubkoll
17 */
18
19#ifndef FUNCTOR_HH_
20#define FUNCTOR_HH_
21
22#include <memory>
23
24// forward declaration
25namespace Dune
26{
27 template <class,class> class BCRSMatrix;
28 template <class,class> class LinearOperator;
29}
30
31namespace Kaskade
32{
33 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
34 /* * * * * * * * * * * * * * * * * * Functor * * * * * * * * * * * * * * * * */
35 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
37 template <typename ReturnType_, typename... Arguments>
39 {
40 public:
41 typedef ReturnType_ ReturnType;
42
43 virtual ~FunctorImpl(){}
44
45 virtual ReturnType operator()(Arguments...) = 0;
46
47 virtual FunctorImpl* clone() const = 0;
48 };
49
51 template <class ParentFunctor, typename Fun, typename... Arguments>
52 class FunctorHandler : public FunctorImpl<typename ParentFunctor::ReturnType, Arguments...>
53 {
54 public:
55 typedef typename ParentFunctor::ReturnType ReturnType;
56
57 FunctorHandler(const Fun& fun_) : fun(fun_){}
58
59 FunctorHandler* clone() const { return new FunctorHandler(*this); }
60
61 ReturnType operator()(Arguments... args) { return fun(args...); }
62
63 private:
64 Fun const& fun;
65 };
66
68
72 template <typename ReturnType_, typename... Arguments>
73 class Functor
74 {
75 typedef FunctorImpl<ReturnType_,Arguments...> Impl;
76 public:
77 typedef ReturnType_ ReturnType;
78
79 Functor() : impl(nullptr){}
80
81 Functor(Functor const& functor) : impl(functor.impl->clone()){}
82
83 Functor(const std::unique_ptr<Impl>& impl_) : impl(impl_->clone()){}
84
85 template <class Fun>
86 Functor(const Fun& fun) : impl(new FunctorHandler<Functor,Fun,Arguments...>(fun)){}
87
88 Functor& operator=(const Functor& functor) { impl.reset(functor.impl->clone()); return *this; }
89
90 ReturnType operator()(Arguments... args) { return (*impl)(args...); }
91
92 ReturnType operator()(Arguments... args) const { return (*impl)(args...); }
93
94 private:
95 std::unique_ptr<Impl> impl;
96 };
97
98
99 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
100 /* * * * * * * * * * * * * * * * ApplyOperator * * * * * * * * * * * * * * * */
101 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
103 template <class Scalar, typename Domain, typename Range, class Assembler>
105 {
106 public:
107 ApplyOperatorImpl(Assembler const& assembler_, bool onlyLowerTriangle_, int rbegin_, int rend_, int cbegin_, int cend_)
108 : assembler(assembler_), onlyLowerTriangle(onlyLowerTriangle_), rbegin(rbegin_), rend(rend_), cbegin(cbegin_), cend(cend_)
109 {}
110
112
113 virtual void apply(Domain const&, Range&) const = 0;
114
115 virtual void applyscaleadd(Scalar,Domain const&,Range&) const = 0;
116
117 virtual ApplyOperatorImpl* clone() const = 0;
118
119 template <class Matrix>
120 Matrix get() const { return assembler.template get<Matrix>(onlyLowerTriangle,rbegin,rend,cbegin,cend); }
121
122 template <class Matrix>
123 std::unique_ptr<Matrix> getPointer() const { return assembler.template get<Matrix>(onlyLowerTriangle,rbegin,rend,cbegin,cend); }
124
125 private:
126 Assembler const& assembler;
127 bool onlyLowerTriangle;
128 int rbegin, rend, cbegin, cend;
129 };
130
132 template <class ParentApplyOperator, typename Operator>
133 class ApplyOperatorHandler : public ApplyOperatorImpl<typename ParentApplyOperator::Scalar, typename ParentApplyOperator::Domain, typename ParentApplyOperator::Range, typename Operator::Assembler>
134 {
135 public:
136 typedef typename ParentApplyOperator::Scalar Scalar;
137 typedef typename ParentApplyOperator::Domain Domain;
138 typedef typename ParentApplyOperator::Range Range;
139 typedef typename Operator::Assembler Assembler;
141
142 ApplyOperatorHandler(const Operator& op_)
143 : Base(op_.getAssembler(), op_.getOnlyLowerTriangle(), Operator::BlockInfo::firstRow, Operator::BlockInfo::lastRow, Operator::BlockInfo::firstCol, Operator::BlockInfo::lastCol),
144 op_(op_)
145 {}
146
147 ApplyOperatorHandler* clone() const { return new ApplyOperatorHandler(*this); }
148
149 void apply(Domain const& x, Range& y) const { op_.apply(x,y); }
150
151 void applyscaleadd(Scalar alpha, Domain const& x, Range& y) const { op_.applyscaleadd(alpha,x,y); }
152
153 private:
154 Operator op_;
155 };
156
158 template <typename Scalar_, typename Domain_, typename Range_,typename Assembler_>
159 class ApplyOperator : public Dune::LinearOperator<Domain_,Range_>
160 {
161 public:
162 typedef Scalar_ Scalar;
163 typedef Domain_ Domain;
164 typedef Range_ Range;
165 typedef Assembler_ Assembler;
166
167 private:
169
170 public:
171 ApplyOperator() : impl(nullptr){}
172
173 ApplyOperator(ApplyOperator const& other) : impl(other.impl->clone()){}
174
175 ApplyOperator(const std::unique_ptr<Impl>& impl_) : impl(impl_->clone()){}
176
177 template <class Operator>
178 ApplyOperator(const Operator& op) : impl(new ApplyOperatorHandler<ApplyOperator,Operator>(op)){}
179
180 ApplyOperator& operator=(const ApplyOperator& other) { impl.reset(other.impl->clone()); }
181
182 void apply(Domain const& x, Range& y) const { impl->apply(x,y); }
183
184 void applyscaleadd(Scalar alpha, Domain const& x, Range& y) const { impl->applyscaleadd(alpha,x,y); }
185
186 template <class Matrix>
187 Matrix get() const { return impl->template get<Matrix>(); }
188
189 template <class Matrix>
190 std::unique_ptr<Matrix> getPointer() const { return impl->template getPointer<Matrix>(); }
191
192 private:
193 std::unique_ptr<Impl> impl;
194 };
195}
196
197#endif /* FUNCTOR_HH_ */
Implementation of FunctorImpl. Allows the use of function pointers as well as functors.
Definition: functor.hh:134
ParentApplyOperator::Range Range
Definition: functor.hh:138
ParentApplyOperator::Domain Domain
Definition: functor.hh:137
void applyscaleadd(Scalar alpha, Domain const &x, Range &y) const
Definition: functor.hh:151
void apply(Domain const &x, Range &y) const
Definition: functor.hh:149
ApplyOperatorImpl< Scalar, Domain, Range, Assembler > Base
Definition: functor.hh:140
ApplyOperatorHandler(const Operator &op_)
Definition: functor.hh:142
ParentApplyOperator::Scalar Scalar
Definition: functor.hh:136
Operator::Assembler Assembler
Definition: functor.hh:139
ApplyOperatorHandler * clone() const
Definition: functor.hh:147
ApplyOperator(const Operator &op)
Definition: functor.hh:178
std::unique_ptr< Matrix > getPointer() const
Definition: functor.hh:190
ApplyOperator(ApplyOperator const &other)
Definition: functor.hh:173
void apply(Domain const &x, Range &y) const
Definition: functor.hh:182
Assembler_ Assembler
Definition: functor.hh:165
ApplyOperator(const std::unique_ptr< Impl > &impl_)
Definition: functor.hh:175
Matrix get() const
Definition: functor.hh:187
ApplyOperator & operator=(const ApplyOperator &other)
Definition: functor.hh:180
void applyscaleadd(Scalar alpha, Domain const &x, Range &y) const
Definition: functor.hh:184
Store operator, independent of operator type.
Definition: functor.hh:105
std::unique_ptr< Matrix > getPointer() const
Definition: functor.hh:123
virtual void applyscaleadd(Scalar, Domain const &, Range &) const =0
ApplyOperatorImpl(Assembler const &assembler_, bool onlyLowerTriangle_, int rbegin_, int rend_, int cbegin_, int cend_)
Definition: functor.hh:107
virtual ApplyOperatorImpl * clone() const =0
Matrix get() const
Definition: functor.hh:120
virtual ~ApplyOperatorImpl()
Definition: functor.hh:111
virtual void apply(Domain const &, Range &) const =0
Implementation of FunctorImpl. Allows the use of function pointers as well as functors.
Definition: functor.hh:53
ReturnType operator()(Arguments... args)
Definition: functor.hh:61
ParentFunctor::ReturnType ReturnType
Definition: functor.hh:55
FunctorHandler * clone() const
Definition: functor.hh:59
FunctorHandler(const Fun &fun_)
Definition: functor.hh:57
Functor with first class semantics.
Definition: functor.hh:74
ReturnType operator()(Arguments... args) const
Definition: functor.hh:92
ReturnType operator()(Arguments... args)
Definition: functor.hh:90
Functor(const std::unique_ptr< Impl > &impl_)
Definition: functor.hh:83
Functor & operator=(const Functor &functor)
Definition: functor.hh:88
ReturnType_ ReturnType
Definition: functor.hh:77
Functor(const Fun &fun)
Definition: functor.hh:86
Functor(Functor const &functor)
Definition: functor.hh:81
Abstract functor implementation base class.
Definition: functor.hh:39
virtual ReturnType operator()(Arguments...)=0
virtual FunctorImpl * clone() const =0
virtual ~FunctorImpl()
Definition: functor.hh:43
ReturnType_ ReturnType
Definition: functor.hh:41