0c62f0fea4b00e76544b6dbd68f3295128fec36e
[gcc.git] / gcc / ada / a-nuflra.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- A D A . N U M E R I C S . F L O A T _ R A N D O M --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 with Interfaces; use Interfaces;
33
34 with System.Random_Numbers; use System.Random_Numbers;
35
36 package body Ada.Numerics.Float_Random is
37
38 -------------------------
39 -- Implementation Note --
40 -------------------------
41
42 -- The design of this spec is a bit awkward, as a result of Ada 95 not
43 -- permitting in-out parameters for function formals (most naturally
44 -- Generator values would be passed this way). In pure Ada 95, the only
45 -- solution would be to add a self-referential component to the generator
46 -- allowing access to the generator object from inside the function. This
47 -- would work because the generator is limited, which prevents any copy.
48
49 -- This is a bit heavy, so what we do is to use Unrestricted_Access to
50 -- get a pointer to the state in the passed Generator. This works because
51 -- Generator is a limited type and will thus always be passed by reference.
52
53 subtype Rep_Generator is System.Random_Numbers.Generator;
54 subtype Rep_State is System.Random_Numbers.State;
55
56 ------------
57 -- Random --
58 ------------
59
60 function Random (Gen : Generator) return Uniformly_Distributed is
61 begin
62 return Random (Gen.Rep);
63 end Random;
64
65 -----------
66 -- Reset --
67 -----------
68
69 -- Version that works from given initiator value
70
71 procedure Reset (Gen : Generator; Initiator : Integer) is
72 G : Rep_Generator renames Gen.Rep'Unrestricted_Access.all;
73 begin
74 Reset (G, Integer_32 (Initiator));
75 end Reset;
76
77 -- Version that works from calendar
78
79 procedure Reset (Gen : Generator) is
80 G : Rep_Generator renames Gen.Rep'Unrestricted_Access.all;
81 begin
82 Reset (G);
83 end Reset;
84
85 -- Version that works from specific saved state
86
87 procedure Reset (Gen : Generator; From_State : State) is
88 G : Rep_Generator renames Gen.Rep'Unrestricted_Access.all;
89 begin
90 Reset (G, From_State);
91 end Reset;
92
93 ----------
94 -- Save --
95 ----------
96
97 procedure Save (Gen : Generator; To_State : out State) is
98 begin
99 Save (Gen.Rep, State (To_State));
100 end Save;
101
102 -----------
103 -- Image --
104 -----------
105
106 function Image (Of_State : State) return String is
107 begin
108 return Image (Rep_State (Of_State));
109 end Image;
110
111 -----------
112 -- Value --
113 -----------
114
115 function Value (Coded_State : String) return State is
116 G : Generator;
117 S : Rep_State;
118 begin
119 Reset (G.Rep, Coded_State);
120 System.Random_Numbers.Save (G.Rep, S);
121 return State (S);
122 end Value;
123
124 end Ada.Numerics.Float_Random;