For ordered collections I use my own lightmap, which I later made somewhat compatible to tdictionary (basic methods and properties have the same name).
What'd be the best way to directly access the i-th key or i-th value (something like ValueOfIndex in TStringList)? I don't that creating an iterator and skipping the first i-1 keys or values would be the best choice. What do you recommend?
Currently I'm getting an iterator and throw the first i-1 elements away to get the i-th one. But I think there has to be a much more elegant solution, depending on how the data is managed internally. I already had a look into that but I'm not sure how to use it correctly.
A more general Question:
If a List is sorted (whatever Object/Container used for it), wouldn't a binary search of the key be the fastest (if you don't have direct access via Index)?
E.g. on very large data a heapsort may be faster
The binary search is fastest on small to medium sized lookups, but it may be that other searches are faster on very small or very large content. What average size do you need?I didn't ask for myself. More as food for thought for the OP, since i read it that he is "just" looping through the list from one end to the other until he hits
Do you sort from memory or from storage?
E.g. on very large data a heapsort or treesort may be faster and on very small data ( < 10 or so) even a bubble sort may be faster. It always pays to have a sorting library available and to test the real world performance.
Keep in mind that the lightmap is basically an array of arrays, and the iterator only revisits the lightmap structures when it changes from array to array, not every element. It stores how many elements left in the current array in the iterator.
Yes, thanks for that point. But in my case, an indexed access is more important than a small decrease in speed. I do not expect you to do my work, but maybe you'd give me a hint on how to do it.Isn't this (https://github.com/avk959/LGenerics/blob/3b3340008f6affd03611869e0c1f175389b9a78d/lgenerics/lgtreap.pas#L103) the approach you asked?
Currently I'm getting an iterator and throw the first i-1 elements away to get the i-th one. But I think there has to be a much more elegant solution, depending on how the data is managed internally. I already had a look into that but I'm not sure how to use it correctly.
Isn't this (https://github.com/avk959/LGenerics/blob/3b3340008f6affd03611869e0c1f175389b9a78d/lgenerics/lgtreap.pas#L103) the approach you asked?
This a very helpful hint, thank you. And I may assume that the arrays are filled in sorted order?