summaryrefslogtreecommitdiff
path: root/newstructure/thirdparty/linux/include/coin/CoinHelperFunctions.hpp
blob: 3409bbcb51ef50ab5c1279aa3f0f1b1a4a4bf132 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
/* $Id: CoinHelperFunctions.hpp 1679 2013-12-05 11:27:45Z forrest $ */
// Copyright (C) 2000, International Business Machines
// Corporation and others.  All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).

#ifndef CoinHelperFunctions_H
#define CoinHelperFunctions_H

#include "CoinUtilsConfig.h"

#if defined(_MSC_VER)
#  include <direct.h>
#  include <cctype>
#  define getcwd _getcwd
#  include <cctype>
#else
#  include <unistd.h>
#endif
//#define USE_MEMCPY

#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include "CoinTypes.hpp"
#include "CoinError.hpp"

// Compilers can produce better code if they know about __restrict
#ifndef COIN_RESTRICT
#ifdef COIN_USE_RESTRICT
#define COIN_RESTRICT __restrict
#else
#define COIN_RESTRICT
#endif
#endif

//#############################################################################

/** This helper function copies an array to another location using Duff's
    device (for a speedup of ~2). The arrays are given by pointers to their
    first entries and by the size of the source array. Overlapping arrays are
    handled correctly. */

template <class T> inline void
CoinCopyN(register const T* from, const int size, register T* to)
{
    if (size == 0 || from == to)
	return;

#ifndef NDEBUG
    if (size < 0)
	throw CoinError("trying to copy negative number of entries",
			"CoinCopyN", "");
#endif

    register int n = (size + 7) / 8;
    if (to > from) {
	register const T* downfrom = from + size;
	register T* downto = to + size;
	// Use Duff's device to copy
	switch (size % 8) {
	case 0: do{     *--downto = *--downfrom;
	case 7:         *--downto = *--downfrom;
	case 6:         *--downto = *--downfrom;
	case 5:         *--downto = *--downfrom;
	case 4:         *--downto = *--downfrom;
	case 3:         *--downto = *--downfrom;
	case 2:         *--downto = *--downfrom;
	case 1:         *--downto = *--downfrom;
	}while(--n>0);
	}
    } else {
	// Use Duff's device to copy
	--from;
	--to;
	switch (size % 8) {
	case 0: do{     *++to = *++from;
	case 7:         *++to = *++from;
	case 6:         *++to = *++from;
	case 5:         *++to = *++from;
	case 4:         *++to = *++from;
	case 3:         *++to = *++from;
	case 2:         *++to = *++from;
	case 1:         *++to = *++from;
	}while(--n>0);
	}
    }
}

//-----------------------------------------------------------------------------

/** This helper function copies an array to another location using Duff's
    device (for a speedup of ~2). The source array is given by its first and
    "after last" entry; the target array is given by its first entry.
    Overlapping arrays are handled correctly.

    All of the various CoinCopyN variants use an int for size. On 64-bit
    architectures, the address diff last-first will be a 64-bit quantity.
    Given that everything else uses an int, I'm going to choose to kick
    the difference down to int.  -- lh, 100823 --
*/
template <class T> inline void
CoinCopy(register const T* first, register const T* last, register T* to)
{
    CoinCopyN(first, static_cast<int>(last-first), to);
}

//-----------------------------------------------------------------------------

/** This helper function copies an array to another location. The two arrays
    must not overlap (otherwise an exception is thrown). For speed 8 entries
    are copied at a time. The arrays are given by pointers to their first
    entries and by the size of the source array. 

    Note JJF - the speed claim seems to be false on IA32 so I have added 
    CoinMemcpyN which can be used for atomic data */
