From 4633684fb696ec7329f310d83f2cc56eae2273c3 Mon Sep 17 00:00:00 2001 From: Jamie Doornbos Date: Tue, 4 Nov 2008 06:41:00 +0000 Subject: [PATCH] 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 --- src/as/com/threerings/util/ArrayUtil.as | 8 ++++++++ src/as/com/threerings/util/Throttle.as | 23 +++++++++++++++-------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/as/com/threerings/util/ArrayUtil.as b/src/as/com/threerings/util/ArrayUtil.as index 4fc97f69e..e2cf2b9df 100644 --- a/src/as/com/threerings/util/ArrayUtil.as +++ b/src/as/com/threerings/util/ArrayUtil.as @@ -228,6 +228,14 @@ public class ArrayUtil 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. */ diff --git a/src/as/com/threerings/util/Throttle.as b/src/as/com/threerings/util/Throttle.as index 86ee81e67..48857fbb2 100644 --- a/src/as/com/threerings/util/Throttle.as +++ b/src/as/com/threerings/util/Throttle.as @@ -77,14 +77,21 @@ public class Throttle public function reinit (operations :int, period :int) :void { var ops :Array = ArrayUtil.create(operations, 0); - // copy as much as we can of our most recent operations into our new array - var copyCount :int = Math.min(operations, _ops.length); - var srcOffset :int = _oldestOp + _ops.length*2 - 1; // need *2 to prevent copying _ops[-1] - var destOffset :int = operations-1; - for (var ii :int = 0; ii < copyCount; ii++) { - ops[destOffset-ii] = _ops[(srcOffset-ii) % _ops.length]; + var ii :int; + var oldestOp :int; + if (operations > _ops.length) { + // copy to a larger buffer, leaving zeroes at the beginning + oldestOp = _oldestOp + operations - _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; _period = period; } @@ -171,7 +178,7 @@ public class Throttle var now :int = getTimer(); var ages :Array = [] 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 + "]"; }