1 // Copyright (C) 2007-2024 CEA, EDF, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #ifndef __SMESHDS_SubMeshHolder_HXX__
23 #define __SMESHDS_SubMeshHolder_HXX__
28 //=======================================================================
30 * \brief A binder of a sub-mesh to its ID which can be negative. Provides fast
31 * access to a sub-mesh by its ID.
33 * Issue 52457: Addition of hypotheses is 8 time longer than meshing.
35 //=======================================================================
37 template <class SUBMESH>
38 class SMESHDS_TSubMeshHolder
40 std::vector< SUBMESH* > myVec; // for ID >= 0
41 std::map< int, SUBMESH* > myMap; // for ID < 0
45 ~SMESHDS_TSubMeshHolder()
49 void Add( int id, SUBMESH* sm )
57 if ( (int)myVec.size() <= id )
58 myVec.resize( id+1, (SUBMESH*) NULL );
62 SUBMESH* Get( int id ) const
66 typename std::map< int, SUBMESH* >::const_iterator i2sm = myMap.find( id );
67 return (SUBMESH*) ( i2sm == myMap.end() ? NULL : i2sm->second );
71 return (SUBMESH*) ( id >= (int)myVec.size() ? NULL : myVec[ id ]);
76 for ( size_t i = 0; i < myVec.size(); ++i )
77 if ( SUBMESH* sm = myVec[i] )
79 myVec[i] = 0; // avoid access via Get(i)
84 typename std::map< int, SUBMESH* >::iterator i2sm = myMap.begin();
85 for ( ; i2sm != myMap.end(); ++i2sm )
86 if ( SUBMESH* sm = i2sm->second )
88 i2sm->second = 0; // avoid access via Get(i)
95 return myMap.empty() ? 0 : myMap.begin()->first;
99 return myVec.empty() ? ( myMap.empty() ? 0 : myMap.rbegin()->first ) : myVec.size();
102 //-----------------------------------------------------------------------
103 struct Iterator : public SMDS_Iterator< SUBMESH* >
105 const SMESHDS_TSubMeshHolder<SUBMESH>* myHolder;
107 int myCurID, myEndID, myIDDelta;
109 void init( const SMESHDS_TSubMeshHolder<SUBMESH>* holder,
110 int firstID, int endID, int delta )
128 SUBMESH* res = myNext;
130 while ( !myNext && myCurID != myEndID )
132 myNext = myHolder->Get( myCurID );
133 myCurID += myIDDelta;
137 virtual ~Iterator() {}
139 //-----------------------------------------------------------------------
141 SMDS_Iterator< SUBMESH* >* GetIterator(const bool reverse=false) const
143 Iterator* iter = new Iterator;
144 if ( reverse ) iter->init( this, GetMaxID(), GetMinID()-1, -1 );
145 else iter->init( this, GetMinID(), GetMaxID()+1, +1 );