[fpc-pascal] How does TFPGMap key compare work?

Sven Barth pascaldragon at googlemail.com
Tue Apr 20 23:10:36 CEST 2021


Am 19.04.2021 um 19:05 schrieb Ryan Joseph via fpc-pascal:
> I have a question I was just curious about. From what I can tell TFPGMap uses CompareByte to compare keys of arbitrary type, which is clever but how does this work for ShortStrings? I have tried to use this method myself and I find it always fails because short strings have garbage at the end and so even if you zero out the memory (which is allocated) a short string passed as a parameter to a function will have garbage and thus fail to compare. Any ideas how this works for TFPGMap then?
>
> function TFPSMap.BinaryCompareKey(Key1, Key2: Pointer): Integer;
> begin
>    Result := CompareByte(Key1^, Key2^, FKeySize);
> end;

If you look at TFPSMap' code you'll see that BinaryCompareKey and 
BinaryCompareData are only used in the way of method pointers 
OnKeyPtrCompare and OnDataPtrCompare. In TFPGMap<,> these are then set 
to compare methods specific to the specialization, most importantly 
TFPGMap<,>.KeyCompare if no custom compare function is set.

Regards,
Sven


More information about the fpc-pascal mailing list