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 #5

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
535b6cb
etc: clang-format rule for preprocessor indentation.
heinezen Oct 31, 2024
3c47b65
refactor: Format cpp files with preprocessor code.
heinezen Oct 31, 2024
31f74ee
Merge pull request #1702 from heinezen/fx/clang_format_ppindent
TheJJ Nov 1, 2024
65d2f7a
coord: Make comparisons not ambiguous.
heinezen Oct 31, 2024
45a7dd5
Merge pull request #1703 from heinezen/fix/ambiguous_compare
TheJJ Nov 2, 2024
4f53084
includes DbgHelp.h after windows.h to prevent errors
jere8184 Nov 6, 2024
d023a2f
Merge pull request #1705 from jere8184/rearrange_dbghelp
TheJJ Nov 6, 2024
73f06b7
Renderer: Added camera boundaries to CameraManager and clamping to th…
EdvinLndh Sep 25, 2024
85918c9
Refactoring and documentation.
EdvinLndh Oct 18, 2024
b907a7d
Added constructor for CameraBoundaries.
EdvinLndh Oct 18, 2024
780949f
Struct refactoring
EdvinLndh Oct 20, 2024
1556502
Refactoring and cleanup
EdvinLndh Oct 24, 2024
0c129fb
Changed default minimum boundary to be largest negative value instead…
EdvinLndh Oct 24, 2024
59cee6a
Fixes for Y boundary
EdvinLndh Oct 31, 2024
af804e6
renderer: Create boundaries.cpp file for compilation checks.
heinezen Nov 7, 2024
179e1ad
renderer: Add method to change camera boundaries in cam manager.
heinezen Nov 7, 2024
6d6a2dd
renderer: Rename current hardcoded camera boundaries.
heinezen Nov 7, 2024
a3376ca
Include missing email for #1691
heinezen Nov 7, 2024
2170ff5
Merge pull request #1691 from EdvinLndh/add_camera_boundaries
heinezen Nov 7, 2024
e4c6891
wip
jere8184 Nov 7, 2024
9d1fefc
wip
jere8184 Nov 7, 2024
81105c0
Merge branch 'paring_heap' of https://github.com/jere8184/openage int…
jere8184 Nov 7, 2024
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
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ IndentCaseBlocks: false
IndentCaseLabels: false
IndentExternBlock: NoIndent
IndentGotoLabels: false
IndentPPDirectives: None
IndentPPDirectives: BeforeHash
IndentWidth: 4
IndentWrappedFunctionNames: false
# clang-format-16 InsertNewlineAtEOF: true
Expand Down
1 change: 1 addition & 0 deletions copying.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ _the openage authors_ are:
| Michael Seibt | RoboSchmied | github à roboschmie dawt de |
| Nikhil Ghosh | NikhilGhosh75 | nghosh606 à gmail dawt com |
| Edvin Lindholm | EdvinLndh | edvinlndh à gmail dawt com |
| Jeremiah Morgan | jere8184 | jeremiahmorgan dawt bham à outlook dawt com |

If you're a first-time committer, add yourself to the above list. This is not
just for legal reasons, but also to keep an overview of all those nicknames.
Expand Down
18 changes: 9 additions & 9 deletions libopenage/console/tests.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright 2014-2019 the openage authors. See copying.md for legal info.
// Copyright 2014-2024 the openage authors. See copying.md for legal info.

#include <vector>
#include <string>
#include <vector>

#ifdef _MSC_VER
#define STDOUT_FILENO 1
#define STDOUT_FILENO 1
#else
#include <unistd.h>
#include <unistd.h>
#endif
#include "../util/fds.h"
#include "../util/pty.h"
Expand All @@ -15,8 +15,8 @@
#include <cstdlib>
#include <fcntl.h>

#include "../log/log.h"
#include "../error/error.h"
#include "../log/log.h"

