View Issue Details

IDProjectCategoryView StatusLast Update
0031600FPCPatchpublic2017-07-09 12:11
ReporterThaddy de KoningAssigned ToMichael Van Canneyt 
PrioritynormalSeverityminorReproducibilityhave not tried
Status resolvedResolutionfixed 
PlatformAllOSAllOS VersionAll
Product Version3.1.1Product Build35664 
Target Version3.2.0Fixed in Version3.1.1 
Summary0031600: Patch: Generic function RandomFrom
DescriptionThe three RandomFrom versions are useful but there are scenarios that demand a more generic approach.
This patch basically adds
generic function RandomFrom<T>(const AValues:T):T; //math is objfpc mode.
TagsNo tags attached.
Fixed in Revision36706
FPCOldBugId
FPCTarget
Attached Files
  • math.pp.patch (886 bytes)
    Index: rtl/objpas/math.pp
    ===================================================================
    --- rtl/objpas/math.pp	(revision 35664)
    +++ rtl/objpas/math.pp	(working copy)
    @@ -613,7 +613,7 @@
     function RandomFrom(const AValues: array of Double): Double; overload;
     function RandomFrom(const AValues: array of Integer): Integer; overload;
     function RandomFrom(const AValues: array of Int64): Int64; overload;
    -
    +generic function RandomFrom<T>((const AValues: array of T):T;
     { cpu specific stuff }
     type
       TFPURoundingMode = system.TFPURoundingMode;
    @@ -2605,6 +2605,11 @@
       result:=AValues[random(High(AValues)+1)];
     end;
     
    +generic function RandomFrom<T>(const AValues: array of T): T;
    +begin
    +  result:=AValues[random(High(AValues)+1)];
    +end;
    +
     function FutureValue(ARate: Float; NPeriods: Integer;
       APayment, APresentValue: Float; APaymentTime: TPaymentTime): Float;
     var
    
    math.pp.patch (886 bytes)
  • math.pp.corrected.patch (971 bytes)
    Index: rtl/objpas/math.pp
    ===================================================================
    --- rtl/objpas/math.pp	(revision 35666)
    +++ rtl/objpas/math.pp	(working copy)
    @@ -613,7 +613,9 @@
     function RandomFrom(const AValues: array of Double): Double; overload;
     function RandomFrom(const AValues: array of Integer): Integer; overload;
     function RandomFrom(const AValues: array of Int64): Int64; overload;
    -
    +{$if FPC_FULLVERSION >=301010}
    +generic function RandomFrom<T>(const AValues:array of T):T;
    +{$ifend}
     { cpu specific stuff }
     type
       TFPURoundingMode = system.TFPURoundingMode;
    @@ -2604,7 +2606,12 @@
     begin
       result:=AValues[random(High(AValues)+1)];
     end;
    -
    +{$if FPC_FULLVERSION >=301010}
    +generic function RandomFrom<T>(const AValues:array of T):T;
    +begin
    +  result:=AValues[random(High(AValues)+1)];
    +end;
    +{$ifend}
     function FutureValue(ARate: Float; NPeriods: Integer;
       APayment, APresentValue: Float; APaymentTime: TPaymentTime): Float;
     var
    
  • testrandomfrom.pas (294 bytes)
    program testrandomfrom;
    {$mode delphi}
    uses math,sysutils;
    var
      i:integer;
      a:array[0..99] of string;
    begin
      for i :=0 to 99 do
        a[i] := IntToStr(i);
      for i :=1 to 100 do
      begin
        write(RandomFrom<string>(a):4);
        if i mod 10 = 0 then writeln;
      end;
      readln;
    end.
    
    testrandomfrom.pas (294 bytes)

Activities

Thaddy de Koning

2017-03-26 18:18

reporter  

math.pp.patch (886 bytes)
Index: rtl/objpas/math.pp
===================================================================
--- rtl/objpas/math.pp	(revision 35664)
+++ rtl/objpas/math.pp	(working copy)
@@ -613,7 +613,7 @@
 function RandomFrom(const AValues: array of Double): Double; overload;
 function RandomFrom(const AValues: array of Integer): Integer; overload;
 function RandomFrom(const AValues: array of Int64): Int64; overload;
-
+generic function RandomFrom<T>((const AValues: array of T):T;
 { cpu specific stuff }
 type
   TFPURoundingMode = system.TFPURoundingMode;
@@ -2605,6 +2605,11 @@
   result:=AValues[random(High(AValues)+1)];
 end;
 
+generic function RandomFrom<T>(const AValues: array of T): T;
+begin
+  result:=AValues[random(High(AValues)+1)];
+end;
+
 function FutureValue(ARate: Float; NPeriods: Integer;
   APayment, APresentValue: Float; APaymentTime: TPaymentTime): Float;
 var
math.pp.patch (886 bytes)

Thaddy de Koning

2017-03-26 18:23

reporter   ~0099234

Last edited: 2017-03-26 18:24

View 2 revisions

The Patch is ok, but description should of course read
generic function RandomFrom<T>(const AValues:array of T):T; //math is objfpc mode. But that was probably obvious.

Aside: These functions are good candidates to move to sysutils?

Thaddy de Koning

2017-03-26 19:05

reporter   ~0099235

Hm. No good. Needs something active, which I overlooked in my second test serie.

The patch chokes, after my clean tests but I can't see why...
Didn't see that.

Derit Agustin

2017-03-27 01:33

reporter   ~0099245

look good function from any type random from array value

Thaddy de Koning

2017-03-27 11:01

reporter   ~0099247

Last edited: 2017-03-27 12:58

View 4 revisions

Yes but the patch *fails* my tests.
This will work, standalone, in a separate unit in trunk only.
I also know why: the global generic function/procedure feature wasn't backported to 3.0.2 and I wrongly assumed that that was the case.
I also had a dangling OVERRIDEVERSIONCHECK in my test setup: then it works...

Now there is a way to improve on the patch:
- math.pp is needed for the compiler it seems.
- So we have to do a version check in the patch.
- Once the compiler is built, it will rebuild the rtl with itself
- After that it works.

Thaddy de Koning

2017-03-27 12:58

reporter  

math.pp.corrected.patch (971 bytes)
Index: rtl/objpas/math.pp
===================================================================
--- rtl/objpas/math.pp	(revision 35666)
+++ rtl/objpas/math.pp	(working copy)
@@ -613,7 +613,9 @@
 function RandomFrom(const AValues: array of Double): Double; overload;
 function RandomFrom(const AValues: array of Integer): Integer; overload;
 function RandomFrom(const AValues: array of Int64): Int64; overload;
-
+{$if FPC_FULLVERSION >=301010}
+generic function RandomFrom<T>(const AValues:array of T):T;
+{$ifend}
 { cpu specific stuff }
 type
   TFPURoundingMode = system.TFPURoundingMode;
@@ -2604,7 +2606,12 @@
 begin
   result:=AValues[random(High(AValues)+1)];
 end;
-
+{$if FPC_FULLVERSION >=301010}
+generic function RandomFrom<T>(const AValues:array of T):T;
+begin
+  result:=AValues[random(High(AValues)+1)];
+end;
+{$ifend}
 function FutureValue(ARate: Float; NPeriods: Integer;
   APayment, APresentValue: Float; APaymentTime: TPaymentTime): Float;
 var

Thaddy de Koning

2017-03-27 13:00

reporter   ~0099249

Last edited: 2017-03-27 13:15

View 2 revisions

Attached new patch. This one is fully tested with bootstrap 3.0.0 and bootstrap 3.0.2.
Attached demo.

Thaddy de Koning

2017-03-27 13:14

reporter  

testrandomfrom.pas (294 bytes)
program testrandomfrom;
{$mode delphi}
uses math,sysutils;
var
  i:integer;
  a:array[0..99] of string;
begin
  for i :=0 to 99 do
    a[i] := IntToStr(i);
  for i :=1 to 100 do
  begin
    write(RandomFrom<string>(a):4);
    if i mod 10 = 0 then writeln;
  end;
  readln;
end.
testrandomfrom.pas (294 bytes)

Michael Van Canneyt

2017-07-09 12:11

administrator   ~0101632

Added, thanks for the contibution!

Issue History

Date Modified Username Field Change
2017-03-26 18:18 Thaddy de Koning New Issue
2017-03-26 18:18 Thaddy de Koning File Added: math.pp.patch
2017-03-26 18:23 Thaddy de Koning Note Added: 0099234
2017-03-26 18:24 Thaddy de Koning Note Edited: 0099234 View Revisions
2017-03-26 19:05 Thaddy de Koning Note Added: 0099235
2017-03-27 01:33 Derit Agustin Note Added: 0099245
2017-03-27 11:01 Thaddy de Koning Note Added: 0099247
2017-03-27 11:07 Thaddy de Koning Note Edited: 0099247 View Revisions
2017-03-27 11:08 Thaddy de Koning Note Edited: 0099247 View Revisions
2017-03-27 12:58 Thaddy de Koning Note Edited: 0099247 View Revisions
2017-03-27 12:58 Thaddy de Koning File Added: math.pp.corrected.patch
2017-03-27 13:00 Thaddy de Koning Note Added: 0099249
2017-03-27 13:14 Thaddy de Koning File Added: testrandomfrom.pas
2017-03-27 13:15 Thaddy de Koning Note Edited: 0099249 View Revisions
2017-07-09 12:11 Michael Van Canneyt Fixed in Revision => 36706
2017-07-09 12:11 Michael Van Canneyt Note Added: 0101632
2017-07-09 12:11 Michael Van Canneyt Status new => resolved
2017-07-09 12:11 Michael Van Canneyt Fixed in Version => 3.1.1
2017-07-09 12:11 Michael Van Canneyt Resolution open => fixed
2017-07-09 12:11 Michael Van Canneyt Assigned To => Michael Van Canneyt
2017-07-09 12:11 Michael Van Canneyt Target Version => 3.2.0