1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.GENERIC_CONSTRAINED_ARRAY_SORT --
9 -- Copyright (C) 2004-2009, 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_G (see g-hesorg.ad[sb])
34 procedure Ada
.Containers
.Generic_Constrained_Array_Sort
35 (Container
: in out Array_Type
)
37 type T
is range System
.Min_Int
.. System
.Max_Int
;
39 function To_Index
(J
: T
) return Index_Type
;
40 pragma Inline
(To_Index
);
42 procedure Sift
(S
: T
);
44 A
: Array_Type
renames Container
;
50 function To_Index
(J
: T
) return Index_Type
is
51 K
: constant T
'Base := Index_Type
'Pos (A
'First) + J
- T
'(1);
53 return Index_Type'Val (K);
63 procedure Sift (S : T) is
74 Son_Index : Index_Type := To_Index (Son);
78 if A (Son_Index) < A (Index_Type'Succ (Son_Index)) then
80 Son_Index := Index_Type'Succ (Son_Index);
84 A (To_Index (C)) := A (Son_Index); -- Move (Son, C);
92 Father : constant T := C / 2;
94 if A (To_Index (Father)) < Temp then -- Lt (Father, 0)
95 A (To_Index (C)) := A (To_Index (Father)); -- Move (Father, C)
103 A (To_Index (C)) := Temp; -- Move (0, C);
106 -- Start of processing for Generic_Constrained_Array_Sort
109 for J in reverse 1 .. Max / 2 loop
110 Temp := Container (To_Index (J)); -- Move (J, 0);
115 Temp := A (To_Index (Max)); -- Move (Max, 0);
116 A (To_Index (Max)) := A (A'First); -- Move (1, Max);
121 end Ada.Containers.Generic_Constrained_Array_Sort;