value.hpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /**
  2. * @file
  3. * @author Caleb Fangmeier <caleb@fangmeier.tech>
  4. * @version 0.1
  5. *
  6. * @section LICENSE
  7. *
  8. *
  9. * MIT License
  10. *
  11. * Copyright (c) 2017 Caleb Fangmeier
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in all
  21. * copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. * SOFTWARE.
  30. *
  31. * @section DESCRIPTION
  32. * This header defines a set of generic classes that wrap up "values". In
  33. * essence, a Value<T> object is just something that contains a value of type T
  34. * and can provide it when requested. The usefulness stems from composing
  35. * values together with calculations. This enables very clear dependency
  36. * mapping and a way to know clearly how every value was arrived at. This could
  37. * be used to, for example, automatically generate commentary for plots that
  38. * explain the exect calculation used to create it. Or easily making a series
  39. * of plots contrasting different values that have been composed slightly
  40. * differently.
  41. */
  42. #ifndef value_hpp
  43. #define value_hpp
  44. #include <iomanip>
  45. #include <iostream>
  46. #include <sstream>
  47. #include <utility>
  48. #include <algorithm>
  49. #include <map>
  50. #include <vector>
  51. #include <tuple>
  52. #include <initializer_list>
  53. #include <functional>
  54. #include "log.hpp"
  55. /**
  56. * The namespace containing all filval classes and functions.
  57. */
  58. namespace fv{
  59. bool in_register_function = false;
  60. template<typename> class Function; // undefined
  61. /**
  62. * Parent class to all Function classes. Holds a class-level collection of all
  63. * created function objects.
  64. */
  65. class GenFunction {
  66. private:
  67. std::string name;
  68. std::string impl;
  69. public:
  70. /**
  71. * Static mapping of functions from their name to the object wrapper of
  72. * the function.
  73. */
  74. inline static std::map<const std::string, GenFunction*> function_registry;
  75. GenFunction(const std::string& name, const std::string& impl)
  76. :impl(impl),
  77. name(name){
  78. }
  79. virtual ~GenFunction() { };
  80. std::string& get_name(){
  81. return name;
  82. }
  83. /**
  84. * Attempt to invoke clang-format for the purpose of printing out
  85. * nicely formatted functions to the log file. If clang-format is not
  86. * present, this function just passes through the code unmodified.
  87. */
  88. static std::string format_code(const std::string& code){
  89. std::stringstream code_out("");
  90. std::string command("echo \""+code+"\" | clang-format");
  91. char buffer[255];
  92. FILE *stream = popen(command.c_str(), "r");
  93. while (fgets(buffer, 255, stream) != NULL)
  94. code_out << buffer;
  95. if (pclose(stream) == 0)
  96. return code_out.str();
  97. else
  98. return code;
  99. }
  100. static std::string summary(){
  101. std::stringstream ss;
  102. ss << "The following functions have been registered" << std::endl;
  103. for(auto p : function_registry){
  104. if (p.second == nullptr) continue;
  105. ss << "-->" << p.second->name << std::endl;
  106. ss << format_code(p.second->impl);
  107. }
  108. return ss.str();
  109. }
  110. template <typename T>
  111. static Function<T>& register_function(const std::string& name, std::function<T> f, const std::string& impl){
  112. in_register_function = true;
  113. Function<T>* func;
  114. if (GenFunction::function_registry[name] != nullptr){
  115. func = dynamic_cast<Function<T>*>(GenFunction::function_registry[name]);
  116. if (func == nullptr){
  117. ERROR("Trying to register function which has already been registerd with a different type");
  118. }
  119. } else {
  120. func = new Function<T>(name, impl, f);
  121. GenFunction::function_registry[name] = func;
  122. }
  123. in_register_function = false;
  124. return *func;
  125. }
  126. };
  127. /**
  128. * In order to enable proper provenance tracking, and at the same time keep
  129. * the ability to embed functions into values, the Function class should be
  130. * used. It is simply a wrapper around a std::function that also has a name.
  131. * This name is used when generating the name of values that use the function.
  132. * A function name is automatically prepended with "func::" to explicitly state
  133. * that the value is the result of a computation encoded within the function
  134. * object, and not from some other Value object. Unfortunately, it is up to the
  135. * user to find where that function is defined in the source code to inspect
  136. * what it is doing. But hopefully this isn't too onerous by just using grep.
  137. */
  138. template <typename R, typename... ArgTypes>
  139. class Function<R(ArgTypes...)> : public GenFunction {
  140. private:
  141. std::function<R(ArgTypes...)> f;
  142. public:
  143. Function(const std::string& name, const std::string& impl, std::function<R(ArgTypes...)> f)
  144. :GenFunction(name, impl), f(f){
  145. if (!in_register_function) {
  146. WARNING("Don't instantiate Function objects directly! Use register_function instead.");
  147. }
  148. }
  149. Function(const std::string& name, std::function<R(ArgTypes...)> f)
  150. :Function(name, "N/A", f){ }
  151. ~Function() { }
  152. R operator()(ArgTypes ...args){
  153. return f(args...);
  154. }
  155. };
  156. #define FUNC(f) f, #f
  157. /**
  158. * A type-agnostic value.
  159. * It is necessary to create a type-agnostic parent class to Value so that
  160. * it is possible to handle collections of them. GenValue also provides the
  161. * rest of the type-independent interface to Value.
  162. */
  163. class GenValue;
  164. typedef std::map<std::string, GenValue*> ValueSet;
  165. class GenValue{
  166. private:
  167. /**
  168. * The name of the value.
  169. * This is used to allow for dynamic lookup of
  170. * values based on their name via GenValue::get_value.
  171. */
  172. std::string name;
  173. protected:
  174. /**
  175. * Mark the internal value as invalid. This is needed for DerivedValue
  176. * to force a recalculation of the internal value when a new
  177. * observation is loaded into memory. It is called automatically for
  178. * all GenValue objects when reset is called.
  179. */
  180. virtual void _reset() = 0;
  181. /**
  182. * A static mapping containing all created Value objects.
  183. * Every value object must have a unique name, and this name is used as
  184. * a key in values to that object. This is used to enable more dynamic
  185. * creation of objects as well as avoiding the uneccesary passing of
  186. * pointers.
  187. */
  188. inline static std::map<const std::string, GenValue*> values;
  189. /**
  190. * Composite value names are typically nested. This makes complex
  191. * values have rather unwieldy names. Therefore, one can declare
  192. * aliases which allow for more human-usable names to be used. When a
  193. * value is requested by name, an alias with that value takes precidence
  194. * over a name with that value.
  195. */
  196. inline static std::map<const std::string, GenValue*> aliases;
  197. public:
  198. GenValue(const std::string& name, const std::string& alias)
  199. :name(name){
  200. values[name] = this;
  201. if (alias != "")
  202. GenValue::alias(alias, this);
  203. }
  204. const std::string& get_name(){
  205. return name;
  206. }
  207. static void reset(){
  208. for (auto val : values){
  209. val.second->_reset();
  210. }
  211. }
  212. static GenValue* get_value(const std::string& name){
  213. if (aliases[name] != nullptr)
  214. return aliases[name];
  215. else if (values[name] != nullptr)
  216. return values[name];
  217. else{
  218. ERROR("Could not find alias or value \"" << name << "\". I'll tell you the ones I know about." << std::endl
  219. << summary());
  220. CRITICAL("Aborting... :(", -1);
  221. }
  222. }
  223. static void alias(const std::string& name, GenValue* value){
  224. if (aliases[name] != nullptr){
  225. WARNING("WARNING: alias \"" << name << "\" overrides previous entry.");
  226. }
  227. aliases[name] = value;
  228. }
  229. static GenValue* alias(const std::string& name){
  230. if (values[name] != nullptr){
  231. WARNING("Alias \"" << name << "\" does not exist.");
  232. }
  233. return aliases[name];
  234. }
  235. static std::string summary(){
  236. std::stringstream ss;
  237. ss << "The following values have been created: " << std::endl;
  238. for (auto value : values){
  239. if (value.second == nullptr) continue;
  240. ss << "\t\"" << value.first << "\" at address " << value.second << std::endl;
  241. }
  242. ss << "And these aliases:" << std::endl;
  243. for (auto alias : aliases){
  244. std::string orig("VOID");
  245. if (alias.second == nullptr) continue;
  246. for (auto value : values){
  247. if (alias.second == value.second){
  248. orig = value.second->get_name();
  249. break;
  250. }
  251. }
  252. ss << "\t\"" << alias.first << "\" referring to \"" << orig << "\"" << std::endl;
  253. }
  254. return ss.str();
  255. }
  256. friend std::ostream& operator<<(std::ostream& os, const GenValue& gv);
  257. };
  258. std::ostream& operator<<(std::ostream& os, GenValue& gv){
  259. os << gv.get_name();
  260. return os;
  261. }
  262. /**
  263. * A generic value.
  264. * In order to facilitate run-time creation of analysis routines, it is
  265. * necessary to have some ability to get and store *values*. Values can either
  266. * be directly taken from some original data source (i.e. ObservedValue), or
  267. * they can be a function of some other set of values (i.e. DerivedValue). They
  268. * template class T of Value<T> is the type of thing that is returned upon
  269. * calling get_value().
  270. */
  271. template <typename T>
  272. class Value : public GenValue{
  273. public:
  274. Value(const std::string& name, const std::string& alias="")
  275. :GenValue(name, alias){ }
  276. /** Calculate, if necessary, and return the value held by this object.
  277. */
  278. virtual T& get_value() = 0;
  279. };
  280. /**
  281. * A generic, observed, value.
  282. * An ObservedValue is the interface to your dataset. Upon creation, an
  283. * ObservedValue is given a pointer to an object of type T. When an observation
  284. * is loaded into memory, the value at the location referenced by that pointer
  285. * must be updated with the associated data from that observation. This is the
  286. * responsibility of whatever DataSet implementation is being used. This object
  287. * then will read that data and return it when requested.
  288. */
  289. template <typename T>
  290. class ObservedValue : public Value<T>{
  291. private:
  292. T *val_ref;
  293. void _reset(){ }
  294. public:
  295. ObservedValue(const std::string& name, T* val_ref, const std::string& alias="")
  296. :Value<T>(name, alias),
  297. val_ref(val_ref){ }
  298. T& get_value(){
  299. return *val_ref;
  300. }
  301. };
  302. /**
  303. * A generic, derived, value.
  304. * A DerivedValue is generally defined as some function of other Value objects.
  305. * For example, a Pair is a function of two other Value objects that makes a
  306. * pair of them. Note that these other Value objects are free to be either
  307. * ObservedValues or other DerivedValues.
  308. *
  309. * It is desireable from a performance standpoint that each DerivedValue be
  310. * calculated no more than once per observation. Therefore, when a get_value is
  311. * called on a DerivedValue, it first checks whether the value that it holds is
  312. * **valid**, meaning it has already been calculated for this observation. If
  313. * so, it simply returns the value. If not, the update_value function is called
  314. * to calculate the value. and then the newly calculated value is marked as
  315. * valid and returned.
  316. */
  317. template <typename T>
  318. class DerivedValue : public Value<T>{
  319. private:
  320. void _reset(){
  321. value_valid = false;
  322. }
  323. protected:
  324. T value;
  325. bool value_valid;
  326. /**
  327. * Updates the internal value.
  328. * This function should be overridden by any child class to do the
  329. * actual work of updating value based on whatever rules the class
  330. * chooses. Normally, this consists of geting the values from some
  331. * associated Value objects, doing some calculation on them, and
  332. * storing the result in value.
  333. */
  334. virtual void update_value() = 0;
  335. public:
  336. DerivedValue(const std::string& name, const std::string& alias="")
  337. :Value<T>(name, alias),
  338. value_valid(false) { }
  339. T& get_value(){
  340. if (!value_valid){
  341. update_value();
  342. value_valid = true;
  343. }
  344. return value;
  345. }
  346. };
  347. /**
  348. * A std::vector wrapper around a C-style array.
  349. * In order to make some of the higher-level Value types easier to work with,
  350. * it is a good idea to wrap all arrays in the original data source with
  351. * std::vector objects. To do this, it is necessary to supply both a Value
  352. * object containing the array itself as well as another Value object
  353. * containing the size of that array. Currently, update_value will simply copy
  354. * the contents of the array into the interally held vector.
  355. * \todo avoid an unneccessary copy and set the vectors data directly.
  356. */
  357. template <typename T>
  358. class WrapperVector : public DerivedValue<std::vector<T> >{
  359. private:
  360. Value<int>* size;
  361. Value<T*>* data;
  362. void update_value(){
  363. int n = size->get_value();
  364. T* data_ref = data->get_value();
  365. this->value.resize(n);
  366. for (int i=0; i<n; i++){
  367. this->value[i] = *(data_ref+i);
  368. }
  369. }
  370. public:
  371. WrapperVector(Value<int>* size, Value<T*>* data, const std::string& alias="")
  372. :DerivedValue<std::vector<T> >("vectorOf("+size->get_name()+","+data->get_name()+")", alias),
  373. size(size), data(data){ }
  374. WrapperVector(const std::string &label_size, const std::string &label_data, const std::string& alias="")
  375. :WrapperVector(dynamic_cast<Value<int>*>(GenValue::get_value(label_size)),
  376. dynamic_cast<Value<T*>*>(GenValue::get_value(label_data)), alias) { }
  377. };
  378. /**
  379. * Creates a std::pair type from a two other Value objects.
  380. */
  381. template <typename T1, typename T2>
  382. class Pair : public DerivedValue<std::pair<T1, T2> >{
  383. protected:
  384. std::pair<Value<T1>*, Value<T2>* > value_pair;
  385. void update_value(){
  386. this->value.first = value_pair.first->get_value();
  387. this->value.second = value_pair.second->get_value();
  388. }
  389. public:
  390. Pair(Value<T1> *value1, Value<T2> *value2, const std::string alias="")
  391. :DerivedValue<std::pair<T1, T2> >("pair("+value1->get_name()+","+value2->get_name()+")", alias),
  392. value_pair(value1, value2){ }
  393. Pair(const std::string& label1, const std::string& label2, const std::string alias="")
  394. :Pair(dynamic_cast<Value<T1>*>(GenValue::get_value(label1)),
  395. dynamic_cast<Value<T1>*>(GenValue::get_value(label2)),
  396. alias){ }
  397. };
  398. /**
  399. * Takes a set of four Value<std::vector<T> > objects and a function of four Ts
  400. * and returns a std::vector<R>. This is used in, for instance, calculating the
  401. * energy of a set of particles when one has separate arrays containing pt,
  402. * eta, phi, and mass. These arrays are first wrapped up in VectorWrappers and
  403. * then passes along with a function to calculate the energy into a ZipMapFour.
  404. * The result of this calculation is a new vector containing the energy for
  405. * each particle. Note that if the input vectors are not all the same size,
  406. * calculations are only performed up to the size of the shortest.
  407. * \see MiniTreeDataSet
  408. * \todo find way to implement for arbitrary number(and possibly type) of
  409. * vector inputs.
  410. */
  411. template <typename R, typename T>
  412. class ZipMapFour : public DerivedValue<std::vector<R> >{
  413. private:
  414. Function<R(T, T, T, T)>& f;
  415. Value<std::vector<T> >* v1;
  416. Value<std::vector<T> >* v2;
  417. Value<std::vector<T> >* v3;
  418. Value<std::vector<T> >* v4;
  419. void update_value(){
  420. std::vector<T> v1_val = v1->get_value();
  421. std::vector<T> v2_val = v2->get_value();
  422. std::vector<T> v3_val = v3->get_value();
  423. std::vector<T> v4_val = v4->get_value();
  424. int n;
  425. std::tie(n, std::ignore) = std::minmax({v1_val.size(), v2_val.size(), v3_val.size(), v4_val.size()});
  426. this->value.resize(n);
  427. for (int i=0; i<n; i++){
  428. this->value[i] = f(v1_val[i], v2_val[i], v3_val[i], v4_val[i]);
  429. }
  430. }
  431. public:
  432. ZipMapFour(Function<R(T, T, T, T)>& f,
  433. Value<std::vector<T> >* v1, Value<std::vector<T> >* v2,
  434. Value<std::vector<T> >* v3, Value<std::vector<T> >* v4, const std::string alias="")
  435. :DerivedValue<std::vector<R> >("zipmap("+f.get_name()+":"+v1->get_name()+","+v2->get_name()+","+
  436. v3->get_name()+","+v4->get_name()+")", alias),
  437. f(f), v1(v1), v2(v2), v3(v3), v4(v4) { }
  438. ZipMapFour(Function<R(T, T, T, T)>& f,
  439. const std::string& label1, const std::string& label2,
  440. const std::string& label3, const std::string& label4, const std::string alias="")
  441. :ZipMapFour(f,
  442. dynamic_cast<Value<std::vector<T> >*>(GenValue::get_value(label1)),
  443. dynamic_cast<Value<std::vector<T> >*>(GenValue::get_value(label2)),
  444. dynamic_cast<Value<std::vector<T> >*>(GenValue::get_value(label3)),
  445. dynamic_cast<Value<std::vector<T> >*>(GenValue::get_value(label4)),
  446. alias){ }
  447. };
  448. /**
  449. * Reduce a Value of type vector<T> to just a T.
  450. * This is useful functionality to model, for instance, calculating the maximum
  451. * element of a vector, or a the mean. See child classes for specific
  452. * implementations.
  453. */
  454. template <typename T>
  455. class Reduce : public DerivedValue<T>{
  456. private:
  457. Function<T(std::vector<T>)>& reduce;
  458. Value<std::vector<T> >* v;
  459. void update_value(){
  460. this->value = reduce(v->get_value());
  461. }
  462. public:
  463. Reduce(Function<T(std::vector<T>)>& reduce, Value<std::vector<T> >* v, const std::string alias="")
  464. :DerivedValue<T>("reduceWith("+reduce.get_name()+":"+v->get_name()+")", alias),
  465. reduce(reduce), v(v) { }
  466. Reduce(Function<T(std::vector<T>)>& reduce, const std::string& v_name, const std::string alias="")
  467. :Reduce(reduce, dynamic_cast<Value<std::vector<T> >*>(GenValue::get_value(v_name)), alias) { }
  468. };
  469. /**
  470. * Find and return the maximum value of a vector.
  471. */
  472. template <typename T>
  473. class Max : public Reduce<T>{
  474. public:
  475. Max(const std::string& v_name, const std::string alias="")
  476. :Reduce<T>(GenFunction::register_function<T(std::vector<T>)>("max",
  477. FUNC(([](std::vector<T> vec){
  478. return *std::max_element(vec.begin(), vec.end());}))),
  479. v_name, alias) { }
  480. };
  481. /**
  482. * Find and return the minimum value of a vector.
  483. */
  484. template <typename T>
  485. class Min : public Reduce<T>{
  486. public:
  487. Min(const std::string& v_name, const std::string alias="")
  488. :Reduce<T>(new Function<T(std::vector<T>)>("min", [](std::vector<T> vec){
  489. return *std::min_element(vec.begin(), vec.end());}),
  490. v_name, alias) { }
  491. };
  492. /**
  493. * Calculate the mean value of a vector.
  494. */
  495. template <typename T>
  496. class Mean : public Reduce<T>{
  497. public:
  498. Mean(const std::string& v_name, const std::string alias="")
  499. :Reduce<T>(new Function<T(std::vector<T>)>("mean", [](std::vector<T> vec){
  500. int n = 0; T sum = 0;
  501. for (T e : vec){ n++; sum += e; }
  502. return n>0 ? sum / n : 0; }),
  503. v_name, alias) { }
  504. };
  505. /**
  506. * Extract the element at a specific index from a vector.
  507. */
  508. template <typename T>
  509. class ElementOf : public Reduce<T>{
  510. public:
  511. ElementOf(Value<int>* index, const std::string& v_name, const std::string alias="")
  512. :Reduce<T>(new Function<T(std::vector<T>)>("elementOf", [index](std::vector<T> vec){return vec[index->get_value()];}),
  513. v_name, alias) { }
  514. ElementOf(const std::string& name, int index, const std::string& v_name, const std::string alias="")
  515. :Reduce<T>(name, [index](std::vector<T> vec){return vec[index];}, v_name, alias) { }
  516. };
  517. /**
  518. * Similar to Reduce, but returns a pair of a T and an int.
  519. * This is useful if you need to know where in the vector exists the element
  520. * being returned.
  521. */
  522. template <typename T>
  523. class ReduceIndex : public DerivedValue<std::pair<T, int> >{
  524. private:
  525. Function<std::pair<T,int>(std::vector<T>)>& reduce;
  526. Value<std::vector<T> >* v;
  527. void update_value(){
  528. this->value = reduce(v->get_value());
  529. }
  530. public:
  531. ReduceIndex(Function<std::pair<T,int>(std::vector<T>)>& reduce, Value<std::vector<T> >* v, const std::string alias="")
  532. :DerivedValue<T>("reduceIndexWith("+reduce.get_name()+":"+v->get_name()+")", alias),
  533. reduce(reduce), v(v) { }
  534. ReduceIndex(Function<std::pair<T,int>(std::vector<T>)>& reduce, const std::string& v_name, const std::string alias="")
  535. :ReduceIndex(reduce, dynamic_cast<Value<std::vector<T> >*>(GenValue::get_value(v_name)), alias) { }
  536. };
  537. /**
  538. * Find and return the maximum value of a vector and its index.
  539. */
  540. template <typename T>
  541. class MaxIndex : public ReduceIndex<T>{
  542. public:
  543. MaxIndex(const std::string& v_name, const std::string alias="")
  544. :ReduceIndex<T>(GenFunction::register_function<T(std::vector<T>)>("maxIndex",
  545. FUNC(([](std::vector<T> vec){
  546. auto elptr = std::max_element(vec.begin(), vec.end());
  547. return std::pair<T,int>(*elptr, int(elptr-vec.begin())); }
  548. ))), v_name, alias) { }
  549. };
  550. /**
  551. * Find and return the minimum value of a vector and its index.
  552. */
  553. template <typename T>
  554. class MinIndex : public ReduceIndex<T>{
  555. public:
  556. MinIndex(const std::string& v_name, const std::string alias="")
  557. :ReduceIndex<T>(GenFunction::register_function<T(std::vector<T>)>("minIndex",
  558. FUNC(([](std::vector<T> vec){
  559. auto elptr = std::min_element(vec.begin(), vec.end());
  560. return std::pair<T,int>(*elptr, int(elptr-vec.begin())); }
  561. ))), v_name, alias) { }
  562. };
  563. /**
  564. * A generic value owning only a function object.
  565. * All necessary values upon which this value depends must be bound to the
  566. * function object.
  567. */
  568. template <typename T>
  569. class BoundValue : public DerivedValue<T>{
  570. protected:
  571. Function<T()>& f;
  572. void update_value(){
  573. this->value = f();
  574. }
  575. public:
  576. BoundValue(Function<T()>& f, const std::string alias="")
  577. :DerivedValue<T>(f.get_name()+"(<bound>)", alias),
  578. f(f) { }
  579. };
  580. /**
  581. * A Value of a pointer. The pointer is constant, however the data the pointer
  582. * points to is variable.
  583. */
  584. template <typename T>
  585. class PointerValue : public DerivedValue<T*>{
  586. protected:
  587. void update_value(){ }
  588. public:
  589. PointerValue(const std::string& name, T* ptr, const std::string alias="")
  590. :DerivedValue<T*>(name, alias){
  591. this->value = ptr;
  592. }
  593. };
  594. /**
  595. * A Value which always returns the same value, supplied in the constructor.
  596. */
  597. template <typename T>
  598. class ConstantValue : public DerivedValue<T>{
  599. protected:
  600. T const_value;
  601. void update_value(){
  602. this->value = const_value;
  603. }
  604. public:
  605. ConstantValue(const std::string& name, T const_value, const std::string alias="")
  606. :DerivedValue<T>("const::"+name, alias),
  607. const_value(const_value) { }
  608. };
  609. }
  610. #endif // value_hpp