15 #ifndef UTIL_MACROS_INCLUDED 
   16 #define UTIL_MACROS_INCLUDED 
   44 #define INT_MAX (static_cast<int>((~(static_cast<unsigned int>(0))) >> 1)) 
   48 #define round(x) floor(x+0.5) 
   64 #define UTIL_DELPTR(x) if(x) {delete    x; x = 0;} 
   65 #define UTIL_DELARR(x) if(x) {delete [] x; x = 0;} 
   72 #define UTIL_DEBUG(param, level, x) 
   75 #define UTIL_DEBUG(param, level, x) if(param >= level) {x fflush(stdout);} 
   79 #define UTIL_MSG(param, level, x)   if(param >= level) {x fflush(stdout);} 
   84 #define UtilAssert(expression,errorMsg,os) assert(expresssion) 
   94       (*os) << 
"ERROR:"  << errorMsg << std::endl;
 
  102                                const std::string& section,
 
  103                                const std::string& name,
 
  106    (*os) << std::left << std::setw(15) << section
 
  107          << std::left << std::setw(25) << name
 
  108          << std::setw(10) << value  << std::endl;
 
  113                                const std::string& section,
 
  114                                const std::string& name,
 
  117    (*os) << std::left << std::setw(15) << section
 
  118          << std::left << std::setw(25) << name
 
  119          << std::setw(10) << value  << std::endl;
 
  124                                const std::string& section,
 
  125                                const std::string& name,
 
  126                                const std::string&   value)
 
  128    (*os) << std::left << std::setw(15) << section
 
  129          << std::left << std::setw(25) << name
 
  130          << std::setw(10) << value  << std::endl;
 
  135 template <
class T> 
inline void 
  137                 std::ostream*          os = &std::cout)
 
  139    typename std::vector<T>::const_iterator it;
 
  141    for (it = v.begin(); it != v.end(); it++) {
 
  149 template <
class T> 
inline void 
  151                 const std::vector<std::string>& label,
 
  152                 std::ostream*               os = &std::cout)
 
  154    typename std::vector<T>::const_iterator it;
 
  156    for (it = v.begin(); it != v.end(); it++) {
 
  157       (*os) << std::setw(5) << *it << 
" -> " 
  158             << std::setw(25) << label[*it] << std::endl;
 
  163 template <
class T> 
inline void 
  165               std::ostream*        os = &std::cout)
 
  167    typename std::list<T>::const_iterator it;
 
  170    for (it = v.begin(); it != v.end(); it++) {
 
  224    return ((n * n) - n) / 2;
 
  230    return i > j ? (i * (i - 1) / 2) + j : (j * (j - 1) / 2) + i;
 
  238                           std::ostream*    os = &std::cout)
 
  241    (*os) << 
"(" << std::setw(2) << uv.first << 
"," << std::setw(
 
  242             2) << uv.second << 
") ";
 
  248    std::stringstream  ss;
 
  250    ss << 
"(" << std::setw(2) << uv.first << 
"," << std::setw(
 
  251          2) << uv.second << 
") ";
 
  260 template <
