1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.RED_BLACK_TREES.GENERIC_KEYS --
9 -- Copyright (C) 2004-2008, Free Software Foundation, Inc. --
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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- This unit was originally developed by Matthew J Heaney. --
30 ------------------------------------------------------------------------------
32 package body Ada.Containers.Red_Black_Trees.Generic_Keys is
34 package Ops renames Tree_Operations;
42 function Ceiling (Tree : Tree_Type; Key : Key_Type) return Node_Access is
49 if Is_Greater_Key_Node (Key, X) then
64 function Find (Tree : Tree_Type; Key : Key_Type) return Node_Access is
71 if Is_Greater_Key_Node (Key, X) then
83 if Is_Less_Key_Node (Key, Y) then
94 function Floor (Tree : Tree_Type; Key : Key_Type) return Node_Access is
101 if Is_Less_Key_Node (Key, X) then
112 --------------------------------
113 -- Generic_Conditional_Insert --
114 --------------------------------
116 procedure Generic_Conditional_Insert
117 (Tree : in out Tree_Type;
119 Node : out Node_Access;
120 Inserted : out Boolean)
122 Y : Node_Access := null;
123 X : Node_Access := Tree.Root;
129 Inserted := Is_Less_Key_Node (Key, X);
138 -- If Inserted is True, then this means either that Tree is
139 -- empty, or there was a least one node (strictly) greater than
140 -- Key. Otherwise, it means that Key is equal to or greater than
144 if Y = Tree.First then
145 Insert_Post (Tree, Y, True, Node);
149 Node := Ops.Previous (Y);
155 -- Here Node has a value that is less than or equal to Key. We
156 -- now have to resolve whether Key is equal to or greater than
157 -- Node, which determines whether the insertion succeeds.
159 if Is_Greater_Key_Node (Key, Node) then
160 Insert_Post (Tree, Y, Inserted, Node);
166 end Generic_Conditional_Insert;
168 ------------------------------------------
169 -- Generic_Conditional_Insert_With_Hint --
170 ------------------------------------------
172 procedure Generic_Conditional_Insert_With_Hint
173 (Tree : in out Tree_Type;
174 Position : Node_Access;
176 Node : out Node_Access;
177 Inserted : out Boolean)
180 -- The purpose of a hint is to avoid a search from the root of
181 -- tree. If we have it hint it means we only need to traverse the
182 -- subtree rooted at the hint to find the nearest neighbor. Note
183 -- that finding the neighbor means merely walking the tree; this
184 -- is not a search and the only comparisons that occur are with
185 -- the hint and its neighbor.
187 -- If Position is null, this is interpreted to mean that Key is
188 -- large relative to the nodes in the tree. If the tree is empty,
189 -- or Key is greater than the last node in the tree, then we're
190 -- done; otherwise the hint was "wrong" and we must search.
192 if Position = null then -- largest
194 or else Is_Greater_Key_Node (Key, Tree.Last)
196 Insert_Post (Tree, Tree.Last, False, Node);
199 Conditional_Insert_Sans_Hint (Tree, Key, Node, Inserted);
205 pragma Assert (Tree.Length > 0);
207 -- A hint can either name the node that immediately follows Key,
208 -- or immediately precedes Key. We first test whether Key is
209 -- less than the hint, and if so we compare Key to the node that
210 -- precedes the hint. If Key is both less than the hint and
211 -- greater than the hint's preceding neighbor, then we're done;
212 -- otherwise we must search.
214 -- Note also that a hint can either be an anterior node or a leaf
215 -- node. A new node is always inserted at the bottom of the tree
216 -- (at least prior to rebalancing), becoming the new left or
217 -- right child of leaf node (which prior to the insertion must
218 -- necessarily be null, since this is a leaf). If the hint names
219 -- an anterior node then its neighbor must be a leaf, and so
220 -- (here) we insert after the neighbor. If the hint names a leaf
221 -- then its neighbor must be anterior and so we insert before the
224 if Is_Less_Key_Node (Key, Position) then
226 Before : constant Node_Access := Ops.Previous (Position);
229 if Before = null then
230 Insert_Post (Tree, Tree.First, True, Node);
233 elsif Is_Greater_Key_Node (Key, Before) then
234 if Ops.Right (Before) = null then
235 Insert_Post (Tree, Before, False, Node);
237 Insert_Post (Tree, Position, True, Node);
243 Conditional_Insert_Sans_Hint (Tree, Key, Node, Inserted);
250 -- We know that Key isn't less than the hint so we try again,
251 -- this time to see if it's greater than the hint. If so we
252 -- compare Key to the node that follows the hint. If Key is both
253 -- greater than the hint and less than the hint's next neighbor,
254 -- then we're done; otherwise we must search.
256 if Is_Greater_Key_Node (Key, Position) then
258 After : constant Node_Access := Ops.Next (Position);
262 Insert_Post (Tree, Tree.Last, False, Node);
265 elsif Is_Less_Key_Node (Key, After) then
266 if Ops.Right (Position) = null then
267 Insert_Post (Tree, Position, False, Node);
269 Insert_Post (Tree, After, True, Node);
275 Conditional_Insert_Sans_Hint (Tree, Key, Node, Inserted);
282 -- We know that Key is neither less than the hint nor greater
283 -- than the hint, and that's the definition of equivalence.
284 -- There's nothing else we need to do, since a search would just
285 -- reach the same conclusion.
289 end Generic_Conditional_Insert_With_Hint;
291 -------------------------
292 -- Generic_Insert_Post --
293 -------------------------
295 procedure Generic_Insert_Post
296 (Tree : in out Tree_Type;
302 if Tree.Length = Count_Type'Last then
303 raise Constraint_Error with "too many elements";
306 if Tree.Busy > 0 then
307 raise Program_Error with
308 "attempt to tamper with cursors (container is busy)";
312 pragma Assert (Z /= null);
313 pragma Assert (Ops.Color (Z) = Red);
316 pragma Assert (Tree.Length = 0);
317 pragma Assert (Tree.Root = null);
318 pragma Assert (Tree.First = null);
319 pragma Assert (Tree.Last = null);
326 pragma Assert (Ops.Left (Y) = null);
330 if Y = Tree.First then
335 pragma Assert (Ops.Right (Y) = null);
337 Ops.Set_Right (Y, Z);
339 if Y = Tree.Last then
344 Ops.Set_Parent (Z, Y);
345 Ops.Rebalance_For_Insert (Tree, Z);
346 Tree.Length := Tree.Length + 1;
347 end Generic_Insert_Post;
349 -----------------------
350 -- Generic_Iteration --
351 -----------------------
353 procedure Generic_Iteration
357 procedure Iterate (Node : Node_Access);
363 procedure Iterate (Node : Node_Access) is
368 if Is_Less_Key_Node (Key, N) then
370 elsif Is_Greater_Key_Node (Key, N) then
373 Iterate (Ops.Left (N));
380 -- Start of processing for Generic_Iteration
384 end Generic_Iteration;
386 -------------------------------
387 -- Generic_Reverse_Iteration --
388 -------------------------------
390 procedure Generic_Reverse_Iteration
394 procedure Iterate (Node : Node_Access);
400 procedure Iterate (Node : Node_Access) is
405 if Is_Less_Key_Node (Key, N) then
407 elsif Is_Greater_Key_Node (Key, N) then
410 Iterate (Ops.Right (N));
417 -- Start of processing for Generic_Reverse_Iteration
421 end Generic_Reverse_Iteration;
423 ----------------------------------
424 -- Generic_Unconditional_Insert --
425 ----------------------------------
427 procedure Generic_Unconditional_Insert
428 (Tree : in out Tree_Type;
430 Node : out Node_Access)
444 Before := Is_Less_Key_Node (Key, X);
453 Insert_Post (Tree, Y, Before, Node);
454 end Generic_Unconditional_Insert;
456 --------------------------------------------
457 -- Generic_Unconditional_Insert_With_Hint --
458 --------------------------------------------
460 procedure Generic_Unconditional_Insert_With_Hint
461 (Tree : in out Tree_Type;
464 Node : out Node_Access)
467 -- There are fewer constraints for an unconditional insertion
468 -- than for a conditional insertion, since we allow duplicate
469 -- keys. So instead of having to check (say) whether Key is
470 -- (strictly) greater than the hint's previous neighbor, here we
471 -- allow Key to be equal to or greater than the previous node.
473 -- There is the issue of what to do if Key is equivalent to the
474 -- hint. Does the new node get inserted before or after the hint?
475 -- We decide that it gets inserted after the hint, reasoning that
476 -- this is consistent with behavior for non-hint insertion, which
477 -- inserts a new node after existing nodes with equivalent keys.
479 -- First we check whether the hint is null, which is interpreted
480 -- to mean that Key is large relative to existing nodes.
481 -- Following our rule above, if Key is equal to or greater than
482 -- the last node, then we insert the new node immediately after
483 -- last. (We don't have an operation for testing whether a key is
484 -- "equal to or greater than" a node, so we must say instead "not
485 -- less than", which is equivalent.)
487 if Hint = null then -- largest
488 if Tree.Last = null then
489 Insert_Post (Tree, null, False, Node);
490 elsif Is_Less_Key_Node (Key, Tree.Last) then
491 Unconditional_Insert_Sans_Hint (Tree, Key, Node);
493 Insert_Post (Tree, Tree.Last, False, Node);
499 pragma Assert (Tree.Length > 0);
501 -- We decide here whether to insert the new node prior to the
502 -- hint. Key could be equivalent to the hint, so in theory we
503 -- could write the following test as "not greater than" (same as
504 -- "less than or equal to"). If Key were equivalent to the hint,
505 -- that would mean that the new node gets inserted before an
506 -- equivalent node. That wouldn't break any container invariants,
507 -- but our rule above says that new nodes always get inserted
508 -- after equivalent nodes. So here we test whether Key is both
509 -- less than the hint and equal to or greater than the hint's
510 -- previous neighbor, and if so insert it before the hint.
512 if Is_Less_Key_Node (Key, Hint) then
514 Before : constant Node_Access := Ops.Previous (Hint);
516 if Before = null then
517 Insert_Post (Tree, Hint, True, Node);
518 elsif Is_Less_Key_Node (Key, Before) then
519 Unconditional_Insert_Sans_Hint (Tree, Key, Node);
520 elsif Ops.Right (Before) = null then
521 Insert_Post (Tree, Before, False, Node);
523 Insert_Post (Tree, Hint, True, Node);
530 -- We know that Key isn't less than the hint, so it must be equal
531 -- or greater. So we just test whether Key is less than or equal
532 -- to (same as "not greater than") the hint's next neighbor, and
533 -- if so insert it after the hint.
536 After : constant Node_Access := Ops.Next (Hint);
539 Insert_Post (Tree, Hint, False, Node);
540 elsif Is_Greater_Key_Node (Key, After) then
541 Unconditional_Insert_Sans_Hint (Tree, Key, Node);
542 elsif Ops.Right (Hint) = null then
543 Insert_Post (Tree, Hint, False, Node);
545 Insert_Post (Tree, After, True, Node);
548 end Generic_Unconditional_Insert_With_Hint;
556 Key : Key_Type) return Node_Access
564 if Is_Less_Key_Node (Key, X) then
575 end Ada.Containers.Red_Black_Trees.Generic_Keys;