1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.GENERIC_SORT --
9 -- Copyright (C) 2011, 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])
34 procedure Ada
.Containers
.Generic_Sort
(First
, Last
: Index_Type
'Base) is
35 type T
is range System
.Min_Int
.. System
.Max_Int
;
37 function To_Index
(J
: T
) return Index_Type
;
38 pragma Inline
(To_Index
);
40 function Lt
(J
, K
: T
) return Boolean;
43 procedure Xchg
(J
, K
: T
);
46 procedure Sift
(S
: T
);
52 function To_Index
(J
: T
) return Index_Type
is
53 K
: constant T
'Base := Index_Type
'Pos (First
) + J
- T
'(1);
55 return Index_Type'Val (K);
62 function Lt (J, K : T) return Boolean is
64 return Before (To_Index (J), To_Index (K));
71 procedure Xchg (J, K : T) is
73 Swap (To_Index (J), To_Index (K));
76 Max : T := Index_Type'Pos (Last) - Index_Type'Pos (First) + T'(1);
82 procedure Sift
(S
: T
) is
92 if Lt
(Son
, Son
+ 1) then
106 if Lt
(Father
, C
) then
115 -- Start of processing for Generic_Sort
118 for J
in reverse 1 .. Max
/ 2 loop
127 end Ada
.Containers
.Generic_Sort
;