Forum > General
TArrayHelper
ginoo:
If I have an array of records like this:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---myarray = record min: Integer; max: Integer;end;arrMyArray: array of myarray;
How can I apply the TArrayHelper soft and binarysearch methods?
Thanks
jamie:
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
Thaddy:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$mode delphi}uses classes,generics.defaults, generics.collections;type TARecord = record min, max:integer; end; TMinComparer = class(TInterfacedObject,IComparer<TARecord>) function Compare(const Left, Right: TARecord): Integer; overload; end; TMaxComparer = class(TInterfacedObject,IComparer<TARecord>) function Compare(const Left, Right: TARecord): Integer; overload; end; function TMinComparer.Compare(const left,right:TARecord):integer; begin result := 0; if right.min > left.min then result := -1 else if right.min < left.min then result := 1; end; function TMaxComparer.Compare(const left,right:TARecord):integer; begin result := 0; if right.max > left.max then result := -1 else if right.max < left.max then result := 1; end; var RecordArray:TArray<TARecord> = nil; // same as array of TArecord. MinComparer, MaxComparer:IComparer<TARecord>; R:TARecord;begin MinComparer := TMinComparer.Create; // note: is IComparer interface. MaxComparer := TMaxComparer.Create; SetLength(RecordArray,4); RecordArray[0].min := 100; RecordArray[0].max := 200;; RecordArray[1].min := 10; RecordArray[1].max := 20;; RecordArray[2].min := 500; RecordArray[2].max := 2; RecordArray[3].min := 7; RecordArray[3].max := 17; TArrayHelper<TARecord>.Sort(RecordArray,MinComparer); for R in RecordArray do write(R.Min:4); writeln; TArrayHelper<TARecord>.Sort(RecordArray,MaxComparer); for R in RecordArray do write(R.Max:4); writeln;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.
Thaddy:
BTW: the above compiles with 3.2.2 too.
cdbc:
Hi
Nice example Thaddy =^
Regards Benny
Navigation
[0] Message Index
[#] Next page