1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package androidx.constraintlayout.core; 17 18 /** 19 * Utility to draw an histogram 20 */ 21 public class HistogramCounter { 22 long[] mCalls = new long[256]; 23 final String mName; 24 inc(int value)25 public void inc(int value) { 26 if (value < 255) { 27 mCalls[value]++; 28 } else { 29 mCalls[255]++; 30 } 31 } 32 HistogramCounter(String name)33 public HistogramCounter(String name) { 34 this.mName = name; 35 } 36 reset()37 public void reset() { 38 mCalls = new long[256]; 39 } 40 print(long n)41 private String print(long n) { 42 String ret = ""; 43 for (int i = 0; i < n; i++) { 44 ret += "X"; 45 } 46 return ret; 47 } 48 49 @Override toString()50 public String toString() { 51 String ret = mName + " :\n"; 52 int lastValue = 255; 53 for (int i = 255; i >= 0; i--) { 54 if (mCalls[i] != 0) { 55 lastValue = i; 56 break; 57 } 58 } 59 int total = 0; 60 for (int i = 0; i <= lastValue; i++) { 61 ret += "[" + i + "] = " + mCalls[i] + " -> " + print(mCalls[i]) + "\n"; 62 total += mCalls[i]; 63 } 64 ret += "Total calls " + total; 65 return ret; 66 } 67 } 68