1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . --
6 -- G E N E R I C _ C O N S T R A I N E D _ A R R A Y _ S O R T --
10 -- Copyright (C) 2004-2006, Free Software Foundation, Inc. --
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. --
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. --
30 -- This unit has originally being developed by Matthew J Heaney. --
31 ------------------------------------------------------------------------------
33 -- This algorithm was adapted from GNAT.Heap_Sort_G (see g-hesorg.ad[sb])
37 procedure Ada
.Containers
.Generic_Constrained_Array_Sort
38 (Container
: in out Array_Type
)
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 procedure Sift
(S
: T
);
47 A
: Array_Type
renames Container
;
53 function To_Index
(J
: T
) return Index_Type
is
54 K
: constant T
'Base := Index_Type
'Pos (A
'First) + J
- T
'(1);
56 return Index_Type'Val (K);
66 procedure Sift (S : T) is
77 Son_Index : Index_Type := To_Index (Son);
81 if A (Son_Index) < A (Index_Type'Succ (Son_Index)) then
83 Son_Index := Index_Type'Succ (Son_Index);
87 A (To_Index (C)) := A (Son_Index); -- Move (Son, C);
95 Father : constant T := C / 2;
96 Father_Elem : Element_Type renames A (To_Index (Father));
99 if Father_Elem < Temp then -- Lt (Father, 0)
100 A (To_Index (C)) := Father_Elem; -- Move (Father, C)
109 A (To_Index (C)) := Temp; -- Move (0, C);
112 -- Start of processing for Generic_Constrained_Array_Sort
115 for J in reverse 1 .. Max / 2 loop
116 Temp := Container (To_Index (J)); -- Move (J, 0);
122 Max_Elem : Element_Type renames A (To_Index (Max));
124 Temp := Max_Elem; -- Move (Max, 0);
125 Max_Elem := A (A'First); -- Move (1, Max);
131 end Ada.Containers.Generic_Constrained_Array_Sort;