-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHuffman.h
47 lines (41 loc) · 1.4 KB
/
Huffman.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
#include <fstream>
#include <vector>
#include <bitset>
#include <unordered_map>
#include "Heap.h"
namespace Huffman {
class Encoder {
public:
explicit Encoder(std::ofstream& out);
~Encoder();
void EncodeFile(const std::string& file_name, bool is_last_file);
void ClearFileInfo();
private:
void AddInputTextChar(const Letter c);
void AddSpecialLetters();
void CreateHuffmanCodes();
void ConvertToCannonCodes();
void WriteArchiveHat(const std::string& file_name);
void WriteCodedText(BitReader& reader);
Letter GetLetterFromFile(BitReader& reader) const;
std::vector<Letter> GetLettersSortedByCodes();
std::vector<size_t> GetAmountOfCodesForEachLength();
std::unordered_map<Letter, size_t> frequency_table_;
CodesMap huffman_codes_;
BitWriter writer_;
};
class Decoder {
public:
explicit Decoder(std::ifstream& in);
~Decoder();
void DecodeArchive();
private:
void ClearData();
void AddLettersWithCodes(std::vector<std::pair<size_t, Letter>>& letters_with_lengths);
Letter GetNextLetter();
std::vector<std::pair<size_t, Letter>> GetLettersWithLengths(const size_t alphabet_size);
std::string GetArchivedFileName(Letter& current_letter);
HuffmanTree* huffman_tree_;
BitReader reader_;
};
}