Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'Adrien.Gougeon/simgrid-master'
[simgrid.git] / include / xbt / utility.hpp
1 /* Copyright (c) 2016-2020. 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 #include <functional>
12
13 namespace simgrid {
14 namespace xbt {
15
16 /** @brief A hash which works with more stuff
17  *
18  *  It can hash pairs: the standard hash currently doesn't include this.
19  */
20 template <class X> class hash : public std::hash<X> {
21 };
22
23 template <class X, class Y> class hash<std::pair<X, Y>> {
24 public:
25   std::size_t operator()(std::pair<X, Y> const& x) const
26   {
27     hash<X> h1;
28     hash<X> h2;
29     return h1(x.first) ^ h2(x.second);
30   }
31 };
32
33 /** @brief Comparator class for using with std::priority_queue or boost::heap.
34  *
35  * Compare two std::pair by their first element (of type double), and return true when the first is greater than the
36  * second.  Useful to have priority queues with the smallest element on top.
37  */
38 template <class Pair> class HeapComparator {
39 public:
40   bool operator()(const Pair& a, const Pair& b) const { return a.first > b.first; }
41 };
42
43 /** @brief Erase an element given by reference from a boost::intrusive::list.
44  */
45 template <class List, class Elem> inline void intrusive_erase(List& list, Elem& elem)
46 {
47   list.erase(list.iterator_to(elem));
48 }
49
50 }
51 }
52 #endif