本文整理汇总了C++中HuffmanTree::hash方法的典型用法代码示例。如果您正苦于以下问题:C++ HuffmanTree::hash方法的具体用法?C++ HuffmanTree::hash怎么用?C++ HuffmanTree::hash使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HuffmanTree
的用法示例。
在下文中一共展示了HuffmanTree::hash方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: compress
bool Compress::compress() {
File * f = new File(m_path, m_fileName);
QByteArray qba = f->read();
if (qba != NULL) {
CountOccurrence * co = new CountOccurrence(qba);
QList<Occurrence> occur = co->orderByOccurrence();
HuffmanTree * cht = new HuffmanTree(occur);
Tree * tree = cht->createTree();
tree->createRep();
cht->createHash(tree);
QString data;
for (int i = 0; i < qba.size(); ++i) {
QString pathNode = cht->hash()->value(qba.at(i));
data.append(pathNode);
}
int garbageSize = 8 - data.size()%8;
if (garbageSize == 8) {
garbageSize = 0;
}
for (int i = 0; i < garbageSize; ++i) {
data.append("0");
}
QBitArray toByte;
toByte.resize(data.size());
for (int i = 0; i < data.size(); ++i) {
if (data.at(i) == '0') {
toByte[i] = true;
}
else {
toByte[i] = false;
}
}
bool ok;
QByteArray encoded;
QString c;
for (int i = 0; i < data.size(); i+=8) {
QString h = data.mid(i,8);
encoded.append(QChar(h.toInt(&ok, 2)));
c.append(h);
}
QString binaryGarbageSize = QString::number(garbageSize,2);
QString binaryTreeSize = QString::number(tree->rep().size(),2);
int zeros = 16 - (binaryGarbageSize.size() + binaryTreeSize.size());
for (int i = 0; i < zeros; ++i) {
binaryTreeSize.prepend(QString::number(0));
}
QString toBit = binaryGarbageSize;
toBit.append(binaryTreeSize);
int h1 = toBit.mid(0,8).toInt(&ok, 2);
int h2 = toBit.mid(8,8).toInt(&ok, 2);
QByteArray toWrite;
toWrite.clear();
toWrite.append(QChar(h1));
toWrite.append(QChar(h2));
toWrite.append(m_fileName);
for (int i = m_fileName.size(); i < 128; ++i ) {
toWrite.append("#");
}
toWrite.append(tree->rep());
toWrite.append(encoded);
f->write(toWrite, m_path + m_compressedFileName);
qDebug() << m_fileName << " comprimido";
return true;
} else {
qDebug() << "Arquivo não encontrado";
return false;
}
}