本文整理汇总了C++中HuffmanTree::size方法的典型用法代码示例。如果您正苦于以下问题:C++ HuffmanTree::size方法的具体用法?C++ HuffmanTree::size怎么用?C++ HuffmanTree::size使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HuffmanTree
的用法示例。
在下文中一共展示了HuffmanTree::size方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: assert
TableInfo phuffman::utility::BuildTable(InputIterator first, InputIterator last, Codes& table) {
using namespace std;
typedef DepthCounterNode Node;
typedef multimap<size_t, Node*> HuffmanTree;
typedef vector<Node*> Nodes;
assert(distance(first, last) <= constants::MAXIMUM_DATABLOCK_SIZE);
assert(table.size() >= constants::ALPHABET_SIZE);
HuffmanTree tree;
Nodes leafs;
TableInfo info;
// Initialize tree
{
Frequencies frequencies = CountFrequencies(first, last);
Frequencies::const_iterator first = frequencies.begin(), last = frequencies.end();
while (first != last) {
Node* leaf = new Node(first->symbol);
tree.insert(make_pair(first->frequency, leaf));
leafs.push_back(leaf);
++first;
}
}
// Build tree
{
for (size_t i=0, size=tree.size(); i<size-1; ++i) {
HuffmanTree::iterator first = tree.begin(), second = tree.begin();
++second;
size_t freq = first->first + second->first; // Calculate freq for a node
Node* node = new Node(first->second, second->second);
++second;
tree.erase(first, second); // Remove two nodes with the smallest frequency
tree.insert(make_pair(freq, node)); // Add node that points to previosly removed nodes
}
assert(tree.size() == 1);
}
// Count codelengths
// In fact, codelengths are already counted in the 'depth' member of a node
// There is only one exception: if the tree contains only one object, we need to set it's depth manually
Node* root = tree.begin()->second;
root->depth = 1;
// Sort nodes by codelength
sort(leafs.begin(), leafs.end(), TreeComparator);
// Build table
{
Nodes::const_iterator first = leafs.begin(), last = leafs.end();
Node *curNode = *first;
info.maximum_codelength = curNode->depth;
Code curCode = CodeMake(curNode->depth, 0);
table[curNode->element] = curCode;
++first;
while (first != last) {
assert(curNode->depth >= curCode.codelength);
curNode = *first;
// If current codeword and next codeword have equal lengths
if (curNode->depth == curCode.codelength) {
// Just increase codeword by 1
curCode.code += 1;
}
// Otherwise
else {
// Increase codeword by 1 and _after_ that shift codeword right
curCode.code = (curCode.code + 1) >> (curNode->depth - curCode.codelength);
}
curCode.codelength = curNode->depth;
table[curNode->element] = curCode;
++first;
}
}
delete root;
return info;
}