CMS 3D CMS Logo

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 {
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
mps_fire.i
i
Definition: mps_fire.py:428
funct::Slave
Definition: FunctClone.h:48
funct::Master::f_
const std::shared_ptr< F > f_
Definition: FunctClone.h:42
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
cms::cuda::assert
assert(be >=bs)
funct::Slave::operator()
double operator()(double x) const
Definition: FunctClone.h:54
funct::Slave::operator()
double operator()() const
Definition: FunctClone.h:53
funct::Master
Definition: FunctClone.h:12
funct::slave
Slave< F > slave(const Master< F > &m)
Definition: FunctClone.h:68
F
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
funct::Slave::setId
void setId(size_t i)
Definition: FunctClone.h:55
funct::Slave::Slave
Slave(const Master< F > &master)
Definition: FunctClone.h:49
funct::master
Master< F > master(const F &f)
Definition: FunctClone.h:63
funct::Master::Master
Master(const F &f)
Definition: FunctClone.h:13
funct::Master::update
void update(double x) const
Definition: FunctClone.h:38
funct::m
m
Definition: Factorize.h:45
funct::Master::operator()
double operator()() const
Definition: FunctClone.h:14
funct::Master::reset
void reset() const
Definition: FunctClone.h:32
funct::Master::operator()
double operator()(double x) const
Definition: FunctClone.h:15
funct::Master::toBeUpdated_
std::vector< bool > toBeUpdated_
Definition: FunctClone.h:44
funct::Master::get
double get(size_t i, double x) const
Definition: FunctClone.h:24
funct::true
true
Definition: Factorize.h:173
ntuplemaker.fill
fill
Definition: ntuplemaker.py:304
funct::Master::clear
void clear() const
Definition: FunctClone.h:33
funct::Slave::id_
size_t id_
Definition: FunctClone.h:59
funct::Slave::master_
const Master< F > & master_
Definition: FunctClone.h:58
funct::Master::add
void add() const
Definition: FunctClone.h:16
funct::Master::size
size_t size() const
Definition: FunctClone.h:17
funct::Master::get
double get(size_t i) const
Definition: FunctClone.h:18
funct
Definition: Abs.h:5
funct::Master::value_
double value_
Definition: FunctClone.h:43
funct::Master::update
void update() const
Definition: FunctClone.h:34
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443