/src/cppitertools/compress.hpp
Line | Count | Source (jump to first uncovered line) |
1 | | #ifndef ITER_COMPRESS_H_ |
2 | | #define ITER_COMPRESS_H_ |
3 | | |
4 | | #include "internal/iterator_wrapper.hpp" |
5 | | #include "internal/iterbase.hpp" |
6 | | |
7 | | #include <iterator> |
8 | | #include <utility> |
9 | | |
10 | | namespace iter { |
11 | | namespace impl { |
12 | | template <typename Container, typename Selector> |
13 | | class Compressed; |
14 | | } |
15 | | |
16 | | template <typename Container, typename Selector> |
17 | | impl::Compressed<Container, Selector> compress(Container&&, Selector&&); |
18 | | } |
19 | | |
20 | | template <typename Container, typename Selector> |
21 | | class iter::impl::Compressed { |
22 | | private: |
23 | | Container container_; |
24 | | Selector selectors_; |
25 | | |
26 | | friend Compressed iter::compress<Container, Selector>( |
27 | | Container&&, Selector&&); |
28 | | |
29 | | Compressed(Container&& in_container, Selector&& in_selectors) |
30 | | : container_(std::forward<Container>(in_container)), |
31 | 506 | selectors_(std::forward<Selector>(in_selectors)) {} |
32 | | |
33 | | public: |
34 | | Compressed(Compressed&&) = default; |
35 | | template <typename ContainerT, typename SelectorT> |
36 | | class Iterator { |
37 | | private: |
38 | | template <typename, typename> |
39 | | friend class Iterator; |
40 | | IteratorWrapper<ContainerT> sub_iter_; |
41 | | IteratorWrapper<ContainerT> sub_end_; |
42 | | |
43 | | IteratorWrapper<SelectorT> selector_iter_; |
44 | | IteratorWrapper<SelectorT> selector_end_; |
45 | | |
46 | 0 | void increment_iterators() { |
47 | 0 | ++sub_iter_; |
48 | 0 | ++selector_iter_; |
49 | 0 | } Unexecuted instantiation: iter::impl::Compressed<std::__1::vector<int, std::__1::allocator<int> >&, std::__1::vector<bool, std::__1::allocator<bool> >&>::Iterator<std::__1::vector<int, std::__1::allocator<int> >&, std::__1::vector<bool, std::__1::allocator<bool> >&>::increment_iterators() Unexecuted instantiation: iter::impl::Compressed<std::__1::vector<int, std::__1::allocator<int> >&, std::__1::vector<bool, std::__1::allocator<bool> >&>::Iterator<std::__1::vector<int, std::__1::allocator<int> > const&, std::__1::vector<bool, std::__1::allocator<bool> > const&>::increment_iterators() |
50 | | |
51 | 2.02k | void skip_failures() { |
52 | 2.02k | while (sub_iter_ != sub_end_ && selector_iter_ != selector_end_ |
53 | 2.02k | && !*selector_iter_) { |
54 | 0 | increment_iterators(); |
55 | 0 | } |
56 | 2.02k | } iter::impl::Compressed<std::__1::vector<int, std::__1::allocator<int> >&, std::__1::vector<bool, std::__1::allocator<bool> >&>::Iterator<std::__1::vector<int, std::__1::allocator<int> >&, std::__1::vector<bool, std::__1::allocator<bool> >&>::skip_failures() Line | Count | Source | 51 | 1.51k | void skip_failures() { | 52 | 1.51k | while (sub_iter_ != sub_end_ && selector_iter_ != selector_end_ | 53 | 1.51k | && !*selector_iter_) { | 54 | 0 | increment_iterators(); | 55 | 0 | } | 56 | 1.51k | } |
iter::impl::Compressed<std::__1::vector<int, std::__1::allocator<int> >&, std::__1::vector<bool, std::__1::allocator<bool> >&>::Iterator<std::__1::vector<int, std::__1::allocator<int> > const&, std::__1::vector<bool, std::__1::allocator<bool> > const&>::skip_failures() Line | Count | Source | 51 | 506 | void skip_failures() { | 52 | 506 | while (sub_iter_ != sub_end_ && selector_iter_ != selector_end_ | 53 | 506 | && !*selector_iter_) { | 54 | 0 | increment_iterators(); | 55 | 0 | } | 56 | 506 | } |
|
57 | | |
58 | | public: |
59 | | using iterator_category = std::input_iterator_tag; |
60 | | using value_type = iterator_traits_deref<ContainerT>; |
61 | | using difference_type = std::ptrdiff_t; |
62 | | using pointer = value_type*; |
63 | | using reference = value_type&; |
64 | | |
65 | | Iterator(IteratorWrapper<ContainerT>&& cont_iter, |
66 | | IteratorWrapper<ContainerT>&& cont_end, |
67 | | IteratorWrapper<SelectorT>&& sel_iter, |
68 | | IteratorWrapper<SelectorT>&& sel_end) |
69 | | : sub_iter_{std::move(cont_iter)}, |
70 | | sub_end_{std::move(cont_end)}, |
71 | | selector_iter_{std::move(sel_iter)}, |
72 | 2.02k | selector_end_{std::move(sel_end)} { |
73 | 2.02k | skip_failures(); |
74 | 2.02k | } iter::impl::Compressed<std::__1::vector<int, std::__1::allocator<int> >&, std::__1::vector<bool, std::__1::allocator<bool> >&>::Iterator<std::__1::vector<int, std::__1::allocator<int> >&, std::__1::vector<bool, std::__1::allocator<bool> >&>::Iterator(std::__1::__wrap_iter<int*>&&, std::__1::__wrap_iter<int*>&&, std::__1::__bit_iterator<std::__1::vector<bool, std::__1::allocator<bool> >, false, 0ul>&&, std::__1::__bit_iterator<std::__1::vector<bool, std::__1::allocator<bool> >, false, 0ul>&&) Line | Count | Source | 72 | 1.51k | selector_end_{std::move(sel_end)} { | 73 | 1.51k | skip_failures(); | 74 | 1.51k | } |
iter::impl::Compressed<std::__1::vector<int, std::__1::allocator<int> >&, std::__1::vector<bool, std::__1::allocator<bool> >&>::Iterator<std::__1::vector<int, std::__1::allocator<int> > const&, std::__1::vector<bool, std::__1::allocator<bool> > const&>::Iterator(std::__1::__wrap_iter<int const*>&&, std::__1::__wrap_iter<int const*>&&, std::__1::__bit_iterator<std::__1::vector<bool, std::__1::allocator<bool> >, true, 0ul>&&, std::__1::__bit_iterator<std::__1::vector<bool, std::__1::allocator<bool> >, true, 0ul>&&) Line | Count | Source | 72 | 506 | selector_end_{std::move(sel_end)} { | 73 | 506 | skip_failures(); | 74 | 506 | } |
|
75 | | |
76 | 0 | iterator_deref<ContainerT> operator*() { |
77 | 0 | return *sub_iter_; |
78 | 0 | } |
79 | | |
80 | | iterator_arrow<ContainerT> operator->() { |
81 | | return apply_arrow(sub_iter_); |
82 | | } |
83 | | |
84 | 0 | Iterator& operator++() { |
85 | 0 | increment_iterators(); |
86 | 0 | skip_failures(); |
87 | 0 | return *this; |
88 | 0 | } |
89 | | |
90 | | Iterator operator++(int) { |
91 | | auto ret = *this; |
92 | | ++*this; |
93 | | return ret; |
94 | | } |
95 | | |
96 | | template <typename T, typename U> |
97 | 1.01k | bool operator!=(const Iterator<T, U>& other) const { |
98 | 1.01k | return sub_iter_ != other.sub_iter_ |
99 | 1.01k | && selector_iter_ != other.selector_iter_; |
100 | 1.01k | } bool iter::impl::Compressed<std::__1::vector<int, std::__1::allocator<int> >&, std::__1::vector<bool, std::__1::allocator<bool> >&>::Iterator<std::__1::vector<int, std::__1::allocator<int> >&, std::__1::vector<bool, std::__1::allocator<bool> >&>::operator!=<std::__1::vector<int, std::__1::allocator<int> >&, std::__1::vector<bool, std::__1::allocator<bool> >&>(iter::impl::Compressed<std::__1::vector<int, std::__1::allocator<int> >&, std::__1::vector<bool, std::__1::allocator<bool> >&>::Iterator<std::__1::vector<int, std::__1::allocator<int> >&, std::__1::vector<bool, std::__1::allocator<bool> >&> const&) const Line | Count | Source | 97 | 506 | bool operator!=(const Iterator<T, U>& other) const { | 98 | 506 | return sub_iter_ != other.sub_iter_ | 99 | 506 | && selector_iter_ != other.selector_iter_; | 100 | 506 | } |
bool iter::impl::Compressed<std::__1::vector<int, std::__1::allocator<int> >&, std::__1::vector<bool, std::__1::allocator<bool> >&>::Iterator<std::__1::vector<int, std::__1::allocator<int> >&, std::__1::vector<bool, std::__1::allocator<bool> >&>::operator!=<std::__1::vector<int, std::__1::allocator<int> > const&, std::__1::vector<bool, std::__1::allocator<bool> > const&>(iter::impl::Compressed<std::__1::vector<int, std::__1::allocator<int> >&, std::__1::vector<bool, std::__1::allocator<bool> >&>::Iterator<std::__1::vector<int, std::__1::allocator<int> > const&, std::__1::vector<bool, std::__1::allocator<bool> > const&> const&) const Line | Count | Source | 97 | 506 | bool operator!=(const Iterator<T, U>& other) const { | 98 | 506 | return sub_iter_ != other.sub_iter_ | 99 | 506 | && selector_iter_ != other.selector_iter_; | 100 | 506 | } |
|
101 | | |
102 | | template <typename T, typename U> |
103 | 506 | bool operator==(const Iterator<T, U>& other) const { |
104 | 506 | return !(*this != other); |
105 | 506 | } |
106 | | }; |
107 | | |
108 | 1.01k | Iterator<Container, Selector> begin() { |
109 | 1.01k | return {get_begin(container_), get_end(container_), get_begin(selectors_), |
110 | 1.01k | get_end(selectors_)}; |
111 | 1.01k | } |
112 | | |
113 | 506 | Iterator<Container, Selector> end() { |
114 | 506 | return {get_end(container_), get_end(container_), get_end(selectors_), |
115 | 506 | get_end(selectors_)}; |
116 | 506 | } |
117 | | |
118 | | Iterator<AsConst<Container>, AsConst<Selector>> begin() const { |
119 | | return {get_begin(std::as_const(container_)), |
120 | | get_end(std::as_const(container_)), |
121 | | get_begin(std::as_const(selectors_)), |
122 | | get_end(std::as_const(selectors_))}; |
123 | | } |
124 | | |
125 | 506 | Iterator<AsConst<Container>, AsConst<Selector>> end() const { |
126 | 506 | return {get_end(std::as_const(container_)), |
127 | 506 | get_end(std::as_const(container_)), |
128 | 506 | get_end(std::as_const(selectors_)), |
129 | 506 | get_end(std::as_const(selectors_))}; |
130 | 506 | } |
131 | | }; |
132 | | |
133 | | template <typename Container, typename Selector> |
134 | | iter::impl::Compressed<Container, Selector> iter::compress( |
135 | 506 | Container&& container_, Selector&& selectors_) { |
136 | 506 | return { |
137 | 506 | std::forward<Container>(container_), std::forward<Selector>(selectors_)}; |
138 | 506 | } |
139 | | |
140 | | #endif |