1 #ifndef __cpExtensions__DataStructures__Graph__hxx__
2 #define __cpExtensions__DataStructures__Graph__hxx__
4 // -------------------------------------------------------------------------
5 template< class V, class C, class I >
6 void cpExtensions::DataStructures::Graph< V, C, I >::
9 this->m_Vertices.clear( );
10 this->m_Matrix.clear( );
13 // -------------------------------------------------------------------------
14 template< class V, class C, class I >
15 bool cpExtensions::DataStructures::Graph< V, C, I >::
16 RenameVertex( const I& old_index, const I& new_index )
18 auto old_v = this->m_Vertices.find( old_index );
19 auto new_v = this->m_Vertices.find( new_index );
20 if( old_v != this->m_Vertices.end( ) && new_v == this->m_Vertices.end( ) )
23 this->m_Vertices[ new_index ] = old_v->second;
24 this->m_Vertices.erase( old_index );
27 auto mIt = this->m_Matrix.begin( );
28 auto found_row = this->m_Matrix.end( );
29 for( ; mIt != this->m_Matrix.end( ); ++mIt )
31 if( mIt->first == old_index )
34 auto rIt = mIt->second.begin( );
35 for( ; rIt != mIt->second.end( ); ++rIt )
37 if( mIt->first == old_index )
38 this->m_Matrix[ new_index ][ rIt->first ] = rIt->second;
39 else if( rIt->first == old_index )
40 this->m_Matrix[ mIt->first ][ new_index ] = rIt->second;
47 if( found_row != this->m_Matrix.end( ) )
48 this->m_Matrix.erase( found_row );
50 mIt = this->m_Matrix.begin( );
51 for( ; mIt != this->m_Matrix.end( ); ++mIt )
53 auto rIt = mIt->second.begin( );
54 while( rIt != mIt->second.end( ) )
56 if( rIt->first == old_index )
58 mIt->second.erase( rIt );
59 rIt = mIt->second.begin( );
73 // -------------------------------------------------------------------------
74 template< class V, class C, class I >
75 void cpExtensions::DataStructures::Graph< V, C, I >::
76 RemoveVertex( const I& index )
78 auto i = this->m_Vertices.find( index );
79 if( i != this->m_Vertices.end( ) )
82 this->m_Vertices.erase( i );
84 // Delete edges starting from given vertex
85 auto mIt = this->m_Matrix.find( index );
86 if( mIt != this->m_Matrix.end( ) )
87 this->m_Matrix.erase( mIt );
89 // Delete edges arriving to given vertex
90 mIt = this->m_Matrix.begin( );
91 for( ; mIt != this->m_Matrix.end( ); ++mIt )
93 auto rIt = mIt->second.begin( );
94 while( rIt != mIt->second.end( ) )
96 if( rIt->first == index )
98 mIt->second.erase( rIt );
99 rIt = mIt->second.begin( );
111 // -------------------------------------------------------------------------
112 template< class V, class C, class I >
113 bool cpExtensions::DataStructures::Graph< V, C, I >::
114 HasEdge( const I& orig, const I& dest ) const
116 auto mIt = this->m_Matrix.find( orig );
117 if( mIt != this->m_Matrix.end( ) )
118 return( mIt->second.find( dest ) != mIt->second.end( ) );
123 // -------------------------------------------------------------------------
124 template< class V, class C, class I >
125 void cpExtensions::DataStructures::Graph< V, C, I >::
126 RemoveEdge( const I& orig, const I& dest, const C& cost )
128 auto m = this->m_Matrix.find( orig );
129 if( m != this->m_Matrix.end( ) )
131 auto r = m->second.find( dest );
132 if( r != m->second.end( ) )
134 auto e = r->second.end( );
136 auto i = r->second.begin( );
137 i != r->second.end( ) && e == r->second.end( );
142 if( e != r->second.end( ) )
144 r->second.erase( e );
145 if( r->second.size( ) == 0 )
147 m->second.erase( r );
148 if( m->second.size( ) == 0 )
149 this->m_Matrix.erase( m );
160 // -------------------------------------------------------------------------
161 template< class V, class C, class I >
162 void cpExtensions::DataStructures::Graph< V, C, I >::
163 RemoveEdges( const I& orig, const I& dest )
165 auto m = this->m_Matrix.find( orig );
166 if( m != this->m_Matrix.end( ) )
168 auto r = m->second.find( dest );
169 if( r != m->second.end( ) )
171 m->second.erase( r );
172 if( m->second.size( ) == 0 )
173 this->m_Matrix.erase( m );
181 // -------------------------------------------------------------------------
182 template< class V, class C, class I >
183 std::set< I > cpExtensions::DataStructures::Graph< V, C, I >::
188 auto vIt = this->m_Vertices.begin( );
189 for( ; vIt != this->m_Vertices.end( ); ++vIt )
190 sinks.insert( vIt->first );
191 auto mIt = this->m_Matrix.begin( );
192 for( ; mIt != this->m_Matrix.end( ); ++mIt )
193 sinks.erase( mIt->first );
198 // -------------------------------------------------------------------------
199 template< class V, class C, class I >
200 cpExtensions::DataStructures::Graph< V, C, I >::
206 // -------------------------------------------------------------------------
207 template< class V, class C, class I >
208 cpExtensions::DataStructures::Graph< V, C, I >::
213 #endif // __cpExtensions__DataStructures__Graph__hxx__