git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4296 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2006-07-29 01:10:15 +00:00
parent 3d7c12a5d1
commit 47a8a27a4e
4 changed files with 36 additions and 57 deletions
+5 -22
View File
@@ -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);
}
}
+7 -21
View File
@@ -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);
}
/**
+23 -8
View File
@@ -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;
}
}
}
+1 -6
View File
@@ -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