based

Opinionated utility library
git clone git://git.dimitrijedobrota.com/based.git
Log | Files | Refs | README | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING

commit428c88f6d0e18918cc824d0d097ec1a71b354704
parent0555ffbfda572bdf91a725d63371a1f1f60fa599
authorDimitrije Dobrota <mail@dimitrijedobrota.com>
dateFri, 11 Apr 2025 19:07:36 +0200

find_adjacent_mismatch, with tests

Diffstat:
Minclude/based/algorithm.hpp|+++++++++++++++++++++-
Mtest/source/find_mismatch_test.cpp|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

2 files changed, 84 insertions(+), 1 deletions(-)


diff --git a/include/based/algorithm.hpp b/include/based/algorithm.hpp

@@ -389,6 +389,25 @@ auto find_mismatch(I0 f0, I0 d0, I1 f1, I1 d1, R r)

return std::make_pair(f0, f1);
}
template<ReadableIterator I, IterRelation<I> R>
I find_adjacent_mismatch(I f, I d, R r)
{
// Precondition: bounded_range(f,d)
if (f == d) {
return d;
}
auto x = *f;
f = successor(f);
while (f != d && r(x, *f)) {
x = *f;
f = successor(f);
}
return f;
}
/* ----- Counted Range Algorithms ----- */
template<ReadableIterator I, IterUnaryProcedure<I> Proc>

@@ -589,7 +608,8 @@ auto find_mismatch_m(I0 f0, I0 d0, I1 f1, iter_dist_t<I1> n1, R r)

template<ReadableIterator I0, ReadableIterator I1, IterRelation<I0> R>
requires SameAs<iter_value_t<I0>, iter_value_t<I1>>
auto find_mismatch_n_m(I0 f0, iter_dist_t<I0> n0, I1 f1, iter_dist_t<I1> n1, R r)
auto find_mismatch_n_m(
I0 f0, iter_dist_t<I0> n0, I1 f1, iter_dist_t<I1> n1, R r)
{
// Precondition: readable_weak_range(f0,n0)
// Precondition: readable_weak_range(f1,n1)

diff --git a/test/source/find_mismatch_test.cpp b/test/source/find_mismatch_test.cpp

@@ -113,3 +113,66 @@ TEST_CASE("find_mismatch(mismatch)", "[algorithm/find_mismatch]")

REQUIRE(itr0 == std::next(std::begin(arr0), 2));
REQUIRE(itr1 == std::next(std::begin(arr1), 2));
}
TEST_CASE("find_adjacent_mismatch(empty)", "[algorithm/find_adjacent_mismatch]")
{
std::array<int, 0> arr = {};
const auto* itr =
based::find_adjacent_mismatch(std::begin(arr), std::end(arr), equal {});
REQUIRE(itr == std::end(arr));
}
TEST_CASE("find_adjacent_mismatch(one)", "[algorithm/find_adjacent_mismatch]")
{
std::array arr = {0};
const auto* itr =
based::find_adjacent_mismatch(std::begin(arr), std::end(arr), equal {});
REQUIRE(itr == std::end(arr));
}
TEST_CASE("find_adjacent_mismatch(two equal)",
"[algorithm/find_adjacent_mismatch]")
{
std::array arr = {0, 0};
const auto* itr =
based::find_adjacent_mismatch(std::begin(arr), std::end(arr), equal {});
REQUIRE(itr == std::end(arr));
}
TEST_CASE("find_adjacent_mismatch(two nonequal)",
"[algorithm/find_adjacent_mismatch]")
{
std::array arr = {0, 1};
const auto* itr =
based::find_adjacent_mismatch(std::begin(arr), std::end(arr), equal {});
REQUIRE(itr == std::next(std::begin(arr), 1));
}
TEST_CASE("find_adjacent_mismatch(equal)", "[algorithm/find_adjacent_mismatch]")
{
std::array arr = {0, 0, 0, 0, 0, 0};
const auto* itr =
based::find_adjacent_mismatch(std::begin(arr), std::end(arr), equal {});
REQUIRE(itr == std::end(arr));
}
TEST_CASE("find_adjacent_mismatch(nonequal)",
"[algorithm/find_adjacent_mismatch]")
{
std::array arr = {0, 0, 0, 0, 1, 1, 1, 1};
const auto* itr =
based::find_adjacent_mismatch(std::begin(arr), std::end(arr), equal {});
REQUIRE(itr == std::next(std::begin(arr), 4));
}