]> git.donarmstrong.com Git - rsem.git/blob - ReadIndex.h
Added .gitignore file back
[rsem.git] / ReadIndex.h
1 #ifndef READINDEX_H_
2 #define READINDEX_H_
3
4 #include<cstdio>
5 #include<cstdlib>
6 #include<iostream>
7 #include<fstream>
8
9 #include "utils.h"
10
11 struct ReadIndex {
12         READ_INT_TYPE nReads;
13         int gap, nPos;
14         std::streampos *index;
15
16         ReadIndex () {
17                 nReads = 0; gap = nPos = 0;
18                 index = NULL;
19         }
20
21         ReadIndex(const char* readF) {
22                 char indexF[STRLEN];
23                 std::ifstream fin;
24
25                 sprintf(indexF, "%s.ridx", readF);
26                 fin.open(indexF, std::ios::binary);
27                 if (!fin.is_open()) { fprintf(stderr, "Cannot open %s! It may not exist.\n", indexF); exit(-1); }
28
29                 nReads = 0; gap = nPos = 0;
30                 index = NULL;
31                 if (fin.is_open()) {
32                         fin.read((char*)&nReads, sizeof(nReads));
33                         fin.read((char*)&gap, sizeof(gap));
34                         fin.read((char*)&nPos, sizeof(nPos));
35                         index = new std::streampos[nPos];
36                         for (int i = 0; i < nPos; i++) {
37                                 fin.read((char*)&index[i], sizeof(std::streampos));
38                         }
39                 }
40         }
41
42         ~ReadIndex() {
43                 nReads = 0; gap = nPos = 0;
44                 if (index != NULL) delete[] index;
45         }
46
47         //rid  0-based , return crid : current seeked rid
48         READ_INT_TYPE locate(READ_INT_TYPE rid, std::ifstream& out) {
49                 if (index == NULL) {
50                         out.seekg(0, std::ios::beg);
51                         return 0;
52                 }
53                 assert(rid >= 0 && rid < nReads);
54                 out.seekg(index[rid / gap]);
55                 return (rid / gap) * gap;
56         }
57 };
58
59 #endif /* READINDEX_H_ */