SmartTables, Advanced recursive table searching & more

Smart Tables

Introduction


All those iterations to get to the right index/key of a nested table, who wants that? creating functionalities like those only extend till so long and when tables get bigger they could eventually fail on keys with the same name. You might as well leave those habits out since SmartTables handles all that for you but better.

What are smart tables?


Smart tables are tables that inherit certain functionalities from metatables in order to recursively search for a given index at indexation, this allows developers to save a lot of time creating iteration functionalities or even getting to the proper key/index manually. So why should you use it? As mentioned, it recursively searches the entire table and returns the first matching key/index it can find with a time difference from 0 to 0.0003 seconds, this is 3/10th of a millisecond (or sometimes even faster than a normal indexation at times). This can be considered nothing in terms of a performance drop.

How do I use this?


nothing changes from a usage aspect! It’s really simple, Lets make an example

local SmartTables = require("SmartTables")
-- replace "SmartTables" with the module location

local Table_To_Convert = {
    Some_Table = {
        Another_Table = {
            Lets_Go_Further_TM = {
                Value = 2
            }
        }
    }
}

local New_SmartTable = SmartTables.new(Table_To_Convert)
-- creates a new smart table with the Table_To_Convert as base in terms of value

print(New_SmartTable.Value) -- outputs 2,
New_SmartTable.Value = 25
print(New_SmartTable.Another_Table.Value) -- outputs 25

This hopefully should be clear where it’s going, This is technically the FindFirstChild(Key, true) of tables and allows you to do that after one another continuously.

HOWEVER. we’re not done there yet. I’ve also implemented a getnumber functionality from the given key that supports dictionaries, this was honestly one of the main things that I hated about lua dictionaries, the lack of certain functionalities such as getting the count of it. The documentations will tell more about this all.

Wow! Where can I find this?


Glad that you ask! You can find SmartTables on 2 places, github and in the roblox toolbox.

found any flaws that I might’ve overlooked or are some things still unclear? feel free to shoot me a message or reply to the post with details on what exactly and I’ll look into it.

Enjoy!

10 Likes

Does changing the value from a smart table also change the value of what’s stored in the Table_To_Convert?

I have not looked through the code as of yet, but what is the best and worst time complexity for the search algorithm you implemented?

It looks like best is O(1) and worst is O(n) based on what I read? I could be mistaken though as I just went based on what you said instead of looking at the code. Would love to know as it is rather important for any search algorithm to know the best/worst time complexity.

It doesn’t. This utility makes a copy of it and modifies it in such a way that using it is just more convenient. you could always re-assign the Table_To_Convert with a smart table of itself.

I’ve done a little more testing and looking at the times itself, the time complexity is nearly always O(1)

local SmartTables = require(script.Parent.SmartTables)


local T = {
	[workspace] = {
		Table21 = {
			Table2 = {
				Table3 = {
					[workspace.Baseplate] = {
						TargetTable = {
							Table5 = {
								Secondary = {
									Value = 256
								}
							}
						},
					}
				}
			}
		};
		Table45 = {
			Value22 = 7;
		};
		{
			Value23 = 2;
		}
	}
}

local T2 = {
	Hi = 5
}

-- letting the server start up properly
wait()

local Ta = SmartTables.new(T)
local Ta2 = SmartTables.new(T2)

local T = os.clock()
print(Ta.Secondary.Value)
print(os.clock()-T)

T = os.clock()
print(Ta2.Hi)
print(os.clock()-T)

when looking at this code you’d expect O(n), however the results are more close O(1) than O(n) (atleast to me)
image

Meaning your performance barely gets a hit from this and the time taken is barely any difference, it should be about the same as manually referencing to values in a table. So to answer that, yes. best case it’s O(1) and worst case it’s O(n)

1 Like