forked from HIITMetagenomics/dsm-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlockArray.h
84 lines (72 loc) · 2.34 KB
/
BlockArray.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
#ifndef _BLOCK_ARRAY_H_
#define _BLOCK_ARRAY_H_
#include "Tools.h"
#include <iostream>
#include <stdexcept>
class BlockArray
{
private:
ulong* data;
ulong n;
ulong index;
ulong blockLength;
public:
BlockArray(ulong len, ulong blockLen) {
n = len;
blockLength = blockLen;
data = new ulong[n*blockLength/W +1];
for (ulong i = 0; i < n*blockLength/W +1; ++i)
data[i] = 0;
}
~BlockArray() {
delete [] data;
}
BlockArray& operator[](ulong i) {
index = i;
return *this;
}
void operator=(const ulong x) {
Tools::SetField(data,blockLength,index,x);
}
BlockArray& operator=(const BlockArray& ba) {
if (this == &ba) return *this;
ulong value = Tools::GetField(ba.data, ba.blockLength, ba.index);
Tools::SetField(data,blockLength,index,value);
return *this;
}
operator ulong() {
return Tools::GetField(data,blockLength,index);
}
ulong spaceInBits() {
return n*blockLength+W; // plus 4 ulong's
}
/**
* Saving data fields:
* ulong n;
* ulong blockLength;
* ulong* data;
*/
void Save(FILE *file) const
{
if (std::fwrite(&(this->n), sizeof(ulong), 1, file) != 1)
throw std::runtime_error("BlockArray::Save(): file write error (n).");
if (std::fwrite(&(this->blockLength), sizeof(ulong), 1, file) != 1)
throw std::runtime_error("BlockArray::Save(): file write error (blockLength).");
if (std::fwrite(this->data, sizeof(ulong), n*blockLength/W+1, file) != n*blockLength/W+1)
throw std::runtime_error("BlockArray::Save(): file write error (data).");
}
/**
* Load from file
*/
BlockArray(FILE *file)
{
if (std::fread(&(this->n), sizeof(ulong), 1, file) != 1)
throw std::runtime_error("BlockArray::Load(): file read error (n).");
if (std::fread(&(this->blockLength), sizeof(ulong), 1, file) != 1)
throw std::runtime_error("BlockArray::Load(): file read error (blockLength).");
data = new ulong[n*blockLength/W+1];
if (std::fread(this->data, sizeof(ulong), n*blockLength/W+1, file) != n*blockLength/W+1)
throw std::runtime_error("BlockArray::Load(): file read error (data).");
}
};
#endif