Jump to content

Table Browsing without Recursion


MX_Master

Recommended Posts

I have one table which contains some values and subtables. Any of subtable also may contains values and subtables, and etc. Look:

myTable = { 
    ["x"] = 0, 
    ["y"] = 0, 
    ["children"] = { 
        [0] = { 
            ["x"] = 10, 
            ["y"] = 15, 
            ["children"] = 
            { 
                [0] = { 
                    ["x"] = 20, 
                    ["y"] = 25, 
                    ["children"] = 
                    { 
                        [0] = { 
                            ["x"] = 30, 
                            ["y"] = 35 
                        }, 
                        [1] = { 
                            ["x"] = 40, 
                            ["y"] = 45 
                        } 
                    } 
                }, 
                [1] = { 
                    ["x"] = 50, 
                    ["y"] = 55 
                } 
            } 
        }, 
        [1] = { 
            ["x"] = 60, 
            ["y"] = 65 
        } 
        -- etc. 
    } 
} 
  

I want to find ALL subtables of myTable and place this list into another table (named children) as simple list without hierarchy. Look:

children = { 
    [0] = { 
        ["x"] = 10, 
        ["y"] = 15 
    }, 
    [1] = { 
        ["x"] = 20, 
        ["y"] = 25 
    }, 
    [2] = { 
        ["x"] = 30, 
        ["y"] = 35 
    }, 
    [3] = { 
        ["x"] = 40, 
        ["y"] = 45 
    }, 
    [4] = { 
        ["x"] = 50, 
        ["y"] = 55 
    }, 
    [5] = { 
        ["x"] = 60, 
        ["y"] = 65 
    } 
    -- etc. 
} 
  

I know this can be done using recursion. But i need method without recursion.

Link to comment

How large your initial table is? Did you try to count how long it takes to look through it? For example on my core2Duo 1.66 it's like 15 ms to list through an array of a million of elements.

Also I don't think there's a better way to do what you want rather than using "ipairs" iteration. Recursion is your friend in this case =)

Link to comment

never try and test recursion in LUA, but in PHP this operation with recursion takes more time and memory than method without recursion

another reason for this is just to see how it's can be done.. just want to understand logic without recursion

How large your initial table is? Did you try to count how long it takes to look through it? For example on my core2Duo 1.66 it's like 15 ms to list through an array of a million of elements.

...

i can write this operation with recursion, but i haven't "without recursion" method code to test it and compare with first one

Link to comment

As you said you have a table which contain an undefined number of subtables which contain an undefined number of subtables which contain an undefined number of subtables which contain..

-> Means you need an undefined number of iterations to go through every table you have in the mess.

-> Means you need a recursive function - iterator

-- or --

an undefined number of non-recursive iterators to solve it.

The logic still stays the same I guess: it's to catch every single (sub)table you have and put it into another. Regardless of the recursion level you do the same thing to every subtable (obviously).

Just in case you want to see how it can be done without rec., I must say I don't really know any efficient ways. Well, you may transform your table into a string using toJSON() and then use string.gsub() to search for table-specific symbols , like "[ [ ... ] ]" I think, and then transform the table-containing-substring into lua table again and then put it into your "children" table..

Jut in case.. =)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...