2

While recursive scans are commonly use to scan through nested objects / data. It can go on an infinite loop, if some of the objects references one another. So what is the most effective way to scan all items, without crashing the computer, nor skipping a specified parameter?

Here is an example of a recursive scanner...

/**
 * 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);
    }
}

However a huge problems occur when the following is passed in

//...
obj1.next = obj2;
//...
obj2.next = obj3;
//...
obj3.next = obj1;
//...
recursiveScanner(obj1, scanFuction);

The objects will trigger scans for one another in an eternal loop. So is there a way to solve this?

I do believe in C/C++ : Each scanFunction call, will be added into a list consisting of scanned 'memory address', thus preventing a repeat. Is this even possible in AS3? Is there a more elegent way?

2 Answers 2

10

Use a Dictionary to keep a list of scanned objects and ignore them if they've been scanned before:

/**
 * 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);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ahh... The object ID would be used in the dictionary as a an attribute... Thx for the solution =)
2

OK, accept the answer from @grapefrukt, but here is some background.

In computer science terms, the problem is equivalent to traversing a directed graph that can contain cycles.

There are essentially two naive ways (ie, not following a heuristic) to traverse, depth first or breadth first.

I'll leave it as an exercise to determine whether the sample code is BFS or DFS.

1 Comment

Depth first :) Which is optimal for my deployment over breadth first, as I will have to read all nodes with no repeat in my deployment. So having a FIFO from BFS is a unneeded cost. Thx for the good read and reference anyway =) didn't know bout BFS before this :) will come useful in the future TY

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.