Skip to content

Commit

Permalink
Updated version and added namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriele A. Ron committed Aug 22, 2022
1 parent 2ffc4f9 commit 9d66dd4
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 66 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.24)
project(FibonacciHeap VERSION 1.1.0 LANGUAGES CXX)
project(FibonacciHeap VERSION 2.0.0 LANGUAGES CXX)

################
# Dependencies #
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ This project is designed with Modern CMake principals in mind, so it can easily
Either install or otherwise link the project, and add:

```cmake
find_package(FibonacciHeap 1.0.0 REQUIRED)
find_package(FibonacciHeap 2.0.0 REQUIRED)
# Add Targets
target_link_libraries(foo PRIVATE GRon::FibonacciHeap)
```

## API
The API for the Fibonacci Heap is quite simple:

* `FibonacciHeap<std::three_way_comparable T, class Container = std::list>`
* `GRon::FibonacciHeap<std::three_way_comparable T, class Container = std::list>`
* Creates a Fibonacci Heap object with the provided key type T.
T **must** be `three_way_comparable`, hence the notation above.

Expand Down
2 changes: 1 addition & 1 deletion benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cmake_policy(SET CMP0126 NEW)

project(HeapBenchmark VERSION 0.0.0 LANGUAGES CXX)

find_package(FibonacciHeap 1.0.0 REQUIRED)
find_package(FibonacciHeap 2.0.0 REQUIRED)

