Skip to content

Commit 17a1742

Browse files
committed
Edited the take implementation. removed reference from lz iterables in ref_or_view
1 parent 616aff8 commit 17a1742

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+599
-405
lines changed

examples/cached_size.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ int main() {
99
* - `lz::chunks`
1010
* - `lz::enumerate`
1111
* - `lz::exclude`
12+
* - `lz::take`
1213
* - `lz::take_every`
1314
* - `lz::zip_longest`
1415
* - `lz::zip`
@@ -52,6 +53,7 @@ int main() {
5253
* - lz::chunks
5354
* - lz::enumerate
5455
* - lz::exclude
56+
* - lz::take
5557
* - lz::take_every
5658
* - lz::zip_longest
5759
* - lz::zip

examples/to_container.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
#include <set>
77
#include <vector>
88

9-
// In case you have a custom container, you can specialize `lz::custom_copier` to copy the elements to your container.
9+
// In case you have a custom container, you can specialize `lz::custom_copier_for` to copy the elements to your container.
1010
// This is useful if you have a custom container that requires a specific way of copying elements. You will need to specialize
11-
// `lz::custom_copier` for your custom container if all of the following are true:
11+
// `lz::custom_copier_for` for your custom container if all of the following are true:
1212
// - Your custom container does not have a `push_back` method
1313
// - Your custom container does not have an `insert` method
1414
// - Your custom container does not have an `insert_after` method (implicitly also requires `before_begin`)
@@ -28,9 +28,9 @@ class custom_container {
2828
}
2929
};
3030

31-
// Specialize `lz::custom_copier` for your custom container
31+
// Specialize `lz::custom_copier_for` for your custom container
3232
template<class T>
33-
struct lz::custom_copier<custom_container<T>> {
33+
struct lz::custom_copier_for<custom_container<T>> {
3434
template<class Iterable>
3535
void copy(Iterable&& iterable, custom_container<T>& container) const {
3636
// Copy the contents of the iterable to the container

include/Lz/basic_iterable.hpp

+54-51
Original file line numberDiff line numberDiff line change
@@ -98,43 +98,45 @@ class sized_iterable_impl : public lazy_view {
9898
using decayed_iterator = decay_t<I>;
9999
using decayed_sentinel = decay_t<S>;
100100

101-
using sentinel_type =
102-
conditional<std::is_same<decayed_iterator, decayed_sentinel>::value, take_iterator<decayed_iterator>, decayed_sentinel>;
103-
104-
take_iterator<decayed_iterator> _begin;
105-
sentinel_type _end;
106-
107-
using difference_type = diff_type<decayed_iterator>;
101+
I _begin;
102+
S _end;
103+
std::size_t _size{};
108104

109105
public:
110-
using iterator = take_iterator<decayed_iterator>;
106+
using iterator = take_iterator<decayed_iterator, S>;
111107
using const_iterator = decayed_iterator;
112108
using value_type = val_t<decayed_iterator>;
113-
using sentinel = sentinel_type;
109+
using sentinel = S;
114110

111+
private:
112+
using diff_type = typename iterator::difference_type;
113+
114+
public:
115115
constexpr sized_iterable_impl() = default;
116116

117117
template<class Cat = typename decayed_iterator::iterator_category, enable_if<is_ra_tag<Cat>::value, int> = 0>
118-
constexpr sized_iterable_impl(decayed_iterator begin, decayed_iterator end) :
119-
_begin{ std::move(begin), end - begin },
120-
_end{ std::move(end), 0 } {
118+
constexpr sized_iterable_impl(decayed_iterator begin, S end) :
119+
_begin{ begin },
120+
_end{ end },
121+
_size{ static_cast<std::size_t>(end - begin) } {
121122
}
122123

123124
template<class Cat = typename decayed_iterator::iterator_category, enable_if<!is_ra_tag<Cat>::value, int> = 0>
124-
constexpr sized_iterable_impl(decayed_iterator begin, decayed_sentinel end) = delete; // Must be random access to get size
125+
constexpr sized_iterable_impl(decayed_iterator begin,
126+
decayed_sentinel end) = delete; // Iterator must be random access to get size
125127

126-
constexpr sized_iterable_impl(decayed_iterator begin, std::size_t size) :
127-
_begin{ std::move(begin), static_cast<difference_type>(size) } {
128+
template<class Iterable, enable_if<sized<Iterable>::value, int> = 0>
129+
constexpr sized_iterable_impl(Iterable&& iterable) :
130+
_begin{ detail::begin(std::forward<Iterable>(iterable)) },
131+
_end{ detail::end(iterable) },
132+
_size{ lz::size(iterable) } {
128133
}
129134

130-
template<class It = decayed_iterator>
131-
LZ_NODISCARD constexpr enable_if<std::is_same<It, decayed_sentinel>::value, std::size_t> size() const {
132-
return static_cast<std::size_t>(begin()._n - end()._n) ;
133-
}
135+
template<class Iterable, enable_if<!sized<Iterable>::value, int> = 0>
136+
constexpr sized_iterable_impl(Iterable&& iterable) = delete; // Iterable must be sized
134137

135-
template<class It = decayed_iterator>
136-
LZ_NODISCARD constexpr enable_if<!std::is_same<It, decayed_sentinel>::value, std::size_t> size() const {
137-
return static_cast<std::size_t>(begin()._n);
138+
LZ_NODISCARD constexpr std::size_t size() const noexcept {
139+
return _size;
138140
}
139141

140142
template<class Rhs>
@@ -170,21 +172,21 @@ class sized_iterable_impl : public lazy_view {
170172
LZ_MODULE_EXPORT_SCOPE_BEGIN
171173

172174
/**
173-
* @brief A class that can be converted to any container. It is a view over an iterable, meaning it does not own the data. It
175+
* @brief A class that can be converted to any container. It only contains the iterators and
174176
* can be used in pipe expressions, converted to a container with `to<Container>()`, used in algorithms, for-each loops, etc...
175177
* It contains the size of the iterable.
176-
* @tparam It The iterator or iterable type.
177-
* @tparam S The sentinel type, if using an iterator/sentinel pair. Otherwise leave as is.
178+
* @tparam It The iterator type.
179+
* @tparam S The sentinel type.
178180
*/
179181
template<class It, class S = It>
180182
using sized_iterable = detail::sized_iterable_impl<It, S>;
181183

182184
/**
183-
* @brief A class that can be converted to any container. It is a view over an iterable, meaning it does not own the data. It
185+
* @brief A class that can be converted to any container. It only contains the iterators and
184186
* can be used in pipe expressions, converted to a container with `to<Container>()`, used in algorithms, for-each loops, etc...
185-
* It *may* contain the size of the iterable, depending on the iterator category or if the iterable is sized.
186-
* @tparam It The iterator or iterable type.
187-
* @tparam S The sentinel type, if using an iterator/sentinel pair. Otherwise leave as is.
187+
* It *may* contain the size of the iterable, depending on the iterator category (needs to be random access).
188+
* @tparam It The iterator type.
189+
* @tparam S The sentinel type.
188190
*/
189191
template<class It, class S = It>
190192
using basic_iterable = detail::basic_iterable_impl<It, S>;
@@ -229,10 +231,11 @@ struct has_push_back<Container, void_t<decltype(0, std::declval<Container>().pus
229231
: std::true_type {};
230232

231233
template<class Container, class = void>
232-
struct custom_copier : std::false_type {};
234+
struct custom_copier_for;
233235

234236
template<class Container>
235-
struct custom_copier<Container, void_t<decltype(0, std::declval<Container>().push(std::declval<typename Container::value_type>()))>> {
237+
struct custom_copier_for<Container,
238+
void_t<decltype(0, std::declval<Container>().push(std::declval<typename Container::value_type>()))>> {
236239
template<class Iterable>
237240
LZ_CONSTEXPR_CXX_20 void copy(Iterable&& iterable, Container& container) const {
238241
using ref = ref_iterable_t<Iterable>;
@@ -242,7 +245,7 @@ struct custom_copier<Container, void_t<decltype(0, std::declval<Container>().pus
242245

243246
// std::array doesnt have push_back, insert, insert_after... etc, so just use copy
244247
template<class T, std::size_t N>
245-
struct custom_copier<std::array<T, N>> {
248+
struct custom_copier_for<std::array<T, N>> {
246249
template<class Iterable>
247250
LZ_CONSTEXPR_CXX_20 void copy(Iterable&& iterable, std::array<T, N>& container) const {
248251
lz::copy(std::forward<Iterable>(iterable), container.begin());
@@ -305,10 +308,9 @@ template<class Iterable, class Container>
305308
LZ_CONSTEXPR_CXX_20
306309
enable_if<!has_push_back<Container>::value && !has_insert<Container>::value && !has_insert_after<Container>::value>
307310
copy_to_container(Iterable&& iterable, Container& container) {
308-
custom_copier<Container>{}.copy(std::forward<Iterable>(iterable), container);
311+
custom_copier_for<Container>{}.copy(std::forward<Iterable>(iterable), container);
309312
}
310313

311-
312314
template<class Container>
313315
struct container_constructor {
314316
template<class Container, LZ_CONCEPT_ITERABLE Iterable, class... Args>
@@ -421,8 +423,9 @@ struct iterable_formatter {
421423
std::string_view fmt{ format };
422424
std::string_view sep{ separator };
423425

426+
const auto empty_fmt_args = std::make_format_args();
424427
for (; begin != end; ++begin) {
425-
std::vformat_to(back_inserter, sep, std::make_format_args());
428+
std::vformat_to(back_inserter, sep, empty_fmt_args);
426429
std::vformat_to(back_inserter, fmt, std::make_format_args(*begin));
427430
}
428431

@@ -601,9 +604,9 @@ LZ_NODISCARD LZ_CONSTEXPR_CXX_14 Closure to(Args&&... args) {
601604
* auto list = lz::to<std::list<int>>(vec, std::allocator<int>{}); // { 1, 2, 3, 4, 5 }
602605
* // etc...
603606
* ```
604-
* In case you have a custom container, you can specialize `lz::custom_copier` to copy the elements to your container.
607+
* In case you have a custom container, you can specialize `lz::custom_copier_for` to copy the elements to your container.
605608
* This is useful if you have a custom container that requires a specific way of copying elements. You will need to specialize
606-
* `lz::custom_copier` for your custom container if all of the following are true:
609+
* `lz::custom_copier_for` for your custom container if all of the following are true:
607610
* - Your custom container does not have a `push_back` method
608611
* - Your custom container does not have an `insert` method
609612
* - Your custom container does not have an `insert_after` method (implicitly also requires `before_begin`)
@@ -623,9 +626,9 @@ LZ_NODISCARD LZ_CONSTEXPR_CXX_14 Closure to(Args&&... args) {
623626
* }
624627
* };
625628
*
626-
* // Specialize `lz::custom_copier` for your custom container
629+
* // Specialize `lz::custom_copier_for` for your custom container
627630
* template<class T>
628-
* struct lz::custom_copier<custom_container<T>> {
631+
* struct lz::custom_copier_for<custom_container<T>> {
629632
* template<class Iterable>
630633
* void copy(Iterable&& iterable, custom_container<T>& container) const {
631634
* // Copy the contents of the iterable to the container. Container is already reserved
@@ -660,9 +663,9 @@ LZ_NODISCARD constexpr Container to(Iterable&& iterable, Args&&... args) {
660663
* auto list = lz::to<std::list<int>>(vec, std::allocator<int>{}); // { 1, 2, 3, 4, 5 }
661664
* // etc...
662665
* ```
663-
* In case you have a custom container, you can specialize `lz::custom_copier` to copy the elements to your container.
666+
* In case you have a custom container, you can specialize `lz::custom_copier_for` to copy the elements to your container.
664667
* This is useful if you have a custom container that requires a specific way of copying elements. You will need to specialize
665-
* `lz::custom_copier` for your custom container if all of the following are true:
668+
* `lz::custom_copier_for` for your custom container if all of the following are true:
666669
* - Your custom container does not have a `push_back` method
667670
* - Your custom container does not have an `insert` method
668671
* - Your custom container does not have an `insert_after` method (implicitly also requires `before_begin`)
@@ -682,9 +685,9 @@ LZ_NODISCARD constexpr Container to(Iterable&& iterable, Args&&... args) {
682685
* }
683686
* };
684687
*
685-
* // Specialize `lz::custom_copier` for your custom container
688+
* // Specialize `lz::custom_copier_for` for your custom container
686689
* template<class T>
687-
* struct lz::custom_copier<custom_container<T>> {
690+
* struct lz::custom_copier_for<custom_container<T>> {
688691
* template<class Iterable>
689692
* void copy(Iterable&& iterable, custom_container<T>& container) const {
690693
* // Copy the contents of the iterable to the container. Container is already reserved
@@ -711,30 +714,30 @@ LZ_NODISCARD constexpr Cont to(Iterable&& iterable, Args&&... args) {
711714
* Example:
712715
* ```cpp
713716
* template<class T>
714-
* class MyContainer {
717+
* class my_container {
715718
* void my_inserter(T t) { ... }
716719
* };
717-
*
720+
*
718721
* template<class T>
719-
* lz::custom_copier<MyContainer<T>> {
722+
* lz::custom_copier_for<my_container<T>> {
720723
* template<class Iterable>
721-
* void copy(Iterable&& iterable, MyContainer<T>& container) {
724+
* void copy(Iterable&& iterable, my_container<T>& container) {
722725
* // Copy the iterable to the container, for example:
723-
* // Container is already reserved if it contains a reserve member function
726+
* // Container is already reserved if it contains a reserve member function and iterable is sized
724727
* for (auto&& i : iterable) {
725728
* container.my_inserter(i);
726729
* }
727730
* }
728731
* };
729-
*
732+
*
730733
* // or you can use enable if
731734
* template<class T>
732-
* lz::custom_copier<MyContainer<T>, std::enable_if_t<...>> {
735+
* lz::custom_copier_for<my_container<T>, std::enable_if_t<...>> {
733736
* // Same as above
734737
* };
735738
* ```
736739
*/
737-
using detail::custom_copier;
740+
using detail::custom_copier_for;
738741

739742
LZ_MODULE_EXPORT_SCOPE_END
740743

include/Lz/cached_size.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace lz {
1919
* - `lz::chunks`
2020
* - `lz::enumerate`
2121
* - `lz::exclude`
22+
* - `lz::take`
2223
* - `lz::take_every`
2324
* - `lz::zip_longest`
2425
* - `lz::zip`
@@ -40,6 +41,7 @@ namespace lz {
4041
* - lz::chunks
4142
* - lz::enumerate
4243
* - lz::exclude
44+
* - lz::take
4345
* - lz::take_every
4446
* - lz::zip_longest
4547
* - lz::zip
@@ -59,6 +61,7 @@ static constexpr detail::cached_size_adaptor cache_size{};
5961
* - `lz::chunks`
6062
* - `lz::enumerate`
6163
* - `lz::exclude`
64+
* - `lz::take`
6265
* - `lz::take_every`
6366
* - `lz::zip_longest`
6467
* - `lz::zip`
@@ -80,6 +83,7 @@ static constexpr detail::cached_size_adaptor cache_size{};
8083
* - lz::chunks
8184
* - lz::enumerate
8285
* - lz::exclude
86+
* - lz::take
8387
* - lz::take_every
8488
* - lz::zip_longest
8589
* - lz::zip

include/Lz/chunks.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ LZ_MODULE_EXPORT_SCOPE_BEGIN
2626
* - lz::chunks
2727
* - lz::enumerate
2828
* - lz::exclude
29+
* - lz::take
2930
* - lz::take_every
3031
* - lz::zip_longest
3132
* - lz::zip
@@ -57,6 +58,7 @@ static constexpr detail::chunks_adaptor detail::chunks_adaptor::chunks;
5758
* - lz::chunks
5859
* - lz::enumerate
5960
* - lz::exclude
61+
* - lz::take
6062
* - lz::take_every
6163
* - lz::zip_longest
6264
* - lz::zip

include/Lz/detail/adaptors/cached_size.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct cached_size_adaptor {
2626
* - `lz::chunks`
2727
* - `lz::enumerate`
2828
* - `lz::exclude`
29+
* - `lz::take`
2930
* - `lz::take_every`
3031
* - `lz::zip_longest`
3132
* - `lz::zip`

include/Lz/detail/adaptors/chunks.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ struct chunks_adaptor {
3333
* - lz::chunks
3434
* - lz::enumerate
3535
* - lz::exclude
36+
* - lz::take
3637
* - lz::take_every
3738
* - lz::zip_longest
3839
* - lz::zip
@@ -66,6 +67,7 @@ struct chunks_adaptor {
6667
* - lz::chunks
6768
* - lz::enumerate
6869
* - lz::exclude
70+
* - lz::take
6971
* - lz::take_every
7072
* - lz::zip_longest
7173
* - lz::zip

include/Lz/detail/adaptors/enumerate.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ struct enumerate_adaptor {
3838
* - lz::chunks
3939
* - lz::enumerate
4040
* - lz::exclude
41+
* - lz::take
4142
* - lz::take_every
4243
* - lz::zip_longest
4344
* - lz::zip
@@ -71,6 +72,7 @@ struct enumerate_adaptor {
7172
* - lz::chunks
7273
* - lz::enumerate
7374
* - lz::exclude
75+
* - lz::take
7476
* - lz::take_every
7577
* - lz::zip_longest
7678
* - lz::zip

include/Lz/detail/adaptors/exclude.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct exclude_adaptor {
3030
* - lz::chunks
3131
* - lz::enumerate
3232
* - lz::exclude
33+
* - lz::take
3334
* - lz::take_every
3435
* - lz::zip_longest
3536
* - lz::zip
@@ -61,6 +62,7 @@ struct exclude_adaptor {
6162
* - lz::chunks
6263
* - lz::enumerate
6364
* - lz::exclude
65+
* - lz::take
6466
* - lz::take_every
6567
* - lz::zip_longest
6668
* - lz::zip

0 commit comments

Comments
 (0)