-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiskVector.h
More file actions
96 lines (78 loc) · 2.29 KB
/
Copy pathDiskVector.h
File metadata and controls
96 lines (78 loc) · 2.29 KB
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
/* -
* Copyright (c) 2012 Nava Whiteford <new@sgenomics.org>
* suffixcore - core suffixtree algorithms
*
* A license to use this software is granted to users given access to the git repository at: https://github.com/sgenomics/suffixcore
* A complete copy of this license is located in the license.txt file of this package.
*
* In Summary this software:
*
* Can be used for creating unlimited applications.
* Can be distributed in binary or object form only.
* Commercial use is allowed.
* Can modify source-code but cannot distribute modifications (derivative works).
*/
#ifndef DISKVECTOR
#define DISKVECTOR
#include <stdio.h>
#include <stdint.h>
#include <zlib.h>
#define gz_buffer_size 256
using namespace std;
template<class data_type>
class DiskVector {
public:
DiskVector() {}
DiskVector(string filename,bool compress=false) {
m_compress = compress;
if(!m_compress) {
filehandle_uc = fopen(filename.c_str(),"a+");
} else {
filehandle_gz = gzopen((filename + ".gz" ).c_str(),"wh");
gzbuffer(filehandle_gz,gz_buffer_size*1024);
}
cout << "filehandle uc: " << filehandle_uc << endl;
cout << "filehandle gz: " << filehandle_gz << endl;
}
data_type operator[](uint64_t index) {
data_type data;
if(!m_compress) {
fseek((FILE *) filehandle_uc,((long)index)*sizeof(data_type),SEEK_SET);
fread(&data,sizeof(data_type),1,(FILE *) filehandle_uc);
}
return data;
}
size_t push_back(data_type i) {
size_t writepos;
if(!m_compress) {
fseek(filehandle_uc,(long)0,SEEK_END);
writepos = ftell(filehandle_uc);
fwrite(&i,sizeof(data_type),1,filehandle_uc);
} else {
writepos = gztell(filehandle_gz);
gzwrite(filehandle_gz,&i,sizeof(data_type));
}
return writepos/sizeof(data_type);
}
size_t size() {
size_t filesize;
if(!m_compress) {
fseek(filehandle_uc,(long)0,SEEK_END);
filesize = ftell((FILE *) filehandle_uc);
} else {
filesize = gztell(filehandle_gz);
}
return filesize/sizeof(data_type);
}
void close() {
if(filehandle_uc != 0) fclose(filehandle_uc);
if(filehandle_gz != 0) {
gzflush(filehandle_gz,Z_FINISH);
gzclose(filehandle_gz);
}
}
FILE *filehandle_uc;
gzFile filehandle_gz;
bool m_compress;
};
#endif