From 3300421741055290357ca6732eb19f71cedac11d Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Thu, 16 Apr 2009 22:51:12 +0000 Subject: [PATCH] - Use [] instead of "new Array()" - Implement keys() and values() using forEach(). - Less code is better code. - But, it's less efficient: - values() now looks up simple-key values by hashing - values() now unwraps non-simple keys, even though unneeded - keys() looks up simple-key values, even though unneeded Say it with me like a 14-year-old girl: whatEVER! git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5724 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/as/com/threerings/util/HashMap.as | 50 ++++++--------------------- 1 file changed, 10 insertions(+), 40 deletions(-) diff --git a/src/as/com/threerings/util/HashMap.as b/src/as/com/threerings/util/HashMap.as index 7a7cc6a30..b4890fadc 100644 --- a/src/as/com/threerings/util/HashMap.as +++ b/src/as/com/threerings/util/HashMap.as @@ -131,7 +131,7 @@ public class HashMap // lazy-create the array holding other hashables if (_entries == null) { - _entries = new Array(); + _entries = []; _entries.length = DEFAULT_BUCKETS; } @@ -215,50 +215,20 @@ public class HashMap /** @inheritDoc */ public function keys () :Array { - var keys :Array = new Array(); - - // get the simple keys first - if (_simpleData != null) { - for (var key :* in _simpleData) { - keys.push(key); - } - } - - // get the more complex keys - if (_entries != null) { - for (var ii :int = _entries.length - 1; ii >= 0; ii--) { - for (var e :HashMap_Entry = (_entries[ii] as HashMap_Entry); e != null; - e = e.next) { - keys.push(e.getOriginalKey()); - } - } - } - + var keys :Array = []; + forEach(function (k :*, v :*) :void { + keys.push(k); + }); return keys; } /** @inheritDoc */ public function values () :Array { - var vals :Array = new Array(); - - // get the simple properties first - if (_simpleData != null) { - for each (var value :* in _simpleData) { - vals.push(value); - } - } - - // get the more complex properties - if (_entries != null) { - for (var ii :int = _entries.length - 1; ii >= 0; ii--) { - for (var e :HashMap_Entry = (_entries[ii] as HashMap_Entry); e != null; - e = e.next) { - vals.push(e.value); - } - } - } - + var vals :Array = []; + forEach(function (k :*, v :*) :void { + vals.push(v); + }); return vals; } @@ -328,7 +298,7 @@ public class HashMap protected function resize (newSize :int) :void { var oldEntries :Array = _entries; - _entries = new Array(); + _entries = []; _entries.length = newSize; // place all the old entries in the new map