template <class T> inline void
CoinDisjointCopyN(register const T* from, const int size, register T* to)
{
#ifndef _MSC_VER
    if (size == 0 || from == to)
	return;

#ifndef NDEBUG
    if (size < 0)
	throw CoinError("trying to copy negative number of entries",
			"CoinDisjointCopyN", "");
#endif

#if 0
    /* There is no point to do this test. If to and from are from different
       blocks then dist is undefined, so this can crash correct code. It's
       better to trust the user that the arrays are really disjoint. */
    const long dist = to - from;
    if (-size < dist && dist < size)
	throw CoinError("overlapping arrays", "CoinDisjointCopyN", "");
#endif

    for (register int n = size / 8; n > 0; --n, from += 8, to += 8) {
	to[0] = from[0];
	to[1] = from[1];
	to[2] = from[2];
	to[3] = from[3];
	to[4] = from[4];
	to[5] = from[5];
	to[6] = from[6];
	to[7] = from[7];
    }
    switch (size % 8) {
    case 7: to[6] = from[6];
    case 6: to[5] = from[5];
    case 5: to[4] = from[4];
    case 4: to[3] = from[3];
    case 3: to[2] = from[2];
    case 2: to[1] = from[1];
    case 1: to[0] = from[0];
    case 0: break;
    }
#else
    CoinCopyN(from, size, to);
#endif
}

//-----------------------------------------------------------------------------

/** This helper function copies an array to another location. The two arrays
    must not overlap (otherwise an exception is thrown). For speed 8 entries
    are copied at a time. The source array is given by its first and "after
    last" entry; the target array is given by its first entry. */
template <class T> inline void
CoinDisjointCopy(register const T* first, register const T* last,
		 register T* to)
{
    CoinDisjointCopyN(first, static_cast<int>(last - first), to);
}

//-----------------------------------------------------------------------------

/*! \brief Return an array of length \p size filled with input from \p array,
  or null if \p array is null.
*/

template <class T> inline T*
CoinCopyOfArray( const T * array, const int size)
{
    if (array) {
	T * arrayNew = new T[size];
	std::memcpy(arrayNew,array,size*sizeof(T));
	return arrayNew;
    } else {
	return NULL;
    }
}


/*! \brief Return an array of length \p size filled with first copySize from \p array,
  or null if \p array is null.
*/

template <class T> inline T*
CoinCopyOfArrayPartial( const T * array, const int size,const int copySize)
{
    if (array||size) {
	T * arrayNew = new T[size];
	assert (copySize<=size);
	std::memcpy(arrayNew,array,copySize*sizeof(T));
	return arrayNew;
    } else {
	return NULL;
    }
}

/*! \brief Return an array of length \p size filled with input from \p array,
  or filled with (scalar) \p value if \p array is null
*/

template <class T> inline T*
CoinCopyOfArray( const T * array, const int size, T value)
{
    T * arrayNew = new T[size];
    if (array) {
        std::memcpy(arrayNew,array,size*sizeof(T));
    } else {
	int i;
	for (i=0;i<size;i++) 
	    arrayNew[i] = value;
    }
    return arrayNew;
}


/*! \brief Return an array of length \p size filled with input from \p array,
  or filled with zero if \p array is null
*/

template <class T> inline T*
CoinCopyOfArrayOrZero( const T * array , const int size)
{
    T * arrayNew = new T[size];
    if (array) {
      std::memcpy(arrayNew,array,size*sizeof(T));
    } else {
      std::memset(arrayNew,0,size*sizeof(T));
    }
    return arrayNew;
}


//-----------------------------------------------------------------------------

/** This helper function copies an array to another location. The two arrays
    must not overlap (otherwise an exception is thrown). For speed 8 entries
    are copied at a time. The arrays are given by pointers to their first
    entries and by the size of the source array. 

    Note JJF - the speed claim seems to be false on IA32 so I have added 
    alternative coding if USE_MEMCPY defined*/
