| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT LIBRARY COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- ADA.CONTAINERS.GENERIC_SORT -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2011-2025, 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 |
------------------------------------------------------------------------------ |
| 29 |
|
| 30 |
-- This algorithm was adapted from GNAT.Heap_Sort (see g-heasor.ad[sb]) |
| 31 |
|
| 32 |
procedure Ada.Containers.Generic_Sort (First, Last : Index_Type'Base) is |
| 33 |
subtype T is Long_Long_Integer; |
| 34 |
|
| 35 |
function To_Index (J : T) return Index_Type; |
| 36 |
pragma Inline (To_Index); |
| 37 |
|
| 38 |
function Lt (J, K : T) return Boolean; |
| 39 |
pragma Inline (Lt); |
| 40 |
|
| 41 |
procedure Xchg (J, K : T); |
| 42 |
pragma Inline (Xchg); |
| 43 |
|
| 44 |
procedure Sift (S : T); |
| 45 |
|
| 46 |
-------------- |
| 47 |
-- To_Index -- |
| 48 |
-------------- |
| 49 |
|
| 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; |
| 55 |
|
| 56 |
-------- |
| 57 |
-- Lt -- |
| 58 |
-------- |
| 59 |
|
| 60 |
function Lt (J, K : T) return Boolean is |
| 61 |
begin |
| 62 |
return Before (To_Index (J), To_Index (K)); |
| 63 |
end Lt; |
| 64 |
|
| 65 |
---------- |
| 66 |
-- Xchg -- |
| 67 |
---------- |
| 68 |
|
| 69 |
procedure Xchg (J, K : T) is |
| 70 |
begin |
| 71 |
Swap (To_Index (J), To_Index (K)); |
| 72 |
end Xchg; |
| 73 |
|
| 74 |
Max : T := Index_Type'Pos (Last) - Index_Type'Pos (First) + T'(1); |
| 75 |
|
| 76 |
---------- |
| 77 |
-- Sift -- |
| 78 |
---------- |
| 79 |
|
| 80 |
procedure Sift (S : T) is |
| 81 |
C : T := S; |
| 82 |
Son : T; |
| 83 |
Father : T; |
| 84 |
|
| 85 |
begin |
| 86 |
loop |
| 87 |
Son := C + C; |
| 88 |
|
| 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; |
| 96 |
|
| 97 |
Xchg (Son, C); |
| 98 |
C := Son; |
| 99 |
end loop; |
| 100 |
|
| 101 |
while C /= S loop |
| 102 |
Father := C / 2; |
| 103 |
|
| 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; |
| 112 |
|
| 113 |
-- Start of processing for Generic_Sort |
| 114 |
|
| 115 |
begin |
| 116 |
for J in reverse 1 .. Max / 2 loop |
| 117 |
Sift (J); |
| 118 |
end loop; |
| 119 |
|
| 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; |