Skip to content

Commit

Permalink
SingleMatrix: Add ArgExtrema and export Rot90
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Jul 22, 2023
1 parent 76086a6 commit d503d00
Show file tree
Hide file tree
Showing 12 changed files with 262 additions and 47 deletions.
1 change: 1 addition & 0 deletions Source/algorithms/simba.algo_difference.pas
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
}
unit simba.algo_difference;

{$DEFINE SIMBA_MAX_OPTIMIZATION}
{$i simba.inc}

interface
Expand Down
1 change: 1 addition & 0 deletions Source/algorithms/simba.algo_intersection.pas
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
}
unit simba.algo_intersection;

{$DEFINE SIMBA_MAX_OPTIMIZATION}
{$i simba.inc}

interface
Expand Down
39 changes: 10 additions & 29 deletions Source/algorithms/simba.algo_sort.pas
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
Project: Simba (https://github.com/MerlijnWajer/Simba)
License: GNU General Public License (https://www.gnu.org/licenses/gpl-3.0)
}

unit simba.algo_sort;

{$DEFINE SIMBA_MAX_OPTIMIZATION}
{$i simba.inc}

{$IFOPT D-}
{$OPTIMIZATION LEVEL4}
{$ENDIF}

interface

uses
Expand All @@ -25,13 +23,11 @@ interface
generic procedure QuickSort<_T>(var AValues: array of _T; ALeft, ARight: SizeInt);
generic procedure QuickSortWeighted<_T, _W>(var Arr: array of _T; var Weights: array of _W; iLo, iHi: SizeInt; SortUp: Boolean);

generic procedure Sort<_T>(var Arr: array of _T);
generic procedure Sort<_T>(var Arr: array of _T; Weights: TIntegerArray; SortUp: Boolean); overload;
generic procedure Sort<_T>(var Arr: array of _T; Weights: TDoubleArray; SortUp: Boolean); overload;
generic procedure Sort<_T>(var Arr: specialize TArray<_T>); overload;
generic procedure Sort<_T, _W>(var Arr: specialize TArray<_T>; Weights: specialize TArray<_W>; SortUp: Boolean); overload;

generic function Sorted<_T>(const Arr: specialize TArray<_T>): specialize TArray<_T>; overload;
generic function Sorted<_T>(const Arr: specialize TArray<_T>; Weights: TIntegerArray; SortUp: Boolean): specialize TArray<_T>; overload;
generic function Sorted<_T>(const Arr: specialize TArray<_T>; Weights: TDoubleArray; SortUp: Boolean): specialize TArray<_T>; overload;
generic function Sorted<_T, _W>(const Arr: specialize TArray<_T>; Weights: specialize TArray<_W>; SortUp: Boolean): specialize TArray<_T>; overload;

implementation

Expand Down Expand Up @@ -185,23 +181,16 @@ implementation
until iLo >= iHi;
end;

generic procedure Sort<_T>(var Arr: array of _T);
generic procedure Sort<_T>(var Arr: specialize TArray<_T>);
begin
specialize QuickSort<_T>(Arr, Low(Arr), High(Arr));
end;

generic procedure Sort<_T>(var Arr: array of _T; Weights: TIntegerArray; SortUp: Boolean);
begin
Weights := Copy(Weights);

specialize QuickSortWeighted<_T, Integer>(Arr, Weights, Low(Arr), High(Arr), SortUp);
end;

generic procedure Sort<_T>(var Arr: array of _T; Weights: TDoubleArray; SortUp: Boolean);
generic procedure Sort<_T, _W>(var Arr: specialize TArray<_T>; Weights: specialize TArray<_W>; SortUp: Boolean);
begin
Weights := Copy(Weights);

specialize QuickSortWeighted<_T, Double>(Arr, Weights, Low(Arr), High(Arr), SortUp);
specialize QuickSortWeighted<_T, _W>(Arr, Weights, Low(Arr), High(Arr), SortUp);
end;

generic function Sorted<_T>(const Arr: specialize TArray<_T>): specialize TArray<_T>;
Expand All @@ -211,20 +200,12 @@ implementation
specialize QuickSort<_T>(Result, Low(Result), High(Result));
end;

generic function Sorted<_T>(const Arr: specialize TArray<_T>; Weights: TIntegerArray; SortUp: Boolean): specialize TArray<_T>;
begin
Weights := Copy(Weights);
Result := Copy(Arr);

specialize QuickSortWeighted<_T, Integer>(Result, Weights, Low(Result), High(Result), SortUp);
end;

generic function Sorted<_T>(const Arr: specialize TArray<_T>; Weights: TDoubleArray; SortUp: Boolean): specialize TArray<_T>;
generic function Sorted<_T, _W>(const Arr: specialize TArray<_T>; Weights: specialize TArray<_W>; SortUp: Boolean): specialize TArray<_T>;
begin
Weights := Copy(Weights);
Result := Copy(Arr);

specialize QuickSortWeighted<_T, Double>(Result, Weights, Low(Result), High(Result), SortUp);
specialize QuickSortWeighted<_T, _W>(Result, Weights, Low(Result), High(Result), SortUp);
end;

end.
Expand Down
1 change: 1 addition & 0 deletions Source/algorithms/simba.algo_symmetricDifference.pas
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
}
unit simba.algo_symmetricDifference;

