From 95390b99801a3539e0b0f8510d4e68058f2c47df Mon Sep 17 00:00:00 2001 From: Davin McCall Date: Sun, 24 Dec 2017 11:36:26 +0000 Subject: [PATCH] Update Dasynq to 1.0.3. --- src/dasynq/dasynq-daryheap.h | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/dasynq/dasynq-daryheap.h b/src/dasynq/dasynq-daryheap.h index 36209fc..9bb61ca 100644 --- a/src/dasynq/dasynq-daryheap.h +++ b/src/dasynq/dasynq-daryheap.h @@ -46,7 +46,6 @@ class dary_heap using hindex_t = typename decltype(hvec)::size_type; - int root_node = -1; hindex_t num_nodes = 0; public: @@ -56,7 +55,15 @@ class dary_heap // separate container, and have the handle be an index into that container). struct handle_t { - T hd; + union hd_u_t { + // The data member is kept in a union so it doesn't get constructed/destructed + // automatically, and we can construct it lazily. + public: + hd_u_t() { } + ~hd_u_t() { } + T hd; + } hd_u; + hindex_t heap_index; handle_t(const handle_t &) = delete; @@ -111,8 +118,12 @@ class dary_heap void bubble_up(hindex_t pos, handle_t &h, const P &p) noexcept { - Compare lt; hindex_t rmax = hvec.size() - 1; + if (rmax == 0) { + return; + } + + Compare lt; hindex_t max = (rmax - 1) / N; while (pos <= max) { @@ -155,14 +166,14 @@ class dary_heap T & node_data(handle_t & index) noexcept { - return index.hd; + return index.hd_u.hd; } // Allocate a slot, but do not incorporate into the heap: // u... : parameters for data constructor T::T(...) template void allocate(handle_t & hnd, U... u) { - new (& hnd.hd) T(u...); + new (& hnd.hd_u.hd) T(u...); hnd.heap_index = -1; constexpr hindex_t max_allowed = std::numeric_limits::is_signed ? std::numeric_limits::max() : ((hindex_t) - 2); @@ -193,6 +204,7 @@ class dary_heap void deallocate(handle_t & index) noexcept { num_nodes--; + index.hd_u.hd.~T(); // shrink the capacity of hvec if num_nodes is sufficiently less than // its current capacity: -- 2.25.1