-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecompressTree.cpp
48 lines (37 loc) · 1.04 KB
/
DecompressTree.cpp
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
#include <stdexcept>
#include "DecompressTree.h"
int DNode::getFirstByte() {
if(parent == nullptr) {
return byte;
} else {
return parent->getFirstByte();
}
}
void DNode::writeStringIn(std::ofstream& file) {
if(parent != nullptr) parent->writeStringIn(file);
file.put(static_cast<char>(byte));
}
DecompressTree::DecompressTree(unsigned long sizeLimit) : SIZE_LIMIT(sizeLimit) {
for(size = 0; size < 256; size++) {
dictionary.push_back(new DNode(nullptr, static_cast<int>(size)));
}
bitsForIndex = 8;
nextBitIncrease = 1u << bitsForIndex;
}
DNode *DecompressTree::newChildNode(unsigned long parent) {
if(size < SIZE_LIMIT) {
auto node = new DNode(dictionary[parent]);
dictionary.push_back(node);
size++;
if(size > nextBitIncrease) {
bitsForIndex++;
nextBitIncrease <<= 1u; // * 2
}
return node;
} else {
return nullptr;
}
}
DNode *DecompressTree::operator[](size_t index) {
return dictionary[index];
}