DGtal  0.9.3beta
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 
222  ConstIterator begin() const {
223  return ConstIterator( myBegin, *myFunctor );
224  }
225 
230  ConstIterator end() const {
231  return ConstIterator( myEnd, *myFunctor );
232  }
233 
238  ConstReverseIterator rbegin() const {
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)
Difference size(const TIterator &itb, const TIterator &ite, BidirectionalCategory) const
ReverseIterator< ConstIterator > ConstReverseIterator
Difference size(const TIterator &itb, const TIterator &ite, RandomAccessCategory) const
This class adapts any iterator so that operator* returns another element than the one pointed to by t...
std::string className() const
ReverseIterator< ConstCirculator > ConstReverseCirculator
ConstReverseCirculator rc() const
This class adapts any bidirectional iterator so that operator++ calls operator-- and vice versa...
void selfDisplay(std::ostream &out) const
ConstIteratorAdapter< TIterator, TFunctor, TReturnType > ConstIterator
ConstReverseIterator rbegin() const
ConstReverseIterator rend() const
ConstCirculator c() const
ConstRangeAdapter & operator=(const ConstRangeAdapter &other)
ConstRangeAdapter(const TIterator &itb, const TIterator &ite, const TFunctor &aFunctor)
ToDGtalCategory< typename boost::iterator_category< IC >::type >::Category Category
BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIteratorConcept< TIterator >))
Go to http://www.boost.org/doc/libs/1_52_0/libs/iterator/doc/BidirectionalTraversal.html.
Aim: Provides an adapter for classical iterators that can iterate through the underlying data structu...
Definition: Circulator.h:85
ConstIterator begin() const
DGtal is the top-level namespace which contains all DGtal functions and types.
Aim: model of CConstBidirectionalRange that adapts any range of elements bounded by two iterators [it...
ConstIterator end() const
Circulator< ConstIterator > ConstCirculator
IteratorCirculatorTraits< ConstIterator >::Difference Difference
ConstRangeAdapter(const ConstRangeAdapter &other)
ConstRangeAdapter(const TIterator &itb, const TIterator &ite, const TFunctor *aFunctorPtr)
Difference size() const
Go to http://www.boost.org/doc/libs/1_52_0/libs/iterator/doc/ReadableIterator.html.