new Graph subclass edge_iterator

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

@ -15,6 +15,7 @@ class Graph {
private: private:
class Vertex; class Vertex;
class EdgeTo;
public: public:
@ -28,7 +29,7 @@ public:
class Edge { class Edge {
public: public:
Edge() : m_source(0), m_destination(0), m_weight(0) {} Edge() : m_source(0), m_destination(0), m_weight(0) {}
Edge(pointer source, pointer destination, float weight) : Edge(pointer source, pointer destination, float weight) :
m_source(source), m_destination(destination), m_weight(weight) {} m_source(source), m_destination(destination), m_weight(weight) {}
@ -42,6 +43,8 @@ public:
pointer m_destination; pointer m_destination;
float m_weight; float m_weight;
}; };
typedef Edge* edge_pointer;
typedef Edge& edge_reference;
Graph(); Graph();
@ -62,15 +65,17 @@ public:
bool contains(const_reference data) const; bool contains(const_reference data) const;
std::vector<pointer> vertices() const; std::vector<pointer> vertices() const;
std::vector<pointer> neighboursOf(const_reference data) 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<float> edgesBetween(const_reference source, const_reference destination) const;
std::vector<Edge> edges() const; std::vector<Edge> edges() const;
// iterators // iterators
class vertex_iterator : public std::iterator<std::forward_iterator_tag, class vertex_iterator : public std::iterator<std::forward_iterator_tag,
value_type, value_type,
difference_type, difference_type,
pointer, pointer,
reference> reference>
{ {
public: public:
typedef vertex_iterator self_type; typedef vertex_iterator self_type;
@ -98,8 +103,50 @@ public:
typename std::vector<Vertex>::iterator m_it; typename std::vector<Vertex>::iterator m_it;
}; };
vertex_iterator begin() { return vertex_iterator(m_vertices.begin()); } vertex_iterator vertex_begin() { return vertex_iterator(m_vertices.begin()); }
vertex_iterator end() { 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: private:

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

Loading…
Cancel
Save