value.hpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  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 <algorithm>
  45. #include <functional>
  46. #include <initializer_list>
  47. #include <iomanip>
  48. #include <iostream>
  49. #include <limits>
  50. #include <map>
  51. #include <sstream>
  52. #include <tuple>
  53. #include <typeindex>
  54. #include <utility>
  55. #include <vector>
  56. #include "log.hpp"
  57. /**
  58. * The namespace containing all filval classes and functions.
  59. */
  60. namespace fv{
  61. namespace detail {
  62. template<typename T, int N, bool Done, typename... TYPES>
  63. struct _HomoTuple {
  64. typedef _HomoTuple<T, N, sizeof...(TYPES)+1==N, TYPES..., T> stype;
  65. typedef typename stype::type type;
  66. };
  67. template<typename T, int N, typename... TYPES>
  68. struct _HomoTuple<T, N, true, TYPES...> {
  69. typedef std::tuple<TYPES...> type;
  70. };
  71. }
  72. template<typename T, int N>
  73. struct HomoTuple {
  74. typedef detail::_HomoTuple<T, N, N==0> stype;
  75. typedef typename stype::type type;
  76. };
  77. namespace detail {
  78. // Convert array into a tuple
  79. template<typename Array, std::size_t... I>
  80. decltype(auto) a2t_impl(const Array& a, std::index_sequence<I...>){
  81. return std::make_tuple(a[I]...);
  82. }
  83. }
  84. /**
  85. * Converts a std::array to a std::tuple.
  86. */
  87. template<typename T, std::size_t N, typename Indices = std::make_index_sequence<N>>
  88. decltype(auto) a2t(const std::array<T, N>& a){
  89. return detail::a2t_impl(a, Indices());
  90. }
  91. namespace detail {
  92. // Convert tuple into a vector
  93. template<typename R, typename Tuple, std::size_t... Is>
  94. decltype(auto) t2v_impl(const Tuple& t, std::index_sequence<Is...>){
  95. /* return std::make_tuple(a[I]...); */
  96. return std::vector<R>({std::get<Is>(t)...});
  97. }
  98. }
  99. /**
  100. * Converts a std::tuple to a std::vector.
  101. */
  102. template<typename R, typename... ArgTypes>
  103. std::vector<R> t2v(const std::tuple<ArgTypes...>& t){
  104. return detail::t2v_impl<R, std::tuple<ArgTypes...>>(t, std::index_sequence_for<ArgTypes...>{});
  105. }
  106. namespace detail {
  107. template <class F, class Tuple, std::size_t... I>
  108. constexpr decltype(auto) call_impl(F &&f, Tuple &&t, std::index_sequence<I...>){
  109. return f(std::get<I>(std::forward<Tuple>(t))...);
  110. }
  111. }
  112. /**
  113. * Call a function f with the elements of the tuple t as arguments
  114. */
  115. template <class F, class Tuple>
  116. constexpr decltype(auto) call(F &&f, Tuple &&t){
  117. return detail::call_impl(
  118. std::forward<F>(f), std::forward<Tuple>(t),
  119. std::make_index_sequence<std::tuple_size<std::decay_t<Tuple>>::value>{});
  120. }
  121. template<typename> class Function; // undefined
  122. /**
  123. * Parent class to all Function classes. Holds a class-level collection of all
  124. * created function objects.
  125. */
  126. class GenFunction {
  127. private:
  128. std::string name;
  129. std::string impl;
  130. protected:
  131. inline static bool in_reg_func=false;
  132. public:
  133. /**
  134. * Static mapping of functions from their name to the object wrapper of
  135. * the function.
  136. */
  137. inline static std::map<const std::string, GenFunction*> function_registry;
  138. GenFunction(const std::string& name, const std::string& impl)
  139. :name(name),
  140. impl(impl){ }
  141. virtual ~GenFunction() { };
  142. std::string& get_name(){
  143. return name;
  144. }
  145. std::string& get_impl(){
  146. return impl;
  147. }
  148. /**
  149. * Attempt to invoke clang-format for the purpose of printing out
  150. * nicely formatted functions to the log file. If clang-format is not
  151. * present, this function just passes through the code unmodified.
  152. */
  153. static std::string format_code(const std::string& code){
  154. std::stringstream code_out("");
  155. std::string command("echo \""+code+"\" | clang-format");
  156. char buffer[255];
  157. FILE *stream = popen(command.c_str(), "r");
  158. while (fgets(buffer, 255, stream) != NULL)
  159. code_out << buffer;
  160. if (pclose(stream) == 0)
  161. return code_out.str();
  162. else
  163. return code;
  164. }
  165. static std::string summary(){
  166. std::stringstream ss;
  167. ss << "The following functions have been registered" << std::endl;
  168. for(auto p : function_registry){
  169. if (p.second == nullptr) continue;
  170. ss << "FUNCTION::" << p.second->name << "@" << p.second << std::endl;
  171. ss << format_code(p.second->impl);
  172. }
  173. return ss.str();
  174. }
  175. template <typename T>
  176. static Function<T>& reg_func(const std::string& name, std::function<T> f, const std::string& impl){
  177. in_reg_func = true;
  178. Function<T>* func;
  179. if (GenFunction::function_registry[name] != nullptr){
  180. func = dynamic_cast<Function<T>*>(GenFunction::function_registry[name]);
  181. if (func == nullptr){
  182. ERROR("Trying to register function which has already been registered with a different type");
  183. }
  184. } else {
  185. func = new Function<T>(name, impl, f);
  186. GenFunction::function_registry[name] = func;
  187. }
  188. in_reg_func = false;
  189. return *func;
  190. }
  191. template <typename T>
  192. static Function<T>& lookup_function(const std::string& name){
  193. if (GenFunction::function_registry[name] == nullptr){
  194. CRITICAL("Function \"" << name << "\" not previously registered", -1);
  195. } else {
  196. Function<T>* func = dynamic_cast<Function<T>*>(GenFunction::function_registry[name]);
  197. if (func == nullptr){
  198. CRITICAL("Function \"" << name << "\" request and register have mismatched types", -1);
  199. }
  200. return *GenFunction::function_registry[name];
  201. }
  202. }
  203. };
  204. /**
  205. * In order to enable proper provenance tracking, and at the same time keep
  206. * the ability to embed functions into values, the Function class should be
  207. * used. It is simply a wrapper around a std::function that also has a name.
  208. * This name is used when generating the name of values that use the function.
  209. * A function name is automatically prepended with "func::" to explicitly state
  210. * that the value is the result of a computation encoded within the function
  211. * object, and not from some other Value object. Unfortunately, it is up to the
  212. * user to find where that function is defined in the source code to inspect
  213. * what it is doing. But hopefully this isn't too onerous by just using grep.
  214. */
  215. template <typename R, typename... ArgTypes>
  216. class Function<R(ArgTypes...)> : public GenFunction {
  217. private:
  218. std::function<R(ArgTypes...)> f;
  219. public:
  220. Function(const std::string& name, const std::string& impl, std::function<R(ArgTypes...)> f)
  221. :GenFunction(name, impl), f(f){
  222. if (!in_reg_func) {
  223. WARNING("Don't instantiate Function objects directly! Use GenFunction::reg_func instead.");
  224. }
  225. }
  226. Function(const std::string& name, std::function<R(ArgTypes...)> f)
  227. :Function(name, "N/A", f){ }
  228. ~Function() { }
  229. R operator()(ArgTypes ...args){
  230. return f(args...);
  231. }
  232. };
  233. #define FUNC(f) f, #f
  234. template <typename T>
  235. class Value;
  236. /**
  237. * A type-agnostic value.
  238. * It is necessary to create a type-agnostic parent class to Value so that
  239. * it is possible to handle collections of them. GenValue also provides the
  240. * rest of the type-independent interface to Value.
  241. */
  242. class GenValue;
  243. typedef std::map<std::string, GenValue*> ValueSet;
  244. class GenValue{
  245. private:
  246. /**
  247. * The name of the value.
  248. * This is used to allow for dynamic lookup of
  249. * values based on their name via GenValue::get_value.
  250. */
  251. std::string name;
  252. protected:
  253. /**
  254. * Mark the internal value as invalid. This is needed for DerivedValue
  255. * to force a recalculation of the internal value when a new
  256. * observation is loaded into memory. It is called automatically for
  257. * all GenValue objects when reset is called.
  258. */
  259. bool value_valid;
  260. void _reset(){
  261. this->value_valid = false;
  262. }
  263. /**
  264. * A static mapping containing all created Value objects.
  265. * Every value object must have a unique name, and this name is used as
  266. * a key in values to that object. This is used to enable more dynamic
  267. * creation of objects as well as avoiding the uneccesary passing of
  268. * pointers.
  269. */
  270. inline static std::map<std::pair<const std::type_index, const std::string>, GenValue*> values;
  271. /**
  272. * Composite value names are typically nested. This makes complex
  273. * values have rather unwieldy names. Therefore, one can declare
  274. * aliases which allow for more human-usable names to be used. When a
  275. * value is requested by name, an alias with that value takes precidence
  276. * over a name with that value.
  277. */
  278. inline static std::map<std::pair<const std::type_index, const std::string>, GenValue*> aliases;
  279. bool logging_enabled;
  280. public:
  281. GenValue(const std::type_index&& ti, const std::string& name, const std::string& alias)
  282. :name(name), value_valid(false), logging_enabled(false){
  283. if (alias != "")
  284. INFO("Registered value: \"" << name << "\" with alias: \"" << alias << "\"");
  285. else
  286. INFO("Registered value: \"" << name);
  287. values[std::make_pair(ti,name)] = this;
  288. if (alias != "")
  289. GenValue::alias(ti, alias, this);
  290. }
  291. const std::string& get_name(){
  292. return name;
  293. }
  294. /**
  295. * If logging is enabled for this value, this function should be
  296. * implemented to format the value to a string and place it as an INFO
  297. * entry in the log file. Useful for debugging, but may produce alot of
  298. * output.
  299. */
  300. virtual void log() = 0;
  301. static void reset(){
  302. for (auto val : values){
  303. if (val.second != nullptr){
  304. val.second->_reset();
  305. }
  306. }
  307. }
  308. template<typename T>
  309. static Value<T>* get_value(const std::string& name){
  310. const std::type_index& ti = typeid(T);
  311. auto lookup_id = std::make_pair(ti,name);
  312. if (aliases[lookup_id] != nullptr)
  313. return (Value<T>*)aliases[lookup_id];
  314. else
  315. return (Value<T>*)values[lookup_id];
  316. }
  317. static void alias(const std::type_index& ti, const std::string& name, GenValue* value){
  318. auto lookup_id = std::make_pair(ti,name);
  319. if (aliases[lookup_id] != nullptr){
  320. WARNING("WARNING: alias \"" << name << "\" overrides previous entry.");
  321. }
  322. aliases[lookup_id] = value;
  323. }
  324. template<typename T>
  325. static void alias(const std::string& name, Value<T>* value){
  326. alias(typeid(T), name, value);
  327. }
  328. static std::string summary(){
  329. std::stringstream ss;
  330. ss << "The following values have been created:" << std::endl;
  331. for (auto item : values){
  332. auto& key = item.first;
  333. auto& value = item.second;
  334. if (value == nullptr) continue;
  335. ss << "\tVALUE::\"" << key.second << "\" at address " << value << std::endl;
  336. }
  337. ss << "And these aliases:" << std::endl;
  338. for (auto item : aliases){
  339. auto& key = item.first;
  340. auto& value = item.second;
  341. std::string orig("VOID");
  342. if (value == nullptr) continue;
  343. for (auto v_item : values){
  344. auto& v_value = v_item.second;
  345. if (v_value == value){
  346. orig = v_value->get_name();
  347. break;
  348. }
  349. }
  350. ss << "\tALIAS::\"" << key.second << "\" referring to \"" << orig << "\"" << std::endl;
  351. }
  352. return ss.str();
  353. }
  354. friend std::ostream& operator<<(std::ostream& os, const GenValue& gv);
  355. };
  356. std::ostream& operator<<(std::ostream& os, GenValue& gv){
  357. os << gv.get_name();
  358. return os;
  359. }
  360. /**
  361. * A templated value.
  362. * In order to facilitate run-time creation of analysis routines, it is
  363. * necessary to have some ability to get and store *values*. Values can either
  364. * be directly taken from some original data source (i.e. ObservedValue), or
  365. * they can be a function of some other set of values (i.e. DerivedValue). They
  366. * template class T of Value<T> is the type of thing that is returned upon
  367. * calling get_value().
  368. */
  369. template <typename T>
  370. class Value : public GenValue{
  371. protected:
  372. std::function<std::string(T)> value_to_string;
  373. public:
  374. Value(const std::string& name, const std::string& alias="")
  375. :value_to_string([](T){return "";}),
  376. GenValue(typeid(T), name, alias){ }
  377. /** Calculate, if necessary, and return the value held by this object.
  378. */
  379. virtual T& get_value() = 0;
  380. void enable_logging(const std::function<std::string(T)>& value_to_string = [](T){return "";}){
  381. logging_enabled = true;
  382. this->value_to_string = value_to_string;
  383. }
  384. void disable_logging(){
  385. logging_enabled = false;
  386. }
  387. };
  388. /**
  389. * A value supplied by the dataset, not derived.
  390. * An ObservedValue is the interface to your dataset. Upon creation, an
  391. * ObservedValue is given a pointer to an object of type T. When an observation
  392. * is loaded into memory, the value at the location referenced by that pointer
  393. * must be updated with the associated data from that observation. This is the
  394. * responsibility of whatever DataSet implementation is being used. This object
  395. * then will read that data and return it when requested.
  396. */
  397. template <typename T>
  398. class ObservedValue : public Value<T>{
  399. private:
  400. T *val_ref;
  401. public:
  402. ObservedValue(const std::string& name, T* val_ref, const std::string& alias="")
  403. :Value<T>(name, alias),
  404. val_ref(val_ref){ }
  405. void log(){
  406. if(this->logging_enabled){
  407. INFO(this->get_name() << ": " << this->value_to_string(*val_ref));
  408. }
  409. }
  410. static std::string fmt_name(const std::string& name){
  411. return name;
  412. }
  413. T& get_value(){
  414. if (!this->value_valid){
  415. this->value_valid = true;
  416. this->log();
  417. }
  418. return *val_ref;
  419. }
  420. };
  421. /**
  422. * A Value derived from some other Values, not directly from the dataset.
  423. * A DerivedValue is generally defined as some function of other Value objects.
  424. * For example, a Pair is a function of two other Value objects that makes a
  425. * pair of them. Note that these other Value objects are free to be either
  426. * ObservedValues or other DerivedValues.
  427. *
  428. * It is desireable from a performance standpoint that each DerivedValue be
  429. * calculated no more than once per observation. Therefore, when a get_value is
  430. * called on a DerivedValue, it first checks whether the value that it holds is
  431. * **valid**, meaning it has already been calculated for this observation. If
  432. * so, it simply returns the value. If not, the update_value function is called
  433. * to calculate the value. and then the newly calculated value is marked as
  434. * valid and returned.
  435. */
  436. template <typename T>
  437. class DerivedValue : public Value<T>{
  438. protected:
  439. T value;
  440. /**
  441. * Updates the internal value.
  442. * This function should be overridden by any child class to do the
  443. * actual work of updating value based on whatever rules the class
  444. * chooses. Normally, this consists of geting the values from some
  445. * associated Value objects, doing some calculation on them, and
  446. * storing the result in value.
  447. */
  448. virtual void update_value() = 0;
  449. public:
  450. DerivedValue(const std::string& name, const std::string& alias="")
  451. :Value<T>(name, alias){ }
  452. void log(){
  453. if(this->logging_enabled){
  454. INFO(this->get_name() << ": " << this->value_to_string(value));
  455. }
  456. }
  457. T& get_value(){
  458. if (!this->value_valid){
  459. update_value();
  460. this->value_valid = true;
  461. this->log();
  462. }
  463. return value;
  464. }
  465. };
  466. /**
  467. * A std::vector wrapper around a C-style array.
  468. * In order to make some of the higher-level Value types easier to work with,
  469. * it is a good idea to wrap all arrays in the original data source with
  470. * std::vector objects. To do this, it is necessary to supply both a Value
  471. * object containing the array itself as well as another Value object
  472. * containing the size of that array. Currently, update_value will simply copy
  473. * the contents of the array into the interally held vector.
  474. */
  475. template <typename T>
  476. class WrapperVector : public DerivedValue<std::vector<T> >{
  477. private:
  478. Value<int>* size;
  479. Value<T*>* data;
  480. void update_value(){
  481. int n = size->get_value();
  482. T* data_ref = data->get_value();
  483. this->value.assign(data_ref, data_ref+n);
  484. }
  485. public:
  486. static std::string fmt_name(Value<int>* size, Value<T*>* data){
  487. return "wrapper_vector("+size->get_name()+","+data->get_name()+")";
  488. }
  489. WrapperVector(Value<int>* size, Value<T*>* data, const std::string& alias="")
  490. :DerivedValue<std::vector<T> >(fmt_name(size,data), alias),
  491. size(size), data(data){ }
  492. };
  493. /**
  494. * Creates a std::pair type from a two other Value objects.
  495. */
  496. template <typename T1, typename T2>
  497. class Pair : public DerivedValue<std::pair<T1, T2> >{
  498. protected:
  499. std::pair<Value<T1>*, Value<T2>* > value_pair;
  500. void update_value(){
  501. this->value.first = value_pair.first->get_value();
  502. this->value.second = value_pair.second->get_value();
  503. }
  504. public:
  505. static std::string fmt_name(Value<T1> *value1, Value<T2> *value2){
  506. return "pair("+value1->get_name()+","+value2->get_name()+")";
  507. }
  508. Pair(Value<T1> *value1, Value<T2> *value2, const std::string alias="")
  509. :DerivedValue<std::pair<T1, T2> >(fmt_name(value1, value2), alias),
  510. value_pair(value1, value2){ }
  511. };
  512. template<typename... T> class _Zip;
  513. template<>
  514. class _Zip<> {
  515. protected:
  516. int _get_size(){
  517. return std::numeric_limits<int>::max();
  518. }
  519. std::tuple<> _get_at(int){
  520. return std::make_tuple();
  521. }
  522. std::string _get_name(){
  523. return "";
  524. }
  525. public:
  526. _Zip() { }
  527. };
  528. template<typename Head, typename... Tail>
  529. class _Zip<Head, Tail...> : private _Zip<Tail...> {
  530. protected:
  531. Value<std::vector<Head>>* head;
  532. int _get_size(){
  533. int this_size = head->get_value().size();
  534. int rest_size = _Zip<Tail...>::_get_size();
  535. return std::min(this_size, rest_size);
  536. }
  537. typename std::tuple<Head,Tail...> _get_at(int idx){
  538. auto tail_tuple = _Zip<Tail...>::_get_at(idx);
  539. return std::tuple_cat(std::make_tuple(head->get_value()[idx]),tail_tuple);
  540. }
  541. std::string _get_name(){
  542. return head->get_name()+","+_Zip<Tail...>::_get_name();
  543. }
  544. public:
  545. _Zip() { }
  546. _Zip(Value<std::vector<Head>>* head, Value<std::vector<Tail>>*... tail)
  547. : _Zip<Tail...>(tail...),
  548. head(head) { }
  549. };
  550. namespace impl {
  551. std::string zip_fmt_name(){
  552. return "";
  553. }
  554. template<typename Head>
  555. std::string zip_fmt_name(Value<std::vector<Head>>* head){
  556. return head->get_name();
  557. }
  558. template<typename Head1, typename Head2, typename... Tail>
  559. std::string zip_fmt_name(Value<std::vector<Head1>>* head1, Value<std::vector<Head2>>* head2, Value<std::vector<Tail>>*... tail){
  560. return head1->get_name() + "," + zip_fmt_name<Head2, Tail...>(head2, tail...);
  561. }
  562. }
  563. /**
  564. * Zips a series of vectors together. Can be combined with Map to
  565. * yield a Value whose elements are individually a function of the
  566. * corresponding elements of the vectors that were zipped together. For those
  567. * familiar with python, it accompilishes the same thing as
  568. * @code{.py}
  569. * xs = [1,2,3,4]
  570. * ys = [10,20,30,40]
  571. * print(list(map(lambda t:t[0]+t[1],zip(xs,ys))))
  572. * @endcode
  573. * which outputs
  574. * @code
  575. * [11, 22, 33, 44]
  576. * @endcode
  577. */
  578. template <typename... ArgTypes>
  579. class Zip : public DerivedValue<std::vector<std::tuple<ArgTypes...>>>,
  580. private _Zip<ArgTypes...>{
  581. protected:
  582. void update_value(){
  583. this->value.clear();
  584. int size = _Zip<ArgTypes...>::_get_size();
  585. for(int i=0; i<size; i++){
  586. this->value.push_back(_Zip<ArgTypes...>::_get_at(i));
  587. }
  588. }
  589. public:
  590. static std::string fmt_name(Value<std::vector<ArgTypes>>*... args){
  591. return "zip("+zip_fmt_name(args...)+")";
  592. }
  593. Zip(Value<std::vector<ArgTypes>>*... args, const std::string& alias)
  594. :DerivedValue<std::vector<std::tuple<ArgTypes...>>>(fmt_name(args...), alias),
  595. _Zip<ArgTypes...>(args...) { }
  596. };
  597. template<typename> class Map; // undefined
  598. /**
  599. * Maps a function over an input vector. The input vector must be a vector of
  600. * tuples, where the the elements of the tuple match the arguments of the
  601. * function. For example if the function takes two floats as arguments, the
  602. * tuple should contain two floats. The Value object required by Map will
  603. * typically be created as a Zip.
  604. */
  605. template <typename Ret, typename ArgType>
  606. class Map<Ret(ArgType)> : public DerivedValue<std::vector<Ret>>{
  607. private:
  608. typedef Value<std::vector<ArgType>> arg_type;
  609. Function<Ret(ArgType)>& fn;
  610. arg_type* arg;
  611. void update_value(){
  612. this->value.clear();
  613. for(auto v : arg->get_value()){
  614. this->value.push_back(fn(v));
  615. }
  616. }
  617. public:
  618. static std::string fmt_name(Function<Ret(ArgType)>& fn, arg_type* arg){
  619. return "map("+fn.get_name()+":"+arg->get_name()+")";
  620. }
  621. Map(Function<Ret(ArgType)>& fn, arg_type* arg, const std::string& alias)
  622. :DerivedValue<std::vector<Ret>>(fmt_name(fn, arg), alias),
  623. fn(fn), arg(arg){ }
  624. };
  625. template<typename> class TupMap; // undefined
  626. /**
  627. * Maps a function over an input vector. The input vector must be a vector of
  628. * tuples, where the the elements of the tuple match the arguments of the
  629. * function. For example if the function takes two floats as arguments, the
  630. * tuple should contain two floats. The Value object required by Map will
  631. * typically be created as a Zip.
  632. */
  633. template <typename Ret, typename... ArgTypes>
  634. class TupMap<Ret(ArgTypes...)> : public DerivedValue<std::vector<Ret>>{
  635. private:
  636. typedef Value<std::vector<std::tuple<ArgTypes...>>> arg_type;
  637. Function<Ret(ArgTypes...)>& fn;
  638. arg_type* arg;
  639. void update_value(){
  640. this->value.clear();
  641. for(auto tup : arg->get_value()){
  642. this->value.push_back(call(fn,tup));
  643. }
  644. }
  645. public:
  646. static std::string fmt_name(Function<Ret(ArgTypes...)>& fn, arg_type* arg){
  647. return "tup_map("+fn.get_name()+":"+arg->get_name()+")";
  648. }
  649. TupMap(Function<Ret(ArgTypes...)>& fn, arg_type* arg, const std::string& alias)
  650. :DerivedValue<std::vector<Ret>>(fmt_name(fn, arg), alias),
  651. fn(fn), arg(arg){ }
  652. };
  653. template<typename... T> class _Tuple;
  654. template<>
  655. class _Tuple<> {
  656. protected:
  657. std::tuple<> _get_value(){
  658. return std::make_tuple();
  659. }
  660. public:
  661. _Tuple() { }
  662. };
  663. template<typename Head, typename... Tail>
  664. class _Tuple<Head, Tail...> : private _Tuple<Tail...> {
  665. protected:
  666. Value<Head>* head;
  667. typename std::tuple<Head,Tail...> _get_value(){
  668. auto tail_tuple = _Tuple<Tail...>::_get_value();
  669. return std::tuple_cat(std::make_tuple(head->get_value()),tail_tuple);
  670. }
  671. public:
  672. _Tuple() { }
  673. _Tuple(Value<Head>* head, Value<Tail>*... tail)
  674. : _Tuple<Tail...>(tail...),
  675. head(head) { }
  676. };
  677. namespace impl {
  678. std::string tuple_fmt_name(){
  679. return "";
  680. }
  681. template<typename Head>
  682. std::string tuple_fmt_name(Value<Head>* head){
  683. return head->get_name();
  684. }
  685. template<typename Head1, typename Head2, typename... Tail>
  686. std::string tuple_fmt_name(Value<Head1>* head1, Value<Head2>* head2, Value<Tail>*... tail){
  687. return head1->get_name() + "," + tuple_fmt_name<Head2, Tail...>(head2, tail...);
  688. }
  689. }
  690. /**
  691. * Takes a series of Value objects and bundles them together into a std::tuple
  692. * object. Typically, this is most usefull when one wants to apply a function
  693. * to a few values and store the result. This class can be used in conjunction
  694. * with Apply to achieve this.
  695. */
  696. template <typename... ArgTypes>
  697. class Tuple : public DerivedValue<std::tuple<ArgTypes...>>,
  698. private _Tuple<ArgTypes...>{
  699. protected:
  700. void update_value(){
  701. this->value = _Tuple<ArgTypes...>::_get_value();
  702. }
  703. public:
  704. static std::string fmt_name(Value<ArgTypes>*... args){
  705. return "tuple("+impl::tuple_fmt_name(args...)+")";
  706. }
  707. Tuple(Value<ArgTypes>*... args, const std::string& alias)
  708. :DerivedValue<std::tuple<ArgTypes...>>(fmt_name(args...), alias),
  709. _Tuple<ArgTypes...>(args...) { }
  710. };
  711. /**
  712. * Gets the Nth element from a tuple value
  713. */
  714. template <size_t N, typename... ArgTypes>
  715. class DeTup : public DerivedValue<typename std::tuple_element<N, std::tuple<ArgTypes...>>::type>{
  716. Value<std::tuple<ArgTypes...>> tup;
  717. protected:
  718. void update_value(){
  719. this->value = std::get<N>(tup->get_value());
  720. }
  721. public:
  722. static std::string fmt_name(Value<std::tuple<ArgTypes...>>* tup){
  723. return "detup("+tup->get_name()+")";
  724. }
  725. DeTup(Value<std::tuple<ArgTypes...>>* tup, const std::string& alias)
  726. :DerivedValue<typename std::tuple_element<N, std::tuple<ArgTypes...>>::type>(fmt_name(tup), alias),
  727. tup(tup) { }
  728. };
  729. /**
  730. * Creates a vector of extracting the Nth value from each entry in a vector of
  731. * tuples.
  732. */
  733. template <size_t N, typename... ArgTypes>
  734. class DeTupVector : public DerivedValue<std::vector<typename std::tuple_element<N, std::tuple<ArgTypes...>>::type>>{
  735. Value<std::vector<std::tuple<ArgTypes...>>>* tup;
  736. protected:
  737. void update_value(){
  738. this->value.clear();
  739. for( auto& t : tup->get_value()){
  740. this->value.push_back(std::get<N>(t));
  741. }
  742. }
  743. public:
  744. static std::string fmt_name(Value<std::vector<std::tuple<ArgTypes...>>>* tup){
  745. return "detup_vec("+tup->get_name()+")";
  746. }
  747. DeTupVector(Value<std::vector<std::tuple<ArgTypes...>>>* tup, const std::string& alias)
  748. :DerivedValue<std::vector<typename std::tuple_element<N, std::tuple<ArgTypes...>>::type>>(fmt_name(tup), alias),
  749. tup(tup) { }
  750. };
  751. template<typename> class Apply; // undefined
  752. /**
  753. * Applies a function to a tuple of values and returns a value. This will
  754. * typically be called with a Tuple object as an argument.
  755. */
  756. template <typename Ret, typename... ArgTypes>
  757. class Apply<Ret(ArgTypes...)> : public DerivedValue<Ret>{
  758. private:
  759. Function<Ret(ArgTypes...)>& fn;
  760. Value<std::tuple<ArgTypes...>>* arg;
  761. void update_value(){
  762. auto &tup = arg->get_value();
  763. this->value = call(fn, tup);
  764. }
  765. public:
  766. static std::string fmt_name(Function<Ret(ArgTypes...)>& fn, Value<std::tuple<ArgTypes...>>* arg){
  767. return "apply("+fn.get_name()+":"+arg->get_name()+")";
  768. }
  769. Apply(Function<Ret(ArgTypes...)>& fn, Value<std::tuple<ArgTypes...>>* arg, const std::string& alias)
  770. :DerivedValue<Ret>(fmt_name(fn,arg), alias),
  771. fn(fn), arg(arg){ }
  772. };
  773. /**
  774. * Returns the count of elements in the input vector passing a test function.
  775. */
  776. template<typename T>
  777. class Count : public DerivedValue<int>{
  778. private:
  779. Function<bool(T)>& selector;
  780. Value<std::vector<T> >* v;
  781. void update_value(){
  782. value = 0;
  783. for(auto val : v->get_value()){
  784. if(selector(val))
  785. value++;
  786. }
  787. }
  788. public:
  789. static std::string fmt_name(Function<bool(T)>& selector, Value<std::vector<T>>* v){
  790. return "count("+selector.get_name()+":"+v->get_name()+")";
  791. }
  792. Count(Function<bool(T)>& selector, Value<std::vector<T>>* v, const std::string alias)
  793. :DerivedValue<int>(fmt_name(selector,v), alias),
  794. selector(selector), v(v) { }
  795. };
  796. /**
  797. * Returns the elements in a vector that pass a test function.
  798. */
  799. template<typename T>
  800. class Filter : public DerivedValue<std::vector<T>>{
  801. private:
  802. Function<bool(T)>& filter;
  803. Value<std::vector<T> >* v;
  804. void update_value(){
  805. this->value.clear();
  806. for(auto val : v->get_value()){
  807. if(this->filter(val))
  808. this->value.push_back(val);
  809. }
  810. }
  811. public:
  812. static std::string fmt_name(Function<bool(T)>& filter, Value<std::vector<T>>* v){
  813. return "filter("+filter.get_name()+":"+v->get_name()+")";
  814. }
  815. Filter(Function<bool(T)>& filter, Value<std::vector<T>>* v, const std::string alias)
  816. :DerivedValue<std::vector<T>>(fmt_name(filter,v), alias),
  817. filter(filter), v(v) { }
  818. };
  819. /**
  820. * Returns the elements in a vector that pass a test function. The elements on
  821. * the vector must be tuples. Typically this will be used in conjunction with
  822. * Zip and Map.
  823. */
  824. template<typename... ArgTypes>
  825. class TupFilter : public DerivedValue<std::vector<std::tuple<ArgTypes...>>>{
  826. private:
  827. typedef std::vector<std::tuple<ArgTypes...>> value_type;
  828. Function<bool(ArgTypes...)>& filter;
  829. Value<value_type>* arg;
  830. void update_value(){
  831. this->value.clear();
  832. for(auto val : arg->get_value()){
  833. if(call(filter,val))
  834. this->value.push_back(val);
  835. }
  836. }
  837. public:
  838. static std::string fmt_name(Function<bool(ArgTypes...)>& filter, Value<value_type>* arg){
  839. return "tup_filter("+filter.get_name()+":"+arg->get_name()+")";
  840. }
  841. TupFilter(Function<bool(ArgTypes...)>& filter, Value<value_type>* arg, const std::string alias)
  842. :DerivedValue<value_type>(fmt_name(filter, arg), alias),
  843. filter(filter), arg(arg) { }
  844. };
  845. /**
  846. * Reduce a Value of type vector<T> to just a T.
  847. * This is useful functionality to model, for instance, calculating the maximum
  848. * element of a vector, or a the mean. See child classes for specific
  849. * implementations.
  850. */
  851. template <typename T>
  852. class Reduce : public DerivedValue<T>{
  853. private:
  854. Function<T(std::vector<T>)>& reduce_fn;
  855. void update_value(){
  856. this->value = reduce_fn(v->get_value());
  857. }
  858. protected:
  859. Value<std::vector<T> >* v;
  860. public:
  861. static std::string fmt_name(Function<T(std::vector<T>)>& reduce_fn, Value<std::vector<T>>* v){
  862. return "reduce("+reduce_fn.get_name()+":"+v->get_name()+")";
  863. }
  864. Reduce(Function<T(std::vector<T>)>& reduce_fn, Value<std::vector<T> >* v, const std::string alias)
  865. :DerivedValue<T>(fmt_name(reduce_fn, v), alias),
  866. reduce_fn(reduce_fn), v(v) { }
  867. };
  868. /**
  869. * Find and return the maximum value of a vector.
  870. */
  871. template <typename T>
  872. class Max : public Reduce<T>{
  873. public:
  874. static std::string fmt_name(Value<std::vector<T>>* v){
  875. return "max("+v->get_name()+")";
  876. }
  877. Max(Value<std::vector<T>>* v, const std::string alias)
  878. :Reduce<T>(GenFunction::reg_func<T(std::vector<T>)>("max",
  879. FUNC(([](std::vector<T> vec){
  880. return *std::max_element(vec.begin(), vec.end());}))),
  881. v, alias) { }
  882. };
  883. /**
  884. * Find and return the minimum value of a vector.
  885. */
  886. template <typename T>
  887. class Min : public Reduce<T>{
  888. public:
  889. static std::string fmt_name(Value<std::vector<T>>* v){
  890. return "min("+v->get_name()+")";
  891. }
  892. Min(Value<std::vector<T>>* v, const std::string alias)
  893. :Reduce<T>(GenFunction::reg_func<T(std::vector<T>)>("min",
  894. FUNC(([](std::vector<T> vec){
  895. return *std::min_element(vec.begin(), vec.end());}))),
  896. v, alias) { }
  897. };
  898. /**
  899. * Calculate the mean value of a vector.
  900. */
  901. template <typename T>
  902. class Mean : public Reduce<T>{
  903. public:
  904. static std::string fmt_name(Value<std::vector<T>>* v){
  905. return "mean("+v->get_name()+")";
  906. }
  907. Mean(Value<std::vector<T>>* v, const std::string alias)
  908. :Reduce<T>(GenFunction::reg_func<T(std::vector<T>)>("mean",
  909. FUNC(([](std::vector<T> vec){
  910. int n = 0; T sum = 0;
  911. for (T e : vec){ n++; sum += e; }
  912. return n>0 ? sum / n : 0; }))),
  913. v, alias) { }
  914. };
  915. /**
  916. * Calculate the range of the values in a vector
  917. */
  918. template <typename T>
  919. class Range : public Reduce<T>{
  920. public:
  921. static std::string fmt_name(Value<std::vector<T>>* v){
  922. return "range("+v->get_name()+")";
  923. }
  924. Range(Value<std::vector<T>>* v, const std::string alias)
  925. :Reduce<T>(GenFunction::reg_func<T(std::vector<T>)>("range",
  926. FUNC(([](std::vector<T> vec){
  927. auto minmax = std::minmax_element(vec.begin(), vec.end());
  928. return (*minmax.second) - (*minmax.first); }))),
  929. v, alias) { }
  930. };
  931. /**
  932. * Extract the element at a specific index from a vector.
  933. */
  934. template <typename T>
  935. class ElementOf : public Reduce<T>{
  936. public:
  937. ElementOf(Value<int>* index, Value<std::vector<T>>* v, const std::string alias)
  938. :Reduce<T>(GenFunction::reg_func<T(std::vector<T>)>("elementOf",
  939. FUNC(([index](std::vector<T> vec){return vec[index->get_value()];}))),
  940. v, alias) { }
  941. };
  942. /**
  943. * Similar to Reduce, but returns a pair of a T and an int.
  944. * This is useful if you need to know where in the vector exists the element
  945. * being returned.
  946. */
  947. template <typename T>
  948. class ReduceIndex : public DerivedValue<std::pair<T, int> >{
  949. private:
  950. Function<std::pair<T,int>(std::vector<T>)>& reduce;
  951. Value<std::vector<T> >* v;
  952. void update_value(){
  953. this->value = reduce(v->get_value());
  954. }
  955. public:
  956. ReduceIndex(Function<std::pair<T,int>(std::vector<T>)>& reduce, Value<std::vector<T> >* v, const std::string alias="")
  957. :DerivedValue<T>("reduceIndexWith("+reduce.get_name()+":"+v->get_name()+")", alias),
  958. reduce(reduce), v(v) { }
  959. };
  960. /**
  961. * Find and return the maximum value of a vector and its index.
  962. */
  963. template <typename T>
  964. class MaxIndex : public ReduceIndex<T>{
  965. public:
  966. MaxIndex(Value<std::vector<T>>* v, const std::string alias="")
  967. :ReduceIndex<T>(GenFunction::reg_func<T(std::vector<T>)>("maxIndex",
  968. FUNC(([](std::vector<T> vec){
  969. auto elptr = std::max_element(vec.begin(), vec.end());
  970. return std::pair<T,int>(*elptr, int(elptr-vec.begin())); }))),
  971. v, alias) { }
  972. };
  973. /**
  974. * Find and return the minimum value of a vector and its index.
  975. */
  976. template <typename T>
  977. class MinIndex : public ReduceIndex<T>{
  978. public:
  979. MinIndex(Value<std::vector<T>>* v, const std::string alias="")
  980. :ReduceIndex<T>(GenFunction::reg_func<T(std::vector<T>)>("minIndex",
  981. FUNC(([](std::vector<T> vec){
  982. auto elptr = std::min_element(vec.begin(), vec.end());
  983. return std::pair<T,int>(*elptr, int(elptr-vec.begin())); }))),
  984. v, alias) { }
  985. };
  986. /**
  987. * Find combinations of items from an input vector
  988. */
  989. template <typename T, int Size>
  990. class Combinations : public DerivedValue<std::vector<typename HomoTuple<T,Size>::type>>{
  991. private:
  992. Value<std::vector<T>>* val;
  993. typedef typename HomoTuple<T,Size>::type tuple_type;
  994. void update_value(){
  995. auto& v = val->get_value();
  996. int data_size = v.size();
  997. this->value.clear();
  998. std::vector<bool> selector(data_size);
  999. std::fill(selector.begin(), selector.begin()+std::min({Size,data_size}), true);
  1000. do {
  1001. std::array<T, Size> perm;
  1002. int idx = 0;
  1003. for (int i=0; i<data_size; i++){
  1004. if (selector[i]){
  1005. perm[idx] = v[i];
  1006. idx++;
  1007. if (idx == Size) break;
  1008. }
  1009. }
  1010. this->value.push_back(a2t(perm)); //!!!
  1011. } while(std::prev_permutation(selector.begin(), selector.end()));
  1012. }
  1013. public:
  1014. static std::string fmt_name(Value<std::vector<T>>* val){
  1015. std::stringstream ss;
  1016. ss << "combinations(" << Size << "," << val->get_name() << ")";
  1017. return ss.str();
  1018. }
  1019. Combinations(Value<std::vector<T>>* val, const std::string alias="")
  1020. :DerivedValue<std::vector<tuple_type>>(fmt_name(val), alias),
  1021. val(val) { }
  1022. };
  1023. /**
  1024. * Calculate the cartesian product of two input vectors
  1025. */
  1026. template <typename FST, typename SND>
  1027. class CartProduct : public DerivedValue<std::vector<std::tuple<FST,SND>>>{
  1028. private:
  1029. Value<std::vector<FST>>* val1;
  1030. Value<std::vector<SND>>* val2;
  1031. void update_value(){
  1032. this->value.clear();
  1033. auto& v1 = val1->get_value();
  1034. auto& v2 = val2->get_value();
  1035. for(int i=0; i<v1.size(); i++){
  1036. for(int j=0; j<v2.size(); j++){
  1037. this->value.push_back(std::tuple<FST,SND>(v1[i],v2[j]));
  1038. }
  1039. }
  1040. }
  1041. static std::string calc_name(Value<std::vector<FST>>* val1, Value<std::vector<SND>>* val2){
  1042. std::stringstream ss;
  1043. ss << "cartProduct("
  1044. << val1->get_name() << ", " << val2->get_name()
  1045. << ")";
  1046. return ss.str();
  1047. }
  1048. public:
  1049. static std::string fmt_name(Value<std::vector<FST>>* val1, Value<std::vector<SND>>* val2){
  1050. return "cartProduct("+val1->get_name()+", "+val2->get_name()+")";
  1051. }
  1052. CartProduct(Value<std::vector<FST>>* val1, Value<std::vector<SND>>* val2, const std::string alias="")
  1053. :DerivedValue<std::vector<std::tuple<FST,SND>>>(calc_name(val1, val2), alias),
  1054. val1(val1), val2(val2) { }
  1055. };
  1056. /**
  1057. * A generic value owning only a function object.
  1058. * All necessary values upon which this value depends must be bound to the
  1059. * function object.
  1060. */
  1061. template <typename T>
  1062. class BoundValue : public DerivedValue<T>{
  1063. protected:
  1064. Function<T()>& f;
  1065. void update_value(){
  1066. this->value = f();
  1067. }
  1068. public:
  1069. static std::string fmt_name(Function<T()> f){
  1070. return f.get_name()+"(<bound>)";
  1071. }
  1072. BoundValue(Function<T()>& f, const std::string alias="")
  1073. :DerivedValue<T>(fmt_name(f), alias),
  1074. f(f) { }
  1075. };
  1076. /**
  1077. * A Value of a pointer. The pointer is constant, however the data the pointer
  1078. * points to is variable.
  1079. */
  1080. template <typename T>
  1081. class PointerValue : public DerivedValue<T*>{
  1082. protected:
  1083. void update_value(){ }
  1084. public:
  1085. PointerValue(const std::string& name, T* ptr, const std::string alias="")
  1086. :DerivedValue<T*>(name, alias){
  1087. this->value = ptr;
  1088. }
  1089. };
  1090. /**
  1091. * Used for when the value is an object whose location in memory is changing,
  1092. * but one has a pointer to a pointer that points to the always updated
  1093. * location. (Mainly when reading objects such as stl containers from a root
  1094. * TTree)
  1095. */
  1096. template <typename T>
  1097. class ObjectValue : public DerivedValue<T>{
  1098. protected:
  1099. T** obj_pointer;
  1100. void update_value(){
  1101. this->value = **obj_pointer;
  1102. }
  1103. public:
  1104. ObjectValue(const std::string& name, T **ptr, const std::string alias="")
  1105. :DerivedValue<T>(name, alias),
  1106. obj_pointer(ptr){ }
  1107. };
  1108. /**
  1109. * A Value which always returns the same value, supplied in the constructor.
  1110. */
  1111. template <typename T>
  1112. class ConstantValue : public DerivedValue<T>{
  1113. protected:
  1114. void update_value(){ }
  1115. public:
  1116. static std::string fmt_name(const std::string& name){
  1117. return "const::"+name;
  1118. }
  1119. ConstantValue(const std::string& name, T const_value, const std::string alias="")
  1120. :DerivedValue<T>(fmt_name(name), alias) {
  1121. this->value = const_value;
  1122. }
  1123. };
  1124. }
  1125. #endif // value_hpp