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.
22 template <typename ...U>
23 stable_prio(uint64_t o, U... u) : p(u...), order(o)
27 // zero-argument constructor should not really be needed, but some
28 // heap implementations aren't yet perfect.
34 template <typename P, typename C>
35 class compare_stable_prio
38 bool operator()(const stable_prio<P> &a, const stable_prio<P> &b)
48 return a.order < b.order;
53 template <template <typename H1, typename H2, typename H3> class H, typename T, typename P, typename C = std::less<P>>
54 class stable_heap : private H<T,stable_prio<P>,compare_stable_prio<P,C>>
56 using Base = H<T,stable_prio<P>,compare_stable_prio<P,C>>;
58 // using H<T,P,compare_stable_prio<P,C>>:H; // inherit constructors
61 uint64_t sequence = 0;
65 using handle_t = typename Base::handle_t;
66 using handle_t_r = typename Base::handle_t_r;
68 bool insert(handle_t & index, P pval = P())
70 auto sp = stable_prio<P>(sequence++, pval);
71 return Base::insert(index, sp);
74 template <typename ...U> void allocate(handle_t & hnd, U&& ...u)
76 Base::allocate(hnd, u...);
79 static void init_handle(handle_t &hndl)
81 Base::init_handle(hndl);
84 T &node_data(handle_t &hndl)
86 return Base::node_data(hndl);
89 bool is_queued(handle_t & hnd)
91 return Base::is_queued(hnd);
94 decltype(std::declval<Base>().get_root()) get_root()
96 return Base::get_root();
104 void deallocate(handle_t_r index)
106 Base::deallocate(index);
109 void remove(handle_t_r hnd)
116 return Base::empty();
120 } // namespace dasynq