1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
6 #ifndef __fpa__Base__Graph__h__
7 #define __fpa__Base__Graph__h__
12 #include <itkDataObject.h>
13 #include <itkObjectFactory.h>
19 /** \brief A generic graph with templated index types.
21 * @param _TVertex Vertex type.
22 * @param _TCost Cost type.
23 * @param _TIndex Index type (it should be a strict weak ordering type).
25 template< class _TVertex, class _TCost, class _TIndex = unsigned long, class _TIndexCompare = std::less< _TIndex > >
27 : public itk::DataObject
31 typedef itk::DataObject Superclass;
32 typedef itk::SmartPointer< Self > Pointer;
33 typedef itk::SmartPointer< const Self > ConstPointer;
35 typedef _TVertex TVertex;
37 typedef _TIndex TIndex;
38 typedef _TIndexCompare TIndexCompare;
41 typedef std::map< TIndex, TVertex, TIndexCompare > TVertices;
42 typedef std::vector< TCost > TEdges;
43 typedef std::map< TIndex, TEdges, TIndexCompare > TMatrixRow;
44 typedef std::map< TIndex, TMatrixRow, TIndexCompare > TMatrix;
48 itkTypeMacro( Graph, itk::DataObject );
51 /*! \brief Iterators over vertices.
52 * These allow you to iterate over all of graph's vertices.
54 * Typical iteration should be done as:
58 * TGraph::TVertices::[const_]iterator vIt = g.BeginVertices( );
59 * for( ; vIt != g.EndVertices( ); ++vIt )
61 * vIt->first; --> this is the vertex's index <--
62 * vIt->second; --> this is the vertex's value <--
65 inline typename TVertices::iterator BeginVertices( )
66 { return( this->m_Vertices.begin( ) ); }
67 inline typename TVertices::iterator EndVertices( )
68 { return( this->m_Vertices.end( ) ); }
69 inline typename TVertices::const_iterator BeginVertices( ) const
70 { return( this->m_Vertices.begin( ) ); }
71 inline typename TVertices::const_iterator EndVertices( ) const
72 { return( this->m_Vertices.end( ) ); }
74 /*! \brief Iterators over edges.
75 * These allow you to iterate over all of graph's edges.
77 * Typical iteration should be done as:
81 * TGraph::TMatrix::[const_]iterator mIt = g.BeginEdgesRows( );
82 * for( ; mIt != g.EndEdgesRows( ); ++mIt )
84 * mIt->first; --> this is the row index. <--
85 * TGraph::TMatrixRow::[const_]iterator rIt = mIt->second.begin( );
86 * for( ; rIt != mIt->second.end( ); ++rIt )
88 * rIt->first; --> this is the column index.
89 * TGraph::TEdges::[const_]iterator eIt = rIt->second.begin( );
90 * for( ; eIt != rIt->second.end( ); ++eIt )
91 * *eIt; --> this is the cost between mIt->first and rIt->first
95 inline typename TMatrix::iterator BeginEdgesRows( )
96 { return( this->m_Matrix.begin( ) ); }
97 inline typename TMatrix::iterator EndEdgetsRows( )
98 { return( this->m_Matrix.end( ) ); }
99 inline typename TMatrix::const_iterator BeginEdgesRows( ) const
100 { return( this->m_Matrix.begin( ) ); }
101 inline typename TMatrix::const_iterator EndEdgesRows( ) const
102 { return( this->m_Matrix.end( ) ); }
104 /*! \brief Clear all vertices and edges.
108 /*! \brief Clear all edges.
110 inline void ClearEdges( )
111 { this->m_Matrix.clear( ); }
113 /*! \brief Vertex manipulation methods.
114 * Names are self-explanatory.
116 inline bool HasVertexIndex( const TIndex& i ) const
117 { return( this->m_Vertices.find( i ) != this->m_Vertices.end( ) ); }
118 inline void SetVertex( const TIndex& index, TVertex& vertex )
119 { this->m_Vertices[ index ] = vertex; }
120 inline TVertex& GetVertex( const TIndex& index )
121 { return( this->m_Vertices[ index ] ); }
122 inline const TVertex& GetVertex( const TIndex& index ) const
123 { return( this->m_Vertices[ index ] ); }
124 bool RenameVertex( const TIndex& old_index, const TIndex& new_index );
125 void RemoveVertex( const TIndex& index );
127 /*! \brief Edge manipulation methods.
128 * Names are self-explanatory.
130 inline void AddEdge( const TIndex& orig, const TIndex& dest, const TCost& cost )
131 { this->m_Matrix[ orig ][ dest ].push_back( cost ); }
132 TEdges& GetEdges( const TIndex& orig, const TIndex& dest );
133 const TEdges& GetEdges( const TIndex& orig, const TIndex& dest ) const;
134 bool HasEdge( const TIndex& orig, const TIndex& dest ) const;
135 void RemoveEdge( const TIndex& orig, const TIndex& dest, const TCost& cost );
136 void RemoveEdges( const TIndex& orig, const TIndex& dest );
138 /*! \brief Returns graph's sinks.
140 * A sink is a special vertex which does not have any "exiting" edges.
142 * @return Sinks ordered by their index.
144 std::set< TIndex, TIndexCompare > GetSinks( ) const;
151 // Purposely not implemented
152 Graph( const Self& other );
153 Self& operator=( const Self& other );
156 TVertices m_Vertices;
164 #ifndef ITK_MANUAL_INSTANTIATION
165 # include <fpa/Base/Graph.hxx>
166 #endif // ITK_MANUAL_INSTANTIATION
168 #endif // __fpa__Base__Graph__h__