-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathHuffWT.h
159 lines (148 loc) · 4.73 KB
/
HuffWT.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#ifndef _HUFFWT_H_
#define _HUFFWT_H_
#include "BitRank.h"
#include <cstdio>
#include <stdexcept>
class HuffWT
{
public:
class TCodeEntry
{
public:
ulong count;
unsigned bits;
unsigned code;
TCodeEntry() {count=0;bits=0;code=0u;};
void load(std::FILE *file, uchar verFlag)
{
if (verFlag < 16)
{
unsigned temp = 0;
if (std::fread(&temp, sizeof(unsigned), 1, file) != 1)
throw std::runtime_error("TCodeEntry: file read error (Rs).");
count = temp;
}
else
if (std::fread(&count, sizeof(ulong), 1, file) != 1)
throw std::runtime_error("TCodeEntry: file read error (Rs).");
if (std::fread(&bits, sizeof(unsigned), 1, file) != 1)
throw std::runtime_error("TCodeEntry: file read error (Rs).");
if (std::fread(&code, sizeof(unsigned), 1, file) != 1)
throw std::runtime_error("TCodeEntry: file read error (Rs).");
}
void save(std::FILE *file)
{
if (std::fwrite(&count, sizeof(ulong), 1, file) != 1)
throw std::runtime_error("TCodeEntry: file write error (Rs).");
if (std::fwrite(&bits, sizeof(unsigned), 1, file) != 1)
throw std::runtime_error("TCodeEntry: file write error (Rs).");
if (std::fwrite(&code, sizeof(unsigned), 1, file) != 1)
throw std::runtime_error("TCodeEntry: file write error (Rs).");
}
};
private:
BitRank *bitrank;
HuffWT *left;
HuffWT *right;
TCodeEntry *codetable;
uchar ch;
bool leaf;
HuffWT(uchar *, ulong, TCodeEntry *, unsigned);
void save(std::FILE *);
HuffWT(std::FILE *, TCodeEntry *);
public:
static HuffWT * makeHuffWT(uchar *bwt, ulong n);
static HuffWT * load(std::FILE *, uchar verFlag);
static void save(HuffWT *, std::FILE *);
static void deleteHuffWT(HuffWT *);
~HuffWT(); const
inline ulong rank(uchar c, ulong i) const { // returns the number of characters c before and including position i
HuffWT const *temp=this;
if (codetable[c].count == 0) return 0;
unsigned level = 0;
unsigned code = codetable[c].code;
while (!temp->leaf) {
if ((code & (1u<<level)) == 0) {
i = i-temp->bitrank->rank(i);
temp = temp->left;
}
else {
i = temp->bitrank->rank(i)-1;
temp = temp->right;
}
++level;
}
return i+1;
};
inline ulong select(uchar c, ulong i, unsigned level = 0) const
{
if (leaf)
return i-1;
// if (codetable[c].count == 0) return 0;
// unsigned level = 0;
unsigned code = codetable[c].code;
if ((code & (1u<<level)) == 0) {
i = left->select(c, i, level+1);
i = bitrank->select0(i+1);
}
else
{
i = right->select(c, i, level+1);
i = bitrank->select(i+1);
}
return i;
};
inline bool IsCharAtPos(uchar c, ulong i)
{
HuffWT const *temp=this;
if (codetable[c].count == 0) return false;
unsigned level = 0;
unsigned code = codetable[c].code;
while (!temp->leaf) {
if ((code & (1u<<level))==0) {
if (temp->bitrank->IsBitSet(i)) return false;
i = i-temp->bitrank->rank(i);
temp = temp->left;
}
else {
if (!temp->bitrank->IsBitSet(i)) return false;
i = temp->bitrank->rank(i)-1;
temp = temp->right;
}
++level;
}
return true;
}
inline uchar access(ulong i) const
{
HuffWT const *temp=this;
while (!temp->leaf) {
if (temp->bitrank->IsBitSet(i)) {
i = temp->bitrank->rank(i)-1;
temp = temp->right;
}
else {
i = i-temp->bitrank->rank(i);
temp = temp->left;
}
}
return (int)temp->ch;
}
inline uchar access(ulong i, ulong &rank) const
{
HuffWT const *temp=this;
while (!temp->leaf) {
if (temp->bitrank->IsBitSet(i)) {
i = temp->bitrank->rank(i)-1;
temp = temp->right;
}
else {
i = i-temp->bitrank->rank(i);
temp = temp->left;
}
}
rank = i+1;
return (int)temp->ch;
}
};
#endif