Created
October 5, 2017 13:12
-
-
Save ahupowerdns/8f4bac369173b56834ea785625b79f16 to your computer and use it in GitHub Desktop.
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
#include <atomic> | |
#include <iostream> | |
/* goal, replace: | |
if(x < 1) | |
++c0_1; | |
else if(x < 10) | |
++c1_10; | |
else if(x< 100) | |
++c10_100; | |
else | |
++cSlow; | |
by: | |
increm(x, 1, c1, 10, c10, 100, c100, cSlow); | |
*/ | |
using std::cout; | |
using std::endl; | |
// for cSlow | |
template<typename T, typename V> | |
void increm(T val, V& counter) | |
{ | |
cout<<"Last val, we increase anyhow"<<endl; | |
++counter; | |
} | |
// if we ended up on the last limit/counter pair | |
template<typename T, typename V> | |
void increm(T val, T lim, V& counter) | |
{ | |
cout<<"single val: "<<val<<", lim="<<lim<<", current counter="<<counter<<endl; | |
if(val < lim) { | |
cout<<"We increase!"<<endl; | |
++counter; | |
} | |
else { | |
cout<<"We hit the end & can't recurse"<<endl; | |
} | |
} | |
// the entry version | |
template<typename T, typename V, typename... Args> | |
void increm(T val, T lim, V& counter, Args&&... args) | |
{ | |
cout<<"mult val: "<<val<<", lim="<<lim<<", current counter="<<counter<<endl; | |
if(val < lim) { | |
cout<<"We increase & are done"<<endl; | |
++counter; | |
} | |
else { | |
cout<<"We recurse.."<<endl; | |
increm(val, std::forward<Args>(args)...); | |
} | |
} | |
int main(int argc, char** argv) | |
{ | |
std::atomic<int> c0_1{0}, c1_10{0}, c10_100{0}, cSlow{0}; | |
increm(7, 1, c0_1, 10, c1_10, cSlow); | |
std::cout<< c0_1<<", "<<c1_10<<", "<<c10_100<<", "<<cSlow<<"\n"; | |
// v l c l c l c c | |
increm(120, 1, c0_1, 10, c1_10, 100, c10_100, cSlow); | |
std::cout<< c0_1<<", "<<c1_10<<", "<<c10_100<<", "<<cSlow<<"\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment