Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cassert>
- #include <cstring>
- #include <iostream>
- using namespace std;
- #define BZ_RUNA 0
- #define BZ_RUNB 1
- void generateMTFValues(char* block) {
- char yy[256];
- int i, j;
- int zPend;
- int wr;
- int EOB;
- // CONSTRUCTION
- int nblock = strlen(block);
- cout << "char count " << nblock << endl;
- unsigned int* ptr = (unsigned int*) malloc(sizeof(unsigned int) * nblock);
- for (int i = 0; i < nblock; i++) {
- ptr[i] = i + 1;
- }
- // mtfv is the mtf output, which the decoder reads on the other side
- unsigned int *mtfv = (unsigned int*) malloc(sizeof(unsigned int) * (nblock + 1));
- bool inUse[256] = {0};
- char* ch = block;
- while (*ch) {
- inUse[*ch] = true;
- ch++;
- }
- int unseqToSeq[256] = {0};
- int mtfFreq[258] = {0};
- // CONSTRUCTION END
- int nInUse = 0;
- for (int i = 0; i < 256; i++)
- if (inUse[i]) {
- unseqToSeq[i] = nInUse;
- nInUse++;
- }
- EOB = nInUse + 1;
- for (i = 0; i <= EOB; i++) mtfFreq[i] = 0;
- wr = 0;
- // this is the *running* frequency of the zero index in mtf
- zPend = 0;
- for (i = 0; i < nInUse; i++) yy[i] = (char) i;
- for (i = 0; i < nblock; i++) {
- char ll_i;
- // j is an index into the original block
- j = ptr[i] - 1;
- if (j < 0) j += nblock;
- // get the mtf index of the j-th byte from the block
- ll_i = unseqToSeq[block[j]];
- if (yy[0] == ll_i) {
- zPend++;
- } else {
- if (zPend > 0) {
- // give the binary output of zPend
- zPend--;
- while (true) {
- if (zPend & 1) {
- mtfv[wr] = BZ_RUNB;
- wr++;
- mtfFreq[BZ_RUNB]++;
- } else {
- mtfv[wr] = BZ_RUNA;
- wr++;
- mtfFreq[BZ_RUNA]++;
- }
- if (zPend < 2) break;
- zPend = (zPend - 2) / 2;
- };
- zPend = 0;
- }
- {
- register char rtmp;
- register char *ryy_j;
- register char rll_i;
- // shift all values to the right in yy
- // and pick the rightmost value to the front
- rtmp = yy[1];
- yy[1] = yy[0];
- ryy_j = &(yy[1]);
- rll_i = ll_i;
- while (rll_i != rtmp) {
- register char rtmp2;
- ryy_j++;
- rtmp2 = rtmp;
- rtmp = *ryy_j;
- *ryy_j = rtmp2;
- };
- yy[0] = rtmp;
- // ryy_j is the pointer index of rtmp in the live yy array
- j = ryy_j - &(yy[0]);
- // so j is the index
- // and j + 1 is the 1-offset position
- mtfv[wr] = j + 1;
- wr++;
- mtfFreq[j + 1]++;
- }
- }
- }
- if (zPend > 0) {
- zPend--;
- while (true) {
- if (zPend & 1) {
- mtfv[wr] = BZ_RUNB;
- wr++;
- mtfFreq[BZ_RUNB]++;
- } else {
- mtfv[wr] = BZ_RUNA;
- wr++;
- mtfFreq[BZ_RUNA]++;
- }
- if (zPend < 2) break;
- zPend = (zPend - 2) / 2;
- };
- zPend = 0;
- }
- mtfv[wr] = EOB;
- wr++;
- mtfFreq[EOB]++;
- // OUTPUT start
- cout << "MTF Selectors" << endl;
- for (int i = 0; i < wr; i++) {
- cout << mtfv[i] << ", ";
- }
- cout << endl;
- cout << "Frequency" << endl;
- for (int i = 0; i <= EOB; i++) {
- cout << mtfFreq[i] << ", ";
- }
- cout << endl;
- // OUTPUT end
- }
- int main(int argc, char* argv[]) {
- // pass string as second argument
- assert(argc == 2);
- generateMTFValues(argv[1]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement