MultiMap<TPair<Double, Integer>, Integer>

154 views
Skip to first unread message

François Piette

unread,
Feb 9, 2021, 3:36:29 AM2/9/21
to Spring4D

Hello,

I'm trying to port C++ code containing this local variable declaration:

    std::multimap<std::pair<double, int>, int, event_less> events;

where event_less is a comprarer class:
class event_less
{
public:
    bool operator()(const std::pair<double, int>& a, const std::pair<double, int>& b) const
    {
        if (a.first < b.first - 1.0e-9)
            return true;
        else if (a.first > b.first + 1.0e-9)
            return false;
        else if (a.second < b.second)
            return true;
        return false;
    }

};

In Delphi, I declared this:
var
    Events : IMultiMap<TPair<Double, Integer>, Integer>;
    KeyComparer : IEqualityComparer<TPair<Double, Integer>>;
begin
    KeyComparer := TEqualityComparerInteger.Create;
    Events := TMultiMap<TPair<Double, Integer>, Integer>.Create(KeyComparer);
    // More code
end;

and this:

    TEqualityComparerInteger = class(TInterfacedObject, IEqualityComparer<TPair<Double, Integer>>)
        function Equals(const Left, Right: TPair<Double, Integer>): Boolean; reintroduce;
        function GetHashCode(const Value: TPair<Double, Integer>): Integer; reintroduce;
    end;
function TEqualityComparerInteger.Equals(const Left, Right: TPair<Double, Integer>): Boolean;
begin
    if Left.Key < (Right.Key - 1E-9) then
        Result := TRUE
    else if Left.Key > (Right.Key + 1E-9) then
        Result := FALSE
    else if Left.Value < Right.Value then
        Result := TRUE
    else
        Result := FALSE;
end;

function TEqualityComparerInteger.GetHashCode(const Value: TPair<Double, Integer>): Integer;
begin
    Result := Value.Value + Round(Value.Key * 1E6);
end;

I get a compiler error on the line calling TMultiMap constructor:

[dcc32 Error] TestCode.pas(113): E2250 There is no overloaded version of 'Create' that can be called with these arguments

I don't understand what I'm doing wrong.

François Piette

unread,
Feb 10, 2021, 12:57:03 AM2/10/21
to Spring4D
This issue is the uses clause that I didn't showed: I was using Spring.Tests.Interception.Types instead of System.Generics.Collections.

Stefan Glienke

unread,
Feb 10, 2021, 5:58:28 AM2/10/21
to Spring4D
Rule number one with spring4d collections - always create them via TCollections.Create.... from Spring.Collections.pas thats usually the only unit that should be in the uses, no other Spring.Collections.* unit
Reply all
Reply to author
Forward
0 new messages