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 = std::find( r->second.begin( ), r->second.end( ), cost );
135 if( e != r->second.end( ) )
137 r->second.erase( e );
138 if( r->second.size( ) == 0 )
140 m->second.erase( r );
141 if( m->second.size( ) == 0 )
142 this->m_Matrix.erase( m );
153 // -------------------------------------------------------------------------
154 template< class V, class C, class I >
155 void cpExtensions::DataStructures::Graph< V, C, I >::
156 RemoveEdges( const I& orig, const I& dest )
158 auto m = this->m_Matrix.find( orig );
159 if( m != this->m_Matrix.end( ) )
161 auto r = m->second.find( dest );
162 if( r != m->second.end( ) )
164 m->second.erase( r );
165 if( m->second.size( ) == 0 )
166 this->m_Matrix.erase( m );
174 // -------------------------------------------------------------------------
175 template< class V, class C, class I >
176 std::set< I > cpExtensions::DataStructures::Graph< V, C, I >::
181 auto vIt = this->m_Vertices.begin( );
182 for( ; vIt != this->m_Vertices.end( ); ++vIt )
183 sinks.insert( vIt->first );
184 auto mIt = this->m_Matrix.begin( );
185 for( ; mIt != this->m_Matrix.end( ); ++mIt )
186 sinks.erase( mIt->first );
191 // -------------------------------------------------------------------------
192 template< class V, class C, class I >
193 cpExtensions::DataStructures::Graph< V, C, I >::
199 // -------------------------------------------------------------------------
200 template< class V, class C, class I >
201 cpExtensions::DataStructures::Graph< V, C, I >::
206 #endif // __CPEXTENSIONS__DATASTRUCTURES__GRAPH__HXX__