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
);
128 X
:= (if Inserted
then Ops
.Left
(X
) else Ops
.Right
(X
));
131 -- If Inserted is True, then this means either that Tree is
132 -- empty, or there was a least one node (strictly) greater than
133 -- Key. Otherwise, it means that Key is equal to or greater than
137 if Y
= Tree
.First
then
138 Insert_Post
(Tree
, Y
, True, Node
);
142 Node
:= Ops
.Previous
(Y
);
148 -- Here Node has a value that is less than or equal to Key. We
149 -- now have to resolve whether Key is equal to or greater than
150 -- Node, which determines whether the insertion succeeds.
152 if Is_Greater_Key_Node
(Key
, Node
) then
153 Insert_Post
(Tree
, Y
, Inserted
, Node
);
159 end Generic_Conditional_Insert
;
161 ------------------------------------------
162 -- Generic_Conditional_Insert_With_Hint --
163 ------------------------------------------
165 procedure Generic_Conditional_Insert_With_Hint
166 (Tree
: in out Tree_Type
;
167 Position
: Node_Access
;
169 Node
: out Node_Access
;
170 Inserted
: out Boolean)
173 -- The purpose of a hint is to avoid a search from the root of
174 -- tree. If we have it hint it means we only need to traverse the
175 -- subtree rooted at the hint to find the nearest neighbor. Note
176 -- that finding the neighbor means merely walking the tree; this
177 -- is not a search and the only comparisons that occur are with
178 -- the hint and its neighbor.
180 -- If Position is null, this is interpreted to mean that Key is
181 -- large relative to the nodes in the tree. If the tree is empty,
182 -- or Key is greater than the last node in the tree, then we're
183 -- done; otherwise the hint was "wrong" and we must search.
185 if Position
= null then -- largest
187 or else Is_Greater_Key_Node
(Key
, Tree
.Last
)
189 Insert_Post
(Tree
, Tree
.Last
, False, Node
);
192 Conditional_Insert_Sans_Hint
(Tree
, Key
, Node
, Inserted
);
198 pragma Assert
(Tree
.Length
> 0);
200 -- A hint can either name the node that immediately follows Key,
201 -- or immediately precedes Key. We first test whether Key is
202 -- less than the hint, and if so we compare Key to the node that
203 -- precedes the hint. If Key is both less than the hint and
204 -- greater than the hint's preceding neighbor, then we're done;
205 -- otherwise we must search.
207 -- Note also that a hint can either be an anterior node or a leaf
208 -- node. A new node is always inserted at the bottom of the tree
209 -- (at least prior to rebalancing), becoming the new left or
210 -- right child of leaf node (which prior to the insertion must
211 -- necessarily be null, since this is a leaf). If the hint names
212 -- an anterior node then its neighbor must be a leaf, and so
213 -- (here) we insert after the neighbor. If the hint names a leaf
214 -- then its neighbor must be anterior and so we insert before the
217 if Is_Less_Key_Node
(Key
, Position
) then
219 Before
: constant Node_Access
:= Ops
.Previous
(Position
);
222 if Before
= null then
223 Insert_Post
(Tree
, Tree
.First
, True, Node
);
226 elsif Is_Greater_Key_Node
(Key
, Before
) then
227 if Ops
.Right
(Before
) = null then
228 Insert_Post
(Tree
, Before
, False, Node
);
230 Insert_Post
(Tree
, Position
, True, Node
);
236 Conditional_Insert_Sans_Hint
(Tree
, Key
, Node
, Inserted
);
243 -- We know that Key isn't less than the hint so we try again,
244 -- this time to see if it's greater than the hint. If so we
245 -- compare Key to the node that follows the hint. If Key is both
246 -- greater than the hint and less than the hint's next neighbor,
247 -- then we're done; otherwise we must search.
249 if Is_Greater_Key_Node
(Key
, Position
) then
251 After
: constant Node_Access
:= Ops
.Next
(Position
);
255 Insert_Post
(Tree
, Tree
.Last
, False, Node
);
258 elsif Is_Less_Key_Node
(Key
, After
) then
259 if Ops
.Right
(Position
) = null then
260 Insert_Post
(Tree
, Position
, False, Node
);
262 Insert_Post
(Tree
, After
, True, Node
);
268 Conditional_Insert_Sans_Hint
(Tree
, Key
, Node
, Inserted
);
275 -- We know that Key is neither less than the hint nor greater
276 -- than the hint, and that's the definition of equivalence.
277 -- There's nothing else we need to do, since a search would just
278 -- reach the same conclusion.
282 end Generic_Conditional_Insert_With_Hint
;
284 -------------------------
285 -- Generic_Insert_Post --
286 -------------------------
288 procedure Generic_Insert_Post
289 (Tree
: in out Tree_Type
;
295 if Tree
.Length
= Count_Type
'Last then
296 raise Constraint_Error
with "too many elements";
299 if Tree
.Busy
> 0 then
300 raise Program_Error
with
301 "attempt to tamper with cursors (container is busy)";
305 pragma Assert
(Z
/= null);
306 pragma Assert
(Ops
.Color
(Z
) = Red
);
309 pragma Assert
(Tree
.Length
= 0);
310 pragma Assert
(Tree
.Root
= null);
311 pragma Assert
(Tree
.First
= null);
312 pragma Assert
(Tree
.Last
= null);
319 pragma Assert
(Ops
.Left
(Y
) = null);
323 if Y
= Tree
.First
then
328 pragma Assert
(Ops
.Right
(Y
) = null);
330 Ops
.Set_Right
(Y
, Z
);
332 if Y
= Tree
.Last
then
337 Ops
.Set_Parent
(Z
, Y
);
338 Ops
.Rebalance_For_Insert
(Tree
, Z
);
339 Tree
.Length
:= Tree
.Length
+ 1;
340 end Generic_Insert_Post
;
342 -----------------------
343 -- Generic_Iteration --
344 -----------------------
346 procedure Generic_Iteration
350 procedure Iterate
(Node
: Node_Access
);
356 procedure Iterate
(Node
: Node_Access
) is
361 if Is_Less_Key_Node
(Key
, N
) then
363 elsif Is_Greater_Key_Node
(Key
, N
) then
366 Iterate
(Ops
.Left
(N
));
373 -- Start of processing for Generic_Iteration
377 end Generic_Iteration
;
379 -------------------------------
380 -- Generic_Reverse_Iteration --
381 -------------------------------
383 procedure Generic_Reverse_Iteration
387 procedure Iterate
(Node
: Node_Access
);
393 procedure Iterate
(Node
: Node_Access
) is
398 if Is_Less_Key_Node
(Key
, N
) then
400 elsif Is_Greater_Key_Node
(Key
, N
) then
403 Iterate
(Ops
.Right
(N
));
410 -- Start of processing for Generic_Reverse_Iteration
414 end Generic_Reverse_Iteration
;
416 ----------------------------------
417 -- Generic_Unconditional_Insert --
418 ----------------------------------
420 procedure Generic_Unconditional_Insert
421 (Tree
: in out Tree_Type
;
423 Node
: out Node_Access
)
437 Before
:= Is_Less_Key_Node
(Key
, X
);
438 X
:= (if Before
then Ops
.Left
(X
) else Ops
.Right
(X
));
441 Insert_Post
(Tree
, Y
, Before
, Node
);
442 end Generic_Unconditional_Insert
;
444 --------------------------------------------
445 -- Generic_Unconditional_Insert_With_Hint --
446 --------------------------------------------
448 procedure Generic_Unconditional_Insert_With_Hint
449 (Tree
: in out Tree_Type
;
452 Node
: out Node_Access
)
455 -- There are fewer constraints for an unconditional insertion
456 -- than for a conditional insertion, since we allow duplicate
457 -- keys. So instead of having to check (say) whether Key is
458 -- (strictly) greater than the hint's previous neighbor, here we
459 -- allow Key to be equal to or greater than the previous node.
461 -- There is the issue of what to do if Key is equivalent to the
462 -- hint. Does the new node get inserted before or after the hint?
463 -- We decide that it gets inserted after the hint, reasoning that
464 -- this is consistent with behavior for non-hint insertion, which
465 -- inserts a new node after existing nodes with equivalent keys.
467 -- First we check whether the hint is null, which is interpreted
468 -- to mean that Key is large relative to existing nodes.
469 -- Following our rule above, if Key is equal to or greater than
470 -- the last node, then we insert the new node immediately after
471 -- last. (We don't have an operation for testing whether a key is
472 -- "equal to or greater than" a node, so we must say instead "not
473 -- less than", which is equivalent.)
475 if Hint
= null then -- largest
476 if Tree
.Last
= null then
477 Insert_Post
(Tree
, null, False, Node
);
478 elsif Is_Less_Key_Node
(Key
, Tree
.Last
) then
479 Unconditional_Insert_Sans_Hint
(Tree
, Key
, Node
);
481 Insert_Post
(Tree
, Tree
.Last
, False, Node
);
487 pragma Assert
(Tree
.Length
> 0);
489 -- We decide here whether to insert the new node prior to the
490 -- hint. Key could be equivalent to the hint, so in theory we
491 -- could write the following test as "not greater than" (same as
492 -- "less than or equal to"). If Key were equivalent to the hint,
493 -- that would mean that the new node gets inserted before an
494 -- equivalent node. That wouldn't break any container invariants,
495 -- but our rule above says that new nodes always get inserted
496 -- after equivalent nodes. So here we test whether Key is both
497 -- less than the hint and equal to or greater than the hint's
498 -- previous neighbor, and if so insert it before the hint.
500 if Is_Less_Key_Node
(Key
, Hint
) then
502 Before
: constant Node_Access
:= Ops
.Previous
(Hint
);
504 if Before
= null then
505 Insert_Post
(Tree
, Hint
, True, Node
);
506 elsif Is_Less_Key_Node
(Key
, Before
) then
507 Unconditional_Insert_Sans_Hint
(Tree
, Key
, Node
);
508 elsif Ops
.Right
(Before
) = null then
509 Insert_Post
(Tree
, Before
, False, Node
);
511 Insert_Post
(Tree
, Hint
, True, Node
);
518 -- We know that Key isn't less than the hint, so it must be equal
519 -- or greater. So we just test whether Key is less than or equal
520 -- to (same as "not greater than") the hint's next neighbor, and
521 -- if so insert it after the hint.
524 After
: constant Node_Access
:= Ops
.Next
(Hint
);
527 Insert_Post
(Tree
, Hint
, False, Node
);
528 elsif Is_Greater_Key_Node
(Key
, After
) then
529 Unconditional_Insert_Sans_Hint
(Tree
, Key
, Node
);
530 elsif Ops
.Right
(Hint
) = null then
531 Insert_Post
(Tree
, Hint
, False, Node
);
533 Insert_Post
(Tree
, After
, True, Node
);
536 end Generic_Unconditional_Insert_With_Hint
;
544 Key
: Key_Type
) return Node_Access
552 if Is_Less_Key_Node
(Key
, X
) then
563 end Ada
.Containers
.Red_Black_Trees
.Generic_Keys
;