00001 package commandline.flatFileToBinaryPackage.HuffMan;
00002
00003
00004
00005
00006
00007
00008
00009 import java.util.ArrayList;
00010 import java.util.Vector;
00011
00012 import commandline.debuggingCode.Debug;
00013
00037 public class Huffman{
00038
00039
00040
00041 public double PMF[] ;
00042 int _dad[] ;
00043 int _code[] ;
00044 int _len[] ;
00045 int _root;
00046
00047
00048 private int PMF_SIZE ;
00049 private int MAX_SIZE;
00050 static final int BITS_PER_CHAR = 8;
00051
00052
00057 public Huffman(double[] PMF){
00058 reinitializePMF(PMF);
00059 }
00060
00061 public Huffman(){}
00062
00063 public void reinitializePMF(double[] PMF){
00064 PairElementPool.reuse();
00065 PMF_SIZE=PMF.length;
00066 if(MAX_SIZE<PMF_SIZE){
00067 this.PMF=new double[2*PMF_SIZE];
00068 _dad = new int[2 * PMF_SIZE];
00069 _code = new int[PMF_SIZE];
00070 _len = new int[PMF_SIZE];
00071 MAX_SIZE=PMF_SIZE;
00072 }
00073
00074
00075 double totaal=0;
00076 for(int i =0;i!=PMF_SIZE;i++){
00077 this.PMF[i]=PMF[i];
00078 totaal+=PMF[i];
00079 }
00080 if(totaal < 1-0.01 || totaal>1+0.01){
00081 Debug.print("arraytotaal"+commandline.Algorithms.ArrayHandler.totaal(PMF));
00082 Debug.print("totaal:= "+totaal);
00083 Debug.print("Error Huffman -> totaal van _count (PDF) is !=1");
00084 Object o=null;
00085 o.notify();
00086 System.exit(-1);
00087 }
00088
00089 compress();
00090 }
00091
00092
00093
00094
00095
00102 public String charCode(int index) throws IndexOutOfBoundsException {
00103 StringBuffer outString = new StringBuffer();
00104 int i;
00105
00106
00107 for(i = _len[index]; i > 0; i--)
00108 outString.append((_code[index] >> (i - 1)) & 1);
00109
00110
00111
00112
00113
00114
00115
00116
00117 return outString.toString();
00118 }
00119
00123 public ArrayList<String> getArrayListOfCodeWords(){
00124 ArrayList<String> codewords=new ArrayList<String>(PMF_SIZE);
00125 for(int i =0; i!=PMF_SIZE;i++){
00126 codewords.add(charCode(i));
00127 }
00128 return codewords;
00129 }
00130
00131
00136 private void compress() {
00137
00138 PriorityQueue pq = new PriorityQueue();
00139 Vector<Integer> path;
00140 int x, j, k, t;
00141 PairElement t1, t2;
00142
00143
00144
00145
00146
00147
00148
00149 for(int i = 0; i < PMF_SIZE; i++){
00150 if(PMF[i] > 0) {
00151 pq.insert(PairElementPool.getPairElement(i, PMF[i]));
00152 }
00153 }
00154
00155
00156
00157
00158
00159 int teller=PMF_SIZE;
00160 while(pq.size() > 1) {
00161 t1 = pq.removeMin();
00162 t2 = pq.removeMin();
00163 _dad[teller] = 0;
00164 _dad[t1.key] = teller;
00165 _dad[t2.key] = -teller;
00166 PMF[teller] = PMF[t1.key] + PMF[t2.key];
00167 if(!pq.isEmpty())
00168 pq.insert(new PairElement(teller,PMF[teller]));
00169 teller++;
00170 }
00171
00172
00173
00174 if(pq.size() == 1) {
00175 t1 = pq.removeMin();
00176 _dad[teller] = 0;
00177 _dad[t1.key] = teller;
00178 PMF[teller] = PMF[t1.key];
00179 teller++;
00180 }
00181
00182
00183 _root = teller - 1;
00184
00185
00186
00187
00188
00189
00190
00191
00192 for(k = 0; k < PMF_SIZE; k++) {
00193 path = new Vector();
00194 int i = 0;
00195 x = 0;
00196 j = 1;
00197
00198 if(PMF[k] != 0) {
00199
00200 t = _dad[k];
00201
00202 while(t != 0) {
00203 if(t < 0) {
00204 x += j;
00205 t = -t;
00206 }
00207 path.addElement(new Integer(t));
00208
00209 t = _dad[t];
00210 j += j;
00211 i++;
00212 }
00213 }
00214
00215 _code[k] = x;
00216 _len[k] = i;
00217 }
00218
00219
00220
00221
00222
00223
00224 }
00225
00226 public static void main(String args[]){
00227 double[] _count=new double[50];
00228 double totaal=0;
00229 for(int i=0;i!=_count.length;i++){
00230 _count[i]=(float)Math.random();
00231 totaal+=_count[i];
00232 }
00233
00234 for(int i =0;i!=_count.length;i++){
00235 _count[i]=_count[i]/totaal;
00236 }
00237
00238 Huffman h =new Huffman(_count);
00239 h.compress();
00240 String code;
00241 float COMPR=0;
00242 float ENTROPIE=0;
00243 for (int i =0;i!=_count.length;i++){
00244 code =h.charCode(i);
00245 Debug.print("de code van de het " +i+"-de woord met kans "+ h.PMF[i]+ " ="+ code);
00246 COMPR+=code.length()*h.PMF[i];
00247 ENTROPIE+=Math.log(h.PMF[i])*h.PMF[i];
00248 }
00249
00250 Debug.print("Entropie:= "+ (-ENTROPIE)+ " Symboollengte := " + COMPR + " Ongecomp. symb. lengte (log_2(N)):= "+ Math.log(_count.length)/Math.log(2));
00251 }
00252 }
00253
00260 class PriorityQueue extends Vector {
00266 public PriorityQueue() {
00267 super();
00268 }
00269
00277 public void insert(PairElement elem) {
00278 int i;
00279
00280 i = 0;
00281 while(i < size() &&
00282 ((PairElement) elementAt(i)).freq < elem.freq) {
00283 i++;
00284 }
00285
00286 insertElementAt(elem, i);
00287 }
00288
00295 public PairElement removeMin() {
00296 PairElement e;
00297
00298 if(size() > 0) {
00299 e = (PairElement) firstElement();
00300 removeElementAt(0);
00301
00302 return e;
00303 } else {
00304 return null;
00305 }
00306 }
00307 }
00308
00309
00313 class PairElementPool {
00314 private static int NBR=100;
00315 private static ArrayList<PairElement> inuseList=new ArrayList<PairElement>(NBR);
00316 private static ArrayList<PairElement> availableList=new ArrayList<PairElement>(NBR);
00317 private static int teller=0;
00318 public static PairElement getPairElement(int key, double freq){
00319 PairElement p;
00320 if(availableList.isEmpty()) {
00321 p=new PairElement(key,freq);
00322 inuseList.add(p);
00323 }
00324 else{
00325 p=availableList.remove(availableList.size()-1);
00326 inuseList.add(p);
00327 p.key=key;
00328 p.freq=freq;
00329 }
00330 return p;
00331 }
00332
00333 public static void reuse(){
00334
00335 availableList.addAll(inuseList);
00336 inuseList.clear();
00337 teller++;
00338 if(Debug.DEBUG && !Debug.DISTIBUTION){
00339 if(teller%100==0){
00340 System.out.println("reusing: # objects:=" + availableList.size());
00341 System.out.println("aantal keer huffman tabel:= "+teller);
00342 }
00343 }
00344 }
00345
00346 }
00347
00353 class PairElement extends Object {
00357 int key;
00361 double freq;
00367 public PairElement(int key, double freq) {
00368 super();
00369
00370 this.key = key;
00371 this.freq = freq;
00372 }
00373 }