From ab75120ea0acdc70e366c97ad0dbcff66740cc96 Mon Sep 17 00:00:00 2001 From: arakov Date: Fri, 10 Nov 2023 12:35:17 +0100 Subject: [PATCH] [FIXED]#132 - anagram sample --- doc/api/system-routines.html | 27 +++++++++++++ rebuild_lib60.bat | 5 +++ src60/algorithms/algorithms.prj | 27 +++++++++++++ src60/algorithms/quicksort.l | 61 ++++++++++++++++++++++++++++++ src60/algorithms/sort_extensions.l | 39 +++++++++++++++++++ src60/system/routines/sorting.l | 14 +++++++ tests60/sandbox/sandbox.l | 3 +- 7 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 src60/algorithms/algorithms.prj create mode 100644 src60/algorithms/quicksort.l create mode 100644 src60/algorithms/sort_extensions.l diff --git a/doc/api/system-routines.html b/doc/api/system-routines.html index b17caaf43..283ac3f1d 100644 --- a/doc/api/system-routines.html +++ b/doc/api/system-routines.html @@ -252,6 +252,15 @@

Method Summary

+setAt(IntNumber index, Object value) + + + + + + + + exchange(IntNumber i, IntNumber j) @@ -817,6 +826,15 @@

Method Summary

+setAt(IntNumber index, Object value) + + + + + + + + exchange(IntNumber i, IntNumber j) @@ -1602,6 +1620,15 @@

Method Summary

abstract   +setAt(IntNumber index, Object value) + + + + + + +abstract   + exchange(IntNumber i, IntNumber j) diff --git a/rebuild_lib60.bat b/rebuild_lib60.bat index 9dfe5dc2b..efde7e848 100644 --- a/rebuild_lib60.bat +++ b/rebuild_lib60.bat @@ -72,6 +72,11 @@ bin\elena-cli src60\cellular\cellular.prj if %ERRORLEVEL% EQU -2 GOTO CompilerError @echo on +bin\elena-cli src60\algorithms\algorithms.prj +@echo off +if %ERRORLEVEL% EQU -2 GOTO CompilerError +@echo on + bin\elena-cli src60\forms\forms.prj @echo off if %ERRORLEVEL% EQU -2 GOTO CompilerError diff --git a/src60/algorithms/algorithms.prj b/src60/algorithms/algorithms.prj new file mode 100644 index 000000000..d3426b1cb --- /dev/null +++ b/src60/algorithms/algorithms.prj @@ -0,0 +1,27 @@ + + + + ..\..\lib60 + + + + + ..\..\lib60_64 + + + + algorithms + + + + ELENA Algorithms Library + 6.0.0 + Aleksey Rakov + + + + quicksort.l + sort_extensions.l + + + \ No newline at end of file diff --git a/src60/algorithms/quicksort.l b/src60/algorithms/quicksort.l new file mode 100644 index 000000000..c97286473 --- /dev/null +++ b/src60/algorithms/quicksort.l @@ -0,0 +1,61 @@ +import system'routines; +import system'collections; + +const int MAX_LEVELS = 64; + +public singleton QuickSortAlgorithm +{ + sort(SortingAgent source, int start, int length, Func2 compf) + { + int beg[MAX_LEVELS]; + int end[MAX_LEVELS]; + + beg[0] := start; + end[0] := length; + + int i := 0; + while (i >= 0) { + int L := beg[i]; + int R := end[i]; + if (R > L + 1) { + R--; + + var piv := source[L]; + while (L < R) { + while ((!compf(source[R], piv)) && L < R) + R--; + + if (L < R) { + source[L] := source[R]; + L++; + }; + + while (compf(source[L], piv) && L < R) + L++; + + if (L < R) { + source[R] := source[L]; + R--; + } + }; + source[L] := piv; + if (L - beg[i] > end[i] - R) { + beg[i + 1] := L + 1; + end[i + 1] := end[i]; + end[i] := L; + i++; + } + else { + beg[i + 1] := beg[i]; + end[i + 1] := L; + beg[i] := L + 1; + i++; + }; + } + else + { + i--; + } + } + } +} diff --git a/src60/algorithms/sort_extensions.l b/src60/algorithms/sort_extensions.l new file mode 100644 index 000000000..11520cb55 --- /dev/null +++ b/src60/algorithms/sort_extensions.l @@ -0,0 +1,39 @@ +import system'collections; +import system'routines; + +public extension quickSortOp : Indexable +{ + quickSort(int length, Func2 compf) + { + QuickSortAlgorithm.sort(new IndexerSortingAgent(self), 0, length, compf); + } + + quickSort(int length) + = self.quickSort(length, ifOrdered); +} + +public extension quickSortOp2 : ArrayList +{ + quickSort(Func2 compf) + { + QuickSortAlgorithm.sort(new IndexerSortingAgent(self), 0, self.Length, compf); + } + + quickSort() + { + QuickSortAlgorithm.sort(new IndexerSortingAgent(self), 0, self.Length, ifOrdered); + } +} + +public extension quickSortOp3 +{ + quickSort(Func2 compf) + { + QuickSortAlgorithm.sort(new IndexerSortingAgent(self), 0, self.Length, compf); + } + + quickSort() + { + QuickSortAlgorithm.sort(new IndexerSortingAgent(self), 0, self.Length, ifOrdered); + } +} \ No newline at end of file diff --git a/src60/system/routines/sorting.l b/src60/system/routines/sorting.l index d5f8934c0..4f3c60631 100644 --- a/src60/system/routines/sorting.l +++ b/src60/system/routines/sorting.l @@ -8,6 +8,8 @@ public interface SortingAgent } abstract at(int index); + + abstract setAt(int index, object value); abstract exchange(int i, int j); } @@ -34,6 +36,11 @@ public sealed ArraySortingAgent : SortingAgent at(int index) = a[index]; + setAt(int index, object value) + { + a[index] := value; + } + exchange(int i, int j) { var tmp := a[i]; @@ -71,6 +78,13 @@ public sealed IndexerSortingAgent : SortingAgent ^ *a } + + setAt(int index, object value) + { + a.Index := index; + + a.Value := value + } exchange(int i, int j) { diff --git a/tests60/sandbox/sandbox.l b/tests60/sandbox/sandbox.l index 4db16219d..a79845f97 100644 --- a/tests60/sandbox/sandbox.l +++ b/tests60/sandbox/sandbox.l @@ -5,6 +5,7 @@ import system'collections; import extensions; import extensions'routines; import extensions'text; +import algorithms; extension op { @@ -32,7 +33,7 @@ public program() }; dictionary.Values - .sort:(former,later => former.Item2.Length > later.Item2.Length ) + .quickSort:(former,later => former.Item2.Length > later.Item2.Length ) .top:20 .forEach:(pair){ console.printLine(pair.Item2) };