lotrointerface.com
Search Downloads

LoTROInterface SVN Calendar

[/] [Release_3.4/] [FrostyPlugins/] [Calendar/] [Table.lua] - Rev 34

Compare with Previous | Blame | View Log

import "Turbine.UI";


--------------------------------------------------------------------------
-- Function to dump contents of a table
table.dump = function(name,obj,indent)
        local indent = indent or 0; -- set to default if not given
        local spaces = string.rep(" ",indent);
        if( "table" ~= type(obj) ) then
                error("Argument is NOT a table",2);
        end
        Turbine.Shell.WriteLine(string.format(
                "%s%s  addr[%s] type[%s] size[%d]", 
                spaces, 
                name, 
                tostring(obj),
                type(obj),
                #obj));
        if( obj ) then
                for k,v in pairs(obj) do
                        Turbine.Shell.WriteLine(string.format("%s  k[%s] v[%s]",
                spaces,tostring(k),tostring(v)));
                        -- recurse if value is table
                        if( "table" == type(v) ) then
                                table.dump( string.format("%s.%s",name,tostring(k)), v, indent+2);
                        end
                end
        end
end

--------------------------------------------------------------------------
--- Function to create a table by filling with incrementing values
table.fill = function(startValue,endValue,increment)
        local increment = increment or 1; -- set to default if not given
        local newtable = {};
        for value = startValue, endValue, increment do
                table.insert(newtable,value);
        end
        return newtable;
end

--------------------------------------------------------------------------
--- Function to return the values of a table
table.keys = function(tbl)
        local keys = {};
        for k,_ in pairs(tbl) do
                table.insert(keys,k);
        end
        return keys;
end

--------------------------------------------------------------------------
--- Function to return the values of a table
table.values = function(tbl)
        local values = {};
        for _,v in pairs(tbl) do
                table.insert(values,v);
        end
        return values;
end

--------------------------------------------------------------------------
--- Function to return the number of table entries
-- This is because '#tbl' or 'table.getn(tbl)' only return the entries 
-- with *integer* keys - so this routine handles keys of other types as well.
table.size = function(tbl)
    local count = 0;
        for _,_ in pairs(tbl) do
                count = count + 1;
        end
        return count;
end

--------------------------------------------------------------------------
-- A function to return entries in a table in sorted order by key value
function table.pairsBySortedKeys(tbl, f)
        -- validate input parameters
        if( "table" ~= type(tbl) ) then
                error("Parameter expected to be a string, not " .. type(tbl), 2 );
        end
        
        -- Build a list of indices
        local a = {};
        for n in pairs(tbl) do table.insert(a, n) end
        -- sort the list of indices
        table.sort(a, f)
        
        local i = 0      -- iterator variable
        local iter = function ()   -- iterator function
                i = i + 1
                if a[i] == nil then return nil
                else return a[i], tbl[a[i]]
                end
        end
        return iter
end


--------------------------------------------------------------------------
--------------------------------------------------------------------------
-- Unit Tests
function UtilsUnitTest()
    Turbine.Shell.WriteLine("\n\n>>> UtilsUnitTest:  START ...");

    Turbine.Shell.WriteLine("LUA version: " .. _VERSION);       
        
        -- Verify table.dump()
        local list = { 'a','b','c','d'};
        table.dump("list",list);

        local nestedList = { 'a',{'b','c'},{'d',{'e','f'},'g'},'h'};
        table.dump("nestedList",nestedList);

        local upTable = table.fill(1,5);
        table.dump("upTable",upTable);

        local downTable = table.fill(5,1,-1);
        table.dump("downTable",downTable);

        -- can not do alpha strings
        --local alphaTable = table.fill('a','d');
        --table.dump("alphaTable",alphaTable);

    -- What happens if you remove something in middle?
    local tbl = { 'a', 'b', 'c', 'd' };
    table.dump("BEFORE: tbl",tbl);
    table.remove(tbl,2); -- shifts to [2]='c', [3]='d'
    table.dump("AFTER: tbl",tbl);

    --
    Turbine.Shell.WriteLine( "Test pairsBySortedKeys" );
    local rslt = {};
    local gapTbl = { [3]="c", [2]="b", [5]="e", [4]="d" };
    for k,v in table.pairsBySortedKeys(gapTbl) do
        Turbine.Shell.WriteLine( string.format(
            "  k[%s] v[%s]", k, v ));
        table.insert(rslt,k);
    end
    -- Verify result
    local expected = {2,3,4,5};
    for k,v in pairs(rslt) do
        if( expected[k] ~= rslt[k] ) then
            error( string.format("FAIL! Mismatch at idx[%d]",k), 2 );
        end
    end

        --assert(false,"ABORT");
        -- if you get this far - it passed!
    Turbine.Shell.WriteLine("\n>>> UtilsUnitTest: -- PASS!\n.\n\n");
        assert(false, "ABORT");
end

--UtilsUnitTest();

Compare with Previous | Blame


All times are GMT -5. The time now is 11:06 AM.


Our Network
EQInterface | EQ2Interface | Minion | WoWInterface | ESOUI | LoTROInterface | MMOUI | Swtorui