#ifndef COIN_USE_RESTRICT
template <class T> inline void
CoinMemcpyN(register const T* from, const int size, register T* to)
{
#ifndef _MSC_VER
#ifdef USE_MEMCPY
    // Use memcpy - seems a lot faster on Intel with gcc
#ifndef NDEBUG
    // Some debug so check
    if (size < 0)
	throw CoinError("trying to copy negative number of entries",
			"CoinMemcpyN", "");
  
#if 0
    /* There is no point to do this test. If to and from are from different
       blocks then dist is undefined, so this can crash correct code. It's
       better to trust the user that the arrays are really disjoint. */
    const long dist = to - from;
    if (-size < dist && dist < size)
	throw CoinError("overlapping arrays", "CoinMemcpyN", "");
#endif
#endif
    std::memcpy(to,from,size*sizeof(T));
#else
    if (size == 0 || from == to)
	return;

#ifndef NDEBUG
    if (size < 0)
	throw CoinError("trying to copy negative number of entries",
			"CoinMemcpyN", "");
#endif

#if 0
    /* There is no point to do this test. If to and from are from different
       blocks then dist is undefined, so this can crash correct code. It's
       better to trust the user that the arrays are really disjoint. */
    const long dist = to - from;
    if (-size < dist && dist < size)
	throw CoinError("overlapping arrays", "CoinMemcpyN", "");
#endif

    for (register int n = size / 8; n > 0; --n, from += 8, to += 8) {
	to[0] = from[0];
	to[1] = from[1];
	to[2] = from[2];
	to[3] = from[3];
	to[4] = from[4];
	to[5] = from[5];
	to[6] = from[6];
	to[7] = from[7];
    }
    switch (size % 8) {
    case 7: to[6] = from[6];
    case 6: to[5] = from[5];
    case 5: to[4] = from[4];
    case 4: to[3] = from[3];
    case 3: to[2] = from[2];
    case 2: to[1] = from[1];
    case 1: to[0] = from[0];
    case 0: break;
    }
#endif
#else
    CoinCopyN(from, size, to);
#endif
}
#else
template <class T> inline void
CoinMemcpyN(const T * COIN_RESTRICT from, int size, T* COIN_RESTRICT to)
{
#ifdef USE_MEMCPY
  std::memcpy(to,from,size*sizeof(T));
#else
  T * COIN_RESTRICT put =  to;
  const T * COIN_RESTRICT get = from;
  for ( ; 0<size ; --size)
    *put++ = *get++;
#endif
}
#endif

//-----------------------------------------------------------------------------

/** This helper function copies an array to another location. The two arrays
    must not overlap (otherwise an exception is thrown). For speed 8 entries
    are copied at a time. The source array is given by its first and "after
    last" entry; the target array is given by its first entry. */
template <class T> inline void
CoinMemcpy(register const T* first, register const T* last,
	   register T* to)
{
    CoinMemcpyN(first, static_cast<int>(last - first), to);
}

//#############################################################################

/** This helper function fills an array with a given value. For speed 8 entries
    are filled at a time. The array is given by a pointer to its first entry
    and its size. 

    Note JJF - the speed claim seems to be false on IA32 so I have added 
    CoinZero to allow for memset. */
template <class T> inline void
CoinFillN(register T* to, const int size, register const T value)
{
    if (size == 0)
	return;

#ifndef NDEBUG
    if (size < 0)
	throw CoinError("trying to fill negative number of entries",
			"CoinFillN", "");
#endif
#if 1
    for (register int n = size / 8; n > 0; --n, to += 8) {
	to[0] = value;
	to[1] = value;
	to[2] = value;
	to[3] = value;
	to[4] = value;
	to[5] = value;
	to[6] = value;
	to[7] = value;
    }
    switch (size % 8) {
    case 7: to[6] = value;
    case 6: to[5] = value;
    case 5: to[4] = value;
    case 4: to[3] = value;
    case 3: to[2] = value;
    case 2: to[1] = value;
    case 1: to[0] = value;
    case 0: break;
    }
#else
    // Use Duff's device to fill
    register int n = (size + 7) / 8;
    --to;
    switch (size % 8) {
    case 0: do{     *++to = value;
    case 7:         *++to = value;
    case 6:         *++to = value;
    case 5:         *++to = value;
    case 4:         *++to = value;
    case 3:         *++to = value;
    case 2:         *++to = value;
    case 1:         *++to = value;
    }while(--n>0);
    }
#endif
}

