-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtf.c
40 lines (33 loc) · 877 Bytes
/
mtf.c
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
// Move-to-Front transform
#include <stdio.h>
void mtf(const unsigned char *in, unsigned char *out, int len) {
unsigned char table[256];
for (int i = 0; i < 256; i++) {
table[i] = i;
}
for (int i = 0; i < len; i++) {
for (int j = 0; j < 256; j++) {
if (table[j] == in[i]) {
out[i] = j;
break;
}
}
for (int j = out[i]; j > 0; j--) {
table[j] = table[j - 1];
}
table[0] = in[i];
}
}
void un_mtf(const unsigned char *in, unsigned char *out, int len) {
unsigned char table[256];
for (int i = 0; i < 256; i++) {
table[i] = i;
}
for (int i = 0; i < len; i++) {
out[i] = table[in[i]];
for (int j = in[i]; j > 0; j--) {
table[j] = table[j - 1];
}
table[0] = out[i];
}
}