From 183081c17fb1c7de25739bad8e9144e31fa31035 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Mon, 22 Apr 2013 19:56:22 +0200 Subject: [PATCH] Moving definitions from class body which only fit to the next line outside of the class --- graph.h | 92 +++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 28 deletions(-) diff --git a/graph.h b/graph.h index 659dd1f..7c64d57 100644 --- a/graph.h +++ b/graph.h @@ -30,18 +30,15 @@ public: struct Edge { Edge() : m_source(0), m_destination(0), m_weight(0) {} - Edge(pointer source, pointer destination, float weight) - : m_source(source), m_destination(destination), m_weight(weight) {} - Edge(const Edge& o) - : m_source(o.m_source), m_destination(o.m_destination), m_weight(o.m_weight) {} + Edge(pointer source, pointer destination, float weight); + Edge(const Edge& o); Edge& operator=(Edge o) { swap(o); return *this; } + void swap(Edge& o); pointer getSource() const { return m_source; } pointer getDestination() const { return m_destination; } float getWeight() const { return m_weight; } - void swap(Edge& o); - pointer m_source; pointer m_destination; float m_weight; @@ -92,10 +89,10 @@ public: ~vertex_iterator() {} vertex_iterator(const_reference_self_type o) : m_it(o.m_it) {} reference_self_type operator=(self_type o) { swap(o); return *this; } + void swap(reference_self_type o) { std::swap(m_it, o.m_it); } pointer operator*() { return (*m_it).m_data; } pointer operator->() { return (*m_it).m_data; } - 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; } @@ -103,8 +100,6 @@ public: bool operator==(const_reference_self_type o) const { return m_it == o.m_it; } bool operator!=(const_reference_self_type o) const { return !(*this == o); } - void swap(reference_self_type o) { std::swap(m_it, o.m_it); } - private: vertex_iterator(typename std::vector::iterator it) : m_it(it) {} @@ -129,15 +124,12 @@ public: edge_iterator() : m_vertices(), m_vertex_it(), m_edge_it(), m_edge(0) {} ~edge_iterator() { if (m_edge) delete m_edge; } - - edge_iterator(const_reference_self_type o) - : m_vertices(o.m_vertices), m_vertex_it(o.m_vertex_it), m_edge_it(o.m_edge_it), m_edge(0) {} - + edge_iterator(const_reference_self_type o); reference_self_type operator=(self_type o) { swap(o); return *this; } + void swap(reference_self_type other); edge_reference operator*() { resetEdge(); return *m_edge; } edge_pointer operator->() { resetEdge(); return m_edge; } - self_type &operator++() { advance(1); return *this; } self_type operator++(int) { self_type tmp(*this); advance(1); return tmp; } self_type operator+(difference_type n) { self_type tmp(*this); tmp.pos_ += n; return tmp; } @@ -145,8 +137,6 @@ public: bool operator==(const_reference_self_type o) const; bool operator!=(const_reference_self_type o) const { return !(*this == o); } - void swap(reference_self_type other); - private: edge_iterator(std::vector vertices, bool begin = true); @@ -167,15 +157,12 @@ private: struct EdgeTo { - EdgeTo(const_reference destination, float weight = 0) - : m_destination(const_cast(&destination)), m_weight(weight) {} + EdgeTo(const_reference destination, float weight = 0); EdgeTo(const EdgeTo& o) : m_destination(o.m_destination), m_weight(o.m_weight) {} EdgeTo& operator=(EdgeTo o) { swap(o); return *this; } - bool operator==(const EdgeTo& other) const - { return m_destination == other.m_destination && m_weight == other.m_weight; } + void swap(EdgeTo& o); - void swap(EdgeTo& o) - { std::swap(m_destination, o.m_destination); std::swap(m_vertices, o.m_weight); } + bool operator==(const EdgeTo& other) const; pointer m_destination; float m_weight; @@ -186,16 +173,15 @@ private: Vertex(const_reference data) : m_data(const_cast(&data)) , m_edges() {} Vertex(const Vertex& o) : m_data(o.m_data), m_edges(o.m_edges) {} Vertex& operator=(Vertex o) { swap(o); return *this; } + void swap(Vertex& o) { std::swap(m_data, o.m_data); std::swap (m_edges, o.m_edges); } + bool operator==(const Vertex& other) const; - void addEdge(const_reference destination, float weight = 0) - { m_edges.push_back(EdgeTo(destination, weight)); } + + void addEdge(const_reference destination, float weight = 0); void removeEdge(const_reference destination, float weight = 0); void removeAllEdgesTo(const_reference destination); std::vector edges() const; - void swap(Vertex& o) - { std::swap(m_data, o.m_data); std::swap (m_edges, o.m_edges); } - pointer m_data; std::list m_edges; }; @@ -212,6 +198,20 @@ private: // Edge +template +Graph::Edge::Edge(pointer source, pointer destination, float weight) + : m_source(source) + , m_destination(destination) + , m_weight(weight) +{} + +template + Graph::Edge::Edge(const Edge& o) + : m_source(o.m_source) + , m_destination(o.m_destination) + , m_weight(o.m_weight) +{} + template void Graph::Edge::swap(Edge& o) { @@ -223,6 +223,14 @@ void Graph::Edge::swap(Edge& o) // edge iterator +template +Graph::edge_iterator::edge_iterator(const_reference_self_type o) + : m_vertices(o.m_vertices) + , m_vertex_it(o.m_vertex_it) + , m_edge_it(o.m_edge_it) + , m_edge(0) +{} + template bool Graph::edge_iterator::operator==(const_reference_self_type o) const { @@ -285,8 +293,8 @@ void Graph::edge_iterator::advance(int n) } if (edgesAhead > 0) n -= edgesAhead; - ++m_vertex_it; + if (m_vertex_it != m_vertices.end()) { m_edge_it = (*m_vertex_it).m_edges.begin(); if (m_edge_it != (*m_vertex_it).m_edges.end()) @@ -295,6 +303,28 @@ void Graph::edge_iterator::advance(int n) } } +// EdgeTo + +template +Graph::EdgeTo::EdgeTo(const_reference destination, float weight) + : m_destination(const_cast(&destination)) + , m_weight(weight) +{} + +template +void Graph::EdgeTo::swap(EdgeTo& o) +{ + std::swap(m_destination, o.m_destination); + std::swap(m_vertices, o.m_weight); +} + +template +bool Graph::EdgeTo::operator==(const EdgeTo& other) const +{ + return m_destination == other.m_destination && + m_weight == other.m_weight; +} + // Vertex @@ -306,6 +336,12 @@ bool Graph::Vertex::operator==(const Vertex& other) const m_edges == other.m_edges; } +template +void Graph::Vertex::addEdge(const_reference destination, float weight) +{ + m_edges.push_back(EdgeTo(destination, weight)); +} + template void Graph::Vertex::removeEdge(const_reference destination, float weight) {