OSDN Git Service

2010-06-22 Paul Hilfinger <hilfinger@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-nudira.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUN-TIME COMPONENTS                         --
4 --                                                                          --
5 --         A D A . N U M E R I C S . D I S C R E T E _ 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 System.Random_Numbers; use System.Random_Numbers;
33
34 package body Ada.Numerics.Discrete_Random is
35
36    -------------------------
37    -- Implementation Note --
38    -------------------------
39
40    --  The design of this spec is very awkward, as a result of Ada 95 not
41    --  permitting in-out parameters for function formals (most naturally
42    --  Generator values would be passed this way). In pure Ada 95, the only
43    --  solution is to use the heap and pointers, and, to avoid memory leaks,
44    --  controlled types.
45
46    --  This is awfully heavy, so what we do is to use Unrestricted_Access to
47    --  get a pointer to the state in the passed Generator. This works because
48    --  Generator is a limited type and will thus always be passed by reference.
49
50    subtype Rep_Generator is System.Random_Numbers.Generator;
51    subtype Rep_State is System.Random_Numbers.State;
52
53    function Rep_Random is
54       new Random_Discrete (Result_Subtype, Result_Subtype'First);
55
56    function Random (Gen : Generator) return Result_Subtype is
57    begin
58       return Rep_Random (Gen.Rep);
59    end Random;
60
61    procedure Reset (Gen       : Generator;
62                     Initiator : Integer) is
63       G : Rep_Generator renames Gen.Rep'Unrestricted_Access.all;
64    begin
65       Reset (G, Initiator);
66    end Reset;
67
68    procedure Reset (Gen       : Generator) is
69       G : Rep_Generator renames Gen.Rep'Unrestricted_Access.all;
70    begin
71       Reset (G);
72    end Reset;
73
74    procedure Save  (Gen        : Generator;
75                     To_State   : out State) is
76    begin
77       Save (Gen.Rep, State (To_State));
78    end Save;
79
80    procedure Reset (Gen        : Generator;
81                     From_State : State) is
82       G : Rep_Generator renames Gen.Rep'Unrestricted_Access.all;
83    begin
84       Reset (G, From_State);
85    end Reset;
86
87    function Image (Of_State    : State)  return String is
88    begin
89       return Image (Rep_State (Of_State));
90    end Image;
91
92    function Value (Coded_State : String) return State is
93       G : Generator;
94       S : Rep_State;
95    begin
96       Reset (G.Rep, Coded_State);
97       System.Random_Numbers.Save (G.Rep, S);
98       return State (S);
99    end Value;
100
101 end Ada.Numerics.Discrete_Random;