Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/smesh.git] / src / SMDS / SMDS_MeshElementIDFactory.cxx
1 //  Copyright (C) 2007-2008  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.
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 //  SMESH SMDS : implementaion of Salome mesh data structure
23 //  File   : SMDS_MeshElementIDFactory.cxx
24 //  Author : Jean-Michel BOULCOURT
25 //  Module : SMESH
26 //
27 #ifdef _MSC_VER
28 #pragma warning(disable:4786)
29 #endif
30
31 #include "SMDS_MeshElementIDFactory.hxx"
32 #include "SMDS_MeshElement.hxx"
33
34 using namespace std;
35
36 //=======================================================================
37 //function : SMDS_MeshElementIDFactory
38 //purpose  : 
39 //=======================================================================
40 SMDS_MeshElementIDFactory::SMDS_MeshElementIDFactory():
41   SMDS_MeshIDFactory(),
42   myMin(0), myMax(0)
43 {
44 }
45
46 //=======================================================================
47 //function : BindID
48 //purpose  : 
49 //=======================================================================
50 bool SMDS_MeshElementIDFactory::BindID(int ID, SMDS_MeshElement * elem)
51 {
52   if (myIDElements.IsBound(ID))
53     return false;
54   myIDElements.Bind(ID,elem);
55   elem->myID=ID;
56   updateMinMax (ID);
57   return true;
58 }
59
60 //=======================================================================
61 //function : MeshElement
62 //purpose  : 
63 //=======================================================================
64 SMDS_MeshElement* SMDS_MeshElementIDFactory::MeshElement(int ID)
65 {
66   if (!myIDElements.IsBound(ID))
67     return NULL;
68   return myIDElements.Find(ID);
69 }
70
71
72 //=======================================================================
73 //function : GetFreeID
74 //purpose  : 
75 //=======================================================================
76 int SMDS_MeshElementIDFactory::GetFreeID()
77 {
78   int ID;
79   do {
80     ID = SMDS_MeshIDFactory::GetFreeID();
81   } while (myIDElements.IsBound(ID));
82   return ID;
83 }
84
85 //=======================================================================
86 //function : ReleaseID
87 //purpose  : 
88 //=======================================================================
89 void SMDS_MeshElementIDFactory::ReleaseID(const int ID)
90 {
91   myIDElements.UnBind(ID);
92   SMDS_MeshIDFactory::ReleaseID(ID);
93   if (ID == myMax)
94     myMax = 0;
95   if (ID == myMin)
96     myMin = 0;
97 }
98
99 //=======================================================================
100 //function : GetMaxID
101 //purpose  : 
102 //=======================================================================
103
104 int SMDS_MeshElementIDFactory::GetMaxID() const
105 {
106   if (myMax == 0)
107     updateMinMax();
108   return myMax;
109 }
110
111 //=======================================================================
112 //function : GetMinID
113 //purpose  : 
114 //=======================================================================
115
116 int SMDS_MeshElementIDFactory::GetMinID() const
117 {
118   if (myMin == 0)
119     updateMinMax();
120   return myMin;
121 }
122
123 //=======================================================================
124 //function : updateMinMax
125 //purpose  : 
126 //=======================================================================
127
128 void SMDS_MeshElementIDFactory::updateMinMax() const
129 {
130   myMin = IntegerLast();
131   myMax = 0;
132   SMDS_IdElementMap::Iterator it(myIDElements);
133   for (; it.More(); it.Next())
134     updateMinMax (it.Key());
135   if (myMin == IntegerLast())
136     myMin = 0;
137 }
138
139 //=======================================================================
140 //function : elementsIterator
141 //purpose  : Return an iterator on elements of the factory
142 //=======================================================================
143
144 class SMDS_Fact_MyElemIterator:public SMDS_ElemIterator
145 {
146   SMDS_IdElementMap::Iterator myIterator;
147  public:
148   SMDS_Fact_MyElemIterator(const SMDS_IdElementMap& s):myIterator(s)
149   {}
150
151   bool more()
152   {
153     return myIterator.More() != Standard_False;
154   }
155
156   const SMDS_MeshElement* next()
157   {
158     const SMDS_MeshElement* current = myIterator.Value();
159     myIterator.Next();
160     return current;
161   }
162 };
163
164 SMDS_ElemIteratorPtr SMDS_MeshElementIDFactory::elementsIterator() const
165 {
166   return SMDS_ElemIteratorPtr
167     (new SMDS_Fact_MyElemIterator(myIDElements));
168 }
169
170 void SMDS_MeshElementIDFactory::Clear()
171 {
172   myIDElements.Clear();
173   myMin = myMax = 0;
174   SMDS_MeshIDFactory::Clear();
175 }