Posted 02 April 2015 - 01:38 PM
Hey everyone!
Tables are pretty much the most important thing in Lua. The standard API already has lots of functions, but very often I need similar algorithms that handle with tables, which are not in the standard API.
That's why I wrote an API called advTable, which adds more functions directly into the table-API, meaning you can directly use table.XXX with the new functions once you load/run the API.
Pastbin Link:
pastebin get sFgHKGnH advTable
Now you are probably wondering, what useful functions it does add:
table.swap(<t: table>, <a: string/number>, <b: string/number>)
table.copy(<t: table>, [inner: boolean])
table.items(<t: table>, [inner: boolean])
table.find(<t: table>, <a>, [inner: boolean])
table.clear(<t: table>)
prev(<t: table>, <i: string/number>)
WARNING: This is way slower than next, because it uses next until it overflows and is one in front of the last item! Don't use this, if your table is very big!
Even though I haven't used every function in the list yet I'm sure there will be some use for them at some point for someone. ;)/>
Of course I am open for new ideas, of what the standard table-API is missing in your opinion and I will gladly add it to my API.
If you find any bugs with the API please report them to me, so I can fix them!
Tables are pretty much the most important thing in Lua. The standard API already has lots of functions, but very often I need similar algorithms that handle with tables, which are not in the standard API.
That's why I wrote an API called advTable, which adds more functions directly into the table-API, meaning you can directly use table.XXX with the new functions once you load/run the API.
Pastbin Link:
pastebin get sFgHKGnH advTable
Now you are probably wondering, what useful functions it does add:
Spoiler
table.equal(<t1: table>, <t2: table>, [inner: boolean])Spoiler
This will compare two given tables and check if every items of both tables are completly the same. If the table has a table in itself you can set inner to true, and it will check those (and their tables, etc.) correctly as well and not just compare their pointer, like what "t1 == t2" would do.Spoiler
This swaps out two elements with index a and b in a table.Spoiler
If you try copying a table with "t2 = t1" then t2 will contain everything, t1 does. But since that only copies the pointer of the table, those two will always be the same. table.copy returns you an exact copy of your table with a different pointer so you can change them individually. If you set inner to true, tables inside your table will get copied like that as well (and their tables as well, etc.).Spoiler
If you use #t you only get the highest numerical index, that your table contains without break starting from 1. table.items really counts every single item of your table and when you set inner to true you can even count tables inside the table not as 1 but as its count.Spoiler
With this you can search for an index, that has "a" as its value. Here you can use inner, to search inside tables in the given table as well and then return all indexes, you have to go through to get to that item.Spoiler
You can get a clear table using "t = {}" but it will give you a new pointer to that table. If you want to keep that pointer, you can use table.clear and it will set every item it finds to nil, which results in clearing the table.Spoiler
This is the opposite of "next", which you can use, to get not the next index of a table, but to the previous one.WARNING: This is way slower than next, because it uses next until it overflows and is one in front of the last item! Don't use this, if your table is very big!
Even though I haven't used every function in the list yet I'm sure there will be some use for them at some point for someone. ;)/>
Of course I am open for new ideas, of what the standard table-API is missing in your opinion and I will gladly add it to my API.
If you find any bugs with the API please report them to me, so I can fix them!