CXXIter 0.2
Loading...
Searching...
No Matches
Collector.h
1#pragma once
2
3#include <tuple>
4#include <type_traits>
5
6#include "Common.h"
7
8namespace CXXIter {
9
10 // ################################################################################################
11 // INTO COLLECTOR
12 // ################################################################################################
14 template<typename TChainInput, typename TContainer>
15 struct IntoCollector {};
17 template<typename TChainInput, typename TContainer>
18 requires util::BackInsertableContainer<TContainer, typename TChainInput::ItemOwned>
19 struct IntoCollector<TChainInput, TContainer> {
20 using Item = typename TChainInput::Item;
21
22 static constexpr void collectInto(TChainInput& input, TContainer& container) {
23 if constexpr(util::ReservableContainer<TContainer>) {
24 container.reserve( container.size() + input.sizeHint().expectedResultSize() );
25 }
26 input.forEach([&container](Item&& item) { container.push_back( std::forward<Item>(item) ); });
27 }
28 };
30 template<typename TChainInput, typename TContainer>
31 requires (!util::BackInsertableContainer<TContainer, typename TChainInput::ItemOwned>)
32 && util::InsertableContainer<TContainer, typename TChainInput::ItemOwned>
33 struct IntoCollector<TChainInput, TContainer> {
34 using Item = typename TChainInput::Item;
35
36 static constexpr void collectInto(TChainInput& input, TContainer& container) {
37 if constexpr(util::ReservableContainer<TContainer>) {
38 container.reserve( container.size() + input.sizeHint().expectedResultSize() );
39 }
40 input.forEach([&container](Item&& item) { container.insert( std::forward<Item>(item) ); });
41 }
42 };
44 template<typename TChainInput, typename TContainer>
45 requires util::StdArrayContainer<TContainer, typename TChainInput::ItemOwned>
46 struct IntoCollector<TChainInput, TContainer> {
47 using Item = typename TChainInput::Item;
48
49 static constexpr void collectInto(TChainInput& input, TContainer& container) {
50 size_t idx = 0;
51 input
52 .take(container.max_size())
53 .forEach([&idx, &container](Item&& item) { container[idx++] = item; });
54 }
55 };
56
57
58 // ################################################################################################
59 // COLLECTOR
60 // ################################################################################################
62 template<typename TChainInput, template <typename...> typename TContainer, typename... TContainerArgs>
63 struct Collector {};
65 template<typename TChainInput, template <typename...> typename TContainer, typename... TContainerArgs>
66 requires util::BackInsertableContainerTemplate<TContainer, typename TChainInput::ItemOwned, TContainerArgs...>
67 || util::InsertableContainerTemplate<TContainer, typename TChainInput::ItemOwned, TContainerArgs...>
68 struct Collector<TChainInput, TContainer, TContainerArgs...> {
69 template<typename Item, typename ItemOwned>
70 static constexpr TContainer<ItemOwned, TContainerArgs...> collect(TChainInput& input) {
71 TContainer<ItemOwned, TContainerArgs...> container;
72 IntoCollector<TChainInput, decltype(container)>::collectInto(input, container);
73 return container;
74 }
75 };
77 template<typename TChainInput, template <typename...> typename TContainer, typename... TContainerArgs>
78 requires (!util::BackInsertableContainerTemplate<TContainer, typename TChainInput::ItemOwned, TContainerArgs...>)
79 && util::is_pair<typename TChainInput::ItemOwned>
80 && util::AssocContainerTemplate<TContainer, std::tuple_element_t<0, typename TChainInput::ItemOwned>, std::tuple_element_t<1, typename TChainInput::ItemOwned>, TContainerArgs...>
81 struct Collector<TChainInput, TContainer, TContainerArgs...> {
82 template<typename Item, typename ItemOwned>
83 static constexpr auto collect(TChainInput& input) {
84 using TKey = std::remove_const_t<std::tuple_element_t<0, typename TChainInput::ItemOwned>>;
85 using TValue = std::tuple_element_t<1, typename TChainInput::ItemOwned>;
86 TContainer<TKey, TValue, TContainerArgs...> container;
87 IntoCollector<TChainInput, decltype(container)>::collectInto(input, container);
88 return container;
89 }
90 };
91
92}
CXXIter.
Definition: CXXIter.h:48