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 ------------------------------
52template <typename Domain, typename Container>
54DGtal::DigitalSetByAssociativeContainer<Domain, Container>::~DigitalSetByAssociativeContainer()
60 * Creates the empty set in the domain [d].
62 * @param d any counted pointer on domain.
64template <typename Domain, typename Container>
66DGtal::DigitalSetByAssociativeContainer<Domain, Container>::DigitalSetByAssociativeContainer
74 * @param other the object to clone.
76template <typename Domain, typename Container>
78DGtal::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'.
88template <typename Domain, typename Container>
90DGtal::DigitalSetByAssociativeContainer<Domain, Container> &
91DGtal::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.
104template <typename Domain, typename Container>
107DGtal::DigitalSetByAssociativeContainer<Domain, Container>::domain() const
112template <typename Domain, typename Container>
115DGtal::DigitalSetByAssociativeContainer<Domain, Container>::domainPointer() const
122///////////////////////////////////////////////////////////////////////////////
123// Interface - public :
127 * @return the number of elements in the set.
129template <typename Domain, typename Container>
131typename DGtal::DigitalSetByAssociativeContainer<Domain, Container>::Size
132DGtal::DigitalSetByAssociativeContainer<Domain, Container>::size() const
138 * @return 'true' iff the set is empty (no element).
140template <typename Domain, typename Container>
143DGtal::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.
155template <typename Domain, typename Container>
158DGtal::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.
173template <typename Domain, typename Container>
174template <typename PointInputIterator>
176DGtal::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.
192template <typename Domain, typename Container>
195DGtal::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.
211template <typename Domain, typename Container>
212template <typename PointInputIterator>
215DGtal::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).
227template <typename Domain, typename Container>
228typename DGtal::DigitalSetByAssociativeContainer<Domain, Container>::Size
229DGtal::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.
240template <typename Domain, typename Container>
243DGtal::DigitalSetByAssociativeContainer<Domain, Container>::erase( Iterator it )
250 * @post this set is empty.
252template <typename Domain, typename Container>
255DGtal::DigitalSetByAssociativeContainer<Domain, Container>::clear()
261 * @param p any digital point.
262 * @return a const iterator pointing on [p] if found, otherwise end().
264template <typename Domain, typename Container>
266typename DGtal::DigitalSetByAssociativeContainer<Domain, Container>::ConstIterator
267DGtal::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().
276template <typename Domain, typename Container>
278typename DGtal::DigitalSetByAssociativeContainer<Domain, Container>::Iterator
279DGtal::DigitalSetByAssociativeContainer<Domain, Container>::find( const Point & p )
281 return mySet.find( p );
285 * @return a const iterator on the first element in this set.
287template <typename Domain, typename Container>
289typename DGtal::DigitalSetByAssociativeContainer<Domain, Container>::ConstIterator
290DGtal::DigitalSetByAssociativeContainer<Domain, Container>::begin() const
292 return mySet.begin();
296 * @return a const iterator on the element after the last in this set.
298template <typename Domain, typename Container>
300typename DGtal::DigitalSetByAssociativeContainer<Domain, Container>::ConstIterator
301DGtal::DigitalSetByAssociativeContainer<Domain, Container>::end() const
307 * @return an iterator on the first element in this set.
309template <typename Domain, typename Container>
311typename DGtal::DigitalSetByAssociativeContainer<Domain, Container>::Iterator
312DGtal::DigitalSetByAssociativeContainer<Domain, Container>::begin()
314 return mySet.begin();
318 * @return a iterator on the element after the last in this set.
320template <typename Domain, typename Container>
322typename DGtal::DigitalSetByAssociativeContainer<Domain, Container>::Iterator
323DGtal::DigitalSetByAssociativeContainer<Domain, Container>::end()
328template <typename Domain, typename Container>
330const typename DGtal::DigitalSetByAssociativeContainer<Domain, Container>::Container &
331DGtal::DigitalSetByAssociativeContainer<Domain, Container>::container() const
336template <typename Domain, typename Container>
338typename DGtal::DigitalSetByAssociativeContainer<Domain, Container>::Container &
339DGtal::DigitalSetByAssociativeContainer<Domain, Container>::container()
346 * @param aSet any other set.
348template <typename Domain, typename Container>
350DGtal::DigitalSetByAssociativeContainer<Domain, Container> &
351DGtal::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//-----------------------------------------------------------------------------
368template <typename Domain, typename Container>
371DGtal::DigitalSetByAssociativeContainer<Domain, Container>
372::operator()( const Point & p ) const
374 return find( p ) != end();
377///////////////////////////////////////////////////////////////////////////////
378// ----------------------- Other Set services -----------------------------
381template <typename Domain, typename Container>
382template <typename TOutputIterator>
385DGtal::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'.
405template <typename Domain, typename Container>
408DGtal::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
432template <typename Domain, typename Container>
435DGtal::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.
457template <typename Domain, typename Container>
460DGtal::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.
469template <typename Domain, typename Container>
472DGtal::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.
488template<typename Domain, typename Container>
491DGtal::DigitalSetByAssociativeContainer<Domain, Container>::className() const
493 return "DigitalSetByAssociativeContainer";
496///////////////////////////////////////////////////////////////////////////////
497// Implementation of inline function //
499template <typename Domain, typename Container>
502DGtal::operator<< ( std::ostream & out, const DGtal::DigitalSetByAssociativeContainer<Domain, Container> & object )
504 object.selfDisplay( out );
509///////////////////////////////////////////////////////////////////////////////