Last active
May 1, 2019 14:35
-
-
Save caiorss/6d1ca8c20e71ea72866fe799408c6add to your computer and use it in GitHub Desktop.
Experiments with C++ ranges v3 Library
This file contains hidden or 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
cmake_minimum_required(VERSION 3.9) | |
project(cppexperiments) | |
set(CMAKE_CXX_STANDARD 17) | |
set(CMAKE_VERBOSE_MAKEFILE ON) | |
set(FORCE_CLANG ON) | |
# Force compiler to Clang++ | |
if(FORCE_CLANG) | |
set(CMAKE_C_COMPILER clang) | |
set(CMAKE_CXX_COMPILER clang++) | |
endif() | |
#======= Targets Settings ===============+# | |
# Assumption: the ranges library is installed at | |
# ~/dev/include, | |
# Directory tree: | |
# + ~/dev/include/v3/ranges | |
# | |
include_directories("/home/archbox/dev/include") | |
add_executable(main1 "main1.cpp") |
This file contains hidden or 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
#include <iostream> | |
#include <vector> | |
#include <range/v3/all.hpp> | |
namespace r = ranges; | |
namespace view = ranges::view; | |
template<typename T> | |
void printValue(T&& x) | |
{ | |
std::cout << " x = " << x << std::endl; | |
} | |
template<typename Element> | |
std::ostream& | |
operator<<(std::ostream& os, std::vector<Element> const& xs ) | |
{ | |
os << "[" << xs.size() << "]( "; | |
for(const auto& x: xs) | |
os << x << " "; | |
return os << " )"; | |
} | |
int main() | |
{ | |
std::cout << " +------------------------------------+\n" | |
<< " | Range Algorithms Experiments |\n" | |
<< " +------------------------------------+\n"; | |
auto printLambda = [](auto x){ | |
std::cout << " x = " << x << std::endl; | |
}; | |
// std::cout << view::iota(2, 10) << std::endl; | |
//-------------------------------------------// | |
std::puts("\n === EXPERIMENT 1 A - for_each ==="); | |
{ | |
std::vector<int> xs = {8, 9, 20, 25}; | |
std::cout << " => Print numbers" << std::endl; | |
ranges::for_each(xs, printLambda); | |
std::deque<std::string> words = {"c++", "c++17", "C++20", "asm", "ADA"}; | |
std::cout << " => Print words" << std::endl; | |
ranges::for_each(xs, printLambda); | |
} | |
//-------------------------------------------// | |
std::puts("\n === EXPERIMENT 1 B - for_each ==="); | |
{ | |
std::string astr = "bolts"; | |
ranges::for_each(astr, printLambda); | |
} | |
std::puts("\n === EXPERIMENT 2 - sort ==="); | |
{ | |
std::vector<double> xs2 = {10.45, -30.0, 45.0, 8.2, 100.0, 10.6}; | |
std::cout << " Reverse vector" << std::endl; | |
std::cout << " BEFORE xs2 = " << xs2 << std::endl; | |
ranges::sort(xs2); | |
std::cout << " AFTER xs2 = " << xs2 << std::endl; | |
} | |
std::puts("\n === EXPERIMENT 3 - fill ==="); | |
{ | |
std::vector<int> xs2(10, 0); | |
std::cout << " BEFORE A xs2 = " << view::all(xs2 )<< std::endl; | |
std::cout << " BEFOREB B xs2 = " << xs2 << std::endl; | |
ranges::fill(xs2, 10); | |
std::cout << " AFTER 1 => x2 = " << xs2 << std::endl; | |
ranges::fill(xs2, 5); | |
std::cout << " AFTER 2 => x2 = " << xs2 << std::endl; | |
} | |
std::puts("\n === EXPERIMENT 4 - reverse ==="); | |
{ | |
std::deque<std::string> words = {"c++", "c++17", "C++20", "asm", "ADA"}; | |
std::cout << " BEFORE => words = " << view::all(words) << std::endl; | |
ranges::reverse(words); | |
std::cout << " AFTER => words = " << view::all(words) << std::endl; | |
} | |
std::puts("\n === EXPERIMENT 5 - remove_if ==="); | |
{ | |
std::vector<int> xvec = {1, 2, 5, 10, 1, 4, 1, 2, 8, 20, 100, 10, 1}; | |
std::cout << " BEFORE => xvec = " << xvec << std::endl; | |
// Erase remove idiom | |
xvec.erase(ranges::remove_if(xvec, [](int x){ return x == 1; }), xvec.end()); | |
std::cout << " AFTER => xvec = " << xvec << std::endl; | |
} | |
std::puts("\n === EXPERIMENT 6 - find ==="); | |
{ | |
std::vector<int> xvec = {1, 2, 5, 10, 1, 4, 1, 2}; | |
auto it = ranges::find(xvec, 10); | |
if(it != xvec.end()){ | |
std::cout << " [OK] Found value == 10 => *it = " << *it << std::endl; | |
std::cout << " Position = " | |
<< ranges::distance(xvec.begin(), it) | |
<< std::endl; | |
} | |
else | |
std::cout << " [FAIL] Not found value " << std::endl; | |
} | |
std::puts("\n === EXPERIMENT 7 - accumulator ==="); | |
{ | |
std::vector<int> xvec = {1, 2, 5, 10, 1, 4, 1, 2}; | |
int result = ranges::accumulate(xvec, 0); | |
std::cout << " => ranges::accumulate(xvec, 0) = " | |
<< result << std::endl; | |
} | |
std::cout << "\n"; | |
std::cout << " +------------------------------------+\n" | |
<< " | Range View Experiments |\n" | |
<< " +------------------------------------+\n"; | |
std::puts("\n === EXPERIMENT 1A - view::all"); | |
{ | |
std::vector<char> xs = {'x', 'y', 'm', 'k', 'm'}; | |
std::cout << " [1] => std::view(xs) = " | |
<< view::all(xs) << std::endl; | |
std::cout << "\n [2] => xs = "; | |
for(auto&& x: view::all(xs)) | |
std::cout << x << " "; | |
std::cout << std::endl; | |
} | |
std::puts("\n === EXPERIMENT 1B - view::reverse ==="); | |
{ | |
std::vector<char> xs = {'x', 'y', 'm', 'k', 'm'}; | |
for(auto const& x: view::reverse(xs)) | |
std::cout << " " << x; | |
std::cout << std::endl; | |
std::cout << " AFTER xs = " << xs << std::endl; | |
} | |
std::puts("\n === EXPERIMENT 1C - view::transform ==="); | |
{ | |
std::vector<int> xs = {100, 5, 20, 9, 10, 6}; | |
auto a_lambda = [](auto x){ return 5 * x -10; }; | |
for(auto const& x: xs | view::transform(a_lambda)) | |
std::cout << " " << x; | |
std::cout << std::endl; | |
//std::cout << " AFTER xs = " << xs << std::endl; | |
} | |
std::puts("\n === EXPERIMENT 2A - Range adaptors pipeline ==="); | |
{ | |
auto a_lambda = [](auto x){ return 5 * x -10; }; | |
std::cout << " => Iota view = "; | |
for(auto const& x: view::iota(1) | view::take(8)) | |
std::cout << " " << x; | |
std::cout << std::endl; | |
} | |
std::puts("\n === EXPERIMENT 2B - Range adaptors pipeline ==="); | |
{ | |
std::cout << " => Iota view [B] = \n"; | |
auto aview = | |
view::iota(1) | |
| view::take(5) | |
| view::transform([](int x){ return 5 * x + 6; }); | |
std::cout << " [INFO] aview = " << aview << std::endl; | |
std::cout << " [INFO] aview | reverse = " | |
<< (aview | view::reverse) | |
<< std::endl; | |
std::cout << "\n Iteration 1 => "; | |
ranges::for_each(aview, [](int a){ std::cout << a << " "; }); | |
std::cout << "\n Iteration 2 => "; | |
for(auto const& x: aview | view::reverse) | |
std::cout << x << " "; | |
std::cout << std::endl; | |
} | |
std::puts("\n === EXPERIMENT 3A - enumerate ==="); | |
{ | |
std::deque<std::string> words = { "c++", "c++17", "C++20", "asm" }; | |
std::cout << " ==== Loop 1 ==== " << std::endl; | |
for(auto const& x: view::enumerate(words)) | |
std::cout << " => n = " << x.first | |
<< " ; w = " << x.second << std::endl; | |
std::cout << " ==== Loop 2 ==== " << std::endl; | |
for(auto const& x: words | view::enumerate) | |
std::cout << " => n = " << x.first | |
<< " ; w = " << x.second << std::endl; | |
} | |
std::puts("\n === EXPERIMENT 4 - ranges::accumulate withe iterator ==="); | |
{ | |
auto aview = view::iota(2) | |
| view::transform([](double x){return 3.0 * x - 5; }) | |
| view::take(15); | |
std::cout << " aview = " << aview << std::endl; | |
std::cout << " accumulate(aview) = " << ranges::accumulate(aview, 0.0) | |
<< std::endl; | |
} | |
std::puts("\n === EXPERIMENT 5 - Copy Range to destination ==="); | |
{ | |
std::vector<int> output; | |
auto aview = view::iota(5) | |
| view::transform([](int n){ return 6 * n - 10;}) | |
| view::take(10); | |
std::cout << " BEFORE => output = " << output << std::endl; | |
ranges::copy(aview, ranges::back_inserter(output)); | |
std::cout << " AFTER => output = " << output << std::endl; | |
} | |
// view::iota(1) | view::take(5) | ranges::for_each(printValue); | |
return 0; | |
} |
This file contains hidden or 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
+------------------------------------+ | |
| Range Algorithms Experiments | | |
+------------------------------------+ | |
=== EXPERIMENT 1 A - for_each === | |
=> Print numbers | |
x = 8 | |
x = 9 | |
x = 20 | |
x = 25 | |
=> Print words | |
x = 8 | |
x = 9 | |
x = 20 | |
x = 25 | |
=== EXPERIMENT 1 B - for_each === | |
x = b | |
x = o | |
x = l | |
x = t | |
x = s | |
=== EXPERIMENT 2 - sort === | |
Reverse vector | |
BEFORE xs2 = [6]( 10.45 -30 45 8.2 100 10.6 ) | |
AFTER xs2 = [6]( -30 8.2 10.45 10.6 45 100 ) | |
=== EXPERIMENT 3 - fill === | |
BEFORE A xs2 = [0,0,0,0,0,0,0,0,0,0] | |
BEFOREB B xs2 = [10]( 0 0 0 0 0 0 0 0 0 0 ) | |
AFTER 1 => x2 = [10]( 10 10 10 10 10 10 10 10 10 10 ) | |
AFTER 2 => x2 = [10]( 5 5 5 5 5 5 5 5 5 5 ) | |
=== EXPERIMENT 4 - reverse === | |
BEFORE => words = [c++,c++17,C++20,asm,ADA] | |
AFTER => words = [ADA,asm,C++20,c++17,c++] | |
=== EXPERIMENT 5 - remove_if === | |
BEFORE => xvec = [13]( 1 2 5 10 1 4 1 2 8 20 100 10 1 ) | |
AFTER => xvec = [9]( 2 5 10 4 2 8 20 100 10 ) | |
=== EXPERIMENT 6 - find === | |
[OK] Found value == 10 => *it = 10 | |
Position = 3 | |
=== EXPERIMENT 7 - accumulator === | |
=> ranges::accumulate(xvec, 0) = 26 | |
+------------------------------------+ | |
| Range View Experiments | | |
+------------------------------------+ | |
=== EXPERIMENT 1A - view::all | |
[1] => std::view(xs) = [x,y,m,k,m] | |
[2] => xs = x y m k m | |
=== EXPERIMENT 1B - view::reverse === | |
m k m y x | |
AFTER xs = [5]( x y m k m ) | |
=== EXPERIMENT 1C - view::transform === | |
490 15 90 35 40 20 | |
=== EXPERIMENT 2A - Range adaptors pipeline === | |
=> Iota view = 1 2 3 4 5 6 7 8 | |
=== EXPERIMENT 2B - Range adaptors pipeline === | |
=> Iota view [B] = | |
[INFO] aview = [11,16,21,26,31] | |
[INFO] aview | reverse = [31,26,21,16,11] | |
Iteration 1 => 11 16 21 26 31 | |
Iteration 2 => 31 26 21 16 11 | |
=== EXPERIMENT 3A - enumerate === | |
==== Loop 1 ==== | |
=> n = 0 ; w = c++ | |
=> n = 1 ; w = c++17 | |
=> n = 2 ; w = C++20 | |
=> n = 3 ; w = asm | |
==== Loop 2 ==== | |
=> n = 0 ; w = c++ | |
=> n = 1 ; w = c++17 | |
=> n = 2 ; w = C++20 | |
=> n = 3 ; w = asm | |
=== EXPERIMENT 4 - ranges::accumulate withe iterator === | |
aview = [1,4,7,10,13,16,19,22,25,28,31,34,37,40,43] | |
accumulate(aview) = 330 | |
=== EXPERIMENT 5 - Copy Range to destination === | |
BEFORE => output = [0]( ) | |
AFTER => output = [10]( 20 26 32 38 44 50 56 62 68 74 ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment