Allow the initial bucket range to be passed to the percentiler on creation. As long as the recorded values stay within the range, the percentiler won't do its 20% greater than range expansion, so the values reported by getRequiredScore will reflect the initial min-max.

git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@728 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
Charlie Groves
2008-08-12 01:45:59 +00:00
parent 8f7b90d131
commit 5982369b2a
@@ -42,6 +42,15 @@ public class Percentiler
{ {
} }
/**
* Creates a percentiler that expects values to fall within the given range.
*/
public Percentiler(int min, int max) {
_min = min;
_max = max;
_fixedRange = true;
}
/** /**
* Creates a percentiler from its serialized representation. * Creates a percentiler from its serialized representation.
*/ */
@@ -88,13 +97,18 @@ public class Percentiler
public void recordValue (float value, boolean logNewMax) public void recordValue (float value, boolean logNewMax)
{ {
// if this is the first value ever recorded; note our min and max // if this is the first value ever recorded; note our min and max
if (_total == 0) { if (_total == 0 && !_fixedRange) {
_min = (int)Math.floor(value); _min = (int)Math.floor(value);
_max = Math.max((int)Math.ceil(value), _min+1); _max = Math.max((int)Math.ceil(value), _min + 1);
} }
// if this value is outside our bounds, we need to redistribute our buckets // if this value is outside our bounds, we need to redistribute our buckets
if (value < _min || value > _max) { if (value < _min || value > _max) {
if (_fixedRange) {
log.warning("Recording value outside of initially fixed range", "min", _min,
"max", _max, "value", value);
_fixedRange = false;
}
// expand by 20% in the direction of either our new minimum or new maximum // expand by 20% in the direction of either our new minimum or new maximum
int newmin = (value < _min) ? (_max - (int)Math.ceil((_max - value) * 1.2f)) : _min; int newmin = (value < _min) ? (_max - (int)Math.ceil((_max - value) * 1.2f)) : _min;
int newmax = (value > _max) ? (_min + (int)Math.ceil((value - _min) * 1.2f)) : _max; int newmax = (value > _max) ? (_min + (int)Math.ceil((value - _min) * 1.2f)) : _max;
@@ -384,6 +398,9 @@ public class Percentiler
return idx; return idx;
} }
/** If this Percentiler was created with a fixed range. */
protected boolean _fixedRange;
/** The total number of data points seen by this percentiler. */ /** The total number of data points seen by this percentiler. */
protected long _total; protected long _total;