1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . R E D _ B L A C K _ T R E E S . --
6 -- G E N E R I C _ K E Y S --
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 was originally developed by Matthew J Heaney. --
31 ------------------------------------------------------------------------------
33 package body Ada.Containers.Red_Black_Trees.Generic_Keys is
35 package Ops renames Tree_Operations;
43 function Ceiling (Tree : Tree_Type; Key : Key_Type) return Node_Access is
50 if Is_Greater_Key_Node (Key, X) then
65 function Find (Tree : Tree_Type; Key : Key_Type) return Node_Access is
72 if Is_Greater_Key_Node (Key, X) then
84 if Is_Less_Key_Node (Key, Y) then
95 function Floor (Tree : Tree_Type; Key : Key_Type) return Node_Access is
102 if Is_Less_Key_Node (Key, X) then
113 --------------------------------
114 -- Generic_Conditional_Insert --
115 --------------------------------
117 procedure Generic_Conditional_Insert
118 (Tree : in out Tree_Type;
120 Node : out Node_Access;
121 Inserted : out Boolean)
123 Y : Node_Access := null;
124 X : Node_Access := Tree.Root;
130 Inserted := Is_Less_Key_Node (Key, X);
139 -- If Inserted is True, then this means either that Tree is
140 -- empty, or there was a least one node (strictly) greater than
141 -- Key. Otherwise, it means that Key is equal to or greater than
145 if Y = Tree.First then
146 Insert_Post (Tree, Y, True, Node);
150 Node := Ops.Previous (Y);
156 -- Here Node has a value that is less than or equal to Key. We
157 -- now have to resolve whether Key is equal to or greater than
158 -- Node, which determines whether the insertion succeeds.
160 if Is_Greater_Key_Node (Key, Node) then
161 Insert_Post (Tree, Y, Inserted, Node);
167 end Generic_Conditional_Insert;
169 ------------------------------------------
170 -- Generic_Conditional_Insert_With_Hint --
171 ------------------------------------------
173 procedure Generic_Conditional_Insert_With_Hint
174 (Tree : in out Tree_Type;
175 Position : Node_Access;
177 Node : out Node_Access;
178 Inserted : out Boolean)
181 -- The purpose of a hint is to avoid a search from the root of
182 -- tree. If we have it hint it means we only need to traverse the
183 -- subtree rooted at the hint to find the nearest neighbor. Note
184 -- that finding the neighbor means merely walking the tree; this
185 -- is not a search and the only comparisons that occur are with
186 -- the hint and its neighbor.
188 -- If Position is null, this is intepreted to mean that Key is
189 -- large relative to the nodes in the tree. If the tree is empty,
190 -- or Key is greater than the last node in the tree, then we're
191 -- done; otherwise the hint was "wrong" and we must search.
193 if Position = null then -- largest
195 or else Is_Greater_Key_Node (Key, Tree.Last)
197 Insert_Post (Tree, Tree.Last, False, Node);
200 Conditional_Insert_Sans_Hint (Tree, Key, Node, Inserted);
206 pragma Assert (Tree.Length > 0);
208 -- A hint can either name the node that immediately follows Key,
209 -- or immediately precedes Key. We first test whether Key is is
210 -- less than the hint, and if so we compare Key to the node that
211 -- precedes the hint. If Key is both less than the hint and
212 -- greater than the hint's preceding neighbor, then we're done;
213 -- otherwise we must search.
215 -- Note also that a hint can either be an anterior node or a leaf
216 -- node. A new node is always inserted at the bottom of the tree
217 -- (at least prior to rebalancing), becoming the new left or
218 -- right child of leaf node (which prior to the insertion must
219 -- necessarily be null, since this is a leaf). If the hint names
220 -- an anterior node then its neighbor must be a leaf, and so
221 -- (here) we insert after the neighbor. If the hint names a leaf
222 -- then its neighbor must be anterior and so we insert before the
225 if Is_Less_Key_Node (Key, Position) then
227 Before : constant Node_Access := Ops.Previous (Position);
230 if Before = null then
231 Insert_Post (Tree, Tree.First, True, Node);
234 elsif Is_Greater_Key_Node (Key, Before) then
235 if Ops.Right (Before) = null then
236 Insert_Post (Tree, Before, False, Node);
238 Insert_Post (Tree, Position, True, Node);
244 Conditional_Insert_Sans_Hint (Tree, Key, Node, Inserted);
251 -- We know that Key isn't less than the hint so we try again,
252 -- this time to see if it's greater than the hint. If so we
253 -- compare Key to the node that follows the hint. If Key is both
254 -- greater than the hint and less than the hint's next neighbor,
255 -- then we're done; otherwise we must search.
257 if Is_Greater_Key_Node (Key, Position) then
259 After : constant Node_Access := Ops.Next (Position);
263 Insert_Post (Tree, Tree.Last, False, Node);
266 elsif Is_Less_Key_Node (Key, After) then
267 if Ops.Right (Position) = null then
268 Insert_Post (Tree, Position, False, Node);
270 Insert_Post (Tree, After, True, Node);
276 Conditional_Insert_Sans_Hint (Tree, Key, Node, Inserted);
283 -- We know that Key is neither less than the hint nor greater
284 -- than the hint, and that's the definition of equivalence.
285 -- There's nothing else we need to do, since a search would just
286 -- reach the same conclusion.
290 end Generic_Conditional_Insert_With_Hint;
292 -------------------------
293 -- Generic_Insert_Post --
294 -------------------------
296 procedure Generic_Insert_Post
297 (Tree : in out Tree_Type;
303 if Tree.Length = Count_Type'Last then
304 raise Constraint_Error with "too many elements";
307 if Tree.Busy > 0 then
308 raise Program_Error with
309 "attempt to tamper with cursors (container is busy)";
313 pragma Assert (Z /= null);
314 pragma Assert (Ops.Color (Z) = Red);
317 pragma Assert (Tree.Length = 0);
318 pragma Assert (Tree.Root = null);
319 pragma Assert (Tree.First = null);
320 pragma Assert (Tree.Last = null);
327 pragma Assert (Ops.Left (Y) = null);
331 if Y = Tree.First then
336 pragma Assert (Ops.Right (Y) = null);
338 Ops.Set_Right (Y, Z);
340 if Y = Tree.Last then
345 Ops.Set_Parent (Z, Y);
346 Ops.Rebalance_For_Insert (Tree, Z);
347 Tree.Length := Tree.Length + 1;
348 end Generic_Insert_Post;
350 -----------------------
351 -- Generic_Iteration --
352 -----------------------
354 procedure Generic_Iteration
358 procedure Iterate (Node : Node_Access);
364 procedure Iterate (Node : Node_Access) is
369 if Is_Less_Key_Node (Key, N) then
371 elsif Is_Greater_Key_Node (Key, N) then
374 Iterate (Ops.Left (N));
381 -- Start of processing for Generic_Iteration
385 end Generic_Iteration;
387 -------------------------------
388 -- Generic_Reverse_Iteration --
389 -------------------------------
391 procedure Generic_Reverse_Iteration
395 procedure Iterate (Node : Node_Access);
401 procedure Iterate (Node : Node_Access) is
406 if Is_Less_Key_Node (Key, N) then
408 elsif Is_Greater_Key_Node (Key, N) then
411 Iterate (Ops.Right (N));
418 -- Start of processing for Generic_Reverse_Iteration
422 end Generic_Reverse_Iteration;
424 ----------------------------------
425 -- Generic_Unconditional_Insert --
426 ----------------------------------
428 procedure Generic_Unconditional_Insert
429 (Tree : in out Tree_Type;
431 Node : out Node_Access)
445 Before := Is_Less_Key_Node (Key, X);
454 Insert_Post (Tree, Y, Before, Node);
455 end Generic_Unconditional_Insert;
457 --------------------------------------------
458 -- Generic_Unconditional_Insert_With_Hint --
459 --------------------------------------------
461 procedure Generic_Unconditional_Insert_With_Hint
462 (Tree : in out Tree_Type;
465 Node : out Node_Access)
468 -- There are fewer constraints for an unconditional insertion
469 -- than for a conditional insertion, since we allow duplicate
470 -- keys. So instead of having to check (say) whether Key is
471 -- (strictly) greater than the hint's previous neighbor, here we
472 -- allow Key to be equal to or greater than the previous node.
474 -- There is the issue of what to do if Key is equivalent to the
475 -- hint. Does the new node get inserted before or after the hint?
476 -- We decide that it gets inserted after the hint, reasoning that
477 -- this is consistent with behavior for non-hint insertion, which
478 -- inserts a new node after existing nodes with equivalent keys.
480 -- First we check whether the hint is null, which is interpreted
481 -- to mean that Key is large relative to existing nodes.
482 -- Following our rule above, if Key is equal to or greater than
483 -- the last node, then we insert the new node immediately after
484 -- last. (We don't have an operation for testing whether a key is
485 -- "equal to or greater than" a node, so we must say instead "not
486 -- less than", which is equivalent.)
488 if Hint = null then -- largest
489 if Tree.Last = null then
490 Insert_Post (Tree, null, False, Node);
491 elsif Is_Less_Key_Node (Key, Tree.Last) then
492 Unconditional_Insert_Sans_Hint (Tree, Key, Node);
494 Insert_Post (Tree, Tree.Last, False, Node);
500 pragma Assert (Tree.Length > 0);
502 -- We decide here whether to insert the new node prior to the
503 -- hint. Key could be equivalent to the hint, so in theory we
504 -- could write the following test as "not greater than" (same as
505 -- "less than or equal to"). If Key were equivalent to the hint,
506 -- that would mean that the new node gets inserted before an
507 -- equivalent node. That wouldn't break any container invariants,
508 -- but our rule above says that new nodes always get inserted
509 -- after equivalent nodes. So here we test whether Key is both
510 -- less than the hint and and equal to or greater than the hint's
511 -- previous neighbor, and if so insert it before the hint.
513 if Is_Less_Key_Node (Key, Hint) then
515 Before : constant Node_Access := Ops.Previous (Hint);
517 if Before = null then
518 Insert_Post (Tree, Hint, True, Node);
519 elsif Is_Less_Key_Node (Key, Before) then
520 Unconditional_Insert_Sans_Hint (Tree, Key, Node);
521 elsif Ops.Right (Before) = null then
522 Insert_Post (Tree, Before, False, Node);
524 Insert_Post (Tree, Hint, True, Node);
531 -- We know that Key isn't less than the hint, so it must be equal
532 -- or greater. So we just test whether Key is less than or equal
533 -- to (same as "not greater than") the hint's next neighbor, and
534 -- if so insert it after the hint.
537 After : constant Node_Access := Ops.Next (Hint);
540 Insert_Post (Tree, Hint, False, Node);
541 elsif Is_Greater_Key_Node (Key, After) then
542 Unconditional_Insert_Sans_Hint (Tree, Key, Node);
543 elsif Ops.Right (Hint) = null then
544 Insert_Post (Tree, Hint, False, Node);
546 Insert_Post (Tree, After, True, Node);
549 end Generic_Unconditional_Insert_With_Hint;
557 Key : Key_Type) return Node_Access
565 if Is_Less_Key_Node (Key, X) then
576 end Ada.Containers.Red_Black_Trees.Generic_Keys;