[Assorted-commits] SF.net SVN: assorted: [828] configs/trunk/src/topcoder/template.cpp
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-05-16 00:40:10
|
Revision: 828 http://assorted.svn.sourceforge.net/assorted/?rev=828&view=rev Author: yangzhang Date: 2008-05-15 17:40:14 -0700 (Thu, 15 May 2008) Log Message: ----------- updated the c++ template Modified Paths: -------------- configs/trunk/src/topcoder/template.cpp Modified: configs/trunk/src/topcoder/template.cpp =================================================================== --- configs/trunk/src/topcoder/template.cpp 2008-05-16 00:40:06 UTC (rev 827) +++ configs/trunk/src/topcoder/template.cpp 2008-05-16 00:40:14 UTC (rev 828) @@ -20,6 +20,7 @@ #include <sstream> #include <stack> #include <string> +#include <utility> #include <vector> using namespace std; @@ -33,7 +34,11 @@ #define bounds(i,xs) for (typeof((xs).size()) i = 0; i < (xs).size(); i++) #define range(i,l,h) for (typeof(l) i = (l); i < (h); i++) #define event(x) { cout << __FILE__ << ":" << __LINE__ << ": " << #x << endl; x; } -#define trace(x) { cout << __FILE__ << ":" << __LINE__ << ": "; pp(x); x; } +#define trace(x) { \ + typeof(x) __x = x; \ + cout << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << __x << endl; \ + __x; \ +} #define ping cout << "ping" << endl; #define pong cout << "pong" << endl; @@ -48,25 +53,28 @@ typedef vector<vs> vvs; typedef vector<vb> vvb; typedef long long ll; -#define vec(x) vector< x > +#define vec(x...) vector< x > typedef string str; template<typename T> inline T mod(T a, T b) { return (a % b + b) % b; } -template<typename T> inline void rev(T xs) { std::reverse(all(xs)); } -template<typename T> inline void sort(T xs) { std::sort(all(xs)); } -template<typename T> inline void ssort(T xs) { std::stable_sort(all(xs)); } -template<typename T> inline void unique(T xs) { std::unique(all(xs)); } -template<typename T> inline void uniq(T xs) { xs.erase( std::unique(all(xs)), xs.end() ); } -template<typename T, typename U> inline void fill(T xs, U x) { std::fill(all(xs), x); } -template<typename T, typename U> inline U minim(const T xs) { return *std::min_element(all(xs)); } -template<typename T, typename U> inline U maxim(const T xs) { return *std::max_element(all(xs)); } -template<typename T> inline void nextp(T xs) { return std::next_permutation(all(xs)); } -template<typename T> inline void prevp(T xs) { return std::prev_permutation(all(xs)); } -template<typename T> inline string tos(T x) { stringstream s; s << x; return s.str(); } +template<typename T> inline void rev(T & xs) { std::reverse(all(xs)); } +template<typename T> inline void sort(T & xs) { std::sort(all(xs)); } +template<typename T> inline void ssort(T & xs) { std::stable_sort(all(xs)); } +template<typename T> inline void unique(T & xs) { std::unique(all(xs)); } +template<typename T> inline void uniq(T & xs) { xs.erase( std::unique(all(xs)), xs.end() ); } +template<typename T, typename U> inline void fill(T & xs, U & x) { std::fill(all(xs), x); } +template<typename T, typename U> inline U minim(const T & xs) { return *std::min_element(all(xs)); } +template<typename T, typename U> inline U maxim(const T & xs) { return *std::max_element(all(xs)); } +template<typename T> inline void nextp(T & xs) { return std::next_permutation(all(xs)); } +template<typename T> inline void prevp(T & xs) { return std::prev_permutation(all(xs)); } +template<typename T> inline string tos(T & x) { stringstream s; s << x; return s.str(); } +// pow, powl, powf are std +inline double powd(double a, double b) { return std::pow(a, b); } inline int powi(int a, int b) { return int( std::pow(double(a), double(b)) ); } -inline int powl(ll a, ll b) { return ll( std::pow(double(a), double(b)) ); } -template<typename T> inline void print(const T & x) { cout << x << endl; } -#define pp(x) cout << #x << ' ' << x << endl; +inline int powll(ll a, ll b) { return ll( std::pow(double(a), double(b)) ); } +template<typename T> inline void pl(const T & x) { cout << x << endl; } +template<typename T, typename U> inline pair<T,U> mkpair(T t, U u) { return make_pair(t,u); } +#define pp(x) cout << #x << " = " << x << endl; inline ll gcd(ll a, ll b) { if (a < 0 && b < 0) return gcd(-a,-b); @@ -81,14 +89,16 @@ template <typename T> inline ostream& operator << (ostream& os, const set<T> & xs) { - bounds(i,xs) os << i ? ", " : "{ " << xs[i]; + os << "{ "; + bounds(i,xs) os << (i ? ", " : "") << xs[i]; return os << " }"; } template <typename T> inline ostream& operator << (ostream& os, const vector<T> & xs) { - bounds(i,xs) os << i ? ", " : "{ " << xs[i]; - return os << " }"; + os << "[ "; + bounds(i,xs) os << (i ? ", " : "") << xs[i]; + return os << " ]"; } template<class S,class T> @@ -116,6 +126,19 @@ return res; } +vs splitstr( const str & s, const str & glue = " " ) { + vs res; + str::size_type i = 0; + str::size_type ln = glue.sz; + while (true) { + str::size_type pos = s.find(glue, i); + res.push_back(string(s, i, pos)); + if (pos == str::npos) break; + i = pos + ln; + } + return res; +} + vi ints( const str & s, const str & delim = " " ) { vs ss = split( s, delim ); vi is; @@ -129,6 +152,26 @@ return s; } +template<typename T, typename U> inline U realfact(T x) { + ll f = 1; + range(i,1,x+1) f *= i; + return f; +} + +template<typename T> inline ll factl(T x) { return realfact<T,ll>(x); } +template<typename T> inline int facti(T x) { return realfact<T,int>(x); } + +ll perms(ll n, ll r) { + assert(n >= r); + ll p = 1; + for (ll i = n; i > n-r; i--) p *= i; + return p; +} + +ll combs(ll n, ll k) { + return perms(n,k) / factl(k); +} + // following is needed for 'main' #define ARRSIZE(x) (sizeof(x)/sizeof(x[0])) // $ENDREUSE$ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |