Salome HOME
Merge branch 'master' into pre/penta18
[modules/smesh.git] / src / SMESHDS / SMESHDS_SubMesh.cxx
index 21d6da43a804367736d594a50a4a18d2532f0eba..89287301641073332320037784571d4cf499aa12 100644 (file)
@@ -257,38 +257,44 @@ int SMESHDS_SubMesh::NbNodes() const
 }
 
 /*!
- * template class used for iteration on submesh elements. Interface of iterator remains
- * unchanged after redesign of SMDS to avoid modification everywhere in SMESH.
- * instances are stored in shared_ptr for automatic destruction.
- * Container is copied for iteration, because original can be modified
- * by addition of elements, for instance, and then reallocated (vector)
+ * Template class used for iteration on vector of elements which can resize
+ * during iteration. The iterator returns only elements present upon its creation.
  */
 template <class ELEM, typename TSET> class MySetIterator : public SMDS_Iterator<ELEM>
 {
 protected:
-  typename TSET::const_iterator _it, _end;
-  TSET _table;
+  int _iCur, _iEnd, _iDelta;
+  const TSET& _table;
 public:
-  MySetIterator(const TSET& table)
+  MySetIterator(const TSET& table, bool reverse): _table( table )
   {
-    _table = table;
-    _it = _table.begin();
-    _end = _table.end();
-    while ((_it != _end) && (*_it == 0))
-      _it++;
+    if ( reverse )
+    {
+      _iCur = _table.size()-1;
+      _iEnd = -1;
+      _iDelta = -1;
+    }
+    else
+    {
+      _iCur = 0;
+      _iEnd = _table.size();
+      _iDelta = 1;
+    }
+    if ( more() && !_table[ _iCur ])
+      next();
   }
 
   virtual bool more()
   {
-    while ((_it != _end) && (*_it == 0))
-      _it++;
-    return (_it != _end);
+    return ( _iEnd - _iCur ) * _iDelta > 0;
   }
 
   virtual ELEM next()
   {
-    ELEM e = *_it;
-    _it++;
+    ELEM e = more() ? _table[ _iCur ] : 0;
+    _iCur += _iDelta;
+    while ( more() && !_table[ _iCur ])
+      _iCur += _iDelta;
     return e;
   }
 };
@@ -361,13 +367,13 @@ public:
 //purpose  :
 //=======================================================================
 
-SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
+SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements( bool reverse ) const
 {
   if ( IsComplexSubmesh() )
     return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
 
-  return SMDS_ElemIteratorPtr
-    (new MySetIterator<const SMDS_MeshElement*, std::vector<const SMDS_MeshElement*> >(myElements));
+  typedef MySetIterator< const SMDS_MeshElement*, std::vector<const SMDS_MeshElement*> > TIter;
+  return SMDS_ElemIteratorPtr( new TIter( myElements, reverse ));
 }
 
 //=======================================================================
@@ -375,13 +381,13 @@ SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
 //purpose  :
 //=======================================================================
 
-SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
+SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes( bool reverse ) const
 {
   if ( IsComplexSubmesh() )
     return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
 
-  return SMDS_NodeIteratorPtr
-    (new MySetIterator<const SMDS_MeshNode*, std::vector<const SMDS_MeshNode*> >(myNodes));
+  typedef MySetIterator< const SMDS_MeshNode*, std::vector<const SMDS_MeshNode*> > TIter;
+  return SMDS_NodeIteratorPtr( new TIter( myNodes, reverse ));
 }
 
 //=======================================================================
@@ -478,7 +484,7 @@ void SMESHDS_SubMesh::RemoveAllSubmeshes()
 
 //=======================================================================
 //function : ContainsSubMesh
-//purpose  : 
+//purpose  :
 //=======================================================================
 
 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
@@ -488,7 +494,7 @@ bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
 
 //=======================================================================
 //function : GetSubMeshIterator
-//purpose  : 
+//purpose  :
 //=======================================================================
 
 SMESHDS_SubMeshIteratorPtr SMESHDS_SubMesh::GetSubMeshIterator() const
@@ -561,6 +567,10 @@ void SMESHDS_SubMesh::compactList()
     myElements.swap(newElems);
     myUnusedIdElements = 0;
   }
+  else
+  {
+    std::vector<const SMDS_MeshElement*>( myElements ).swap( myElements );
+  }
 
   if ( myUnusedIdNodes > 0 )
   {
@@ -576,6 +586,10 @@ void SMESHDS_SubMesh::compactList()
     myNodes.swap(newNodes);
     myUnusedIdNodes = 0;
   }
+  else
+  {
+    std::vector<const SMDS_MeshNode*>( myNodes ).swap( myNodes );
+  }
 }
 
 //=======================================================================