Пример #1
0
int main(int argc, char *argv[]){
	char **codes = create2dArr(10);
	
	createCodes(codes, 10);
	int argCnt = argc - 1;
	if (argCnt >= 1)	{
		scanNames(argCnt, argv, codes);
	}
	return 0;

}
Пример #2
0
int compress(Options mainOptions){
    if(mainOptions.inputFilename != "")
        freopen(mainOptions.inputFilename.c_str(), "r", stdin);
    scanf("%[^\n]", buffer);
    cerr << buffer << endl;
    string matrixCode;
    bool pattern = true;
    if(string(buffer).find("real") != string::npos)
        pattern = false;
    int M, N, nnz;
    scanf("%d %d %d", &M, &N, &nnz);
    mainOptions.M = M;
    mainOptions.N = N;
    mainOptions.nnz = nnz;
    //TODO: sub row sub col
    int subRow=mainOptions.subHeight;
    int subCol=mainOptions.subWidth;
    vector<ll> row;
    vector<ll> col;
    vector<double> values;
    cerr << nnz << "\n";
    cerr << M << "\n";
    cerr << N << "\n";
    for(ll i = 0; i < nnz; ++i){
        ll tmp1, tmp2;
        if(!pattern){
            double tmp3;
            scanf("%lld %lld %lf", &tmp1, &tmp2, &tmp3);
        }else{
            scanf("%lld %lld", &tmp1, &tmp2);
        }
        row.push_back(tmp1-1);
        col.push_back(tmp2-1);
    }
    //rcr 42
    cerr << "creating matrix map\n";
    map<ll, map<ll, map<ll, map<ll, pair<int,int> > > > > matrix;
    for(ll i = 0; i < nnz; ++i){
        matrix[row[i]/subRow][col[i]/subCol][row[i]%subRow][col[i]%subCol] = make_pair(row[i], col[i]);
    }
    vector<ll> deltas;
    ll delta = 0;
    ll p1 = 0; ll p2 = 0; ll p3 = 0; ll p4 = 0;
    cerr << "\n";
    for(auto i1 = matrix.begin(); i1 != matrix.end(); ++i1){
        //delta += (i1->first - p1)*4*N; //new line
        for(int i = 0; i < i1->first - p1; ++i)
            deltas.push_back(-1);
        for(auto i2 = i1->second.begin(); i2 != i1->second.end(); ++i2){
            delta += (i2->first - p2)*subRow*subCol;
            for(auto i3 = i2->second.begin(); i3 != i2->second.end(); ++i3){
                delta += (i3->first - p3)*subCol;
                for(auto i4 = i3->second.begin(); i4 != i3->second.end(); ++i4){
                    delta += i4->first - p4;
                    deltas.push_back(delta);
                    delta = -1;
                    p4 = i4->first;
                }
                p3 = i3->first;
            }
            p2 = i2->first;
        }
        p1 = i1->first;
        delta = 0;
        p2=0; p3=0; p4=0;
    }
    map<Code, ll> distribution;
    int huffmanCodesSize = mainOptions.huffmanEncodedDeltas + 2;
    cerr << "creating huffman codes\n";
    for(int i = 0; i < deltas.size(); ++i){
        if(deltas[i] == -1)
            distribution[Code(NEWLINE, 0)]++;
        else if(deltas[i] < huffmanCodesSize-2)
            distribution[Code(CONSTANT,deltas[i])]++;
        else
            distribution[Code(RANGE,(ll)log2(deltas[i]))]++;
    }
    vector<Code> codes;
    if(mainOptions.maxHuffmanLength != -1)
        codes = createCodes(distribution, mainOptions.nnz, mainOptions.maxHuffmanLength);
    else
        codes = createCodes(distribution);
    int maxCodeLength = FindMaxCodeLength(codes);
    cerr << "max encode length: " << maxCodeLength << endl;
    map<Code, Code> codeMap;
    cerr << "creating codeMap\n";
    for(int i = 0; i < codes.size(); ++i){
        codeMap[codes[i]] = codes[i];
    }
    vector<ull> encodedStream;
    vector<ull> argumentStream;
    ll encodedCurrBit = 0;
    ll encodedLatest = 0;
    ll argumentCurrBit = 0;
    ll argumentLatest = 0;
    cerr << "encoding deltas\n";
    for(ll i = 0; i < deltas.size(); ++i){
        ll delta = deltas[i];
        Code deltaCode;
        ll deltaArgument = 0;
        if(delta == -1)
            deltaCode = Code(NEWLINE, 0);
        else if(delta >= huffmanCodesSize - 2)
            deltaCode = Code(RANGE, (ll)log2(delta));
        else
            deltaCode = Code(CONSTANT, delta);
        encodedLatest |= codeMap[deltaCode].encode << encodedCurrBit;
        if(encodedCurrBit + codeMap[deltaCode].encode_length == 64){
            encodedStream.push_back(encodedLatest);
            encodedLatest = 0;
            encodedCurrBit = 0;
        }else if(encodedCurrBit + codeMap[deltaCode].encode_length > 64){
            encodedStream.push_back(encodedLatest);
            encodedLatest = codeMap[deltaCode].encode >> (64-encodedCurrBit);
            encodedCurrBit = (encodedCurrBit + codeMap[deltaCode].encode_length) % 64;
        }else{