OSDN Git Service

gcc/ada/
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-cgaaso.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 _ A N O N Y M O U S _ 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 was originally developed by Matthew J Heaney.                  --
31 ------------------------------------------------------------------------------
32
33 --  This algorithm was adapted from GNAT.Heap_Sort (see g-heasor.ad[sb])
34
35 with System;
36
37 procedure Ada.Containers.Generic_Anonymous_Array_Sort
38   (First, Last : Index_Type'Base)
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    function Lt (J, K : T) return Boolean;
46    pragma Inline (Lt);
47
48    procedure Xchg (J, K : T);
49    pragma Inline (Xchg);
50
51    procedure Sift (S : T);
52
53    --------------
54    -- To_Index --
55    --------------
56
57    function To_Index (J : T) return Index_Type is
58       K : constant T'Base := Index_Type'Pos (First) + J - T'(1);
59    begin
60       return Index_Type'Val (K);
61    end To_Index;
62
63    --------
64    -- Lt --
65    --------
66
67    function Lt (J, K : T) return Boolean is
68    begin
69       return Less (To_Index (J), To_Index (K));
70    end Lt;
71
72    ----------
73    -- Xchg --
74    ----------
75
76    procedure Xchg (J, K : T) is
77    begin
78       Swap (To_Index (J), To_Index (K));
79    end Xchg;
80
81    Max : T := Index_Type'Pos (Last) - Index_Type'Pos (First) + T'(1);
82
83    ----------
84    -- Sift --
85    ----------
86
87    procedure Sift (S : T) is
88       C      : T := S;
89       Son    : T;
90       Father : T;
91
92    begin
93       loop
94          Son := C + C;
95
96          if Son < Max then
97             if Lt (Son, Son + 1) then
98                Son := Son + 1;
99             end if;
100          elsif Son > Max then
101             exit;
102          end if;
103
104          Xchg (Son, C);
105          C := Son;
106       end loop;
107
108       while C /= S loop
109          Father := C / 2;
110
111          if Lt (Father, C) then
112             Xchg (Father, C);
113             C := Father;
114          else
115             exit;
116          end if;
117       end loop;
118    end Sift;
119
120 --  Start of processing for Generic_Anonymous_Array_Sort
121
122 begin
123    for J in reverse 1 .. Max / 2 loop
124       Sift (J);
125    end loop;
126
127    while Max > 1 loop
128       Xchg (1, Max);
129       Max := Max - 1;
130       Sift (1);
131    end loop;
132 end Ada.Containers.Generic_Anonymous_Array_Sort;