1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.GENERIC_SORT --
9 -- Copyright (C) 2011-2024, Free Software Foundation, Inc. --
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. --
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. --
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/>. --
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;
41 procedure Xchg
(J
, K
: T
);
44 procedure Sift
(S
: T
);
50 function To_Index
(J
: T
) return Index_Type
is
51 K
: constant T
'Base := Index_Type
'Pos (First
) + J
- T
'(1);
53 return Index_Type'Val (K);
60 function Lt (J, K : T) return Boolean is
62 return Before (To_Index (J), To_Index (K));
69 procedure Xchg (J, K : T) is
71 Swap (To_Index (J), To_Index (K));
74 Max : T := Index_Type'Pos (Last) - Index_Type'Pos (First) + T'(1);
80 procedure Sift
(S
: T
) is
90 if Lt
(Son
, Son
+ 1) then
104 if Lt
(Father
, C
) then
113 -- Start of processing for Generic_Sort
116 for J
in reverse 1 .. Max
/ 2 loop
125 end Ada
.Containers
.Generic_Sort
;