Salome HOME
Correct some memory leaks
[modules/smesh.git] / src / SMDS / SMDS_IteratorOfElements.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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21
22 #include "SMDS_IteratorOfElements.hxx"
23
24 bool SMDS_IteratorOfElements::subMore()
25 {
26         if((t2Iterator==NULL)||(!t2Iterator->more()))
27         {
28                 if(t1Iterator->more())
29                 {
30                         if(t2Iterator!=NULL) delete t2Iterator;
31                         t2Iterator=t1Iterator->next()->elementsIterator(myType);
32                         return subMore();
33                 }
34                 else return false;
35         }
36         else return true;
37 }
38
39 const SMDS_MeshElement * SMDS_IteratorOfElements::subNext()
40 {
41         if((t2Iterator==NULL)||(!t2Iterator->more()))
42         {
43                 if(t1Iterator->more())
44                 {
45                         if(t2Iterator!=NULL) delete t2Iterator;
46                         t2Iterator=t1Iterator->next()->elementsIterator(myType);
47                 }
48         }
49         return t2Iterator->next();
50 }
51
52 /////////////////////////////////////////////////////////////////////////////
53 /// Create an iterator which look for elements of type type which are linked 
54 /// to the element element. it is the iterator to get connectivity of element
55 //////////////////////////////////////////////////////////////////////////////
56 SMDS_IteratorOfElements::SMDS_IteratorOfElements(const SMDS_MeshElement * element,
57         SMDSAbs_ElementType type, SMDS_Iterator<const SMDS_MeshElement *>* it)
58         :t1Iterator(it), t2Iterator(NULL), myType(type), myElement(element),
59         myProxyElement(NULL)
60 {
61         while(subMore())
62                 alreadyReturnedElements.insert(subNext());
63         itAlreadyReturned= alreadyReturnedElements.begin();
64         switch(myElement->GetType())
65         {
66         case SMDSAbs_Node: 
67         case SMDSAbs_Edge: myReverseIteration=true; break;
68         case SMDSAbs_Face: myReverseIteration=(type==SMDSAbs_Volume); break;
69         default: myReverseIteration=false;
70         }       
71 }
72
73 bool SMDS_IteratorOfElements::more()
74 {
75         if(myProxyElement==NULL)
76         {
77                 while(itAlreadyReturned!=alreadyReturnedElements.end())
78                 {
79                         myProxyElement=*itAlreadyReturned;
80                         itAlreadyReturned++;    
81
82                         if(myReverseIteration)
83                         {
84                                 SMDS_Iterator<const SMDS_MeshElement*> * it=
85                                         myProxyElement->elementsIterator(myElement->GetType());
86                                 while(it->more())
87                                 {                               
88                                         if(it->next()==myElement) return true;
89                                 }
90                         }
91                         else return true;
92                 }
93                 myProxyElement=NULL;
94                 return false;
95         }
96         else return true;
97 }
98
99 const SMDS_MeshElement * SMDS_IteratorOfElements::next()
100 {
101         more();
102         const SMDS_MeshElement *e=myProxyElement;
103         myProxyElement=NULL;
104         return e;
105 }
106
107 SMDS_IteratorOfElements::~SMDS_IteratorOfElements()
108 {
109         delete t1Iterator;
110         if(t2Iterator!=NULL) delete t2Iterator;
111 }