Ejemplo n.º 1
0
void Encoding::buildEncodingForInput(ibstream& infile){


	map<int, int> charMap;
	getCharCount(infile, charMap);
	PQueue<node*> pq;
	map<int, int>::iterator iter;
	for (iter = charMap.begin(); iter != charMap.end(); iter++){
		node* tempNode = new node;
		tempNode->letter = iter->first;
		tempNode->weight = iter->second;
		tempNode->leftChild = tempNode->rightChild = NULL;
		//cout << tempNode->letter << endl;
		pq.add(tempNode, iter->second);
	}
	map <int, string> encodingMap;
	while (pq.size() != 1){
		node *parent = new node;
		parent->letter = 26;

		parent->weight = 0; //probably don't need this
		parent->leftChild = pq.extractMin();
		parent->leftChild->path +='0';

		parent->rightChild = pq.extractMin();

		parent->rightChild->path += '1';


		parent->weight = parent->leftChild->weight+parent->rightChild->weight;
		pq.add(parent, parent->weight);
	}

	//for testing

	map<int, int>::iterator it;
	node* tree = pq.extractMin();
	for (it = charMap.begin(); it != charMap.end(); it++){
		BuildEncodingMap(tree, mp[it->first], it->first);
		string temps = mp[it->first];
		char tempc = it->first;
		//cout << tempc << " has the encoding " << temps << endl;
	}

	//PrintTree(tree);
}
Ejemplo n.º 2
0
 /// @brief Counts the number of newlines in a std::string.
 /// @param mStr String to work with.
 /// @return Returns the count of newlines.
 inline auto getNewLinesCount(const std::string& mStr)
 {
     return getCharCount(mStr, '\n');
 }
Ejemplo n.º 3
0
	unsigned int getNewLinesCount(const string& mString) { return getCharCount(mString, '\n'); }