Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Define and use utility class to compare pairs in priority queues.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Mon, 13 Nov 2017 21:08:27 +0000 (22:08 +0100)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Tue, 14 Nov 2017 09:33:27 +0000 (10:33 +0100)
include/xbt/algorithm.hpp
src/simix/smx_global.cpp
src/surf/surf_interface.hpp

index 20e148e..67e4c2e 100644 (file)
 namespace simgrid {
 namespace xbt {
 
+/** @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
+ * second.  Useful to have priority queues with the smallest element on top.
+ */
+template <class Pair> class HeapComparator {
+public:
+  bool operator()(const Pair& a, const Pair& b) const { return a.first > b.first; }
+};
+
 /** @brief Sorts the elements of the sequence [first, last) according to their color assuming elements can have only
  * three colors.  Since there are only three colors, it is linear and much faster than a classical sort.  See for
  * example http://en.wikipedia.org/wiki/Dutch_national_flag_problem
index 50019ea..d6a29e1 100644 (file)
@@ -66,7 +66,7 @@ public:
 
 namespace {
 typedef std::pair<double, smx_timer_t> TimerQelt;
-std::priority_queue<TimerQelt, std::vector<TimerQelt>, std::greater<TimerQelt>> simix_timers;
+std::priority_queue<TimerQelt, std::vector<TimerQelt>, simgrid::xbt::HeapComparator<TimerQelt>> simix_timers;
 void SIMIX_timer_flush()
 {
   while (not simix_timers.empty() && simix_timers.top().second->isDisabled()) {
index 40f18b0..4f5dcb8 100644 (file)
@@ -6,6 +6,7 @@
 #ifndef SURF_MODEL_H_
 #define SURF_MODEL_H_
 
+#include "xbt/algorithm.hpp"
 #include "xbt/signal.hpp"
 
 #include "src/surf/surf_private.hpp"
@@ -76,12 +77,8 @@ namespace simgrid {
 namespace surf {
 
 typedef std::pair<double, simgrid::surf::Action*> heap_element_type;
-class heap_element_compare {
-public:
-  bool operator()(const heap_element_type& a, const heap_element_type& b) const { return a.first > b.first; }
-};
 typedef boost::heap::pairing_heap<heap_element_type, boost::heap::constant_time_size<false>, boost::heap::stable<true>,
-                                  boost::heap::compare<heap_element_compare>>
+                                  boost::heap::compare<simgrid::xbt::HeapComparator<heap_element_type>>>
     heap_type;
 
 /** @ingroup SURF_interface