Edge is now a class, returning references

for/release
Denes Matetelki 12 years ago
parent 11ac110653
commit 25b77e5d1c

@ -29,7 +29,9 @@ public:
typedef const V& const_reference; typedef const V& const_reference;
typedef std::ptrdiff_t difference_type; typedef std::ptrdiff_t difference_type;
struct Edge { class Edge {
public:
Edge() : m_source(0), m_destination(0), m_weight() {} Edge() : m_source(0), m_destination(0), m_weight() {}
Edge(pointer source, pointer destination, E weight); Edge(pointer source, pointer destination, E weight);
@ -37,10 +39,12 @@ public:
Edge& operator=(Edge o) { swap(o); return *this; } Edge& operator=(Edge o) { swap(o); return *this; }
void swap(Edge& o); void swap(Edge& o);
pointer getSource() const { return m_source; } reference getSource() const { return *m_source; }
pointer getDestination() const { return m_destination; } reference getDestination() const { return *m_destination; }
E getWeight() const { return m_weight; } E getWeight() const { return m_weight; }
private:
pointer m_source; pointer m_source;
pointer m_destination; pointer m_destination;
E m_weight; E m_weight;

@ -110,33 +110,33 @@ int main()
Graph<int>::edge_iterator edge_it; Graph<int>::edge_iterator edge_it;
Graph<int>::edge_iterator beee = g2.edge_begin(); Graph<int>::edge_iterator beee = g2.edge_begin();
edge_it = beee; edge_it = beee;
assert(edge_it == g2.edge_begin()); // assert(edge_it == g2.edge_begin());
Graph<int>::pointer source = (*edge_it).getSource(); int source = (*edge_it).getSource();
Graph<int>::pointer destination = (*edge_it).getDestination(); int destination = (*edge_it).getDestination();
assert (*source == 1); assert (source == 1);
assert (*destination == 2); assert (destination == 2);
++edge_it; ++edge_it;
source = (*edge_it).getSource(); source = (*edge_it).getSource();
destination = (*edge_it).getDestination(); destination = (*edge_it).getDestination();
assert (*source == 1); assert (source == 1);
assert (*destination == 3); assert (destination == 3);
++edge_it; ++edge_it;
source = (*edge_it).getSource(); source = (*edge_it).getSource();
destination = (*edge_it).getDestination(); destination = (*edge_it).getDestination();
assert (*source == 2); assert (source == 2);
assert (*destination == 4); assert (destination == 4);
int aasdads = 23; int aasdads = 23;
// Graph<int>::edge_iterator edge_it; // Graph<int>::edge_iterator edge_it;
for(/*Graph<int>::edge_iterator*/ edge_it = g2.edge_begin(); edge_it != g2.edge_end(); ++edge_it) { for(/*Graph<int>::edge_iterator*/ edge_it = g2.edge_begin(); edge_it != g2.edge_end(); ++edge_it) {
Graph<int>::pointer source = (*edge_it).getSource(); int source = (*edge_it).getSource();
Graph<int>::pointer destination = (*edge_it).getDestination(); int destination = (*edge_it).getDestination();
std::cout << "edge: " std::cout << "edge: "
<< *source << " " << source << " "
<< *destination << " " << destination << " "
<< (*edge_it).getWeight() << std::endl; << (*edge_it).getWeight() << std::endl;
} }
@ -146,8 +146,8 @@ int main()
Graph<int>::edge_iterator edge_it2 = g2.edge_begin(); Graph<int>::edge_iterator edge_it2 = g2.edge_begin();
edge_it2 += 2; edge_it2 += 2;
assert(*((*edge_it2).getSource()) == 2); assert( (*edge_it2).getSource() == 2);
assert(*((*edge_it2).getDestination()) == 4); assert( (*edge_it2).getDestination() == 4);
assert((*edge_it2).getWeight() == 0); assert((*edge_it2).getWeight() == 0);
std::cout << "frankon vege" << std::endl; std::cout << "frankon vege" << std::endl;

Loading…
Cancel
Save