if(NOT EXISTS "${PROJECT_SOURCE_DIR}/extern/CppBenchmark/CMakeLists.txt")
message(FATAL_ERROR "The submodules were not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules and try again.")
Expand Down
2 changes: 1 addition & 1 deletion benchmark/app/int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace GRon {
template<typename T>
class priority_queue_mfh : public FibonacciHeap<T> {
class priority_queue_mfh : public FibonacciHeap<T> {
public:

priority_queue_mfh() = default;
Expand Down
109 changes: 64 additions & 45 deletions include/GRon/FibonacciHeap/FibonacciHeap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,61 +10,80 @@
#include <optional>
#include <ostream>

template<std::three_way_comparable T, template<typename...> class Container = std::list>
class FibonacciHeap {
public:
struct Node {
std::optional<T> key{std::nullopt};
bool marked{false};
Container<Node*> children{};
Node* parent{nullptr};

Node() = default;
explicit Node(T value) : key(value) {};
Node(const Node&) = default;
Node(Node&&) noexcept = default;
Node& operator=(const Node&) = default;
Node& operator=(Node&&) noexcept = default;
virtual ~Node() = default;

[[nodiscard]] size_t degree() const;
[[nodiscard]] size_t size() const;
namespace GRon {
template<std::three_way_comparable T, template<typename...> class Container = std::list>
class FibonacciHeap {
public:
struct Node {
std::optional<T> key{std::nullopt};
bool marked{false};
Container<Node *> children{};
Node *parent{nullptr};

bool operator==(const Node& obj) const;
bool operator==(const T& obj) const;
Node() = default;

std::weak_ordering operator<=>(Node& obj) const;
std::weak_ordering operator<=>(T& obj) const;
};
explicit Node(T value) : key(value) {};

Node(const Node &) = default;

Node(Node &&) noexcept = default;

Node &operator=(const Node &) = default;

Node &operator=(Node &&) noexcept = default;

virtual ~Node() = default;

[[nodiscard]] size_t degree() const;

[[nodiscard]] size_t size() const;

bool operator==(const Node &obj) const;

bool operator==(const T &obj) const;

Container<Node*> root_list;
std::weak_ordering operator<=>(Node &obj) const;

FibonacciHeap() = default;
explicit FibonacciHeap(size_t reserve) : _size(0), _clean(false), _minimum(nullptr), _nodes(reserve), _removed(),root_list() {};
FibonacciHeap(const FibonacciHeap&) = default;
FibonacciHeap(FibonacciHeap&&) noexcept = default;
FibonacciHeap& operator=(const FibonacciHeap&) = default;
FibonacciHeap& operator=(FibonacciHeap&&) noexcept = default;
virtual ~FibonacciHeap() = default;
std::weak_ordering operator<=>(T &obj) const;
};

[[nodiscard]] size_t size() const;
Container<Node *> root_list;

void insert(const T& key);
FibonacciHeap() = default;

Node* get_minimum();
std::optional<Node> pop_minimum();
explicit FibonacciHeap(size_t reserve) : _size(0), _clean(false), _minimum(nullptr), _nodes(reserve),
_removed(), root_list() {};

void alter_key(const T& key, const T& new_key);
FibonacciHeap(const FibonacciHeap &) = default;

protected:
size_t _size{0};
bool _clean{false};
Node* _minimum{nullptr};
Container<Node> _nodes;
Container<Node*> _removed;
FibonacciHeap(FibonacciHeap &&) noexcept = default;

void cut_key(Node* node, const T& key);
};
FibonacciHeap &operator=(const FibonacciHeap &) = default;

FibonacciHeap &operator=(FibonacciHeap &&) noexcept = default;

virtual ~FibonacciHeap() = default;

[[nodiscard]] size_t size() const;

void insert(const T &key);

Node *get_minimum();

std::optional<Node> pop_minimum();

void alter_key(const T &key, const T &new_key);

protected:
size_t _size{0};
bool _clean{false};
Node *_minimum{nullptr};
Container<Node> _nodes;
Container<Node *> _removed;

void cut_key(Node *node, const T &key);
};
}

#include "GRon/FibonacciHeap/template/FibonacciHeap.tpp"
#endif //GRON_FIBONACCI_HEAP_HPP
24 changes: 12 additions & 12 deletions include/GRon/FibonacciHeap/template/FibonacciHeap.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

/// Node
template<std::three_way_comparable T, template<typename...> class Container>
size_t FibonacciHeap<T, Container>::Node::degree() const {
size_t GRon::FibonacciHeap<T, Container>::Node::degree() const {
return children.size();
}

template<std::three_way_comparable T, template<typename...> class Container>
size_t FibonacciHeap<T, Container>::Node::size() const {
size_t GRon::FibonacciHeap<T, Container>::Node::size() const {
size_t ret = 1;

for (auto& i : children) {
Expand All @@ -23,33 +23,33 @@ size_t FibonacciHeap<T, Container>::Node::size() const {
}

template<std::three_way_comparable T, template<typename...> class Container>
bool FibonacciHeap<T, Container>::Node::operator==(const FibonacciHeap::Node& obj) const {
bool GRon::FibonacciHeap<T, Container>::Node::operator==(const FibonacciHeap::Node& obj) const {
return key == obj.key;
}

template<std::three_way_comparable T, template<typename...> class Container>
bool FibonacciHeap<T, Container>::Node::operator==(const T& obj) const {
bool GRon::FibonacciHeap<T, Container>::Node::operator==(const T& obj) const {
return key == obj;
}

template<std::three_way_comparable T, template<typename...> class Container>
std::weak_ordering FibonacciHeap<T, Container>::Node::operator<=>(Node& obj) const {
std::weak_ordering GRon::FibonacciHeap<T, Container>::Node::operator<=>(Node& obj) const {
return key <=> obj.key;
}

template<std::three_way_comparable T, template<typename...> class Container>
std::weak_ordering FibonacciHeap<T, Container>::Node::operator<=>(T& obj) const {
std::weak_ordering GRon::FibonacciHeap<T, Container>::Node::operator<=>(T& obj) const {
return key <=> obj;
}

/// Fibonacci Heap
template<std::three_way_comparable T, template<typename...> class Container>
size_t FibonacciHeap<T, Container>::size() const {
size_t GRon::FibonacciHeap<T, Container>::size() const {
return _size;
}

template<std::three_way_comparable T, template<typename...> class Container>
void FibonacciHeap<T, Container>::insert(const T& key) {
void GRon::FibonacciHeap<T, Container>::insert(const T& key) {
_clean = true;
_size++;

Expand All @@ -64,7 +64,7 @@ void FibonacciHeap<T, Container>::insert(const T& key) {
}

template<std::three_way_comparable T, template<typename...> class Container>
typename FibonacciHeap<T, Container>::Node* FibonacciHeap<T, Container>::get_minimum() {
typename GRon::FibonacciHeap<T, Container>::Node* GRon::FibonacciHeap<T, Container>::get_minimum() {
if (_clean) {


Expand Down Expand Up @@ -124,7 +124,7 @@ typename FibonacciHeap<T, Container>::Node* FibonacciHeap<T, Container>::get_min
}

template<std::three_way_comparable T, template<typename...> class Container>
std::optional<typename FibonacciHeap<T, Container>::Node> FibonacciHeap<T, Container>::pop_minimum() {
std::optional<typename GRon::FibonacciHeap<T, Container>::Node> GRon::FibonacciHeap<T, Container>::pop_minimum() {
const Node* min = get_minimum();

if (!min) return std::nullopt;
Expand All @@ -147,7 +147,7 @@ std::optional<typename FibonacciHeap<T, Container>::Node> FibonacciHeap<T, Conta
}

template<std::three_way_comparable T, template<typename...> class Container>
void FibonacciHeap<T, Container>::cut_key(FibonacciHeap::Node* node, const T& key) {
void GRon::FibonacciHeap<T, Container>::cut_key(FibonacciHeap::Node* node, const T& key) {
if (!node) return;

_clean = true;
Expand All @@ -171,7 +171,7 @@ void FibonacciHeap<T, Container>::cut_key(FibonacciHeap::Node* node, const T& ke
}

template<std::three_way_comparable T, template<typename...> class Container>
void FibonacciHeap<T, Container>::alter_key(const T& key, const T& new_key) {
void GRon::FibonacciHeap<T, Container>::alter_key(const T& key, const T& new_key) {
auto old = std::find(_nodes.begin(), _nodes.end(), key);
if (old == _nodes.end()) return;

Expand Down
2 changes: 1 addition & 1 deletion tests/profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ int main() {

int pop_cnt = 0, insert_cnt = 0;

FibonacciHeap<int> test = FibonacciHeap<int>(static_cast<size_t>(n));
GRon::FibonacciHeap<int> test = GRon::FibonacciHeap<int>(static_cast<size_t>(n));
for (int i = 0; i < n; i++) {
if (!(i % one)) {
std::cout << '\r' << (i/(double) n) * 100 << "% Complete" << std::flush;
Expand Down
6 changes: 3 additions & 3 deletions tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <algorithm>

TEST_CASE("heap basic functions", "[heap]") {
FibonacciHeap<int> test;
GRon::FibonacciHeap<int> test;

SECTION("init") {
REQUIRE(test.size() == 0);
Expand Down Expand Up @@ -50,7 +50,7 @@ TEST_CASE("heap basic functions", "[heap]") {

SECTION("pop_minimum") {
int min_key = min->key.value();
FibonacciHeap<int>::Node pop_min = test.pop_minimum().value();
GRon::FibonacciHeap<int>::Node pop_min = test.pop_minimum().value();

REQUIRE(test.size() == 3);

Expand All @@ -76,7 +76,7 @@ TEST_CASE("heap basic functions", "[heap]") {
test.get_minimum();
test.alter_key(15, -15);

FibonacciHeap<int>::Node* min = test.get_minimum();
GRon::FibonacciHeap<int>::Node* min = test.get_minimum();

REQUIRE(min->key == -15);

Expand Down

0 comments on commit 9d66dd4

Please sign in to comment.