| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT LIBRARY COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- A D A . C O N T A I N E R S . S T A B L E _ S O R T I N G -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1995-2025, AdaCore -- |
| 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 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 28 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 29 |
-- -- |
| 30 |
------------------------------------------------------------------------------ |
| 31 |
|
| 32 |
package body Ada.Containers.Stable_Sorting is |
| 33 |
package body List_Descriptors is |
| 34 |
procedure Doubly_Linked_List_Sort (List : List_Descriptor) is |
| 35 |
|
| 36 |
Empty : constant List_Descriptor := (Nil, Nil, 0); |
| 37 |
|
| 38 |
function Merge_Sort (Arg : List_Descriptor) return List_Descriptor; |
| 39 |
-- Sort list of given length using MergeSort; length must be >= 2. |
| 40 |
-- As required by RM, the sort is stable. |
| 41 |
|
| 42 |
---------------- |
| 43 |
-- Merge_Sort -- |
| 44 |
---------------- |
| 45 |
|
| 46 |
function Merge_Sort (Arg : List_Descriptor) return List_Descriptor |
| 47 |
is |
| 48 |
procedure Split_List |
| 49 |
(Unsplit : List_Descriptor; Part1, Part2 : out List_Descriptor); |
| 50 |
-- Split list into two parts for divide-and-conquer. |
| 51 |
-- Unsplit.Length must be >= 2. |
| 52 |
|
| 53 |
function Merge_Parts |
| 54 |
(Part1, Part2 : List_Descriptor) return List_Descriptor; |
| 55 |
-- Merge two sorted lists, preserving sorted property. |
| 56 |
|
| 57 |
---------------- |
| 58 |
-- Split_List -- |
| 59 |
---------------- |
| 60 |
|
| 61 |
procedure Split_List |
| 62 |
(Unsplit : List_Descriptor; Part1, Part2 : out List_Descriptor) |
| 63 |
is |
| 64 |
Rover : Node_Ref := Unsplit.First; |
| 65 |
Bump_Count : constant Count_Type := (Unsplit.Length - 1) / 2; |
| 66 |
begin |
| 67 |
for Iter in 1 .. Bump_Count loop |
| 68 |
Rover := Next (Rover); |
| 69 |
end loop; |
| 70 |
|
| 71 |
Part1 := (First => Unsplit.First, |
| 72 |
Last => Rover, |
| 73 |
Length => Bump_Count + 1); |
| 74 |
|
| 75 |
Part2 := (First => Next (Rover), |
| 76 |
Last => Unsplit.Last, |
| 77 |
Length => Unsplit.Length - Part1.Length); |
| 78 |
|
| 79 |
-- Detach |
| 80 |
Set_Next (Part1.Last, Nil); |
| 81 |
Set_Prev (Part2.First, Nil); |
| 82 |
end Split_List; |
| 83 |
|
| 84 |
----------------- |
| 85 |
-- Merge_Parts -- |
| 86 |
----------------- |
| 87 |
|
| 88 |
function Merge_Parts |
| 89 |
(Part1, Part2 : List_Descriptor) return List_Descriptor |
| 90 |
is |
| 91 |
procedure Detach_First (Source : in out List_Descriptor; |
| 92 |
Detached : out Node_Ref); |
| 93 |
-- Detach the first element from a non-empty list and |
| 94 |
-- return the detached node via the Detached parameter. |
| 95 |
|
| 96 |
------------------ |
| 97 |
-- Detach_First -- |
| 98 |
------------------ |
| 99 |
|
| 100 |
procedure Detach_First (Source : in out List_Descriptor; |
| 101 |
Detached : out Node_Ref) is |
| 102 |
begin |
| 103 |
Detached := Source.First; |
| 104 |
|
| 105 |
if Source.Length = 1 then |
| 106 |
Source := Empty; |
| 107 |
else |
| 108 |
Source := (Next (Source.First), |
| 109 |
Source.Last, |
| 110 |
Source.Length - 1); |
| 111 |
|
| 112 |
Set_Prev (Next (Detached), Nil); |
| 113 |
Set_Next (Detached, Nil); |
| 114 |
end if; |
| 115 |
end Detach_First; |
| 116 |
|
| 117 |
P1 : List_Descriptor := Part1; |
| 118 |
P2 : List_Descriptor := Part2; |
| 119 |
Merged : List_Descriptor := Empty; |
| 120 |
|
| 121 |
Take_From_P2 : Boolean; |
| 122 |
Detached : Node_Ref; |
| 123 |
|
| 124 |
-- Start of processing for Merge_Parts |
| 125 |
|
| 126 |
begin |
| 127 |
while P1.Length /= 0 or P2.Length /= 0 loop |
| 128 |
if P1.Length = 0 then |
| 129 |
Take_From_P2 := True; |
| 130 |
elsif P2.Length = 0 then |
| 131 |
Take_From_P2 := False; |
| 132 |
else |
| 133 |
-- If the compared elements are equal then Take_From_P2 |
| 134 |
-- must be False in order to ensure stability. |
| 135 |
|
| 136 |
Take_From_P2 := P2.First < P1.First; |
| 137 |
end if; |
| 138 |
|
| 139 |
if Take_From_P2 then |
| 140 |
Detach_First (P2, Detached); |
| 141 |
else |
| 142 |
Detach_First (P1, Detached); |
| 143 |
end if; |
| 144 |
|
| 145 |
if Merged.Length = 0 then |
| 146 |
Merged := (First | Last => Detached, Length => 1); |
| 147 |
else |
| 148 |
Set_Prev (Detached, Merged.Last); |
| 149 |
Set_Next (Merged.Last, Detached); |
| 150 |
Merged.Last := Detached; |
| 151 |
Merged.Length := Merged.Length + 1; |
| 152 |
end if; |
| 153 |
end loop; |
| 154 |
return Merged; |
| 155 |
end Merge_Parts; |
| 156 |
|
| 157 |
-- Start of processing for Merge_Sort |
| 158 |
|
| 159 |
begin |
| 160 |
if Positive (Arg.Length) < 2 then |
| 161 |
-- already sorted |
| 162 |
return Arg; |
| 163 |
end if; |
| 164 |
|
| 165 |
declare |
| 166 |
Part1, Part2 : List_Descriptor; |
| 167 |
begin |
| 168 |
Split_List (Unsplit => Arg, Part1 => Part1, Part2 => Part2); |
| 169 |
|
| 170 |
Part1 := Merge_Sort (Part1); |
| 171 |
Part2 := Merge_Sort (Part2); |
| 172 |
|
| 173 |
return Merge_Parts (Part1, Part2); |
| 174 |
end; |
| 175 |
end Merge_Sort; |
| 176 |
|
| 177 |
-- Start of processing for Sort |
| 178 |
|
| 179 |
begin |
| 180 |
if List.Length > 1 then |
| 181 |
-- If a call to the formal "<" op references the container |
| 182 |
-- during sorting, seeing an empty container seems preferable |
| 183 |
-- to seeing an internally inconsistent container. |
| 184 |
-- |
| 185 |
Update_Container (Empty); |
| 186 |
|
| 187 |
Update_Container (Merge_Sort (List)); |
| 188 |
end if; |
| 189 |
end Doubly_Linked_List_Sort; |
| 190 |
end List_Descriptors; |
| 191 |
end Ada.Containers.Stable_Sorting; |