wl_lib_0.1.3_1c94dc7c/src/wl-random-weighted_random_choices.ads

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
private with Ada.Containers.Vectors;

generic
   type Element_Type is private;
   with function "=" (Left, Right : Element_Type) return Boolean is <>;
package WL.Random.Weighted_Random_Choices is

   type Weighted_Choice_Set is tagged private;

   function Is_Empty (Set : Weighted_Choice_Set) return Boolean;

   procedure Insert
     (Set    : in out Weighted_Choice_Set;
      Item   : Element_Type;
      Score  : Natural)
     with Post => Score = 0 or else not Set.Is_Empty;

   procedure Delete
     (Set    : in out Weighted_Choice_Set;
      Item   : Element_Type);

   function Choose
     (Set : Weighted_Choice_Set)
      return Element_Type
     with Pre => not Set.Is_Empty;

private

   type Weighted_Element is
      record
         Element : Element_Type;
         Score   : Natural;
      end record;

   package Vectors is new Ada.Containers.Vectors (Positive, Weighted_Element);

   type Weighted_Choice_Set is tagged
      record
         Vector      : Vectors.Vector;
         Total_Score : Natural := 0;
      end record;

   function Is_Empty (Set : Weighted_Choice_Set) return Boolean
   is (Set.Vector.Is_Empty);

end WL.Random.Weighted_Random_Choices;