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