Version: 9.12.0
SMESHDS_TSubMeshHolder.hxx
Go to the documentation of this file.
1 // Copyright (C) 2007-2023 CEA, EDF, 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 //=======================================================================
35 //=======================================================================
36 
37 template <class SUBMESH>
39 {
40  std::vector< SUBMESH* > myVec; // for ID >= 0
41  std::map< int, SUBMESH* > myMap; // for ID < 0
42 
43 public:
44 
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 ( (int)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 >= (int)myVec.size() ? NULL : myVec[ id ]);
72  }
73  }
74  void DeleteAll()
75  {
76  for ( size_t i = 0; i < myVec.size(); ++i )
77  if ( SUBMESH* sm = myVec[i] )
78  {
79  myVec[i] = 0; // avoid access via Get(i)
80  delete sm;
81  }
82  myVec.clear();
83 
84  typename std::map< int, SUBMESH* >::iterator i2sm = myMap.begin();
85  for ( ; i2sm != myMap.end(); ++i2sm )
86  if ( SUBMESH* sm = i2sm->second )
87  {
88  i2sm->second = 0; // avoid access via Get(i)
89  delete sm;
90  }
91  myMap.clear();
92  }
93  int GetMinID() const
94  {
95  return myMap.empty() ? 0 : myMap.begin()->first;
96  }
97  int GetMaxID() const
98  {
99  return myVec.empty() ? ( myMap.empty() ? 0 : myMap.rbegin()->first ) : myVec.size();
100  }
101 
102  //-----------------------------------------------------------------------
103  struct Iterator : public SMDS_Iterator< SUBMESH* >
104  {
108 
110  int firstID, int endID, int delta )
111  {
112  myHolder = holder;
113  myNext = 0;
114  myCurID = firstID;
115  myEndID = endID;
116  myIDDelta = delta;
117 
118  next();
119  }
120 
121  bool more()
122  {
123  return myNext;
124  }
125 
127  {
128  SUBMESH* res = myNext;
129  myNext = 0;
130  while ( !myNext && myCurID != myEndID )
131  {
132  myNext = myHolder->Get( myCurID );
133  myCurID += myIDDelta;
134  }
135  return res;
136  }
137  virtual ~Iterator() {}
138  };
139  //-----------------------------------------------------------------------
140 
142  {
143  Iterator* iter = new Iterator;
144  if ( reverse ) iter->init( this, GetMaxID(), GetMinID()-1, -1 );
145  else iter->init( this, GetMinID(), GetMaxID()+1, +1 );
146  return iter;
147  }
148 };
149 
150 
151 #endif
void reverse(vector< T > &vec)
reverse order of vector elements
Definition: StdMeshers_FaceSide.cxx:941
Abstract class for iterators.
Definition: SMDS_Iterator.hxx:33
A binder of a sub-mesh to its ID which can be negative.
Definition: SMESHDS_TSubMeshHolder.hxx:39
SMDS_Iterator< SUBMESH * > * GetIterator(const bool reverse=false) const
Definition: SMESHDS_TSubMeshHolder.hxx:141
int GetMaxID() const
Definition: SMESHDS_TSubMeshHolder.hxx:97
~SMESHDS_TSubMeshHolder()
Definition: SMESHDS_TSubMeshHolder.hxx:45
void Add(int id, SUBMESH *sm)
Definition: SMESHDS_TSubMeshHolder.hxx:49
std::vector< SUBMESH * > myVec
Definition: SMESHDS_TSubMeshHolder.hxx:40
void DeleteAll()
Definition: SMESHDS_TSubMeshHolder.hxx:74
std::map< int, SUBMESH * > myMap
Definition: SMESHDS_TSubMeshHolder.hxx:41
int GetMinID() const
Definition: SMESHDS_TSubMeshHolder.hxx:93
SUBMESH * Get(int id) const
Definition: SMESHDS_TSubMeshHolder.hxx:62
@ SUBMESH
Definition: SMESH_Type.h:47
Definition: SMESHDS_TSubMeshHolder.hxx:104
void init(const SMESHDS_TSubMeshHolder< SUBMESH > *holder, int firstID, int endID, int delta)
Definition: SMESHDS_TSubMeshHolder.hxx:109
bool more()
Return true if and only if there are other object in this iterator.
Definition: SMESHDS_TSubMeshHolder.hxx:121
SUBMESH * next()
Return the current object and step to the next one.
Definition: SMESHDS_TSubMeshHolder.hxx:126
int myCurID
Definition: SMESHDS_TSubMeshHolder.hxx:107
virtual ~Iterator()
Definition: SMESHDS_TSubMeshHolder.hxx:137
SUBMESH * myNext
Definition: SMESHDS_TSubMeshHolder.hxx:106
const SMESHDS_TSubMeshHolder< SUBMESH > * myHolder
Definition: SMESHDS_TSubMeshHolder.hxx:105
int myEndID
Definition: SMESHDS_TSubMeshHolder.hxx:107
int myIDDelta
Definition: SMESHDS_TSubMeshHolder.hxx:107