How to Rangify Your Code - Tina Ulbrich - C++ on Sea 2022

Поділитися
Вставка
  • Опубліковано 18 жов 2024

КОМЕНТАРІ • 4

  • @victoreijkhout7115
    @victoreijkhout7115 Рік тому

    The "subtract mean" example implicitly uses the traversal order of the matrix. What if you go through it by columns? Is there a "ranges::view::explode" that turns each mean value into a sequence of identical values, and so the array of means into a sequence of sequences?

    • @AlfredoCorrea
      @AlfredoCorrea 9 місяців тому +1

      there’s ranges::view::repeat for that.

    • @Roibarkan
      @Roibarkan 9 місяців тому +1

      In C++23 there's std::views::repeat which can accomplish that. So the following code should convert a range of N values to a range-of-ranges where each of the inner ranges is N repetitions of the corresponding item in the input: ' rng | transform([N=size(rng)](const auto& x) { return repeat(x, N);});'

    • @AlfredoCorrea
      @AlfredoCorrea 8 місяців тому

      I wrote a C++ multidimensional array library that supports broadcasting. Broadcasting is the common name in Julia and Numpy for what you refer as explode. I won't put the link to my library here, but you can search for it. The advantage of broadcast over std::ranges::repeat is that it is specialized for arrays by adding one dimension to the data structure and also the layout information will be preserved, (some of the strides will be zero, though). It can also be a bit faster than repeat.