Dip-All  0.91.0
UtilMacros.h
Go to the documentation of this file.
1 //===========================================================================//
2 // This file is part of the DIP Solver Framework. //
3 // //
4 // DIP is distributed under the Eclipse Public License as part of the //
5 // COIN-OR repository (http://www.coin-or.org). //
6 // //
7 // Author: Matthew Galati, SAS Institute Inc. (matthew.galati@sas.com) //
8 // //
9 // Conceptual Design: Matthew Galati, SAS Institute Inc. //
10 // Ted Ralphs, Lehigh University //
11 // //
12 // Copyright (C) 2002-2015, Lehigh University, Matthew Galati, Ted Ralphs //
13 // All Rights Reserved. //
14 //===========================================================================//
15 
16 #ifndef UTIL_MACROS_INCLUDED
17 #define UTIL_MACROS_INCLUDED
18 
19 // =========================================================================
20 #include <cstdio>
21 #include <cassert>
22 #include <vector>
23 #include <list>
24 #include <iostream>
25 #include <fstream>
26 #include <iomanip>
27 #include <numeric>
28 #include <sstream>
29 #include <algorithm>
30 #include <functional>
31 #include <string>
32 #include <map>
33 #include <climits>
34 #include <cmath>
35 #include <cstring>
36 #include <ctime>
37 #include <memory>
38 
39 // =========================================================================
40 const std::string UtilSpaces = " \t\r\n";
41 const double UtilEpsilon = 1.0e-6;
42 const double UtilTooBig = 1.0e20;
43 const double UtilSmallerThanTooBig = 1.0e19;
44 #ifndef INT_MAX
45 #define INT_MAX (static_cast<int>((~(static_cast<unsigned int>(0))) >> 1))
46 #endif
47 
48 #ifndef round
49 #define round(x) floor(x+0.5)
50 #endif
51 
52 // =========================================================================
53 
54 // =========================================================================
55 // Util Error Codes
56 // =========================================================================
57 enum UtilStatus {
60 };
61 
62 // =========================================================================
63 // Memory Macros
64 // =========================================================================
65 #define UTIL_DELPTR(x) if(x) {delete x; x = 0;}
66 #define UTIL_DELARR(x) if(x) {delete [] x; x = 0;}
67 
68 // =========================================================================
69 // Debug Macros
70 // =========================================================================
71 #ifdef NDEBUG
72 //use with LogDebugLevel
73 #define UTIL_DEBUG(param, level, x)
74 //#define UTIL_DEBUG(param, level, x) if(param >= level) {x fflush(stdout);}
75 #else
76 #define UTIL_DEBUG(param, level, x) if(param >= level) {x fflush(stdout);}
77 #endif
78 
79 //use with LogLevel
80 #define UTIL_MSG(param, level, x) if(param >= level) {x fflush(stdout);}
81 
82 
83 // ------------------------------------------------------------------------- //
84 #ifndef NDEBUG
85 #define UtilAssert(expression,errorMsg,os) assert(expresssion)
86 #else
87 inline void UtilAssert(bool expression,
88  std::string errorMsg,
89  std::ostream* os)
90 {
91  //---
92  //--- this is a forced assertion (even when -NDEBUG)
93  //---
94  if (!expression) {
95  (*os) << "ERROR:" << errorMsg << std::endl;
96  abort();
97  }
98 }
99 #endif
100 
101 // ------------------------------------------------------------------------- //
102 inline void UtilPrintParameter(std::ostream* os,
103  const std::string& section,
104  const std::string& name,
105  const int value)
106 {
107  (*os) << std::left << std::setw(15) << section
108  << std::left << std::setw(25) << name
109  << std::setw(10) << value << std::endl;
110 }
111 
112 // ------------------------------------------------------------------------- //
113 inline void UtilPrintParameter(std::ostream* os,
114  const std::string& section,
115  const std::string& name,
116  const double value)
117 {
118  (*os) << std::left << std::setw(15) << section
119  << std::left << std::setw(25) << name
120  << std::setw(10) << value << std::endl;
121 }
122 
123 
124 inline void UtilPrintParameter(std::ostream* os,
125  const std::string& section,
126  const std::string& name,
127  const std::string& value)
128 {
129  (*os) << std::left << std::setw(15) << section
130  << std::left << std::setw(25) << name
131  << std::setw(10) << value << std::endl;
132 }
133 
134 
135 // ------------------------------------------------------------------------- //
136 template <class T> inline void
137 UtilPrintVector(const std::vector<T>& v,
138  std::ostream* os = &std::cout)
139 {
140  typename std::vector<T>::const_iterator it;
141 
142  for (it = v.begin(); it != v.end(); it++) {
143  (*os) << *it << " ";
144  }
145 
146  (*os) << "\n";
147 }
148 
149 // ------------------------------------------------------------------------- //
150 template <class T> inline void
151 UtilPrintVector(const std::vector<T>& v,
152  const std::vector<std::string>& label,
153  std::ostream* os = &std::cout)
154 {
155  typename std::vector<T>::const_iterator it;
156 
157  for (it = v.begin(); it != v.end(); it++) {
158  (*os) << std::setw(5) << *it << " -> "
159  << std::setw(25) << label[*it] << std::endl;
160  }
161 }
162 
163 // ------------------------------------------------------------------------- //
164 template <class T> inline void
165 UtilPrintList(const std::list<T>& v,
166  std::ostream* os = &std::cout)
167 {
168  typename std::list<T>::const_iterator it;
169  (*os) << "\n";
170 
171  for (it = v.begin(); it != v.end(); it++) {
172  (*os) << *it << " ";
173  }
174 }
175 
176 // =========================================================================
177 // Graph Macros
178 // =========================================================================
179 
180 //TODO: should this be in a UtilGraph.h or something?
181 /* -------------------------------------------------------------------------
182  --- Assumption: a complete undirected graph,
183  --- (i,j) = (j,i), i!=j (setup for i>j)
184 
185  --- Loop thru edges: i: 1 -> n-1, j: 0 -> i-1
186  --- Number of edges: m = (n^2 - n)/2
187 
188  --- Get the edge index from (i,j):
189  --- INDEX_U(i,j) = i > j ? (i * (i-1) / 2) + j : (j * (j-1) / 2) + i
190 
191  --- Get (i,j) from the edge index:
192  --- index = (i * (i-1) / 2) + j
193  --- index = (j * (j-1) / 2) + i
194  --- ax^2 + bx + c = 0 -> x = (-b +- sqrt(b^2 - 4ac)) / 2a
195  --- j = index - (j * (j-1))/2
196  --- i = -1/2 + 1/2 sqrt(1 + 8 * index)
197 
198  --- Example: n = 5 (i>j)
199 
200  --- 0 1 2 3 = j
201  --- 0
202  --- 1 0 (1,0)
203  --- 2 1 (2,0) 2 (2,1)
204  --- 3 3 (3,0) 4 (3,1) 5 (3,2)
205  --- 4 6 (4,0) 7 (4,1) 8 (4,2) 9 (4,3)
206 
207  --- For the directed version (see EWCP):
208 
209  --- Loop thru edges:
210  --- i: 1 -> n-1, j: 0 -> i-1 (i>j)
211  --- j: 1 -> n-1, i: 0 -> j-1 (i<j)
212 
213  --- Number of edges: m = (n^2 - n)/2
214 
215  --- 0 1 2 3 4 = j
216  --- 0 10 (0,1) 11 (0,2) 13 (0,3) 16 (0,4)
217  --- 1 12 (1,2) 14 (1,3) 17 (1,4)
218  --- 2 15 (2,3) 18 (2,4)
219  --- 3 19 (3,4)
220  -------------------------------------------------------------------------*/
221 
222 /*-----------------------------------------------------------------------*/
223 inline int UtilNumEdgesU(const int n)
224 {
225  return ((n * n) - n) / 2;
226 }
227 
228 /*-----------------------------------------------------------------------*/
229 inline int UtilIndexU(const int i, const int j)
230 {
231  return i > j ? (i * (i - 1) / 2) + j : (j * (j - 1) / 2) + i;
232 }
233 
234 /*-----------------------------------------------------------------------*/
235 std::pair<int, int> UtilBothEndsU(const int index);
236 
237 /*-----------------------------------------------------------------------*/
238 inline void UtilPrintEdge(const int index,
239  std::ostream* os = &std::cout)
240 {
241  std::pair<int, int> uv = UtilBothEndsU(index);
242  (*os) << "(" << std::setw(2) << uv.first << "," << std::setw(
243  2) << uv.second << ") ";
244 }
245 
246 /*-----------------------------------------------------------------------*/
247 inline std::string UtilEdgeToStr(const int index)
248 {
249  std::stringstream ss;
250  std::pair<int, int> uv = UtilBothEndsU(index);
251  ss << "(" << std::setw(2) << uv.first << "," << std::setw(
252  2) << uv.second << ") ";
253  return ss.str();
254 }
255 
256 // =========================================================================
257 // Fill-In Macros
258 // =========================================================================
259 
260 // =========================================================================
261 template <class T> inline void
262 UtilFillN(T* to, const int size, const T value)
263 {
264  int i;
265 
266  for (i = 0; i < size; i++) {
267  to[i] = value;
268  }
269 }
270 
271 // =========================================================================
272 template <class T> inline void
273 UtilFillN(std::vector<T>& v, const int size, const T value)
274 {
275  std::fill_n(back_inserter(v), size, value);
276 }
277 
278 /*-----------------------------------------------------------------------*/
279 inline void UtilIotaN(int* first,
280  const int size,
281  const int init)
282 {
283  int val = init + size;
284  int ii;
285 
286  for (ii = size; ii-- != 0; ) {
287  first[ii] = --val;
288  }
289 }
290 
291 /*-----------------------------------------------------------------------*/
292 inline void UtilIotaN(std::vector<int>& first,
293  const int size,
294  const int init)
295 {
296  first.reserve(size);
297  int i, val = init + size;
298 
299  for (i = init; i < val; i++) {
300  first.push_back(i);
301  }
302 }
303 
304 // =========================================================================
305 // Random Numbers
306 // =========================================================================
307 
308 /*-----------------------------------------------------------------------*/
309 inline double UtilURand(const double a, const double b)
310 {
311  double rand01 = static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
312  return a + (rand01 * (b - a));
313 }
314 
315 /*-----------------------------------------------------------------------*/
316 inline int UtilURand(const int a, const int b)
317 {
318  double rand01 = static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
319  return a + static_cast<int>(rand01 * (b - a));
320 }
321 
322 /*-----------------------------------------------------------------------*/
323 inline double UtilNormRand(const double mean,
324  const double sigma)
325 {
326  //http://mathworld.wolfram.com/Box-MullerTransformation.html
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;
332 }
333 
334 // =========================================================================
335 // Statistics
336 // =========================================================================
337 
338 // ------------------------------------------------------------------------- //
339 inline double UtilAve(const std::vector<double>& x)
340 {
341  return std::accumulate(x.begin(), x.end(), 0.0) /
342  static_cast<double>(x.size());
343 }
344 
345 // ------------------------------------------------------------------------- //
346 inline double UtilAve(const std::vector<int>& x)
347 {
348  return std::accumulate(x.begin(), x.end(), 0.0) /
349  static_cast<double>(x.size());
350 }
351 
352 // ------------------------------------------------------------------------- //
353 inline double UtilAve(const double* x,
354  const int len)
355 {
356  return std::accumulate(x, x + len, 0.0) / static_cast<double>(len);
357 }
358 
359 // =========================================================================
360 // String Macros
361 // =========================================================================
362 
363 // ------------------------------------------------------------------------- //
364 inline void UtilStringTokenize(std::string const& input,
365  std::string const& delimiters,
366  std::vector<std::string>& tokens)
367 {
368  std::string::size_type last_pos = 0;
369  std::string::size_type pos = 0;
370 
371  while (true) {
372  pos = input.find_first_of(delimiters, last_pos);
373 
374  if ( pos == std::string::npos ) {
375  tokens.push_back(input.substr(last_pos));
376  break;
377  } else {
378  tokens.push_back(input.substr(last_pos, pos - last_pos));
379  last_pos = pos + 1;
380  }
381  }
382 }
383 
384 // ------------------------------------------------------------------------- //
385 inline std::string UtilStringRandom(int iLength)
386 {
387  std::string strReturn;
388  srand( (unsigned int)time(NULL) );
389 
390  for ( int i = 0 ; i < iLength ; ++i ) {
391  int iNumber;
392  iNumber = rand() % 122;
393 
394  if ( 48 > iNumber ) {
395  iNumber += 48;
396  }
397 
398  if ( ( 57 < iNumber ) && ( 65 > iNumber ) ) {
399  iNumber += 7;
400  }
401 
402  if ( ( 90 < iNumber ) && ( 97 > iNumber ) ) {
403  iNumber += 6;
404  }
405 
406  strReturn += (char)iNumber;
407  }
408 
409  srand(1);
410  return strReturn;
411 }
412 
413 // ------------------------------------------------------------------------- //
414 //trims white space (as defined by UtilSpaces) in-place
415 inline std::string& UtilStrTrim(std::string& s,
416  const std::string& t = UtilSpaces)
417 {
418  if (s.size() == 0) {
419  return s;
420  }
421 
422  std::string::size_type pos = s.find_last_not_of(t);
423 
424  if (pos != std::string::npos) {
425  s.erase(pos + 1);
426  pos = s.find_first_not_of(t);
427 
428  if (pos != std::string::npos) {
429  s.erase(0, pos);
430  }
431  } else {
432  s.erase(s.begin(), s.end());
433  }
434 
435  return s;
436 }
437 
438 // ------------------------------------------------------------------------- //
439 // returns a lower case version of the std::string in-place
440 inline std::string& UtilStrToLower(std::string& s)
441 {
442  // Purify did not like this version:
443  // transform (s.begin(), s.end(), s.begin(), myToLower());
444  if (s.size() == 0) {
445  return s;
446  }
447 
448  // This is a fix for MSVC++ The old version is below
449  std::transform(s.begin(), s.end(), s.begin(), std::ptr_fun<int, int>(tolower));
450  //int i;
451  //for (i = 0; s[i] != '\0'; i++)
452  // s[i] = static_cast<char>(tolower(s[i]));
453  return s;
454 }
455 
456 
457 // ------------------------------------------------------------------------- //
458 // returns an upper case version of the std::string in-place
459 inline std::string& UtilStrToUpper(std::string& s)
460 {
461  // Purify did not like this version:
462  // transform (s.begin(), s.end(), s.begin(), myToUpper());
463  int i;
464 
465  if (s.size() == 0) {
466  return s;
467  }
468 
469  for (i = 0; s[i] != '\0'; i++) {
470  s[i] = static_cast<char>(toupper(s[i]));
471  }
472 
473  return s;
474 }
475 
476 // =========================================================================
477 // Other Macros
478 // =========================================================================
479 
480 // ------------------------------------------------------------------------- //
481 template <class T> inline int UtilGetSize(const std::vector<T>& vec)
482 {
483  return static_cast<int>(vec.size());
484 }
485 
486 // ------------------------------------------------------------------------- //
487 inline bool UtilIsInSet(const int value,
488  const int* set,
489  const int setSize)
490 {
491  int i;
492  bool inSet = false;
493 
494  for (i = 0; i < setSize; i++) {
495  if (set[i] == value) {
496  inSet = true;
497  break;
498  }
499  }
500 
501  return inSet;
502 }
503 
504 // ------------------------------------------------------------------------- //
505 inline int UtilNumNonzeros(const double* x,
506  const int len,
507  const double etol = 1.0e-8)
508 {
509  int i;
510  int nzs = 0;
511 
512  for (i = 0; i < len; i++) {
513  if (fabs(x[i]) > etol) {
514  nzs++;
515  }
516  }
517 
518  return nzs;
519 }
520 
521 // ------------------------------------------------------------------------- //
522 inline double UtilFracPart(const double x)
523 {
524  double floor_x = floor(x);
525  double floor_xplus = floor(x + 0.5);
526 
527  if (fabs(floor_xplus - x) < (UtilEpsilon * (fabs(floor_xplus) + 1.0))) {
528  return 0.0;
529  }
530 
531  return x - floor_x;
532 }
533 
534 // ------------------------------------------------------------------------- //
535 int UtilScaleDblToIntArr(const int arrLen,
536  const double* arrDbl,
537  int* arrInt,
538  const double oneDbl,
539  int* oneInt,
540  const double epstol = UtilEpsilon);
541 
542 // ------------------------------------------------------------------------- //
543 int UtilScaleDblToIntArr(const int arrLen,
544  const double* arrDbl,
545  int* arrInt,
546  const double epstol = UtilEpsilon);
547 
548 // ------------------------------------------------------------------------- //
549 inline bool UtilIsZero(const double x,
550  const double etol = 1.0e-8)
551 {
552  return fabs(x) < etol;
553 }
554 
555 // ------------------------------------------------------------------------- //
556 inline std::string UtilIntToStr(const int i)
557 {
558  std::stringstream ss;
559  ss << i;
560  return ss.str();
561 }
562 
563 // ------------------------------------------------------------------------- //
564 inline std::string UtilDblToStr(const double x,
565  const int precision = -1,
566  const double tooBig = UtilSmallerThanTooBig)
567 {
568  std::stringstream ss;
569 
570  if (fabs(x) > tooBig) {
571  if (x < 0) {
572  ss << "-INF";
573  } else {
574  ss << " INF";
575  }
576  } else {
577  if (precision >= 0) {
578  ss << std::setiosflags(std::ios::fixed | std::ios::showpoint);
579  ss << std::setprecision(precision);
580  }
581 
582  ss << x;
583  }
584 
585  return ss.str();
586 }
587 
588 // ------------------------------------------------------------------------- //
589 inline void UtilPrintMemUsage(std::ostream* os = &std::cout,
590  int logLevel = 0,
591  int logLimit = 2)
592 {
593  // This doesn't build in gcc 4.5 (at least on MinGW)
594 #if 0
595 #if not defined(_MSC_VER)
596 
597  if (logLevel >= logLimit) {
598  struct mallinfo memInfo = mallinfo();
599  double memUsage = static_cast<double>(memInfo.uordblks +
600  memInfo.hblkhd) / 1024.0;
601  memUsage /= 1024.0;
602  (*os) << "memUsage = " << UtilDblToStr(memUsage, 2) << " MB\n";
603  }
604 
605 #endif
606 #endif
607 }
608 
609 // ------------------------------------------------------------------------- //
610 template <class T>
611 void UtilDeleteVectorPtr(std::vector<T*>& vectorPtr,
612  typename std::vector<T*>::iterator first,
613  typename std::vector<T*>::iterator last)
614 {
615  typename std::vector<T*>::iterator it;
616 
617  for (it = first; it != last; it++) {
618  delete *it;
619  }
620 
621  vectorPtr.erase(first, last);
622 }
623 
624 // ------------------------------------------------------------------------- //
625 template <class T> void UtilDeleteVectorPtr(std::vector<T*>& vectorPtr)
626 {
627  UtilDeleteVectorPtr(vectorPtr, vectorPtr.begin(), vectorPtr.end());
628 }
629 
630 // ------------------------------------------------------------------------- //
631 template <class T> void UtilDeleteListPtr(std::list<T*>& listPtr,
632  typename std::list<T*>::iterator first,
633  typename std::list<T*>::iterator last)
634 {
635  typename std::list<T*>::iterator it;
636 
637  for (it = first; it != last; it++) {
638  delete *it;
639  }
640 
641  listPtr.erase(first, last);
642 }
643 
644 // ------------------------------------------------------------------------- //
645 template <class T> void UtilDeleteListPtr(std::list<T*>& listPtr)
646 {
647  UtilDeleteListPtr(listPtr, listPtr.begin(), listPtr.end());
648 }
649 
650 // ------------------------------------------------------------------------- //
651 template <class S, class T>
652 void UtilDeleteMapPtr(std::map<S, T*>& mapPtr,
653  typename std::map<S, T*>::iterator first,
654  typename std::map<S, T*>::iterator last)
655 {
656  typename std::map<S, T*>::iterator it;
657 
658  for (it = first; it != last; it++) {
659  delete (*it).second;
660  }
661 
662  mapPtr.erase(first, last);
663 }
664 
665 // ------------------------------------------------------------------------- //
666 template <class S, class T> void UtilDeleteMapPtr(std::map<S, T*>& mapPtr)
667 {
668  UtilDeleteMapPtr(mapPtr, mapPtr.begin(), mapPtr.end());
669 }
670 
671 // ------------------------------------------------------------------------- //
672 template <class S, class T>
673 void UtilDeleteMapVecPtr(std::map<S, std::vector<T*> >& mapPtr,
674  typename std::map<S, std::vector<T*> >::iterator first,
675  typename std::map<S, std::vector<T*> >::iterator last)
676 {
677  typename std::map<S, std::vector<T*> >::iterator it;
678 
679  for (it = first; it != last; it++) {
680  UtilDeleteVectorPtr((*it).second);
681  }
682 
683  mapPtr.erase(first, last);
684 }
685 
686 // ------------------------------------------------------------------------- //
687 template <class S, class T>
688 void UtilDeleteMapVecPtr(std::map<S, std::vector<T*> >& mapPtr)
689 {
690  UtilDeleteMapVecPtr(mapPtr, mapPtr.begin(), mapPtr.end());
691 }
692 
693 // ------------------------------------------------------------------------- //
694 inline bool UtilIsIntegral(const double x,
695  const double etol = 1.0e-10)
696 {
697  return UtilIsZero(x - floor(x), etol) || UtilIsZero(ceil(x) - x, etol);
698 }
699 
700 // ------------------------------------------------------------------------- //
701 inline bool UtilIsIntegral(const double* x,
702  const int len,
703  const double etol = 1.0e-10)
704 {
705  int i;
706 
707  for (i = 0; i < len; i++) {
708  if (!UtilIsIntegral(x[i], etol)) {
709  return false;
710  }
711  }
712 
713  return true;
714 }
715 
716 // ------------------------------------------------------------------------- //
717 template <class T> inline void UtilNegateArr(const int arrLen,
718  T* arr)
719 {
720  transform(arr, arr + arrLen, arr, std::negate<T>());
721 }
722 
723 // ------------------------------------------------------------------------- //
724 template <class T>
725 struct AddOffset : public std::unary_function<T, T> {
726  T m_n;
727  T operator() (const T& k) {
728  return k + m_n;
729  }
730  AddOffset(T n) : m_n (n) {};
731 };
732 
733 // ------------------------------------------------------------------------- //
734 template <class T> inline void UtilAddOffsetArr(const int arrLen,
735  T offset,
736  T* arr)
737 {
738  transform(arr, arr + arrLen, arr, AddOffset<T>(offset));
739 }
740 
741 // ------------------------------------------------------------------------- //
742 struct Perturb { //: public unary_function
743  double m_randLB;
744  double m_randUB;
745  double operator() (const double& k) {
746  return k + UtilURand(m_randLB, m_randUB);
747  }
748  Perturb(double randLB, double randUB) :
749  m_randLB(randLB), m_randUB(randUB) {};
750 };
751 
752 // ------------------------------------------------------------------------- //
753 inline void UtilPerturbCost(const int seed,
754  const int arrLen,
755  const double randLB,
756  const double randUB,
757  double* arr)
758 {
759  srand(seed);
760  std::transform(arr, arr + arrLen, arr, Perturb(randLB, randUB));
761 }
762 
763 // ------------------------------------------------------------------------- //
764 inline void UtilFlipRowLtoG(const int len,
765  double* els,
766  char& sense,
767  double& rhs)
768 {
769  if (sense == 'L') {
770  return;
771  }
772 
773  if (sense == 'G') {
774  for (int i = 0; i < len; i++) {
775  els[i] = -els[i];
776  }
777 
778  sense = 'L';
779  rhs = -rhs;
780  }
781 
782  assert(0);
783 }
784 
785 // ------------------------------------------------------------------------- //
786 inline void UtilBoundToSense(const double lb,
787  const double ub,
788  const double inf,
789  char& sense,
790  double& rhs,
791  double& range)
792 {
793  range = 0.0;
794 
795  if (lb > -inf) {
796  if (ub < inf) {
797  rhs = ub;
798 
799  if (UtilIsZero(ub - lb)) {
800  sense = 'E';
801  } else {
802  sense = 'R';
803  range = ub - lb;
804  }
805  } else {
806  sense = 'G';
807  rhs = lb;
808  }
809  } else {
810  if (ub < inf) {
811  sense = 'L';
812  rhs = ub;
813  } else {
814  sense = 'N';
815  rhs = 0.0;
816  }
817  }
818 }
819 
820 // ------------------------------------------------------------------------- //
821 inline void UtilSenseToBound(const char sense,
822  const double rhs,
823  const double range,
824  const double inf,
825  double& lb,
826  double& ub)
827 {
828  switch (sense) {
829  case 'E':
830  lb = rhs;
831  ub = rhs;
832  break;
833  case 'L':
834  lb = -inf;
835  ub = rhs;
836  break;
837  case 'G':
838  lb = rhs;
839  ub = inf;
840  break;
841  case 'R':
842  lb = rhs - range;
843  ub = rhs;
844  break;
845  case 'N':
846  lb = -inf;
847  ub = inf;
848  break;
849  }
850 }
851 
852 // --------------------------------------------------------------------- //
853 template<class S, class T> class UtilIsGreaterThan {
854 public:
855  bool operator()( const std::pair<S, T>& x,
856  const std::pair<S, T>& y) {
857  return x.second > y.second;
858  }
859 };
860 
861 // --------------------------------------------------------------------- //
862 template<class S, class T> class UtilIsLessThan {
863 public:
864  bool operator()( const std::pair<S, T>& x,
865  const std::pair<S, T>& y) {
866  return x.second < y.second;
867  }
868 };
869 
870 // ------------------------------------------------------------------------- //
871 inline std::string UtilDirSlash()
872 {
873  std::string slash;
874 #if defined(_MSC_VER)
875  slash = "\\";
876 #else
877  slash = "/";
878 #endif
879  return slash;
880 }
881 
882 // ------------------------------------------------------------------------- //
883 inline int UtilOpenFile(std::ofstream& os,
884  const char* fileName)
885 {
886  int status = UtilStatusOk;
887  os.open(fileName);
888 
889  if (!os) {
890  std::string errMessage = "Error: Filename = ";
891  errMessage += fileName;
892  errMessage += " failed to open.";
893  std::cerr << errMessage.c_str() << std::endl;
894  fflush(stdout);
895  status = UtilStatusFileIO;
896  assert(os);
897  }
898 
899  return status;
900 }
901 
902 // ------------------------------------------------------------------------- //
903 inline int UtilOpenFile(std::ifstream& is,
904  const char* fileName)
905 {
906  int status = UtilStatusOk;
907  is.open(fileName);
908 
909  if (!is) {
910  std::string errMessage = "Error: Filename = ";
911  errMessage += fileName;
912  errMessage += " failed to open.";
913  std::cerr << errMessage.c_str() << std::endl;
914  fflush(stdout);
915  status = UtilStatusFileIO;
916  assert(is);
917  }
918 
919  return status;
920 }
921 
922 // ------------------------------------------------------------------------- //
923 inline int UtilOpenFile(std::ofstream& os,
924  const std::string& fileName)
925 {
926  return UtilOpenFile(os, fileName.c_str());
927 }
928 
929 // ------------------------------------------------------------------------- //
930 inline int UtilOpenFile(std::ifstream& is,
931  const std::string& fileName)
932 {
933  return UtilOpenFile(is, fileName.c_str());
934 }
935 
936 
937 
938 #endif
int UtilIndexU(const int i, const int j)
Definition: UtilMacros.h:134
void UtilSenseToBound(const char sense, const double rhs, const double range, const double inf, double &lb, double &ub)
Definition: UtilMacros.h:440
void UtilDeleteVectorPtr(vector< T * > &vectorPtr, typename vector< T * >::iterator first, typename vector< T * >::iterator last)
Definition: UtilMacros.h:288
std::string UtilStringRandom(int iLength)
Definition: UtilMacros.h:385
int UtilNumNonzeros(const double *x, const int len, const double etol=1.0e-8)
Definition: UtilMacros.h:505
void UtilNegateArr(const int arrLen, T *arr)
Definition: UtilMacros.h:335
bool UtilIsZero(const double x, const double etol=1.0e-8)
Definition: UtilMacros.h:272
void UtilPrintList(const list< T > &v, ostream *os=&cout)
Definition: UtilMacros.h:52
double m_randLB
Definition: UtilMacros.h:361
std::string UtilDblToStr(const double x, const int precision=-1, const double tooBig=UtilSmallerThanTooBig)
Definition: UtilMacros.h:564
int UtilNumEdgesU(const int n)
Definition: UtilMacros.h:128
void UtilBoundToSense(const double lb, const double ub, const double inf, char &sense, double &rhs, double &range)
Definition: UtilMacros.h:405
const double UtilTooBig
Definition: UtilMacros.h:42
pair< int, int > UtilBothEndsU(const int index)
void UtilPerturbCost(const int seed, const int arrLen, const double randLB, const double randUB, double *arr)
Definition: UtilMacros.h:371
double UtilAve(const vector< double > &x)
Definition: UtilMacros.h:220
#define UtilAssert(expression, errorMsg, os)
Definition: UtilMacros.h:85
void UtilStringTokenize(std::string const &input, std::string const &delimiters, std::vector< std::string > &tokens)
Definition: UtilMacros.h:364
string & UtilStrToUpper(string &s)
Definition: UtilMacros.h:590
void UtilPrintEdge(const int index, ostream *os=&cout)
Definition: UtilMacros.h:143
string UtilDirSlash()
Definition: UtilMacros.h:514
void UtilIotaN(int *first, const int size, const int init)
Definition: UtilMacros.h:171
void UtilDeleteListPtr(list< T * > &listPtr, typename list< T * >::iterator first, typename list< T * >::iterator last)
Definition: UtilMacros.h:308
void UtilOpenFile(ifstream &fs, const char *fileName)
Definition: UtilMacros.h:526
bool operator()(const std::pair< S, T > &x, const std::pair< S, T > &y)
Definition: UtilMacros.h:864
const double UtilSmallerThanTooBig
Definition: UtilMacros.h:43
void UtilPrintVector(const vector< T > &v, ostream *os=&cout)
Definition: UtilMacros.h:39
#define srand(x)
Definition: mcknap.h:40
std::string UtilEdgeToStr(const int index)
Definition: UtilMacros.h:247
bool UtilIsInSet(const int value, const int *set, const int setSize)
Definition: UtilMacros.h:487
double UtilFracPart(const double x)
Definition: UtilMacros.h:243
void UtilAddOffsetArr(const int arrLen, T offset, T *arr)
Definition: UtilMacros.h:352
void UtilPrintMemUsage(std::ostream *os=&std::cout, int logLevel=0, int logLimit=2)
Definition: UtilMacros.h:589
void UtilFillN(T *to, const int size, const T value)
Definition: UtilMacros.h:158
T operator()(const T &k)
Definition: UtilMacros.h:345
string UtilIntToStr(const int i)
Definition: UtilMacros.h:279
UtilStatus
Definition: UtilMacros.h:57
Perturb(double randLB, double randUB)
Definition: UtilMacros.h:748
int UtilGetSize(const std::vector< T > &vec)
Definition: UtilMacros.h:481
double m_randUB
Definition: UtilMacros.h:362
double UtilNormRand(const double mean, const double sigma)
Definition: UtilMacros.h:323
bool UtilIsIntegral(const double x, const double etol=1.0e-10)
Definition: UtilMacros.h:328
bool operator()(const std::pair< S, T > &x, const std::pair< S, T > &y)
Definition: UtilMacros.h:855
const double UtilEpsilon
Definition: UtilMacros.h:22
double UtilURand(const double a, const double b)
Definition: UtilMacros.h:202
AddOffset(T n)
Definition: UtilMacros.h:730
void UtilPrintParameter(std::ostream *os, const std::string &section, const std::string &name, const int value)
Definition: UtilMacros.h:102
string & UtilStrTrim(string &s, const string &t=UtilSpaces)
Definition: UtilMacros.h:545
void UtilDeleteMapPtr(std::map< S, T * > &mapPtr, typename std::map< S, T * >::iterator first, typename std::map< S, T * >::iterator last)
Definition: UtilMacros.h:652
void UtilFlipRowLtoG(const int len, double *els, char &sense, double &rhs)
Definition: UtilMacros.h:382
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)
Definition: UtilMacros.h:673
string & UtilStrToLower(string &s)
Definition: UtilMacros.h:570
const string UtilSpaces
Definition: UtilMacros.h:21
double operator()(const double &k)
Definition: UtilMacros.h:363