From 8929c2e89e3d8c4f9626139ec3f3a50381647b87 Mon Sep 17 00:00:00 2001 From: Tom Conkling Date: Wed, 20 Feb 2008 00:40:43 +0000 Subject: [PATCH] added RingBuffer.every(), RingBuffer.forEach(), RingBuffer.indexOf() git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4940 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/as/com/threerings/util/RingBuffer.as | 45 ++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/as/com/threerings/util/RingBuffer.as b/src/as/com/threerings/util/RingBuffer.as index 8d1b9b547..233aa9328 100644 --- a/src/as/com/threerings/util/RingBuffer.as +++ b/src/as/com/threerings/util/RingBuffer.as @@ -171,6 +171,51 @@ public class RingBuffer return _array[index]; } } + + /** + * Executes a test function on each item in the ring buffer + * until an item is reached that returns false for the specified + * function. + * + * Returns a Boolean value of true if all items in the buffer return + * true for the specified function; otherwise, false. + */ + public function every (callback :Function, thisObject :* = null) :Boolean + { + for (var i :int = 0; i < _length; ++i) { + if (!callback.apply(thisObject, this.at(i))) { + return false; + } + } + + return true; + } + + /** + * Executes a function on each item in the ring buffer. + */ + public function forEach (callback :Function, thisObject :* = null) :void + { + for (var i :int = 0; i < _length; ++i) { + callback.apply(thisObject, this.at(i)); + } + } + + /** + * Searches for an item in the ring buffer by using strict equality + * (===) and returns the index position of the item, or -1 + * if the item is not found. + */ + public function indexOf (searchElement :*, fromIndex :int = 0) :int + { + for (var i :int = 0; i < _length; ++i) { + if (this.at(i) === searchElement) { + return i; + } + } + + return -1; + } protected var _array :Array = new Array(); protected var _capacity :uint;