//-----------------------------------------------------------------------------

/** This helper function fills an array with a given value. For speed 8
    entries are filled at a time. The array is given by its first and "after
    last" entry. */
template <class T> inline void
CoinFill(register T* first, register T* last, const T value)
{
    CoinFillN(first, last - first, value);
}

//#############################################################################

/** This helper function fills an array with zero. For speed 8 entries
    are filled at a time. The array is given by a pointer to its first entry
    and its size.

    Note JJF - the speed claim seems to be false on IA32 so I have allowed 
    for memset as an alternative */
template <class T> inline void
CoinZeroN(register T* to, const int size)
{
#ifdef USE_MEMCPY
    // Use memset - seems faster on Intel with gcc
#ifndef NDEBUG
    // Some debug so check
    if (size < 0)
	throw CoinError("trying to fill negative number of entries",
			"CoinZeroN", "");
#endif
    memset(to,0,size*sizeof(T));
#else
    if (size == 0)
	return;

#ifndef NDEBUG
    if (size < 0)
	throw CoinError("trying to fill negative number of entries",
			"CoinZeroN", "");
#endif
#if 1
    for (register int n = size / 8; n > 0; --n, to += 8) {
	to[0] = 0;
	to[1] = 0;
	to[2] = 0;
	to[3] = 0;
	to[4] = 0;
	to[5] = 0;
	to[6] = 0;
	to[7] = 0;
    }
    switch (size % 8) {
    case 7: to[6] = 0;
    case 6: to[5] = 0;
    case 5: to[4] = 0;
    case 4: to[3] = 0;
    case 3: to[2] = 0;
    case 2: to[1] = 0;
    case 1: to[0] = 0;
    case 0: break;
    }
#else
    // Use Duff's device to fill
    register int n = (size + 7) / 8;
    --to;
    switch (size % 8) {
    case 0: do{     *++to = 0;
    case 7:         *++to = 0;
    case 6:         *++to = 0;
    case 5:         *++to = 0;
    case 4:         *++to = 0;
    case 3:         *++to = 0;
    case 2:         *++to = 0;
    case 1:         *++to = 0;
    }while(--n>0);
    }
#endif
#endif
}
/// This Debug helper function checks an array is all zero
inline void
CoinCheckDoubleZero(double * to, const int size)
{
    int n=0;
    for (int j=0;j<size;j++) {
	if (to[j]) 
	    n++;
    }
    if (n) {
	printf("array of length %d should be zero has %d nonzero\n",size,n);
    }
}
/// This Debug helper function checks an array is all zero
inline void
CoinCheckIntZero(int * to, const int size)
{
    int n=0;
    for (int j=0;j<size;j++) {
	if (to[j]) 
	    n++;
    }
    if (n) {
	printf("array of length %d should be zero has %d nonzero\n",size,n);
    }
}

//-----------------------------------------------------------------------------

/** This helper function fills an array with a given value. For speed 8
    entries are filled at a time. The array is given by its first and "after
    last" entry. */
template <class T> inline void
CoinZero(register T* first, register T* last)
{
    CoinZeroN(first, last - first);
}

//#############################################################################

/** Returns strdup or NULL if original NULL */
inline char * CoinStrdup(const char * name)
{
  char* dup = NULL;
  if (name) {
    const int len = static_cast<int>(strlen(name));
    dup = static_cast<char*>(malloc(len+1));
    CoinMemcpyN(name, len, dup);
    dup[len] = 0;
  }
  return dup;
}

//#############################################################################

/** Return the larger (according to <code>operator<()</code> of the arguments.
    This function was introduced because for some reason compiler tend to
    handle the <code>max()</code> function differently. */
template <class T> inline T
CoinMax(register const T x1, register const T x2)
{
    return (x1 > x2) ? x1 : x2;
}

//-----------------------------------------------------------------------------

/** Return the smaller (according to <code>operator<()</code> of the arguments.
    This function was introduced because for some reason compiler tend to
    handle the min() function differently. */
template <class T> inline T
CoinMin(register const T x1, register const T x2)
{
    return (x1 < x2) ? x1 : x2;
}

//-----------------------------------------------------------------------------

/** Return the absolute value of the argument. This function was introduced
    because for some reason compiler tend to handle the abs() function
    differently. */
template <class T> inline T
CoinAbs(const T value)
{
    return value<0 ? -value : value;
}

//#############################################################################

/** This helper function tests whether the entries of an array are sorted
    according to operator<. The array is given by a pointer to its first entry
    and by its size. */
template <class T> inline bool
CoinIsSorted(register const T* first, const int size)
{
    if (size == 0)
	return true;

#ifndef NDEBUG
    if (size < 0)
	throw CoinError("negative number of entries", "CoinIsSorted", "");
#endif
#if 1
    // size1 is the number of comparisons to be made
    const int size1 = size  - 1;
    for (register int n = size1 / 8; n > 0; --n, first += 8) {
	if (first[8] < first[7]) return false;
	if (first[7] < first[6]) return false;
	if (first[6] < first[5]) return false;
	if (first[5] < first[4]) return false;
	if (first[4] < first[3]) return false;
	if (first[3] < first[2]) return false;
	if (first[2] < first[1]) return false;
	if (first[1] < first[0]) return false;
    }

    switch (size1 % 8) {
    case 7: if (first[7] < first[6]) return false;
    case 6: if (first[6] < first[5]) return false;
    case 5: if (first[5] < first[4]) return false;
    case 4: if (first[4] < first[3]) return false;
    case 3: if (first[3] < first[2]) return false;
    case 2: if (first[2] < first[1]) return false;
    case 1: if (first[1] < first[0]) return false;
    case 0: break;
    }
#else
    register const T* next = first;
    register const T* last = first + size;
    for (++next; next != last; first = next, ++next)
	if (*next < *first)
	    return false;
#endif   
    return true;
}

//-----------------------------------------------------------------------------

/** This helper function tests whether the entries of an array are sorted
    according to operator<. The array is given by its first and "after
    last" entry. */
template <class T> inline bool
CoinIsSorted(register const T* first, register const T* last)
{
    return CoinIsSorted(first, static_cast<int>(last - first));
}

//#############################################################################

/** This helper function fills an array with the values init, init+1, init+2,
    etc. For speed 8 entries are filled at a time. The array is given by a
    pointer to its first entry and its size. */
template <class T> inline void
CoinIotaN(register T* first, const int size, register T init)
{
    if (size == 0)
	return;

#ifndef NDEBUG
    if (size < 0)
	throw CoinError("negative number of entries", "CoinIotaN", "");
#endif
#if 1
    for (register int n = size / 8; n > 0; --n, first += 8, init += 8) {
	first[0] = init;
	first[1] = init + 1;
	first[2] = init + 2;
	first[3] = init + 3;
	first[4] = init + 4;
	first[5] = init + 5;
	first[6] = init + 6;
	first[7] = init + 7;
    }
    switch (size % 8) {
    case 7: first[6] = init + 6;
    case 6: first[5] = init + 5;
    case 5: first[4] = init + 4;
    case 4: first[3] = init + 3;
    case 3: first[2] = init + 2;
    case 2: first[1] = init + 1;
    case 1: first[0] = init;
    case 0: break;
    }
#else
    // Use Duff's device to fill
    register int n = (size + 7) / 8;
    --first;
    --init;
    switch (size % 8) {
    case 0: do{     *++first = ++init;
    case 7:         *++first = ++init;
    case 6:         *++first = ++init;
    case 5:         *++first = ++init;
    case 4:         *++first = ++init;
    case 3:         *++first = ++init;
    case 2:         *++first = ++init;
    case 1:         *++first = ++init;
    }while(--n>0);
    }
#endif
}

//-----------------------------------------------------------------------------

