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