Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ea36639c679ace8dd8620d9a887ea360086b5ea5
[simgrid.git] / include / xbt / utility.hpp
1 /* Copyright (c) 2016-2019. 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 A hash which works with more stuff
16  *
17  *  It can hash pairs: the standard hash currently doesn't include this.
18  */
19 template <class X> class hash : public std::hash<X> {
20 };
21
22 template <class X, class Y> class hash<std::pair<X, Y>> {
23 public:
24   std::size_t operator()(std::pair<X, Y> const& x) const
25   {
26     hash<X> h1;
27     hash<X> h2;
28     return h1(x.first) ^ h2(x.second);
29   }
30 };
31
32 /** @brief Comparator class for using with std::priority_queue or boost::heap.
33  *
34  * Compare two std::pair by their first element (of type double), and return true when the first is greater than the
35  * second.  Useful to have priority queues with the smallest element on top.
36  */
37 template <class Pair> class HeapComparator {
38 public:
39   bool operator()(const Pair& a, const Pair& b) const { return a.first > b.first; }
40 };
41
42 /** @brief Erase an element given by reference from a boost::intrusive::list.
43  */
44 template <class List, class Elem> inline void intrusive_erase(List& list, Elem& elem)
45 {
46   list.erase(list.iterator_to(elem));
47 }
48
49 // integer_sequence and friends from C++14
50 // We need them to implement `apply` from C++17.
51
52 /** A compile-time sequence of integers (from C++14)
53  *
54  * `index_sequence<std::size_t,1,5,7,9>` represents the sequence `(1,5,7,9)`.
55  *
56  * @code{.cpp}
57  * template<class T, std::size_t... I>
58  * auto extract_tuple(T&& t, integer_sequence<std::size_t, I...>)
59  *   -> decltype(std::make_tuple(std::get<I>(std::forward<T>(t))...))
60  * {
61  *  return std::make_tuple(std::get<I>(std::forward<T>(t))...);
62  * }
63  *
64  * int main()
65  * {
66  *   integer_sequence<std::size_t, 1, 3> seq;
67  *   auto a = std::make_tuple(1, 2.0, false, 'a');
68  *   auto b = extract_tuple(a, seq);
69  *   std::cout << std::get<0>(b) << '\n'; // 2
70  *   std::cout << std::get<1>(b) << '\n'; // a
71  *   return 0;
72  * }
73  * @endcode
74  */
75 template<class T, T... N>
76 class integer_sequence {
77 };
78
79 namespace bits {
80   template<class T, long long N, long long... M>
81   struct make_integer_sequence :
82     make_integer_sequence<T, N-1, N-1, M...>
83   {};
84   template<class T, long long... M>
85   struct make_integer_sequence<T, 0, M...> {
86     typedef integer_sequence<T, (T) M...> type;
87   };
88 }
89
90 /** A compile-time sequence of integers of the form `(0,1,2,3,...,N-1)` (from C++14) */
91 template<class T, T N>
92 using make_integer_sequence = typename simgrid::xbt::bits::make_integer_sequence<T,N>::type;
93
94 /** A compile-time sequence of indices (from C++14) */
95 template<std::size_t... Ints>
96 using index_sequence = integer_sequence<std::size_t, Ints...>;
97
98 /** A compile-time sequence of indices of the form `(0,1,2,3,...,N-1)` (from C++14) */
99 template<std::size_t N>
100 using make_index_sequence = make_integer_sequence<std::size_t, N>;
101
102 /** Convert a type parameter pack into a index_sequence (from C++14) */
103 template<class... T>
104 using index_sequence_for = make_index_sequence<sizeof...(T)>;
105
106 static_assert(std::is_same< make_index_sequence<0>, index_sequence<> >::value, "seq0");
107 static_assert(std::is_same< make_index_sequence<1>, index_sequence<0> >::value, "seq1");
108 static_assert(std::is_same< make_index_sequence<2>, index_sequence<0, 1> >::value, "seq2");
109 static_assert(std::is_same< make_index_sequence<3>, index_sequence<0, 1, 2> >::value, "seq3");
110 static_assert(std::is_same< index_sequence_for<int,double,float>, make_index_sequence<3> >::value, "seq4");
111
112 }
113 }
114 #endif