6 #ifndef CoinHelperFunctions_H
7 #define CoinHelperFunctions_H
14 # define getcwd _getcwd
28 #ifdef COIN_USE_RESTRICT
29 #define COIN_RESTRICT __restrict
42 template <
class T>
inline void
43 CoinCopyN(
register const T* from,
const int size,
register T* to)
45 if (size == 0 || from == to)
50 throw CoinError(
"trying to copy negative number of entries",
54 register int n = (size + 7) / 8;
56 register const T* downfrom = from + size;
57 register T* downto = to + size;
60 case 0:
do{ *--downto = *--downfrom;
61 case 7: *--downto = *--downfrom;
62 case 6: *--downto = *--downfrom;
63 case 5: *--downto = *--downfrom;
64 case 4: *--downto = *--downfrom;
65 case 3: *--downto = *--downfrom;
66 case 2: *--downto = *--downfrom;
67 case 1: *--downto = *--downfrom;
75 case 0:
do{ *++to = *++from;
76 case 7: *++to = *++from;
77 case 6: *++to = *++from;
78 case 5: *++to = *++from;
79 case 4: *++to = *++from;
80 case 3: *++to = *++from;
81 case 2: *++to = *++from;
82 case 1: *++to = *++from;
100 template <
class T>
inline void
101 CoinCopy(
register const T* first,
register const T* last,
register T* to)
103 CoinCopyN(first, static_cast<int>(last-first), to);
115 template <
class T>
inline void
119 if (size == 0 || from == to)
124 throw CoinError(
"trying to copy negative number of entries",
125 "CoinDisjointCopyN",
"");
132 const long dist = to - from;
133 if (-size < dist && dist < size)
134 throw CoinError(
"overlapping arrays",
"CoinDisjointCopyN",
"");
137 for (
register int n = size / 8; n > 0; --n, from += 8, to += 8) {
148 case 7: to[6] = from[6];
149 case 6: to[5] = from[5];
150 case 5: to[4] = from[4];
151 case 4: to[3] = from[3];
152 case 3: to[2] = from[2];
153 case 2: to[1] = from[1];
154 case 1: to[0] = from[0];
168 template <
class T>
inline void
181 template <
class T>
inline T*
185 T * arrayNew =
new T[size];
186 std::memcpy(arrayNew,array,size*
sizeof(T));
198 template <
class T>
inline T*
202 T * arrayNew =
new T[size];
203 assert (copySize<=size);
204 std::memcpy(arrayNew,array,copySize*
sizeof(T));
215 template <
class T>
inline T*
218 T * arrayNew =
new T[size];
220 std::memcpy(arrayNew,array,size*
sizeof(T));
234 template <
class T>
inline T*
237 T * arrayNew =
new T[size];
239 std::memcpy(arrayNew,array,size*
sizeof(T));
241 std::memset(arrayNew,0,size*
sizeof(T));
256 #ifndef COIN_USE_RESTRICT
257 template <
class T>
inline void
258 CoinMemcpyN(
register const T* from,
const int size,
register T* to)
266 throw CoinError(
"trying to copy negative number of entries",
273 const long dist = to - from;
274 if (-size < dist && dist < size)
275 throw CoinError(
"overlapping arrays",
"CoinMemcpyN",
"");
278 std::memcpy(to,from,size*
sizeof(T));
280 if (size == 0 || from == to)
285 throw CoinError(
"trying to copy negative number of entries",
293 const long dist = to - from;
294 if (-size < dist && dist < size)
295 throw CoinError(
"overlapping arrays",
"CoinMemcpyN",
"");
298 for (
register int n = size / 8; n > 0; --n, from += 8, to += 8) {
309 case 7: to[6] = from[6];
310 case 6: to[5] = from[5];
311 case 5: to[4] = from[4];
312 case 4: to[3] = from[3];
313 case 3: to[2] = from[2];
314 case 2: to[1] = from[1];
315 case 1: to[0] = from[0];
324 template <
class T>
inline void
328 std::memcpy(to,from,size*
sizeof(T));
332 for ( ; 0<size ; --size)
344 template <
class T>
inline void
348 CoinMemcpyN(first, static_cast<int>(last - first), to);
359 template <
class T>
inline void
360 CoinFillN(
register T* to,
const int size,
register const T value)
367 throw CoinError(
"trying to fill negative number of entries",
371 for (
register int n = size / 8; n > 0; --n, to += 8) {
382 case 7: to[6] = value;
383 case 6: to[5] = value;
384 case 5: to[4] = value;
385 case 4: to[3] = value;
386 case 3: to[2] = value;
387 case 2: to[1] = value;
388 case 1: to[0] = value;
393 register int n = (size + 7) / 8;
396 case 0:
do{ *++to = value;
397 case 7: *++to = value;
398 case 6: *++to = value;
399 case 5: *++to = value;
400 case 4: *++to = value;
401 case 3: *++to = value;
402 case 2: *++to = value;
403 case 1: *++to = value;
414 template <
class T>
inline void
415 CoinFill(
register T* first,
register T* last,
const T value)
428 template <
class T>
inline void
436 throw CoinError(
"trying to fill negative number of entries",
439 memset(to,0,size*
sizeof(T));
446 throw CoinError(
"trying to fill negative number of entries",
450 for (
register int n = size / 8; n > 0; --n, to += 8) {
472 register int n = (size + 7) / 8;
475 case 0:
do{ *++to = 0;
493 for (
int j=0;j<size;j++) {
498 printf(
"array of length %d should be zero has %d nonzero\n",size,n);
506 for (
int j=0;j<size;j++) {
511 printf(
"array of length %d should be zero has %d nonzero\n",size,n);
520 template <
class T>
inline void
533 const int len =
static_cast<int>(strlen(name));
534 dup =
static_cast<char*
>(malloc(len+1));
546 template <
class T>
inline T
547 CoinMax(
register const T x1,
register const T x2)
549 return (x1 > x2) ? x1 : x2;
557 template <
class T>
inline T
558 CoinMin(
register const T x1,
register const T x2)
560 return (x1 < x2) ? x1 : x2;
568 template <
class T>
inline T
571 return value<0 ? -value : value;
579 template <
class T>
inline bool
587 throw CoinError(
"negative number of entries",
"CoinIsSorted",
"");
591 const int size1 = size - 1;
592 for (
register int n = size1 / 8; n > 0; --n, first += 8) {
593 if (first[8] < first[7])
return false;
594 if (first[7] < first[6])
return false;
595 if (first[6] < first[5])
return false;
596 if (first[5] < first[4])
return false;
597 if (first[4] < first[3])
return false;
598 if (first[3] < first[2])
return false;
599 if (first[2] < first[1])
return false;
600 if (first[1] < first[0])
return false;
604 case 7:
if (first[7] < first[6])
return false;
605 case 6:
if (first[6] < first[5])
return false;
606 case 5:
if (first[5] < first[4])
return false;
607 case 4:
if (first[4] < first[3])
return false;
608 case 3:
if (first[3] < first[2])
return false;
609 case 2:
if (first[2] < first[1])
return false;
610 case 1:
if (first[1] < first[0])
return false;
614 register const T* next = first;
615 register const T* last = first + size;
616 for (++next; next != last; first = next, ++next)
628 template <
class T>
inline bool
631 return CoinIsSorted(first, static_cast<int>(last - first));
639 template <
class T>
inline void
640 CoinIotaN(
register T* first,
const int size,
register T init)
647 throw CoinError(
"negative number of entries",
"CoinIotaN",
"");
650 for (
register int n = size / 8; n > 0; --n, first += 8, init += 8) {
661 case 7: first[6] = init + 6;
662 case 6: first[5] = init + 5;
663 case 5: first[4] = init + 4;
664 case 4: first[3] = init + 3;
665 case 3: first[2] = init + 2;
666 case 2: first[1] = init + 1;
667 case 1: first[0] = init;
672 register int n = (size + 7) / 8;
676 case 0:
do{ *++first = ++init;
677 case 7: *++first = ++init;
678 case 6: *++first = ++init;
679 case 5: *++first = ++init;
680 case 4: *++first = ++init;
681 case 3: *++first = ++init;
682 case 2: *++first = ++init;
683 case 1: *++first = ++init;
694 template <
class T>
inline void
707 template <
class T>
inline T *
709 const int * firstDelPos,
const int * lastDelPos)
711 int delNum =
static_cast<int>(lastDelPos - firstDelPos);
716 throw CoinError(
"trying to delete negative number of entries",
717 "CoinDeleteEntriesFromArray",
"");
719 int * delSortedPos = NULL;
721 std::adjacent_find(firstDelPos, lastDelPos) == lastDelPos)) {
723 delSortedPos =
new int[delNum];
725 std::sort(delSortedPos, delSortedPos + delNum);
726 delNum =
static_cast<int>(std::unique(delSortedPos,
727 delSortedPos+delNum) - delSortedPos);
729 const int * delSorted = delSortedPos ? delSortedPos : firstDelPos;
731 const int last = delNum - 1;
732 int size = delSorted[0];
733 for (
int i = 0; i < last; ++i) {
734 const int copyFirst = delSorted[i] + 1;
735 const int copyLast = delSorted[i+1];
736 CoinCopy(arrayFirst + copyFirst, arrayFirst + copyLast,
738 size += copyLast - copyFirst;
740 const int copyFirst = delSorted[last] + 1;
741 const int copyLast =
static_cast<int>(arrayLast - arrayFirst);
742 CoinCopy(arrayFirst + copyFirst, arrayFirst + copyLast,
744 size += copyLast - copyFirst;
747 delete[] delSortedPos;
749 return arrayFirst + size;
754 #define COIN_OWN_RANDOM_32
756 #if defined COIN_OWN_RANDOM_32
772 inline double CoinDrand48 (
bool isSeed =
false,
unsigned int seed = 1)
774 static unsigned int last = 123456;
778 last = 1664525*last+1013904223;
779 return ((static_cast<double> (last))/4294967296.0);
790 #else // COIN_OWN_RANDOM_32
792 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN32__)
795 inline double CoinDrand48() {
return rand() / (double) RAND_MAX; }
808 #endif // COIN_OWN_RANDOM_32
819 buf =
new char[size];
820 if (getcwd(buf, size))
828 char dirsep = buf[0] ==
'/' ?
'/' :
'\\';
837 for (
size_t i = 0; i < len; ++i) {
839 return s1[i] == 0 ? 0 : -1;
844 const int c0 = std::tolower(s0[i]);
845 const int c1 = std::tolower(s1[i]);
857 template <
class T>
inline void CoinSwap (T &x, T &y)
870 template <
class T>
inline int
876 static_cast<CoinBigIndex>(fwrite(&size,
sizeof(
int),1,fp));
877 if (numberWritten!=1)
880 static_cast<CoinBigIndex>(fwrite(array,
sizeof(T),
size_t(size),fp));
881 if (numberWritten!=size)
886 static_cast<CoinBigIndex>(fwrite(&size,
sizeof(
int),1,fp));
887 if (numberWritten!=1)
901 template <
class T>
inline int
906 static_cast<CoinBigIndex>(fread(&newSize,
sizeof(
int),1,fp));
910 if (size!=newSize&&(newSize||array))
913 array =
new T [newSize];
915 static_cast<CoinBigIndex>(fread(array,
sizeof(T),newSize,fp));
916 if (numberRead!=newSize)
928 inline double CoinCbrt(
double x)
930 #if defined(_MSC_VER)
931 return pow(x,(1./3.));
941 #define CoinSizeofAsInt(type) (static_cast<int>(sizeof(type)))
946 return static_cast<int>(strlen(
string));
951 #if defined COIN_OWN_RANDOM_32
999 retVal = ((
static_cast<double> (
seed_))/4294967296.0);
1007 for (
int i=0;i<n;i++)
1017 mutable unsigned int seed_;
1032 { memcpy(
seed_,seed,3*
sizeof(
unsigned short));}
1036 union {
int i[2];
unsigned short int s[4];} put;
1039 memcpy(
seed_,put.s,3*
sizeof(
unsigned short));
1045 { memcpy(
seed_,rhs.
seed_,3*
sizeof(
unsigned short));}
1050 memcpy(
seed_,rhs.
seed_,3*
sizeof(
unsigned short));
1061 inline void setSeed(
const unsigned short seed[3])
1062 { memcpy(
seed_,seed,3*
sizeof(
unsigned short));}
1066 union {
int i[2];
unsigned short int s[4];} put;
1069 memcpy(
seed_,put.s,3*
sizeof(
unsigned short));
1075 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN32__)
1077 retVal=retVal/(double) RAND_MAX;
1079 retVal = erand48(
seed_);
1090 for (
int i=0;i<n;i++)
1100 mutable unsigned short seed_[3];
1106 #define COIN_DETAIL_PRINT(s) {}
1108 #define COIN_DETAIL_PRINT(s) s
bool CoinIsSorted(register const T *first, const int size)
This helper function tests whether the entries of an array are sorted according to operator<...
Error Class thrown by an exception.
CoinThreadRandom(const CoinThreadRandom &rhs)
void CoinZero(register T *first, register T *last)
This helper function fills an array with a given value.
void CoinDisjointCopyN(register const T *from, const int size, register T *to)
This helper function copies an array to another location.
unsigned int getSeed() const
Get seed.
void CoinSeedRandom(int iseed)
Set the seed for the random number generator.
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 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.
T CoinMin(register const T x1, register const T x2)
Return the smaller (according to operator<() of the arguments.
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.
double CoinDrand48(bool isSeed=false, unsigned int seed=1)
Return a random number between 0 and 1.
unsigned int seed_
Current seed.
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 * CoinStrdup(const char *name)
Returns strdup or NULL if original NULL.
void CoinCheckDoubleZero(double *to, const int size)
This Debug helper function checks an array is all zero.
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 CoinCheckIntZero(int *to, const int size)
This Debug helper function checks an array is all zero.
void CoinSwap(T &x, T &y)
Swap the arguments.
void randomize(int n=0)
make more random (i.e. for startup)
void CoinMemcpy(register const T *first, register const T *last, register T *to)
This helper function copies an array to another location.
void CoinDisjointCopy(register const T *first, register const T *last, register T *to)
This helper function copies an array to another location.
CoinThreadRandom()
Default constructor.
int CoinStrNCaseCmp(const char *s0, const char *s1, const size_t len)
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 CoinAbs(const T value)
Return the absolute value of the argument.
void CoinFillN(register T *to, const int size, register const T value)
This helper function fills an array with a given value.
char CoinFindDirSeparator()
This function figures out whether file names should contain slashes or backslashes as directory separ...
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...
T * CoinDeleteEntriesFromArray(register T *arrayFirst, register T *arrayLast, const int *firstDelPos, const int *lastDelPos)
This helper function deletes certain entries from an array.
void CoinZeroN(register T *to, const int size)
This helper function fills an array with zero.
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...
void setSeed(int seed)
Set seed.
int CoinStrlenAsInt(const char *string)
This helper returns "strlen" as an int.
CoinThreadRandom & operator=(const CoinThreadRandom &rhs)
~CoinThreadRandom()
Destructor.
int CoinFromFile(T *&array, CoinBigIndex size, FILE *fp, CoinBigIndex &newSize)
This helper function copies an array from file and creates with new.
T CoinMax(register const T x1, register const T x2)
Return the larger (according to operator<() of the arguments.
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)...
CoinThreadRandom(int seed)
Constructor wih seed.
Class for thread specific random numbers.