annotate gcc/ada/libgnat/a-rbtgbk.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_BOUNDED_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_Bounded_Keys is
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 package Ops renames Tree_Operations;
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 -------------
kono
parents:
diff changeset
35 -- Ceiling --
kono
parents:
diff changeset
36 -------------
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 -- AKA Lower_Bound
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 function Ceiling
kono
parents:
diff changeset
41 (Tree : Tree_Type'Class;
kono
parents:
diff changeset
42 Key : Key_Type) return Count_Type
kono
parents:
diff changeset
43 is
kono
parents:
diff changeset
44 Y : Count_Type;
kono
parents:
diff changeset
45 X : Count_Type;
kono
parents:
diff changeset
46 N : Nodes_Type renames Tree.Nodes;
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 begin
kono
parents:
diff changeset
49 Y := 0;
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 X := Tree.Root;
kono
parents:
diff changeset
52 while X /= 0 loop
kono
parents:
diff changeset
53 if Is_Greater_Key_Node (Key, N (X)) then
kono
parents:
diff changeset
54 X := Ops.Right (N (X));
kono
parents:
diff changeset
55 else
kono
parents:
diff changeset
56 Y := X;
kono
parents:
diff changeset
57 X := Ops.Left (N (X));
kono
parents:
diff changeset
58 end if;
kono
parents:
diff changeset
59 end loop;
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 return Y;
kono
parents:
diff changeset
62 end Ceiling;
kono
parents:
diff changeset
63
kono
parents:
diff changeset
64 ----------
kono
parents:
diff changeset
65 -- Find --
kono
parents:
diff changeset
66 ----------
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 function Find
kono
parents:
diff changeset
69 (Tree : Tree_Type'Class;
kono
parents:
diff changeset
70 Key : Key_Type) return Count_Type
kono
parents:
diff changeset
71 is
kono
parents:
diff changeset
72 Y : Count_Type;
kono
parents:
diff changeset
73 X : Count_Type;
kono
parents:
diff changeset
74 N : Nodes_Type renames Tree.Nodes;
kono
parents:
diff changeset
75
kono
parents:
diff changeset
76 begin
kono
parents:
diff changeset
77 Y := 0;
kono
parents:
diff changeset
78
kono
parents:
diff changeset
79 X := Tree.Root;
kono
parents:
diff changeset
80 while X /= 0 loop
kono
parents:
diff changeset
81 if Is_Greater_Key_Node (Key, N (X)) then
kono
parents:
diff changeset
82 X := Ops.Right (N (X));
kono
parents:
diff changeset
83 else
kono
parents:
diff changeset
84 Y := X;
kono
parents:
diff changeset
85 X := Ops.Left (N (X));
kono
parents:
diff changeset
86 end if;
kono
parents:
diff changeset
87 end loop;
kono
parents:
diff changeset
88
kono
parents:
diff changeset
89 if Y = 0 then
kono
parents:
diff changeset
90 return 0;
kono
parents:
diff changeset
91 end if;
kono
parents:
diff changeset
92
kono
parents:
diff changeset
93 if Is_Less_Key_Node (Key, N (Y)) then
kono
parents:
diff changeset
94 return 0;
kono
parents:
diff changeset
95 end if;
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 return Y;
kono
parents:
diff changeset
98 end Find;
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 -----------
kono
parents:
diff changeset
101 -- Floor --
kono
parents:
diff changeset
102 -----------
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 function Floor
kono
parents:
diff changeset
105 (Tree : Tree_Type'Class;
kono
parents:
diff changeset
106 Key : Key_Type) return Count_Type
kono
parents:
diff changeset
107 is
kono
parents:
diff changeset
108 Y : Count_Type;
kono
parents:
diff changeset
109 X : Count_Type;
kono
parents:
diff changeset
110 N : Nodes_Type renames Tree.Nodes;
kono
parents:
diff changeset
111
kono
parents:
diff changeset
112 begin
kono
parents:
diff changeset
113 Y := 0;
kono
parents:
diff changeset
114
kono
parents:
diff changeset
115 X := Tree.Root;
kono
parents:
diff changeset
116 while X /= 0 loop
kono
parents:
diff changeset
117 if Is_Less_Key_Node (Key, N (X)) then
kono
parents:
diff changeset
118 X := Ops.Left (N (X));
kono
parents:
diff changeset
119 else
kono
parents:
diff changeset
120 Y := X;
kono
parents:
diff changeset
121 X := Ops.Right (N (X));
kono
parents:
diff changeset
122 end if;
kono
parents:
diff changeset
123 end loop;
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 return Y;
kono
parents:
diff changeset
126 end Floor;
kono
parents:
diff changeset
127
kono
parents:
diff changeset
128 --------------------------------
kono
parents:
diff changeset
129 -- Generic_Conditional_Insert --
kono
parents:
diff changeset
130 --------------------------------
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132 procedure Generic_Conditional_Insert
kono
parents:
diff changeset
133 (Tree : in out Tree_Type'Class;
kono
parents:
diff changeset
134 Key : Key_Type;
kono
parents:
diff changeset
135 Node : out Count_Type;
kono
parents:
diff changeset
136 Inserted : out Boolean)
kono
parents:
diff changeset
137 is
kono
parents:
diff changeset
138 Y : Count_Type;
kono
parents:
diff changeset
139 X : Count_Type;
kono
parents:
diff changeset
140 N : Nodes_Type renames Tree.Nodes;
kono
parents:
diff changeset
141
kono
parents:
diff changeset
142 begin
kono
parents:
diff changeset
143 -- This is a "conditional" insertion, meaning that the insertion request
kono
parents:
diff changeset
144 -- can "fail" in the sense that no new node is created. If the Key is
kono
parents:
diff changeset
145 -- equivalent to an existing node, then we return the existing node and
kono
parents:
diff changeset
146 -- Inserted is set to False. Otherwise, we allocate a new node (via
kono
parents:
diff changeset
147 -- Insert_Post) and Inserted is set to True.
kono
parents:
diff changeset
148
kono
parents:
diff changeset
149 -- Note that we are testing for equivalence here, not equality. Key must
kono
parents:
diff changeset
150 -- be strictly less than its next neighbor, and strictly greater than
kono
parents:
diff changeset
151 -- its previous neighbor, in order for the conditional insertion to
kono
parents:
diff changeset
152 -- succeed.
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 -- We search the tree to find the nearest neighbor of Key, which is
kono
parents:
diff changeset
155 -- either the smallest node greater than Key (Inserted is True), or the
kono
parents:
diff changeset
156 -- largest node less or equivalent to Key (Inserted is False).
kono
parents:
diff changeset
157
kono
parents:
diff changeset
158 Y := 0;
kono
parents:
diff changeset
159 X := Tree.Root;
kono
parents:
diff changeset
160 Inserted := True;
kono
parents:
diff changeset
161 while X /= 0 loop
kono
parents:
diff changeset
162 Y := X;
kono
parents:
diff changeset
163 Inserted := Is_Less_Key_Node (Key, N (X));
kono
parents:
diff changeset
164 X := (if Inserted then Ops.Left (N (X)) else Ops.Right (N (X)));
kono
parents:
diff changeset
165 end loop;
kono
parents:
diff changeset
166
kono
parents:
diff changeset
167 if Inserted then
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 -- Either Tree is empty, or Key is less than Y. If Y is the first
kono
parents:
diff changeset
170 -- node in the tree, then there are no other nodes that we need to
kono
parents:
diff changeset
171 -- search for, and we insert a new node into the tree.
kono
parents:
diff changeset
172
kono
parents:
diff changeset
173 if Y = Tree.First then
kono
parents:
diff changeset
174 Insert_Post (Tree, Y, True, Node);
kono
parents:
diff changeset
175 return;
kono
parents:
diff changeset
176 end if;
kono
parents:
diff changeset
177
kono
parents:
diff changeset
178 -- Y is the next nearest-neighbor of Key. We know that Key is not
kono
parents:
diff changeset
179 -- equivalent to Y (because Key is strictly less than Y), so we move
kono
parents:
diff changeset
180 -- to the previous node, the nearest-neighbor just smaller or
kono
parents:
diff changeset
181 -- equivalent to Key.
kono
parents:
diff changeset
182
kono
parents:
diff changeset
183 Node := Ops.Previous (Tree, Y);
kono
parents:
diff changeset
184
kono
parents:
diff changeset
185 else
kono
parents:
diff changeset
186 -- Y is the previous nearest-neighbor of Key. We know that Key is not
kono
parents:
diff changeset
187 -- less than Y, which means either that Key is equivalent to Y, or
kono
parents:
diff changeset
188 -- greater than Y.
kono
parents:
diff changeset
189
kono
parents:
diff changeset
190 Node := Y;
kono
parents:
diff changeset
191 end if;
kono
parents:
diff changeset
192
kono
parents:
diff changeset
193 -- Key is equivalent to or greater than Node. We must resolve which is
kono
parents:
diff changeset
194 -- the case, to determine whether the conditional insertion succeeds.
kono
parents:
diff changeset
195
kono
parents:
diff changeset
196 if Is_Greater_Key_Node (Key, N (Node)) then
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 -- Key is strictly greater than Node, which means that Key is not
kono
parents:
diff changeset
199 -- equivalent to Node. In this case, the insertion succeeds, and we
kono
parents:
diff changeset
200 -- insert a new node into the tree.
kono
parents:
diff changeset
201
kono
parents:
diff changeset
202 Insert_Post (Tree, Y, Inserted, Node);
kono
parents:
diff changeset
203 Inserted := True;
kono
parents:
diff changeset
204 return;
kono
parents:
diff changeset
205 end if;
kono
parents:
diff changeset
206
kono
parents:
diff changeset
207 -- Key is equivalent to Node. This is a conditional insertion, so we do
kono
parents:
diff changeset
208 -- not insert a new node in this case. We return the existing node and
kono
parents:
diff changeset
209 -- report that no insertion has occurred.
kono
parents:
diff changeset
210
kono
parents:
diff changeset
211 Inserted := False;
kono
parents:
diff changeset
212 end Generic_Conditional_Insert;
kono
parents:
diff changeset
213
kono
parents:
diff changeset
214 ------------------------------------------
kono
parents:
diff changeset
215 -- Generic_Conditional_Insert_With_Hint --
kono
parents:
diff changeset
216 ------------------------------------------
kono
parents:
diff changeset
217
kono
parents:
diff changeset
218 procedure Generic_Conditional_Insert_With_Hint
kono
parents:
diff changeset
219 (Tree : in out Tree_Type'Class;
kono
parents:
diff changeset
220 Position : Count_Type;
kono
parents:
diff changeset
221 Key : Key_Type;
kono
parents:
diff changeset
222 Node : out Count_Type;
kono
parents:
diff changeset
223 Inserted : out Boolean)
kono
parents:
diff changeset
224 is
kono
parents:
diff changeset
225 N : Nodes_Type renames Tree.Nodes;
kono
parents:
diff changeset
226
kono
parents:
diff changeset
227 begin
kono
parents:
diff changeset
228 -- The purpose of a hint is to avoid a search from the root of
kono
parents:
diff changeset
229 -- tree. If we have it hint it means we only need to traverse the
kono
parents:
diff changeset
230 -- subtree rooted at the hint to find the nearest neighbor. Note
kono
parents:
diff changeset
231 -- that finding the neighbor means merely walking the tree; this
kono
parents:
diff changeset
232 -- is not a search and the only comparisons that occur are with
kono
parents:
diff changeset
233 -- the hint and its neighbor.
kono
parents:
diff changeset
234
kono
parents:
diff changeset
235 -- If Position is 0, this is interpreted to mean that Key is
kono
parents:
diff changeset
236 -- large relative to the nodes in the tree. If the tree is empty,
kono
parents:
diff changeset
237 -- or Key is greater than the last node in the tree, then we're
kono
parents:
diff changeset
238 -- done; otherwise the hint was "wrong" and we must search.
kono
parents:
diff changeset
239
kono
parents:
diff changeset
240 if Position = 0 then -- largest
kono
parents:
diff changeset
241 if Tree.Last = 0
kono
parents:
diff changeset
242 or else Is_Greater_Key_Node (Key, N (Tree.Last))
kono
parents:
diff changeset
243 then
kono
parents:
diff changeset
244 Insert_Post (Tree, Tree.Last, False, Node);
kono
parents:
diff changeset
245 Inserted := True;
kono
parents:
diff changeset
246 else
kono
parents:
diff changeset
247 Conditional_Insert_Sans_Hint (Tree, Key, Node, Inserted);
kono
parents:
diff changeset
248 end if;
kono
parents:
diff changeset
249
kono
parents:
diff changeset
250 return;
kono
parents:
diff changeset
251 end if;
kono
parents:
diff changeset
252
kono
parents:
diff changeset
253 pragma Assert (Tree.Length > 0);
kono
parents:
diff changeset
254
kono
parents:
diff changeset
255 -- A hint can either name the node that immediately follows Key,
kono
parents:
diff changeset
256 -- or immediately precedes Key. We first test whether Key is
kono
parents:
diff changeset
257 -- less than the hint, and if so we compare Key to the node that
kono
parents:
diff changeset
258 -- precedes the hint. If Key is both less than the hint and
kono
parents:
diff changeset
259 -- greater than the hint's preceding neighbor, then we're done;
kono
parents:
diff changeset
260 -- otherwise we must search.
kono
parents:
diff changeset
261
kono
parents:
diff changeset
262 -- Note also that a hint can either be an anterior node or a leaf
kono
parents:
diff changeset
263 -- node. A new node is always inserted at the bottom of the tree
kono
parents:
diff changeset
264 -- (at least prior to rebalancing), becoming the new left or
kono
parents:
diff changeset
265 -- right child of leaf node (which prior to the insertion must
kono
parents:
diff changeset
266 -- necessarily be null, since this is a leaf). If the hint names
kono
parents:
diff changeset
267 -- an anterior node then its neighbor must be a leaf, and so
kono
parents:
diff changeset
268 -- (here) we insert after the neighbor. If the hint names a leaf
kono
parents:
diff changeset
269 -- then its neighbor must be anterior and so we insert before the
kono
parents:
diff changeset
270 -- hint.
kono
parents:
diff changeset
271
kono
parents:
diff changeset
272 if Is_Less_Key_Node (Key, N (Position)) then
kono
parents:
diff changeset
273 declare
kono
parents:
diff changeset
274 Before : constant Count_Type := Ops.Previous (Tree, Position);
kono
parents:
diff changeset
275
kono
parents:
diff changeset
276 begin
kono
parents:
diff changeset
277 if Before = 0 then
kono
parents:
diff changeset
278 Insert_Post (Tree, Tree.First, True, Node);
kono
parents:
diff changeset
279 Inserted := True;
kono
parents:
diff changeset
280
kono
parents:
diff changeset
281 elsif Is_Greater_Key_Node (Key, N (Before)) then
kono
parents:
diff changeset
282 if Ops.Right (N (Before)) = 0 then
kono
parents:
diff changeset
283 Insert_Post (Tree, Before, False, Node);
kono
parents:
diff changeset
284 else
kono
parents:
diff changeset
285 Insert_Post (Tree, Position, True, Node);
kono
parents:
diff changeset
286 end if;
kono
parents:
diff changeset
287
kono
parents:
diff changeset
288 Inserted := True;
kono
parents:
diff changeset
289
kono
parents:
diff changeset
290 else
kono
parents:
diff changeset
291 Conditional_Insert_Sans_Hint (Tree, Key, Node, Inserted);
kono
parents:
diff changeset
292 end if;
kono
parents:
diff changeset
293 end;
kono
parents:
diff changeset
294
kono
parents:
diff changeset
295 return;
kono
parents:
diff changeset
296 end if;
kono
parents:
diff changeset
297
kono
parents:
diff changeset
298 -- We know that Key isn't less than the hint so we try again,
kono
parents:
diff changeset
299 -- this time to see if it's greater than the hint. If so we
kono
parents:
diff changeset
300 -- compare Key to the node that follows the hint. If Key is both
kono
parents:
diff changeset
301 -- greater than the hint and less than the hint's next neighbor,
kono
parents:
diff changeset
302 -- then we're done; otherwise we must search.
kono
parents:
diff changeset
303
kono
parents:
diff changeset
304 if Is_Greater_Key_Node (Key, N (Position)) then
kono
parents:
diff changeset
305 declare
kono
parents:
diff changeset
306 After : constant Count_Type := Ops.Next (Tree, Position);
kono
parents:
diff changeset
307
kono
parents:
diff changeset
308 begin
kono
parents:
diff changeset
309 if After = 0 then
kono
parents:
diff changeset
310 Insert_Post (Tree, Tree.Last, False, Node);
kono
parents:
diff changeset
311 Inserted := True;
kono
parents:
diff changeset
312
kono
parents:
diff changeset
313 elsif Is_Less_Key_Node (Key, N (After)) then
kono
parents:
diff changeset
314 if Ops.Right (N (Position)) = 0 then
kono
parents:
diff changeset
315 Insert_Post (Tree, Position, False, Node);
kono
parents:
diff changeset
316 else
kono
parents:
diff changeset
317 Insert_Post (Tree, After, True, Node);
kono
parents:
diff changeset
318 end if;
kono
parents:
diff changeset
319
kono
parents:
diff changeset
320 Inserted := True;
kono
parents:
diff changeset
321
kono
parents:
diff changeset
322 else
kono
parents:
diff changeset
323 Conditional_Insert_Sans_Hint (Tree, Key, Node, Inserted);
kono
parents:
diff changeset
324 end if;
kono
parents:
diff changeset
325 end;
kono
parents:
diff changeset
326
kono
parents:
diff changeset
327 return;
kono
parents:
diff changeset
328 end if;
kono
parents:
diff changeset
329
kono
parents:
diff changeset
330 -- We know that Key is neither less than the hint nor greater
kono
parents:
diff changeset
331 -- than the hint, and that's the definition of equivalence.
kono
parents:
diff changeset
332 -- There's nothing else we need to do, since a search would just
kono
parents:
diff changeset
333 -- reach the same conclusion.
kono
parents:
diff changeset
334
kono
parents:
diff changeset
335 Node := Position;
kono
parents:
diff changeset
336 Inserted := False;
kono
parents:
diff changeset
337 end Generic_Conditional_Insert_With_Hint;
kono
parents:
diff changeset
338
kono
parents:
diff changeset
339 -------------------------
kono
parents:
diff changeset
340 -- Generic_Insert_Post --
kono
parents:
diff changeset
341 -------------------------
kono
parents:
diff changeset
342
kono
parents:
diff changeset
343 procedure Generic_Insert_Post
kono
parents:
diff changeset
344 (Tree : in out Tree_Type'Class;
kono
parents:
diff changeset
345 Y : Count_Type;
kono
parents:
diff changeset
346 Before : Boolean;
kono
parents:
diff changeset
347 Z : out Count_Type)
kono
parents:
diff changeset
348 is
kono
parents:
diff changeset
349 N : Nodes_Type renames Tree.Nodes;
kono
parents:
diff changeset
350
kono
parents:
diff changeset
351 begin
kono
parents:
diff changeset
352 TC_Check (Tree.TC);
kono
parents:
diff changeset
353
kono
parents:
diff changeset
354 if Checks and then Tree.Length >= Tree.Capacity then
kono
parents:
diff changeset
355 raise Capacity_Error with "not enough capacity to insert new item";
kono
parents:
diff changeset
356 end if;
kono
parents:
diff changeset
357
kono
parents:
diff changeset
358 Z := New_Node;
kono
parents:
diff changeset
359 pragma Assert (Z /= 0);
kono
parents:
diff changeset
360
kono
parents:
diff changeset
361 if Y = 0 then
kono
parents:
diff changeset
362 pragma Assert (Tree.Length = 0);
kono
parents:
diff changeset
363 pragma Assert (Tree.Root = 0);
kono
parents:
diff changeset
364 pragma Assert (Tree.First = 0);
kono
parents:
diff changeset
365 pragma Assert (Tree.Last = 0);
kono
parents:
diff changeset
366
kono
parents:
diff changeset
367 Tree.Root := Z;
kono
parents:
diff changeset
368 Tree.First := Z;
kono
parents:
diff changeset
369 Tree.Last := Z;
kono
parents:
diff changeset
370
kono
parents:
diff changeset
371 elsif Before then
kono
parents:
diff changeset
372 pragma Assert (Ops.Left (N (Y)) = 0);
kono
parents:
diff changeset
373
kono
parents:
diff changeset
374 Ops.Set_Left (N (Y), Z);
kono
parents:
diff changeset
375
kono
parents:
diff changeset
376 if Y = Tree.First then
kono
parents:
diff changeset
377 Tree.First := Z;
kono
parents:
diff changeset
378 end if;
kono
parents:
diff changeset
379
kono
parents:
diff changeset
380 else
kono
parents:
diff changeset
381 pragma Assert (Ops.Right (N (Y)) = 0);
kono
parents:
diff changeset
382
kono
parents:
diff changeset
383 Ops.Set_Right (N (Y), Z);
kono
parents:
diff changeset
384
kono
parents:
diff changeset
385 if Y = Tree.Last then
kono
parents:
diff changeset
386 Tree.Last := Z;
kono
parents:
diff changeset
387 end if;
kono
parents:
diff changeset
388 end if;
kono
parents:
diff changeset
389
kono
parents:
diff changeset
390 Ops.Set_Color (N (Z), Red);
kono
parents:
diff changeset
391 Ops.Set_Parent (N (Z), Y);
kono
parents:
diff changeset
392 Ops.Rebalance_For_Insert (Tree, Z);
kono
parents:
diff changeset
393 Tree.Length := Tree.Length + 1;
kono
parents:
diff changeset
394 end Generic_Insert_Post;
kono
parents:
diff changeset
395
kono
parents:
diff changeset
396 -----------------------
kono
parents:
diff changeset
397 -- Generic_Iteration --
kono
parents:
diff changeset
398 -----------------------
kono
parents:
diff changeset
399
kono
parents:
diff changeset
400 procedure Generic_Iteration
kono
parents:
diff changeset
401 (Tree : Tree_Type'Class;
kono
parents:
diff changeset
402 Key : Key_Type)
kono
parents:
diff changeset
403 is
kono
parents:
diff changeset
404 procedure Iterate (Index : Count_Type);
kono
parents:
diff changeset
405
kono
parents:
diff changeset
406 -------------
kono
parents:
diff changeset
407 -- Iterate --
kono
parents:
diff changeset
408 -------------
kono
parents:
diff changeset
409
kono
parents:
diff changeset
410 procedure Iterate (Index : Count_Type) is
kono
parents:
diff changeset
411 J : Count_Type;
kono
parents:
diff changeset
412 N : Nodes_Type renames Tree.Nodes;
kono
parents:
diff changeset
413
kono
parents:
diff changeset
414 begin
kono
parents:
diff changeset
415 J := Index;
kono
parents:
diff changeset
416 while J /= 0 loop
kono
parents:
diff changeset
417 if Is_Less_Key_Node (Key, N (J)) then
kono
parents:
diff changeset
418 J := Ops.Left (N (J));
kono
parents:
diff changeset
419 elsif Is_Greater_Key_Node (Key, N (J)) then
kono
parents:
diff changeset
420 J := Ops.Right (N (J));
kono
parents:
diff changeset
421 else
kono
parents:
diff changeset
422 Iterate (Ops.Left (N (J)));
kono
parents:
diff changeset
423 Process (J);
kono
parents:
diff changeset
424 J := Ops.Right (N (J));
kono
parents:
diff changeset
425 end if;
kono
parents:
diff changeset
426 end loop;
kono
parents:
diff changeset
427 end Iterate;
kono
parents:
diff changeset
428
kono
parents:
diff changeset
429 -- Start of processing for Generic_Iteration
kono
parents:
diff changeset
430
kono
parents:
diff changeset
431 begin
kono
parents:
diff changeset
432 Iterate (Tree.Root);
kono
parents:
diff changeset
433 end Generic_Iteration;
kono
parents:
diff changeset
434
kono
parents:
diff changeset
435 -------------------------------
kono
parents:
diff changeset
436 -- Generic_Reverse_Iteration --
kono
parents:
diff changeset
437 -------------------------------
kono
parents:
diff changeset
438
kono
parents:
diff changeset
439 procedure Generic_Reverse_Iteration
kono
parents:
diff changeset
440 (Tree : Tree_Type'Class;
kono
parents:
diff changeset
441 Key : Key_Type)
kono
parents:
diff changeset
442 is
kono
parents:
diff changeset
443 procedure Iterate (Index : Count_Type);
kono
parents:
diff changeset
444
kono
parents:
diff changeset
445 -------------
kono
parents:
diff changeset
446 -- Iterate --
kono
parents:
diff changeset
447 -------------
kono
parents:
diff changeset
448
kono
parents:
diff changeset
449 procedure Iterate (Index : Count_Type) is
kono
parents:
diff changeset
450 J : Count_Type;
kono
parents:
diff changeset
451 N : Nodes_Type renames Tree.Nodes;
kono
parents:
diff changeset
452
kono
parents:
diff changeset
453 begin
kono
parents:
diff changeset
454 J := Index;
kono
parents:
diff changeset
455 while J /= 0 loop
kono
parents:
diff changeset
456 if Is_Less_Key_Node (Key, N (J)) then
kono
parents:
diff changeset
457 J := Ops.Left (N (J));
kono
parents:
diff changeset
458 elsif Is_Greater_Key_Node (Key, N (J)) then
kono
parents:
diff changeset
459 J := Ops.Right (N (J));
kono
parents:
diff changeset
460 else
kono
parents:
diff changeset
461 Iterate (Ops.Right (N (J)));
kono
parents:
diff changeset
462 Process (J);
kono
parents:
diff changeset
463 J := Ops.Left (N (J));
kono
parents:
diff changeset
464 end if;
kono
parents:
diff changeset
465 end loop;
kono
parents:
diff changeset
466 end Iterate;
kono
parents:
diff changeset
467
kono
parents:
diff changeset
468 -- Start of processing for Generic_Reverse_Iteration
kono
parents:
diff changeset
469
kono
parents:
diff changeset
470 begin
kono
parents:
diff changeset
471 Iterate (Tree.Root);
kono
parents:
diff changeset
472 end Generic_Reverse_Iteration;
kono
parents:
diff changeset
473
kono
parents:
diff changeset
474 ----------------------------------
kono
parents:
diff changeset
475 -- Generic_Unconditional_Insert --
kono
parents:
diff changeset
476 ----------------------------------
kono
parents:
diff changeset
477
kono
parents:
diff changeset
478 procedure Generic_Unconditional_Insert
kono
parents:
diff changeset
479 (Tree : in out Tree_Type'Class;
kono
parents:
diff changeset
480 Key : Key_Type;
kono
parents:
diff changeset
481 Node : out Count_Type)
kono
parents:
diff changeset
482 is
kono
parents:
diff changeset
483 Y : Count_Type;
kono
parents:
diff changeset
484 X : Count_Type;
kono
parents:
diff changeset
485 N : Nodes_Type renames Tree.Nodes;
kono
parents:
diff changeset
486
kono
parents:
diff changeset
487 Before : Boolean;
kono
parents:
diff changeset
488
kono
parents:
diff changeset
489 begin
kono
parents:
diff changeset
490 Y := 0;
kono
parents:
diff changeset
491 Before := False;
kono
parents:
diff changeset
492
kono
parents:
diff changeset
493 X := Tree.Root;
kono
parents:
diff changeset
494 while X /= 0 loop
kono
parents:
diff changeset
495 Y := X;
kono
parents:
diff changeset
496 Before := Is_Less_Key_Node (Key, N (X));
kono
parents:
diff changeset
497 X := (if Before then Ops.Left (N (X)) else Ops.Right (N (X)));
kono
parents:
diff changeset
498 end loop;
kono
parents:
diff changeset
499
kono
parents:
diff changeset
500 Insert_Post (Tree, Y, Before, Node);
kono
parents:
diff changeset
501 end Generic_Unconditional_Insert;
kono
parents:
diff changeset
502
kono
parents:
diff changeset
503 --------------------------------------------
kono
parents:
diff changeset
504 -- Generic_Unconditional_Insert_With_Hint --
kono
parents:
diff changeset
505 --------------------------------------------
kono
parents:
diff changeset
506
kono
parents:
diff changeset
507 procedure Generic_Unconditional_Insert_With_Hint
kono
parents:
diff changeset
508 (Tree : in out Tree_Type'Class;
kono
parents:
diff changeset
509 Hint : Count_Type;
kono
parents:
diff changeset
510 Key : Key_Type;
kono
parents:
diff changeset
511 Node : out Count_Type)
kono
parents:
diff changeset
512 is
kono
parents:
diff changeset
513 N : Nodes_Type renames Tree.Nodes;
kono
parents:
diff changeset
514
kono
parents:
diff changeset
515 begin
kono
parents:
diff changeset
516 -- There are fewer constraints for an unconditional insertion
kono
parents:
diff changeset
517 -- than for a conditional insertion, since we allow duplicate
kono
parents:
diff changeset
518 -- keys. So instead of having to check (say) whether Key is
kono
parents:
diff changeset
519 -- (strictly) greater than the hint's previous neighbor, here we
kono
parents:
diff changeset
520 -- allow Key to be equal to or greater than the previous node.
kono
parents:
diff changeset
521
kono
parents:
diff changeset
522 -- There is the issue of what to do if Key is equivalent to the
kono
parents:
diff changeset
523 -- hint. Does the new node get inserted before or after the hint?
kono
parents:
diff changeset
524 -- We decide that it gets inserted after the hint, reasoning that
kono
parents:
diff changeset
525 -- this is consistent with behavior for non-hint insertion, which
kono
parents:
diff changeset
526 -- inserts a new node after existing nodes with equivalent keys.
kono
parents:
diff changeset
527
kono
parents:
diff changeset
528 -- First we check whether the hint is null, which is interpreted
kono
parents:
diff changeset
529 -- to mean that Key is large relative to existing nodes.
kono
parents:
diff changeset
530 -- Following our rule above, if Key is equal to or greater than
kono
parents:
diff changeset
531 -- the last node, then we insert the new node immediately after
kono
parents:
diff changeset
532 -- last. (We don't have an operation for testing whether a key is
kono
parents:
diff changeset
533 -- "equal to or greater than" a node, so we must say instead "not
kono
parents:
diff changeset
534 -- less than", which is equivalent.)
kono
parents:
diff changeset
535
kono
parents:
diff changeset
536 if Hint = 0 then -- largest
kono
parents:
diff changeset
537 if Tree.Last = 0 then
kono
parents:
diff changeset
538 Insert_Post (Tree, 0, False, Node);
kono
parents:
diff changeset
539 elsif Is_Less_Key_Node (Key, N (Tree.Last)) then
kono
parents:
diff changeset
540 Unconditional_Insert_Sans_Hint (Tree, Key, Node);
kono
parents:
diff changeset
541 else
kono
parents:
diff changeset
542 Insert_Post (Tree, Tree.Last, False, Node);
kono
parents:
diff changeset
543 end if;
kono
parents:
diff changeset
544
kono
parents:
diff changeset
545 return;
kono
parents:
diff changeset
546 end if;
kono
parents:
diff changeset
547
kono
parents:
diff changeset
548 pragma Assert (Tree.Length > 0);
kono
parents:
diff changeset
549
kono
parents:
diff changeset
550 -- We decide here whether to insert the new node prior to the
kono
parents:
diff changeset
551 -- hint. Key could be equivalent to the hint, so in theory we
kono
parents:
diff changeset
552 -- could write the following test as "not greater than" (same as
kono
parents:
diff changeset
553 -- "less than or equal to"). If Key were equivalent to the hint,
kono
parents:
diff changeset
554 -- that would mean that the new node gets inserted before an
kono
parents:
diff changeset
555 -- equivalent node. That wouldn't break any container invariants,
kono
parents:
diff changeset
556 -- but our rule above says that new nodes always get inserted
kono
parents:
diff changeset
557 -- after equivalent nodes. So here we test whether Key is both
kono
parents:
diff changeset
558 -- less than the hint and equal to or greater than the hint's
kono
parents:
diff changeset
559 -- previous neighbor, and if so insert it before the hint.
kono
parents:
diff changeset
560
kono
parents:
diff changeset
561 if Is_Less_Key_Node (Key, N (Hint)) then
kono
parents:
diff changeset
562 declare
kono
parents:
diff changeset
563 Before : constant Count_Type := Ops.Previous (Tree, Hint);
kono
parents:
diff changeset
564 begin
kono
parents:
diff changeset
565 if Before = 0 then
kono
parents:
diff changeset
566 Insert_Post (Tree, Hint, True, Node);
kono
parents:
diff changeset
567 elsif Is_Less_Key_Node (Key, N (Before)) then
kono
parents:
diff changeset
568 Unconditional_Insert_Sans_Hint (Tree, Key, Node);
kono
parents:
diff changeset
569 elsif Ops.Right (N (Before)) = 0 then
kono
parents:
diff changeset
570 Insert_Post (Tree, Before, False, Node);
kono
parents:
diff changeset
571 else
kono
parents:
diff changeset
572 Insert_Post (Tree, Hint, True, Node);
kono
parents:
diff changeset
573 end if;
kono
parents:
diff changeset
574 end;
kono
parents:
diff changeset
575
kono
parents:
diff changeset
576 return;
kono
parents:
diff changeset
577 end if;
kono
parents:
diff changeset
578
kono
parents:
diff changeset
579 -- We know that Key isn't less than the hint, so it must be equal
kono
parents:
diff changeset
580 -- or greater. So we just test whether Key is less than or equal
kono
parents:
diff changeset
581 -- to (same as "not greater than") the hint's next neighbor, and
kono
parents:
diff changeset
582 -- if so insert it after the hint.
kono
parents:
diff changeset
583
kono
parents:
diff changeset
584 declare
kono
parents:
diff changeset
585 After : constant Count_Type := Ops.Next (Tree, Hint);
kono
parents:
diff changeset
586 begin
kono
parents:
diff changeset
587 if After = 0 then
kono
parents:
diff changeset
588 Insert_Post (Tree, Hint, False, Node);
kono
parents:
diff changeset
589 elsif Is_Greater_Key_Node (Key, N (After)) then
kono
parents:
diff changeset
590 Unconditional_Insert_Sans_Hint (Tree, Key, Node);
kono
parents:
diff changeset
591 elsif Ops.Right (N (Hint)) = 0 then
kono
parents:
diff changeset
592 Insert_Post (Tree, Hint, False, Node);
kono
parents:
diff changeset
593 else
kono
parents:
diff changeset
594 Insert_Post (Tree, After, True, Node);
kono
parents:
diff changeset
595 end if;
kono
parents:
diff changeset
596 end;
kono
parents:
diff changeset
597 end Generic_Unconditional_Insert_With_Hint;
kono
parents:
diff changeset
598
kono
parents:
diff changeset
599 -----------------
kono
parents:
diff changeset
600 -- Upper_Bound --
kono
parents:
diff changeset
601 -----------------
kono
parents:
diff changeset
602
kono
parents:
diff changeset
603 function Upper_Bound
kono
parents:
diff changeset
604 (Tree : Tree_Type'Class;
kono
parents:
diff changeset
605 Key : Key_Type) return Count_Type
kono
parents:
diff changeset
606 is
kono
parents:
diff changeset
607 Y : Count_Type;
kono
parents:
diff changeset
608 X : Count_Type;
kono
parents:
diff changeset
609 N : Nodes_Type renames Tree.Nodes;
kono
parents:
diff changeset
610
kono
parents:
diff changeset
611 begin
kono
parents:
diff changeset
612 Y := 0;
kono
parents:
diff changeset
613
kono
parents:
diff changeset
614 X := Tree.Root;
kono
parents:
diff changeset
615 while X /= 0 loop
kono
parents:
diff changeset
616 if Is_Less_Key_Node (Key, N (X)) then
kono
parents:
diff changeset
617 Y := X;
kono
parents:
diff changeset
618 X := Ops.Left (N (X));
kono
parents:
diff changeset
619 else
kono
parents:
diff changeset
620 X := Ops.Right (N (X));
kono
parents:
diff changeset
621 end if;
kono
parents:
diff changeset
622 end loop;
kono
parents:
diff changeset
623
kono
parents:
diff changeset
624 return Y;
kono
parents:
diff changeset
625 end Upper_Bound;
kono
parents:
diff changeset
626
kono
parents:
diff changeset
627 end Ada.Containers.Red_Black_Trees.Generic_Bounded_Keys;