/** This helper function fills an array with the values init, init+1, init+2,
    etc. For speed 8 entries are filled at a time. The array is given by its
    first and "after last" entry. */
template <class T> inline void
CoinIota(T* first, const T* last, T init)
{
    CoinIotaN(first, last-first, init);
}

//#############################################################################

/** This helper function deletes certain entries from an array. The array is
    given by pointers to its first and "after last" entry (first two
    arguments). The positions of the entries to be deleted are given in the
    integer array specified by the last two arguments (again, first and "after
    last" entry). */
template <class T> inline T *
CoinDeleteEntriesFromArray(register T * arrayFirst, register T * arrayLast,
			   const int * firstDelPos, const int * lastDelPos)
{
    int delNum = static_cast<int>(lastDelPos - firstDelPos);
    if (delNum == 0)
	return arrayLast;

    if (delNum < 0)
	throw CoinError("trying to delete negative number of entries",
			"CoinDeleteEntriesFromArray", "");

    int * delSortedPos = NULL;
    if (! (CoinIsSorted(firstDelPos, lastDelPos) &&
	   std::adjacent_find(firstDelPos, lastDelPos) == lastDelPos)) {
	// the positions of the to be deleted is either not sorted or not unique
	delSortedPos = new int[delNum];
	CoinDisjointCopy(firstDelPos, lastDelPos, delSortedPos);
	std::sort(delSortedPos, delSortedPos + delNum);
	delNum = static_cast<int>(std::unique(delSortedPos,
				  delSortedPos+delNum) - delSortedPos);
    }
    const int * delSorted = delSortedPos ? delSortedPos : firstDelPos;

    const int last = delNum - 1;
    int size = delSorted[0];
    for (int i = 0; i < last; ++i) {
	const int copyFirst = delSorted[i] + 1;
	const int copyLast = delSorted[i+1];
	CoinCopy(arrayFirst + copyFirst, arrayFirst + copyLast,
		 arrayFirst + size);
	size += copyLast - copyFirst;
    }
    const int copyFirst = delSorted[last] + 1;
    const int copyLast = static_cast<int>(arrayLast - arrayFirst);
    CoinCopy(arrayFirst + copyFirst, arrayFirst + copyLast,
	     arrayFirst + size);
    size += copyLast - copyFirst;

    if (delSortedPos)
	delete[] delSortedPos;

    return arrayFirst + size;
}

//#############################################################################

#define COIN_OWN_RANDOM_32

#if defined COIN_OWN_RANDOM_32
/* Thanks to Stefano Gliozzi for providing an operating system
   independent random number generator.  */

/*! \brief Return a random number between 0 and 1

  A platform-independent linear congruential generator. For a given seed, the
  generated sequence is always the same regardless of the (32-bit)
  architecture. This allows to build & test in different environments, getting
  in most cases the same optimization path.

  Set \p isSeed to true and supply an integer seed to set the seed
  (vid. #CoinSeedRandom)

  \todo Anyone want to volunteer an upgrade for 64-bit architectures?
*/
inline double CoinDrand48 (bool isSeed = false, unsigned int seed = 1)
{
  static unsigned int last = 123456;
  if (isSeed) { 
    last = seed;
  } else {
    last = 1664525*last+1013904223;
    return ((static_cast<double> (last))/4294967296.0);
  }
  return (0.0);
}

/// Set the seed for the random number generator
inline void CoinSeedRandom(int iseed)
{
  CoinDrand48(true, iseed);
}

#else // COIN_OWN_RANDOM_32

#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN32__)

/// Return a random number between 0 and 1
inline double CoinDrand48() { return rand() / (double) RAND_MAX; }
/// Set the seed for the random number generator
inline void CoinSeedRandom(int iseed) { srand(iseed + 69822); }

#else

/// Return a random number between 0 and 1
inline double CoinDrand48() { return drand48(); }
/// Set the seed for the random number generator
inline void CoinSeedRandom(int iseed) { srand48(iseed + 69822); }

#endif

