Medusa  1.1
Coordinate Free Mehless Method implementation
stdtypesutils.hpp
Go to the documentation of this file.
1 #ifndef MEDUSA_BITS_UTILS_STDTYPESUTILS_HPP_
2 #define MEDUSA_BITS_UTILS_STDTYPESUTILS_HPP_
3 
11 #include <medusa/Config.hpp>
13 #include <string>
14 #include <vector>
15 #include <tuple>
16 #include <type_traits>
17 
18 namespace mm {
19 
27 std::vector<std::string> split(const std::string& s, const std::string& delim);
29 std::vector<std::string> split(const std::string& s, char delim);
30 
37 std::string join(const std::vector<std::string>& parts, const std::string& joiner);
39 std::string join(const std::vector<std::string>& parts, char joiner);
40 
42 template<typename container_t>
43 container_t& sort(container_t& v) {
44  std::sort(v.begin(), v.end());
45  return v;
46 }
47 
49 template<typename container_t, typename predicate_t>
50 container_t& sort(container_t& v, const predicate_t& pred) {
51  std::sort(v.begin(), v.end(), pred);
52  return v;
53 }
54 
56 template<typename container_t>
57 container_t sorted(container_t v) {
58  std::sort(v.begin(), v.end());
59  return v;
60 }
61 
63 template<typename container_t, typename predicate_t>
64 container_t sorted(container_t v, const predicate_t& pred) {
65  std::sort(v.begin(), v.end(), pred);
66  return v;
67 }
68 
77 template <typename container_t, typename T>
78 container_t pad(container_t container, T value) {
79  decltype(container.begin()->size()) maxsize = 0;
80  for (const auto& x : container) {
81  if (x.size() > maxsize) {
82  maxsize = x.size();
83  }
84  }
85  for (auto& x : container) {
86  for (auto i = x.size(); i < maxsize; ++i) x.push_back(value);
87  }
88  return container;
89 }
90 
97 template <typename T, typename Tuple>
98 struct tuple_index {
100  static const std::size_t value = -1;
101  static_assert(!std::is_same<Tuple, std::tuple<>>::value, // Did you access invalid operators?
102  "Could not get index of type `T` in given `Tuple`");
103 };
104 
106 template <typename T, typename... Types>
107 struct tuple_index<T, std::tuple<T, Types...>> {
108  static const std::size_t value = 0;
109 };
110 
112 template <typename T, typename U, typename... Types>
113 struct tuple_index<T, std::tuple<U, Types...>> {
115  static const std::size_t value = 1 + tuple_index<T, std::tuple<Types...>>::value;
116 };
117 
119 template <typename T, typename Tuple>
121 
123 template <typename T>
124 struct tuple_has_type<T, std::tuple<>> : std::false_type {};
125 
127 template <typename T, typename U, typename... Ts>
128 struct tuple_has_type<T, std::tuple<U, Ts...>> : tuple_has_type<T, std::tuple<Ts...>> {};
129 
131 template <typename T, typename... Ts>
132 struct tuple_has_type<T, std::tuple<T, Ts...>> : std::true_type {};
133 
134 } // namespace mm
135 
136 #endif // MEDUSA_BITS_UTILS_STDTYPESUTILS_HPP_
mm
Root namespace for the whole library.
Definition: Gaussian.hpp:14
mm::join
std::string join(const std::vector< std::string > &parts, const std::string &joiner)
Joins a vector of strings back together.
mm::tuple_index
Returns the first index of type T in Tuple.
Definition: stdtypesutils.hpp:98
mm::tuple_index::value
static const std::size_t value
Index of T in tuple.
Definition: stdtypesutils.hpp:100
Config.hpp
mm::tuple_has_type
Find type T in Tuple – declaration.
Definition: stdtypesutils.hpp:120
mm::sorted
container_t sorted(container_t v)
Returns a sorted copy of container.
Definition: stdtypesutils.hpp:57
assert.hpp
mm::sort
container_t & sort(container_t &v, const predicate_t &pred)
Sorts a container inplace according to ordering defined by pred.
Definition: stdtypesutils.hpp:50
mm::sort
container_t & sort(container_t &v)
Sorts a container inplace.
Definition: stdtypesutils.hpp:43
mm::pad
container_t pad(container_t container, T value)
Pads a ragged array with given value.
Definition: stdtypesutils.hpp:78
mm::split
std::vector< std::string > split(const std::string &s, const std::string &delim)
Splits string by delim, returning a vector of tokens (including empty).
Definition: stdtypesutils.cpp:10