adagsl_335d13f0/examples/permutation/src/permutation.adb

 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
with Ada.Text_IO;            use Ada.Text_IO;
with Ada.Text_IO.C_Streams;
with Ada.Long_Float_Text_IO; use Ada.Long_Float_Text_IO;
with Ada.Integer_Text_IO;    use Ada.Integer_Text_IO;
with Interfaces.C;           use Interfaces.C;
with Interfaces.C.Strings;   use Interfaces.C.Strings;
with Ada.Strings.Unbounded;  use Ada.Strings.Unbounded;
with Ada.Command_Line;       use Ada.Command_Line;

with GNAT.Source_Info; use GNAT.Source_Info;

with gsl;
with gsl.rng;
with gsl.randist;
with gsl.permutation;

procedure permutation is

   letters : Unbounded_String := To_Unbounded_String ("0123456789abcdef");
   numbers : Unbounded_String := To_Unbounded_String("0123") ;

   Status  : int;
   procedure show (p : access gsl.permutation.gsl_permutation) is

   begin
      Status :=
        gsl.permutation.fprintf
          (Ada.Text_Io.C_Streams.C_Stream (Ada.Text_Io.Standard_Output), p,
           New_String (" %u "));
      New_Line;
   end show;

   procedure Test1 is
      perm : access gsl.permutation
        .gsl_permutation := -- gsl.permutation.alloc(20) ;
        gsl.permutation.alloc
          (size_t (Ada.Strings.Unbounded.Length (letters)));
      perminv : access gsl.permutation.gsl_permutation :=
        gsl.permutation.alloc (size_t (Length (letters)));
      rng : access gsl.rng.gsl_rng := gsl.rng.alloc (gsl.rng.default);
   begin
      Put_Line (GNAT.Source_Info.Enclosing_Entity);
      gsl.permutation.init (perm);
      show (perm);
      gsl.randist.shuffle
        (rng, perm.data, size_t (Length (letters) + 1), size_t (8));
      Put_Line ("Shuffled");
      Status := gsl.permutation.show (perm);

      declare
         s : String := To_String (letters);
         n : size_t;
      begin
         for sp in s'Range loop
            n := gsl.permutation.get (perm, size_t (sp - 1));
            Put (s (1 + Integer (n)));
         end loop;
         New_Line;
      end;

      Status := gsl.permutation.inverse (perminv, perm);
      Put_Line ("Inverted");
      Status := gsl.permutation.show (perminv);

   end Test1;
   procedure Test2 is
      perm : access gsl.permutation.gsl_permutation
        := gsl.permutation.alloc
          (size_t (Ada.Strings.Unbounded.Length (numbers)));
      Status : Int := gsl.GSL_SUCCESS ;
   begin
      gsl.permutation.init (perm);
      loop
         Status := gsl.permutation.show(perm);
         Status := gsl.permutation.next(perm);
         exit when Status /= gsl.GSL_SUCCESS ;  
      end loop ;
   end Test2;
begin
   if Argument_Count > 0 then
      letters := To_Unbounded_String (Argument (1));
   end if;
   Test1;
   Test2;
end permutation;