Skip to content

Commit

Permalink
refs #121 Слияние разнотипных диапазонов
Browse files Browse the repository at this point in the history
  • Loading branch information
izvolov committed Jul 24, 2020
1 parent c969f30 commit a9edfc2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
12 changes: 6 additions & 6 deletions include/burst/iterator/merge_iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <burst/container/access/front.hpp>
#include <burst/functional/each.hpp>
#include <burst/functional/invert.hpp>
#include <burst/iterator/detail/uniform_range_tuple_please.hpp>
#include <burst/iterator/end_tag.hpp>
#include <burst/range/make_range_vector.hpp>
#include <burst/range/own_as_range.hpp>
Expand All @@ -21,6 +22,7 @@
#include <algorithm>
#include <functional>
#include <iterator>
#include <tuple>
#include <utility>

namespace burst
Expand Down Expand Up @@ -206,10 +208,11 @@ namespace burst
template <typename ... Ranges, typename Compare>
auto make_merge_iterator (std::tuple<Ranges &...> ranges, Compare compare)
{
auto common_ranges = detail::uniform_range_tuple_please(ranges);
return
make_merge_iterator
(
burst::own_as_range(burst::apply(burst::make_range_vector, ranges)),
burst::own_as_range(burst::apply(burst::make_range_vector, common_ranges)),
std::move(compare)
);
}
Expand Down Expand Up @@ -252,11 +255,8 @@ namespace burst
template <typename ... Ranges>
auto make_merge_iterator (std::tuple<Ranges &...> ranges)
{
return
make_merge_iterator
(
burst::own_as_range(burst::apply(burst::make_range_vector, ranges))
);
auto common_ranges = detail::uniform_range_tuple_please(ranges);
return make_merge_iterator(own_as_range(burst::apply(make_range_vector, common_ranges)));
}

//! Функция для создания итератора на конец слияния с предикатом.
Expand Down
12 changes: 12 additions & 0 deletions test/burst/range/adaptor/merged.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,16 @@ TEST_SUITE("merged")
const auto expected = {5, 3, 3, 2, 1, 1};
CHECK(merged == expected);
}

TEST_CASE("Может работать с кортежем диапазонов")
{
const auto first = burst::make_vector({1, 4, 7});
const auto second = burst::make_vector({2, 5, 8});
const auto third = burst::make_forward_list({3, 6, 9});

const auto merged = std::tie(first, second, third) | burst::merged;

const auto expected = {1, 2, 3, 4, 5, 6, 7, 8, 9};
CHECK(merged == expected);
}
}
25 changes: 25 additions & 0 deletions test/burst/range/merge.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <burst/container/make_deque.hpp>
#include <burst/container/make_list.hpp>
#include <burst/container/make_vector.hpp>
#include <burst/range/make_range_vector.hpp>
Expand Down Expand Up @@ -83,4 +84,28 @@ TEST_SUITE("merge")
auto expected_collection = {0, 1, 2, 3, 4, 5, 6, 7, 8};
CHECK(merged_range == expected_collection);
}

TEST_CASE("Можно сливать диапазоны разных типов")
{
auto one = burst::make_vector({0, 3, 6, 9});
auto two = burst::make_list({1, 4, 7, 10});
auto three = burst::make_deque({2, 5, 8, 11});

auto merged_range = burst::merge(std::tie(one, two, three));

auto expected_collection = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
CHECK(merged_range == expected_collection);
}

TEST_CASE("Можно сливать с предикатом диапазоны разных типов")
{
auto one = burst::make_vector({9, 6, 3, 0});
auto two = burst::make_list({10, 7, 4, 1});
auto three = burst::make_deque({11, 8, 5, 2});

auto merged_range = burst::merge(std::tie(one, two, three), std::greater<>{});

auto expected_collection = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
CHECK(merged_range == expected_collection);
}
}

0 comments on commit a9edfc2

Please sign in to comment.