TTTT Analysis  0.1
value.hpp
Go to the documentation of this file.
1 
42 #ifndef value_hpp
43 #define value_hpp
44 #include <iostream>
45 #include <utility>
46 #include <algorithm>
47 #include <map>
48 #include <vector>
49 #include <tuple>
50 #include <initializer_list>
51 #include <functional>
52 
56 namespace filval{
57 
64 class GenValue{
65  private:
71  std::string name;
72  protected:
79  virtual void _reset() = 0;
87  inline static std::map<const std::string, GenValue*> values;
88  public:
89  GenValue(const std::string& name)
90  :name(name) {
91  values[name] = this;
92  }
93  const std::string& get_name(){
94  return name;
95  }
96  static void reset(){
97  for (auto val : values){
98  val.second->_reset();
99  }
100  }
101  static GenValue* get_value(const std::string& name){
102  return values.at(name);
103  }
104  static void summary(){
105  std::cout << "The following values have been created: " << std::endl;
106  for (auto value : values){
107  std::cout << "\t\"" << value.first << "\" at address " << value.second << std::endl;
108  }
109  }
110 };
111 typedef std::map<std::string, GenValue*> ValueSet;
112 
113 
123 template <typename T>
124 class Value : public GenValue{
125  public:
126  Value(const std::string& name)
127  :GenValue(name){ }
130  virtual T& get_value() = 0;
131 };
132 
133 
143 template <typename T>
144 class ObservedValue : public Value<T>{
145  private:
146  T *val_ref;
147  void _reset(){ }
148  public:
149  ObservedValue(const std::string& name, T* val_ref)
150  :Value<T>(name),
151  val_ref(val_ref){ }
152  T& get_value(){
153  return *val_ref;
154  }
155 };
156 
157 
173 template <typename T>
174 class DerivedValue : public Value<T>{
175  private:
176  void _reset(){
177  value_valid = false;
178  }
179  protected:
180  T value;
181  bool value_valid;
182 
191  virtual void update_value() = 0;
192  public:
193  DerivedValue(const std::string& name)
194  :Value<T>(name),
195  value_valid(false) { }
196 
197  T& get_value(){
198  if (!value_valid){
199  update_value();
200  value_valid = true;
201  }
202  return value;
203  }
204 };
205 
206 
217 template <typename T>
218 class WrapperVector : public DerivedValue<std::vector<T> >{
219  private:
220  Value<int>* size;
221  Value<T*>* data;
222 
223  void update_value(){
224  int n = size->get_value();
225  T* data_ref = data->get_value();
226  this->value.resize(n);
227  for (int i=0; i<n; i++){
228  this->value[i] = *(data_ref+i);
229  }
230  }
231 
232  public:
233  WrapperVector(const std::string& name, Value<int>* _size, Value<T*>* _data)
235  size(_size), data(_data){ }
236 
237  WrapperVector(const std::string& name, const std::string &label_size, const std::string &label_data)
238  :WrapperVector(name,
239  dynamic_cast<Value<int>*>(GenValue::values.at(label_size)),
240  dynamic_cast<Value<T*>*>(GenValue::values.at(label_data))) { }
241 };
242 
246 template <typename T1, typename T2>
247 class Pair : public DerivedValue<std::pair<T1, T2> >{
248  protected:
249  std::pair<Value<T1>*, Value<T2>* > value_pair;
250  void update_value(){
251  this->value.first = value_pair.first->get_value();
252  this->value.second = value_pair.second->get_value();
253  }
254  public:
255  Pair(const std::string name, Value<T1> *value1, Value<T2> *value2)
257  value_pair(value1, value2){ }
258  Pair(const std::string& name, const std::string& label1, const std::string& label2)
259  :Pair(name,
260  dynamic_cast<Value<T1>*>(GenValue::values.at(label1)),
261  dynamic_cast<Value<T1>*>(GenValue::values.at(label2))){ }
262 };
263 
277 template <typename R, typename T>
278 class ZipMapFour : public DerivedValue<std::vector<R> >{
279  private:
280  std::function<R(T, T, T, T)> f;
281  Value<std::vector<T> >* v1;
282  Value<std::vector<T> >* v2;
283  Value<std::vector<T> >* v3;
284  Value<std::vector<T> >* v4;
285 
286  void update_value(){
287  std::vector<T> v1_val = v1->get_value();
288  std::vector<T> v2_val = v2->get_value();
289  std::vector<T> v3_val = v3->get_value();
290  std::vector<T> v4_val = v4->get_value();
291 
292  int n;
293  std::tie(n, std::ignore) = std::minmax({v1_val.size(), v2_val.size(), v3_val.size(), v4_val.size()});
294  this->value.resize(n);
295  for (int i=0; i<n; i++){
296  this->value[i] = f(v1_val[i], v2_val[i], v3_val[i], v4_val[i]);
297  }
298  }
299 
300  public:
301  ZipMapFour(const std::string& name, std::function<R(T, T, T, T)> f,
302  Value<std::vector<T> >* v1, Value<std::vector<T> >* v2,
303  Value<std::vector<T> >* v3, Value<std::vector<T> >* v4)
305  f(f), v1(v1), v2(v2), v3(v3), v4(v4) { }
306 
307  ZipMapFour(const std::string& name,
308  std::function<R(T, T, T, T)> f,
309  const std::string &label1, const std::string &label2,
310  const std::string &label3, const std::string &label4)
311  :ZipMapFour(name, f,
312  dynamic_cast<Value<std::vector<T> >*>(GenValue::values.at(label1)),
313  dynamic_cast<Value<std::vector<T> >*>(GenValue::values.at(label2)),
314  dynamic_cast<Value<std::vector<T> >*>(GenValue::values.at(label3)),
315  dynamic_cast<Value<std::vector<T> >*>(GenValue::values.at(label4))){ }
316 };
317 
324 template <typename T>
325 class Reduce : public DerivedValue<T>{
326  private:
327  std::function<T(std::vector<T>)> reduce;
329  void update_value(){
330  this->value = reduce(v->get_value());
331  }
332  public:
333  Reduce(const std::string& name, std::function<T(std::vector<T>)> reduce, Value<std::vector<T> >* v)
335  reduce(reduce), v(v) { }
336 
337  Reduce(const std::string& name, std::function<T(std::vector<T>)> reduce, const std::string& v_name)
338  :Reduce(name, reduce, dynamic_cast<Value<std::vector<T> >*>(GenValue::get_value(v_name))) { }
339 };
340 
344 template <typename T>
345 class Max : public Reduce<T>{
346  public:
347  Max(const std::string& name, const std::string& v_name)
348  :Reduce<T>(name, [](std::vector<T> vec){ return std::max(vec.begin(), vec.end());}, v_name) { }
349 };
350 
354 template <typename T>
355 class Min : public Reduce<T>{
356  public:
357  Min(const std::string& name, const std::string& v_name)
358  :Reduce<T>(name, [](std::vector<T> vec){ return std::min(vec.begin(), vec.end());}, v_name) { }
359 };
360 
364 template <typename T>
365 class Mean : public Reduce<T>{
366  public:
367  Mean(const std::string& name, const std::string& v_name)
368  :Reduce<T>(name,
369  [](std::vector<T> vec){
370  int n = 0; T sum = 0;
371  for (T e : vec){ n++; sum += e; }
372  return n>0 ? sum / n : 0; },
373  v_name) { }
374 };
375 
379 template <typename T>
380 class ElementOf : public Reduce<T>{
381  public:
382  ElementOf(const std::string& name, Value<int>* index, const std::string& v_name)
383  :Reduce<T>(name, [index](std::vector<T> vec){return vec[index->get_value()];}, v_name) { }
384  ElementOf(const std::string& name, int index, const std::string& v_name)
385  :Reduce<T>(name, [index](std::vector<T> vec){return vec[index];}, v_name) { }
386 };
387 
393 template <typename T>
394 class ReduceIndex : public DerivedValue<std::pair<T, int> >{
395  private:
396  std::function<std::pair<T,int>(std::vector<T>)> reduce;
398  void update_value(){
399  this->value = reduce(v->get_value());
400  }
401  public:
402  ReduceIndex(const std::string& name, std::function<std::pair<T,int>(std::vector<T>)> reduce, Value<std::vector<T> >* v)
404  reduce(reduce), v(v) { }
405 
406  ReduceIndex(const std::string& name, std::function<std::pair<T,int>(std::vector<T>)> reduce, const std::string& v_name)
407  :ReduceIndex(name, reduce, dynamic_cast<Value<std::vector<T> >*>(GenValue::get_value(v_name))) { }
408 };
409 
413 template <typename T>
414 class MaxIndex : public ReduceIndex<T>{
415  public:
416  MaxIndex(const std::string& name, const std::string& v_name)
418  [](std::vector<T> vec){
419  auto elptr = std::max_element(vec.begin(), vec.end());
420  return std::pair<T,int>(*elptr, int(elptr-vec.begin()));},
421  v_name) { }
422 };
423 
427 template <typename T>
428 class MinIndex : public ReduceIndex<T>{
429  public:
430  MinIndex(const std::string& name, const std::string& v_name)
432  [](std::vector<T> vec){
433  auto elptr = std::min_element(vec.begin(), vec.end());
434  return std::pair<T,int>(*elptr, int(elptr-vec.begin()));},
435  v_name) { }
436 };
437 
438 
442 template <typename R, typename... T>
443 class MultiFunc : public DerivedValue<R>{
444  private:
445  std::function<R(T...)> f;
446  std::tuple<T...> value_tuple;
447 
448  void update_value(){
449  this->value = f(value_tuple);
450  }
451 
452  public:
453  MultiFunc(const std::string& name, std::function<R(std::tuple<T...>)> f, T... varargs)
455  f(f),
456  value_tuple(varargs...){ }
457 };
458 
464 template <typename T>
465 class BoundValue : public DerivedValue<T>{
466  protected:
467  std::function<T()> f;
468  void update_value(){
469  this->value = f();
470  }
471  public:
472  BoundValue(const std::string& name, std::function<T()> f)
474  f(f) { }
475 };
476 
480 template <typename T>
481 class ConstantValue : public DerivedValue<T>{
482  protected:
483  T const_value;
484  void update_value(){
485  this->value = const_value;
486  }
487  public:
488  ConstantValue(const std::string& name, T const_value)
490  const_value(const_value) { }
491 };
492 }
493 #endif // value_hpp
void update_value()
Updates the internal value.
Definition: value.hpp:329
std::string name
The name of the value.
Definition: value.hpp:71
virtual T & get_value()=0
Calculate, if necessary, and return the value held by this object.
A type-agnostic value.
Definition: value.hpp:64
Calculate the mean value of a vector.
Definition: value.hpp:365
A generic, derived, value.
Definition: value.hpp:174
T & get_value()
Calculate, if necessary, and return the value held by this object.
Definition: value.hpp:152
A variadic.
Definition: value.hpp:443
void _reset()
Mark the internal value as invalid.
Definition: value.hpp:147
A Value which always returns the same value, supplied in the constructor.
Definition: value.hpp:481
The namespace containing all filval classes and functions.
Definition: container.hpp:7
A generic value owning only a function object.
Definition: value.hpp:465
A std::vector wrapper around a C-style array.
Definition: value.hpp:218
void update_value()
Updates the internal value.
Definition: value.hpp:223
void update_value()
Updates the internal value.
Definition: value.hpp:398
Creates a std::pair type from a two other Value objects.
Definition: value.hpp:247
Find and return the minimum value of a vector.
Definition: value.hpp:355
Find and return the maximum value of a vector.
Definition: value.hpp:345
Takes a set of four Value<std::vector<T> > objects and a function of four Ts and returns a std::vecto...
Definition: value.hpp:278
void update_value()
Updates the internal value.
Definition: value.hpp:484
Similar to Reduce, but returns a pair of a T and an int.
Definition: value.hpp:394
A generic value.
Definition: value.hpp:124
Find and return the maximum value of a vector and its index.
Definition: value.hpp:414
static std::map< const std::string, GenValue * > values
A static mapping containing all created Value objects.
Definition: value.hpp:87
void update_value()
Updates the internal value.
Definition: value.hpp:448
Reduce a Value of type vector<T> to just a T.
Definition: value.hpp:325
Find and return the minimum value of a vector and its index.
Definition: value.hpp:428
void update_value()
Updates the internal value.
Definition: value.hpp:250
A generic, observed, value.
Definition: value.hpp:144
void update_value()
Updates the internal value.
Definition: value.hpp:468
void _reset()
Mark the internal value as invalid.
Definition: value.hpp:176
T & get_value()
Calculate, if necessary, and return the value held by this object.
Definition: value.hpp:197
Extract the element at a specific index from a vector.
Definition: value.hpp:380
virtual void _reset()=0
Mark the internal value as invalid.
void update_value()
Updates the internal value.
Definition: value.hpp:286