Recent

Author Topic: Output TFPGMap<T,T> key or value as an array  (Read 4469 times)

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
Output TFPGMap<T,T> key or value as an array
« on: June 21, 2018, 03:17:01 pm »
How can I output all the keys or values in a TFPGMap as an array?

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: Output TFPGMap<T,T> key or value as an array
« Reply #1 on: June 21, 2018, 04:17:39 pm »
Other than looping through the elements yourself? Nope.
But with the TDictionary<> from rlt-generics, you simply can call ToArray. (A dictionary is the same as a map, if you don't know)
Specialize a type, not a var.

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
Re: Output TFPGMap<T,T> key or value as an array
« Reply #2 on: June 21, 2018, 04:35:33 pm »
Other than looping through the elements yourself? Nope.
But with the TDictionary<> from rlt-generics, you simply can call ToArray. (A dictionary is the same as a map, if you don't know)
thanks thaddy for replying.
I know that I can use rtl-generics but for this particular purpose, I don't want to import it.
do you have any example code I can use to loop through either the keys or values of a  TFPGMap<T,T> and output it to an array?

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: Output TFPGMap<T,T> key or value as an array
« Reply #3 on: June 21, 2018, 04:43:35 pm »
No, but I have the opposite:
Code: Pascal  [Select][+][-]
  1. {$ifdef fpc}{$mode delphi}{$H+}{$endif}
  2. uses
  3.   generics.collections;
  4. Type
  5.   TIntegerMap = TDictionary<integer,integer>;
  6. var
  7.   C:TIntegerMap;
  8.   K,V:array of integer;
  9.   i:integer;
  10. begin
  11.   C:= TIntegerMap.Create;
  12.   try
  13.     C.Add(100,102);
  14.     C.Add(103,104);
  15. // edit for comment: this is all...
  16.     K := C.keys.ToArray;
  17.     V := C.values.ToArray;
  18. // The rest of the code in both examples is the same.
  19.   finally
  20.     for i in K do write('Key':8,i:8); // dump keys
  21.     writeln;
  22.     for i in V do write('Value':8,i:8); // dump values
  23.     C.free;
  24.   end;
  25. end.
That's:
- a: Delphi compatible
- b: also available for FPC 3.0.4 through Maciej's website. (but it is in trunk as standard)
- c: really fast.

[EDIT]
Ok, same code for FGL (not!!!!! recommended, but almost just as easy to write except you have to write the loop yourself):
Code: Pascal  [Select][+][-]
  1. {$ifdef fpc}{$mode delphi}{$H+}{$endif}
  2. uses fgl;
  3.  type
  4.    TIntegerMap = TFPGMap<integer, integer>;
  5. var
  6.   C:TIntegerMap;
  7.   K,V:array of integer;
  8.   I:integer;
  9. begin
  10.   c := TIntegermap.Create;
  11.   try
  12.     c.Add(100,102);
  13.     c.Add(103,104);
  14. // edit for comment: set the size of the arrays, and write the loop..
  15.     Setlength(K, c.Count);
  16.     Setlength(V, c.count);
  17.     for i := 0 to c.count-1 do
  18.     begin
  19.       K[i] := c.Keys[i];
  20.       V[i] := c.data[i];
  21.     end;
  22. // The rest of the code in both examples is the same.
  23.   finally
  24.     for i in k do write('Key':8,i:8); // dump keys
  25.     writeln;
  26.     for i in v do write('Value':8,i:8); // dump values
  27.     c.Free;
  28.   end;    
  29. end.

Usually I ignore such questions from"programmers" that are not noobs. If you don't know how to read the headers from FGL .... or write a loop... you are really not a programmer. < very grumpy  >:D >:D >:D >.
Laziness is not an excuse and plz follow sound advice.
« Last Edit: June 21, 2018, 08:10:45 pm by Thaddy »
Specialize a type, not a var.

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
Re: Output TFPGMap<T,T> key or value as an array
« Reply #4 on: June 21, 2018, 06:06:17 pm »
Well, you are right thaddy, may be I was been lazy.  :D
Thanks anyway.

 

TinyPortal © 2005-2018