C++20: Pythons range Function, the Second

In my last post, C++20: Pythonic with the Ranges Library, I started my experiment to implement the beloved Python functions range and filter in C++. Due to two very interesting comments to my last post, I revisit the function range.

Admittedly, it took me quite a time to become comfortable with the ranges library, but the effort paid off. You will see why.

I got a few fascinating remarks about my range implementation in my last post. Therefore, I have to visit it once more.

range

As a short reminder, the call range(begin, end, step) generates in Python 2 a list of all integers from begin to end in stepsize steps. begin is inclusive, and the end is exclusive. step is per default 1.

Over-Engineering

My last range implementation of the last was over-engineered, as one of my German readers remarked. The following code snippet shows the over-engineered and the improved version.

 

Rainer D 6 P2 500x500Modernes C++ Mentoring

Be part of my mentoring programs:

  • "Fundamentals for C++ Professionals" (open)
  • "Design Patterns and Architectural Patterns with C++" (open)
  • "C++20: Get the Details" (open)
  • "Concurrency with Modern C++" (starts March 2024)
  • Do you want to stay informed: Subscribe.

     

    std::vector<int> range(int begin, int end, int stepsize = 1) {
        std::vector<int> result{};
        if (begin < end) {                                     
            auto boundary = [end](int i){ return i < end; };    
            for (int i: ranges::views::iota(begin)               // (2)
                      | ranges::views::stride(stepsize) 
                      | ranges::views::take_while(boundary)) {   // (1)
                result.push_back(i);
            }
        }
        else {                                                
            begin++;
            end++;
            stepsize *= -1;
            auto boundary = [begin](int i){ return i < begin; }; 
            for (int i: ranges::views::iota(end)                
                      | ranges::views::take_while(boundary)      
                      | ranges::views::reverse 
                      | ranges::views::stride(stepsize)) {
                result.push_back(i);
            }
        }
        return result;
    }  
    
    std::vector<int> range(int begin, int end, int stepsize = 1) {
        std::vector<int> result{};
        if (begin < end) {
            for (int i: ranges::views::iota(begin, end)         // (3)
                     | ranges::views::stride(stepsize)) {
                result.push_back(i);
            }
        }
        else {
            begin++;
            end++;
            stepsize *= -1;
            for (int i: ranges::views::iota(end, begin)         
                      | ranges::views::reverse 
                      | ranges::views::stride(stepsize)) {
                result.push_back(i);
            }
        }
        return result;
    }
    

    I removed the boundary condition (line 1) in the first implementation and changed the infinite number generator ranges::views::iota(begin) (line 2) to a finite number generator ranges::view::iota(begin, end) (line 3). Consequentially, I did the same in the else branch.

    From range to xrange

    The presented range function is eager. It generates a std::vector<int>.  Aleksei Guzev reminded me that Python 2 also has a lazy xrange function which corresponds to the Python 3 range function. He is right. I’m sufficiently comfortable with the ranges library to apply functional concepts to C++. If you are puzzled by the terms eager and lazy, read my previous post, C++20: Functional Patterns with the Ranges Library.

    The following example shows a lazy range variant, which I called, consequentially, xrange.

    // xrange.hpp
    
    #include <range/v3/all.hpp>
    
    template <long long Begin, long long End>           // (3)
    auto xrange(int stepsize = 1) {
        if constexpr (Begin < End) {                    // (2)
            return ranges::views::iota(Begin, End)      // (1)
                   | ranges::views::stride(stepsize); 
        }
        else {
            long long end  = End + 1;                   // (4)
            long long begin = Begin + 1;                // (4)
            stepsize *= -1; 
            return ranges::views::iota(end, begin)      // (1)
                   | ranges::views::reverse 
                   | ranges::views::stride(stepsize);
        }
    }
    

    Implementing the lazy xrange function is way more complicated than the previous eager range function. But the added complexity pays off. The following numbers correspond to the numbers in the source code snippet.

    1. The xrange function returns not a std::vector<int> but a composition of views. I let the compiler deduce the return type with auto to ease my job. OK, but the return type caused the first challenge. The return types of the if and else branch diver. A function with different return types is not valid in C++.
    2. To overcome this issue, I used a C++17 feature: constexpr if. constexpr if allows conditional compilation. When the expression if constexpr (Begin < End)) becomes true, the if branch is compiled; if not, the else branch. To be valid, Begin and End have to be constant expressions.
    3. Begin and End are now non-type template parameters which makes it possible to use them in a constexpr if (line 2) expression. I used a non-type template parameter of type long long to deal with big numbers. You read in a few sentences why.
    4. Constant expressions such as Begin and End can not be modified. Consequentially, I introduced the variables end and begin to adapt the boundaries for the ranges::views::iota call.

    Let’s try it out.

    // range.cpp
    
    #include "xrange.hpp"
    
    #include <iostream>
    #include <range/v3/all.hpp>
    #include <vector>
    
            
    int main() {
        
        std::cout << std::endl;
    
        auto res = xrange<1, 10>();                    // (1)
        for (auto i: res) std::cout << i << " ";
        
        std::cout << "\n\n";
        
        res = xrange<1, 50>(5);                        // (2)
        for (auto i: res) std::cout << i << " ";
        
        std::cout << "\n\n";
        
        auto res2 = xrange<20, 10>(-1);                // (3)
        for (auto i: res2) std::cout << i << " ";
        
        std::cout << "\n\n";
        
        res2 = xrange<50, 10>(-5);                     // (4)
        for (auto i: res2) std::cout << i << " ";
        
        std::cout << "\n\n";
        
        res = xrange<1, 1'000'000'000'000'000'000>();  // (5)
        // for (auto i: res) std::cout << i << " ";    // (6)
        
        
                                                       // (7)
        for (auto i: res | ranges::views::take(10)) std::cout << i << " ";
        
        std::cout << "\n\n";
        
                                                      // (8)
        for (auto i: res | ranges::views::drop_while([](int i){ return i < 1'000'000; })
                         | ranges::views::take_while([](int i){ return i < 1'000'010; })) {
            std::cout << i << " ";
        }
        
        std::cout << "\n\n";
        
    }
    

    Lines (1) – (4) show that the xrange function works as the previous range function. The only difference is that the function arguments become template arguments. I have to kill the program when I want to have all numbers up to a quintillion (line 6).

    xrangeBreak

    Using tics for numbers (1’000’000’000’000’000’000) (line 5) is valid since C++14 and makes the big numbers easier to read. I should not be so eager but lazy. If I ask only for ten numbers (line 7) or between 1’000’000 and 1’000’010 (line 8) the program works like a charm. Only the numbers are generated that are requested.

    xrange

    What’s next?

    As I already promised in my last post C++20: Pythonic with the Ranges Library, I present in my next post Python’s map function. map empowers you to apply a function to sequences. For convenience reasons, I combine the map and filter functions into one function.

    Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, moon, Philipp Lenk, Hobsbawm, and Charles-Jianye Chen.

    Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.

    My special thanks to Embarcadero
    My special thanks to PVS-Studio
    My special thanks to Tipi.build 
    My special thanks to Take Up Code
    My special thanks to SHAVEDYAKS

    Seminars

    I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.

    Standard Seminars (English/German)

    Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.

    • C++ – The Core Language
    • C++ – The Standard Library
    • C++ – Compact
    • C++11 and C++14
    • Concurrency with Modern C++
    • Design Pattern and Architectural Pattern with C++
    • Embedded Programming with Modern C++
    • Generic Programming (Templates) with C++
    • Clean Code with Modern C++
    • C++20

    Online Seminars (German)

    Contact Me

    Modernes C++ Mentoring,

     

     

    0 replies

    Leave a Reply

    Want to join the discussion?
    Feel free to contribute!

    Leave a Reply

    Your email address will not be published. Required fields are marked *