ruby-minigraph 0.0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +62 -0
- data/ext/Rakefile +56 -0
- data/ext/cmappy/cmappy.c +7 -0
- data/ext/cmappy/cmappy.h +8 -0
- data/ext/minigraph/LICENSE.txt +23 -0
- data/ext/minigraph/Makefile +66 -0
- data/ext/minigraph/NEWS.md +317 -0
- data/ext/minigraph/README.md +207 -0
- data/ext/minigraph/algo.c +194 -0
- data/ext/minigraph/algo.h +33 -0
- data/ext/minigraph/asm-call.c +147 -0
- data/ext/minigraph/bseq.c +133 -0
- data/ext/minigraph/bseq.h +76 -0
- data/ext/minigraph/cal_cov.c +139 -0
- data/ext/minigraph/doc/example1.png +0 -0
- data/ext/minigraph/doc/example2.png +0 -0
- data/ext/minigraph/doc/examples.graffle +0 -0
- data/ext/minigraph/format.c +241 -0
- data/ext/minigraph/galign.c +140 -0
- data/ext/minigraph/gchain1.c +532 -0
- data/ext/minigraph/gcmisc.c +223 -0
- data/ext/minigraph/gfa-aug.c +260 -0
- data/ext/minigraph/gfa-base.c +526 -0
- data/ext/minigraph/gfa-bbl.c +372 -0
- data/ext/minigraph/gfa-ed.c +617 -0
- data/ext/minigraph/gfa-io.c +395 -0
- data/ext/minigraph/gfa-priv.h +154 -0
- data/ext/minigraph/gfa.h +166 -0
- data/ext/minigraph/ggen.c +182 -0
- data/ext/minigraph/ggen.h +21 -0
- data/ext/minigraph/ggsimple.c +570 -0
- data/ext/minigraph/gmap.c +211 -0
- data/ext/minigraph/index.c +230 -0
- data/ext/minigraph/kalloc.c +224 -0
- data/ext/minigraph/kalloc.h +82 -0
- data/ext/minigraph/kavl.h +414 -0
- data/ext/minigraph/kdq.h +134 -0
- data/ext/minigraph/ketopt.h +116 -0
- data/ext/minigraph/khashl.h +348 -0
- data/ext/minigraph/krmq.h +474 -0
- data/ext/minigraph/kseq.h +256 -0
- data/ext/minigraph/ksort.h +164 -0
- data/ext/minigraph/kstring.h +165 -0
- data/ext/minigraph/kthread.c +159 -0
- data/ext/minigraph/kthread.h +15 -0
- data/ext/minigraph/kvec-km.h +105 -0
- data/ext/minigraph/kvec.h +110 -0
- data/ext/minigraph/lchain.c +441 -0
- data/ext/minigraph/main.c +301 -0
- data/ext/minigraph/map-algo.c +500 -0
- data/ext/minigraph/mgpriv.h +128 -0
- data/ext/minigraph/minigraph.1 +359 -0
- data/ext/minigraph/minigraph.h +176 -0
- data/ext/minigraph/miniwfa.c +834 -0
- data/ext/minigraph/miniwfa.h +95 -0
- data/ext/minigraph/misc/mgutils.js +1451 -0
- data/ext/minigraph/misc.c +12 -0
- data/ext/minigraph/options.c +134 -0
- data/ext/minigraph/shortk.c +251 -0
- data/ext/minigraph/sketch.c +109 -0
- data/ext/minigraph/sys.c +147 -0
- data/ext/minigraph/sys.h +20 -0
- data/ext/minigraph/test/MT-chimp.fa +277 -0
- data/ext/minigraph/test/MT-human.fa +239 -0
- data/ext/minigraph/test/MT-orangA.fa +276 -0
- data/ext/minigraph/test/MT.gfa +19 -0
- data/ext/minigraph/tex/Makefile +13 -0
- data/ext/minigraph/tex/minigraph.bib +676 -0
- data/ext/minigraph/tex/minigraph.tex +986 -0
- data/ext/minigraph/tex/plots/CHM13-f1-90.bb.anno.gp +42 -0
- data/ext/minigraph/tex/plots/CHM13-f1-90.bb.anno.tbl +13 -0
- data/ext/minigraph/tex/plots/CHM13-f1-90.bb.mini-inter-none.win.gp +269 -0
- data/ext/minigraph/tex/plots/CHM13-f1-90.bb.mini-inter-none.win.sh +7 -0
- data/ext/minigraph/tex/plots/CHM13v1.cen.bed +23 -0
- data/ext/minigraph/tex/plots/CHM13v1.size +23 -0
- data/ext/minigraph/tex/plots/anno2tbl.js +40 -0
- data/ext/minigraph/tex/plots/bedutils.js +367 -0
- data/ext/minigraph/tex/plots/chr-plot.js +130 -0
- data/ext/minigraph/tex/plots/gen-anno.mak +24 -0
- data/ext/minigraph.patch +21 -0
- data/lib/minigraph/ffi/constants.rb +230 -0
- data/lib/minigraph/ffi/functions.rb +70 -0
- data/lib/minigraph/ffi/mappy.rb +8 -0
- data/lib/minigraph/ffi.rb +27 -0
- data/lib/minigraph/version.rb +5 -0
- data/lib/minigraph.rb +72 -0
- metadata +159 -0
@@ -0,0 +1,256 @@
|
|
1
|
+
/* The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2008, 2009, 2011 Attractive Chaos <attractor@live.co.uk>
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
20
|
+
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
21
|
+
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
22
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
|
+
SOFTWARE.
|
24
|
+
*/
|
25
|
+
|
26
|
+
/* Last Modified: 05MAR2012 */
|
27
|
+
|
28
|
+
#ifndef AC_KSEQ_H
|
29
|
+
#define AC_KSEQ_H
|
30
|
+
|
31
|
+
#include <ctype.h>
|
32
|
+
#include <string.h>
|
33
|
+
#include <stdlib.h>
|
34
|
+
|
35
|
+
#define KS_SEP_SPACE 0 // isspace(): \t, \n, \v, \f, \r
|
36
|
+
#define KS_SEP_TAB 1 // isspace() && !' '
|
37
|
+
#define KS_SEP_LINE 2 // line separator: "\n" (Unix) or "\r\n" (Windows)
|
38
|
+
#define KS_SEP_MAX 2
|
39
|
+
|
40
|
+
#ifndef klib_unused
|
41
|
+
#if (defined __clang__ && __clang_major__ >= 3) || (defined __GNUC__ && __GNUC__ >= 3)
|
42
|
+
#define klib_unused __attribute__ ((__unused__))
|
43
|
+
#else
|
44
|
+
#define klib_unused
|
45
|
+
#endif
|
46
|
+
#endif /* klib_unused */
|
47
|
+
|
48
|
+
#define __KS_TYPE(type_t) \
|
49
|
+
typedef struct __kstream_t { \
|
50
|
+
int begin, end; \
|
51
|
+
int is_eof:2, bufsize:30; \
|
52
|
+
type_t f; \
|
53
|
+
unsigned char *buf; \
|
54
|
+
} kstream_t;
|
55
|
+
|
56
|
+
#define ks_eof(ks) ((ks)->is_eof && (ks)->begin >= (ks)->end)
|
57
|
+
#define ks_rewind(ks) ((ks)->is_eof = (ks)->begin = (ks)->end = 0)
|
58
|
+
|
59
|
+
#define __KS_BASIC(SCOPE, type_t, __bufsize) \
|
60
|
+
SCOPE kstream_t *ks_init(type_t f) \
|
61
|
+
{ \
|
62
|
+
kstream_t *ks = (kstream_t*)calloc(1, sizeof(kstream_t)); \
|
63
|
+
ks->f = f; ks->bufsize = __bufsize; \
|
64
|
+
ks->buf = (unsigned char*)malloc(__bufsize); \
|
65
|
+
return ks; \
|
66
|
+
} \
|
67
|
+
SCOPE void ks_destroy(kstream_t *ks) \
|
68
|
+
{ \
|
69
|
+
if (!ks) return; \
|
70
|
+
free(ks->buf); \
|
71
|
+
free(ks); \
|
72
|
+
}
|
73
|
+
|
74
|
+
#define __KS_INLINED(__read) \
|
75
|
+
static klib_unused inline int ks_getc(kstream_t *ks) \
|
76
|
+
{ \
|
77
|
+
if (ks->is_eof && ks->begin >= ks->end) return -1; \
|
78
|
+
if (ks->begin >= ks->end) { \
|
79
|
+
ks->begin = 0; \
|
80
|
+
ks->end = __read(ks->f, ks->buf, ks->bufsize); \
|
81
|
+
if (ks->end < ks->bufsize) ks->is_eof = 1; \
|
82
|
+
if (ks->end == 0) return -1; \
|
83
|
+
} \
|
84
|
+
return (int)ks->buf[ks->begin++]; \
|
85
|
+
} \
|
86
|
+
static inline int ks_getuntil(kstream_t *ks, int delimiter, kstring_t *str, int *dret) \
|
87
|
+
{ return ks_getuntil2(ks, delimiter, str, dret, 0); }
|
88
|
+
|
89
|
+
#ifndef KSTRING_T
|
90
|
+
#define KSTRING_T kstring_t
|
91
|
+
typedef struct __kstring_t {
|
92
|
+
unsigned l, m;
|
93
|
+
char *s;
|
94
|
+
} kstring_t;
|
95
|
+
#endif
|
96
|
+
|
97
|
+
#ifndef kroundup32
|
98
|
+
#define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
|
99
|
+
#endif
|
100
|
+
|
101
|
+
#define __KS_GETUNTIL(SCOPE, __read) \
|
102
|
+
SCOPE int ks_getuntil2(kstream_t *ks, int delimiter, kstring_t *str, int *dret, int append) \
|
103
|
+
{ \
|
104
|
+
if (dret) *dret = 0; \
|
105
|
+
str->l = append? str->l : 0; \
|
106
|
+
if (ks->begin >= ks->end && ks->is_eof) return -1; \
|
107
|
+
for (;;) { \
|
108
|
+
int i; \
|
109
|
+
if (ks->begin >= ks->end) { \
|
110
|
+
if (!ks->is_eof) { \
|
111
|
+
ks->begin = 0; \
|
112
|
+
ks->end = __read(ks->f, ks->buf, ks->bufsize); \
|
113
|
+
if (ks->end < ks->bufsize) ks->is_eof = 1; \
|
114
|
+
if (ks->end == 0) break; \
|
115
|
+
} else break; \
|
116
|
+
} \
|
117
|
+
if (delimiter == KS_SEP_LINE) { \
|
118
|
+
for (i = ks->begin; i < ks->end; ++i) \
|
119
|
+
if (ks->buf[i] == '\n') break; \
|
120
|
+
} else if (delimiter > KS_SEP_MAX) { \
|
121
|
+
for (i = ks->begin; i < ks->end; ++i) \
|
122
|
+
if (ks->buf[i] == delimiter) break; \
|
123
|
+
} else if (delimiter == KS_SEP_SPACE) { \
|
124
|
+
for (i = ks->begin; i < ks->end; ++i) \
|
125
|
+
if (isspace(ks->buf[i])) break; \
|
126
|
+
} else if (delimiter == KS_SEP_TAB) { \
|
127
|
+
for (i = ks->begin; i < ks->end; ++i) \
|
128
|
+
if (isspace(ks->buf[i]) && ks->buf[i] != ' ') break; \
|
129
|
+
} else i = 0; /* never come to here! */ \
|
130
|
+
if (str->m - str->l < (size_t)(i - ks->begin + 1)) { \
|
131
|
+
str->m = str->l + (i - ks->begin) + 1; \
|
132
|
+
kroundup32(str->m); \
|
133
|
+
str->s = (char*)realloc(str->s, str->m); \
|
134
|
+
} \
|
135
|
+
memcpy(str->s + str->l, ks->buf + ks->begin, i - ks->begin); \
|
136
|
+
str->l = str->l + (i - ks->begin); \
|
137
|
+
ks->begin = i + 1; \
|
138
|
+
if (i < ks->end) { \
|
139
|
+
if (dret) *dret = ks->buf[i]; \
|
140
|
+
break; \
|
141
|
+
} \
|
142
|
+
} \
|
143
|
+
if (str->s == 0) { \
|
144
|
+
str->m = 1; \
|
145
|
+
str->s = (char*)calloc(1, 1); \
|
146
|
+
} else if (delimiter == KS_SEP_LINE && str->l > 1 && str->s[str->l-1] == '\r') --str->l; \
|
147
|
+
str->s[str->l] = '\0'; \
|
148
|
+
return str->l; \
|
149
|
+
}
|
150
|
+
|
151
|
+
#define KSTREAM_INIT2(SCOPE, type_t, __read, __bufsize) \
|
152
|
+
__KS_TYPE(type_t) \
|
153
|
+
__KS_BASIC(SCOPE, type_t, __bufsize) \
|
154
|
+
__KS_GETUNTIL(SCOPE, __read) \
|
155
|
+
__KS_INLINED(__read)
|
156
|
+
|
157
|
+
#define KSTREAM_INIT(type_t, __read, __bufsize) KSTREAM_INIT2(static, type_t, __read, __bufsize)
|
158
|
+
|
159
|
+
#define KSTREAM_DECLARE(type_t, __read) \
|
160
|
+
__KS_TYPE(type_t) \
|
161
|
+
extern int ks_getuntil2(kstream_t *ks, int delimiter, kstring_t *str, int *dret, int append); \
|
162
|
+
extern kstream_t *ks_init(type_t f); \
|
163
|
+
extern void ks_destroy(kstream_t *ks); \
|
164
|
+
__KS_INLINED(__read)
|
165
|
+
|
166
|
+
/******************
|
167
|
+
* FASTA/Q parser *
|
168
|
+
******************/
|
169
|
+
|
170
|
+
#define kseq_rewind(ks) ((ks)->last_char = (ks)->f->is_eof = (ks)->f->begin = (ks)->f->end = 0)
|
171
|
+
|
172
|
+
#define __KSEQ_BASIC(SCOPE, type_t) \
|
173
|
+
SCOPE kseq_t *kseq_init(type_t fd) \
|
174
|
+
{ \
|
175
|
+
kseq_t *s = (kseq_t*)calloc(1, sizeof(kseq_t)); \
|
176
|
+
s->f = ks_init(fd); \
|
177
|
+
return s; \
|
178
|
+
} \
|
179
|
+
SCOPE void kseq_destroy(kseq_t *ks) \
|
180
|
+
{ \
|
181
|
+
if (!ks) return; \
|
182
|
+
free(ks->name.s); free(ks->comment.s); free(ks->seq.s); free(ks->qual.s); \
|
183
|
+
ks_destroy(ks->f); \
|
184
|
+
free(ks); \
|
185
|
+
}
|
186
|
+
|
187
|
+
/* Return value:
|
188
|
+
>=0 length of the sequence (normal)
|
189
|
+
-1 end-of-file
|
190
|
+
-2 truncated quality string
|
191
|
+
*/
|
192
|
+
#define __KSEQ_READ(SCOPE) \
|
193
|
+
SCOPE int kseq_read(kseq_t *seq) \
|
194
|
+
{ \
|
195
|
+
int c; \
|
196
|
+
kstream_t *ks = seq->f; \
|
197
|
+
if (seq->last_char == 0) { /* then jump to the next header line */ \
|
198
|
+
while ((c = ks_getc(ks)) != -1 && c != '>' && c != '@'); \
|
199
|
+
if (c == -1) return -1; /* end of file */ \
|
200
|
+
seq->last_char = c; \
|
201
|
+
} /* else: the first header char has been read in the previous call */ \
|
202
|
+
seq->comment.l = seq->seq.l = seq->qual.l = 0; /* reset all members */ \
|
203
|
+
if (ks_getuntil(ks, 0, &seq->name, &c) < 0) return -1; /* normal exit: EOF */ \
|
204
|
+
if (c != '\n') ks_getuntil(ks, KS_SEP_LINE, &seq->comment, 0); /* read FASTA/Q comment */ \
|
205
|
+
if (seq->seq.s == 0) { /* we can do this in the loop below, but that is slower */ \
|
206
|
+
seq->seq.m = 256; \
|
207
|
+
seq->seq.s = (char*)malloc(seq->seq.m); \
|
208
|
+
} \
|
209
|
+
while ((c = ks_getc(ks)) != -1 && c != '>' && c != '+' && c != '@') { \
|
210
|
+
if (c == '\n') continue; /* skip empty lines */ \
|
211
|
+
seq->seq.s[seq->seq.l++] = c; /* this is safe: we always have enough space for 1 char */ \
|
212
|
+
ks_getuntil2(ks, KS_SEP_LINE, &seq->seq, 0, 1); /* read the rest of the line */ \
|
213
|
+
} \
|
214
|
+
if (c == '>' || c == '@') seq->last_char = c; /* the first header char has been read */ \
|
215
|
+
if (seq->seq.l + 1 >= seq->seq.m) { /* seq->seq.s[seq->seq.l] below may be out of boundary */ \
|
216
|
+
seq->seq.m = seq->seq.l + 2; \
|
217
|
+
kroundup32(seq->seq.m); /* rounded to the next closest 2^k */ \
|
218
|
+
seq->seq.s = (char*)realloc(seq->seq.s, seq->seq.m); \
|
219
|
+
} \
|
220
|
+
seq->seq.s[seq->seq.l] = 0; /* null terminated string */ \
|
221
|
+
if (c != '+') return seq->seq.l; /* FASTA */ \
|
222
|
+
if (seq->qual.m < seq->seq.m) { /* allocate memory for qual in case insufficient */ \
|
223
|
+
seq->qual.m = seq->seq.m; \
|
224
|
+
seq->qual.s = (char*)realloc(seq->qual.s, seq->qual.m); \
|
225
|
+
} \
|
226
|
+
while ((c = ks_getc(ks)) != -1 && c != '\n'); /* skip the rest of '+' line */ \
|
227
|
+
if (c == -1) return -2; /* error: no quality string */ \
|
228
|
+
while (ks_getuntil2(ks, KS_SEP_LINE, &seq->qual, 0, 1) >= 0 && seq->qual.l < seq->seq.l); \
|
229
|
+
seq->last_char = 0; /* we have not come to the next header line */ \
|
230
|
+
if (seq->seq.l != seq->qual.l) return -2; /* error: qual string is of a different length */ \
|
231
|
+
return seq->seq.l; \
|
232
|
+
}
|
233
|
+
|
234
|
+
#define __KSEQ_TYPE(type_t) \
|
235
|
+
typedef struct { \
|
236
|
+
kstring_t name, comment, seq, qual; \
|
237
|
+
int last_char; \
|
238
|
+
kstream_t *f; \
|
239
|
+
} kseq_t;
|
240
|
+
|
241
|
+
#define KSEQ_INIT2(SCOPE, type_t, __read) \
|
242
|
+
KSTREAM_INIT2(SCOPE, type_t, __read, 65536) \
|
243
|
+
__KSEQ_TYPE(type_t) \
|
244
|
+
__KSEQ_BASIC(SCOPE, type_t) \
|
245
|
+
__KSEQ_READ(SCOPE)
|
246
|
+
|
247
|
+
#define KSEQ_INIT(type_t, __read) KSEQ_INIT2(static, type_t, __read)
|
248
|
+
|
249
|
+
#define KSEQ_DECLARE(type_t) \
|
250
|
+
__KS_TYPE(type_t) \
|
251
|
+
__KSEQ_TYPE(type_t) \
|
252
|
+
extern kseq_t *kseq_init(type_t fd); \
|
253
|
+
void kseq_destroy(kseq_t *ks); \
|
254
|
+
int kseq_read(kseq_t *seq);
|
255
|
+
|
256
|
+
#endif
|
@@ -0,0 +1,164 @@
|
|
1
|
+
/* The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2008, 2011 Attractive Chaos <attractor@live.co.uk>
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
20
|
+
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
21
|
+
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
22
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
|
+
SOFTWARE.
|
24
|
+
*/
|
25
|
+
|
26
|
+
// This is a simplified version of ksort.h
|
27
|
+
|
28
|
+
#ifndef AC_KSORT_H
|
29
|
+
#define AC_KSORT_H
|
30
|
+
|
31
|
+
#include <stdlib.h>
|
32
|
+
#include <string.h>
|
33
|
+
#include <assert.h>
|
34
|
+
|
35
|
+
typedef struct {
|
36
|
+
void *left, *right;
|
37
|
+
int depth;
|
38
|
+
} ks_isort_stack_t;
|
39
|
+
|
40
|
+
#define KSORT_SWAP(type_t, a, b) { type_t t=(a); (a)=(b); (b)=t; }
|
41
|
+
|
42
|
+
#define KSORT_INIT(name, type_t, __sort_lt) \
|
43
|
+
void ks_heapdown_##name(size_t i, size_t n, type_t l[]) \
|
44
|
+
{ \
|
45
|
+
size_t k = i; \
|
46
|
+
type_t tmp = l[i]; \
|
47
|
+
while ((k = (k << 1) + 1) < n) { \
|
48
|
+
if (k != n - 1 && __sort_lt(l[k], l[k+1])) ++k; \
|
49
|
+
if (__sort_lt(l[k], tmp)) break; \
|
50
|
+
l[i] = l[k]; i = k; \
|
51
|
+
} \
|
52
|
+
l[i] = tmp; \
|
53
|
+
} \
|
54
|
+
void ks_heapup_##name(size_t n, type_t l[]) \
|
55
|
+
{ \
|
56
|
+
size_t i, k = n - 1; \
|
57
|
+
type_t tmp = l[k]; \
|
58
|
+
while (k) { \
|
59
|
+
i = (k - 1) >> 1; \
|
60
|
+
if (__sort_lt(tmp, l[i])) break; \
|
61
|
+
l[k] = l[i]; k = i; \
|
62
|
+
} \
|
63
|
+
l[k] = tmp; \
|
64
|
+
} \
|
65
|
+
void ks_heapmake_##name(size_t lsize, type_t l[]) \
|
66
|
+
{ \
|
67
|
+
size_t i; \
|
68
|
+
for (i = (lsize >> 1) - 1; i != (size_t)(-1); --i) \
|
69
|
+
ks_heapdown_##name(i, lsize, l); \
|
70
|
+
} \
|
71
|
+
type_t ks_ksmall_##name(size_t n, type_t arr[], size_t kk) \
|
72
|
+
{ \
|
73
|
+
type_t *low, *high, *k, *ll, *hh, *mid; \
|
74
|
+
low = arr; high = arr + n - 1; k = arr + kk; \
|
75
|
+
for (;;) { \
|
76
|
+
if (high <= low) return *k; \
|
77
|
+
if (high == low + 1) { \
|
78
|
+
if (__sort_lt(*high, *low)) KSORT_SWAP(type_t, *low, *high); \
|
79
|
+
return *k; \
|
80
|
+
} \
|
81
|
+
mid = low + (high - low) / 2; \
|
82
|
+
if (__sort_lt(*high, *mid)) KSORT_SWAP(type_t, *mid, *high); \
|
83
|
+
if (__sort_lt(*high, *low)) KSORT_SWAP(type_t, *low, *high); \
|
84
|
+
if (__sort_lt(*low, *mid)) KSORT_SWAP(type_t, *mid, *low); \
|
85
|
+
KSORT_SWAP(type_t, *mid, *(low+1)); \
|
86
|
+
ll = low + 1; hh = high; \
|
87
|
+
for (;;) { \
|
88
|
+
do ++ll; while (__sort_lt(*ll, *low)); \
|
89
|
+
do --hh; while (__sort_lt(*low, *hh)); \
|
90
|
+
if (hh < ll) break; \
|
91
|
+
KSORT_SWAP(type_t, *ll, *hh); \
|
92
|
+
} \
|
93
|
+
KSORT_SWAP(type_t, *low, *hh); \
|
94
|
+
if (hh <= k) low = ll; \
|
95
|
+
if (hh >= k) high = hh - 1; \
|
96
|
+
} \
|
97
|
+
} \
|
98
|
+
|
99
|
+
#define ks_ksmall(name, n, a, k) ks_ksmall_##name(n, a, k)
|
100
|
+
|
101
|
+
#define ks_lt_generic(a, b) ((a) < (b))
|
102
|
+
#define ks_lt_str(a, b) (strcmp((a), (b)) < 0)
|
103
|
+
|
104
|
+
typedef const char *ksstr_t;
|
105
|
+
|
106
|
+
#define KSORT_INIT_GENERIC(type_t) KSORT_INIT(type_t, type_t, ks_lt_generic)
|
107
|
+
#define KSORT_INIT_STR KSORT_INIT(str, ksstr_t, ks_lt_str)
|
108
|
+
|
109
|
+
#define RS_MIN_SIZE 64
|
110
|
+
#define RS_MAX_BITS 8
|
111
|
+
|
112
|
+
#define KRADIX_SORT_INIT(name, rstype_t, rskey, sizeof_key) \
|
113
|
+
typedef struct { \
|
114
|
+
rstype_t *b, *e; \
|
115
|
+
} rsbucket_##name##_t; \
|
116
|
+
void rs_insertsort_##name(rstype_t *beg, rstype_t *end) \
|
117
|
+
{ \
|
118
|
+
rstype_t *i; \
|
119
|
+
for (i = beg + 1; i < end; ++i) \
|
120
|
+
if (rskey(*i) < rskey(*(i - 1))) { \
|
121
|
+
rstype_t *j, tmp = *i; \
|
122
|
+
for (j = i; j > beg && rskey(tmp) < rskey(*(j-1)); --j) \
|
123
|
+
*j = *(j - 1); \
|
124
|
+
*j = tmp; \
|
125
|
+
} \
|
126
|
+
} \
|
127
|
+
void rs_sort_##name(rstype_t *beg, rstype_t *end, int n_bits, int s) \
|
128
|
+
{ \
|
129
|
+
rstype_t *i; \
|
130
|
+
int size = 1<<n_bits, m = size - 1; \
|
131
|
+
rsbucket_##name##_t *k, b[1<<RS_MAX_BITS], *be = b + size; \
|
132
|
+
assert(n_bits <= RS_MAX_BITS); \
|
133
|
+
for (k = b; k != be; ++k) k->b = k->e = beg; \
|
134
|
+
for (i = beg; i != end; ++i) ++b[rskey(*i)>>s&m].e; \
|
135
|
+
for (k = b + 1; k != be; ++k) \
|
136
|
+
k->e += (k-1)->e - beg, k->b = (k-1)->e; \
|
137
|
+
for (k = b; k != be;) { \
|
138
|
+
if (k->b != k->e) { \
|
139
|
+
rsbucket_##name##_t *l; \
|
140
|
+
if ((l = b + (rskey(*k->b)>>s&m)) != k) { \
|
141
|
+
rstype_t tmp = *k->b, swap; \
|
142
|
+
do { \
|
143
|
+
swap = tmp; tmp = *l->b; *l->b++ = swap; \
|
144
|
+
l = b + (rskey(tmp)>>s&m); \
|
145
|
+
} while (l != k); \
|
146
|
+
*k->b++ = tmp; \
|
147
|
+
} else ++k->b; \
|
148
|
+
} else ++k; \
|
149
|
+
} \
|
150
|
+
for (b->b = beg, k = b + 1; k != be; ++k) k->b = (k-1)->e; \
|
151
|
+
if (s) { \
|
152
|
+
s = s > n_bits? s - n_bits : 0; \
|
153
|
+
for (k = b; k != be; ++k) \
|
154
|
+
if (k->e - k->b > RS_MIN_SIZE) rs_sort_##name(k->b, k->e, n_bits, s); \
|
155
|
+
else if (k->e - k->b > 1) rs_insertsort_##name(k->b, k->e); \
|
156
|
+
} \
|
157
|
+
} \
|
158
|
+
void radix_sort_##name(rstype_t *beg, rstype_t *end) \
|
159
|
+
{ \
|
160
|
+
if (end - beg <= RS_MIN_SIZE) rs_insertsort_##name(beg, end); \
|
161
|
+
else rs_sort_##name(beg, end, RS_MAX_BITS, (sizeof_key - 1) * RS_MAX_BITS); \
|
162
|
+
}
|
163
|
+
|
164
|
+
#endif
|
@@ -0,0 +1,165 @@
|
|
1
|
+
/* The MIT License
|
2
|
+
|
3
|
+
Copyright (c) by Attractive Chaos <attractor@live.co.uk>
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
20
|
+
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
21
|
+
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
22
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
|
+
SOFTWARE.
|
24
|
+
*/
|
25
|
+
|
26
|
+
#ifndef KSTRING_H
|
27
|
+
#define KSTRING_H
|
28
|
+
|
29
|
+
#include <stdlib.h>
|
30
|
+
#include <string.h>
|
31
|
+
#include <stdint.h>
|
32
|
+
#include <stdarg.h>
|
33
|
+
|
34
|
+
#ifndef kroundup32
|
35
|
+
#define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
|
36
|
+
#endif
|
37
|
+
|
38
|
+
#ifndef KSTRING_T
|
39
|
+
#define KSTRING_T kstring_t
|
40
|
+
typedef struct __kstring_t {
|
41
|
+
size_t l, m;
|
42
|
+
char *s;
|
43
|
+
} kstring_t;
|
44
|
+
#endif
|
45
|
+
|
46
|
+
typedef struct {
|
47
|
+
uint64_t tab[4];
|
48
|
+
int sep, finished;
|
49
|
+
const char *p; // end of the current token
|
50
|
+
} ks_tokaux_t;
|
51
|
+
|
52
|
+
static inline void ks_resize(kstring_t *s, size_t size)
|
53
|
+
{
|
54
|
+
if (s->m < size) {
|
55
|
+
s->m = size;
|
56
|
+
kroundup32(s->m);
|
57
|
+
s->s = (char*)realloc(s->s, s->m);
|
58
|
+
}
|
59
|
+
}
|
60
|
+
|
61
|
+
static inline int kputsn(const char *p, int l, kstring_t *s)
|
62
|
+
{
|
63
|
+
if (s->l + l + 1 >= s->m) {
|
64
|
+
s->m = s->l + l + 2;
|
65
|
+
kroundup32(s->m);
|
66
|
+
s->s = (char*)realloc(s->s, s->m);
|
67
|
+
}
|
68
|
+
memcpy(s->s + s->l, p, l);
|
69
|
+
s->l += l;
|
70
|
+
s->s[s->l] = 0;
|
71
|
+
return l;
|
72
|
+
}
|
73
|
+
|
74
|
+
static inline int kputs(const char *p, kstring_t *s)
|
75
|
+
{
|
76
|
+
return kputsn(p, strlen(p), s);
|
77
|
+
}
|
78
|
+
|
79
|
+
static inline int kputc(int c, kstring_t *s)
|
80
|
+
{
|
81
|
+
if (s->l + 1 >= s->m) {
|
82
|
+
s->m = s->l + 2;
|
83
|
+
kroundup32(s->m);
|
84
|
+
s->s = (char*)realloc(s->s, s->m);
|
85
|
+
}
|
86
|
+
s->s[s->l++] = c;
|
87
|
+
s->s[s->l] = 0;
|
88
|
+
return c;
|
89
|
+
}
|
90
|
+
|
91
|
+
static inline void kputc_(int c, kstring_t *s)
|
92
|
+
{
|
93
|
+
if (s->l + 1 > s->m) {
|
94
|
+
s->m = s->l + 1;
|
95
|
+
kroundup32(s->m);
|
96
|
+
s->s = (char*)realloc(s->s, s->m);
|
97
|
+
}
|
98
|
+
s->s[s->l++] = c;
|
99
|
+
}
|
100
|
+
|
101
|
+
static inline void kputsn_(const void *p, int l, kstring_t *s)
|
102
|
+
{
|
103
|
+
if (s->l + l > s->m) {
|
104
|
+
s->m = s->l + l;
|
105
|
+
kroundup32(s->m);
|
106
|
+
s->s = (char*)realloc(s->s, s->m);
|
107
|
+
}
|
108
|
+
memcpy(s->s + s->l, p, l);
|
109
|
+
s->l += l;
|
110
|
+
}
|
111
|
+
|
112
|
+
static inline int kputw(int c, kstring_t *s)
|
113
|
+
{
|
114
|
+
char buf[16];
|
115
|
+
int l, x;
|
116
|
+
if (c == 0) return kputc('0', s);
|
117
|
+
for (l = 0, x = c < 0? -c : c; x > 0; x /= 10) buf[l++] = x%10 + '0';
|
118
|
+
if (c < 0) buf[l++] = '-';
|
119
|
+
if (s->l + l + 1 >= s->m) {
|
120
|
+
s->m = s->l + l + 2;
|
121
|
+
kroundup32(s->m);
|
122
|
+
s->s = (char*)realloc(s->s, s->m);
|
123
|
+
}
|
124
|
+
for (x = l - 1; x >= 0; --x) s->s[s->l++] = buf[x];
|
125
|
+
s->s[s->l] = 0;
|
126
|
+
return 0;
|
127
|
+
}
|
128
|
+
|
129
|
+
static inline int kputuw(unsigned c, kstring_t *s)
|
130
|
+
{
|
131
|
+
char buf[16];
|
132
|
+
int l, i;
|
133
|
+
unsigned x;
|
134
|
+
if (c == 0) return kputc('0', s);
|
135
|
+
for (l = 0, x = c; x > 0; x /= 10) buf[l++] = x%10 + '0';
|
136
|
+
if (s->l + l + 1 >= s->m) {
|
137
|
+
s->m = s->l + l + 2;
|
138
|
+
kroundup32(s->m);
|
139
|
+
s->s = (char*)realloc(s->s, s->m);
|
140
|
+
}
|
141
|
+
for (i = l - 1; i >= 0; --i) s->s[s->l++] = buf[i];
|
142
|
+
s->s[s->l] = 0;
|
143
|
+
return 0;
|
144
|
+
}
|
145
|
+
|
146
|
+
static inline int ksprintf(kstring_t *s, const char *fmt, ...)
|
147
|
+
{
|
148
|
+
va_list ap;
|
149
|
+
int l;
|
150
|
+
va_start(ap, fmt);
|
151
|
+
l = vsnprintf(s->s + s->l, s->m - s->l, fmt, ap); // This line does not work with glibc 2.0. See `man snprintf'.
|
152
|
+
va_end(ap);
|
153
|
+
if ((size_t)l + 1 > s->m - s->l) {
|
154
|
+
s->m = s->l + l + 2;
|
155
|
+
kroundup32(s->m);
|
156
|
+
s->s = (char*)realloc(s->s, s->m);
|
157
|
+
va_start(ap, fmt);
|
158
|
+
l = vsnprintf(s->s + s->l, s->m - s->l, fmt, ap);
|
159
|
+
}
|
160
|
+
va_end(ap);
|
161
|
+
s->l += l;
|
162
|
+
return l;
|
163
|
+
}
|
164
|
+
|
165
|
+
#endif
|