apmccartney apmccartney -4 years ago 141
C++ Question

Split range into range of overlapping ranges

I attempting to use the Ranges-V3 library to slice up an container of values into a range of ranges such that neighboring ranges share boundary elements.

Consider the following:

using namespace ranges;

std::vector<int> v = { 1, 2, 3, 0, 4, 0, 5, 0, 6, 7, 8, 0, 0, 9 };
auto myRanges = v | /* something like adjacent split */
for_each( myRanges, []( auto&& range ){ std::cout << range << std::endl;} );


I would like to divide the range into overlapping subranges based whether the region fullfills two criteria:


  1. whether the element has a value of zero

  2. or is adjacent to one or more elements with a value of zero



Desired output:

[1,2,3]
[3,0,4,0,5,0,6]
[6,7,8]
[8,0,0,9]


my attempt:

auto degenerate =
[]( auto&& arg ){
return distance( arg ) < 2;
};

auto myRanges = v | view::split(0) | view::remove_if( degenerate );
for_each( myRanges, []( auto&& range ){ std::cout << range << std::endl;} );


Output:

[1,2,3]
[6,7,8]


I'm at a loss on how I might


  1. "insert" the range from 3 to 6

  2. "append" the range from 8 to 9


Answer Source

If I understand your requirements correctly, then you can implement a generator in terms of adjacent_find:

template<typename IterT>
struct seg_generator_ {
    IterT it_, end_;
    bool fz_ = true;

    ranges::iterator_range<IterT> operator ()() {
        if (it_ == end_) {
            return {it_, end_};
        }

        auto n = ranges::adjacent_find(
            it_, end_,
            [fz = std::exchange(fz_, !fz_)](auto const a, auto const b) {
                return a && !b == fz;
            }
        );
        return {
            std::exchange(it_, n),
            n != end_ ? ranges::next(std::move(n)) : std::move(n)
        };
    }
};

template<typename RngT>
auto seg_generator(RngT&& rng) -> seg_generator_<decltype(ranges::begin(rng))> {
    return {ranges::begin(rng), ranges::end(rng)};
}

int main() {
    std::vector<int> const v{1, 2, 3, 0, 4, 0, 5, 0, 6, 7, 8, 0, 0, 9};
    auto myRanges =
        ranges::view::generate(seg_generator(v))
      | ranges::view::take_while([](auto const& r) {
            return ranges::begin(r) != ranges::end(r);
        });
    ranges::for_each(myRanges, [](auto const& r) { std::cout << r << '\n'; });
}

Online Demo

Not exactly as succinct as one might hope... :-[

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download