Recent

Author Topic: TArrayHelper  (Read 996 times)

ginoo

  • Full Member
  • ***
  • Posts: 148
TArrayHelper
« on: November 05, 2025, 02:28:11 pm »
If I have an array of records like this:
Code: Pascal  [Select][+][-]
  1. myarray = record
  2.   min: Integer;
  3.   max: Integer;
  4. end;
  5. arrMyArray: array of myarray;
  6.  

How can I apply the TArrayHelper soft and binarysearch methods?

Thanks

jamie

  • Hero Member
  • *****
  • Posts: 7515
Re: TArrayHelper
« Reply #1 on: November 06, 2025, 12:38:58 am »
The implementation of this https://github.com/WilliCommer/ArrayHelper/blob/master/ArrayHelper.pas is
over the top, plus with fpc 3.2.2 that can't be compiled as is.
 
 You would be better off if you were to use an Advanced Generic record<T> that holds your array plus add the needed procedures and functions to compute the needed actions.

  If you are not fluid with this, I am sure plenty can help.

Jamie


 
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 18728
  • To Europe: simply sell USA bonds: dollar collapses
Re: TArrayHelper
« Reply #2 on: November 06, 2025, 07:49:05 am »
TArrayHelper is in generics.collections.....Jamie...
All you need for sort and binarysearch is to provide a comparer for your record type.

Simple example:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. uses
  3.    classes,generics.defaults, generics.collections;
  4. type
  5.   TARecord = record
  6.     min,
  7.     max:integer;
  8.   end;
  9.  
  10.   TMinComparer = class(TInterfacedObject,IComparer<TARecord>)
  11.     function Compare(const Left, Right: TARecord): Integer; overload;
  12.   end;
  13.  
  14.   TMaxComparer = class(TInterfacedObject,IComparer<TARecord>)
  15.     function Compare(const Left, Right: TARecord): Integer; overload;
  16.   end;
  17.  
  18.   function TMinComparer.Compare(const left,right:TARecord):integer;
  19.   begin
  20.     result := 0;
  21.     if right.min > left.min then result := -1
  22.     else if right.min < left.min then result := 1;
  23.   end;
  24.  
  25.   function TMaxComparer.Compare(const left,right:TARecord):integer;
  26.   begin
  27.     result := 0;
  28.     if right.max > left.max then result := -1
  29.     else if right.max < left.max then result := 1;
  30.   end;
  31.  
  32. var
  33.   RecordArray:TArray<TARecord> = nil; // same as array of TArecord.
  34.   MinComparer, MaxComparer:IComparer<TARecord>;
  35.   R:TARecord;
  36. begin
  37.   MinComparer := TMinComparer.Create; // note: is IComparer interface.
  38.   MaxComparer := TMaxComparer.Create;
  39.   SetLength(RecordArray,4);
  40.   RecordArray[0].min := 100;
  41.   RecordArray[0].max := 200;;
  42.   RecordArray[1].min := 10;
  43.   RecordArray[1].max := 20;;
  44.   RecordArray[2].min := 500;
  45.   RecordArray[2].max := 2;
  46.   RecordArray[3].min := 7;
  47.   RecordArray[3].max := 17;
  48.   TArrayHelper<TARecord>.Sort(RecordArray,MinComparer);
  49.   for R in RecordArray do write(R.Min:4);
  50.   writeln;
  51.   TArrayHelper<TARecord>.Sort(RecordArray,MaxComparer);
  52.   for R in RecordArray do write(R.Max:4);
  53.   writeln;
  54. end.
The BinarySearch works the same. Sort and BinarySearch are class methods, so TArrayHelper needs no instance.
The comparers are interfaces, so need no free.
You can use the same comparers for both.
« Last Edit: November 06, 2025, 09:44:46 am by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

Thaddy

  • Hero Member
  • *****
  • Posts: 18728
  • To Europe: simply sell USA bonds: dollar collapses
Re: TArrayHelper
« Reply #3 on: November 06, 2025, 09:48:36 am »
BTW: the above compiles with 3.2.2 too.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

cdbc

  • Hero Member
  • *****
  • Posts: 2600
    • http://www.cdbc.dk
Re: TArrayHelper
« Reply #4 on: November 06, 2025, 11:02:37 am »
Hi
Nice example Thaddy =^
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

avk

  • Hero Member
  • *****
  • Posts: 825
Re: TArrayHelper
« Reply #5 on: November 06, 2025, 11:40:14 am »
BTW: the above compiles with 3.2.2 too.

Of course not, FPC-3.2.2 requires a different comparator signature.

jamie

  • Hero Member
  • *****
  • Posts: 7515
Re: TArrayHelper
« Reply #6 on: November 06, 2025, 11:58:50 am »
Good luck getting that to work in a serious application I have given up on using anything from the collections the compiler will choke on it with untraceable error at the end of the main source.

Also I already implemented a class using the custom array helper in there why would you lard up the code so badly using interfaces to start with?

 If you are looking for auto cleanup at the end of the use you could do much better using advanced records where you can initiate automatically at the start and clean up without additional intervention code .

 Have it your way lard and choke the compiler or efficiency and make it work.
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 18728
  • To Europe: simply sell USA bonds: dollar collapses
Re: TArrayHelper
« Reply #7 on: November 06, 2025, 12:44:51 pm »
Well, opinions differ. I found it a bit erratic that you came up with something that is not even in the rtl and nobody ever heared of.
The question is obviously for the rtl.


For completeness binarysearch:
Code: Pascal  [Select][+][-]
  1.  
  2. // add a var named Idx:SizeInt;
  3.   R.min := 100;
  4.   R.max := 200;
  5.   writeln(TArrayHelper<TARecord>.BinarySearch(RecordArray, R,Idx, MinComparer));
  6.   writeln(idx);// idx is one based. Subtract 1 for the true index.
  7.  

One remark: there are indeed very few simple and practical examples on how to use rtl-generics. Even for Delphi. I had a look at the tests.
« Last Edit: November 06, 2025, 03:26:25 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

ginoo

  • Full Member
  • ***
  • Posts: 148
Re: TArrayHelper
« Reply #8 on: November 06, 2025, 02:42:20 pm »
@Thaddy Thanks, great example. I think you could post it on the wiki; it would be helpful.

@avk. For fpc3.2.2, just replace constref with const

Thaddy

  • Hero Member
  • *****
  • Posts: 18728
  • To Europe: simply sell USA bonds: dollar collapses
Re: TArrayHelper
« Reply #9 on: November 06, 2025, 03:00:05 pm »
Of course not, FPC-3.2.2 requires a different comparator signature.
You are right. I compiled the test with 3.2.3.... (Bug fixed) I did not realize that I compiled for 3.2.3. My main compilers are always, well, ...main.
https://wiki.freepascal.org/User_Changes_3.2.4  // in this case matches 3.2.3
But I should have been more careful. 3.2.2 or lower needs constref, which is not optimal.
« Last Edit: November 06, 2025, 03:19:58 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

Thaddy

  • Hero Member
  • *****
  • Posts: 18728
  • To Europe: simply sell USA bonds: dollar collapses
Re: TArrayHelper
« Reply #10 on: November 06, 2025, 07:10:33 pm »
Example got stolen by A.I... :D (already)
Anyway, I don't care, as long as you all are happy...and you all know I wrote it.
(Good or bad)
« Last Edit: November 06, 2025, 07:18:10 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

 

TinyPortal © 2005-2018