Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

range-v3: Adapting custom classes which already implement iterator interfaces (begin/end)

I have a custom container implementing begin and end. How can I pipe this container to ranges-v3 views?

std::vector is pipeable, so I tried piping my custom class in the same way, but pipe operator is not found for my container.

I looked into documentation, but I couldn't find any way other then reimplementing a wrapper class with Range interface. I have multiple such classes and I believe this might be a fairly common case, so I'd rather use some function (or class base) offered by the library, but I couldn't figure it out from the documentation.

Here is a minimal example:

#include <iostream>
#include <iterator>
#include <range/v3/all.hpp>

struct Test {
    struct iterator;
    struct sentinel {};
    int counter;
    Test() = default;
    iterator begin();
    sentinel end() const { return {}; }
    iterator begin() const;
};

struct Test::iterator {
    using value_type = int;
    using reference = int&;
    using pointer = int*;
    using iterator_category = std::input_iterator_tag;
    using difference_type = void;
    Test* test;
    iterator& operator++() {
        test->counter++;
        return *this;
    }
    iterator operator++(int) {
        auto it = *this;
        ++*this;
        return it;
    }
    int operator*() { return test->counter; }
    int operator*() const { return test->counter; }
    bool operator!=(const iterator& rhs) const {
        return rhs.test != test;
    }
    bool operator!=(sentinel) const {
        return true;
    }
};

Test::iterator Test::begin() { return iterator {this}; }
Test::iterator Test::begin() const { return iterator {const_cast<Test*>(this)}; }

int main() {
    auto container = Test();
    static_assert(ranges::range<Test>, "It is not a range");
    static_assert(ranges::viewable_range<Test>, "It is not a viewable range");
    auto rng = container | ranges::views::take(10);
    for (auto n : rng) { std::cerr << n << std::endl;}
    return 0;
}

This is the error I'm getting with this code:

~/tmp/range$ g++ main.cpp -Irange-v3/include -o main 2>&1 | grep error
main.cpp:46:19: error: static assertion failed: It is not a range
main.cpp:47:19: error: static assertion failed: It is not a viewable range
range-v3/include/range/v3/functional/pipeable.hpp:63:53: error: no matching function for call to ‘ranges::pipeable_access::impl<ranges::views::view<ranges::make_pipeable_fn::operator()(Fun) const [with Fun = ranges::detail::bind_back_fn_<ranges::views::take_fn, int>]::_> >::pipe(Test&, ranges::views::view<ranges::make_pipeable_fn::operator()(Fun) const [with Fun = ranges::detail::bind_back_fn_<ranges::views::take_fn, int>]::_>&)’
main.cpp:48:10: error: ‘void rng’ has incomplete type
main.cpp:49:19: error: unable to deduce ‘auto&&’ from ‘rng’
like image 978
ofo Avatar asked Aug 24 '19 14:08

ofo


2 Answers

This is my solution:

template <typename T>                                                                                                                                                                                              
class ContainerView : public ranges::view_facade<ContainerView<T>> {                                                                                                                                              
    friend ranges::range_access;                                                                                                                                                                                   

    using iterator_type = decltype(std::declval<T>().begin());                                                                                                                                                     
    using value_type = decltype(*std::declval<iterator_type>());                                                                                                                                                   

    T* container_;                                                                                                                                                                                                 
    iterator_type it_;                                                                                                                                                                                             

    value_type read() const { return *it_; }                                                                                                                                                                       
    bool equal(ranges::default_sentinel_t) const {                                                                                                                                                                 
        return !(it_ != container_->end());                                                                                                                                                                        
    }                                                                                                                                                                                                              
    void next() { ++it_; }                                                                                                                                                                                         

   public:                                                                                                                                                                                                         
    ContainerView() = default;                                                                                                                                                                                     
    ContainerView(T& container)                                                                                                                                                                                    
        : container_(&container), it_(container_->begin()) {}                                                                                                                                                      
    using ranges::view_facade<ContainerView<T>>::begin;                                                                                                                                                            
    using ranges::view_facade<ContainerView<T>>::end;                                                                                                                                                              
};                                                                                                                                                                                                                 

int main() {                                                                                                                                                                                                       
    auto container = Test();                                                                                                                                                                                       
    auto view = ContainerView(container);                                                                                                                                                                          
    static_assert(ranges::range<decltype(view)>, "It is not a range");                                                                                                                                             
    static_assert(ranges::viewable_range<decltype(view)>,                                                                                                                                                          
                  "It is not a viewable range");                                                                                                                                                                   
    auto rng = view | ranges::views::take(10);                                                                                                                                                                     
    for (auto n : rng) {                                                                                                                                                                                           
        std::cerr << n << std::endl;                                                                                                                                                                               
    }                                                                                                                                                                                                              
    return 0;                                                                                                                                                                                                      
}

I'm not sure if I reimplemented something already implemented in the library. So, I won't mark this as answer for a while. Please comment or post a new answer if there is an existing way doing this.

like image 83
ofo Avatar answered Oct 19 '22 00:10

ofo


  • To model sentinel_for, the iterator and sentinel must be comparable using both == and !=, in both directions. (It's not necessary for input iterators to be comparable to each other in the new world.) You only provided != and only in one direction.
  • difference_type cannot be void for an input iterator. It must be a signed integer type, like ptrdiff_t.

Additionally:

  • reference should be the return type of operator*; it does not have to be a reference type.
  • viewable_range<Test> asks whether an rvalue Test is viewable. Since you are trying to view lvalues, consider using Test&.
like image 42
T.C. Avatar answered Oct 19 '22 02:10

T.C.