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 @@ -10,6 +10,7 @@
#include "map.hpp"
#include "moving_window.hpp"
#include "skip.hpp"
#include "step_by.hpp"
#include "take.hpp"
#include "zip.hpp"

Expand Down Expand Up @@ -47,6 +48,7 @@ using iterator::Inspect;
using iterator::Map;
using iterator::MovingWindow;
using iterator::Skip;
using iterator::StepBy;
using iterator::Take;
using iterator::Zip;

Expand Down Expand Up @@ -152,6 +154,7 @@ class IterInterface
[[nodiscard]] auto reduce(Functor&& f) -> std::optional<T>;

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

template <class R = T>
requires Summable<R>
Expand Down Expand Up @@ -452,6 +455,13 @@ auto rusty_iterators::interface::IterInterface<T, Derived>::skip(size_t n) -> Sk
return Skip<T, Derived>{std::forward<Derived>(self()), n};
}

template <class T, class Derived>
auto rusty_iterators::interface::IterInterface<T, Derived>::stepBy(size_t step)
-> StepBy<T, Derived>
{
return StepBy<T, Derived>{std::forward<Derived>(self()), step};
}

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

#include "interface.fwd.hpp"

#include <optional>
#include <stdexcept>

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

template <class T, class Other>
class StepBy : public IterInterface<T, StepBy<T, Other>>
{
public:
explicit StepBy(Other&& it, size_t step) : it(std::forward<Other>(it))
{
if (step == 0)
throw std::length_error{"Step has to be greater than zero."};

stepMinusOne = step - 1;
}

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

private:
Other it;
size_t stepMinusOne;
bool firstTake = true;

[[nodiscard]] inline auto originalStep() const -> size_t { return stepMinusOne + 1; }

[[nodiscard]] inline auto firstSize(size_t underlyingSize) const -> size_t
{
auto step = originalStep();
return underlyingSize == 0 ? 0 : 1 + (underlyingSize - 1) / step;
}

[[nodiscard]] inline auto otherSize(size_t underlyingSize) const -> size_t
{
auto step = originalStep();
return underlyingSize / step;
}
};
} // namespace rusty_iterators::iterator

template <class T, class Other>
auto rusty_iterators::iterator::StepBy<T, Other>::next() -> std::optional<T>
{
auto stepSize = firstTake ? 0 : stepMinusOne;
firstTake = false;

return std::move(it.nth(stepSize));
}

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

if (!underlyingSize.has_value())
return std::nullopt;

if (firstTake)
return firstSize(underlyingSize.value());

return otherSize(underlyingSize.value());
}
69 changes: 69 additions & 0 deletions tests/step_by.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <rusty_iterators/iterator.hpp>
#include <stdexcept>

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

TEST(TestStepByIterator, ThrowWhenSizeIsZero)
{
auto vec = std::vector{1, 2, 3};

EXPECT_THROW(auto _ = LazyIterator{vec}.stepBy(0), std::length_error);
}

TEST(TestStepByIterator, NextReturnsFirstElement)
{
auto vec = std::vector{1, 2, 3, 4, 5};
auto it = LazyIterator{vec}.stepBy(2);

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

TEST(TestStepByIterator, NextWhenStepLargerThanIterator)
{
auto vec = std::vector{1, 2, 3};
auto it = LazyIterator{vec}.stepBy(10);

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

TEST(TestStepByIterator, TestCollectIterator)
{
auto vec = std::vector{1, 2, 3, 4, 5};
auto it = LazyIterator{vec}.stepBy(2);

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

TEST(TestStepByIterator, TestSizeHintWhenUnderlyingInfinite)
{
auto vec = std::vector{1, 2};
auto it = LazyIterator{vec}.cycle().stepBy(3);

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

TEST(TestStepByIterator, TestSizeHintFirstTake)
{
auto vec = std::vector{1, 2, 3, 4, 5};
auto it = LazyIterator{vec}.stepBy(2);

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

TEST(TestStepByIterator, TestSizeHintOtherTake)
{
auto vec = std::vector{1, 2, 3, 4, 5};
auto it = LazyIterator{vec}.stepBy(2);

it.next();

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