2 //**************************************************************************
3 //* This file is property of and copyright by the ALICE HLT Project *
4 //* ALICE Experiment at CERN, All rights reserved. *
6 //* Primary Authors: Thorsten Kollegger <kollegge@ikf.uni-frankfurt.de> *
7 //* for The ALICE HLT Project. *
9 //* Permission to use, copy, modify and distribute this software and its *
10 //* documentation strictly for non-commercial purposes is hereby granted *
11 //* without fee, provided that the above copyright notice appears in all *
12 //* copies and that both the copyright notice and this permission notice *
13 //* appear in the supporting documentation. The authors make no claims *
14 //* about the suitability of this software for any purpose. It is *
15 //* provided "as is" without express or implied warranty. *
16 //**************************************************************************
18 /// @file AliHLTHuffman.cxx
19 /// @author Thorsten Kollegger, Matthias Richter
21 /// @brief Huffman code generator/encoder/decoder
23 #include "AliHLTHuffman.h"
30 AliHLTHuffmanNode::AliHLTHuffmanNode()
38 AliHLTHuffmanNode::AliHLTHuffmanNode(const AliHLTHuffmanNode& other)
40 , fValue(other.GetValue())
41 , fWeight(other.GetWeight())
45 AliHLTHuffmanNode& AliHLTHuffmanNode::operator =(const AliHLTHuffmanNode& other) {
46 /// assignment operator
47 if (this==&other) return *this;
48 this->fValue = other.fValue;
49 this->fWeight = other.fWeight;
53 AliHLTHuffmanNode::~AliHLTHuffmanNode() {
56 void AliHLTHuffmanNode::AssignCode(bool bReverse) {
57 /// assign code to this node loop to right and left nodes
58 /// the decoding always has to start from the least significant bit since the
59 /// code length is variable. Thats why the bit corresponding to the parent node
60 /// has to be right of the bit of child nodes, i.e. bits correspond to the
61 /// current code length. For storage in a bit stream however, bits are stored
62 /// in a stream from MSB to LSB and overwrapping to the MSBs of the next byte.
63 /// Here the reverse code is needed and the word of fixed length read from the
64 /// stream needs to be reversed before decoding.
65 /// Note: by changing the AliHLTDataDeflater interface to write from LSB to MSB
66 /// this can be avoided.
69 std::bitset < 64 > v = (this->GetBinaryCode() << 1);
71 GetLeftChild()->SetBinaryCode(this->GetBinaryCodeLength() + 1, v);
73 std::bitset < 64 > v = (this->GetBinaryCode());
74 int codelen=this->GetBinaryCodeLength();
76 GetLeftChild()->SetBinaryCode(codelen + 1, v);
78 GetLeftChild()->AssignCode(bReverse);
80 if (GetRightChild()) {
82 std::bitset < 64 > v = (this->GetBinaryCode() << 1);
84 GetRightChild()->SetBinaryCode(this->GetBinaryCodeLength() + 1, v);
86 std::bitset < 64 > v = (this->GetBinaryCode());
87 int codelen=this->GetBinaryCodeLength();
89 GetRightChild()->SetBinaryCode(codelen + 1, v);
91 GetRightChild()->AssignCode(bReverse);
95 void AliHLTHuffmanNode::Print(Option_t* /*option*/) const {
97 std::cout << "value=" << GetValue() << ", weight=" << GetWeight() << ", length="
98 << GetBinaryCodeLength() << ", code=" << GetBinaryCode().to_string()
100 if (GetLeftChild()) {
101 GetLeftChild()->Print();
103 if (GetRightChild()) {
104 GetRightChild()->Print();
108 ClassImp(AliHLTHuffmanNode)
110 ///////////////////////////////////////////////////////////////////////////////////////////////
112 AliHLTHuffmanTreeNode::AliHLTHuffmanTreeNode()
113 : AliHLTHuffmanNode()
114 , fBinaryCodeLength(0)
122 AliHLTHuffmanTreeNode::AliHLTHuffmanTreeNode(const AliHLTHuffmanTreeNode& other)
123 : AliHLTHuffmanNode()
124 , fBinaryCodeLength(other.fBinaryCodeLength)
125 , fBinaryCode(other.fBinaryCode)
126 , fLeft(other.GetLeftChild())
127 , fRight(other.GetRightChild())
132 AliHLTHuffmanTreeNode& AliHLTHuffmanTreeNode::operator =(const AliHLTHuffmanTreeNode& other)
134 /// assignment operator
135 if (&other==this) return *this;
136 this->fBinaryCodeLength = other.GetBinaryCodeLength();
137 this->fBinaryCode = other.GetBinaryCode();
138 this->fLeft = other.GetLeftChild();
139 this->fRight = other.GetRightChild();
140 AliHLTHuffmanNode::operator=(other);
144 AliHLTHuffmanTreeNode::AliHLTHuffmanTreeNode(AliHLTHuffmanNode* l, AliHLTHuffmanNode* r)
145 : AliHLTHuffmanNode()
146 , fBinaryCodeLength(0)
151 SetWeight(l->GetWeight() + r->GetWeight());
152 } else if (l && !r) {
153 SetWeight(l->GetWeight());
154 } else if (!l && r) {
155 SetWeight(r->GetWeight());
159 AliHLTHuffmanTreeNode::~AliHLTHuffmanTreeNode()
164 ClassImp(AliHLTHuffmanTreeNode)
166 ///////////////////////////////////////////////////////////////////////////////////////////////
168 AliHLTHuffmanLeaveNode::AliHLTHuffmanLeaveNode()
169 : AliHLTHuffmanNode()
170 , fBinaryCodeLength(0)
178 AliHLTHuffmanLeaveNode::AliHLTHuffmanLeaveNode(const AliHLTHuffmanLeaveNode& other)
179 : AliHLTHuffmanNode()
180 , fBinaryCodeLength(other.fBinaryCodeLength)
181 , fBinaryCode(other.fBinaryCode)
182 , fLeft(other.GetLeftChild())
183 , fRight(other.GetRightChild())
188 AliHLTHuffmanLeaveNode& AliHLTHuffmanLeaveNode::operator =(const AliHLTHuffmanLeaveNode& other)
190 /// assignment operator
191 if (&other==this) return *this;
192 this->fBinaryCodeLength = other.GetBinaryCodeLength();
193 this->fBinaryCode = other.GetBinaryCode();
194 this->fLeft = other.GetLeftChild();
195 this->fRight = other.GetRightChild();
196 AliHLTHuffmanNode::operator=(other);
200 AliHLTHuffmanLeaveNode::~AliHLTHuffmanLeaveNode()
205 ClassImp(AliHLTHuffmanLeaveNode)
207 ///////////////////////////////////////////////////////////////////////////////////////////////
209 AliHLTHuffman::AliHLTHuffman()
220 AliHLTHuffman::AliHLTHuffman(const AliHLTHuffman& other)
223 , fMaxBits(other.fMaxBits)
224 , fMaxValue(other.fMaxValue)
225 , fNodes(other.fNodes)
227 , fReverseCode(other.fReverseCode)
232 AliHLTHuffman::AliHLTHuffman(const char* name, UInt_t maxBits)
235 , fMaxValue((((AliHLTUInt64_t) 1) << maxBits) - 1)
236 , fNodes((((AliHLTUInt64_t) 1) << maxBits))
240 /// standard constructor
241 for (AliHLTUInt64_t i = 0; i <= fMaxValue; i++) {
242 fNodes[i].SetValue(i);
246 AliHLTHuffman::~AliHLTHuffman() {
250 const std::bitset<64>& AliHLTHuffman::Encode(const AliHLTUInt64_t v, AliHLTUInt64_t& codeLength) const {
253 if (v <= fMaxValue) {
254 // valid symbol/value
256 // huffman code has been generated
257 codeLength = fNodes[v].GetBinaryCodeLength();
258 return fNodes[v].GetBinaryCode();
260 HLTError("encoder '%s' does not seem to be initialized", GetName());
263 HLTError("encoder %s: value %llu exceeds range of %d bits", GetName(), v, GetMaxBits());
266 static const std::bitset<64> dummy;
270 Bool_t AliHLTHuffman::Decode(std::bitset<64> bits, AliHLTUInt64_t& value,
271 AliHLTUInt32_t& length, AliHLTUInt32_t& codeLength) const {
272 // TODO: check decoding logic, righ now it is just as written
273 AliHLTHuffmanNode* currNode = fHuffTopNode;
274 if (!currNode) return kFALSE;
275 if (currNode->GetValue() >= 0) {
276 // handle case with just one node - also quite unlikely
277 value = currNode->GetValue();
281 if (bits[0] && currNode->GetLeftChild()) {
282 // follow left branch
283 currNode = currNode->GetLeftChild();
285 if (currNode->GetValue() >= 0) {
286 value = currNode->GetValue();
288 codeLength = currNode->GetBinaryCodeLength();
293 if (!bits[0] && currNode->GetRightChild()) {
294 currNode = currNode->GetRightChild();
296 if (currNode->GetValue() >= 0) {
297 value = currNode->GetValue();
299 codeLength = currNode->GetBinaryCodeLength();
306 value = ((AliHLTUInt64_t)1) << 63;
310 Bool_t AliHLTHuffman::AddTrainingValue(const AliHLTUInt64_t value,
311 const Float_t weight) {
312 if (value > fMaxValue) {
313 /* TODO: ERROR message */
316 fNodes[value].AddWeight(weight);
320 Bool_t AliHLTHuffman::GenerateHuffmanTree() {
321 // insert pointer to nodes into ordered structure to build tree
322 std::multiset<AliHLTHuffmanNode*, AliHLTHuffmanNode::less> nodeCollection;
323 // std::copy(fNodes.begin(), fNodes.end(),
324 // std::inserter(freq_coll, freq_coll.begin()));
325 for (std::vector<AliHLTHuffmanLeaveNode>::iterator i = fNodes.begin(); i
326 != fNodes.end(); ++i) {
327 nodeCollection.insert(&(*i));
329 while (nodeCollection.size() > 1) {
330 // insert new node into structure, combining the two with lowest probability
331 AliHLTHuffmanNode* node=new AliHLTHuffmanTreeNode(*nodeCollection.begin(), *++nodeCollection.begin());
332 if (!node) return kFALSE;
333 nodeCollection.insert(node);
334 nodeCollection.erase(nodeCollection.begin());
335 nodeCollection.erase(nodeCollection.begin());
338 fHuffTopNode = *nodeCollection.begin();
339 fHuffTopNode->AssignCode(fReverseCode);
343 void AliHLTHuffman::Print(Option_t* option) const {
344 std::cout << GetName() << endl;
345 bool bPrintShort=strcmp(option, "short")==0;
346 if (fHuffTopNode && !bPrintShort) {
347 std::cout << "Huffman tree:" << endl;
348 fHuffTopNode->Print();
350 Double_t uncompressedSize = 0;
351 Double_t compressedSize = 0;
352 Double_t totalWeight = 0;
354 std::cout << std::endl << "Huffman codes:" << std::endl;
355 for (AliHLTUInt64_t i = 0; i <= fMaxValue; i++) {
356 if (!bPrintShort) fNodes[i].Print();
357 totalWeight += fNodes[i].GetWeight();
358 uncompressedSize += fNodes[i].GetWeight() * fMaxBits;
359 compressedSize += fNodes[i].GetWeight()
360 * fNodes[i].GetBinaryCodeLength();
362 if (uncompressedSize > 0) {
363 std::cout << "compression ratio: " << compressedSize
364 / uncompressedSize << std::endl;
365 std::cout << "<bits> uncompressed: " << uncompressedSize / totalWeight
367 std::cout << "<bits> compressed: " << compressedSize / totalWeight
372 AliHLTHuffman& AliHLTHuffman::operator =(const AliHLTHuffman& other) {
373 if (this==&other) return *this;
374 fMaxValue = other.fMaxValue;
375 fNodes = other.fNodes;
380 bool AliHLTHuffman::CheckConsistency() const
383 cout << "huffman table not yet generated" << endl;
386 for (AliHLTUInt64_t v=0; v<GetMaxValue(); v++) {
387 AliHLTUInt64_t codeLength=0;
388 std::bitset<64> code=AliHLTHuffman::Encode(v, codeLength);
389 AliHLTUInt64_t readback=0;
390 AliHLTUInt32_t readbacklen=0;
391 AliHLTUInt32_t readbackcodelen=0;
394 // Note: for optimized bit stream the huffman code is reversed, and
395 // that needs to be taken into account
396 std::bitset<64> rcode;
397 for (AliHLTUInt64_t i=0; i<codeLength; i++) {rcode<<=1; rcode[0]=code[i];}
400 if (!Decode(code, readback, readbacklen, readbackcodelen)) {
401 cout << "Decode failed" << endl;
405 cout << "readback of value " << v << " code length " << codeLength << " failed: got " << readback << " code length " << readbackcodelen << endl;
412 ClassImp(AliHLTHuffman)