]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/DataStructures/Graph.hxx
First dump for version 0.1.0
[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 = std::find( r->second.begin( ), r->second.end( ), cost );
135       if( e != r->second.end( ) )
136       {
137         r->second.erase( e );
138         if( r->second.size( ) == 0 )
139         {
140           m->second.erase( r );
141           if( m->second.size( ) == 0 )
142             this->m_Matrix.erase( m );
143
144         } // fi
145
146       } // fi
147
148     } // fi
149
150   } // fi
151 }
152
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 )
157 {
158   auto m = this->m_Matrix.find( orig );
159   if( m != this->m_Matrix.end( ) )
160   {
161     auto r = m->second.find( dest );
162     if( r != m->second.end( ) )
163     {
164       m->second.erase( r );
165       if( m->second.size( ) == 0 )
166         this->m_Matrix.erase( m );
167
168     } // fi
169
170   } // fi
171
172 }
173
174 // -------------------------------------------------------------------------
175 template< class V, class C, class I >
176 std::set< I > cpExtensions::DataStructures::Graph< V, C, I >::
177 GetSinks( ) const
178 {
179   std::set< I > sinks;
180
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 );
187
188   return( sinks );
189 }
190
191 // -------------------------------------------------------------------------
192 template< class V, class C, class I >
193 cpExtensions::DataStructures::Graph< V, C, I >::
194 Graph( )
195   : Superclass( )
196 {
197 }
198
199 // -------------------------------------------------------------------------
200 template< class V, class C, class I >
201 cpExtensions::DataStructures::Graph< V, C, I >::
202 ~Graph( )
203 {
204 }
205
206 #endif // __CPEXTENSIONS__DATASTRUCTURES__GRAPH__HXX__
207
208 // eof - $RCSfile$