]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/AliHLTHuffman.h
dedx method is float but variable declared int, corrected
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTHuffman.h
CommitLineData
6a1b3945 1//-*- Mode: C++ -*-
2// $Id$
3#ifndef ALIHLTHUFFMAN_H
4#define ALIHLTHUFFMAN_H
5//* This file is property of and copyright by the ALICE HLT Project *
6//* ALICE Experiment at CERN, All rights reserved. *
7//* See cxx source for full Copyright notice *
8
9/// @file AliHLTDataHuffman.h
10/// @author Thorsten Kollegger, Matthias Richter
11/// @date 2011-08-14
12/// @brief Huffman code generator/encoder/decode
13
14#include "AliHLTLogging.h"
15
16#include "TNamed.h"
17
18#include <vector>
19#include <bitset>
20#include <string>
21
22/**
23 * @class AliHLTHuffmanNode
24 * Huffman code nodes. This is the base class for two types of nodes,
25 * AliHLTHuffmanTreeNode and AliHLTHuffmanLeaveNode. The latter nodes
26 * represent the index array of values to huffman codes. The former are
27 * used in a tree reaching from the value of highest occuremce to all
28 * leaves. The two node types are defined in order to optimize the storage
29 * format for the huffman table. Leave nodes don't need persistent pointers
30 * to childs, tree nodes don't need persistent binary code values.
31 *
32 * @ingroup alihlt_base
33 */
34class AliHLTHuffmanNode: public TObject {
35public:
36 /// default constructor
37 AliHLTHuffmanNode();
38 /// copy constructor
39 AliHLTHuffmanNode(const AliHLTHuffmanNode& other);
40 /// assignment operator
41 AliHLTHuffmanNode& operator=(const AliHLTHuffmanNode& other);
42 /// destructor
43 virtual ~AliHLTHuffmanNode();
44
45 /// set symbol value of node
46 void SetValue(AliHLTInt64_t v) {
47 fValue = v;
48 }
49 /// add weight to node
50 void AddWeight(Float_t w) {
51 fWeight += w;
52 }
53 /// set weight of node
54 void SetWeight(Float_t w) {
55 fWeight = w;
56 }
57 /// return symbol value of node
58 AliHLTInt64_t GetValue() const {
59 return fValue;
60 }
61 /// return weight of node
62 Float_t GetWeight() const {
63 return fWeight;
64 }
65
66 /// assign huffman code to this node and its children
67 void AssignCode();
68 /// set binary huffman code and length of node
69 virtual void SetBinaryCode(AliHLTUInt64_t length, std::bitset<64> code) = 0;
70 /// set pointer to left child
71 virtual void SetLeftChild(AliHLTHuffmanNode* n) = 0;
72 /// set pointer to right child
73 virtual void SetRightChild(AliHLTHuffmanNode* n) = 0;
74 /// Return length of huffman code
75 virtual AliHLTUInt64_t GetBinaryCodeLength() const = 0;
76 /// Return binary huffman code
77 virtual const std::bitset<64>& GetBinaryCode() const = 0;
78 /// Return pointer to left Child
79 virtual AliHLTHuffmanNode* GetLeftChild() const = 0;
80 /// Return pointer to right Child
81 virtual AliHLTHuffmanNode* GetRightChild() const = 0;
82
83 /// Print information about node
84 void Print(Option_t* option = "") const;
85 /// Overload less operator, based on node weights
86 Bool_t operator <(const AliHLTHuffmanNode& other) const {
87 return (this->GetWeight() < other.GetWeight());
88 }
89 class less {
90 public:
91 bool operator()(const AliHLTHuffmanNode* i1,
92 const AliHLTHuffmanNode* i2) const {
93 //reverse sort, less likely to most likely
94 return ((*i1) < (*i2));
95 }
96 };
97
98private:
99 AliHLTInt64_t fValue; // value
100 Float_t fWeight; // weight
101
102ClassDef(AliHLTHuffmanNode, 1)
103};
104
105/**
106 * @class AliHLTHuffmanTreeNode
107 * Tree nodes store the childs persistently. The binary code is
108 * a transient member.
109 */
110class AliHLTHuffmanTreeNode: public AliHLTHuffmanNode {
111public:
112 /// default constructor
113 AliHLTHuffmanTreeNode();
114 /// copy constructor
115 AliHLTHuffmanTreeNode(const AliHLTHuffmanTreeNode& other);
116 /// assignment operator
117 AliHLTHuffmanTreeNode& operator=(const AliHLTHuffmanTreeNode& other);
118 /// constructor for internal nodes, based on two input nodes (leaves or internal nodes)
119 AliHLTHuffmanTreeNode(AliHLTHuffmanNode* l, AliHLTHuffmanNode* r);
120 /// desstructor
121 ~AliHLTHuffmanTreeNode();
122
123 /// set binary huffman code and length of node
124 void SetBinaryCode(AliHLTUInt64_t length, std::bitset<64> code) {
125 fBinaryCodeLength = length;
126 fBinaryCode = code;
127 }
128
129 /// set pointer to left child
130 void SetLeftChild(AliHLTHuffmanNode* n) {
131 fLeft = n;
132 }
133 /// set pointer to right child
134 void SetRightChild(AliHLTHuffmanNode* n) {
135 fRight = n;
136 }
137 /// Return length of huffman code
138 AliHLTUInt64_t GetBinaryCodeLength() const {
139 return fBinaryCodeLength;
140 }
141 /// Return binary huffman code
142 const std::bitset<64>& GetBinaryCode() const {
143 return fBinaryCode;
144 }
145 /// Return pointer to left Child
146 AliHLTHuffmanNode* GetLeftChild() const {
147 return fLeft;
148 }
149 /// Return pointer to right Child
150 AliHLTHuffmanNode* GetRightChild() const {
151 return fRight;
152 }
153
154private:
155 AliHLTUInt8_t fBinaryCodeLength; //! code length
156 std::bitset<64> fBinaryCode; //! WARNING: this fixed the maximum code length to 128
157 AliHLTHuffmanNode* fLeft; // left neighbor
158 AliHLTHuffmanNode* fRight; // right neighbor
159
160ClassDef(AliHLTHuffmanTreeNode, 1)
161};
162
163/**
164 * @class AliHLTHuffmanLeaveNode
165 * Leave nodes store the binary code persistently, while the childs are transient
166 */
167class AliHLTHuffmanLeaveNode: public AliHLTHuffmanNode {
168public:
169 /// default constructor
170 AliHLTHuffmanLeaveNode();
171 /// copy constructor
172 AliHLTHuffmanLeaveNode(const AliHLTHuffmanLeaveNode& other);
173 /// assignment operator
174 AliHLTHuffmanLeaveNode& operator=(const AliHLTHuffmanLeaveNode& other);
175 /// destructor
176 ~AliHLTHuffmanLeaveNode();
177
178 /// set binary huffman code and length of node
179 void SetBinaryCode(AliHLTUInt64_t length, std::bitset<64> code) {
180 fBinaryCodeLength = length;
181 fBinaryCode = code;
182 }
183 /// set pointer to left child
184 void SetLeftChild(AliHLTHuffmanNode* n) {
185 fLeft = n;
186 }
187 /// set pointer to right child
188 void SetRightChild(AliHLTHuffmanNode* n) {
189 fRight = n;
190 }
191 /// Return length of huffman code
192 AliHLTUInt64_t GetBinaryCodeLength() const {
193 return fBinaryCodeLength;
194 }
195 /// Return binary huffman code
196 const std::bitset<64>& GetBinaryCode() const {
197 return fBinaryCode;
198 }
199 /// Return pointer to left Child
200 AliHLTHuffmanNode* GetLeftChild() const {
201 return fLeft;
202 }
203 /// Return pointer to right Child
204 AliHLTHuffmanNode* GetRightChild() const {
205 return fRight;
206 }
207
208private:
209 AliHLTUInt8_t fBinaryCodeLength; // code length
210 std::bitset<64> fBinaryCode; // WARNING: this fixed the maximum code length to 128
211 AliHLTHuffmanNode* fLeft; //! left neighbor
212 AliHLTHuffmanNode* fRight; //! right neighbor
213
214ClassDef(AliHLTHuffmanLeaveNode, 1)
215};
216
217/**
218 * @class AliHLTHuffman
219 * Huffman code generator/encoder/decoder
220 *
221 * @ingroup alihlt_base
222 */
223class AliHLTHuffman: public TNamed, public AliHLTLogging {
224public:
225 AliHLTHuffman();
226 AliHLTHuffman(const AliHLTHuffman& other);
227 AliHLTHuffman(const char* name, UInt_t maxBits);
228 ~AliHLTHuffman();
229
230 UInt_t GetMaxBits() const {return fMaxBits;}
231 UInt_t GetMaxValue() const {return fMaxValue;}
232
233 /// Return huffman code for a value
234 const std::bitset<64>& Encode(const AliHLTUInt64_t v, AliHLTUInt64_t& codeLength) const;
235
236 /// Return value for bit pattern
237 Bool_t Decode(std::bitset<64> /*bits*/, AliHLTUInt64_t& /*value*/) const;
238 /// Add a new training value (with optional weight) to the training sample
239 Bool_t AddTrainingValue(const AliHLTUInt64_t value,
240 const Float_t weight = 1.);
241 /// Generate huffman tree from training sample
242 Bool_t GenerateHuffmanTree();
243 /// Print info about huffman en-/decoder
244 void Print(Option_t* option = "short") const;
245 /// Overload assignment operator
246 AliHLTHuffman& operator =(const AliHLTHuffman& other);
247
248private:
249 UInt_t fMaxBits; // bit lenght
250 UInt_t fMaxValue; // maximum value
251 std::vector<AliHLTHuffmanLeaveNode> fNodes; // array of nodes
252 AliHLTHuffmanNode* fHuffTopNode; // top node
253
254ClassDef(AliHLTHuffman, 1)
255};
256
257#endif