value.hpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  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 std::invoke(std::forward<F>(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_register_function=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>& register_function(const std::string& name, std::function<T> f, const std::string& impl){
  177. in_register_function = 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_register_function = 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_register_function) {
  223. WARNING("Don't instantiate Function objects directly! Use GenFunction::register_function 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->logging_enabled){ */
  459. /* std::cout << "Getting value for " << this->get_name() << std::endl; */
  460. /* } */
  461. if (!this->value_valid){
  462. /* if (this->logging_enabled){ */
  463. /* std::cout << "Updating value for " << this->get_name() << std::endl; */
  464. /* } */
  465. update_value();
  466. this->value_valid = true;
  467. this->log();
  468. }
  469. return value;
  470. }
  471. };
  472. /**
  473. * A std::vector wrapper around a C-style array.
  474. * In order to make some of the higher-level Value types easier to work with,
  475. * it is a good idea to wrap all arrays in the original data source with
  476. * std::vector objects. To do this, it is necessary to supply both a Value
  477. * object containing the array itself as well as another Value object
  478. * containing the size of that array. Currently, update_value will simply copy
  479. * the contents of the array into the interally held vector.
  480. */
  481. template <typename T>
  482. class WrapperVector : public DerivedValue<std::vector<T> >{
  483. private:
  484. Value<int>* size;
  485. Value<T*>* data;
  486. void update_value(){
  487. int n = size->get_value();
  488. T* data_ref = data->get_value();
  489. this->value.assign(data_ref, data_ref+n);
  490. }
  491. public:
  492. static std::string fmt_name(Value<int>* size, Value<T*>* data){
  493. return "wrapper_vector("+size->get_name()+","+data->get_name()+")";
  494. }
  495. WrapperVector(Value<int>* size, Value<T*>* data, const std::string& alias="")
  496. :DerivedValue<std::vector<T> >(fmt_name(size,data), alias),
  497. size(size), data(data){ }
  498. };
  499. /**
  500. * Creates a std::pair type from a two other Value objects.
  501. */
  502. template <typename T1, typename T2>
  503. class Pair : public DerivedValue<std::pair<T1, T2> >{
  504. protected:
  505. std::pair<Value<T1>*, Value<T2>* > value_pair;
  506. void update_value(){
  507. this->value.first = value_pair.first->get_value();
  508. this->value.second = value_pair.second->get_value();
  509. }
  510. public:
  511. static std::string fmt_name(Value<T1> *value1, Value<T2> *value2){
  512. return "pair("+value1->get_name()+","+value2->get_name()+")";
  513. }
  514. Pair(Value<T1> *value1, Value<T2> *value2, const std::string alias="")
  515. :DerivedValue<std::pair<T1, T2> >(fmt_name(value1, value2), alias),
  516. value_pair(value1, value2){ }
  517. };
  518. template<typename... T> class _Zip;
  519. template<>
  520. class _Zip<> {
  521. protected:
  522. int _get_size(){
  523. return std::numeric_limits<int>::max();
  524. }
  525. std::tuple<> _get_at(int){
  526. return std::make_tuple();
  527. }
  528. std::string _get_name(){
  529. return "";
  530. }
  531. public:
  532. _Zip() { }
  533. };
  534. template<typename Head, typename... Tail>
  535. class _Zip<Head, Tail...> : private _Zip<Tail...> {
  536. protected:
  537. Value<std::vector<Head>>* head;
  538. int _get_size(){
  539. int this_size = head->get_value().size();
  540. int rest_size = _Zip<Tail...>::_get_size();
  541. return std::min(this_size, rest_size);
  542. }
  543. typename std::tuple<Head,Tail...> _get_at(int idx){
  544. auto tail_tuple = _Zip<Tail...>::_get_at(idx);
  545. return std::tuple_cat(std::make_tuple(head->get_value()[idx]),tail_tuple);
  546. }
  547. std::string _get_name(){
  548. return head->get_name()+","+_Zip<Tail...>::_get_name();
  549. }
  550. public:
  551. _Zip() { }
  552. _Zip(Value<std::vector<Head>>* head, Value<std::vector<Tail>>*... tail)
  553. : _Zip<Tail...>(tail...),
  554. head(head) { }
  555. };
  556. namespace impl {
  557. std::string zip_fmt_name(){
  558. return "";
  559. }
  560. template<typename Head>
  561. std::string zip_fmt_name(Value<std::vector<Head>>* head){
  562. return head->get_name();
  563. }
  564. template<typename Head1, typename Head2, typename... Tail>
  565. std::string zip_fmt_name(Value<std::vector<Head1>>* head1, Value<std::vector<Head2>>* head2, Value<std::vector<Tail>>*... tail){
  566. return head1->get_name() + "," + zip_fmt_name<Head2, Tail...>(head2, tail...);
  567. }
  568. }
  569. /**
  570. * Zips a series of vectors together. Can be combined with Map to
  571. * yield a Value whose elements are individually a function of the
  572. * corresponding elements of the vectors that were zipped together. For those
  573. * familiar with python, it accompilishes the same thing as
  574. * @code{.py}
  575. * xs = [1,2,3,4]
  576. * ys = [10,20,30,40]
  577. * print(list(map(lambda t:t[0]+t[1],zip(xs,ys))))
  578. * @endcode
  579. * which outputs
  580. * @code
  581. * [11, 22, 33, 44]
  582. * @endcode
  583. */
  584. template <typename... ArgTypes>
  585. class Zip : public DerivedValue<std::vector<std::tuple<ArgTypes...>>>,
  586. private _Zip<ArgTypes...>{
  587. protected:
  588. void update_value(){
  589. this->value.clear();
  590. int size = _Zip<ArgTypes...>::_get_size();
  591. for(int i=0; i<size; i++){
  592. this->value.push_back(_Zip<ArgTypes...>::_get_at(i));
  593. }
  594. }
  595. public:
  596. static std::string fmt_name(Value<std::vector<ArgTypes>>*... args){
  597. return "zip("+zip_fmt_name(args...)+")";
  598. }
  599. Zip(Value<std::vector<ArgTypes>>*... args, const std::string& alias)
  600. :DerivedValue<std::vector<std::tuple<ArgTypes...>>>(fmt_name(args...), alias),
  601. _Zip<ArgTypes...>(args...) { }
  602. };
  603. template<typename> class Map; // undefined
  604. /**
  605. * Maps a function over an input vector. The input vector must be a vector of
  606. * tuples, where the the elements of the tuple match the arguments of the
  607. * function. For example if the function takes two floats as arguments, the
  608. * tuple should contain two floats. The Value object required by Map will
  609. * typically be created as a Zip.
  610. */
  611. template <typename Ret, typename... ArgTypes>
  612. class Map<Ret(ArgTypes...)> : public DerivedValue<std::vector<Ret>>{
  613. private:
  614. typedef Value<std::vector<std::tuple<ArgTypes...>>> arg_type;
  615. Function<Ret(ArgTypes...)>& fn;
  616. arg_type* arg;
  617. void update_value(){
  618. this->value.clear();
  619. for(auto tup : arg->get_value()){
  620. this->value.push_back(call(fn,tup));
  621. }
  622. }
  623. public:
  624. static std::string fmt_name(Function<Ret(ArgTypes...)>& fn, arg_type* arg){
  625. return "map("+fn.get_name()+":"+arg->get_name()+")";
  626. }
  627. Map(Function<Ret(ArgTypes...)>& fn, arg_type* arg, const std::string& alias)
  628. :DerivedValue<std::vector<Ret>>(fmt_name(fn, arg), alias),
  629. fn(fn), arg(arg){ }
  630. };
  631. template<typename... T> class _Tuple;
  632. template<>
  633. class _Tuple<> {
  634. protected:
  635. std::tuple<> _get_value(){
  636. return std::make_tuple();
  637. }
  638. public:
  639. _Tuple() { }
  640. };
  641. template<typename Head, typename... Tail>
  642. class _Tuple<Head, Tail...> : private _Tuple<Tail...> {
  643. protected:
  644. Value<Head>* head;
  645. typename std::tuple<Head,Tail...> _get_value(){
  646. auto tail_tuple = _Tuple<Tail...>::_get_value();
  647. return std::tuple_cat(std::make_tuple(head->get_value()),tail_tuple);
  648. }
  649. public:
  650. _Tuple() { }
  651. _Tuple(Value<Head>* head, Value<Tail>*... tail)
  652. : _Tuple<Tail...>(tail...),
  653. head(head) { }
  654. };
  655. namespace impl {
  656. std::string tuple_fmt_name(){
  657. return "";
  658. }
  659. template<typename Head>
  660. std::string tuple_fmt_name(Value<Head>* head){
  661. return head->get_name();
  662. }
  663. template<typename Head1, typename Head2, typename... Tail>
  664. std::string tuple_fmt_name(Value<Head1>* head1, Value<Head2>* head2, Value<Tail>*... tail){
  665. return head1->get_name() + "," + tuple_fmt_name<Head2, Tail...>(head2, tail...);
  666. }
  667. }
  668. /**
  669. * Takes a series of Value objects and bundles them together into a std::tuple
  670. * object. Typically, this is most usefull when one wants to apply a function
  671. * to a few values and store the result. This class can be used in conjunction
  672. * with Apply to achieve this.
  673. */
  674. template <typename... ArgTypes>
  675. class Tuple : public DerivedValue<std::tuple<ArgTypes...>>,
  676. private _Tuple<ArgTypes...>{
  677. protected:
  678. void update_value(){
  679. this->value = _Tuple<ArgTypes...>::_get_value();
  680. }
  681. public:
  682. static std::string fmt_name(Value<ArgTypes>*... args){
  683. return "tuple("+impl::tuple_fmt_name(args...)+")";
  684. }
  685. Tuple(Value<ArgTypes>*... args, const std::string& alias)
  686. :DerivedValue<std::tuple<ArgTypes...>>(fmt_name(args...), alias),
  687. _Tuple<ArgTypes...>(args...) { }
  688. };
  689. /**
  690. * Gets the Nth element from a tuple value
  691. */
  692. template <size_t N, typename... ArgTypes>
  693. class DeTup : public DerivedValue<typename std::tuple_element<N, std::tuple<ArgTypes...>>::type>{
  694. Value<std::tuple<ArgTypes...>> tup;
  695. protected:
  696. void update_value(){
  697. this->value = std::get<N>(tup->get_value());
  698. }
  699. public:
  700. static std::string fmt_name(Value<std::tuple<ArgTypes...>>* tup){
  701. return "detup("+tup->get_name()+")";
  702. }
  703. DeTup(Value<std::tuple<ArgTypes...>>* tup, const std::string& alias)
  704. :DerivedValue<typename std::tuple_element<N, std::tuple<ArgTypes...>>::type>(fmt_name(tup), alias),
  705. tup(tup) { }
  706. };
  707. /**
  708. * Creates a vector of extracting the Nth value from each entry in a vector of
  709. * tuples.
  710. */
  711. template <size_t N, typename... ArgTypes>
  712. class DeTupVector : public DerivedValue<std::vector<typename std::tuple_element<N, std::tuple<ArgTypes...>>::type>>{
  713. Value<std::vector<std::tuple<ArgTypes...>>>* tup;
  714. protected:
  715. void update_value(){
  716. this->value.clear();
  717. for( auto& t : tup->get_value()){
  718. this->value.push_back(std::get<N>(t));
  719. }
  720. }
  721. public:
  722. static std::string fmt_name(Value<std::vector<std::tuple<ArgTypes...>>>* tup){
  723. return "detup_vec("+tup->get_name()+")";
  724. }
  725. DeTupVector(Value<std::vector<std::tuple<ArgTypes...>>>* tup, const std::string& alias)
  726. :DerivedValue<std::vector<typename std::tuple_element<N, std::tuple<ArgTypes...>>::type>>(fmt_name(tup), alias),
  727. tup(tup) { }
  728. };
  729. template<typename> class Apply; // undefined
  730. /**
  731. * Applies a function to a tuple of values and returns a value. This will
  732. * typically be called with a Tuple object as an argument.
  733. */
  734. template <typename Ret, typename... ArgTypes>
  735. class Apply<Ret(ArgTypes...)> : public DerivedValue<Ret>{
  736. private:
  737. Function<Ret(ArgTypes...)>& fn;
  738. Value<std::tuple<ArgTypes...>>* arg;
  739. void update_value(){
  740. auto &tup = arg->get_value();
  741. this->value = call(fn, tup);
  742. }
  743. public:
  744. static std::string fmt_name(Function<Ret(ArgTypes...)>& fn, Value<std::tuple<ArgTypes...>>* arg){
  745. return "apply("+fn.get_name()+":"+arg->get_name()+")";
  746. }
  747. Apply(Function<Ret(ArgTypes...)>& fn, Value<std::tuple<ArgTypes...>>* arg, const std::string& alias)
  748. :DerivedValue<Ret>(fmt_name(fn,arg), alias),
  749. fn(fn), arg(arg){ }
  750. };
  751. /**
  752. * Returns the count of elements in the input vector passing a test function.
  753. */
  754. template<typename T>
  755. class Count : public DerivedValue<int>{
  756. private:
  757. Function<bool(T)>& selector;
  758. Value<std::vector<T> >* v;
  759. void update_value(){
  760. value = 0;
  761. for(auto val : v->get_value()){
  762. if(selector(val))
  763. value++;
  764. }
  765. }
  766. public:
  767. static std::string fmt_name(Function<bool(T)>& selector, Value<std::vector<T>>* v){
  768. return "count("+selector.get_name()+":"+v->get_name()+")";
  769. }
  770. Count(Function<bool(T)>& selector, Value<std::vector<T>>* v, const std::string alias)
  771. :DerivedValue<int>(fmt_name(selector,v), alias),
  772. selector(selector), v(v) { }
  773. };
  774. /**
  775. * Returns the elements in a vector that pass a test function.
  776. */
  777. template<typename T>
  778. class Filter : public DerivedValue<std::vector<T>>{
  779. private:
  780. Function<bool(T)>& filter;
  781. Value<std::vector<T> >* v;
  782. void update_value(){
  783. this->value.clear();
  784. for(auto val : v->get_value()){
  785. if(this->filter(val))
  786. this->value.push_back(val);
  787. }
  788. }
  789. public:
  790. static std::string fmt_name(Function<bool(T)>& filter, Value<std::vector<T>>* v){
  791. return "filter("+filter.get_name()+":"+v->get_name()+")";
  792. }
  793. Filter(Function<bool(T)>& filter, Value<std::vector<T>>* v, const std::string alias)
  794. :DerivedValue<std::vector<T>>(fmt_name(filter,v), alias),
  795. filter(filter), v(v) { }
  796. };
  797. /**
  798. * Returns the elements in a vector that pass a test function. The elements on
  799. * the vector must be tuples. Typically this will be used in conjunction with
  800. * Zip and Map.
  801. */
  802. template<typename... ArgTypes>
  803. class TupFilter : public DerivedValue<std::vector<std::tuple<ArgTypes...>>>{
  804. private:
  805. typedef std::vector<std::tuple<ArgTypes...>> value_type;
  806. Function<bool(ArgTypes...)>& filter;
  807. Value<value_type>* arg;
  808. void update_value(){
  809. this->value.clear();
  810. for(auto val : arg->get_value()){
  811. if(call(filter,val))
  812. this->value.push_back(val);
  813. }
  814. }
  815. public:
  816. static std::string fmt_name(Function<bool(ArgTypes...)>& filter, Value<value_type>* arg){
  817. return "tup_filter("+filter.get_name()+":"+arg->get_name()+")";
  818. }
  819. TupFilter(Function<bool(ArgTypes...)>& filter, Value<value_type>* arg, const std::string alias)
  820. :DerivedValue<value_type>(fmt_name(filter, arg), alias),
  821. filter(filter), arg(arg) { }
  822. };
  823. /**
  824. * Reduce a Value of type vector<T> to just a T.
  825. * This is useful functionality to model, for instance, calculating the maximum
  826. * element of a vector, or a the mean. See child classes for specific
  827. * implementations.
  828. */
  829. template <typename T>
  830. class Reduce : public DerivedValue<T>{
  831. private:
  832. Function<T(std::vector<T>)>& reduce;
  833. void update_value(){
  834. this->value = reduce(v->get_value());
  835. }
  836. protected:
  837. Value<std::vector<T> >* v;
  838. public:
  839. Reduce(Function<T(std::vector<T>)>& reduce, Value<std::vector<T> >* v, const std::string alias)
  840. :DerivedValue<T>("reduceWith("+reduce.get_name()+":"+v->get_name()+")", alias),
  841. reduce(reduce), v(v) { }
  842. };
  843. /**
  844. * Find and return the maximum value of a vector.
  845. */
  846. template <typename T>
  847. class Max : public Reduce<T>{
  848. public:
  849. static std::string fmt_name(Value<std::vector<T>>* v){
  850. return "max("+v->get_name()+")";
  851. }
  852. Max(Value<std::vector<T>>* v, const std::string alias)
  853. :Reduce<T>(GenFunction::register_function<T(std::vector<T>)>("max",
  854. FUNC(([](std::vector<T> vec){
  855. return *std::max_element(vec.begin(), vec.end());}))),
  856. v, alias) { }
  857. };
  858. /**
  859. * Find and return the minimum value of a vector.
  860. */
  861. template <typename T>
  862. class Min : public Reduce<T>{
  863. public:
  864. static std::string fmt_name(Value<std::vector<T>>* v){
  865. return "min("+v->get_name()+")";
  866. }
  867. Min(Value<std::vector<T>>* v, const std::string alias)
  868. :Reduce<T>(GenFunction::register_function<T(std::vector<T>)>("min",
  869. FUNC(([](std::vector<T> vec){
  870. return *std::min_element(vec.begin(), vec.end());}))),
  871. v, alias) { }
  872. };
  873. /**
  874. * Calculate the mean value of a vector.
  875. */
  876. template <typename T>
  877. class Mean : public Reduce<T>{
  878. public:
  879. static std::string fmt_name(Value<std::vector<T>>* v){
  880. return "mean("+v->get_name()+")";
  881. }
  882. Mean(Value<std::vector<T>>* v, const std::string alias)
  883. :Reduce<T>(GenFunction::register_function<T(std::vector<T>)>("mean",
  884. FUNC(([](std::vector<T> vec){
  885. int n = 0; T sum = 0;
  886. for (T e : vec){ n++; sum += e; }
  887. return n>0 ? sum / n : 0; }))),
  888. v, alias) { }
  889. };
  890. /**
  891. * Calculate the range of the values in a vector
  892. */
  893. template <typename T>
  894. class Range : public Reduce<T>{
  895. public:
  896. static std::string fmt_name(Value<std::vector<T>>* v){
  897. return "range("+v->get_name()+")";
  898. }
  899. Range(Value<std::vector<T>>* v, const std::string alias)
  900. :Reduce<T>(GenFunction::register_function<T(std::vector<T>)>("range",
  901. FUNC(([](std::vector<T> vec){
  902. auto minmax = std::minmax_element(vec.begin(), vec.end());
  903. return (*minmax.second) - (*minmax.first); }))),
  904. v, alias) { }
  905. };
  906. /**
  907. * Extract the element at a specific index from a vector.
  908. */
  909. template <typename T>
  910. class ElementOf : public Reduce<T>{
  911. public:
  912. ElementOf(Value<int>* index, Value<std::vector<T>>* v, const std::string alias)
  913. :Reduce<T>(GenFunction::register_function<T(std::vector<T>)>("elementOf",
  914. FUNC(([index](std::vector<T> vec){return vec[index->get_value()];}))),
  915. v, alias) { }
  916. };
  917. /**
  918. * Similar to Reduce, but returns a pair of a T and an int.
  919. * This is useful if you need to know where in the vector exists the element
  920. * being returned.
  921. */
  922. template <typename T>
  923. class ReduceIndex : public DerivedValue<std::pair<T, int> >{
  924. private:
  925. Function<std::pair<T,int>(std::vector<T>)>& reduce;
  926. Value<std::vector<T> >* v;
  927. void update_value(){
  928. this->value = reduce(v->get_value());
  929. }
  930. public:
  931. ReduceIndex(Function<std::pair<T,int>(std::vector<T>)>& reduce, Value<std::vector<T> >* v, const std::string alias="")
  932. :DerivedValue<T>("reduceIndexWith("+reduce.get_name()+":"+v->get_name()+")", alias),
  933. reduce(reduce), v(v) { }
  934. };
  935. /**
  936. * Find and return the maximum value of a vector and its index.
  937. */
  938. template <typename T>
  939. class MaxIndex : public ReduceIndex<T>{
  940. public:
  941. MaxIndex(Value<std::vector<T>>* v, const std::string alias="")
  942. :ReduceIndex<T>(GenFunction::register_function<T(std::vector<T>)>("maxIndex",
  943. FUNC(([](std::vector<T> vec){
  944. auto elptr = std::max_element(vec.begin(), vec.end());
  945. return std::pair<T,int>(*elptr, int(elptr-vec.begin())); }))),
  946. v, alias) { }
  947. };
  948. /**
  949. * Find and return the minimum value of a vector and its index.
  950. */
  951. template <typename T>
  952. class MinIndex : public ReduceIndex<T>{
  953. public:
  954. MinIndex(Value<std::vector<T>>* v, const std::string alias="")
  955. :ReduceIndex<T>(GenFunction::register_function<T(std::vector<T>)>("minIndex",
  956. FUNC(([](std::vector<T> vec){
  957. auto elptr = std::min_element(vec.begin(), vec.end());
  958. return std::pair<T,int>(*elptr, int(elptr-vec.begin())); }))),
  959. v, alias) { }
  960. };
  961. /**
  962. * Find combinations of items from an input vector
  963. */
  964. template <typename T, int Size>
  965. class Combinations : public DerivedValue<std::vector<typename HomoTuple<T,Size>::type>>{
  966. private:
  967. Value<std::vector<T>>* val;
  968. typedef typename HomoTuple<T,Size>::type tuple_type;
  969. void update_value(){
  970. auto& v = val->get_value();
  971. int data_size = v.size();
  972. this->value.clear();
  973. std::vector<bool> selector(data_size);
  974. std::fill(selector.begin(), selector.begin()+std::min({Size,data_size}), true);
  975. do {
  976. std::array<T, Size> perm;
  977. int idx = 0;
  978. for (int i=0; i<data_size; i++){
  979. if (selector[i]){
  980. perm[idx] = v[i];
  981. idx++;
  982. if (idx == Size) break;
  983. }
  984. }
  985. this->value.push_back(a2t(perm)); //!!!
  986. } while(std::prev_permutation(selector.begin(), selector.end()));
  987. }
  988. public:
  989. static std::string fmt_name(Value<std::vector<T>>* val){
  990. std::stringstream ss;
  991. ss << "combinations(" << Size << "," << val->get_name() << ")";
  992. return ss.str();
  993. }
  994. Combinations(Value<std::vector<T>>* val, const std::string alias="")
  995. :DerivedValue<std::vector<tuple_type>>(fmt_name(val), alias),
  996. val(val) { }
  997. };
  998. /**
  999. * Calculate the cartesian product of two input vectors
  1000. */
  1001. template <typename FST, typename SND>
  1002. class CartProduct : public DerivedValue<std::vector<std::tuple<FST,SND>>>{
  1003. private:
  1004. Value<std::vector<FST>>* val1;
  1005. Value<std::vector<SND>>* val2;
  1006. void update_value(){
  1007. this->value.clear();
  1008. auto& v1 = val1->get_value();
  1009. auto& v2 = val2->get_value();
  1010. for(int i=0; i<v1.size(); i++){
  1011. for(int j=0; j<v2.size(); j++){
  1012. this->value.push_back(std::tuple<FST,SND>(v1[i],v2[j]));
  1013. }
  1014. }
  1015. }
  1016. static std::string calc_name(Value<std::vector<FST>>* val1, Value<std::vector<SND>>* val2){
  1017. std::stringstream ss;
  1018. ss << "cartProduct("
  1019. << val1->get_name() << ", " << val2->get_name()
  1020. << ")";
  1021. return ss.str();
  1022. }
  1023. public:
  1024. static std::string fmt_name(Value<std::vector<FST>>* val1, Value<std::vector<SND>>* val2){
  1025. return "cartProduct("+val1->get_name()+", "+val2->get_name()+")";
  1026. }
  1027. CartProduct(Value<std::vector<FST>>* val1, Value<std::vector<SND>>* val2, const std::string alias="")
  1028. :DerivedValue<std::vector<std::tuple<FST,SND>>>(calc_name(val1, val2), alias),
  1029. val1(val1), val2(val2) { }
  1030. };
  1031. /**
  1032. * A generic value owning only a function object.
  1033. * All necessary values upon which this value depends must be bound to the
  1034. * function object.
  1035. */
  1036. template <typename T>
  1037. class BoundValue : public DerivedValue<T>{
  1038. protected:
  1039. Function<T()>& f;
  1040. void update_value(){
  1041. this->value = f();
  1042. }
  1043. public:
  1044. BoundValue(Function<T()>& f, const std::string alias="")
  1045. :DerivedValue<T>(f.get_name()+"(<bound>)", alias),
  1046. f(f) { }
  1047. };
  1048. /**
  1049. * A Value of a pointer. The pointer is constant, however the data the pointer
  1050. * points to is variable.
  1051. */
  1052. template <typename T>
  1053. class PointerValue : public DerivedValue<T*>{
  1054. protected:
  1055. void update_value(){ }
  1056. public:
  1057. PointerValue(const std::string& name, T* ptr, const std::string alias="")
  1058. :DerivedValue<T*>(name, alias){
  1059. this->value = ptr;
  1060. }
  1061. };
  1062. /**
  1063. * A Value which always returns the same value, supplied in the constructor.
  1064. */
  1065. template <typename T>
  1066. class ConstantValue : public DerivedValue<T>{
  1067. protected:
  1068. void update_value(){ }
  1069. public:
  1070. ConstantValue(const std::string& name, T const_value, const std::string alias="")
  1071. :DerivedValue<T>("const::"+name, alias),
  1072. Value<T>::value(const_value) { }
  1073. };
  1074. }
  1075. #endif // value_hpp