Salome HOME
Update copyrights
[modules/shaper.git] / src / GeomAPI / GeomAPI_DataMapOfShapeMapOfShapes.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <GeomAPI_DataMapOfShapeMapOfShapes.h>
21
22 #include <NCollection_DataMap.hxx>
23 #include <NCollection_Map.hxx>
24 #include <TopoDS_Shape.hxx>
25
26 typedef NCollection_DataMap<TopoDS_Shape, NCollection_Map<TopoDS_Shape> > MAP;
27 #define MY_MAP implPtr<MAP>()
28
29 //=================================================================================================
30 GeomAPI_DataMapOfShapeMapOfShapes::GeomAPI_DataMapOfShapeMapOfShapes()
31 : GeomAPI_Interface(new NCollection_DataMap<TopoDS_Shape, NCollection_Map<TopoDS_Shape> >)
32 {}
33
34 //=================================================================================================
35 bool GeomAPI_DataMapOfShapeMapOfShapes::bind(const std::shared_ptr<GeomAPI_Shape> theKey,
36                                              const ListOfShape& theItems)
37 {
38   const TopoDS_Shape& aKey = theKey->impl<TopoDS_Shape>();
39   if(MY_MAP->IsBound(aKey)) {
40     MY_MAP->ChangeFind(aKey).Clear();
41   }
42   for(ListOfShape::const_iterator anIt = theItems.cbegin(); anIt != theItems.cend(); anIt++) {
43     const TopoDS_Shape& anItem = (*anIt)->impl<TopoDS_Shape>();
44     if(MY_MAP->IsBound(aKey)) {
45       MY_MAP->ChangeFind(aKey).Add(anItem);
46     } else {
47       NCollection_Map<TopoDS_Shape> anItems;
48       anItems.Add(anItem);
49       MY_MAP->Bind(aKey, anItems);
50     }
51   }
52
53   return true;
54 }
55
56 //=================================================================================================
57 bool GeomAPI_DataMapOfShapeMapOfShapes::add(const std::shared_ptr<GeomAPI_Shape> theKey,
58                                             const std::shared_ptr<GeomAPI_Shape> theItem)
59 {
60   const TopoDS_Shape& aKey = theKey->impl<TopoDS_Shape>();
61   const TopoDS_Shape& anItem = theItem->impl<TopoDS_Shape>();
62   if(MY_MAP->IsBound(aKey)) {
63     return MY_MAP->ChangeFind(aKey).Add(anItem) == Standard_True;
64   } else {
65     NCollection_Map<TopoDS_Shape> anItems;
66     anItems.Add(anItem);
67     return MY_MAP->Bind(aKey, anItems) == Standard_True;
68   }
69 }
70
71 //=================================================================================================
72 bool GeomAPI_DataMapOfShapeMapOfShapes::isBound(const std::shared_ptr<GeomAPI_Shape> theKey) const
73 {
74   const TopoDS_Shape& aKey = theKey->impl<TopoDS_Shape>();
75   return MY_MAP->IsBound(aKey) == Standard_True;
76 }
77
78 //=================================================================================================
79 bool GeomAPI_DataMapOfShapeMapOfShapes::find(const std::shared_ptr<GeomAPI_Shape> theKey,
80                                              ListOfShape& theItems) const
81 {
82   const TopoDS_Shape& aKey = theKey->impl<TopoDS_Shape>();
83
84   if(MY_MAP->IsBound(aKey) == Standard_False) {
85     return false;
86   }
87
88   const NCollection_Map<TopoDS_Shape>& aMap = MY_MAP->Find(aKey);
89   for(NCollection_Map<TopoDS_Shape>::Iterator anIt(aMap); anIt.More(); anIt.Next()) {
90     std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
91     aShape->setImpl(new TopoDS_Shape(anIt.Value()));
92     theItems.push_back(aShape);
93   }
94
95   return true;
96 }
97
98 //=================================================================================================
99 bool GeomAPI_DataMapOfShapeMapOfShapes::unBind(const std::shared_ptr<GeomAPI_Shape> theKey)
100 {
101   const TopoDS_Shape& aKey = theKey->impl<TopoDS_Shape>();
102   return MY_MAP->UnBind(aKey) == Standard_True;
103 }
104
105 //=================================================================================================
106 void GeomAPI_DataMapOfShapeMapOfShapes::clear()
107 {
108   return MY_MAP->Clear();
109 }
110
111 //=================================================================================================
112 int GeomAPI_DataMapOfShapeMapOfShapes::size() const
113 {
114   return MY_MAP->Size();
115 }
116
117
118 //=================================================================================================
119 //   iterator implementation
120 //=================================================================================================
121
122 class IteratorImpl : public GeomAPI_DataMapOfShapeMapOfShapes::iterator
123 {
124 public:
125   IteratorImpl() {}
126
127   IteratorImpl(const MAP::Iterator& theIterator)
128     : myIterator(theIterator)
129   {}
130
131   IteratorImpl(const std::shared_ptr<IteratorImpl>& theIterator)
132   {
133     mySelf = theIterator;
134   }
135
136   bool operator==(const std::shared_ptr<IteratorImpl>& theOther) const
137   {
138     if (theOther)
139       return theOther->myIterator.IsEqual(myIterator);
140
141     // theOther is end iterator => check the current iterator is not at the end
142     return !myIterator.More();
143   }
144
145   virtual iterator& operator++()
146   {
147     myIterator.Next();
148     return *this;
149   }
150   virtual iterator operator++(int)
151   {
152     std::shared_ptr<IteratorImpl> aSelf = std::dynamic_pointer_cast<IteratorImpl>(mySelf);
153     std::shared_ptr<IteratorImpl> aCopy(new IteratorImpl(aSelf->myIterator));
154     myIterator.Next();
155     return IteratorImpl(aCopy);
156   }
157
158   virtual key_type first() const
159   {
160     if (mySelf)
161       return iterator::first();
162
163     GeomShapePtr aShape(new GeomAPI_Shape);
164     aShape->setImpl(new TopoDS_Shape(myIterator.Key()));
165     return aShape;
166   }
167
168   virtual mapped_type second() const
169   {
170     if (mySelf)
171       return iterator::second();
172
173     ListOfShape anItems;
174     const NCollection_Map<TopoDS_Shape>& aMap = myIterator.Value();
175     for(NCollection_Map<TopoDS_Shape>::Iterator anIt(aMap); anIt.More(); anIt.Next()) {
176       std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
177       aShape->setImpl(new TopoDS_Shape(anIt.Value()));
178       anItems.push_back(aShape);
179     }
180     return anItems;
181   }
182
183 private:
184   MAP::Iterator myIterator;
185 };
186
187
188 GeomAPI_DataMapOfShapeMapOfShapes::iterator::iterator()
189 {
190 }
191
192 GeomAPI_DataMapOfShapeMapOfShapes::iterator::iterator(
193     const GeomAPI_DataMapOfShapeMapOfShapes::iterator& theOther)
194   : mySelf(theOther.mySelf)
195 {
196 }
197
198 const GeomAPI_DataMapOfShapeMapOfShapes::iterator&
199     GeomAPI_DataMapOfShapeMapOfShapes::iterator::operator=(
200       const GeomAPI_DataMapOfShapeMapOfShapes::iterator& theOther)
201 {
202   mySelf = theOther.mySelf;
203   return *this;
204 }
205
206 bool GeomAPI_DataMapOfShapeMapOfShapes::iterator::operator==(const iterator& theOther) const
207 {
208   std::shared_ptr<IteratorImpl> aSelf = std::dynamic_pointer_cast<IteratorImpl>(mySelf);
209   std::shared_ptr<IteratorImpl> aSelfOther =
210       std::dynamic_pointer_cast<IteratorImpl>(theOther.mySelf);
211
212   return aSelf ? aSelf->operator==(aSelfOther)
213                 : (aSelfOther ? aSelfOther->operator==(aSelf) : true);
214 }
215
216 bool GeomAPI_DataMapOfShapeMapOfShapes::iterator::operator!=(const iterator& theOther) const
217 {
218   return !operator==(theOther);
219 }
220
221 GeomAPI_DataMapOfShapeMapOfShapes::iterator&
222     GeomAPI_DataMapOfShapeMapOfShapes::iterator::operator++()
223 {
224   mySelf->operator++();
225   return *this;
226 }
227
228 GeomAPI_DataMapOfShapeMapOfShapes::iterator
229     GeomAPI_DataMapOfShapeMapOfShapes::iterator::operator++(int)
230 {
231   return ++(*mySelf);
232 }
233
234 GeomAPI_DataMapOfShapeMapOfShapes::iterator::key_type
235     GeomAPI_DataMapOfShapeMapOfShapes::iterator::first() const
236 {
237   return mySelf->first();
238 }
239
240 GeomAPI_DataMapOfShapeMapOfShapes::iterator::mapped_type
241     GeomAPI_DataMapOfShapeMapOfShapes::iterator::second() const
242 {
243   return mySelf->second();
244 }
245
246
247
248 GeomAPI_DataMapOfShapeMapOfShapes::iterator GeomAPI_DataMapOfShapeMapOfShapes::begin()
249 {
250   MAP::Iterator anIt(*MY_MAP);
251   std::shared_ptr<IteratorImpl> anIter(new IteratorImpl(anIt));
252   return IteratorImpl(anIter);
253 }
254
255 GeomAPI_DataMapOfShapeMapOfShapes::const_iterator GeomAPI_DataMapOfShapeMapOfShapes::begin() const
256 {
257   MAP::Iterator anIt(*MY_MAP);
258   std::shared_ptr<IteratorImpl> anIter(new IteratorImpl(anIt));
259   return IteratorImpl(anIter);
260 }
261
262 GeomAPI_DataMapOfShapeMapOfShapes::iterator GeomAPI_DataMapOfShapeMapOfShapes::end()
263 {
264   return IteratorImpl(std::shared_ptr<IteratorImpl>());
265 }
266
267 GeomAPI_DataMapOfShapeMapOfShapes::const_iterator GeomAPI_DataMapOfShapeMapOfShapes::end() const
268 {
269   return IteratorImpl(std::shared_ptr<IteratorImpl>());
270 }