Commit e736618d authored by Michaël Roynard's avatar Michaël Roynard
Browse files

Migrate generate

Migrate for_each
parent 0538832b
......@@ -317,11 +317,11 @@ void for_each_baseline(mln::image2d<mln::rgb8>& in)
}
void for_each(mln::image2d<mln::uint8>& in)
{
mln::experimental::for_each(in, plus_one_inplace);
mln::for_each(in, plus_one_inplace);
}
void for_each(mln::image2d<mln::rgb8>& in)
{
mln::experimental::for_each(in, plus_one_inplace);
mln::for_each(in, plus_one_inplace);
}
......@@ -338,7 +338,7 @@ void generate_baseline(mln::image2d<mln::uint8>& ima)
}
void generate(mln::image2d<mln::uint8>& ima)
{
mln::experimental::generate(ima, iota_generator);
mln::generate(ima, iota_generator);
}
......
......@@ -7,32 +7,22 @@
namespace mln
{
namespace experimental
{
/// \brief Apply a function on the values of an image.
template <class InputImage, class UnaryFunction>
void for_each(InputImage in, UnaryFunction f);
} // namespace experimental
/// \brief Apply a function on the values of an image.
template <class InputImage, class UnaryFunction>
void for_each(InputImage in, UnaryFunction f);
/******************************************/
/**** Implementation ****/
/******************************************/
namespace experimental
template <class InputImage, class UnaryFunction>
void for_each(InputImage input, UnaryFunction f)
{
template <class InputImage, class UnaryFunction>
void for_each(InputImage input, UnaryFunction f)
{
static_assert(mln::is_a<InputImage, Image>());
static_assert(::ranges::Invocable<UnaryFunction, image_reference_t<InputImage>>());
static_assert(mln::is_a<InputImage, experimental::Image>());
static_assert(::ranges::Invocable<UnaryFunction, image_reference_t<InputImage>>());
auto&& vals = input.new_values();
for (auto r : ranges::rows(vals))
::ranges::for_each(r, f);
}
} // namespace experimental
auto&& vals = input.new_values();
for (auto r : ranges::rows(vals))
::ranges::for_each(r, f);
}
} // namespace mln
......@@ -5,31 +5,23 @@
namespace mln
{
namespace experimental
{
template <class OutputImage, class Generator>
void generate(OutputImage output, Generator g);
}
template <class OutputImage, class Generator>
void generate(OutputImage output, Generator g);
/******************************************/
/**** Implementation ****/
/******************************************/
namespace experimental
template <class OutputImage, class Generator>
void generate(OutputImage output, Generator g)
{
template <class OutputImage, class Generator>
void generate(OutputImage output, Generator g)
{
static_assert(mln::is_a<OutputImage, Image>());
static_assert(std::is_convertible_v<std::invoke_result_t<Generator>, image_value_t<OutputImage>>);
static_assert(mln::is_a<OutputImage, experimental::Image>());
static_assert(std::is_convertible_v<std::invoke_result_t<Generator>, image_value_t<OutputImage>>);
auto&& vals = output.new_values();
for (auto row : mln::ranges::rows(vals))
for (auto& v : row)
v = g();
}
} // namespace experimental
auto&& vals = output.new_values();
for (auto row : mln::ranges::rows(vals))
for (auto& v : row)
v = g();
}
} // namespace mln
......@@ -3,15 +3,15 @@
#include <mln/core/algorithm/for_each.hpp>
#include <mln/core/image/image.hpp>
#include <mln/core/rangev3/foreach.hpp>
#include <mln/core/trace.hpp>
#include <mln/core/value/indexer.hpp>
#include <mln/core/value/value_traits.hpp>
#include <mln/core/trace.hpp>
#include <range/v3/utility/functional.hpp>
#include <range/v3/range_concepts.hpp>
#include <vector>
#include <algorithm>
#include <numeric>
#include <range/v3/range_concepts.hpp>
#include <range/v3/utility/functional.hpp>
#include <vector>
namespace mln::experimental
{
......@@ -43,11 +43,11 @@ namespace mln::experimental
void sort_compute_cumulated_histogram(I& input, int* histogram, std::size_t /* nvalues */, Proj proj)
{
// Comute histogram
mln::experimental::for_each(input, [histogram, proj](image_value_t<I> v) { ++histogram[proj(v)]; });
mln::for_each(input, [histogram, proj](image_value_t<I> v) { ++histogram[proj(v)]; });
// Sum up values
// This is what we should use after getting ride of indexer (and use value set / projection)
//std::partial_sum(histogram, histogram + nvalues, histogram);
// std::partial_sum(histogram, histogram + nvalues, histogram);
using proj_value_t = typename Proj::index_type;
......@@ -55,7 +55,7 @@ namespace mln::experimental
auto i = value_traits<proj_value_t>::min();
do
{
int tmp = histogram[i];
int tmp = histogram[i];
histogram[i] = count;
count += tmp;
} while (i++ < value_traits<proj_value_t>::max());
......@@ -66,17 +66,19 @@ namespace mln::experimental
{
mln_entering("mln::sort (counting sort)");
using projecter_t = indexer<image_value_t<I>, Compare>;
using projecter_t = indexer<image_value_t<I>, Compare>;
using proj_value_t = typename projecter_t::index_type;
constexpr std::size_t nvalues = 1 << value_traits<proj_value_t>::quant;
projecter_t proj;
int histogram[nvalues] = {0, };
int histogram[nvalues] = {
0,
};
sort_compute_cumulated_histogram(input, histogram, nvalues, proj);
auto out = ::ranges::begin(rng);
mln_foreach_new(auto px, input.new_pixels())
mln_foreach_new (auto px, input.new_pixels())
{
std::ptrdiff_t pos = histogram[proj(px.val())]++;
if constexpr (use_p)
......@@ -87,7 +89,6 @@ namespace mln::experimental
}
template <bool use_p, class I, class OutputRange, class Compare>
void sort_by_quicksort(I& input, OutputRange& rng, Compare cmp)
{
......@@ -96,7 +97,7 @@ namespace mln::experimental
// Copy to container
{
auto it = ::ranges::begin(rng);
mln_foreach_new(auto px, input.new_pixels())
mln_foreach_new (auto px, input.new_pixels())
{
if constexpr (use_p)
*it = px.point();
......@@ -108,14 +109,15 @@ namespace mln::experimental
// sort
if constexpr (use_p) // By point
std::sort(::ranges::begin(rng), ::ranges::end(rng), [&input, cmp](const auto& x, const auto& y) { return cmp(input(x), input(y)); });
std::sort(::ranges::begin(rng), ::ranges::end(rng),
[&input, cmp](const auto& x, const auto& y) { return cmp(input(x), input(y)); });
else
std::sort(::ranges::begin(rng), ::ranges::end(rng), [&input, cmp](auto x, auto y) { return cmp(input[x], input[y]); });
std::sort(::ranges::begin(rng), ::ranges::end(rng),
[&input, cmp](auto x, auto y) { return cmp(input[x], input[y]); });
}
}
} // namespace impl
template <class InputImage, class R, class Compare>
......
......@@ -15,7 +15,7 @@ TEST(Core, Algorithm_For_Each)
mln::image2d<uint8_t> ref = {{2, 3, 4}, {5, 6, 7}};
mln::experimental::for_each(ima, [](uint8_t& x) { x += 1; });
mln::for_each(ima, [](uint8_t& x) { x += 1; });
ASSERT_TRUE(mln::all_of(ima == ref));
}
......@@ -29,6 +29,6 @@ TEST(Core, Algorithm_For_Each_Side_Effects)
int sum = 0;
auto g = [&sum](uint8_t v) { sum += v; };
mln::experimental::for_each(ima, g);
mln::for_each(ima, g);
ASSERT_EQ(6 * 7 / 2, sum);
}
......@@ -14,7 +14,7 @@ TEST(Core, Algorithm_Generate)
auto gen = [&i]() { return i++; };
mln::image2d<uint8_t> out(3, 4);
mln::experimental::generate(out, gen);
mln::generate(out, gen);
mln::image2d<uint8_t> ref = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment