DGtal  0.9.2
DigitalSetBySTLSet.ih
1 /**
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  **/
16 
17 /**
18  * @file DigitalSetBySTLSet.ih
19  * @author Jacques-Olivier Lachaud (\c jacques-olivier.lachaud@univ-savoie.fr )
20  * Laboratory of Mathematics (CNRS, UMR 5807), University of Savoie, France
21  *
22  * @author Sebastien Fourey (\c Sebastien.Fourey@greyc.ensicaen.fr )
23  * Groupe de Recherche en Informatique, Image, Automatique et
24  * Instrumentation de Caen - GREYC (CNRS, UMR 6072), ENSICAEN, France
25  *
26  * @date 2010/07/01
27  *
28  * Implementation of inline methods defined in DigitalSetBySTLSet.h
29  *
30  * This file is part of the DGtal library.
31  */
32 
33 
34 //////////////////////////////////////////////////////////////////////////////
35 #include <cstdlib>
36 //////////////////////////////////////////////////////////////////////////////
37 
38 ///////////////////////////////////////////////////////////////////////////////
39 // IMPLEMENTATION of inline methods.
40 ///////////////////////////////////////////////////////////////////////////////
41 
42 ///////////////////////////////////////////////////////////////////////////////
43 // ----------------------- Standard services ------------------------------
44 
45 /**
46  * Destructor.
47  */
48 template <typename Domain, typename Compare>
49 inline
50 DGtal::DigitalSetBySTLSet<Domain, Compare>::~DigitalSetBySTLSet()
51 {
52 }
53 
54 /**
55  * Constructor.
56  * Creates the empty set in the domain [d].
57  *
58  * @param d any counted pointer on domain.
59  */
60 template <typename Domain, typename Compare>
61 inline
62 DGtal::DigitalSetBySTLSet<Domain, Compare>::DigitalSetBySTLSet
63 ( Clone<Domain> d, const Compare & c )
64  : myDomain( d ), mySet( c )
65 {
66 }
67 
68 /**
69  * Copy constructor.
70  * @param other the object to clone.
71  */
72 template <typename Domain, typename Compare>
73 inline
74 DGtal::DigitalSetBySTLSet<Domain, Compare>::DigitalSetBySTLSet( const DigitalSetBySTLSet<Domain, Compare> & other )
75  : myDomain( other.myDomain ), mySet( other.mySet )
76 {
77 }
78 
79 /**
80  * Assignment.
81  * @param other the object to copy.
82  * @return a reference on 'this'.
83  */
84 template <typename Domain, typename Compare>
85 inline
86 DGtal::DigitalSetBySTLSet<Domain, Compare> &
87 DGtal::DigitalSetBySTLSet<Domain, Compare>::operator= ( const DigitalSetBySTLSet<Domain, Compare> & other )
88 {
89  ASSERT( ( domain().lowerBound() <= other.domain().lowerBound() )
90  && ( domain().upperBound() >= other.domain().upperBound() )
91  && "This domain should include the domain of the other set in case of assignment." );
92  mySet = other.mySet;
93  return *this;
94 }
95 
96 
97 /**
98  * @return the embedding domain.
99  */
100 template <typename Domain, typename Compare>
101 inline
102 const Domain &
103 DGtal::DigitalSetBySTLSet<Domain, Compare>::domain() const
104 {
105  return *myDomain;
106 }
107 
108 template <typename Domain, typename Compare>
109 inline
110 DGtal::CowPtr<Domain>
111 DGtal::DigitalSetBySTLSet<Domain, Compare>::domainPointer() const
112 {
113  return myDomain;
114 }
115 
116 
117 
118 ///////////////////////////////////////////////////////////////////////////////
119 // Interface - public :
120 
121 
122 /**
123  * @return the number of elements in the set.
124  */
125 template <typename Domain, typename Compare>
126 inline
127 typename DGtal::DigitalSetBySTLSet<Domain, Compare>::Size
128 DGtal::DigitalSetBySTLSet<Domain, Compare>::size() const
129 {
130  return (unsigned int)mySet.size();
131 }
132 
133 /**
134  * @return 'true' iff the set is empty (no element).
135  */
136 template <typename Domain, typename Compare>
137 inline
138 bool
139 DGtal::DigitalSetBySTLSet<Domain, Compare>::empty() const
140 {
141  return mySet.empty();
142 }
143 
144 
145 /**
146  * Adds point [p] to this set.
147  *
148  * @param p any digital point.
149  * @pre p should belong to the associated domain.
150  */
151 template <typename Domain, typename Compare>
152 inline
153 void
154 DGtal::DigitalSetBySTLSet<Domain, Compare>::insert( const Point & p )
155 {
156  // ASSERT( domain().isInside( p ) );
157  mySet.insert( p );
158 }
159 
160 
161 /**
162  * Adds the collection of points specified by the two iterators to
163  * this set.
164  *
165  * @param first the start point in the collection of Point.
166  * @param last the last point in the collection of Point.
167  * @pre all points should belong to the associated domain.
168  */
169 template <typename Domain, typename Compare>
170 template <typename PointInputIterator>
171 void
172 DGtal::DigitalSetBySTLSet<Domain, Compare>::insert( PointInputIterator first, PointInputIterator last )
173 {
174  mySet.insert( first, last );
175 }
176 
177 
178 /**
179  * Adds point [p] to this set if the point is not already in the
180  * set.
181  *
182  * @param p any digital point.
183  *
184  * @pre p should belong to the associated domain.
185  * @pre p should not belong to this.
186  */
187 template <typename Domain, typename Compare>
188 inline
189 void
190 DGtal::DigitalSetBySTLSet<Domain, Compare>::insertNew( const Point & p )
191 {
192  // ASSERT( domain().isInside( p ) );
193  mySet.insert( p );
194 }
195 
196 /**
197  * Adds the collection of points specified by the two iterators to
198  * this set.
199  *
200  * @param first the start point in the collection of Point.
201  * @param last the last point in the collection of Point.
202  *
203  * @pre all points should belong to the associated domain.
204  * @pre each point should not belong to this.
205  */
206 template <typename Domain, typename Compare>
207 template <typename PointInputIterator>
208 inline
209 void
210 DGtal::DigitalSetBySTLSet<Domain, Compare>::insertNew
211 ( PointInputIterator first, PointInputIterator last )
212 {
213  mySet.insert( first, last );
214 }
215 
216 /**
217  * Removes point [p] from the set.
218  *
219  * @param p the point to remove.
220  * @return the number of removed elements (0 or 1).
221  */
222 template <typename Domain, typename Compare>
223 typename DGtal::DigitalSetBySTLSet<Domain, Compare>::Size
224 DGtal::DigitalSetBySTLSet<Domain, Compare>::erase( const Point & p )
225 {
226  return (unsigned int)mySet.erase( p );
227 }
228 
229 /**
230  * Removes the point pointed by [it] from the set.
231  *
232  * @param it an iterator on this set.
233  * Note: generally faster than giving just the point.
234  */
235 template <typename Domain, typename Compare>
236 inline
237 void
238 DGtal::DigitalSetBySTLSet<Domain, Compare>::erase( Iterator it )
239 {
240  mySet.erase( it );
241 }
242 
243 /**
244  * Clears the set.
245  * @post this set is empty.
246  */
247 template <typename Domain, typename Compare>
248 inline
249 void
250 DGtal::DigitalSetBySTLSet<Domain, Compare>::clear()
251 {
252  mySet.clear();
253 }
254 
255 /**
256  * @param p any digital point.
257  * @return a const iterator pointing on [p] if found, otherwise end().
258  */
259 template <typename Domain, typename Compare>
260 inline
261 typename DGtal::DigitalSetBySTLSet<Domain, Compare>::ConstIterator
262 DGtal::DigitalSetBySTLSet<Domain, Compare>::find( const Point & p ) const
263 {
264  return mySet.find( p );
265 }
266 
267 /**
268  * @param p any digital point.
269  * @return an iterator pointing on [p] if found, otherwise end().
270  */
271 template <typename Domain, typename Compare>
272 inline
273 typename DGtal::DigitalSetBySTLSet<Domain, Compare>::Iterator
274 DGtal::DigitalSetBySTLSet<Domain, Compare>::find( const Point & p )
275 {
276  return mySet.find( p );
277 }
278 
279 /**
280  * @return a const iterator on the first element in this set.
281  */
282 template <typename Domain, typename Compare>
283 inline
284 typename DGtal::DigitalSetBySTLSet<Domain, Compare>::ConstIterator
285 DGtal::DigitalSetBySTLSet<Domain, Compare>::begin() const
286 {
287  return mySet.begin();
288 }
289 
290 /**
291  * @return a const iterator on the element after the last in this set.
292  */
293 template <typename Domain, typename Compare>
294 inline
295 typename DGtal::DigitalSetBySTLSet<Domain, Compare>::ConstIterator
296 DGtal::DigitalSetBySTLSet<Domain, Compare>::end() const
297 {
298  return mySet.end();
299 }
300 
301 /**
302  * @return an iterator on the first element in this set.
303  */
304 template <typename Domain, typename Compare>
305 inline
306 typename DGtal::DigitalSetBySTLSet<Domain, Compare>::Iterator
307 DGtal::DigitalSetBySTLSet<Domain, Compare>::begin()
308 {
309  return mySet.begin();
310 }
311 
312 /**
313  * @return a iterator on the element after the last in this set.
314  */
315 template <typename Domain, typename Compare>
316 inline
317 typename DGtal::DigitalSetBySTLSet<Domain, Compare>::Iterator
318 DGtal::DigitalSetBySTLSet<Domain, Compare>::end()
319 {
320  return mySet.end();
321 }
322 
323 /**
324  * set union to left.
325  * @param aSet any other set.
326  */
327 template <typename Domain, typename Compare>
328 inline
329 DGtal::DigitalSetBySTLSet<Domain, Compare> &
330 DGtal::DigitalSetBySTLSet<Domain, Compare>::operator+=( const DigitalSetBySTLSet<Domain, Compare> & aSet )
331 {
332  if ( this != &aSet )
333  {
334  Iterator it_dst = end();
335  for ( ConstIterator it_src = aSet.begin();
336  it_src != aSet.end();
337  ++it_src )
338  {
339  // Use hint it_dst to go faster.
340  it_dst = mySet.insert( it_dst, *it_src );
341  }
342  }
343  return *this;
344 }
345 
346 //-----------------------------------------------------------------------------
347 template <typename Domain, typename Compare>
348 inline
349 bool
350 DGtal::DigitalSetBySTLSet<Domain, Compare>
351 ::operator()( const Point & p ) const
352 {
353  return find( p ) != end();
354 }
355 
356 ///////////////////////////////////////////////////////////////////////////////
357 // ----------------------- Other Set services -----------------------------
358 
359 
360 template <typename Domain, typename Compare>
361 template <typename TOutputIterator>
362 inline
363 void
364 DGtal::DigitalSetBySTLSet<Domain, Compare>::computeComplement(TOutputIterator& ito) const
365 {
366  typename Domain::ConstIterator itPoint = domain().begin();
367  typename Domain::ConstIterator itEnd = domain().end();
368  while ( itPoint != itEnd ) {
369  if ( find( *itPoint ) == end() ) {
370  *ito++ = *itPoint;
371  }
372  ++itPoint;
373  }
374 }
375 
376 /**
377  * Builds the complement in the domain of the set [other_set] in
378  * this.
379  *
380  * @param other_set defines the set whose complement is assigned to 'this'.
381  */
382 template <typename Domain, typename Compare>
383 inline
384 void
385 DGtal::DigitalSetBySTLSet<Domain, Compare>::assignFromComplement
386 ( const DigitalSetBySTLSet<Domain, Compare> & other_set )
387 {
388  clear();
389  typename Domain::ConstIterator itPoint = domain().begin();
390  typename Domain::ConstIterator itEnd = domain().end();
391  while ( itPoint != itEnd ) {
392  if ( other_set.find( *itPoint ) == other_set.end() ) {
393  insert( *itPoint );
394  }
395  ++itPoint;
396  }
397 }
398 
399 /**
400  * Computes the bounding box of this set.
401  *
402  * @param lower the first point of the bounding box (lowest in all
403  * directions).
404  * @param upper the last point of the bounding box (highest in all
405  * directions).
406  */
407 template <typename Domain, typename Compare>
408 inline
409 void
410 DGtal::DigitalSetBySTLSet<Domain, Compare>::computeBoundingBox
411 ( Point & lower, Point & upper ) const
412 {
413  lower = domain().upperBound();
414  upper = domain().lowerBound();
415  ConstIterator it = begin();
416  ConstIterator itEnd = end();
417  while ( it != itEnd ) {
418  lower = lower.inf( *it );
419  upper = upper.sup( *it );
420  ++it;
421  }
422 }
423 
424 ///////////////////////////////////////////////////////////////////////////////
425 // Interface - public :
426 
427 /**
428  * Writes/Displays the object on an output stream.
429  * @param out the output stream where the object is written.
430  */
431 template <typename Domain, typename Compare>
432 inline
433 void
434 DGtal::DigitalSetBySTLSet<Domain, Compare>::selfDisplay ( std::ostream & out ) const
435 {
436  out << "[DigitalSetBySTLSet]" << " size=" << size();
437 }
438 
439 /**
440  * Checks the validity/consistency of the object.
441  * @return 'true' if the object is valid, 'false' otherwise.
442  */
443 template <typename Domain, typename Compare>
444 inline
445 bool
446 DGtal::DigitalSetBySTLSet<Domain, Compare>::isValid() const
447 {
448  return true;
449 }
450 
451 
452 // --------------- CDrawableWithBoard2D realization -------------------------
453 
454 /**
455  * Default drawing style object.
456  * @return the dyn. alloc. default style for this object.
457  */
458 
459 /**
460  * @return the style name used for drawing this object.
461  */
462 template<typename Domain, typename Compare>
463 inline
464 std::string
465 DGtal::DigitalSetBySTLSet<Domain, Compare>::className() const
466 {
467  return "DigitalSetBySTLSet";
468 }
469 
470 ///////////////////////////////////////////////////////////////////////////////
471 // Implementation of inline function //
472 
473 template <typename Domain, typename Compare>
474 inline
475 std::ostream &
476 DGtal::operator<< ( std::ostream & out, const DGtal::DigitalSetBySTLSet<Domain, Compare> & object )
477 {
478  object.selfDisplay( out );
479  return out;
480 }
481 
482 // //
483 ///////////////////////////////////////////////////////////////////////////////
484 
485