{$DEFINE SIMBA_MAX_OPTIMIZATION}
{$i simba.inc}

interface
Expand Down
1 change: 1 addition & 0 deletions Source/algorithms/simba.algo_unique.pas
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
}
unit simba.algo_unique;

{$DEFINE SIMBA_MAX_OPTIMIZATION}
{$i simba.inc}

interface
Expand Down
4 changes: 2 additions & 2 deletions Source/boxarray.inc
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,12 @@ end;

function TBoxArrayHelper.Sort(Weights: TIntegerArray; LowToHigh: Boolean): TBoxArray;
begin
Result := specialize Sorted<TBox>(Self, Weights, LowToHigh);
Result := specialize Sorted<TBox, Integer>(Self, Weights, LowToHigh);
end;

function TBoxArrayHelper.Sort(Weights: TDoubleArray; LowToHigh: Boolean): TBoxArray;
begin
Result := specialize Sorted<TBox>(Self, Weights, LowToHigh);
Result := specialize Sorted<TBox, Double>(Self, Weights, LowToHigh);
end;
{$ENDIF}

Expand Down
2 changes: 1 addition & 1 deletion Source/forms/simba.backupsform.lfm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ object SimbaBackupsForm: TSimbaBackupsForm
Height = 450
Top = 543
Width = 739
Caption = 'SimbaBackupsForm'
Caption = 'Backups'
ClientHeight = 450
ClientWidth = 739
DesignTimePPI = 120
Expand Down
12 changes: 12 additions & 0 deletions Source/script/imports/simba/simba.import_matrix.pas
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,16 @@ procedure _LapeSingleMatrix_Copy2(const Params: PParamArray; const Result: Point
PSingleMatrix(Result)^ := PSingleMatrix(Params^[0])^.Copy(PInteger(Params^[1])^, PInteger(Params^[2])^);
end;

procedure _LapeSingleMatrix_Rot90(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PSingleMatrix(Result)^ := PSingleMatrix(Params^[0])^.Rot90();
end;

procedure _LapeSingleMatrix_ArgExtrema(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PPointArray(Result)^ := PSingleMatrix(Params^[0])^.ArgExtrema(PInteger(Params^[1])^, PBoolean(Params^[2])^);
end;

// Double
procedure _LapeDoubleMatrix_Width(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
Expand Down Expand Up @@ -330,6 +340,8 @@ procedure ImportMatrix(Compiler: TSimbaScript_Compiler);
addGlobalFunc('function TSingleMatrix.Equals(Other: TSingleMatrix; Epsilon: Single = 0): Boolean;', @_LapeSingleMatrix_Equals);
addGlobalFunc('function TSingleMatrix.Copy: TSingleMatrix; overload', @_LapeSingleMatrix_Copy1);
addGlobalFunc('function TSingleMatrix.Copy(Y1, Y2: Integer): TSingleMatrix; overload', @_LapeSingleMatrix_Copy2);
addGlobalFunc('function TSingleMatrix.Rot90: TSingleMatrix;', @_LapeSingleMatrix_Rot90);
addGlobalFunc('function TSingleMatrix.ArgExtrema(Count: Int32; HiLo: Boolean = True): TPointArray;', @_LapeSingleMatrix_ArgExtrema);

// integer
addGlobalFunc('function TIntegerMatrix.Width: Integer;', @_LapeIntegerMatrix_Width);
Expand Down
10 changes: 5 additions & 5 deletions Source/simba.atpa.pas
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function T2DPointArrayHelper.Sort(Weights: TIntegerArray; LowToHigh: Boolean): T
for I := 0 to High(Result) do
Result[I] := Copy(Self[I]);

specialize Sort<TPointArray>(Result, Weights, LowToHigh);
specialize Sort<TPointArray, Integer>(Result, Weights, LowToHigh);
end;

function T2DPointArrayHelper.Sort(Weights: TDoubleArray; LowToHigh: Boolean): T2DPointArray;
Expand All @@ -88,7 +88,7 @@ function T2DPointArrayHelper.Sort(Weights: TDoubleArray; LowToHigh: Boolean): T2
for I := 0 to High(Result) do
Result[I] := Copy(Self[I]);

specialize Sort<TPointArray>(Result, Weights, LowToHigh);
specialize Sort<TPointArray, Double>(Result, Weights, LowToHigh);
end;

function T2DPointArrayHelper.SortFromSize(Size: Integer): T2DPointArray;
Expand Down Expand Up @@ -131,7 +131,7 @@ function T2DPointArrayHelper.SortFromFirstPoint(From: TPoint): T2DPointArray;
for I := 0 to High(Self) do
Weights[I] := Sqr(From.X - Self[I][0].X) + Sqr(From.Y - Self[I][0].Y);

specialize Sort<TPointArray>(Result, Weights, True);
specialize Sort<TPointArray, Integer>(Result, Weights, True);
end;

function T2DPointArrayHelper.SortFromFirstPointX(From: TPoint): T2DPointArray;
Expand All @@ -145,7 +145,7 @@ function T2DPointArrayHelper.SortFromFirstPointX(From: TPoint): T2DPointArray;
for I := 0 to High(Self) do
Weights[I] := Sqr(From.X - Self[I][0].X);

specialize Sort<TPointArray>(Result, Weights, True);
specialize Sort<TPointArray, Integer>(Result, Weights, True);
end;

function T2DPointArrayHelper.SortFromFirstPointY(From: TPoint): T2DPointArray;
Expand All @@ -159,7 +159,7 @@ function T2DPointArrayHelper.SortFromFirstPointY(From: TPoint): T2DPointArray;
for I := 0 to High(Self) do
Weights[I] := Sqr(From.Y - Self[I][0].Y);

specialize Sort<TPointArray>(Result, Weights, True);
specialize Sort<TPointArray, Integer>(Result, Weights, True);
end;

function T2DPointArrayHelper.SortFrom(From: TPoint): T2DPointArray;
Expand Down
8 changes: 6 additions & 2 deletions Source/simba.overallocatearray.pas
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,12 @@ procedure TSimbaOverAllocateArray.EnsureGrowth(const Len: Integer);
begin
FLength := FLength + Len;
if (FLength < 32) then
FLength := 32;
FLength := FLength * 2;
FLength := 32
else
if (FLength > 256000) then
FLength := FLength * 4
else
FLength := FLength * 2;

SetLength(FArr, FLength);
end;
Expand Down

0 comments on commit d503d00

Please sign in to comment.