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 = static_cast<CoinBigIndex>(size>>3); 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",
506 throw CoinError(
"trying to fill negative number of entries",
509 memset(to, 0, size *
sizeof(T));
516 throw CoinError(
"trying to fill negative number of entries",
585 printf(
"array of length %d should be zero has %d nonzero\n",
586 static_cast< int >(size), static_cast< int >(n));
599 printf(
"array of length %d should be zero has %d nonzero\n",
600 static_cast< int >(size),
601 static_cast< int >(n));
624 const int len = static_cast< int >(strlen(name));
625 dup = static_cast< char * >(malloc(len + 1));
641 return (x1 > x2) ? x1 : x2;
653 return (x1 < x2) ? x1 : x2;
665 return value < 0 ? -value : value;
682 throw CoinError(
"negative number of entries",
"CoinIsSorted",
"");
687 for (
CoinBigIndex n = size1 / 8; n > 0; --n, first += 8) {
688 if (first[8] < first[7])
690 if (first[7] < first[6])
692 if (first[6] < first[5])
694 if (first[5] < first[4])
696 if (first[4] < first[3])
698 if (first[3] < first[2])
700 if (first[2] < first[1])
702 if (first[1] < first[0])
708 if (first[7] < first[6])
711 if (first[6] < first[5])
714 if (first[5] < first[4])
717 if (first[4] < first[3])
720 if (first[3] < first[2])
723 if (first[2] < first[1])
726 if (first[1] < first[0])
732 const T *next = first;
733 const T *last = first + size;
734 for (++next; next != last; first = next, ++next)
750 return CoinIsSorted(first, static_cast< CoinBigIndex >(last - first));
767 throw CoinError(
"negative number of entries",
"CoinIotaN",
"");
770 for (
CoinBigIndex n = size / 8; n > 0; --n, first += 8, init += 8) {
848 const int *firstDelPos,
const int *lastDelPos)
850 CoinBigIndex delNum = static_cast< CoinBigIndex >(lastDelPos - firstDelPos);
855 throw CoinError(
"trying to delete negative number of entries",
856 "CoinDeleteEntriesFromArray",
"");
858 int *delSortedPos = NULL;
859 if (!(
CoinIsSorted(firstDelPos, lastDelPos) && std::adjacent_find(firstDelPos, lastDelPos) == lastDelPos)) {
861 delSortedPos =
new int[delNum];
863 std::sort(delSortedPos, delSortedPos + delNum);
864 delNum = static_cast< CoinBigIndex >(std::unique(delSortedPos,
865 delSortedPos + delNum)
868 const int *delSorted = delSortedPos ? delSortedPos : firstDelPos;
871 int size = delSorted[0];
873 const int copyFirst = delSorted[i] + 1;
874 const int copyLast = delSorted[i + 1];
875 CoinCopy(arrayFirst + copyFirst, arrayFirst + copyLast,
877 size += copyLast - copyFirst;
879 const int copyFirst = delSorted[last] + 1;
880 const int copyLast = static_cast< int >(arrayLast - arrayFirst);
881 CoinCopy(arrayFirst + copyFirst, arrayFirst + copyLast,
883 size += copyLast - copyFirst;
886 delete[] delSortedPos;
888 return arrayFirst + size;
893 #define COIN_OWN_RANDOM_32
895 #if defined COIN_OWN_RANDOM_32
911 inline double CoinDrand48(
bool isSeed =
false,
unsigned int seed = 1)
913 static unsigned int last = 123456;
917 last = 1664525 * last + 1013904223;
918 return ((static_cast< double >(last)) / 4294967296.0);
929 #else // COIN_OWN_RANDOM_32
931 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN32__)
934 inline double CoinDrand48() {
return rand() / (double)RAND_MAX; }
947 #endif // COIN_OWN_RANDOM_32
958 buf =
new char[size];
959 if (getcwd(buf, size))
967 char dirsep = buf[0] ==
'/' ?
'/' :
'\\';
976 for (
size_t i = 0; i < len; ++i) {
978 return s1[i] == 0 ? 0 : -1;
983 const int c0 = std::tolower(s0[i]);
984 const int c1 = std::tolower(s1[i]);
1010 template <
class T >
1015 if (array && size) {
1016 numberWritten = static_cast< CoinBigIndex >(fwrite(&size,
sizeof(
int), 1, fp));
1017 if (numberWritten != 1)
1019 numberWritten = static_cast< CoinBigIndex >(fwrite(array,
sizeof(T),
size_t(size), fp));
1020 if (numberWritten != size)
1024 numberWritten = static_cast< CoinBigIndex >(fwrite(&size,
sizeof(
int), 1, fp));
1025 if (numberWritten != 1)
1039 template <
class T >
1044 numberRead = static_cast< CoinBigIndex >(fread(&newSize,
sizeof(
int), 1, fp));
1045 if (numberRead != 1)
1048 if (size != newSize && (newSize || array))
1051 array =
new T[newSize];
1052 numberRead = static_cast< CoinBigIndex >(fread(array,
sizeof(T), newSize, fp));
1053 if (numberRead != newSize)
1065 inline double CoinCbrt(
double x)
1067 #if defined(_MSC_VER)
1068 return pow(x,(1./3.));
1078 #define CoinSizeofAsInt(type) (static_cast< int >(sizeof(type)))
1083 return static_cast< int >(strlen(
string));
1088 #if defined COIN_OWN_RANDOM_32
1140 retVal = ((static_cast< double >(
seed_)) / 4294967296.0);
1148 for (
int i = 0; i < n; i++)
1157 mutable unsigned int seed_;
1177 memcpy(
seed_, seed, 3 *
sizeof(
unsigned short));
1184 unsigned short int s[4];
1188 memcpy(
seed_, put.s, 3 *
sizeof(
unsigned short));
1195 memcpy(
seed_, rhs.
seed_, 3 *
sizeof(
unsigned short));
1201 memcpy(
seed_, rhs.
seed_, 3 *
sizeof(
unsigned short));
1212 inline void setSeed(
const unsigned short seed[3])
1214 memcpy(
seed_, seed, 3 *
sizeof(
unsigned short));
1221 unsigned short int s[4];
1225 memcpy(
seed_, put.s, 3 *
sizeof(
unsigned short));
1231 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN32__)
1233 retVal = retVal / (double)RAND_MAX;
1235 retVal = erand48(
seed_);
1246 for (
int i = 0; i < n; i++)
1255 mutable unsigned short seed_[3];
1261 #define COIN_DETAIL_PRINT(s) \
1265 #define COIN_DETAIL_PRINT(s) s