[AS3]常用数据集合类(Map,Queue,Stack)

Submitted by kinglong on 2007-4-4 10:43:24

[AS3]常用数据集合类
[ActionScript版本]  ActionScript3
[Flash  Player版本] Flash 9.0

     本来我是想参考Java方式采用接口方式来实现像“Map,Iterator,Queue,Stack,Iterable,Collection,ArrayList,Hashtable”等Java中常用的数据类,发现越写越复杂了!而且我后来感觉没有必要搞得那么复杂,比如说像ArrayList类吧,Flash当中已经有Array类了,也没有必要再去写一个了,而像HashMap吧,Flash又不能实现Hash表,还不如干脆去掉Hash了,直接用Map了,像Iterator,Collection类吧,完全用Array都可以实现了,也没有必要再写新的了,对于Flash来说,简单,高效就可以了!所以我就重新写了Map,Queue,Stack等常用的数据集合类!(效率以后不断去优化了)
    另外发现Flash不能像Java那样可以强制转换类型,所以这几个类只好采用Flash的方式了,也就是不确定类型都用*号来代替,如 get(key:*):*;同时也发现Flash也采用像Java那样trace(new Point(x,y)),显示一个对象就会自动去调用这个对象中的toString方法!
   本人觉得AS3语法太综合其他语言了,有些不伦不类的。说不好听的话,就是一个“杂交”品种!

Map类:类似于Java中的HashMap类的功能,可以储存不同的键和键值;

  1. package com.klstudio.data{   
  2.            
  3.         import flash.utils.Dictionary;   
  4.            
  5.         public class Map{   
  6.                    
  7.                 private var _keys:Array = null;   
  8.                 private var props:Dictionary = null;   
  9.                    
  10.                 public function Map(){   
  11.                         this.clear();   
  12.                 }   
  13.                 public function clear():void{   
  14.                         this.props = new Dictionary();   
  15.                         this._keys = new Array();   
  16.                 }   
  17.                 public function containsKey(key:*):Boolean{   
  18.                         return this.props[key] != null;   
  19.                 }   
  20.                 public function containsValue(value:*):Boolean{   
  21.                         var result:Boolean = false;   
  22.                         var len:uint = this.size();   
  23.                         if(len > 0){   
  24.                                 for(var i:uint = 0 ; i < len ; i++){   
  25.                                         if(this.props[this._keys[i]] == value) return true;   
  26.                                 }   
  27.                         }   
  28.                 }   
  29.                 public function get(key:*):*{   
  30.                         return this.props[key];   
  31.                 }   
  32.                 public function put(key:*,value:*):*{   
  33.                         var result:* = null;   
  34.                         if(this.containsKey(key)){   
  35.                                 result = this.get(key);   
  36.                                 this.props[key] = value;   
  37.                         }else{   
  38.                                 this.props[key] = value;   
  39.                                 this._keys.push(key);   
  40.                         }   
  41.                         return result;   
  42.                 }   
  43.                 public function remove(key:*):*{   
  44.                         var result:* = null;   
  45.                         if(this.containsKey(key)){   
  46.                                 delete this.props[key];   
  47.                                 var index:int = this._keys.indexOf(key);   
  48.                                 if(index > -1){   
  49.                                         this._keys.splice(index,1);   
  50.                                 }   
  51.                         }   
  52.                         return result;   
  53.                 }   
  54.                 public function putAll(map:Map):void{   
  55.                         this.clear();   
  56.                         var len:uint = map.size();   
  57.                         if(len > 0){   
  58.                                 var arr:Array = map.keys;   
  59.                                 for(var i:uint=0;i<len;i++){   
  60.                                         this.put(arr[i],map.get(arr[i]));   
  61.                                 }   
  62.                         }   
  63.                 }   
  64.                 public function size():uint{   
  65.                         return this._keys.length;   
  66.                 }   
  67.                 public function isEmpty():Boolean{   
  68.                         return this.size < 1;   
  69.                 }   
  70.                 public function values():Array{   
  71.                         var result:Array = new Array();   
  72.                         var len:uint = this.size();   
  73.                         if(len > 0){   
  74.                                 for(var i:uint = 0;i<len;i++){   
  75.                                         result.push(this.props[this._keys[i]]);   
  76.                                 }   
  77.                         }   
  78.                         return result;   
  79.                 }   
  80.                 public function keys():Array{   
  81.                         return this._keys;   
  82.                 }   
  83.                    
  84.                 public function toString():String{   
  85.                         var out:String = "";   
  86.                         for(var i:uint=0;i<this.size();i++){   
  87.                                 out += this._keys[i] + ":"+this.get(this._keys[i]) + "\n";   
  88.                         }   
  89.                         return out;   
  90.                 }   
  91.         }   
  92. }   
  93.   


Queue类:也类似于Java中的Queue接口实现的队列功能,以 FIFO(先进先出)的方式排序各个元素。

  1. package com.klstudio.data{   
  2.         public class Queue{   
  3.                 private var arr:Array;   
  4.                 private var pointer:uint;   
  5.                 function Queue(){   
  6.                         this.arr = new Array();   
  7.                         this.pointer = 0;   
  8.                 }   
  9.                 public function element():*{   
  10.                         if(isEmpty()){   
  11.                                 return null;   
  12.                         }else{   
  13.                                 if(this.pointer < this.size()){   
  14.                                         return this.arr[this.pointer++];   
  15.                                 }else{   
  16.                                         return null;   
  17.                                 }   
  18.                         }   
  19.                 }   
  20.                 public function peek():*{   
  21.                         if(isEmpty()){   
  22.                                 return null;   
  23.                         }else{   
  24.                                 return this.arr[0];   
  25.                         }   
  26.                 }   
  27.                 public function poll():*{   
  28.                         this.pointer = 0;   
  29.                         if(isEmpty()){   
  30.                                 return null;   
  31.                         }else{   
  32.                                 return this.arr.shift();   
  33.                         }   
  34.                 }   
  35.                 public function add(object:*):Boolean{   
  36.                         this.pointer = 0;   
  37.                         if(this.contains(object)){   
  38.                                 return false;   
  39.                         }else{   
  40.                                 this.arr.push(object);   
  41.                                 return true;   
  42.                         }   
  43.                 }   
  44.                 public function contains(object:*):Boolean{   
  45.                         for(var i:uint = 0;i<this.size();i++){   
  46.                                 if(this.arr[i] == object){   
  47.                                         return true;   
  48.                                 }   
  49.                         }   
  50.                         return false;   
  51.                 }   
  52.                 public function clear():void{   
  53.                         this.arr = new Array();   
  54.                         this.pointer = 0;   
  55.                 }   
  56.                 public function isEmpty():Boolean{   
  57.                         return this.size() == 0;   
  58.                 }   
  59.                 public function size():uint{   
  60.                         return this.arr.length;   
  61.                 }   
  62.                 public function toString():String{   
  63.                         return this.arr.toString();   
  64.                 }   
  65.         }   
  66. }   


Stack类:也参考Java中的Stack类实现堆栈功能,按 LIFO(后进先出)的方式对元素进行排序。

  1. package com.klstudio.data{   
  2.         public class Stack{   
  3.                 private var arr:Array;   
  4.                 private var pointer:uint;   
  5.                    
  6.                 function Stack(){   
  7.                         this.arr = new Array();   
  8.                         this.pointer = 0;   
  9.                 }   
  10.                    
  11.                 public function element():*{   
  12.                         if(isEmpty()){   
  13.                                 return null;   
  14.                         }else{   
  15.                                 if(this.pointer < 0){   
  16.                                         return null;   
  17.                                 }else{   
  18.                                         return this.arr[this.pointer--];   
  19.                                 }   
  20.                         }   
  21.                 }   
  22.                    
  23.                 public function peek():*{   
  24.                         if(isEmpty()){   
  25.                                 return null;   
  26.                         }else{   
  27.                                 return this.arr[this.arr.length];   
  28.                         }   
  29.                 }   
  30.                    
  31.                 public function pop():*{   
  32.                         if(isEmpty()){   
  33.                                 return null;   
  34.                         }else{   
  35.                                 this.pointer = (this.size()>1)?this.size() - 2:0;   
  36.                                 return this.arr.pop();   
  37.                         }   
  38.                 }   
  39.                    
  40.                 public function add(object:*):Boolean{   
  41.                         if(this.contains(object)){   
  42.                                 this.pointer = this.size() - 2;   
  43.                                 return false;   
  44.                         }else{   
  45.                                 this.pointer = this.size() - 1;   
  46.                                 this.arr.push(object);   
  47.                                 return true;   
  48.                         }   
  49.                 }   
  50.                 public function contains(object:*):Boolean{   
  51.                         for(var i:uint = 0;i<this.size();i++){   
  52.                                 if(this.arr[i] == object){   
  53.                                         return true;   
  54.                                 }   
  55.                         }   
  56.                         return false;   
  57.                 }   
  58.                    
  59.                 public function clear():void{   
  60.                         this.arr = new Array();   
  61.                         this.pointer = 0;   
  62.                 }   
  63.                    
  64.                 public function isEmpty():Boolean{   
  65.                         return this.size() == 0;   
  66.                 }   
  67.                    
  68.                 public function size():uint{   
  69.                         return this.arr.length;   
  70.                 }   
  71.                    
  72.                 public function toString():String{   
  73.                         return this.arr.toString();   
  74.                 }   
  75.         }   
  76. }   
  77.