From 9466e4aa8c3bf7fa8124a022715843546e4e0753 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Mon, 13 Nov 2017 22:08:27 +0100 Subject: [PATCH] Define and use utility class to compare pairs in priority queues. --- include/xbt/algorithm.hpp | 10 ++++++++++ src/simix/smx_global.cpp | 2 +- src/surf/surf_interface.hpp | 7 ++----- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/include/xbt/algorithm.hpp b/include/xbt/algorithm.hpp index 20e148e3c9..67e4c2ec68 100644 --- a/include/xbt/algorithm.hpp +++ b/include/xbt/algorithm.hpp @@ -11,6 +11,16 @@ 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 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 diff --git a/src/simix/smx_global.cpp b/src/simix/smx_global.cpp index 50019eae59..d6a29e1b4f 100644 --- a/src/simix/smx_global.cpp +++ b/src/simix/smx_global.cpp @@ -66,7 +66,7 @@ public: namespace { typedef std::pair TimerQelt; -std::priority_queue, std::greater> simix_timers; +std::priority_queue, simgrid::xbt::HeapComparator> simix_timers; void SIMIX_timer_flush() { while (not simix_timers.empty() && simix_timers.top().second->isDisabled()) { diff --git a/src/surf/surf_interface.hpp b/src/surf/surf_interface.hpp index 40f18b0188..4f5dcb8734 100644 --- a/src/surf/surf_interface.hpp +++ b/src/surf/surf_interface.hpp @@ -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 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, boost::heap::stable, - boost::heap::compare> + boost::heap::compare>> heap_type; /** @ingroup SURF_interface -- 2.20.1