
%\noindent \textbf{Common Prelude Template File}
\subsection{Common Prelude Template File}


\begin{lstlisting}
// prelude.cpp
#include <bitset> // includes string too!
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <vector>
#include <cstdio>

using namespace std;

ifstream fin("file.in"); // CHANGE FILENAME!
#define cin fin

#define ALL(x) x.begin(), x.end()

#define AUTO(v,e) typeof(e) v = e

    // loop with i as iterator for collection c
#define FOREACH(i,c) \
        for(AUTO(i, c.begin()); i != c.end(); ++i)

    // loop with i as index from 0 to n-1
#define FOR(i,n) for(int i = 0; i < n; ++i)

    // debugging function
#ifdef DEBUG
#define D(x) ( cout << "\t" << #x << " = " \
                  << (x) << endl );
#else
#define D(x)
#endif

    // useful for "normalizing" angles
int mod(int n, int s) { return (s+(n%s)) % s; }

    // generic printing of maps, vectors, etc
namespace std {
  // Allows cout << make_pair(1,2) << endl;
  template<class A, class B>
  ostream& operator<<(ostream& out,
                      const pair<A,B>& x) {
      return out << "{" << x.first << ", "
                        << x.second << "}";
  }
}

template <class S>
string str(const S& v,
               const char* d = " ") {
 stringstream ss;
 FOREACH(i,v) ss << *i << d;
 string s = ss.str(); // rem trailing delim
 if(!s.empty())
   s.erase(s.size() - strlen(d));
 return s;
}



/*
Using common prelude functions:

Compile during testing with -DDEBUG

Liberally use D(var);
  and D(str(container));

Instead of
for(int i = 0;
  i < n; ++i) { blah; }
  use FOR(i,n) { blah; }

And instead of
  for(map<k,v>::iterator i = m.begin();
      i != m.end(); ++i) { blah; }
just use FOREACH(i,m) { blah; }
*/
\end{lstlisting}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "../ref-doc"
%%% End:
