using hindex_t = typename decltype(hvec)::size_type;
- int root_node = -1;
hindex_t num_nodes = 0;
public:
// 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;
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) {
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 <typename ...U> 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<hindex_t>::is_signed ?
std::numeric_limits<hindex_t>::max() : ((hindex_t) - 2);
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: