DGtal  1.1.0
ConstRangeAdapter.h
1 
17 #pragma once
18 
33 #if defined(ConstRangeAdapter_RECURSES)
34 #error Recursive header files inclusion detected in ConstRangeAdapter.h
35 #else // defined(ConstRangeAdapter_RECURSES)
36 
37 #define ConstRangeAdapter_RECURSES
38 
39 #if !defined ConstRangeAdapter_h
40 
41 #define ConstRangeAdapter_h
42 
44 // Inclusions
45 #include "DGtal/base/BasicFunctors.h"
46 //#include "boost/iterator/reverse_iterator.hpp"
47 #include "DGtal/base/ReverseIterator.h"
48 #include "DGtal/base/Circulator.h"
49 #include "DGtal/base/ConstIteratorAdapter.h"
50 #include "boost/concept_check.hpp"
52 
53 namespace DGtal
54 {
55 
56 
58  // class ConstRangeAdapter
60 
85  template <typename TIterator, typename TFunctor, typename TReturnType>
87  {
88 
91 
92  // ------------------------- inner types --------------------------------
93  public:
94 
97 
100 
102  // ------------------------- standard services --------------------------------
103 
112  ConstRangeAdapter(const TIterator& itb, const TIterator& ite,
113  const TFunctor& aFunctor )
114  : myBegin(itb), myEnd(ite), myFunctor(&aFunctor), myFlagIsOwned(false) {}
115 
124  ConstRangeAdapter(const TIterator& itb, const TIterator& ite,
125  const TFunctor* aFunctorPtr )
126  : myBegin(itb), myEnd(ite), myFunctor(aFunctorPtr), myFlagIsOwned(true) {}
127 
133  : myBegin(other.myBegin), myEnd(other.myEnd), myFlagIsOwned(other.myFlagIsOwned)
134  {
135  if (myFlagIsOwned)
136  myFunctor = new TFunctor( *other.myFunctor ); //owned copy
137  else
138  myFunctor = other.myFunctor; //copy of the alias
139  }
140 
145  {
146  if (myFlagIsOwned) delete myFunctor;
147  }
148 
153  bool isValid() const { return true; }
154 
158  Difference size() const
159  {
160  typedef typename IteratorCirculatorTraits<TIterator>::Category Category;
161  return size( myBegin, myEnd, Category() );
162  }
163 
164  // ------------------------- display --------------------------------
169  void selfDisplay ( std::ostream & out ) const
170  {
171  out << "[ConstRangeAdapter]" << std::endl;
172  out << "\t";
173  std::copy( this->begin(), this->end(), std::ostream_iterator<TReturnType>(out, ", ") );
174  out << std::endl;
175  }
176 
180  std::string className() const
181  {
182  return "ConstRangeAdapter";
183  }
184 
185 
186  // ------------------------- private data --------------------------------
187  private:
191  TIterator myBegin;
195  TIterator myEnd;
199  const TFunctor* myFunctor;
204 
205  // ------------------------- private methods --------------------------------
206  private:
207 
214 
215  // ------------------------- iterator services --------------------------------
216  public:
217 
223  return ConstIterator( myBegin, *myFunctor );
224  }
225 
230  ConstIterator end() const {
231  return ConstIterator( myEnd, *myFunctor );
232  }
233 
239  return ConstReverseIterator(this->end());
240  }
241 
246  ConstReverseIterator rend() const {
247  return ConstReverseIterator(this->begin());
248  }
249 
257  ConstCirculator c() const {
258  return ConstCirculator( this->begin(), this->begin(), this->end() );
259  }
260 
269  ConstReverseCirculator rc() const {
270  //implemented so that *rc() == *c()
271  ConstCirculator tmp = this->c();
272  ++tmp;
273  return ConstReverseCirculator( tmp );
274  }
275 
276  private:
277 
285  Difference size(const TIterator& itb, const TIterator& ite, RandomAccessCategory) const
286  {
287  return (ite-itb);
288  }
289 
297  Difference size(const TIterator& itb, const TIterator& ite, BidirectionalCategory) const
298  {
299  TIterator it = itb;
300  unsigned int d = 0;
301  for ( ; it != ite; ++it, ++d)
302  {}
303  return d;
304  }
305 
306  }; //end class ConstRangeAdapter
307 
314  template <typename TIterator, typename TFunctor, typename TReturnType>
315  std::ostream&
316  operator<< ( std::ostream & out, const ConstRangeAdapter<TIterator, TFunctor, TReturnType> & object )
317  {
318  object.selfDisplay( out );
319  return out;
320  }
321 
328  template <typename A, typename B>
329  std::ostream&
330  operator<< ( std::ostream & out, const std::pair<A, B> & object )
331  {
332  out << object.first << "|" << object.second;
333  return out;
334  }
335 } // namespace DGtal
336 
338 
339 
340 #endif // !defined ConstRangeAdapter_h
341 
342 #undef ConstRangeAdapter_RECURSES
343 #endif // else defined(ConstRangeAdapter_RECURSES)
DGtal::ConstRangeAdapter::size
Difference size() const
Definition: ConstRangeAdapter.h:158
DGtal::RandomAccessCategory
Definition: IteratorCirculatorTraits.h:59
DGtal::Circulator
Aim: Provides an adapter for classical iterators that can iterate through the underlying data structu...
Definition: Circulator.h:86
DGtal::ConstRangeAdapter
Aim: model of CConstBidirectionalRange that adapts any range of elements bounded by two iterators [it...
Definition: ConstRangeAdapter.h:87
DGtal::ConstRangeAdapter::size
Difference size(const TIterator &itb, const TIterator &ite, BidirectionalCategory) const
Definition: ConstRangeAdapter.h:297
DGtal::ConstRangeAdapter::begin
ConstIterator begin() const
Definition: ConstRangeAdapter.h:222
DGtal::ConstRangeAdapter::BOOST_CONCEPT_ASSERT
BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIteratorConcept< TIterator >))
DGtal::ReverseIterator
This class adapts any bidirectional iterator so that operator++ calls operator-- and vice versa.
Definition: ReverseIterator.h:70
DGtal::ConstRangeAdapter::myFunctor
const TFunctor * myFunctor
Definition: ConstRangeAdapter.h:199
DGtal::ConstRangeAdapter::ConstReverseIterator
ReverseIterator< ConstIterator > ConstReverseIterator
Definition: ConstRangeAdapter.h:96
DGtal::ConstRangeAdapter::myBegin
TIterator myBegin
Definition: ConstRangeAdapter.h:191
DGtal::ConstRangeAdapter::className
std::string className() const
Definition: ConstRangeAdapter.h:180
DGtal::ConstRangeAdapter::ConstIterator
ConstIteratorAdapter< TIterator, TFunctor, TReturnType > ConstIterator
Definition: ConstRangeAdapter.h:95
DGtal::ConstRangeAdapter::ConstRangeAdapter
ConstRangeAdapter(const TIterator &itb, const TIterator &ite, const TFunctor *aFunctorPtr)
Definition: ConstRangeAdapter.h:124
DGtal::ConstRangeAdapter::end
ConstIterator end() const
Definition: ConstRangeAdapter.h:230
DGtal::ConstRangeAdapter::isValid
bool isValid() const
Definition: ConstRangeAdapter.h:153
DGtal::ConstRangeAdapter::Difference
IteratorCirculatorTraits< ConstIterator >::Difference Difference
Definition: ConstRangeAdapter.h:101
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
Definition: ClosedIntegerHalfPlane.h:49
DGtal::ConstRangeAdapter::c
ConstCirculator c() const
Definition: ConstRangeAdapter.h:257
DGtal::ConstRangeAdapter::ConstCirculator
Circulator< ConstIterator > ConstCirculator
Definition: ConstRangeAdapter.h:98
DGtal::operator<<
std::ostream & operator<<(std::ostream &out, const ClosedIntegerHalfPlane< TSpace > &object)
DGtal::ConstRangeAdapter::rend
ConstReverseIterator rend() const
Definition: ConstRangeAdapter.h:246
DGtal::ConstRangeAdapter::rbegin
ConstReverseIterator rbegin() const
Definition: ConstRangeAdapter.h:238
DGtal::ConstRangeAdapter::ConstRangeAdapter
ConstRangeAdapter(const TIterator &itb, const TIterator &ite, const TFunctor &aFunctor)
Definition: ConstRangeAdapter.h:112
DGtal::ConstRangeAdapter::operator=
ConstRangeAdapter & operator=(const ConstRangeAdapter &other)
DGtal::ConstRangeAdapter::~ConstRangeAdapter
~ConstRangeAdapter()
Definition: ConstRangeAdapter.h:144
DGtal::ConstRangeAdapter::size
Difference size(const TIterator &itb, const TIterator &ite, RandomAccessCategory) const
Definition: ConstRangeAdapter.h:285
DGtal::ConstRangeAdapter::ConstReverseCirculator
ReverseIterator< ConstCirculator > ConstReverseCirculator
Definition: ConstRangeAdapter.h:99
DGtal::BidirectionalCategory
Definition: IteratorCirculatorTraits.h:58
DGtal::ConstRangeAdapter::selfDisplay
void selfDisplay(std::ostream &out) const
Definition: ConstRangeAdapter.h:169
DGtal::ConstRangeAdapter::rc
ConstReverseCirculator rc() const
Definition: ConstRangeAdapter.h:269
DGtal::ConstRangeAdapter::ConstRangeAdapter
ConstRangeAdapter(const ConstRangeAdapter &other)
Definition: ConstRangeAdapter.h:132
boost_concepts::BidirectionalTraversalConcept
Go to http://www.boost.org/doc/libs/1_52_0/libs/iterator/doc/BidirectionalTraversal....
Definition: BoostConcepts.dox:50
boost_concepts::ReadableIteratorConcept
Go to http://www.boost.org/doc/libs/1_52_0/libs/iterator/doc/ReadableIterator.html.
Definition: BoostConcepts.dox:29
DGtal::IteratorCirculatorTraits::Category
ToDGtalCategory< typename boost::iterator_category< IC >::type >::Category Category
Definition: IteratorCirculatorTraits.h:301
DGtal::ConstRangeAdapter::myEnd
TIterator myEnd
Definition: ConstRangeAdapter.h:195
DGtal::ConstRangeAdapter::myFlagIsOwned
bool myFlagIsOwned
Definition: ConstRangeAdapter.h:203
DGtal::ConstRangeAdapter::BOOST_CONCEPT_ASSERT
BOOST_CONCEPT_ASSERT((boost_concepts::BidirectionalTraversalConcept< TIterator >))
DGtal::IteratorCirculatorTraits::Difference
IC::difference_type Difference
Definition: IteratorCirculatorTraits.h:304
DGtal::ConstIteratorAdapter
This class adapts any iterator so that operator* returns another element than the one pointed to by t...
Definition: ConstIteratorAdapter.h:88