OSDN Git Service

2007-04-20 Arnaud Charlet <charlet@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-cgcaso.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT LIBRARY COMPONENTS                          --
4 --                                                                          --
5 --                      A D A . C O N T A I N E R S .                       --
6 --       G E N E R I C _ C O N S T R A I N E D _ A R R A Y _ S O R T        --
7 --                                                                          --
8 --                                 B o d y                                  --
9 --                                                                          --
10 --          Copyright (C) 2004-2006, Free Software Foundation, Inc.         --
11 --                                                                          --
12 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
13 -- terms of the  GNU General Public License as published  by the Free Soft- --
14 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
15 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
18 -- for  more details.  You should have  received  a copy of the GNU General --
19 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
20 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
21 -- Boston, MA 02110-1301, USA.                                              --
22 --                                                                          --
23 -- As a special exception,  if other files  instantiate  generics from this --
24 -- unit, or you link  this unit with other files  to produce an executable, --
25 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
26 -- covered  by the  GNU  General  Public  License.  This exception does not --
27 -- however invalidate  any other reasons why  the executable file  might be --
28 -- covered by the  GNU Public License.                                      --
29 --                                                                          --
30 -- This unit has originally being developed by Matthew J Heaney.            --
31 ------------------------------------------------------------------------------
32
33 --  This algorithm was adapted from GNAT.Heap_Sort_G (see g-hesorg.ad[sb])
34
35 with System;
36
37 procedure Ada.Containers.Generic_Constrained_Array_Sort
38   (Container : in out Array_Type)
39 is
40    type T is range System.Min_Int .. System.Max_Int;
41
42    function To_Index (J : T) return Index_Type;
43    pragma Inline (To_Index);
44
45    procedure Sift (S : T);
46
47    A : Array_Type renames Container;
48
49    --------------
50    -- To_Index --
51    --------------
52
53    function To_Index (J : T) return Index_Type is
54       K : constant T'Base := Index_Type'Pos (A'First) + J - T'(1);
55    begin
56       return Index_Type'Val (K);
57    end To_Index;
58
59    Max  : T := A'Length;
60    Temp : Element_Type;
61
62    ----------
63    -- Sift --
64    ----------
65
66    procedure Sift (S : T) is
67       C   : T := S;
68       Son : T;
69
70    begin
71       loop
72          Son := 2 * C;
73
74          exit when Son > Max;
75
76          declare
77             Son_Index : Index_Type := To_Index (Son);
78
79          begin
80             if Son < Max then
81                if A (Son_Index) < A (Index_Type'Succ (Son_Index)) then
82                   Son := Son + 1;
83                   Son_Index := Index_Type'Succ (Son_Index);
84                end if;
85             end if;
86
87             A (To_Index (C)) := A (Son_Index);  -- Move (Son, C);
88          end;
89
90          C := Son;
91       end loop;
92
93       while C /= S loop
94          declare
95             Father      : constant T := C / 2;
96             Father_Elem : Element_Type renames A (To_Index (Father));
97
98          begin
99             if Father_Elem < Temp then           -- Lt (Father, 0)
100                A (To_Index (C)) := Father_Elem;  -- Move (Father, C)
101                C := Father;
102
103             else
104                exit;
105             end if;
106          end;
107       end loop;
108
109       A (To_Index (C)) := Temp; -- Move (0, C);
110    end Sift;
111
112 --  Start of processing for Generic_Constrained_Array_Sort
113
114 begin
115    for J in reverse 1 .. Max / 2 loop
116       Temp := Container (To_Index (J)); --  Move (J, 0);
117       Sift (J);
118    end loop;
119
120    while Max > 1 loop
121       declare
122          Max_Elem : Element_Type renames A (To_Index (Max));
123       begin
124          Temp := Max_Elem;         --  Move (Max, 0);
125          Max_Elem := A (A'First);  --  Move (1, Max);
126       end;
127
128       Max := Max - 1;
129       Sift (1);
130    end loop;
131 end Ada.Containers.Generic_Constrained_Array_Sort;