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