53 #include <initializer_list> 63 template <
typename F,
typename TUPLE,
bool Done,
int Total,
int... N>
66 static auto call(F& f, TUPLE && t)
68 return call_impl<F, TUPLE, Total == 1 +
sizeof...(N), Total, N...,
sizeof...(N)>
::call(f, std::forward<TUPLE>(t));
72 template <
typename F,
typename TUPLE,
int Total,
int... N>
73 struct call_impl<F, TUPLE, true, Total, N...>
75 static auto call(F& f, TUPLE && t)
77 return f(std::get<N>(std::forward<TUPLE>(t))...);
85 template <
typename F,
typename TUPLE>
86 auto call(F& f, TUPLE && t)
88 typedef typename std::decay<TUPLE>::type ttype;
89 return call_impl<F, TUPLE, 0 == std::tuple_size<ttype>::value, std::tuple_size<ttype>::value>
::call(f, std::forward<TUPLE>(t));
92 template<
typename>
class Function;
103 inline static bool in_register_function=
false;
112 GenFunction(
const std::string& name,
const std::string& impl)
118 std::string& get_name(){
128 std::stringstream code_out(
"");
129 std::string command(
"echo \""+code+
"\" | clang-format");
131 FILE *stream = popen(command.c_str(),
"r");
132 while (fgets(buffer, 255, stream) != NULL)
134 if (pclose(stream) == 0)
135 return code_out.str();
140 static std::string summary(){
141 std::stringstream ss;
142 ss <<
"The following functions have been registered" << std::endl;
143 for(
auto p : function_registry){
144 if (p.second ==
nullptr)
continue;
145 ss <<
"-->" << p.second->name <<
"@" << p.second << std::endl;
146 ss << format_code(p.second->impl);
151 template <
typename T>
152 static Function<T>& register_function(
const std::string& name, std::function<T> f,
const std::string& impl){
153 in_register_function =
true;
157 if (func ==
nullptr){
158 ERROR(
"Trying to register function which has already been registered with a different type");
161 func =
new Function<T>(name, impl, f);
164 in_register_function =
false;
181 template <
typename R,
typename... ArgTypes>
184 std::function<R(ArgTypes...)> f;
187 Function(
const std::string& name,
const std::string& impl, std::function<R(ArgTypes...)> f)
189 if (!in_register_function) {
190 WARNING(
"Don't instantiate Function objects directly! Use GenFunction::register_function instead.");
193 Function(
const std::string& name, std::function<R(ArgTypes...)> f)
194 :Function(name,
"N/A", f){ }
197 R operator()(ArgTypes ...args){
204 #define FUNC(f) f, #f 213 typedef std::map<std::string, GenValue*> ValueSet;
230 virtual void _reset() = 0;
238 inline static std::map<const std::string, GenValue*> values;
246 inline static std::map<const std::string, GenValue*> aliases;
249 GenValue(
const std::string& name,
const std::string& alias)
253 GenValue::alias(alias,
this);
256 const std::string& get_name(){
260 void set_name(
const std::string& new_name){
261 values[name] =
nullptr;
267 for (
auto val : values){
268 if (val.second !=
nullptr){
269 val.second->_reset();
274 static GenValue* get_value(
const std::string& name){
275 if (aliases[name] !=
nullptr)
276 return aliases[name];
277 else if (values[name] !=
nullptr)
280 ERROR(
"Could not find alias or value \"" << name <<
"\". I'll tell you the ones I know about." << std::endl
282 CRITICAL(
"Aborting... :(",-1);
286 static void alias(
const std::string& name, GenValue* value){
287 if (aliases[name] !=
nullptr){
288 WARNING(
"WARNING: alias \"" << name <<
"\" overrides previous entry.");
290 aliases[name] = value;
293 static GenValue* alias(
const std::string& name){
294 if (values[name] !=
nullptr){
295 WARNING(
"Alias \"" << name <<
"\" does not exist.");
297 return aliases[name];
300 static std::string summary(){
301 std::stringstream ss;
302 ss <<
"The following values have been created: " << std::endl;
303 for (
auto value : values){
304 if (value.second ==
nullptr)
continue;
305 ss <<
"\t\"" << value.first <<
"\" at address " << value.second << std::endl;
307 ss <<
"And these aliases:" << std::endl;
308 for (
auto alias : aliases){
309 std::string orig(
"VOID");
310 if (alias.second ==
nullptr)
continue;
311 for (
auto value : values){
312 if (alias.second == value.second){
313 orig = value.second->get_name();
317 ss <<
"\t\"" << alias.first <<
"\" referring to \"" << orig <<
"\"" << std::endl;
321 friend std::ostream& operator<<(std::ostream& os,
const GenValue& gv);
323 std::ostream& operator<<(std::ostream& os, GenValue& gv){
338 template <
typename T>
341 Value(
const std::string& name,
const std::string& alias=
"")
342 :GenValue(name, alias){ }
345 virtual T& get_value() = 0;
358 template <
typename T>
365 ObservedValue(
const std::string& name, T* val_ref,
const std::string& alias=
"")
389 template <
typename T>
407 virtual void update_value() = 0;
409 DerivedValue(
const std::string& name,
const std::string& alias=
"")
411 value_valid(
false) { }
432 template <
typename T>
441 this->value.assign(data_ref, data_ref+n);
447 size(size), data(data){ }
453 template <
typename T1,
typename T2>
456 std::pair<Value<T1>*,
Value<T2>* > value_pair;
458 this->value.first = value_pair.first->get_value();
459 this->value.second = value_pair.second->get_value();
465 value_pair(value1, value2){ }
468 template<
typename... T>
class _Zip;
474 return std::numeric_limits<int>::max();
477 std::tuple<> _get_at(
int idx){
478 return std::make_tuple();
481 std::string _get_name(){
489 template<
typename Head,
typename... Tail>
490 class _Zip<Head, Tail...> :
private _Zip<Tail...> {
495 int this_size = head->
get_value().size();
496 int rest_size = _Zip<Tail...>::_get_size();
497 return std::min(this_size, rest_size);
500 typename std::tuple<Head,Tail...> _get_at(
int idx){
501 auto tail_tuple = _Zip<Tail...>::_get_at(idx);
502 return std::tuple_cat(std::make_tuple(head->
get_value()[idx]),tail_tuple);
505 std::string _get_name(){
506 return head->get_name()+
","+_Zip<Tail...>::_get_name();
512 _Zip(
Value<std::vector<Head>>* head,
Value<std::vector<Tail>>*... tail)
513 : _Zip<Tail...>(tail...),
532 template <
typename... ArgTypes>
534 private _Zip<ArgTypes...>{
538 int size = _Zip<ArgTypes...>::_get_size();
539 for(
int i=0; i<size; i++){
540 this->value.push_back(_Zip<ArgTypes...>::_get_at(i));
544 std::string _get_name(){
545 return "zip("+_Zip<ArgTypes...>::_get_name()+
")";
549 Zip(
Value<std::vector<ArgTypes>>*... args,
const std::string& alias)
550 :
DerivedValue<std::vector<std::tuple<ArgTypes...>>>(
"", alias),
551 _Zip<ArgTypes...>(args...) {
552 this->set_name(_get_name());
556 template<
typename>
class Map;
564 template <
typename Ret,
typename... ArgTypes>
567 Function<Ret(ArgTypes...)>& fn;
568 Zip<ArgTypes...>* arg;
572 for(
auto tup : arg->get_value()){
573 this->value.push_back(
call(fn,tup));
578 Map(Function<Ret(ArgTypes...)>& fn,
Zip<ArgTypes...>* arg,
const std::string& alias)
584 template<
typename... T>
class _Tuple;
589 std::tuple<> _get_value(){
590 return std::make_tuple();
593 std::string _get_name(){
601 template<
typename Head,
typename... Tail>
602 class _Tuple<Head, Tail...> :
private _Tuple<Tail...> {
606 typename std::tuple<Head,Tail...> _get_value(){
607 auto tail_tuple = _Tuple<Tail...>::_get_value();
608 return std::tuple_cat(std::make_tuple(head->
get_value()),tail_tuple);
612 std::string _get_name(){
613 return head->get_name()+
","+_Tuple<Tail...>::_get_name();
620 : _Tuple<Tail...>(tail...),
630 template <
typename... ArgTypes>
632 private _Tuple<ArgTypes...>{
635 this->value = _Tuple<ArgTypes...>::_get_value();
638 std::string _get_name(){
639 return "tuple("+_Tuple<ArgTypes...>::_get_name()+
")";
645 _Tuple<ArgTypes...>(args...) {
646 this->set_name(_get_name());
650 template<
typename>
class Apply;
655 template <
typename Ret,
typename... ArgTypes>
658 Function<Ret(ArgTypes...)>& fn;
659 Tuple<ArgTypes...>* arg;
662 auto &tup = arg->get_value();
663 this->value =
call(fn, tup);
667 Apply(Function<Ret(ArgTypes...)>& fn,
Tuple<ArgTypes...>* arg,
const std::string& alias)
680 Function<bool(T)>& selector;
692 Count(Function<
bool(T)>& selector,
Value<std::vector<T>>* v,
const std::string alias)
694 selector(selector), v(v) { }
704 template <
typename T>
707 Function<T(std::vector<T>)>& reduce;
710 this->value = reduce(v->get_value());
717 Reduce(Function<T(std::vector<T>)>& reduce,
Value<std::vector<T> >* v,
const std::string alias)
718 :
DerivedValue<T>(
"reduceWith("+reduce.get_name()+
":"+v->get_name()+
")", alias),
719 reduce(reduce), v(v) { }
725 template <
typename T>
728 Max(
Value<std::vector<T>>* v,
const std::string alias)
729 :
Reduce<T>(GenFunction::register_function<T(std::vector<T>)>(
"max",
730 FUNC(([](std::vector<T> vec){
731 return *std::max_element(vec.begin(), vec.end());}))),
738 template <
typename T>
741 Min(
Value<std::vector<T>>* v,
const std::string alias)
742 :
Reduce<T>(GenFunction::register_function<T(std::vector<T>)>(
"min",
743 FUNC(([](std::vector<T> vec){
744 return *std::min_element(vec.begin(), vec.end());}))),
751 template <
typename T>
754 Mean(
Value<std::vector<T>>* v,
const std::string alias)
755 :
Reduce<T>(GenFunction::register_function<T(std::vector<T>)>(
"mean",
756 FUNC(([](std::vector<T> vec){
757 int n = 0; T sum = 0;
758 for (T e : vec){ n++; sum += e; }
759 return n>0 ? sum / n : 0; }))),
766 template <
typename T>
769 Range(
Value<std::vector<T>>* v,
const std::string alias)
770 :
Reduce<T>(GenFunction::register_function<T(std::vector<T>)>(
"range",
771 FUNC(([](std::vector<T> vec){
772 auto minmax = std::minmax_element(vec.begin(), vec.end());
773 return (*minmax.second) - (*minmax.first); }))),
780 template <
typename T>
784 :
Reduce<T>(GenFunction::register_function<T(std::vector<T>)>(
"elementOf",
785 FUNC(([index](std::vector<T> vec){
return vec[index->
get_value()];}))),
794 template <
typename T>
797 Function<std::pair<T,int>(std::vector<T>)>& reduce;
801 this->value = reduce(v->get_value());
805 ReduceIndex(Function<std::pair<T,int>(std::vector<T>)>& reduce,
Value<std::vector<T> >* v,
const std::string alias=
"")
806 :
DerivedValue<T>(
"reduceIndexWith("+reduce.get_name()+
":"+v->get_name()+
")", alias),
807 reduce(reduce), v(v) { }
813 template <
typename T>
816 MaxIndex(
Value<std::vector<T>>* v,
const std::string alias=
"")
817 :
ReduceIndex<T>(GenFunction::register_function<T(std::vector<T>)>(
"maxIndex",
818 FUNC(([](std::vector<T> vec){
819 auto elptr = std::max_element(vec.begin(), vec.end());
820 return std::pair<T,int>(*elptr, int(elptr-vec.begin())); }))),
827 template <
typename T>
830 MinIndex(
Value<std::vector<T>>* v,
const std::string alias=
"")
831 :
ReduceIndex<T>(GenFunction::register_function<T(std::vector<T>)>(
"minIndex",
832 FUNC(([](std::vector<T> vec){
833 auto elptr = std::min_element(vec.begin(), vec.end());
834 return std::pair<T,int>(*elptr, int(elptr-vec.begin())); }))),
843 template <
typename T>
851 BoundValue(Function<T()>& f,
const std::string alias=
"")
860 template <
typename T>
866 PointerValue(
const std::string& name, T* ptr,
const std::string alias=
"")
875 template <
typename T>
881 ConstantValue(
const std::string& name, T const_value,
const std::string alias=
"")
void update_value()
Updates the internal value.
Definition: value.hpp:847
Find and return the maximum value of a vector.
Definition: value.hpp:726
Zips a series of vectors together.
Definition: value.hpp:533
Reduce a Value of type vector<T> to just a T.
Definition: value.hpp:705
void update_value()
Updates the internal value.
Definition: value.hpp:683
void update_value()
Updates the internal value.
Definition: value.hpp:457
Find and return the minimum value of a vector and its index.
Definition: value.hpp:828
A generic value owning only a function object.
Definition: value.hpp:844
T & get_value()
Calculate, if necessary, and return the value held by this object.
Definition: value.hpp:413
Calculate the range of the values in a vector.
Definition: value.hpp:767
Takes a series of Value objects and bundles them together into a std::tuple object.
Definition: value.hpp:631
A std::vector wrapper around a C-style array.
Definition: value.hpp:433
Similar to Reduce, but returns a pair of a T and an int.
Definition: value.hpp:795
Returns the count of elements in the input vector passing a test function.
Definition: value.hpp:678
Find and return the maximum value of a vector and its index.
Definition: value.hpp:814
void update_value()
Updates the internal value.
Definition: value.hpp:709
Parent class to all Function classes.
Definition: value.hpp:98
void update_value()
Updates the internal value.
Definition: value.hpp:634
A value supplied by the dataset, not derived.
Definition: value.hpp:359
Find and return the minimum value of a vector.
Definition: value.hpp:739
Creates a std::pair type from a two other Value objects.
Definition: value.hpp:454
void update_value()
Updates the internal value.
Definition: value.hpp:661
T & get_value()
Calculate, if necessary, and return the value held by this object.
Definition: value.hpp:368
void update_value()
Updates the internal value.
Definition: value.hpp:800
void update_value()
Updates the internal value.
Definition: value.hpp:570
void update_value()
Updates the internal value.
Definition: value.hpp:878
The namespace containing all filval classes and functions.
Definition: api.hpp:6
Extract the element at a specific index from a vector.
Definition: value.hpp:781
Calculate the mean value of a vector.
Definition: value.hpp:752
void update_value()
Updates the internal value.
Definition: value.hpp:438
A generic value.
Definition: value.hpp:339
void update_value()
Updates the internal value.
Definition: value.hpp:536
static std::string format_code(const std::string &code)
Attempt to invoke clang-format for the purpose of printing out nicely formatted functions to the log ...
Definition: value.hpp:127
A Value derived from some other Values, not directly from the dataset.
Definition: value.hpp:390
void update_value()
Updates the internal value.
Definition: value.hpp:863
virtual T & get_value()=0
Calculate, if necessary, and return the value held by this object.
auto call(F &f, TUPLE &&t)
This calls a function of type F with the contents of the tuple as separate arguments.
Definition: value.hpp:86
A Value which always returns the same value, supplied in the constructor.
Definition: value.hpp:876
static std::map< const std::string, GenFunction * > function_registry
Static mapping of functions from their name to the object wrapper of the function.
Definition: value.hpp:110
A Value of a pointer.
Definition: value.hpp:861