Skip to content

Commit 1e702cf

Browse files
feat: add ne and neBy to iterator interface (#92)
1 parent 3e30ee3 commit 1e702cf

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

include/rusty_iterators/concepts.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ concept EqFunctor = requires(Functor f, std::tuple<T, T> t) {
2525
{ f(t) } -> std::same_as<bool>;
2626
};
2727

28+
template <class T, class Functor>
29+
concept NeFunctor = EqFunctor<T, Functor>;
30+
2831
template <class T, class Functor>
2932
concept FilterFunctor = AllFunctor<T&, Functor>;
3033

include/rusty_iterators/interface.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ using concepts::FoldFunctor;
2727
using concepts::ForEachFunctor;
2828
using concepts::Indexable;
2929
using concepts::InspectFunctor;
30+
using concepts::NeFunctor;
3031
using concepts::PositionFunctor;
3132
using concepts::Summable;
3233
using concepts::TryFoldFunctor;
@@ -123,6 +124,14 @@ class IterInterface
123124
[[nodiscard]] auto min() -> std::optional<R>;
124125

125126
[[nodiscard]] auto movingWindow(size_t size) -> MovingWindow<T, Derived>;
127+
128+
template <class Other>
129+
[[nodiscard]] auto ne(Other&& it) -> bool;
130+
131+
template <class Other, class Functor>
132+
requires NeFunctor<T, Functor>
133+
[[nodiscard]] auto neBy(Other&& it, Functor&& f) -> bool;
134+
126135
[[nodiscard]] auto nth(size_t element) -> std::optional<T>;
127136

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

367+
template <class T, class Derived>
368+
template <class Other>
369+
auto rusty_iterators::interface::IterInterface<T, Derived>::ne(Other&& it) -> bool
370+
{
371+
return self().neBy(std::forward<Other>(it),
372+
[](auto x) { return std::get<0>(x) != std::get<1>(x); });
373+
}
374+
375+
template <class T, class Derived>
376+
template <class Other, class Functor>
377+
requires rusty_iterators::concepts::NeFunctor<T, Functor>
378+
auto rusty_iterators::interface::IterInterface<T, Derived>::neBy(Other&& it, Functor&& f) -> bool
379+
{
380+
return self().zip(std::forward<Other>(it)).any(std::forward<Functor>(f));
381+
}
382+
358383
template <class T, class Derived>
359384
auto rusty_iterators::interface::IterInterface<T, Derived>::nth(size_t element) -> std::optional<T>
360385
{

tests/iterator.test.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,34 @@ TEST(TestIterator, TestEqBy)
319319
ASSERT_TRUE(result);
320320
}
321321

322+
TEST(TestIterator, TestNeWhenSame)
323+
{
324+
auto v1 = std::vector{1, 2, 3};
325+
auto v2 = std::vector{1, 2, 3};
326+
327+
ASSERT_FALSE(LazyIterator{v1}.ne(LazyIterator{v2}));
328+
}
329+
330+
TEST(TestIterator, TestNeWhenDifferent)
331+
{
332+
auto v1 = std::vector{1, 2, 3};
333+
auto v2 = std::vector{1, 2, 4};
334+
335+
ASSERT_TRUE(LazyIterator{v1}.ne(LazyIterator{v2}));
336+
}
337+
338+
TEST(TestIterator, TestNeBy)
339+
{
340+
auto v1 = std::vector<std::string>{"a", "bc", "def"};
341+
auto v2 = std::vector<std::string>{"d", "jk", "cgde"};
342+
343+
auto result = LazyIterator{v1}.neBy(LazyIterator{v2}, [](auto x) {
344+
return std::get<0>(x).get().size() != std::get<1>(x).get().size();
345+
});
346+
347+
ASSERT_TRUE(result);
348+
}
349+
322350
TEST(TestIterator, TestTryFoldEarlyExit)
323351
{
324352
auto vec = std::vector{1, 2, 3, 4, 5};

0 commit comments

Comments
 (0)