Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tools/Utils.hh
Go to the documentation of this file.
1 ///
2 /// @file Utils.hh
3 /// @brief Hammer utility functions
4 ///
5 
6 //**** This file is a part of the HAMMER library
7 //**** Copyright (C) 2016 - 2020 The HAMMER Collaboration
8 //**** HAMMER is licensed under version 3 of the GPL; see COPYING for details
9 //**** Please note the MCnet academic guidelines; see GUIDELINES for details
10 
11 // -*- C++ -*-
12 #ifndef HAMMER_TOOLS_UTILS
13 #define HAMMER_TOOLS_UTILS
14 
15 #include <exception>
16 #include <map>
17 #include <unordered_map>
18 #include <string>
19 #include <complex>
20 #include <algorithm>
21 #include <iterator>
22 #include <vector>
23 
24 #include <boost/functional/hash.hpp>
25 
27 
28 #define UNUSED(x) ((void)(x))
29 
30 namespace Hammer {
31 
32  /// @brief
33  /// @return
34  inline std::string version() {
35  return HAMMER_NAME " ver. " HAMMER_VERSION;
36  }
37 
38  template<typename KeyType, typename ValueType>
39  ValueType getOrDefault(const std::map<KeyType, ValueType>& data, KeyType key, ValueType fallback) {
40  auto it = data.find(key);
41  return (it == data.end()) ? fallback : it->second;
42  }
43 
44  template <typename KeyType, typename ValueType>
45  ValueType getOrDefault(const std::unordered_map<KeyType, ValueType>& data, KeyType key, ValueType fallback) {
46  auto it = data.find(key);
47  return (it == data.end()) ? fallback : it->second;
48  }
49 
50  template <typename KeyType, typename ValueType>
51  auto const& getOrThrow(const std::map<KeyType, ValueType>& data, KeyType key, std::exception error) {
52  auto it = data.find(key);
53  if(it == data.end()) {
54  throw error;
55  }
56  return it->second;
57  }
58 
59  template<typename KeyType, typename ValueType>
60  auto& getOrThrow(std::map<KeyType, ValueType>& data, KeyType key, std::exception error) {
61  auto it = data.find(key);
62  if(it == data.end()) {
63  throw error;
64  }
65  return it->second;
66  }
67 
68  template <typename _InputIterator, typename _OutputIterator, typename _UnaryOperation>
69  _OutputIterator transform_n(_InputIterator __first, size_t __n, _OutputIterator __result, _UnaryOperation __op) {
70  return std::generate_n(__result, __n, [&__first, &__op]() -> decltype(auto) { return __op(*__first++); });
71  }
72 
73  template <typename T>
76  };
77 
78  template <typename T>
80  return std::rbegin(w.iterable);
81  }
82 
83  template <typename T>
85  return std::rend(w.iterable);
86  }
87 
88  template <typename T>
90  return {iterable};
91  }
92 
93  template <typename T>
94  std::ostream& operator<<(std::ostream& out, const std::vector<T>& v) {
95  if (!v.empty()) {
96  out << '[';
97  std::copy(v.begin(), v.end(), std::ostream_iterator<T>(out, ", "));
98  out << "\b\b]";
99  }
100  return out;
101  }
102 
103  template<typename K, typename V>
104  using UMap = std::unordered_map<K, V, boost::hash<K>>;
105 
106 } // namespace Hammer
107 
108 #endif
std::string version()
Definition: Tools/Utils.hh:34
#define HAMMER_VERSION
std::unordered_map< K, V, boost::hash< K >> UMap
Definition: Tools/Utils.hh:104
auto const & getOrThrow(const std::map< KeyType, ValueType > &data, KeyType key, std::exception error)
Definition: Tools/Utils.hh:51
reversion_wrapper< T > reverse_range(T &&iterable)
Definition: Tools/Utils.hh:89
#define HAMMER_NAME
Hammer configuration definitions.
ValueType getOrDefault(const std::map< KeyType, ValueType > &data, KeyType key, ValueType fallback)
Definition: Tools/Utils.hh:39
auto begin(reversion_wrapper< T > w)
Definition: Tools/Utils.hh:79
_OutputIterator transform_n(_InputIterator __first, size_t __n, _OutputIterator __result, _UnaryOperation __op)
Definition: Tools/Utils.hh:69
auto end(reversion_wrapper< T > w)
Definition: Tools/Utils.hh:84