Copied code from java implementation of Throttle.reinit(). After testing, I found this method was leaving older values in the middle of younger values

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5497 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Jamie Doornbos
2008-11-04 06:41:00 +00:00
parent be8597a5c1
commit 4633684fb6
2 changed files with 23 additions and 8 deletions
+8
View File
@@ -228,6 +228,14 @@ public class ArrayUtil
return true; return true;
} }
public static function copy (
src :Array, srcoffset :uint, dst :Array, dstoffset :uint, count :uint) :void
{
for (var ii :uint = 0; ii < count; ++ii) {
dst[dstoffset++] = src[srcoffset++];
}
}
/** /**
* Implementation of remove methods. * Implementation of remove methods.
*/ */
+15 -8
View File
@@ -77,14 +77,21 @@ public class Throttle
public function reinit (operations :int, period :int) :void public function reinit (operations :int, period :int) :void
{ {
var ops :Array = ArrayUtil.create(operations, 0); var ops :Array = ArrayUtil.create(operations, 0);
// copy as much as we can of our most recent operations into our new array var ii :int;
var copyCount :int = Math.min(operations, _ops.length); var oldestOp :int;
var srcOffset :int = _oldestOp + _ops.length*2 - 1; // need *2 to prevent copying _ops[-1] if (operations > _ops.length) {
var destOffset :int = operations-1; // copy to a larger buffer, leaving zeroes at the beginning
for (var ii :int = 0; ii < copyCount; ii++) { oldestOp = _oldestOp + operations - _ops.length;
ops[destOffset-ii] = _ops[(srcOffset-ii) % _ops.length]; ArrayUtil.copy(_ops, 0, ops, 0, _oldestOp);
} else if (operations < _ops.length) {
// copy to a smaller buffer, truncating older operations
oldestOp = (_oldestOp + _ops.length - operations) % _ops.length;
var endCount :int = Math.min(_ops.length - oldestOp, operations);
ArrayUtil.copy(_ops, oldestOp, ops, 0, endCount);
ArrayUtil.copy(_ops, 0, ops, endCount, operations - endCount);
_oldestOp = 0;
} }
_oldestOp = Math.max(0, operations-_ops.length);
_ops = ops; _ops = ops;
_period = period; _period = period;
} }
@@ -171,7 +178,7 @@ public class Throttle
var now :int = getTimer(); var now :int = getTimer();
var ages :Array = [] var ages :Array = []
for (var ii :int = 0; ii < _ops.length; ++ii) { for (var ii :int = 0; ii < _ops.length; ++ii) {
ages.push(now - _ops[(_oldestOp + ii) % _ops.length]); ages.push(now - _ops[ii]);
} }
return "[" + StringUtil.toString(ages) + "] [Oldest idx = " + _oldestOp + "]"; return "[" + StringUtil.toString(ages) + "] [Oldest idx = " + _oldestOp + "]";
} }