nysol-take 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (161) hide show
  1. checksums.yaml +7 -0
  2. data/bin/mbiclique.rb +317 -0
  3. data/bin/mbipolish.rb +362 -0
  4. data/bin/mccomp.rb +235 -0
  5. data/bin/mclique.rb +295 -0
  6. data/bin/mclique2g.rb +105 -0
  7. data/bin/mcliqueInfo.rb +203 -0
  8. data/bin/mfriends.rb +202 -0
  9. data/bin/mgdiff.rb +252 -0
  10. data/bin/mhifriend.rb +456 -0
  11. data/bin/mhipolish.rb +465 -0
  12. data/bin/mitemset.rb +168 -0
  13. data/bin/mpal.rb +410 -0
  14. data/bin/mpolishing.rb +399 -0
  15. data/bin/msequence.rb +165 -0
  16. data/bin/mtra2g.rb +476 -0
  17. data/bin/mtra2gc.rb +360 -0
  18. data/ext/grhfilrun/extconf.rb +12 -0
  19. data/ext/grhfilrun/grhfilrun.c +85 -0
  20. data/ext/grhfilrun/src/_sspc.c +358 -0
  21. data/ext/grhfilrun/src/aheap.c +545 -0
  22. data/ext/grhfilrun/src/aheap.h +251 -0
  23. data/ext/grhfilrun/src/base.c +92 -0
  24. data/ext/grhfilrun/src/base.h +59 -0
  25. data/ext/grhfilrun/src/fstar.c +497 -0
  26. data/ext/grhfilrun/src/fstar.h +80 -0
  27. data/ext/grhfilrun/src/grhfil.c +214 -0
  28. data/ext/grhfilrun/src/itemset.c +713 -0
  29. data/ext/grhfilrun/src/itemset.h +170 -0
  30. data/ext/grhfilrun/src/problem.c +415 -0
  31. data/ext/grhfilrun/src/problem.h +179 -0
  32. data/ext/grhfilrun/src/queue.c +533 -0
  33. data/ext/grhfilrun/src/queue.h +182 -0
  34. data/ext/grhfilrun/src/sample.c +19 -0
  35. data/ext/grhfilrun/src/sspc.c +597 -0
  36. data/ext/grhfilrun/src/sspc2.c +491 -0
  37. data/ext/grhfilrun/src/stdlib2.c +1482 -0
  38. data/ext/grhfilrun/src/stdlib2.h +892 -0
  39. data/ext/grhfilrun/src/trsact.c +817 -0
  40. data/ext/grhfilrun/src/trsact.h +160 -0
  41. data/ext/grhfilrun/src/vec.c +745 -0
  42. data/ext/grhfilrun/src/vec.h +172 -0
  43. data/ext/lcmrun/extconf.rb +20 -0
  44. data/ext/lcmrun/lcmrun.cpp +99 -0
  45. data/ext/lcmrun/src/aheap.c +216 -0
  46. data/ext/lcmrun/src/aheap.h +111 -0
  47. data/ext/lcmrun/src/base.c +92 -0
  48. data/ext/lcmrun/src/base.h +59 -0
  49. data/ext/lcmrun/src/itemset.c +496 -0
  50. data/ext/lcmrun/src/itemset.h +157 -0
  51. data/ext/lcmrun/src/lcm.c +427 -0
  52. data/ext/lcmrun/src/problem.c +349 -0
  53. data/ext/lcmrun/src/problem.h +177 -0
  54. data/ext/lcmrun/src/queue.c +528 -0
  55. data/ext/lcmrun/src/queue.h +176 -0
  56. data/ext/lcmrun/src/sgraph.c +359 -0
  57. data/ext/lcmrun/src/sgraph.h +173 -0
  58. data/ext/lcmrun/src/stdlib2.c +1282 -0
  59. data/ext/lcmrun/src/stdlib2.h +823 -0
  60. data/ext/lcmrun/src/trsact.c +747 -0
  61. data/ext/lcmrun/src/trsact.h +159 -0
  62. data/ext/lcmrun/src/vec.c +731 -0
  63. data/ext/lcmrun/src/vec.h +171 -0
  64. data/ext/lcmseq0run/extconf.rb +20 -0
  65. data/ext/lcmseq0run/lcmseq0run.cpp +59 -0
  66. data/ext/lcmseq0run/src/aheap.c +216 -0
  67. data/ext/lcmseq0run/src/aheap.h +111 -0
  68. data/ext/lcmseq0run/src/base.c +92 -0
  69. data/ext/lcmseq0run/src/base.h +59 -0
  70. data/ext/lcmseq0run/src/itemset.c +518 -0
  71. data/ext/lcmseq0run/src/itemset.h +157 -0
  72. data/ext/lcmseq0run/src/itemset_zero.c +522 -0
  73. data/ext/lcmseq0run/src/lcm_seq.c +446 -0
  74. data/ext/lcmseq0run/src/lcm_seq_zero.c +446 -0
  75. data/ext/lcmseq0run/src/problem.c +439 -0
  76. data/ext/lcmseq0run/src/problem.h +179 -0
  77. data/ext/lcmseq0run/src/problem_zero.c +439 -0
  78. data/ext/lcmseq0run/src/queue.c +533 -0
  79. data/ext/lcmseq0run/src/queue.h +182 -0
  80. data/ext/lcmseq0run/src/stdlib2.c +1350 -0
  81. data/ext/lcmseq0run/src/stdlib2.h +864 -0
  82. data/ext/lcmseq0run/src/trsact.c +747 -0
  83. data/ext/lcmseq0run/src/trsact.h +159 -0
  84. data/ext/lcmseq0run/src/vec.c +779 -0
  85. data/ext/lcmseq0run/src/vec.h +172 -0
  86. data/ext/lcmseqrun/extconf.rb +20 -0
  87. data/ext/lcmseqrun/lcmseqrun.cpp +101 -0
  88. data/ext/lcmseqrun/src/aheap.c +216 -0
  89. data/ext/lcmseqrun/src/aheap.h +111 -0
  90. data/ext/lcmseqrun/src/base.c +92 -0
  91. data/ext/lcmseqrun/src/base.h +59 -0
  92. data/ext/lcmseqrun/src/itemset.c +518 -0
  93. data/ext/lcmseqrun/src/itemset.h +157 -0
  94. data/ext/lcmseqrun/src/itemset_zero.c +522 -0
  95. data/ext/lcmseqrun/src/lcm_seq.c +447 -0
  96. data/ext/lcmseqrun/src/lcm_seq_zero.c +446 -0
  97. data/ext/lcmseqrun/src/problem.c +439 -0
  98. data/ext/lcmseqrun/src/problem.h +179 -0
  99. data/ext/lcmseqrun/src/problem_zero.c +439 -0
  100. data/ext/lcmseqrun/src/queue.c +533 -0
  101. data/ext/lcmseqrun/src/queue.h +182 -0
  102. data/ext/lcmseqrun/src/stdlib2.c +1350 -0
  103. data/ext/lcmseqrun/src/stdlib2.h +864 -0
  104. data/ext/lcmseqrun/src/trsact.c +747 -0
  105. data/ext/lcmseqrun/src/trsact.h +159 -0
  106. data/ext/lcmseqrun/src/vec.c +779 -0
  107. data/ext/lcmseqrun/src/vec.h +172 -0
  108. data/ext/lcmtransrun/extconf.rb +18 -0
  109. data/ext/lcmtransrun/lcmtransrun.cpp +264 -0
  110. data/ext/macerun/extconf.rb +20 -0
  111. data/ext/macerun/macerun.cpp +57 -0
  112. data/ext/macerun/src/aheap.c +217 -0
  113. data/ext/macerun/src/aheap.h +112 -0
  114. data/ext/macerun/src/itemset.c +491 -0
  115. data/ext/macerun/src/itemset.h +158 -0
  116. data/ext/macerun/src/mace.c +503 -0
  117. data/ext/macerun/src/problem.c +346 -0
  118. data/ext/macerun/src/problem.h +174 -0
  119. data/ext/macerun/src/queue.c +529 -0
  120. data/ext/macerun/src/queue.h +177 -0
  121. data/ext/macerun/src/sgraph.c +360 -0
  122. data/ext/macerun/src/sgraph.h +174 -0
  123. data/ext/macerun/src/stdlib2.c +993 -0
  124. data/ext/macerun/src/stdlib2.h +811 -0
  125. data/ext/macerun/src/vec.c +634 -0
  126. data/ext/macerun/src/vec.h +170 -0
  127. data/ext/sspcrun/extconf.rb +20 -0
  128. data/ext/sspcrun/src/_sspc.c +358 -0
  129. data/ext/sspcrun/src/aheap.c +545 -0
  130. data/ext/sspcrun/src/aheap.h +251 -0
  131. data/ext/sspcrun/src/base.c +92 -0
  132. data/ext/sspcrun/src/base.h +59 -0
  133. data/ext/sspcrun/src/fstar.c +496 -0
  134. data/ext/sspcrun/src/fstar.h +80 -0
  135. data/ext/sspcrun/src/grhfil.c +213 -0
  136. data/ext/sspcrun/src/itemset.c +713 -0
  137. data/ext/sspcrun/src/itemset.h +170 -0
  138. data/ext/sspcrun/src/problem.c +415 -0
  139. data/ext/sspcrun/src/problem.h +179 -0
  140. data/ext/sspcrun/src/queue.c +533 -0
  141. data/ext/sspcrun/src/queue.h +182 -0
  142. data/ext/sspcrun/src/sample.c +19 -0
  143. data/ext/sspcrun/src/sspc.c +598 -0
  144. data/ext/sspcrun/src/sspc2.c +491 -0
  145. data/ext/sspcrun/src/stdlib2.c +1482 -0
  146. data/ext/sspcrun/src/stdlib2.h +892 -0
  147. data/ext/sspcrun/src/trsact.c +817 -0
  148. data/ext/sspcrun/src/trsact.h +160 -0
  149. data/ext/sspcrun/src/vec.c +745 -0
  150. data/ext/sspcrun/src/vec.h +172 -0
  151. data/ext/sspcrun/sspcrun.cpp +54 -0
  152. data/lib/nysol/enumLcmEp.rb +338 -0
  153. data/lib/nysol/enumLcmEsp.rb +284 -0
  154. data/lib/nysol/enumLcmIs.rb +275 -0
  155. data/lib/nysol/enumLcmSeq.rb +143 -0
  156. data/lib/nysol/items.rb +201 -0
  157. data/lib/nysol/seqDB.rb +256 -0
  158. data/lib/nysol/take.rb +39 -0
  159. data/lib/nysol/taxonomy.rb +113 -0
  160. data/lib/nysol/traDB.rb +257 -0
  161. metadata +239 -0
