Skip to content
Merged
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
201 changes: 201 additions & 0 deletions kagen/edge_range.cpp
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
81 changes: 81 additions & 0 deletions kagen/edge_range.h
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;
Copy link

Copilot AI Nov 6, 2025

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 noexcept keyword. Should be EdgeRange(const Graph& graph) noexcept; for consistency with other declarations in the file.

Suggested change
EdgeRange(const Graph& graph)noexcept;
EdgeRange(const Graph& graph) noexcept;

Copilot uses AI. Check for mistakes.

static EdgeRange FromGraph(const Graph& graph) noexcept;

class iterator {
public:
using iterator_category = std::forward_iterator_tag;
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
3 changes: 3 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,6 @@ kagen_add_test(test_permutation
FILES permutation/permutation_test.cpp
CORES 1 2 4)

kagen_add_test(test_edge_range
FILES edge_range.cpp
CORES 1)
Loading