Recent

Author Topic: [SOLVED] LGenerics - How to sort TGVector<integer>?  (Read 968 times)

ikel

  • New Member
  • *
  • Posts: 26
[SOLVED] LGenerics - How to sort TGVector<integer>?
« on: December 31, 2023, 08:33:37 am »
Hi,

I've been playing with LGenerics lately, and really like it.
Can someone show me as for why, in the following code I got this error.

Quote
LGIntegerListSortCmpRel.lpr(45,30) Error: Call by var for arg no. 1 has to match exactly: Got "TGVector<System.LongInt>" expected "TGLiteVector<System.LongInt>"

I passed TGVector into sort, not TGLiteVector.
Am I sorting the `TGVector<integer>` correctly?


Code: Pascal  [Select][+][-]
  1. program LGIntegerListSortCmpRel;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes,
  7.   lgVector,
  8.   lgUtils;
  9.  
  10. type
  11.   TIntCmpRel = class
  12.     class function Less(const L, R: integer): boolean; static;
  13.   end;
  14.  
  15.   class function TIntCmpRel.Less(const L, R: integer): boolean;
  16.   begin
  17.     Result := L < R;
  18.   end;
  19.  
  20. type
  21.   TIntVector = specialize TGVector<integer>;
  22.   TIntHelper = specialize TGBaseVectorHelper<integer, TIntCmpRel>;
  23.  
  24. var
  25.   myInteger: TIntVector;
  26.   i: integer;
  27.  
  28. begin
  29.   // Creating a new instance of TGVector<integer>
  30.   myInteger := TIntVector.Create([11, 33, 22, 55, 44, 66]);
  31.   try
  32.     // Adding numbers
  33.     myInteger.Add(1);
  34.     myInteger.Add(10);
  35.     myInteger.Add(100);
  36.  
  37.     // Printing list
  38.     WriteLn('--original');
  39.     for i := 0 to myInteger.Count - 1 do
  40.     begin
  41.       WriteLn(myInteger[i]);
  42.     end;
  43.  
  44.     // Sorting descending using .Sort()
  45.     // this one gives: LGIntegerListSortCmpRel.lpr(45,30)
  46.     // Error: Call by var for arg no. 1 has to match exactly: Got "TGVector<System.LongInt>" expected "TGLiteVector<System.LongInt>"
  47.     TIntHelper.Sort(myInteger, TSortOrder.soDesc);
  48.  
  49.     // Printing sorted list
  50.     WriteLn('--sorted');
  51.     for i := 0 to myInteger.Count - 1 do
  52.     begin
  53.       WriteLn(myInteger[i]);
  54.     end;
  55.  
  56.  
  57.   finally
  58.     myInteger.Free;
  59.   end;
  60.  
  61.   ReadLn;
  62. end.
  63.  

Thanks.

-Ikel
« Last Edit: January 01, 2024, 02:49:44 am by ikel »

paweld

  • Hero Member
  • *****
  • Posts: 970
Re: LGenerics - How to sort TGVector<integer>?
« Reply #1 on: December 31, 2023, 09:24:41 am »
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes, SysUtils, lgVector;
  7.  
  8. type
  9.   TLGList = specialize TGVector<Integer>;
  10.   TLGHelper = specialize TGComparableVectorHelper<Integer>;
  11.  
  12. var
  13.   i: Integer;
  14.   list: TLGList;
  15. begin
  16.   Randomize;
  17.   list := TLGList.Create;
  18.   for i := 1 to 1000000 do
  19.     list.Add(Random(999999));
  20.   for i := 0 to 100 do  //top 100 unsorted
  21.     WriteLn(list[i]);
  22.   TLGHelper.Sort(list);
  23.   for i := 0 to 100 do  //top 100 sorted
  24.     WriteLn(list[i]);
  25.   ReadLn;
  26.   list.Free;
  27. end.  
Best regards / Pozdrawiam
paweld

ikel

  • New Member
  • *
  • Posts: 26
Re: LGenerics - How to sort TGVector<integer>?
« Reply #2 on: December 31, 2023, 10:40:49 am »
paweld,

Many thanks.
I see, integer can be compared using comparison operator.

I'll dig into the source code again and look into TGComparableVectorHelper.

Happy new year!

-ikel

avk

  • Hero Member
  • *****
  • Posts: 752
Re: LGenerics - How to sort TGVector<integer>?
« Reply #3 on: December 31, 2023, 11:31:56 am »
Quote
Code: Pascal  [Select][+][-]
  1.   TIntHelper.Sort(myInteger, TSortOrder.soDesc);
  2.  
-> Error: Call by var for arg no. 1 has to match exactly: Got "TGVector<System.LongInt>" expected "TGLiteVector<System.LongInt>"

To be honest, it looks like a compiler bug.
« Last Edit: December 31, 2023, 11:35:42 am by avk »

ikel

  • New Member
  • *
  • Posts: 26
Re: LGenerics - How to sort TGVector<integer>?
« Reply #4 on: December 31, 2023, 11:39:18 pm »
Quote
Code: Pascal  [Select][+][-]
  1.   TIntHelper.Sort(myInteger, TSortOrder.soDesc);
  2.  
-> Error: Call by var for arg no. 1 has to match exactly: Got "TGVector<System.LongInt>" expected "TGLiteVector<System.LongInt>"

To be honest, it looks like a compiler bug.

Thanks avk!
Can you show me how to submit a bug report?
Secondly, do you have a PayPal account? Or something similar? I'd like to contribute financially to LGenerics.

-ikel

avk

  • Hero Member
  • *****
  • Posts: 752
Re: LGenerics - How to sort TGVector<integer>?
« Reply #5 on: January 01, 2024, 11:45:08 am »
...
Can you show me how to submit a bug report?
...

A bit of investigation showed that the problem seems to be not in the compiler, but in the implementation code. I have made a fix, try to test it.

...
I'd like to contribute financially to LGenerics.
...

This is very surprising, and in the five and a half years that the library has been in the public space, this is the first suggestion of such a kind.
And it's clear that I'm not at all ready for that, please don't take offense.

ikel

  • New Member
  • *
  • Posts: 26
Re: [SOLVED] LGenerics - How to sort TGVector<integer>?
« Reply #6 on: January 02, 2024, 12:20:23 pm »
Avk,

I'll test the latest version. Many thanks.

Understood, no offence taken. Thanks for considering it.

-ikel

 

TinyPortal © 2005-2018