Change GraphBase::NeighborIterator to use pointer to graph instead of reference

This enables default copy-assignment operator and copy constructor.
Also removed GraphBase::NeighborIterator::operator!=(), since it will be generated from the equality operator.
This commit is contained in:
Stefan Müller 2025-06-23 20:51:16 +02:00
parent b71d904770
commit 1834109040
2 changed files with 8 additions and 12 deletions

View File

@ -46,19 +46,19 @@ class GraphBase
using reference = const value_type&; using reference = const value_type&;
using iterator_category = std::forward_iterator_tag; using iterator_category = std::forward_iterator_tag;
NeighborIterator(const GraphBase& graph, const int vertex) NeighborIterator(const GraphBase* graph, const int vertex)
: graph_{ graph }, incidence_{ -1 }, value_{ -1, -1 } : graph_{ graph }, incidence_{ -1 }, value_{ -1, -1 }
{ {
if (vertex >= 0 && vertex < graph_.firstVertexIncidences.size()) if (vertex >= 0 && vertex < graph_->getNVertices())
{ {
setIncidence(graph_.firstVertexIncidences[vertex]); setIncidence(graph_->firstVertexIncidences[vertex]);
} }
}; };
NeighborIterator& operator++() NeighborIterator& operator++()
{ {
if (incidence_ >= 0) if (incidence_ >= 0)
{ {
setIncidence(graph_.nextIncidences[incidence_]); setIncidence(graph_->nextIncidences[incidence_]);
} }
return *this; return *this;
} }
@ -72,10 +72,6 @@ class GraphBase
{ {
return incidence_ == other.incidence_; return incidence_ == other.incidence_;
} }
bool operator!=(NeighborIterator other) const
{
return !(*this == other);
}
const reference operator*() const const reference operator*() const
{ {
return value_; return value_;
@ -86,7 +82,7 @@ class GraphBase
} }
private: private:
const GraphBase& graph_; const GraphBase* graph_;
int incidence_; int incidence_;
value_type value_; value_type value_;
void setIncidence(const int incidence) void setIncidence(const int incidence)
@ -94,7 +90,7 @@ class GraphBase
incidence_ = incidence; incidence_ = incidence;
if (incidence_ >= 0) if (incidence_ >= 0)
{ {
value_ = { graph_.incidenceVertices[incidence_], incidence_ >> 1 }; value_ = { graph_->incidenceVertices[incidence_], incidence_ >> 1 };
} }
} }
}; };

View File

@ -41,12 +41,12 @@ bool GraphBase::areAdjacent(const int vertex1, const int vertex2) const
GraphBase::NeighborIterator GraphBase::begin(const int vertex) const GraphBase::NeighborIterator GraphBase::begin(const int vertex) const
{ {
return { *this, vertex }; return { this, vertex };
} }
GraphBase::NeighborIterator GraphBase::end() const GraphBase::NeighborIterator GraphBase::end() const
{ {
return { *this, -1 }; return { this, -1 };
} }
int GraphBase::addVertexInternal() int GraphBase::addVertexInternal()