PLSSVM - Parallel Least Squares Support Vector Machine  2.0.0
A Least Squares Support Vector Machine implementation using different backends.
utility.hpp
Go to the documentation of this file.
1 
12 #ifndef PLSSVM_DETAIL_UTILITY_HPP_
13 #define PLSSVM_DETAIL_UTILITY_HPP_
14 #pragma once
15 
16 #include "plssvm/default_value.hpp" // plssvm::default_value
17 #include "plssvm/detail/type_traits.hpp" // PLSSVM_REQUIRES, plssvm::detail::always_false_v
18 
19 #include <algorithm> // std::remove_if, std::find
20 #include <cstddef> // std::size_t
21 #include <iterator> // std::distance
22 #include <string> // std::string
23 #include <tuple> // std::forward_as_tuple, std::get
24 #include <type_traits> // std::underlying_type_t, std::is_enum_v
25 
26 namespace plssvm::detail {
27 
32 [[noreturn]] inline void unreachable() {
33  // Uses compiler specific extensions if possible.
34  // Even if no extension is used, undefined behavior is still raised by
35  // an empty function body and the noreturn attribute.
36 #if defined(__GNUC__) // GCC, Clang, ICC
37  __builtin_unreachable();
38 #elif defined(_MSC_VER) // MSVC
39  __assume(false);
40 #endif
41 }
42 
50 template <std::size_t I, typename... Types>
51 [[nodiscard]] constexpr decltype(auto) get(Types &&...args) noexcept {
52  static_assert(I < sizeof...(Types), "Out-of-bounce access!: too few elements in parameter pack");
53  return std::get<I>(std::forward_as_tuple(args...));
54 }
55 
62 template <typename Enum>
63 [[nodiscard]] constexpr std::underlying_type_t<Enum> to_underlying(const Enum e) noexcept {
64  static_assert(std::is_enum_v<Enum>, "e must be an enumeration type!");
65  return static_cast<std::underlying_type_t<Enum>>(e);
66 }
67 
74 template <typename Enum>
75 [[nodiscard]] constexpr std::underlying_type_t<Enum> to_underlying(const default_value<Enum> &e) noexcept {
76  static_assert(std::is_enum_v<Enum>, "e must be an enumeration type!");
77  return to_underlying(e.value());
78 }
79 
88 template <typename Container, typename Pred, PLSSVM_REQUIRES(is_container_v<Container>)>
89 inline typename Container::size_type erase_if(Container &c, Pred pred) {
90  if constexpr (is_vector_v<Container>) {
91  // use optimized version for std::vector
92  auto iter = std::remove_if(c.begin(), c.end(), pred);
93  auto dist = std::distance(iter, c.end());
94  c.erase(iter, c.end());
95  return dist;
96  } else {
97  // generic version otherwise
98  auto old_size = c.size();
99  for (auto i = c.begin(), last = c.end(); i != last;) {
100  if (pred(*i)) {
101  i = c.erase(i);
102  } else {
103  ++i;
104  }
105  }
106  return old_size - c.size();
107  }
108 }
109 
118 template <typename Container, typename T, PLSSVM_REQUIRES(is_container_v<Container>)>
119 [[nodiscard]] inline bool contains(const Container &c, const T &val) {
120  if constexpr (is_sequence_container_v<Container>) {
121  // use std::find for sequence containers
122  return std::find(c.cbegin(), c.cend(), val) != c.cend();
123  } else {
124  // use count otherwise
125  return c.count(val) > 0;
126  }
127 }
128 
133 [[nodiscard]] std::string current_date_time();
134 
135 } // namespace plssvm::detail
136 
137 #endif // PLSSVM_DETAIL_UTILITY_HPP_
This class encapsulates a value that may be a default value or not.
Definition: default_value.hpp:62
Implements a class used to be able to distinguish between the default value of a variable and an assi...
Namespace containing implementation details. Should not directly be used by users.
Definition: csvm.hpp:27
Container::size_type erase_if(Container &c, Pred pred)
Implements an erase_if function for different containers according to https://en.cppreference....
Definition: utility.hpp:89
std::string current_date_time()
Return the current date time in the format "YYYY-MM-DD hh:mm:ss".
void unreachable()
Invokes undefined behavior. Used to mark code paths that may never be reachable.
Definition: utility.hpp:32
bool contains(std::string_view str, std::string_view sv) noexcept
Checks if the string str contains the string sv.
constexpr decltype(auto) get(Types &&...args) noexcept
Get the I-th element of the parameter pack args at compile-time.
Definition: utility.hpp:51
constexpr std::underlying_type_t< Enum > to_underlying(const Enum e) noexcept
Converts an enumeration to its underlying type.
Definition: utility.hpp:63
Defines some generic type traits used in the PLSSVM library.