Recent

Author Topic: FGL’s TFPGMap vs. Generics.Collections’ TDictionary  (Read 353 times)

JdeHaan

  • Jr. Member
  • **
  • Posts: 89
FGL’s TFPGMap vs. Generics.Collections’ TDictionary
« on: December 02, 2022, 03:36:25 pm »
Hi,

I have a question on the use of FGL’s TFPGMap class vs. Generics.Collections’ TDictionary.
If I add a new item or change an existing one in a TFPGMap I can use:
 
Code: Pascal  [Select][+][-]
  1. Map[Key] := Value;     (1)

However when using TDictionary I have to use:

Code: Pascal  [Select][+][-]
  1. Map.AddOrSetValue(Key, Value);    (2)

since I don’t know if the (Key, Value) pair is added or changed at a certain moment.

Needless to say I like (1) more.

Why this difference? Can it be changed for TDictionary? Or is it for Delphi…

Cheers

Thaddy

  • Hero Member
  • *****
  • Posts: 12599
Re: FGL’s TFPGMap vs. Generics.Collections’ TDictionary
« Reply #1 on: December 02, 2022, 04:21:18 pm »
Both work almost the same way.
In generics.collections you can also do:
Code: Pascal  [Select][+][-]
  1. Map[Key] := Value;
assuming map is a Tdictionary. Items[] is a default property.
rtl-generics is indeed Delphi compatible. It is also often a bit faster on some operations.
« Last Edit: December 02, 2022, 04:27:26 pm by Thaddy »
Writing code in an overly complex way is usually merely a complete misunderstanding of the goal you are trying to achieve.

jamie

  • Hero Member
  • *****
  • Posts: 5068
Re: FGL’s TFPGMap vs. Generics.Collections’ TDictionary
« Reply #2 on: December 02, 2022, 04:25:16 pm »
Call TryGetData first to see if it already exists?
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 12599
Re: FGL’s TFPGMap vs. Generics.Collections’ TDictionary
« Reply #3 on: December 02, 2022, 04:28:53 pm »
nonsense. He can do the simple assignment in both rtl-generics and fgl.
Writing code in an overly complex way is usually merely a complete misunderstanding of the goal you are trying to achieve.

avk

  • Hero Member
  • *****
  • Posts: 686
Re: FGL’s TFPGMap vs. Generics.Collections’ TDictionary
« Reply #4 on: December 02, 2022, 05:50:24 pm »
Hi,

I have a question on the use of FGL’s TFPGMap class vs. Generics.Collections’ TDictionary.
If I add a new item or change an existing one in a TFPGMap I can use:
 
Code: Pascal  [Select][+][-]
  1. Map[Key] := Value;     (1)

However when using TDictionary I have to use:

Code: Pascal  [Select][+][-]
  1. Map.AddOrSetValue(Key, Value);    (2)

since I don’t know if the (Key, Value) pair is added or changed at a certain moment.

Needless to say I like (1) more.

Why this difference? Can it be changed for TDictionary? Or is it for Delphi…

Cheers

I agree, it's inconvenient. Perhaps it would make sense to issue a feature request. Or maybe use a custom TDictionary descendant:
Code: Pascal  [Select][+][-]
  1. type
  2.   generic TMyDictionary<TKey, TValue> = class(specialize TDictionary<TKey, TValue>)
  3.   private
  4.      function GetValue(const aKey: TKey): TValue; inline;
  5.   public
  6.      procedure AddOrSetValue(const AKey: TKey; const AValue: TValue); inline;
  7.      property Items[Index: TKey]: TValue read GetValue write AddOrSetValue; default;
  8.   end;
  9.   ...
  10.   ...
  11. function TMyDictionary.GetValue(const aKey: TKey): TValue;
  12. begin
  13.   Result := inherited Items[aKey];
  14. end;
  15.  
  16. procedure TMyDictionary.AddOrSetValue(const AKey: TKey; const AValue: TValue);
  17. begin
  18.   inherited AddOrSetValue(AKey, AValue);
  19. end;
  20.  

Thaddy

  • Hero Member
  • *****
  • Posts: 12599
Re: FGL’s TFPGMap vs. Generics.Collections’ TDictionary
« Reply #5 on: December 02, 2022, 06:07:00 pm »
@avk
It  is rare that you did not see my point.
Writing code in an overly complex way is usually merely a complete misunderstanding of the goal you are trying to achieve.

avk

  • Hero Member
  • *****
  • Posts: 686
Re: FGL’s TFPGMap vs. Generics.Collections’ TDictionary
« Reply #6 on: December 02, 2022, 06:24:29 pm »
As far as I've been able to figure out, the OP's problem is that the setter TDictionary.SetItem() raises an exception when trying to assign a value to a non-existent key, or am I missing something?

Thaddy

  • Hero Member
  • *****
  • Posts: 12599
Re: FGL’s TFPGMap vs. Generics.Collections’ TDictionary
« Reply #7 on: December 02, 2022, 08:48:43 pm »
Well,  it is weekend, so I will  investigate with code. There should not be a difference.
Writing code in an overly complex way is usually merely a complete misunderstanding of the goal you are trying to achieve.

JdeHaan

  • Jr. Member
  • **
  • Posts: 89
Re: FGL’s TFPGMap vs. Generics.Collections’ TDictionary
« Reply #8 on: December 02, 2022, 08:58:32 pm »
Thanks for the responses. Indeed for TDictionary the Key,Value pair must already exist in order to use "Map[Key] := Value;".
So, I should create a child class of TDictionary to make this happen?


Thaddy

  • Hero Member
  • *****
  • Posts: 12599
Re: FGL’s TFPGMap vs. Generics.Collections’ TDictionary
« Reply #9 on: December 02, 2022, 09:06:03 pm »
No, not at all, it is a generic! Anyway come back to this tomorrow, End of week first, weekend later and it is EASY.
Writing code in an overly complex way is usually merely a complete misunderstanding of the goal you are trying to achieve.

 

TinyPortal © 2005-2018