class T> 
inline void 
  265    for (i = 0; i < size; i++) {
 
  271 template <
class T> 
inline void 
  272 UtilFillN(std::vector<T>& v, 
const int size, 
const T value)
 
  274    std::fill_n(back_inserter(v), size, value);
 
  282    int val = init + size;
 
  285    for (ii = size; ii-- != 0; ) {
 
  296    int i, val = init + size;
 
  298    for (i = init; i < val; i++) {
 
  310    double rand01 = 
static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
 
  311    return a + (rand01 * (b - a));
 
  317    double rand01 = 
static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
 
  318    return a + 
static_cast<int>(rand01 * (b - a));
 
  326    double rand01a = 
static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
 
  327    double rand01b = 
static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
 
  328    const double pi = 3.14159265358979323846;
 
  329    double       z1 = sqrt(-2.0 * log(rand01a)) * cos(2.0 * pi * rand01b);
 
  330    return z1 * sigma + mean;
 
  338 inline double UtilAve(
const std::vector<double>& x)
 
  340    return std::accumulate(x.begin(), x.end(), 0.0) /
 
  341           static_cast<double>(x.size());
 
  345 inline double UtilAve(
const std::vector<int>& x)
 
  347    return std::accumulate(x.begin(), x.end(), 0.0) /
 
  348           static_cast<double>(x.size());
 
  355    return std::accumulate(x, x + len, 0.0) / 
static_cast<double>(len);
 
  364                                std::string 
const&    delimiters,
 
  365                                std::vector<std::string>& tokens)
 
  367    std::string::size_type last_pos = 0;
 
  368    std::string::size_type pos = 0;
 
  371       pos = input.find_first_of(delimiters, last_pos);
 
  373       if ( pos == std::string::npos ) {
 
  374          tokens.push_back(input.substr(last_pos));
 
  377          tokens.push_back(input.substr(last_pos, pos - last_pos));
 
  386    std::string strReturn;
 
  387    srand( (
unsigned int)time(NULL) );
 
  389    for ( 
int i = 0 ; i < iLength ; ++i ) {
 
  391       iNumber = rand() % 122;
 
  393       if ( 48 > iNumber ) {
 
  397       if ( ( 57 < iNumber ) && ( 65 > iNumber ) ) {
 
  401       if ( ( 90 < iNumber ) && ( 97 > iNumber ) ) {
 
  405       strReturn += (char)iNumber;
 
  421    std::string::size_type pos = s.find_last_not_of(t);
 
  423    if (pos != std::string::npos) {
 
  425       pos = s.find_first_not_of(t);
 
  427       if (pos != std::string::npos) {
 
  431       s.erase(s.begin(), s.end());
 
  448    std::transform(s.begin(), s.end(), s.begin(), std::ptr_fun<int, int>(tolower));
 
  468    for (i = 0; s[i] != 
'\0'; i++) {
 
  469       s[i] = 
static_cast<char>(toupper(s[i]));
 
  480 template <
class T> 
inline int UtilGetSize(
const std::vector<T>& vec)
 
  482    return static_cast<int>(vec.size());
 
  493    for (i = 0; i < setSize; i++) {
 
  494       if (set[i] == value) {
 
  506                            const double   etol = 1.0e-8)
 
  511    for (i = 0; i < len; i++) {
 
  512       if (fabs(x[i]) > etol) {
 
  523    double floor_x     = floor(x);
 
  524    double floor_xplus = floor(x + 0.5);
 
  526    if (fabs(floor_xplus - x) < (
UtilEpsilon * (fabs(floor_xplus) + 1.0))) {
 
  535                          const double* arrDbl,
 
  543                          const double* arrDbl,
 
  549                        const double etol = 1.0e-8)
 
  551    return fabs(x) < etol;
 
  557    std::stringstream ss;
 
  564                                 const int    precision = -1,
 
  567    std::stringstream ss;
 
  569    if (fabs(x) > tooBig) {
 
  576       if (precision >= 0) {
 
  577          ss << std::setiosflags(std::ios::fixed | std::ios::showpoint);
 
  578          ss << std::setprecision(precision);
 
  594 #if not defined(_MSC_VER) 
  596    if (logLevel >= logLimit) {
 
  597       struct mallinfo memInfo = mallinfo();
 
  598       double memUsage = 
static_cast<double>(memInfo.uordblks +
 
  599                                             memInfo.hblkhd) / 1024.0;
 
  601       (*os) << 
"memUsage = " << 
UtilDblToStr(memUsage, 2) << 
" MB\n";
 
  611                          typename std::vector<T*>::iterator first,
 
  612                          typename std::vector<T*>::iterator last)
 
  614    typename std::vector<T*>::iterator it;
 
  616    for (it = first; it != last; it++) {
 
  620    vectorPtr.erase(first, last);
 
  631       typename std::list<T*>::iterator first,
 
  632       typename std::list<T*>::iterator last)
 
  634    typename std::list<T*>::iterator it;
 
  636    for (it = first; it != last; it++) {
 
  640    listPtr.erase(first, last);
 
  650 template <
class S, 
class T>
 
  652                       typename std::map<S, T*>::iterator first,
 
  653                       typename std::map<S, T*>::iterator last)
 
  655    typename std::map<S, T*>::iterator it;
 
  657    for (it = first; it != last; it++) {
 
  661    mapPtr.erase(first, last);
 
  671 template <
class S, 
class T>
 
  673                          typename std::map<S, std::vector<T*> >::iterator first,
 
  674                          typename std::map<S, std::vector<T*> >::iterator last)
 
  676    typename std::map<S, std::vector<T*> >::iterator it;
 
  678    for (it = first; it != last; it++) {
 
  682    mapPtr.erase(first, last);
 
  686 template <
class S, 
class T>
 
  694                            const double etol = 1.0e-10)
 
  702                            const double   etol = 1.0e-10)
 
  706    for (i = 0; i < len; i++) {
 
  719    transform(arr, arr + arrLen, arr, std::negate<T>());
 
  724 struct AddOffset : 
public std::unary_function<T, T> {
 
  737    transform(arr, arr + arrLen, arr, 
AddOffset<T>(offset));
 
  759    std::transform(arr, arr + arrLen, arr, 
Perturb(randLB, randUB));
 
  773       for (
int i = 0; i < len; i++) {
 
  855                     const std::pair<S, T>& y) {
 
  856       return x.second > y.second;
 
  864                     const std::pair<S, T>& y) {
 
  865       return x.second < y.second;
 
  873 #if defined(_MSC_VER) 
  883                         const char* fileName)
 
  889       std::string errMessage = 
"Error: Filename = ";
 
  890       errMessage += fileName;
 
  891       errMessage += 
" failed to open.";
 
  892       std::cerr << errMessage.c_str() << std::endl;
 
  903                         const char* fileName)
 
  909       std::string errMessage = 
"Error: Filename = ";
 
  910       errMessage += fileName;
 
  911       errMessage += 
" failed to open.";
 
  912       std::cerr << errMessage.c_str() << std::endl;
 
  923                         const std::string& fileName)
 
  930                         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)