Thread: [Assorted-commits] SF.net SVN: assorted: [823] configs/trunk/src/topcoder/template.cpp
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-05-15 00:04:00
|
Revision: 823 http://assorted.svn.sourceforge.net/assorted/?rev=823&view=rev Author: yangzhang Date: 2008-05-14 17:04:02 -0700 (Wed, 14 May 2008) Log Message: ----------- making way for new template.cpp Removed Paths: ------------- configs/trunk/src/topcoder/template.cpp Deleted: configs/trunk/src/topcoder/template.cpp =================================================================== --- configs/trunk/src/topcoder/template.cpp 2008-05-14 23:20:40 UTC (rev 822) +++ configs/trunk/src/topcoder/template.cpp 2008-05-15 00:04:02 UTC (rev 823) @@ -1,171 +0,0 @@ -// vim:et:sw=2:ts=2 - -$BEGINCUT$ -#if 0 -$PROBLEMDESC$ -#endif -$ENDCUT$ - - - - - - - -//#line $NEXTLINENUMBER$ "$FILENAME$" -#include <algorithm> -#include <cassert> -#include <cctype> -#include <cmath> -#include <cstdio> -#include <cstdlib> -#include <deque> -#include <iostream> -#include <map> -#include <queue> -#include <set> -#include <sstream> -#include <stack> -#include <string> -#include <vector> -using namespace std; - -#define ARRSIZE(x) (sizeof(x)/sizeof(x[0])) - - - - - - - -$BEGINCUT$ -template<typename T> void print( T a ) { - cerr << a; -} -static void print( long long a ) { - cerr << a << "L"; -} -static void print( string a ) { - cerr << '"' << a << '"'; -} -template<typename T> void print( vector<T> a ) { - cerr << "{"; - for ( int i = 0 ; i != a.size() ; i++ ) { - if ( i != 0 ) cerr << ", "; - print( a[i] ); - } - cerr << "}" << endl; -} -template<typename T> void eq( int n, T have, T need ) { - if ( have == need ) { - cerr << "Case " << n << " passed." << endl; - } else { - cerr << "Case " << n << " failed: expected "; - print( need ); - cerr << " received "; - print( have ); - cerr << "." << endl; - } -} -template<typename T> void eq( int n, vector<T> have, vector<T> need ) { - if( have.size() != need.size() ) { - cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements." << endl; - cerr << " have: "; print( have ); - cerr << " need: "; print( need ); - return; - } - for( size_t i= 0; i < have.size(); i++ ) { - if( have[i] != need[i] ) { - cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << "." << endl; - cerr << " have: "; print( have ); - cerr << " need: "; print( need ); - return; - } - } - cerr << "Case " << n << " passed." << endl; -} -static void eq( int n, string have, string need ) { - if ( have == need ) { - cerr << "Case " << n << " passed." << endl; - } else { - cerr << "Case " << n << " failed: expected "; - print( need ); - cerr << " received "; - print( have ); - cerr << "." << endl; - } -} -$ENDCUT$ - - - - - - - -$BEGINCUT$ -#if 0 -template<typename T> void pp(const T & xs) { - for (typename T::const_iterator it = xs.begin(); - it != xs.end(); - it++) - cout << *it << " "; - cout << endl; -} - -vector<string> split( const string& s, const string& delim =" " ) { - vector<string> res; - string t; - for ( int i = 0 ; i != s.size() ; i++ ) { - if ( delim.find( s[i] ) != string::npos ) { - if ( !t.empty() ) { - res.push_back( t ); - t = ""; - } - } else { - t += s[i]; - } - } - if ( !t.empty() ) { - res.push_back(t); - } - return res; -} - -vector<int> splitInt( const string& s, const string& delim =" " ) { - vector<string> tok = split( s, delim ); - vector<int> res; - for ( int i = 0 ; i != tok.size(); i++ ) - res.push_back( atoi( tok[i].c_str() ) ); - return res; -} -#endif -$ENDCUT$ - - - - - - - -class $CLASSNAME$ { - public: - $RC$ $METHODNAME$($METHODPARMS$) { - $RC$ res; - return res; - } -}; - - - - - - - -$BEGINCUT$ -int main() { - $MAINBODY$ - cin.get(); - return 0; -} -$ENDCUT$ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <yan...@us...> - 2008-05-15 06:27:06
|
Revision: 825 http://assorted.svn.sourceforge.net/assorted/?rev=825&view=rev Author: yangzhang Date: 2008-05-14 23:27:13 -0700 (Wed, 14 May 2008) Log Message: ----------- fixed and enhanced 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-15 00:04:24 UTC (rev 824) +++ configs/trunk/src/topcoder/template.cpp 2008-05-15 06:27:13 UTC (rev 825) @@ -29,9 +29,13 @@ #define pb(x) push_back((x)); #define all(A) (A).begin(), (A).end() #define rall(A) (A).rbegin(), (A).rend() -#define forea(it,xs) for (typeof(xs.begin()) it = xs.begin(); it != xs.end(); it++) -#define FOR(i,xs) for (typeof((xs).size()) i = 0; i < (xs).size(); i++) -#define FOR(i,l,h) for (typeof(l) i = (l); i < (h); i++) +#define each(it,xs) for (typeof(xs.begin()) it = xs.begin(); it != xs.end(); it++) +#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 ping cout << "ping" << endl; +#define pong cout << "pong" << endl; // CHOOSE //int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1}; @@ -44,7 +48,7 @@ typedef vector<vs> vvs; typedef vector<vb> vvb; typedef long long ll; -typedef vector vec; // XXX +#define vec(x) vector< x > typedef string str; template<typename T> inline T mod(T a, T b) { return (a % b + b) % b; } @@ -61,7 +65,8 @@ template<typename T> inline string tos(T x) { stringstream s; s << x; return s.str(); } 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 pp(const T & x) { cout << x << endl; } +template<typename T> inline void print(const T & x) { cout << x << endl; } +#define pp(x) cout << #x << ' ' << x << endl; inline ll gcd(ll a, ll b) { if (a < 0 && b < 0) return gcd(-a,-b); @@ -76,13 +81,13 @@ template <typename T> inline ostream& operator << (ostream& os, const set<T> & xs) { - FOR(i,xs) os << i ? ", " : "{ " << xs[i]; + bounds(i,xs) os << i ? ", " : "{ " << xs[i]; return os << " }"; } template <typename T> inline ostream& operator << (ostream& os, const vector<T> & xs) { - FOR(i,xs) os << i ? ", " : "{ " << xs[i]; + bounds(i,xs) os << i ? ", " : "{ " << xs[i]; return os << " }"; } @@ -95,14 +100,14 @@ vs split( const string & s, const string & delim = " " ) { vs res; string t; - forea(c,s) { - if ( delim.find( c ) != string::npos ) { + each(c,s) { + if ( delim.find( *c ) != string::npos ) { if ( !t.empty() ) { res.pb( t ); t = ""; } } else { - t += c; + t += *c; } } if ( ! t.empty() ) { @@ -114,10 +119,16 @@ vi ints( const str & s, const str & delim = " " ) { vs ss = split( s, delim ); vi is; - forea(s,ss) is.push_back( atoi( s.c_str() ) ); + each(s,ss) is.push_back( atoi( s->c_str() ) ); return is; } +string vi2str( const vi xs ) { + string s(xs.sz, '\0'); + bounds(i,xs) s[i] = xs[i] + '0'; + return s; +} + // following is needed for 'main' #define ARRSIZE(x) (sizeof(x)/sizeof(x[0])) // $ENDREUSE$ @@ -132,15 +143,15 @@ template<typename T> void print( T a ) { cerr << a; } -static void print( long long a ) { +void print( long long a ) { cerr << a << "L"; } -static void print( string a ) { +void print( string a ) { cerr << '"' << a << '"'; } template<typename T> void print( vector<T> a ) { cerr << "{"; - for ( int i = 0 ; i != a.size() ; i++ ) { + bounds(i,a) { if ( i != 0 ) cerr << ", "; print( a[i] ); } @@ -174,7 +185,7 @@ } cerr << "Case " << n << " passed." << endl; } -static void eq( int n, string have, string need ) { +void eq( int n, string have, string need ) { if ( have == need ) { cerr << "Case " << n << " passed." << endl; } else { @@ -199,6 +210,7 @@ +// asdf class $CLASSNAME$ { public: $RC$ $METHODNAME$($METHODPARMS$) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
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. |
From: <yan...@us...> - 2008-06-02 00:13:42
|
Revision: 832 http://assorted.svn.sourceforge.net/assorted/?rev=832&view=rev Author: yangzhang Date: 2008-06-01 17:13:50 -0700 (Sun, 01 Jun 2008) Log Message: ----------- small macro bug Modified Paths: -------------- configs/trunk/src/topcoder/template.cpp Modified: configs/trunk/src/topcoder/template.cpp =================================================================== --- configs/trunk/src/topcoder/template.cpp 2008-06-02 00:13:06 UTC (rev 831) +++ configs/trunk/src/topcoder/template.cpp 2008-06-02 00:13:50 UTC (rev 832) @@ -74,7 +74,7 @@ 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; +#define pp(x) cout << #x << " = " << (x) << endl; inline ll gcd(ll a, ll b) { if (a < 0 && b < 0) return gcd(-a,-b); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |