Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paring heap without shared_ptr #1707

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 85 additions & 91 deletions libopenage/datastructure/pairing_heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class PairingHeap;


template <typename T, typename compare = std::less<T>>
class PairingHeapNode : public std::enable_shared_from_this<PairingHeapNode<T, compare>> {
class PairingHeapNode {
public:
using this_type = PairingHeapNode<T, compare>;

Expand All @@ -47,7 +47,6 @@ class PairingHeapNode : public std::enable_shared_from_this<PairingHeapNode<T, c
T data;
compare cmp;

public:
PairingHeapNode(const T &data) :
data{data} {}

Expand All @@ -56,6 +55,10 @@ class PairingHeapNode : public std::enable_shared_from_this<PairingHeapNode<T, c

~PairingHeapNode() = default;

PairingHeapNode(const this_type &other) = delete;

this_type &operator=(const this_type &other) = delete;

/**
* Get contained node data.
*/
Expand All @@ -66,14 +69,14 @@ class PairingHeapNode : public std::enable_shared_from_this<PairingHeapNode<T, c
/**
* Let this node become a child of the given one.
*/
void become_child_of(const std::shared_ptr<this_type> &node) {
node->add_child(this->shared_from_this());
void become_child_of(this_type *const node) {
node->add_child(this);
}

/**
* Add the given node as a child to this one.
*/
void add_child(const std::shared_ptr<this_type> &new_child) {
void add_child(this_type *const new_child) {
// first child is the most recently attached one
// it must not have siblings as they will get lost.

Expand All @@ -85,31 +88,31 @@ class PairingHeapNode : public std::enable_shared_from_this<PairingHeapNode<T, c
}

this->first_child = new_child;
new_child->parent = this->shared_from_this();
new_child->parent = this;
}

/**
* This method decides which node becomes the new root node
* by comparing `this` with `node`.
* The new root is returned, it has the other node as child.
*/
std::shared_ptr<this_type> link_with(const std::shared_ptr<this_type> &node) {
std::shared_ptr<this_type> new_root;
std::shared_ptr<this_type> new_child;
this_type *link_with(this_type *const node) {
this_type *new_root;
this_type *new_child;

if (this->cmp(this->data, node->data)) {
new_root = this->shared_from_this();
new_root = this;
new_child = node;
}
else {
new_root = node;
new_child = this->shared_from_this();
new_child = this;
}

// children of new root become siblings of new new_child
// -> parent of new child = new root

// this whll be set by the add_child method
// this will be set by the add_child method
new_child->prev_sibling = nullptr;
new_child->next_sibling = nullptr;

Expand All @@ -128,15 +131,15 @@ class PairingHeapNode : public std::enable_shared_from_this<PairingHeapNode<T, c
* Recursive call, one stage for each all childs of the root node.
* This results in the computation of the new subtree root.
*/
std::shared_ptr<this_type> link_backwards() {
this_type *link_backwards() {
if (this->next_sibling == nullptr) {
// reached end, return this as current root,
// the previous siblings will be linked to it.
return this->shared_from_this();
return this;
}

// recurse to last sibling,
std::shared_ptr<this_type> node = this->next_sibling->link_backwards();
this_type *node = this->next_sibling->link_backwards();

// then link ourself to the new root.
this->next_sibling = nullptr;
Expand All @@ -153,9 +156,9 @@ class PairingHeapNode : public std::enable_shared_from_this<PairingHeapNode<T, c
*/
void loosen() {
// release us from some other node
if (this->parent and this->parent->first_child == this->shared_from_this()) {
// we are the first child
// make the next sibling the first child
if (this->parent and this->parent->first_child == this) {
// we are child
// make the next sibling child
this->parent->first_child = this->next_sibling;
}
// if we have a previous sibling
Expand All @@ -176,10 +179,10 @@ class PairingHeapNode : public std::enable_shared_from_this<PairingHeapNode<T, c
}

private:
std::shared_ptr<this_type> first_child;
std::shared_ptr<this_type> prev_sibling;
std::shared_ptr<this_type> next_sibling;
std::shared_ptr<this_type> parent; // for decrease-key and delete
this_type *first_child = nullptr;
this_type *prev_sibling = nullptr;
this_type *next_sibling = nullptr;
this_type *parent = nullptr; // for decrease-key and delete
};


Expand All @@ -191,10 +194,8 @@ template <typename T,
typename heapnode_t = PairingHeapNode<T, compare>>
class PairingHeap final {
public:
using node_t = heapnode_t;
using element_t = std::shared_ptr<node_t>;
using this_type = PairingHeap<T, compare, node_t>;
using cmp_t = compare;
using element_t = heapnode_t *;
using this_type = PairingHeap<T, compare, heapnode_t>;

/**
* create a empty heap.
Expand All @@ -204,14 +205,16 @@ class PairingHeap final {
root_node(nullptr) {
}

~PairingHeap() = default;
~PairingHeap() {
this->clear();
};

/**
* adds the given item to the heap.
* O(1)
*/
element_t push(const T &item) {
element_t new_node = std::make_shared<node_t>(item);
element_t new_node = new heapnode_t(item);
this->push_node(new_node);
return new_node;
}
Expand All @@ -221,31 +224,30 @@ class PairingHeap final {
* O(1)
*/
element_t push(T &&item) {
element_t new_node = std::make_shared<node_t>(std::move(item));
element_t new_node = new heapnode_t(std::move(item));
this->push_node(new_node);
return new_node;
}

/**
* returns and removes the smallest item on the heap.
*/
T pop() {
return std::move(this->pop_node()->data);
}

/**
* returns the smallest item on the heap and deletes it.
* also known as delete_min.
* _________
* Ω(log log n), O(2^(2*√log log n'))
*/
element_t pop_node() {
T pop() {
if (this->root_node == nullptr) {
throw Error{MSG(err) << "Can't pop an empty heap!"};
}

// 0. remove tree root, it's the minimum.
element_t ret = this->root_node;

if (!this->nodes.erase(ret)) {
throw Error{MSG(err) << "didn't remove node"};
}

this->node_count -= 1;
element_t current_sibling = this->root_node->first_child;
this->root_node = nullptr;

Expand Down Expand Up @@ -303,49 +305,14 @@ class PairingHeap final {
this->root_node = first_pair->link_backwards();
}

this->node_count -= 1;

#if OPENAGE_PAIRINGHEAP_DEBUG
if (1 != this->nodes.erase(ret)) {
throw Error{ERR << "didn't remove node"};
}
#endif

// (to find those two lines, 14h of debugging passed)
ret->loosen();
ret->first_child = nullptr;

// and it's done!
return ret;
}

/**
* Unlink a node from the heap.
*
* If the item is the current root, just pop().
* else, cut the node from its parent, pop() that subtree
* and merge these trees.
*
* O(pop_node)
*/
void unlink_node(const element_t &node) {
if (node == this->root_node) {
this->pop_node();
}
else {
node->loosen();

element_t real_root = this->root_node;
this->root_node = node;
this->pop_node();

element_t new_root = this->root_node;
this->root_node = real_root;

if (new_root != nullptr) {
this->root_insert(new_root);
}
}
T data = std::move(ret->data);
delete ret;
return data;
}

/**
Expand Down Expand Up @@ -391,14 +358,43 @@ class PairingHeap final {
*
* O(1) (but slower than decrease), and O(pop) when node is the root.
*/
void update(const element_t &node) {
void update(element_t &node) {
if (node != this->root_node) [[likely]] {
this->unlink_node(node);
this->push_node(node);
node = this->push(this->remove_node(node));
}
else {
// it's the root node, so we just pop and push it.
this->push_node(this->pop_node());
node = this->push(this->pop());
}
}

/**
* remove a node from the heap. Return its data.
*
* If the item is the current root, just pop().
* else, cut the node from its parent, pop() that subtree
* and merge these trees.
*
* O(pop_node)
*/
T remove_node(const element_t &node) {
if (node == this->root_node) {
return this->pop();
}
else {
node->loosen();

element_t real_root = this->root_node;
this->root_node = node;
T data = this->pop();

element_t new_root = this->root_node;
this->root_node = real_root;

if (new_root != nullptr) {
this->root_insert(new_root);
}
return data;
}
}

Expand All @@ -408,9 +404,10 @@ class PairingHeap final {
void clear() {
this->root_node = nullptr;
this->node_count = 0;
#if OPENAGE_PAIRINGHEAP_DEBUG
for (auto &node : nodes) {
delete node;
}
this->nodes.clear();
#endif
}

/**
Expand Down Expand Up @@ -583,7 +580,7 @@ class PairingHeap final {
this->walk_tree(this->root_node, func);
}

protected:
private:
void walk_tree(const element_t &root,
const std::function<void(const element_t &)> &func) const {
func(root);
Expand All @@ -607,16 +604,17 @@ class PairingHeap final {
* O(1)
*/
void push_node(const element_t &node) {
this->root_insert(node);
auto [iter, result] = this->nodes.insert(node);
if (result) {
this->root_insert(node);
this->node_count += 1;
}

#if OPENAGE_PAIRINGHEAP_DEBUG
auto ins = this->nodes.insert(node);
if (not ins.second) {
if (not result) {
throw Error{ERR << "node already known"};
}
#endif

this->node_count += 1;
}

/**
Expand All @@ -631,14 +629,10 @@ class PairingHeap final {
}
}

protected:
compare cmp;
size_t node_count;
element_t root_node;

#if OPENAGE_PAIRINGHEAP_DEBUG
std::unordered_set<element_t> nodes;
#endif
Comment on lines -639 to -641
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could keep this set only when OPENAGE_PAIRINGHEAP_DEBUG is true, so that the eap becomes even more efficient. Looks like it's only necessary for clearing up the nodes on deletion right now. I suggest we could try this solution:

  1. From clear(), use iter_all to iterate through all the nodes and call delete on them.
  2. Wrap all interactions of nodes with the #if OPENAGE_PAIRINGHEAP_DEBUG preprocessor thing again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alright sound like a plan :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@heinezen requested changes Implemented at #1713

};

} // namespace openage::datastructure
5 changes: 3 additions & 2 deletions libopenage/datastructure/tests.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014-2023 the openage authors. See copying.md for legal info.
// Copyright 2014-2024 the openage authors. See copying.md for legal info.

#include "tests.h"

Expand Down Expand Up @@ -118,7 +118,8 @@ void pairing_heap_2() {
heap.push(heap_elem{3});

// state: 1 2 3, now remove 2
heap.unlink_node(node);
auto data = heap.remove_node(node);
TESTEQUALS(data.data, 2);

// state: 1 3
TESTEQUALS(heap.pop().data, 1);
Expand Down
4 changes: 2 additions & 2 deletions libopenage/event/eventstore.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2023 the openage authors. See copying.md for legal info.
// Copyright 2018-2024 the openage authors. See copying.md for legal info.

#include "eventstore.h"

Expand Down Expand Up @@ -56,7 +56,7 @@ bool EventStore::erase(const std::shared_ptr<Event> &event) {
bool erased = false;
auto it = this->events.find(event);
if (it != std::end(this->events)) {
this->heap.unlink_node(it->second);
this->heap.remove_node(it->second);
this->events.erase(it);
erased = true;
}
Expand Down
Loading