gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-2191-ga4742ac
a Global And Modular Bsm Inference Tool
variadic_functions.hpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
14 //
20 
21 #ifndef VARIADIC_FUNCTIONS_HPP
22 #define VARIADIC_FUNCTIONS_HPP
23 
24 #include <iostream>
25 #include <fstream>
26 #include <string>
27 #include <type_traits>
28 #include <cassert>
29 #include <map>
30 #include <unordered_map>
31 #include <set>
32 #include <unordered_set>
33 #include <vector>
34 #include <list>
35 #include <forward_list>
36 #include <deque>
37 #include <array>
38 
39 namespace Gambit
40 {
42  // Lazy vector initialization
43  //
44  // usage:
45  // auto vec = initVector("this", "is", "the", "initializer", "list");
46  // auto vec = initVector(2.3, 4.2, 1.3);
47  // ...
48  //
50 
51  template <typename T>
52  std::vector<T> initVector(std::vector<T> vector)
53  {
54  return vector;
55  }
56 
57  template <typename T, typename... Args>
58  std::vector<T> initVector(std::vector<T> vector, T value, Args... args)
59  {
60  vector.push_back(value);
61  return initVector(vector, args...);
62  }
63 
64  // This function causes a (readable) compile-time error when T != U.
65  // In case types are convertable, they are converted.
66  template <typename T, typename U, typename... Args>
67  std::vector<T> initVector(std::vector<T> vector, U value, Args... args)
68  {
69  T value_converted = value;
70  vector.push_back(value_converted);
71  return initVector(vector, args...);
72  }
73 
74  template <typename T, typename... Args>
75  std::vector<T> initVector(T value, Args... args)
76  {
77  std::vector<T> vector;
78  vector.push_back(value);
79  vector = initVector(vector, args...);
80  return vector;
81  }
82 
84 
85  template <typename T>
86  std::set<T> initSet(std::set<T> set)
87  {
88  return set;
89  }
90 
91  template <typename T, typename... Args>
92  std::set<T> initSet(std::set<T> set, T value, Args... args)
93  {
94  set.insert(value);
95  return initSet(set, args...);
96  }
97 
98  // This function causes a (readable) compile-time error when T != U.
99  // In case types are convertable, they are converted.
100  template <typename T, typename U, typename... Args>
101  std::set<T> initSet(std::set<T> set, U value, Args... args)
102  {
103  T value_converted = value;
104  set.insert(value_converted);
105  return initSet(set, args...);
106  }
107 
108  template <typename T, typename... Args>
109  std::set<T> initSet(T value, Args... args)
110  {
111  std::set<T> set;
112  set.insert(value);
113  set = initSet(set, args...);
114  return set;
115  }
116 
117 
119  //div_ints_by_half
121 
122  template <int low, int hi>
124  {
125  static const int value = (low + hi) >> 1;
126  };
127 
129  //remove_all
131 
132  template <typename T>
133  struct remove_all
134  {
135  typedef typename std::remove_cv
136  <
137  typename std::remove_volatile
138  <
139  typename std::remove_const
140  <
141  typename std::remove_reference
142  <
143  T
144  >::type
145  >::type
146  >::type
148  };
149 
151  //is_container
153 
154  template <typename T>
156  {
157  static const bool value = false;
158  typedef T type;
159  };
160 
161  template <typename T>
162  struct __is_container__<std::vector<T>>
163  {
164  static const bool value = true;
165  typedef T type;
166  };
167 
168  template <typename T>
169  struct __is_container__<std::set<T>>
170  {
171  static const bool value = true;
172  typedef T type;
173  };
174 
175  template <typename T>
176  struct __is_container__<std::multiset<T>>
177  {
178  static const bool value = true;
179  typedef T type;
180  };
181 
182  template <typename T1, typename T2>
183  struct __is_container__<std::map<T1, T2>>
184  {
185  static const bool value = true;
186  typedef std::pair<T1, T2> type;
187  };
188 
189  template <typename T1, typename T2>
190  struct __is_container__<std::multimap<T1, T2>>
191  {
192  static const bool value = true;
193  typedef std::pair<T1, T2> type;
194  };
195 
196  template <typename T1, typename T2>
197  struct __is_container__<std::unordered_map<T1, T2>>
198  {
199  static const bool value = true;
200  typedef std::pair<T1, T2> type;
201  };
202 
203  template <typename T1, typename T2>
204  struct __is_container__<std::unordered_multimap<T1, T2>>
205  {
206  static const bool value = true;
207  typedef std::pair<T1, T2> type;
208  };
209 
210  template <typename T>
211  struct __is_container__<std::unordered_set<T>>
212  {
213  static const bool value = true;
214  typedef T type;
215  };
216 
217  template <typename T>
218  struct __is_container__<std::unordered_multiset<T>>
219  {
220  static const bool value = true;
221  typedef T type;
222  };
223 
224  template <typename T>
225  struct __is_container__<std::deque<T>>
226  {
227  static const bool value = true;
228  typedef T type;
229  };
230 
231  template <typename T, size_t N>
232  struct __is_container__<std::array<T, N>>
233  {
234  static const bool value = true;
235  typedef T type;
236  };
237 
238  template <typename T>
239  struct __is_container__<std::list<T>>
240  {
241  static const bool value = true;
242  typedef T type;
243  };
244 
245  template <typename T>
246  struct __is_container__<std::forward_list<T>>
247  {
248  static const bool value = true;
249  typedef T type;
250  };
251 
252  template <typename T>
254  {
257  };
258 
260  //is_vector
262 
263  template <typename T>
265  {
266  static const bool value = false;
267  typedef T type;
268  };
269 
270  template <typename T>
271  struct __is_vector__<std::vector<T>>
272  {
273  static const bool value = true;
274  typedef T type;
275  };
276 
277  template <typename T>
278  struct is_vector
279  {
282  };
283 
285  //is_pair
287 
288  template <typename T>
289  struct __is_pair__
290  {
291  const static bool value = false;
292  typedef T first_type;
293  typedef T second_type;
294  };
295 
296  template <typename T1, typename T2>
297  struct __is_pair__ <std::pair<T1, T2>>
298  {
299  const static bool value = true;
300  typedef T1 first_type;
301  typedef T2 second_type;
302  };
303 
304  template <typename T>
305  struct is_pair
306  {
310  };
311 
313  //string functions
315 
316  inline const std::string stringifyVariadic() {return "";}
317 
318  inline const std::string stringifyVariadic(const std::string &str) {return str;}
319 
320  template<typename... args>
321  inline const std::string stringifyVariadic(const std::string &str, const args&... strs) {return str + ", " + stringifyVariadic(strs...);}
322 
324  //mult_types
326 
327  template <typename... args>
328  struct mult_types
329  {
330  typedef void type (args...);
331  };
332 
334  //is_same_type
336 
337  template <typename T, typename type>
339 
340  template <typename type>
341  struct is_same_type_internal <void (), type>
342  {
343  static const bool value = false;
344  };
345 
346  template <typename T, typename... args>
347  struct is_same_type_internal <void (T, args...), T>
348  {
349  static const bool value = true;
350  };
351 
352  template <typename type, typename T, typename... args>
353  struct is_same_type_internal <void (T, args...), type>
354  {
355  static const bool value = is_same_type_internal <void (args...), type>::value;
356  };
357 
358  template <typename type, typename T>
360  {
361  static const bool value = false;
362  };
363 
364  template <typename T>
365  struct is_same_type <T, T>
366  {
367  static const bool value = true;
368  };
369 
370  template <typename T, typename... args>
371  struct is_same_type <mult_types<args...>, T>
372  {
373  static const bool value = is_same_type_internal <typename mult_types<args...>::type, T>::value;
374  };
375 
377  //is_one_member
379 
380  template <typename type, typename T>
382 
383  template <typename type>
384  struct is_one_member_internal <type, void ()>
385  {
386  static const bool value = false;
387  };
388 
389  template <typename type, typename T, typename... args>
390  struct is_one_member_internal <type, void (T, args...)>
391  {
393  };
394 
395  template <typename type, typename... args>
397  {
398  static const bool value = is_one_member_internal<type, void (args...)>::value;
399  };
400 
402  //is_all_member
404 
405  template <typename type, typename T>
407 
408  template <typename type>
409  struct is_all_member_internal <type, void ()>
410  {
411  static const bool value = true;
412  };
413 
414  template <typename type, typename T, typename... args>
415  struct is_all_member_internal <type, void (T, args...)>
416  {
418  };
419 
420  template <typename type, typename... args>
422  {
423  static const bool value = is_all_member_internal<type, void (args...)>::value;
424  };
425 
427  //is_one_member_vector
429 
430  template <typename T>
432 
433  template <>
435  {
436  static const bool value = false;
437  };
438 
439  template <typename T, typename... args>
440  struct is_one_member_vector_internal <void (std::vector<T>, args...)>
441  {
442  static const bool value = true;
443  };
444 
445  template <typename T, typename... args>
446  struct is_one_member_vector_internal <void (std::vector<T> &, args...)>
447  {
448  static const bool value = true;
449  };
450 
451  template <typename T, typename... args>
452  struct is_one_member_vector_internal <void (const std::vector<T> &, args...)>
453  {
454  static const bool value = true;
455  };
456 
457  template <typename T, typename... args>
459  {
461  };
462 
463  template <typename... args>
465  {
467  };
468 
470  //is_all_member_vector
472 
473  template <typename T>
475 
476  template <>
478  {
479  static const bool value = true;
480  };
481 
482  template <typename T, typename... args>
483  struct is_all_member_vector_internal <void (std::vector<T>, args...)>
484  {
486  };
487 
488  template <typename T, typename... args>
489  struct is_all_member_vector_internal <void (std::vector<T> &, args...)>
490  {
492  };
493 
494  template <typename T, typename... args>
495  struct is_all_member_vector_internal <void (const std::vector<T> &, args...)>
496  {
498  };
499 
500  template <typename T, typename... args>
502  {
503  static const bool value = false;
504  };
505 
506  template <typename... args>
508  {
510  };
511 
513  //enable_if's
515 
516  template <typename T, typename ret, typename... args>
518  {
519  typedef std::enable_if<is_one_member<T, args...>::value, ret> type;
520  };
521 
522  template <typename T, typename ret, typename... args>
524  {
525  typedef std::enable_if<is_all_member<T, args...>::value, ret> type;
526  };
527 
528  template <typename ret, typename... args>
530  {
531  typedef std::enable_if<is_one_member_vector<args...>::value, ret> type;
532  };
533 
534  template <typename ret, typename... args>
536  {
537  typedef std::enable_if<is_all_member_vector<args...>::value, ret> type;
538  };
539 
541  //enable_if_not's
543 
544  template <typename T, typename ret, typename... args>
546  {
547  typedef std::enable_if<!is_one_member<T, args...>::value, ret> type;
548  };
549 
550  template <typename T, typename ret, typename... args>
552  {
553  typedef std::enable_if<!is_all_member<T, args...>::value, ret> type;
554  };
555 
556  template <typename ret, typename... args>
558  {
559  typedef std::enable_if<!is_one_member_vector<args...>::value, ret> type;
560  };
561 
562  template <typename ret, typename... args>
564  {
565  typedef std::enable_if<!is_all_member_vector<args...>::value, ret> type;
566  };
567 }
568 
569 #endif
__is_pair__< typename remove_all< T >::type >::second_type second_type
STL namespace.
const std::string stringifyVariadic()
std::enable_if< is_all_member< T, args... >::value, ret > type
__is_pair__< typename remove_all< T >::type >::first_type first_type
std::enable_if< is_all_member_vector< args... >::value, ret > type
std::enable_if<!is_one_member_vector< args... >::value, ret > type
hb_ModelParameters void
std::string str
Shorthand for a standard string.
Definition: Analysis.hpp:35
std::vector< T > initVector(std::vector< T > vector)
std::enable_if< is_one_member< T, args... >::value, ret > type
std::set< T > initSet(std::set< T > set)
Same as above, but for sets.
__is_container__< typename remove_all< T >::type >::type type
std::enable_if< is_one_member_vector< args... >::value, ret > type
std::enable_if<!is_all_member_vector< args... >::value, ret > type
__is_vector__< typename remove_all< T >::type >::type type
std::enable_if<!is_all_member< T, args... >::value, ret > type
std::remove_cv< typename std::remove_volatile< typename std::remove_const< typename std::remove_reference< T >::type >::type >::type >::type type
TODO: see if we can use this one:
Definition: Analysis.hpp:33
std::enable_if<!is_one_member< T, args... >::value, ret > type