]> git.donarmstrong.com Git - rsem.git/blob - Buffer.h
rewrote parallelization of calcCI.cpp
[rsem.git] / Buffer.h
1 #ifndef BUFFER_H_
2 #define BUFFER_H_
3
4 #include<cstdio>
5 #include<fstream>
6
7 typedef unsigned long long bufsize_type;
8 const int FLOATSIZE = sizeof(float);
9
10 class Buffer {
11 public:
12         Buffer(bufsize_type size, int sp, int nSamples, int cvlen, const char* tmpF) {
13                 cpos = 0;
14                 this->size = size;
15                 buffer = new float[size];
16                 ftmpOut.open(tmpF, std::ios::binary);
17
18                 fr = to = sp;
19                 this->nSamples = nSamples;
20                 this->cvlen = cvlen;
21
22         }
23
24         ~Buffer() {
25                 if (fr < to) flushToTempFile();
26
27                 delete[] buffer;
28                 ftmpOut.close();
29         }
30
31         void write(float *vec) {
32                 if (size - cpos < bufsize_type(cvlen)) flushToTempFile();
33                 memcpy(buffer + cpos, vec, FLOATSIZE * cvlen);
34                 cpos += cvlen;
35                 ++to;
36         }
37
38 private:
39         bufsize_type size, cpos; // cpos : current position
40
41         float *buffer;
42         std::ofstream ftmpOut;
43
44         int fr, to; // each flush, sample fr .. to - 1
45         int nSamples, cvlen;
46
47         void flushToTempFile() {
48                 std::streampos gap1 = std::streampos(fr) * FLOATSIZE;
49                 std::streampos gap2 = std::streampos(nSamples - to) * FLOATSIZE;
50                 float *p = NULL;
51
52                 ftmpOut.seekp(0, std::ios::beg);
53                 for (int i = 0; i < cvlen; i++) {
54                         p = buffer + i;
55                         ftmpOut.seekp(gap1, std::ios::cur);
56                         for (int j = fr; j < to; j++) {
57                                 ftmpOut.write((char*)p, FLOATSIZE);
58                                 p += cvlen;
59                         }
60                         ftmpOut.seekp(gap2, std::ios::cur);
61                 }
62
63                 cpos = 0;
64                 fr = to;
65         }
66 };
67
68 #endif /* BUFFER_H_ */