* gcc.dg/pr26570.c: Clean up coverage files.
[official-gcc.git] / gcc / ada / a-cgaaso.adb
blob5c2e22d3b0d91b4f16fb554335c90a200fce9eaa
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 ------------------------------------------------------------------------------
33 -- This algorithm was adapted from GNAT.Heap_Sort (see g-heasor.ad[sb])
35 with System;
37 procedure Ada.Containers.Generic_Anonymous_Array_Sort
38 (First, Last : Index_Type'Base)
40 type T is range System.Min_Int .. System.Max_Int;
42 function To_Index (J : T) return Index_Type;
43 pragma Inline (To_Index);
45 function Lt (J, K : T) return Boolean;
46 pragma Inline (Lt);
48 procedure Xchg (J, K : T);
49 pragma Inline (Xchg);
51 procedure Sift (S : T);
53 --------------
54 -- To_Index --
55 --------------
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;
63 --------
64 -- Lt --
65 --------
67 function Lt (J, K : T) return Boolean is
68 begin
69 return Less (To_Index (J), To_Index (K));
70 end Lt;
72 ----------
73 -- Xchg --
74 ----------
76 procedure Xchg (J, K : T) is
77 begin
78 Swap (To_Index (J), To_Index (K));
79 end Xchg;
81 Max : T := Index_Type'Pos (Last) - Index_Type'Pos (First) + T'(1);
83 ----------
84 -- Sift --
85 ----------
87 procedure Sift (S : T) is
88 C : T := S;
89 Son : T;
90 Father : T;
92 begin
93 loop
94 Son := C + C;
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;
104 Xchg (Son, C);
105 C := Son;
106 end loop;
108 while C /= S loop
109 Father := C / 2;
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;
120 -- Start of processing for Generic_Anonymous_Array_Sort
122 begin
123 for J in reverse 1 .. Max / 2 loop
124 Sift (J);
125 end loop;
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;