[hist4j commit] r13 - trunk/src/com/flaptor/hist4j

0 views
Skip to first unread message

codesite...@google.com

unread,
Jan 12, 2009, 4:21:19 PM1/12/09
to flaptor-o...@googlegroups.com
Author: jhandl
Date: Mon Jan 12 13:07:39 2009
New Revision: 13

Modified:
trunk/src/com/flaptor/hist4j/AdaptiveHistogram.java
trunk/src/com/flaptor/hist4j/HistogramDataNode.java

Log:
bug fixes


Modified: trunk/src/com/flaptor/hist4j/AdaptiveHistogram.java
==============================================================================
--- trunk/src/com/flaptor/hist4j/AdaptiveHistogram.java (original)
+++ trunk/src/com/flaptor/hist4j/AdaptiveHistogram.java Mon Jan 12 13:07:39
2009
@@ -1,19 +1,15 @@
/*
Copyright 2007 Flaptor (flaptor.com)
-
Licensed 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
-
+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 com.flaptor.hist4j;

import java.io.Serializable;
@@ -31,17 +27,16 @@
* data set at a given percentile.
* @author Jorge Handl
*/
-
public class AdaptiveHistogram implements Serializable {

- private static final long serialVersionUID = -1L;
- private long totalCount; // total number of data points
+ private static final long serialVersionUID = -1L;
+ private long totalCount; // total number of data points
private HistogramNode root; // root of the tree

/**
* Class constructor.
*/
- public AdaptiveHistogram () {
+ public AdaptiveHistogram() {
root = null;
reset();
}
@@ -49,7 +44,7 @@
/**
* Erases all data from the histogram.
*/
- public void reset () {
+ public void reset() {
if (null != root) {
root.reset();
root = null;
@@ -61,9 +56,11 @@
* Adds a data point to the histogram.
* @param value the data point to add.
*/
- public void addValue (float value) {
+ public synchronized void addValue(float value) {
totalCount++;
- if (null == root) root = new HistogramDataNode();
+ if (null == root) {
+ root = new HistogramDataNode();
+ }
root = root.addValue(this, value);
}

@@ -72,7 +69,7 @@
* @param value the reference data point.
* @return the number of data points stored in the same bucket as the
reference point.
*/
- public long getCount (float value) {
+ public long getCount(float value) {
long count = 0;
if (null != root) {
count = root.getCount(value);
@@ -85,7 +82,7 @@
* @param value the reference data point.
* @return the cumulative density function for the reference point.
*/
- public long getAccumCount (float value) {
+ public long getAccumCount(float value) {
long count = 0;
if (null != root) {
count = root.getAccumCount(value);
@@ -98,16 +95,13 @@
* @param percentile the percentile at which the data set is split.
* @return the data point that splits the data set at the given
percentile.
*/
- public float getValueForPercentile (int percentile) {
+ public float getValueForPercentile(int percentile) {
long targetAccumCount = (totalCount * percentile) / 100;
Float value = new Float(0);
if (null != root) {
- value = root.getValueForAccumCount(new long[] {0,
targetAccumCount});
- if (null == value) {
- return 0;
- }
+ value = root.getValueForAccumCount(new long[]{0,
targetAccumCount});
}
- return value.floatValue();
+ return (null != value) ? value.floatValue() : null;
}

/**
@@ -115,8 +109,8 @@
* limit of data points that should be counted at one bucket.
* @return the limit of data points to store a one bucket.
*/
- protected int getCountPerNodeLimit () {
- int limit = (int)(totalCount / 10);
+ protected int getCountPerNodeLimit() {
+ int limit = (int) (totalCount / 10);
if (0 == limit) {
limit = 1;
}
@@ -132,7 +126,7 @@
* @param value the input value.
* @return the resulting converted value.
*/
- float convertValue (float value);
+ float convertValue(float value);
}

/**
@@ -140,36 +134,36 @@
* @param targetMin the target new minimum value.
* @param targetMax the target new maximum value.
*/
- public void normalize (float targetMin, float targetMax) {
+ public void normalize(float targetMin, float targetMax) {
if (null != root) {
final float min = getValueForPercentile(0);
final float max = getValueForPercentile(100);
final float m = (targetMax - targetMin) * ((max > min) ? 1 /
(max - min) : 1);
final float b = targetMin;
- root.apply(new ValueConversion() { public float
convertValue(float value) { return m*(value-min)+b; } });
+ root.apply(new ValueConversion() { public float
convertValue(float value) { return m * (value - min) + b; } });
}
}

/**
* Shows the histograms' underlying data structure.
*/
- public void show () {
- System.out.println("Histogram has "+totalCount+" nodes:");
+ public void show() {
+ System.out.println("Histogram has " + totalCount + " values:");
if (null != root) {
root.show(0);
}
}
-
+
/**
* Return a table representing the data in this histogram.
* Each element is a table cell containing the range limit values and
the count for that range.
*/
- public ArrayList<Cell> toTable () {
- ArrayList<Cell> table = new ArrayList<Cell>();
- if (null != root) {
- root.toTable(table);
- }
- return table;
+ public ArrayList<Cell> toTable() {
+ ArrayList<Cell> table = new ArrayList<Cell>();
+ if (null != root) {
+ root.toTable(table);
+ }
+ return table;
}

}

Modified: trunk/src/com/flaptor/hist4j/HistogramDataNode.java
==============================================================================
--- trunk/src/com/flaptor/hist4j/HistogramDataNode.java (original)
+++ trunk/src/com/flaptor/hist4j/HistogramDataNode.java Mon Jan 12 13:07:39
2009
@@ -17,7 +17,6 @@

package com.flaptor.hist4j;

-import java.io.Serializable;
import java.util.ArrayList;

/**
@@ -78,9 +77,10 @@
// "self" is what is returned to the caller. If this node needs to
be replaced by a fork node,
// this variable will hold the new fork node and it will be
returned to the caller.
// Otherwise, the node returned will be this, in which case
nothing changes.
- HistogramNode self = this;
+ HistogramNode self = this;
if (value >= cell.minValue && value <= cell.maxValue) { // the
value falls within this nodes' range
- if (cell.count < root.getCountPerNodeLimit()) { // there is
enough room in this node for the new value
+ if (cell.count < root.getCountPerNodeLimit() // there is
enough room in this node for the new value
+ || cell.minValue == cell.maxValue) { // or the node
defines a zero-width range so it can't be split
cell.count++;
} else { // not enough room, distribute the value count among
the new nodes, assuming uniform distribution
float splitValue = (cell.minValue + cell.maxValue) / 2;
@@ -188,7 +188,7 @@
*/
public void show (int level) {
margin(level);
- System.out.println("Data: " + cell.count + "
("+cell.minValue+"-"+cell.maxValue+")");
+ System.out.println("Data: " + cell.count + "
("+cell.minValue+","+cell.maxValue+")");
}

/**

Reply all
Reply to author
Forward
0 new messages