Wednesday 15 July 2015

c++ - Optimization code for a debug function (c++11 with templates) -



c++ - Optimization code for a debug function (c++11 with templates) -

i've created debug "function", without knowing how utilize templates. wondering if there easier way obtain same result. if know function don't know can help me, allow me know. , if need help understand something, inquire :)

//containers #include <array> #include <deque> #include <forward_list> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <unordered_map> #include <unordered_set> #include <vector> //input/output #include <iostream> //other #include <algorithm> #include <bitset> #include <chrono> #include <complex> #include <exception> #include <functional> #include <initializer_list> #include <iterator> #include <limits> #include <locale> #include <memory> #include <new> #include <numeric> #include <random> #include <ratio> #include <regex> #include <stdexcept> #include <string> #include <system_error> #include <tuple> #include <typeindex> #include <typeinfo> #include <utility> #include <valarray> using namespace std; template<bool b> struct f22{}; //else (just maps left imho) template<> struct f22<false>{ template<typename t> static void f23(string name, t a){ auto t = a; int i=0; (auto it=t.begin(); it!=t.end(); ++it) cout << it->first << " => " << it->second << endl; } }; //forward list template<> struct f22<true>{ template<typename t> static void f23(string name, t a){ (int n( a.max_size() ) , i(0); < n; i++) cout << name << "[" << << "]: " << *(next(a.begin(),i)) << endl; } }; //check if forwards list template<typename a> struct f20{ template<typename t> static void f21(string name, t t){ f22< is_same< t,forward_list<a> >::value >::f23(name,t); } }; template<bool b> struct f18{}; //!(vector, list, deque or set) template<> struct f18<false>{ template<typename t> static void f19(string name, t a){ f20<typename t::value_type>::f21(name,a); } }; //vector, list, deque or set template<> struct f18<true>{ template<typename t> static void f19(string name, t a){ (int n( a.size() ) , i(0); < n; i++) cout << name << "[" << << "]: " << *(next(a.begin(),i)) << endl; } }; //check if vector, list, deque or set template<typename a> struct f16{ template<typename t> static void f17(string name, t t){ f18< is_same< t,vector<a> >::value || is_same< t,list<a> >::value || is_same< t,deque<a> >::value || is_same< t,set<a> >::value >::f19(name,t); } }; template<bool b> struct f14{}; //queue template<> struct f14<true>{ template<typename t> static void f15(string name, t a){ auto t = a; int i=0; while(!t.empty()){ cout << name << "[" << << "]: " << t.front() << endl; t.pop(); i++; } } }; //not queue template<> struct f14<false>{ template<typename t> static void f15(string name, t a){ f16<typename t::value_type>::f17(name,a); } }; //check if queue template<typename a> struct f12{ template<typename t> static void f13(string name, t t){ f14< is_same< t,queue<a> >::value >::f15(name,t); } }; template<bool b> struct f10{}; //stack template<> struct f10<true>{ template<typename t> static void f11(string name, t a){ auto t = a; int i=0; while(!t.empty()){ cout << name << "[" << << "]: " << t.top() << endl; t.pop(); i++; } } }; //not stack template<> struct f10<false>{ template<typename t> static void f11(string name, t a){ f12<typename t::value_type>::f13(name,a); } }; //check if stack template<typename a> struct f8{ template<typename t> static void f9(string name, t t){ f10< is_same< t,stack<a> >::value >::f11(name,t); } }; template<bool b> struct f6{}; //is_scalar template<> struct f6<true>{ template<typename t> static void f7(string name, t t){ cout << name << ": " << t << endl; } }; //is_not_scalar template<> struct f6<false>{ template<typename t> static void f7(string name, t a){ f8<typename t::value_type>::f9(name,a); } }; //controls if it's scalar type template<int n> struct f4{ template<typename t> static void f5(vector<string> vec,t t){ f6< is_scalar < t > ::value >::f7(vec[n],t); } }; //this split args template<int n,int m> struct f3{ template<typename t> static void f2(vector<string> vec,t const& args){ if(n>=0){ f4<n>::f5(vec,get<n>(args)); } if(n<m){ f3<n+1,m>::f2(vec,args); } } }; //f3's loop's end template<int m> struct f3<m,m>{ template<typename t> static void f2(vector<string> vec,t const& args){ } }; //this split names in vector<string> template<typename ... t> void f1(const char* nomi,tuple<t...> const& args){ vector<string> vec; istringstream is(nomi); for(string buf4er;getline(is,buf4er,',');){ vec.push_back(buf4er); } f3<0,tuple_size<tuple<t...>>::value>::f2(vec,args); } #define debug(...) f1(#__va_args__, tie(__va_args__)) int main(){ int = 1; float b = 2; vector<int> c; c.push_back(3); deque<int> d; d.push_back(4); stack<float> e; e.push(5); queue<double> f; f.push(6); map<char,int> g; g['g']=7; forward_list<int> h; h.push_front(8); set<int> i; i.insert(9); debug(a,b,c,d,e,f,g,i); //with h, in pc crashes homecoming 0; }

std::forward_list::max_size returns maximum number of elements forward_list container can hold.

your forwards list implementation should be:

//forward list template<> struct f22<true>{ template<typename t> static void f23(const string& name, const t& a){ std::size_t = 0; (const auto& e : a) { std::cout << name << "[" << << "]: " << e << std::endl; ++i; } } };

btw, propose implementation debug function.

you may interested pretty-print-c-stl-containers

c++ function templates c++11 optimization

No comments:

Post a Comment