From bb8718463277c70c5288e5fb721eb39ec420f9a4 Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Fri, 17 Apr 2009 20:01:01 +0000 Subject: [PATCH] Should resolve infinite recursion with SortedHashMap. Tip from the pros: when you change a class, you might want to check the subclasses for possible problems with that change. :P git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5729 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/as/com/threerings/util/HashMap.as | 13 ++++++++++-- src/as/com/threerings/util/SortedHashMap.as | 10 ++++------ .../com/threerings/util/WeakValueHashMap.as | 20 ++++++------------- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/as/com/threerings/util/HashMap.as b/src/as/com/threerings/util/HashMap.as index b4890fadc..636d4a51d 100644 --- a/src/as/com/threerings/util/HashMap.as +++ b/src/as/com/threerings/util/HashMap.as @@ -216,7 +216,7 @@ public class HashMap public function keys () :Array { var keys :Array = []; - forEach(function (k :*, v :*) :void { + forEach0(function (k :*, v :*) :void { keys.push(k); }); return keys; @@ -226,7 +226,7 @@ public class HashMap public function values () :Array { var vals :Array = []; - forEach(function (k :*, v :*) :void { + forEach0(function (k :*, v :*) :void { vals.push(v); }); return vals; @@ -234,6 +234,15 @@ public class HashMap /** @inheritDoc */ public function forEach (fn :Function) :void + { + forEach0(fn); + } + + /** + * Internal forEach. + * @private + */ + protected function forEach0 (fn :Function) :void { if (_simpleData != null) { for (var key :Object in _simpleData) { diff --git a/src/as/com/threerings/util/SortedHashMap.as b/src/as/com/threerings/util/SortedHashMap.as index 4e41f5eeb..df8746706 100644 --- a/src/as/com/threerings/util/SortedHashMap.as +++ b/src/as/com/threerings/util/SortedHashMap.as @@ -69,12 +69,10 @@ public class SortedHashMap extends HashMap override public function values () :Array { - var keys :Array = keys(); - var vals :Array = new Array(); - for each (var key :Object in keys) { - vals.push(get(key)); - } - return vals; + // not very optimal, we need to look up each value from the sorted keys. + return keys().map(function (key :Object, ... rest) :Object { + return get(key); + }); } override public function forEach (fn :Function) :void diff --git a/src/as/com/threerings/util/WeakValueHashMap.as b/src/as/com/threerings/util/WeakValueHashMap.as index 8731c9eb0..97d849f8e 100644 --- a/src/as/com/threerings/util/WeakValueHashMap.as +++ b/src/as/com/threerings/util/WeakValueHashMap.as @@ -49,28 +49,19 @@ public class WeakValueHashMap extends HashMap /** @inheritDoc */ override public function size () :int { - prune(); + forEach0(function (k :*, v: *) :void {}); return super.size(); // alternately, we could prune and increment a count for all present.. } - /** @inheritDoc */ - override public function forEach (fn :Function) :void - { - prune(fn); // just prune & run the forEach at the same time - } - - /** - * Prune garbage collected values from the map, optionally calling a function for each - * found key/value pair. - */ - protected function prune (fn :Function = null) :void + /** @private */ + override protected function forEach0 (fn :Function) :void { var removeKeys :Array = []; - super.forEach(function (key :*, value :WeakReference) :void { + super.forEach0(function (key :*, value :WeakReference) :void { var rawVal :* = (value == null) ? null : value.get(); if (rawVal === undefined) { removeKeys.push(key); - } else if (fn != null) { + } else { fn(key, rawVal); } }); @@ -81,6 +72,7 @@ public class WeakValueHashMap extends HashMap /** * Unwrap a possible WeakReference for returning to the user. + * @private */ protected function unwrap (val :*) :* {