jdk/src/share/classes/com/sun/java/util/jar/pack/Histogram.java
changeset 7795 98021fc612af
parent 7192 445c518364c4
child 7816 55a18147b4bf
equal deleted inserted replaced
7551:dc77388d186a 7795:98021fc612af
     1 /*
     1 /*
     2  * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    32 
    32 
    33 /**
    33 /**
    34  * Histogram derived from an integer array of events (int[]).
    34  * Histogram derived from an integer array of events (int[]).
    35  * @author John Rose
    35  * @author John Rose
    36  */
    36  */
    37 class Histogram {
    37 final class Histogram {
    38     // Compact histogram representation:  4 bytes per distinct value,
    38     // Compact histogram representation:  4 bytes per distinct value,
    39     // plus 5 words per distinct count.
    39     // plus 5 words per distinct count.
    40     protected final int[][] matrix;  // multi-row matrix {{counti,valueij...}}
    40     protected final int[][] matrix;  // multi-row matrix {{counti,valueij...}}
    41     protected final int     totalWeight;  // sum of all counts
    41     protected final int     totalWeight;  // sum of all counts
    42 
    42 
   302     }
   302     }
   303 
   303 
   304     public
   304     public
   305     String[] getRowTitles(String name) {
   305     String[] getRowTitles(String name) {
   306         int totalUnique = getTotalLength();
   306         int totalUnique = getTotalLength();
   307         int totalWeight = getTotalWeight();
   307         int ltotalWeight = getTotalWeight();
   308         String[] histTitles = new String[matrix.length];
   308         String[] histTitles = new String[matrix.length];
   309         int cumWeight = 0;
   309         int cumWeight = 0;
   310         int cumUnique = 0;
   310         int cumUnique = 0;
   311         for (int i = 0; i < matrix.length; i++) {
   311         for (int i = 0; i < matrix.length; i++) {
   312             int count  = getRowFrequency(i);
   312             int count  = getRowFrequency(i);
   313             int unique = getRowLength(i);
   313             int unique = getRowLength(i);
   314             int weight = getRowWeight(i);
   314             int weight = getRowWeight(i);
   315             cumWeight += weight;
   315             cumWeight += weight;
   316             cumUnique += unique;
   316             cumUnique += unique;
   317             long wpct = ((long)cumWeight * 100 + totalWeight/2) / totalWeight;
   317             long wpct = ((long)cumWeight * 100 + ltotalWeight/2) / ltotalWeight;
   318             long upct = ((long)cumUnique * 100 + totalUnique/2) / totalUnique;
   318             long upct = ((long)cumUnique * 100 + totalUnique/2) / totalUnique;
   319             double len = getRowBitLength(i);
   319             double len = getRowBitLength(i);
   320             assert(0.1 > Math.abs(len - getBitLength(matrix[i][1])));
   320             assert(0.1 > Math.abs(len - getBitLength(matrix[i][1])));
   321             histTitles[i] = name+"["+i+"]"
   321             histTitles[i] = name+"["+i+"]"
   322                 +" len="+round(len,10)
   322                 +" len="+round(len,10)
   344     /** Print a report of this histogram.
   344     /** Print a report of this histogram.
   345      */
   345      */
   346     public
   346     public
   347     void print(String name, String[] histTitles, PrintStream out) {
   347     void print(String name, String[] histTitles, PrintStream out) {
   348         int totalUnique = getTotalLength();
   348         int totalUnique = getTotalLength();
   349         int totalWeight = getTotalWeight();
   349         int ltotalWeight = getTotalWeight();
   350         double tlen = getBitLength();
   350         double tlen = getBitLength();
   351         double avgLen = tlen / totalWeight;
   351         double avgLen = tlen / ltotalWeight;
   352         double avg = (double) totalWeight / totalUnique;
   352         double avg = (double) ltotalWeight / totalUnique;
   353         String title = (name
   353         String title = (name
   354                         +" len="+round(tlen,10)
   354                         +" len="+round(tlen,10)
   355                         +" avgLen="+round(avgLen,10)
   355                         +" avgLen="+round(avgLen,10)
   356                         +" weight("+totalWeight+")"
   356                         +" weight("+ltotalWeight+")"
   357                         +" unique["+totalUnique+"]"
   357                         +" unique["+totalUnique+"]"
   358                         +" avgWeight("+round(avg,100)+")");
   358                         +" avgWeight("+round(avg,100)+")");
   359         if (histTitles == null) {
   359         if (histTitles == null) {
   360             out.println(title);
   360             out.println(title);
   361         } else {
   361         } else {
   362             out.println(title+" {");
   362             out.println(title+" {");
   363             StringBuffer buf = new StringBuffer();
   363             StringBuffer buf = new StringBuffer();
   364             for (int i = 0; i < matrix.length; i++) {
   364             for (int i = 0; i < matrix.length; i++) {
   365                 buf.setLength(0);
   365                 buf.setLength(0);
   366                 buf.append("  "+histTitles[i]+" {");
   366                 buf.append("  ").append(histTitles[i]).append(" {");
   367                 for (int j = 1; j < matrix[i].length; j++) {
   367                 for (int j = 1; j < matrix[i].length; j++) {
   368                     buf.append(" "+matrix[i][j]);
   368                     buf.append(" ").append(matrix[i][j]);
   369                 }
   369                 }
   370                 buf.append(" }");
   370                 buf.append(" }");
   371                 out.println(buf);
   371                 out.println(buf);
   372             }
   372             }
   373             out.println("}");
   373             out.println("}");
   601 
   601 
   602     /** Clone and sort the array, if not already sorted. */
   602     /** Clone and sort the array, if not already sorted. */
   603     private static
   603     private static
   604     int[] maybeSort(int[] values) {
   604     int[] maybeSort(int[] values) {
   605         if (!isSorted(values, 0, false)) {
   605         if (!isSorted(values, 0, false)) {
   606             values = (int[]) values.clone();
   606             values = values.clone();
   607             Arrays.sort(values);
   607             Arrays.sort(values);
   608         }
   608         }
   609         return values;
   609         return values;
   610     }
   610     }
   611 
   611