Is there a way to have a dictionary only with keys, no values? It’s because I want to check only for indexes, I don’t need values (the key is also the value).
For example:
if dic1[key] then...
Is there a way to have a dictionary only with keys, no values? It’s because I want to check only for indexes, I don’t need values (the key is also the value).
For example:
if dic1[key] then...
You can just use true
as the value.
Ok, so the answer to my question is: No, there is no way to have a dictionary only with keys…
You can use normal arrays but you would have to use table.find()
to see if a value exists, and that runs in linear O(n) time, while indexing a value by it’s key can be done in constant O(1) time.
Even with normal arrays there are still key-value pairs, too.
You could just use a table and loop through it
local dic1 = {"Item1", "Item2"}
if table.find(dic1, key) then...
Scroll to the bottom of this page: table | Documentation - Roblox Creator Hub
No because I want to use a fast test as if dic[key] then...
It probably loops through the table because I don’t see there is another way of getting a key in a table without looping through it
The key is an INDEX, it means that it’s indexed, so it’s not a linear searching, it goes straight to the value, as a database.
So it’s absolutely faster.
You can do what you want by just using “true” as an arbitrary placeholder value. if dic[key] then
would work as long as the value stored under key
is truthy.
To remove a value from the dictionary you would just do dic[key] = nil
This way, all the operations of adding something to the set, removing something, or checking if something exists in the set, will run in constant time.
This is the simplest way to achieve a set data structure in Roblox.
You could just do what he said and use true
as a value for your keys or use table.find
Let me explain better. I know I can create a dictionary like dic = {k1=0, k2=0 ... kn=n}
So to test if a key already exists I just need to
key = "k1"
if dic[key] then print('found') end
But I want performance plus less space. That’s why I’m asking because I don’t need the value, I just need to know if the key already exists or not.
It’s because I’ll deal with a huge dictionary inside a RenderStepped, so imagine, it must be fast and use less memory… table.find is unthinkable in this case.
But again, the question was already answered: no.
All dictionaries in Lua are sets of key-value pairs, so there’s no such thing as a table that uses only keys. It is still entirely possible to implement what you want, you just have to ignore the value part as it’s only purpose in this case would be to check if a key exists.
In regards to time there’s no need to worry since indexing a table is done in constant time.
And unless you are declaring new table in every render frame memory shouldn’t be an issue either, but then again we don’t really know any more context of what you are working on.
Doesn’t that depend on how many keys would exist in the dictionary? You could test it using values and test table.find then see which runs faster and use it, if they both don’t impact performance then try testing them using an FPS unlocker
The time saved form using if dic[key] then
instead of table.find(dic, key)
isn’t that much.
If you really want to just set each key to true like.
function createKey(key)
dict[key] = true
end
Ran this in command line
In @rokoblox5’s post he wasn’t saying that table.find was faster. It took .0046 seconds with table.find compared to the .000000715 seconds it took with indexing, which is on par with the ~5000x speedup that you found in your test.
I meant that even with table.find it’s still fast enough to run in RenderStepped, and in the benchmarks he did there was a little mistake in the code
Nope… it’s needed to put both in the same base of comparison.