Skip to content

Instantly share code, notes, and snippets.

@Bulat-Ziganshin
Created June 3, 2016 18:04
Show Gist options
  • Save Bulat-Ziganshin/a5095366a9b976b53f611cf4a0d07afd to your computer and use it in GitHub Desktop.
Save Bulat-Ziganshin/a5095366a9b976b53f611cf4a0d07afd to your computer and use it in GitHub Desktop.
MTF by Eugene Shelwien
template< int Mode=0 >
struct MTF {
enum{ CNUM=256 };
typedef short ranktypeF;
typedef byte ranktypeB;
ranktypeF RankF[CNUM];
ranktypeB RankB[CNUM];
void Init( void ) {
uint i;
for( i=0; i<CNUM; i++ ) RankF[i] = i;
for( i=0; i<CNUM; i++ ) RankB[i] = i;
}
byte Transform( byte c ) {
return Mode ? Inverse(c) : Forward(c);
}
byte Forward( byte c ) {
uint j;
//printf( "<%02X ", c );
ranktypeF d = RankF[c];
//printf( ">%02X\n", d );
//getc(stdin);
for( j=0; j<CNUM; j++ ) RankF[j] += ( RankF[j]<d );
RankF[c] = 0;
return d;
}
byte Inverse( byte c ) {
uint j;
uint d = RankB[c];
for( j=c; j>0; j-- ) RankB[j]=RankB[j-1];
RankB[0] = d;
return d;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment