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
33 changes: 31 additions & 2 deletions include/rusty_iterators/interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ using iterator::Take;
using iterator::Zip;
using iterator::ZipLongest;

enum class Ordering : uint8_t
{
Equal,
Less,
Greater,
};

template <class T, class Derived>
class IterInterface
{
Expand All @@ -85,6 +92,9 @@ class IterInterface
requires AllFunctor<T, Functor>
[[nodiscard]] auto all(Functor&& f) -> bool;

template <class Other>
[[nodiscard]] auto cmp(Other&& it) -> Ordering;

[[nodiscard]] auto collect() -> std::vector<T>;
[[nodiscard]] auto count() -> size_t;

Expand Down Expand Up @@ -229,7 +239,7 @@ template <class Functor>
requires rusty_iterators::concepts::AnyFunctor<T, Functor>
auto rusty_iterators::interface::IterInterface<T, Derived>::any(Functor&& f) -> bool // NOLINT
{
auto anyf = [f = std::forward<Functor>(f)](auto acc, auto x) {
auto anyf = [f = std::forward<Functor>(f)](auto acc, auto x) -> std::expected<bool, bool> {
return f(x) ? std::expected<bool, bool>{true} : std::unexpected{false};
};
return self().tryFold(false, std::move(anyf));
Expand All @@ -240,12 +250,31 @@ template <class Functor>
requires rusty_iterators::concepts::AllFunctor<T, Functor>
auto rusty_iterators::interface::IterInterface<T, Derived>::all(Functor&& f) -> bool // NOLINT
{
auto allf = [f = std::forward<Functor>(f)](auto acc, auto x) {
auto allf = [f = std::forward<Functor>(f)](auto acc, auto x) -> std::expected<bool, bool> {
return !f(x) ? std::expected<bool, bool>{false} : std::unexpected{true};
};
return self().tryFold(true, std::move(allf));
}

template <class T, class Derived>
template <class Other>
auto rusty_iterators::interface::IterInterface<T, Derived>::cmp(Other&& it) -> Ordering
{
auto func = [](auto acc, auto x) -> std::expected<Ordering, Ordering> {
auto left = std::get<0>(x);
auto right = std::get<1>(x);

if (left > right)
return std::expected<Ordering, Ordering>{Ordering::Greater};

if (left < right)
return std::expected<Ordering, Ordering>{Ordering::Less};

return std::unexpected{Ordering::Equal};
};
return self().zipLongest(std::forward<Other>(it)).tryFold(Ordering::Equal, std::move(func));
}

template <class T, class Derived>
auto rusty_iterators::interface::IterInterface<T, Derived>::collect() -> std::vector<T>
{
Expand Down
51 changes: 51 additions & 0 deletions tests/iterator.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <rusty_iterators/iterator.hpp>

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

Expand Down Expand Up @@ -411,3 +412,53 @@ TEST(TestIterator, TestGetWithUbAndLb)

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

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

auto result = LazyIterator{v1}.cmp(LazyIterator{v2});

ASSERT_EQ(result, Ordering::Greater);
}

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

auto result = LazyIterator{v1}.cmp(LazyIterator{v2});

ASSERT_EQ(result, Ordering::Less);
}

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

auto result = LazyIterator{v1}.cmp(LazyIterator{v2});

ASSERT_EQ(result, Ordering::Equal);
}

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

auto result = LazyIterator{v1}.cmp(LazyIterator{v2});

ASSERT_EQ(result, Ordering::Less);
}

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

auto result = LazyIterator{v1}.cmp(LazyIterator{v2});

ASSERT_EQ(result, Ordering::Greater);
}