0

I'm using Boost's Fibonacci heap in my code, and I noticed online it's possible to update an erased/popped element using it's handle: Why can I update a popped out element in a boost::fibonacci_heap?

I have a class wrapping the fib heap and has it's own add, erase, update operations. My question is, what's the best way to enforce a popped/removed element won't be updated?

I store the handle as a member:

typedef boost::heap::fibonacci_heap<EventPtr, boost::heap::compare<sched_events_comp>> fib_heap;

        class Event
        {
          private:
            sched_time_min_t _time;
            sched_event_cb_t _cb;
            fib_heap::handle_type _handle;

            explicit Event(const sched_event_cb_t& cb);

            friend class Sched;
        }

right now this is what I do - when I pop/remove the element I nullify the node:

event->_handle.node_ = nullptr;

when I update an element I assert assert(event->_handle.node_ != nulltptr);. What I don't like is that I change inner members of handle_type which is not implemented by me. I can add another boolean member in my Event class but I'd prefer not to add more members.

what's the best way to assert the element is in the heap before updating (of course in constant time)? thank you!

Wolf
  • 77
  • 8
  • "and I noticed online it's possible to update an erased/popped element using it's handle: .." Note that this notion of "possible" is a little off, because in the presence of UB really everything is possible. What is possible in C++ code that has defined behavior is something else – 463035818_is_not_an_ai Nov 15 '22 at 10:21
  • @463035818_is_not_a_number True, I want to eliminate the possibility of UB with this particular operations. – Wolf Nov 15 '22 at 10:22
  • In general in C++ you cannot avoid people holding on to references outside your control (escape analysis). If you really need to know container membership, perhaps Boost Intrusive fits your needs (with safe/auto-unlink link modes), possible starting point https://www.boost.org/doc/libs/1_80_0/doc/html/intrusive/treap_set_multiset.html (but be sure to scan up the documentation hierarchy) – sehe Nov 15 '22 at 13:21

0 Answers0