-
Notifications
You must be signed in to change notification settings - Fork 17
Feature/edge range #75
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8723531
edge range unifying access to a graph's edges
mschimek 2e74eb1
Update tests/edge_range.cpp
mschimek d913bbf
Update tests/edge_range.cpp
mschimek cb642e9
Update tests/CMakeLists.txt
mschimek 9e7ae63
Refactor edge_range tests to use GoogleTest matchers and parameterize…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| #include "kagen/edge_range.h" | ||
|
|
||
| #include <cassert> | ||
| #include <memory> | ||
|
|
||
| namespace kagen { | ||
|
|
||
| EdgeRange::EdgeRange(const Edgelist& edgelist) noexcept | ||
| : representation_(GraphRepresentation::EDGE_LIST), | ||
| edgelist_ptr_(std::addressof(edgelist)), | ||
| xadj_ptr_(nullptr), | ||
| adjncy_ptr_(nullptr), | ||
| vertex_base_(Vertex(0)) {} | ||
|
|
||
| EdgeRange::EdgeRange(const XadjArray& xadj, const AdjncyArray& adjncy, VertexRange vertex_range) noexcept | ||
| : representation_(GraphRepresentation::CSR), | ||
| edgelist_ptr_(nullptr), | ||
| xadj_ptr_(std::addressof(xadj)), | ||
| adjncy_ptr_(std::addressof(adjncy)), | ||
| vertex_base_(vertex_range.first) { | ||
| assert(xadj_ptr_->size() >= 1); | ||
| } | ||
|
|
||
| EdgeRange::EdgeRange(const Graph& graph) noexcept : EdgeRange(FromGraph(graph)) {} | ||
|
|
||
| EdgeRange EdgeRange::FromGraph(const Graph& g) noexcept { | ||
| if (g.representation == GraphRepresentation::EDGE_LIST) { | ||
| return EdgeRange(g.edges); | ||
| } else { | ||
| return EdgeRange(g.xadj, g.adjncy, g.vertex_range); | ||
| } | ||
| } | ||
|
|
||
| EdgeRange::iterator EdgeRange::iterator::edgelist_begin(const EdgeRange* parent) noexcept { | ||
| iterator it; | ||
| it.parent_ = parent; | ||
| it.idx_ = 0; | ||
| it.load_current(); | ||
| return it; | ||
| } | ||
|
|
||
| EdgeRange::iterator EdgeRange::iterator::edgelist_end(const EdgeRange* parent) noexcept { | ||
| iterator it; | ||
| it.parent_ = parent; | ||
| it.idx_ = parent->edgelist_ptr_->size(); | ||
| return it; | ||
| } | ||
|
|
||
| EdgeRange::iterator EdgeRange::iterator::csr_begin(const EdgeRange* parent) noexcept { | ||
| iterator it; | ||
| it.parent_ = parent; | ||
| it.u_ = 0; | ||
| it.off_ = 0; | ||
| it.init_csr_begin(); // finds first valid (u,off) or sets to end | ||
| it.load_current(); | ||
| return it; | ||
| } | ||
|
|
||
| EdgeRange::iterator EdgeRange::iterator::csr_end(const EdgeRange* parent) noexcept { | ||
| iterator it; | ||
| it.parent_ = parent; | ||
| it.u_ = parent->xadj_ptr_->size() == 0 ? 0u : parent->xadj_ptr_->size() - 1; | ||
| it.off_ = parent->adjncy_ptr_->size(); | ||
| return it; | ||
| } | ||
|
|
||
| std::size_t EdgeRange::iterator::edge_index() const noexcept { | ||
| if (parent_->representation_ == GraphRepresentation::EDGE_LIST) { | ||
| return idx_; | ||
| } else { | ||
| return off_; | ||
| } | ||
| } | ||
|
|
||
| EdgeRange::iterator::value_type EdgeRange::iterator::operator*() const noexcept { | ||
| return cur_; | ||
| } | ||
|
|
||
| EdgeRange::iterator& EdgeRange::iterator::operator++() { | ||
| assert(parent_ != nullptr); | ||
|
|
||
| if (parent_->representation_ == GraphRepresentation::EDGE_LIST) { | ||
| ++idx_; | ||
| load_current(); | ||
| return *this; | ||
| } | ||
|
|
||
| advance_to_next_valid_csr(); | ||
| load_current(); | ||
| return *this; | ||
| } | ||
|
|
||
| EdgeRange::iterator EdgeRange::iterator::operator++(int) { | ||
| iterator tmp = *this; | ||
| ++(*this); | ||
| return tmp; | ||
| } | ||
|
|
||
| bool EdgeRange::iterator::operator==(const iterator& other) const noexcept { | ||
| // must belong to same parent to compare reliably | ||
| if (parent_ != other.parent_) | ||
| return false; | ||
| if (parent_ == nullptr) | ||
| return true; // both default constructed? | ||
| if (parent_->representation_ != other.parent_->representation_) | ||
| return false; | ||
| if (parent_->representation_ == GraphRepresentation::EDGE_LIST) { | ||
| return idx_ == other.idx_; | ||
| } else { | ||
| return (u_ == other.u_) && (off_ == other.off_); | ||
| } | ||
| } | ||
|
|
||
| bool EdgeRange::iterator::operator!=(const iterator& other) const noexcept { | ||
| return !(*this == other); | ||
| } | ||
|
|
||
| void EdgeRange::iterator::load_current() noexcept { | ||
| if (parent_->representation_ == GraphRepresentation::EDGE_LIST) { | ||
| const auto& elist = *parent_->edgelist_ptr_; | ||
| if (idx_ < elist.size()) { | ||
| cur_ = elist[idx_]; | ||
| } | ||
| // else leave cur_ unspecified for end() | ||
| } else { | ||
| const auto& xadj = *parent_->xadj_ptr_; | ||
| const auto& adjncy = *parent_->adjncy_ptr_; | ||
| const std::size_t n_local = xadj.empty() ? 0 : (xadj.size() - 1); | ||
| if (u_ >= n_local || off_ >= adjncy.size()) { | ||
| // end iterator; cur_ remains unspecified | ||
| return; | ||
| } | ||
| Vertex u_global = static_cast<Vertex>(u_) + parent_->vertex_base_; | ||
| Vertex v_global = adjncy[off_]; | ||
| cur_.first = u_global; | ||
| cur_.second = v_global; | ||
| } | ||
| } | ||
|
|
||
| void EdgeRange::iterator::init_csr_begin() noexcept { | ||
| const auto& xadj = *parent_->xadj_ptr_; | ||
| const auto& adjncy = *parent_->adjncy_ptr_; | ||
| const std::size_t n_local = xadj.empty() ? 0 : (xadj.size() - 1); | ||
| if (n_local == 0) { | ||
| // empty CSR: set end | ||
| u_ = 0; | ||
| off_ = 0; | ||
| return; | ||
| } | ||
| off_ = xadj[0]; | ||
| u_ = 0; | ||
| // skip vertices with empty adjacency ranges | ||
| while (u_ < n_local && off_ >= xadj[u_ + 1]) { | ||
| ++u_; | ||
| if (u_ < n_local) | ||
| off_ = xadj[u_]; | ||
| } | ||
| if (u_ >= n_local) { | ||
| // set to end state | ||
| u_ = n_local; | ||
| off_ = adjncy.size(); | ||
| } | ||
| } | ||
|
|
||
| void EdgeRange::iterator::advance_to_next_valid_csr() noexcept { | ||
| // CSR: advance off_, move to next vertex if necessary | ||
| ++off_; | ||
| const auto& xadj = *parent_->xadj_ptr_; | ||
| const std::size_t n_local = xadj.empty() ? 0 : (xadj.size() - 1); | ||
| while (u_ < n_local && off_ >= xadj[u_ + 1]) { | ||
| ++u_; | ||
| if (u_ < n_local) | ||
| off_ = xadj[u_]; | ||
| } | ||
| if (u_ >= n_local) { | ||
| // set canonical end state | ||
| off_ = parent_->adjncy_ptr_->size(); | ||
| } | ||
| } | ||
|
|
||
| EdgeRange::iterator EdgeRange::begin() const noexcept { | ||
| if (representation_ == GraphRepresentation::EDGE_LIST) | ||
| return iterator::edgelist_begin(this); | ||
| return iterator::csr_begin(this); | ||
| } | ||
|
|
||
| EdgeRange::iterator EdgeRange::end() const noexcept { | ||
| if (representation_ == GraphRepresentation::EDGE_LIST) | ||
| return iterator::edgelist_end(this); | ||
| return iterator::csr_end(this); | ||
| } | ||
|
|
||
| std::size_t EdgeRange::size() const noexcept { | ||
| if (representation_ == GraphRepresentation::EDGE_LIST) { | ||
| return edgelist_ptr_->size(); | ||
| } else { | ||
| return adjncy_ptr_->size(); | ||
| } | ||
| } | ||
|
|
||
| } // namespace kagen |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| #pragma once | ||
|
|
||
| #include "kagen/kagen.h" | ||
|
|
||
| #include <cstddef> | ||
| #include <iterator> | ||
| #include <utility> | ||
|
|
||
| namespace kagen { | ||
|
|
||
| class EdgeRange { | ||
| public: | ||
| using Vertex = SInt; | ||
| using Edge = std::pair<Vertex, Vertex>; | ||
|
|
||
| explicit EdgeRange(const Edgelist& edgelist) noexcept; | ||
| EdgeRange(const XadjArray& xadj, const AdjncyArray& adjncy, VertexRange vertex_range) noexcept; | ||
| EdgeRange(const Graph& graph)noexcept; | ||
|
|
||
| static EdgeRange FromGraph(const Graph& graph) noexcept; | ||
|
|
||
| class iterator { | ||
| public: | ||
| using iterator_category = std::forward_iterator_tag; | ||
niklas-uhl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| using value_type = Edge; | ||
| using difference_type = std::ptrdiff_t; | ||
|
|
||
| iterator() noexcept = default; | ||
|
|
||
| static iterator edgelist_begin(const EdgeRange* parent) noexcept; | ||
| static iterator edgelist_end(const EdgeRange* parent) noexcept; | ||
|
|
||
| // begin / end for CSR | ||
| static iterator csr_begin(const EdgeRange* parent) noexcept; | ||
| static iterator csr_end(const EdgeRange* parent) noexcept; | ||
|
|
||
| std::size_t edge_index() const noexcept; | ||
|
|
||
| value_type operator*() const noexcept; | ||
|
|
||
| iterator& operator++(); | ||
|
|
||
| iterator operator++(int); | ||
|
|
||
| bool operator==(const iterator& other) const noexcept; | ||
| bool operator!=(const iterator& other) const noexcept; | ||
|
|
||
| private: | ||
| const EdgeRange* parent_{nullptr}; | ||
|
|
||
| // EDGELIST state | ||
| std::size_t idx_{0}; | ||
|
|
||
| // CSR state | ||
| std::size_t u_{0}; // current vertex index (0..n_local-1) | ||
| std::size_t off_{0}; // current offset into adjncy | ||
|
|
||
| Edge cur_{}; | ||
|
|
||
| void load_current() noexcept; | ||
| void init_csr_begin() noexcept; | ||
| void advance_to_next_valid_csr() noexcept; | ||
| }; | ||
|
|
||
| iterator begin() const noexcept; | ||
| iterator end() const noexcept; | ||
|
|
||
| std::size_t size() const noexcept; | ||
|
|
||
| private: | ||
| GraphRepresentation representation_; | ||
|
|
||
| const Edgelist* edgelist_ptr_; | ||
| const XadjArray* xadj_ptr_; | ||
| const AdjncyArray* adjncy_ptr_; | ||
| Vertex vertex_base_; | ||
|
|
||
| friend class iterator; | ||
| }; | ||
|
|
||
| } // namespace kagen | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing space between closing parenthesis and
noexceptkeyword. Should beEdgeRange(const Graph& graph) noexcept;for consistency with other declarations in the file.