14 template<
typename TChainInput>
15 class [[nodiscard(CXXITER_CHAINER_NODISCARD_WARNING)]] SkipN :
public IterApi<SkipN<TChainInput>> {
16 friend struct trait::Iterator<SkipN<TChainInput>>;
17 friend struct trait::ExactSizeIterator<SkipN<TChainInput>>;
18 friend struct trait::ContiguousMemoryIterator<SkipN<TChainInput>>;
22 bool skipEnded =
false;
24 constexpr SkipN(TChainInput&& input,
size_t n) : input(std::move(input)), n(n) {}
29 template<
typename TChainInput>
30 struct trait::Iterator<op::SkipN<TChainInput>> {
31 using ChainInputIterator = trait::Iterator<TChainInput>;
32 using InputItem =
typename TChainInput::Item;
34 using Self = op::SkipN<TChainInput>;
35 using Item = InputItem;
37 static constexpr inline IterValue<Item> next(Self& self) {
38 if(!self.skipEnded) [[unlikely]] {
39 ChainInputIterator::advanceBy(self.input, self.n);
40 self.skipEnded =
true;
42 return ChainInputIterator::next(self.input);
44 static constexpr inline SizeHint sizeHint(
const Self& self) {
45 SizeHint result = ChainInputIterator::sizeHint(self.input);
46 result.subtract(self.n);
49 static constexpr inline size_t advanceBy(Self& self,
size_t n) {
50 if(!self.skipEnded) [[unlikely]] {
51 size_t skippedCnt = ChainInputIterator::advanceBy(self.input, n + self.n);
52 self.skipEnded =
true;
53 if(skippedCnt <= self.n) {
return 0; }
54 return (skippedCnt - self.n);
56 return ChainInputIterator::advanceBy(self.input, n);
61 template<CXXIterExactSizeIterator TChainInput>
62 struct trait::ExactSizeIterator<op::SkipN<TChainInput>> {
63 static constexpr inline size_t size(
const op::SkipN<TChainInput>& self) {
64 return trait::Iterator<op::SkipN<TChainInput>>::sizeHint(self).lowerBound;
68 template<CXXIterContiguousMemoryIterator TChainInput>
69 struct trait::ContiguousMemoryIterator<op::SkipN<TChainInput>> {
70 using ItemPtr = std::add_pointer_t<std::remove_reference_t<typename op::SkipN<TChainInput>::Item>>;
71 static constexpr inline ItemPtr currentPtr(op::SkipN<TChainInput>& self) {
72 size_t offset = (self.skipEnded) ? 0 : self.n;
73 return (trait::ContiguousMemoryIterator<TChainInput>::currentPtr(self.input) + offset);