forked from voutcn/megahit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdbg_builder_writers.h
216 lines (186 loc) · 7.03 KB
/
sdbg_builder_writers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* MEGAHIT
* Copyright (C) 2014 The University of Hong Kong
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SDBG_BUILDER_WRITERS_H_
#define SDBG_BUILDER_WRITERS_H_
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
#include "mem_file_checker-inl.h"
struct DBG_BinaryWriter {
static const int kBufferSize = 4096;
static const int kCharsPerWWord = 16;
static const int kBitsPerWWord = 64;
uint64_t _w_buffer[kBufferSize];
uint64_t _last_buffer[kBufferSize];
uint64_t _is_dollar_buffer[kBufferSize];
uint64_t _is_low_cover_buffer[kBufferSize];
int _w_index, _last_index, _is_dollar_index;
int _w_index_in_word, _last_index_in_word, _is_dollar_index_in_word;
FILE *_w_file, *_last_file, *_is_dollar_file;
DBG_BinaryWriter(): _w_file(NULL), _last_file(NULL), _is_dollar_file(NULL) {}
~DBG_BinaryWriter() {
if ((_w_index_in_word > 0) + _w_index > 0) {
fwrite(_w_buffer, sizeof(uint64_t), (_w_index_in_word > 0) + _w_index, _w_file);
}
if ((_last_index_in_word > 0) + _last_index > 0) {
fwrite(_last_buffer, sizeof(uint64_t), (_last_index_in_word > 0) + _last_index, _last_file);
}
if ((_is_dollar_index_in_word > 0) + _is_dollar_index > 0) {
fwrite(_is_dollar_buffer, sizeof(uint64_t), (_is_dollar_index_in_word > 0) + _is_dollar_index, _is_dollar_file);
}
if (_w_file != NULL)
fclose(_w_file);
if (_last_file != NULL)
fclose(_last_file);
if (_is_dollar_file != NULL)
fclose(_is_dollar_file);
}
void init(const char *w_file_name, const char *last_file_name, const char *is_dollar_file_name) {
_w_file = OpenFileAndCheck(w_file_name, "wb");
if (_w_file == NULL) {
fprintf(stderr, "Open %s failed\n", w_file_name);
exit(1);
}
_last_file = OpenFileAndCheck(last_file_name, "wb");
if (_last_file == NULL) {
fprintf(stderr, "Open %s failed\n", last_file_name);
exit(1);
}
_is_dollar_file = OpenFileAndCheck(is_dollar_file_name, "wb");
if (_is_dollar_file == NULL) {
fprintf(stderr, "Open %s failed\n", is_dollar_file_name);
exit(1);
}
memset(_w_buffer, 0, sizeof(_w_buffer));
memset(_last_buffer, 0, sizeof(_last_buffer));
memset(_is_dollar_buffer, 0, sizeof(_is_dollar_buffer));
_w_index = 0;
_w_index_in_word = 0;
_last_index = 0;
_last_index_in_word = 0;
_is_dollar_index = 0;
_is_dollar_index_in_word = 0;
}
void outputW(int c) {
_w_buffer[_w_index] |= ((uint64_t)c << (4 * _w_index_in_word));
_w_index_in_word++;
if (_w_index_in_word >= kCharsPerWWord) {
_w_index_in_word = 0;
++_w_index;
if (_w_index >= kBufferSize) {
fwrite(_w_buffer, sizeof(uint64_t), kBufferSize, _w_file);
memset(_w_buffer, 0, sizeof(_w_buffer));
_w_index = 0;
}
}
}
void _outputOneBit(uint64_t *buffer, FILE *file, int &index, int &index_in_word, int c) {
buffer[index] |= ((uint64_t)c << index_in_word);
index_in_word++;
if (index_in_word >= kBitsPerWWord) {
index_in_word = 0;
++index;
if (index >= kBufferSize) {
fwrite(buffer, sizeof(buffer[0]), kBufferSize, file);
memset(buffer, 0, sizeof(buffer[0]) * kBufferSize);
index = 0;
}
}
}
void outputLast(int c) {
_outputOneBit(_last_buffer, _last_file, _last_index, _last_index_in_word, c);
}
void outputIsDollar(int c) {
_outputOneBit(_is_dollar_buffer, _is_dollar_file, _is_dollar_index, _is_dollar_index_in_word, c);
}
void outputC(int c, long long number) {
while (number > 0 && (_w_index_in_word != 0 || _w_index != 0)) {
outputW(c);
--number;
}
if (number >= kBufferSize * kCharsPerWWord) {
memset(_w_buffer, (c << 4) | c, sizeof(_w_buffer));
while (number >= kBufferSize * kCharsPerWWord) {
fwrite(_w_buffer, sizeof(uint64_t), kBufferSize, _w_file);
number -= kBufferSize * kCharsPerWWord;
}
memset(_w_buffer, 0, sizeof(_w_buffer));
}
while (number > 0) {
outputW(c);
--number;
}
}
void _outputOnes(uint64_t *buffer, FILE *file, int &index, int &index_in_word, long long number) {
while (number > 0 && (index_in_word != 0 || index != 0)) {
_outputOneBit(buffer, file, index, index_in_word, 1);
--number;
}
if (number >= kBufferSize * kBitsPerWWord) {
memset(buffer, 0xFF, sizeof(buffer[0]) * kBufferSize);
while (number >= kBufferSize * kBitsPerWWord) {
fwrite(buffer, sizeof(uint64_t), kBufferSize, file);
number -= kBufferSize * kBitsPerWWord;
}
memset(buffer, 0, sizeof(buffer[0]) * kBufferSize);
}
while (number > 0) {
_outputOneBit(buffer, file, index, index_in_word, 1);
--number;
}
}
void outputOnesToLast(int64_t number) {
_outputOnes(_last_buffer, _last_file, _last_index, _last_index_in_word, number);
}
void outputOnesToIsDollar(int64_t number) {
_outputOnes(_is_dollar_buffer, _is_dollar_file, _is_dollar_index, _is_dollar_index_in_word, number);
}
};
struct WordWriter {
static const int kBufferSize = 4096;
int buffer_index;
edge_word_t output_buffer[kBufferSize];
FILE *file;
WordWriter(): file(NULL) {}
~WordWriter() {
if (file != NULL)
destroy();
}
void init(const char *file_name) {
file = OpenFileAndCheck(file_name, "wb");
buffer_index = 0;
assert(file != NULL);
}
void destroy() {
if (file != NULL) {
if (buffer_index > 0) {
fwrite(output_buffer, sizeof(edge_word_t), buffer_index, file);
}
fclose(file);
file = NULL;
}
}
void output(edge_word_t w) {
output_buffer[buffer_index++] = w;
if (buffer_index == kBufferSize) {
fwrite(output_buffer, sizeof(edge_word_t), kBufferSize, file);
buffer_index = 0;
}
}
};
#endif // SDBG_BUILDER_WRITERS_H_