Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into actor-yield
[simgrid.git] / include / xbt / utility.hpp
1 /* Copyright (c) 2016-2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef XBT_UTILITY_HPP
8 #define XBT_UTILITY_HPP
9
10 #include <tuple>
11
12 namespace simgrid {
13 namespace xbt {
14
15 /** @brief Comparator class for using with std::priority_queue or boost::heap.
16  *
17  * Compare two std::pair by their first element (of type double), and return true when the first is greater than the
18  * second.  Useful to have priority queues with the smallest element on top.
19  */
20 template <class Pair> class HeapComparator {
21 public:
22   bool operator()(const Pair& a, const Pair& b) const { return a.first > b.first; }
23 };
24
25 // integer_sequence and friends from C++14
26 // We need them to implement `apply` from C++17.
27
28 /** A compile-time sequence of integers (from C++14)
29  *
30  * `index_sequence<std::size_t,1,5,7,9>` represents the sequence `(1,5,7,9)`.
31  *
32  * @code{.cpp}
33  * template<class T, std::size_t... I>
34  * auto extract_tuple(T&& t, integer_sequence<std::size_t, I...>)
35  *   -> decltype(std::make_tuple(std::get<I>(std::forward<T>(t))...))
36  * {
37  *  return std::make_tuple(std::get<I>(std::forward<T>(t))...);
38  * }
39  *
40  * int main()
41  * {
42  *   integer_sequence<std::size_t, 1, 3> seq;
43  *   auto a = std::make_tuple(1, 2.0, false, 'a');
44  *   auto b = extract_tuple(a, seq);
45  *   std::cout << std::get<0>(b) << '\n'; // 2
46  *   std::cout << std::get<1>(b) << '\n'; // a
47  *   return 0;
48  * }
49  * @endcode
50  */
51 template<class T, T... N>
52 class integer_sequence {
53   static constexpr std::size_t size()
54   {
55     return std::tuple_size<decltype(std::make_tuple(N...))>::value;
56   }
57 };
58
59 namespace bits {
60   template<class T, long long N, long long... M>
61   struct make_integer_sequence :
62     make_integer_sequence<T, N-1, N-1, M...>
63   {};
64   template<class T, long long... M>
65   struct make_integer_sequence<T, 0, M...> {
66     typedef integer_sequence<T, (T) M...> type;
67   };
68 }
69
70 /** A compile-time sequence of integers of the form `(0,1,2,3,...,N-1)` (from C++14) */
71 template<class T, T N>
72 using make_integer_sequence = typename simgrid::xbt::bits::make_integer_sequence<T,N>::type;
73
74 /** A compile-time sequence of indices (from C++14) */
75 template<std::size_t... Ints>
76 using index_sequence = integer_sequence<std::size_t, Ints...>;
77
78 /** A compile-time sequence of indices of the form `(0,1,2,3,...,N-1)` (from C++14) */
79 template<std::size_t N>
80 using make_index_sequence = make_integer_sequence<std::size_t, N>;
81
82 /** Convert a type parameter pack into a index_sequence (from C++14) */
83 template<class... T>
84 using index_sequence_for = make_index_sequence<sizeof...(T)>;
85
86 static_assert(std::is_same< make_index_sequence<0>, index_sequence<> >::value, "seq0");
87 static_assert(std::is_same< make_index_sequence<1>, index_sequence<0> >::value, "seq1");
88 static_assert(std::is_same< make_index_sequence<2>, index_sequence<0, 1> >::value, "seq2");
89 static_assert(std::is_same< make_index_sequence<3>, index_sequence<0, 1, 2> >::value, "seq3");
90 static_assert(std::is_same< index_sequence_for<int,double,float>, make_index_sequence<3> >::value, "seq4");
91
92 }
93 }
94 #endif