1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.RED_BLACK_TREES.GENERIC_KEYS --
9 -- Copyright (C) 2004-2009, 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 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- This unit was originally developed by Matthew J Heaney. --
28 ------------------------------------------------------------------------------
30 package body Ada.Containers.Red_Black_Trees.Generic_Keys is
32 package Ops renames Tree_Operations;
40 function Ceiling (Tree : Tree_Type; Key : Key_Type) return Node_Access is
47 if Is_Greater_Key_Node (Key, X) then
62 function Find (Tree : Tree_Type; Key : Key_Type) return Node_Access is
69 if Is_Greater_Key_Node (Key, X) then
81 if Is_Less_Key_Node (Key, Y) then
92 function Floor (Tree : Tree_Type; Key : Key_Type) return Node_Access is
99 if Is_Less_Key_Node (Key, X) then
110 --------------------------------
111 -- Generic_Conditional_Insert --
112 --------------------------------
114 procedure Generic_Conditional_Insert
115 (Tree : in out Tree_Type;
117 Node : out Node_Access;
118 Inserted : out Boolean)
120 Y : Node_Access := null;
121 X : Node_Access := Tree.Root;
127 Inserted := Is_Less_Key_Node (Key, X);
136 -- If Inserted is True, then this means either that Tree is
137 -- empty, or there was a least one node (strictly) greater than
138 -- Key. Otherwise, it means that Key is equal to or greater than
142 if Y = Tree.First then
143 Insert_Post (Tree, Y, True, Node);
147 Node := Ops.Previous (Y);
153 -- Here Node has a value that is less than or equal to Key. We
154 -- now have to resolve whether Key is equal to or greater than
155 -- Node, which determines whether the insertion succeeds.
157 if Is_Greater_Key_Node (Key, Node) then
158 Insert_Post (Tree, Y, Inserted, Node);
164 end Generic_Conditional_Insert;
166 ------------------------------------------
167 -- Generic_Conditional_Insert_With_Hint --
168 ------------------------------------------
170 procedure Generic_Conditional_Insert_With_Hint
171 (Tree : in out Tree_Type;
172 Position : Node_Access;
174 Node : out Node_Access;
175 Inserted : out Boolean)
178 -- The purpose of a hint is to avoid a search from the root of
179 -- tree. If we have it hint it means we only need to traverse the
180 -- subtree rooted at the hint to find the nearest neighbor. Note
181 -- that finding the neighbor means merely walking the tree; this
182 -- is not a search and the only comparisons that occur are with
183 -- the hint and its neighbor.
185 -- If Position is null, this is interpreted to mean that Key is
186 -- large relative to the nodes in the tree. If the tree is empty,
187 -- or Key is greater than the last node in the tree, then we're
188 -- done; otherwise the hint was "wrong" and we must search.
190 if Position = null then -- largest
192 or else Is_Greater_Key_Node (Key, Tree.Last)
194 Insert_Post (Tree, Tree.Last, False, Node);
197 Conditional_Insert_Sans_Hint (Tree, Key, Node, Inserted);
203 pragma Assert (Tree.Length > 0);
205 -- A hint can either name the node that immediately follows Key,
206 -- or immediately precedes Key. We first test whether Key is
207 -- less than the hint, and if so we compare Key to the node that
208 -- precedes the hint. If Key is both less than the hint and
209 -- greater than the hint's preceding neighbor, then we're done;
210 -- otherwise we must search.
212 -- Note also that a hint can either be an anterior node or a leaf
213 -- node. A new node is always inserted at the bottom of the tree
214 -- (at least prior to rebalancing), becoming the new left or
215 -- right child of leaf node (which prior to the insertion must
216 -- necessarily be null, since this is a leaf). If the hint names
217 -- an anterior node then its neighbor must be a leaf, and so
218 -- (here) we insert after the neighbor. If the hint names a leaf
219 -- then its neighbor must be anterior and so we insert before the
222 if Is_Less_Key_Node (Key, Position) then
224 Before : constant Node_Access := Ops.Previous (Position);
227 if Before = null then
228 Insert_Post (Tree, Tree.First, True, Node);
231 elsif Is_Greater_Key_Node (Key, Before) then
232 if Ops.Right (Before) = null then
233 Insert_Post (Tree, Before, False, Node);
235 Insert_Post (Tree, Position, True, Node);
241 Conditional_Insert_Sans_Hint (Tree, Key, Node, Inserted);
248 -- We know that Key isn't less than the hint so we try again,
249 -- this time to see if it's greater than the hint. If so we
250 -- compare Key to the node that follows the hint. If Key is both
251 -- greater than the hint and less than the hint's next neighbor,
252 -- then we're done; otherwise we must search.
254 if Is_Greater_Key_Node (Key, Position) then
256 After : constant Node_Access := Ops.Next (Position);
260 Insert_Post (Tree, Tree.Last, False, Node);
263 elsif Is_Less_Key_Node (Key, After) then
264 if Ops.Right (Position) = null then
265 Insert_Post (Tree, Position, False, Node);
267 Insert_Post (Tree, After, True, Node);
273 Conditional_Insert_Sans_Hint (Tree, Key, Node, Inserted);
280 -- We know that Key is neither less than the hint nor greater
281 -- than the hint, and that's the definition of equivalence.
282 -- There's nothing else we need to do, since a search would just
283 -- reach the same conclusion.
287 end Generic_Conditional_Insert_With_Hint;
289 -------------------------
290 -- Generic_Insert_Post --
291 -------------------------
293 procedure Generic_Insert_Post
294 (Tree : in out Tree_Type;
300 if Tree.Length = Count_Type'Last then
301 raise Constraint_Error with "too many elements";
304 if Tree.Busy > 0 then
305 raise Program_Error with
306 "attempt to tamper with cursors (container is busy)";
310 pragma Assert (Z /= null);
311 pragma Assert (Ops.Color (Z) = Red);
314 pragma Assert (Tree.Length = 0);
315 pragma Assert (Tree.Root = null);
316 pragma Assert (Tree.First = null);
317 pragma Assert (Tree.Last = null);
324 pragma Assert (Ops.Left (Y) = null);
328 if Y = Tree.First then
333 pragma Assert (Ops.Right (Y) = null);
335 Ops.Set_Right (Y, Z);
337 if Y = Tree.Last then
342 Ops.Set_Parent (Z, Y);
343 Ops.Rebalance_For_Insert (Tree, Z);
344 Tree.Length := Tree.Length + 1;
345 end Generic_Insert_Post;
347 -----------------------
348 -- Generic_Iteration --
349 -----------------------
351 procedure Generic_Iteration
355 procedure Iterate (Node : Node_Access);
361 procedure Iterate (Node : Node_Access) is
366 if Is_Less_Key_Node (Key, N) then
368 elsif Is_Greater_Key_Node (Key, N) then
371 Iterate (Ops.Left (N));
378 -- Start of processing for Generic_Iteration
382 end Generic_Iteration;
384 -------------------------------
385 -- Generic_Reverse_Iteration --
386 -------------------------------
388 procedure Generic_Reverse_Iteration
392 procedure Iterate (Node : Node_Access);
398 procedure Iterate (Node : Node_Access) is
403 if Is_Less_Key_Node (Key, N) then
405 elsif Is_Greater_Key_Node (Key, N) then
408 Iterate (Ops.Right (N));
415 -- Start of processing for Generic_Reverse_Iteration
419 end Generic_Reverse_Iteration;
421 ----------------------------------
422 -- Generic_Unconditional_Insert --
423 ----------------------------------
425 procedure Generic_Unconditional_Insert
426 (Tree : in out Tree_Type;
428 Node : out Node_Access)
442 Before := Is_Less_Key_Node (Key, X);
451 Insert_Post (Tree, Y, Before, Node);
452 end Generic_Unconditional_Insert;
454 --------------------------------------------
455 -- Generic_Unconditional_Insert_With_Hint --
456 --------------------------------------------
458 procedure Generic_Unconditional_Insert_With_Hint
459 (Tree : in out Tree_Type;
462 Node : out Node_Access)
465 -- There are fewer constraints for an unconditional insertion
466 -- than for a conditional insertion, since we allow duplicate
467 -- keys. So instead of having to check (say) whether Key is
468 -- (strictly) greater than the hint's previous neighbor, here we
469 -- allow Key to be equal to or greater than the previous node.
471 -- There is the issue of what to do if Key is equivalent to the
472 -- hint. Does the new node get inserted before or after the hint?
473 -- We decide that it gets inserted after the hint, reasoning that
474 -- this is consistent with behavior for non-hint insertion, which
475 -- inserts a new node after existing nodes with equivalent keys.
477 -- First we check whether the hint is null, which is interpreted
478 -- to mean that Key is large relative to existing nodes.
479 -- Following our rule above, if Key is equal to or greater than
480 -- the last node, then we insert the new node immediately after
481 -- last. (We don't have an operation for testing whether a key is
482 -- "equal to or greater than" a node, so we must say instead "not
483 -- less than", which is equivalent.)
485 if Hint = null then -- largest
486 if Tree.Last = null then
487 Insert_Post (Tree, null, False, Node);
488 elsif Is_Less_Key_Node (Key, Tree.Last) then
489 Unconditional_Insert_Sans_Hint (Tree, Key, Node);
491 Insert_Post (Tree, Tree.Last, False, Node);
497 pragma Assert (Tree.Length > 0);
499 -- We decide here whether to insert the new node prior to the
500 -- hint. Key could be equivalent to the hint, so in theory we
501 -- could write the following test as "not greater than" (same as
502 -- "less than or equal to"). If Key were equivalent to the hint,
503 -- that would mean that the new node gets inserted before an
504 -- equivalent node. That wouldn't break any container invariants,
505 -- but our rule above says that new nodes always get inserted
506 -- after equivalent nodes. So here we test whether Key is both
507 -- less than the hint and equal to or greater than the hint's
508 -- previous neighbor, and if so insert it before the hint.
510 if Is_Less_Key_Node (Key, Hint) then
512 Before : constant Node_Access := Ops.Previous (Hint);
514 if Before = null then
515 Insert_Post (Tree, Hint, True, Node);
516 elsif Is_Less_Key_Node (Key, Before) then
517 Unconditional_Insert_Sans_Hint (Tree, Key, Node);
518 elsif Ops.Right (Before) = null then
519 Insert_Post (Tree, Before, False, Node);
521 Insert_Post (Tree, Hint, True, Node);
528 -- We know that Key isn't less than the hint, so it must be equal
529 -- or greater. So we just test whether Key is less than or equal
530 -- to (same as "not greater than") the hint's next neighbor, and
531 -- if so insert it after the hint.
534 After : constant Node_Access := Ops.Next (Hint);
537 Insert_Post (Tree, Hint, False, Node);
538 elsif Is_Greater_Key_Node (Key, After) then
539 Unconditional_Insert_Sans_Hint (Tree, Key, Node);
540 elsif Ops.Right (Hint) = null then
541 Insert_Post (Tree, Hint, False, Node);
543 Insert_Post (Tree, After, True, Node);
546 end Generic_Unconditional_Insert_With_Hint;
554 Key : Key_Type) return Node_Access
562 if Is_Less_Key_Node (Key, X) then
573 end Ada.Containers.Red_Black_Trees.Generic_Keys;