#pragma once
#pragma warning(disable: 4018 4267)
#include <vector>
#include <list>
#include <deque>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
#define LINE_LENGTH 5000
#if !defined(SAFE_DELETE)
#define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }
#endif
#if !defined(SAFE_DELETE_ARRAY)
#define SAFE_DELETE_ARRAY(p) { if(p) { delete[] (p); (p)=NULL; } }
#endif
#if !defined(SAFE_RELEASE)
#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
#endif
#define FORMAT_STRING(s, fmt) { va_list args; va_start(args, fmt); s = format(fmt, &args); va_end(args); }
vector<double> Vd();
vector<double> Vd(double a0);
vector<double> Vd(double a0, double a1);
vector<double> Vd(double a0, double a1, double a2);
vector<double> Vd(double a0, double a1, double a2, double a3);
vector<double> Vd(double a0, double a1, double a2, double a3, double a4);
vector<double> Vd(double a0, double a1, double a2, double a3, double a4, double a5);
vector<double> Vd(double a0, double a1, double a2, double a3, double a4, double a5, double a6);
vector<double> Vd(double a0, double a1, double a2, double a3, double a4, double a5, double a6, double a7);
vector<double> Vd(double a0, double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8);
vector<double> Vd(double a0, double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9);
vector<double> Vd(double a0, double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10);
vector<double> Vd(double a0, double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11);
vector<double> Vd(double a0, double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11, double a12);
vector<int> Vi();
vector<int> Vi(int a0);
vector<int> Vi(int a0, int a1);
vector<int> Vi(int a0, int a1, int a2);
vector<int> Vi(int a0, int a1, int a2, int a3);
vector<int> Vi(int a0, int a1, int a2, int a3, int a4);
vector<int> Vi(int a0, int a1, int a2, int a3, int a4, int a5);
vector<int> Vi(int a0, int a1, int a2, int a3, int a4, int a5, int a6);
vector<int> Vi(int a0, int a1, int a2, int a3, int a4, int a5, int a6, int a7);
vector<int> Vi(int a0, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8);
vector<int> Vi(int a0, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9);
vector<int> Vi(int a0, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, int a10);
vector<int> Vi(int a0, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, int a10, int a11);
vector<int> Vi(int a0, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, int a10, int a11, int a12);
vector<bool> Vb();
vector<bool> Vb(bool a0);
vector<bool> Vb(bool a0, bool a1);
vector<bool> Vb(bool a0, bool a1, bool a2);
vector<bool> Vb(bool a0, bool a1, bool a2, bool a3);
vector<bool> Vb(bool a0, bool a1, bool a2, bool a3, bool a4);
vector<bool> Vb(bool a0, bool a1, bool a2, bool a3, bool a4, bool a5);
vector<bool> Vb(bool a0, bool a1, bool a2, bool a3, bool a4, bool a5, bool a6);
vector<bool> Vb(bool a0, bool a1, bool a2, bool a3, bool a4, bool a5, bool a6, bool a7);
vector<bool> Vb(bool a0, bool a1, bool a2, bool a3, bool a4, bool a5, bool a6, bool a7, bool a8);
vector<bool> Vb(bool a0, bool a1, bool a2, bool a3, bool a4, bool a5, bool a6, bool a7, bool a8, bool a9);
vector<bool> Vb(bool a0, bool a1, bool a2, bool a3, bool a4, bool a5, bool a6, bool a7, bool a8, bool a9, bool a10);
vector<bool> Vb(bool a0, bool a1, bool a2, bool a3, bool a4, bool a5, bool a6, bool a7, bool a8, bool a9, bool a10, bool a11);
vector<bool> Vb(bool a0, bool a1, bool a2, bool a3, bool a4, bool a5, bool a6, bool a7, bool a8, bool a9, bool a10, bool a11, bool a12);
vector<string> Vs();
vector<string> Vs(const string &a0);
vector<string> Vs(const string &a0, const string &a1);
vector<string> Vs(const string &a0, const string &a1, const string &a2);
vector<string> Vs(const string &a0, const string &a1, const string &a2, const string &a3);
vector<string> Vs(const string &a0, const string &a1, const string &a2, const string &a3, const string &a4);
vector<string> Vs(const string &a0, const string &a1, const string &a2, const string &a3, const string &a4, const string &a5);
vector<string> Vs(const string &a0, const string &a1, const string &a2, const string &a3, const string &a4, const string &a5, const string &a6);
vector<string> Vs(const string &a0, const string &a1, const string &a2, const string &a3, const string &a4, const string &a5, const string &a6, const string &a7);
vector<string> Vs(const string &a0, const string &a1, const string &a2, const string &a3, const string &a4, const string &a5, const string &a6, const string &a7, const string &a8);
vector<string> Vs(const string &a0, const string &a1, const string &a2, const string &a3, const string &a4, const string &a5, const string &a6, const string &a7, const string &a8, const string &a9);
vector<string> Vs(const string &a0, const string &a1, const string &a2, const string &a3, const string &a4, const string &a5, const string &a6, const string &a7, const string &a8, const string &a9, const string &a10);
void bound(double *px, double lower, double upper);
void bound(float *px, float lower, float upper);
void bound(long *px, long lower, long upper);
void bound(int *px, int lower, int upper);
double fmod(double x, double lower, double upper);
int day_of_week(int nYear, int nMonth, int nDay);
string date_time(int year, int month, int day, int hour, int minute, int second);
string date_time(bool whitespace = true);
string date_time(const string &dashform);
int extract_from_string(char *str, vector<double> &x);
int extract_from_string(char *str, vector<int> &x);
bool append_file(const string &file_target, const string &file_source, bool skip_empty = false);
bool append_file(ofstream &target_stream, const string &file_source, bool skip_empty = false);
string parent_dir(const string d);
bool ensure_dir(const string &dir, const bool go_there = false);
bool ensure_dir_file(const string &fn, const bool go_there = false);
bool file_exists(const string &fn);
string get_path(const string &fn);
string remove_file_suffix(const string &fn);
string file_suffix(const string &fn);
string remove_dir(const string &fn);
void find_file_numeric(const string &dir, const string &pre, vector<int> &res);
void find_file_numeric(const string &dir, const string &pre, const string &post, vector<int> &res, bool strict_num = true);
bool find_file_numeric_max(const string &dir, const string &pre, int &res);
bool find_file_numeric_max(const string &dir, const string &pre, const string &post, int &res, bool strict_num = true);
bool directory(const string &dir, list<string> &out, bool include_dir = false);
bool change_dir_lower_case(const string &dir);
string pre_pad(const string &s, const string &with, const int up_to_length);
string pre_pad(const int n, const string &with, const int up_to_length);
string pad0(const int n, const int up_to_length);
string post_pad(const string &s, const string &with, const int up_to_length);
string post_pad(const int n, const string &with, const int up_to_length);
void trim_space(string &s, bool leading = true, bool trailing = true); // removes leading and/or trailing whitespace
void normalize_space(string &s); // replaces any sequence of one or more whitespace with a single ordinary space
string lower_case(const string &in);
string upper_case(const string &in);
string narrow_string(const wstring &s);
wstring widen_string(const string &s);
double round(double x, unsigned int places);
double interp(double t, double control); // linear center with quadratic edges, v=0 at beginning and end
unsigned short gray_to_binary(unsigned short n);
//unsigned long gray_to_binary(unsigned long n);
string make_absolute(const string& relname);
string get_curr_dir();
void monomials(int dim, int deg, vector< vector<int> > &out); // lists of all monomials in dim variables of degree deg
void monomials_deque(int dim, int deg, vector< deque<int> > &out);
double polynomial(const vector<double> &coeff, double x);
bool between_angles(double ang, double ang_limit1, double ang_limit2);
bool read_binary(const string &prompt = "", char true_value = 'y', char false_value = 'n');
inline double gamma(const double x);
double binomial(const int N, const int n);
string format(const char *fmt, ...); // does printf formatting
string format(const char *fmt, va_list *pargs); // use, together with FORMAT_STRING macro, in functions that have already received a ... argument
typedef vector<double>::iterator VDI;
typedef vector<double>::const_iterator VDCI;
#define LOOPd(v, i) for(vector<double>::const_iterator (i) = (v).begin(); (i) != (v).end(); (i)++)
#define LOOPi(v, i) for(vector<int>::const_iterator (i) = (v).begin(); (i) != (v).end(); (i)++)
#define LOOPs(v, i) for(vector<string>::const_iterator (i) = (v).begin(); (i) != (v).end(); (i)++)
template <class DataType>
ostream& operator <<(ostream &os, const vector<DataType> &vec)
{
for(vector<DataType>::const_iterator i = vec.begin(); i != vec.end(); i++)
os << *i << '\t';
return os;
}
template <class DataType>
istream& operator >>(istream &is, vector<DataType> &vec)
{
DataType elem;
while(is >> elem)
vec.push_back(elem);
return is;
}
template <class DataType>
ofstream& operator <<(ofstream &os, const vector<DataType> &vec)
{
for(vector<DataType>::const_iterator i = vec.begin(); i != vec.end(); i++)
os << *i << '\t';
return os;
}
template <class DataType>
ifstream& operator >>(ifstream &is, vector<DataType> &vec)
{
DataType elem;
while(is >> elem)
vec.push_back(elem);
return is;
}
template <class DataType>
void read_vector(vector<DataType> &vec, istream &is = cin)
{
char line[LINE_LENGTH];
is.getline(line, LINE_LENGTH);
istringstream stream(line);
DataType elem;
while(stream >> elem)
vec.push_back(elem);
}
// usage example for multidimensional() functions:
// vector< vector<int> > m;
// multidimensional(m, 3, 4);
// redimensions an array with (p) elements
template <class D> multidimensional(vector< vector<D> > &m, const int p)
{
m.resize(p);
}
// redimensions a rectangular (p x q) matrix
template <class D> multidimensional(vector< vector<D> > &m, const int p, const int q)
{
m.resize(p);
for(int i = 0; i < m.size(); i++)
m[i].resize(q);
}
// redimensions a (p x q x r) tensor
template <class D> multidimensional(vector< vector<D> > &m, const int p, const int q, const int r)
{
m.resize(p);
for(int i = 0; i < m.size(); i++) {
m[i].resize(q);
for(int j = 0; j < m[i].size(); j++)
m[i][j].resize(r);
}
}
// redimensions a (p x q x r x s) tensor
template <class D> multidimensional(vector< vector<D> > &m, const int p, const int q, const int r, const int s)
{
m.resize(p);
for(int i = 0; i < m.size(); i++) {
m[i].resize(q);
for(int j = 0; j < m[i].size(); j++) {
m[i][j].resize(r);
for(int k = 0; k < m[i][j].size(); k++)
m[i][j][k].resize(s);
}
}
}
template <class T> T interpolate(const T &a, const T &b, double t)
{
return (a + t*(b - a));
}
template <class T> vector<T> vectorize(const T &a)
{
vector<T> v;
v.push_back(a);
return v;
}
template <class T> string str(T x, int n = 0)
{
ostringstream out;
out << x;
string result = out.str();
while(n > 0 && result.size() < n)
result = string("0") + result;
return result;
}
template <class T> wstring wstr(T x, int n = 0)
{
wostringstream out;
out << x;
wstring result = out.str();
while(n > 0 && result.size() < n)
result = wstring(L"0") + result;
return result;
}
// returns a string form of a number with at most n decimal places (but fewer if there are 0s at the end)
template <class T> string strdec(T x, int n = 4)
{
ostringstream out;
if(n > 0) out << setprecision(n);
out << setiosflags(ios::fixed);
out << x;
string result = out.str();
while(result.length() > 0 && result[result.length() - 1] == '0') result.erase(result.length() - 1, 1);
if(result.length() > 0 && result[result.length() - 1] == '.') result.erase(result.length() - 1, 1);
return result;
}
// linear center with quadratic edges, v=0 at beginning and end
// if control=0, 100% linear; if control=1, 100% quadratic
template<class C> C smooth_traj(double t, double T, const C &x0, const C &x1, double control)
{
return x0 + interp(t/T, control)*(x1 - x0);
}
struct interval {
interval(double f = 0, double t = 0) : fr(f), to(t) {}
bool in(double x) { return ((fr <= x) && (x <= to)); }
double fr, to;
};
class interval_set {
public:
interval_set();
~interval_set();
void add(double fr, double to) { add(interval(fr, to)); }
void add(const interval &a);
bool in(double x, list<interval>::iterator *pit = 0);
void get(list<interval> &out);
private:
list<interval> _i;
};
template<class C> bool between(const C &x, const C &lim0, const C &lim1)
{
if(lim0 < lim1)
return(x >= lim0 && x <= lim1);
else
return(x >= lim1 && x <= lim0);
}
template<class T, class M> vector<T> operator *(const M &m, const vector<T> &v)
{
vector<T> out(v);
for(vector<T>::iterator it = out.begin(); it != out.end(); it++)
*it *= m;
return out;
}
template<class T, class M> vector<T> operator *(const vector<T> &v, const M &m)
{
vector<T> out(v);
for(vector<T>::iterator it = out.begin(); it != out.end(); it++)
*it *= m;
return out;
}
template<class T, class M> vector<T> operator /(const vector<T> &v, const M &m)
{
vector<T> out(v);
for(vector<T>::iterator it = out.begin(); it != out.end(); it++)
*it /= m;
return out;
}
template<class T, class M> vector<T>& operator *=(vector<T> &v, const M &m)
{
for(vector<T>::iterator it = v.begin(); it != v.end(); it++)
*it *= m;
return v;
}
template<class T, class M> vector<T>& operator /=(vector<T> &v, const M &m)
{
for(vector<T>::iterator it = v.begin(); it != v.end(); it++)
*it /= m;
return v;
}
// given two equal-length vectors in0 & in1, constructs the vector
// in0[0], in1[0], in0[1], in1[1], in0[2], ...
template<class T> void bisample(vector<T> &out, const vector<T> &in0, const vector<T> &in1)
{
const int n = in0.size();
if(n != in1.size()) throw(error("newutil", "unequal-length vectors in bisample()"));
out.clear();
for(int i = 0; i < n; i++) {
out.push_back(in0[i]);
out.push_back(in1[i]);
}
}
template<class C> class latch {
public:
latch() { _set = false; }
void operator =(const C &val) { if(!_set) { _val = val; _set = true; } }
operator C() const { if(!_set) throw(error("newutil", "trying to read unset latch")); return _val; }
private:
C _val;
bool _set;
};