static Node* pop_less(list<Node*> &first, list<Node*> &second) {
Node* node1 = first.size() ? first.front() : NULL;
Node* node2 = second.size() ? second.front() : NULL;
if (node1 && (!node2 || Node::compare(node1, node2))) {
first.pop_front();
return node1;
} else {
second.pop_front();
return node2;
}
}
void Compressor::buildCharTree()
{
list<Node*> first, second;
for(CharMap::iterator i = charMap.begin(); i != charMap.end(); i++)
first.push_back(new Node(i->second, i->first));
first.sort(Node::compare);
while (first.size() + second.size() > 1)
{
Node *left = pop_less(first, second);
Node *right = pop_less(first, second);
second.push_back(new Node(left, right));
}
charTree = pop_less(first, second);
}