fgl.TFPGMap

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

fgl.TFPGMap

Alexander Shishkin
Following code causes error "Operator is not overloaded: "TObject" <
"TObject""
How to make it work?

program generictest2;
{$mode objfpc}{$H+}
uses
   fgl;
operator < (a: TObject; b: TObject): boolean;
begin
   result := PtrInt(a) < PtrInt(b);
end;
type
   TMyMap = specialize TFPGMap <TObject, TObject>;
begin
end.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fgl.TFPGMap

Honza-5
2011/1/20 Alex Shishkin <[hidden email]>:

> Following code causes error "Operator is not overloaded: "TObject" <
> "TObject""
> How to make it work?
>
> program generictest2;
> {$mode objfpc}{$H+}
> uses
>  fgl;
> operator < (a: TObject; b: TObject): boolean;
> begin
>  result := PtrInt(a) < PtrInt(b);
> end;
> type
>  TMyMap = specialize TFPGMap <TObject, TObject>;
> begin
> end.
AFAIK TFPGMap can be specialized only with types which have the
relational operators predefined by the compiler. See also:
http://bugs.freepascal.org/view.php?id=15480

You can take a look on this container lib, which might be helpful for your task:
http://code.google.com/p/fprb/wiki/heContnrs
--
bflm
freepascal-bits.blogspot.com
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fgl.TFPGMap

Alexander Shishkin
20.01.2011 22:18, Honza пишет:

> 2011/1/20 Alex Shishkin<[hidden email]>:
>> Following code causes error "Operator is not overloaded: "TObject"<
>> "TObject""
>> How to make it work?
>>
>> program generictest2;
>> {$mode objfpc}{$H+}
>> uses
>>   fgl;
>> operator<  (a: TObject; b: TObject): boolean;
>> begin
>>   result := PtrInt(a)<  PtrInt(b);
>> end;
>> type
>>   TMyMap = specialize TFPGMap<TObject, TObject>;
>> begin
>> end.
> AFAIK TFPGMap can be specialized only with types which have the
> relational operators predefined by the compiler. See also:
> http://bugs.freepascal.org/view.php?id=15480
As I understand problem is in the compiler. It cant use overloaded
operator if it was defined after generic.
BTW this code compiles successfully.
program generictest2;
{$mode objfpc}{$H+}
operator > (a: TObject; b: TObject): boolean;
begin
   result := PtrInt(a) > PtrInt(b);
end;
type
   generic TComparator <T> = class
     class function Compare(a,b:T): Integer;
   end;
class function TComparator.Compare(a, b: T): Integer;
begin
   if a>b then
     Result := 1
   else if b>a then
     Result := -1
   else
     Result :=0;
end;

type
   TObjectComparator = specialize TComparator<TObject>;

begin

end.
Data can be any type  but Key only "native comparable". If drop
TFPGMap.KeyCompare along with DataCompare leaving only custom comparison
it will work with any Key type.
> You can take a look on this container lib, which might be helpful for your task:
> http://code.google.com/p/fprb/wiki/heContnrs
Good lib, but Lazarus codetools does not support  nested classes as for now.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fgl.TFPGMap

Honza-5
2011/1/20 Alex Shishkin <[hidden email]>:
> As I understand problem is in the compiler. It cant use overloaded operator
> if it was defined after generic.
AFAIK exactly that is the problem and that's why TFPGMap can't cope
with some types.

>> You can take a look on this container lib, which might be helpful for your
>> task:
>> http://code.google.com/p/fprb/wiki/heContnrs
>
> Good lib, but Lazarus codetools does not support  nested classes as for now.
That's unfortunately a thing I can't help you with.

--
bflm
freepascal-bits.blogspot.com
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fgl.TFPGMap

leledumbo
Administrator
In reply to this post by Alexander Shishkin
Another good one: STLPascal
Uses pattern commonly used by C++ STL, but fortunately written in a much more readable way. It has more data structures than heContnrs, but missing the BTree implementation. Really, IMHO somebody's got to merge all of these libraries (including fgl).
Reply | Threaded
Open this post in threaded view
|

Re: fgl.TFPGMap

Alexander Shishkin
21.01.2011 0:26, leledumbo пишет:
> Another good one:  http://code.google.com/p/stlpascal/ STLPascal
> Uses pattern commonly used by C++ STL, but fortunately written in a much
> more readable way. It has more data structures than heContnrs, but missing
> the BTree implementation. Really, IMHO somebody's got to merge all of these
> libraries (including fgl).
stlpascal released under GPLv3 but fpc libraries under modified LGPL. If
stlpascal will change license to modified LGPL it will be good to see it
in fcl.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fgl.TFPGMap

Paul Ishenin-2
In reply to this post by Alexander Shishkin
21.01.2011 0:28, Alex Shishkin wrote:

> Following code causes error "Operator is not overloaded: "TObject" <
> "TObject""
> How to make it work?
>
> program generictest2;
> {$mode objfpc}{$H+}
> uses
>   fgl;
> operator < (a: TObject; b: TObject): boolean;
> begin
>   result := PtrInt(a) < PtrInt(b);
> end;
> type
>   TMyMap = specialize TFPGMap <TObject, TObject>;
> begin
> end.
Please report this bug to the tracker.

Best regards,
Paul Ishenin
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fgl.TFPGMap

Alexander Shishkin
21.01.2011 3:07, Paul Ishenin пишет:

> 21.01.2011 0:28, Alex Shishkin wrote:
>> Following code causes error "Operator is not overloaded: "TObject" <
>> "TObject""
>> How to make it work?
>>
>> program generictest2;
>> {$mode objfpc}{$H+}
>> uses
>>   fgl;
>> operator < (a: TObject; b: TObject): boolean;
>> begin
>>   result := PtrInt(a) < PtrInt(b);
>> end;
>> type
>>   TMyMap = specialize TFPGMap <TObject, TObject>;
>> begin
>> end.
> Please report this bug to the tracker.
>
> Best regards,
> Paul Ishenin
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
>
Also please see http://bugs.freepascal.org/view.php?id=18573, this
related with generics to.
Alex
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal