Salome HOME
52457: Addition of hypotheses is 8 time longer than meshing.
[modules/smesh.git] / src / SMESHDS / SMESHDS_TSubMeshHolder.hxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #ifndef __SMESHDS_SubMeshHolder_HXX__
23 #define __SMESHDS_SubMeshHolder_HXX__
24
25 #include <vector>
26 #include <map>
27
28 //=======================================================================
29 /*!
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.
32  *
33  * Issue 52457: Addition of hypotheses is 8 time longer than meshing.
34  */
35 //=======================================================================
36
37 template <class SUBMESH>
38 class SMESHDS_TSubMeshHolder
39 {
40   std::vector< SUBMESH* >   myVec; // for ID >= 0
41   std::map< int, SUBMESH* > myMap; // for ID < 0
42
43 public:
44
45   ~SMESHDS_TSubMeshHolder()
46   {
47     DeleteAll();
48   }
49   void Add( int id, SUBMESH* sm )
50   {
51     if ( id < 0 )
52     {
53       myMap[ id ] = sm;
54     }
55     else
56     {
57       if ( myVec.size() <= id )
58         myVec.resize( id+1, (SUBMESH*) NULL );
59       myVec[ id ] = sm;
60     }
61   }
62   SUBMESH* Get( int id ) const
63   {
64     if ( id < 0 )
65     {
66       typename std::map< int, SUBMESH* >::const_iterator i2sm = myMap.find( id );
67       return (SUBMESH*) ( i2sm == myMap.end() ? NULL : i2sm->second );
68     }
69     else
70     {
71       return (SUBMESH*) ( id >= myVec.size() ? NULL : myVec[ id ]);
72     }
73   }
74   void DeleteAll()
75   {
76     for ( size_t i = 0; i < myVec.size(); ++i )
77       delete myVec[i];
78     myVec.clear();
79
80     typename std::map< int, SUBMESH* >::iterator i2sm = myMap.begin();
81     for ( ; i2sm != myMap.end(); ++i2sm )
82       delete i2sm->second;
83     myMap.clear();
84   }
85   int GetMinID() const
86   {
87     return myMap.empty() ? 0 : myMap.begin()->first;
88   }
89   int GetMaxID() const
90   {
91     return myVec.empty() ? ( myMap.empty() ? 0 : myMap.rbegin()->first ) : myVec.size();
92   }
93
94   //-----------------------------------------------------------------------
95   struct Iterator : public SMDS_Iterator< SUBMESH* >
96   {
97     const SMESHDS_TSubMeshHolder<SUBMESH>* myHolder;
98     SUBMESH* myNext;
99     int myCurID, myEndID, myIDDelta;
100
101     void init( const SMESHDS_TSubMeshHolder<SUBMESH>* holder,
102                int firstID, int endID, int delta )
103     {
104       myHolder  = holder;
105       myNext    = 0;
106       myCurID   = firstID;
107       myEndID   = endID;
108       myIDDelta = delta;
109
110       next();
111     }
112
113     bool more()
114     {
115       return myNext;
116     }
117
118     SUBMESH* next()
119     {
120       SUBMESH* res = myNext;
121       myNext = 0;
122       while ( !myNext && myCurID != myEndID )
123       {
124         myNext = myHolder->Get( myCurID );
125         myCurID += myIDDelta;
126       }
127       return res;
128     }
129     virtual ~Iterator() {}
130   };
131   //-----------------------------------------------------------------------
132
133   SMDS_Iterator< SUBMESH* >* GetIterator(const bool reverse=false) const
134   {
135     Iterator* iter = new Iterator;
136     if ( reverse ) iter->init( this, GetMaxID(), GetMinID()-1, -1 );
137     else           iter->init( this, GetMinID(), GetMaxID()+1, +1 );
138     return iter;
139   }
140 };
141
142
143 #endif