7

There is a zip_with function provided by Eric Niebler.

But, now that C++20 have support for ranges I would like to build something similar.

The problem with filter and transform is that they iterate a range?

How would I go about doing this? I have been stuck with this for a while and would hate to use Expression Templates for the same.

Let's say for example I have two vectors M1{1,2,3} and M2{4,5,6}.

I would like to use the ranges library to overload a operator to return a view which contains matrix addition of these two - M1+M2 := {5,7,9}.

With ranges-v3, I can perform auto sum = zip_with(std::plus,M1,M2);

The above expression is evaluated lazily. How can I re-create this expression with C++20 Ranges?

6
  • You should add a code example that shows your intent. a lazily evaluated function on two C++20 ranges could be interpreted in a number of ways.
    – super
    Mar 17, 2020 at 17:09
  • @super Added the example as well. Mar 17, 2020 at 17:15
  • I don't understand what you're asking for. Is it: how do you implement zip_with?
    – Barry
    Mar 18, 2020 at 15:06
  • @Barry Kind of but with C++20 Ranges Library Mar 18, 2020 at 21:50
  • Okay but range-v3's zip_with works fine with C++20 ranges, what's wrong with it?
    – Barry
    Mar 18, 2020 at 22:03

2 Answers 2

2

The principle is quite trivial. Create an iterator that stores an iterator for each vectors, that when incremented, increments the two stored iterators and does the addition only when it is dereferenced.

Here is a piece of code that shoes the principle:

template <class It1, class It2>
struct adder_iterator{
  It1 it1;
  It2 it2;

  decltype(auto)
  operator++(){
    ++it1; ++it2;
    return *this;
    }

  auto
  operator *()const{
    return *it1+*it2;
    }
  //....
  };

You will also need to implement a sentinel and a view (by deriving from std::view_interface).

The sentinel is the end iterator. You can use the adder_iterator class for that. But you can think about optimization: in your view constructor, you ensure that the shortest vector begin iterator is always it1 end then only use this iterator to test the end of the iteration. You should try to see.

6
  • That's what I was looking for. I understand the view part, can you tell me a bit more about the sentinel. And, do I need to implement an adaptor too? Mar 19, 2020 at 19:20
  • @AniketChowdhury The adaptor object is only usefull to get the stream like syntax "a_view | an_adaptor | an_other_adaptor" ...
    – Oliv
    Mar 19, 2020 at 21:54
  • 1
    @AniketChowdhury The sentinel is just a 'end' iterator that has a type that differs than the begin iterator. It is mostly used because it makes range like algorithm more efficient. For example, in this case, in a dumb implementation of the zip_range, the test to check if the adder_iterator has reached the end could be to test if any of it1 or it2 as reached the end. But that would be overkilling if both iterators were random_access_iterators.
    – Oliv
    Mar 19, 2020 at 21:57
  • 1
    @AniketChowdhury The first time I read about sentinel were in Niebler blog: ericniebler.com
    – Oliv
    Mar 19, 2020 at 22:05
  • Another thing, where does the view_interface derived class get called. If I understand it, we are calling adder_iterator from adder_view constructor. Is that correct? It seems a bit confusing still. Mar 20, 2020 at 12:21
1

I don't know what is allowed in c++20, but the following works with range-v3's cpp20 namespace.

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

int main() {

  std::vector<int> m1 = {1, 2, 3};
  std::vector<int> m2 = {4, 5, 6};

  auto sum = ranges::cpp20::views::transform(m1, m2, std::plus{});

  for (auto i : sum)
      std::cout << i << " "; // 5 7 9
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.