Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/java2d/marlin/stats/Histogram.java
38918 views
/*1* Copyright (c) 2015, 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;2627import java.util.Arrays;2829/**30* Generic histogram based on long statistics31*/32public final class Histogram extends StatLong {3334static final int BUCKET = 2;35static final int MAX = 20;36static final int LAST = MAX - 1;37static final int[] STEPS = new int[MAX];3839static {40STEPS[0] = 0;41STEPS[1] = 1;4243for (int i = 2; i < MAX; i++) {44STEPS[i] = STEPS[i - 1] * BUCKET;45}46// System.out.println("Histogram.STEPS = " + Arrays.toString(STEPS));47}4849static int bucket(int val) {50for (int i = 1; i < MAX; i++) {51if (val < STEPS[i]) {52return i - 1;53}54}55return LAST;56}5758private final StatLong[] stats = new StatLong[MAX];5960public Histogram(final String name) {61super(name);62for (int i = 0; i < MAX; i++) {63stats[i] = new StatLong(String.format("%5s .. %5s", STEPS[i],64((i + 1 < MAX) ? STEPS[i + 1] : "~")));65}66}6768@Override69public void reset() {70super.reset();71for (int i = 0; i < MAX; i++) {72stats[i].reset();73}74}7576@Override77public void add(int val) {78super.add(val);79stats[bucket(val)].add(val);80}8182@Override83public void add(long val) {84add((int) val);85}8687@Override88public String toString() {89final StringBuilder sb = new StringBuilder(2048);90super.toString(sb).append(" { ");9192for (int i = 0; i < MAX; i++) {93if (stats[i].count != 0l) {94sb.append("\n ").append(stats[i].toString());95}96}9798return sb.append(" }").toString();99}100}101102103104