Octane v1.01.20 - The Open Compression Toolkit for C++ http://octane.sourceforge.net/
Homepage | Main | Modules | Class Hierarchy | Compound List | File List | Compound Members | Related Pages

modelers/symbolweightvector.cpp

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 //---------------------------------------------------------------------------
00022 
00023 //---------------------------------------------------------------------------
00024 // application includes
00025 #include "symbolweightvector.hpp"
00026 // system includes
00027 using namespace std;
00028 //---------------------------------------------------------------------------
00029 
00030 
00031 
00032 //---------------------------------------------------------------------------
00033 // OCTANE PUBLIC API - AUXILIARY FUNCTIONS - these are supported by all derived classes
00034 
00035 void SymbolWeightVector::ShowDebuggingInfo()
00036 {
00037         // list the weight vectors
00038         cout << "Symbol Weight Vector ("<<symbolcount<<" symbols):"<<endl;
00039         for (int count=0;count<symbolcount;++count)
00040                 cout << " " << count << ": " << weightvector[count] << "\n";
00041         cout << endl;
00042 }
00043 
00044 unsigned int SymbolWeightVector::GetMemoryUsed()
00045 {
00046         // memory/file usage (in bytes) 
00047         // ATTN: this is only approximate - vector will actually use more - is there a way to get good memory usage info?
00048         return (unsigned int)(sizeof(this)+sizeof(TSymbolWeightVectorWeight)*weightvector.capacity());
00049 }
00050 
00051 unsigned int SymbolWeightVector::GetDiskspaceUsed(bool fortempdecompressiononly)
00052 {
00053         // how much disk space will it take to save this vector?
00054         // its just size of an int and then the data
00055         return (unsigned int)(sizeof(int)+sizeof(TSymbolWeightVectorWeight)*symbolcount);
00056 }
00057 
00058 bool SymbolWeightVector::SaveState(bitwriter &to,bool fortempdecompressiononly)
00059 {
00060         // save state
00061         // return true on success
00062         to.put(symbolcount);
00063         for (int count=0;count<symbolcount;++count)
00064                 to.put(weightvector[count]);
00065         return true;
00066 }
00067 
00068 bool SymbolWeightVector::LoadState(bitreader &from)
00069 {
00070         // load state
00071         from.get(symbolcount);
00072         // force size of vector
00073         SetSymbolCount(symbolcount);
00074         // read weights
00075         for (int count=0;count<symbolcount;++count)
00076                 from.get(weightvector[count]);
00077         // return true on success
00078         return true;
00079 }
00080 //---------------------------------------------------------------------------
00081 
00082 
00083 //---------------------------------------------------------------------------
00084 // SYMBOLWEIGHTVECTOR PUBLIC API - SYMBOL ACCESS
00085 
00086 void SymbolWeightVector::ResetWeights()
00087 {
00088         // reset frequencies(probabilities)
00089         for (int count=0;count<symbolcount;++count)
00090                 weightvector[count]=0;
00091 }
00092 
00093 void SymbolWeightVector::NormalizeToProbabilityDistribution()
00094 {
00095         // normalize into a probability distribution
00096 
00097         // compute sum
00098         TSymbolWeightVectorWeight sumweight=0;
00099         for (int count=0;count<symbolcount;++count)
00100                 sumweight+=weightvector[count];
00101         // now divide by sum to normalize
00102         if (sumweight>0)
00103                 {
00104                 for (int count=0;count<symbolcount;++count)
00105                         weightvector[count]/=sumweight;
00106                 }
00107 }
00108 
00109 void SymbolWeightVector::SetSymbolCount(int newsize)
00110 {
00111         // set the number of symbols we should store weights for (this will contract or enlarge vector)
00112         weightvector.resize(newsize);
00113         symbolcount=newsize;
00114 }
00115 
00116 bool SymbolWeightVector::CountSymbolFrequencies(OctaneParser *parserp, bitreader &from)
00117 {
00118         // higher level function for automatic setting of frequencies with help of parser; returns true on success
00119         bool bretv=true;
00120         int symbolnumber;
00121 
00122         // set our weight vector to size of symbolcount
00123         SetSymbolCount(parserp->GetSymbolCount());
00124         // reset probabilities
00125         ResetWeights();
00126 
00127         // now parse input and count frequencies, OOP parsing a symbol from parser until we hit end
00128         while (parserp->ParseNextSymbolFromInput(from,symbolnumber))
00129                 IncremementSymbolWeight(symbolnumber);
00130                 
00131         // return success
00132         return bretv;
00133 
00134 }
00135 
00136 void SymbolWeightVector::EnforceZeroCountFloor(TSymbolWeightVectorWeight symbolfloorval)
00137 {
00138         // it's common that we may have symbols for characters we dont see for completeness
00139         // and it's common to want to force these to some minimal value (like 1)
00140         for (int count=0;count<symbolcount;++count)
00141                 {
00142                 if (weightvector[count]==0)
00143                         weightvector[count]=symbolfloorval;
00144                 }
00145 }
00146 //---------------------------------------------------------------------------
00147 
 
Generated on 20 May 2004 by doxygen 1.3.3