TTTT Analysis  0.1
api.hpp
Go to the documentation of this file.
1 
33 #ifndef API_HPP
34 #define API_HPP
35 #include <string>
36 #include <vector>
37 #include "filval/value.hpp"
38 namespace fv{
39 
40  template<typename T>
41  Value<T>* lookup(const std::string& name){
42  GenValue* gv = GenValue::get_value(name);
43  if (gv == nullptr){
44  CRITICAL("Could not find alias or value \"" << name << "\"."
45  <<" I'll tell you the ones I know about." << std::endl
46  << GenValue::summary(), -1);
47  }
48  Value<T>* tv = dynamic_cast<Value<T>*>(gv);
49  if(tv == nullptr){
50  CRITICAL("Value: \""+gv->get_name() + "\" has improper type.",-1);
51  }
52  return tv;
53  }
54 
55  template<typename T>
56  bool check_exists(const std::string name){
57  GenValue* gv = GenValue::get_value(name);
58  Value<T>* tv = dynamic_cast<Value<T>*>(gv);
59  return tv != nullptr;
60  }
61 
62  ObsFilter* lookup_obs_filter(const std::string& name){
63  ObsFilter* f = dynamic_cast<ObsFilter*>(GenValue::get_value(name));
64  if(f == nullptr){
65  CRITICAL("ObsFilter: "+f->get_name() + "has improper type.",-1);
66  }
67  return f;
68  }
69 
70  template <typename T>
71  decltype(auto)
72  wrapper_vector(Value<int>* size, Value<T*>* data, const std::string& alias=""){
73  typedef std::vector<T> type;
74  const std::string& name = WrapperVector<T>::fmt_name(size, data);
75  if (check_exists<type>(name))
76  return lookup<type>(name);
77  else
78  return (Value<type>*)new WrapperVector<T>(size, data, alias);
79  }
80 
81  template <typename... ArgTypes>
82  decltype(auto)
83  zip(Value<std::vector<ArgTypes>>*... args, const std::string& alias=""){
84  typedef std::vector<std::tuple<ArgTypes...>> type;
85  std::string& name = Zip<ArgTypes...>::fmt_name(args...);
86  if (check_exists<type>(name))
87  return lookup<type>(name);
88  else
89  return (Value<type>*)new Zip<ArgTypes...>(args..., alias);
90  }
91 
92  template <typename Ret, typename... ArgTypes>
93  decltype(auto)
94  map(Function<Ret(ArgTypes...)>& fn, Value<std::vector<std::tuple<ArgTypes...>>>* arg, const std::string& alias=""){
95  typedef std::vector<Ret> type;
96  const std::string& name = Map<Ret(ArgTypes...)>::fmt_name(fn, arg);
97  if (check_exists<type>(name))
98  return lookup<type>(name);
99  else
100  return (Value<type>*)new Map<Ret(ArgTypes...)>(fn, arg, alias);
101  }
102 
103  template <typename... ArgTypes>
104  decltype(auto)
105  tuple(Value<ArgTypes>*... args){
106  typedef std::tuple<ArgTypes...> type;
107  const std::string& name = Tuple<ArgTypes...>::fmt_name(args...);
108  if (check_exists<type>(name))
109  return lookup<type>(name);
110  else
111  return (Value<type>*)new Tuple<ArgTypes...>(args..., "");
112  }
113 
114  template <size_t N, typename... ArgTypes>
115  decltype(auto)
116  detup(Value<std::tuple<ArgTypes...>>* tup, const std::string& alias=""){
117  typedef typename std::tuple_element<N, std::tuple<ArgTypes...>>::type type;
118  const std::string& name = DeTup<N, ArgTypes...>::fmt_name(tup);
119  if (check_exists<type>(name))
120  return lookup<type>(name);
121  else
122  return (Value<type>*)new DeTup<N, ArgTypes...>(tup, alias);
123  }
124 
125  template <size_t N, typename... ArgTypes>
126  decltype(auto)
127  detup_vec(Value<std::vector<std::tuple<ArgTypes...>>>* tup, const std::string& alias=""){
128  typedef std::vector<typename std::tuple_element<N, std::tuple<ArgTypes...>>::type> type;
129  const std::string& name = DeTupVector<N, ArgTypes...>::fmt_name(tup);
130  if (check_exists<type>(name))
131  return lookup<type>(name);
132  else
133  return (Value<type>*)new DeTupVector<N, ArgTypes...>(tup, alias);
134  }
135 
136  template <typename Ret, typename... ArgTypes>
137  decltype(auto)
138  apply(Function<Ret(ArgTypes...)>& fn, Value<std::tuple<ArgTypes...>>* arg, const std::string& alias=""){
139  typedef Ret type;
140  const std::string& name = Apply<Ret(ArgTypes...)>::fmt_name(fn, arg);
141  if (check_exists<type>(name))
142  return lookup<type>(name);
143  else
144  return (Value<type>*)new Apply<Ret(ArgTypes...)>(fn, arg, alias);
145  }
146 
147  template <typename T1, typename T2>
148  decltype(auto)
149  pair(Value<T1>* val1, Value<T2>* val2, const std::string& alias=""){
150  typedef std::pair<T1,T2> type;
151  const std::string& name = Pair<T1,T2>::fmt_name(val1, val2);
152  if (check_exists<type>(name))
153  return lookup<type>(name);
154  else
155  return (Value<type>*)new Pair<T1,T2>(val1, val2, alias);
156  }
157 
158  template <typename T1, typename T2>
159  decltype(auto)
160  pair(const std::string& name1, const std::string& name2, const std::string& alias=""){
161  return pair<T1,T2>(lookup<T1>(name1), lookup<T2>(name2), alias);
162  }
163 
164 
165  template <typename T>
166  decltype(auto)
167  max(Value<std::vector<T>>* v, const std::string& alias=""){
168  typedef T type;
169  const std::string& name = Max<T>::fmt_name(v);
170  if (check_exists<type>(name))
171  return lookup<type>(name);
172  else
173  return (Value<type>*)new Max<T>(v, alias);
174  }
175 
176  template <typename T>
177  decltype(auto)
178  max(const std::string& v_name, const std::string& alias=""){
179  return max(lookup<std::vector<T>>(v_name), alias);
180  }
181 
182  template <typename T>
183  decltype(auto)
184  min(Value<std::vector<T>>* v, const std::string& alias=""){
185  typedef T type;
186  const std::string& name = Min<T>::fmt_name(v);
187  if (check_exists<type>(name))
188  return lookup<type>(name);
189  else
190  return (Value<type>*)new Min<T>(v, alias);
191  }
192 
193  template <typename T>
194  decltype(auto)
195  min(const std::string& v_name, const std::string& alias=""){
196  return min(lookup<std::vector<T>>(v_name), alias);
197  }
198 
199  template <typename T>
200  decltype(auto)
201  range(Value<std::vector<T>>* v, const std::string& alias=""){
202  typedef T type;
203  const std::string& name = Range<T>::fmt_name(v);
204  if (check_exists<type>(name))
205  return lookup<type>(name);
206  else
207  return (Value<type>*)new Range<T>(v, alias);
208  }
209 
210  template <typename T>
211  decltype(auto)
212  range(const std::string& v_name, const std::string& alias=""){
213  return range(lookup<std::vector<T>>(v_name), alias);
214  }
215 
216  template <typename T>
217  decltype(auto)
218  mean(Value<std::vector<T>>* v, const std::string& alias=""){
219  typedef T type;
220  const std::string& name = Mean<T>::fmt_name(v);
221  if (check_exists<type>(name))
222  return lookup<type>(name);
223  else
224  return (Value<type>*)new Mean<T>(v, alias);
225  }
226 
227  template <typename T>
228  decltype(auto)
229  mean(const std::string& v_name, const std::string& alias=""){
230  return mean(lookup<std::vector<T>>(v_name), alias);
231  }
232 
233  template <typename T>
234  decltype(auto)
235  count(Function<bool(T)>& selector, Value<std::vector<T>>* v, const std::string& alias=""){
236  typedef int type;
237  const std::string& name = Count<T>::fmt_name(selector, v);
238  if (check_exists<type>(name))
239  return lookup<type>(name);
240  else
241  return (Value<type>*)new Count<T>(selector, v, alias);
242  }
243 
244  template <typename T>
245  decltype(auto)
246  count(Function<bool(T)>& selector, const std::string& v_name, const std::string& alias=""){
247  return count<T>(selector, lookup<std::vector<T>>(v_name), alias);
248  }
249 
250  template <typename FST, typename SND>
251  decltype(auto)
252  cart_product(Value<std::vector<FST>>* val1, Value<std::vector<SND>>* val2, const std::string& alias=""){
253  typedef std::vector<std::tuple<FST,SND>> type;
254  const std::string& name = CartProduct<FST, SND>::fmt_name(val1, val2);
255  if (check_exists<type>(name))
256  return lookup<type>(name);
257  else
258  return (Value<type>*)new CartProduct<FST, SND>(val1, val2, alias);
259  }
260 
261  template <typename FST, typename SND>
262  decltype(auto)
263  cart_product(const std::string& val1_name, const std::string& val2_name, const std::string& alias=""){
264  return cart_product<FST,SND>(lookup<std::vector<FST>>(val1_name), lookup<std::vector<SND>>(val2_name), alias);
265  }
266 
267  template <typename T, int Size>
268  decltype(auto)
269  combinations(Value<std::vector<T>>* val, const std::string& alias=""){
270  typedef std::vector<typename HomoTuple<T,Size>::type> type;
271  const std::string& name = Combinations<T, Size>::fmt_name(val);
272  if (check_exists<type>(name))
273  return lookup<type>(name);
274  else
275  return (Value<type>*)new Combinations<T, Size>(val, alias);
276  }
277 
278  template <typename T, int Size>
279  decltype(auto)
280  combinations(const std::string& val_name, const std::string alias = ""){
281  return combinations<T, Size>(lookup<std::vector<T>>(val_name), alias);
282  }
283 
284  template <typename T>
285  decltype(auto)
286  filter(Function<bool(T)>& filter, Value<std::vector<T>>* val, const std::string alias=""){
287  typedef std::vector<T> type;
288  const std::string& name = Filter<T>::fmt_name(filter, val);
289  if (check_exists<type>(name))
290  return lookup<type>(name);
291  else
292  return (Value<type>*)new Filter<T>(filter, val, alias);
293  }
294 
295  template <typename T>
296  decltype(auto)
297  filter(Function<bool(T)>& filter_func, const std::string& val_name, const std::string alias=""){
298  return filter<T>(filter_func, lookup<std::vector<T>>(val_name), alias);
299  }
300 
301  template <typename... ArgTypes>
302  decltype(auto)
303  tup_filter(Function<bool(ArgTypes...)>& filter, Value<std::vector<std::tuple<ArgTypes...>>>* val, const std::string alias=""){
304  typedef std::vector<std::tuple<ArgTypes...>> type;
305  const std::string& name = TupFilter<ArgTypes...>::fmt_name(filter, val);
306  if (check_exists<type>(name))
307  return lookup<type>(name);
308  else
309  return (Value<type>*)new TupFilter<ArgTypes...>(filter, val, alias);
310  }
311 
312  template <typename... ArgTypes>
313  decltype(auto)
314  tup_filter(Function<bool(ArgTypes...)>& filter, const std::string& val_name, const std::string alias=""){
315  return tup_filter<ArgTypes...>(filter, lookup<std::vector<std::tuple<ArgTypes...>>>(val_name), alias);
316  }
317 
318  ObsFilter* obs_filter(const std::string& name, std::function<bool()> filter_function, const std::string& impl=""){
319  return new ObsFilter(name, filter_function, impl);
320  }
321 }
322 #endif // API_HPP
Find and return the maximum value of a vector.
Definition: value.hpp:976
Zips a series of vectors together.
Definition: value.hpp:666
Find combinations of items from an input vector.
Definition: value.hpp:1108
Calculate the range of the values in a vector.
Definition: value.hpp:1029
Takes a series of Value objects and bundles them together into a std::tuple object.
Definition: value.hpp:774
A std::vector wrapper around a C-style array.
Definition: value.hpp:543
Returns the count of elements in the input vector passing a test function.
Definition: value.hpp:868
Returns the elements in a vector that pass a test function.
Definition: value.hpp:895
Gets the Nth element from a tuple value.
Definition: value.hpp:795
Find and return the minimum value of a vector.
Definition: value.hpp:993
Creates a vector of extracting the Nth value from each entry in a vector of tuples.
Definition: value.hpp:817
Creates a std::pair type from a two other Value objects.
Definition: value.hpp:568
The namespace containing all filval classes and functions.
Definition: api.hpp:38
Returns the elements in a vector that pass a test function.
Definition: value.hpp:924
Calculate the mean value of a vector.
Definition: value.hpp:1010
A templated value.
Definition: value.hpp:415
Calculate the cartesian product of two input vectors.
Definition: value.hpp:1151