00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "mtfll.hpp"
00013
00014 #include <iomanip>
00015 using namespace std;
00016
00017 mtfllCompressor GloballyInstantiated_mtfll(true);
00018
00019 mtfllCompressor::mtfllCompressor(bool registerme)
00020 :OctaneCompressor(registerme)
00021 {
00022 if (registerme)CompressorManager_SingletonInsurer managerinsurance(this);
00023 }
00024
00025 bool mtfllCompressor::DoProtectedCompress(bitreader &s, bitwriter &d)
00026 {
00027 unsigned int array[256], pointers[256];
00028
00029 for (unsigned int i = 0; i < 256; i++) array[i] = pointers[i] = i;
00030
00031 while (!s.empty())
00032 {
00033 unsigned char b = s.get_byte();
00034 d.put_byte((unsigned char)pointers[b]);
00035
00036 if (pointers[b])
00037 {
00038 for (unsigned char c = (unsigned char)pointers[b]; c; c--)
00039 {
00040 array[c] = array[c-1];
00041 pointers[array[c]]++;
00042 }
00043 array[0] = b;
00044 pointers[b] = 0;
00045 }
00046 }
00047 return true;
00048 }
00049
00050 bool mtfllCompressor::DoProtectedDecompress(bitreader &s, bitwriter &d)
00051 {
00052 unsigned int array[256], pointers[256];
00053
00054 for (unsigned int i = 0; i < 256; i++) array[i] = pointers[i] = i;
00055
00056 while (!s.empty())
00057 {
00058 unsigned char b = (unsigned char)array[s.get_byte()];
00059 d.put_byte(b);
00060
00061 if (pointers[b])
00062 {
00063 for (unsigned char c = (unsigned char)pointers[b]; c; c--)
00064 {
00065 array[c] = array[c-1];
00066 pointers[array[c]]++;
00067 }
00068 array[0] = b;
00069 pointers[b] = 0;
00070 }
00071 }
00072 return true;
00073 }