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,545 @@
1
+ /*
2
+ array-based simple heap (fixex size)
3
+ 12/Apr/2001 by Takeaki Uno e-mail:uno@nii.jp,
4
+ homepage: http://research.nii.ac.jp/~uno/index.html */
5
+ /* This program is available for only academic use, basically.
6
+ Anyone can modify this program, but he/she has to write down
7
+ the change of the modification on the top of the source code.
8
+ Neither contact nor appointment to Takeaki Uno is needed.
9
+ If one wants to re-distribute this code, please
10
+ refer the newest code, and show the link to homepage of
11
+ Takeaki Uno, to notify the news about the codes for the users. */
12
+
13
+ #ifndef _aheap_c_
14
+ #define _aheap_c_
15
+
16
+ #include"aheap.h"
17
+ #include"stdlib2.c"
18
+
19
+ QSORT_TYPE (AHEAP_KEY, AHEAP_KEY)
20
+ QSORT_TYPE (AHEAP_ID, AHEAP_ID)
21
+ AHEAP INIT_AHEAP = {TYPE_AHEAP,NULL,0,0};
22
+
23
+ /* print heap keys according to the structure of the heap */
24
+ void AHEAP_print (AHEAP *H){
25
+ AHEAP_ID i, j=1;
26
+ while ( j<=H->end*2-1 ){
27
+ FLOOP (i, j-1, MIN(j, H->end)*2-1) printf (AHEAP_KEYF ",", H->v[i] );
28
+ printf ("\n");
29
+ j = j*2;
30
+ }
31
+ }
32
+
33
+ /* allocate memory */
34
+ void AHEAP_alloc (AHEAP *H, AHEAP_ID num){
35
+ AHEAP_ID i;
36
+ #ifdef ERROR_CHECK
37
+ if ( num<0 ) error_num ("size is out of range", num, EXIT);
38
+ #endif
39
+ *H = INIT_AHEAP;
40
+ if ( num>0 ) malloc2 (H->v, num*2, EXIT);
41
+ H->end = num;
42
+ ARY_FILL (H->v, 0, num*2, AHEAP_KEYHUGE);
43
+ for (i=0 ; i<num-1 ; i=i*2+1);
44
+ H->base = i - num + 1;
45
+ }
46
+
47
+ /* termination */
48
+ void AHEAP_end (AHEAP *H){
49
+ free2 (H->v);
50
+ *H = INIT_AHEAP;
51
+ }
52
+
53
+ /* return the index of the leaf having the minimum key among the descendants
54
+ of the given node i. If several leaves with the smallest key are there,
55
+ return the minimum index among them if f=0, maximum index if f=1, and
56
+ random choice if f=2 */
57
+ /* random choice version. choose one child to go down randomly for each node,
58
+ thus it is not uniformly random */
59
+ /* node_ returns the ID of leaf */
60
+ AHEAP_ID AHEAP_findmin_node_ (AHEAP *H, AHEAP_ID i, int f){
61
+ while ( i < H->end-1 ){
62
+ if ( H->v[i*2+1] == H->v[i] )
63
+ if ( H->v[i*2+2] == H->v[i] )
64
+ if ( f == 2 ) i = i*2 + 1 + rand()%2;
65
+ else i = i*2+1+f;
66
+ else i = i*2+1;
67
+ else i = i*2+2;
68
+ }
69
+ return (i);
70
+ }
71
+ AHEAP_ID AHEAP_findmin_node (AHEAP *H, AHEAP_ID i, int f){
72
+ if ( H->end <= 0 ) return (-1);
73
+ return (AHEAP_IDX(*H, AHEAP_findmin_node_ (H, i, f)));
74
+ }
75
+ AHEAP_ID AHEAP_findmin_head (AHEAP *H){ return (AHEAP_findmin_node (H, 0, 0) ); }
76
+ AHEAP_ID AHEAP_findmin_tail (AHEAP *H){ return (AHEAP_findmin_node (H, 0, 1) ); }
77
+ AHEAP_ID AHEAP_findmin_rnd (AHEAP *H){ return (AHEAP_findmin_node (H, 0, 2) ); }
78
+
79
+ /* return the index of the leaf having smaller value than a among the
80
+ descendants of the given node i. If several leaves with the smallest key
81
+ are there, return the minimum index among them if f=0, maximum index if f=1,
82
+ and random choice if f=2 */
83
+ AHEAP_ID AHEAP_findlow_node (AHEAP *H, AHEAP_KEY a, AHEAP_ID i, int f){
84
+ if ( H->end == 0 ) return (-1);
85
+ if ( H->v[0] > a ) return (-1);
86
+ while ( i < H->end-1 ){
87
+ if ( f == 2 ) {
88
+ if ( H->v[i*2+1] <= a )
89
+ if ( H->v[i*2+2] <= a ) i = i*2 + 1 + rand()%2;
90
+ else i = i*2+1;
91
+ else i = i*2+2;
92
+ } else if ( H->v[i*2+1] <= a ) i = i*2+1+f; else i = i*2+2-f;
93
+ }
94
+ return (AHEAP_IDX(*H, i) );
95
+ }
96
+ AHEAP_ID AHEAP_findlow_head (AHEAP *H, AHEAP_KEY a){ return (AHEAP_findlow_node (H, a, 0, 0) ); }
97
+ AHEAP_ID AHEAP_findlow_tail (AHEAP *H, AHEAP_KEY a){ return (AHEAP_findlow_node (H, a, 0, 1) ); }
98
+ AHEAP_ID AHEAP_findlow_rnd (AHEAP *H, AHEAP_KEY a){ return (AHEAP_findlow_node (H, a, 0, 2) ); }
99
+
100
+ /* return the index of the leaf having smaller value than a next/previous to
101
+ leaf i. return -1 if such a leaf does not exist */
102
+ AHEAP_ID AHEAP_findlow_nxt (AHEAP *H, AHEAP_ID i, AHEAP_KEY a){
103
+ if ( H->end == 0 ) return (-1);
104
+ if ( i<0 || i>= H->end ) return ( AHEAP_findlow_head (H, a));
105
+ for (i=AHEAP_LEAF(*H,i); i>0 ; i=(i-1)/2){
106
+ /* i is the child of smaller index, and the key of the sibling of i is less than a */
107
+ if ( i%2 == 1 && H->v[i+1] <= a ) return (AHEAP_findlow_node (H, a, i+1, 0) );
108
+ }
109
+ return (-1);
110
+ }
111
+ AHEAP_ID AHEAP_findlow_prv (AHEAP *H, AHEAP_ID i, AHEAP_KEY a){
112
+ if ( H->end == 0 ) return (-1);
113
+ if ( i<0 || i>= H->end ) return ( AHEAP_findlow_head (H, a));
114
+ for (i=AHEAP_LEAF(*H,i); i>0 ; i=(i-1)/2){
115
+ /* i is the child of larger index, and the key of the sibling of i is less than a */
116
+ if ( i%2 == 0 && H->v[i-1] <= a ) return (AHEAP_findlow_node (H, a, i-1, 1) );
117
+ }
118
+ return (-1);
119
+ }
120
+
121
+ /* change the key of node i to a /Add a to the key of node i, and update heap H */
122
+ void AHEAP_chg (AHEAP *H, AHEAP_ID i, AHEAP_KEY a){
123
+ i = AHEAP_LEAF (*H, i);
124
+ H->v[i] = a;
125
+ AHEAP_update (H, i);
126
+ }
127
+ void AHEAP_add (AHEAP *H, AHEAP_ID i, AHEAP_KEY a){
128
+ i = AHEAP_LEAF (*H, i);
129
+ H->v[i] += a;
130
+ AHEAP_update (H, i);
131
+ }
132
+
133
+ /* update the ancestor of node i */
134
+ void AHEAP_update (AHEAP *H, AHEAP_ID i){
135
+ AHEAP_ID j;
136
+ AHEAP_KEY a = H->v[i];
137
+ while ( i>0 ){
138
+ j = i - 1 + (i%2)*2; /* j = the sibling of i */
139
+ i = (i-1) / 2;
140
+ if ( H->v[j] < a ) a = H->v[j];
141
+ if ( a == H->v[i] ) break;
142
+ H->v[i] = a;
143
+ }
144
+ }
145
+
146
+ /* find the leaf with the minimum key value among the leaves having index
147
+ smaller/larger than i, or between i and j */
148
+ AHEAP_ID AHEAP_upper_min (AHEAP *H, AHEAP_ID i){
149
+ AHEAP_ID fi=0, j = AHEAP_LEAF (*H, H->end - 1);
150
+ AHEAP_KEY fm = AHEAP_KEYHUGE;
151
+ if ( i == 0 ) return (AHEAP_findmin_head (H) );
152
+ i = AHEAP_LEAF (*H, i-1);
153
+ while ( i != j ){
154
+ if ( i%2 ){ /* if i is the child with smaller index */
155
+ if ( fm > H->v[i+1] ){
156
+ fm = H->v[i+1];
157
+ fi = i+1;
158
+ }
159
+ }
160
+ i = (i-1)/2;
161
+ if ( j == i ) break; /* stop if the right pointer and the left pointer are the same */
162
+ j = (j-1)/2;
163
+ }
164
+ while ( fi < H->end-1 ) fi = fi*2 + (H->v[fi*2+1]<=fm?1:2);
165
+ return ( AHEAP_IDX(*H, fi) );
166
+ }
167
+ AHEAP_ID AHEAP_lower_min (AHEAP *H, AHEAP_ID i){
168
+ AHEAP_ID fi=0, j = AHEAP_LEAF (*H, 0);
169
+ AHEAP_KEY fm = AHEAP_KEYHUGE;
170
+ if ( i == H->end-1 ) return (AHEAP_findmin_head (H) );
171
+ i = AHEAP_LEAF (*H, i+1);
172
+ while ( i != j ){
173
+ if ( i%2 == 0 ){ /* if i is the child of larger index */
174
+ if ( fm > H->v[i-1] ){
175
+ fm = H->v[i-1];
176
+ fi = i-1;
177
+ }
178
+ }
179
+ j = (j-1)/2;
180
+ if ( j == i ) break; /* stop if the right pointer and the left pointer are the same */
181
+ i = (i-1)/2;
182
+ }
183
+ while ( fi < H->end-1 ) fi = fi*2 + (H->v[fi*2+1]<=fm?1:2);
184
+ return (AHEAP_IDX(*H, fi) );
185
+ }
186
+
187
+ /* find the index having the minimum among given two indices */
188
+ AHEAP_ID AHEAP_interval_min (AHEAP *H, AHEAP_ID i, AHEAP_ID j){
189
+ AHEAP_ID fi=0;
190
+ AHEAP_KEY fm = AHEAP_KEYHUGE;
191
+ if ( i == 0 ) return (AHEAP_lower_min (H, j) );
192
+ if ( j == H->end-1 ) return (AHEAP_upper_min (H, i) );
193
+ i = AHEAP_LEAF (*H, i-1);
194
+ j = AHEAP_LEAF (*H, j+1);
195
+ while ( i != j && i != j-1 ){
196
+ if ( i%2 ){ /* if i is the child of smaller index */
197
+ if ( fm > H->v[i+1] ){
198
+ fm = H->v[i+1];
199
+ fi = i+1;
200
+ }
201
+ }
202
+ i = (i-1)/2;
203
+ if ( j == i || j == i+1 ) break; // stop if the right pointer and the left pointer are the same
204
+ if ( j%2 == 0 ){ // if j is the child of larger index
205
+ if ( fm > H->v[j-1] ){
206
+ fm = H->v[j-1];
207
+ fi = j-1;
208
+ }
209
+ }
210
+ j = (j-1)/2;
211
+ }
212
+ while ( fi < H->end-1 )
213
+ fi = fi*2 + (H->v[fi*2+1] <= fm?1:2);
214
+ return (AHEAP_IDX(*H, fi) );
215
+ }
216
+
217
+
218
+ /******************************************************************************/
219
+ /* VHEAP: variable size heap */
220
+ /******************************************************************************/
221
+
222
+ QSORT_TYPE (VHEAP_KEY, VHEAP_KEY)
223
+ QSORT_TYPE (VHEAP_ID, VHEAP_ID)
224
+ VHEAP INIT_VHEAP = {TYPE_VHEAP,NULL,0,0,0};
225
+
226
+ /* print heap keys according to the structure of the heap */
227
+ void VHEAP_print (FILE *fp, VHEAP *H){
228
+ VHEAP_ID i=0, j=1, ii;
229
+ fprintf (fp, "siz:" VHEAP_IDF ", end: " VHEAP_IDF ", unit:%d\n", H->siz, H->end, H->unit);
230
+ while (i < H->siz){
231
+ ii = MIN (i+j, H->siz);
232
+ while (i < ii){
233
+ fprintf (fp, VHEAP_KEYF ",", VHEAP_V(*H,i) );
234
+ i++;
235
+ }
236
+ fprintf (fp, "\n");
237
+ j = j*2;
238
+ }
239
+ }
240
+ void VHEAP_print_ele (FILE *fp, VHEAP *H){
241
+ VHEAP_ID i=0, j=1, ii;
242
+ fprintf (fp, "siz:" VHEAP_IDF ", end: " VHEAP_IDF ", unit:%d\n", H->siz, H->end, H->unit);
243
+ while (i < H->siz){
244
+ ii = MIN (i+j, H->siz);
245
+ while (i < ii){
246
+ fprintf (fp, VHEAP_KEYF "(" VHEAP_IDF "),", VHEAP_V(*H,i), VHEAP_I(*H,i) );
247
+ i++;
248
+ }
249
+ fprintf (fp, "\n");
250
+ j = j*2;
251
+ }
252
+ }
253
+
254
+ /* allocate memory */
255
+ void VHEAP_alloc (VHEAP *H, VHEAP_ID num, int unit){
256
+ #ifdef ERROR_CHECK
257
+ if ( num<0 ) error_num ("index is out of range", num, EXIT);
258
+ #endif
259
+ *H = INIT_VHEAP;
260
+ ENMAX (unit, sizeof(VHEAP_KEY));
261
+ if (num == 0) num = 16;
262
+ malloc2 (H->v, num*unit, EXIT);
263
+ H->end = num;
264
+ H->unit = unit;
265
+ }
266
+
267
+ /* termination */
268
+ void VHEAP_end (VHEAP *H){
269
+ free2 (H->v);
270
+ *H = INIT_VHEAP;
271
+ }
272
+
273
+ /* update VHEAP for decrease/increase of the value of i-th node to w
274
+ return the index to that w is written */
275
+ /* no appended area version (each cell consists only of a VHEAP_KEY) */
276
+ VHEAP_ID VHEAP_dec_ (VHEAP *H, VHEAP_ID i, VHEAP_KEY w){
277
+ VHEAP_KEY *h=(VHEAP_KEY *)H->v;
278
+ VHEAP_ID ii;
279
+ while (i > 0){
280
+ ii = (i-1)/2;
281
+ if (h[ii] <= w) break;
282
+ h[i] = h[ii];
283
+ i = ii;
284
+ }
285
+ h[i] = w;
286
+ return (i);
287
+ }
288
+ VHEAP_ID VHEAP_inc_ (VHEAP *H, VHEAP_ID i, VHEAP_KEY w){
289
+ VHEAP_ID jj, j1, j2, end=(H->siz-1)/2;
290
+ VHEAP_KEY *h=(VHEAP_KEY *)H->v;
291
+ while (i < end){
292
+ j1 = i*2+1; j2 = j1+1;
293
+ jj = h[j1]<=h[j2]? j1: j2;
294
+ if ( w <= h[jj] ) goto END;
295
+ h[i] = h[jj];
296
+ i = jj;
297
+ }
298
+ if (i == end && (H->siz&1)==0){
299
+ jj = i*2+1;
300
+ if ( w > h[jj] ){ h[i] = h[jj]; i = jj; }
301
+ }
302
+ END:;
303
+ h[i] = w;
304
+ return (i);
305
+ }
306
+
307
+
308
+ /* update VHEAP for decrease/increase of the value of i-th node to w
309
+ return the index to that w is written */
310
+ VHEAP_ID VHEAP_dec (VHEAP *H, VHEAP_ID i, VHEAP_KEY w, size_t ii, VHEAP_KEY **hh){
311
+ VHEAP_KEY *h;
312
+ // size_t unit = H->unit-sizeof(VHEAP_KEY);
313
+ while (i > 0){
314
+ ii = (i&1)? (ii-H->unit)/2: ii/2-H->unit;
315
+ h = (VHEAP_KEY *)(H->v + ii);
316
+ if (*h <= w) break;
317
+ memcpy (*hh, h, H->unit);
318
+ i = (i-1)/2; *hh = h;
319
+ }
320
+ **hh = w;
321
+ return (i);
322
+ }
323
+ VHEAP_ID VHEAP_inc (VHEAP *H, VHEAP_ID i, VHEAP_KEY w, size_t ii, VHEAP_KEY **hh){
324
+ size_t jj, j1, j2, end=(H->siz-1)/2;
325
+ VHEAP_KEY *h1, *h2, *h;
326
+ end *= H->unit;
327
+ while (ii < end){
328
+ j1 = ii*2+H->unit; j2 = j1+H->unit;
329
+ h1 = (VHEAP_KEY *)(H->v + j1); h2 = (VHEAP_KEY *)(H->v + j2);
330
+ if ( *h1 <= *h2 ){ jj = j1; h = h1; } else { jj = j2; h = h2; }
331
+ if ( w <= *h ) goto END;
332
+ memcpy (*hh, h, H->unit);
333
+ *hh = h; ii = jj;
334
+ }
335
+ if (ii == end && (H->siz&1)==0){
336
+ jj = ii*2+H->unit; h = (VHEAP_KEY *)(H->v + jj);
337
+ if ( w > *h ){ memcpy (*hh, h, H->unit); ii = jj; *hh = h; }
338
+ }
339
+ END:;
340
+ **hh = w;
341
+ return (ii/H->unit);
342
+ }
343
+
344
+ // memcpy (hh+1, he, H->unit-sizeof(VHEAP_KEY)); // copy preserved appended area to the target position
345
+
346
+ /* change the value of i-th node to w */
347
+ VHEAP_ID VHEAP_chg (VHEAP *H, VHEAP_ID i, VHEAP_KEY w){
348
+ size_t ii = i*H->unit, unit = H->unit-sizeof(VHEAP_KEY);
349
+ VHEAP_KEY *h = (VHEAP_KEY *)(H->v + ii), *he = (VHEAP_KEY *)(H->v + H->siz*H->unit);
350
+ #ifdef ERROR_CHECK
351
+ if ( i<0 || i>H->siz ) error_num ("index is out of range", i, EXIT);
352
+ #endif
353
+ if ( H->end <= 0 ) return (-1);
354
+ memcpy (he, h+1, unit); // preserve appended area
355
+ if ( w < *h ) i = VHEAP_dec (H, i, w, ii, &h);
356
+ else i = VHEAP_inc (H, i, w, ii, &h);
357
+ memcpy (h+1, he, unit); // preserve appended area
358
+ return (i);
359
+ }
360
+ /* non-appended version (only with a VHEAP_KEY) */
361
+ VHEAP_ID VHEAP_chg_ (VHEAP *H, VHEAP_ID i, VHEAP_KEY w){
362
+ #ifdef ERROR_CHECK
363
+ if ( i<0 || i>H->siz ) error_num ("index is out of range", i, EXIT);
364
+ #endif
365
+ if ( H->end <= 0 ) return (-1);
366
+ if ( w < VHEAP_V(*H,i) ) return (VHEAP_dec_ (H, i, w));
367
+ else return (VHEAP_inc_ (H, i, w));
368
+ }
369
+
370
+ /* insert/delete an element */
371
+ void VHEAP_ins_ (VHEAP *H, VHEAP_KEY w){
372
+ H->siz++;
373
+ if (H->siz >= H->end){ H->end = MAX(H->end*2, H->siz+1); realloc2 (H->v, H->end*sizeof(VHEAP_KEY),EXIT);}
374
+ VHEAP_dec_ (H, H->siz-1, w);
375
+ }
376
+ VHEAP_ID VHEAP_ins (VHEAP *H, VHEAP_KEY w){
377
+ size_t ii = H->siz*H->unit;
378
+ VHEAP_KEY *h = (VHEAP_KEY *)(H->v + ii);
379
+ H->siz++;
380
+ if (H->siz >= H->end){ H->end = MAX(H->end*2, H->siz+1); realloc2 (H->v, H->end*H->unit,exit(1));}
381
+ // if (H->siz >= H->end){ H->end = MAX(H->end*2, H->siz+1); realloc2 (H->v, H->end*H->unit,EXIT);}
382
+ return (VHEAP_dec (H, H->siz-1, w, ii, &h));
383
+ }
384
+ void VHEAP_rm_ (VHEAP *H, VHEAP_ID i){
385
+ H->siz--;
386
+ VHEAP_chg_ (H, i, ((VHEAP_KEY *)(H->v))[H->siz]);
387
+ }
388
+ void VHEAP_rm (VHEAP *H, VHEAP_ID i){
389
+ H->siz--;
390
+ VHEAP_chg (H, i, VHEAP_V(*H,H->siz));
391
+ }
392
+ void VHEAP_ins_ele (VHEAP *H, VHEAP_KEY w, VHEAP_ID i){
393
+ size_t ii = H->siz*H->unit;
394
+ VHEAP_KEY *h;
395
+ H->siz++;
396
+ if (H->siz >= H->end){ H->end = MAX(H->end*2, H->siz+1); realloc2 (H->v, H->end*H->unit,EXIT);}
397
+ h = (VHEAP_KEY *)(H->v + ii);
398
+ VHEAP_dec (H, H->siz-1, w, ii, &h);
399
+ *((VHEAP_ID *)(&h[1])) = i;
400
+ }
401
+
402
+ /* extract the minimum element from H */
403
+ VHEAP_KEY VHEAP_ext_min_ (VHEAP *H){
404
+ VHEAP_KEY w= *((VHEAP_KEY *)H->v);
405
+ H->siz--;
406
+ VHEAP_inc_ (H, 0, ((VHEAP_KEY *)(H->v))[H->siz]);
407
+ return (w);
408
+ }
409
+ VHEAP_KEY VHEAP_ext_min (VHEAP *H){
410
+ VHEAP_KEY w= *((VHEAP_KEY *)H->v), *h=(VHEAP_KEY *)H->v, *hh;
411
+ H->siz--;
412
+ hh=(VHEAP_KEY *)(H->v+H->siz*H->unit);
413
+ VHEAP_inc (H, 0, *hh, 0, &h);
414
+ memcpy (h+1, hh+1, H->unit-sizeof(VHEAP_KEY)); // preserve appended area
415
+ return (w);
416
+ }
417
+ VHEAP_KEY VHEAP_ext_min_ele (VHEAP *H, VHEAP_ID *i){
418
+ VHEAP_KEY w= *((VHEAP_KEY *)H->v), *h=(VHEAP_KEY *)H->v, *hh;
419
+ *i = *((VHEAP_ID *)(H->v+sizeof(VHEAP_KEY)));
420
+ H->siz--;
421
+ hh=(VHEAP_KEY *)(H->v+H->siz*H->unit);
422
+ VHEAP_inc (H, 0, *hh, 0, &h);
423
+ *((VHEAP_ID *)(&h[1])) = *((VHEAP_ID *)(&hh[1]));
424
+ return (w);
425
+ }
426
+
427
+
428
+ /******************************************************************************/
429
+ /* VHEAP: variable size, index heap */
430
+ /******************************************************************************/
431
+
432
+ QSORT_TYPE (IHEAP_KEY, IHEAP_KEY)
433
+ QSORT_TYPE (IHEAP_ID, IHEAP_ID)
434
+ IHEAP INIT_IHEAP = {TYPE_IHEAP,NULL,0,0,NULL,0,NULL,0};
435
+
436
+ /* print heap keys according to the structure of the heap */
437
+ void IHEAP_print (FILE *fp, IHEAP *H){
438
+ IHEAP_ID i=0, j=1, ii;
439
+ fprintf (fp, "siz:" IHEAP_IDF ", end: " IHEAP_IDF ", unit:%d\n", H->siz, H->end, H->unit);
440
+ while (i < H->siz){
441
+ ii = MIN (i+j, H->siz);
442
+ while (i < ii){
443
+ fprintf (fp, IHEAP_KEYF "(" IHEAP_IDF "),", H->v[i], H->x[i]);
444
+ i++;
445
+ }
446
+ fprintf (fp, "\n");
447
+ j = j*2;
448
+ }
449
+ }
450
+
451
+ /* allocate memory */
452
+ void IHEAP_alloc (IHEAP *H, IHEAP_ID num, int mode, IHEAP_KEY *x){
453
+ #ifdef ERROR_CHECK
454
+ if ( num<0 ) error_num ("index is out of range", num, EXIT);
455
+ #endif
456
+ *H = INIT_IHEAP;
457
+ if (num == 0) num = 16;
458
+ malloc2 (H->v, num, EXIT);
459
+ H->end = num;
460
+ H->mode = mode;
461
+ H->x = x;
462
+ H->unit = 0;
463
+ }
464
+
465
+ /* termination */
466
+ void IHEAP_end (IHEAP *H){
467
+ free2 (H->v);
468
+ *H = INIT_IHEAP;
469
+ }
470
+
471
+ /* heap key comparison */
472
+ int IHEAP_compare (IHEAP *H, IHEAP_ID a, IHEAP_ID b){
473
+ if ( H->f == NULL ){
474
+ if ( H->mode == 1 ) return (H->x[a] <= H->x[b]);
475
+ if ( H->mode == 2 ) return (H->x[a] >= H->x[b]);
476
+ }// else return H->f( ((char *)H->x)[a*H->unit], ((char *)H->x)[b*H->unit]);
477
+ return (0);
478
+ }
479
+
480
+
481
+ /* update IHEAP for decrease/increase of the value of i-th node to j
482
+ return the index to that j is written */
483
+ IHEAP_ID IHEAP_dec (IHEAP *H, IHEAP_ID i, IHEAP_ID j){
484
+ IHEAP_ID ii;
485
+ while (i > 0){
486
+ ii = (i-1)/2;
487
+ if (IHEAP_compare (H, H->v[i], j)) break;
488
+ H->v[i] = H->v[ii];
489
+ i = ii;
490
+ }
491
+ H->v[i] = j;
492
+ return (i);
493
+ }
494
+ IHEAP_ID IHEAP_inc (IHEAP *H, IHEAP_ID i, IHEAP_ID j){
495
+ IHEAP_ID jj, j1, j2, end=(H->siz-1)/2;
496
+ while (i < end){
497
+ j1 = i*2+1; j2 = j1+1;
498
+ jj = IHEAP_compare (H, H->v[j1], H->v[j2])? j1: j2;
499
+ if ( IHEAP_compare (H, j, H->v[jj])) goto END;
500
+ H->v[i] = H->v[jj];
501
+ i = jj;
502
+ }
503
+ if (i == end && (H->siz&1)==0){
504
+ jj = i*2+1;
505
+ if ( !IHEAP_compare (H, H->v[jj], j)){ H->v[i] = H->v[jj]; i = jj; }
506
+ }
507
+ END:;
508
+ H->v[i] = j;
509
+ return (i);
510
+ }
511
+
512
+
513
+
514
+ /* change the value of i-th node to j */
515
+ IHEAP_ID IHEAP_chg (IHEAP *H, IHEAP_ID i, IHEAP_ID j){
516
+ #ifdef ERROR_CHECK
517
+ if ( i<0 || i>H->siz ) error_num ("index is out of range", i, EXIT);
518
+ #endif
519
+ if ( H->end <= 0 ) return (IHEAP_ID_HUGE);
520
+ if ( IHEAP_compare (H, j, H->v[i]) ) return (IHEAP_dec (H, i, j));
521
+ else return (IHEAP_inc (H, i, j));
522
+ }
523
+
524
+ /* insert/delete an element */
525
+ IHEAP_ID IHEAP_ins (IHEAP *H, IHEAP_ID j){
526
+ H->siz++;
527
+ if (H->siz >= H->end){ H->end = MAX(H->end*2, H->siz+1); realloc2 (H->v, H->end, exit(1));}
528
+ return (IHEAP_dec (H, H->siz-1, j));
529
+ }
530
+ void IHEAP_rm (IHEAP *H, IHEAP_ID i){
531
+ H->siz--;
532
+ IHEAP_chg (H, i, H->v[H->siz]);
533
+ H->v[H->siz] = i; // preserve the deleted cell
534
+ }
535
+
536
+ /* extract the minimum element from H */
537
+ IHEAP_ID IHEAP_ext_min (IHEAP *H){
538
+ IHEAP_ID j = H->v[0];
539
+ H->siz--;
540
+ IHEAP_inc (H, 0, H->v[H->siz]);
541
+ H->v[H->siz] = j; // preserve the deleted cell
542
+ return (j);
543
+ }
544
+
545
+ #endif