PCL
Iterator.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.7.0
6 // ----------------------------------------------------------------------------
7 // pcl/Iterator.h - Released 2024-06-18T15:48:54Z
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-2024 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 (https://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 
94 {
95 };
96 
97 // ----------------------------------------------------------------------------
98 
103 template <class C, class T>
104 struct PCL_CLASS Iterator
105 {
106  using iterator_class = C;
107  using item_type = T;
108 };
109 
110 // ----------------------------------------------------------------------------
111 
116 template <class C, class T> inline
118 {
119  return C();
120 }
121 
126 template <typename T> inline
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 
279  : pcl::Iterator<C,T>( i )
280  , iterator( i.iterator )
281  {
282  }
283 
288  ReverseIteratorBase( const BI& i )
289  : iterator( i )
290  {
291  }
292 
297  T& operator *() const
298  {
299  return (T&)*iterator;
300  }
301 
306  BI operator ->() const
307  {
308  return this->Iterator();
309  }
310 
315  operator BI() const
316  {
317  return this->Iterator();
318  }
319 
323  BI Iterator() const
324  {
325  return iterator;
326  }
327 
328 protected:
329 
330  BI iterator;
331 
332  void PreIncrement()
333  {
334  --iterator;
335  }
336 
337  BI PostIncrement()
338  {
339  BI tmp = iterator;
340  --iterator;
341  return tmp;
342  }
343 
344  void PreDecrement()
345  {
346  ++iterator;
347  }
348 
349  BI PostDecrement()
350  {
351  BI tmp = iterator;
352  ++iterator;
353  return tmp;
354  }
355 };
356 
361 template <class BI, class C, class T> inline
363 {
364  return i.Iterator() == j.Iterator();
365 }
366 
367 // ----------------------------------------------------------------------------
368 
369 #define IMPLEMENT_INCDEC_OPERATORS \
370  __I__& operator ++() { __R__::PreIncrement(); return *this; } \
371  __I__ operator ++( int ) { return __I__( __R__::PostIncrement() ); } \
372  __I__& operator --() { __R__::PreDecrement(); return *this; } \
373  __I__ operator --( int ) { return __I__( __R__::PostDecrement() ); }
374 
379 template <class BI, class T>
380 class PCL_CLASS ReverseBidirectionalIterator : public ReverseIteratorBase<BI,BidirectionalIterator,T>
381 {
384 
385 public:
386 
391 
396  : __R__( i )
397  {
398  }
399 
405  : __R__( i )
406  {
407  }
408 
409  IMPLEMENT_INCDEC_OPERATORS
410 };
411 
412 // ----------------------------------------------------------------------------
413 
418 template <class RI, class T>
419 class PCL_CLASS ReverseRandomAccessIterator : public ReverseIteratorBase<RI,RandomAccessIterator,T>
420 {
423 
424 public:
425 
430 
435  : __R__( i )
436  {
437  }
438 
444  : __R__( i )
445  {
446  }
447 
452  T& operator[]( size_type d ) const
453  {
454  return (T&)*(__R__::iterator - d);
455  }
456 
462  {
463  __R__::iterator -= d;
464  return *this;
465  }
466 
472  {
473  __R__::iterator += d;
474  return *this;
475  }
476 
477  IMPLEMENT_INCDEC_OPERATORS
478 };
479 
480 #undef IMPLEMENT_INCDEC_OPERATORS
481 
484 template <class RI, class T> inline
486 {
487  return j.Iterator() < i.Iterator();
488 }
489 
492 template <class RI, class T> inline
494 {
495  RI r = i.Iterator();
496  return r -= d;
497 }
498 
501 template <class RI, class T> inline
503 {
504  return i + d;
505 }
506 
509 template <class RI, class T> inline
511 {
512  RI r = i.Iterator();
513  return r += d;
514 }
515 
518 template <class RI, class T> inline
520 {
521  return j.Iterator() - i.Iterator();
522 }
523 
524 // ----------------------------------------------------------------------------
525 
552 template <class C>
553 class PCL_CLASS ReverseIterable
554 {
555 public:
556 
557  using container = C;
558 
559  ReverseIterable( container& c )
560  : m_container( c )
561  {
562  }
563 
564  ReverseIterable( const ReverseIterable& ) = default;
565 
572  auto Begin()
573  {
574  return m_container.ReverseBegin();
575  }
576 
583  auto Begin() const
584  {
585  return m_container.ReverseBegin();
586  }
587 
594  auto ConstBegin() const
595  {
596  return m_container.ConstReverseBegin();
597  }
598 
606  auto End()
607  {
608  return m_container.ReverseEnd();
609  }
610 
618  auto End() const
619  {
620  return m_container.ReverseEnd();
621  }
622 
630  auto ConstEnd() const
631  {
632  return m_container.ConstReverseEnd();
633  }
634 
640  {
641  return m_container.Begin();
642  }
643 
648  auto ReverseBegin() const
649  {
650  return m_container.Begin();
651  }
652 
657  auto ConstReverseBegin() const
658  {
659  return m_container.ConstBegin();
660  }
661 
665  auto ReverseEnd()
666  {
667  return m_container.End();
668  }
669 
674  auto ReverseEnd() const
675  {
676  return m_container.End();
677  }
678 
683  auto ConstReverseEnd() const
684  {
685  return m_container.ConstEnd();
686  }
687 
688 #ifndef __PCL_NO_STL_COMPATIBLE_ITERATORS
692  auto begin()
693  {
694  return Begin();
695  }
696 
700  auto begin() const
701  {
702  return Begin();
703  }
704 
708  auto end()
709  {
710  return End();
711  }
712 
716  auto end() const
717  {
718  return End();
719  }
720 #endif // !__PCL_NO_STL_COMPATIBLE_ITERATORS
721 
722 private:
723 
724  container& m_container;
725 };
726 
727 // ----------------------------------------------------------------------------
728 
729 } // pcl
730 
731 #endif // __PCL_Iterator_h
732 
733 // ----------------------------------------------------------------------------
734 // EOF pcl/Iterator.h - Released 2024-06-18T15:48:54Z
Bidirectional iterator class.
Definition: Iterator.h:85
Forward iterator class.
Definition: Iterator.h:76
Random access iterator class.
Definition: Iterator.h:94
Reverse bidirectional iterator.
Definition: Iterator.h:381
ReverseBidirectionalIterator(const BI &i)
Definition: Iterator.h:404
ReverseBidirectionalIterator(const ReverseBidirectionalIterator &i)
Definition: Iterator.h:395
Reverse container adaptor.
Definition: Iterator.h:554
auto ConstEnd() const
Definition: Iterator.h:630
auto ReverseBegin() const
Definition: Iterator.h:648
auto ConstReverseEnd() const
Definition: Iterator.h:683
auto Begin() const
Definition: Iterator.h:583
auto End() const
Definition: Iterator.h:618
auto begin() const
Definition: Iterator.h:700
auto ReverseEnd() const
Definition: Iterator.h:674
auto end() const
Definition: Iterator.h:716
auto ConstBegin() const
Definition: Iterator.h:594
auto ConstReverseBegin() const
Definition: Iterator.h:657
Base class of reverse iterators.
Definition: Iterator.h:267
ReverseIteratorBase(const BI &i)
Definition: Iterator.h:288
ReverseIteratorBase(const ReverseIteratorBase &i)
Definition: Iterator.h:278
Reverse random access iterator.
Definition: Iterator.h:420
ReverseRandomAccessIterator(const ReverseRandomAccessIterator &i)
Definition: Iterator.h:434
ReverseRandomAccessIterator(const RI &i)
Definition: Iterator.h:443
bool operator==(const Array< T, A > &x1, const Array< T, A > &x2) noexcept
Definition: Array.h:2267
bool operator<(const Array< T, A > &x1, const Array< T, A > &x2) noexcept
Definition: Array.h:2278
Complex< T1 > operator-(const Complex< T1 > &c1, const Complex< T2 > &c2) noexcept
Definition: Complex.h:504
Complex< T1 > operator*(const Complex< T1 > &c1, const Complex< T2 > &c2) noexcept
Definition: Complex.h:548
Complex< T1 > operator+(const Complex< T1 > &c1, const Complex< T2 > &c2) noexcept
Definition: Complex.h:464
ptrdiff_t distance_type
Definition: Defs.h:615
size_t size_type
Definition: Defs.h:609
PCL root namespace.
Definition: AbstractImage.h:77
void MoveBackward(BI &i, size_type n)
Definition: Iterator.h:242
void MoveForward(FI &i, size_type n)
Definition: Iterator.h:219
void Advance(FI &i, distance_type d)
Definition: Iterator.h:186
distance_type Distance(FI i, FI j)
Definition: Iterator.h:161
C IteratorClass(const Iterator< C, T > &)
Definition: Iterator.h:117
T * ItemType(const Iterator< C, T > &)
Definition: Iterator.h:139
Generic container iterator.
Definition: Iterator.h:105
C iterator_class
Represents the iterator class.
Definition: Iterator.h:106
T item_type
Represents the item type.
Definition: Iterator.h:107