Hello people,
i am learning nim right know and for my first project i choose to implement a brainf*ck interpreter, in doing so i implemented a memory dump feature, that was quit easy but to do so i need to know the last index of the Table i am using for the memory, in order to only dump from zero to the last index memory
So i read the documentation of the tables module, but there's nothing to get the last key, the only wait to get the last index with Tables.keys iterator, the dump procedure look like this
proc dump(mem: Table[Natural, char]) : string =
var last : Natural
for key in mem.keys:
if key > last: last = key
for index in countup(0, last, 16):
...
As you can it is not the most optimal way to do it, do someone here know a better way at getting the last index ?
I am curious if there is a reason you're using a table instead of a sequence? I guess you're using the table identically except you have it unorded/hashing to access.
If you used a seq[char] you could use mem[^1] to get the last element in the sequence.
@Clonk
Technically this is would give me the value that i am looking for, but high isn't available for Table, and openArray is not the right type for my needs
@ElegantBeef
i am using a Table because the memory is a random access memory, if i were to use a seq and would like to set mem[50] = 'A' i would need to fill mem with unneeded values until i can set mem[50], which is not the most efficient way to do it,
If you used a seq[char] you could use mem[^1] to get the last element in the sequence.
then again mem[^1] would give me the last element, but i need the last greatest index, because in my dump procedure i need to know the range which mem span across, for optimization and not needing to scan through whole range of Natural when i could just scan in range of 0..last
There doesn't seems to have an alternative for that, i am stuck doing O(N+M) passes,
Well, anyways thank for your answers.