Go to the documentation of this file.
6 #ifndef CoinHelperFunctions_H
7 #define CoinHelperFunctions_H
14 # define getcwd _getcwd
29 #ifdef COIN_USE_RESTRICT
30 #define COIN_RESTRICT __restrict
43 template <
class T>
inline void
44 CoinCopyN(
register const T* from,
const int size,
register T* to)
46 if (size == 0 || from == to)
51 throw CoinError(
"trying to copy negative number of entries",
55 register int n = (size + 7) / 8;
57 register const T* downfrom = from + size;
58 register T* downto = to + size;
61 case 0:
do{ *--downto = *--downfrom;
62 case 7: *--downto = *--downfrom;
63 case 6: *--downto = *--downfrom;
64 case 5: *--downto = *--downfrom;
65 case 4: *--downto = *--downfrom;
66 case 3: *--downto = *--downfrom;
67 case 2: *--downto = *--downfrom;
68 case 1: *--downto = *--downfrom;
76 case 0:
do{ *++to = *++from;
77 case 7: *++to = *++from;
78 case 6: *++to = *++from;
79 case 5: *++to = *++from;
80 case 4: *++to = *++from;
81 case 3: *++to = *++from;
82 case 2: *++to = *++from;
83 case 1: *++to = *++from;
101 template <
class T>
inline void
102 CoinCopy(
register const T* first,
register const T* last,
register T* to)
104 CoinCopyN(first,
static_cast<int>(last-first), to);
116 template <
class T>
inline void
120 if (size == 0 || from == to)
125 throw CoinError(
"trying to copy negative number of entries",
126 "CoinDisjointCopyN",
"");
133 const long dist = to - from;
134 if (-size < dist && dist < size)
135 throw CoinError(
"overlapping arrays",
"CoinDisjointCopyN",
"");
138 for (
register int n = size / 8; n > 0; --n, from += 8, to += 8) {
149 case 7: to[6] = from[6];
150 case 6: to[5] = from[5];
151 case 5: to[4] = from[4];
152 case 4: to[3] = from[3];
153 case 3: to[2] = from[2];
154 case 2: to[1] = from[1];
155 case 1: to[0] = from[0];
169 template <
class T>
inline void
182 template <
class T>
inline T*
186 T * arrayNew =
new T[size];
187 std::memcpy(arrayNew,array,size*
sizeof(T));
199 template <
class T>
inline T*
203 T * arrayNew =
new T[size];
204 assert (copySize<=size);
205 std::memcpy(arrayNew,array,copySize*
sizeof(T));
216 template <
class T>
inline T*
219 T * arrayNew =
new T[size];
221 std::memcpy(arrayNew,array,size*
sizeof(T));
235 template <
class T>
inline T*
238 T * arrayNew =
new T[size];
240 std::memcpy(arrayNew,array,size*
sizeof(T));
242 std::memset(arrayNew,0,size*
sizeof(T));
257 #ifndef COIN_USE_RESTRICT
258 template <
class T>
inline void
259 CoinMemcpyN(
register const T* from,
const int size,
register T* to)
267 throw CoinError(
"trying to copy negative number of entries",
274 const long dist = to - from;
275 if (-size < dist && dist < size)
276 throw CoinError(
"overlapping arrays",
"CoinMemcpyN",
"");
279 std::memcpy(to,from,size*
sizeof(T));
281 if (size == 0 || from == to)
286 throw CoinError(
"trying to copy negative number of entries",
294 const long dist = to - from;
295 if (-size < dist && dist < size)
296 throw CoinError(
"overlapping arrays",
"CoinMemcpyN",
"");
299 for (
register int n = size / 8; n > 0; --n, from += 8, to += 8) {
310 case 7: to[6] = from[6];
311 case 6: to[5] = from[5];
312 case 5: to[4] = from[4];
313 case 4: to[3] = from[3];
314 case 3: to[2] = from[2];
315 case 2: to[1] = from[1];
316 case 1: to[0] = from[0];
325 template <
class T>
inline void
329 std::memcpy(to,from,size*
sizeof(T));
333 for ( ; 0<size ; --size)
345 template <
class T>
inline void
349 CoinMemcpyN(first,
static_cast<int>(last - first), to);
360 template <
class T>
inline void
361 CoinFillN(
register T* to,
const int size,
register const T value)
368 throw CoinError(
"trying to fill negative number of entries",
372 for (
register int n = size / 8; n > 0; --n, to += 8) {
383 case 7: to[6] = value;
384 case 6: to[5] = value;
385 case 5: to[4] = value;
386 case 4: to[3] = value;
387 case 3: to[2] = value;
388 case 2: to[1] = value;
389 case 1: to[0] = value;
394 register int n = (size + 7) / 8;
397 case 0:
do{ *++to = value;
398 case 7: *++to = value;
399 case 6: *++to = value;
400 case 5: *++to = value;
401 case 4: *++to = value;
402 case 3: *++to = value;
403 case 2: *++to = value;
404 case 1: *++to = value;
415 template <
class T>
inline void
416 CoinFill(
register T* first,
register T* last,
const T value)
429 template <
class T>
inline void
437 throw CoinError(
"trying to fill negative number of entries",
440 memset(to,0,size*
sizeof(T));
447 throw CoinError(
"trying to fill negative number of entries",
451 for (
register int n = size / 8; n > 0; --n, to += 8) {
473 register int n = (size + 7) / 8;
476 case 0:
do{ *++to = 0;
494 for (
int j=0;j<size;j++) {
499 printf(
"array of length %d should be zero has %d nonzero\n",size,n);
507 for (
int j=0;j<size;j++) {
512 printf(
"array of length %d should be zero has %d nonzero\n",size,n);
521 template <
class T>
inline void
534 const int len =
static_cast<int>(strlen(name));
535 dup =
static_cast<char*
>(malloc(len+1));
547 template <
class T>
inline T
548 CoinMax(
register const T x1,
register const T x2)
550 return (x1 > x2) ? x1 : x2;
558 template <
class T>
inline T
559 CoinMin(
register const T x1,
register const T x2)
561 return (x1 < x2) ? x1 : x2;
569 template <
class T>
inline T
572 return value<0 ? -value : value;
580 template <
class T>
inline bool
588 throw CoinError(
"negative number of entries",
"CoinIsSorted",
"");
592 const int size1 = size - 1;
593 for (
register int n = size1 / 8; n > 0; --n, first += 8) {
594 if (first[8] < first[7])
return false;
595 if (first[7] < first[6])
return false;
596 if (first[6] < first[5])
return false;
597 if (first[5] < first[4])
return false;
598 if (first[4] < first[3])
return false;
599 if (first[3] < first[2])
return false;
600 if (first[2] < first[1])
return false;
601 if (first[1] < first[0])
return false;
605 case 7:
if (first[7] < first[6])
return false;
606 case 6:
if (first[6] < first[5])
return false;
607 case 5:
if (first[5] < first[4])
return false;
608 case 4:
if (first[4] < first[3])
return false;
609 case 3:
if (first[3] < first[2])
return false;
610 case 2:
if (first[2] < first[1])
return false;
611 case 1:
if (first[1] < first[0])
return false;
615 register const T* next = first;
616 register const T* last = first + size;
617 for (++next; next != last; first = next, ++next)
629 template <
class T>
inline bool
632 return CoinIsSorted(first,
static_cast<int>(last - first));
640 template <
class T>
inline void
641 CoinIotaN(
register T* first,
const int size,
register T init)
648 throw CoinError(
"negative number of entries",
"CoinIotaN",
"");
651 for (
register int n = size / 8; n > 0; --n, first += 8, init += 8) {
662 case 7: first[6] = init + 6;
663 case 6: first[5] = init + 5;
664 case 5: first[4] = init + 4;
665 case 4: first[3] = init + 3;
666 case 3: first[2] = init + 2;
667 case 2: first[1] = init + 1;
668 case 1: first[0] = init;
673 register int n = (size + 7) / 8;
677 case 0:
do{ *++first = ++init;
678 case 7: *++first = ++init;
679 case 6: *++first = ++init;
680 case 5: *++first = ++init;
681 case 4: *++first = ++init;
682 case 3: *++first = ++init;
683 case 2: *++first = ++init;
684 case 1: *++first = ++init;
695 template <
class T>
inline void
708 template <
class T>
inline T *
710 const int * firstDelPos,
const int * lastDelPos)
712 int delNum =
static_cast<int>(lastDelPos - firstDelPos);
717 throw CoinError(
"trying to delete negative number of entries",
718 "CoinDeleteEntriesFromArray",
"");
720 int * delSortedPos = NULL;
722 std::adjacent_find(firstDelPos, lastDelPos) == lastDelPos)) {
724 delSortedPos =
new int[delNum];
726 std::sort(delSortedPos, delSortedPos + delNum);
727 delNum =
static_cast<int>(std::unique(delSortedPos,
728 delSortedPos+delNum) - delSortedPos);
730 const int * delSorted = delSortedPos ? delSortedPos : firstDelPos;
732 const int last = delNum - 1;
733 int size = delSorted[0];
734 for (
int i = 0; i < last; ++i) {
735 const int copyFirst = delSorted[i] + 1;
736 const int copyLast = delSorted[i+1];
737 CoinCopy(arrayFirst + copyFirst, arrayFirst + copyLast,
739 size += copyLast - copyFirst;
741 const int copyFirst = delSorted[last] + 1;
742 const int copyLast =
static_cast<int>(arrayLast - arrayFirst);
743 CoinCopy(arrayFirst + copyFirst, arrayFirst + copyLast,
745 size += copyLast - copyFirst;
748 delete[] delSortedPos;
750 return arrayFirst + size;
755 #define COIN_OWN_RANDOM_32
757 #if defined COIN_OWN_RANDOM_32
773 inline double CoinDrand48 (
bool isSeed =
false,
unsigned int seed = 1)
775 static unsigned int last = 123456;
779 last = 1664525*last+1013904223;
780 return ((
static_cast<double> (last))/4294967296.0);
791 #else // COIN_OWN_RANDOM_32
793 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN32__)
796 inline double CoinDrand48() {
return rand() / (double) RAND_MAX; }
809 #endif // COIN_OWN_RANDOM_32
820 buf =
new char[size];
821 if (getcwd(buf, size))
829 char dirsep = buf[0] ==
'/' ?
'/' :
'\\';
838 for (
size_t i = 0; i < len; ++i) {
840 return s1[i] == 0 ? 0 : -1;
845 const int c0 = std::tolower(s0[i]);
846 const int c1 = std::tolower(s1[i]);
858 template <
class T>
inline void CoinSwap (T &x, T &y)
871 template <
class T>
inline int
877 static_cast<CoinBigIndex>(fwrite(&size,
sizeof(
int),1,fp));
878 if (numberWritten!=1)
881 static_cast<CoinBigIndex>(fwrite(array,
sizeof(T),
size_t(size),fp));
882 if (numberWritten!=size)
887 static_cast<CoinBigIndex>(fwrite(&size,
sizeof(
int),1,fp));
888 if (numberWritten!=1)
902 template <
class T>
inline int
907 static_cast<CoinBigIndex>(fread(&newSize,
sizeof(
int),1,fp));
911 if (size!=newSize&&(newSize||array))
914 array =
new T [newSize];
916 static_cast<CoinBigIndex>(fread(array,
sizeof(T),newSize,fp));
917 if (numberRead!=newSize)
929 inline double CoinCbrt(
double x)
931 #if defined(_MSC_VER)
932 return pow(x,(1./3.));
942 #define CoinSizeofAsInt(type) (static_cast<int>(sizeof(type)))
947 return static_cast<int>(strlen(
string));
952 #if defined COIN_OWN_RANDOM_32
1000 retVal = ((
static_cast<double> (
seed_))/4294967296.0);
1008 for (
int i=0;i<n;i++)
1018 mutable unsigned int seed_;
1033 { memcpy(
seed_,seed,3*
sizeof(
unsigned short));}
1037 union {
int i[2];
unsigned short int s[4];} put;
1040 memcpy(
seed_,put.s,3*
sizeof(
unsigned short));
1046 { memcpy(
seed_,rhs.
seed_,3*
sizeof(
unsigned short));}
1051 memcpy(
seed_,rhs.
seed_,3*
sizeof(
unsigned short));
1062 inline void setSeed(
const unsigned short seed[3])
1063 { memcpy(
seed_,seed,3*
sizeof(
unsigned short));}
1067 union {
int i[2];
unsigned short int s[4];} put;
1070 memcpy(
seed_,put.s,3*
sizeof(
unsigned short));
1076 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN32__)
1078 retVal=retVal/(double) RAND_MAX;
1080 retVal = erand48(
seed_);
1091 for (
int i=0;i<n;i++)
1101 mutable unsigned short seed_[3];
1107 #define COIN_DETAIL_PRINT(s) {}
1109 #define COIN_DETAIL_PRINT(s) s
void CoinZeroN(register T *to, const int size)
This helper function fills an array with zero.
int CoinStrNCaseCmp(const char *s0, const char *s1, const size_t len)
void CoinDisjointCopyN(register const T *from, const int size, register T *to)
This helper function copies an array to another location.
T * CoinCopyOfArrayPartial(const T *array, const int size, const int copySize)
Return an array of length size filled with first copySize from array, or null if array is null.
void CoinMemcpyN(register const T *from, const int size, register T *to)
This helper function copies an array to another location.
void CoinFill(register T *first, register T *last, const T value)
This helper function fills an array with a given value.
void randomize(int n=0)
make more random (i.e. for startup)
double randomDouble() const
return a random number
void CoinCopy(register const T *first, register const T *last, register T *to)
This helper function copies an array to another location using Duff's device (for a speedup of ~2).
void CoinZero(register T *first, register T *last)
This helper function fills an array with a given value.
void CoinCopyN(register const T *from, const int size, register T *to)
This helper function copies an array to another location using Duff's device (for a speedup of ~2).
void CoinDisjointCopy(register const T *first, register const T *last, register T *to)
This helper function copies an array to another location.
Class for thread specific random numbers.
T CoinAbs(const T value)
Return the absolute value of the argument.
int CoinToFile(const T *array, CoinBigIndex size, FILE *fp)
This helper function copies an array to file Returns 0 if OK, 1 if bad write.
void CoinCheckIntZero(int *to, const int size)
This Debug helper function checks an array is all zero.
unsigned int seed_
Current seed.
T CoinMin(register const T x1, register const T x2)
Return the smaller (according to operator<() of the arguments.
CoinThreadRandom()
Default constructor.
void setSeed(int seed)
Set seed.
CoinThreadRandom(int seed)
Constructor wih seed.
void CoinFillN(register T *to, const int size, register const T value)
This helper function fills an array with a given value.
char * CoinStrdup(const char *name)
Returns strdup or NULL if original NULL.
void CoinIota(T *first, const T *last, T init)
This helper function fills an array with the values init, init+1, init+2, etc.
void CoinCheckDoubleZero(double *to, const int size)
This Debug helper function checks an array is all zero.
T * CoinCopyOfArray(const T *array, const int size)
Return an array of length size filled with input from array, or null if array is null.
char CoinFindDirSeparator()
This function figures out whether file names should contain slashes or backslashes as directory separ...
bool CoinIsSorted(register const T *first, const int size)
This helper function tests whether the entries of an array are sorted according to operator<.
void CoinMemcpy(register const T *first, register const T *last, register T *to)
This helper function copies an array to another location.
CoinThreadRandom & operator=(const CoinThreadRandom &rhs)
T * CoinDeleteEntriesFromArray(register T *arrayFirst, register T *arrayLast, const int *firstDelPos, const int *lastDelPos)
This helper function deletes certain entries from an array.
CoinThreadRandom(const CoinThreadRandom &rhs)
T * CoinCopyOfArrayOrZero(const T *array, const int size)
Return an array of length size filled with input from array, or filled with zero if array is null.
unsigned int getSeed() const
Get seed.
int CoinFromFile(T *&array, CoinBigIndex size, FILE *fp, CoinBigIndex &newSize)
This helper function copies an array from file and creates with new.
Error Class thrown by an exception.
~CoinThreadRandom()
Destructor.
void CoinSwap(T &x, T &y)
Swap the arguments.
void CoinIotaN(register T *first, const int size, register T init)
This helper function fills an array with the values init, init+1, init+2, etc.
T CoinMax(register const T x1, register const T x2)
Return the larger (according to operator<() of the arguments.
double CoinDrand48(bool isSeed=false, unsigned int seed=1)
Return a random number between 0 and 1.
int CoinStrlenAsInt(const char *string)
This helper returns "strlen" as an int.
void CoinSeedRandom(int iseed)
Set the seed for the random number generator.