@@ -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 }
0 commit comments