From 78cefe9a8f016b42039accced638a3cebc9425c7 Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Tue, 11 Jul 2006 18:22:29 +0000 Subject: [PATCH] Added the values() function to the Map interface, as well as a new method, forEach(), in which you pass a visitor function that will visit each key/value stored in the map. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4257 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/as/com/threerings/util/Hashtable.as | 38 +++++++++++++++++++++---- src/as/com/threerings/util/Map.as | 11 +++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/as/com/threerings/util/Hashtable.as b/src/as/com/threerings/util/Hashtable.as index 2eddb4b02..2d57c4537 100644 --- a/src/as/com/threerings/util/Hashtable.as +++ b/src/as/com/threerings/util/Hashtable.as @@ -210,12 +210,7 @@ public class Hashtable for (var ii :int = _entries.length - 1; ii >= 0; ii--) { for (var e :Entry = (_entries[ii] as Entry); e != null; e = e.next) { - if (e.key is KeyWrapper) { - keys.push((e.key as KeyWrapper).key); - - } else { - keys.push(e.key) - } + keys.push(e.getOriginalKey()); } } } @@ -223,6 +218,7 @@ public class Hashtable return keys; } + // documentation inherited from interface Map public function values () :Array { var vals :Array = new Array(); @@ -247,6 +243,25 @@ public class Hashtable return vals; } + // documentation inherited from interface Map + public function forEach (fn :Function) :void + { + if (_simpleData != null) { + for (var key :Object in _simpleData) { + fn(key, _simpleData[key]); + } + } + + if (_entries != null) { + for (var ii :int = _entries.length - 1; ii >= 0; ii--) { + for (var e :Entry = (_entries[ii] as Entry); e != null; + e = e.next) { + fn(e.getOriginalKey(), e.value); + } + } + } + } + /** * Return a Hashable that represents the key. */ @@ -348,6 +363,17 @@ class Entry this.value = value; this.next = next; } + + /** + * Get the original key used to store this entry. + */ + public function getOriginalKey () :Object + { + if (key is KeyWrapper) { + return (key as KeyWrapper).key; + } + return key; + } } class KeyWrapper diff --git a/src/as/com/threerings/util/Map.as b/src/as/com/threerings/util/Map.as index 642cc514c..fe954ba5e 100644 --- a/src/as/com/threerings/util/Map.as +++ b/src/as/com/threerings/util/Map.as @@ -21,6 +21,12 @@ public interface Map */ function get (key :Object) :*; + /** + * Call the specified function, which accepts to args: key and value, + * for every mapping. + */ + function forEach (fn :Function) :void; + /** * Returns true if this map contains no elements. */ @@ -47,6 +53,11 @@ public interface Map * Return the current size of the map. */ function size () :int; + + /** + * Return all the values in this Map, in Array form. + */ + function values () :Array; } }