43 template <
typename Container1,
typename Container2>
46 using Iterator1 = decltype(std::declval<Container1&>().begin());
47 using Iterator2 = decltype(std::declval<Container2&>().begin());
48 using Value1 = decltype(*std::declval<Iterator1>());
49 using Value2 = decltype(*std::declval<Iterator2>());
51 Zip(Container1 &container1, Container2 &container2) {
52 m_beginC1 = container1.begin();
53 m_beginC2 = container2.begin();
55 m_endC1 = container1.end();
56 m_endC2 = container2.end();
58 auto distanceC1 = std::distance(m_beginC1, m_endC1);
59 auto distanceC2 = std::distance(m_beginC2, m_endC2);
61 if (distanceC1 == distanceC2) {
65 else if (distanceC1 < distanceC2) {
67 m_zipEndC2 = std::next(m_beginC2, distanceC1);
71 m_zipEndC1 = std::next(m_beginC1, distanceC2);
82 m_currentIt1(currentIt1),
83 m_currentIt2(currentIt2),
104 bool operator==(
const Iterator &other)
const
106 return m_currentIt1 == other.m_currentIt1
107 && m_currentIt2 == other.m_currentIt2;
110 bool operator!=(
const Iterator &other)
const
112 return m_currentIt1 != other.m_currentIt1
113 || m_currentIt2 != other.m_currentIt2;
116 std::pair<Value1, Value2> operator*()
118 return std::pair<Value1, Value2>(*m_currentIt1, *m_currentIt2);
121 Iterator1 firstIterator()
const
126 Iterator2 secondIterator()
const
131 bool hasFirstEnded()
const
133 return m_currentIt1 == m_endIt1;
136 bool hasSecondEnded()
const
138 return m_currentIt2 == m_endIt2;
141 bool hasBothEnded()
const
143 return m_currentIt1 == m_endIt1 && m_currentIt2 == m_endIt2;
147 Iterator1 m_currentIt1;
148 Iterator2 m_currentIt2;
156 return Iterator(m_beginC1, m_beginC2, m_endC1, m_endC2);
161 return Iterator(m_zipEndC1, m_zipEndC2, m_endC1, m_endC2);
164 Iterator1 firstEnd()
const
169 Iterator2 secondEnd()
const
179 Iterator1 m_zipEndC1;
180 Iterator2 m_zipEndC2;
Definition: ZipIterator.h:75
The class provides an elegant way to iterate over 2 containers, which size can be different...
Definition: ZipIterator.h:44