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

แชร์
ฝัง
  • เผยแพร่เมื่อ 29 ต.ค. 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 9 หลายเดือนก่อน

      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.