Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / include / xbt / utility.hpp
index e577631..22da5d1 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2016-2017. The SimGrid Team.
+/* Copyright (c) 2016-2022. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
@@ -7,11 +7,45 @@
 #ifndef XBT_UTILITY_HPP
 #define XBT_UTILITY_HPP
 
+#include <array>
+#include <functional>
 #include <tuple>
+#include <xbt/base.h>
+
+/** @brief Helper macro to declare enum class
+ *
+ * Declares an enum class EnumType, and a function "const char* to_c_str(EnumType)" to retrieve a C-string description
+ * for each value.
+ */
+#define XBT_DECLARE_ENUM_CLASS(EnumType, ...)                                                                          \
+  enum class EnumType;                                                                                                 \
+  static constexpr char const* to_c_str(EnumType value)                                                                \
+  {                                                                                                                    \
+    constexpr std::array<const char*, _XBT_COUNT_ARGS(__VA_ARGS__)> names{{_XBT_STRINGIFY_ARGS(__VA_ARGS__)}};         \
+    return names[static_cast<int>(value)];                                                                             \
+  }                                                                                                                    \
+  enum class EnumType { __VA_ARGS__ } /* defined here to handle trailing semicolon */
 
 namespace simgrid {
 namespace xbt {
 
+/** @brief A hash which works with more stuff
+ *
+ *  It can hash pairs: the standard hash currently doesn't include this.
+ */
+template <class X> class hash : public std::hash<X> {
+};
+
+template <class X, class Y> class hash<std::pair<X, Y>> {
+public:
+  std::size_t operator()(std::pair<X, Y> const& x) const
+  {
+    hash<X> h1;
+    hash<X> h2;
+    return h1(x.first) ^ h2(x.second);
+  }
+};
+
 /** @brief Comparator class for using with std::priority_queue or boost::heap.
  *
  * Compare two std::pair by their first element (of type double), and return true when the first is greater than the
@@ -22,73 +56,13 @@ public:
   bool operator()(const Pair& a, const Pair& b) const { return a.first > b.first; }
 };
 
-// integer_sequence and friends from C++14
-// We need them to implement `apply` from C++17.
-
-/** A compile-time sequence of integers (from C++14)
- *
- * `index_sequence<std::size_t,1,5,7,9>` represents the sequence `(1,5,7,9)`.
- *
- * @code{.cpp}
- * template<class T, std::size_t... I>
- * auto extract_tuple(T&& t, integer_sequence<std::size_t, I...>)
- *   -> decltype(std::make_tuple(std::get<I>(std::forward<T>(t))...))
- * {
- *  return std::make_tuple(std::get<I>(std::forward<T>(t))...);
- * }
- *
- * int main()
- * {
- *   integer_sequence<std::size_t, 1, 3> seq;
- *   auto a = std::make_tuple(1, 2.0, false, 'a');
- *   auto b = extract_tuple(a, seq);
- *   std::cout << std::get<0>(b) << '\n'; // 2
- *   std::cout << std::get<1>(b) << '\n'; // a
- *   return 0;
- * }
- * @endcode
+/** @brief Erase an element given by reference from a boost::intrusive::list.
  */
-template<class T, T... N>
-class integer_sequence {
-  static constexpr std::size_t size()
-  {
-    return std::tuple_size<decltype(std::make_tuple(N...))>::value;
-  }
-};
-
-namespace bits {
-  template<class T, long long N, long long... M>
-  struct make_integer_sequence :
-    make_integer_sequence<T, N-1, N-1, M...>
-  {};
-  template<class T, long long... M>
-  struct make_integer_sequence<T, 0, M...> {
-    typedef integer_sequence<T, (T) M...> type;
-  };
+template <class List, class Elem> inline void intrusive_erase(List& list, Elem& elem)
+{
+  list.erase(list.iterator_to(elem));
 }
 
-/** A compile-time sequence of integers of the form `(0,1,2,3,...,N-1)` (from C++14) */
-template<class T, T N>
-using make_integer_sequence = typename simgrid::xbt::bits::make_integer_sequence<T,N>::type;
-
-/** A compile-time sequence of indices (from C++14) */
-template<std::size_t... Ints>
-using index_sequence = integer_sequence<std::size_t, Ints...>;
-
-/** A compile-time sequence of indices of the form `(0,1,2,3,...,N-1)` (from C++14) */
-template<std::size_t N>
-using make_index_sequence = make_integer_sequence<std::size_t, N>;
-
-/** Convert a type parameter pack into a index_sequence (from C++14) */
-template<class... T>
-using index_sequence_for = make_index_sequence<sizeof...(T)>;
-
-static_assert(std::is_same< make_index_sequence<0>, index_sequence<> >::value, "seq0");
-static_assert(std::is_same< make_index_sequence<1>, index_sequence<0> >::value, "seq1");
-static_assert(std::is_same< make_index_sequence<2>, index_sequence<0, 1> >::value, "seq2");
-static_assert(std::is_same< make_index_sequence<3>, index_sequence<0, 1, 2> >::value, "seq3");
-static_assert(std::is_same< index_sequence_for<int,double,float>, make_index_sequence<3> >::value, "seq4");
-
 }
 }
 #endif