1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.RED_BLACK_TREES.GENERIC_KEYS --
9 -- Copyright (C) 2004-2013, 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
41 B
: Natural renames Tree
'Unrestricted_Access.Busy
;
42 L
: Natural renames Tree
'Unrestricted_Access.Lock
;
48 -- If the container is empty, return a result immediately, so that we do
49 -- not manipulate the tamper bits unnecessarily.
51 if Tree
.Root
= null then
55 -- Per AI05-0022, the container implementation is required to detect
56 -- element tampering by a generic actual subprogram.
63 if Is_Greater_Key_Node
(Key
, X
) then
87 function Find
(Tree
: Tree_Type
; Key
: Key_Type
) return Node_Access
is
88 B
: Natural renames Tree
'Unrestricted_Access.Busy
;
89 L
: Natural renames Tree
'Unrestricted_Access.Lock
;
97 -- If the container is empty, return a result immediately, so that we do
98 -- not manipulate the tamper bits unnecessarily.
100 if Tree
.Root
= null then
104 -- Per AI05-0022, the container implementation is required to detect
105 -- element tampering by a generic actual subprogram.
112 if Is_Greater_Key_Node
(Key
, X
) then
123 elsif Is_Less_Key_Node
(Key
, Y
) then
146 function Floor
(Tree
: Tree_Type
; Key
: Key_Type
) return Node_Access
is
147 B
: Natural renames Tree
'Unrestricted_Access.Busy
;
148 L
: Natural renames Tree
'Unrestricted_Access.Lock
;
154 -- If the container is empty, return a result immediately, so that we do
155 -- not manipulate the tamper bits unnecessarily.
157 if Tree
.Root
= null then
161 -- Per AI05-0022, the container implementation is required to detect
162 -- element tampering by a generic actual subprogram.
169 if Is_Less_Key_Node
(Key
, X
) then
189 --------------------------------
190 -- Generic_Conditional_Insert --
191 --------------------------------
193 procedure Generic_Conditional_Insert
194 (Tree
: in out Tree_Type
;
196 Node
: out Node_Access
;
197 Inserted
: out Boolean)
202 -- Per AI05-0022, the container implementation is required to detect
203 -- element tampering by a generic actual subprogram.
205 B
: Natural renames Tree
.Busy
;
206 L
: Natural renames Tree
.Lock
;
211 -- This is a "conditional" insertion, meaning that the insertion request
212 -- can "fail" in the sense that no new node is created. If the Key is
213 -- equivalent to an existing node, then we return the existing node and
214 -- Inserted is set to False. Otherwise, we allocate a new node (via
215 -- Insert_Post) and Inserted is set to True.
217 -- Note that we are testing for equivalence here, not equality. Key must
218 -- be strictly less than its next neighbor, and strictly greater than
219 -- its previous neighbor, in order for the conditional insertion to
222 -- Handle insertion into an empty container as a special case, so that
223 -- we do not manipulate the tamper bits unnecessarily.
225 if Tree
.Root
= null then
226 Insert_Post
(Tree
, null, True, Node
);
231 -- We search the tree to find the nearest neighbor of Key, which is
232 -- either the smallest node greater than Key (Inserted is True), or the
233 -- largest node less or equivalent to Key (Inserted is False).
244 Inserted
:= Is_Less_Key_Node
(Key
, X
);
245 X
:= (if Inserted
then Ops
.Left
(X
) else Ops
.Right
(X
));
260 -- Key is less than Y. If Y is the first node in the tree, then there
261 -- are no other nodes that we need to search for, and we insert a new
262 -- node into the tree.
264 if Y
= Tree
.First
then
265 Insert_Post
(Tree
, Y
, True, Node
);
269 -- Y is the next nearest-neighbor of Key. We know that Key is not
270 -- equivalent to Y (because Key is strictly less than Y), so we move
271 -- to the previous node, the nearest-neighbor just smaller or
272 -- equivalent to Key.
274 Node
:= Ops
.Previous
(Y
);
277 -- Y is the previous nearest-neighbor of Key. We know that Key is not
278 -- less than Y, which means either that Key is equivalent to Y, or
284 -- Key is equivalent to or greater than Node. We must resolve which is
285 -- the case, to determine whether the conditional insertion succeeds.
291 Compare
:= Is_Greater_Key_Node
(Key
, Node
);
305 -- Key is strictly greater than Node, which means that Key is not
306 -- equivalent to Node. In this case, the insertion succeeds, and we
307 -- insert a new node into the tree.
309 Insert_Post
(Tree
, Y
, Inserted
, Node
);
314 -- Key is equivalent to Node. This is a conditional insertion, so we do
315 -- not insert a new node in this case. We return the existing node and
316 -- report that no insertion has occurred.
319 end Generic_Conditional_Insert
;
321 ------------------------------------------
322 -- Generic_Conditional_Insert_With_Hint --
323 ------------------------------------------
325 procedure Generic_Conditional_Insert_With_Hint
326 (Tree
: in out Tree_Type
;
327 Position
: Node_Access
;
329 Node
: out Node_Access
;
330 Inserted
: out Boolean)
332 -- Per AI05-0022, the container implementation is required to detect
333 -- element tampering by a generic actual subprogram.
335 B
: Natural renames Tree
.Busy
;
336 L
: Natural renames Tree
.Lock
;
342 -- The purpose of a hint is to avoid a search from the root of
343 -- tree. If we have it hint it means we only need to traverse the
344 -- subtree rooted at the hint to find the nearest neighbor. Note
345 -- that finding the neighbor means merely walking the tree; this
346 -- is not a search and the only comparisons that occur are with
347 -- the hint and its neighbor.
349 -- Handle insertion into an empty container as a special case, so that
350 -- we do not manipulate the tamper bits unnecessarily.
352 if Tree
.Root
= null then
353 Insert_Post
(Tree
, null, True, Node
);
358 -- If Position is null, this is interpreted to mean that Key is large
359 -- relative to the nodes in the tree. If Key is greater than the last
360 -- node in the tree, then we're done; otherwise the hint was "wrong" and
363 if Position
= null then -- largest
368 Compare
:= Is_Greater_Key_Node
(Key
, Tree
.Last
);
381 Insert_Post
(Tree
, Tree
.Last
, False, Node
);
384 Conditional_Insert_Sans_Hint
(Tree
, Key
, Node
, Inserted
);
390 pragma Assert
(Tree
.Length
> 0);
392 -- A hint can either name the node that immediately follows Key,
393 -- or immediately precedes Key. We first test whether Key is
394 -- less than the hint, and if so we compare Key to the node that
395 -- precedes the hint. If Key is both less than the hint and
396 -- greater than the hint's preceding neighbor, then we're done;
397 -- otherwise we must search.
399 -- Note also that a hint can either be an anterior node or a leaf
400 -- node. A new node is always inserted at the bottom of the tree
401 -- (at least prior to rebalancing), becoming the new left or
402 -- right child of leaf node (which prior to the insertion must
403 -- necessarily be null, since this is a leaf). If the hint names
404 -- an anterior node then its neighbor must be a leaf, and so
405 -- (here) we insert after the neighbor. If the hint names a leaf
406 -- then its neighbor must be anterior and so we insert before the
413 Compare
:= Is_Less_Key_Node
(Key
, Position
);
426 Test
:= Ops
.Previous
(Position
); -- "before"
428 if Test
= null then -- new first node
429 Insert_Post
(Tree
, Tree
.First
, True, Node
);
439 Compare
:= Is_Greater_Key_Node
(Key
, Test
);
452 if Ops
.Right
(Test
) = null then
453 Insert_Post
(Tree
, Test
, False, Node
);
455 Insert_Post
(Tree
, Position
, True, Node
);
461 Conditional_Insert_Sans_Hint
(Tree
, Key
, Node
, Inserted
);
467 -- We know that Key isn't less than the hint so we try again, this time
468 -- to see if it's greater than the hint. If so we compare Key to the
469 -- node that follows the hint. If Key is both greater than the hint and
470 -- less than the hint's next neighbor, then we're done; otherwise we
477 Compare
:= Is_Greater_Key_Node
(Key
, Position
);
490 Test
:= Ops
.Next
(Position
); -- "after"
492 if Test
= null then -- new last node
493 Insert_Post
(Tree
, Tree
.Last
, False, Node
);
503 Compare
:= Is_Less_Key_Node
(Key
, Test
);
516 if Ops
.Right
(Position
) = null then
517 Insert_Post
(Tree
, Position
, False, Node
);
519 Insert_Post
(Tree
, Test
, True, Node
);
525 Conditional_Insert_Sans_Hint
(Tree
, Key
, Node
, Inserted
);
531 -- We know that Key is neither less than the hint nor greater than the
532 -- hint, and that's the definition of equivalence. There's nothing else
533 -- we need to do, since a search would just reach the same conclusion.
537 end Generic_Conditional_Insert_With_Hint
;
539 -------------------------
540 -- Generic_Insert_Post --
541 -------------------------
543 procedure Generic_Insert_Post
544 (Tree
: in out Tree_Type
;
550 if Tree
.Length
= Count_Type
'Last then
551 raise Constraint_Error
with "too many elements";
554 if Tree
.Busy
> 0 then
555 raise Program_Error
with
556 "attempt to tamper with cursors (container is busy)";
560 pragma Assert
(Z
/= null);
561 pragma Assert
(Ops
.Color
(Z
) = Red
);
564 pragma Assert
(Tree
.Length
= 0);
565 pragma Assert
(Tree
.Root
= null);
566 pragma Assert
(Tree
.First
= null);
567 pragma Assert
(Tree
.Last
= null);
574 pragma Assert
(Ops
.Left
(Y
) = null);
578 if Y
= Tree
.First
then
583 pragma Assert
(Ops
.Right
(Y
) = null);
585 Ops
.Set_Right
(Y
, Z
);
587 if Y
= Tree
.Last
then
592 Ops
.Set_Parent
(Z
, Y
);
593 Ops
.Rebalance_For_Insert
(Tree
, Z
);
594 Tree
.Length
:= Tree
.Length
+ 1;
595 end Generic_Insert_Post
;
597 -----------------------
598 -- Generic_Iteration --
599 -----------------------
601 procedure Generic_Iteration
605 procedure Iterate
(Node
: Node_Access
);
611 procedure Iterate
(Node
: Node_Access
) is
616 if Is_Less_Key_Node
(Key
, N
) then
618 elsif Is_Greater_Key_Node
(Key
, N
) then
621 Iterate
(Ops
.Left
(N
));
628 -- Start of processing for Generic_Iteration
632 end Generic_Iteration
;
634 -------------------------------
635 -- Generic_Reverse_Iteration --
636 -------------------------------
638 procedure Generic_Reverse_Iteration
642 procedure Iterate
(Node
: Node_Access
);
648 procedure Iterate
(Node
: Node_Access
) is
653 if Is_Less_Key_Node
(Key
, N
) then
655 elsif Is_Greater_Key_Node
(Key
, N
) then
658 Iterate
(Ops
.Right
(N
));
665 -- Start of processing for Generic_Reverse_Iteration
669 end Generic_Reverse_Iteration
;
671 ----------------------------------
672 -- Generic_Unconditional_Insert --
673 ----------------------------------
675 procedure Generic_Unconditional_Insert
676 (Tree
: in out Tree_Type
;
678 Node
: out Node_Access
)
692 Before
:= Is_Less_Key_Node
(Key
, X
);
693 X
:= (if Before
then Ops
.Left
(X
) else Ops
.Right
(X
));
696 Insert_Post
(Tree
, Y
, Before
, Node
);
697 end Generic_Unconditional_Insert
;
699 --------------------------------------------
700 -- Generic_Unconditional_Insert_With_Hint --
701 --------------------------------------------
703 procedure Generic_Unconditional_Insert_With_Hint
704 (Tree
: in out Tree_Type
;
707 Node
: out Node_Access
)
710 -- There are fewer constraints for an unconditional insertion
711 -- than for a conditional insertion, since we allow duplicate
712 -- keys. So instead of having to check (say) whether Key is
713 -- (strictly) greater than the hint's previous neighbor, here we
714 -- allow Key to be equal to or greater than the previous node.
716 -- There is the issue of what to do if Key is equivalent to the
717 -- hint. Does the new node get inserted before or after the hint?
718 -- We decide that it gets inserted after the hint, reasoning that
719 -- this is consistent with behavior for non-hint insertion, which
720 -- inserts a new node after existing nodes with equivalent keys.
722 -- First we check whether the hint is null, which is interpreted
723 -- to mean that Key is large relative to existing nodes.
724 -- Following our rule above, if Key is equal to or greater than
725 -- the last node, then we insert the new node immediately after
726 -- last. (We don't have an operation for testing whether a key is
727 -- "equal to or greater than" a node, so we must say instead "not
728 -- less than", which is equivalent.)
730 if Hint
= null then -- largest
731 if Tree
.Last
= null then
732 Insert_Post
(Tree
, null, False, Node
);
733 elsif Is_Less_Key_Node
(Key
, Tree
.Last
) then
734 Unconditional_Insert_Sans_Hint
(Tree
, Key
, Node
);
736 Insert_Post
(Tree
, Tree
.Last
, False, Node
);
742 pragma Assert
(Tree
.Length
> 0);
744 -- We decide here whether to insert the new node prior to the
745 -- hint. Key could be equivalent to the hint, so in theory we
746 -- could write the following test as "not greater than" (same as
747 -- "less than or equal to"). If Key were equivalent to the hint,
748 -- that would mean that the new node gets inserted before an
749 -- equivalent node. That wouldn't break any container invariants,
750 -- but our rule above says that new nodes always get inserted
751 -- after equivalent nodes. So here we test whether Key is both
752 -- less than the hint and equal to or greater than the hint's
753 -- previous neighbor, and if so insert it before the hint.
755 if Is_Less_Key_Node
(Key
, Hint
) then
757 Before
: constant Node_Access
:= Ops
.Previous
(Hint
);
759 if Before
= null then
760 Insert_Post
(Tree
, Hint
, True, Node
);
761 elsif Is_Less_Key_Node
(Key
, Before
) then
762 Unconditional_Insert_Sans_Hint
(Tree
, Key
, Node
);
763 elsif Ops
.Right
(Before
) = null then
764 Insert_Post
(Tree
, Before
, False, Node
);
766 Insert_Post
(Tree
, Hint
, True, Node
);
773 -- We know that Key isn't less than the hint, so it must be equal
774 -- or greater. So we just test whether Key is less than or equal
775 -- to (same as "not greater than") the hint's next neighbor, and
776 -- if so insert it after the hint.
779 After
: constant Node_Access
:= Ops
.Next
(Hint
);
782 Insert_Post
(Tree
, Hint
, False, Node
);
783 elsif Is_Greater_Key_Node
(Key
, After
) then
784 Unconditional_Insert_Sans_Hint
(Tree
, Key
, Node
);
785 elsif Ops
.Right
(Hint
) = null then
786 Insert_Post
(Tree
, Hint
, False, Node
);
788 Insert_Post
(Tree
, After
, True, Node
);
791 end Generic_Unconditional_Insert_With_Hint
;
799 Key
: Key_Type
) return Node_Access
807 if Is_Less_Key_Node
(Key
, X
) then
818 end Ada
.Containers
.Red_Black_Trees
.Generic_Keys
;