1 #ifndef __CPEXTENSIONS__DATASTRUCTURES__GRAPH__HXX__
2 #define __CPEXTENSIONS__DATASTRUCTURES__GRAPH__HXX__
4 // -------------------------------------------------------------------------
5 template< class V, class C, class I >
6 typename cpExtensions::DataStructures::Graph< V, C, I >::
7 TVertices::iterator cpExtensions::DataStructures::Graph< V, C, I >::
10 return( this->m_Vertices.begin( ) );
13 // -------------------------------------------------------------------------
14 template< class V, class C, class I >
15 typename cpExtensions::DataStructures::Graph< V, C, I >::
16 TVertices::iterator cpExtensions::DataStructures::Graph< V, C, I >::
19 return( this->m_Vertices.end( ) );
22 // -------------------------------------------------------------------------
23 template< class V, class C, class I >
24 typename cpExtensions::DataStructures::Graph< V, C, I >::
25 TVertices::const_iterator cpExtensions::DataStructures::Graph< V, C, I >::
26 BeginVertices( ) const
28 return( this->m_Vertices.begin( ) );
31 // -------------------------------------------------------------------------
32 template< class V, class C, class I >
33 typename cpExtensions::DataStructures::Graph< V, C, I >::
34 TVertices::const_iterator cpExtensions::DataStructures::Graph< V, C, I >::
37 return( this->m_Vertices.end( ) );
40 // -------------------------------------------------------------------------
41 template< class V, class C, class I >
42 typename cpExtensions::DataStructures::Graph< V, C, I >::
43 TMatrix::iterator cpExtensions::DataStructures::Graph< V, C, I >::
46 return( this->m_Matrix.begin( ) );
49 // -------------------------------------------------------------------------
50 template< class V, class C, class I >
51 typename cpExtensions::DataStructures::Graph< V, C, I >::
52 TMatrix::iterator cpExtensions::DataStructures::Graph< V, C, I >::
55 return( this->m_Matrix.end( ) );
58 // -------------------------------------------------------------------------
59 template< class V, class C, class I >
60 typename cpExtensions::DataStructures::Graph< V, C, I >::
61 TMatrix::const_iterator cpExtensions::DataStructures::Graph< V, C, I >::
62 BeginEdgesRows( ) const
64 return( this->m_Matrix.begin( ) );
67 // -------------------------------------------------------------------------
68 template< class V, class C, class I >
69 typename cpExtensions::DataStructures::Graph< V, C, I >::
70 TMatrix::const_iterator cpExtensions::DataStructures::Graph< V, C, I >::
73 return( this->m_Matrix.end( ) );
76 // -------------------------------------------------------------------------
77 template< class V, class C, class I >
78 void cpExtensions::DataStructures::Graph< V, C, I >::
81 this->m_Vertices.clear( );
82 this->m_Matrix.clear( );
85 // -------------------------------------------------------------------------
86 template< class V, class C, class I >
87 bool cpExtensions::DataStructures::Graph< V, C, I >::
88 HasVertexIndex( const I& index ) const
90 return( this->m_Vertices.find( index ) != this->m_Vertices.end( ) );
93 // -------------------------------------------------------------------------
94 template< class V, class C, class I >
95 void cpExtensions::DataStructures::Graph< V, C, I >::
96 SetVertex( const I& index, V& vertex )
98 this->m_Vertices[ index ] = vertex;
101 // -------------------------------------------------------------------------
102 template< class V, class C, class I >
103 bool cpExtensions::DataStructures::Graph< V, C, I >::
104 RenameVertex( const I& old_index, const I& new_index )
106 auto old_v = this->m_Vertices.find( old_index );
107 auto new_v = this->m_Vertices.find( new_index );
108 if( old_v != this->m_Vertices.end( ) && new_v == this->m_Vertices.end( ) )
111 typename TVertices::value_type new_entry( new_index, old_v->second );
112 new_v = this->m_Vertices.insert( new_entry ).first;
113 this->m_Vertices.erase( old_index );
116 auto mIt = this->m_Matrix.begin( );
117 auto found_row = this->m_Matrix.end( );
118 for( ; mIt != this->m_Matrix.end( ); ++mIt )
120 if( mIt->first == old_index )
123 auto rIt = mIt->second.begin( );
124 for( ; rIt != mIt->second.end( ); ++rIt )
126 if( mIt->first == old_index )
127 this->m_Matrix[ new_index ][ rIt->first ] = rIt->second;
128 else if( rIt->first == old_index )
129 this->m_Matrix[ mIt->first ][ new_index ] = rIt->second;
136 if( found_row != this->m_Matrix.end( ) )
137 this->m_Matrix.erase( found_row );
139 mIt = this->m_Matrix.begin( );
140 for( ; mIt != this->m_Matrix.end( ); ++mIt )
142 auto rIt = mIt->second.begin( );
143 while( rIt != mIt->second.end( ) )
145 if( rIt->first == old_index )
147 mIt->second.erase( rIt );
148 rIt = mIt->second.begin( );
162 // -------------------------------------------------------------------------
163 template< class V, class C, class I >
164 V& cpExtensions::DataStructures::Graph< V, C, I >::
165 GetVertex( const I& index )
167 return( this->m_Vertices[ index ] );
170 // -------------------------------------------------------------------------
171 template< class V, class C, class I >
172 const V& cpExtensions::DataStructures::Graph< V, C, I >::
173 GetVertex( const I& index ) const
175 return( this->m_Vertices[ index ] );
178 // -------------------------------------------------------------------------
179 template< class V, class C, class I >
180 void cpExtensions::DataStructures::Graph< V, C, I >::
181 AddConnection( const I& orig, const I& dest, const C& cost )
183 this->m_Matrix[ orig ][ dest ].push_back( cost );
186 // -------------------------------------------------------------------------
187 template< class V, class C, class I >
188 std::set< I > cpExtensions::DataStructures::Graph< V, C, I >::
193 auto vIt = this->m_Vertices.begin( );
194 for( ; vIt != this->m_Vertices.end( ); ++vIt )
195 sinks.insert( vIt->first );
196 auto mIt = this->m_Matrix.begin( );
197 for( ; mIt != this->m_Matrix.end( ); ++mIt )
198 sinks.erase( mIt->first );
203 // -------------------------------------------------------------------------
204 template< class V, class C, class I >
205 cpExtensions::DataStructures::Graph< V, C, I >::
211 // -------------------------------------------------------------------------
212 template< class V, class C, class I >
213 cpExtensions::DataStructures::Graph< V, C, I >::
218 #endif // __CPEXTENSIONS__DATASTRUCTURES__GRAPH__HXX__