Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2020-05

TFPGMap retrieving values

7 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ryan Joseph

I've been starting to use the RTL so I'm not very familiar with it but I thought TFPGMap was supposed to be a hash table for fast lookup, so why does TFPSMap.Find using a binary search instead of computing a hash key and indexing into the array like that? Is this not the type I should be using in the RTL for a generic hash table? What even is TFPGMap supposed to be if it isn't a hash table?

function TFPSMap.Find(AKey: Pointer; out Index: Integer): Boolean;
{ Searches for the first item <= Key, returns True if exact match,
  sets index to the index of the found string. }
var
  I,L,R,Dir: Integer;
begin
  Result := false;
  Index := -1;
  if not Sorted then
    raise EListError.Create(SErrFindNeedsSortedList);
  // Use binary search.
  L := 0;
  R := FCount-1;
  while L<=R do
  begin
    I := L + (R - L) div 2;
    Dir := FOnKeyPtrCompare(Items[I], AKey);
    if Dir < 0 then
      L := I+1
    else begin
      R := I-1;
      if Dir = 0 then
      begin
        Result := true;
        if Duplicates  dupAccept then
          L := I;
      end;
    end;
  end;
  Index := L;
end;

Regards,
Ryan Joseph

Reply by email

Sven Barth
to Ryan Joseph
Ryan Joseph wrote
I've been starting to use the RTL so I'm not very familiar with it but I thought TFPGMap was supposed to be a hash table for fast lookup, so why does TFPSMap.Find using a binary search instead of computing a hash key and indexing into the array like that? Is this not the type I should be using in the RTL for a generic hash table? What even is TFPGMap supposed to be if it isn't a hash table?

It is a map. It maps keys to values. Nowhere does it say how the map is implemented.

If you need to decide which map to use I suggest you to look at this benchmark page: http://www.benibela.de/fpc-map-benchmark_en.html You can select various of the maps that FPC supports (both ones distributed with FPC and third party ones) and compare them.

Regards,
Sven

Reply by email

Ryan Joseph
to Sven Barth
Show quoted text 5 lines
On May 1, 2020, at 10:18 PM, Sven Barth wrote: It is a map. It maps keys to values. Nowhere does it say how the map is implemented. If you need to decide which map to use I suggest you to look at this benchmark page: http://www.benibela.de/fpc-map-benchmark_en.html You can select various of the maps that FPC supports (both ones distributed with FPC and third party ones) and compare them.

I thought it was a hash map! From the page:

"The array based TStringList and TFPGMap are the most memory efficient, using less memory than even the cuckoo maps. However, as they are not hash maps, their usage is extremely slow, even if the maps are sorted, so they are nearly useless as maps. Their default setting of unsorted and (in case of TStringList) case-insensitive matching are quite dangerous."

Regards,
Ryan Joseph

Reply by email

Ryan Joseph
to Sven Barth: It is a map. It maps keys to values. Nowhere does it say how the map is implemented. If…
On May 1, 2020, at 10:18 PM, Sven Barth wrote: If you need to decide which map to use I suggest you to look at this benchmark page: http://www.benibela.de/fpc-map-benchmark_en.html You can select various of the maps that FPC supports (both ones distributed with FPC and third party ones) and compare them.

and btw, why isn't there a TFPGHashList? I can't find one anyways.

Regards,
Ryan Joseph

Reply by email

Sven Barth
to Ryan Joseph

Ryan Joseph via fpc-pascal schrieb am
Fr., 1. Mai 2020, 17:48:

Show quoted text 12 lines
> On May 1, 2020, at 10:18 PM, Sven Barth wrote: > > If you need to decide which map to use I suggest you to look at this benchmark page: http://www.benibela.de/fpc-map-benchmark_en.html You can select various of the maps that FPC supports (both ones distributed with FPC and third party ones) and compare them. and btw, why isn't there a TFPGHashList? I can't find one anyways.

Because none was needed. Fgl is mainly a demonstrator for the generic feature and nowadays mainly useful for smaller targets. The main suggestion is to use Collections.Generics and there the TDictionary class.

Regards,
Sven

Reply by email

Marco van de Voort
to Sven Barth

Op 2020-05-01 om 20:15 schreef Sven Barth via fpc-pascal:

Show quoted text 24 lines
Ryan Joseph via fpc-pascal < > schrieb am Fr., 1. Mai 2020, 17:48: > On May 1, 2020, at 10:18 PM, Sven Barth < > wrote: > > If you need to decide which map to use I suggest you to look at this benchmark page: http://www.benibela.de/fpc-map-benchmark_en.html You can select various of the maps that FPC supports (both ones distributed with FPC and third party ones) and compare them. and btw, why isn't there a TFPGHashList? I can't find one anyways. Because none was needed. Fgl is mainly a demonstrator for the generic feature and nowadays mainly useful for smaller targets. The main suggestion is to use Collections.Generics and there the TDictionary class.

And don't forget to benchmark. If you test/search for existence of items, a faster hash that looks up faster but takes long to build and eats more memory might be counter productive.

Reply by email

Sven Barth
to Ryan Joseph: I thought it was a hash map! From the page: "The array based TStringList and TFPGMap are…

Ryan Joseph via fpc-pascal schrieb am
Sa., 2. Mai 2020, 12:33:

Show quoted text 15 lines
> On May 1, 2020, at 10:18 PM, Sven Barth wrote: > > It is a map. It maps keys to values. Nowhere does it say how the map is implemented. > > If you need to decide which map to use I suggest you to look at this benchmark page: http://www.benibela.de/fpc-map-benchmark_en.html You can select various of the maps that FPC supports (both ones distributed with FPC and third party ones) and compare them. I thought it was a hash map! From the page:

And as I said: it's nowhere required that a map is a hash map. It was simply an assumption you made.

Regards,
Sven

Reply by email