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,993 @@
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, do not forget to
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
+ For the commercial use, please make a contact to Takeaki Uno. */
12
+
13
+ #ifndef _stdlib2_c_
14
+ #define _stdlib2_c_
15
+
16
+ #include"stdlib2.h"
17
+ #ifdef MTWISTER
18
+ #include"dSFMT.c"
19
+ #endif
20
+
21
+ size_t common_size_t;
22
+ INT common_INT, common_INT2;
23
+ char *common_charp, *common_pnt;
24
+ FILE *common_FILE;
25
+ FILE2 common_FILE2;
26
+ PERM common_PERM;
27
+ char common_comm[1024], common_comm2[1024], *common_argv[100]; // max. command length = 2048, max. #params = 100
28
+
29
+ char *ERROR_MES = NULL;
30
+ int print_time_flag=0;
31
+ PARAMS internal_params;
32
+ #ifdef MULTI_CORE
33
+ int SPIN_LOCK_dummy;
34
+ #endif
35
+ FILE2 INIT_FILE2 = {TYPE_FILE2,NULL,NULL,NULL,NULL,0};
36
+ VEC INIT_VEC = {TYPE_VEC,NULL,0,0};
37
+ 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};
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
+ /* remove many files */
124
+ void mremove_ (char *x, ...){
125
+ va_list argp;
126
+ char *a;
127
+ va_start (argp, x);
128
+ while((a = va_arg(argp, char *))){
129
+ sprintf (common_comm, "%s%s", x, a);
130
+ remove (common_comm);
131
+ }
132
+ va_end (argp);
133
+ }
134
+
135
+ /* compute the minimum prime no less than n */
136
+ #define MINPRIME_END 6000
137
+ LONG min_prime (LONG n){
138
+ LONG i, j=30, k;
139
+ char f[MINPRIME_END];
140
+ while(1) {
141
+ FLOOP (i, 0, j) f[i]=0;
142
+ for ( i=3 ; i*i < n+j ; i+=2 )
143
+ for ( k=((n+i-1)/i)*i ; k<i+j ; k+=i ) f[(k-n)/2] = 1;
144
+ FLOOP (i, 0, j) if ( f[i] == 0 ) return (n+ i*2+1);
145
+ j *= 2;
146
+ }
147
+ }
148
+
149
+ /* decompose the string by separator, and set v[i] to each resulted string.
150
+ consecutive separators are regarded as one separator. */
151
+ /* string s has to have end mark 0 at the end */
152
+ /* original string s will be written, so that each separator will be end mark 0 */
153
+ /* at most [num] strings will be generated */
154
+ int string_decompose ( char **v, char *s, char sep, int max){
155
+ int i=0, dq;
156
+ char *ss = s;
157
+ do {
158
+ dq = 0;
159
+ while (*ss == sep) ss++;
160
+ if ( *ss == 0 ) break;
161
+ if ( *ss == '\"' ){ dq = 1; v[i++] = ++ss; }
162
+ else v[i++] = ss;
163
+ while (*ss != sep || dq){
164
+ if ( dq==0 && *ss == 0 ) break;
165
+ if ( dq && *ss == '\"' ) break;
166
+ ss++;
167
+ }
168
+ if ( *ss == 0 ) break;
169
+ *(ss++) = 0;
170
+ } while ( i<max);
171
+ return (i);
172
+ }
173
+
174
+ unsigned long xor128(){
175
+ static unsigned long x=123456789,y=362436069,z=521288629,w=88675123;
176
+ unsigned long t;
177
+ t=(x^(x<<11));x=y;y=z;z=w; return( w=(w^(w>>19))^(t^(t>>8)) );
178
+ }
179
+
180
+ /***********************************************************************/
181
+ /***********************************************************************/
182
+ #ifdef USE_MATH
183
+ #define NORMAL_RAND_BASE 2147483648LL
184
+
185
+ /* make two random numbers under normal distribution N(0,1) */
186
+ void rand_mk_2normal (double *a, double *b){
187
+ double r1, r2;
188
+ do {
189
+ r1 = RAND1;
190
+ } while (r1==0);
191
+ r2 = RAND1;
192
+ r1 = sqrt(-log(r1)*2);
193
+ r2 *= 2*PI;
194
+ *a = r1*sin(r2);
195
+ *b = r1*cos(r2);
196
+ }
197
+
198
+ /* make a random point on a supersphere of d-dim., and set to double array already allocated */
199
+ void rand_d_gaussian (double *p, int d){
200
+ int i;
201
+ double a, b;
202
+ for (i=0 ; i<d ; i+=2){
203
+ rand_mk_2normal ( &a, &b);
204
+ p[i] = a;
205
+ if ( i+1 < d ) p[i+1] = b;
206
+ }
207
+ }
208
+ void rand_sphere (double *p, int d){
209
+ rand_d_gaussian (p, d);
210
+ ARY_NORMALIZE (p, d);
211
+ }
212
+
213
+ /* compute the integral of cosin x with exponent n */
214
+ double int_cos_n (double x, int n){
215
+ if ( n == 1 ) return (sin(x));
216
+ if ( n == 2 ) return (x/2+sin(2*x)/4);
217
+ return ( int_cos_n (x, n-2) *(n-1)/n - pow(cos(x), n-1)*sin(x) /n );
218
+ }
219
+ double int_sin_n (double x, int n){
220
+ if ( n == 1 ) return (-cos(x));
221
+ if ( n == 2 ) return (x/2-sin(2*x)/4);
222
+ return ( int_sin_n (x, n-2) *(n-1)/n - pow(sin(x), n-1)*cos(x) /n );
223
+ }
224
+ #endif
225
+
226
+ /******************** file I/O routines ********************************/
227
+
228
+ int FILE_err; /* signals 0: for normal termination
229
+ 1: read a number, then encountered a newline,
230
+ 2: read a number, then encountered the end-of-file
231
+ 5: read no number, and encountered a newline
232
+ 6: read no number, and encountered the end-of-file */
233
+
234
+
235
+ void FILE2_flush (FILE2 *fp){
236
+ if ( fp->buf > fp->buf_org+FILE2_BUFSIZ/2 ){
237
+ fwrite ( fp->buf_org, fp->buf-fp->buf_org, 1, fp->fp);
238
+ *fp->buf_org = *fp->buf;
239
+ fp->buf = fp->buf_org;
240
+ }
241
+ }
242
+ void FILE2_flush_last (FILE2 *fp){
243
+ if ( fp->bit > 0 ) fp->buf++;
244
+ if ( fp->buf > fp->buf_org ){
245
+ fwrite ( fp->buf_org, fp->buf-fp->buf_org, 1, fp->fp);
246
+ fp->buf = fp->buf_org;
247
+ }
248
+ }
249
+ void FILE2_close (FILE2 *fp){
250
+ fclose2 (fp->fp);
251
+ free2 (fp->buf_org);
252
+ fp->buf = fp->buf_end = 0;
253
+ }
254
+ void FILE2_closew (FILE2 *fp){
255
+ FILE2_flush_last (fp);
256
+ fclose2 (fp->fp);
257
+ free2 (fp->buf_org);
258
+ fp->buf = fp->buf_end = 0;
259
+ }
260
+ void FILE2_reset (FILE2 *fp){
261
+ fp->buf = fp->buf_org;
262
+ fp->buf_end = fp->buf_org-1;
263
+ fseek (fp->fp, 0, SEEK_SET);
264
+ }
265
+ /* fast file routine, getc, putc, puts, */
266
+ int FILE2_getc (FILE2 *fp){
267
+ int c;
268
+ if ( fp->buf >= fp->buf_end ){
269
+ if ( (fp->buf_end < fp->buf_org+FILE2_BUFSIZ) && (fp->buf_end>=fp->buf_org) ){ FILE_err=2; return (-1); }
270
+ fp->buf = fp->buf_org;
271
+ fp->buf_end = fp->buf_org + fread (fp->buf, 1, FILE2_BUFSIZ, fp->fp);
272
+ return (FILE2_getc (fp));
273
+ }
274
+ c = (unsigned char)(*(fp->buf));
275
+ fp->buf++;
276
+ return (c);
277
+ }
278
+ void FILE2_puts (FILE2 *fp, char *s){
279
+ while ( *s != 0 ){
280
+ *(fp->buf) = *s;
281
+ s++;
282
+ fp->buf++;
283
+ }
284
+ }
285
+ void FILE2_putc (FILE2 *fp, char c){
286
+ *(fp->buf) = c;
287
+ fp->buf++;
288
+ }
289
+ int FILE2_getbit (FILE2 *fp){
290
+ int a;
291
+ unsigned char *p;
292
+ if ( fp->buf >= fp->buf_end ){
293
+ if ( (fp->buf_end < fp->buf_org+FILE2_BUFSIZ) && (fp->buf_end>=fp->buf_org) ){ FILE_err=2; return (-1); }
294
+ fp->buf = fp->buf_org;
295
+ fp->buf_end = fp->buf_org + fread (fp->buf, 1, FILE2_BUFSIZ, fp->fp);
296
+ }
297
+ p = (unsigned char *)fp->buf; a = (*p) %2; (*p) /=2;
298
+ if ( (++fp->bit) == 8 ){ fp->bit = 0; fp->buf++; }
299
+ return (a);
300
+ }
301
+ void FILE2_putbit (FILE2 *fp, int a){ // a is non-zero => 1
302
+ if ( a ){
303
+ a = 1 << fp->bit;
304
+ *fp->buf |= a;
305
+ }
306
+ if ( (++fp->bit) == 8 ){ fp->bit = 0; fp->buf++; *fp->buf = 0; }
307
+ }
308
+
309
+ /* fast file routine, print number, c is the char to be printed preceding to the number
310
+ if c==0, nothing will be printed preceding the number
311
+ if len<0 then the #digits following '.' does not change (filed by '0') */
312
+ void FILE2_print_int (FILE2 *fp, LONG n, char c){
313
+ LONG nn = n;
314
+ char *s;
315
+ if ( c ) FILE2_putc ( fp, c);
316
+ if ( n == 0 ){ *(fp->buf) = '0'; fp->buf++; return; }
317
+ if ( n < 0 ){ *(fp->buf) = '-'; fp->buf++; n = -n; }
318
+ while ( nn>0 ){ nn /= 10; fp->buf++; }
319
+ s = fp->buf-1;
320
+ *(fp->buf) = 0;
321
+ while ( n>0 ){ *s = '0'+(char)(n%10); s--; n/=10; }
322
+ }
323
+ /******/
324
+ void FILE2_print_real (FILE2 *fp, double n, int len, char c){
325
+ int i=0, flag=1;
326
+ double j=1;
327
+ char *back;
328
+
329
+ if ( c ) FILE2_putc (fp, c);
330
+ if ( n<0 ){ FILE2_putc (fp, '-'); n *= -1; }
331
+ while ( n >= j ) j*=10;
332
+ if ( j==1 ){ *(fp->buf) = '0'; fp->buf++; }
333
+ else while ( j>1 ){
334
+ j /= 10;
335
+ i = (int)(n/j);
336
+ *(fp->buf) = '0'+i;
337
+ n -= j*i;
338
+ fp->buf++;
339
+ }
340
+ *(fp->buf) = '.'; back = fp->buf;
341
+ fp->buf++;
342
+ if ( len<0 ){ len = -len; flag = 0; }
343
+ for ( ; len>0 ; len--){
344
+ n *= 10.0;
345
+ i = (int)n;
346
+ *(fp->buf) = '0'+i;
347
+ n -= i;
348
+ fp->buf++;
349
+ if ( i>0 ) back = fp->buf;
350
+ }
351
+ if ( flag ) fp->buf = back;
352
+ }
353
+ /******/
354
+ void FILE2_print_WEIGHT (FILE2 *fp, WEIGHT w, int len, char c){
355
+ #ifdef WEIGHT_DOUBLE
356
+ FILE2_print_real(fp, w, len, c);
357
+ #else
358
+ FILE2_print_int(fp, w, c);
359
+ #endif
360
+ }
361
+
362
+ // print integer to file in a bit string manner
363
+ // first bit 0 -> 0to127
364
+ // 10 -> 128 to 16384+127
365
+ // 110 -> 16384+128 to 32*65536+16384+127
366
+ // 1110 -> ... to 16*
367
+ void FILE2_print_mpint (FILE2 *fp, LONG a){
368
+ LONG b=128, x=0;
369
+ int i=0;
370
+ while ( a-x >= b ){ x += b; b *= 128; i++; }
371
+ a -= x;
372
+ FLOOP (x, 0, i) FILE2_putbit (fp, 1); // length of the integer
373
+ FILE2_putbit (fp, 0); // end mark
374
+ for (b/=2 ; b>0 ; b/=2) FILE2_putbit (fp, (a&b)>0);
375
+ }
376
+
377
+ LONG FILE2_read_mpint (FILE2 *fp){
378
+ LONG a, b,x ;
379
+ int j;
380
+ for (x=0,b=128; (j=FILE2_getbit (fp)) ; b*=128){
381
+ if ( j<0 ){ FILE_err = 2; return (0); }
382
+ x += b;
383
+ }
384
+ for (a=0 ; b>1 ; b/=2) a = a*2 + FILE2_getbit(fp);
385
+ return (a+x);
386
+ }
387
+ /* print lowest k bits to file */
388
+ void FILE2_print_intbit (FILE2 *fp, LONG a, int k){
389
+ LONG b= 1 << (k-1);
390
+ while (b>0){ FILE2_putbit (fp, (a&b)>0); b /= 2; }
391
+ }
392
+ /* read k bits from file as integer */
393
+ LONG FILE2_read_intbit (FILE2 *fp, int k){
394
+ LONG a=0;
395
+ while (k>0){ a = a*2 + FILE2_getbit (fp); }
396
+ return (a);
397
+ }
398
+
399
+ /* Read an integer/a double from the file and return it,
400
+ with read through the non-numeric letters.
401
+ If it reaches to the end-of-file, then set FILE_err=2, if it reads a
402
+ newline, then set FILE_err=1.
403
+ If read either the end-of-file or newline before reading an integer,
404
+ return 5, and 6 */
405
+ FILE_LONG FILE2_read_int (FILE2 *fp){
406
+ FILE_LONG item;
407
+ int flag =1;
408
+ int ch;
409
+ FILE_err = 0;
410
+ do {
411
+ ch = FILE2_getc(fp);
412
+ if ( ch == '\n' ){ FILE_err = 5; return (-INTHUGE); }
413
+ if ( ch < 0 ){ FILE_err = 6; return (-INTHUGE); }
414
+ if ( ch=='-' ) flag = -1;
415
+ } while ( ch<'0' || ch>'9' );
416
+ for ( item=(int)(ch-'0') ; 1 ; item=item*10 +(int)(ch-'0') ){
417
+ ch = FILE2_getc(fp);
418
+ if ( ch == '\n' ){ FILE_err = 1; return (flag*item); }
419
+ if ( ch < 0 ){ FILE_err = 2; return (flag*item); }
420
+ if ( (ch < '0') || (ch > '9')) return (flag*item);
421
+ }
422
+ }
423
+ double FILE2_read_double (FILE2 *fp){
424
+ double item, geta=1;
425
+ int sign=1, ch;
426
+ FILE_err = 0;
427
+ while (1){
428
+ ch = FILE2_getc(fp);
429
+ if ( ch < 0 ){ FILE_err = 6; return (-DOUBLEHUGE); }
430
+ if ( ch == '\n' ){ FILE_err = 5; return (-DOUBLEHUGE); }
431
+ if ( ch=='-' ) sign *= -1;
432
+ else if ( ch=='.' ) geta = 0.1;
433
+ else if ( ch>='0' && ch<='9' ) break;
434
+ else { sign = 1; geta = 1; }
435
+ }
436
+
437
+ item = geta * (ch-'0');
438
+ if ( geta < 1.0 ) geta *= .1;
439
+ while (1){
440
+ ch = FILE2_getc(fp);
441
+ if ( ch == '\n' ){ FILE_err = 1; return (item*sign); }
442
+ if ( ch<0 ){ FILE_err = 2; return (item*sign); }
443
+ if ( ch == '.' ) geta = .1;
444
+ else if ( (ch < '0') || (ch > '9')) return (item*sign);
445
+ else if ( geta < 1.0 ){
446
+ item += geta*(ch-'0');
447
+ geta *= 0.1;
448
+ } else item = item*10 + (ch-'0');
449
+ }
450
+ }
451
+
452
+ /* string has to have non-numeric alphabet until its end */
453
+ FILE_LONG STR_read_int (char **s){
454
+ FILE_LONG item;
455
+ int flag =1;
456
+ FILE_err = 0;
457
+ while (1) {
458
+ if ( **s == '\n' ){ FILE_err = 5; return (-INTHUGE); }
459
+ if ( **s=='-' ) flag = -1;
460
+ if ( **s >= '0' && **s <= '9' ) break;
461
+ (*s)++;
462
+ }
463
+ for ( item=(int)((**s)-'0') ; 1 ; item=item*10 +(int)((**s)-'0') ){
464
+ (*s)++;
465
+ if ( (**s) == '\n' ){ FILE_err = 1; return (flag*item); }
466
+ if ( ((**s) < '0') || ((**s) > '9')) return (flag*item);
467
+ }
468
+ }
469
+ double STR_read_double (char **s){
470
+ double item, geta=1;
471
+ int sign=1;
472
+ FILE_err = 0;
473
+ while (1){
474
+ if ( **s == '\n' ){ FILE_err = 5; return (-DOUBLEHUGE); }
475
+ if ( **s == '-' ) sign *= -1;
476
+ else if ( **s == '.' ) geta = 0.1;
477
+ else if ( **s >= '0' && **s <= '9' ) break;
478
+ else { sign = 1; geta = 1; }
479
+ (*s)++;
480
+ }
481
+
482
+ item = geta * (**s-'0');
483
+ if ( geta < 1.0 ) geta *= .1;
484
+ while (1){
485
+ if ( **s == '\n' ){ FILE_err = 1; return (item*sign); }
486
+ if ( **s == '.' ) geta = .1;
487
+ else if ( (**s < '0') || (**s > '9')) return (item*sign);
488
+ else if ( geta < 1.0 ){
489
+ item += geta*(**s - '0');
490
+ geta *= 0.1;
491
+ } else item = item*10 + (**s-'0');
492
+ (*s)++;
493
+ }
494
+ }
495
+
496
+ /* read a WEIGHT from file */
497
+ WEIGHT FILE2_read_WEIGHT (FILE2 *fp){
498
+ #ifdef WEIGHT_DOUBLE
499
+ return (FILE2_read_double(fp));
500
+ #else
501
+ return ((WEIGHT)FILE2_read_int(fp));
502
+ #endif
503
+ }
504
+
505
+ /* read through the file until newline or EOF */
506
+ void FILE2_read_until_newline (FILE2 *fp){
507
+ int ch;
508
+ if (FILE_err & 3) return;
509
+ while (1){
510
+ ch = FILE2_getc (fp);
511
+ if ( ch == '\n' ){ FILE_err = 5; return; }
512
+ if ( ch < 0 ){ FILE_err = 6; return; }
513
+ }
514
+ }
515
+
516
+ void STR_copy_until_newline (char **s, size_t *x, size_t *end, FILE2 *fp){
517
+ int ch;
518
+ char *start = fp->buf;
519
+ while (1){
520
+ if ( FILE_err & 3 ) return;
521
+ ch = FILE2_getc (fp);
522
+ if ( ch == '\n' ) FILE_err = 5;
523
+ else if ( ch < 0 ){ FILE_err = 6; if ( fp->buf == start ) return; } //
524
+ else if ( fp->buf < fp->buf_end ) continue;
525
+
526
+ if ( start == fp->buf_end ) start = fp->buf_org;
527
+ if ( (*x) + fp->buf - start >= *end ){
528
+ *end = MAX((*x) + fp->buf - start+64, (*end)*2);
529
+ realloc2 (*s, *end, EXIT);
530
+ }
531
+ memcpy (&(*s)[*x], start, fp->buf-start);
532
+ (*x) += fp->buf - start;
533
+ if ( FILE_err & 3 ) return;
534
+ start = fp->buf;
535
+ }
536
+ }
537
+
538
+ void FILE2_printf (FILE2 *fp, char *mes, ...){
539
+ va_list argp;
540
+ va_start (argp, mes);
541
+ sprintf (fp->buf, mes, argp);
542
+ va_end (argp);
543
+ }
544
+
545
+ /* print a real number in a good style */
546
+ void fprint_real (FILE *fp, double f){
547
+ char s[200];
548
+ size_t i;
549
+ i = sprintf (s, "%f", f);
550
+ while ( s[i-1] == '0' ) i--;
551
+ if ( s[i-1] == '.' ) i--;
552
+ s[i] = 0;
553
+ fprintf (fp, s);
554
+ }
555
+ void print_real (double f){
556
+ fprint_real (stdout, f);
557
+ }
558
+
559
+ void fprint_WEIGHT (FILE *fp, WEIGHT f){
560
+ #ifdef WEIGHT_DOUBLE
561
+ fprint_real (fp, f);
562
+ #else
563
+ fprintf (fp, "%d", f);
564
+ #endif
565
+ }
566
+ void print_WEIGHT (WEIGHT f){
567
+ fprint_WEIGHT (stdout, f);
568
+ }
569
+
570
+ /* count the clms, rows, items, each row size, each column size */
571
+ /* file types can be, array list and element list*/
572
+ /* support transpose */
573
+ 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){
574
+ FILE_COUNT_INT k=0, j, x, y, t=0;
575
+ int fr = flag&FILE_COUNT_ROWT, fc = flag&FILE_COUNT_CLMT;
576
+ int fe = flag&LOAD_ELE, ft = flag&LOAD_TPOSE;
577
+ FILE_COUNT C = INIT_FILE_COUNT;
578
+ C.flag = flag;
579
+
580
+ FLOOP (j, 0, skip_rows) FILE2_read_until_newline (fp);
581
+ if ( flag & (FILE_COUNT_NUM+FILE_COUNT_GRAPHNUM) ){
582
+ C.clms = (FILE_COUNT_INT)FILE2_read_int (fp);
583
+ C.rows = (flag & FILE_COUNT_NUM)? (FILE_COUNT_INT)FILE2_read_int (fp): C.clms;
584
+ C.eles = (FILE_COUNT_INT)FILE2_read_int (fp);
585
+ if ( !(flag & (FILE_COUNT_ROWT + FILE_COUNT_CLMT)) ) return (C);
586
+ FILE2_read_until_newline (fp);
587
+ }
588
+ do {
589
+ if ( fe ){
590
+ FLOOP (j, 0, skip_clms){ FILE2_read_double (fp); if ( FILE_err&3 ) goto ROW_END; }
591
+ x = (FILE_COUNT_INT)FILE2_read_int (fp); if ( FILE_err&3 ) goto ROW_END;
592
+ y = (FILE_COUNT_INT)FILE2_read_int (fp); if ( FILE_err&4 ) goto ROW_END;
593
+ FILE2_read_until_newline (fp);
594
+ } else {
595
+ if ( k==0 ) FLOOP (j, 0, skip_clms){ FILE2_read_double (fp); if (FILE_err&3) goto ROW_END; }
596
+ x = t;
597
+ y = (FILE_COUNT_INT)FILE2_read_int (fp); if (FILE_err&4 ) goto ROW_END;
598
+ FLOOP (j, 0, int_clms){ FILE2_read_double (fp); if (FILE_err&3 ) break; }
599
+ k++;
600
+ }
601
+
602
+ if ( ft ) SWAP_FILE_COUNT_INT (x, y);
603
+ if ( y >= C.clms ){
604
+ C.clms = y+1;
605
+ if ( fc ) reallocx (C.clmt, C.clm_end, C.clms, 0, goto END);
606
+ }
607
+ if ( x >= C.rows ){
608
+ C.rows = x+1;
609
+ if ( fr ) reallocx (C.rowt, C.row_end, C.rows, 0, goto END);
610
+ }
611
+ if ( x < C.clm_btm || C.eles == 0 ) C.clm_btm = x;
612
+ if ( y < C.row_btm || C.eles == 0 ) C.row_btm = y;
613
+ if ( fc ) C.clmt[y]++;
614
+ if ( fr ) C.rowt[x]++;
615
+ C.eles++;
616
+
617
+ ROW_END:;
618
+ if ( !fe && (FILE_err&1) ){
619
+ t++; C.rows = t;
620
+ ENMAX (C.clm_max, k);
621
+ ENMAX (C.clm_min, k);
622
+ FLOOP (j, 0, int_rows) FILE2_read_until_newline (fp);
623
+ if ( row_limit>0 && t>=row_limit ) break;
624
+ } else if ( row_limit>0 && C.eles>=row_limit ) break;
625
+
626
+ } while ( (FILE_err&2)==0 );
627
+ END:;
628
+ if ( fr ){
629
+ reallocx (C.rowt, C.row_end, C.rows, 0, goto END);
630
+ ARY_MAX (C.row_max, k, C.rowt, 0, C.rows);
631
+ ARY_MIN (C.row_min, k, C.rowt, 0, C.rows);
632
+ }
633
+ if ( fe && C.clmt ){
634
+ if ( fc ) reallocx (C.clmt, C.clm_end, C.clms, 0, goto END);
635
+ ARY_MAX (C.clm_max, k, C.clmt, 0, C.clms);
636
+ ARY_MIN (C.clm_min, k, C.clmt, 0, C.clms);
637
+ }
638
+ if ( ERROR_MES ) mfree (C.rowt, C.clmt);
639
+ return (C);
640
+ }
641
+
642
+
643
+ /* SLIST:very simple one-sided list */
644
+ void SLIST_init (int *l, int num, int siz){
645
+ malloc2 (l, num+siz, EXIT);
646
+ ARY_FILL (l, num, num+siz, -1);
647
+ }
648
+ void SLIST_end (int *l){ free (l); }
649
+ #define SLIST_INS(l,m,e) (l[e]=l[m],l[m]=e);
650
+
651
+ /* qsort according to "->t" */
652
+ int qsort_cmp_VECt (const void *x, const void *y){
653
+ if ( ((VEC *)x)->t < ((VEC *)y)->t ) return (-1);
654
+ else return ( ((VEC *)x)->t > ((VEC *)y)->t);
655
+ }
656
+ int qsort_cmp__VECt (const void *x, const void *y){
657
+ if ( ((VEC *)x)->t > ((VEC *)y)->t ) return (-1);
658
+ else return ( ((VEC *)x)->t < ((VEC *)y)->t);
659
+ }
660
+ void qsort_VECt (VEC *v, size_t siz, int unit){
661
+ if ( unit == 1 || unit==-1 ) unit *= sizeof (VEC);
662
+ if ( unit < 0 ) qsort (v, siz, -unit, qsort_cmp__VECt);
663
+ else qsort (v, siz, unit, qsort_cmp_VECt);
664
+ }
665
+
666
+ int qqsort_cmp_VECt (const void *x, const void *y){
667
+ if ( QQSORT_ELEt(VEC,x) < QQSORT_ELEt(VEC,y) ) return (-1);
668
+ else return ( QQSORT_ELEt(VEC,x) > QQSORT_ELEt(VEC,y) );
669
+ }
670
+ int qqsort_cmp__VECt (const void *x, const void *y){
671
+ if ( QQSORT_ELEt(VEC,x) > QQSORT_ELEt(VEC,y) ) return (-1);
672
+ else return ( QQSORT_ELEt(VEC,x) < QQSORT_ELEt(VEC,y) );
673
+ }
674
+ void qsort_perm__VECt (VEC *v, size_t siz, PERM *perm, int unit){
675
+ if ( unit == 1 || unit==-1 ) unit *= sizeof(VEC);
676
+ common_int=MAX(unit,-unit); common_pnt=(char *)v;
677
+ if (unit<0) qsort (perm, siz, sizeof(PERM), qqsort_cmp__VECt);
678
+ else qsort (perm, siz, sizeof(PERM), qqsort_cmp_VECt);
679
+ }
680
+
681
+ PERM *qsort_perm_VECt (VEC *v, size_t siz, int unit){
682
+ PERM *perm;
683
+ malloc2 (perm, siz, EXIT0);
684
+ ARY_INIT_PERM (perm, siz);
685
+ qsort_perm__VECt (v, siz, perm, unit);
686
+ return(perm);
687
+ }
688
+
689
+ #ifdef STDLIB2_RADIX_SORT // radix sort with 1M byte static memory
690
+
691
+ #define RADIX_SORT_BUCKET_SIZ 2048
692
+ /* sort of integer array with combination of radix sort and quick sort */
693
+ /* flag&1: sort in decreasing order */
694
+
695
+ // sort by lower bits
696
+ void intarray_sort_iter (unsigned int *a, size_t siz, int unit){
697
+ static size_t cnt[RADIX_SORT_BUCKET_SIZ], cnt2[RADIX_SORT_BUCKET_SIZ], init_flag = 1;
698
+ size_t k, x;
699
+ int i, ii, j, flag=1;
700
+ static char bbuf[1000], bbuf2[1000];
701
+ char *aa, *aaa, *aa_end, *buf, *buf2;
702
+
703
+ if ( siz<1000 ){ qsort_uint ( a, siz, unit); return; }
704
+ if ( unit <0 ){ unit = -unit; flag = -1; }
705
+ if ( unit == 1 ) unit = sizeof (int);
706
+ buf = bbuf; buf2 = bbuf2;
707
+ if ( init_flag == 1 ){
708
+ init_flag = 0;
709
+ ARY_FILL (cnt, 0, RADIX_SORT_BUCKET_SIZ, 0);
710
+ }
711
+ // count elements of each number
712
+ for ( aa=(char*)a,aa_end=aa+siz*unit ; aa<aa_end ; aa+=unit )
713
+ cnt[(*((unsigned int *)aa)) & (RADIX_SORT_BUCKET_SIZ-1)]++; // difference!!
714
+
715
+ // sum up the numbers in increasing order
716
+ k=0; FLOOP (i, 0, RADIX_SORT_BUCKET_SIZ){
717
+ ii = flag==1? i: RADIX_SORT_BUCKET_SIZ-i-1;
718
+ cnt2[ii] = k;
719
+ k += cnt[ii];
720
+ cnt[ii] = k;
721
+ }
722
+
723
+ FLOOP (i, 0, RADIX_SORT_BUCKET_SIZ){
724
+ ii = flag==1? i: RADIX_SORT_BUCKET_SIZ-i-1;
725
+ FLOOP (x, cnt2[ii], cnt[ii]){
726
+ aa = ((char*)a) + x*unit;
727
+ memcpy ( buf, aa, unit);
728
+ while (1){
729
+ j = *((unsigned int *)buf) & (RADIX_SORT_BUCKET_SIZ-1); // difference!!
730
+ if ( j == ii ) break;
731
+ aaa = ((char*)a) + cnt2[j]*unit;
732
+ // printf ("pos[xx]=%d, cnt %d, cnt+1 %d\n", pos[xx], S->let_cnt[dep][cc], S->let_cnt[dep+1][cc]);
733
+ memcpy ( buf2, aaa, unit);
734
+ memcpy ( aaa, buf, unit);
735
+ SWAP_PNT ( buf, buf2);
736
+ cnt2[j]++;
737
+ }
738
+ memcpy ( aa, buf, unit);
739
+ }
740
+ cnt[i]=0; // difference!!
741
+ }
742
+ }
743
+ // sort by middle bits
744
+ void intarray_sort_iter_ ( unsigned int *a, size_t siz, int unit){
745
+ static size_t cnt[RADIX_SORT_BUCKET_SIZ], cnt2[RADIX_SORT_BUCKET_SIZ], init_flag = 1;
746
+ int i, ii, j, flag=1;
747
+ size_t k, x;
748
+ static char bbuf[1000], bbuf2[1000];
749
+ char *aa, *aaa, *aa_end, *buf, *buf2;
750
+
751
+ if ( siz<1000 ){ qsort_uint ( a, siz, unit); return; }
752
+ buf = bbuf; buf2 = bbuf2;
753
+ if ( unit <0 ){ unit = -unit; flag = -1; }
754
+ if ( unit == 1 ) unit = sizeof (int);
755
+ if ( init_flag == 1 ){
756
+ init_flag = 0;
757
+ ARY_FILL ( cnt, 0, RADIX_SORT_BUCKET_SIZ, 0);
758
+ }
759
+ // count elements of each number
760
+ for ( aa=(char*)a,aa_end=aa+siz*unit ; aa<aa_end ; aa+=unit )
761
+ cnt[((*((unsigned int *)aa))/RADIX_SORT_BUCKET_SIZ) & (RADIX_SORT_BUCKET_SIZ-1)]++; // difference!!
762
+
763
+ // sum up the numbers in increasing order
764
+ k=0; FLOOP (i, 0, RADIX_SORT_BUCKET_SIZ){
765
+ ii = flag==1? i: RADIX_SORT_BUCKET_SIZ-i-1;
766
+ cnt2[ii] = k;
767
+ k += cnt[ii];
768
+ cnt[ii] = k;
769
+ }
770
+
771
+ FLOOP (i, 0, RADIX_SORT_BUCKET_SIZ){
772
+ ii = flag==1? i: RADIX_SORT_BUCKET_SIZ-i-1;
773
+ FLOOP(x, cnt2[ii], cnt[ii]){
774
+ aa = ((char*)a) + x*unit;
775
+ memcpy ( buf, aa, unit);
776
+ while (1){
777
+ j = (*((unsigned int *)buf)/RADIX_SORT_BUCKET_SIZ) & (RADIX_SORT_BUCKET_SIZ-1); // difference!!
778
+ if ( j == ii ) break;
779
+ aaa = ((char*)a) + cnt2[j]*unit;
780
+ // printf ("pos[xx]=%d, cnt %d, cnt+1 %d\n", pos[xx], S->let_cnt[dep][cc], S->let_cnt[dep+1][cc]);
781
+ memcpy (buf2, aaa, unit);
782
+ memcpy (aaa, buf, unit);
783
+ SWAP_PNT (buf, buf2);
784
+ cnt2[j]++;
785
+ }
786
+ memcpy (aa, buf, unit);
787
+ }
788
+ }
789
+ k=0; FLOOP (i, 0, RADIX_SORT_BUCKET_SIZ){
790
+ ii = flag==1? i: RADIX_SORT_BUCKET_SIZ-i-1;
791
+ intarray_sort_iter ( (unsigned int*)(((char*)a)+unit*k), cnt[ii]-k, unit*flag);
792
+ k = cnt[ii];
793
+ cnt[i]=0;
794
+ }
795
+ }
796
+
797
+ // sort by upper bits
798
+ void intarray_sort ( unsigned int *a, size_t siz, int unit){
799
+ static size_t cnt[RADIX_SORT_BUCKET_SIZ], cnt2[RADIX_SORT_BUCKET_SIZ], init_flag = 1;
800
+ int i, ii, j, flag=1;
801
+ size_t k, x;
802
+ static char bbuf[1000], bbuf2[1000];
803
+ char *aa, *aaa, *aa_end, *buf, *buf2;
804
+
805
+ if ( siz<1000 ){ qsort_uint ( a, siz, unit); return; }
806
+ if ( unit <0 ){ unit = -unit; flag = -1; }
807
+ if ( unit == 1 ) unit = sizeof (int);
808
+ buf = bbuf; buf2 = bbuf2;
809
+ if ( init_flag == 1){
810
+ init_flag = 0;
811
+ ARY_FILL (cnt, 0, RADIX_SORT_BUCKET_SIZ, 0);
812
+ }
813
+ // count elements of each number
814
+ for ( aa=(char*)a,aa_end=aa+siz*unit ; aa<aa_end ; aa+=unit )
815
+ cnt[(*((unsigned int *)aa)) / RADIX_SORT_BUCKET_SIZ / RADIX_SORT_BUCKET_SIZ]++; // difference!!
816
+
817
+ // sum up the numbers in increasing order
818
+ k=0; FLOOP (i, 0, RADIX_SORT_BUCKET_SIZ){
819
+ ii = flag==1? i: RADIX_SORT_BUCKET_SIZ-i-1;
820
+ cnt2[ii] = k;
821
+ k += cnt[ii];
822
+ cnt[ii] = k;
823
+ }
824
+
825
+ FLOOP (i, 0, RADIX_SORT_BUCKET_SIZ){
826
+ ii = flag==1? i: RADIX_SORT_BUCKET_SIZ-i-1;
827
+ FLOOP (x, cnt2[ii], cnt[ii]){
828
+ aa = ((char*)a) + x*unit;
829
+ memcpy ( buf, aa, unit);
830
+ while (1){
831
+ j = *((unsigned int *)buf) / RADIX_SORT_BUCKET_SIZ / RADIX_SORT_BUCKET_SIZ; // difference!!
832
+ if ( j == ii ) break;
833
+ aaa = ((char*)a) + cnt2[j]*unit;
834
+ // printf ("pos[xx]=%d, cnt %d, cnt+1 %d\n", pos[xx], S->let_cnt[dep][cc], S->let_cnt[dep+1][cc]);
835
+ memcpy (buf2, aaa, unit);
836
+ memcpy (aaa, buf, unit);
837
+ SWAP_PNT (buf, buf2);
838
+ cnt2[j]++;
839
+ }
840
+ memcpy ( aa, buf, unit);
841
+ }
842
+ }
843
+ k=0; FLOOP (i, 0, RADIX_SORT_BUCKET_SIZ){
844
+ ii = flag==1? i: RADIX_SORT_BUCKET_SIZ-i-1;
845
+ intarray_sort_iter_ ( (unsigned int*)(((char*)a)+unit*k), cnt[ii]-k, unit*flag);
846
+ k = cnt[ii];
847
+ cnt[i]=0;
848
+ }
849
+
850
+ /*
851
+ for ( i=0 ; i<siz ; i++){
852
+ k = *((int *)(((char*)a) + i*unit));
853
+ printf ("%d %d,%d\n", k, k/65536, k&65535);
854
+ }
855
+ */
856
+ }
857
+
858
+ #endif
859
+
860
+ /* radix sort for array of structures, by their integer members
861
+ ranging from mm to m */
862
+ /* sort array "perm" according to (int/void*) array "a".
863
+ if perm==NULL, allocate memory and for perm */
864
+ /* return the permutation array of the result of the sorting
865
+ in the decreasing order if unit<0 (unimplemented!!!) */
866
+ int *radix_sort ( void *a, size_t siz, int unit, int mm, int m, int *perm){
867
+ int *ll, *l, k, i, t, flag=1;
868
+ malloc2 (l, m-mm, EXIT0);
869
+ ARY_FILL (l, 0, m-mm, -1);
870
+ malloc2 (ll, siz, {free2(l);EXIT0;});
871
+ if ( unit <0 ){ unit = -unit; flag = -1; }
872
+ FLOOP (i, 0, (int)siz){
873
+ k = (*((int *)(((char *)a) + unit*i ))) - mm;
874
+ ll[i] = l[k];
875
+ l[k] = i;
876
+ }
877
+ if ( perm ){
878
+ i=0; FLOOP (k, 0, m-mm){
879
+ while ( l[k] >= 0 ){
880
+ t = l[k];
881
+ l[k] = ll[t];
882
+ ll[t] = perm[i];
883
+ i++;
884
+ }
885
+ }
886
+ memcpy (perm, ll, sizeof(int)*siz);
887
+ free ( ll);
888
+ free ( l);
889
+ return ( perm);
890
+ } else {
891
+ i=0; FLOOP (k, 0, m-mm){
892
+ while ( l[k] >= 0 ){
893
+ t = l[k];
894
+ l[k] = ll[t];
895
+ ll[t] = i;
896
+ i++;
897
+ }
898
+ }
899
+ free (l);
900
+ return (ll);
901
+ }
902
+ }
903
+
904
+ /* permutate structure array *tt of unit size unit of size num, according to perm array *pp */
905
+ /* num has to be <INTHUGE/2 */
906
+ /* unit<0 means decreasing order (un-implemented!!!) */
907
+ void structure_permute (void *tt, int unit, int num, void *pp, int weight_siz){
908
+ int i, ii, *ip, flag=1;
909
+ char *tmp, *t=(char *)tt, *p=(char *)pp;
910
+ if ( unit <0 ){ unit = -unit; flag = -1; }
911
+ malloc2 (tmp, unit, EXIT);
912
+ FLOOP (i, 0, num){
913
+ ip = (int *)(p + (sizeof(int)+weight_siz)*i + weight_siz);
914
+ if ( *ip< num && *ip != i ){
915
+ ii = i;
916
+ memcpy ( tmp, t + unit*i, unit);
917
+ while (1) {
918
+ if ( *ip == i ){
919
+ memcpy ( t+unit*ii, tmp, unit);
920
+ *ip += num;
921
+ break;
922
+ }
923
+ memcpy ( t+unit*ii, t+unit*(*ip), unit);
924
+ ii = *ip;
925
+ *ip += num;
926
+ ip = (int *)(p + (sizeof(int)+weight_siz)*ii + weight_siz);
927
+ }
928
+ } else *ip += num;
929
+ }
930
+ FLOOP (i, 0, num) *(int *)(p + (sizeof(int)+weight_siz)*i + weight_siz ) -= num;
931
+ free (tmp);
932
+ }
933
+
934
+
935
+
936
+ #endif
937
+
938
+
939
+ /******************************************/
940
+ /* ==== terminology for comments ====
941
+ range check: to check the input parameter is valid, or in the valid range.
942
+ If a function does not have this, its comment has "no range check"
943
+ */
944
+
945
+ /* ==== rules for the name of functions and routines ====
946
+ init: initialization for an object, structure, etc. memory is allocated
947
+ if needed.
948
+ end: compared to init, termination of structures, etc.
949
+ free allocated memory if it exists, but not free itself.
950
+ different from ordinary new, create, del, free.
951
+
952
+ cpy: copy an object without allocating memory
953
+ dup: make a duplication of an object with allocating new memory
954
+
955
+ new: new. allocate memory for new object. also used for re-allocation from
956
+ the list of deleted objects
957
+ del: delete. free memory, or insert it to the list of deleted objects
958
+
959
+ ins : insert. insert an element (active, not deleted) to an object, possible
960
+ at the given position.
961
+ out : extract. extract an element, possibly specified, from an object.
962
+ it will be not deleted.
963
+ rm : extract, and delete
964
+ rmall: delete all (specified) elements of an object
965
+ mk : make. new+ins�B
966
+ mv : move. move the elements from an object to another,
967
+ or change the position.
968
+
969
+ update : update an object, possibly of specified position, to the exact,
970
+ or to the current one.
971
+ chg : change the status of an object to the specified one.
972
+
973
+ prv: point the previous element
974
+ nxt: point the next element
975
+ end: the maximum size (allocated size) of an array, etc.
976
+ num: the (current) number of elements in an array, etc.
977
+ kth: for the use of "first k object"
978
+ tkth: for the use of "first k object from the end". to kth.
979
+ rnd: random
980
+ print: print structure and etc.
981
+ find: search or find an specified element from the set of structures, etc.
982
+ */
983
+
984
+ /* ==== rules for the name of variables ====
985
+ - use i or j for the counter in loops
986
+ - use e for elements
987
+ - use k for the specified rank
988
+ - use f or flag for flagment
989
+ - use m for maximum value or minimum value
990
+ - use c for counters
991
+ */
992
+
993
+