c++: Fix tree_contains_struct for TRAIT_EXPR
[official-gcc.git] / gcc / ada / libgnat / a-cogeso.adb
blob09a3456d4ff69c02ac676e3f5116ff23bdda2fc3
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- ADA.CONTAINERS.GENERIC_SORT --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2011-2024, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- This unit was originally developed by Matthew J Heaney. --
28 ------------------------------------------------------------------------------
30 -- This algorithm was adapted from GNAT.Heap_Sort (see g-heasor.ad[sb])
32 procedure Ada.Containers.Generic_Sort (First, Last : Index_Type'Base) is
33 subtype T is Long_Long_Integer;
35 function To_Index (J : T) return Index_Type;
36 pragma Inline (To_Index);
38 function Lt (J, K : T) return Boolean;
39 pragma Inline (Lt);
41 procedure Xchg (J, K : T);
42 pragma Inline (Xchg);
44 procedure Sift (S : T);
46 --------------
47 -- To_Index --
48 --------------
50 function To_Index (J : T) return Index_Type is
51 K : constant T'Base := Index_Type'Pos (First) + J - T'(1);
52 begin
53 return Index_Type'Val (K);
54 end To_Index;
56 --------
57 -- Lt --
58 --------
60 function Lt (J, K : T) return Boolean is
61 begin
62 return Before (To_Index (J), To_Index (K));
63 end Lt;
65 ----------
66 -- Xchg --
67 ----------
69 procedure Xchg (J, K : T) is
70 begin
71 Swap (To_Index (J), To_Index (K));
72 end Xchg;
74 Max : T := Index_Type'Pos (Last) - Index_Type'Pos (First) + T'(1);
76 ----------
77 -- Sift --
78 ----------
80 procedure Sift (S : T) is
81 C : T := S;
82 Son : T;
83 Father : T;
85 begin
86 loop
87 Son := C + C;
89 if Son < Max then
90 if Lt (Son, Son + 1) then
91 Son := Son + 1;
92 end if;
93 elsif Son > Max then
94 exit;
95 end if;
97 Xchg (Son, C);
98 C := Son;
99 end loop;
101 while C /= S loop
102 Father := C / 2;
104 if Lt (Father, C) then
105 Xchg (Father, C);
106 C := Father;
107 else
108 exit;
109 end if;
110 end loop;
111 end Sift;
113 -- Start of processing for Generic_Sort
115 begin
116 for J in reverse 1 .. Max / 2 loop
117 Sift (J);
118 end loop;
120 while Max > 1 loop
121 Xchg (1, Max);
122 Max := Max - 1;
123 Sift (1);
124 end loop;
125 end Ada.Containers.Generic_Sort;