Skip to content

Commit 7ebf1c1

Browse files
Prepare Primes example in Pascal
1 parent 7d8f290 commit 7ebf1c1

File tree

4 files changed

+137
-13
lines changed

4 files changed

+137
-13
lines changed

Examples/Primes/LibPrimes_component/Implementations/Pascal/Stub/libprimes_impl.pas

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ interface
2222
libprimes_types,
2323
libprimes_exception,
2424
libprimes_interfaces,
25+
libprimes_impl_factorizationcalculator,
26+
libprimes_impl_sievecalculator,
2527
Classes,
2628
sysutils;
2729

@@ -49,7 +51,7 @@ class procedure TLibPrimesWrapper.GetVersion(out AMajor: Cardinal; out AMinor: C
4951

5052
class function TLibPrimesWrapper.GetLastError(AInstance: TObject; out AErrorMessage: String): Boolean;
5153
begin
52-
raise ELibPrimesException.Create (LIBPRIMES_ERROR_NOTIMPLEMENTED);
54+
result := (AInstance as ILibPrimesBase).GetLastErrorMessage(AErrorMessage);
5355
end;
5456

5557
class procedure TLibPrimesWrapper.AcquireInstance(AInstance: TObject);
@@ -64,12 +66,12 @@ class procedure TLibPrimesWrapper.ReleaseInstance(AInstance: TObject);
6466

6567
class function TLibPrimesWrapper.CreateFactorizationCalculator(): TObject;
6668
begin
67-
raise ELibPrimesException.Create (LIBPRIMES_ERROR_NOTIMPLEMENTED);
69+
result := TLibPrimesFactorizationCalculator.Create();
6870
end;
6971

7072
class function TLibPrimesWrapper.CreateSieveCalculator(): TObject;
7173
begin
72-
raise ELibPrimesException.Create (LIBPRIMES_ERROR_NOTIMPLEMENTED);
74+
result := TLibPrimesSieveCalculator.Create();
7375
end;
7476

7577
class procedure TLibPrimesWrapper.SetJournal(const AFileName: String);

Examples/Primes/LibPrimes_component/Implementations/Pascal/Stub/libprimes_impl_calculator.pas

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,35 @@ TLibPrimesCalculator = class(TLibPrimesBase, ILibPrimesCalculator)
2626
private
2727

2828
protected
29-
29+
FValue : QWord;
30+
FProgressCallback: PLibPrimes_ProgressCallback;
3031
public
3132
function GetValue(): QWord;
3233
procedure SetValue(const AValue: QWord);
33-
procedure Calculate();
34+
procedure Calculate(); virtual;
3435
procedure SetProgressCallback(const AProgressCallback: PLibPrimes_ProgressCallback);
3536
end;
3637

3738
implementation
3839

3940
function TLibPrimesCalculator.GetValue(): QWord;
4041
begin
41-
raise ELibPrimesException.Create (LIBPRIMES_ERROR_NOTIMPLEMENTED);
42+
result := FValue;
4243
end;
4344

4445
procedure TLibPrimesCalculator.SetValue(const AValue: QWord);
4546
begin
46-
raise ELibPrimesException.Create (LIBPRIMES_ERROR_NOTIMPLEMENTED);
47+
FValue := AValue;
4748
end;
4849

4950
procedure TLibPrimesCalculator.Calculate();
5051
begin
51-
raise ELibPrimesException.Create (LIBPRIMES_ERROR_NOTIMPLEMENTED);
52+
raise ELibPrimesException.Create(LIBPRIMES_ERROR_NOTIMPLEMENTED);
5253
end;
5354

5455
procedure TLibPrimesCalculator.SetProgressCallback(const AProgressCallback: PLibPrimes_ProgressCallback);
5556
begin
56-
raise ELibPrimesException.Create (LIBPRIMES_ERROR_NOTIMPLEMENTED);
57+
FProgressCallback:=AProgressCallback
5758
end;
5859

5960
end.

Examples/Primes/LibPrimes_component/Implementations/Pascal/Stub/libprimes_impl_factorizationcalculator.pas

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,78 @@ interface
2424
type
2525
TLibPrimesFactorizationCalculator = class(TLibPrimesCalculator, ILibPrimesFactorizationCalculator)
2626
private
27-
27+
FPrimeFactors : Array Of TLibPrimesPrimeFactor;
2828
protected
2929

3030
public
31+
destructor Destroy(); override;
3132
procedure GetPrimeFactors(const APrimeFactorsCount: QWord; PPrimeFactorsNeededCount: PQWord; APrimeFactors: PLibPrimesPrimeFactor);
33+
procedure Calculate(); override;
3234
end;
3335

3436
implementation
3537

38+
destructor TLibPrimesFactorizationCalculator.Destroy();
39+
begin
40+
SetLength(FPrimeFactors, 0);
41+
inherited Destroy();
42+
end;
43+
3644
procedure TLibPrimesFactorizationCalculator.GetPrimeFactors(const APrimeFactorsCount: QWord; PPrimeFactorsNeededCount: PQWord; APrimeFactors: PLibPrimesPrimeFactor);
45+
var
46+
i : QWord;
47+
begin
48+
if (Length(FPrimeFactors) = 0) then
49+
raise ELibPrimesException.Create(LIBPRIMES_ERROR_NORESULTAVAILABLE);
50+
51+
if (assigned(PPrimeFactorsNeededCount)) then
52+
PPrimeFactorsNeededCount^ := Length(FPrimeFactors);
53+
54+
if (APrimeFactorsCount >= Length(FPrimeFactors)) then
55+
begin
56+
for i:=0 to Length(FPrimeFactors) -1 do begin
57+
APrimeFactors^ := FPrimeFactors[i];
58+
inc(APrimeFactors);
59+
end;
60+
end;
61+
end;
62+
63+
procedure TLibPrimesFactorizationCalculator.Calculate();
64+
var
65+
AValue: QWord;
66+
I: QWord;
67+
APFCount: QWord;
68+
APrimeFactor: TLibPrimesPrimeFactor;
69+
AShouldAbort: Byte;
3770
begin
38-
raise ELibPrimesException.Create (LIBPRIMES_ERROR_NOTIMPLEMENTED);
71+
SetLength(FPrimeFactors, 0);
72+
73+
APFCount := 0;
74+
AValue := FValue;
75+
I := 2;
76+
while I < AValue
77+
do begin
78+
if (assigned(FProgressCallback)) then begin
79+
AShouldAbort := 0;
80+
FProgressCallback(1 - 1.0*AValue / FValue, AShouldAbort);
81+
if (AShouldAbort <> 0) then
82+
raise ELibPrimesException.Create(LIBPRIMES_ERROR_CALCULATIONABORTED);
83+
end;
84+
85+
APrimeFactor.FMultiplicity:=0;
86+
APrimeFactor.FPrime:=I;
87+
while (AValue mod i = 0) do begin
88+
inc(APrimeFactor.FMultiplicity);
89+
AValue := AValue div I;
90+
end;
91+
if (APrimeFactor.FMultiplicity > 0) then begin
92+
inc(APFCount);
93+
SetLength(FPrimeFactors, APFCount);
94+
FPrimeFactors[APFCount-1] := APrimeFactor;
95+
end;
96+
inc(I);
97+
end;
3998
end;
4099

41100
end.
101+

Examples/Primes/LibPrimes_component/Implementations/Pascal/Stub/libprimes_impl_sievecalculator.pas

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,79 @@ interface
2424
type
2525
TLibPrimesSieveCalculator = class(TLibPrimesCalculator, ILibPrimesSieveCalculator)
2626
private
27-
27+
FPrimes: array of QWord;
2828
protected
2929

3030
public
31+
destructor Destroy(); override;
3132
procedure GetPrimes(const APrimesCount: QWord; PPrimesNeededCount: PQWord; APrimes: PQWord);
33+
procedure Calculate(); override;
3234
end;
3335

3436
implementation
3537

38+
destructor TLibPrimesSieveCalculator.Destroy();
39+
begin
40+
SetLength(FPrimes, 0);
41+
inherited Destroy();
42+
end;
43+
3644
procedure TLibPrimesSieveCalculator.GetPrimes(const APrimesCount: QWord; PPrimesNeededCount: PQWord; APrimes: PQWord);
45+
var
46+
i : QWord;
3747
begin
38-
raise ELibPrimesException.Create (LIBPRIMES_ERROR_NOTIMPLEMENTED);
48+
if (Length(FPrimes) = 0) then
49+
raise ELibPrimesException.Create(LIBPRIMES_ERROR_NORESULTAVAILABLE);
50+
51+
if (assigned(PPrimesNeededCount)) then
52+
PPrimesNeededCount^ := Length(FPrimes);
53+
54+
if (APrimesCount >= Length(FPrimes)) then
55+
begin
56+
for i:=0 to Length(FPrimes) -1 do begin
57+
APrimes^ := FPrimes[i];
58+
inc(APrimes);
59+
end;
60+
end;
61+
end;
62+
63+
procedure TLibPrimesSieveCalculator.Calculate();
64+
var
65+
AStrikenOut : array of Boolean;
66+
I, J : QWord;
67+
ASqrtValue : QWord;
68+
ANumPrimes: QWord;
69+
begin
70+
SetLength(FPrimes, 0);
71+
ANumPrimes := 0;
72+
73+
SetLength(AStrikenOut, FValue + 1);
74+
for I := 0 to FValue do begin
75+
AStrikenOut[I] := I < 2;
76+
end;
77+
78+
ASqrtValue := round(sqrt(FValue));
79+
80+
for I := 2 to ASqrtValue do begin
81+
if not AStrikenOut[I] then begin
82+
inc(ANumPrimes);
83+
SetLength(FPrimes, ANumPrimes);
84+
FPrimes[ANumPrimes - 1] := I;
85+
J := I*I;
86+
while (J <= FValue) do begin
87+
AStrikenOut[j] := true;
88+
inc(J, I);
89+
end;
90+
end;
91+
end;
92+
93+
for I:= ASqrtValue to FValue do begin
94+
if not AStrikenOut[i] then begin
95+
inc(ANumPrimes);
96+
SetLength(FPrimes, ANumPrimes);
97+
FPrimes[ANumPrimes - 1] := I;
98+
end;
99+
end;
39100
end;
40101

41102
end.

0 commit comments

Comments
 (0)