Re: [fpc-pascal] Need some advice

2024-01-24 Thread ppadilcdx via fpc-pascal

Oh I didn't notice the typo, duh.  Thanks!!

On 1/24/24 02:57PM, Sven Barth via fpc-pascal wrote:
ppadilcdx via fpc-pascal  schrieb am 
Mi., 24. Jan. 2024, 22:07:


Thanks for that, now it compiles but it doesn't work, i.e. I added a
pair and value and then used trygetdata and it can't find it. So
perhaps
I'm missing something else :


Your operator needs to establish a true order between the two passed 
in values. Right now your operators only check a, but not b. That is 
wrong and thus the map won't work correctly.


Regards,
Sven

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Need some advice

2024-01-24 Thread Sven Barth via fpc-pascal
ppadilcdx via fpc-pascal  schrieb am Mi.,
24. Jan. 2024, 22:07:

> Thanks for that, now it compiles but it doesn't work, i.e. I added a
> pair and value and then used trygetdata and it can't find it. So perhaps
> I'm missing something else :
>

Your operator needs to establish a true order between the two passed in
values. Right now your operators only check a, but not b. That is wrong and
thus the map won't work correctly.

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Need some advice

2024-01-24 Thread ppadilcdx via fpc-pascal
Thanks for that, now it compiles but it doesn't work, i.e. I added a 
pair and value and then used trygetdata and it can't find it. So perhaps 
I'm missing something else :


program test;
{$mode objfpc}
{$modeswitch advancedrecords}
uses
  fgl;

type
   pair = record
  x, y : integer;
  class operator > (a,b: pair) r:boolean;
  class operator < (a,b: pair) r:boolean;
   end;

class operator pair.< (a,b: pair) r:boolean;
begin
   if a.x < a.y then
  r := true
   else
  r := false;
end;

class operator pair.> (a,b: pair) r:boolean;
begin
   if a.x > a.y then
  r := true
   else
  r := false;
end;

function cmp(const a,b : pair) : LongInt;
begin
   if a < b then
  cmp := -1
   else
  if a > b then
 cmp := 1
  else
 cmp := 0;
end;

type
  TMyDict = specialize TFPGMap;
var
  Dict: TMyDict;
  key: pair;
  Value: Integer;
  flg: boolean;
begin
   Dict := TMyDict.Create;
   Dict.OnKeyCompare := @cmp;
   key.x := 2;
   key.y := 4;
   Dict.Add(key, 99);
   flg := Dict.trygetdata(key, Value);
   writeln(flg);
   writeln(Value);
   Dict.Free;
end.

On 1/24/24 12:34PM, Luca Olivetti via fpc-pascal wrote:

El 24/1/24 a les 20:31, ppadilcdx via fpc-pascal ha escrit:
Trying to use the fgl unit, specifically the TFPGMap.   Below is a 
simple program I'm trying to compile; it gives me two errors:


fgl.pp(1582,18) Error: Operator is not overloaded: "pair" < "pair"
fgl.pp(1584,23) Error: Operator is not overloaded: "pair" > "pair"




add a {$Modeswitch advancedrecords} and use class operators


program test;
{$mode objfpc}

  {$modeswitch advancedrecords}


uses
   fgl;

type
    pair = record
   x, y : integer;

    class operator < (a,b:pair):boolean;
    class operator > (a,b:pair):boolean;

    end;





operator < (a,b: pair) r:boolean;


   class operator pair.<(a,b:pair):boolean;


begin
    if a.x < a.y then
   r := true

 result:=true

    else
   r := false;

 result:=false;

end;


bye

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Need some advice

2024-01-24 Thread Luca Olivetti via fpc-pascal

El 24/1/24 a les 20:31, ppadilcdx via fpc-pascal ha escrit:
Trying to use the fgl unit, specifically the TFPGMap.   Below is a 
simple program I'm trying to compile; it gives me two errors:


fgl.pp(1582,18) Error: Operator is not overloaded: "pair" < "pair"
fgl.pp(1584,23) Error: Operator is not overloaded: "pair" > "pair"




add a {$Modeswitch advancedrecords} and use class operators


program test;
{$mode objfpc}

  {$modeswitch advancedrecords}


uses
   fgl;

type
    pair = record
   x, y : integer;

class operator < (a,b:pair):boolean;
class operator > (a,b:pair):boolean;

    end;





operator < (a,b: pair) r:boolean;


   class operator pair.<(a,b:pair):boolean;


begin
    if a.x < a.y then
   r := true

 result:=true

    else
   r := false;

 result:=false;

end;


bye
--
Luca

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


[fpc-pascal] Need some advice

2024-01-24 Thread ppadilcdx via fpc-pascal
Trying to use the fgl unit, specifically the TFPGMap.   Below is a 
simple program I'm trying to compile; it gives me two errors:


fgl.pp(1582,18) Error: Operator is not overloaded: "pair" < "pair"
fgl.pp(1584,23) Error: Operator is not overloaded: "pair" > "pair"


program test;
{$mode objfpc}

uses
  fgl;

type
   pair = record
  x, y : integer;
   end;

operator < (a,b: pair) r:boolean;
begin
   if a.x < a.y then
  r := true
   else
  r := false;
end;

operator > (a,b: pair) r:boolean;
begin
   if a.x > a.y then
  r := true
   else
  r := false;
end;

function cmp(const a,b : pair) : LongInt;
begin
   if a < b then
  cmp := -1
   else
  if a > b then
 cmp := 1
  else
 cmp := 0;
end;

type
  TMyDict = specialize TFPGMap;
var
  Dict: TMyDict;
  key: pair;
  Value: Integer;
begin
   Dict := TMyDict.Create;
   Dict.OnKeyCompare := @cmp;
   key.x := 2;
   key.y := 4;
   Dict.Add(key, 99);
   Value := Dict[key];
   writeln(Value);
   Dict.Free;
end.

Any advice appreciated. Thanks.

Pete

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal