CXXIter 0.2
Loading...
Searching...
No Matches
TakeN.h
1#pragma once
2
3#include <utility>
4
5#include "../Common.h"
6
7namespace CXXIter {
8
9 // ################################################################################################
10 // TAKE WHILE
11 // ################################################################################################
12 namespace op {
14 template<typename TChainInput>
15 class [[nodiscard(CXXITER_CHAINER_NODISCARD_WARNING)]] TakeN : public IterApi<TakeN<TChainInput>> {
16 friend struct trait::Iterator<TakeN<TChainInput>>;
17 private:
18 TChainInput input;
19 size_t n;
20 size_t remaining;
21 public:
22 constexpr TakeN(TChainInput&& input, size_t n) : input(std::move(input)), n(n), remaining(n) {}
23 };
24 }
25 // ------------------------------------------------------------------------------------------------
27 template<typename TChainInput>
28 struct trait::Iterator<op::TakeN<TChainInput>> {
29 using ChainInputIterator = trait::Iterator<TChainInput>;
30 using InputItem = typename TChainInput::Item;
31 // CXXIter Interface
32 using Self = op::TakeN<TChainInput>;
33 using Item = InputItem;
34
35 static constexpr inline IterValue<Item> next(Self& self) {
36 if(self.remaining == 0) [[unlikely]] { return {}; }
37 auto item = ChainInputIterator::next(self.input);
38 if(!item.has_value()) [[unlikely]] { return {}; }
39 self.remaining -= 1;
40 return item;
41 }
42 static constexpr inline SizeHint sizeHint(const Self& self) {
43 SizeHint input = ChainInputIterator::sizeHint(self.input);
44 return SizeHint(
45 std::min(input.lowerBound, self.n),
46 SizeHint::upperBoundMin(input.upperBound, self.n)
47 );
48 }
49 static constexpr inline size_t advanceBy(Self& self, size_t n) {
50 size_t skipN = ChainInputIterator::advanceby(self.input, n);
51 if(self.n < skipN) { self.n = 0; } else { self.n -= skipN; }
52 return skipN;
53 }
54 };
56 template<CXXIterExactSizeIterator TChainInput>
57 struct trait::ExactSizeIterator<op::TakeN<TChainInput>> {
58 static constexpr inline size_t size(const op::TakeN<TChainInput>& self) {
59 return trait::Iterator<op::TakeN<TChainInput>>::sizeHint(self).lowerBound;
60 }
61 };
62}
CXXIter.
Definition: CXXIter.h:48