new Graph subclass edge_iterator

for/release
Denes Matetelki 12 years ago
parent 056381483d
commit 502c8278c0

@ -15,6 +15,7 @@ class Graph {
private:
class Vertex;
class EdgeTo;
public:
@ -42,6 +43,8 @@ public:
pointer m_destination;
float m_weight;
};
typedef Edge* edge_pointer;
typedef Edge& edge_reference;
Graph();
@ -62,6 +65,8 @@ public:
bool contains(const_reference data) const;
std::vector<pointer> vertices() const;
std::vector<pointer> neighboursOf(const_reference data) const;
/// @todo come up with a more clear name
std::vector<float> edgesBetween(const_reference source, const_reference destination) const;
std::vector<Edge> edges() const;
@ -98,8 +103,50 @@ public:
typename std::vector<Vertex>::iterator m_it;
};
vertex_iterator begin() { return vertex_iterator(m_vertices.begin()); }
vertex_iterator end() { return vertex_iterator(m_vertices.begin()); }
vertex_iterator vertex_begin() { return vertex_iterator(m_vertices.begin()); }
vertex_iterator vertex_end() { return vertex_iterator(m_vertices.end()); }
class edge_iterator : public std::iterator<std::forward_iterator_tag,
Edge,
difference_type,
edge_pointer,
edge_reference>
{
public:
typedef edge_iterator self_type;
typedef edge_iterator& reference_self_type;
typedef const edge_iterator& const_reference_self_type;
edge_iterator() : m_vertex_it(), m_edge_it(), m_edge(0) {}
edge_iterator(typename std::vector<Vertex>::iterator vertex_it,
typename std::list<EdgeTo>::iterator edge_it)
: m_vertex_it(vertex_it), m_edge_it(edge_it) {}
~edge_iterator() { if (m_edge) delete m_edge; }
edge_iterator(const_reference_self_type o)
: m_vertex_it(o.m_vertex_it), m_edge_it(o.m_edge_it) {}
reference_self_type operator=(const_reference_self_type o)
{ if (this != &o) { m_vertex_it = o.m_vertex_it; m_edge_it = o.m_edge_it; } return *this; }
edge_reference operator*() { if (!m_edge) { m_edge = new Edge(*m_vertex_it, (*m_edge_it).m_destination, (*m_edge_it).m_weight); } return *m_edge; }
edge_pointer operator->() { if (!m_edge) { m_edge = new Edge(*m_vertex_it, (*m_edge_it).m_destination, (*m_edge_it).m_weight); } return m_edge; }
/// @todo implement: delete m_edge & step;
// self_type &operator++() { ++m_it; return *this; }
// self_type operator++(int) { self_type tmp(*this); ++(*this); return tmp; }
// self_type operator+(difference_type n) { self_type tmp(*this); tmp.pos_ += n; return tmp; }
// self_type &operator+=(difference_type n) { m_it += n; return *this; }
bool operator==(const_reference_self_type o) { return m_vertex_it == o.m_vertex_it && m_edge_it == o.m_edge_it; }
bool operator!=(const_reference_self_type o) { return !(*this == o); }
private:
typename std::vector<Vertex>::iterator m_vertex_it;
typename std::list<EdgeTo>::iterator m_edge_it;
edge_pointer m_edge;
};
edge_iterator edge_begin() { return edge_iterator(m_vertices.begin()); }
edge_iterator edge_end() { return edge_iterator(m_vertices.end()); }
private:

@ -63,8 +63,8 @@ int main()
std::vector<int> v(vertices_array, vertices_array + sizeof(vertices_array) / sizeof(int) );
Graph<int>::vertex_iterator g_it;
std::vector<int>::iterator v_it;
for (g_it = g.begin(), v_it = v.begin();
g_it != g.end();
for (g_it = g.vertex_begin(), v_it = v.begin();
g_it != g.vertex_end();
g_it++, v_it++)
assert(*g_it == *v_it);
@ -79,8 +79,8 @@ int main()
int vertices_array2[] = {1, 2, 3, 4};
std::vector<int> v2(vertices_array2, vertices_array2 + sizeof(vertices_array2) / sizeof(int) );
for (g_it = g2.begin(), v_it = v2.begin();
g_it != g2.end();
for (g_it = g2.vertex_begin(), v_it = v2.begin();
g_it != g2.vertex_end();
g_it++, v_it++)
assert(*g_it == *v_it);

Loading…
Cancel
Save