Skip to content

Commit 887367e

Browse files
committed
added test, improve implementaion
1 parent ed95050 commit 887367e

File tree

2 files changed

+54
-35
lines changed

2 files changed

+54
-35
lines changed

include/osp/graph_algorithms/directed_graph_edge_view.hpp

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -83,35 +83,29 @@ class edge_view {
8383

8484
DirectedEdgeIterator(const vertex_idx_t<Graph_t> edge_idx, const Graph_t &graph)
8585
: graph_(&graph), currentVertex_(0), currentEdgeIdx_(edge_idx) {
86-
if (currentEdgeIdx_ < graph_->num_edges()) {
87-
vertex_idx_t<Graph_t> tmp = 0u;
88-
advanceToValid();
8986

90-
while (currentVertex_ != graph_->num_vertices() && tmp < currentEdgeIdx_) {
91-
while (currentChild_ != graph_->children(currentVertex_).end()) {
92-
if (tmp == currentEdgeIdx_) {
93-
return;
94-
}
95-
currentChild_++;
96-
tmp++;
97-
}
98-
// Move to next vertex
99-
currentVertex_++;
100-
if (currentVertex_ != graph_->num_vertices()) {
101-
currentChild_ = graph_->children(currentVertex_).begin();
102-
// Skip empty adjacency lists
103-
while (currentVertex_ != graph_->num_vertices() &&
104-
graph_->children(currentVertex_).begin() == graph_->children(currentVertex_).end()) {
105-
currentVertex_++;
106-
if (currentVertex_ != graph_->num_vertices()) {
107-
currentChild_ = graph_->children(currentVertex_).begin();
108-
}
109-
}
110-
}
111-
}
112-
} else {
87+
if (currentEdgeIdx_ >= graph_->num_edges()) {
11388
currentEdgeIdx_ = graph_->num_edges();
11489
currentVertex_ = graph_->num_vertices();
90+
return;
91+
}
92+
93+
vertex_idx_t<Graph_t> currentAccumulatedEdges = 0;
94+
95+
// Optimization: Skip vertices entirely if their degree is small enough
96+
while (currentVertex_ < graph_->num_vertices()) {
97+
const auto degree = graph_->out_degree(currentVertex_);
98+
if (currentAccumulatedEdges + degree > currentEdgeIdx_) {
99+
break;
100+
}
101+
currentAccumulatedEdges += degree;
102+
currentVertex_++;
103+
}
104+
105+
// Initialize child iterator and advance within the specific vertex
106+
if (currentVertex_ < graph_->num_vertices()) {
107+
currentChild_ = graph_->children(currentVertex_).begin();
108+
std::advance(currentChild_, currentEdgeIdx_ - currentAccumulatedEdges);
115109
}
116110
}
117111

@@ -124,14 +118,7 @@ class edge_view {
124118

125119
if (currentChild_ == graph_->children(currentVertex_).end()) {
126120
currentVertex_++;
127-
// Skip empty vertices
128-
while (currentVertex_ != graph_->num_vertices()) {
129-
if (graph_->children(currentVertex_).begin() != graph_->children(currentVertex_).end()) {
130-
currentChild_ = graph_->children(currentVertex_).begin();
131-
break;
132-
}
133-
currentVertex_++;
134-
}
121+
advanceToValid();
135122
}
136123
return *this;
137124
}

tests/directed_graph_util.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,13 @@ BOOST_AUTO_TEST_CASE(ComputationalDagConstructor) {
602602
BOOST_CHECK(get_bottom_node_distance(graph) == bottom_dist);
603603

604604
const std::vector<std::vector<VertexType>> graph_second_Out = {
605-
{1, 2}, {3, 4}, {4, 5}, {6}, {}, {6}, {},
605+
{1, 2},
606+
{3, 4},
607+
{4, 5},
608+
{6},
609+
{},
610+
{6},
611+
{},
606612
};
607613
const std::vector<int> graph_second_workW = {1, 1, 1, 1, 1, 1, 3};
608614
const std::vector<int> graph_second_commW = graph_second_workW;
@@ -675,4 +681,30 @@ BOOST_AUTO_TEST_CASE(ComputationalDagConstructor) {
675681
// rev_edge_in_rev_graph);
676682
// }
677683
// }
684+
}
685+
686+
BOOST_AUTO_TEST_CASE(test_edge_view_indexed_access) {
687+
computational_dag_vector_impl_def_t graph = constr_graph_1();
688+
auto all_edges = edge_view(graph);
689+
690+
// Check initial iterator
691+
auto it = all_edges.begin();
692+
693+
// Check each edge by index
694+
for (size_t i = 0; i < graph.num_edges(); ++i) {
695+
// Construct iterator directly to index i
696+
auto indexed_it = decltype(all_edges)::iterator(i, graph);
697+
BOOST_CHECK(indexed_it == it);
698+
BOOST_CHECK(*indexed_it == *it);
699+
700+
++it;
701+
}
702+
703+
// Check end condition
704+
auto end_it = decltype(all_edges)::iterator(graph.num_edges(), graph);
705+
BOOST_CHECK(end_it == all_edges.end());
706+
707+
// Check out of bounds
708+
auto oob_it = decltype(all_edges)::iterator(graph.num_edges() + 5, graph);
709+
BOOST_CHECK(oob_it == all_edges.end());
678710
}

0 commit comments

Comments
 (0)