PCL
Iterator.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.1.19
6 // ----------------------------------------------------------------------------
7 // pcl/Iterator.h - Released 2019-11-07T10:59:34Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2019 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (http://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_Iterator_h
53 #define __PCL_Iterator_h
54 
56 
57 #include <pcl/Defs.h>
58 
59 #include <pcl/Relational.h>
60 
61 namespace pcl
62 {
63 
64 // ----------------------------------------------------------------------------
65 
75 class PCL_CLASS ForwardIterator
76 {
77 };
78 
84 class PCL_CLASS BidirectionalIterator : public ForwardIterator
85 {
86 };
87 
93 class PCL_CLASS RandomAccessIterator : public BidirectionalIterator
94 {
95 };
96 
97 // ----------------------------------------------------------------------------
98 
103 template <class C, class T>
104 struct PCL_CLASS Iterator
105 {
106  typedef C iterator_class;
107  typedef T item_type;
108 };
109 
110 // ----------------------------------------------------------------------------
111 
116 template <class C, class T> inline
118 {
119  return C();
120 }
121 
126 template <typename T> inline
127 RandomAccessIterator IteratorClass( const T* )
128 {
129  return RandomAccessIterator();
130 }
131 
132 // ----------------------------------------------------------------------------
133 
138 template <class C, class T> inline
140 {
141  return nullptr;
142 }
143 
148 template <typename T> inline
149 T* ItemType( const T* )
150 {
151  return nullptr;
152 }
153 
154 // ----------------------------------------------------------------------------
155 
160 template <class FI> inline
161 distance_type Distance( FI i, FI j )
162 {
163  return __pcl_distance__( i, j, IteratorClass( i ) );
164 }
165 
166 template <class FI> inline
167 distance_type __pcl_distance__( FI i, FI j, ForwardIterator )
168 {
169  distance_type d = 0;
170  for ( ; i != j; ++i, ++d ) {}
171  return d;
172 }
173 
174 template <class RI> inline
175 distance_type __pcl_distance__( RI i, RI j, RandomAccessIterator )
176 {
177  return j - i;
178 }
179 
180 // ----------------------------------------------------------------------------
181 
185 template <class FI> inline
186 void Advance( FI& i, distance_type d )
187 {
188  __pcl_advance__( i, d, IteratorClass( i ) );
189 }
190 
191 template <class FI> inline
192 void __pcl_advance__( FI& i, distance_type d, ForwardIterator )
193 {
194  PCL_PRECONDITION( d >= 0 )
195  for ( ; d > 0; --d, ++i ) {}
196 }
197 
198 template <class BI> inline
199 void __pcl_advance__( BI& i, distance_type d, BidirectionalIterator )
200 {
201  if ( d > 0 )
202  for ( ; ++i, --d > 0; ) {}
203  else
204  for ( ; d < 0; ++d, --i ) {}
205 }
206 
207 template <class RI> inline
208 void __pcl_advance__( RI& i, distance_type d, RandomAccessIterator )
209 {
210  i += d;
211 }
212 
213 // ----------------------------------------------------------------------------
214 
218 template <class FI> inline
219 void MoveForward( FI& i, size_type n )
220 {
221  __pcl_move_forward__( i, n, IteratorClass( i ) );
222 }
223 
224 template <class FI> inline
225 void __pcl_move_forward__( FI& i, size_type n, ForwardIterator )
226 {
227  for ( ; n > 0; --n, ++i ) {}
228 }
229 
230 template <class RI> inline
231 void __pcl_move_forward__( RI& i, size_type n, RandomAccessIterator )
232 {
233  i += n;
234 }
235 
236 // ----------------------------------------------------------------------------
237 
241 template <class BI> inline
242 void MoveBackward( BI& i, size_type n )
243 {
244  __pcl_move_backward__( i, n, IteratorClass( i ) );
245 }
246 
247 template <class BI> inline
248 void __pcl_move_backward__( BI& i, size_type n, BidirectionalIterator )
249 {
250  for ( ; n > 0; --n, --i ) {}
251 }
252 
253 template <class RI> inline
254 void __pcl_move_backward__( RI& i, size_type n, RandomAccessIterator )
255 {
256  i -= n;
257 }
258 
259 // ----------------------------------------------------------------------------
260 
265 template <class BI, class C, class T>
266 class PCL_CLASS ReverseIteratorBase : public pcl::Iterator<C,T>
267 {
268 public:
269 
273  ReverseIteratorBase() = default;
274 
278  ReverseIteratorBase( const ReverseIteratorBase& i ) : pcl::Iterator<C,T>( i ), iterator( i.iterator )
279  {
280  }
281 
286  ReverseIteratorBase( const BI& i ) : iterator( i )
287  {
288  }
289 
294  T& operator *() const
295  {
296  return (T&)*iterator;
297  }
298 
303  BI operator ->() const
304  {
305  return this->Iterator();
306  }
307 
312  operator BI() const
313  {
314  return this->Iterator();
315  }
316 
320  BI Iterator() const
321  {
322  return iterator;
323  }
324 
325 protected:
326 
327  BI iterator;
328 
329  void PreIncrement()
330  {
331  --iterator;
332  }
333 
334  BI PostIncrement()
335  {
336  BI tmp = iterator;
337  --iterator;
338  return tmp;
339  }
340 
341  void PreDecrement()
342  {
343  ++iterator;
344  }
345 
346  BI PostDecrement()
347  {
348  BI tmp = iterator;
349  ++iterator;
350  return tmp;
351  }
352 };
353 
358 template <class BI, class C, class T> inline
360 {
361  return i.Iterator() == j.Iterator();
362 }
363 
364 // ----------------------------------------------------------------------------
365 
366 #define IMPLEMENT_INCDEC_OPERATORS \
367  __I__& operator ++() { __R__::PreIncrement(); return *this; } \
368  __I__ operator ++( int ) { return __I__( __R__::PostIncrement() ); } \
369  __I__& operator --() { __R__::PreDecrement(); return *this; } \
370  __I__ operator --( int ) { return __I__( __R__::PostDecrement() ); }
371 
376 template <class BI, class T>
377 class PCL_CLASS ReverseBidirectionalIterator : public ReverseIteratorBase<BI,BidirectionalIterator,T>
378 {
381 
382 public:
383 
387  ReverseBidirectionalIterator() = default;
388 
393  {
394  }
395 
400  ReverseBidirectionalIterator( const BI& i ) : __R__( i )
401  {
402  }
403 
404  IMPLEMENT_INCDEC_OPERATORS
405 };
406 
407 // ----------------------------------------------------------------------------
408 
413 template <class RI, class T>
414 class PCL_CLASS ReverseRandomAccessIterator : public ReverseIteratorBase<RI,RandomAccessIterator,T>
415 {
418 
419 public:
420 
424  ReverseRandomAccessIterator() = default;
425 
430  {
431  }
432 
437  ReverseRandomAccessIterator( const RI& i ) : __R__( i )
438  {
439  }
440 
445  T& operator[]( size_type d ) const
446  {
447  return (T&)*(__R__::iterator - d);
448  }
449 
455  {
456  __R__::iterator -= d;
457  return *this;
458  }
459 
465  {
466  __R__::iterator += d;
467  return *this;
468  }
469 
470  IMPLEMENT_INCDEC_OPERATORS
471 };
472 
473 #undef IMPLEMENT_INCDEC_OPERATORS
474 
477 template <class RI, class T> inline
478 bool operator <( const ReverseRandomAccessIterator<RI,T>& i, const ReverseRandomAccessIterator<RI,T>& j )
479 {
480  return j.Iterator() < i.Iterator();
481 }
482 
485 template <class RI, class T> inline
487 {
488  RI r = i.Iterator();
489  return r -= d;
490 }
491 
494 template <class RI, class T> inline
496 {
497  return i + d;
498 }
499 
502 template <class RI, class T> inline
504 {
505  RI r = i.Iterator();
506  return r += d;
507 }
508 
511 template <class RI, class T> inline
513 {
514  return j.Iterator() - i.Iterator();
515 }
516 
517 // ----------------------------------------------------------------------------
518 
545 template <class C>
546 class PCL_CLASS ReverseIterable
547 {
548 public:
549 
550  typedef C container;
551 
552  ReverseIterable( container& c ) : m_container( c )
553  {
554  }
555 
556  ReverseIterable( const ReverseIterable& ) = default;
557 
564  auto Begin()
565  {
566  return m_container.ReverseBegin();
567  }
568 
575  auto Begin() const
576  {
577  return m_container.ReverseBegin();
578  }
579 
586  auto ConstBegin() const
587  {
588  return m_container.ConstReverseBegin();
589  }
590 
598  auto End()
599  {
600  return m_container.ReverseEnd();
601  }
602 
610  auto End() const
611  {
612  return m_container.ReverseEnd();
613  }
614 
622  auto ConstEnd() const
623  {
624  return m_container.ConstReverseEnd();
625  }
626 
632  {
633  return m_container.Begin();
634  }
635 
640  auto ReverseBegin() const
641  {
642  return m_container.Begin();
643  }
644 
649  auto ConstReverseBegin() const
650  {
651  return m_container.ConstBegin();
652  }
653 
657  auto ReverseEnd()
658  {
659  return m_container.End();
660  }
661 
666  auto ReverseEnd() const
667  {
668  return m_container.End();
669  }
670 
675  auto ConstReverseEnd() const
676  {
677  return m_container.ConstEnd();
678  }
679 
680 #ifndef __PCL_NO_STL_COMPATIBLE_ITERATORS
681 
684  auto begin()
685  {
686  return Begin();
687  }
688 
692  auto begin() const
693  {
694  return Begin();
695  }
696 
700  auto end()
701  {
702  return End();
703  }
704 
708  auto end() const
709  {
710  return End();
711  }
712 #endif // !__PCL_NO_STL_COMPATIBLE_ITERATORS
713 
714 private:
715 
716  container& m_container;
717 };
718 
719 // ----------------------------------------------------------------------------
720 
721 } // pcl
722 
723 #endif // __PCL_Iterator_h
724 
725 // ----------------------------------------------------------------------------
726 // EOF pcl/Iterator.h - Released 2019-11-07T10:59:34Z
distance_type Distance(FI i, FI j)
Definition: Iterator.h:161
T item_type
Represents the item type.
Definition: Iterator.h:107
auto ConstReverseBegin() const
Definition: Iterator.h:649
Random access iterator class.
Definition: Iterator.h:93
Reverse bidirectional iterator.
Definition: Iterator.h:377
auto ConstBegin() const
Definition: Iterator.h:586
Complex< T1 > operator*(const Complex< T1 > &c1, const Complex< T2 > &c2)
Definition: Complex.h:539
ReverseIteratorBase(const ReverseIteratorBase &i)
Definition: Iterator.h:278
Generic container iterator.
Definition: Iterator.h:104
Complex< T1 > operator+(const Complex< T1 > &c1, const Complex< T2 > &c2)
Definition: Complex.h:455
auto ConstReverseEnd() const
Definition: Iterator.h:675
PCL root namespace.
Definition: AbstractImage.h:76
void MoveBackward(BI &i, size_type n)
Definition: Iterator.h:242
ReverseRandomAccessIterator(const RI &i)
Definition: Iterator.h:437
ReverseRandomAccessIterator(const ReverseRandomAccessIterator &i)
Definition: Iterator.h:429
Forward iterator class.
Definition: Iterator.h:75
T * ItemType(const Iterator< C, T > &)
Definition: Iterator.h:139
auto begin() const
Definition: Iterator.h:692
void Advance(FI &i, distance_type d)
Definition: Iterator.h:186
auto end() const
Definition: Iterator.h:708
size_t size_type
Definition: Defs.h:543
Base class of reverse iterators.
Definition: Iterator.h:266
auto Begin() const
Definition: Iterator.h:575
Complex< T1 > operator-(const Complex< T1 > &c1, const Complex< T2 > &c2)
Definition: Complex.h:495
auto ReverseBegin() const
Definition: Iterator.h:640
Bidirectional iterator class.
Definition: Iterator.h:84
Reverse random access iterator.
Definition: Iterator.h:414
bool operator==(const Array< T, A > &x1, const Array< T, A > &x2)
Definition: Array.h:2075
void MoveForward(FI &i, size_type n)
Definition: Iterator.h:219
ReverseBidirectionalIterator(const ReverseBidirectionalIterator &i)
Definition: Iterator.h:392
auto ReverseEnd() const
Definition: Iterator.h:666
ptrdiff_t distance_type
Definition: Defs.h:549
ReverseBidirectionalIterator(const BI &i)
Definition: Iterator.h:400
auto ConstEnd() const
Definition: Iterator.h:622
Reverse container adaptor.
Definition: Iterator.h:546
ReverseIteratorBase(const BI &i)
Definition: Iterator.h:286
auto End() const
Definition: Iterator.h:610
C IteratorClass(const Iterator< C, T > &)
Definition: Iterator.h:117