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
3 changes: 3 additions & 0 deletions include/rusty_iterators/concepts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ concept EqFunctor = requires(Functor f, std::tuple<T, T> t) {
{ f(t) } -> std::same_as<bool>;
};

template <class T, class Functor>
concept NeFunctor = EqFunctor<T, Functor>;

template <class T, class Functor>
concept FilterFunctor = AllFunctor<T&, Functor>;

Expand Down
25 changes: 25 additions & 0 deletions include/rusty_iterators/interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ using concepts::FoldFunctor;
using concepts::ForEachFunctor;
using concepts::Indexable;
using concepts::InspectFunctor;
using concepts::NeFunctor;
using concepts::PositionFunctor;
using concepts::Summable;
using concepts::TryFoldFunctor;
Expand Down Expand Up @@ -123,6 +124,14 @@ class IterInterface
[[nodiscard]] auto min() -> std::optional<R>;

[[nodiscard]] auto movingWindow(size_t size) -> MovingWindow<T, Derived>;

template <class Other>
[[nodiscard]] auto ne(Other&& it) -> bool;

template <class Other, class Functor>
requires NeFunctor<T, Functor>
[[nodiscard]] auto neBy(Other&& it, Functor&& f) -> bool;

[[nodiscard]] auto nth(size_t element) -> std::optional<T>;

template <class Functor>
Expand Down Expand Up @@ -355,6 +364,22 @@ auto rusty_iterators::interface::IterInterface<T, Derived>::movingWindow(size_t
return MovingWindow<T, Derived>{std::forward<Derived>(self()), size};
}

template <class T, class Derived>
template <class Other>
auto rusty_iterators::interface::IterInterface<T, Derived>::ne(Other&& it) -> bool
{
return self().neBy(std::forward<Other>(it),
[](auto x) { return std::get<0>(x) != std::get<1>(x); });
}

template <class T, class Derived>
template <class Other, class Functor>
requires rusty_iterators::concepts::NeFunctor<T, Functor>
auto rusty_iterators::interface::IterInterface<T, Derived>::neBy(Other&& it, Functor&& f) -> bool
{
return self().zip(std::forward<Other>(it)).any(std::forward<Functor>(f));
}

template <class T, class Derived>
auto rusty_iterators::interface::IterInterface<T, Derived>::nth(size_t element) -> std::optional<T>
{
Expand Down
28 changes: 28 additions & 0 deletions tests/iterator.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,34 @@ TEST(TestIterator, TestEqBy)
ASSERT_TRUE(result);
}

TEST(TestIterator, TestNeWhenSame)
{
auto v1 = std::vector{1, 2, 3};
auto v2 = std::vector{1, 2, 3};

ASSERT_FALSE(LazyIterator{v1}.ne(LazyIterator{v2}));
}

TEST(TestIterator, TestNeWhenDifferent)
{
auto v1 = std::vector{1, 2, 3};
auto v2 = std::vector{1, 2, 4};

ASSERT_TRUE(LazyIterator{v1}.ne(LazyIterator{v2}));
}

TEST(TestIterator, TestNeBy)
{
auto v1 = std::vector<std::string>{"a", "bc", "def"};
auto v2 = std::vector<std::string>{"d", "jk", "cgde"};

auto result = LazyIterator{v1}.neBy(LazyIterator{v2}, [](auto x) {
return std::get<0>(x).get().size() != std::get<1>(x).get().size();
});

ASSERT_TRUE(result);
}

TEST(TestIterator, TestTryFoldEarlyExit)
{
auto vec = std::vector{1, 2, 3, 4, 5};
Expand Down