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.
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.
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/>.
18 * @file DigitalSetByAssociativeContainer.ih
19 * @author Jacques-Olivier Lachaud (\c jacques-olivier.lachaud@univ-savoie.fr )
20 * Laboratory of Mathematics (CNRS, UMR 5807), University of Savoie, France
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
26 * @author David Coeurjolly (\c david.coeurjolly@liris.cnrs.fr )
27 * Laboratoire LIRIS (CNRS, UMR 5205), Université de Lyon, France
32 * Implementation of inline methods defined in DigitalSetByAssociativeContainer.h
34 * This file is part of the DGtal library.
38 //////////////////////////////////////////////////////////////////////////////
40 //////////////////////////////////////////////////////////////////////////////
42 ///////////////////////////////////////////////////////////////////////////////
43 // IMPLEMENTATION of inline methods.
44 ///////////////////////////////////////////////////////////////////////////////
46 ///////////////////////////////////////////////////////////////////////////////
47 // ----------------------- Standard services ------------------------------
52 template <typename Domain, typename Container>
54 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::~DigitalSetByAssociativeContainer()
60 * Creates the empty set in the domain [d].
62 * @param d any counted pointer on domain.
64 template <typename Domain, typename Container>
66 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::DigitalSetByAssociativeContainer
74 * @param other the object to clone.
76 template <typename Domain, typename Container>
78 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::DigitalSetByAssociativeContainer( const DigitalSetByAssociativeContainer<Domain, Container> & other )
79 : myDomain( other.myDomain ), mySet( other.mySet )
85 * @param other the object to copy.
86 * @return a reference on 'this'.
88 template <typename Domain, typename Container>
90 DGtal::DigitalSetByAssociativeContainer<Domain, Container> &
91 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::operator= ( const DigitalSetByAssociativeContainer<Domain, Container> & other )
93 ASSERT( ( domain().lowerBound() <= other.domain().lowerBound() )
94 && ( domain().upperBound() >= other.domain().upperBound() )
95 && "This domain should include the domain of the other set in case of assignment." );
102 * @return the embedding domain.
104 template <typename Domain, typename Container>
107 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::domain() const
112 template <typename Domain, typename Container>
114 DGtal::CowPtr<Domain>
115 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::domainPointer() const
122 ///////////////////////////////////////////////////////////////////////////////
123 // Interface - public :
127 * @return the number of elements in the set.
129 template <typename Domain, typename Container>
131 typename DGtal::DigitalSetByAssociativeContainer<Domain, Container>::Size
132 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::size() const
138 * @return 'true' iff the set is empty (no element).
140 template <typename Domain, typename Container>
143 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::empty() const
145 return mySet.empty();
150 * Adds point [p] to this set.
152 * @param p any digital point.
153 * @pre p should belong to the associated domain.
155 template <typename Domain, typename Container>
158 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::insert( const Point & p )
160 ASSERT( domain().isInside( p ) );
166 * Adds the collection of points specified by the two iterators to
169 * @param first the start point in the collection of Point.
170 * @param last the last point in the collection of Point.
171 * @pre all points should belong to the associated domain.
173 template <typename Domain, typename Container>
174 template <typename PointInputIterator>
176 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::insert( PointInputIterator first,
177 PointInputIterator last )
179 mySet.insert( first, last );
184 * Adds point [p] to this set if the point is not already in the
187 * @param p any digital point.
189 * @pre p should belong to the associated domain.
190 * @pre p should not belong to this.
192 template <typename Domain, typename Container>
195 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::insertNew( const Point & p )
197 ASSERT( domain().isInside( p ) );
202 * Adds the collection of points specified by the two iterators to
205 * @param first the start point in the collection of Point.
206 * @param last the last point in the collection of Point.
208 * @pre all points should belong to the associated domain.
209 * @pre each point should not belong to this.
211 template <typename Domain, typename Container>
212 template <typename PointInputIterator>
215 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::insertNew
216 ( PointInputIterator first, PointInputIterator last )
218 mySet.insert( first, last );
222 * Removes point [p] from the set.
224 * @param p the point to remove.
225 * @return the number of removed elements (0 or 1).
227 template <typename Domain, typename Container>
228 typename DGtal::DigitalSetByAssociativeContainer<Domain, Container>::Size
229 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::erase( const Point & p )
231 return mySet.erase( p );
235 * Removes the point pointed by [it] from the set.
237 * @param it an iterator on this set.
238 * Note: generally faster than giving just the point.
240 template <typename Domain, typename Container>
243 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::erase( Iterator it )
250 * @post this set is empty.
252 template <typename Domain, typename Container>
255 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::clear()
261 * @param p any digital point.
262 * @return a const iterator pointing on [p] if found, otherwise end().
264 template <typename Domain, typename Container>
266 typename DGtal::DigitalSetByAssociativeContainer<Domain, Container>::ConstIterator
267 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::find( const Point & p ) const
269 return mySet.find( p );
273 * @param p any digital point.
274 * @return an iterator pointing on [p] if found, otherwise end().
276 template <typename Domain, typename Container>
278 typename DGtal::DigitalSetByAssociativeContainer<Domain, Container>::Iterator
279 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::find( const Point & p )
281 return mySet.find( p );
285 * @return a const iterator on the first element in this set.
287 template <typename Domain, typename Container>
289 typename DGtal::DigitalSetByAssociativeContainer<Domain, Container>::ConstIterator
290 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::begin() const
292 return mySet.begin();
296 * @return a const iterator on the element after the last in this set.
298 template <typename Domain, typename Container>
300 typename DGtal::DigitalSetByAssociativeContainer<Domain, Container>::ConstIterator
301 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::end() const
307 * @return an iterator on the first element in this set.
309 template <typename Domain, typename Container>
311 typename DGtal::DigitalSetByAssociativeContainer<Domain, Container>::Iterator
312 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::begin()
314 return mySet.begin();
318 * @return a iterator on the element after the last in this set.
320 template <typename Domain, typename Container>
322 typename DGtal::DigitalSetByAssociativeContainer<Domain, Container>::Iterator
323 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::end()
328 template <typename Domain, typename Container>
330 const typename DGtal::DigitalSetByAssociativeContainer<Domain, Container>::Container &
331 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::container() const
336 template <typename Domain, typename Container>
338 typename DGtal::DigitalSetByAssociativeContainer<Domain, Container>::Container &
339 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::container()
346 * @param aSet any other set.
348 template <typename Domain, typename Container>
350 DGtal::DigitalSetByAssociativeContainer<Domain, Container> &
351 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::operator+=( const DigitalSetByAssociativeContainer<Domain, Container> & aSet )
355 Iterator it_dst = end();
356 for ( ConstIterator it_src = aSet.begin();
357 it_src != aSet.end();
360 // Use hint it_dst to go faster.
361 it_dst = mySet.insert( it_dst, *it_src );
367 //-----------------------------------------------------------------------------
368 template <typename Domain, typename Container>
371 DGtal::DigitalSetByAssociativeContainer<Domain, Container>
372 ::operator()( const Point & p ) const
374 return find( p ) != end();
377 ///////////////////////////////////////////////////////////////////////////////
378 // ----------------------- Other Set services -----------------------------
381 template <typename Domain, typename Container>
382 template <typename TOutputIterator>
385 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::computeComplement(TOutputIterator& ito) const
387 typename Domain::ConstIterator itPoint = domain().begin();
388 typename Domain::ConstIterator itEnd = domain().end();
389 while ( itPoint != itEnd )
391 if ( find( *itPoint ) == end() )
400 * Builds the complement in the domain of the set [other_set] in
403 * @param other_set defines the set whose complement is assigned to 'this'.
405 template <typename Domain, typename Container>
408 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::assignFromComplement
409 ( const DigitalSetByAssociativeContainer<Domain, Container> & other_set )
412 typename Domain::ConstIterator itPoint = domain().begin();
413 typename Domain::ConstIterator itEnd = domain().end();
414 while ( itPoint != itEnd )
416 if ( other_set.find( *itPoint ) == other_set.end() )
425 * Computes the bounding box of this set.
427 * @param lower the first point of the bounding box (lowest in all
429 * @param upper the last point of the bounding box (highest in all
432 template <typename Domain, typename Container>
435 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::computeBoundingBox
436 ( Point & lower, Point & upper ) const
438 lower = domain().upperBound();
439 upper = domain().lowerBound();
440 ConstIterator it = begin();
441 ConstIterator itEnd = end();
442 while ( it != itEnd )
444 lower = lower.inf( *it );
445 upper = upper.sup( *it );
450 ///////////////////////////////////////////////////////////////////////////////
451 // Interface - public :
454 * Writes/Displays the object on an output stream.
455 * @param out the output stream where the object is written.
457 template <typename Domain, typename Container>
460 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::selfDisplay ( std::ostream & out ) const
462 out << "[DigitalSetByAssociativeContainer]" << " size=" << size();
466 * Checks the validity/consistency of the object.
467 * @return 'true' if the object is valid, 'false' otherwise.
469 template <typename Domain, typename Container>
472 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::isValid() const
478 // --------------- CDrawableWithBoard2D realization -------------------------
481 * Default drawing style object.
482 * @return the dyn. alloc. default style for this object.
486 * @return the style name used for drawing this object.
488 template<typename Domain, typename Container>
491 DGtal::DigitalSetByAssociativeContainer<Domain, Container>::className() const
493 return "DigitalSetByAssociativeContainer";
496 ///////////////////////////////////////////////////////////////////////////////
497 // Implementation of inline function //
499 template <typename Domain, typename Container>
502 DGtal::operator<< ( std::ostream & out, const DGtal::DigitalSetByAssociativeContainer<Domain, Container> & object )
504 object.selfDisplay( out );
509 ///////////////////////////////////////////////////////////////////////////////