From 47a8a27a4e4f84465e596a606a0fb8267815530d Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Sat, 29 Jul 2006 01:10:15 +0000 Subject: [PATCH] Cleanup. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4296 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/as/com/threerings/presents/dobj/DSet.as | 27 +++------------- .../com/threerings/presents/dobj/OidList.as | 28 +++++------------ src/as/com/threerings/util/ArrayUtil.as | 31 ++++++++++++++----- src/as/com/threerings/util/Name.as | 7 +---- 4 files changed, 36 insertions(+), 57 deletions(-) diff --git a/src/as/com/threerings/presents/dobj/DSet.as b/src/as/com/threerings/presents/dobj/DSet.as index d2bc2fd4e..82ef413c9 100644 --- a/src/as/com/threerings/presents/dobj/DSet.as +++ b/src/as/com/threerings/presents/dobj/DSet.as @@ -6,6 +6,7 @@ import com.threerings.util.ArrayIterator; import com.threerings.util.Equalable; import com.threerings.util.Iterator; import com.threerings.util.StringBuilder; +import com.threerings.util.Util; import com.threerings.io.ObjectInputStream; import com.threerings.io.ObjectOutputStream; @@ -70,7 +71,7 @@ public class DSet { // o(n) for now for each (var entry :DSet_Entry in _entries) { - if (isSameKey(key, entry.getKey())) { + if (Util.equals(key, entry.getKey())) { return entry; } } @@ -157,7 +158,7 @@ public class DSet // o(n) for now for (var ii :int = 0; ii < _entries.length; ii++) { var entry :DSet_Entry = _entries[ii]; - if (isSameKey(key, entry.getKey())) { + if (Util.equals(key, entry.getKey())) { _entries.splice(ii, 1); return entry; } @@ -180,7 +181,7 @@ public class DSet var key :Object = elem.getKey(); for (var ii :int = 0; ii < _entries.length; ii++) { var entry :DSet_Entry = _entries[ii]; - if (isSameKey(key, entry.getKey())) { + if (Util.equals(key, entry.getKey())) { _entries[ii] = elem; return entry; // return the old entry } @@ -188,23 +189,6 @@ public class DSet return null; } - /** - * Internal function to determine whether a key matches the key for - * an existing entry. - */ - private function isSameKey (key :Object, otherKey :Object) :Boolean - { - if (ObjectUtil.isSimple(key)) { - return (key === otherKey); - - } else if (key is Equalable) { - return (key as Equalable).equals(otherKey); - - } else { - throw new Error("Element key is neither simple or Equalable"); - } - } - /** * Generates a string representation of this set instance. */ @@ -235,7 +219,6 @@ public class DSet } /** The entries of the set (in a sparse array). */ - protected var _entries :TypedArray = - new TypedArray("[Lcom.threerings.presents.dobj.DSet$Entry;"); + protected var _entries :TypedArray = TypedArray.create(DSet_Entry); } } diff --git a/src/as/com/threerings/presents/dobj/OidList.as b/src/as/com/threerings/presents/dobj/OidList.as index 5efe54d27..5ed805e71 100644 --- a/src/as/com/threerings/presents/dobj/OidList.as +++ b/src/as/com/threerings/presents/dobj/OidList.as @@ -1,5 +1,7 @@ package com.threerings.presents.dobj { +import com.threerings.util.ArrayUtil; + import com.threerings.io.Streamable; import com.threerings.io.ObjectInputStream; @@ -22,7 +24,7 @@ public class OidList */ public function OidList () { - _oids = new TypedArray("[I"); + _oids = TypedArray.create(int); } /** @@ -43,10 +45,8 @@ public class OidList public function add (oid :int) :Boolean { // check for existence - for (var ii :int = 0; ii < _oids.length; ii++) { - if (_oids[ii] == oid) { - return false; - } + if (ArrayUtil.contains(_oids, oid)) { + return false; } // add the oid @@ -63,15 +63,7 @@ public class OidList public function remove (oid :int) :Boolean { // scan for the oid in question - for (var ii :int = 0; ii < _oids.length; ii++) { - if (_oids[ii] == oid) { - // shift the rest of the list back one - _oids.splice(ii, 1); - return true; - } - } - - return false; + return ArrayUtil.removeFirst(_oids, oid); } /** @@ -79,13 +71,7 @@ public class OidList */ public function contains (oid :int) :Boolean { - for (var ii :int = 0; ii < _oids.length; ii++) { - if (_oids[ii] == oid) { - return true; - } - } - - return false; + return ArrayUtil.contains(_oids, oid); } /** diff --git a/src/as/com/threerings/util/ArrayUtil.as b/src/as/com/threerings/util/ArrayUtil.as index f03804ad9..1c2e20deb 100644 --- a/src/as/com/threerings/util/ArrayUtil.as +++ b/src/as/com/threerings/util/ArrayUtil.as @@ -40,46 +40,61 @@ public class ArrayUtil return -1; // never found } + public static function contains (arr :Array, element :Object) :Boolean + { + return (indexOf(arr, element) != -1); + } + /** * Remove the first instance of the specified element from the array. + * + * @return true if an element was removed, false otherwise. */ - public static function removeFirst (arr :Array, element :Object) :void + public static function removeFirst (arr :Array, element :Object) :Boolean { - removeImpl(arr, element, true); + return removeImpl(arr, element, true); } /** * Remove the last instance of the specified element from the array. + * + * @return true if an element was removed, false otherwise. */ - public static function removeLast (arr :Array, element :Object) :void + public static function removeLast (arr :Array, element :Object) :Boolean { arr.reverse(); - removeFirst(arr, element); + var removed :Boolean = removeFirst(arr, element); arr.reverse(); + return removed; } /** * Removes all instances of the specified element from the array. + * + * @return true if at least one element was removed, false otherwise. */ - public static function removeAll (arr :Array, element :Object) :void + public static function removeAll (arr :Array, element :Object) :Boolean { - removeImpl(arr, element, false); + return removeImpl(arr, element, false); } /** * Implementation of remove methods. */ private static function removeImpl ( - arr :Array, element :Object, firstOnly :Boolean) :void + arr :Array, element :Object, firstOnly :Boolean) :Boolean { + var removed :Boolean = false; for (var ii :int = 0; ii < arr.length; ii++) { if (Util.equals(arr[ii], element)) { arr.splice(ii--, 1); if (firstOnly) { - return; + return true; } + removed = true; } } + return removed; } } } diff --git a/src/as/com/threerings/util/Name.as b/src/as/com/threerings/util/Name.as index adeec1dcc..65b20958a 100644 --- a/src/as/com/threerings/util/Name.as +++ b/src/as/com/threerings/util/Name.as @@ -47,12 +47,7 @@ public class Name extends Object // from interface Hashable public function hashCode () :int { - var norm :String = getNormal(); - var hash :int = 0; - for (var ii :int = 0; ii < norm.length; ii++) { - hash = (hash << 1) ^ int(norm.charCodeAt(ii)); - } - return hash; + return StringUtil.hashCode(getNormal()); } // from interface Comparable