6 #ifndef CoinHelperFunctions_H
7 #define CoinHelperFunctions_H
14 #define getcwd _getcwd
29 #ifdef COIN_USE_RESTRICT
30 #define COIN_RESTRICT __restrict
47 if (size == 0 || from == to)
52 throw CoinError(
"trying to copy negative number of entries",
58 const T *downfrom = from + size;
59 T *downto = to + size;
64 *--downto = *--downfrom;
66 *--downto = *--downfrom;
68 *--downto = *--downfrom;
70 *--downto = *--downfrom;
72 *--downto = *--downfrom;
74 *--downto = *--downfrom;
76 *--downto = *--downfrom;
78 *--downto = *--downfrom;
124 CoinCopyN(first, static_cast< CoinBigIndex >(last - first), to);
141 if (size == 0 || from == to)
146 throw CoinError(
"trying to copy negative number of entries",
147 "CoinDisjointCopyN",
"");
154 const long dist = to - from;
155 if (-size < dist && dist < size)
156 throw CoinError(
"overlapping arrays",
"CoinDisjointCopyN",
"");
159 for (
CoinBigIndex n = size / 8; n > 0; --n, from += 8, to += 8) {
217 T *arrayNew =
new T[size];
218 std::memcpy(arrayNew, array, size *
sizeof(T));
234 T *arrayNew =
new T[size];
235 assert(copySize <= size);
236 std::memcpy(arrayNew, array, copySize *
sizeof(T));
251 T *arrayNew =
new T[size];
253 std::memcpy(arrayNew, array, size *
sizeof(T));
256 for (i = 0; i < size; i++)
270 T *arrayNew =
new T[size];
272 std::memcpy(arrayNew, array, size *
sizeof(T));
274 std::memset(arrayNew, 0, size *
sizeof(T));
288 #ifndef COIN_USE_RESTRICT
299 throw CoinError(
"trying to copy negative number of entries",
306 const long dist = to - from;
307 if (-size < dist && dist < size)
308 throw CoinError(
"overlapping arrays",
"CoinMemcpyN",
"");
311 std::memcpy(to, from, size *
sizeof(T));
313 if (size == 0 || from == to)
318 throw CoinError(
"trying to copy negative number of entries",
326 const long dist = to - from;
327 if (-size < dist && dist < size)
328 throw CoinError(
"overlapping arrays",
"CoinMemcpyN",
"");
331 for (
CoinBigIndex n = size / 8; n > 0; --n, from += 8, to += 8) {
370 std::memcpy(to, from, size *
sizeof(T));
374 for (; 0 < size; --size)
391 CoinMemcpyN(first, static_cast< CoinBigIndex >(last - first), to);
411 throw CoinError(
"trying to fill negative number of entries",
499 throw CoinError(
"trying to fill negative number of entries",
502 memset(to, 0, size *
sizeof(T));
509 throw CoinError(
"trying to fill negative number of entries",
578 printf(
"array of length %d should be zero has %d nonzero\n",
579 static_cast< int >(size), static_cast< int >(n));
592 printf(
"array of length %d should be zero has %d nonzero\n",
593 static_cast< int >(size),
594 static_cast< int >(n));
617 const int len =
static_cast< int >(strlen(name));
618 dup =
static_cast< char *
>(malloc(len + 1));
634 return (x1 > x2) ? x1 : x2;
646 return (x1 < x2) ? x1 : x2;
658 return value < 0 ? -value : value;
675 throw CoinError(
"negative number of entries",
"CoinIsSorted",
"");
680 for (
CoinBigIndex n = size1 / 8; n > 0; --n, first += 8) {
681 if (first[8] < first[7])
683 if (first[7] < first[6])
685 if (first[6] < first[5])
687 if (first[5] < first[4])
689 if (first[4] < first[3])
691 if (first[3] < first[2])
693 if (first[2] < first[1])
695 if (first[1] < first[0])
701 if (first[7] < first[6])
704 if (first[6] < first[5])
707 if (first[5] < first[4])
710 if (first[4] < first[3])
713 if (first[3] < first[2])
716 if (first[2] < first[1])
719 if (first[1] < first[0])
725 const T *next = first;
726 const T *last = first + size;
727 for (++next; next != last; first = next, ++next)
743 return CoinIsSorted(first, static_cast< CoinBigIndex >(last - first));
760 throw CoinError(
"negative number of entries",
"CoinIotaN",
"");
763 for (
CoinBigIndex n = size / 8; n > 0; --n, first += 8, init += 8) {
841 const int *firstDelPos,
const int *lastDelPos)
848 throw CoinError(
"trying to delete negative number of entries",
849 "CoinDeleteEntriesFromArray",
"");
851 int *delSortedPos = NULL;
852 if (!(
CoinIsSorted(firstDelPos, lastDelPos) && std::adjacent_find(firstDelPos, lastDelPos) == lastDelPos)) {
854 delSortedPos =
new int[delNum];
856 std::sort(delSortedPos, delSortedPos + delNum);
857 delNum =
static_cast< CoinBigIndex >(std::unique(delSortedPos,
858 delSortedPos + delNum)
861 const int *delSorted = delSortedPos ? delSortedPos : firstDelPos;
864 int size = delSorted[0];
866 const int copyFirst = delSorted[i] + 1;
867 const int copyLast = delSorted[i + 1];
868 CoinCopy(arrayFirst + copyFirst, arrayFirst + copyLast,
870 size += copyLast - copyFirst;
872 const int copyFirst = delSorted[last] + 1;
873 const int copyLast =
static_cast< int >(arrayLast - arrayFirst);
874 CoinCopy(arrayFirst + copyFirst, arrayFirst + copyLast,
876 size += copyLast - copyFirst;
879 delete[] delSortedPos;
881 return arrayFirst + size;
886 #define COIN_OWN_RANDOM_32
888 #if defined COIN_OWN_RANDOM_32
904 inline double CoinDrand48(
bool isSeed =
false,
unsigned int seed = 1)
906 static unsigned int last = 123456;
910 last = 1664525 * last + 1013904223;
911 return ((static_cast< double >(last)) / 4294967296.0);
922 #else // COIN_OWN_RANDOM_32
924 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN32__)
927 inline double CoinDrand48() {
return rand() / (double)RAND_MAX; }
940 #endif // COIN_OWN_RANDOM_32
951 buf =
new char[size];
952 if (getcwd(buf, size))
960 char dirsep = buf[0] ==
'/' ?
'/' :
'\\';
969 for (
size_t i = 0; i < len; ++i) {
971 return s1[i] == 0 ? 0 : -1;
976 const int c0 = std::tolower(s0[i]);
977 const int c1 = std::tolower(s1[i]);
1003 template <
class T >
1008 if (array && size) {
1009 numberWritten =
static_cast< CoinBigIndex >(fwrite(&size,
sizeof(
int), 1, fp));
1010 if (numberWritten != 1)
1012 numberWritten =
static_cast< CoinBigIndex >(fwrite(array,
sizeof(T),
size_t(size), fp));
1013 if (numberWritten != size)
1017 numberWritten =
static_cast< CoinBigIndex >(fwrite(&size,
sizeof(
int), 1, fp));
1018 if (numberWritten != 1)
1032 template <
class T >
1037 numberRead =
static_cast< CoinBigIndex >(fread(&newSize,
sizeof(
int), 1, fp));
1038 if (numberRead != 1)
1041 if (size != newSize && (newSize || array))
1044 array =
new T[newSize];
1045 numberRead =
static_cast< CoinBigIndex >(fread(array,
sizeof(T), newSize, fp));
1046 if (numberRead != newSize)
1058 inline double CoinCbrt(
double x)
1060 #if defined(_MSC_VER)
1061 return pow(x,(1./3.));
1071 #define CoinSizeofAsInt(type) (static_cast< int >(sizeof(type)))
1076 return static_cast< int >(strlen(
string));
1081 #if defined COIN_OWN_RANDOM_32
1133 retVal = ((
static_cast< double >(
seed_)) / 4294967296.0);
1141 for (
int i = 0; i < n; i++)
1150 mutable unsigned int seed_;
1170 memcpy(
seed_, seed, 3 *
sizeof(
unsigned short));
1177 unsigned short int s[4];
1181 memcpy(
seed_, put.s, 3 *
sizeof(
unsigned short));
1188 memcpy(
seed_, rhs.
seed_, 3 *
sizeof(
unsigned short));
1194 memcpy(
seed_, rhs.
seed_, 3 *
sizeof(
unsigned short));
1205 inline void setSeed(
const unsigned short seed[3])
1207 memcpy(
seed_, seed, 3 *
sizeof(
unsigned short));
1214 unsigned short int s[4];
1218 memcpy(
seed_, put.s, 3 *
sizeof(
unsigned short));
1224 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN32__)
1226 retVal = retVal / (double)RAND_MAX;
1228 retVal = erand48(
seed_);
1239 for (
int i = 0; i < n; i++)
1248 mutable unsigned short seed_[3];
1254 #define COIN_DETAIL_PRINT(s) \
1258 #define COIN_DETAIL_PRINT(s) s
Error Class thrown by an exception.
CoinThreadRandom(const CoinThreadRandom &rhs)
void CoinDisjointCopyN(const T *from, const CoinBigIndex size, 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.
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.
T * CoinCopyOfArrayOrZero(const T *array, const CoinBigIndex size)
Return an array of length size filled with input from array, or filled with zero if array is null...
double CoinDrand48(bool isSeed=false, unsigned int seed=1)
Return a random number between 0 and 1.
unsigned int seed_
Current seed.
void CoinIotaN(T *first, const CoinBigIndex size, T init)
This helper function fills an array with the values init, init+1, init+2, etc.
void CoinMemcpyN(const T *from, const CoinBigIndex size, T *to)
This helper function copies an array to another location.
void CoinCheckDoubleZero(double *to, const CoinBigIndex size)
This Debug helper function checks an array is all zero.
void CoinFillN(T *to, const CoinBigIndex size, 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.
bool CoinIsSorted(const T *first, const CoinBigIndex size)
This helper function tests whether the entries of an array are sorted according to operator<...
T * CoinDeleteEntriesFromArray(T *arrayFirst, T *arrayLast, const int *firstDelPos, const int *lastDelPos)
This helper function deletes certain entries from an array.
void CoinSwap(T &x, T &y)
Swap the arguments.
void CoinZeroN(T *to, const CoinBigIndex size)
This helper function fills an array with zero.
T * CoinCopyOfArrayPartial(const T *array, const CoinBigIndex size, const CoinBigIndex copySize)
Return an array of length size filled with first copySize from array, or null if array is null...
void randomize(int n=0)
make more random (i.e. for startup)
void CoinDisjointCopy(const T *first, const T *last, T *to)
This helper function copies an array to another location.
void CoinFill(T *first, T *last, const T value)
This helper function fills an array with a given value.
T CoinMax(const T x1, const T x2)
Return the larger (according to operator<() of the arguments.
void CoinZero(T *first, T *last)
This helper function fills an array with a given value.
CoinThreadRandom()
Default constructor.
int CoinStrNCaseCmp(const char *s0, const char *s1, const size_t len)
T CoinAbs(const T value)
Return the absolute value of the argument.
char CoinFindDirSeparator()
This function figures out whether file names should contain slashes or backslashes as directory separ...
void CoinMemcpy(const T *first, const T *last, T *to)
This helper function copies an array to another location.
void setSeed(int seed)
Set seed.
int CoinStrlenAsInt(const char *string)
This helper returns "strlen" as an int.
T CoinMin(const T x1, const T x2)
Return the smaller (according to operator<() of the arguments.
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.
double randomDouble() const
return a random number
void CoinCopyN(const T *from, const CoinBigIndex size, T *to)
This helper function copies an array to another location using Duff's device (for a speedup of ~2)...
T * CoinCopyOfArray(const T *array, const CoinBigIndex size)
Return an array of length size filled with input from array, or null if array is null.
CoinThreadRandom(int seed)
Constructor wih seed.
void CoinCopy(const T *first, const T *last, T *to)
This helper function copies an array to another location using Duff's device (for a speedup of ~2)...
Class for thread specific random numbers.
void CoinCheckIntZero(int *to, const CoinBigIndex size)
This Debug helper function checks an array is all zero.