Created
February 16, 2013 08:26
-
-
Save rayyee/4966074 to your computer and use it in GitHub Desktop.
利用Dictionary的弱引用实现的动态内存管理
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (c) 2013 Ray Yee. All rights reserved. | |
* | |
* @author Ray Yee | |
* Created 2013-2-16 下午3:37:51 | |
**/ | |
package eg | |
{ | |
import flash.utils.Dictionary; | |
public class DynamicMemoryManager | |
{ | |
private var _memory:Dictionary = new Dictionary(true); | |
/** | |
* Constructor | |
*/ | |
public function DynamicMemoryManager() | |
{ | |
} | |
public function push(value:Object):* | |
{ | |
var data:Array; | |
var key:Class = value.constructor; | |
for (var item:Array in _memory) | |
{ | |
if (_memory[item] == key) | |
{ | |
data = item; | |
break; | |
} | |
} | |
if (data == null) | |
{ | |
data = []; | |
_memory[data] = key; | |
} | |
data.push(value); | |
return value; | |
} | |
public function pull(key:Class):* | |
{ | |
var data:Array = _memory[key]; | |
for (var item:Array in _memory) | |
{ | |
if (_memory[item] == key) | |
{ | |
data = item; | |
break; | |
} | |
} | |
return (data && data.length) ? data.shift() : push(new key); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment