CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
FunctClone.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_Utilities_FunctClone_h
2 #define PhysicsTools_Utilities_FunctClone_h
3 
4 #include <vector>
5 #include <algorithm>
6 #include <memory>
7 #include <cassert>
8 
9 namespace funct {
10 
11  template <typename F>
12  struct Master {
13  Master(const F& f) : f_(new F(f)), toBeUpdated_(1, true) {}
14  double operator()() const { return get(0); }
15  double operator()(double x) const { return get(0, x); }
16  void add() const { toBeUpdated_.resize(size() + 1, true); }
17  size_t size() const { return toBeUpdated_.size(); }
18  double get(size_t i) const {
19  if (toBeUpdated_[i])
20  update();
21  toBeUpdated_[i] = true;
22  return value_;
23  }
24  double get(size_t i, double x) const {
25  if (toBeUpdated_[i])
26  update(x);
27  toBeUpdated_[i] = true;
28  return value_;
29  }
30 
31  private:
32  void reset() const { std::fill(toBeUpdated_.begin(), toBeUpdated_.end(), true); }
33  void clear() const { std::fill(toBeUpdated_.begin(), toBeUpdated_.end(), false); }
34  void update() const {
35  clear();
36  value_ = (*f_)();
37  }
38  void update(double x) const {
39  clear();
40  value_ = (*f_)(x);
41  }
42  const std::shared_ptr<F> f_;
43  mutable double value_;
44  mutable std::vector<bool> toBeUpdated_;
45  };
46 
47  template <typename F>
48  struct Slave {
49  Slave(const Master<F>& master) : master_(master), id_(master.size()) {
50  assert(id_ > 0);
51  master_.add();
52  }
53  double operator()() const { return master_.get(id_); }
54  double operator()(double x) const { return master_.get(id_, x); }
55  void setId(size_t i) { id_ = i; }
56 
57  private:
59  size_t id_;
60  };
61 
62  template <typename F>
63  Master<F> master(const F& f) {
64  return Master<F>(f);
65  }
66 
67  template <typename F>
69  return Slave<F>(m);
70  }
71 } // namespace funct
72 
73 #endif
Master< F > master(const F &f)
Definition: FunctClone.h:63
std::vector< bool > toBeUpdated_
Definition: FunctClone.h:44
double operator()(double x) const
Definition: FunctClone.h:54
double operator()(double x) const
Definition: FunctClone.h:15
double operator()() const
Definition: FunctClone.h:53
size_t size() const
Definition: FunctClone.h:17
void update() const
Definition: FunctClone.h:34
void update(double x) const
Definition: FunctClone.h:38
assert(be >=bs)
void clear() const
Definition: FunctClone.h:33
double operator()() const
Definition: FunctClone.h:14
Slave< F > slave(const Master< F > &m)
Definition: FunctClone.h:68
double value_
Definition: FunctClone.h:43
void add() const
Definition: FunctClone.h:16
void reset() const
Definition: FunctClone.h:32
const std::shared_ptr< F > f_
Definition: FunctClone.h:42
void fill(std::map< std::string, TH1 * > &h, const std::string &s, double x)
const Master< F > & master_
Definition: FunctClone.h:58
void setId(size_t i)
Definition: FunctClone.h:55
Slave(const Master< F > &master)
Definition: FunctClone.h:49
Master(const F &f)
Definition: FunctClone.h:13
float x
size_t id_
Definition: FunctClone.h:59
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
tuple size
Write out results.