@@ -0,0 +1,1350 @@
1
+ /* library for standard macros and functions */
2
+ /* by Takeaki Uno 2/22/2002, e-mail: uno@nii.jp
3
+ homepage: http://research.nii.ac.jp/~uno/index.html */
4
+ /* This program is available for only academic use, basically.
5
+ Anyone can modify this program, but he/she has to write down
6
+ the change of the modification on the top of the source code.
7
+ Neither contact nor appointment to Takeaki Uno is needed.
8
+ If one wants to re-distribute this code, please
9
+ refer the newest code, and show the link to homepage of
10
+ Takeaki Uno, to notify the news about the codes for the users. */
11
+
12
+ #ifndef _stdlib2_c_
13
+ #define _stdlib2_c_
14
+
15
+ #include"stdlib2.h"
16
+ #ifdef MTWISTER
17
+ #include"dSFMT.c"
18
+ #endif
19
+
20
+ size_t common_size_t;
21
+ INT common_INT, common_INT2;
22
+ char *common_charp, *common_pnt;
23
+ FILE *common_FILE;
24
+ FILE2 common_FILE2;
25
+ PERM common_PERM;
26
+ char common_comm[1024], common_comm2[1024], *common_argv[100]; // max. command length = 2048, max. #params = 100
27
+
28
+ char *ERROR_MES = NULL;
29
+ int print_time_flag=0;
30
+ PARAMS internal_params;
31
+ #ifdef MULTI_CORE
32
+ int SPIN_LOCK_dummy;
33
+ #endif
34
+ FILE2 INIT_FILE2 = {TYPE_FILE2,NULL,NULL,NULL,NULL,0};
35
+ VEC INIT_VEC = {TYPE_VEC,NULL,0,0};
36
+ FILE_COUNT INIT_FILE_COUNT = {0,0,0,0,0,0,0,0,0,0,0,0,NULL,NULL,0,0,NULL,NULL,0,0,NULL,NULL};
37
+ short FILE2_POW[5] = {1, 10, 100, 1000, 10000};
38
+ #ifdef _FILE2_LOAD_FROM_MEMORY_
39
+ void *__load_from_memory__=NULL, *__load_from_memory_org__=NULL, *__load_from_memory_org2__=NULL;
40
+ char *__write_to_memory__=NULL, *__write_to_memory_org__=NULL, *__write_to_memory_next__=NULL;
41
+ #endif
42
+
43
+ QSORT_TYPE (int, int)
44
+ QSORT_TYPE (uint, unsigned int)
45
+ QSORT_TYPE (double, double)
46
+ QSORT_TYPE (char, char)
47
+ QSORT_TYPE (uchar, unsigned char)
48
+ QSORT_TYPE (short, short)
49
+ QSORT_TYPE (ushort, unsigned short)
50
+ QSORT_TYPE (WEIGHT, WEIGHT)
51
+ QSORT_TYPE (LONG, LONG)
52
+ QSORT_TYPE (VEC_ID, VEC_ID)
53
+ QSORT_TYPE (VEC_VAL, VEC_VAL)
54
+ QSORT_TYPE (VEC_VAL2, VEC_VAL2)
55
+ QSORT_TYPE (FILE_COUNT_INT, FILE_COUNT_INT)
56
+
57
+ /* bitmasks, used for bit operations */
58
+ int BITMASK_UPPER1[32] = { 0xffffffff, 0xfffffffe, 0xfffffffc, 0xfffffff8,
59
+ 0xfffffff0, 0xffffffe0, 0xffffffc0, 0xffffff80,
60
+ 0xffffff00, 0xfffffe00, 0xfffffc00, 0xfffff800,
61
+ 0xfffff000, 0xffffe000, 0xffffc000, 0xffff8000,
62
+ 0xffff0000, 0xfffe0000, 0xfffc0000, 0xfff80000,
63
+ 0xfff00000, 0xffe00000, 0xffc00000, 0xff800000,
64
+ 0xff000000, 0xfe000000, 0xfc000000, 0xf8000000,
65
+ 0xf0000000, 0xe0000000, 0xc0000000, 0x80000000 };
66
+ int BITMASK_UPPER1_[32] = { 0xfffffffe, 0xfffffffc, 0xfffffff8, 0xfffffff0,
67
+ 0xffffffe0, 0xffffffc0, 0xffffff80, 0xffffff00,
68
+ 0xfffffe00, 0xfffffc00, 0xfffff800, 0xfffff000,
69
+ 0xffffe000, 0xffffc000, 0xffff8000, 0xffff0000,
70
+ 0xfffe0000, 0xfffc0000, 0xfff80000, 0xfff00000,
71
+ 0xffe00000, 0xffc00000, 0xff800000, 0xff000000,
72
+ 0xfe000000, 0xfc000000, 0xf8000000, 0xf0000000,
73
+ 0xe0000000, 0xc0000000, 0x80000000, 0x00000000 };
74
+
75
+ int BITMASK_LOWER1[32] = { 0x00000000, 0x00000001, 0x00000003, 0x00000007,
76
+ 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f,
77
+ 0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff,
78
+ 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff,
79
+ 0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff,
80
+ 0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff,
81
+ 0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff,
82
+ 0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff };
83
+ int BITMASK_LOWER1_[32] = { 0x00000001, 0x00000003, 0x00000007, 0x0000000f,
84
+ 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
85
+ 0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
86
+ 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
87
+ 0x0001ffff, 0x0003ffff, 0x0007ffff, 0x000fffff,
88
+ 0x001fffff, 0x003fffff, 0x007fffff, 0x00ffffff,
89
+ 0x01ffffff, 0x03ffffff, 0x07ffffff, 0x0fffffff,
90
+ 0x1fffffff, 0x3fffffff, 0x7fffffff, 0xffffffff };
91
+
92
+ int BITMASK_1[32] = { 0x00000001, 0x00000002, 0x00000004, 0x00000008,
93
+ 0x00000010, 0x00000020, 0x00000040, 0x00000080,
94
+ 0x00000100, 0x00000200, 0x00000400, 0x00000800,
95
+ 0x00001000, 0x00002000, 0x00004000, 0x00008000,
96
+ 0x00010000, 0x00020000, 0x00040000, 0x00080000,
97
+ 0x00100000, 0x00200000, 0x00400000, 0x00800000,
98
+ 0x01000000, 0x02000000, 0x04000000, 0x08000000,
99
+ 0x10000000, 0x20000000, 0x40000000, 0x80000000 };
100
+ int BITMASK_31[32] = { 0xfffffffe, 0xfffffffd, 0xfffffffb, 0xfffffff7,
101
+ 0xffffffef, 0xffffffdf, 0xffffffbf, 0xffffff7f,
102
+ 0xfffffeff, 0xfffffdff, 0xfffffbff, 0xfffff7ff,
103
+ 0xffffefff, 0xffffdfff, 0xffffbfff, 0xffff7fff,
104
+ 0xfffeffff, 0xfffdffff, 0xfffbffff, 0xfff7ffff,
105
+ 0xffefffff, 0xffdfffff, 0xffbfffff, 0xff7fffff,
106
+ 0xfeffffff, 0xfdffffff, 0xfbffffff, 0xf7ffffff,
107
+ 0xefffffff, 0xdfffffff, 0xbfffffff, 0x7fffffff };
108
+
109
+ int BITMASK_16[8] = { 0x0000000f, 0x000000f0, 0x00000f00, 0x0000f000,
110
+ 0x000f0000, 0x00f00000, 0x0f000000, 0xf0000000 };
111
+ int BITMASK_UPPER16[8] = { 0xffffffff, 0xfffffff0, 0xffffff00, 0xfffff000,
112
+ 0xffff0000, 0xfff00000, 0xff000000, 0xf0000000 };
113
+ int BITMASK_LOWER16[8] = { 0x0000000f, 0x000000ff, 0x00000fff, 0x0000ffff,
114
+ 0x000fffff, 0x00ffffff, 0x0fffffff, 0xffffffff };
115
+ int BITMASK_FACT16[8] = { 0x1, 0x10, 0x100, 0x1000,
116
+ 0x10000, 0x100000, 0x1000000,0x10000000 };
117
+
118
+
119
+ /* free many pointers */
120
+ void mfree_(void *x, ...){
121
+ va_list argp;
122
+ void *a;
123
+ va_start (argp, x);
124
+ while((a = va_arg(argp, void *)) != (void*)1){ free2 (a); }
125
+ va_end (argp);
126
+ }
127
+ /* free many pointers */
128
+ void mfree2_(void *x, ...){
129
+ va_list argp;
130
+ void *a;
131
+ va_start (argp, x);
132
+ while((a = va_arg(argp, void *)) != (void*)1){ free2 (*((char **)a)); }
133
+ va_end (argp);
134
+ }
135
+ /* remove many files */
136
+ void mremove_ (char *x, ...){
137
+ va_list argp;
138
+ char *a;
139
+ va_start (argp, x);
140
+ while((a = va_arg(argp, char *))){
141
+ sprintf (common_comm, "%s%s", x, a);
142
+ remove (common_comm);
143
+ }
144
+ va_end (argp);
145
+ }
146
+
147
+ /* compute the minimum prime no less than n */
148
+ #define MINPRIME_END 6000
149
+ LONG min_prime (LONG n){
150
+ LONG i, j=30, k;
151
+ char f[MINPRIME_END];
152
+ while(1) {
153
+ FLOOP (i, 0, j) f[i]=0;
154
+ for ( i=3 ; i*i < n+j ; i+=2 )
155
+ for ( k=((n+i-1)/i)*i ; k<i+j ; k+=i ) f[(k-n)/2] = 1;
156
+ FLOOP (i, 0, j) if ( f[i] == 0 ) return (n+ i*2+1);
157
+ j *= 2;
158
+ }
159
+ }
160
+
161
+ /* decompose the string by separator, and set v[i] to each resulted string.
162
+ consecutive separators are regarded as one separator. */
163
+ /* string s has to have end mark 0 at the end */
164
+ /* original string s will be written, so that each separator will be end mark 0 */
165
+ /* at most [num] strings will be generated */
166
+ int string_decompose ( char **v, char *s, char sep, int max){
167
+ int i=0, dq;
168
+ char *ss = s;
169
+ do {
170
+ dq = 0;
171
+ while (*ss == sep) ss++;
172
+ if ( *ss == 0 ) break;
173
+ if ( *ss == '\"' ){ dq = 1; v[i++] = ++ss; }
174
+ else v[i++] = ss;
175
+ while (*ss != sep || dq){
176
+ if ( dq==0 && *ss == 0 ) break;
177
+ if ( dq && *ss == '\"' ) break;
178
+ ss++;
179
+ }
180
+ if ( *ss == 0 ) break;
181
+ *(ss++) = 0;
182
+ } while ( i<max);
183
+ return (i);
184
+ }
185
+
186
+ unsigned long xor128(){
187
+ static unsigned long x=123456789,y=362436069,z=521288629,w=88675123;
188
+ unsigned long t;
189
+ t=(x^(x<<11));x=y;y=z;z=w; return( w=(w^(w>>19))^(t^(t>>8)) );
190
+ }
191
+
192
+ /***********************************************************************/
193
+ /***********************************************************************/
194
+ #ifdef USE_MATH
195
+ #define NORMAL_RAND_BASE 2147483648LL
196
+
197
+ double SQRT(double x){
198
+ #ifdef USE_SIMD
199
+ __m128d a;
200
+ a = _mm_load1_pd (&x);
201
+ _mm_sqrt_pd (a);
202
+ _mm_storel_pd (&x, a);
203
+ #else
204
+ x = sqrt (x);
205
+ #endif
206
+ return (x);
207
+ }
208
+ /* make two random numbers under normal distribution N(0,1) */
209
+ void rand_mk_2normal (double *a, double *b){
210
+ double r1, r2;
211
+ do {
212
+ r1 = RAND1;
213
+ } while (r1==0);
214
+ r2 = RAND1;
215
+ r1 = sqrt(-log(r1)*2);
216
+ r2 *= 2*PI;
217
+ *a = r1*sin(r2);
218
+ *b = r1*cos(r2);
219
+ }
220
+
221
+ /* make a random point on a supersphere of d-dim., and set to double array already allocated */
222
+ void rand_d_gaussian (double *p, int d){
223
+ int i;
224
+ double a, b;
225
+ for (i=0 ; i<d ; i+=2){
226
+ rand_mk_2normal ( &a, &b);
227
+ p[i] = a;
228
+ if ( i+1 < d ) p[i+1] = b;
229
+ }
230
+ }
231
+ void rand_sphere (double *p, int d){
232
+ rand_d_gaussian (p, d);
233
+ ARY_NORMALIZE (p, d);
234
+ }
235
+
236
+ /* compute the integral of cosin x with exponent n */
237
+ double int_cos_n (double x, int n){
238
+ if ( n == 1 ) return (sin(x));
239
+ if ( n == 2 ) return (x/2+sin(2*x)/4);
240
+ return ( int_cos_n (x, n-2) *(n-1)/n - pow(cos(x), n-1)*sin(x) /n );
241
+ }
242
+ double int_sin_n (double x, int n){
243
+ if ( n == 1 ) return (-cos(x));
244
+ if ( n == 2 ) return (x/2-sin(2*x)/4);
245
+ return ( int_sin_n (x, n-2) *(n-1)/n - pow(sin(x), n-1)*cos(x) /n );
246
+ }
247
+
248
+
249
+ double sphere_prob_even (int d, double t) {
250
+ int i;
251
+ double q, r, ss;
252
+ double y = acos(t);
253
+ double s = sin(y);
254
+
255
+ /*
256
+ * sum_{k=0}^{d/2-2}sin^{d-2k-1}(y) (d-2k-2)!!/(d-2k-1)!!
257
+ * = sum_{k=1}^{d/2-1}sin^{2k+1}(y) (2k)!!/(2k+1)!!
258
+ */
259
+ q = 0.0;
260
+ ss = s * s;
261
+ r = s;
262
+ for (i = 3; i < d; i += 2) {
263
+ r *= ss * (i - 1) / i;
264
+ q += r;
265
+ }
266
+ return (y - sin(2*y)/2 - t*q) / M_PI;
267
+ }
268
+
269
+ double sphere_prob_odd (int d, double t) {
270
+ int i;
271
+ double q, r, ss;
272
+ double s = sin(acos(t));
273
+
274
+ /*
275
+ * sum_{k=0}^{d/2-2}s^{d-2k-1} (d-2k-2)!!/(d-2k-1)!!
276
+ * = sum_{k=1}^{d/2-1}s^{2k} (2k-1)!!/(2k)!!
277
+ */
278
+ q = r = 1.0; ss = s * s;
279
+ for (i = 2; i < d; i += 2) {
280
+ r *= ss * (i - 1) / i;
281
+ q += r;
282
+ }
283
+ return (1 - t*q) / 2;
284
+ }
285
+
286
+ double sphere_prob (int d, double t){
287
+ if ( d % 2 ) return (sphere_prob_odd (d, t));
288
+ return (sphere_prob_even (d, t));
289
+ }
290
+
291
+ double sphere_prob_inv (int d, double m){
292
+ double s=0, t=1;
293
+ while ( t-s > 0.000001 ){
294
+ if ( sphere_prob (d, (t+s)/2) > m ) s = (t+s)/2;
295
+ else t = (t+s)/2;
296
+ }
297
+ return (s);
298
+ }
299
+
300
+
301
+ #endif
302
+
303
+ /******************** file I/O routines ********************************/
304
+
305
+ int FILE_err; /* signals 0: for normal termination
306
+ 1: read a number, then encountered a newline,
307
+ 2: read a number, then encountered the end-of-file
308
+ 5: read no number, and encountered a newline
309
+ 6: read no number, and encountered the end-of-file */
310
+
311
+
312
+ void FILE2_flush (FILE2 *fp){
313
+ #ifndef _FILE2_LOAD_FROM_MEMORY_
314
+ if ( fp->buf > fp->buf_org+FILE2_BUFSIZ/2 ){
315
+ fwrite (fp->buf_org, fp->buf-fp->buf_org, 1, fp->fp);
316
+ *fp->buf_org = *fp->buf;
317
+ fp->buf = fp->buf_org;
318
+ }
319
+ #endif
320
+ }
321
+ void FILE2_flush_last (FILE2 *fp){
322
+ #ifdef _FILE2_LOAD_FROM_MEMORY_
323
+ FILE2_WRITE_MEMORY (FILE2_LOAD_FROM_MEMORY_ENDTYPE, FILE2_LOAD_FROM_MEMORY_END-1);
324
+ #else
325
+ if ( fp->bit > 0 ) fp->buf++;
326
+ if ( fp->buf > fp->buf_org ){
327
+ fwrite ( fp->buf_org, fp->buf-fp->buf_org, 1, fp->fp);
328
+ fp->buf = fp->buf_org;
329
+ }
330
+ #endif
331
+ }
332
+ void FILE2_close (FILE2 *fp){
333
+ fclose2 (fp->fp);
334
+ free2 (fp->buf_org);
335
+ fp->buf = fp->buf_end = 0;
336
+ }
337
+ void FILE2_closew (FILE2 *fp){
338
+ FILE2_flush_last (fp);
339
+ #ifdef _FILE2_LOAD_FROM_MEMORY_
340
+ FILE2_WRITE_MEMORY (FILE2_LOAD_FROM_MEMORY_ENDTYPE, FILE2_LOAD_FROM_MEMORY_END-1);
341
+ #endif
342
+ fclose2 (fp->fp);
343
+ free2 (fp->buf_org);
344
+ fp->buf = fp->buf_end = 0;
345
+ }
346
+ void FILE2_reset (FILE2 *fp){
347
+ #ifdef _FILE2_LOAD_FROM_MEMORY_
348
+ __load_from_memory__=__load_from_memory_org__;
349
+ #else
350
+ fp->buf = fp->buf_org;
351
+ fp->buf_end = fp->buf_org-1;
352
+ fseek (fp->fp, 0, SEEK_SET);
353
+ #endif
354
+ }
355
+ void FILE2_copy (char *f1, char *f2){ // copy file f1 to f2, f2 will be cleared or created
356
+ FILE *fp, *fp2;
357
+ char buf[16384];
358
+ int s;
359
+
360
+ fopen2 (fp, f1, "r", EXIT);
361
+ fopen2 (fp2, f2, "w", EXIT);
362
+ do {
363
+ s = fread (buf, 1, 16384, fp);
364
+ fwrite (buf, 1, s, fp2);
365
+ } while (s);
366
+ fclose (fp);
367
+ fclose (fp2);
368
+ }
369
+
370
+ /* fast file routine, getc, putc, puts, */
371
+ int FILE2_getc (FILE2 *fp){
372
+ int c;
373
+ #ifdef _FILE2_LOAD_FROM_MEMORY_
374
+
375
+ #else
376
+ if ( fp->buf >= fp->buf_end ){
377
+ if ( (fp->buf_end < fp->buf_org+FILE2_BUFSIZ) && (fp->buf_end>=fp->buf_org) ){ FILE_err=2; return (-1); }
378
+ fp->buf = fp->buf_org;
379
+ fp->buf_end = fp->buf_org + fread (fp->buf, 1, FILE2_BUFSIZ, fp->fp);
380
+ return (FILE2_getc (fp));
381
+ }
382
+ c = (unsigned char)(*(fp->buf));
383
+ fp->buf++;
384
+ #endif
385
+ return (c);
386
+ }
387
+ void FILE2_puts (FILE2 *fp, char *s){
388
+ #ifndef _FILE2_LOAD_FROM_MEMORY_
389
+ while ( *s != 0 ){
390
+ *(fp->buf) = *s;
391
+ s++;
392
+ fp->buf++;
393
+ }
394
+ #endif
395
+ }
396
+ void FILE2_putc (FILE2 *fp, char c){
397
+ #ifndef _FILE2_LOAD_FROM_MEMORY_
398
+ *(fp->buf) = c;
399
+ fp->buf++;
400
+ #endif
401
+ }
402
+ int FILE2_getbit (FILE2 *fp){
403
+ int a;
404
+ unsigned char *p;
405
+ if ( fp->buf >= fp->buf_end ){
406
+ if ( (fp->buf_end < fp->buf_org+FILE2_BUFSIZ) && (fp->buf_end>=fp->buf_org) ){ FILE_err=2; return (-1); }
407
+ fp->buf = fp->buf_org;
408
+ fp->buf_end = fp->buf_org + fread (fp->buf, 1, FILE2_BUFSIZ, fp->fp);
409
+ }
410
+ p = (unsigned char *)fp->buf; a = (*p) %2; (*p) /=2;
411
+ if ( (++fp->bit) == 8 ){ fp->bit = 0; fp->buf++; }
412
+ return (a);
413
+ }
414
+ void FILE2_putbit (FILE2 *fp, int a){ // a is non-zero => 1
415
+ if ( a ){
416
+ a = 1 << fp->bit;
417
+ *fp->buf |= a;
418
+ }
419
+ if ( (++fp->bit) == 8 ){
420
+ fp->bit = 0;
421
+ fp->buf++;
422
+ FILE2_flush (fp);
423
+ *fp->buf = 0;
424
+ }
425
+ }
426
+
427
+ /* fast file routine, print number, c is the char to be printed preceding to the number
428
+ if c==0, nothing will be printed preceding the number
429
+ if len<0 then the #digits following '.' does not change (filed by '0') */
430
+ void STR_print_int (char **s, LONG n, char c){
431
+ LONG nn = n;
432
+ char *ss;
433
+ if ( c ){ **s = c; (*s)++; }
434
+ if ( n == 0 ){ **s = '0'; (*s)++; return; }
435
+ if ( n < 0 ){ **s = '-'; (*s)++; n = -n; }
436
+ while ( nn>99999 ){ nn /= 1000000; (*s)+=6; }
437
+ while ( nn>99 ){ nn /= 1000; (*s)+=3; }
438
+ while ( nn>0 ){ nn /= 10; (*s)++; }
439
+ ss = (*s)-1;
440
+ **s = 0;
441
+ while ( n>0 ){ *ss = '0'+(char)(n%10); ss--; n/=10; }
442
+ }
443
+ void STR_print_int_ (char **s, LONG n, int len, char c){
444
+ char *ss;
445
+ if ( c ){ **s = c; (*s)++; }
446
+ if ( n == 0 ){ **s = '0'; (*s)++; return; }
447
+ if ( n < 0 ){ **s = '-'; (*s)++; n = -n; }
448
+ (*s)+=len;
449
+ ss = (*s)-1;
450
+ **s = 0;
451
+ while ( len>0 ){ *ss = '0'+(char)(n%10); ss--; n/=10; len--; }
452
+ }
453
+ void FILE2_print_int (FILE2 *fp, LONG n, char c){
454
+ #ifdef _FILE2_LOAD_FROM_MEMORY_
455
+ FILE2_WRITE_MEMORY (int, n);
456
+ #else
457
+ STR_print_int (&(fp->buf), n, c);
458
+ #endif
459
+ }
460
+ /* print a real number to string pointed by *s,
461
+ (print_reale prints in format "x.xxxxxe+10e"
462
+ [len] is #decimal digits to be printed
463
+ for speeding up, give 10^{len} as [len], when len >1 */
464
+ void STR_print_reale (char **s, double n, int len, char c){
465
+ int flag=0, d=0, dd;
466
+ LONG nn;
467
+
468
+ if ( len >= 100 ) nn = len;
469
+ else {
470
+ nn = 1;
471
+ while ( len >= 9 ){ nn *= 1000000000; len -= 9; }
472
+ while ( len >= 3 ){ nn *= 1000; len -= 3; }
473
+ while ( len >= 1 ){ nn *= 10; len --; }
474
+ }
475
+
476
+ if ( c ){ **s = c; (*s)++; }
477
+ if ( n<0 ){ **s = c; (*s)++; n = -n; }
478
+ if ( n<1 ){
479
+ flag = 1;
480
+ while ( n < 0.00000001 ){ d+=9; n *= 1000000000;}
481
+ while ( n < 0.01 ){ d+=3; n *= 1000;}
482
+ while ( n < 1 ){ d++; n *= 10;}
483
+ } else {
484
+ while ( n >= 1000000000 ){ d+=9; n /= 1000000000;}
485
+ while ( n >= 1000 ){ d+=3; n /= 1000;}
486
+ while ( n >= 10 ){ d++; n /= 10;}
487
+ }
488
+
489
+ dd = n; **s = '0' + dd; (*s)++; n -= dd;
490
+ **s = '.'; (*s)++;
491
+ nn = n*nn; STR_print_int (s, nn, 0);
492
+ while ( *(*s -1) == '0' ) (*s)--;
493
+ if ( *(*s -1) == '.' ) (*s)--;
494
+ **s = 'e'; (*s)++; **s = flag? '-': '+'; (*s)++;
495
+ STR_print_int (s, d, 0);
496
+ **s = 0;
497
+ }
498
+ void FILE2_print_reale (FILE2 *fp, double n, int len, char c){
499
+ #ifdef _FILE2_LOAD_FROM_MEMORY_
500
+ FILE2_WRITE_MEMORY (double, n);
501
+ #else
502
+ STR_print_reale (&(fp->buf), n, len, c);
503
+ #endif
504
+ }
505
+ /* print a real number to string pointed by *s */
506
+ void STR_print_real (char **s, double n, int len, char c){
507
+ int flag=1, d=0, dd, ll = len;
508
+ LONG nn;
509
+
510
+ if ( c ){ **s = c; (*s)++; }
511
+ if ( n<0 ){ **s = '-'; (*s)++; n = -n; }
512
+ if ( n<1 ){ **s = '0'; (*s)++; }
513
+ else {
514
+ while ( n > (1LL<<60) ){ d++; n /= (1000000000000000000LL);}
515
+ nn = n; STR_print_int (s, nn, 0);
516
+ BLOOP (d, d, 0) FLOOP (dd, 0, 18){ **s = '0'; (*s)++; }
517
+ n -= nn;
518
+ }
519
+ if ( len == 0 ) return; // no decimal digits
520
+ **s = '.'; (*s)++;
521
+ if ( len<0 ){ len = -len; flag = 0; }
522
+ nn = 1;
523
+ if ( len >= 100 ) nn = len;
524
+ else {
525
+ while ( len >= 6 ){ nn *= 1000000; len -= 6; }
526
+ while ( len >= 3 ){ nn *= 1000; len -= 3; }
527
+ while ( len >= 1 ){ nn *= 10; len --; }
528
+ }
529
+ nn = n*nn;
530
+ STR_print_int_ (s, nn, ll, 0);
531
+ if ( flag ){
532
+ while ( *(*s -1) == '0' ) (*s)--;
533
+ if ( *(*s -1) == '.' ) (*s)--;
534
+ }
535
+ **s = 0;
536
+ }
537
+ void FILE2_print_real (FILE2 *fp, double n, int len, char c){
538
+ #ifdef _FILE2_LOAD_FROM_MEMORY_
539
+ FILE2_WRITE_MEMORY (double, n);
540
+ #else
541
+ STR_print_real (&(fp->buf), n, len, c);
542
+ #endif
543
+ }
544
+ void FILE2_print_real__ (FILE2 *fp, double n, int len, char c){
545
+ int i=0, flag=1, d=0, dd=0;
546
+ char *back;
547
+ LONG nn;
548
+
549
+ if ( c ) FILE2_putc (fp, c);
550
+ if ( n<0 ){ FILE2_putc (fp, '-'); n *= -1; }
551
+ if ( n<1 ){ *(fp->buf) = '0'; fp->buf++; }
552
+ else {
553
+ while ( n > (1LL<<60) ){ d++; n /= (1000000000000000000LL);}
554
+ nn = n; FILE2_print_int (fp, nn, 0);
555
+ BLOOP (d, d, 0) FLOOP (dd, 0, 18) FILE2_putc (fp, '0');
556
+ n -= nn;
557
+ }
558
+ if ( len == 0 ) return; // no digits smaller than 1
559
+ *(fp->buf) = '.'; back = fp->buf;
560
+ fp->buf++;
561
+ if ( len<0 ){ len = -len; flag = 0; }
562
+ for (d=0 ; len>0 ; len--){
563
+ if ( d==0 ){
564
+ d = 4;
565
+ n *= 10000.0;
566
+ dd = n;
567
+ n -= dd;
568
+ }
569
+ if ( --d > 0 ){
570
+ i = dd/FILE2_POW[d];
571
+ dd -= i*FILE2_POW[d];
572
+ }
573
+ *(fp->buf) = '0'+i;
574
+ fp->buf++;
575
+ if ( i>0 ) back = fp->buf;
576
+ }
577
+ if ( flag ) fp->buf = back;
578
+ }
579
+ void FILE2_print_real_ (FILE2 *fp, double n, int len, char c){
580
+ int i=0, flag=1;
581
+ double j=1;
582
+ char *back;
583
+
584
+ if ( c ) FILE2_putc (fp, c);
585
+ if ( n<0 ){ FILE2_putc (fp, '-'); n *= -1; }
586
+ while ( n >= j ) j*=10;
587
+ if ( j==1 ){ *(fp->buf) = '0'; fp->buf++; }
588
+ else while ( j>1 ){
589
+ j /= 10;
590
+ i = (int)(n/j);
591
+ *(fp->buf) = '0'+i;
592
+ n -= j*i;
593
+ fp->buf++;
594
+ }
595
+ *(fp->buf) = '.'; back = fp->buf;
596
+ fp->buf++;
597
+ if ( len<0 ){ len = -len; flag = 0; }
598
+ for ( ; len>0 ; len--){
599
+ n *= 10.0;
600
+ i = (int)n;
601
+ *(fp->buf) = '0'+i;
602
+ n -= i;
603
+ fp->buf++;
604
+ if ( i>0 ) back = fp->buf;
605
+ }
606
+ if ( flag ) fp->buf = back;
607
+ }
608
+ /******/
609
+ void FILE2_print_WEIGHT (FILE2 *fp, WEIGHT w, int len, char c){
610
+ #ifdef WEIGHT_DOUBLE
611
+ FILE2_print_real(fp, w, len, c);
612
+ #else
613
+ FILE2_print_int(fp, w, c);
614
+ #endif
615
+ }
616
+
617
+ // print integer to file in a bit string manner
618
+ // first bit 0 -> 0to127
619
+ // 10 -> 128 to 16384+127
620
+ // 110 -> 16384+128 to 32*65536+16384+127
621
+ // 1110 -> ... to 16*
622
+ // max: limit of the bit length
623
+ void FILE2_print_mpint (FILE2 *fp, LONG a, int max, int skip){
624
+ int i=0, bb;
625
+ LONG b, x=0;
626
+ bb = 1<<skip; b = bb;
627
+ while ( a-x >= b ){ x += b; b *= bb; i++; if ( (i+1)*skip >= max ) break; }
628
+ a -= x;
629
+ FLOOP (x, 0, i) FILE2_putbit (fp, 1); // length of the integer
630
+ if ( (i+1)*skip < max ) FILE2_putbit (fp, 0); // end mark
631
+ else for ( ; max < (i+1)*skip ; max++, b/=2);
632
+ for (b/=2 ; b>0 ; b/=2) FILE2_putbit (fp, (a&b)>0);
633
+ }
634
+
635
+ LONG FILE2_read_mpint (FILE2 *fp, int max, int skip){
636
+ LONG a, b, x=0;
637
+ int i=0, j, bb;
638
+ for (b=bb=1<<skip; (j=FILE2_getbit (fp)) ; i++){
639
+ if ( j<0 ){ FILE_err = 2; return (0); }
640
+ x += b; b *= bb;
641
+ if ( (i+2)*skip >= max ){
642
+ for ( ; max < (i+2)*skip ; max++, b/=2);
643
+ break;
644
+ }
645
+ }
646
+ for (a=0 ; b>1 ; b/=2) a = a*2 + FILE2_getbit(fp);
647
+ return (a+x);
648
+ }
649
+ /* print lowest k bits to file */
650
+ void FILE2_print_intbit (FILE2 *fp, LONG a, int k){
651
+ LONG b= 1LL << (k-1);
652
+ while (b>0){ FILE2_putbit (fp, (a&b)>0); b /= 2; }
653
+ }
654
+ /* read k bits from file as integer */
655
+ LONG FILE2_read_intbit (FILE2 *fp, int k){
656
+ LONG a=0;
657
+ while (k-->0){ a = a*2 + FILE2_getbit (fp); }
658
+ return (a);
659
+ }
660
+
661
+ #define FILE2_READ_CH(v) \
662
+
663
+ //#define FILE2_READ_(v)
664
+
665
+ /* Read an integer/a double from the file and return it,
666
+ with read through the non-numeric letters.
667
+ If it reaches to the end-of-file just after reading a number, then set FILE_err=2,
668
+ if it reads a newline just after reading a number, then set FILE_err=1.
669
+ If read either the end-of-file or newline before reading an integer,
670
+ return 5, and 6 */
671
+ FILE_LONG FILE2_read_int (FILE2 *fp){
672
+ FILE_LONG item;
673
+ int sign=1, ch;
674
+ FILE_err = 0;
675
+ #ifdef _FILE2_LOAD_FROM_MEMORY_
676
+ ch = *((int *)__load_from_memory__);
677
+ __load_from_memory__ = ((int *)__load_from_memory__) + 1;
678
+ if ( ch == FILE2_LOAD_FROM_MEMORY_END ){ FILE_err = 5; return (-INTHUGE); }
679
+ if ( ch == FILE2_LOAD_FROM_MEMORY_END-1 ){ FILE_err = 6; return (-INTHUGE); }
680
+ return (ch);
681
+ #else
682
+ while (1){
683
+ ch = FILE2_getc(fp);
684
+ if ( ch>='0' && ch<='9' ) break;
685
+ if ( ch == '\n' ){ FILE_err = 5; return (-INTHUGE); }
686
+ if ( ch < 0 ){ FILE_err = 6; return (-INTHUGE); }
687
+ if ( ch=='-' ) sign = -1;
688
+ }
689
+ for (item=ch-'0' ; 1 ; item=item*10 +ch-'0'){
690
+ ch = FILE2_getc(fp);
691
+ if ( ch<'0' || ch>'9' ){
692
+ if ( ch == '\n' ) FILE_err = 1;
693
+ if ( ch < 0 ) FILE_err = 2;
694
+ return (item*sign);
695
+ }
696
+ }
697
+ #endif
698
+ }
699
+ double FILE2_read_double (FILE2 *fp){
700
+ double item, geta=1;
701
+ int sign=1, ch, n=0, d, flag=0;
702
+ FILE_err = 0;
703
+ #ifdef _FILE2_LOAD_FROM_MEMORY_
704
+ ch = *((double *)__load_from_memory__);
705
+ __load_from_memory__ = ((double *)__load_from_memory__) + 1;
706
+ if ( ch == DOUBLEHUGE ){ FILE_err = 5; return (-DOUBLEHUGE); }
707
+ if ( ch == DOUBLEHUGE-1 ){ FILE_err = 6; return (-DOUBLEHUGE); }
708
+ #else
709
+ while (1){
710
+ ch = FILE2_getc(fp);
711
+ if ( ch<'0' || ch>'9' ){
712
+ if ( ch == '\n' ){ FILE_err = 5; return (-DOUBLEHUGE); }
713
+ if ( ch < 0 ){ FILE_err = 6; return (-DOUBLEHUGE); }
714
+ if ( ch=='-' ) sign = -1;
715
+ else if ( ch=='.' ){ flag = 1; geta = 0.1; }
716
+ else { sign = 1; geta = 1; flag = 0; }
717
+ } else break;
718
+ }
719
+ item = 0; d = 0; item = ch-'0'; if ( flag ) item /= 10;
720
+
721
+ while (1){
722
+ ch = FILE2_getc(fp);
723
+ if ( ch < '0' || ch > '9' ){
724
+ if ( ch == '\n' ){ FILE_err = 1; break; }
725
+ else if ( ch < 0 ){ FILE_err = 2; break; }
726
+ else if ( ch == '.' ){ // decimal
727
+ if ( flag ) break; // second decimal; illigal syntax
728
+ if ( d ) item = item * FILE2_POW[d] + n;
729
+ flag = 1; n = 0; d = 0;
730
+ } else if ( (ch & 0xdf) == 'E' ){ // power
731
+ if ( d ){
732
+ if ( flag ) item += geta * n / FILE2_POW[d];
733
+ else item = item * FILE2_POW[d] + n;
734
+ }
735
+ flag = n = 0;
736
+ ch = FILE2_getc(fp);
737
+ if ( ch == '-' ){ flag = 1; ch = FILE2_getc(fp); } // power is minus
738
+ else if ( ch == '+' ) ch = FILE2_getc(fp); // power is plus
739
+ else if ( ch < '0' || ch > '9') break;
740
+ for ( n=ch-'0' ; 1 ; n=n*10 + ch-'0' ){
741
+ ch = FILE2_getc(fp);
742
+ if ( ch<'0' || ch>'9' ){
743
+ if ( ch == '\n' ) FILE_err = 1;
744
+ if ( ch < 0 ) FILE_err = 2;
745
+ if ( flag ){
746
+ while ( n>=9 ){ item /= 1000000000.0; n -= 9; }
747
+ while ( n>=3 ){ item /= 1000.0; n -= 3; }
748
+ while ( n>=1 ){ item /= 10.0; n --; }
749
+ } else {
750
+ while ( n>=9 ){ item *= 1000000000.0; n -= 9; }
751
+ while ( n>=3 ){ item *= 1000.0; n -= 3; }
752
+ while ( n>=1 ){ item *= 10.0; n --; }
753
+ }
754
+ return (item*sign);
755
+ }
756
+ }
757
+ } else break;
758
+ } else {
759
+ if ( n>0 ) n *= 10;
760
+ n += ch - '0';
761
+ if ( ++d == 5 ){
762
+ if ( flag ){
763
+ geta /= 100000.0;
764
+ item += geta * n;
765
+ } else item = item * 100000.0 + n;
766
+ n = d = 0;
767
+ }
768
+ }
769
+ }
770
+ if ( d ){
771
+ if ( flag ) item += geta * n / FILE2_POW[d];
772
+ else item = item * FILE2_POW[d] + n;
773
+ }
774
+ return (item*sign);
775
+ #endif
776
+ }
777
+ double FILE2_read_double_ (FILE2 *fp){
778
+ double item, geta=1;
779
+ int sign=1, ch;
780
+ FILE_err = 0;
781
+ while (1){
782
+ ch = FILE2_getc(fp);
783
+ if ( ch < 0 ){ FILE_err = 6; return (-DOUBLEHUGE); }
784
+ if ( ch == '\n' ){ FILE_err = 5; return (-DOUBLEHUGE); }
785
+ if ( ch=='-' ) sign *= -1;
786
+ else if ( ch=='.' ) geta = 0.1;
787
+ else if ( ch>='0' && ch<='9' ) break;
788
+ else { sign = 1; geta = 1; }
789
+ }
790
+
791
+ item = geta * (ch-'0');
792
+ if ( geta < 1.0 ) geta *= .1;
793
+ while (1){
794
+ ch = FILE2_getc(fp);
795
+ if ( ch == '\n' ){ FILE_err = 1; return (item*sign); }
796
+ if ( ch<0 ){ FILE_err = 2; return (item*sign); }
797
+ if ( ch == '.' ) geta = .1;
798
+ else if ( (ch < '0') || (ch > '9')) return (item*sign);
799
+ else if ( geta < 1.0 ){
800
+ item += geta*(ch-'0');
801
+ geta *= 0.1;
802
+ } else item = item*10 + (ch-'0');
803
+ }
804
+ }
805
+
806
+ /* string has to have non-numeric alphabet until its end */
807
+ FILE_LONG STR_read_int (char **s){
808
+ FILE_LONG item;
809
+ int flag =1;
810
+ FILE_err = 0;
811
+ while (1) {
812
+ if ( **s == '\n' ){ FILE_err = 5; return (-INTHUGE); }
813
+ if ( **s=='-' ) flag = -1;
814
+ if ( **s >= '0' && **s <= '9' ) break;
815
+ (*s)++;
816
+ }
817
+ for ( item=(int)((**s)-'0') ; 1 ; item=item*10 +(int)((**s)-'0') ){
818
+ (*s)++;
819
+ if ( ((**s) < '0') || ((**s) > '9')){
820
+ if ( (**s) == '\n' ) FILE_err = 1;
821
+ return (flag*item);
822
+ }
823
+ }
824
+ }
825
+ double STR_read_double (char **s){
826
+ double item, geta=1;
827
+ int sign=1;
828
+ FILE_err = 0;
829
+ while (1){
830
+ if ( **s == '\n' ){ FILE_err = 5; return (-DOUBLEHUGE); }
831
+ if ( **s == '-' ) sign *= -1;
832
+ else if ( **s == '.' ) geta = 0.1;
833
+ else if ( **s >= '0' && **s <= '9' ) break;
834
+ else { sign = 1; geta = 1; }
835
+ (*s)++;
836
+ }
837
+
838
+ item = geta * (**s-'0');
839
+ if ( geta < 1.0 ) geta *= .1;
840
+ while (1){
841
+ if ( **s == '\n' ){ FILE_err = 1; return (item*sign); }
842
+ if ( **s == '.' ) geta = .1;
843
+ else if ( (**s < '0') || (**s > '9')) return (item*sign);
844
+ else if ( geta < 1.0 ){
845
+ item += geta*(**s - '0');
846
+ geta *= 0.1;
847
+ } else item = item*10 + (**s-'0');
848
+ (*s)++;
849
+ }
850
+ }
851
+
852
+ /* read a WEIGHT from file */
853
+ WEIGHT FILE2_read_WEIGHT (FILE2 *fp){
854
+ #ifdef WEIGHT_DOUBLE
855
+ return (FILE2_read_double(fp));
856
+ #else
857
+ return ((WEIGHT)FILE2_read_int(fp));
858
+ #endif
859
+ }
860
+
861
+ /* read through the file until newline or EOF */
862
+ void FILE2_read_until_newline (FILE2 *fp){
863
+ int ch;
864
+ if (FILE_err & 3) return;
865
+ while (1){
866
+ ch = FILE2_getc (fp);
867
+ if ( ch == '\n' ){ FILE_err = 5; return; }
868
+ if ( ch < 0 ){ FILE_err = 6; return; }
869
+ }
870
+ }
871
+
872
+ void STR_copy_until_newline (char **s, size_t *x, size_t *end, FILE2 *fp){
873
+ int ch;
874
+ char *start = fp->buf;
875
+ while (1){
876
+ if ( FILE_err & 3 ) return;
877
+ ch = FILE2_getc (fp);
878
+ if ( ch == '\n' ) FILE_err = 5;
879
+ else if ( ch < 0 ){ FILE_err = 6; if ( fp->buf == start ) return; } //
880
+ else if ( fp->buf < fp->buf_end ) continue;
881
+
882
+ if ( start == fp->buf_end ) start = fp->buf_org;
883
+ if ( (*x) + fp->buf - start >= *end ){
884
+ *end = MAX((*x) + fp->buf - start+64, (*end)*2);
885
+ realloc2 (*s, *end, EXIT);
886
+ }
887
+ memcpy (&(*s)[*x], start, fp->buf-start);
888
+ (*x) += fp->buf - start;
889
+ if ( FILE_err & 3 ) return;
890
+ start = fp->buf;
891
+ }
892
+ }
893
+
894
+ void FILE2_printf (FILE2 *fp, char *mes, ...){
895
+ va_list argp;
896
+ va_start (argp, mes);
897
+ sprintf (fp->buf, mes, argp);
898
+ va_end (argp);
899
+ }
900
+
901
+ /* print a real number in a good style */
902
+ void fprint_real (FILE *fp, double f){
903
+ char s[200];
904
+ size_t i;
905
+ i = sprintf (s, "%f", f);
906
+ while ( s[i-1] == '0' ) i--;
907
+ if ( s[i-1] == '.' ) i--;
908
+ s[i] = 0;
909
+ fprintf (fp, s);
910
+ }
911
+ void print_real (double f){
912
+ fprint_real (stdout, f);
913
+ }
914
+
915
+ void fprint_WEIGHT (FILE *fp, WEIGHT f){
916
+ #ifdef WEIGHT_DOUBLE
917
+ fprint_real (fp, f);
918
+ #else
919
+ fprintf (fp, "%d", f);
920
+ #endif
921
+ }
922
+ void print_WEIGHT (WEIGHT f){
923
+ fprint_WEIGHT (stdout, f);
924
+ }
925
+
926
+ /* count the clms, rows, items, each row size, each column size */
927
+ /* file types can be, array list and element list*/
928
+ /* support transpose */
929
+ FILE_COUNT FILE2_count (FILE2 *fp, int flag, int skip_rows, int int_rows, int skip_clms, int int_clms, FILE_COUNT_INT row_limit){
930
+ FILE_COUNT_INT k=0, j, x, y, t=0;
931
+ int fr = flag&FILE_COUNT_ROWT, fc = flag&FILE_COUNT_CLMT;
932
+ int fe = flag&LOAD_ELE, ft = flag&LOAD_TPOSE;
933
+ FILE_COUNT C = INIT_FILE_COUNT;
934
+ C.flag = flag;
935
+
936
+ FLOOP (j, 0, skip_rows) FILE2_read_until_newline (fp);
937
+ if ( flag & (FILE_COUNT_NUM+FILE_COUNT_GRAPHNUM) ){
938
+ C.clms = (FILE_COUNT_INT)FILE2_read_int (fp);
939
+ C.rows = (flag & FILE_COUNT_NUM)? (FILE_COUNT_INT)FILE2_read_int (fp): C.clms;
940
+ C.eles = (FILE_COUNT_INT)FILE2_read_int (fp);
941
+ if ( !(flag & (FILE_COUNT_ROWT + FILE_COUNT_CLMT)) ) return (C);
942
+ FILE2_read_until_newline (fp);
943
+ }
944
+ do {
945
+ if ( fe ){
946
+ FLOOP (j, 0, skip_clms){ FILE2_read_double (fp); if ( FILE_err&3 ) goto ROW_END; }
947
+ x = (FILE_COUNT_INT)FILE2_read_int (fp); //printf ("%d\n", FILE_err);
948
+ if ( FILE_err&3 ) goto ROW_END;
949
+ y = (FILE_COUNT_INT)FILE2_read_int (fp); if ( FILE_err&4 ) goto ROW_END;
950
+ FILE2_read_until_newline (fp);
951
+ } else {
952
+ if ( k==0 ) FLOOP (j, 0, skip_clms){ FILE2_read_double (fp); if (FILE_err&3) goto ROW_END; }
953
+ x = t;
954
+ y = (FILE_COUNT_INT)FILE2_read_int (fp); if (FILE_err&4 ) goto ROW_END;
955
+ FLOOP (j, 0, int_clms){ FILE2_read_double (fp); if (FILE_err&3 ) break; }
956
+ k++;
957
+ }
958
+
959
+ if ( ft ) SWAP_FILE_COUNT_INT (x, y);
960
+ if ( y >= C.clms ){
961
+ C.clms = y+1;
962
+ if ( fc ) reallocx (C.clmt, C.clm_end, C.clms, 0, goto END);
963
+ }
964
+ if ( x >= C.rows ){
965
+ C.rows = x+1;
966
+ if ( fr ) reallocx (C.rowt, C.row_end, C.rows, 0, goto END);
967
+ }
968
+ if ( x < C.clm_btm || C.eles == 0 ) C.clm_btm = x;
969
+ if ( y < C.row_btm || C.eles == 0 ) C.row_btm = y;
970
+ if ( fc ) C.clmt[y]++;
971
+ if ( fr ) C.rowt[x]++;
972
+ C.eles++;
973
+
974
+ ROW_END:;
975
+ if ( !fe && (FILE_err&1) ){
976
+ t++; if ( ft ) C.clms = t; else C.rows = t;
977
+ ENMAX (C.clm_max, k);
978
+ ENMAX (C.clm_min, k);
979
+ FLOOP (j, 0, int_rows) FILE2_read_until_newline (fp);
980
+ if ( row_limit>0 && t>=row_limit ) break;
981
+ } else if ( row_limit>0 && C.eles>=row_limit ) break;
982
+
983
+ } while ( (FILE_err&2)==0 );
984
+ END:;
985
+ if ( fr ){
986
+ reallocx (C.rowt, C.row_end, C.rows, 0, goto END);
987
+ ARY_MAX (C.row_max, k, C.rowt, 0, C.rows);
988
+ ARY_MIN (C.row_min, k, C.rowt, 0, C.rows);
989
+ }
990
+ if ( fe && C.clmt ){
991
+ if ( fc ) reallocx (C.clmt, C.clm_end, C.clms, 0, goto END);
992
+ ARY_MAX (C.clm_max, k, C.clmt, 0, C.clms);
993
+ ARY_MIN (C.clm_min, k, C.clmt, 0, C.clms);
994
+ }
995
+ if ( ERROR_MES ) mfree (C.rowt, C.clmt);
996
+ return (C);
997
+ }
998
+
999
+
1000
+ /* SLIST:very simple one-sided list */
1001
+ void SLIST_init (int *l, int num, int siz){
1002
+ malloc2 (l, num+siz, EXIT);
1003
+ ARY_FILL (l, num, num+siz, -1);
1004
+ }
1005
+ void SLIST_end (int *l){ free (l); }
1006
+ #define SLIST_INS(l,m,e) (l[e]=l[m],l[m]=e);
1007
+
1008
+ /* qsort according to "->t" */
1009
+ int qsort_cmp_VECt (const void *x, const void *y){
1010
+ if ( ((VEC *)x)->t < ((VEC *)y)->t ) return (-1);
1011
+ else return ( ((VEC *)x)->t > ((VEC *)y)->t);
1012
+ }
1013
+ int qsort_cmp__VECt (const void *x, const void *y){
1014
+ if ( ((VEC *)x)->t > ((VEC *)y)->t ) return (-1);
1015
+ else return ( ((VEC *)x)->t < ((VEC *)y)->t);
1016
+ }
1017
+ void qsort_VECt (VEC *v, size_t siz, int unit){
1018
+ if ( unit == 1 || unit==-1 ) unit *= sizeof (VEC);
1019
+ if ( unit < 0 ) qsort (v, siz, -unit, qsort_cmp__VECt);
1020
+ else qsort (v, siz, unit, qsort_cmp_VECt);
1021
+ }
1022
+
1023
+ int qqsort_cmp_VECt (const void *x, const void *y){
1024
+ if ( QQSORT_ELEt(VEC,x) < QQSORT_ELEt(VEC,y) ) return (-1);
1025
+ else return ( QQSORT_ELEt(VEC,x) > QQSORT_ELEt(VEC,y) );
1026
+ }
1027
+ int qqsort_cmp__VECt (const void *x, const void *y){
1028
+ if ( QQSORT_ELEt(VEC,x) > QQSORT_ELEt(VEC,y) ) return (-1);
1029
+ else return ( QQSORT_ELEt(VEC,x) < QQSORT_ELEt(VEC,y) );
1030
+ }
1031
+ void qsort_perm__VECt (VEC *v, size_t siz, PERM *perm, int unit){
1032
+ if ( unit == 1 || unit==-1 ) unit *= sizeof(PERM);
1033
+ common_int=MAX(unit,-unit); common_pnt=(char *)v;
1034
+ if (unit<0) qsort (perm, siz, sizeof(PERM), qqsort_cmp__VECt);
1035
+ else qsort (perm, siz, sizeof(PERM), qqsort_cmp_VECt);
1036
+ }
1037
+
1038
+ PERM *qsort_perm_VECt (VEC *v, size_t siz, int unit){
1039
+ PERM *perm;
1040
+ malloc2 (perm, siz, EXIT0);
1041
+ ARY_INIT_PERM (perm, siz);
1042
+ qsort_perm__VECt (v, siz, perm, unit);
1043
+ return(perm);
1044
+ }
1045
+
1046
+ #ifdef STDLIB2_RADIX_SORT // radix sort with 1M byte static memory
1047
+
1048
+ #define RADIX_SORT_BUCKET_SIZ 2048
1049
+ /* sort of integer array with combination of radix sort and quick sort */
1050
+ /* flag&1: sort in decreasing order */
1051
+
1052
+ // sort by lower bits
1053
+ void intarray_sort_iter (unsigned int *a, size_t siz, int unit){
1054
+ static size_t cnt[RADIX_SORT_BUCKET_SIZ], cnt2[RADIX_SORT_BUCKET_SIZ], init_flag = 1;
1055
+ size_t k, x;
1056
+ int i, ii, j, flag=1;
1057
+ static char bbuf[1000], bbuf2[1000];
1058
+ char *aa, *aaa, *aa_end, *buf, *buf2;
1059
+
1060
+ if ( siz<1000 ){ qsort_uint ( a, siz, unit); return; }
1061
+ if ( unit <0 ){ unit = -unit; flag = -1; }
1062
+ if ( unit == 1 ) unit = sizeof (int);
1063
+ buf = bbuf; buf2 = bbuf2;
1064
+ if ( init_flag == 1 ){
1065
+ init_flag = 0;
1066
+ ARY_FILL (cnt, 0, RADIX_SORT_BUCKET_SIZ, 0);
1067
+ }
1068
+ // count elements of each number
1069
+ for ( aa=(char*)a,aa_end=aa+siz*unit ; aa<aa_end ; aa+=unit )
1070
+ cnt[(*((unsigned int *)aa)) & (RADIX_SORT_BUCKET_SIZ-1)]++; // difference!!
1071
+
1072
+ // sum up the numbers in increasing order
1073
+ k=0; FLOOP (i, 0, RADIX_SORT_BUCKET_SIZ){
1074
+ ii = flag==1? i: RADIX_SORT_BUCKET_SIZ-i-1;
1075
+ cnt2[ii] = k;
1076
+ k += cnt[ii];
1077
+ cnt[ii] = k;
1078
+ }
1079
+
1080
+ FLOOP (i, 0, RADIX_SORT_BUCKET_SIZ){
1081
+ ii = flag==1? i: RADIX_SORT_BUCKET_SIZ-i-1;
1082
+ FLOOP (x, cnt2[ii], cnt[ii]){
1083
+ aa = ((char*)a) + x*unit;
1084
+ memcpy ( buf, aa, unit);
1085
+ while (1){
1086
+ j = *((unsigned int *)buf) & (RADIX_SORT_BUCKET_SIZ-1); // difference!!
1087
+ if ( j == ii ) break;
1088
+ aaa = ((char*)a) + cnt2[j]*unit;
1089
+ // printf ("pos[xx]=%d, cnt %d, cnt+1 %d\n", pos[xx], S->let_cnt[dep][cc], S->let_cnt[dep+1][cc]);
1090
+ memcpy ( buf2, aaa, unit);
1091
+ memcpy ( aaa, buf, unit);
1092
+ SWAP_PNT ( buf, buf2);
1093
+ cnt2[j]++;
1094
+ }
1095
+ memcpy ( aa, buf, unit);
1096
+ }
1097
+ cnt[i]=0; // difference!!
1098
+ }
1099
+ }
1100
+ // sort by middle bits
1101
+ void intarray_sort_iter_ ( unsigned int *a, size_t siz, int unit){
1102
+ static size_t cnt[RADIX_SORT_BUCKET_SIZ], cnt2[RADIX_SORT_BUCKET_SIZ], init_flag = 1;
1103
+ int i, ii, j, flag=1;
1104
+ size_t k, x;
1105
+ static char bbuf[1000], bbuf2[1000];
1106
+ char *aa, *aaa, *aa_end, *buf, *buf2;
1107
+
1108
+ if ( siz<1000 ){ qsort_uint ( a, siz, unit); return; }
1109
+ buf = bbuf; buf2 = bbuf2;
1110
+ if ( unit <0 ){ unit = -unit; flag = -1; }
1111
+ if ( unit == 1 ) unit = sizeof (int);
1112
+ if ( init_flag == 1 ){
1113
+ init_flag = 0;
1114
+ ARY_FILL ( cnt, 0, RADIX_SORT_BUCKET_SIZ, 0);
1115
+ }
1116
+ // count elements of each number
1117
+ for ( aa=(char*)a,aa_end=aa+siz*unit ; aa<aa_end ; aa+=unit )
1118
+ cnt[((*((unsigned int *)aa))/RADIX_SORT_BUCKET_SIZ) & (RADIX_SORT_BUCKET_SIZ-1)]++; // difference!!
1119
+
1120
+ // sum up the numbers in increasing order
1121
+ k=0; FLOOP (i, 0, RADIX_SORT_BUCKET_SIZ){
1122
+ ii = flag==1? i: RADIX_SORT_BUCKET_SIZ-i-1;
1123
+ cnt2[ii] = k;
1124
+ k += cnt[ii];
1125
+ cnt[ii] = k;
1126
+ }
1127
+
1128
+ FLOOP (i, 0, RADIX_SORT_BUCKET_SIZ){
1129
+ ii = flag==1? i: RADIX_SORT_BUCKET_SIZ-i-1;
1130
+ FLOOP(x, cnt2[ii], cnt[ii]){
1131
+ aa = ((char*)a) + x*unit;
1132
+ memcpy ( buf, aa, unit);
1133
+ while (1){
1134
+ j = (*((unsigned int *)buf)/RADIX_SORT_BUCKET_SIZ) & (RADIX_SORT_BUCKET_SIZ-1); // difference!!
1135
+ if ( j == ii ) break;
1136
+ aaa = ((char*)a) + cnt2[j]*unit;
1137
+ // printf ("pos[xx]=%d, cnt %d, cnt+1 %d\n", pos[xx], S->let_cnt[dep][cc], S->let_cnt[dep+1][cc]);
1138
+ memcpy (buf2, aaa, unit);
1139
+ memcpy (aaa, buf, unit);
1140
+ SWAP_PNT (buf, buf2);
1141
+ cnt2[j]++;
1142
+ }
1143
+ memcpy (aa, buf, unit);
1144
+ }
1145
+ }
1146
+ k=0; FLOOP (i, 0, RADIX_SORT_BUCKET_SIZ){
1147
+ ii = flag==1? i: RADIX_SORT_BUCKET_SIZ-i-1;
1148
+ intarray_sort_iter ( (unsigned int*)(((char*)a)+unit*k), cnt[ii]-k, unit*flag);
1149
+ k = cnt[ii];
1150
+ cnt[i]=0;
1151
+ }
1152
+ }
1153
+
1154
+ // sort by upper bits
1155
+ void intarray_sort ( unsigned int *a, size_t siz, int unit){
1156
+ static size_t cnt[RADIX_SORT_BUCKET_SIZ], cnt2[RADIX_SORT_BUCKET_SIZ], init_flag = 1;
1157
+ int i, ii, j, flag=1;
1158
+ size_t k, x;
1159
+ static char bbuf[1000], bbuf2[1000];
1160
+ char *aa, *aaa, *aa_end, *buf, *buf2;
1161
+
1162
+ if ( siz<1000 ){ qsort_uint ( a, siz, unit); return; }
1163
+ if ( unit <0 ){ unit = -unit; flag = -1; }
1164
+ if ( unit == 1 ) unit = sizeof (int);
1165
+ buf = bbuf; buf2 = bbuf2;
1166
+ if ( init_flag == 1){
1167
+ init_flag = 0;
1168
+ ARY_FILL (cnt, 0, RADIX_SORT_BUCKET_SIZ, 0);
1169
+ }
1170
+ // count elements of each number
1171
+ for ( aa=(char*)a,aa_end=aa+siz*unit ; aa<aa_end ; aa+=unit )
1172
+ cnt[(*((unsigned int *)aa)) / RADIX_SORT_BUCKET_SIZ / RADIX_SORT_BUCKET_SIZ]++; // difference!!
1173
+
1174
+ // sum up the numbers in increasing order
1175
+ k=0; FLOOP (i, 0, RADIX_SORT_BUCKET_SIZ){
1176
+ ii = flag==1? i: RADIX_SORT_BUCKET_SIZ-i-1;
1177
+ cnt2[ii] = k;
1178
+ k += cnt[ii];
1179
+ cnt[ii] = k;
1180
+ }
1181
+
1182
+ FLOOP (i, 0, RADIX_SORT_BUCKET_SIZ){
1183
+ ii = flag==1? i: RADIX_SORT_BUCKET_SIZ-i-1;
1184
+ FLOOP (x, cnt2[ii], cnt[ii]){
1185
+ aa = ((char*)a) + x*unit;
1186
+ memcpy ( buf, aa, unit);
1187
+ while (1){
1188
+ j = *((unsigned int *)buf) / RADIX_SORT_BUCKET_SIZ / RADIX_SORT_BUCKET_SIZ; // difference!!
1189
+ if ( j == ii ) break;
1190
+ aaa = ((char*)a) + cnt2[j]*unit;
1191
+ // printf ("pos[xx]=%d, cnt %d, cnt+1 %d\n", pos[xx], S->let_cnt[dep][cc], S->let_cnt[dep+1][cc]);
1192
+ memcpy (buf2, aaa, unit);
1193
+ memcpy (aaa, buf, unit);
1194
+ SWAP_PNT (buf, buf2);
1195
+ cnt2[j]++;
1196
+ }
1197
+ memcpy ( aa, buf, unit);
1198
+ }
1199
+ }
1200
+ k=0; FLOOP (i, 0, RADIX_SORT_BUCKET_SIZ){
1201
+ ii = flag==1? i: RADIX_SORT_BUCKET_SIZ-i-1;
1202
+ intarray_sort_iter_ ( (unsigned int*)(((char*)a)+unit*k), cnt[ii]-k, unit*flag);
1203
+ k = cnt[ii];
1204
+ cnt[i]=0;
1205
+ }
1206
+
1207
+ /*
1208
+ for ( i=0 ; i<siz ; i++){
1209
+ k = *((int *)(((char*)a) + i*unit));
1210
+ printf ("%d %d,%d\n", k, k/65536, k&65535);
1211
+ }
1212
+ */
1213
+ }
1214
+
1215
+ #endif
1216
+
1217
+ /* radix sort for array of structures, by their integer members
1218
+ ranging from mm to m */
1219
+ /* sort array "perm" according to (int/void*) array "a".
1220
+ if perm==NULL, allocate memory and for perm */
1221
+ /* return the permutation array of the result of the sorting
1222
+ in the decreasing order if unit<0 (unimplemented!!!) */
1223
+ int *radix_sort ( void *a, size_t siz, int unit, int mm, int m, int *perm){
1224
+ int *ll, *l, k, i, t;
1225
+ malloc2 (l, m-mm, EXIT0);
1226
+ ARY_FILL (l, 0, m-mm, -1);
1227
+ malloc2 (ll, siz, {free2(l);EXIT0;});
1228
+ if ( unit <0 ) unit = -unit;
1229
+ FLOOP (i, 0, (int)siz){
1230
+ k = (*((int *)(((char *)a) + unit*i ))) - mm;
1231
+ ll[i] = l[k];
1232
+ l[k] = i;
1233
+ }
1234
+ if ( perm ){
1235
+ i=0; FLOOP (k, 0, m-mm){
1236
+ while ( l[k] >= 0 ){
1237
+ t = l[k];
1238
+ l[k] = ll[t];
1239
+ ll[t] = perm[i];
1240
+ i++;
1241
+ }
1242
+ }
1243
+ memcpy (perm, ll, sizeof(int)*siz);
1244
+ free ( ll);
1245
+ free ( l);
1246
+ return ( perm);
1247
+ } else {
1248
+ i=0; FLOOP (k, 0, m-mm){
1249
+ while ( l[k] >= 0 ){
1250
+ t = l[k];
1251
+ l[k] = ll[t];
1252
+ ll[t] = i;
1253
+ i++;
1254
+ }
1255
+ }
1256
+ free (l);
1257
+ return (ll);
1258
+ }
1259
+ }
1260
+
1261
+ /* permutate structure array *tt of unit size unit of size num, according to perm array *pp */
1262
+ /* num has to be <INTHUGE/2 */
1263
+ /* unit<0 means decreasing order (un-implemented!!!) */
1264
+ void structure_permute (void *tt, int unit, int num, void *pp, int weight_siz){
1265
+ int i, ii, *ip;
1266
+ char *tmp, *t=(char *)tt, *p=(char *)pp;
1267
+ if ( unit <0 ) unit = -unit;
1268
+ malloc2 (tmp, unit, EXIT);
1269
+ FLOOP (i, 0, num){
1270
+ ip = (int *)(p + (sizeof(int)+weight_siz)*i + weight_siz);
1271
+ if ( *ip< num && *ip != i ){
1272
+ ii = i;
1273
+ memcpy ( tmp, t + unit*i, unit);
1274
+ while (1) {
1275
+ if ( *ip == i ){
1276
+ memcpy ( t+unit*ii, tmp, unit);
1277
+ *ip += num;
1278
+ break;
1279
+ }
1280
+ memcpy ( t+unit*ii, t+unit*(*ip), unit);
1281
+ ii = *ip;
1282
+ *ip += num;
1283
+ ip = (int *)(p + (sizeof(int)+weight_siz)*ii + weight_siz);
1284
+ }
1285
+ } else *ip += num;
1286
+ }
1287
+ FLOOP (i, 0, num) *(int *)(p + (sizeof(int)+weight_siz)*i + weight_siz ) -= num;
1288
+ free (tmp);
1289
+ }
1290
+
1291
+
1292
+
1293
+ #endif
1294
+
1295
+
1296
+ /******************************************/
1297
+ /* ==== terminology for comments ====
1298
+ range check: to check the input parameter is valid, or in the valid range.
1299
+ If a function does not have this, its comment has "no range check"
1300
+ */
1301
+
1302
+ /* ==== rules for the name of functions and routines ====
1303
+ init: initialization for an object, structure, etc. memory is allocated
1304
+ if needed.
1305
+ end: compared to init, termination of structures, etc.
1306
+ free allocated memory if it exists, but not free itself.
1307
+ different from ordinary new, create, del, free.
1308
+
1309
+ cpy: copy an object without allocating memory
1310
+ dup: make a duplication of an object with allocating new memory
1311
+
1312
+ new: new. allocate memory for new object. also used for re-allocation from
1313
+ the list of deleted objects
1314
+ del: delete. free memory, or insert it to the list of deleted objects
1315
+
1316
+ ins : insert. insert an element (active, not deleted) to an object, possible
1317
+ at the given position.
1318
+ out : extract. extract an element, possibly specified, from an object.
1319
+ it will be not deleted.
1320
+ rm : extract, and delete
1321
+ rmall: delete all (specified) elements of an object
1322
+ mk : make. new+ins�B
1323
+ mv : move. move the elements from an object to another,
1324
+ or change the position.
1325
+
1326
+ update : update an object, possibly of specified position, to the exact,
1327
+ or to the current one.
1328
+ chg : change the status of an object to the specified one.
1329
+
1330
+ prv: point the previous element
1331
+ nxt: point the next element
1332
+ end: the maximum size (allocated size) of an array, etc.
1333
+ num: the (current) number of elements in an array, etc.
1334
+ kth: for the use of "first k object"
1335
+ tkth: for the use of "first k object from the end". to kth.
1336
+ rnd: random
1337
+ print: print structure and etc.
1338
+ find: search or find an specified element from the set of structures, etc.
1339
+ */
1340
+
1341
+ /* ==== rules for the name of variables ====
1342
+ - use i or j for the counter in loops
1343
+ - use e for elements
1344
+ - use k for the specified rank
1345
+ - use f or flag for flagment
1346
+ - use m for maximum value or minimum value
1347
+ - use c for counters
1348
+ */
1349
+
1350
+