// Author : Jean-Michel BOULCOURT
// Module : SMESH
+#ifdef _MSC_VER
+#pragma warning(disable:4786)
+#endif
#include "SMDS_MeshElementIDFactory.hxx"
#include "SMDS_MeshElement.hxx"
//function : SMDS_MeshElementIDFactory
//purpose :
//=======================================================================
-SMDS_MeshElementIDFactory::SMDS_MeshElementIDFactory():SMDS_MeshIDFactory()
+SMDS_MeshElementIDFactory::SMDS_MeshElementIDFactory():
+ SMDS_MeshIDFactory(),
+ myMin(0), myMax(0)
{
}
//=======================================================================
bool SMDS_MeshElementIDFactory::BindID(int ID, SMDS_MeshElement * elem)
{
- bool bound=myIDElements.insert(
- map<int, SMDS_MeshElement*>::value_type(ID,elem)).second;
- if(bound) elem->myID=ID;
- return bound;
+ if (myIDElements.IsBound(ID))
+ return false;
+ myIDElements.Bind(ID,elem);
+ elem->myID=ID;
+ updateMinMax (ID);
+ return true;
}
//=======================================================================
//=======================================================================
SMDS_MeshElement* SMDS_MeshElementIDFactory::MeshElement(int ID)
{
- map<int, SMDS_MeshElement*>::iterator it=myIDElements.find(ID);
- if(it==myIDElements.end()) return NULL; else return (*it).second;
+ if (!myIDElements.IsBound(ID))
+ return NULL;
+ return myIDElements.Find(ID);
}
int ID;
do {
ID = SMDS_MeshIDFactory::GetFreeID();
- } while (myIDElements.find(ID) != myIDElements.end());
+ } while (myIDElements.IsBound(ID));
return ID;
}
//=======================================================================
void SMDS_MeshElementIDFactory::ReleaseID(const int ID)
{
- myIDElements.erase(ID);
+ myIDElements.UnBind(ID);
SMDS_MeshIDFactory::ReleaseID(ID);
+ if (ID == myMax)
+ myMax = 0;
+ if (ID == myMin)
+ myMin = 0;
}
//=======================================================================
int SMDS_MeshElementIDFactory::GetMaxID() const
{
- map<int, SMDS_MeshElement*>::const_reverse_iterator it = myIDElements.rbegin();
- if ( it != myIDElements.rend() )
- return (*it).first;
-
- return 0;
+ if (myMax == 0)
+ updateMinMax();
+ return myMax;
}
//=======================================================================
int SMDS_MeshElementIDFactory::GetMinID() const
{
- map<int, SMDS_MeshElement*>::const_iterator it = myIDElements.begin();
- if ( it != myIDElements.end() )
- return (*it).first;
+ if (myMin == 0)
+ updateMinMax();
+ return myMin;
+}
- return 0;
+//=======================================================================
+//function : updateMinMax
+//purpose :
+//=======================================================================
+
+void SMDS_MeshElementIDFactory::updateMinMax() const
+{
+ myMin = IntegerLast();
+ myMax = 0;
+ SMDS_IdElementMap::Iterator it(myIDElements);
+ for (; it.More(); it.Next())
+ updateMinMax (it.Key());
+ if (myMin == IntegerLast())
+ myMin = 0;
}
+//=======================================================================
+//function : elementsIterator
+//purpose : Return an iterator on elements of the factory
+//=======================================================================
+
+class SMDS_Fact_MyElemIterator:public SMDS_ElemIterator
+{
+ SMDS_IdElementMap::Iterator myIterator;
+ public:
+ SMDS_Fact_MyElemIterator(const SMDS_IdElementMap& s):myIterator(s)
+ {}
+
+ bool more()
+ {
+ return myIterator.More() != Standard_False;
+ }
+
+ const SMDS_MeshElement* next()
+ {
+ const SMDS_MeshElement* current = myIterator.Value();
+ myIterator.Next();
+ return current;
+ }
+};
+
+SMDS_ElemIteratorPtr SMDS_MeshElementIDFactory::elementsIterator() const
+{
+ return SMDS_ElemIteratorPtr
+ (new SMDS_Fact_MyElemIterator(myIDElements));
+}
#define _SMDS_MeshElementIDFactory_HeaderFile
#include "SMDS_MeshIDFactory.hxx"
+#include "SMDS_ElemIterator.hxx"
-#include <map>
+#include <NCollection_DataMap.hxx>
class SMDS_MeshElement;
-typedef std::map<int, SMDS_MeshElement *> SMDS_IdElementMap;
+typedef NCollection_DataMap<int, SMDS_MeshElement *> SMDS_IdElementMap;
class SMDS_MeshElementIDFactory:public SMDS_MeshIDFactory
{
- public:
- SMDS_MeshElementIDFactory();
- bool BindID(int ID, SMDS_MeshElement * elem);
- SMDS_MeshElement * MeshElement(int ID);
- virtual int GetFreeID();
- virtual void ReleaseID(int ID);
- int GetMaxID() const;
- int GetMinID() const;
- const SMDS_IdElementMap & GetIdElementMap() const { return myIDElements; }
- private:
- SMDS_IdElementMap myIDElements;
+public:
+ SMDS_MeshElementIDFactory();
+ bool BindID(int ID, SMDS_MeshElement * elem);
+ SMDS_MeshElement * MeshElement(int ID);
+ virtual int GetFreeID();
+ virtual void ReleaseID(int ID);
+ int GetMaxID() const;
+ int GetMinID() const;
+ SMDS_ElemIteratorPtr elementsIterator() const;
+private:
+ void updateMinMax() const;
+ void updateMinMax(int id) const
+ {
+ if (id > myMax) myMax = id;
+ if (id < myMin) myMin = id;
+ }
+
+ SMDS_IdElementMap myIDElements;
+ mutable int myMin, myMax;
};