Recent

Author Topic: Is there a generic equivalent to THashedStringList?  (Read 1683 times)

asdf121

  • New Member
  • *
  • Posts: 35
Is there a generic equivalent to THashedStringList?
« on: February 13, 2019, 06:32:24 pm »
Hello,
I've some working code with a THashedStringList (was TStringList before) which is used to save a few hundreds of last inputs on my service. It's used to avoid double entries in a fast way with the hashing.
Now I was thinking about using a generic version like TDictionary for it as it has a better readability and I've read that you should better use generics if you write new code/do a refactoring.

So I'm adding my key=value pair to the THashedStringList as
Code: Pascal  [Select][+][-]
  1. Insert(0, key + '=' + value);
For cleanup I use the following code:
Code: Pascal  [Select][+][-]
  1.       i := THashedStringList.Count - 1;
  2.       if i > 1000 then
  3.       begin
  4.         while i > 500 do
  5.         begin
  6.           THashedStringList.Delete(i);
  7.           i := THashedStringList.Count - 1;
  8.         end;
  9.       end;

For the key and value I do
Code: Pascal  [Select][+][-]
  1. i := IndexOfName(key);
  2. value := ValueFromIndex[i];

But now my question, is there any equivalent version which uses generics where I can delete the oldest entries like I actually do?

I thought about using TDictionary<int id, TRecord<key,value>> but then I don't benefit from the hashing and have to think about the insertion of id. The same issue with hashing when I use a TList with TDictionary entries.

Thanks in advance!  :)

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Is there a generic equivalent to THashedStringList?
« Reply #1 on: February 16, 2019, 10:56:45 am »
From generics.collections
- THashMap
- TFastHashMap
- TObjectHashMap
- THashSet may also be an option
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

asdf121

  • New Member
  • *
  • Posts: 35
Re: Is there a generic equivalent to THashedStringList?
« Reply #2 on: February 16, 2019, 02:03:53 pm »
Oh, seems I forgot to mention that it should work in FPC and Delphi.  %)

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Is there a generic equivalent to THashedStringList?
« Reply #3 on: February 16, 2019, 02:20:16 pm »
generics.collections is supposed to be Delphi compatible? But indeed, Delphi lags a bit. In that case your stuck with a TList<T> or a TDictionary<Tkey,TValue>
« Last Edit: February 16, 2019, 02:25:51 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018