From 4bc5627d829d961f597131c558e73d105d1cc452 Mon Sep 17 00:00:00 2001 From: Par Winzell Date: Tue, 4 Nov 2008 18:21:29 +0000 Subject: [PATCH] Let's have Set implement forEach, like Map does. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5500 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/as/com/threerings/util/ExpiringSet.as | 8 ++++++++ src/as/com/threerings/util/HashSet.as | 7 +++++++ src/as/com/threerings/util/Set.as | 3 +++ 3 files changed, 18 insertions(+) diff --git a/src/as/com/threerings/util/ExpiringSet.as b/src/as/com/threerings/util/ExpiringSet.as index 9caad76d8..3e5c6ec45 100644 --- a/src/as/com/threerings/util/ExpiringSet.as +++ b/src/as/com/threerings/util/ExpiringSet.as @@ -62,6 +62,14 @@ public class ExpiringSet extends EventDispatcher _data.length = 0; } + // from Set + public function forEach (fn :Function) :void + { + for each (var e :ExpiringElement in _data) { + fn(e); + } + } + // from Set public function size () :int { diff --git a/src/as/com/threerings/util/HashSet.as b/src/as/com/threerings/util/HashSet.as index 996528385..99e4f722b 100644 --- a/src/as/com/threerings/util/HashSet.as +++ b/src/as/com/threerings/util/HashSet.as @@ -76,6 +76,13 @@ public class HashSet return (_hashMap.containsKey(o)); } + public function forEach (fn :Function) :void + { + _hashMap.forEach(function (key :Object, val :Object) :void { + fn(key); + }); + } + public function size () :int { return (_hashMap.size()); diff --git a/src/as/com/threerings/util/Set.as b/src/as/com/threerings/util/Set.as index 0be6c7ea3..e527c8aa8 100644 --- a/src/as/com/threerings/util/Set.as +++ b/src/as/com/threerings/util/Set.as @@ -41,6 +41,9 @@ public interface Set /** Returns true if this set contains the specified element. */ function contains (o :Object) :Boolean; + /** Call the specified function, which accepts an element as an argument. */ + function forEach (fn :Function) :void; + /** Retuns the number of elements in this set. */ function size () :int; // @TSC - should this be uint?