/
huffman_coding.cc
45 lines (41 loc) · 1.36 KB
/
huffman_coding.cc
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
#include <string>
#include <vector>
#include "test_framework/generic_test.h"
#include "test_framework/serialization_traits.h"
using std::string;
using std::vector;
namespace huffman {
struct CharWithFrequency {
char c;
double freq;
};
double HuffmanEncoding(vector<CharWithFrequency>* symbols) {
// TODO - you fill in here.
return 0.0;
}
} // namespace huffman
namespace test_framework {
template <>
struct SerializationTrait<huffman::CharWithFrequency>
: UserSerTrait<huffman::CharWithFrequency, std::string, double> {
static huffman::CharWithFrequency FromTuple(
const std::tuple<std::string, double>& values) {
if (std::get<0>(values).size() != 1) {
throw std::runtime_error(
"CharWithFrequency parser: string length is not 1");
}
return huffman::CharWithFrequency{std::get<0>(values)[0],
std::get<1>(values)};
}
};
} // namespace test_framework
double HuffmanEncodingWrapper(vector<huffman::CharWithFrequency> symbols) {
return huffman::HuffmanEncoding(&symbols);
}
int main(int argc, char* argv[]) {
std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"symbols"};
return GenericTestMain(args, "huffman_coding.cc", "huffman_coding.tsv",
&HuffmanEncodingWrapper, DefaultComparator{},
param_names);
}