1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.RED_BLACK_TREES.GENERIC_KEYS --
9 -- Copyright (C) 2004-2015, 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 pragma Warnings
(Off
, "variable ""Busy*"" is not referenced");
33 pragma Warnings
(Off
, "variable ""Lock*"" is not referenced");
34 -- See comment in Ada.Containers.Helpers
36 package Ops
renames Tree_Operations
;
44 function Ceiling
(Tree
: Tree_Type
; Key
: Key_Type
) return Node_Access
is
45 -- Per AI05-0022, the container implementation is required to detect
46 -- element tampering by a generic actual subprogram.
48 Lock
: With_Lock
(Tree
.TC
'Unrestricted_Access);
54 -- If the container is empty, return a result immediately, so that we do
55 -- not manipulate the tamper bits unnecessarily.
57 if Tree
.Root
= null then
63 if Is_Greater_Key_Node
(Key
, X
) then
78 function Find
(Tree
: Tree_Type
; Key
: Key_Type
) return Node_Access
is
79 -- Per AI05-0022, the container implementation is required to detect
80 -- element tampering by a generic actual subprogram.
82 Lock
: With_Lock
(Tree
.TC
'Unrestricted_Access);
88 -- If the container is empty, return a result immediately, so that we do
89 -- not manipulate the tamper bits unnecessarily.
91 if Tree
.Root
= null then
97 if Is_Greater_Key_Node
(Key
, X
) then
105 if Y
= null or else Is_Less_Key_Node
(Key
, Y
) then
116 function Floor
(Tree
: Tree_Type
; Key
: Key_Type
) return Node_Access
is
117 -- Per AI05-0022, the container implementation is required to detect
118 -- element tampering by a generic actual subprogram.
120 Lock
: With_Lock
(Tree
.TC
'Unrestricted_Access);
126 -- If the container is empty, return a result immediately, so that we do
127 -- not manipulate the tamper bits unnecessarily.
129 if Tree
.Root
= null then
135 if Is_Less_Key_Node
(Key
, X
) then
146 --------------------------------
147 -- Generic_Conditional_Insert --
148 --------------------------------
150 procedure Generic_Conditional_Insert
151 (Tree
: in out Tree_Type
;
153 Node
: out Node_Access
;
154 Inserted
: out Boolean)
162 -- This is a "conditional" insertion, meaning that the insertion request
163 -- can "fail" in the sense that no new node is created. If the Key is
164 -- equivalent to an existing node, then we return the existing node and
165 -- Inserted is set to False. Otherwise, we allocate a new node (via
166 -- Insert_Post) and Inserted is set to True.
168 -- Note that we are testing for equivalence here, not equality. Key must
169 -- be strictly less than its next neighbor, and strictly greater than
170 -- its previous neighbor, in order for the conditional insertion to
173 -- Handle insertion into an empty container as a special case, so that
174 -- we do not manipulate the tamper bits unnecessarily.
176 if Tree
.Root
= null then
177 Insert_Post
(Tree
, null, True, Node
);
182 -- We search the tree to find the nearest neighbor of Key, which is
183 -- either the smallest node greater than Key (Inserted is True), or the
184 -- largest node less or equivalent to Key (Inserted is False).
187 Lock
: With_Lock
(Tree
.TC
'Unrestricted_Access);
194 Inserted
:= Is_Less_Key_Node
(Key
, X
);
195 X
:= (if Inserted
then Ops
.Left
(X
) else Ops
.Right
(X
));
201 -- Key is less than Y. If Y is the first node in the tree, then there
202 -- are no other nodes that we need to search for, and we insert a new
203 -- node into the tree.
205 if Y
= Tree
.First
then
206 Insert_Post
(Tree
, Y
, True, Node
);
210 -- Y is the next nearest-neighbor of Key. We know that Key is not
211 -- equivalent to Y (because Key is strictly less than Y), so we move
212 -- to the previous node, the nearest-neighbor just smaller or
213 -- equivalent to Key.
215 Node
:= Ops
.Previous
(Y
);
218 -- Y is the previous nearest-neighbor of Key. We know that Key is not
219 -- less than Y, which means either that Key is equivalent to Y, or
225 -- Key is equivalent to or greater than Node. We must resolve which is
226 -- the case, to determine whether the conditional insertion succeeds.
229 Lock
: With_Lock
(Tree
.TC
'Unrestricted_Access);
231 Compare
:= Is_Greater_Key_Node
(Key
, Node
);
236 -- Key is strictly greater than Node, which means that Key is not
237 -- equivalent to Node. In this case, the insertion succeeds, and we
238 -- insert a new node into the tree.
240 Insert_Post
(Tree
, Y
, Inserted
, Node
);
245 -- Key is equivalent to Node. This is a conditional insertion, so we do
246 -- not insert a new node in this case. We return the existing node and
247 -- report that no insertion has occurred.
250 end Generic_Conditional_Insert
;
252 ------------------------------------------
253 -- Generic_Conditional_Insert_With_Hint --
254 ------------------------------------------
256 procedure Generic_Conditional_Insert_With_Hint
257 (Tree
: in out Tree_Type
;
258 Position
: Node_Access
;
260 Node
: out Node_Access
;
261 Inserted
: out Boolean)
267 -- The purpose of a hint is to avoid a search from the root of
268 -- tree. If we have it hint it means we only need to traverse the
269 -- subtree rooted at the hint to find the nearest neighbor. Note
270 -- that finding the neighbor means merely walking the tree; this
271 -- is not a search and the only comparisons that occur are with
272 -- the hint and its neighbor.
274 -- Handle insertion into an empty container as a special case, so that
275 -- we do not manipulate the tamper bits unnecessarily.
277 if Tree
.Root
= null then
278 Insert_Post
(Tree
, null, True, Node
);
283 -- If Position is null, this is interpreted to mean that Key is large
284 -- relative to the nodes in the tree. If Key is greater than the last
285 -- node in the tree, then we're done; otherwise the hint was "wrong" and
288 if Position
= null then -- largest
290 Lock
: With_Lock
(Tree
.TC
'Unrestricted_Access);
292 Compare
:= Is_Greater_Key_Node
(Key
, Tree
.Last
);
296 Insert_Post
(Tree
, Tree
.Last
, False, Node
);
299 Conditional_Insert_Sans_Hint
(Tree
, Key
, Node
, Inserted
);
305 pragma Assert
(Tree
.Length
> 0);
307 -- A hint can either name the node that immediately follows Key,
308 -- or immediately precedes Key. We first test whether Key is
309 -- less than the hint, and if so we compare Key to the node that
310 -- precedes the hint. If Key is both less than the hint and
311 -- greater than the hint's preceding neighbor, then we're done;
312 -- otherwise we must search.
314 -- Note also that a hint can either be an anterior node or a leaf
315 -- node. A new node is always inserted at the bottom of the tree
316 -- (at least prior to rebalancing), becoming the new left or
317 -- right child of leaf node (which prior to the insertion must
318 -- necessarily be null, since this is a leaf). If the hint names
319 -- an anterior node then its neighbor must be a leaf, and so
320 -- (here) we insert after the neighbor. If the hint names a leaf
321 -- then its neighbor must be anterior and so we insert before the
325 Lock
: With_Lock
(Tree
.TC
'Unrestricted_Access);
327 Compare
:= Is_Less_Key_Node
(Key
, Position
);
331 Test
:= Ops
.Previous
(Position
); -- "before"
333 if Test
= null then -- new first node
334 Insert_Post
(Tree
, Tree
.First
, True, Node
);
341 Lock
: With_Lock
(Tree
.TC
'Unrestricted_Access);
343 Compare
:= Is_Greater_Key_Node
(Key
, Test
);
347 if Ops
.Right
(Test
) = null then
348 Insert_Post
(Tree
, Test
, False, Node
);
350 Insert_Post
(Tree
, Position
, True, Node
);
356 Conditional_Insert_Sans_Hint
(Tree
, Key
, Node
, Inserted
);
362 -- We know that Key isn't less than the hint so we try again, this time
363 -- to see if it's greater than the hint. If so we compare Key to the
364 -- node that follows the hint. If Key is both greater than the hint and
365 -- less than the hint's next neighbor, then we're done; otherwise we
369 Lock
: With_Lock
(Tree
.TC
'Unrestricted_Access);
371 Compare
:= Is_Greater_Key_Node
(Key
, Position
);
375 Test
:= Ops
.Next
(Position
); -- "after"
377 if Test
= null then -- new last node
378 Insert_Post
(Tree
, Tree
.Last
, False, Node
);
385 Lock
: With_Lock
(Tree
.TC
'Unrestricted_Access);
387 Compare
:= Is_Less_Key_Node
(Key
, Test
);
391 if Ops
.Right
(Position
) = null then
392 Insert_Post
(Tree
, Position
, False, Node
);
394 Insert_Post
(Tree
, Test
, True, Node
);
400 Conditional_Insert_Sans_Hint
(Tree
, Key
, Node
, Inserted
);
406 -- We know that Key is neither less than the hint nor greater than the
407 -- hint, and that's the definition of equivalence. There's nothing else
408 -- we need to do, since a search would just reach the same conclusion.
412 end Generic_Conditional_Insert_With_Hint
;
414 -------------------------
415 -- Generic_Insert_Post --
416 -------------------------
418 procedure Generic_Insert_Post
419 (Tree
: in out Tree_Type
;
425 if Checks
and then Tree
.Length
= Count_Type
'Last then
426 raise Constraint_Error
with "too many elements";
432 pragma Assert
(Z
/= null);
433 pragma Assert
(Ops
.Color
(Z
) = Red
);
436 pragma Assert
(Tree
.Length
= 0);
437 pragma Assert
(Tree
.Root
= null);
438 pragma Assert
(Tree
.First
= null);
439 pragma Assert
(Tree
.Last
= null);
446 pragma Assert
(Ops
.Left
(Y
) = null);
450 if Y
= Tree
.First
then
455 pragma Assert
(Ops
.Right
(Y
) = null);
457 Ops
.Set_Right
(Y
, Z
);
459 if Y
= Tree
.Last
then
464 Ops
.Set_Parent
(Z
, Y
);
465 Ops
.Rebalance_For_Insert
(Tree
, Z
);
466 Tree
.Length
:= Tree
.Length
+ 1;
467 end Generic_Insert_Post
;
469 -----------------------
470 -- Generic_Iteration --
471 -----------------------
473 procedure Generic_Iteration
477 procedure Iterate
(Node
: Node_Access
);
483 procedure Iterate
(Node
: Node_Access
) is
488 if Is_Less_Key_Node
(Key
, N
) then
490 elsif Is_Greater_Key_Node
(Key
, N
) then
493 Iterate
(Ops
.Left
(N
));
500 -- Start of processing for Generic_Iteration
504 end Generic_Iteration
;
506 -------------------------------
507 -- Generic_Reverse_Iteration --
508 -------------------------------
510 procedure Generic_Reverse_Iteration
514 procedure Iterate
(Node
: Node_Access
);
520 procedure Iterate
(Node
: Node_Access
) is
525 if Is_Less_Key_Node
(Key
, N
) then
527 elsif Is_Greater_Key_Node
(Key
, N
) then
530 Iterate
(Ops
.Right
(N
));
537 -- Start of processing for Generic_Reverse_Iteration
541 end Generic_Reverse_Iteration
;
543 ----------------------------------
544 -- Generic_Unconditional_Insert --
545 ----------------------------------
547 procedure Generic_Unconditional_Insert
548 (Tree
: in out Tree_Type
;
550 Node
: out Node_Access
)
564 Before
:= Is_Less_Key_Node
(Key
, X
);
565 X
:= (if Before
then Ops
.Left
(X
) else Ops
.Right
(X
));
568 Insert_Post
(Tree
, Y
, Before
, Node
);
569 end Generic_Unconditional_Insert
;
571 --------------------------------------------
572 -- Generic_Unconditional_Insert_With_Hint --
573 --------------------------------------------
575 procedure Generic_Unconditional_Insert_With_Hint
576 (Tree
: in out Tree_Type
;
579 Node
: out Node_Access
)
582 -- There are fewer constraints for an unconditional insertion
583 -- than for a conditional insertion, since we allow duplicate
584 -- keys. So instead of having to check (say) whether Key is
585 -- (strictly) greater than the hint's previous neighbor, here we
586 -- allow Key to be equal to or greater than the previous node.
588 -- There is the issue of what to do if Key is equivalent to the
589 -- hint. Does the new node get inserted before or after the hint?
590 -- We decide that it gets inserted after the hint, reasoning that
591 -- this is consistent with behavior for non-hint insertion, which
592 -- inserts a new node after existing nodes with equivalent keys.
594 -- First we check whether the hint is null, which is interpreted
595 -- to mean that Key is large relative to existing nodes.
596 -- Following our rule above, if Key is equal to or greater than
597 -- the last node, then we insert the new node immediately after
598 -- last. (We don't have an operation for testing whether a key is
599 -- "equal to or greater than" a node, so we must say instead "not
600 -- less than", which is equivalent.)
602 if Hint
= null then -- largest
603 if Tree
.Last
= null then
604 Insert_Post
(Tree
, null, False, Node
);
605 elsif Is_Less_Key_Node
(Key
, Tree
.Last
) then
606 Unconditional_Insert_Sans_Hint
(Tree
, Key
, Node
);
608 Insert_Post
(Tree
, Tree
.Last
, False, Node
);
614 pragma Assert
(Tree
.Length
> 0);
616 -- We decide here whether to insert the new node prior to the
617 -- hint. Key could be equivalent to the hint, so in theory we
618 -- could write the following test as "not greater than" (same as
619 -- "less than or equal to"). If Key were equivalent to the hint,
620 -- that would mean that the new node gets inserted before an
621 -- equivalent node. That wouldn't break any container invariants,
622 -- but our rule above says that new nodes always get inserted
623 -- after equivalent nodes. So here we test whether Key is both
624 -- less than the hint and equal to or greater than the hint's
625 -- previous neighbor, and if so insert it before the hint.
627 if Is_Less_Key_Node
(Key
, Hint
) then
629 Before
: constant Node_Access
:= Ops
.Previous
(Hint
);
631 if Before
= null then
632 Insert_Post
(Tree
, Hint
, True, Node
);
633 elsif Is_Less_Key_Node
(Key
, Before
) then
634 Unconditional_Insert_Sans_Hint
(Tree
, Key
, Node
);
635 elsif Ops
.Right
(Before
) = null then
636 Insert_Post
(Tree
, Before
, False, Node
);
638 Insert_Post
(Tree
, Hint
, True, Node
);
645 -- We know that Key isn't less than the hint, so it must be equal
646 -- or greater. So we just test whether Key is less than or equal
647 -- to (same as "not greater than") the hint's next neighbor, and
648 -- if so insert it after the hint.
651 After
: constant Node_Access
:= Ops
.Next
(Hint
);
654 Insert_Post
(Tree
, Hint
, False, Node
);
655 elsif Is_Greater_Key_Node
(Key
, After
) then
656 Unconditional_Insert_Sans_Hint
(Tree
, Key
, Node
);
657 elsif Ops
.Right
(Hint
) = null then
658 Insert_Post
(Tree
, Hint
, False, Node
);
660 Insert_Post
(Tree
, After
, True, Node
);
663 end Generic_Unconditional_Insert_With_Hint
;
671 Key
: Key_Type
) return Node_Access
679 if Is_Less_Key_Node
(Key
, X
) then
690 end Ada
.Containers
.Red_Black_Trees
.Generic_Keys
;