Class documentation of Concepts

Loading...
Searching...
No Matches
flyweight.hh
Go to the documentation of this file.
1
5#ifndef CONCEPTS_FLYWEIGHT
6#define CONCEPTS_FLYWEIGHT
7
8#include <unordered_map>
11
12namespace concepts {
13
15 public:
16 class Hash {
17 public:
18 std::size_t operator()(const ShapeFunction1DOrder& order) const {
19 return ( (order.polynomialDegree_ << 10 | order.nQuadraturePoints_) << 10) | order.quadratureType_;
20 }
21 };
22
26 polynomialDegree_(polynomialDegree), nQuadraturePoints_(nQuadraturePoints),
27 quadratureType_(quadratureType)
28 {}
29
30 // bool operator<(const Order& other) const {
31 // if (static_cast<const Orders<1>&>(*this) == static_cast<const Orders<1>&>(other))
32 // return quadratureType_ < other.quadratureType_;
33 // else
34 // return static_cast<const Orders<1>&>(*this) < static_cast<const Orders<1>&>(other);
35 // }
36
37 bool operator==(const ShapeFunction1DOrder& other) const {
38 return
39 polynomialDegree_ == other.polynomialDegree_ &&
40 nQuadraturePoints_ == other.nQuadraturePoints_ &&
41 quadratureType_ == other.quadratureType_;
42 }
43
44 private:
45 uint polynomialDegree_;
46 uint nQuadraturePoints_;
47 concepts::intRule quadratureType_;
48 };
49
51 public:
52 class Hash {
53 public:
54 std::size_t operator()(const QuadratureOrder& order) const {
55 return (order.nQuadraturePoints_ << 10) | order.quadratureType_;
56 }
57 };
58
61 nQuadraturePoints_(nQuadraturePoints),
62 quadratureType_(quadratureType)
63 {}
64
65
66 bool operator==(const QuadratureOrder& other) const {
67 return
68 nQuadraturePoints_ == other.nQuadraturePoints_ &&
69 quadratureType_ == other.quadratureType_;
70 }
71
72 private:
73 uint nQuadraturePoints_;
74 concepts::intRule quadratureType_;
75 };
76
77 template<class KeyT>
78 class Hash;
79
80 template<>
82 {
83 typedef typename ShapeFunction1DOrder::Hash type;
84 };
85
86 template<>
88 {
89 typedef typename QuadratureOrder::Hash type;
90 };
91
93 template<class KeyT, class ValueT>
94 class Flyweight {
95 public:
96 Flyweight() {}
97
98 std::shared_ptr<const ValueT> get(const KeyT& key) {
99 auto it = container_.find(key);
100 if (it != container_.end() )
101 return it->lock();
102 else
103 return std::make_shared<const ValueT>();
104 }
105
106 void insert(const KeyT& key, std::shared_ptr<const ValueT>& value) {
107 auto it = container_.find(key);
108 if (it != container_.end() )
110 container_[key] = value;
111 }
112
115
116 protected:
117 std::unordered_map<KeyT,std::weak_ptr<const ValueT>, Hash<KeyT> > container_;
118 };
119
121 template <class FunctionT>
122 std::shared_ptr<const FunctionT> makeShapeFunction(const concepts::QuadratureRule1d& quadratureRule,
124 {
125
128
130 if (dynamic_cast<const QuadratureRule1dGaussJacobi*>(&quadratureRule) != nullptr)
131 ruleType = intRule::GAUSS_JACOBI;
132 else if (dynamic_cast<const QuadratureRule1dGaussLobatto*>(&quadratureRule) != nullptr)
133 ruleType = intRule::GAUSS_LOBATTO;
134 else if (dynamic_cast<const QuadratureRule1dTrapeze*>(&quadratureRule) != nullptr)
135 ruleType = intRule::TRAPEZE;
136
138 quadratureRule.n(),
139 ruleType);
140
141 if(auto function = flyweight_.lock(order))
142 return function;
143 else {
144 function = std::make_shared<FunctionT>(polynomialDegree,
146
147 flyweight_.insert(order,function);
148 return function;
149 }
150 };
151
153 template <class FunctionT>
154 std::shared_ptr<const FunctionT> makeQuadrature(const uint nQuadraturePoints,
155 const intRule quadratureType) {
156
157 using Order = QuadratureOrder;
159
161
162 if(auto quadrature = flyweight_quadrature_.lock(order))
163 return quadrature;
164 else {
165
167 if (quadratureType == intRule::GAUSS_JACOBI)
168 {
169 quadrature = std::make_shared<QuadratureRule1dGaussJacobi>(nQuadraturePoints);
170 }
171 else if (quadratureType == intRule::GAUSS_LOBATTO)
172 {
173 quadrature = std::make_shared<QuadratureRule1dGaussLobatto>(nQuadraturePoints);
174 }
175 else if (quadratureType == intRule::TRAPEZE)
176 {
177 quadrature = std::make_shared<QuadratureRule1dTrapeze>(nQuadraturePoints);
178 }
179 else
180 {
181 throw conceptsException(concepts::MissingFeature("quadrature rule not defined"));
182 }
183
184 flyweight_quadrature_.insert(order,quadrature);
185 return quadrature;
186 }
187 }
188
190 template <class FunctionT>
191 std::shared_ptr<const FunctionT> makeAdaptiveQuadrature(const uint nQuadraturePoints,
192 const intRule quadratureType) {
193
194 using Order = QuadratureOrder;
195
197
199
200 if(auto quadrature = flyweight_quadrature_.lock(order))
201 return quadrature;
202 else {
204 if (quadratureType == intRule::GAUSS_JACOBI)
205 {
206 quadrature = std::make_shared<AdaptiveQuadratureRule1d<intRule::GAUSS_JACOBI> >(nQuadraturePoints);
207 }
208 else if (quadratureType == intRule::GAUSS_LOBATTO)
209 {
210 quadrature = std::make_shared<AdaptiveQuadratureRule1d<intRule::GAUSS_LOBATTO> >(nQuadraturePoints);
211 }
212 else if (quadratureType == intRule::TRAPEZE)
213 {
214 quadrature = std::make_shared<AdaptiveQuadratureRule1d<intRule::TRAPEZE> >(nQuadraturePoints);
215 }
216 else
217 {
218 throw conceptsException(concepts::MissingFeature("quadrature rule not defined"));
219 }
220
221 flyweight_quadrature_.insert(order,quadrature);
222 return quadrature;
223 }
224 }
225
226}
227
228#endif
229
#define conceptsException(exc)
flyweight memory manager
Definition flyweight.hh:94
std::unordered_map< KeyT, std::weak_ptr< const ValueT >, Hash< KeyT > > container_
Definition flyweight.hh:117
#define conceptsAssert(cond, exc)
std::shared_ptr< const FunctionT > makeQuadrature(const uint nQuadraturePoints, const intRule quadratureType)
factory function encapsulating the memory manager
Definition flyweight.hh:154
std::shared_ptr< const FunctionT > makeAdaptiveQuadrature(const uint nQuadraturePoints, const intRule quadratureType)
factory function encapsulating the memory manager
Definition flyweight.hh:191
std::shared_ptr< const FunctionT > makeShapeFunction(const concepts::QuadratureRule1d &quadratureRule, const uint polynomialDegree)
factory function encapsulating the memory manager
Definition flyweight.hh:122
Set< F > makeSet(uint n, const F &first,...)
Definition set.hh:320
intRule
Types of integration rules to choose from.
Definition defines.hh:13