ai.platon.pulsar.common.Histogram Maven / Gradle / Ivy
/*******************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package ai.platon.pulsar.common;
import java.util.*;
/**
* Histogram class.
*
* @author vincent
* @version $Id: $Id
*/
public class Histogram {
private Map map = new HashMap();
private float totalValue = 0;
private int totalCount = 0;
/**
* add.
*
* @param x a E object.
*/
public void add(E x) {
add(x, 1);
}
/**
* add.
*
* @param x a E object.
* @param value a float.
*/
public void add(E x, float value) {
HistogramEntry entry;
if (map.containsKey(x)) {
entry = map.get(x);
entry.value += value;
entry.count++;
} else {
entry = new HistogramEntry();
entry.value = value;
entry.count = 1;
map.put(x, entry);
}
totalValue += value;
totalCount += 1;
}
/**
* getKeys.
*
* @return a {@link java.util.Set} object.
*/
public Set getKeys() {
return map.keySet();
}
/**
* getValue.
*
* @param x a E object.
* @return a float.
*/
public float getValue(E x) {
return map.get(x).value;
}
/**
* getCount.
*
* @param x a E object.
* @return a int.
*/
public int getCount(E x) {
return map.get(x).count;
}
/**
* add.
*
* @param other a {@link ai.platon.pulsar.common.Histogram} object.
*/
public void add(Histogram other) {
for (E x : other.getKeys()) {
add(x, other.getValue(x));
}
}
/**
* normalize.
*
* @return a {@link ai.platon.pulsar.common.Histogram} object.
*/
public Histogram normalize() {
Histogram normalized = new Histogram();
Set keys = getKeys();
for (E x : keys) {
normalized.add(x, getValue(x) / totalValue);
}
return normalized;
}
/**
* sortInverseByValue.
*
* @return a {@link java.util.List} object.
*/
public List sortInverseByValue() {
List> list = new Vector>(
map.entrySet());
// Sort the list using an annonymous inner class implementing Comparator for
// the compare method
java.util.Collections.sort(list,
new Comparator>() {
public int compare(Map.Entry entry,
Map.Entry entry1) {
return (entry.getValue().equals(entry1.getValue()) ? 0 : (entry
.getValue().value < entry1.getValue().value ? 1 : -1));
}
});
List list2 = new Vector();
for (Map.Entry entry : list) {
list2.add(entry.getKey());
}
return list2;
}
/**
* sortByValue.
*
* @return a {@link java.util.List} object.
*/
public List sortByValue() {
List> list = new Vector>(
map.entrySet());
// Sort the list using an annonymous inner class implementing Comparator for
// the compare method
java.util.Collections.sort(list,
new Comparator>() {
public int compare(Map.Entry entry,
Map.Entry entry1) {
return (entry.getValue().equals(entry1.getValue()) ? 0 : (entry
.getValue().value > entry1.getValue().value ? 1 : -1));
}
});
List list2 = new Vector();
for (Map.Entry entry : list) {
list2.add(entry.getKey());
}
return list2;
}
/**
* toString.
*
* @param items a {@link java.util.List} object.
* @return a {@link java.lang.String} object.
*/
public String toString(List items) {
StringBuilder strBuilder = new StringBuilder();
for (E item : items) {
strBuilder.append(map.get(item).value).append(",").append(item)
.append("\n");
}
return strBuilder.toString();
}
class HistogramEntry {
float value;
int count;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy