Recent

Author Topic: [SOLVED] Generics.Collections TDictionary indexed access  (Read 6882 times)

avk

  • Hero Member
  • *****
  • Posts: 505
    • my self-education project
Re: [SOLVED] Generics.Collections TDictionary indexed access
« Reply #15 on: September 21, 2021, 06:51:31 pm »
Quote from: Posted by: piola on: September 20, 2021, 11:37:18 pm
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.

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 the approach you asked?

piola

  • Jr. Member
  • **
  • Posts: 74
Re: [SOLVED] Generics.Collections TDictionary indexed access
« Reply #16 on: September 21, 2021, 07:10:11 pm »
Isn't this the approach you asked?

Hmm... didn't know about LGenerics, thank you. The problem is that there are dozens (or even hundreds) of TDictionary, THashMap,... implementations and probably none of them can handle everythin  ;D

piola

  • Jr. Member
  • **
  • Posts: 74
Re: [SOLVED] Generics.Collections TDictionary indexed access
« Reply #17 on: September 21, 2021, 07:11:35 pm »
Meanwhile I have come to the following code:

Code: Pascal  [Select][+][-]
  1. function TLightDictionary<tkey, tvalue>.getItems(Index: integer): TKey;
  2. var blknr   : integer;
  3. begin
  4.   blknr:=0;
  5.   if nrblocks=0 then Raise EListError.CreateFmt('No name=value pair at position %d.',[Index]);
  6.   while (blknr<=nrblocks) and (Index>blks[0].Entries) do begin
  7.     dec(Index,blks[0].Entries);
  8.     inc(blknr);
  9.     if blknr=nrblocks then Raise EListError.CreateFmt('No name=value pair at position %d.',[Index]);
  10.   end;
  11.   if Index>blks[blknr].Entries then Raise EListError.CreateFmt('No name=value pair at position %d.',[Index]);
  12.   result:=blks[blknr].keys[Index];
  13. end;
  14.  

Maybe marcov could confirm that it is compatible with the way GenLight works  O:-)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9706
  • FPC developer.
Re: [SOLVED] Generics.Collections TDictionary indexed access
« Reply #18 on: September 21, 2021, 08:46:34 pm »
This a very helpful hint, thank you. And I may assume that the arrays are filled in sorted order?

Yes.

 

TinyPortal © 2005-2018