1 #ifndef DASYNQ_STABLE_HEAP_INCLUDED
2 #define DASYNQ_STABLE_HEAP_INCLUDED 1
4 // Convert an "unstable" priority queue (which doesn't use FIFO ordering for same-priority elements)
5 // into a "stable" queue (which does deliver same-priority elements in FIFO order). This is done by
6 // adding a generation counter to each element added to the queue, and using it as a second-order
7 // priority key (after the original key).
9 // The generation counter is a 64-bit integer and can not realistically overflow.
23 template <typename ...U>
24 stable_prio(uint64_t o, U... u) : p(u...), order(o)
28 // zero-argument constructor should not really be needed, but some
29 // heap implementations aren't yet perfect.
35 template <typename P, typename C>
36 class compare_stable_prio
39 bool operator()(const stable_prio<P> &a, const stable_prio<P> &b)
49 return a.order < b.order;
54 template <template <typename H1, typename H2, typename H3> class H, typename T, typename P, typename C = std::less<P>>
55 class stable_heap : private H<T,stable_prio<P>,compare_stable_prio<P,C>>
57 using Base = H<T,stable_prio<P>,compare_stable_prio<P,C>>;
59 // using H<T,P,compare_stable_prio<P,C>>:H; // inherit constructors
62 uint64_t sequence = 0;
66 using handle_t = typename Base::handle_t;
67 using handle_t_r = typename Base::handle_t_r;
69 bool insert(handle_t & index, P pval = P())
71 auto sp = stable_prio<P>(sequence++, pval);
72 return Base::insert(index, sp);
75 template <typename ...U> void allocate(handle_t & hnd, U&& ...u)
77 Base::allocate(hnd, std::forward<U>(u)...);
80 static void init_handle(handle_t &hndl)
82 Base::init_handle(hndl);
85 T &node_data(handle_t &hndl)
87 return Base::node_data(hndl);
90 bool is_queued(handle_t & hnd)
92 return Base::is_queued(hnd);
95 decltype(std::declval<Base>().get_root()) get_root()
97 return Base::get_root();
105 void deallocate(handle_t_r index)
107 Base::deallocate(index);
110 void remove(handle_t_r hnd)
117 return Base::empty();
121 } // namespace dasynq