Skip to content

Commit

Permalink
[FIXED]#132 - anagram sample
Browse files Browse the repository at this point in the history
  • Loading branch information
arakov committed Nov 10, 2023
1 parent 08ad647 commit ab75120
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 1 deletion.
27 changes: 27 additions & 0 deletions doc/api/system-routines.html
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ <H3>Method Summary</H3>
<CODE>
</CODE></TD>
<TD CLASS="colLast">
<CODE>setAt(<SPAN CLASS="memberNameLink"><A HREF="system.html#IntNumber">IntNumber</A></SPAN> index, <SPAN CLASS="memberNameLink"><A HREF="system.html#Object">Object</A></SPAN> value)
</CODE>
</TD>
</TR>
<TR CLASS="altColor">
<TD CLASS="colFirst">
<CODE>
</CODE></TD>
<TD CLASS="colLast">
<CODE>exchange(<SPAN CLASS="memberNameLink"><A HREF="system.html#IntNumber">IntNumber</A></SPAN> i, <SPAN CLASS="memberNameLink"><A HREF="system.html#IntNumber">IntNumber</A></SPAN> j)
</CODE>
</TD>
Expand Down Expand Up @@ -817,6 +826,15 @@ <H3>Method Summary</H3>
<CODE>
</CODE></TD>
<TD CLASS="colLast">
<CODE>setAt(<SPAN CLASS="memberNameLink"><A HREF="system.html#IntNumber">IntNumber</A></SPAN> index, <SPAN CLASS="memberNameLink"><A HREF="system.html#Object">Object</A></SPAN> value)
</CODE>
</TD>
</TR>
<TR CLASS="altColor">
<TD CLASS="colFirst">
<CODE>
</CODE></TD>
<TD CLASS="colLast">
<CODE>exchange(<SPAN CLASS="memberNameLink"><A HREF="system.html#IntNumber">IntNumber</A></SPAN> i, <SPAN CLASS="memberNameLink"><A HREF="system.html#IntNumber">IntNumber</A></SPAN> j)
</CODE>
</TD>
Expand Down Expand Up @@ -1602,6 +1620,15 @@ <H3>Method Summary</H3>
<CODE>
<i>abstract </i>&nbsp;</CODE></TD>
<TD CLASS="colLast">
<CODE>setAt(<SPAN CLASS="memberNameLink"><A HREF="system.html#IntNumber">IntNumber</A></SPAN> index, <SPAN CLASS="memberNameLink"><A HREF="system.html#Object">Object</A></SPAN> value)
</CODE>
</TD>
</TR>
<TR CLASS="altColor">
<TD CLASS="colFirst">
<CODE>
<i>abstract </i>&nbsp;</CODE></TD>
<TD CLASS="colLast">
<CODE>exchange(<SPAN CLASS="memberNameLink"><A HREF="system.html#IntNumber">IntNumber</A></SPAN> i, <SPAN CLASS="memberNameLink"><A HREF="system.html#IntNumber">IntNumber</A></SPAN> j)
</CODE>
</TD>
Expand Down
5 changes: 5 additions & 0 deletions rebuild_lib60.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions src60/algorithms/algorithms.prj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<configuration>
<platform key="Win_x86">
<project>
<output>..\..\lib60</output>
</project>
</platform>
<platform key="Win_x64">
<project>
<output>..\..\lib60_64</output>
</project>
</platform>
<project>
<namespace>algorithms</namespace>
<template>lib60</template>
</project>
<manifest>
<name>ELENA Algorithms Library</name>
<version>6.0.0</version>
<author>Aleksey Rakov</author>
</manifest>
<files>
<module>
<include>quicksort.l</include>
<include>sort_extensions.l</include>
</module>
</files>
</configuration>
61 changes: 61 additions & 0 deletions src60/algorithms/quicksort.l
Original file line number Diff line number Diff line change
@@ -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--;
}
}
}
}
39 changes: 39 additions & 0 deletions src60/algorithms/sort_extensions.l
Original file line number Diff line number Diff line change
@@ -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);
}
}
14 changes: 14 additions & 0 deletions src60/system/routines/sorting.l
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public interface SortingAgent
}

abstract at(int index);

abstract setAt(int index, object value);

abstract exchange(int i, int j);
}
Expand All @@ -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];
Expand Down Expand Up @@ -71,6 +78,13 @@ public sealed IndexerSortingAgent : SortingAgent

^ *a

This comment has been minimized.

Copy link
@bencz

bencz Nov 14, 2023

Member

Is this like returning a value from a pointer?

This comment has been minimized.

Copy link
@arakov

arakov Nov 14, 2023

Author Member

Yes, to some extend. As you may know there are a relation between an operator (strong) and a message (weak).

E.g.:

 a + b

is similar to

 a.add(b) 

The difference is that operator can be implemented natively - in this case if a and b integers, the compiler will directly sum them. Otherwise it will send a message.

So the operator * - is similar to the property Value.

 *a

is the same as

a.Value

I'm planning to add a native support for the operator for some classes like Reference , Indexer and so on

So for the UnsafePointer it will be like C operator with a pointer.

}

setAt(int index, object value)
{
a.Index := index;

a.Value := value
}

exchange(int i, int j)
{
Expand Down
3 changes: 2 additions & 1 deletion tests60/sandbox/sandbox.l
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import system'collections;
import extensions;
import extensions'routines;
import extensions'text;
import algorithms;

extension op
{
Expand Down Expand Up @@ -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) };

Expand Down

0 comments on commit ab75120

Please sign in to comment.