凯子刚写的 AS3 的 Hashtable 实现,马上拿出来和大家共享!
Hashtable.as
package cn.Otu.util
{
public final class Hashtable
{
private var value:Array;
public function Hashtable()
{
//TODO: implement function
value = new Array();
}
private function getIndex(o:Object, f:String = "key"):int
{
var r:int;
for (r = 0; r < value.length && value[r][f] != o; r++);
return r;
}
public function clear():void
{
value.length = 0;
}
public function clone():Hashtable
{
var r:Hashtable = new Hashtable();
for(var i:int = 0; i < value.length; i ++)
{
r.put(value[i]["key"], value[i]["value"]);
}
return r;
}
public function containsKey(key:Object):Boolean
{
return getIndex(key) != value.length;
}
public function containsValue(value:Object):Boolean
{
return getIndex(value, "value") != this.value.length;
}
public function get(key:Object):Object {
var i:int = getIndex(key);
if (i != value.length) {
return value[i].value;
}
return null;
}
public function isEmpty():Boolean {
return value.length == 0;
}
public function keySet():Array {
var r:Array = new Array();
for (var i:int = 0; i < value.length; i++) {
r[i] = value[i].key;
}
return r;
}
public function put(key:Object, value:Object):Object
{
var i:int = getIndex(key);
var r:Object;
try {
r = this.value[i]["value"];
} catch(e:TypeError) {
r = null;
}
this.value.length += (i == this.value.length);
this.value[i] = new HashtableElement(key, value);
return r;
}
public function putAll(array:Array):void {
for (var i:int = 0; i < array.length; i++) {
put(i, array[i]);
}
}
/**
* 如果存在该 key, 则删除, 返回删除的值, 否则返回 null
*/
public function remove(key:Object):Object {
var i:int = getIndex(key);
var r:Object;
try {
r = value[i].value;
} catch(e:TypeError) {
r = null;
}
value.splice(i, 1);
return r;
}
public function size():int
{
return value.length;
}
public function toString():String {
var r:String = "";
for (var i:int = 0; i < value.length; i++) {
r += ", " + value[i].toString();
}
return r.substring(2 * (r.length > 0 ? 1 : 0));
}
public function values():Array {
var r:Array = new Array();
for (var i:int = 0; i < value.length; i++) {
r[i] = value[i].value;
}
return r;
}
}
}
HashtableElement.as
package cn.Otu.util
{
internal class HashtableElement
{
public var key:Object;
public var value:Object;
public function HashtableElement(key:Object, value:Object)
{
this.key = key;
this.value = value;
}
public function toString():String
{
return "{" + this.key.toString() + "=" + this.value.toString() + "}";
}
}
}
Hashtable.as
package cn.Otu.util
{
public final class Hashtable
{
private var value:Array;
public function Hashtable()
{
//TODO: implement function
value = new Array();
}
private function getIndex(o:Object, f:String = "key"):int
{
var r:int;
for (r = 0; r < value.length && value[r][f] != o; r++);
return r;
}
public function clear():void
{
value.length = 0;
}
public function clone():Hashtable
{
var r:Hashtable = new Hashtable();
for(var i:int = 0; i < value.length; i ++)
{
r.put(value[i]["key"], value[i]["value"]);
}
return r;
}
public function containsKey(key:Object):Boolean
{
return getIndex(key) != value.length;
}
public function containsValue(value:Object):Boolean
{
return getIndex(value, "value") != this.value.length;
}
public function get(key:Object):Object {
var i:int = getIndex(key);
if (i != value.length) {
return value[i].value;
}
return null;
}
public function isEmpty():Boolean {
return value.length == 0;
}
public function keySet():Array {
var r:Array = new Array();
for (var i:int = 0; i < value.length; i++) {
r[i] = value[i].key;
}
return r;
}
public function put(key:Object, value:Object):Object
{
var i:int = getIndex(key);
var r:Object;
try {
r = this.value[i]["value"];
} catch(e:TypeError) {
r = null;
}
this.value.length += (i == this.value.length);
this.value[i] = new HashtableElement(key, value);
return r;
}
public function putAll(array:Array):void {
for (var i:int = 0; i < array.length; i++) {
put(i, array[i]);
}
}
/**
* 如果存在该 key, 则删除, 返回删除的值, 否则返回 null
*/
public function remove(key:Object):Object {
var i:int = getIndex(key);
var r:Object;
try {
r = value[i].value;
} catch(e:TypeError) {
r = null;
}
value.splice(i, 1);
return r;
}
public function size():int
{
return value.length;
}
public function toString():String {
var r:String = "";
for (var i:int = 0; i < value.length; i++) {
r += ", " + value[i].toString();
}
return r.substring(2 * (r.length > 0 ? 1 : 0));
}
public function values():Array {
var r:Array = new Array();
for (var i:int = 0; i < value.length; i++) {
r[i] = value[i].value;
}
return r;
}
}
}
HashtableElement.as
package cn.Otu.util
{
internal class HashtableElement
{
public var key:Object;
public var value:Object;
public function HashtableElement(key:Object, value:Object)
{
this.key = key;
this.value = value;
}
public function toString():String
{
return "{" + this.key.toString() + "=" + this.value.toString() + "}";
}
}
}