-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHuffmanEncoder.h
More file actions
43 lines (39 loc) · 1.42 KB
/
HuffmanEncoder.h
File metadata and controls
43 lines (39 loc) · 1.42 KB
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
#ifndef HUFFMANENCODER_H_INCLUDED
#define HUFFMANENCODER_H_INCLUDED
#include <vector>
#include <map>
#include <utility>
using namespace std;
/**
@author mgorvat
Class, which associate values with their codes. Values after constructing class
are immutable.
*/
template <class T>
class HuffmanEncoder{
public:
/**
Constructor. Makes the map.
input:
values — pointer to vector<T>, which containes values, which are keys for codes
codes — pointer to vector of codes. Code is a pair of two int values: length of code
and it's numeric value. i-th element in vector must be code of -th element in values
vector. Number of elements in values and codes must be equal.
*/
HuffmanEncoder(vector<T>* values, vector<pair<int, int> >* codes){
for(int i = 0; i < (int)values->size(); i++){
mp[(*values)[i]] = (*codes)[i];
}
}
/**
Reutrn code, associated with element.
input:
val — value, code of which need to be getted
outpit:
Code of velue as pair<int, int>, where first int is code length, and second is it's numeric value
*/
pair<int, int> getCode(T* val){return mp[*val];}
private:
map<T, pair<int, int> > mp;//Here class store values
};
#endif // HuffmanEncoder_H_INCLUDED