Пример #1
0
bool Encoding::compress(ibstream &inStream, obstream &outStream){
	buildEncodingForInput(inStream);
	int input;
	string code;

	map<int, string>::iterator it;
	outStream.put(mp.size());
	outStream.put('|');
	for (it = mp.begin(); it != mp.end(); it++){
		outStream.put(it->first);
		//cout << it->first << " has the code ";
		for (int i = 0; i < it->second.size(); i++){
			outStream.put(it->second[i]);
			//cout << it->second[i];
		}
		outStream.put('|');
		//cout << endl;
	}

	inStream.rewind();
	string temp;
	int letter;
	int temps;
	for (int i = 0; i < inStream.size(); i++){
		temps = inStream.size();
		input = inStream.get();
		code = mp[input];
		for (int i = 0; i < code.length(); i++){
			temp = code[i];
			outStream.writebit(StringToInteger(temp));
			//cout << code[i];
		}
		//cout << endl;
	}
	outStream.put(_EOF_);
	outStream.close();


	return true;

}
Пример #2
0
bool Encoding::decompress(ibstream &inStream, obstream &outStream){
	int mapSize = inStream.get();
	char kill = inStream.get(); //get rid of pipe

	map<string, int> dmap; //remove this after testing
	for (int i = 0; i < mapSize; i++){
		int code;
		string key;
		code = inStream.get();
		int val = inStream.get();
		while (val != '|'){
			key += val;
			val = inStream.get();
		}
		dmap[key] = code;
	}

		int input;
		string code;

	while (true){
			code += IntegerToString(inStream.readbit());
			if (dmap.count(code) > 0){
				input = dmap[code];
				//cout << input << endl;
				outStream.put(input);
				code.clear();
				if (inStream.peek() == _EOF_)
				break;
			}
	}


	cout << " made it out of the loop";
		outStream.close();


	return true;
}