iterator.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // -*- C++ -*-
  2. // Copyright (C) 2007-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the terms
  6. // of the GNU General Public License as published by the Free Software
  7. // Foundation; either version 3, or (at your option) any later
  8. // version.
  9. // This library is distributed in the hope that it will be useful, but
  10. // WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file parallel/iterator.h
  21. * @brief Helper iterator classes for the std::transform() functions.
  22. * This file is a GNU parallel extension to the Standard C++ Library.
  23. */
  24. // Written by Johannes Singler.
  25. #ifndef _GLIBCXX_PARALLEL_ITERATOR_H
  26. #define _GLIBCXX_PARALLEL_ITERATOR_H 1
  27. #include <parallel/basic_iterator.h>
  28. #include <bits/stl_pair.h>
  29. namespace __gnu_parallel
  30. {
  31. /** @brief A pair of iterators. The usual iterator operations are
  32. * applied to both child iterators.
  33. */
  34. template<typename _Iterator1, typename _Iterator2,
  35. typename _IteratorCategory>
  36. class _IteratorPair : public std::pair<_Iterator1, _Iterator2>
  37. {
  38. private:
  39. typedef std::pair<_Iterator1, _Iterator2> _Base;
  40. public:
  41. typedef _IteratorCategory iterator_category;
  42. typedef void value_type;
  43. typedef std::iterator_traits<_Iterator1> _TraitsType;
  44. typedef typename _TraitsType::difference_type difference_type;
  45. typedef _IteratorPair* pointer;
  46. typedef _IteratorPair& reference;
  47. _IteratorPair() { }
  48. _IteratorPair(const _Iterator1& __first, const _Iterator2& __second)
  49. : _Base(__first, __second) { }
  50. // Pre-increment operator.
  51. _IteratorPair&
  52. operator++()
  53. {
  54. ++_Base::first;
  55. ++_Base::second;
  56. return *this;
  57. }
  58. // Post-increment operator.
  59. const _IteratorPair
  60. operator++(int)
  61. { return _IteratorPair(_Base::first++, _Base::second++); }
  62. // Pre-decrement operator.
  63. _IteratorPair&
  64. operator--()
  65. {
  66. --_Base::first;
  67. --_Base::second;
  68. return *this;
  69. }
  70. // Post-decrement operator.
  71. const _IteratorPair
  72. operator--(int)
  73. { return _IteratorPair(_Base::first--, _Base::second--); }
  74. // Type conversion.
  75. operator _Iterator2() const
  76. { return _Base::second; }
  77. _IteratorPair&
  78. operator=(const _IteratorPair& __other)
  79. {
  80. _Base::first = __other.first;
  81. _Base::second = __other.second;
  82. return *this;
  83. }
  84. _IteratorPair
  85. operator+(difference_type __delta) const
  86. { return _IteratorPair(_Base::first + __delta, _Base::second + __delta);
  87. }
  88. difference_type
  89. operator-(const _IteratorPair& __other) const
  90. { return _Base::first - __other.first; }
  91. };
  92. /** @brief A triple of iterators. The usual iterator operations are
  93. applied to all three child iterators.
  94. */
  95. template<typename _Iterator1, typename _Iterator2, typename _Iterator3,
  96. typename _IteratorCategory>
  97. class _IteratorTriple
  98. {
  99. public:
  100. typedef _IteratorCategory iterator_category;
  101. typedef void value_type;
  102. typedef typename std::iterator_traits<_Iterator1>::difference_type
  103. difference_type;
  104. typedef _IteratorTriple* pointer;
  105. typedef _IteratorTriple& reference;
  106. _Iterator1 _M_first;
  107. _Iterator2 _M_second;
  108. _Iterator3 _M_third;
  109. _IteratorTriple() { }
  110. _IteratorTriple(const _Iterator1& __first, const _Iterator2& __second,
  111. const _Iterator3& __third)
  112. {
  113. _M_first = __first;
  114. _M_second = __second;
  115. _M_third = __third;
  116. }
  117. // Pre-increment operator.
  118. _IteratorTriple&
  119. operator++()
  120. {
  121. ++_M_first;
  122. ++_M_second;
  123. ++_M_third;
  124. return *this;
  125. }
  126. // Post-increment operator.
  127. const _IteratorTriple
  128. operator++(int)
  129. { return _IteratorTriple(_M_first++, _M_second++, _M_third++); }
  130. // Pre-decrement operator.
  131. _IteratorTriple&
  132. operator--()
  133. {
  134. --_M_first;
  135. --_M_second;
  136. --_M_third;
  137. return *this;
  138. }
  139. // Post-decrement operator.
  140. const _IteratorTriple
  141. operator--(int)
  142. { return _IteratorTriple(_M_first--, _M_second--, _M_third--); }
  143. // Type conversion.
  144. operator _Iterator3() const
  145. { return _M_third; }
  146. _IteratorTriple&
  147. operator=(const _IteratorTriple& __other)
  148. {
  149. _M_first = __other._M_first;
  150. _M_second = __other._M_second;
  151. _M_third = __other._M_third;
  152. return *this;
  153. }
  154. _IteratorTriple
  155. operator+(difference_type __delta) const
  156. { return _IteratorTriple(_M_first + __delta, _M_second + __delta,
  157. _M_third + __delta); }
  158. difference_type
  159. operator-(const _IteratorTriple& __other) const
  160. { return _M_first - __other._M_first; }
  161. };
  162. }
  163. #endif /* _GLIBCXX_PARALLEL_ITERATOR_H */