CXXIter 0.2
Loading...
Searching...
No Matches
Zipper.h
1#pragma once
2
3#include <tuple>
4#include <cstdlib>
5#include <optional>
6#include <algorithm>
7
8#include "../Common.h"
9#include "../util/TraitImpl.h"
10
11namespace CXXIter {
12
13 // ################################################################################################
14 // ZIPPER
15 // ################################################################################################
16 namespace op {
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...>>;
22 private:
23 struct source_ended_exception {};
24 std::tuple<TChainInput1, TChainInputs...> inputs;
25 public:
26 constexpr Zipper(TChainInput1&& input1, TChainInputs&&... inputs) : inputs( std::forward_as_tuple(std::move(input1), std::move(inputs)...) ) {}
27 };
28 }
29 // ------------------------------------------------------------------------------------------------
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);
35 // CXXIter Interface
36 using Self = op::Zipper<TChainInput1, TZipContainer, TChainInputs...>;
37 using Item = TZipContainer<typename TChainInput1::Item, typename TChainInputs::Item...>;
38
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{};
44 }
45 return input.value();
46 };
47 auto constructZipped = [&]<size_t... IDX>(std::integer_sequence<size_t, IDX...>) -> Item {
48 return { getElementFromChainInput(std::integral_constant<size_t, IDX>())... };
49 };
50
51 try {
52 return constructZipped(std::make_index_sequence<INPUT_CNT>{});
53 } catch(typename Self::source_ended_exception) {
54 return {};
55 }
56 }
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);
64 return true;
65 });
66 return SizeHint(lowerBoundMin, upperBoundMin);
67 }
68 static constexpr inline size_t advanceBy(Self& self, size_t n) { return util::advanceByPull(self, n); }
69 };
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;
75 }
76 };
77
78}
CXXIter.
Definition: CXXIter.h:48