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,171 @@
1
+ /* library for sparse vector */
2
+ /* Takeaki Uno 27/Dec/2008 */
3
+
4
+ #ifndef _vec_h_
5
+ #define _vec_h_
6
+
7
+ //#define USE_MATH
8
+
9
+ #include"math.h"
10
+ #include"queue.h"
11
+
12
+ #ifndef SVEC_VAL
13
+ #ifdef SVEC_VAL_INT
14
+ #define SVEC_VAL int
15
+ #define SVEC_VAL2 LONG
16
+ #define SVEC_VAL_END INTHUGE
17
+ #define SVEC_VAL2_END LONGHUGE
18
+ #define SVEC_VALF "%d"
19
+ #else
20
+ #define SVEC_VAL double
21
+ #define SVEC_VAL2 double
22
+ #define SVEC_VAL_END DOUBLEHUGE
23
+ #define SVEC_VAL2_END DOUBLEHUGE
24
+ #define SVEC_VALF "%f"
25
+ #endif
26
+ #endif
27
+
28
+ #define VEC_LOAD_BIN 16777216 // read binary file
29
+ #define VEC_LOAD_BIN2 33554432 // read binary file with 2byte for each number
30
+ #define VEC_LOAD_BIN4 67108864 // read binary file with 4byte for each number
31
+ #define VEC_LOAD_CENTERIZE 134217728 // read binary file, and minus the half(128) from each number
32
+ #define VEC_NORMALIZE 268435456 // read binary file, and minus the half(128) from each number
33
+
34
+ /* matrix */
35
+ typedef struct {
36
+ unsigned char type; // mark to identify type of the structure
37
+ char *fname; // input file name
38
+ int flag; // flag
39
+
40
+ VEC *v;
41
+ VEC_ID end;
42
+ VEC_ID t;
43
+ VEC_VAL *buf, *buf2;
44
+ VEC_ID clms;
45
+ size_t eles;
46
+ VEC_VAL *buf_org, *buf2_org;
47
+ } MAT;
48
+
49
+ /* sparse vector, element */
50
+ typedef struct {
51
+ QUEUE_ID i;
52
+ SVEC_VAL a;
53
+ } SVEC_ELE;
54
+
55
+ /* sparse vector, vector */
56
+ typedef struct {
57
+ unsigned char type; // mark to identify type of the structure
58
+ SVEC_ELE *v;
59
+ VEC_ID end;
60
+ VEC_ID t;
61
+ } SVEC;
62
+
63
+ /* sparse vector, matrix */
64
+ typedef struct {
65
+ unsigned char type; // mark to identify type of the structure
66
+ char *fname; // input file name
67
+ int flag; // flag
68
+
69
+ SVEC *v;
70
+ VEC_ID end;
71
+ VEC_ID t;
72
+ SVEC_ELE *buf, *buf2;
73
+ VEC_ID clms;
74
+ size_t eles, ele_end;
75
+ } SMAT;
76
+
77
+ /* set family */
78
+ typedef struct {
79
+ unsigned char type; // mark to identify type of the structure
80
+ char *fname; // input file name
81
+ int flag; // flag
82
+
83
+ QUEUE *v;
84
+ VEC_ID end;
85
+ VEC_ID t;
86
+ QUEUE_INT *buf, *buf2;
87
+ VEC_ID clms;
88
+ size_t eles, ele_end;
89
+ WEIGHT *cw, *rw, **w, *wbuf;
90
+ int unit;
91
+ char *wfname, *cwfname, *rwfname; // weight file name
92
+ } SETFAMILY;
93
+
94
+ #define INIT_SETFAMILY_ {TYPE_SETFAMILY,NULL,0,NULL,0,0,NULL,NULL,0,0,0,NULL,NULL,NULL,NULL,sizeof(QUEUE_INT),NULL,NULL,NULL}
95
+
96
+ extern MAT INIT_MAT;
97
+ extern SVEC INIT_SVEC;
98
+ extern SMAT INIT_SMAT;
99
+ extern SETFAMILY INIT_SETFAMILY;
100
+
101
+ QSORT_TYPE_HEADER (SVEC_VAL, SVEC_VAL)
102
+ QSORT_TYPE_HEADER (SVEC_VAL2, SVEC_VAL2)
103
+
104
+ #define ARY_QUEUE_INPRO(f,U,V) do{(f)=0;FLOOP(common_QUEUE_ID, 0, (QUEUE_ID)(U).t)(f)+=(V)[(U).v[common_QUEUE_ID]];}while(0)
105
+ #define ARY_SVEC_INPRO(f,U,V) do{(f)=0;FLOOP(common_VEC_ID, 0, (VEC_ID)(U).t)(f)+=((double)(U).v[common_VEC_ID].a)*(V)[(U).v[common_VEC_ID].i];}while(0)
106
+
107
+ /* terminate routine for VEC */
108
+ void VEC_end (VEC *V);
109
+ void MAT_end (MAT *M);
110
+ void SVEC_end (SVEC *V);
111
+ void SMAT_end (SMAT *M);
112
+ void SETFAMILY_end (SETFAMILY *M);
113
+
114
+ /* allocate memory according to rows and rowt */
115
+ void VEC_alloc (VEC *V, VEC_ID clms);
116
+ void MAT_alloc (MAT *M, VEC_ID rows, VEC_ID clms);
117
+ void SVEC_alloc (SVEC *V, VEC_ID end);
118
+ void SMAT_alloc (SMAT *M, VEC_ID rows, VEC_ID *rowt, VEC_ID clms, size_t eles);
119
+ void SETFAMILY_alloc (SETFAMILY *M, VEC_ID rows, VEC_ID *rowt, VEC_ID clms, size_t eles);
120
+ void SETFAMILY_alloc_weight (SETFAMILY *M);
121
+
122
+ /* count/read the number in file for MAT */
123
+ /* if *rows>0, only read count the numbers in a row, for the first scan. */
124
+ void MAT_load_bin (MAT *M, FILE2 *fp, int unit);
125
+ void MAT_file_load (MAT *M, FILE2 *fp);
126
+ void MAT_load (MAT *M);
127
+ void SMAT_load (SMAT *M);
128
+ void SETFAMILY_load (SETFAMILY *M);
129
+ void SETFAMILY_load_weight (SETFAMILY *M);
130
+ void SETFAMILY_load_row_weight (SETFAMILY *M);
131
+ void SETFAMILY_load_column_weight (SETFAMILY *M);
132
+
133
+ void MAT_print (FILE *fp, MAT *M);
134
+ void SVEC_print (FILE *fp, SVEC *M);
135
+ void SMAT_print (FILE *fp, SMAT *M);
136
+ void SETFAMILY_print (FILE *fp, SETFAMILY *M);
137
+ void SETFAMILY_print_weight (FILE *fp, SETFAMILY *M);
138
+
139
+
140
+ /* norm, normalization **************************/
141
+ double SVEC_norm (SVEC *V);
142
+ void SVEC_normalize (SVEC *V);
143
+
144
+ /* inner product **************************/
145
+ SVEC_VAL2 SVEC_inpro (SVEC *V1, SVEC *V2);
146
+
147
+ /** Euclidean distance routines *********************************/
148
+ double VEC_eucdist (VEC *V1, VEC *V2);
149
+ double SVEC_eucdist (SVEC *V1, SVEC *V2);
150
+ double VEC_SVEC_eucdist (VEC *V1, SVEC *V2);
151
+ double QUEUE_eucdist (QUEUE *Q1, QUEUE *Q2);
152
+ double VEC_QUEUE_eucdist (VEC *V, QUEUE *Q);
153
+
154
+ void VEC_rand_gaussian (VEC *V);
155
+
156
+ double VEC_linfdist (VEC *V1, VEC *V2);
157
+
158
+ /* compute the inner product, Euclidean distance for multi vector */
159
+ double MVEC_norm (void *V);
160
+ double MVEC_inpro (void *V, void *U);
161
+ double MVEC_double_inpro (void *V, double *p);
162
+ double MVEC_eucdist (void *V, void *U);
163
+
164
+ /* compute the inner product, euclidean distance for i,jth vector */
165
+ double MMAT_inpro_ij (void *M, int i, int j);
166
+ double MMAT_double_inpro_i (void *M, int i, double *p);
167
+ double MMAT_eucdist_ij (void *M, int i, int j);
168
+ double MMAT_norm_i (void *M, int i);
169
+
170
+
171
+ #endif
@@ -0,0 +1,20 @@
1
+ require "rubygems"
2
+ require "mkmf"
3
+
4
+ unless have_library("kgmod3")
5
+ puts("need libkgmod.")
6
+ puts("refer https://github.com/nysol/mcmd")
7
+ exit 1
8
+ end
9
+
10
+
11
+ cp = "$(srcdir)"
12
+
13
+ $CFLAGS = " -O3 -Wall -I. -I#{cp}/src -DB_STATIC -D_NO_MAIN_ -DLINE -fPIC -Wno-error=format-security"
14
+ $CPPFLAGS = " -O3 -Wall -I. -I#{cp}/src -DB_STATIC -D_NO_MAIN_ -DLINE -fPIC -Wno-error=format-security"
15
+ $CXXFLAGS = " -O3 -Wall -I. -I#{cp}/src -DB_STATIC -D_NO_MAIN_ -DLINE -fPIC -Wno-error=format-security"
16
+
17
+ $LOCAL_LIBS += " -lstdc++ -lkgmod3"
18
+
19
+ create_makefile("nysol/lcmseq0run")
20
+
@@ -0,0 +1,59 @@
1
+ #include <stdio.h>
2
+ #include <stdlib.h>
3
+ #include <unistd.h>
4
+ #include <fcntl.h>
5
+ #include <sys/stat.h>
6
+ #include <string>
7
+ #include <ruby.h>
8
+
9
+ #include "src/lcm_seq_zero.c"
10
+
11
+
12
+ #include <kgMethod.h>
13
+
14
+ extern "C" {
15
+ void Init_lcmseq0run(void);
16
+ }
17
+
18
+
19
+ VALUE lcmseq0(VALUE self,VALUE argvV){
20
+
21
+ string argstr=RSTRING_PTR(argvV);
22
+ vector<char *> opts = kglib::splitToken(const_cast<char*>(argstr.c_str()), ' ',true);
23
+
24
+ // 引数文字列へのポインタの領域はここでauto変数に確保する
25
+ kglib::kgAutoPtr2<char*> argv;
26
+ char** vv;
27
+ try{
28
+ argv.set(new char*[opts.size()+1]);
29
+ vv = argv.get();
30
+ }catch(...){
31
+ rb_raise(rb_eRuntimeError,"memory allocation error");
32
+ }
33
+
34
+ // vv配列0番目はコマンド名
35
+ vv[0]=const_cast<char*>("lcmseq");
36
+
37
+ size_t vvSize;
38
+ for(vvSize=0; vvSize<opts.size(); vvSize++){
39
+ vv[vvSize+1] = opts.at(vvSize);
40
+ }
41
+ vvSize+=1;
42
+
43
+ LCMseq_main(vvSize,vv);
44
+ return Qtrue;
45
+ }
46
+
47
+
48
+
49
+ // -----------------------------------------------------------------------------
50
+ // ruby Mcsvin クラス init
51
+ // -----------------------------------------------------------------------------
52
+ void Init_lcmseq0run(void)
53
+ {
54
+ // モジュール定義:MCMD::xxxxの部分
55
+ VALUE mtake=rb_define_module("TAKE");
56
+ rb_define_module_function(mtake,"run_lcmseq_zero" , (VALUE (*)(...))lcmseq0,1);
57
+ }
58
+
59
+
@@ -0,0 +1,216 @@
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
+
18
+ QSORT_TYPE (AHEAP_KEY, AHEAP_KEY)
19
+ QSORT_TYPE (AHEAP_ID, AHEAP_ID)
20
+ AHEAP INIT_AHEAP = {TYPE_AHEAP,NULL,0,0};
21
+
22
+ /* allocate memory */
23
+ void AHEAP_alloc (AHEAP *H, AHEAP_ID num){
24
+ AHEAP_ID i;
25
+ #ifdef ERROR_CHECK
26
+ if ( num<0 ) error_num ("size is out of range", num, EXIT);
27
+ #endif
28
+ *H = INIT_AHEAP;
29
+ if ( num>0 ) malloc2 (H->v, num*2, EXIT);
30
+ H->end = num;
31
+ ARY_FILL (H->v, 0, num*2, AHEAP_KEYHUGE);
32
+ for (i=0 ; i<num-1 ; i=i*2+1);
33
+ H->base = i - num + 1;
34
+ }
35
+
36
+ /* termination */
37
+ void AHEAP_end (AHEAP *H){
38
+ free2 (H->v);
39
+ *H = INIT_AHEAP;
40
+ }
41
+
42
+ /* return the index of the leaf having the minimum key among the descendants
43
+ of the given node i. If several leaves with the smallest key are there,
44
+ return the minimum index among them if f=0, maximum index if f=1, and
45
+ random choice if f=2 */
46
+ /* random choice version. choose one child to go down randomly for each node,
47
+ thus it is not uniformly random */
48
+ /* node_ returns the ID of leaf */
49
+ AHEAP_ID AHEAP_findmin_node_ (AHEAP *H, AHEAP_ID i, int f){
50
+ while ( i < H->end-1 ){
51
+ if ( H->v[i*2+1] == H->v[i] )
52
+ if ( H->v[i*2+2] == H->v[i] )
53
+ if ( f == 2 ) i = i*2 + 1 + rand()%2;
54
+ else i = i*2+1+f;
55
+ else i = i*2+1;
56
+ else i = i*2+2;
57
+ }
58
+ return (i);
59
+ }
60
+ AHEAP_ID AHEAP_findmin_node (AHEAP *H, AHEAP_ID i, int f){
61
+ if ( H->end <= 0 ) return (-1);
62
+ return (AHEAP_IDX(*H, AHEAP_findmin_node_ (H, i, f)));
63
+ }
64
+ AHEAP_ID AHEAP_findmin_head (AHEAP *H){ return (AHEAP_findmin_node (H, 0, 0) ); }
65
+ AHEAP_ID AHEAP_findmin_tail (AHEAP *H){ return (AHEAP_findmin_node (H, 0, 1) ); }
66
+ AHEAP_ID AHEAP_findmin_rnd (AHEAP *H){ return (AHEAP_findmin_node (H, 0, 2) ); }
67
+
68
+ /* return the index of the leaf having smaller value than a among the
69
+ descendants of the given node i. If several leaves with the smallest key
70
+ are there, return the minimum index among them if f=0, maximum index if f=1,
71
+ and random choice if f=2 */
72
+ AHEAP_ID AHEAP_findlow_node (AHEAP *H, AHEAP_KEY a, AHEAP_ID i, int f){
73
+ if ( H->end == 0 ) return (-1);
74
+ if ( H->v[0] > a ) return (-1);
75
+ while ( i < H->end-1 ){
76
+ if ( f == 2 ) {
77
+ if ( H->v[i*2+1] <= a )
78
+ if ( H->v[i*2+2] <= a ) i = i*2 + 1 + rand()%2;
79
+ else i = i*2+1;
80
+ else i = i*2+2;
81
+ } else if ( H->v[i*2+1] <= a ) i = i*2+1+f; else i = i*2+2-f;
82
+ }
83
+ return (AHEAP_IDX(*H, i) );
84
+ }
85
+ AHEAP_ID AHEAP_findlow_head (AHEAP *H, AHEAP_KEY a){ return (AHEAP_findlow_node (H, a, 0, 0) ); }
86
+ AHEAP_ID AHEAP_findlow_tail (AHEAP *H, AHEAP_KEY a){ return (AHEAP_findlow_node (H, a, 0, 1) ); }
87
+ AHEAP_ID AHEAP_findlow_rnd (AHEAP *H, AHEAP_KEY a){ return (AHEAP_findlow_node (H, a, 0, 2) ); }
88
+
89
+ /* return the index of the leaf having smaller value than a next/previous to
90
+ leaf i. return -1 if such a leaf does not exist */
91
+ AHEAP_ID AHEAP_findlow_nxt (AHEAP *H, AHEAP_ID i, AHEAP_KEY a){
92
+ if ( H->end == 0 ) return (-1);
93
+ if ( i<0 || i>= H->end ) return ( AHEAP_findlow_head (H, a));
94
+ for (i=AHEAP_LEAF(*H,i); i>0 ; i=(i-1)/2){
95
+ /* i is the child of smaller index, and the key of the sibling of i is less than a */
96
+ if ( i%2 == 1 && H->v[i+1] <= a ) return (AHEAP_findlow_node (H, a, i+1, 0) );
97
+ }
98
+ return (-1);
99
+ }
100
+ AHEAP_ID AHEAP_findlow_prv (AHEAP *H, AHEAP_ID i, AHEAP_KEY a){
101
+ if ( H->end == 0 ) return (-1);
102
+ if ( i<0 || i>= H->end ) return ( AHEAP_findlow_head (H, a));
103
+ for (i=AHEAP_LEAF(*H,i); i>0 ; i=(i-1)/2){
104
+ /* i is the child of larger index, and the key of the sibling of i is less than a */
105
+ if ( i%2 == 0 && H->v[i-1] <= a ) return (AHEAP_findlow_node (H, a, i-1, 1) );
106
+ }
107
+ return (-1);
108
+ }
109
+
110
+ /* change the key of node i to a /Add a to the key of node i, and update heap H */
111
+ void AHEAP_chg (AHEAP *H, AHEAP_ID i, AHEAP_KEY a){
112
+ i = AHEAP_LEAF (*H, i);
113
+ H->v[i] = a;
114
+ AHEAP_update (H, i);
115
+ }
116
+ void AHEAP_add (AHEAP *H, AHEAP_ID i, AHEAP_KEY a){
117
+ i = AHEAP_LEAF (*H, i);
118
+ H->v[i] += a;
119
+ AHEAP_update (H, i);
120
+ }
121
+
122
+ /* update the ancestor of node i */
123
+ void AHEAP_update (AHEAP *H, AHEAP_ID i){
124
+ AHEAP_ID j;
125
+ AHEAP_KEY a = H->v[i];
126
+ while ( i>0 ){
127
+ j = i - 1 + (i%2)*2; /* j = the sibling of i */
128
+ i = (i-1) / 2;
129
+ if ( H->v[j] < a ) a = H->v[j];
130
+ if ( a == H->v[i] ) break;
131
+ H->v[i] = a;
132
+ }
133
+ }
134
+
135
+ /* find the leaf with the minimum key value among the leaves having index
136
+ smaller/larger than i, or between i and j */
137
+ AHEAP_ID AHEAP_upper_min (AHEAP *H, AHEAP_ID i){
138
+ AHEAP_ID fi=0, j = AHEAP_LEAF (*H, H->end - 1);
139
+ AHEAP_KEY fm = AHEAP_KEYHUGE;
140
+ if ( i == 0 ) return (AHEAP_findmin_head (H) );
141
+ i = AHEAP_LEAF (*H, i-1);
142
+ while ( i != j ){
143
+ if ( i%2 ){ /* if i is the child with smaller index */
144
+ if ( fm > H->v[i+1] ){
145
+ fm = H->v[i+1];
146
+ fi = i+1;
147
+ }
148
+ }
149
+ i = (i-1)/2;
150
+ if ( j == i ) break; /* stop if the right pointer and the left pointer are the same */
151
+ j = (j-1)/2;
152
+ }
153
+ while ( fi < H->end-1 ) fi = fi*2 + (H->v[fi*2+1]<=fm?1:2);
154
+ return ( AHEAP_IDX(*H, fi) );
155
+ }
156
+ AHEAP_ID AHEAP_lower_min (AHEAP *H, AHEAP_ID i){
157
+ AHEAP_ID fi=0, j = AHEAP_LEAF (*H, 0);
158
+ AHEAP_KEY fm = AHEAP_KEYHUGE;
159
+ if ( i == H->end-1 ) return (AHEAP_findmin_head (H) );
160
+ i = AHEAP_LEAF (*H, i+1);
161
+ while ( i != j ){
162
+ if ( i%2 == 0 ){ /* if i is the child of larger index */
163
+ if ( fm > H->v[i-1] ){
164
+ fm = H->v[i-1];
165
+ fi = i-1;
166
+ }
167
+ }
168
+ j = (j-1)/2;
169
+ if ( j == i ) break; /* stop if the right pointer and the left pointer are the same */
170
+ i = (i-1)/2;
171
+ }
172
+ while ( fi < H->end-1 ) fi = fi*2 + (H->v[fi*2+1]<=fm?1:2);
173
+ return (AHEAP_IDX(*H, fi) );
174
+ }
175
+
176
+ /* find the index having the minimum among given two indices */
177
+ AHEAP_ID AHEAP_interval_min (AHEAP *H, AHEAP_ID i, AHEAP_ID j){
178
+ AHEAP_ID fi=0;
179
+ AHEAP_KEY fm = AHEAP_KEYHUGE;
180
+ if ( i == 0 ) return (AHEAP_lower_min (H, j) );
181
+ if ( j == H->end-1 ) return (AHEAP_upper_min (H, i) );
182
+ i = AHEAP_LEAF (*H, i-1);
183
+ j = AHEAP_LEAF (*H, j+1);
184
+ while ( i != j && i != j-1 ){
185
+ if ( i%2 ){ /* if i is the child of smaller index */
186
+ if ( fm > H->v[i+1] ){
187
+ fm = H->v[i+1];
188
+ fi = i+1;
189
+ }
190
+ }
191
+ i = (i-1)/2;
192
+ if ( j == i || j == i+1 ) break; /* stop if the right pointer and the left pointer are the same */
193
+ if ( j%2 == 0 ){ /* if j is the child of larger index */
194
+ if ( fm > H->v[j-1] ){
195
+ fm = H->v[j-1];
196
+ fi = j-1;
197
+ }
198
+ }
199
+ j = (j-1)/2;
200
+ }
201
+ while ( fi < H->end-1 )
202
+ fi = fi*2 + (H->v[fi*2+1] <= fm?1:2);
203
+ return (AHEAP_IDX(*H, fi) );
204
+ }
205
+
206
+ /* print heap keys according to the structure of the heap */
207
+ void AHEAP_print (AHEAP *H){
208
+ AHEAP_ID i, j=1;
209
+ while ( j<=H->end*2-1 ){
210
+ FLOOP (i, j-1, MIN(j, H->end)*2-1) printf (AHEAP_KEYF ",", H->v[i] );
211
+ printf ("\n");
212
+ j = j*2;
213
+ }
214
+ }
215
+
216
+ #endif
@@ -0,0 +1,111 @@
1
+ /*
2
+ array-based simple heap (fixed 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
+ /* bench mark
14
+ PentiumIII 500MHz, Memory 256MB Linux
15
+ values 0-1,000,000 : set & del & get 1,000,000 times
16
+ 2.55sec
17
+
18
+ # rotation == 1/5 per 1 set/del
19
+
20
+ *** simple array ***
21
+ value 0-1,000,000 set & set & set 1,000,000 times,
22
+ 0.88sec
23
+ */
24
+
25
+ #ifndef _aheap_h_
26
+ #define _aheap_h_
27
+
28
+ #include"stdlib2.h"
29
+
30
+ #ifndef AHEAP_KEY
31
+ #ifdef AHEAP_KEY_DOUBLE
32
+ #define AHEAP_KEY double
33
+ #define AHEAP_KEYHUGE DOUBLEHUGE
34
+ #define AHEAP_KEYF "%f"
35
+ #elif defined(AHEAP_KEY_WEIGHT)
36
+ #define AHEAP_KEY WEIGHT
37
+ #define AHEAP_KEYHUGE WEIGHTHUGE
38
+ #define AHEAP_KEYF WEIGHTF
39
+ #else
40
+ #define AHEAP_KEY int
41
+ #define AHEAP_KEYHUGE INTHUGE
42
+ #define AHEAP_KEYF "%d"
43
+ #endif
44
+ #endif
45
+
46
+ #ifndef AHEAP_ID
47
+ #define AHEAP_ID int
48
+ #define AHEAP_ID_END INTHUGE
49
+ #define AHEAP_IDF "%d"
50
+ #endif
51
+
52
+ #define AHEAP_IDX(H,i) (((i)+1-(H).base)%(H).end)
53
+ #define AHEAP_LEAF(H,i) (((i)+(H).base)%(H).end+(H).end-1)
54
+ #define AHEAP_H(H,i) (H).v[(((i)+(H).base)%(H).end+(H).end-1)]
55
+
56
+ typedef struct {
57
+ unsigned char type;
58
+ AHEAP_KEY *v; /* array for heap key */
59
+ int end; /* the number of maximum elements */
60
+ int base; /* the constant for set 0 to the leftmost leaf */
61
+ } AHEAP;
62
+
63
+ QSORT_TYPE_HEADER (AHEAP_KEY, AHEAP_KEY)
64
+ QSORT_TYPE_HEADER (AHEAP_ID, AHEAP_ID)
65
+ extern AHEAP INIT_AHEAP;
66
+
67
+ /* initialization. allocate memory for H and fill it by +infinity */
68
+ void AHEAP_alloc (AHEAP *H, int num);
69
+ void AHEAP_end (AHEAP *H);
70
+
71
+ /* return the index of the leaf having the minimum key among the descendants
72
+ of the given node i. If several leaves with the smallest key are there,
73
+ return the minimum index among them if f=0, maximum index if f=1, and
74
+ random choice if f=2 */
75
+ AHEAP_ID AHEAP_findmin_node_ (AHEAP *H, AHEAP_ID i, int f);
76
+ AHEAP_ID AHEAP_findmin_node (AHEAP *H, AHEAP_ID i, int f);
77
+ AHEAP_ID AHEAP_findmin_head (AHEAP *H);
78
+ AHEAP_ID AHEAP_findmin_tail (AHEAP *H);
79
+ AHEAP_ID AHEAP_findmin_rnd (AHEAP *H);
80
+
81
+ /* return the index of the leaf having smaller value than a among the
82
+ descendants of the given node i. If several leaves with the smallest key
83
+ are there, return the minimum index among them if f=0, maximum index if f=1,
84
+ and random choice if f=2 */
85
+ AHEAP_ID AHEAP_findlow_node (AHEAP *H, AHEAP_KEY a, AHEAP_ID i, int f);
86
+ AHEAP_ID AHEAP_findlow_head (AHEAP *H, AHEAP_KEY a);
87
+ AHEAP_ID AHEAP_findlow_tail (AHEAP *H, AHEAP_KEY a);
88
+ AHEAP_ID AHEAP_findlow_rnd (AHEAP *H, AHEAP_KEY a);
89
+
90
+ /* return the index of the leaf having smaller value than a next/previous to
91
+ leaf i. return -1 if such a leaf does not exist */
92
+ AHEAP_ID AHEAP_findlow_nxt (AHEAP *H, AHEAP_ID i, AHEAP_KEY a);
93
+ AHEAP_ID AHEAP_findlow_prv (AHEAP *H, AHEAP_ID i, AHEAP_KEY a);
94
+
95
+ /* change the key of node i to a /Add a to the key of node i, and update heap H */
96
+ void AHEAP_chg (AHEAP *H, AHEAP_ID i, AHEAP_KEY a);
97
+ void AHEAP_add (AHEAP *H, AHEAP_ID i, AHEAP_KEY a);
98
+
99
+ /* update the ancestor of node i */
100
+ void AHEAP_update (AHEAP *H, AHEAP_ID i);
101
+
102
+ /* find the leaf with the minimum key value among the leaves having index
103
+ smaller/larger than i, or between i and j */
104
+ AHEAP_ID AHEAP_upper_min (AHEAP *H, AHEAP_ID i);
105
+ AHEAP_ID AHEAP_lower_min (AHEAP *H, AHEAP_ID i);
106
+ AHEAP_ID AHEAP_interval_min (AHEAP *H, AHEAP_ID i, AHEAP_ID j);
107
+
108
+ /* print heap keys according to the structure of the heap */
109
+ void AHEAP_print (AHEAP *H);
110
+
111
+ #endif
@@ -0,0 +1,92 @@
1
+ /*
2
+ blocked memory allocation library
3
+ 12/Mar/2002 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 _base_c_
14
+ #define _base_c_
15
+
16
+ #include"base.h"
17
+
18
+ BASE INIT_BASE = {TYPE_BASE,NULL,0,0,0,0,-1,NULL};
19
+
20
+ /* initialization, and allocate memory for header */
21
+ void BASE_alloc (BASE *B, int unit, int block_siz){
22
+ *B = INIT_BASE;
23
+ B->dellist = B;
24
+ B->unit = unit;
25
+ B->block_siz = block_siz;
26
+ B->num = block_siz;
27
+ B->block_num = -1;
28
+ calloc2 (B->base, 20, EXIT);
29
+ B->block_end = 20;
30
+ }
31
+
32
+ /* termination */
33
+ void BASE_end (BASE *B){
34
+ int i;
35
+ FLOOP (i, 0, B->block_end) free2 (B->base[i]);
36
+ free2 (B->base);
37
+ *B = INIT_BASE;
38
+ }
39
+
40
+ /* return pointer to the cell corresponding to the given index */
41
+ void *BASE_pnt (BASE *B, size_t i){
42
+ return ( B->base[i/BASE_BLOCK] + (i%BASE_BLOCK)*B->unit);
43
+ }
44
+ /* return index corresponding to the given pointer */
45
+ size_t BASE_index (BASE *B, void *x){
46
+ size_t i;
47
+ FLOOP (i, 0, (size_t)(B->block_end+1)){
48
+ if ( ((char*)x)>= B->base[i] && ((char*)x)<=B->base[i]+B->unit*BASE_BLOCK )
49
+ return ( i*BASE_BLOCK + ((size_t)(((char *)x) - B->base[i])) / B->unit);
50
+ }
51
+ return (0);
52
+ }
53
+
54
+ /* increment the current memory block pointer and (re)allcate memory if necessary */
55
+ void *BASE_get_memory (BASE *B, int i){
56
+ B->num += i;
57
+ if ( B->num >= B->block_siz ){ /* if reach to the end of base array */
58
+ B->num = i; /* allocate one more base array, and increment the counter */
59
+ B->block_num++;
60
+ reallocx(B->base, B->block_end, B->block_num, NULL, EXIT0);
61
+ if ( B->base[B->block_num] == NULL )
62
+ malloc2 (B->base[B->block_num], B->block_siz*B->unit, EXIT0);
63
+ return (B->base[B->block_num]);
64
+ }
65
+ return (B->base[B->block_num] + (B->num-i)*B->unit);
66
+ }
67
+
68
+
69
+ /* allocate new cell */
70
+ void *BASE_new (BASE *B){
71
+ char *x;
72
+
73
+ /* use deleted cell if it exists */
74
+ if ( B->dellist != ((void *)B) ){
75
+ x = (char *)B->dellist; /* return the deleted cell */
76
+ B->dellist = (void *)(*((char **)x)); /* increment the head of the list */
77
+ } else {
78
+ /* take a new cell from the base array if no deleted one exists */
79
+ x = (char *)BASE_get_memory (B, 1);
80
+ }
81
+ return (x);
82
+ }
83
+
84
+ /* delete one cell. (add to the deleted list) */
85
+ void BASE_del (BASE *B, void *x){
86
+ *((void **)x) = B->dellist;
87
+ B->dellist = x;
88
+ }
89
+
90
+ #endif
91
+
92
+