CMS 3D CMS Logo

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