Path: blob/master/src/java.desktop/share/classes/sun/java2d/marlin/stats/Histogram.java
41161 views
/*1* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d.marlin.stats;2627/**28* Generic histogram based on long statistics29*/30public final class Histogram extends StatLong {3132static final int BUCKET = 2;33static final int MAX = 20;34static final int LAST = MAX - 1;35static final int[] STEPS = new int[MAX];3637static {38STEPS[0] = 0;39STEPS[1] = 1;4041for (int i = 2; i < MAX; i++) {42STEPS[i] = STEPS[i - 1] * BUCKET;43}44}4546static int bucket(int val) {47for (int i = 1; i < MAX; i++) {48if (val < STEPS[i]) {49return i - 1;50}51}52return LAST;53}5455private final StatLong[] stats = new StatLong[MAX];5657public Histogram(final String name) {58super(name);59for (int i = 0; i < MAX; i++) {60stats[i] = new StatLong(String.format("%5s .. %5s", STEPS[i],61((i + 1 < MAX) ? STEPS[i + 1] : "~")));62}63}6465@Override66public void reset() {67super.reset();68for (int i = 0; i < MAX; i++) {69stats[i].reset();70}71}7273@Override74public void add(int val) {75super.add(val);76stats[bucket(val)].add(val);77}7879@Override80public void add(long val) {81add((int) val);82}8384@Override85public String toString() {86final StringBuilder sb = new StringBuilder(2048);87super.toString(sb).append(" { ");8889for (int i = 0; i < MAX; i++) {90if (stats[i].count != 0l) {91sb.append("\n ").append(stats[i].toString());92}93}9495return sb.append(" }").toString();96}97}9899100101