Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions include/rusty_iterators/interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "inspect.hpp"
#include "map.hpp"
#include "moving_window.hpp"
#include "skip.hpp"
#include "take.hpp"
#include "zip.hpp"

Expand Down Expand Up @@ -45,6 +46,7 @@ using iterator::FilterMap;
using iterator::Inspect;
using iterator::Map;
using iterator::MovingWindow;
using iterator::Skip;
using iterator::Take;
using iterator::Zip;

Expand Down Expand Up @@ -149,6 +151,8 @@ class IterInterface
requires ReduceFunctor<T, Functor>
[[nodiscard]] auto reduce(Functor&& f) -> std::optional<T>;

[[nodiscard]] auto skip(size_t n) -> Skip<T, Derived>;

template <class R = T>
requires Summable<R>
[[nodiscard]] auto sum() -> R;
Expand Down Expand Up @@ -443,6 +447,12 @@ auto rusty_iterators::interface::IterInterface<T, Derived>::reduce(Functor&& f)
return fold(std::move(first.value()), std::forward<Functor>(f));
}

template <class T, class Derived>
auto rusty_iterators::interface::IterInterface<T, Derived>::skip(size_t n) -> Skip<T, Derived>
{
return Skip<T, Derived>{std::forward<Derived>(self()), n};
}

template <class T, class Derived>
template <class R>
requires rusty_iterators::concepts::Summable<R>
Expand Down
50 changes: 50 additions & 0 deletions include/rusty_iterators/skip.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once

#include "interface.fwd.hpp"

#include <algorithm>
#include <optional>

namespace rusty_iterators::iterator
{
using interface::IterInterface;

template <class T, class Other>
class Skip : public IterInterface<T, Skip<T, Other>>
{
public:
explicit Skip(Other&& it, size_t n) : it(std::forward<Other>(it)), n(n) {}

auto next() -> std::optional<T>;
[[nodiscard]] auto sizeHint() const -> std::optional<size_t>;

private:
Other it;
size_t n;
};
} // namespace rusty_iterators::iterator

template <class T, class Other>
auto rusty_iterators::iterator::Skip<T, Other>::next() -> std::optional<T>
{
[[unlikely]] if (n > 0)
{
it = it.advanceBy(n);
n = 0;
return std::move(it.next());
}
return std::move(it.next());
}

template <class T, class Other>
auto rusty_iterators::iterator::Skip<T, Other>::sizeHint() const -> std::optional<size_t>
{
auto underlyingSize = it.sizeHint();

if (!underlyingSize.has_value())
{
return std::nullopt;
}
int32_t estimatedSize = underlyingSize.value() - n;
return std::max(0, estimatedSize);
}
57 changes: 57 additions & 0 deletions tests/skip.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <rusty_iterators/iterator.hpp>

using ::rusty_iterators::iterator::LazyIterator;
using ::testing::ElementsAreArray;

TEST(TestSkipIterator, NextSkipsElements)
{
auto vec = std::vector{1, 2, 3, 4, 5};
auto it = LazyIterator{vec}.skip(3);

ASSERT_EQ(it.next(), 4);
ASSERT_EQ(it.next(), 5);
ASSERT_EQ(it.next(), std::nullopt);
}

TEST(TestSkipIterator, NextOnTooSmallIterator)
{
auto vec = std::vector{1, 2, 3};
auto it = LazyIterator{vec}.skip(5);

ASSERT_EQ(it.next(), std::nullopt);
}

TEST(TestSkipIterator, CollectAllItemsExceptSkippedOnes)
{
auto vec = std::vector{1, 2, 3, 4, 5};
auto it = LazyIterator{vec}.skip(3);

EXPECT_THAT(it.collect(), ElementsAreArray({4, 5}));
}

TEST(TestSkipIterator, SizeHintGivesEstimatedSize)
{
auto vec = std::vector{1, 2, 3, 4, 5};
auto it = LazyIterator{vec}.skip(3);

ASSERT_EQ(it.sizeHint(), 2);
}

TEST(TestSkipIterator, SizeHintOnInfiniteIterator)
{
auto vec = std::vector{1, 2, 3};
auto it = LazyIterator{vec}.cycle().skip(3);

ASSERT_EQ(it.sizeHint(), std::nullopt);
}

TEST(TestSkipIterator, TestSizeHintOnTooSmallIterator)
{
auto vec = std::vector{1, 2, 3};
auto it = LazyIterator{vec}.skip(10);

ASSERT_EQ(it.sizeHint(), 0);
}