OSDN Git Service

* config/vax/vax.h (target_flags, MASK_UNIX_ASM, MASK_VAXC_ALIGNMENT)
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-cgaaso.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT LIBRARY COMPONENTS                          --
4 --                                                                          --
5 --               ADA.CONTAINERS.GENERIC_ANONYMOUS_ARRAY_SORT                --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --             Copyright (C) 2004 Free Software Foundation, Inc.            --
10 --                                                                          --
11 -- This specification is derived from the Ada Reference Manual for use with --
12 -- GNAT. The copyright notice above, and the license provisions that follow --
13 -- apply solely to the  contents of the part following the private keyword. --
14 --                                                                          --
15 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
16 -- terms of the  GNU General Public License as published  by the Free Soft- --
17 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
18 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
19 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
20 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
21 -- for  more details.  You should have  received  a copy of the GNU General --
22 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
23 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
24 -- MA 02111-1307, USA.                                                      --
25 --                                                                          --
26 -- As a special exception,  if other files  instantiate  generics from this --
27 -- unit, or you link  this unit with other files  to produce an executable, --
28 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
29 -- covered  by the  GNU  General  Public  License.  This exception does not --
30 -- however invalidate  any other reasons why  the executable file  might be --
31 -- covered by the  GNU Public License.                                      --
32 --                                                                          --
33 -- This unit was originally developed by Matthew J Heaney.                  --
34 ------------------------------------------------------------------------------
35
36 procedure Ada.Containers.Generic_Anonymous_Array_Sort
37   (First, Last : Index_Type'Base)
38 is
39    Pivot, Lo, Mid, Hi : Index_Type;
40
41 begin
42    if Last <= First then
43       return;
44    end if;
45
46    Lo := First;
47    Hi := Last;
48
49    if Last = Index_Type'Succ (First) then
50       if not Less (Lo, Hi) then
51          Swap (Lo, Hi);
52       end if;
53
54       return;
55    end if;
56
57    Mid := Index_Type'Val
58      (Index_Type'Pos (Lo) +
59       (Index_Type'Pos (Hi) - Index_Type'Pos (Lo)) / 2);
60
61    --  We need to figure out which case we have:
62    --  x < y < z
63    --  x < z < y
64    --  z < x < y
65    --  y < x < z
66    --  y < z < x
67    --  z < y < x
68
69    if Less (Lo, Mid) then
70       if Less (Lo, Hi) then
71          if Less (Mid, Hi) then
72             Swap (Lo, Mid);
73
74          else
75             Swap (Lo, Hi);
76
77          end if;
78
79       else
80          null;  --  lo is median
81       end if;
82
83    elsif Less (Lo, Hi) then
84       null; --  lo is median
85
86    elsif Less (Mid, Hi) then
87       Swap (Lo, Hi);
88
89    else
90       Swap (Lo, Mid);
91    end if;
92
93    Pivot := Lo;
94    Outer : loop
95       loop
96          exit Outer when not (Pivot < Hi);
97
98          if Less (Hi, Pivot) then
99             Swap (Hi, Pivot);
100             Pivot := Hi;
101             Lo := Index_Type'Succ (Lo);
102             exit;
103          else
104             Hi := Index_Type'Pred (Hi);
105          end if;
106       end loop;
107
108       loop
109          exit Outer when not (Lo < Pivot);
110
111          if Less (Lo, Pivot) then
112             Lo := Index_Type'Succ (Lo);
113          else
114             Swap (Lo, Pivot);
115             Pivot := Lo;
116             Hi := Index_Type'Pred (Hi);
117             exit;
118          end if;
119       end loop;
120    end loop Outer;
121
122    Generic_Anonymous_Array_Sort (First, Index_Type'Pred (Pivot));
123    Generic_Anonymous_Array_Sort (Index_Type'Succ (Pivot), Last);
124
125 end Ada.Containers.Generic_Anonymous_Array_Sort;