9#include "../util/TraitImpl.h"
18 template<
typename TChainInput1,
template<
typename...>
typename TZipContainer,
typename... TChainInputs>
19 class [[nodiscard(CXXITER_CHAINER_NODISCARD_WARNING)]] Zipper :
public IterApi<Zipper<TChainInput1, TZipContainer, TChainInputs...>> {
20 friend struct trait::Iterator<Zipper<TChainInput1, TZipContainer, TChainInputs...>>;
21 friend struct trait::ExactSizeIterator<Zipper<TChainInput1, TZipContainer, TChainInputs...>>;
23 struct source_ended_exception {};
24 std::tuple<TChainInput1, TChainInputs...> inputs;
26 constexpr Zipper(TChainInput1&& input1, TChainInputs&&... inputs) : inputs( std::forward_as_tuple(std::move(input1), std::move(inputs)...) ) {}
31 template<
typename TChainInput1,
template<
typename...>
typename TZipContainer,
typename... TChainInputs>
32 struct trait::Iterator<op::Zipper<TChainInput1, TZipContainer, TChainInputs...>> {
33 using ChainInputIterators = std::tuple<Iterator<TChainInput1>, trait::Iterator<TChainInputs>...>;
34 static constexpr size_t INPUT_CNT = 1 +
sizeof...(TChainInputs);
36 using Self = op::Zipper<TChainInput1, TZipContainer, TChainInputs...>;
37 using Item = TZipContainer<
typename TChainInput1::Item,
typename TChainInputs::Item...>;
39 static constexpr inline IterValue<Item> next(Self& self) {
40 auto getElementFromChainInput = [&]<
size_t IDX>(std::integral_constant<size_t, IDX>) -> std::tuple_element_t<IDX, Item> {
41 auto input = std::tuple_element_t<IDX, ChainInputIterators>::next( std::get<IDX>(self.inputs) );
42 if(!input.has_value()) [[unlikely]] {
43 throw typename Self::source_ended_exception{};
47 auto constructZipped = [&]<
size_t... IDX>(std::integer_sequence<size_t, IDX...>) -> Item {
48 return { getElementFromChainInput(std::integral_constant<size_t, IDX>())... };
52 return constructZipped(std::make_index_sequence<INPUT_CNT>{});
53 }
catch(
typename Self::source_ended_exception) {
57 static constexpr inline SizeHint sizeHint(
const Self& self) {
58 size_t lowerBoundMin = std::numeric_limits<size_t>::max();
59 std::optional<size_t> upperBoundMin = {};
60 constexpr_for<0, INPUT_CNT>([&](
auto idx) {
61 SizeHint tmp = std::tuple_element_t<idx, ChainInputIterators>::sizeHint( std::get<idx>(self.inputs) );
62 lowerBoundMin = std::min(lowerBoundMin, tmp.lowerBound);
63 upperBoundMin = SizeHint::upperBoundMin(upperBoundMin, tmp.upperBound);
66 return SizeHint(lowerBoundMin, upperBoundMin);
68 static constexpr inline size_t advanceBy(Self& self,
size_t n) {
return util::advanceByPull(self, n); }
71 template<CXXIterExactSizeIterator TChainInput1,
template<
typename...>
typename TZipContainer, CXXIterExactSizeIterator... TChainInputs>
72 struct trait::ExactSizeIterator<op::Zipper<TChainInput1, TZipContainer, TChainInputs...>> {
73 static constexpr inline size_t size(
const op::Zipper<TChainInput1, TZipContainer, TChainInputs...>& self) {
74 return trait::Iterator<op::Zipper<TChainInput1, TZipContainer, TChainInputs...>>::sizeHint(self).lowerBound;