Dune-Fufem 2.11-git
Loading...
Searching...
No Matches
cachedfunction.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3
4// SPDX-FileCopyrightText: Copyright © DUNE-FUFEM Project contributors, see file AUTHORS.md
5// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
6
7#ifndef DUNE_FUFEM_FUNCTIONS_CACHEDFUNCTION_HH
8#define DUNE_FUFEM_FUNCTIONS_CACHEDFUNCTION_HH
9
15#include <type_traits>
16#include <utility>
17#include <functional>
18#include <map>
19
22
23
24
25namespace Dune::Fufem {
26
27
28
29struct Less : public std::less<>
30{
31 template<class T, int n>
33 {
34 for (std::size_t i=0; i<a.size(); ++i)
35 {
36 if (a[i]<b[i])
37 return true;
38 if (a[i]>b[i])
39 return false;
40 }
41 return false;
42 }
43};
44
45
46
49template <class F, class D, class Comp = Less>
51{
52 using Domain = D;
54 using EvaluationCache = typename std::map<Domain, Range, Comp>;
55
56public:
57
58 template<class FT>
60 f_(std::forward<FT>(f))
61 {}
62
63 template<class FT>
65 f_(std::forward<FT>(f))
66 {}
67
70 template<class X>
71 Range operator()(const X& x) const
72 {
73 auto it = evalCache_.find(x);
74 if (it != evalCache_.end())
75 return it->second;
76 else
77 return (evalCache_[x] = f_(x));
78 }
79
80protected:
81 F f_;
82 mutable EvaluationCache evalCache_;
83};
84
85template<class FT, class D>
87
88
89
90} // namespace Dune::Fufem
91
92
93
94#endif // DUNE_FUFEM_FUNCTIONS_CACHEDFUNCTION_HH
95
virtual void operator()()=0
STL namespace.
Definition dunefunctionsboundaryfunctionalassembler.hh:29
constexpr size_type size() const
Definition cachedfunction.hh:30
Cached evaluation of a function.
Definition cachedfunction.hh:51
EvaluationCache evalCache_
Definition cachedfunction.hh:82
F f_
Definition cachedfunction.hh:81
CachedFunction(FT &&f)
Definition cachedfunction.hh:59
Range operator()(const X &x) const
Evaluate function.
Definition cachedfunction.hh:71
CachedFunction(FT &&f, Dune::MetaType< D >)
Definition cachedfunction.hh:64