]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/DataStructures/Graph.hxx
e794c458375ba09f86edaead04308e40f1e6197d
[cpPlugins.git] / lib / cpExtensions / DataStructures / Graph.hxx
1 #ifndef __cpExtensions__DataStructures__Graph__hxx__
2 #define __cpExtensions__DataStructures__Graph__hxx__
3
4 // -------------------------------------------------------------------------
5 template< class V, class C, class I >
6 void cpExtensions::DataStructures::Graph< V, C, I >::
7 Clear( )
8 {
9   this->m_Vertices.clear( );
10   this->m_Matrix.clear( );
11 }
12
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 )
17 {
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( ) )
21   {
22     // Replace vertex
23     this->m_Vertices[ new_index ] = old_v->second;
24     this->m_Vertices.erase( old_index );
25
26     // Duplicate edges
27     auto mIt = this->m_Matrix.begin( );
28     auto found_row = this->m_Matrix.end( );
29     for( ; mIt != this->m_Matrix.end( ); ++mIt )
30     {
31       if( mIt->first == old_index )
32         found_row = mIt;
33
34       auto rIt = mIt->second.begin( );
35       for( ; rIt != mIt->second.end( ); ++rIt )
36       {
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;
41
42       } // rof
43
44     } // rof
45
46     // Delete old edges
47     if( found_row != this->m_Matrix.end( ) )
48       this->m_Matrix.erase( found_row );
49
50     mIt = this->m_Matrix.begin( );
51     for( ; mIt != this->m_Matrix.end( ); ++mIt )
52     {
53       auto rIt = mIt->second.begin( );
54       while( rIt != mIt->second.end( ) )
55       {
56         if( rIt->first == old_index )
57         {
58           mIt->second.erase( rIt );
59           rIt = mIt->second.begin( );
60         }
61         else
62           ++rIt;
63
64       } // elihw
65
66     } // rof
67     return( true );
68   }
69   else
70     return( false );
71 }
72
73 // -------------------------------------------------------------------------
74 template< class V, class C, class I >
75 void cpExtensions::DataStructures::Graph< V, C, I >::
76 RemoveVertex( const I& index )
77 {
78   auto i = this->m_Vertices.find( index );
79   if( i != this->m_Vertices.end( ) )
80   {
81     // Delete vertex
82     this->m_Vertices.erase( i );
83
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 );
88
89     // Delete edges arriving to given vertex
90     mIt = this->m_Matrix.begin( );
91     for( ; mIt != this->m_Matrix.end( ); ++mIt )
92     {
93       auto rIt = mIt->second.begin( );
94       while( rIt != mIt->second.end( ) )
95       {
96         if( rIt->first == index )
97         {
98           mIt->second.erase( rIt );
99           rIt = mIt->second.begin( );
100         }
101         else
102           ++rIt;
103
104       } // elihw
105
106     } // rof
107
108   } // fi
109 }
110
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
115 {
116   auto mIt = this->m_Matrix.find( orig );
117   if( mIt != this->m_Matrix.end( ) )
118     return( mIt->second.find( dest ) != mIt->second.end( ) );
119   else
120     return( false );
121 }
122
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 )
127 {
128   auto m = this->m_Matrix.find( orig );
129   if( m != this->m_Matrix.end( ) )
130   {
131     auto r = m->second.find( dest );
132     if( r != m->second.end( ) )
133     {
134       auto e = r->second.end( );
135       for(
136         auto i = r->second.begin( );
137         i != r->second.end( ) && e == r->second.end( );
138         ++i
139         )
140         if( *i == cost )
141           e = i;
142       if( e != r->second.end( ) )
143       {
144         r->second.erase( e );
145         if( r->second.size( ) == 0 )
146         {
147           m->second.erase( r );
148           if( m->second.size( ) == 0 )
149             this->m_Matrix.erase( m );
150
151         } // fi
152
153       } // fi
154
155     } // fi
156
157   } // fi
158 }
159
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 )
164 {
165   auto m = this->m_Matrix.find( orig );
166   if( m != this->m_Matrix.end( ) )
167   {
168     auto r = m->second.find( dest );
169     if( r != m->second.end( ) )
170     {
171       m->second.erase( r );
172       if( m->second.size( ) == 0 )
173         this->m_Matrix.erase( m );
174
175     } // fi
176
177   } // fi
178
179 }
180
181 // -------------------------------------------------------------------------
182 template< class V, class C, class I >
183 std::set< I > cpExtensions::DataStructures::Graph< V, C, I >::
184 GetSinks( ) const
185 {
186   std::set< I > sinks;
187
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 );
194
195   return( sinks );
196 }
197
198 // -------------------------------------------------------------------------
199 template< class V, class C, class I >
200 cpExtensions::DataStructures::Graph< V, C, I >::
201 Graph( )
202   : Superclass( )
203 {
204 }
205
206 // -------------------------------------------------------------------------
207 template< class V, class C, class I >
208 cpExtensions::DataStructures::Graph< V, C, I >::
209 ~Graph( )
210 {
211 }
212
213 #endif // __cpExtensions__DataStructures__Graph__hxx__
214
215 // eof - $RCSfile$