Pic*_*tor 2 apache-flex flash recursion actionscript actionscript-3
虽然递归扫描通常用于扫描嵌套对象/数据.如果某些对象相互引用,它可以进行无限循环.那么扫描所有项目的最有效方法是什么,不会导致计算机崩溃,也不会跳过指定的参数?
这是一个递归扫描仪的例子......
/**
* Triggers the scan function for each object given
**/
function recursiveScanner( object:* , scanFunction:Function ):void {
if( typeof(object) == 'object' ) {
for( var key:String in object ) {
recursiveScanner( object[key], scanFunction );
}
} else {
scanFunction.call(this, object);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当传入以下内容时会出现巨大问题
//...
obj1.next = obj2;
//...
obj2.next = obj3;
//...
obj3.next = obj1;
//...
recursiveScanner(obj1, scanFuction);
Run Code Online (Sandbox Code Playgroud)
对象将在永恒循环中触发彼此的扫描.有没有办法解决这个问题?
我相信C/C++:每个scanFunction调用都会被添加到一个由扫描的"内存地址"组成的列表中,从而防止重复.这在AS3中甚至可能吗?有更优雅的方式吗?
gra*_*ukt 10
使用a Dictionary保留扫描对象列表,如果之前已扫描过,则忽略它们:
/**
* Triggers the scan function for each object given
**/
function recursiveScanner( object:* , scanFunction:Function, ignoreList:Dictonary = null ):void {
// if we have no list, create one
if (!ignoreList) ignoreList = new Dictionary();
// if the item is in the list, we bail
if (ignoreList[object]) return;
// mark the item as scanned before recursing into it
ignoreList[object] = true;
if( typeof(object) == 'object' ) {
for( var key:String in object ) {
recursiveScanner( object[key], scanFunction, ignoreList );
}
} else {
scanFunction.call(this, object);
}
}
Run Code Online (Sandbox Code Playgroud)