#endif // COIN_OWN_RANDOM_32

//#############################################################################

/** This function figures out whether file names should contain slashes or 
    backslashes as directory separator */
inline char CoinFindDirSeparator()
{
    int size = 1000;
    char* buf = 0;
    while (true) {
	buf = new char[size];
	if (getcwd(buf, size))
	    break;
	delete[] buf;
	buf = 0;
	size = 2*size;
    }
    // if first char is '/' then it's unix and the dirsep is '/'. otherwise we 
    // assume it's dos and the dirsep is '\'
    char dirsep = buf[0] == '/' ? '/' : '\\';
    delete[] buf;
    return dirsep;
}
//#############################################################################

inline int CoinStrNCaseCmp(const char* s0, const char* s1,
			   const size_t len)
{
    for (size_t i = 0; i < len; ++i) {
	if (s0[i] == 0) {
	    return s1[i] == 0 ? 0 : -1;
	}
	if (s1[i] == 0) {
	    return 1;
	}
	const int c0 = std::tolower(s0[i]);
	const int c1 = std::tolower(s1[i]);
	if (c0 < c1)
	    return -1;
	if (c0 > c1)
	    return 1;
    }
    return 0;
}

//#############################################################################

/// Swap the arguments.
template <class T> inline void CoinSwap (T &x, T &y)
{
    T t = x;
    x = y;
    y = t;
}

//#############################################################################

/** This helper function copies an array to file 
    Returns 0 if OK, 1 if bad write.
*/

template <class T> inline int
CoinToFile( const T* array, CoinBigIndex size, FILE * fp)
{
    CoinBigIndex numberWritten;
    if (array&&size) {
	numberWritten =
	    static_cast<CoinBigIndex>(fwrite(&size,sizeof(int),1,fp));
	if (numberWritten!=1)
	    return 1;
	numberWritten =
	    static_cast<CoinBigIndex>(fwrite(array,sizeof(T),size_t(size),fp));
	if (numberWritten!=size)
	    return 1;
    } else {
	size = 0;
	numberWritten = 
	    static_cast<CoinBigIndex>(fwrite(&size,sizeof(int),1,fp));
	if (numberWritten!=1)
	    return 1;
    }
    return 0;
}

//#############################################################################

/** This helper function copies an array from file and creates with new.
    Passed in array is ignored i.e. not deleted.
    But if NULL and size does not match and newSize 0 then leaves as NULL and 0
    Returns 0 if OK, 1 if bad read, 2 if size did not match.
*/

template <class T> inline int
CoinFromFile( T* &array, CoinBigIndex size, FILE * fp, CoinBigIndex & newSize)
{
    CoinBigIndex numberRead;
    numberRead =
        static_cast<CoinBigIndex>(fread(&newSize,sizeof(int),1,fp));
    if (numberRead!=1)
	return 1;
    int returnCode=0;
    if (size!=newSize&&(newSize||array))
	returnCode=2;
    if (newSize) {
	array = new T [newSize];
	numberRead =
	    static_cast<CoinBigIndex>(fread(array,sizeof(T),newSize,fp));
	if (numberRead!=newSize)
	    returnCode=1;
    } else {
	array = NULL;
    }
    return returnCode;
}

//#############################################################################

/// Cube Root
#if 0
inline double CoinCbrt(double x)
{
#if defined(_MSC_VER) 
    return pow(x,(1./3.));
#else
    return cbrt(x);
#endif
}
#endif

//-----------------------------------------------------------------------------

/// This helper returns "sizeof" as an int 
#define CoinSizeofAsInt(type) (static_cast<int>(sizeof(type)))
/// This helper returns "strlen" as an int 
inline int
CoinStrlenAsInt(const char * string)
{
    return static_cast<int>(strlen(string));
}

/** Class for thread specific random numbers
*/
#if defined COIN_OWN_RANDOM_32
class CoinThreadRandom  {
public:
  /**@name Constructors, destructor */

