forked from hxim/paq8px
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMTFList.cpp
43 lines (39 loc) · 837 Bytes
/
MTFList.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
38
39
40
41
42
43
#include "MTFList.hpp"
MTFList::MTFList(const uint16_t n) : root(0), Index(0), previous(n), next(n) {
#ifdef VERBOSE
printf("Created MTFList with n = %d\n", n);
#endif
assert(n > 0);
for( int i = 0; i < n; i++ ) {
previous[i] = i - 1;
next[i] = i + 1;
}
next[n - 1] = -1;
}
auto MTFList::getFirst() -> int {
return Index = root;
}
auto MTFList::getNext() -> int {
if( Index >= 0 ) {
Index = next[Index];
}
return Index;
}
void MTFList::moveToFront(const int i) {
assert(uint32_t(i) < previous.size());
if((Index = i) == root ) {
return;
}
const int p = previous[Index];
const int n = next[Index];
if( p >= 0 ) {
next[p] = next[Index];
}
if( n >= 0 ) {
previous[n] = previous[Index];
}
previous[root] = Index;
next[Index] = root;
root = Index;
previous[root] = -1;
}