DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 
89  BOOST_CONCEPT_ASSERT(( boost_concepts::ReadableIteratorConcept<TIterator> ));
90  BOOST_CONCEPT_ASSERT(( boost_concepts::BidirectionalTraversalConcept<TIterator> ));
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(), 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 
247  return ConstReverseIterator(this->begin());
248  }
249 
254  ConstCirculator c() const {
255  return ConstCirculator( this->begin(), this->begin(), this->end() );
256  }
257 
263  return ConstReverseCirculator( this->c() );
264  }
265 
266  private:
267 
275  Difference size(const TIterator& itb, const TIterator& ite, RandomAccessCategory) const
276  {
277  return (ite-itb);
278  }
279 
287  Difference size(const TIterator& itb, const TIterator& ite, BidirectionalCategory) const
288  {
289  TIterator it = itb;
290  unsigned int d = 0;
291  for ( ; it != ite; ++it, ++d)
292  {}
293  return d;
294  }
295 
296  }; //end class ConstRangeAdapter
297 
304  template <typename TIterator, typename TFunctor, typename TReturnType>
305  std::ostream&
306  operator<< ( std::ostream & out, const ConstRangeAdapter<TIterator, TFunctor, TReturnType> & object )
307  {
308  object.selfDisplay( out );
309  return out;
310  }
311 
318  template <typename A, typename B>
319  std::ostream&
320  operator<< ( std::ostream & out, const std::pair<A, B> & object )
321  {
322  out << object.first << "|" << object.second;
323  return out;
324  }
325 } // namespace DGtal
326 
328 
329 
330 #endif // !defined ConstRangeAdapter_h
331 
332 #undef ConstRangeAdapter_RECURSES
333 #endif // else defined(ConstRangeAdapter_RECURSES)