annotate gcc/ada/libgnat/a-crbtgk.adb @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT LIBRARY COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- ADA.CONTAINERS.RED_BLACK_TREES.GENERIC_KEYS --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- B o d y --
kono
parents:
diff changeset
8 -- --
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
9 -- Copyright (C) 2004-2018, Free Software Foundation, Inc. --
111
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
17 -- --
kono
parents:
diff changeset
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
19 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
20 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
21 -- --
kono
parents:
diff changeset
22 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
23 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
25 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
26 -- --
kono
parents:
diff changeset
27 -- This unit was originally developed by Matthew J Heaney. --
kono
parents:
diff changeset
28 ------------------------------------------------------------------------------
kono
parents:
diff changeset
29
kono
parents:
diff changeset
30 package body Ada.Containers.Red_Black_Trees.Generic_Keys is
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 pragma Warnings (Off, "variable ""Busy*"" is not referenced");
kono
parents:
diff changeset
33 pragma Warnings (Off, "variable ""Lock*"" is not referenced");
kono
parents:
diff changeset
34 -- See comment in Ada.Containers.Helpers
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 package Ops renames Tree_Operations;
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 -------------
kono
parents:
diff changeset
39 -- Ceiling --
kono
parents:
diff changeset
40 -------------
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 -- AKA Lower_Bound
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 function Ceiling (Tree : Tree_Type; Key : Key_Type) return Node_Access is
kono
parents:
diff changeset
45 -- Per AI05-0022, the container implementation is required to detect
kono
parents:
diff changeset
46 -- element tampering by a generic actual subprogram.
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 Lock : With_Lock (Tree.TC'Unrestricted_Access);
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 Y : Node_Access;
kono
parents:
diff changeset
51 X : Node_Access;
kono
parents:
diff changeset
52
kono
parents:
diff changeset
53 begin
kono
parents:
diff changeset
54 -- If the container is empty, return a result immediately, so that we do
kono
parents:
diff changeset
55 -- not manipulate the tamper bits unnecessarily.
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 if Tree.Root = null then
kono
parents:
diff changeset
58 return null;
kono
parents:
diff changeset
59 end if;
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 X := Tree.Root;
kono
parents:
diff changeset
62 while X /= null loop
kono
parents:
diff changeset
63 if Is_Greater_Key_Node (Key, X) then
kono
parents:
diff changeset
64 X := Ops.Right (X);
kono
parents:
diff changeset
65 else
kono
parents:
diff changeset
66 Y := X;
kono
parents:
diff changeset
67 X := Ops.Left (X);
kono
parents:
diff changeset
68 end if;
kono
parents:
diff changeset
69 end loop;
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 return Y;
kono
parents:
diff changeset
72 end Ceiling;
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 ----------
kono
parents:
diff changeset
75 -- Find --
kono
parents:
diff changeset
76 ----------
kono
parents:
diff changeset
77
kono
parents:
diff changeset
78 function Find (Tree : Tree_Type; Key : Key_Type) return Node_Access is
kono
parents:
diff changeset
79 -- Per AI05-0022, the container implementation is required to detect
kono
parents:
diff changeset
80 -- element tampering by a generic actual subprogram.
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 Lock : With_Lock (Tree.TC'Unrestricted_Access);
kono
parents:
diff changeset
83
kono
parents:
diff changeset
84 Y : Node_Access;
kono
parents:
diff changeset
85 X : Node_Access;
kono
parents:
diff changeset
86
kono
parents:
diff changeset
87 begin
kono
parents:
diff changeset
88 -- If the container is empty, return a result immediately, so that we do
kono
parents:
diff changeset
89 -- not manipulate the tamper bits unnecessarily.
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 if Tree.Root = null then
kono
parents:
diff changeset
92 return null;
kono
parents:
diff changeset
93 end if;
kono
parents:
diff changeset
94
kono
parents:
diff changeset
95 X := Tree.Root;
kono
parents:
diff changeset
96 while X /= null loop
kono
parents:
diff changeset
97 if Is_Greater_Key_Node (Key, X) then
kono
parents:
diff changeset
98 X := Ops.Right (X);
kono
parents:
diff changeset
99 else
kono
parents:
diff changeset
100 Y := X;
kono
parents:
diff changeset
101 X := Ops.Left (X);
kono
parents:
diff changeset
102 end if;
kono
parents:
diff changeset
103 end loop;
kono
parents:
diff changeset
104
kono
parents:
diff changeset
105 if Y = null or else Is_Less_Key_Node (Key, Y) then
kono
parents:
diff changeset
106 return null;
kono
parents:
diff changeset
107 else
kono
parents:
diff changeset
108 return Y;
kono
parents:
diff changeset
109 end if;
kono
parents:
diff changeset
110 end Find;
kono
parents:
diff changeset
111
kono
parents:
diff changeset
112 -----------
kono
parents:
diff changeset
113 -- Floor --
kono
parents:
diff changeset
114 -----------
kono
parents:
diff changeset
115
kono
parents:
diff changeset
116 function Floor (Tree : Tree_Type; Key : Key_Type) return Node_Access is
kono
parents:
diff changeset
117 -- Per AI05-0022, the container implementation is required to detect
kono
parents:
diff changeset
118 -- element tampering by a generic actual subprogram.
kono
parents:
diff changeset
119
kono
parents:
diff changeset
120 Lock : With_Lock (Tree.TC'Unrestricted_Access);
kono
parents:
diff changeset
121
kono
parents:
diff changeset
122 Y : Node_Access;
kono
parents:
diff changeset
123 X : Node_Access;
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 begin
kono
parents:
diff changeset
126 -- If the container is empty, return a result immediately, so that we do
kono
parents:
diff changeset
127 -- not manipulate the tamper bits unnecessarily.
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 if Tree.Root = null then
kono
parents:
diff changeset
130 return null;
kono
parents:
diff changeset
131 end if;
kono
parents:
diff changeset
132
kono
parents:
diff changeset
133 X := Tree.Root;
kono
parents:
diff changeset
134 while X /= null loop
kono
parents:
diff changeset
135 if Is_Less_Key_Node (Key, X) then
kono
parents:
diff changeset
136 X := Ops.Left (X);
kono
parents:
diff changeset
137 else
kono
parents:
diff changeset
138 Y := X;
kono
parents:
diff changeset
139 X := Ops.Right (X);
kono
parents:
diff changeset
140 end if;
kono
parents:
diff changeset
141 end loop;
kono
parents:
diff changeset
142
kono
parents:
diff changeset
143 return Y;
kono
parents:
diff changeset
144 end Floor;
kono
parents:
diff changeset
145
kono
parents:
diff changeset
146 --------------------------------
kono
parents:
diff changeset
147 -- Generic_Conditional_Insert --
kono
parents:
diff changeset
148 --------------------------------
kono
parents:
diff changeset
149
kono
parents:
diff changeset
150 procedure Generic_Conditional_Insert
kono
parents:
diff changeset
151 (Tree : in out Tree_Type;
kono
parents:
diff changeset
152 Key : Key_Type;
kono
parents:
diff changeset
153 Node : out Node_Access;
kono
parents:
diff changeset
154 Inserted : out Boolean)
kono
parents:
diff changeset
155 is
kono
parents:
diff changeset
156 X : Node_Access;
kono
parents:
diff changeset
157 Y : Node_Access;
kono
parents:
diff changeset
158
kono
parents:
diff changeset
159 Compare : Boolean;
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 begin
kono
parents:
diff changeset
162 -- This is a "conditional" insertion, meaning that the insertion request
kono
parents:
diff changeset
163 -- can "fail" in the sense that no new node is created. If the Key is
kono
parents:
diff changeset
164 -- equivalent to an existing node, then we return the existing node and
kono
parents:
diff changeset
165 -- Inserted is set to False. Otherwise, we allocate a new node (via
kono
parents:
diff changeset
166 -- Insert_Post) and Inserted is set to True.
kono
parents:
diff changeset
167
kono
parents:
diff changeset
168 -- Note that we are testing for equivalence here, not equality. Key must
kono
parents:
diff changeset
169 -- be strictly less than its next neighbor, and strictly greater than
kono
parents:
diff changeset
170 -- its previous neighbor, in order for the conditional insertion to
kono
parents:
diff changeset
171 -- succeed.
kono
parents:
diff changeset
172
kono
parents:
diff changeset
173 -- Handle insertion into an empty container as a special case, so that
kono
parents:
diff changeset
174 -- we do not manipulate the tamper bits unnecessarily.
kono
parents:
diff changeset
175
kono
parents:
diff changeset
176 if Tree.Root = null then
kono
parents:
diff changeset
177 Insert_Post (Tree, null, True, Node);
kono
parents:
diff changeset
178 Inserted := True;
kono
parents:
diff changeset
179 return;
kono
parents:
diff changeset
180 end if;
kono
parents:
diff changeset
181
kono
parents:
diff changeset
182 -- We search the tree to find the nearest neighbor of Key, which is
kono
parents:
diff changeset
183 -- either the smallest node greater than Key (Inserted is True), or the
kono
parents:
diff changeset
184 -- largest node less or equivalent to Key (Inserted is False).
kono
parents:
diff changeset
185
kono
parents:
diff changeset
186 declare
kono
parents:
diff changeset
187 Lock : With_Lock (Tree.TC'Unrestricted_Access);
kono
parents:
diff changeset
188 begin
kono
parents:
diff changeset
189 X := Tree.Root;
kono
parents:
diff changeset
190 Y := null;
kono
parents:
diff changeset
191 Inserted := True;
kono
parents:
diff changeset
192 while X /= null loop
kono
parents:
diff changeset
193 Y := X;
kono
parents:
diff changeset
194 Inserted := Is_Less_Key_Node (Key, X);
kono
parents:
diff changeset
195 X := (if Inserted then Ops.Left (X) else Ops.Right (X));
kono
parents:
diff changeset
196 end loop;
kono
parents:
diff changeset
197 end;
kono
parents:
diff changeset
198
kono
parents:
diff changeset
199 if Inserted then
kono
parents:
diff changeset
200
kono
parents:
diff changeset
201 -- Key is less than Y. If Y is the first node in the tree, then there
kono
parents:
diff changeset
202 -- are no other nodes that we need to search for, and we insert a new
kono
parents:
diff changeset
203 -- node into the tree.
kono
parents:
diff changeset
204
kono
parents:
diff changeset
205 if Y = Tree.First then
kono
parents:
diff changeset
206 Insert_Post (Tree, Y, True, Node);
kono
parents:
diff changeset
207 return;
kono
parents:
diff changeset
208 end if;
kono
parents:
diff changeset
209
kono
parents:
diff changeset
210 -- Y is the next nearest-neighbor of Key. We know that Key is not
kono
parents:
diff changeset
211 -- equivalent to Y (because Key is strictly less than Y), so we move
kono
parents:
diff changeset
212 -- to the previous node, the nearest-neighbor just smaller or
kono
parents:
diff changeset
213 -- equivalent to Key.
kono
parents:
diff changeset
214
kono
parents:
diff changeset
215 Node := Ops.Previous (Y);
kono
parents:
diff changeset
216
kono
parents:
diff changeset
217 else
kono
parents:
diff changeset
218 -- Y is the previous nearest-neighbor of Key. We know that Key is not
kono
parents:
diff changeset
219 -- less than Y, which means either that Key is equivalent to Y, or
kono
parents:
diff changeset
220 -- greater than Y.
kono
parents:
diff changeset
221
kono
parents:
diff changeset
222 Node := Y;
kono
parents:
diff changeset
223 end if;
kono
parents:
diff changeset
224
kono
parents:
diff changeset
225 -- Key is equivalent to or greater than Node. We must resolve which is
kono
parents:
diff changeset
226 -- the case, to determine whether the conditional insertion succeeds.
kono
parents:
diff changeset
227
kono
parents:
diff changeset
228 declare
kono
parents:
diff changeset
229 Lock : With_Lock (Tree.TC'Unrestricted_Access);
kono
parents:
diff changeset
230 begin
kono
parents:
diff changeset
231 Compare := Is_Greater_Key_Node (Key, Node);
kono
parents:
diff changeset
232 end;
kono
parents:
diff changeset
233
kono
parents:
diff changeset
234 if Compare then
kono
parents:
diff changeset
235
kono
parents:
diff changeset
236 -- Key is strictly greater than Node, which means that Key is not
kono
parents:
diff changeset
237 -- equivalent to Node. In this case, the insertion succeeds, and we
kono
parents:
diff changeset
238 -- insert a new node into the tree.
kono
parents:
diff changeset
239
kono
parents:
diff changeset
240 Insert_Post (Tree, Y, Inserted, Node);
kono
parents:
diff changeset
241 Inserted := True;
kono
parents:
diff changeset
242 return;
kono
parents:
diff changeset
243 end if;
kono
parents:
diff changeset
244
kono
parents:
diff changeset
245 -- Key is equivalent to Node. This is a conditional insertion, so we do
kono
parents:
diff changeset
246 -- not insert a new node in this case. We return the existing node and
kono
parents:
diff changeset
247 -- report that no insertion has occurred.
kono
parents:
diff changeset
248
kono
parents:
diff changeset
249 Inserted := False;
kono
parents:
diff changeset
250 end Generic_Conditional_Insert;
kono
parents:
diff changeset
251
kono
parents:
diff changeset
252 ------------------------------------------
kono
parents:
diff changeset
253 -- Generic_Conditional_Insert_With_Hint --
kono
parents:
diff changeset
254 ------------------------------------------
kono
parents:
diff changeset
255
kono
parents:
diff changeset
256 procedure Generic_Conditional_Insert_With_Hint
kono
parents:
diff changeset
257 (Tree : in out Tree_Type;
kono
parents:
diff changeset
258 Position : Node_Access;
kono
parents:
diff changeset
259 Key : Key_Type;
kono
parents:
diff changeset
260 Node : out Node_Access;
kono
parents:
diff changeset
261 Inserted : out Boolean)
kono
parents:
diff changeset
262 is
kono
parents:
diff changeset
263 Test : Node_Access;
kono
parents:
diff changeset
264 Compare : Boolean;
kono
parents:
diff changeset
265
kono
parents:
diff changeset
266 begin
kono
parents:
diff changeset
267 -- The purpose of a hint is to avoid a search from the root of
kono
parents:
diff changeset
268 -- tree. If we have it hint it means we only need to traverse the
kono
parents:
diff changeset
269 -- subtree rooted at the hint to find the nearest neighbor. Note
kono
parents:
diff changeset
270 -- that finding the neighbor means merely walking the tree; this
kono
parents:
diff changeset
271 -- is not a search and the only comparisons that occur are with
kono
parents:
diff changeset
272 -- the hint and its neighbor.
kono
parents:
diff changeset
273
kono
parents:
diff changeset
274 -- Handle insertion into an empty container as a special case, so that
kono
parents:
diff changeset
275 -- we do not manipulate the tamper bits unnecessarily.
kono
parents:
diff changeset
276
kono
parents:
diff changeset
277 if Tree.Root = null then
kono
parents:
diff changeset
278 Insert_Post (Tree, null, True, Node);
kono
parents:
diff changeset
279 Inserted := True;
kono
parents:
diff changeset
280 return;
kono
parents:
diff changeset
281 end if;
kono
parents:
diff changeset
282
kono
parents:
diff changeset
283 -- If Position is null, this is interpreted to mean that Key is large
kono
parents:
diff changeset
284 -- relative to the nodes in the tree. If Key is greater than the last
kono
parents:
diff changeset
285 -- node in the tree, then we're done; otherwise the hint was "wrong" and
kono
parents:
diff changeset
286 -- we must search.
kono
parents:
diff changeset
287
kono
parents:
diff changeset
288 if Position = null then -- largest
kono
parents:
diff changeset
289 declare
kono
parents:
diff changeset
290 Lock : With_Lock (Tree.TC'Unrestricted_Access);
kono
parents:
diff changeset
291 begin
kono
parents:
diff changeset
292 Compare := Is_Greater_Key_Node (Key, Tree.Last);
kono
parents:
diff changeset
293 end;
kono
parents:
diff changeset
294
kono
parents:
diff changeset
295 if Compare then
kono
parents:
diff changeset
296 Insert_Post (Tree, Tree.Last, False, Node);
kono
parents:
diff changeset
297 Inserted := True;
kono
parents:
diff changeset
298 else
kono
parents:
diff changeset
299 Conditional_Insert_Sans_Hint (Tree, Key, Node, Inserted);
kono
parents:
diff changeset
300 end if;
kono
parents:
diff changeset
301
kono
parents:
diff changeset
302 return;
kono
parents:
diff changeset
303 end if;
kono
parents:
diff changeset
304
kono
parents:
diff changeset
305 pragma Assert (Tree.Length > 0);
kono
parents:
diff changeset
306
kono
parents:
diff changeset
307 -- A hint can either name the node that immediately follows Key,
kono
parents:
diff changeset
308 -- or immediately precedes Key. We first test whether Key is
kono
parents:
diff changeset
309 -- less than the hint, and if so we compare Key to the node that
kono
parents:
diff changeset
310 -- precedes the hint. If Key is both less than the hint and
kono
parents:
diff changeset
311 -- greater than the hint's preceding neighbor, then we're done;
kono
parents:
diff changeset
312 -- otherwise we must search.
kono
parents:
diff changeset
313
kono
parents:
diff changeset
314 -- Note also that a hint can either be an anterior node or a leaf
kono
parents:
diff changeset
315 -- node. A new node is always inserted at the bottom of the tree
kono
parents:
diff changeset
316 -- (at least prior to rebalancing), becoming the new left or
kono
parents:
diff changeset
317 -- right child of leaf node (which prior to the insertion must
kono
parents:
diff changeset
318 -- necessarily be null, since this is a leaf). If the hint names
kono
parents:
diff changeset
319 -- an anterior node then its neighbor must be a leaf, and so
kono
parents:
diff changeset
320 -- (here) we insert after the neighbor. If the hint names a leaf
kono
parents:
diff changeset
321 -- then its neighbor must be anterior and so we insert before the
kono
parents:
diff changeset
322 -- hint.
kono
parents:
diff changeset
323
kono
parents:
diff changeset
324 declare
kono
parents:
diff changeset
325 Lock : With_Lock (Tree.TC'Unrestricted_Access);
kono
parents:
diff changeset
326 begin
kono
parents:
diff changeset
327 Compare := Is_Less_Key_Node (Key, Position);
kono
parents:
diff changeset
328 end;
kono
parents:
diff changeset
329
kono
parents:
diff changeset
330 if Compare then
kono
parents:
diff changeset
331 Test := Ops.Previous (Position); -- "before"
kono
parents:
diff changeset
332
kono
parents:
diff changeset
333 if Test = null then -- new first node
kono
parents:
diff changeset
334 Insert_Post (Tree, Tree.First, True, Node);
kono
parents:
diff changeset
335
kono
parents:
diff changeset
336 Inserted := True;
kono
parents:
diff changeset
337 return;
kono
parents:
diff changeset
338 end if;
kono
parents:
diff changeset
339
kono
parents:
diff changeset
340 declare
kono
parents:
diff changeset
341 Lock : With_Lock (Tree.TC'Unrestricted_Access);
kono
parents:
diff changeset
342 begin
kono
parents:
diff changeset
343 Compare := Is_Greater_Key_Node (Key, Test);
kono
parents:
diff changeset
344 end;
kono
parents:
diff changeset
345
kono
parents:
diff changeset
346 if Compare then
kono
parents:
diff changeset
347 if Ops.Right (Test) = null then
kono
parents:
diff changeset
348 Insert_Post (Tree, Test, False, Node);
kono
parents:
diff changeset
349 else
kono
parents:
diff changeset
350 Insert_Post (Tree, Position, True, Node);
kono
parents:
diff changeset
351 end if;
kono
parents:
diff changeset
352
kono
parents:
diff changeset
353 Inserted := True;
kono
parents:
diff changeset
354
kono
parents:
diff changeset
355 else
kono
parents:
diff changeset
356 Conditional_Insert_Sans_Hint (Tree, Key, Node, Inserted);
kono
parents:
diff changeset
357 end if;
kono
parents:
diff changeset
358
kono
parents:
diff changeset
359 return;
kono
parents:
diff changeset
360 end if;
kono
parents:
diff changeset
361
kono
parents:
diff changeset
362 -- We know that Key isn't less than the hint so we try again, this time
kono
parents:
diff changeset
363 -- to see if it's greater than the hint. If so we compare Key to the
kono
parents:
diff changeset
364 -- node that follows the hint. If Key is both greater than the hint and
kono
parents:
diff changeset
365 -- less than the hint's next neighbor, then we're done; otherwise we
kono
parents:
diff changeset
366 -- must search.
kono
parents:
diff changeset
367
kono
parents:
diff changeset
368 declare
kono
parents:
diff changeset
369 Lock : With_Lock (Tree.TC'Unrestricted_Access);
kono
parents:
diff changeset
370 begin
kono
parents:
diff changeset
371 Compare := Is_Greater_Key_Node (Key, Position);
kono
parents:
diff changeset
372 end;
kono
parents:
diff changeset
373
kono
parents:
diff changeset
374 if Compare then
kono
parents:
diff changeset
375 Test := Ops.Next (Position); -- "after"
kono
parents:
diff changeset
376
kono
parents:
diff changeset
377 if Test = null then -- new last node
kono
parents:
diff changeset
378 Insert_Post (Tree, Tree.Last, False, Node);
kono
parents:
diff changeset
379
kono
parents:
diff changeset
380 Inserted := True;
kono
parents:
diff changeset
381 return;
kono
parents:
diff changeset
382 end if;
kono
parents:
diff changeset
383
kono
parents:
diff changeset
384 declare
kono
parents:
diff changeset
385 Lock : With_Lock (Tree.TC'Unrestricted_Access);
kono
parents:
diff changeset
386 begin
kono
parents:
diff changeset
387 Compare := Is_Less_Key_Node (Key, Test);
kono
parents:
diff changeset
388 end;
kono
parents:
diff changeset
389
kono
parents:
diff changeset
390 if Compare then
kono
parents:
diff changeset
391 if Ops.Right (Position) = null then
kono
parents:
diff changeset
392 Insert_Post (Tree, Position, False, Node);
kono
parents:
diff changeset
393 else
kono
parents:
diff changeset
394 Insert_Post (Tree, Test, True, Node);
kono
parents:
diff changeset
395 end if;
kono
parents:
diff changeset
396
kono
parents:
diff changeset
397 Inserted := True;
kono
parents:
diff changeset
398
kono
parents:
diff changeset
399 else
kono
parents:
diff changeset
400 Conditional_Insert_Sans_Hint (Tree, Key, Node, Inserted);
kono
parents:
diff changeset
401 end if;
kono
parents:
diff changeset
402
kono
parents:
diff changeset
403 return;
kono
parents:
diff changeset
404 end if;
kono
parents:
diff changeset
405
kono
parents:
diff changeset
406 -- We know that Key is neither less than the hint nor greater than the
kono
parents:
diff changeset
407 -- hint, and that's the definition of equivalence. There's nothing else
kono
parents:
diff changeset
408 -- we need to do, since a search would just reach the same conclusion.
kono
parents:
diff changeset
409
kono
parents:
diff changeset
410 Node := Position;
kono
parents:
diff changeset
411 Inserted := False;
kono
parents:
diff changeset
412 end Generic_Conditional_Insert_With_Hint;
kono
parents:
diff changeset
413
kono
parents:
diff changeset
414 -------------------------
kono
parents:
diff changeset
415 -- Generic_Insert_Post --
kono
parents:
diff changeset
416 -------------------------
kono
parents:
diff changeset
417
kono
parents:
diff changeset
418 procedure Generic_Insert_Post
kono
parents:
diff changeset
419 (Tree : in out Tree_Type;
kono
parents:
diff changeset
420 Y : Node_Access;
kono
parents:
diff changeset
421 Before : Boolean;
kono
parents:
diff changeset
422 Z : out Node_Access)
kono
parents:
diff changeset
423 is
kono
parents:
diff changeset
424 begin
kono
parents:
diff changeset
425 if Checks and then Tree.Length = Count_Type'Last then
kono
parents:
diff changeset
426 raise Constraint_Error with "too many elements";
kono
parents:
diff changeset
427 end if;
kono
parents:
diff changeset
428
kono
parents:
diff changeset
429 TC_Check (Tree.TC);
kono
parents:
diff changeset
430
kono
parents:
diff changeset
431 Z := New_Node;
kono
parents:
diff changeset
432 pragma Assert (Z /= null);
kono
parents:
diff changeset
433 pragma Assert (Ops.Color (Z) = Red);
kono
parents:
diff changeset
434
kono
parents:
diff changeset
435 if Y = null then
kono
parents:
diff changeset
436 pragma Assert (Tree.Length = 0);
kono
parents:
diff changeset
437 pragma Assert (Tree.Root = null);
kono
parents:
diff changeset
438 pragma Assert (Tree.First = null);
kono
parents:
diff changeset
439 pragma Assert (Tree.Last = null);
kono
parents:
diff changeset
440
kono
parents:
diff changeset
441 Tree.Root := Z;
kono
parents:
diff changeset
442 Tree.First := Z;
kono
parents:
diff changeset
443 Tree.Last := Z;
kono
parents:
diff changeset
444
kono
parents:
diff changeset
445 elsif Before then
kono
parents:
diff changeset
446 pragma Assert (Ops.Left (Y) = null);
kono
parents:
diff changeset
447
kono
parents:
diff changeset
448 Ops.Set_Left (Y, Z);
kono
parents:
diff changeset
449
kono
parents:
diff changeset
450 if Y = Tree.First then
kono
parents:
diff changeset
451 Tree.First := Z;
kono
parents:
diff changeset
452 end if;
kono
parents:
diff changeset
453
kono
parents:
diff changeset
454 else
kono
parents:
diff changeset
455 pragma Assert (Ops.Right (Y) = null);
kono
parents:
diff changeset
456
kono
parents:
diff changeset
457 Ops.Set_Right (Y, Z);
kono
parents:
diff changeset
458
kono
parents:
diff changeset
459 if Y = Tree.Last then
kono
parents:
diff changeset
460 Tree.Last := Z;
kono
parents:
diff changeset
461 end if;
kono
parents:
diff changeset
462 end if;
kono
parents:
diff changeset
463
kono
parents:
diff changeset
464 Ops.Set_Parent (Z, Y);
kono
parents:
diff changeset
465 Ops.Rebalance_For_Insert (Tree, Z);
kono
parents:
diff changeset
466 Tree.Length := Tree.Length + 1;
kono
parents:
diff changeset
467 end Generic_Insert_Post;
kono
parents:
diff changeset
468
kono
parents:
diff changeset
469 -----------------------
kono
parents:
diff changeset
470 -- Generic_Iteration --
kono
parents:
diff changeset
471 -----------------------
kono
parents:
diff changeset
472
kono
parents:
diff changeset
473 procedure Generic_Iteration
kono
parents:
diff changeset
474 (Tree : Tree_Type;
kono
parents:
diff changeset
475 Key : Key_Type)
kono
parents:
diff changeset
476 is
kono
parents:
diff changeset
477 procedure Iterate (Node : Node_Access);
kono
parents:
diff changeset
478
kono
parents:
diff changeset
479 -------------
kono
parents:
diff changeset
480 -- Iterate --
kono
parents:
diff changeset
481 -------------
kono
parents:
diff changeset
482
kono
parents:
diff changeset
483 procedure Iterate (Node : Node_Access) is
kono
parents:
diff changeset
484 N : Node_Access;
kono
parents:
diff changeset
485 begin
kono
parents:
diff changeset
486 N := Node;
kono
parents:
diff changeset
487 while N /= null loop
kono
parents:
diff changeset
488 if Is_Less_Key_Node (Key, N) then
kono
parents:
diff changeset
489 N := Ops.Left (N);
kono
parents:
diff changeset
490 elsif Is_Greater_Key_Node (Key, N) then
kono
parents:
diff changeset
491 N := Ops.Right (N);
kono
parents:
diff changeset
492 else
kono
parents:
diff changeset
493 Iterate (Ops.Left (N));
kono
parents:
diff changeset
494 Process (N);
kono
parents:
diff changeset
495 N := Ops.Right (N);
kono
parents:
diff changeset
496 end if;
kono
parents:
diff changeset
497 end loop;
kono
parents:
diff changeset
498 end Iterate;
kono
parents:
diff changeset
499
kono
parents:
diff changeset
500 -- Start of processing for Generic_Iteration
kono
parents:
diff changeset
501
kono
parents:
diff changeset
502 begin
kono
parents:
diff changeset
503 Iterate (Tree.Root);
kono
parents:
diff changeset
504 end Generic_Iteration;
kono
parents:
diff changeset
505
kono
parents:
diff changeset
506 -------------------------------
kono
parents:
diff changeset
507 -- Generic_Reverse_Iteration --
kono
parents:
diff changeset
508 -------------------------------
kono
parents:
diff changeset
509
kono
parents:
diff changeset
510 procedure Generic_Reverse_Iteration
kono
parents:
diff changeset
511 (Tree : Tree_Type;
kono
parents:
diff changeset
512 Key : Key_Type)
kono
parents:
diff changeset
513 is
kono
parents:
diff changeset
514 procedure Iterate (Node : Node_Access);
kono
parents:
diff changeset
515
kono
parents:
diff changeset
516 -------------
kono
parents:
diff changeset
517 -- Iterate --
kono
parents:
diff changeset
518 -------------
kono
parents:
diff changeset
519
kono
parents:
diff changeset
520 procedure Iterate (Node : Node_Access) is
kono
parents:
diff changeset
521 N : Node_Access;
kono
parents:
diff changeset
522 begin
kono
parents:
diff changeset
523 N := Node;
kono
parents:
diff changeset
524 while N /= null loop
kono
parents:
diff changeset
525 if Is_Less_Key_Node (Key, N) then
kono
parents:
diff changeset
526 N := Ops.Left (N);
kono
parents:
diff changeset
527 elsif Is_Greater_Key_Node (Key, N) then
kono
parents:
diff changeset
528 N := Ops.Right (N);
kono
parents:
diff changeset
529 else
kono
parents:
diff changeset
530 Iterate (Ops.Right (N));
kono
parents:
diff changeset
531 Process (N);
kono
parents:
diff changeset
532 N := Ops.Left (N);
kono
parents:
diff changeset
533 end if;
kono
parents:
diff changeset
534 end loop;
kono
parents:
diff changeset
535 end Iterate;
kono
parents:
diff changeset
536
kono
parents:
diff changeset
537 -- Start of processing for Generic_Reverse_Iteration
kono
parents:
diff changeset
538
kono
parents:
diff changeset
539 begin
kono
parents:
diff changeset
540 Iterate (Tree.Root);
kono
parents:
diff changeset
541 end Generic_Reverse_Iteration;
kono
parents:
diff changeset
542
kono
parents:
diff changeset
543 ----------------------------------
kono
parents:
diff changeset
544 -- Generic_Unconditional_Insert --
kono
parents:
diff changeset
545 ----------------------------------
kono
parents:
diff changeset
546
kono
parents:
diff changeset
547 procedure Generic_Unconditional_Insert
kono
parents:
diff changeset
548 (Tree : in out Tree_Type;
kono
parents:
diff changeset
549 Key : Key_Type;
kono
parents:
diff changeset
550 Node : out Node_Access)
kono
parents:
diff changeset
551 is
kono
parents:
diff changeset
552 Y : Node_Access;
kono
parents:
diff changeset
553 X : Node_Access;
kono
parents:
diff changeset
554
kono
parents:
diff changeset
555 Before : Boolean;
kono
parents:
diff changeset
556
kono
parents:
diff changeset
557 begin
kono
parents:
diff changeset
558 Y := null;
kono
parents:
diff changeset
559 Before := False;
kono
parents:
diff changeset
560
kono
parents:
diff changeset
561 X := Tree.Root;
kono
parents:
diff changeset
562 while X /= null loop
kono
parents:
diff changeset
563 Y := X;
kono
parents:
diff changeset
564 Before := Is_Less_Key_Node (Key, X);
kono
parents:
diff changeset
565 X := (if Before then Ops.Left (X) else Ops.Right (X));
kono
parents:
diff changeset
566 end loop;
kono
parents:
diff changeset
567
kono
parents:
diff changeset
568 Insert_Post (Tree, Y, Before, Node);
kono
parents:
diff changeset
569 end Generic_Unconditional_Insert;
kono
parents:
diff changeset
570
kono
parents:
diff changeset
571 --------------------------------------------
kono
parents:
diff changeset
572 -- Generic_Unconditional_Insert_With_Hint --
kono
parents:
diff changeset
573 --------------------------------------------
kono
parents:
diff changeset
574
kono
parents:
diff changeset
575 procedure Generic_Unconditional_Insert_With_Hint
kono
parents:
diff changeset
576 (Tree : in out Tree_Type;
kono
parents:
diff changeset
577 Hint : Node_Access;
kono
parents:
diff changeset
578 Key : Key_Type;
kono
parents:
diff changeset
579 Node : out Node_Access)
kono
parents:
diff changeset
580 is
kono
parents:
diff changeset
581 begin
kono
parents:
diff changeset
582 -- There are fewer constraints for an unconditional insertion
kono
parents:
diff changeset
583 -- than for a conditional insertion, since we allow duplicate
kono
parents:
diff changeset
584 -- keys. So instead of having to check (say) whether Key is
kono
parents:
diff changeset
585 -- (strictly) greater than the hint's previous neighbor, here we
kono
parents:
diff changeset
586 -- allow Key to be equal to or greater than the previous node.
kono
parents:
diff changeset
587
kono
parents:
diff changeset
588 -- There is the issue of what to do if Key is equivalent to the
kono
parents:
diff changeset
589 -- hint. Does the new node get inserted before or after the hint?
kono
parents:
diff changeset
590 -- We decide that it gets inserted after the hint, reasoning that
kono
parents:
diff changeset
591 -- this is consistent with behavior for non-hint insertion, which
kono
parents:
diff changeset
592 -- inserts a new node after existing nodes with equivalent keys.
kono
parents:
diff changeset
593
kono
parents:
diff changeset
594 -- First we check whether the hint is null, which is interpreted
kono
parents:
diff changeset
595 -- to mean that Key is large relative to existing nodes.
kono
parents:
diff changeset
596 -- Following our rule above, if Key is equal to or greater than
kono
parents:
diff changeset
597 -- the last node, then we insert the new node immediately after
kono
parents:
diff changeset
598 -- last. (We don't have an operation for testing whether a key is
kono
parents:
diff changeset
599 -- "equal to or greater than" a node, so we must say instead "not
kono
parents:
diff changeset
600 -- less than", which is equivalent.)
kono
parents:
diff changeset
601
kono
parents:
diff changeset
602 if Hint = null then -- largest
kono
parents:
diff changeset
603 if Tree.Last = null then
kono
parents:
diff changeset
604 Insert_Post (Tree, null, False, Node);
kono
parents:
diff changeset
605 elsif Is_Less_Key_Node (Key, Tree.Last) then
kono
parents:
diff changeset
606 Unconditional_Insert_Sans_Hint (Tree, Key, Node);
kono
parents:
diff changeset
607 else
kono
parents:
diff changeset
608 Insert_Post (Tree, Tree.Last, False, Node);
kono
parents:
diff changeset
609 end if;
kono
parents:
diff changeset
610
kono
parents:
diff changeset
611 return;
kono
parents:
diff changeset
612 end if;
kono
parents:
diff changeset
613
kono
parents:
diff changeset
614 pragma Assert (Tree.Length > 0);
kono
parents:
diff changeset
615
kono
parents:
diff changeset
616 -- We decide here whether to insert the new node prior to the
kono
parents:
diff changeset
617 -- hint. Key could be equivalent to the hint, so in theory we
kono
parents:
diff changeset
618 -- could write the following test as "not greater than" (same as
kono
parents:
diff changeset
619 -- "less than or equal to"). If Key were equivalent to the hint,
kono
parents:
diff changeset
620 -- that would mean that the new node gets inserted before an
kono
parents:
diff changeset
621 -- equivalent node. That wouldn't break any container invariants,
kono
parents:
diff changeset
622 -- but our rule above says that new nodes always get inserted
kono
parents:
diff changeset
623 -- after equivalent nodes. So here we test whether Key is both
kono
parents:
diff changeset
624 -- less than the hint and equal to or greater than the hint's
kono
parents:
diff changeset
625 -- previous neighbor, and if so insert it before the hint.
kono
parents:
diff changeset
626
kono
parents:
diff changeset
627 if Is_Less_Key_Node (Key, Hint) then
kono
parents:
diff changeset
628 declare
kono
parents:
diff changeset
629 Before : constant Node_Access := Ops.Previous (Hint);
kono
parents:
diff changeset
630 begin
kono
parents:
diff changeset
631 if Before = null then
kono
parents:
diff changeset
632 Insert_Post (Tree, Hint, True, Node);
kono
parents:
diff changeset
633 elsif Is_Less_Key_Node (Key, Before) then
kono
parents:
diff changeset
634 Unconditional_Insert_Sans_Hint (Tree, Key, Node);
kono
parents:
diff changeset
635 elsif Ops.Right (Before) = null then
kono
parents:
diff changeset
636 Insert_Post (Tree, Before, False, Node);
kono
parents:
diff changeset
637 else
kono
parents:
diff changeset
638 Insert_Post (Tree, Hint, True, Node);
kono
parents:
diff changeset
639 end if;
kono
parents:
diff changeset
640 end;
kono
parents:
diff changeset
641
kono
parents:
diff changeset
642 return;
kono
parents:
diff changeset
643 end if;
kono
parents:
diff changeset
644
kono
parents:
diff changeset
645 -- We know that Key isn't less than the hint, so it must be equal
kono
parents:
diff changeset
646 -- or greater. So we just test whether Key is less than or equal
kono
parents:
diff changeset
647 -- to (same as "not greater than") the hint's next neighbor, and
kono
parents:
diff changeset
648 -- if so insert it after the hint.
kono
parents:
diff changeset
649
kono
parents:
diff changeset
650 declare
kono
parents:
diff changeset
651 After : constant Node_Access := Ops.Next (Hint);
kono
parents:
diff changeset
652 begin
kono
parents:
diff changeset
653 if After = null then
kono
parents:
diff changeset
654 Insert_Post (Tree, Hint, False, Node);
kono
parents:
diff changeset
655 elsif Is_Greater_Key_Node (Key, After) then
kono
parents:
diff changeset
656 Unconditional_Insert_Sans_Hint (Tree, Key, Node);
kono
parents:
diff changeset
657 elsif Ops.Right (Hint) = null then
kono
parents:
diff changeset
658 Insert_Post (Tree, Hint, False, Node);
kono
parents:
diff changeset
659 else
kono
parents:
diff changeset
660 Insert_Post (Tree, After, True, Node);
kono
parents:
diff changeset
661 end if;
kono
parents:
diff changeset
662 end;
kono
parents:
diff changeset
663 end Generic_Unconditional_Insert_With_Hint;
kono
parents:
diff changeset
664
kono
parents:
diff changeset
665 -----------------
kono
parents:
diff changeset
666 -- Upper_Bound --
kono
parents:
diff changeset
667 -----------------
kono
parents:
diff changeset
668
kono
parents:
diff changeset
669 function Upper_Bound
kono
parents:
diff changeset
670 (Tree : Tree_Type;
kono
parents:
diff changeset
671 Key : Key_Type) return Node_Access
kono
parents:
diff changeset
672 is
kono
parents:
diff changeset
673 Y : Node_Access;
kono
parents:
diff changeset
674 X : Node_Access;
kono
parents:
diff changeset
675
kono
parents:
diff changeset
676 begin
kono
parents:
diff changeset
677 X := Tree.Root;
kono
parents:
diff changeset
678 while X /= null loop
kono
parents:
diff changeset
679 if Is_Less_Key_Node (Key, X) then
kono
parents:
diff changeset
680 Y := X;
kono
parents:
diff changeset
681 X := Ops.Left (X);
kono
parents:
diff changeset
682 else
kono
parents:
diff changeset
683 X := Ops.Right (X);
kono
parents:
diff changeset
684 end if;
kono
parents:
diff changeset
685 end loop;
kono
parents:
diff changeset
686
kono
parents:
diff changeset
687 return Y;
kono
parents:
diff changeset
688 end Upper_Bound;
kono
parents:
diff changeset
689
kono
parents:
diff changeset
690 end Ada.Containers.Red_Black_Trees.Generic_Keys;