#line 1 "/user/cvsspst/ees1cg/RAVL/RAVL-0.7/Core/Container/Graph/GraphNode.hh" // This file is part of RAVL, Recognition And Vision Library // Copyright (C) 2001, University of Surrey // This code may be redistributed under the terms of the GNU Lesser // General Public License (LGPL). See the lgpl.licence file for details or // see http://www.gnu.org/copyleft/lesser.html // file-header-ends-here #ifndef RAVL_GRAPHNODE_HEADER #define RAVL_GRAPHNODE_HEADER 1 ////////////////////////////////////////////////////////////// //! file="Ravl/Core/Container/Graph/GraphNode.hh" //! lib=RavlCore //! author="Charles Galambos" //! date="10/12/1996" //! docentry="Ravl.Core.Graphs" //! rcsid="$Id: GraphNode.hh,v 1.5 2002/08/09 18:57:53 craftit Exp $" //! userlevel=Normal //! example=exGraph.cc #include "Ravl/GraphBase.hh" #include "Ravl/Assert.hh" namespace RavlN { template class GraphNodeDatC; template class GraphEdgeIterC; template class GraphAdjIterC; template class GraphC; template class GraphNodeHC; template class GraphLinearIterC; template class GraphConnIterC; const int GraphNodeDat_CheckNum = 0x12345678; //! userlevel=Develop //: Graph node data. template class GraphNodeDatC : public GraphNodeBaseBodyC { public: inline GraphNodeDatC(GraphBaseBodyC &gr,const NodeT &Dat,UIntT nid); // Constructor. ~GraphNodeDatC(); // Destructor. NodeT &Data(void) { return UserData; } // Get at data. const NodeT &Data(void) const { return UserData; } // Get at data. #if RAVL_GRAPH_DEBUG bool IsValid() const { return CheckVal == GraphNodeDat_CheckNum; } #else bool IsValid() const { return true; } #endif protected: NodeT UserData; #if RAVL_GRAPH_DEBUG int CheckVal; #endif friend class GraphNodeHC; }; //! userlevel=Normal //: Node iterator. // Iterates through all the nodes in a graph. template class GraphNodeIterC : public GraphNodeBaseC { public: GraphNodeIterC(GraphC &AGraph); // Constructor. inline GraphNodeIterC(); // Default constructor. inline GraphNodeIterC(const GraphNodeIterC &Another); // Copy constructor. ~GraphNodeIterC(); // Destructor. inline const GraphNodeIterC &operator=(const GraphNodeIterC &Oth); // Assigment. inline bool operator==(const GraphNodeIterC &Oth) const; // Comparison. inline bool operator!=(const GraphNodeIterC &Oth) const; // Comparison. inline NodeT &Data(void); // Access data. inline const NodeT &Data(void) const; // Access data. inline GraphAdjIterC In(); // Returns the iterator that is set to the first item of the list // of the graph adjacencies in which this node is the target node. inline GraphAdjIterC Out(); // Returns the iterator that is set to the first item of the list // of the graph adjacencies in which this node is the source node. inline void Invalidate(); // Invalidate handle, IsValid() will return false after this // call until the handle is assigmed to another node. inline void Del(); // Unlinks edge from the graph and deletes it. The iterator // will point to the previous edge in the graph edge list. inline void DelMoveNext(); // Unlinks edge from the graph and deletes it. The iterator // will point to the next edge in the graph edge list. #if RAVL_GRAPH_DEBUG bool IsNodeValid() const { if(!IsElm()) return true; return NodeDat().IsValid(); } bool IsIterValid() const { if(!IsElm()) return false; return NodeDat().IsValid(); } #else bool IsNodeValid() const { return true; } bool IsIterValid() const { return true; } #endif protected: GraphNodeBaseC &BaseNodeIter() { return *this; } // Access to the base graph node iterator. // Use ONLY for implementing new base graph operations/iteratiors. const GraphNodeBaseC &BaseNodeIter() const { return *this; } // Access to the base graph node iterator. // Use ONLY for implementing new base graph operations/iterators. GraphNodeIterC(const GraphNodeBaseC &aNode) : GraphNodeBaseC(aNode) {} // Construct from a base node iterator. GraphNodeIterC(GraphNodeBaseBodyC &aNode,GraphBaseC &bg) : GraphNodeBaseC(aNode,bg) {} // Construct from a base node iterator. GraphNodeIterC(GraphNodeBaseBodyC &aNode,GraphBaseBodyC &bg) : GraphNodeBaseC(aNode,bg) {} // Construct from a base node iterator. inline GraphNodeDatC &NodeDat(); // Get a node rep. inline const GraphNodeDatC &NodeDat() const; // Get a node rep. friend class GraphC; friend class GraphLinearIterC; friend class GraphConnIterC; friend class GraphAdjIterC; friend class GraphEdgeIterC; friend class GraphNodeHC; }; //: Node handle. // Handle for a single node in a graph. template class GraphNodeHC { public: inline GraphNodeHC(); // Default constructor. inline GraphNodeHC(const GraphNodeHC &Oth); // Copy constructor. inline GraphNodeHC(const GraphNodeIterC &Oth); // Construct from a node iter. inline GraphNodeHC(GraphNodeDatC &Rep); // Construct form a node rep. inline bool IsValid() const; // Is a valid handle ? inline const GraphNodeHC &operator=(const GraphNodeHC &Oth); // Assigment. inline const GraphNodeHC &operator=(const GraphNodeIterC &Oth); // Assignment. inline bool operator==(const GraphNodeHC &Oth) const ; // Comparison. inline bool operator!=(const GraphNodeHC &Oth) const ; // Comparison. inline NodeT &Data(void); // Access data. inline const NodeT &Data(void) const; // Access data. inline GraphAdjIterC In(); // Returns the iterator that is set to the first item of the list // of the graph adjacencies in which this node is the target node. inline GraphAdjIterC Out(); // Returns the iterator that is set to the first item of the list // of the graph adjacencies in which this node is the source node. inline void Invalidate(); // Invalidate handle, IsValid() will return false after this // call until the handle is assigmed to another node. inline void Del(); // Unlinks edge from the graph and deletes it. The iterator // will point to the previous edge in the graph edge list. inline GraphNodeBaseBodyC & NodeRep() const { return *Node; } // Returns the node. inline GraphNodeDatC & NodeRef() { return *Node; } // Returns the node. //protected: GraphNodeHC(const GraphNodeBaseC &ANode) : Node(&((GraphNodeIterC(ANode)).NodeDat())) {} // Construct from a base node iterator. UIntT Hash() const { return (UIntT) Node >> 2; } // Get hash value for node. private: GraphNodeDatC *Node; }; } #include "Ravl/GraphEdge.hh" #include "Ravl/GraphAdj.hh" namespace RavlN { template inline GraphNodeDatC::GraphNodeDatC(GraphBaseBodyC &graph,const NodeT &Dat,UIntT nid) : GraphNodeBaseBodyC(graph,nid), UserData(Dat) { #if RAVL_GRAPH_DEBUG CheckVal = GraphNodeDat_CheckNum; #endif } template GraphNodeDatC::~GraphNodeDatC() { RavlAssert(IsValid()); GraphAdjIterC ItOut(Out(),BaseGraph()); while(ItOut.IsElm()) ItOut.DelMoveNext(); GraphAdjIterC ItIn(In(),BaseGraph()); while(ItIn.IsElm()) ItIn.DelMoveNext(); #if RAVL_GRAPH_DEBUG CheckVal = 0xf0f0f0f0; #endif } ///////////////////////////////////////////////////////////////////////// // GraphNodeIterC. template inline GraphNodeIterC::GraphNodeIterC() : GraphNodeBaseC() {} template GraphNodeIterC::GraphNodeIterC(GraphC &aGraph) : GraphNodeBaseC(aGraph.Nodes()) {} template inline GraphNodeIterC::GraphNodeIterC(const GraphNodeIterC &Another) : GraphNodeBaseC(Another) { RavlAssert(Another.IsNodeValid()); } template GraphNodeIterC::~GraphNodeIterC() { RavlAssert(IsNodeValid()); } template inline const GraphNodeIterC & GraphNodeIterC::operator=(const GraphNodeIterC &Oth) { RavlAssert(Oth.IsNodeValid()); ((GraphNodeBaseC &) *this) = Oth; return *this; } template inline bool GraphNodeIterC::operator==(const GraphNodeIterC &Oth) const { RavlAssert(IsNodeValid()); RavlAssert(Oth.IsNodeValid()); return GraphNodeBaseC::operator==(Oth); } template inline bool GraphNodeIterC::operator!=(const GraphNodeIterC &Oth) const { RavlAssert(IsNodeValid()); RavlAssert(Oth.IsNodeValid()); return !GraphNodeBaseC::operator==(Oth); } template inline NodeT & GraphNodeIterC::Data(void) { RavlAssert(IsIterValid()); return static_cast &>(NodeRep()).Data(); } template inline const NodeT & GraphNodeIterC::Data(void) const { RavlAssert(IsIterValid()); return static_cast &>(NodeRep()).Data(); } template inline GraphAdjIterC GraphNodeIterC::In() { RavlAssert(IsIterValid()); return GraphAdjIterC(GraphNodeBaseC::NodeRep().In(),Graph()); } template inline GraphAdjIterC GraphNodeIterC::Out() { RavlAssert(IsIterValid()); return GraphAdjIterC(GraphNodeBaseC::NodeRep().Out(),Graph()); } template inline void GraphNodeIterC::Invalidate() { IntrDLIterC::Invalidate(); } template inline void GraphNodeIterC::Del() { RavlAssert(IsIterValid()); GraphNodeDatC *node = &static_cast &>(NodeRep()); Prev(); delete node; } template inline void GraphNodeIterC::DelMoveNext() { RavlAssert(IsIterValid()); GraphNodeDatC *node = &NodeDat(); Next(); delete node; } template inline GraphNodeDatC & GraphNodeIterC::NodeDat() { return static_cast &>(GraphNodeBaseC::NodeRep()); } template inline const GraphNodeDatC & GraphNodeIterC::NodeDat() const { return static_cast &>(GraphNodeBaseC::NodeRep()); } //////////////////////////////////////////////////////////////////////////////////// // GraphNodeHC Stuff. template inline GraphNodeHC::GraphNodeHC() : Node(0) {} template inline GraphNodeHC::GraphNodeHC(const GraphNodeHC &Oth) : Node(Oth.Node) {} template inline GraphNodeHC::GraphNodeHC(const GraphNodeIterC &Oth) : Node(&((GraphNodeIterC &) Oth).NodeDat()) {} template inline GraphNodeHC::GraphNodeHC(GraphNodeDatC &Rep) : Node(&Rep) {} template inline bool GraphNodeHC::IsValid(void) const { return Node != 0; } template inline const GraphNodeHC & GraphNodeHC::operator=(const GraphNodeHC &Oth) { Node = Oth.Node; return *this; } template inline const GraphNodeHC & GraphNodeHC::operator=(const GraphNodeIterC &Oth) { Node = &((GraphNodeIterC &)Oth).NodeDat(); return *this; } template inline bool GraphNodeHC::operator==(const GraphNodeHC &Oth) const { return Oth.Node == Node; } template inline bool GraphNodeHC::operator!=(const GraphNodeHC &Oth) const { return Oth.Node != Node; } template inline NodeT & GraphNodeHC::Data(void) { return Node->Data(); } template inline const NodeT & GraphNodeHC::Data(void) const { return Node->Data(); } template inline GraphAdjIterC GraphNodeHC::In() { return GraphAdjIterC(Node->In(), // Yep, its right ! Node->BaseGraph()); } template inline GraphAdjIterC GraphNodeHC::Out() { return GraphAdjIterC(Node->Out(), // Yep, its right ! Node->BaseGraph()); } template inline void GraphNodeHC::Invalidate() { Node = 0; } template inline void GraphNodeHC::Del() { GraphNodeDatC *nd = static_cast *>(Node); Node = 0; // Invalidate. delete nd; } } #endif