#include "buf.h"
#include "console.h"
Expand Down Expand Up @@ -51,7 +51,7 @@ void render() {


void interactive() {
#ifndef _WIN32
#ifndef _WIN32

console::Buf buf{{80, 25}, 1337, 80};
struct winsize ws;
Expand All @@ -75,7 +75,7 @@ void interactive() {
throw Error(MSG(err) << "execl(\"" << shell << "\", \"" << shell << "\", nullptr) failed: " << strerror(errno));
}
default:
//we are the parent
// we are the parent
break;
}

Expand Down Expand Up @@ -143,7 +143,7 @@ void interactive() {
ssize_t retval = read(ptyin.fd, rdbuf, rdbuf_size);
switch (retval) {
case -1:
switch(errno) {
switch (errno) {
case EIO:
loop = false;
break;
Expand Down Expand Up @@ -175,7 +175,7 @@ void interactive() {
// show cursor
termout.puts("\x1b[?25h");

#endif /* _WIN32 */
#endif /* _WIN32 */
}


Expand Down
16 changes: 4 additions & 12 deletions libopenage/coord/coord.h.template
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,8 @@ struct Coord${camelcase}Absolute {
return static_cast<Absolute &>(*this);
}

constexpr bool operator ==(const Absolute &other) const {
return ${formatted_members("(this->{0} == other.{0})", join_with=" && ")};
}

constexpr bool operator !=(const Absolute &other) const {
return !(*this == other);
friend constexpr bool operator ==(const Absolute &lhs, const Absolute &rhs) {
return ${formatted_members("(lhs.{0} == rhs.{0})", join_with=" && ")};
}
};

Expand Down Expand Up @@ -167,12 +163,8 @@ struct Coord${camelcase}Relative {
return static_cast<Relative &>(*this);
}

constexpr bool operator ==(const Relative &other) const {
return ${formatted_members("(this->{0} == other.{0})", join_with=" && ")};
}

constexpr bool operator !=(const Relative &other) const {
return !(*this == other);
friend constexpr bool operator ==(const Relative &lhs, const Relative &rhs) {
return ${formatted_members("(lhs.{0} == rhs.{0})", join_with=" && ")};
}
};

Expand Down
59 changes: 31 additions & 28 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,14 +47,17 @@ class PairingHeapNode : public std::enable_shared_from_this<PairingHeapNode<T, c
T data;
compare cmp;

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

PairingHeapNode(T &&data) :
data{std::move(data)} {}

~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(const this_type& 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(const this_type &new_child) {
// first child is the most recently attached one
// it must not have siblings as they will get lost.

Expand All @@ -85,25 +88,25 @@ 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(const this_type* 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
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,7 +156,7 @@ 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()) {
if (this->parent and this->parent->first_child == this) {
// we are the first child
// make the next sibling the first child
this->parent->first_child = this->next_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;
this_type* prev_sibling;
this_type* next_sibling;
this_type* parent; // 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 @@ -211,7 +212,7 @@ class PairingHeap final {
* 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,7 +222,7 @@ 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;
}
Expand All @@ -230,7 +231,10 @@ class PairingHeap final {
* returns and removes the smallest item on the heap.
*/
T pop() {
return std::move(this->pop_node()->data);
element_t poped_node = this->pop_node();
T data = std::move(poped_node->data);
delete poped_node;
return data;
}

/**
Expand Down Expand Up @@ -583,7 +587,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 Down Expand Up @@ -631,7 +635,6 @@ class PairingHeap final {
}
}

protected:
compare cmp;
size_t node_count;
element_t root_node;
Expand Down
6 changes: 3 additions & 3 deletions libopenage/engine/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

// TODO: Remove custom jthread definition when clang/libc++ finally supports it
#if __llvm__
#if !__cpp_lib_jthread
#if !__cpp_lib_jthread
namespace std {
class jthread : public thread {
public:
Expand All @@ -27,9 +27,9 @@ class jthread : public thread {
}
};
} // namespace std
#endif
#endif
#else
#include <stop_token>
#include <stop_token>
#endif


Expand Down
6 changes: 3 additions & 3 deletions libopenage/error/handlers.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015-2023 the openage authors. See copying.md for legal info.
// Copyright 2015-2024 the openage authors. See copying.md for legal info.

/*
* This file holds handlers for std::terminate and SIGSEGV.
Expand All @@ -17,9 +17,9 @@
#include <iostream>

#ifdef _MSC_VER
#include <io.h>
#include <io.h>
#else
#include <unistd.h>
#include <unistd.h>
#endif

#include "util/init.h"
Expand Down
20 changes: 10 additions & 10 deletions libopenage/error/stackanalyzer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015-2023 the openage authors. See copying.md for legal info.
// Copyright 2015-2024 the openage authors. See copying.md for legal info.

#include "stackanalyzer.h"

Expand Down Expand Up @@ -30,8 +30,8 @@ constexpr uint64_t base_skip_frames = 1;

#if WITH_BACKTRACE

// use modern <backtrace.h>
#include <backtrace.h>
// use modern <backtrace.h>
#include <backtrace.h>

namespace openage {
namespace error {
Expand Down Expand Up @@ -59,7 +59,7 @@ struct backtrace_state *bt_state;
util::OnInit init_backtrace_state([]() {
bt_state = backtrace_create_state(
nullptr, // auto-determine filename
1, // threaded
1, // threaded
backtrace_error_callback,
nullptr // passed to the callback
);
Expand Down Expand Up @@ -189,8 +189,8 @@ void StackAnalyzer::get_symbols(std::function<void(const backtrace_symbol *)> cb

#else // WITHOUT_BACKTRACE

#ifdef _WIN32
#include <windows.h>
#ifdef _WIN32
#include <windows.h>

namespace openage {
namespace error {
Expand All @@ -208,10 +208,10 @@ void StackAnalyzer::analyze() {
} // namespace error
} // namespace openage

#else // not _MSC_VER
#else // not _MSC_VER

// use GNU's <execinfo.h>
#include <execinfo.h>
// use GNU's <execinfo.h>
#include <execinfo.h>

namespace openage::error {

Expand Down Expand Up @@ -256,7 +256,7 @@ void StackAnalyzer::analyze() {

} // namespace openage::error

#endif // for _MSC_VER or GNU execinfo
#endif // for _MSC_VER or GNU execinfo

namespace openage::error {

Expand Down
Loading