Skip to content

Instantly share code, notes, and snippets.

@makc
Created June 27, 2012 17:09

Revisions

  1. makc created this gist Jun 27, 2012.
    36 changes: 36 additions & 0 deletions Pool.as
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    package com.realaxy.operators{

    import flash.utils.Dictionary;

    internal class Pool {
    private var instances : Dictionary =new Dictionary() ;
    private var instanceCounts : Dictionary =new Dictionary() ;
    public function getInstance ( type : Class ) : * {
    var cache : Vector.<*>;
    if ( instanceCounts[type] ) {
    cache = instances[type];
    }else{
    cache = new Vector.<*>();
    instances[type] = cache;
    instanceCounts[type] = 0;
    }
    if ( instanceCounts[type] < cache.length ) {
    return cache[instanceCounts[type]++];
    }else{
    var object : * =new type();
    cache[instanceCounts[type]++] = object;
    return object;
    }
    }
    public function resetCounts ( type : Class ) : void {
    if ( instanceCounts[type] ) {
    instanceCounts[type] = 0;
    }
    }
    public function resetAllCounts ( ) : void {
    for ( var type : Object in instanceCounts ) {
    instanceCounts[type] = 0;
    }
    }
    }
    }