DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
DigitalSetBySTLVector.ih
1 
30 
31 #include <cstdlib>
32 #include <algorithm>
34 
36 // IMPLEMENTATION of inline methods.
38 
40 // ----------------------- Standard services ------------------------------
41 
45 template <typename Domain>
46 inline
48 {
49 }
50 
57 template <typename Domain>
58 inline
60 ( const Domain & d )
61  : myDomain( d ), myVector()
62 {
63 }
64 
69 template <typename Domain>
70 inline
72 ( const DigitalSetBySTLVector & other )
73  : myDomain( other.myDomain ), myVector( other.myVector )
74 {
75 }
76 
82 template <typename Domain>
83 inline
86 ( const DigitalSetBySTLVector & other )
87 {
88  ASSERT( ( myDomain.lowerBound() <= other.myDomain.lowerBound() )
89  && ( myDomain.upperBound() >= other.myDomain.upperBound() )
90  && "This domain should include the domain of the other set in case of assignment." );
91  myVector = other.myVector;
92  return *this;
93 }
94 
95 
99 template <typename Domain>
100 inline
101 const Domain &
103 {
104  return myDomain;
105 }
106 
107 
108 // ----------------------- Standard Set services --------------------------
109 
113 template <typename Domain>
114 inline
117 {
118  return (unsigned int)myVector.size();
119 }
120 
124 template <typename Domain>
125 inline
126 bool
128 {
129  return myVector.empty();
130 }
131 
138 template <typename Domain>
139 inline
140 void
142 {
143  // ASSERT( myDomain.isInside( p ) );
144  Iterator it = find( p );
145  if ( it == end() )
146  myVector.push_back( p );
147 }
148 
157 template <typename Domain>
158 template <typename PointInputIterator>
159 inline
160 void
162 ( PointInputIterator first, PointInputIterator last )
163 {
164  for ( ; first != last; ++first )
165  insert( *first );
166 }
167 
178 template <typename Domain>
179 inline
180 void
182 {
183  // ASSERT( myDomain.isInside( p ) );
184  ASSERT_ALL_PRE( find( p ) == end() );
185  myVector.push_back( p );
186 }
187 
202 template <typename Domain>
203 template <typename PointInputIterator>
204 inline
205 void
207 ( PointInputIterator first, PointInputIterator last )
208 {
209  while ( first != last )
210  myVector.push_back( *first++ );
211  // std::copy( first, last, myVector.end() );
212 }
213 
214 
221 template <typename Domain>
222 inline
225 {
226  Iterator it = find( p );
227  if ( it != end() )
228  {
229  erase( it );
230  return 1;
231  }
232  return 0;
233 }
234 
242 template <typename Domain>
243 inline
244 void
246 {
247  *it = myVector.back();
248  myVector.pop_back();
249 }
250 
258 template <typename Domain>
259 inline
260 void
262 {
263  while ( ( last != end() )
264  && ( first != last ) )
265  {
266  *first++ = myVector.back();
267  myVector.pop_back();
268  }
269  if ( first != last )
270  while ( first != end() )
271  myVector.pop_back();
272 }
273 
278 template <typename Domain>
279 inline
280 void
282 {
283  myVector.clear();
284 }
285 
290 template <typename Domain>
291 inline
294 {
295  const ConstIterator it_end = end();
296  for ( ConstIterator it = begin(); it != it_end; ++it )
297  if ( p == *it ) return it;
298  return it_end;
299 }
300 
305 template <typename Domain>
306 inline
309 {
310  const Iterator it_end = end();
311  for ( Iterator it = begin(); it != it_end; ++it )
312  if ( p == *it ) return it;
313  return it_end;
314 }
315 
316 
320 template <typename Domain>
321 inline
324 {
325  return myVector.begin();
326 }
327 
331 template <typename Domain>
332 inline
335 {
336  return myVector.end();
337 }
338 
339 
343 template <typename Domain>
344 inline
347 {
348  return myVector.begin();
349 }
350 
351 
355 template <typename Domain>
356 inline
359 {
360  return myVector.end();
361 }
362 
367 template <typename Domain>
368 inline
372 {
373  if ( this != &aSet )
374  {
375  std::vector<Point> other( aSet.myVector );
376  std::stable_sort( other.begin(), other.end() );
377  std::stable_sort( begin(), end() );
378  std::vector<Point> new_vector;
379  new_vector.reserve( size() + other.size() );
380  std::set_union( begin(), end(), other.begin(), other.end(),
381  std::back_insert_iterator< std::vector<Point> >
382  ( new_vector ) );
383  myVector.swap( new_vector );
384  }
385  return *this;
386 }
387 
388 
390 // ----------------------- Other Set services -----------------------------
391 
392 
393 template <typename Domain>
394 template <typename TOutputIterator>
395 inline
396 void
398 {
399  typename Domain::ConstIterator itPoint = myDomain.begin();
400  typename Domain::ConstIterator itEnd = myDomain.end();
401  while ( itPoint != itEnd ) {
402  if ( std::find( begin(), end(), *itPoint ) == end() ) {
403  *ito++ = *itPoint;
404  }
405  ++itPoint;
406  }
407 }
408 
415 template <typename Domain>
416 inline
417 void
419 ( const DigitalSetBySTLVector<Domain> & other_set )
420 {
421  clear();
422  typename Domain::ConstIterator itPoint = myDomain.begin();
423  typename Domain::ConstIterator itEnd = myDomain.end();
424  while ( itPoint != itEnd ) {
425  if ( std::find( other_set.begin(),
426  other_set.end(),
427  *itPoint ) == other_set.end() ) {
428  insert( *itPoint );
429  }
430  ++itPoint;
431  }
432 }
433 
442 template <typename Domain>
443 inline
444 void
446 ( Point & lower, Point & upper ) const
447 {
448  if ( begin() != end() )
449  {
450  ConstIterator it = begin();
451  ConstIterator it_end = end();
452  upper = lower = *it;
453  for ( ; it != it_end; ++it )
454  if ( it->isLower( lower ) ) lower = *it;
455  else if ( it->isUpper( upper ) ) upper = *it;
456  }
457  else
458  {
459  lower = myDomain.upperBound();
460  upper = myDomain.lowerBound();
461  }
462 }
463 
464 
466 // Interface - public :
467 
472 template <typename Domain>
473 inline
474 void
476 {
477  out << "[DigitalSetBySTLVector]" << " size=" << size();
478 }
479 
484 template <typename Domain>
485 inline
486 bool
488 {
489  return true;
490 }
491 
492 
493 
494 // --------------- CDrawableWithBoard2D realization -------------------------
495 
496 
500 template<typename Domain>
501 inline
502 std::string
504 {
505  return "DigitalSetBySTLVector";
506 }
507 
509 // Implementation of inline function //
510 
511 template <typename Domain>
512 inline
513 std::ostream&
514 DGtal::operator<< ( std::ostream & out,
515  const DigitalSetBySTLVector<Domain> & object )
516 {
517  object.selfDisplay( out );
518  return out;
519 }
520 
521 // //
523 
524