Is there a Dictionary object for FreePascal?
Asked Answered
P

3

6

It's incredibly impossible to find things for FreePascal because all searches end up at some sort of Delphi related site.

Is there a built-in Dictionary object?

This page references "TDictionary" under the Generic Classes section, but I have no idea what unit it might be in, or if it even exists.

Pteridophyte answered 2/4, 2013 at 21:45 Comment(3)
Unit fgl contains generic classes defined here. An example using TFPGMap: Class Map or Dictionary, anyone?.Poisoning
Closest, indeed, is fgl.TFPGMap I think. LU RD: make it a proper answer so that it can be accepted.Trainload
To Google articles prepend "fpc pascal" to your searchesRevelry
P
7

The unit fgl contains the basic generic classes for freepascal.

Among those classes, the closest to a TDictionary is TFPGMap.

An example how to use this class can be found here: Class Map or Dictionary, anyone?.

Poisoning answered 3/4, 2013 at 10:55 Comment(4)
Forget about TFPGMap. It is implemented as array, which is just too slow for any purpose. Even using an actual array would be faster. If you are forced to use it, you need to set sorted to true, then it uses at least a binary search for reading rather than a linear one.By
@By so what did you end up using?Primeval
@jj_ First I benchmarked them all. Rtl-generics seem to be the best overall, but I do not trust them below fpc 3.1. (complex generics are tricky, even Lazarus crashed when I opened their source) ghashmap.THashMap is an okay fallback. Although I ended up using the hash map from Bero's Fast Light Regular Expressions library, because I was already using that library for regexs and it is surprisingly fastBy
Thanks! Good source of updated info for someone new to Free Pascal.Primeval
M
7

You can use very compatible TDictionary from Generics.Collections unit:

works for FPC trunk rev. 30239 and newer.

Edit 05.08.2016

Generics.Collections library has been added to FPC trunk as rtl-generics package in r34229. Latest version of precompiled FPC trunk (with Generics.Collections) for Win32 + Lazarus trunk available at http://newpascal.org . The repository of Generics.Collections ( https://github.com/dathox/generics.collections ) will be still used for maintenance (should be synced often with FPC trunk).

Melgar answered 24/1, 2015 at 20:18 Comment(0)
F
-1

Here is Example how to use

uses
  Classes, SysUtils, Generics.Collections;
type
  HashMap = specialize TDictionary<string, string>;

implementation

function parseCommandLine: HashMap;
var
  ArgMap: HashMap;
  i: Integer;
begin
  ArgMap := HashMap.Create;
  try
    i := 1;
    while i <= ParamCount do
    begin
      if ParamStr(i).StartsWith('-') and (i < ParamCount) then
      begin
        ArgMap.Add(ParamStr(i), ParamStr(i + 1));
        Inc(i); // Skip the next parameter as it's already paired
      end;
      Inc(i);
    end;
    Result := ArgMap;
  except
    ArgMap.Free;
    raise;
  end;
end;          
Faro answered 18/8, 2024 at 22:25 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.