16 #ifndef UTIL_MACROS_INCLUDED
17 #define UTIL_MACROS_INCLUDED
45 #define INT_MAX (static_cast<int>((~(static_cast<unsigned int>(0))) >> 1))
49 #define round(x) floor(x+0.5)
65 #define UTIL_DELPTR(x) if(x) {delete x; x = 0;}
66 #define UTIL_DELARR(x) if(x) {delete [] x; x = 0;}
73 #define UTIL_DEBUG(param, level, x)
76 #define UTIL_DEBUG(param, level, x) if(param >= level) {x fflush(stdout);}
80 #define UTIL_MSG(param, level, x) if(param >= level) {x fflush(stdout);}
85 #define UtilAssert(expression,errorMsg,os) assert(expresssion)
95 (*os) <<
"ERROR:" << errorMsg << std::endl;
103 const std::string& section,
104 const std::string& name,
107 (*os) << std::left << std::setw(15) << section
108 << std::left << std::setw(25) << name
109 << std::setw(10) << value << std::endl;
114 const std::string& section,
115 const std::string& name,
118 (*os) << std::left << std::setw(15) << section
119 << std::left << std::setw(25) << name
120 << std::setw(10) << value << std::endl;
125 const std::string& section,
126 const std::string& name,
127 const std::string& value)
129 (*os) << std::left << std::setw(15) << section
130 << std::left << std::setw(25) << name
131 << std::setw(10) << value << std::endl;
136 template <
class T>
inline void
138 std::ostream* os = &std::cout)
140 typename std::vector<T>::const_iterator it;
142 for (it = v.begin(); it != v.end(); it++) {
150 template <
class T>
inline void
152 const std::vector<std::string>& label,
153 std::ostream* os = &std::cout)
155 typename std::vector<T>::const_iterator it;
157 for (it = v.begin(); it != v.end(); it++) {
158 (*os) << std::setw(5) << *it <<
" -> "
159 << std::setw(25) << label[*it] << std::endl;
164 template <
class T>
inline void
166 std::ostream* os = &std::cout)
168 typename std::list<T>::const_iterator it;
171 for (it = v.begin(); it != v.end(); it++) {
225 return ((n * n) - n) / 2;
231 return i > j ? (i * (i - 1) / 2) + j : (j * (j - 1) / 2) + i;
239 std::ostream* os = &std::cout)
242 (*os) <<
"(" << std::setw(2) << uv.first <<
"," << std::setw(
243 2) << uv.second <<
") ";
249 std::stringstream ss;
251 ss <<
"(" << std::setw(2) << uv.first <<
"," << std::setw(
252 2) << uv.second <<
") ";
261 template <
class T>
inline void
266 for (i = 0; i < size; i++) {
272 template <
class T>
inline void
273 UtilFillN(std::vector<T>& v,
const int size,
const T value)
275 std::fill_n(back_inserter(v), size, value);
283 int val = init + size;
286 for (ii = size; ii-- != 0; ) {
297 int i, val = init + size;
299 for (i = init; i < val; i++) {
311 double rand01 =
static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
312 return a + (rand01 * (b - a));
318 double rand01 =
static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
319 return a +
static_cast<int>(rand01 * (b - a));
327 double rand01a =
static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
328 double rand01b =
static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
329 const double pi = 3.14159265358979323846;
330 double z1 = sqrt(-2.0 * log(rand01a)) * cos(2.0 * pi * rand01b);
331 return z1 * sigma + mean;
339 inline double UtilAve(
const std::vector<double>& x)
341 return std::accumulate(x.begin(), x.end(), 0.0) /
342 static_cast<double>(x.size());
346 inline double UtilAve(
const std::vector<int>& x)
348 return std::accumulate(x.begin(), x.end(), 0.0) /
349 static_cast<double>(x.size());
356 return std::accumulate(x, x + len, 0.0) /
static_cast<double>(len);
365 std::string
const& delimiters,
366 std::vector<std::string>& tokens)
368 std::string::size_type last_pos = 0;
369 std::string::size_type pos = 0;
372 pos = input.find_first_of(delimiters, last_pos);
374 if ( pos == std::string::npos ) {
375 tokens.push_back(input.substr(last_pos));
378 tokens.push_back(input.substr(last_pos, pos - last_pos));
387 std::string strReturn;
388 srand( (
unsigned int)time(NULL) );
390 for (
int i = 0 ; i < iLength ; ++i ) {
392 iNumber = rand() % 122;
394 if ( 48 > iNumber ) {
398 if ( ( 57 < iNumber ) && ( 65 > iNumber ) ) {
402 if ( ( 90 < iNumber ) && ( 97 > iNumber ) ) {
406 strReturn += (char)iNumber;
422 std::string::size_type pos = s.find_last_not_of(t);
424 if (pos != std::string::npos) {
426 pos = s.find_first_not_of(t);
428 if (pos != std::string::npos) {
432 s.erase(s.begin(), s.end());
449 std::transform(s.begin(), s.end(), s.begin(), std::ptr_fun<int, int>(tolower));
469 for (i = 0; s[i] !=
'\0'; i++) {
470 s[i] =
static_cast<char>(toupper(s[i]));
481 template <
class T>
inline int UtilGetSize(
const std::vector<T>& vec)
483 return static_cast<int>(vec.size());
494 for (i = 0; i < setSize; i++) {
495 if (set[i] == value) {
507 const double etol = 1.0e-8)
512 for (i = 0; i < len; i++) {
513 if (fabs(x[i]) > etol) {
524 double floor_x = floor(x);
525 double floor_xplus = floor(x + 0.5);
527 if (fabs(floor_xplus - x) < (
UtilEpsilon * (fabs(floor_xplus) + 1.0))) {
536 const double* arrDbl,
544 const double* arrDbl,
550 const double etol = 1.0e-8)
552 return fabs(x) < etol;
558 std::stringstream ss;
565 const int precision = -1,
568 std::stringstream ss;
570 if (fabs(x) > tooBig) {
577 if (precision >= 0) {
578 ss << std::setiosflags(std::ios::fixed | std::ios::showpoint);
579 ss << std::setprecision(precision);
595 #if not defined(_MSC_VER)
597 if (logLevel >= logLimit) {
598 struct mallinfo memInfo = mallinfo();
599 double memUsage =
static_cast<double>(memInfo.uordblks +
600 memInfo.hblkhd) / 1024.0;
602 (*os) <<
"memUsage = " <<
UtilDblToStr(memUsage, 2) <<
" MB\n";
612 typename std::vector<T*>::iterator first,
613 typename std::vector<T*>::iterator last)
615 typename std::vector<T*>::iterator it;
617 for (it = first; it != last; it++) {
621 vectorPtr.erase(first, last);
632 typename std::list<T*>::iterator first,
633 typename std::list<T*>::iterator last)
635 typename std::list<T*>::iterator it;
637 for (it = first; it != last; it++) {
641 listPtr.erase(first, last);
651 template <
class S,
class T>
653 typename std::map<S, T*>::iterator first,
654 typename std::map<S, T*>::iterator last)
656 typename std::map<S, T*>::iterator it;
658 for (it = first; it != last; it++) {
662 mapPtr.erase(first, last);
672 template <
class S,
class T>
674 typename std::map<S, std::vector<T*> >::iterator first,
675 typename std::map<S, std::vector<T*> >::iterator last)
677 typename std::map<S, std::vector<T*> >::iterator it;
679 for (it = first; it != last; it++) {
683 mapPtr.erase(first, last);
687 template <
class S,
class T>
695 const double etol = 1.0e-10)
703 const double etol = 1.0e-10)
707 for (i = 0; i < len; i++) {
720 transform(arr, arr + arrLen, arr, std::negate<T>());
725 struct AddOffset :
public std::unary_function<T, T> {
738 transform(arr, arr + arrLen, arr,
AddOffset<T>(offset));
760 std::transform(arr, arr + arrLen, arr,
Perturb(randLB, randUB));
774 for (
int i = 0; i < len; i++) {
856 const std::pair<S, T>& y) {
857 return x.second > y.second;
865 const std::pair<S, T>& y) {
866 return x.second < y.second;
874 #if defined(_MSC_VER)
884 const char* fileName)
890 std::string errMessage =
"Error: Filename = ";
891 errMessage += fileName;
892 errMessage +=
" failed to open.";
893 std::cerr << errMessage.c_str() << std::endl;
904 const char* fileName)
910 std::string errMessage =
"Error: Filename = ";
911 errMessage += fileName;
912 errMessage +=
" failed to open.";
913 std::cerr << errMessage.c_str() << std::endl;
924 const std::string& fileName)
931 const std::string& fileName)
int UtilIndexU(const int i, const int j)
void UtilSenseToBound(const char sense, const double rhs, const double range, const double inf, double &lb, double &ub)
void UtilDeleteVectorPtr(vector< T * > &vectorPtr, typename vector< T * >::iterator first, typename vector< T * >::iterator last)
std::string UtilStringRandom(int iLength)
int UtilNumNonzeros(const double *x, const int len, const double etol=1.0e-8)
void UtilNegateArr(const int arrLen, T *arr)
bool UtilIsZero(const double x, const double etol=1.0e-8)
void UtilPrintList(const list< T > &v, ostream *os=&cout)
std::string UtilDblToStr(const double x, const int precision=-1, const double tooBig=UtilSmallerThanTooBig)
int UtilNumEdgesU(const int n)
void UtilBoundToSense(const double lb, const double ub, const double inf, char &sense, double &rhs, double &range)
pair< int, int > UtilBothEndsU(const int index)
void UtilPerturbCost(const int seed, const int arrLen, const double randLB, const double randUB, double *arr)
double UtilAve(const vector< double > &x)
#define UtilAssert(expression, errorMsg, os)
void UtilStringTokenize(std::string const &input, std::string const &delimiters, std::vector< std::string > &tokens)
string & UtilStrToUpper(string &s)
void UtilPrintEdge(const int index, ostream *os=&cout)
void UtilIotaN(int *first, const int size, const int init)
void UtilDeleteListPtr(list< T * > &listPtr, typename list< T * >::iterator first, typename list< T * >::iterator last)
void UtilOpenFile(ifstream &fs, const char *fileName)
bool operator()(const std::pair< S, T > &x, const std::pair< S, T > &y)
const double UtilSmallerThanTooBig
void UtilPrintVector(const vector< T > &v, ostream *os=&cout)
std::string UtilEdgeToStr(const int index)
bool UtilIsInSet(const int value, const int *set, const int setSize)
double UtilFracPart(const double x)
void UtilAddOffsetArr(const int arrLen, T offset, T *arr)
void UtilPrintMemUsage(std::ostream *os=&std::cout, int logLevel=0, int logLimit=2)
void UtilFillN(T *to, const int size, const T value)
string UtilIntToStr(const int i)
Perturb(double randLB, double randUB)
int UtilGetSize(const std::vector< T > &vec)
double UtilNormRand(const double mean, const double sigma)
bool UtilIsIntegral(const double x, const double etol=1.0e-10)
bool operator()(const std::pair< S, T > &x, const std::pair< S, T > &y)
double UtilURand(const double a, const double b)
void UtilPrintParameter(std::ostream *os, const std::string §ion, const std::string &name, const int value)
string & UtilStrTrim(string &s, const string &t=UtilSpaces)
void UtilDeleteMapPtr(std::map< S, T * > &mapPtr, typename std::map< S, T * >::iterator first, typename std::map< S, T * >::iterator last)
void UtilFlipRowLtoG(const int len, double *els, char &sense, double &rhs)
int UtilScaleDblToIntArr(const int arrLen, const double *arrDbl, int *arrInt, const double oneDbl, int *oneInt, const double epstol=UtilEpsilon)
void UtilDeleteMapVecPtr(std::map< S, std::vector< T * > > &mapPtr, typename std::map< S, std::vector< T * > >::iterator first, typename std::map< S, std::vector< T * > >::iterator last)
string & UtilStrToLower(string &s)
double operator()(const double &k)