Thursday, February 23, 2012

Clear Dictionary AS3


//Use two steps to avoid changing the dictionary while iterating over it.
//Also, the keys are type "Object" not "String. For speed, use a vector and
//store it's length in a variable before iterating over it. Lastly, just to be safe,
//nullify entries in vector as you go along. Since the whole point of clearing out
//a dictionary in the first place is to de-reference the items within it.

function clearD(d:Dictionary):void{
    //Get Keys from dictionary.
    var idVec:Vector.<Object> = new Vector.<Object>(0);
    for(var obj:Object in d){
        idVec.push(obj);
    }//[next obj]
    
    //Delete Keys from dictionary and clear from vector at same time.
    var vLen:int = idVec.length;
    for(var vi:int = 0; vi<vLen; vi++){
        delete d[ idVec[vi] ];
        idVec[vi] = null;
    }//[next vi]
}//[FN:clearD]

No comments:

Post a Comment