/************************************************************************** * Copyright(c) 1998-2003, ALICE Experiment at CERN, All rights reserved. * * * * Author: The ALICE Off-line Project. * * Contributors are mentioned in the code where appropriate. * * * * Permission to use, copy, modify and distribute this software and its * * documentation strictly for non-commercial purposes is hereby granted * * without fee, provided that the above copyright notice appears in all * * copies and that both the copyright notice and this permission notice * * appear in the supporting documentation. The authors make no claims * * about the suitability of this software for any purpose. It is * * provided "as is" without express or implied warranty. * **************************************************************************/ /* $Id:*/ //////////////////////////////////////////////// // Huffman classes for set:TPC // //////////////////////////////////////////////// //This file contains two classes and it implements //the Huffman algorithm for creating tables //used in the compression phase. //The class AliTPCHNode represents a node of the Huffman tree, while //the class AliTPCHTable represents a compression table #include #include #include #include "AliTPCBuffer160.h" #include "AliTPCHuffman.h" ClassImp(AliTPCHNode) AliTPCHNode::AliTPCHNode(){ //Constructor fLeft=0; fRight=0; } ////////////////////////////////////////////////////////////////////////////// AliTPCHNode::AliTPCHNode(Int_t sym, Double_t freq){ //Standard constructor fSymbol=sym; fFrequency=freq; fLeft=0; fRight=0; } ////////////////////////////////////////////////////////////////////////////// AliTPCHNode::AliTPCHNode(const AliTPCHNode &source) :TObject(source){ //Copy Constructor if(&source == this) return; this->fSymbol = source.fSymbol; this->fFrequency = source.fFrequency; this->fLeft = source.fLeft; this->fRight = source.fRight; return; } ////////////////////////////////////////////////////////////////////////////// AliTPCHNode& AliTPCHNode::operator=(const AliTPCHNode &source){ //Assignment operator if(&source == this) return *this; this->fSymbol = source.fSymbol; this->fFrequency = source.fFrequency; this->fLeft = source.fLeft; this->fRight = source.fRight; return *this; } ////////////////////////////////////////////////////////////////////////////// Int_t AliTPCHNode::Compare(const TObject *obj)const{ //Function called by Sort method of TObjArray AliTPCHNode *node=(AliTPCHNode *)obj; Double_t f=fFrequency; Double_t fo=node->fFrequency; if (ffo) return -1; else return 0; } ////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// ClassImp(AliTPCHTable) AliTPCHTable::AliTPCHTable(){ //Constructor fCodeLen=0; fCode=0; fHNodes=0; fNnodes=0; fNum=0; fVerbose=0; } ////////////////////////////////////////////////////////////////////////////// AliTPCHTable::AliTPCHTable(Int_t size){ //Initialization fSize=size; fCodeLen = new UChar_t[fSize]; fCode = new Double_t[fSize]; fHNodes = new TObjArray; fNnodes=0; fNum=0; fVerbose=0; fSym= new Short_t[fSize]; for (Short_t i=0;ifSize = source.fSize; this->fCodeLen = source.fCodeLen; this->fCode = source.fCode; this->fSym = source.fSym; this->fHNodes = source.fHNodes; this->fNnodes = source.fNnodes; this->fNum=source.fNum; this->fVerbose=source.fVerbose; return; } ////////////////////////////////////////////////////////////////////////////// AliTPCHTable& AliTPCHTable::operator=(const AliTPCHTable &source) { //Assignment operator if(&source == this) return *this; this->fSize = source.fSize; this->fCodeLen = source.fCodeLen; this->fCode = source.fCode; this->fSym = source.fSym; this->fHNodes = source.fHNodes; this->fNnodes = source.fNnodes; this->fNum=source.fNum; this->fVerbose=source.fVerbose; return *this; } ////////////////////////////////////////////////////////////////////////////// AliTPCHTable::~AliTPCHTable(){ //HTable destructor if(fVerbose) cout<<"HTable destructor !\n"; if (fCodeLen) delete[] fCodeLen; if (fCode) delete [] fCode; if (fHNodes) { fHNodes->Delete(); //Clear out the collection and deletes the removed objects delete fHNodes; }//end if } ////////////////////////////////////////////////////////////////////////////// void AliTPCHTable::SetCodeLen(UChar_t len,Int_t val){ //Sets codelength of "val" to the variable "len" fCodeLen[val]=len; return; } ////////////////////////////////////////////////////////////////////////////// void AliTPCHTable::SetCode(Double_t code,Int_t val){ //Sets the binary code of the variable "val" fCode[val]=code; return; } ////////////////////////////////////////////////////////////////////////////// void AliTPCHTable::PrintTable()const{ //This method prints a table cout<<"Table for Huffman coding\n"; cout<<" Symbol| Code | Length \n"; for (Int_t i=0;iGetSymbol(); if (!visited->GetLeft()) { fCode[idx] = code; fCodeLen[idx] = len; return kTRUE; }//end if SpanTree(visited->GetLeft(), code << 1, len + 1); SpanTree(visited->GetRight(), code << 1 | 0x0001, len + 1); return kTRUE; } ////////////////////////////////////////////////////////////////////////////// void AliTPCHTable::ResetHNodes(){ //Reset number of HNodes and the HNodes array if (fHNodes) fHNodes->Clear(); if (fNnodes) fNnodes=0; } ////////////////////////////////////////////////////////////////////////////// void AliTPCHTable::ClearTable(){ //Clear the table memset(fCodeLen,0,sizeof(UChar_t)*fSize); memset(fCode,0,sizeof(Double_t)*fSize); } ////////////////////////////////////////////////////////////////////////////// Int_t AliTPCHTable::GetFrequencies(const char *fname){ //It fills the "fCode" array with the frequencies of the symbols read from the file AliTPCBuffer160 buff(fname,0); UInt_t numberOfWords=0; Int_t val; while((val=buff.GetNext())!=-1){ fCode[val]++; fNum++; numberOfWords++; } cout<<"Total number of words: "< 0.){ fNnodes++; //cout<< "Symbol:"<Add(new AliTPCHNode(i,fCode[i])); }//end if }//end for Int_t nentries=fHNodes->GetEntriesFast(); //cout<<"Number of symbols: "< 0){ fHNodes->Sort(nindex+1); AliTPCHNode *aux = new AliTPCHNode(0,0); AliTPCHNode *node= (AliTPCHNode*)fHNodes->UncheckedAt(nindex-1); AliTPCHNode *node1= (AliTPCHNode*)fHNodes->UncheckedAt(nindex); aux->SetLeft(node); aux->SetRight(node1); aux->SetFrequency(node->GetFrequency() + node1->GetFrequency()); fHNodes->RemoveAt(nindex-1); fHNodes->AddAt(aux,nindex-1); nindex--; }//end while ClearTable(); AliTPCHNode *start= (AliTPCHNode*)fHNodes->UncheckedAt(0); SpanTree(start,0,0); }//end if else{ cout<<"Table contains 0 elements\n"; }//end else return 0; } //////////////////////////////////////////////////////////////////////////////