Skip to content

Commit

Permalink
Document the Histogram class
Browse files Browse the repository at this point in the history
  • Loading branch information
afeinberg committed Feb 15, 2012
1 parent 7900d32 commit 32744e3
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/java/voldemort/store/stats/Histogram.java
Expand Up @@ -5,7 +5,11 @@
import java.util.Arrays;

/**
* A class for computing percentiles based on a histogram
* A class for computing percentiles based on a histogram. Values are bucketed
* by a configurable bound (e.g., 0-1, 1-2, 2-3). When a value is inserted,
* perform a binary search to find the correct bucket.
*
*
*/
@NotThreadsafe
public class Histogram {
Expand All @@ -17,9 +21,10 @@ public class Histogram {
private int size;

/**
*
* @param nBuckets
* @param step
* Initialize an empty histogram
*
* @param nBuckets The number of buckets to use
* @param step The size of each bucket
*/
public Histogram(int nBuckets, int step) {
this.nBuckets = nBuckets;
Expand All @@ -38,24 +43,33 @@ protected void init() {
}

/**
*
* Reset the histogram back to empty (set all values to 0)
*/
public void reset() {
Arrays.fill(buckets, 0);
size = 0;
}

/**
*
* @param data
* Insert a value into the right bucket of the histogram. If the value is
* larger than any bound, insert into the last bucket
*
* @param data The value to insert into the histogram
*/
public void insert(int data) {
int index = findBucket(data);
assert(index != -1);
buckets[index]++;
size++;
}


/**
* Find the a value <em>n</em> such that the percentile falls within
* [<em>n</em>, <em>n + step</em>)
*
* @param quantile The percentile to find
* @return Lower bound associated with the percentile
*/
public int getQuantile(double quantile) {
int total = 0;
for(int i = 0; i < nBuckets; i++) {
Expand Down Expand Up @@ -92,7 +106,7 @@ private int findBucket(int needle) {
private int compareToBucket(int bucket, int needle) {
int low = bounds[bucket];
int high = low + step;
if (low <= needle && high > needle) {
if(low <= needle && high > needle) {
return 0;
} else if(low > needle) {
return 1;
Expand Down

0 comments on commit 32744e3

Please sign in to comment.