]> Creatis software - cpPlugins.git/blobdiff - lib/cpExtensions/DataStructures/Graph.hxx
More on graph editor
[cpPlugins.git] / lib / cpExtensions / DataStructures / Graph.hxx
index 9322977597fdbeed7be3208866e1f13351ad2b03..d89be75d279fed8105849d18a16913f4d5d195eb 100644 (file)
@@ -73,6 +73,15 @@ EndEdgesRows( ) const
   return( this->m_Matrix.end( ) );
 }
 
+// -------------------------------------------------------------------------
+template< class V, class C, class I >
+void cpExtensions::DataStructures::Graph< V, C, I >::
+Clear( )
+{
+  this->m_Vertices.clear( );
+  this->m_Matrix.clear( );
+}
+
 // -------------------------------------------------------------------------
 template< class V, class C, class I >
 bool cpExtensions::DataStructures::Graph< V, C, I >::
@@ -84,11 +93,72 @@ HasVertexIndex( const I& index ) const
 // -------------------------------------------------------------------------
 template< class V, class C, class I >
 void cpExtensions::DataStructures::Graph< V, C, I >::
-InsertVertex( const I& index, V& vertex )
+SetVertex( const I& index, V& vertex )
 {
   this->m_Vertices[ index ] = vertex;
 }
 
+// -------------------------------------------------------------------------
+template< class V, class C, class I >
+bool cpExtensions::DataStructures::Graph< V, C, I >::
+RenameVertex( const I& old_index, const I& new_index )
+{
+  auto old_v = this->m_Vertices.find( old_index );
+  auto new_v = this->m_Vertices.find( new_index );
+  if( old_v != this->m_Vertices.end( ) && new_v == this->m_Vertices.end( ) )
+  {
+    // Replace vertex
+    typename TVertices::value_type new_entry( new_index, old_v->second );
+    new_v = this->m_Vertices.insert( new_entry ).first;
+    this->m_Vertices.erase( old_index );
+
+    // Duplicate edges
+    auto mIt = this->m_Matrix.begin( );
+    auto found_row = this->m_Matrix.end( );
+    for( ; mIt != this->m_Matrix.end( ); ++mIt )
+    {
+      if( mIt->first == old_index )
+        found_row = mIt;
+
+      auto rIt = mIt->second.begin( );
+      for( ; rIt != mIt->second.end( ); ++rIt )
+      {
+        if( mIt->first == old_index )
+          this->m_Matrix[ new_index ][ rIt->first ].push_back( rIt->second );
+        else if( rIt->first == old_index )
+          this->m_Matrix[ mIt->first ][ new_index ].push_back( rIt->second );
+
+      } // rof
+
+    } // rof
+
+    // Delete old edges
+    if( found_row != this->m_Matrix.end( ) )
+      this->m_Matrix.erase( found_row );
+
+    mIt = this->m_Matrix.begin( );
+    for( ; mIt != this->m_Matrix.end( ); ++mIt )
+    {
+      auto rIt = mIt->second.begin( );
+      while( rIt != mIt->second.end( ) )
+      {
+        if( rIt->first == old_index )
+        {
+          mIt->second.erase( rIt );
+          rIt = mIt->second.begin( );
+        }
+        else
+          ++rIt;
+
+      } // elihw
+
+    } // rof
+    return( true );
+  }
+  else
+    return( false );
+}
+
 // -------------------------------------------------------------------------
 template< class V, class C, class I >
 V& cpExtensions::DataStructures::Graph< V, C, I >::