Created
December 7, 2021 14:18
-
-
Save withs/15176bde6325162bca3c967c11dfbf0d to your computer and use it in GitHub Desktop.
poc of a simple implementation of python range bases loop in c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// poc of a simple implementation of python range bases loop in c++ | |
#include "iostream" | |
#include "vector" | |
template <typename T> | |
struct Range { | |
private: | |
std::vector<T> _vecRange = { }; | |
public: | |
Range(T fromRange, const T toRange) noexcept{ | |
if ( fromRange == toRange ) return; | |
const int8_t inc = fromRange > toRange ? -1:1; | |
for ( this->_vecRange.push_back(fromRange) ; fromRange != toRange ; | |
this->_vecRange.push_back(fromRange += inc) | |
); | |
} | |
typename std::vector<T>::iterator begin() noexcept { | |
return this->_vecRange.begin(); | |
} | |
typename std::vector<T>::iterator end() noexcept{ | |
return this->_vecRange.end(); | |
} | |
}; | |
std::int32_t main(std::int32_t argc, char const *argv[]) { | |
std::printf("[ 0, 7 ] ( uint8_t ) \n"); | |
for ( const auto& entry: Range<std::uint8_t>(0, 7) ) { | |
std::printf("%u\n", entry); | |
} | |
std::printf("[ 0, -7] ( int8_t ) \n"); | |
for ( const auto& entry: Range<std::int8_t>(0, -7) ) { | |
std::printf("%d\n", entry); | |
} | |
std::printf("[ -7, 0] ( int16_t ) \n"); | |
for ( const auto& entry: Range<std::int16_t>(-7, 0) ) { | |
std::printf("%d\n", entry); | |
} | |
std::printf("[ 7, 0 ] ( uint16_t ) \n"); | |
for ( const auto& entry: Range<std::uint16_t>(7, 0) ) { | |
std::printf("%u\n", entry); | |
} | |
std::printf("[ -7, 7 ] ( int16_t ) \n"); | |
for ( const auto& entry: Range<std::int16_t>(-7, 7) ) { | |
std::printf("%d\n", entry); | |
} | |
std::printf("[ 7.f, -7.f] ( float ) \n"); | |
for ( const auto& entry: Range<float>(7.f, -7.f) ) { | |
std::printf("%f\n", entry); | |
} | |
std::printf("[ -7, -7 ] ( int32_t ) \n"); | |
for ( const auto& entry: Range<std::int32_t>(-7, -7) ) { | |
std::printf("%d\n", entry); | |
} | |
std::printf("[ -7.0, -17.0 ] ( double ) \n"); | |
for ( const auto& entry: Range<double>(-7.0, -17.0) ) { | |
std::printf("%f\n", entry); | |
} | |
std::printf("[ -17, -7 ] ( int32_t ) \n"); | |
for ( const auto& entry: Range<std::int32_t>(-17, -7) ) { | |
std::printf("%d\n", entry); | |
} | |
return 0; | |
} | |
/* Output | |
[ 0, 7 ] ( uint8_t ) | |
0 | |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
[ 0, -7] ( int8_t ) | |
0 | |
-1 | |
-2 | |
-3 | |
-4 | |
-5 | |
-6 | |
-7 | |
[ -7, 0] ( int16_t ) | |
-7 | |
-6 | |
-5 | |
-4 | |
-3 | |
-2 | |
-1 | |
0 | |
[ 7, 0 ] ( uint16_t ) | |
7 | |
6 | |
5 | |
4 | |
3 | |
2 | |
1 | |
0 | |
[ -7, 7 ] ( int16_t ) | |
-7 | |
-6 | |
-5 | |
-4 | |
-3 | |
-2 | |
-1 | |
0 | |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
[ 7, -7] ( float ) | |
7.000000 | |
6.000000 | |
5.000000 | |
4.000000 | |
3.000000 | |
2.000000 | |
1.000000 | |
0.000000 | |
-1.000000 | |
-2.000000 | |
-3.000000 | |
-4.000000 | |
-5.000000 | |
-6.000000 | |
-7.000000 | |
[ -7, -7 ] ( int32_t ) | |
[ -7.0, -17.0 ] ( double ) | |
-7.000000 | |
-8.000000 | |
-9.000000 | |
-10.000000 | |
-11.000000 | |
-12.000000 | |
-13.000000 | |
-14.000000 | |
-15.000000 | |
-16.000000 | |
-17.000000 | |
[ -17, -7 ] ( int32_t ) | |
-17 | |
-16 | |
-15 | |
-14 | |
-13 | |
-12 | |
-11 | |
-10 | |
-9 | |
-8 | |
-7 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment