美文网首页
LayaBox:字典实现类似map功能

LayaBox:字典实现类似map功能

作者: 一眼就认出你 | 来源:发表于2020-10-14 15:30 被阅读0次

    代码:

    package Common.Utils {
    
        import laya.utils.Pool;
    
        //同类型的字典
        public class ObjDictionary {
    
            private var _strongType:Class;
            public function ObjDictionary(strongType:Class=null)
            {
                this._strongType = strongType;
                this._length = 0;
                _container = new Object();
            }
    
            //废弃
            public function init(strongType:Class=null):void
            {
                this._strongType = strongType;
                this._length = 0;
            }
    
            private var _container:Object=new Object();
            private var _length:uint=0;
            private var _attributions:Object=new Object();
    
            //废弃
            public function recover():void
            {
                _strongType=null;
                var key:String;
                for(key in _container)
                {
                    _container[key]=null;
                    delete _container[key];
                }
                _length=0;
                for(key in _attributions)
                {
                    _attributions[key]=null;
                    delete _attributions[key];
                }
                
                //Pool.recover(PoolSignEnum.ObjDictionary,this);
            }
    
    
            public function get container():Object
            {
                return _container;
    
            }
    
            public function get length():uint
            {
                return _length;
            }
    
            public function add(key:*,value:*):void
            {
                if(_strongType)
                {
                    if(!(value is _strongType))
                    {
                        throw new Error("Obj Dictionary [add] Type check Error, Need"+ _strongType);
                    }
                }
    
                if (!containsKey(key))
                {
                    _length ++;
                }
                _container[key] = value;
    
            }
    
            //重设的话 前提是已经包含了这个值
            public function reset(key:*,value:*):void
            {
                if(_strongType)
                {
                    if(!(value is _strongType))
                    {
                        throw new Error("Obj Dictionary [add] Type check Error, Need"+ _strongType);
                    }
                }
    
                if(containsKey(key))
                {
                    _container[key] = value;
                }else
                {
                    trace("ObjDictionary:warning you reset a not exsit key!");
                }
    
            }
    
            public function remove(key:*):void
            {
                if(_container.hasOwnProperty(key))
                {
                    _container[key] = null;
                    delete _container[key];
                    _length --;
                }
            }
    
            public function get(key:*):*
            {
                return _container[key];
            }
    
            public function containsKey(key:*):Boolean
            {
                return _container.hasOwnProperty(key);
            }
    
            public function clear():void
            {
                _length = 0;
                _container = null;
                _container = {};
                _attributions = null;
                _attributions = {};
            }
    
            public function getAllKeys():Array
            {
                var keysArray:Array = new Array();
    
                for (var key:String in _container) 
                {
                    keysArray.push(key);
                }
                return keysArray;
            }
            
            public function getAllValues():Array
            {
                var valueArray:Array = new Array();
                
                for (var key:String in _container) 
                {
                    valueArray.push(_container[key]);
                }
                return valueArray;
            }
    
            public function setAttribution(attrName:String,value:*):void
            {
                // if(!_attributions)
                //  _attributions = new Object();
                
                _attributions[attrName] = value;
                
            }
    
            public function getAttibutionByName(attrName:String):*
            {
                if(_attributions.hasOwnProperty(attrName))
                {
                    return _attributions[attrName];
                }
                else
                {
                    trace("ObjDictionary getAttibutionByName:no attrName " + attrName);
                    return null;
                }
            }
    
        }
    }
    

    相关文章

      网友评论

          本文标题:LayaBox:字典实现类似map功能

          本文链接:https://www.haomeiwen.com/subject/ueljpktx.html