  //@{
  /** Default constructor. */
  CoinThreadRandom()
  { seed_=12345678;}
  /** Constructor wih seed. */
  CoinThreadRandom(int seed)
  { 
    seed_ = seed;
  }
  /** Destructor */
  ~CoinThreadRandom() {}
  // Copy
  CoinThreadRandom(const CoinThreadRandom & rhs)
  { seed_ = rhs.seed_;}
  // Assignment
  CoinThreadRandom& operator=(const CoinThreadRandom & rhs)
  {
    if (this != &rhs) {
      seed_ = rhs.seed_;
    }
    return *this;
  }

  //@}
  
  /**@name Sets/gets */

  //@{
  /** Set seed. */
  inline void setSeed(int seed)
  { 
    seed_ = seed;
  }
  /** Get seed. */
  inline unsigned int getSeed() const
  { 
    return seed_;
  }
  /// return a random number
  inline double randomDouble() const
  {
    double retVal;
    seed_ = 1664525*(seed_)+1013904223;
    retVal = ((static_cast<double> (seed_))/4294967296.0);
    return retVal;
  }
  /// make more random (i.e. for startup)
  inline void randomize(int n=0)
  {
    if (!n) 
      n=seed_ & 255;
    for (int i=0;i<n;i++)
      randomDouble();
  }
  //@}
  
  
protected:
  /**@name Data members
     The data members are protected to allow access for derived classes. */
  //@{
  /// Current seed
  mutable unsigned int seed_;
  //@}
};
#else
class CoinThreadRandom  {
public:
  /**@name Constructors, destructor */

  //@{
  /** Default constructor. */
  CoinThreadRandom()
  { seed_[0]=50000;seed_[1]=40000;seed_[2]=30000;}
  /** Constructor wih seed. */
  CoinThreadRandom(const unsigned short seed[3])
  { memcpy(seed_,seed,3*sizeof(unsigned short));}
  /** Constructor wih seed. */
  CoinThreadRandom(int seed)
  { 
    union { int i[2]; unsigned short int s[4];} put;
    put.i[0]=seed;
    put.i[1]=seed;
    memcpy(seed_,put.s,3*sizeof(unsigned short));
  }
  /** Destructor */
  ~CoinThreadRandom() {}
  // Copy
  CoinThreadRandom(const CoinThreadRandom & rhs)
  { memcpy(seed_,rhs.seed_,3*sizeof(unsigned short));}
  // Assignment
  CoinThreadRandom& operator=(const CoinThreadRandom & rhs)
  {
    if (this != &rhs) {
      memcpy(seed_,rhs.seed_,3*sizeof(unsigned short));
    }
    return *this;
  }

  //@}
  
  /**@name Sets/gets */

  //@{
  /** Set seed. */
  inline void setSeed(const unsigned short seed[3])
  { memcpy(seed_,seed,3*sizeof(unsigned short));}
  /** Set seed. */
  inline void setSeed(int seed)
  { 
    union { int i[2]; unsigned short int s[4];} put;
    put.i[0]=seed;
    put.i[1]=seed;
    memcpy(seed_,put.s,3*sizeof(unsigned short));
  }
  /// return a random number
  inline double randomDouble() const
  {
    double retVal;
#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN32__)
    retVal=rand();
    retVal=retVal/(double) RAND_MAX;
#else
    retVal = erand48(seed_);
#endif
    return retVal;
  }
  /// make more random (i.e. for startup)
  inline void randomize(int n=0)
  {
    if (!n) {
      n=seed_[0]+seed_[1]+seed_[2];
      n &= 255;
    }
    for (int i=0;i<n;i++)
      randomDouble();
  }
  //@}
  
  
protected:
  /**@name Data members
     The data members are protected to allow access for derived classes. */
  //@{
  /// Current seed
  mutable unsigned short seed_[3];
  //@}
};
#endif
#ifndef COIN_DETAIL
#define COIN_DETAIL_PRINT(s) {}
#else
#define COIN_DETAIL_PRINT(s) s
#endif
#endif