// // C++ Interface: vertex // // Description: // // // Author: Alex Brandt , (C) 2007 // // Copyright: See COPYING file that comes with this distribution // // #ifndef GRAPHVERTEX_H #define GRAPHVERTEX_H #include #include #include #include #include #include namespace Graphs { template class Vertex; template std::ostream &operator<< (std::ostream &, const Vertex &); /** @class Vertex vertex.h @brief Node in the graph. @author Alex Brandt Vertex of a graph. Basically a node in the network. It's a wonderful day in the neighborhood... */ template class Vertex { public: /** @param mrRogers The data the vertex should hold. Constructor */ Vertex(T mrRogers); /** @param otherVertex The other vertex to copy. Copy Constructor */ Vertex(const Vertex &otherVertex); /** Destructor */ ~Vertex(void); /** @param neighbor The neighbor to say goodbye to. Delete neighbor */ void DeleteNeighbor(const Vertex &neighbor); /** @param neighbor Please won't you be my neighbor? @param weight Not that they are fat, just more expensive. Create neighbor */ void CreateNeighbor(const Vertex &neighbor, const int weight = 1); /** @return The data Mr. Rogers holds for us. Get Mr. Rogers */ T Get(void) const; /** @param data The new data we want Mr. Rogers to hold. Set Mr. Rogers */ void Set(T data); /** @return The neighborhood. Getting the neighborhood to play with. */ std::list*, int> > Neighborhood(void); /** @return The neighbors. Collects the neighbors to play with. */ std::list*> Neighbors(void); /** @param that That vertex to compare. @return True if the vertices are equivalent. Equivalency Operator */ bool operator==(const Vertex &that) const; /** @param that That vertex to compare. @return True if the vertices are not equal. Inequality Operator */ bool operator!=(const Vertex &that) const; /** @param that That vertex to compare. @return True if this is greater than that. Greater than operator */ bool operator>(const Vertex &that) const; /** @param that That vertex to compare. @return True if this is greater than or equal to that. Greater than or equal operator. */ bool operator>=(const Vertex &that) const; /** @param that That vertex to compare. @return True if this is less than that. Less than operator. outStream << otherVertex.mrRogers; */ bool operator<(const Vertex &that) const; /** @param that That vertex to compare. @return True if this is less than or equal to that. Less than or equal operator. */ bool operator<=(const Vertex &that) const; /** @param other The vertex to assign to this one. @return A reference to the new vertex. Assignment Operator. */ Vertex &operator=(const Vertex &other); /** @param otherVertex That vertex to output to the stream. @param outStream The stream to output on to. @return The stream state. Dump the neighbor in the river. */ std::ostream &Dump(std::ostream &out) const; /** @param otherVertex The vertex to find the weight to. @return The weight from this vertex to that vertex. Grab the weight of the path. */ int GetWeight(const Vertex &otherVertex) const; private: std::list*, int> > neighborhood; //!< Neighboring nodes in the graph. The list contains tuples that correspond the end of the edge to the weight of the edge. T mrRogers; //!< The man himself to hold our dear data. /** @param myTuple This static methods very own tuple. @return The weight of this neighbor. Get the weight of the neighbor. */ static int GetWeightFromTuple(boost::tuple*, int> myTuple); /** @param myTuple The tuple[trunk] to extract our neighbor from. @return The neighbor to dispense with. Get the neighbor. */ static Vertex* GetVertexFromTuple(boost::tuple*, int> myTuple); }; template Vertex::Vertex(T mrRogers) :neighborhood(std::list*, int> >()), mrRogers(mrRogers) { } template Vertex::Vertex(const Vertex &otherVertex) :neighborhood(otherVertex.neighborhood), mrRogers(otherVertex.mrRogers) { } template Vertex &Vertex::operator=(const Vertex &other) { if (this != &other) { this->neighborhood = other.neighborhood; this->mrRogers = other.mrRogers; } return *this; } template Vertex::~Vertex(void) { } template void Vertex::CreateNeighbor(const Vertex &neighbor, const int weight) { neighborhood.insert(neighborhood.end(), boost::tuple*, int>(const_cast*>(&neighbor), weight)); return; } template void Vertex::DeleteNeighbor(const Vertex &neighbor) { for (class std::list*, int> >::iterator i = neighborhood.begin(); i != neighborhood.end(); i++) if (boost::get<0>(*i) == boost::get<0>(neighbor)) neighborhood.erase(i); return; } template T Vertex::Get(void) const { return mrRogers; } template void Vertex::Set(T data) { mrRogers = data; return; } template bool Vertex::operator<=(const Vertex &that) const { return static_cast(this) <= static_cast(&that); } template bool Vertex::operator<(const Vertex &that) const { return static_cast(this) < static_cast(&that); } template bool Vertex::operator==(const Vertex &that) const { return static_cast(this) == static_cast(&that); } template bool Vertex::operator>=(const Vertex &that) const { return static_cast(this) >= static_cast(&that); } template bool Vertex::operator>(const Vertex &that) const { return static_cast(this) > static_cast(&that); } template bool Vertex::operator!=(const Vertex &that) const { return static_cast(this) != static_cast(&that); } template std::list*, int> > Vertex::Neighborhood(void) { return neighborhood; } template std::list*> Vertex::Neighbors(void) { std::list*> neighbors; // The neigbors. // std::transform(neighborhood.begin(), neighborhood.end(), neighbors.begin(), boost::lambda::bind(&GetVertexFromTuple, boost::lambda::_1)); std::for_each(neighborhood.begin(), neighborhood.end(), boost::lambda::bind(&std::list*>::push_back, var(neighbors), boost::lambda::bind(&GetVertexFromTuple, boost::lambda::_1))); return neighbors; } template std::ostream &Vertex::Dump(std::ostream &outStream) const { outStream << mrRogers; for_each(neighborhood.begin(), neighborhood.end(), boost::lambda::var(outStream) << boost::lambda::constant(" -> ") << boost::lambda::bind(&Vertex::Get, *boost::lambda::bind*>(&Vertex::GetVertexFromTuple, boost::lambda::_1)) << boost::lambda::constant(":") << boost::lambda::bind(&Vertex::GetWeightFromTuple, boost::lambda::_1)); return outStream; } template std::ostream &operator<< (std::ostream &out, const Vertex &vertex) { return vertex.Dump(out); } template int Vertex::GetWeightFromTuple(boost::tuple*, int> myTuple) { return boost::get<1>(myTuple); } template Vertex* Vertex::GetVertexFromTuple(boost::tuple*, int> myTuple) { return boost::get<0>(myTuple); } template int Vertex::GetWeight(const Vertex &otherVertex) const { return GetWeightFromTuple(*find_if(neighborhood.begin(), neighborhood.end(), boost::lambda::bind(&GetVertexFromTuple, boost::lambda::_1) == boost::lambda::constant(&otherVertex))); } } #endif