2015-09-28 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / ada / a-crbtgk.adb
blobae8dd7c6c7aee2f33f6d723677fd4f70907fd9d0
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- ADA.CONTAINERS.RED_BLACK_TREES.GENERIC_KEYS --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2004-2013, Free Software Foundation, Inc. --
10 -- --
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. --
17 -- --
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. --
21 -- --
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/>. --
26 -- --
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;
34 -------------
35 -- Ceiling --
36 -------------
38 -- AKA Lower_Bound
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;
44 Y : Node_Access;
45 X : Node_Access;
47 begin
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
52 return null;
53 end if;
55 -- Per AI05-0022, the container implementation is required to detect
56 -- element tampering by a generic actual subprogram.
58 B := B + 1;
59 L := L + 1;
61 X := Tree.Root;
62 while X /= null loop
63 if Is_Greater_Key_Node (Key, X) then
64 X := Ops.Right (X);
65 else
66 Y := X;
67 X := Ops.Left (X);
68 end if;
69 end loop;
71 B := B - 1;
72 L := L - 1;
74 return Y;
76 exception
77 when others =>
78 B := B - 1;
79 L := L - 1;
81 raise;
82 end Ceiling;
84 ----------
85 -- Find --
86 ----------
88 function Find (Tree : Tree_Type; Key : Key_Type) return Node_Access is
89 B : Natural renames Tree'Unrestricted_Access.Busy;
90 L : Natural renames Tree'Unrestricted_Access.Lock;
92 Y : Node_Access;
93 X : Node_Access;
95 Result : Node_Access;
97 begin
98 -- If the container is empty, return a result immediately, so that we do
99 -- not manipulate the tamper bits unnecessarily.
101 if Tree.Root = null then
102 return null;
103 end if;
105 -- Per AI05-0022, the container implementation is required to detect
106 -- element tampering by a generic actual subprogram.
108 B := B + 1;
109 L := L + 1;
111 X := Tree.Root;
112 while X /= null loop
113 if Is_Greater_Key_Node (Key, X) then
114 X := Ops.Right (X);
115 else
116 Y := X;
117 X := Ops.Left (X);
118 end if;
119 end loop;
121 if Y = null then
122 Result := null;
124 elsif Is_Less_Key_Node (Key, Y) then
125 Result := null;
127 else
128 Result := Y;
129 end if;
131 B := B - 1;
132 L := L - 1;
134 return Result;
136 exception
137 when others =>
138 B := B - 1;
139 L := L - 1;
141 raise;
142 end Find;
144 -----------
145 -- Floor --
146 -----------
148 function Floor (Tree : Tree_Type; Key : Key_Type) return Node_Access is
149 B : Natural renames Tree'Unrestricted_Access.Busy;
150 L : Natural renames Tree'Unrestricted_Access.Lock;
152 Y : Node_Access;
153 X : Node_Access;
155 begin
156 -- If the container is empty, return a result immediately, so that we do
157 -- not manipulate the tamper bits unnecessarily.
159 if Tree.Root = null then
160 return null;
161 end if;
163 -- Per AI05-0022, the container implementation is required to detect
164 -- element tampering by a generic actual subprogram.
166 B := B + 1;
167 L := L + 1;
169 X := Tree.Root;
170 while X /= null loop
171 if Is_Less_Key_Node (Key, X) then
172 X := Ops.Left (X);
173 else
174 Y := X;
175 X := Ops.Right (X);
176 end if;
177 end loop;
179 B := B - 1;
180 L := L - 1;
182 return Y;
184 exception
185 when others =>
186 B := B - 1;
187 L := L - 1;
189 raise;
190 end Floor;
192 --------------------------------
193 -- Generic_Conditional_Insert --
194 --------------------------------
196 procedure Generic_Conditional_Insert
197 (Tree : in out Tree_Type;
198 Key : Key_Type;
199 Node : out Node_Access;
200 Inserted : out Boolean)
202 X : Node_Access;
203 Y : Node_Access;
205 -- Per AI05-0022, the container implementation is required to detect
206 -- element tampering by a generic actual subprogram.
208 B : Natural renames Tree.Busy;
209 L : Natural renames Tree.Lock;
211 Compare : Boolean;
213 begin
214 -- This is a "conditional" insertion, meaning that the insertion request
215 -- can "fail" in the sense that no new node is created. If the Key is
216 -- equivalent to an existing node, then we return the existing node and
217 -- Inserted is set to False. Otherwise, we allocate a new node (via
218 -- Insert_Post) and Inserted is set to True.
220 -- Note that we are testing for equivalence here, not equality. Key must
221 -- be strictly less than its next neighbor, and strictly greater than
222 -- its previous neighbor, in order for the conditional insertion to
223 -- succeed.
225 -- Handle insertion into an empty container as a special case, so that
226 -- we do not manipulate the tamper bits unnecessarily.
228 if Tree.Root = null then
229 Insert_Post (Tree, null, True, Node);
230 Inserted := True;
231 return;
232 end if;
234 -- We search the tree to find the nearest neighbor of Key, which is
235 -- either the smallest node greater than Key (Inserted is True), or the
236 -- largest node less or equivalent to Key (Inserted is False).
238 begin
239 B := B + 1;
240 L := L + 1;
242 X := Tree.Root;
243 Y := null;
244 Inserted := True;
245 while X /= null loop
246 Y := X;
247 Inserted := Is_Less_Key_Node (Key, X);
248 X := (if Inserted then Ops.Left (X) else Ops.Right (X));
249 end loop;
251 L := L - 1;
252 B := B - 1;
254 exception
255 when others =>
256 L := L - 1;
257 B := B - 1;
259 raise;
260 end;
262 if Inserted then
264 -- Key is less than Y. If Y is the first node in the tree, then there
265 -- are no other nodes that we need to search for, and we insert a new
266 -- node into the tree.
268 if Y = Tree.First then
269 Insert_Post (Tree, Y, True, Node);
270 return;
271 end if;
273 -- Y is the next nearest-neighbor of Key. We know that Key is not
274 -- equivalent to Y (because Key is strictly less than Y), so we move
275 -- to the previous node, the nearest-neighbor just smaller or
276 -- equivalent to Key.
278 Node := Ops.Previous (Y);
280 else
281 -- Y is the previous nearest-neighbor of Key. We know that Key is not
282 -- less than Y, which means either that Key is equivalent to Y, or
283 -- greater than Y.
285 Node := Y;
286 end if;
288 -- Key is equivalent to or greater than Node. We must resolve which is
289 -- the case, to determine whether the conditional insertion succeeds.
291 begin
292 B := B + 1;
293 L := L + 1;
295 Compare := Is_Greater_Key_Node (Key, Node);
297 L := L - 1;
298 B := B - 1;
300 exception
301 when others =>
302 L := L - 1;
303 B := B - 1;
305 raise;
306 end;
308 if Compare then
310 -- Key is strictly greater than Node, which means that Key is not
311 -- equivalent to Node. In this case, the insertion succeeds, and we
312 -- insert a new node into the tree.
314 Insert_Post (Tree, Y, Inserted, Node);
315 Inserted := True;
316 return;
317 end if;
319 -- Key is equivalent to Node. This is a conditional insertion, so we do
320 -- not insert a new node in this case. We return the existing node and
321 -- report that no insertion has occurred.
323 Inserted := False;
324 end Generic_Conditional_Insert;
326 ------------------------------------------
327 -- Generic_Conditional_Insert_With_Hint --
328 ------------------------------------------
330 procedure Generic_Conditional_Insert_With_Hint
331 (Tree : in out Tree_Type;
332 Position : Node_Access;
333 Key : Key_Type;
334 Node : out Node_Access;
335 Inserted : out Boolean)
337 -- Per AI05-0022, the container implementation is required to detect
338 -- element tampering by a generic actual subprogram.
340 B : Natural renames Tree.Busy;
341 L : Natural renames Tree.Lock;
343 Test : Node_Access;
344 Compare : Boolean;
346 begin
347 -- The purpose of a hint is to avoid a search from the root of
348 -- tree. If we have it hint it means we only need to traverse the
349 -- subtree rooted at the hint to find the nearest neighbor. Note
350 -- that finding the neighbor means merely walking the tree; this
351 -- is not a search and the only comparisons that occur are with
352 -- the hint and its neighbor.
354 -- Handle insertion into an empty container as a special case, so that
355 -- we do not manipulate the tamper bits unnecessarily.
357 if Tree.Root = null then
358 Insert_Post (Tree, null, True, Node);
359 Inserted := True;
360 return;
361 end if;
363 -- If Position is null, this is interpreted to mean that Key is large
364 -- relative to the nodes in the tree. If Key is greater than the last
365 -- node in the tree, then we're done; otherwise the hint was "wrong" and
366 -- we must search.
368 if Position = null then -- largest
369 begin
370 B := B + 1;
371 L := L + 1;
373 Compare := Is_Greater_Key_Node (Key, Tree.Last);
375 L := L - 1;
376 B := B - 1;
378 exception
379 when others =>
380 L := L - 1;
381 B := B - 1;
383 raise;
384 end;
386 if Compare then
387 Insert_Post (Tree, Tree.Last, False, Node);
388 Inserted := True;
389 else
390 Conditional_Insert_Sans_Hint (Tree, Key, Node, Inserted);
391 end if;
393 return;
394 end if;
396 pragma Assert (Tree.Length > 0);
398 -- A hint can either name the node that immediately follows Key,
399 -- or immediately precedes Key. We first test whether Key is
400 -- less than the hint, and if so we compare Key to the node that
401 -- precedes the hint. If Key is both less than the hint and
402 -- greater than the hint's preceding neighbor, then we're done;
403 -- otherwise we must search.
405 -- Note also that a hint can either be an anterior node or a leaf
406 -- node. A new node is always inserted at the bottom of the tree
407 -- (at least prior to rebalancing), becoming the new left or
408 -- right child of leaf node (which prior to the insertion must
409 -- necessarily be null, since this is a leaf). If the hint names
410 -- an anterior node then its neighbor must be a leaf, and so
411 -- (here) we insert after the neighbor. If the hint names a leaf
412 -- then its neighbor must be anterior and so we insert before the
413 -- hint.
415 begin
416 B := B + 1;
417 L := L + 1;
419 Compare := Is_Less_Key_Node (Key, Position);
421 L := L - 1;
422 B := B - 1;
424 exception
425 when others =>
426 L := L - 1;
427 B := B - 1;
429 raise;
430 end;
432 if Compare then
433 Test := Ops.Previous (Position); -- "before"
435 if Test = null then -- new first node
436 Insert_Post (Tree, Tree.First, True, Node);
438 Inserted := True;
439 return;
440 end if;
442 begin
443 B := B + 1;
444 L := L + 1;
446 Compare := Is_Greater_Key_Node (Key, Test);
448 L := L - 1;
449 B := B - 1;
451 exception
452 when others =>
453 L := L - 1;
454 B := B - 1;
456 raise;
457 end;
459 if Compare then
460 if Ops.Right (Test) = null then
461 Insert_Post (Tree, Test, False, Node);
462 else
463 Insert_Post (Tree, Position, True, Node);
464 end if;
466 Inserted := True;
468 else
469 Conditional_Insert_Sans_Hint (Tree, Key, Node, Inserted);
470 end if;
472 return;
473 end if;
475 -- We know that Key isn't less than the hint so we try again, this time
476 -- to see if it's greater than the hint. If so we compare Key to the
477 -- node that follows the hint. If Key is both greater than the hint and
478 -- less than the hint's next neighbor, then we're done; otherwise we
479 -- must search.
481 begin
482 B := B + 1;
483 L := L + 1;
485 Compare := Is_Greater_Key_Node (Key, Position);
487 L := L - 1;
488 B := B - 1;
490 exception
491 when others =>
492 L := L - 1;
493 B := B - 1;
495 raise;
496 end;
498 if Compare then
499 Test := Ops.Next (Position); -- "after"
501 if Test = null then -- new last node
502 Insert_Post (Tree, Tree.Last, False, Node);
504 Inserted := True;
505 return;
506 end if;
508 begin
509 B := B + 1;
510 L := L + 1;
512 Compare := Is_Less_Key_Node (Key, Test);
514 L := L - 1;
515 B := B - 1;
517 exception
518 when others =>
519 L := L - 1;
520 B := B - 1;
522 raise;
523 end;
525 if Compare then
526 if Ops.Right (Position) = null then
527 Insert_Post (Tree, Position, False, Node);
528 else
529 Insert_Post (Tree, Test, True, Node);
530 end if;
532 Inserted := True;
534 else
535 Conditional_Insert_Sans_Hint (Tree, Key, Node, Inserted);
536 end if;
538 return;
539 end if;
541 -- We know that Key is neither less than the hint nor greater than the
542 -- hint, and that's the definition of equivalence. There's nothing else
543 -- we need to do, since a search would just reach the same conclusion.
545 Node := Position;
546 Inserted := False;
547 end Generic_Conditional_Insert_With_Hint;
549 -------------------------
550 -- Generic_Insert_Post --
551 -------------------------
553 procedure Generic_Insert_Post
554 (Tree : in out Tree_Type;
555 Y : Node_Access;
556 Before : Boolean;
557 Z : out Node_Access)
559 begin
560 if Tree.Length = Count_Type'Last then
561 raise Constraint_Error with "too many elements";
562 end if;
564 if Tree.Busy > 0 then
565 raise Program_Error with
566 "attempt to tamper with cursors (container is busy)";
567 end if;
569 Z := New_Node;
570 pragma Assert (Z /= null);
571 pragma Assert (Ops.Color (Z) = Red);
573 if Y = null then
574 pragma Assert (Tree.Length = 0);
575 pragma Assert (Tree.Root = null);
576 pragma Assert (Tree.First = null);
577 pragma Assert (Tree.Last = null);
579 Tree.Root := Z;
580 Tree.First := Z;
581 Tree.Last := Z;
583 elsif Before then
584 pragma Assert (Ops.Left (Y) = null);
586 Ops.Set_Left (Y, Z);
588 if Y = Tree.First then
589 Tree.First := Z;
590 end if;
592 else
593 pragma Assert (Ops.Right (Y) = null);
595 Ops.Set_Right (Y, Z);
597 if Y = Tree.Last then
598 Tree.Last := Z;
599 end if;
600 end if;
602 Ops.Set_Parent (Z, Y);
603 Ops.Rebalance_For_Insert (Tree, Z);
604 Tree.Length := Tree.Length + 1;
605 end Generic_Insert_Post;
607 -----------------------
608 -- Generic_Iteration --
609 -----------------------
611 procedure Generic_Iteration
612 (Tree : Tree_Type;
613 Key : Key_Type)
615 procedure Iterate (Node : Node_Access);
617 -------------
618 -- Iterate --
619 -------------
621 procedure Iterate (Node : Node_Access) is
622 N : Node_Access;
623 begin
624 N := Node;
625 while N /= null loop
626 if Is_Less_Key_Node (Key, N) then
627 N := Ops.Left (N);
628 elsif Is_Greater_Key_Node (Key, N) then
629 N := Ops.Right (N);
630 else
631 Iterate (Ops.Left (N));
632 Process (N);
633 N := Ops.Right (N);
634 end if;
635 end loop;
636 end Iterate;
638 -- Start of processing for Generic_Iteration
640 begin
641 Iterate (Tree.Root);
642 end Generic_Iteration;
644 -------------------------------
645 -- Generic_Reverse_Iteration --
646 -------------------------------
648 procedure Generic_Reverse_Iteration
649 (Tree : Tree_Type;
650 Key : Key_Type)
652 procedure Iterate (Node : Node_Access);
654 -------------
655 -- Iterate --
656 -------------
658 procedure Iterate (Node : Node_Access) is
659 N : Node_Access;
660 begin
661 N := Node;
662 while N /= null loop
663 if Is_Less_Key_Node (Key, N) then
664 N := Ops.Left (N);
665 elsif Is_Greater_Key_Node (Key, N) then
666 N := Ops.Right (N);
667 else
668 Iterate (Ops.Right (N));
669 Process (N);
670 N := Ops.Left (N);
671 end if;
672 end loop;
673 end Iterate;
675 -- Start of processing for Generic_Reverse_Iteration
677 begin
678 Iterate (Tree.Root);
679 end Generic_Reverse_Iteration;
681 ----------------------------------
682 -- Generic_Unconditional_Insert --
683 ----------------------------------
685 procedure Generic_Unconditional_Insert
686 (Tree : in out Tree_Type;
687 Key : Key_Type;
688 Node : out Node_Access)
690 Y : Node_Access;
691 X : Node_Access;
693 Before : Boolean;
695 begin
696 Y := null;
697 Before := False;
699 X := Tree.Root;
700 while X /= null loop
701 Y := X;
702 Before := Is_Less_Key_Node (Key, X);
703 X := (if Before then Ops.Left (X) else Ops.Right (X));
704 end loop;
706 Insert_Post (Tree, Y, Before, Node);
707 end Generic_Unconditional_Insert;
709 --------------------------------------------
710 -- Generic_Unconditional_Insert_With_Hint --
711 --------------------------------------------
713 procedure Generic_Unconditional_Insert_With_Hint
714 (Tree : in out Tree_Type;
715 Hint : Node_Access;
716 Key : Key_Type;
717 Node : out Node_Access)
719 begin
720 -- There are fewer constraints for an unconditional insertion
721 -- than for a conditional insertion, since we allow duplicate
722 -- keys. So instead of having to check (say) whether Key is
723 -- (strictly) greater than the hint's previous neighbor, here we
724 -- allow Key to be equal to or greater than the previous node.
726 -- There is the issue of what to do if Key is equivalent to the
727 -- hint. Does the new node get inserted before or after the hint?
728 -- We decide that it gets inserted after the hint, reasoning that
729 -- this is consistent with behavior for non-hint insertion, which
730 -- inserts a new node after existing nodes with equivalent keys.
732 -- First we check whether the hint is null, which is interpreted
733 -- to mean that Key is large relative to existing nodes.
734 -- Following our rule above, if Key is equal to or greater than
735 -- the last node, then we insert the new node immediately after
736 -- last. (We don't have an operation for testing whether a key is
737 -- "equal to or greater than" a node, so we must say instead "not
738 -- less than", which is equivalent.)
740 if Hint = null then -- largest
741 if Tree.Last = null then
742 Insert_Post (Tree, null, False, Node);
743 elsif Is_Less_Key_Node (Key, Tree.Last) then
744 Unconditional_Insert_Sans_Hint (Tree, Key, Node);
745 else
746 Insert_Post (Tree, Tree.Last, False, Node);
747 end if;
749 return;
750 end if;
752 pragma Assert (Tree.Length > 0);
754 -- We decide here whether to insert the new node prior to the
755 -- hint. Key could be equivalent to the hint, so in theory we
756 -- could write the following test as "not greater than" (same as
757 -- "less than or equal to"). If Key were equivalent to the hint,
758 -- that would mean that the new node gets inserted before an
759 -- equivalent node. That wouldn't break any container invariants,
760 -- but our rule above says that new nodes always get inserted
761 -- after equivalent nodes. So here we test whether Key is both
762 -- less than the hint and equal to or greater than the hint's
763 -- previous neighbor, and if so insert it before the hint.
765 if Is_Less_Key_Node (Key, Hint) then
766 declare
767 Before : constant Node_Access := Ops.Previous (Hint);
768 begin
769 if Before = null then
770 Insert_Post (Tree, Hint, True, Node);
771 elsif Is_Less_Key_Node (Key, Before) then
772 Unconditional_Insert_Sans_Hint (Tree, Key, Node);
773 elsif Ops.Right (Before) = null then
774 Insert_Post (Tree, Before, False, Node);
775 else
776 Insert_Post (Tree, Hint, True, Node);
777 end if;
778 end;
780 return;
781 end if;
783 -- We know that Key isn't less than the hint, so it must be equal
784 -- or greater. So we just test whether Key is less than or equal
785 -- to (same as "not greater than") the hint's next neighbor, and
786 -- if so insert it after the hint.
788 declare
789 After : constant Node_Access := Ops.Next (Hint);
790 begin
791 if After = null then
792 Insert_Post (Tree, Hint, False, Node);
793 elsif Is_Greater_Key_Node (Key, After) then
794 Unconditional_Insert_Sans_Hint (Tree, Key, Node);
795 elsif Ops.Right (Hint) = null then
796 Insert_Post (Tree, Hint, False, Node);
797 else
798 Insert_Post (Tree, After, True, Node);
799 end if;
800 end;
801 end Generic_Unconditional_Insert_With_Hint;
803 -----------------
804 -- Upper_Bound --
805 -----------------
807 function Upper_Bound
808 (Tree : Tree_Type;
809 Key : Key_Type) return Node_Access
811 Y : Node_Access;
812 X : Node_Access;
814 begin
815 X := Tree.Root;
816 while X /= null loop
817 if Is_Less_Key_Node (Key, X) then
818 Y := X;
819 X := Ops.Left (X);
820 else
821 X := Ops.Right (X);
822 end if;
823 end loop;
825 return Y;
826 end Upper_Bound;
828 end Ada.Containers.Red_Black_Trees.Generic_Keys;