forked from DaehwanKimLab/hisat2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bit_packed_array.h
105 lines (79 loc) · 2.78 KB
/
bit_packed_array.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
/*
* Copyright 2018, Chanhee Park <[email protected]> and Daehwan Kim <[email protected]>
*
* This file is part of HISAT 2.
*
* HISAT 2 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.
*
* HISAT 2 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 HISAT 2. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __HISAT2_BIT_PACKED_ARRAY_H
#define __HISAT2_BIT_PACKED_ARRAY_H
#include <iostream>
#include <fstream>
#include <limits>
#include <map>
#include "assert_helpers.h"
#include "word_io.h"
#include "mem_ids.h"
#include "ds.h"
using namespace std;
class BitPackedArray {
public:
BitPackedArray () {}
~BitPackedArray();
/**
* Return true iff there are no items
* @return
*/
inline bool empty() const { return cur_ == 0; }
inline size_t size() const { return cur_; }
TIndexOffU get(size_t idx) const;
inline TIndexOffU operator[](size_t i) const { return get(i); }
void pushBack(TIndexOffU val);
void init(size_t max_value);
void reset();
void writeFile(const char *filename);
void writeFile(const string& filename);
void writeFile(ofstream &fp);
void readFile(const char *filename);
void readFile(const string& filename);
void readFile(ifstream &fp);
void dump() const;
size_t getMemUsage() const;
private:
void init_by_log2(size_t ceil_log2);
void put(size_t index, TIndexOffU val);
inline uint64_t bitToMask(size_t bit) const
{
return (uint64_t) ((1ULL << bit) - 1);
}
TIndexOffU getItem(uint64_t *block, size_t idx, size_t offset) const;
void setItem(uint64_t *block, size_t idx, size_t offset, TIndexOffU val);
pair<size_t, size_t> indexToAddress(size_t index) const;
pair<size_t, size_t> columnToPosition(size_t col) const;
void expand(size_t count = 1);
void allocSize(size_t sz);
void allocItems(size_t count);
private:
size_t item_bit_size_; // item bit size(e.g. 33bit)
size_t elm_bit_size_; // 64bit
size_t items_per_block_bit_;
size_t items_per_block_bit_mask_;
size_t items_per_block_; // number of items in block
size_t cur_; // current item count
size_t sz_; // maximum item count
size_t block_size_; // block size in byte
// List of packed array
EList<uint64_t *> blocks_;
};
#endif //__HISAT2_BIT_PACKED_ARRAY_H