Edge iteragor advance works

for/release
Denes Matetelki 12 years ago
parent d3203f0e7f
commit 9afcf3fc5b

@ -90,7 +90,7 @@ public:
reference_self_type operator=(const_reference_self_type o) reference_self_type operator=(const_reference_self_type o)
{ if (this != &o) { m_it = o.m_it; } return *this; } { if (this != &o) { m_it = o.m_it; } return *this; }
reference operator*() { return *((*m_it).m_data); } pointer operator*() { return (*m_it).m_data; }
pointer operator->() { return (*m_it).m_data; } pointer operator->() { return (*m_it).m_data; }
self_type &operator++() { ++m_it; return *this; } self_type &operator++() { ++m_it; return *this; }
@ -137,12 +137,20 @@ public:
self_type operator+(difference_type n) { self_type tmp(*this); tmp.pos_ += n; return tmp; } self_type operator+(difference_type n) { self_type tmp(*this); tmp.pos_ += n; return tmp; }
self_type &operator+=(difference_type n) { advance(n); return *this; } self_type &operator+=(difference_type n) { advance(n); return *this; }
bool operator==(const_reference_self_type o) bool operator==(const_reference_self_type o)
{ return m_vertex_it == o.m_vertex_it && m_edge_it == o.m_edge_it; } { return (m_vertex_it == m_vertices.end() && o.m_vertex_it == o.m_vertices.end()) ||
(m_vertex_it == o.m_vertex_it && m_edge_it == o.m_edge_it); }
bool operator!=(const_reference_self_type o) { return !(*this == o); } bool operator!=(const_reference_self_type o) { return !(*this == o); }
private: private:
edge_iterator(std::vector<Vertex> vertices) edge_iterator(std::vector<Vertex> vertices, bool begin = true)
: m_vertices(vertices), m_vertex_it(m_vertices.begin()), m_edge_it(), m_edge(0) {} : m_vertices(vertices), m_vertex_it(), m_edge_it(), m_edge(0) {
if (begin) {
m_vertex_it = m_vertices.begin();
if (!m_vertices.empty()) m_edge_it = (*m_vertex_it).m_edges.begin();
} else {
m_vertex_it = m_vertices.end();
}
}
void resetEdge(); void resetEdge();
void advance(int n); void advance(int n);
@ -154,7 +162,7 @@ public:
}; };
edge_iterator edge_begin() { return edge_iterator(m_vertices); } edge_iterator edge_begin() { return edge_iterator(m_vertices); }
edge_iterator edge_end() { return edge_iterator(m_vertices); } edge_iterator edge_end() { return edge_iterator(m_vertices, false); }
private: private:
@ -214,37 +222,44 @@ void Graph<T>::edge_iterator::resetEdge()
{ {
if (m_edge) delete m_edge; if (m_edge) delete m_edge;
if (m_vertex_it == m_vertices.end()) { if (m_vertex_it == m_vertices.end() ||
(*m_vertex_it).m_edges.empty()) {
m_edge = 0; m_edge = 0;
} else { return;
m_edge = new Edge( (*m_vertex_it).m_data, (*m_edge_it).m_destination, (*m_edge_it).m_weight);
} }
pointer source = (*m_vertex_it).m_data;
pointer destination = (*m_edge_it).m_destination;
float weight = (*m_edge_it).m_weight;
m_edge = new Edge(source, destination, weight);
} }
template <typename T> template <typename T>
void Graph<T>::edge_iterator::advance(int n) void Graph<T>::edge_iterator::advance(int n)
{ {
if (m_vertex_it == m_vertices.end()) if (m_vertex_it == m_vertices.end()) return;
return;
std::list<Graph<T>::EdgeTo> edges = (*m_vertex_it).m_edges; while (true) {
const int remaining = std::distance(m_edge_it, edges.end()); const int edgesAhead = std::distance(m_edge_it, (*m_vertex_it).m_edges.end()) - 1;
if (remaining < n) { if (n <= edgesAhead) {
std::advance(m_edge_it, n); std::advance(m_edge_it, n);
return; return;
} }
int counter = n - remaining; if (edgesAhead > 0)
n -= edgesAhead;
++m_vertex_it; ++m_vertex_it;
while (counter > 0 && m_vertex_it != m_vertices.end()) {
const int number_of_edges = (*m_vertex_it).m_edges.size(); if (m_vertex_it == m_vertices.end())
if (counter < number_of_edges) {
m_edge_it = (*m_vertex_it).m_edges.begin();
std::advance(m_edge_it, counter);
return; return;
}
counter -= number_of_edges; m_edge_it = (*m_vertex_it).m_edges.begin();
++m_vertex_it; if (m_edge_it != (*m_vertex_it).m_edges.end())
--n;
if (n == 0) return;
} }
} }

@ -57,46 +57,62 @@ int main()
int c = 13; int c = 13;
int d = 1; int d = 1;
g.addVertex(d); assert(g.numberOfVertices() == 2);
g.addVertex(c); g.addVertex(c);
g.addVertex(d);
assert(g.numberOfVertices() == 4);
int vertices_array[] = {2, 5, 13, 1}; int vertices_array[] = {2, 5, 13, 1};
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.vertex_begin(), v_it = v.begin(); for (g_it = g.vertex_begin(), v_it = v.begin();
g_it != g.vertex_end(); g_it != g.vertex_end();
g_it++, v_it++) g_it++, v_it++) {
assert(*g_it == *v_it); assert(**g_it == *v_it);
}
assert(g.neighboursOf(5).size() == 0); assert(g.neighboursOf(5).size() == 0);
Graph<int> g2; Graph<int> g2;
g.addVertex(1); g2.addVertex(1);
g.addVertex(2); g2.addVertex(2);
g.addVertex(3); g2.addVertex(3);
g.addVertex(4); g2.addVertex(4);
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.vertex_begin(), v_it = v2.begin(); for (g_it = g2.vertex_begin(), v_it = v2.begin();
g_it != g2.vertex_end(); g_it != g2.vertex_end();
g_it++, v_it++) g_it++, v_it++) {
assert(*g_it == *v_it); std::cout << "vector: "
<< *g_it << " "
<< **g_it
<< std::endl;
assert(**g_it == *v_it);
}
assert(g.addEdge(b, c) == true); assert(g2.addEdge(1, 2) == true);
assert(g.addEdge(a, d) == true); assert(g2.addEdge(1, 3) == true);
std::vector<Graph<int>::Edge> e = g.edges(); assert(g2.addEdge(2, 4) == true);
std::vector<Graph<int>::Edge> e = g2.edges();
assert(e.size() == 3); assert(e.size() == 3);
for(Graph<int>::edge_iterator edge_it = g.edge_begin(); edge_it != g.edge_end(); ++edge_it) { for(Graph<int>::edge_iterator edge_it = g2.edge_begin();
std::cout << (*edge_it).getSource() << " " edge_it != g2.edge_end();
<< (*edge_it).getDestination() << " " ++edge_it) {
Graph<int>::pointer source = (*edge_it).getSource();
Graph<int>::pointer destination = (*edge_it).getDestination();
std::cout << "edge: "
<< source << " " << *source << " "
<< destination << " " << *destination << " "
<< (*edge_it).getWeight() << std::endl; << (*edge_it).getWeight() << std::endl;
} }
std::cout << "frankon vege" << std::endl;
return 0; return 0;
} }

Loading…
Cancel
Save