-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchar_code.cpp
37 lines (28 loc) · 964 Bytes
/
char_code.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
#include "char_code.h"
#include <algorithm>
uint16_t CharCode::Length() const {
return code.size();
}
bool CharCode::operator<(const CharCode& other) const {
return Length() < other.Length() || (Length() == other.Length() && character < other.character);
}
void CharCode::Add2PowK(uint16_t k) {
size_t first_zero = 0;
for (size_t j = 0; j < Length() - k; j++) {
if (!code[Length() - k - 1 - j]) {
first_zero = j;
break;
}
}
for (size_t j = 0; j < first_zero; j++) {
code[Length() - k - 1 - j] = false;
}
code[Length() - k - 1 - first_zero] = true;
}
void CalcCanonicalHuffmanCode(std::vector<CharCode>& codes) {
std::sort(codes.begin(), codes.end());
for (size_t i = 1; i < codes.size(); i++) {
std::copy(codes[i - 1].code.begin(), codes[i - 1].code.end(), codes[i].code.begin());
codes[i].Add2PowK(codes[i].Length() - codes[i - 1].Length());
}
}