* gcc.dg/predict-12.c: New testcase.
[official-gcc.git] / gcc / ada / a-coorma.adb
blob6083b4cf45b7000759e2170a7fd3d5f57176f5d7
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- A D A . C O N T A I N E R S . O R D E R E D _ M A P S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2004-2015, 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 with Ada.Unchecked_Deallocation;
32 with Ada.Containers.Helpers; use Ada.Containers.Helpers;
34 with Ada.Containers.Red_Black_Trees.Generic_Operations;
35 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Operations);
37 with Ada.Containers.Red_Black_Trees.Generic_Keys;
38 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Keys);
40 with System; use type System.Address;
42 package body Ada.Containers.Ordered_Maps is
44 pragma Warnings (Off, "variable ""Busy*"" is not referenced");
45 pragma Warnings (Off, "variable ""Lock*"" is not referenced");
46 -- See comment in Ada.Containers.Helpers
48 -----------------------------
49 -- Node Access Subprograms --
50 -----------------------------
52 -- These subprograms provide a functional interface to access fields
53 -- of a node, and a procedural interface for modifying these values.
55 function Color (Node : Node_Access) return Color_Type;
56 pragma Inline (Color);
58 function Left (Node : Node_Access) return Node_Access;
59 pragma Inline (Left);
61 function Parent (Node : Node_Access) return Node_Access;
62 pragma Inline (Parent);
64 function Right (Node : Node_Access) return Node_Access;
65 pragma Inline (Right);
67 procedure Set_Parent (Node : Node_Access; Parent : Node_Access);
68 pragma Inline (Set_Parent);
70 procedure Set_Left (Node : Node_Access; Left : Node_Access);
71 pragma Inline (Set_Left);
73 procedure Set_Right (Node : Node_Access; Right : Node_Access);
74 pragma Inline (Set_Right);
76 procedure Set_Color (Node : Node_Access; Color : Color_Type);
77 pragma Inline (Set_Color);
79 -----------------------
80 -- Local Subprograms --
81 -----------------------
83 function Copy_Node (Source : Node_Access) return Node_Access;
84 pragma Inline (Copy_Node);
86 procedure Free (X : in out Node_Access);
88 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean;
89 pragma Inline (Is_Equal_Node_Node);
91 function Is_Greater_Key_Node
92 (Left : Key_Type;
93 Right : Node_Access) return Boolean;
94 pragma Inline (Is_Greater_Key_Node);
96 function Is_Less_Key_Node
97 (Left : Key_Type;
98 Right : Node_Access) return Boolean;
99 pragma Inline (Is_Less_Key_Node);
101 --------------------------
102 -- Local Instantiations --
103 --------------------------
105 package Tree_Operations is
106 new Red_Black_Trees.Generic_Operations (Tree_Types);
108 procedure Delete_Tree is
109 new Tree_Operations.Generic_Delete_Tree (Free);
111 function Copy_Tree is
112 new Tree_Operations.Generic_Copy_Tree (Copy_Node, Delete_Tree);
114 use Tree_Operations;
116 package Key_Ops is
117 new Red_Black_Trees.Generic_Keys
118 (Tree_Operations => Tree_Operations,
119 Key_Type => Key_Type,
120 Is_Less_Key_Node => Is_Less_Key_Node,
121 Is_Greater_Key_Node => Is_Greater_Key_Node);
123 function Is_Equal is
124 new Tree_Operations.Generic_Equal (Is_Equal_Node_Node);
126 ---------
127 -- "<" --
128 ---------
130 function "<" (Left, Right : Cursor) return Boolean is
131 begin
132 if Checks and then Left.Node = null then
133 raise Constraint_Error with "Left cursor of ""<"" equals No_Element";
134 end if;
136 if Checks and then Right.Node = null then
137 raise Constraint_Error with "Right cursor of ""<"" equals No_Element";
138 end if;
140 pragma Assert (Vet (Left.Container.Tree, Left.Node),
141 "Left cursor of ""<"" is bad");
143 pragma Assert (Vet (Right.Container.Tree, Right.Node),
144 "Right cursor of ""<"" is bad");
146 return Left.Node.Key < Right.Node.Key;
147 end "<";
149 function "<" (Left : Cursor; Right : Key_Type) return Boolean is
150 begin
151 if Checks and then Left.Node = null then
152 raise Constraint_Error with "Left cursor of ""<"" equals No_Element";
153 end if;
155 pragma Assert (Vet (Left.Container.Tree, Left.Node),
156 "Left cursor of ""<"" is bad");
158 return Left.Node.Key < Right;
159 end "<";
161 function "<" (Left : Key_Type; Right : Cursor) return Boolean is
162 begin
163 if Checks and then Right.Node = null then
164 raise Constraint_Error with "Right cursor of ""<"" equals No_Element";
165 end if;
167 pragma Assert (Vet (Right.Container.Tree, Right.Node),
168 "Right cursor of ""<"" is bad");
170 return Left < Right.Node.Key;
171 end "<";
173 ---------
174 -- "=" --
175 ---------
177 function "=" (Left, Right : Map) return Boolean is
178 begin
179 return Is_Equal (Left.Tree, Right.Tree);
180 end "=";
182 ---------
183 -- ">" --
184 ---------
186 function ">" (Left, Right : Cursor) return Boolean is
187 begin
188 if Checks and then Left.Node = null then
189 raise Constraint_Error with "Left cursor of "">"" equals No_Element";
190 end if;
192 if Checks and then Right.Node = null then
193 raise Constraint_Error with "Right cursor of "">"" equals No_Element";
194 end if;
196 pragma Assert (Vet (Left.Container.Tree, Left.Node),
197 "Left cursor of "">"" is bad");
199 pragma Assert (Vet (Right.Container.Tree, Right.Node),
200 "Right cursor of "">"" is bad");
202 return Right.Node.Key < Left.Node.Key;
203 end ">";
205 function ">" (Left : Cursor; Right : Key_Type) return Boolean is
206 begin
207 if Checks and then Left.Node = null then
208 raise Constraint_Error with "Left cursor of "">"" equals No_Element";
209 end if;
211 pragma Assert (Vet (Left.Container.Tree, Left.Node),
212 "Left cursor of "">"" is bad");
214 return Right < Left.Node.Key;
215 end ">";
217 function ">" (Left : Key_Type; Right : Cursor) return Boolean is
218 begin
219 if Checks and then Right.Node = null then
220 raise Constraint_Error with "Right cursor of "">"" equals No_Element";
221 end if;
223 pragma Assert (Vet (Right.Container.Tree, Right.Node),
224 "Right cursor of "">"" is bad");
226 return Right.Node.Key < Left;
227 end ">";
229 ------------
230 -- Adjust --
231 ------------
233 procedure Adjust is
234 new Tree_Operations.Generic_Adjust (Copy_Tree);
236 procedure Adjust (Container : in out Map) is
237 begin
238 Adjust (Container.Tree);
239 end Adjust;
241 ------------
242 -- Assign --
243 ------------
245 procedure Assign (Target : in out Map; Source : Map) is
246 procedure Insert_Item (Node : Node_Access);
247 pragma Inline (Insert_Item);
249 procedure Insert_Items is
250 new Tree_Operations.Generic_Iteration (Insert_Item);
252 -----------------
253 -- Insert_Item --
254 -----------------
256 procedure Insert_Item (Node : Node_Access) is
257 begin
258 Target.Insert (Key => Node.Key, New_Item => Node.Element);
259 end Insert_Item;
261 -- Start of processing for Assign
263 begin
264 if Target'Address = Source'Address then
265 return;
266 end if;
268 Target.Clear;
269 Insert_Items (Source.Tree);
270 end Assign;
272 -------------
273 -- Ceiling --
274 -------------
276 function Ceiling (Container : Map; Key : Key_Type) return Cursor is
277 Node : constant Node_Access := Key_Ops.Ceiling (Container.Tree, Key);
279 begin
280 if Node = null then
281 return No_Element;
282 end if;
284 return Cursor'(Container'Unrestricted_Access, Node);
285 end Ceiling;
287 -----------
288 -- Clear --
289 -----------
291 procedure Clear is new Tree_Operations.Generic_Clear (Delete_Tree);
293 procedure Clear (Container : in out Map) is
294 begin
295 Clear (Container.Tree);
296 end Clear;
298 -----------
299 -- Color --
300 -----------
302 function Color (Node : Node_Access) return Color_Type is
303 begin
304 return Node.Color;
305 end Color;
307 ------------------------
308 -- Constant_Reference --
309 ------------------------
311 function Constant_Reference
312 (Container : aliased Map;
313 Position : Cursor) return Constant_Reference_Type
315 begin
316 if Checks and then Position.Container = null then
317 raise Constraint_Error with
318 "Position cursor has no element";
319 end if;
321 if Checks and then Position.Container /= Container'Unrestricted_Access
322 then
323 raise Program_Error with
324 "Position cursor designates wrong map";
325 end if;
327 pragma Assert (Vet (Container.Tree, Position.Node),
328 "Position cursor in Constant_Reference is bad");
330 declare
331 T : Tree_Type renames Position.Container.all.Tree;
332 TC : constant Tamper_Counts_Access :=
333 T.TC'Unrestricted_Access;
334 begin
335 return R : constant Constant_Reference_Type :=
336 (Element => Position.Node.Element'Access,
337 Control => (Controlled with TC))
339 Lock (TC.all);
340 end return;
341 end;
342 end Constant_Reference;
344 function Constant_Reference
345 (Container : aliased Map;
346 Key : Key_Type) return Constant_Reference_Type
348 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
350 begin
351 if Checks and then Node = null then
352 raise Constraint_Error with "key not in map";
353 end if;
355 declare
356 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
357 TC : constant Tamper_Counts_Access :=
358 T.TC'Unrestricted_Access;
359 begin
360 return R : constant Constant_Reference_Type :=
361 (Element => Node.Element'Access,
362 Control => (Controlled with TC))
364 Lock (TC.all);
365 end return;
366 end;
367 end Constant_Reference;
369 --------------
370 -- Contains --
371 --------------
373 function Contains (Container : Map; Key : Key_Type) return Boolean is
374 begin
375 return Find (Container, Key) /= No_Element;
376 end Contains;
378 ----------
379 -- Copy --
380 ----------
382 function Copy (Source : Map) return Map is
383 begin
384 return Target : Map do
385 Target.Assign (Source);
386 end return;
387 end Copy;
389 ---------------
390 -- Copy_Node --
391 ---------------
393 function Copy_Node (Source : Node_Access) return Node_Access is
394 Target : constant Node_Access :=
395 new Node_Type'(Color => Source.Color,
396 Key => Source.Key,
397 Element => Source.Element,
398 Parent => null,
399 Left => null,
400 Right => null);
401 begin
402 return Target;
403 end Copy_Node;
405 ------------
406 -- Delete --
407 ------------
409 procedure Delete (Container : in out Map; Position : in out Cursor) is
410 Tree : Tree_Type renames Container.Tree;
412 begin
413 if Checks and then Position.Node = null then
414 raise Constraint_Error with
415 "Position cursor of Delete equals No_Element";
416 end if;
418 if Checks and then Position.Container /= Container'Unrestricted_Access
419 then
420 raise Program_Error with
421 "Position cursor of Delete designates wrong map";
422 end if;
424 pragma Assert (Vet (Tree, Position.Node),
425 "Position cursor of Delete is bad");
427 Tree_Operations.Delete_Node_Sans_Free (Tree, Position.Node);
428 Free (Position.Node);
430 Position.Container := null;
431 end Delete;
433 procedure Delete (Container : in out Map; Key : Key_Type) is
434 X : Node_Access := Key_Ops.Find (Container.Tree, Key);
436 begin
437 if Checks and then X = null then
438 raise Constraint_Error with "key not in map";
439 end if;
441 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
442 Free (X);
443 end Delete;
445 ------------------
446 -- Delete_First --
447 ------------------
449 procedure Delete_First (Container : in out Map) is
450 X : Node_Access := Container.Tree.First;
452 begin
453 if X /= null then
454 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
455 Free (X);
456 end if;
457 end Delete_First;
459 -----------------
460 -- Delete_Last --
461 -----------------
463 procedure Delete_Last (Container : in out Map) is
464 X : Node_Access := Container.Tree.Last;
466 begin
467 if X /= null then
468 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
469 Free (X);
470 end if;
471 end Delete_Last;
473 -------------
474 -- Element --
475 -------------
477 function Element (Position : Cursor) return Element_Type is
478 begin
479 if Checks and then Position.Node = null then
480 raise Constraint_Error with
481 "Position cursor of function Element equals No_Element";
482 end if;
484 pragma Assert (Vet (Position.Container.Tree, Position.Node),
485 "Position cursor of function Element is bad");
487 return Position.Node.Element;
488 end Element;
490 function Element (Container : Map; Key : Key_Type) return Element_Type is
491 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
493 begin
494 if Checks and then Node = null then
495 raise Constraint_Error with "key not in map";
496 end if;
498 return Node.Element;
499 end Element;
501 ---------------------
502 -- Equivalent_Keys --
503 ---------------------
505 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
506 begin
507 if Left < Right
508 or else Right < Left
509 then
510 return False;
511 else
512 return True;
513 end if;
514 end Equivalent_Keys;
516 -------------
517 -- Exclude --
518 -------------
520 procedure Exclude (Container : in out Map; Key : Key_Type) is
521 X : Node_Access := Key_Ops.Find (Container.Tree, Key);
523 begin
524 if X /= null then
525 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
526 Free (X);
527 end if;
528 end Exclude;
530 --------------
531 -- Finalize --
532 --------------
534 procedure Finalize (Object : in out Iterator) is
535 begin
536 if Object.Container /= null then
537 Unbusy (Object.Container.Tree.TC);
538 end if;
539 end Finalize;
541 ----------
542 -- Find --
543 ----------
545 function Find (Container : Map; Key : Key_Type) return Cursor is
546 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
547 begin
548 return (if Node = null then No_Element
549 else Cursor'(Container'Unrestricted_Access, Node));
550 end Find;
552 -----------
553 -- First --
554 -----------
556 function First (Container : Map) return Cursor is
557 T : Tree_Type renames Container.Tree;
558 begin
559 if T.First = null then
560 return No_Element;
561 else
562 return Cursor'(Container'Unrestricted_Access, T.First);
563 end if;
564 end First;
566 function First (Object : Iterator) return Cursor is
567 begin
568 -- The value of the iterator object's Node component influences the
569 -- behavior of the First (and Last) selector function.
571 -- When the Node component is null, this means the iterator object was
572 -- constructed without a start expression, in which case the (forward)
573 -- iteration starts from the (logical) beginning of the entire sequence
574 -- of items (corresponding to Container.First, for a forward iterator).
576 -- Otherwise, this is iteration over a partial sequence of items. When
577 -- the Node component is non-null, the iterator object was constructed
578 -- with a start expression, that specifies the position from which the
579 -- (forward) partial iteration begins.
581 if Object.Node = null then
582 return Object.Container.First;
583 else
584 return Cursor'(Object.Container, Object.Node);
585 end if;
586 end First;
588 -------------------
589 -- First_Element --
590 -------------------
592 function First_Element (Container : Map) return Element_Type is
593 T : Tree_Type renames Container.Tree;
594 begin
595 if Checks and then T.First = null then
596 raise Constraint_Error with "map is empty";
597 end if;
599 return T.First.Element;
600 end First_Element;
602 ---------------
603 -- First_Key --
604 ---------------
606 function First_Key (Container : Map) return Key_Type is
607 T : Tree_Type renames Container.Tree;
608 begin
609 if Checks and then T.First = null then
610 raise Constraint_Error with "map is empty";
611 end if;
613 return T.First.Key;
614 end First_Key;
616 -----------
617 -- Floor --
618 -----------
620 function Floor (Container : Map; Key : Key_Type) return Cursor is
621 Node : constant Node_Access := Key_Ops.Floor (Container.Tree, Key);
622 begin
623 if Node = null then
624 return No_Element;
625 else
626 return Cursor'(Container'Unrestricted_Access, Node);
627 end if;
628 end Floor;
630 ----------
631 -- Free --
632 ----------
634 procedure Free (X : in out Node_Access) is
635 procedure Deallocate is
636 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
638 begin
639 if X = null then
640 return;
641 end if;
643 X.Parent := X;
644 X.Left := X;
645 X.Right := X;
647 Deallocate (X);
648 end Free;
650 ------------------------
651 -- Get_Element_Access --
652 ------------------------
654 function Get_Element_Access
655 (Position : Cursor) return not null Element_Access is
656 begin
657 return Position.Node.Element'Access;
658 end Get_Element_Access;
660 -----------------
661 -- Has_Element --
662 -----------------
664 function Has_Element (Position : Cursor) return Boolean is
665 begin
666 return Position /= No_Element;
667 end Has_Element;
669 -------------
670 -- Include --
671 -------------
673 procedure Include
674 (Container : in out Map;
675 Key : Key_Type;
676 New_Item : Element_Type)
678 Position : Cursor;
679 Inserted : Boolean;
681 begin
682 Insert (Container, Key, New_Item, Position, Inserted);
684 if not Inserted then
685 TE_Check (Container.Tree.TC);
687 Position.Node.Key := Key;
688 Position.Node.Element := New_Item;
689 end if;
690 end Include;
692 ------------
693 -- Insert --
694 ------------
696 procedure Insert
697 (Container : in out Map;
698 Key : Key_Type;
699 New_Item : Element_Type;
700 Position : out Cursor;
701 Inserted : out Boolean)
703 function New_Node return Node_Access;
704 pragma Inline (New_Node);
706 procedure Insert_Post is
707 new Key_Ops.Generic_Insert_Post (New_Node);
709 procedure Insert_Sans_Hint is
710 new Key_Ops.Generic_Conditional_Insert (Insert_Post);
712 --------------
713 -- New_Node --
714 --------------
716 function New_Node return Node_Access is
717 begin
718 return new Node_Type'(Key => Key,
719 Element => New_Item,
720 Color => Red_Black_Trees.Red,
721 Parent => null,
722 Left => null,
723 Right => null);
724 end New_Node;
726 -- Start of processing for Insert
728 begin
729 Insert_Sans_Hint
730 (Container.Tree,
731 Key,
732 Position.Node,
733 Inserted);
735 Position.Container := Container'Unrestricted_Access;
736 end Insert;
738 procedure Insert
739 (Container : in out Map;
740 Key : Key_Type;
741 New_Item : Element_Type)
743 Position : Cursor;
744 pragma Unreferenced (Position);
746 Inserted : Boolean;
748 begin
749 Insert (Container, Key, New_Item, Position, Inserted);
751 if Checks and then not Inserted then
752 raise Constraint_Error with "key already in map";
753 end if;
754 end Insert;
756 procedure Insert
757 (Container : in out Map;
758 Key : Key_Type;
759 Position : out Cursor;
760 Inserted : out Boolean)
762 function New_Node return Node_Access;
763 pragma Inline (New_Node);
765 procedure Insert_Post is
766 new Key_Ops.Generic_Insert_Post (New_Node);
768 procedure Insert_Sans_Hint is
769 new Key_Ops.Generic_Conditional_Insert (Insert_Post);
771 --------------
772 -- New_Node --
773 --------------
775 function New_Node return Node_Access is
776 begin
777 return new Node_Type'(Key => Key,
778 Element => <>,
779 Color => Red_Black_Trees.Red,
780 Parent => null,
781 Left => null,
782 Right => null);
783 end New_Node;
785 -- Start of processing for Insert
787 begin
788 Insert_Sans_Hint
789 (Container.Tree,
790 Key,
791 Position.Node,
792 Inserted);
794 Position.Container := Container'Unrestricted_Access;
795 end Insert;
797 --------------
798 -- Is_Empty --
799 --------------
801 function Is_Empty (Container : Map) return Boolean is
802 begin
803 return Container.Tree.Length = 0;
804 end Is_Empty;
806 ------------------------
807 -- Is_Equal_Node_Node --
808 ------------------------
810 function Is_Equal_Node_Node
811 (L, R : Node_Access) return Boolean
813 begin
814 if L.Key < R.Key then
815 return False;
816 elsif R.Key < L.Key then
817 return False;
818 else
819 return L.Element = R.Element;
820 end if;
821 end Is_Equal_Node_Node;
823 -------------------------
824 -- Is_Greater_Key_Node --
825 -------------------------
827 function Is_Greater_Key_Node
828 (Left : Key_Type;
829 Right : Node_Access) return Boolean
831 begin
832 -- Left > Right same as Right < Left
834 return Right.Key < Left;
835 end Is_Greater_Key_Node;
837 ----------------------
838 -- Is_Less_Key_Node --
839 ----------------------
841 function Is_Less_Key_Node
842 (Left : Key_Type;
843 Right : Node_Access) return Boolean
845 begin
846 return Left < Right.Key;
847 end Is_Less_Key_Node;
849 -------------
850 -- Iterate --
851 -------------
853 procedure Iterate
854 (Container : Map;
855 Process : not null access procedure (Position : Cursor))
857 procedure Process_Node (Node : Node_Access);
858 pragma Inline (Process_Node);
860 procedure Local_Iterate is
861 new Tree_Operations.Generic_Iteration (Process_Node);
863 ------------------
864 -- Process_Node --
865 ------------------
867 procedure Process_Node (Node : Node_Access) is
868 begin
869 Process (Cursor'(Container'Unrestricted_Access, Node));
870 end Process_Node;
872 Busy : With_Busy (Container.Tree.TC'Unrestricted_Access);
874 -- Start of processing for Iterate
876 begin
877 Local_Iterate (Container.Tree);
878 end Iterate;
880 function Iterate
881 (Container : Map) return Map_Iterator_Interfaces.Reversible_Iterator'Class
883 begin
884 -- The value of the Node component influences the behavior of the First
885 -- and Last selector functions of the iterator object. When the Node
886 -- component is null (as is the case here), this means the iterator
887 -- object was constructed without a start expression. This is a
888 -- complete iterator, meaning that the iteration starts from the
889 -- (logical) beginning of the sequence of items.
891 -- Note: For a forward iterator, Container.First is the beginning, and
892 -- for a reverse iterator, Container.Last is the beginning.
894 return It : constant Iterator :=
895 (Limited_Controlled with
896 Container => Container'Unrestricted_Access,
897 Node => null)
899 Busy (Container.Tree.TC'Unrestricted_Access.all);
900 end return;
901 end Iterate;
903 function Iterate (Container : Map; Start : Cursor)
904 return Map_Iterator_Interfaces.Reversible_Iterator'Class
906 begin
907 -- It was formerly the case that when Start = No_Element, the partial
908 -- iterator was defined to behave the same as for a complete iterator,
909 -- and iterate over the entire sequence of items. However, those
910 -- semantics were unintuitive and arguably error-prone (it is too easy
911 -- to accidentally create an endless loop), and so they were changed,
912 -- per the ARG meeting in Denver on 2011/11. However, there was no
913 -- consensus about what positive meaning this corner case should have,
914 -- and so it was decided to simply raise an exception. This does imply,
915 -- however, that it is not possible to use a partial iterator to specify
916 -- an empty sequence of items.
918 if Checks and then Start = No_Element then
919 raise Constraint_Error with
920 "Start position for iterator equals No_Element";
921 end if;
923 if Checks and then Start.Container /= Container'Unrestricted_Access then
924 raise Program_Error with
925 "Start cursor of Iterate designates wrong map";
926 end if;
928 pragma Assert (Vet (Container.Tree, Start.Node),
929 "Start cursor of Iterate is bad");
931 -- The value of the Node component influences the behavior of the First
932 -- and Last selector functions of the iterator object. When the Node
933 -- component is non-null (as is the case here), it means that this
934 -- is a partial iteration, over a subset of the complete sequence of
935 -- items. The iterator object was constructed with a start expression,
936 -- indicating the position from which the iteration begins. Note that
937 -- the start position has the same value irrespective of whether this
938 -- is a forward or reverse iteration.
940 return It : constant Iterator :=
941 (Limited_Controlled with
942 Container => Container'Unrestricted_Access,
943 Node => Start.Node)
945 Busy (Container.Tree.TC'Unrestricted_Access.all);
946 end return;
947 end Iterate;
949 ---------
950 -- Key --
951 ---------
953 function Key (Position : Cursor) return Key_Type is
954 begin
955 if Checks and then Position.Node = null then
956 raise Constraint_Error with
957 "Position cursor of function Key equals No_Element";
958 end if;
960 pragma Assert (Vet (Position.Container.Tree, Position.Node),
961 "Position cursor of function Key is bad");
963 return Position.Node.Key;
964 end Key;
966 ----------
967 -- Last --
968 ----------
970 function Last (Container : Map) return Cursor is
971 T : Tree_Type renames Container.Tree;
972 begin
973 if T.Last = null then
974 return No_Element;
975 else
976 return Cursor'(Container'Unrestricted_Access, T.Last);
977 end if;
978 end Last;
980 function Last (Object : Iterator) return Cursor is
981 begin
982 -- The value of the iterator object's Node component influences the
983 -- behavior of the Last (and First) selector function.
985 -- When the Node component is null, this means the iterator object was
986 -- constructed without a start expression, in which case the (reverse)
987 -- iteration starts from the (logical) beginning of the entire sequence
988 -- (corresponding to Container.Last, for a reverse iterator).
990 -- Otherwise, this is iteration over a partial sequence of items. When
991 -- the Node component is non-null, the iterator object was constructed
992 -- with a start expression, that specifies the position from which the
993 -- (reverse) partial iteration begins.
995 if Object.Node = null then
996 return Object.Container.Last;
997 else
998 return Cursor'(Object.Container, Object.Node);
999 end if;
1000 end Last;
1002 ------------------
1003 -- Last_Element --
1004 ------------------
1006 function Last_Element (Container : Map) return Element_Type is
1007 T : Tree_Type renames Container.Tree;
1008 begin
1009 if Checks and then T.Last = null then
1010 raise Constraint_Error with "map is empty";
1011 end if;
1013 return T.Last.Element;
1014 end Last_Element;
1016 --------------
1017 -- Last_Key --
1018 --------------
1020 function Last_Key (Container : Map) return Key_Type is
1021 T : Tree_Type renames Container.Tree;
1022 begin
1023 if Checks and then T.Last = null then
1024 raise Constraint_Error with "map is empty";
1025 end if;
1027 return T.Last.Key;
1028 end Last_Key;
1030 ----------
1031 -- Left --
1032 ----------
1034 function Left (Node : Node_Access) return Node_Access is
1035 begin
1036 return Node.Left;
1037 end Left;
1039 ------------
1040 -- Length --
1041 ------------
1043 function Length (Container : Map) return Count_Type is
1044 begin
1045 return Container.Tree.Length;
1046 end Length;
1048 ----------
1049 -- Move --
1050 ----------
1052 procedure Move is
1053 new Tree_Operations.Generic_Move (Clear);
1055 procedure Move (Target : in out Map; Source : in out Map) is
1056 begin
1057 Move (Target => Target.Tree, Source => Source.Tree);
1058 end Move;
1060 ----------
1061 -- Next --
1062 ----------
1064 procedure Next (Position : in out Cursor) is
1065 begin
1066 Position := Next (Position);
1067 end Next;
1069 function Next (Position : Cursor) return Cursor is
1070 begin
1071 if Position = No_Element then
1072 return No_Element;
1073 end if;
1075 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1076 "Position cursor of Next is bad");
1078 declare
1079 Node : constant Node_Access := Tree_Operations.Next (Position.Node);
1081 begin
1082 if Node = null then
1083 return No_Element;
1084 end if;
1086 return Cursor'(Position.Container, Node);
1087 end;
1088 end Next;
1090 function Next
1091 (Object : Iterator;
1092 Position : Cursor) return Cursor
1094 begin
1095 if Position.Container = null then
1096 return No_Element;
1097 end if;
1099 if Checks and then Position.Container /= Object.Container then
1100 raise Program_Error with
1101 "Position cursor of Next designates wrong map";
1102 end if;
1104 return Next (Position);
1105 end Next;
1107 ------------
1108 -- Parent --
1109 ------------
1111 function Parent (Node : Node_Access) return Node_Access is
1112 begin
1113 return Node.Parent;
1114 end Parent;
1116 --------------
1117 -- Previous --
1118 --------------
1120 procedure Previous (Position : in out Cursor) is
1121 begin
1122 Position := Previous (Position);
1123 end Previous;
1125 function Previous (Position : Cursor) return Cursor is
1126 begin
1127 if Position = No_Element then
1128 return No_Element;
1129 end if;
1131 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1132 "Position cursor of Previous is bad");
1134 declare
1135 Node : constant Node_Access :=
1136 Tree_Operations.Previous (Position.Node);
1138 begin
1139 if Node = null then
1140 return No_Element;
1141 end if;
1143 return Cursor'(Position.Container, Node);
1144 end;
1145 end Previous;
1147 function Previous
1148 (Object : Iterator;
1149 Position : Cursor) return Cursor
1151 begin
1152 if Position.Container = null then
1153 return No_Element;
1154 end if;
1156 if Checks and then Position.Container /= Object.Container then
1157 raise Program_Error with
1158 "Position cursor of Previous designates wrong map";
1159 end if;
1161 return Previous (Position);
1162 end Previous;
1164 ----------------------
1165 -- Pseudo_Reference --
1166 ----------------------
1168 function Pseudo_Reference
1169 (Container : aliased Map'Class) return Reference_Control_Type
1171 TC : constant Tamper_Counts_Access :=
1172 Container.Tree.TC'Unrestricted_Access;
1173 begin
1174 return R : constant Reference_Control_Type := (Controlled with TC) do
1175 Lock (TC.all);
1176 end return;
1177 end Pseudo_Reference;
1179 -------------------
1180 -- Query_Element --
1181 -------------------
1183 procedure Query_Element
1184 (Position : Cursor;
1185 Process : not null access procedure (Key : Key_Type;
1186 Element : Element_Type))
1188 begin
1189 if Checks and then Position.Node = null then
1190 raise Constraint_Error with
1191 "Position cursor of Query_Element equals No_Element";
1192 end if;
1194 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1195 "Position cursor of Query_Element is bad");
1197 declare
1198 T : Tree_Type renames Position.Container.Tree;
1199 Lock : With_Lock (T.TC'Unrestricted_Access);
1200 K : Key_Type renames Position.Node.Key;
1201 E : Element_Type renames Position.Node.Element;
1202 begin
1203 Process (K, E);
1204 end;
1205 end Query_Element;
1207 ----------
1208 -- Read --
1209 ----------
1211 procedure Read
1212 (Stream : not null access Root_Stream_Type'Class;
1213 Container : out Map)
1215 function Read_Node
1216 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1217 pragma Inline (Read_Node);
1219 procedure Read is
1220 new Tree_Operations.Generic_Read (Clear, Read_Node);
1222 ---------------
1223 -- Read_Node --
1224 ---------------
1226 function Read_Node
1227 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1229 Node : Node_Access := new Node_Type;
1230 begin
1231 Key_Type'Read (Stream, Node.Key);
1232 Element_Type'Read (Stream, Node.Element);
1233 return Node;
1234 exception
1235 when others =>
1236 Free (Node);
1237 raise;
1238 end Read_Node;
1240 -- Start of processing for Read
1242 begin
1243 Read (Stream, Container.Tree);
1244 end Read;
1246 procedure Read
1247 (Stream : not null access Root_Stream_Type'Class;
1248 Item : out Cursor)
1250 begin
1251 raise Program_Error with "attempt to stream map cursor";
1252 end Read;
1254 procedure Read
1255 (Stream : not null access Root_Stream_Type'Class;
1256 Item : out Reference_Type)
1258 begin
1259 raise Program_Error with "attempt to stream reference";
1260 end Read;
1262 procedure Read
1263 (Stream : not null access Root_Stream_Type'Class;
1264 Item : out Constant_Reference_Type)
1266 begin
1267 raise Program_Error with "attempt to stream reference";
1268 end Read;
1270 ---------------
1271 -- Reference --
1272 ---------------
1274 function Reference
1275 (Container : aliased in out Map;
1276 Position : Cursor) return Reference_Type
1278 begin
1279 if Checks and then Position.Container = null then
1280 raise Constraint_Error with
1281 "Position cursor has no element";
1282 end if;
1284 if Checks and then Position.Container /= Container'Unrestricted_Access
1285 then
1286 raise Program_Error with
1287 "Position cursor designates wrong map";
1288 end if;
1290 pragma Assert (Vet (Container.Tree, Position.Node),
1291 "Position cursor in function Reference is bad");
1293 declare
1294 T : Tree_Type renames Position.Container.all.Tree;
1295 TC : constant Tamper_Counts_Access :=
1296 T.TC'Unrestricted_Access;
1297 begin
1298 return R : constant Reference_Type :=
1299 (Element => Position.Node.Element'Access,
1300 Control => (Controlled with TC))
1302 Lock (TC.all);
1303 end return;
1304 end;
1305 end Reference;
1307 function Reference
1308 (Container : aliased in out Map;
1309 Key : Key_Type) return Reference_Type
1311 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
1313 begin
1314 if Checks and then Node = null then
1315 raise Constraint_Error with "key not in map";
1316 end if;
1318 declare
1319 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
1320 TC : constant Tamper_Counts_Access :=
1321 T.TC'Unrestricted_Access;
1322 begin
1323 return R : constant Reference_Type :=
1324 (Element => Node.Element'Access,
1325 Control => (Controlled with TC))
1327 Lock (TC.all);
1328 end return;
1329 end;
1330 end Reference;
1332 -------------
1333 -- Replace --
1334 -------------
1336 procedure Replace
1337 (Container : in out Map;
1338 Key : Key_Type;
1339 New_Item : Element_Type)
1341 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
1343 begin
1344 if Checks and then Node = null then
1345 raise Constraint_Error with "key not in map";
1346 end if;
1348 TE_Check (Container.Tree.TC);
1350 Node.Key := Key;
1351 Node.Element := New_Item;
1352 end Replace;
1354 ---------------------
1355 -- Replace_Element --
1356 ---------------------
1358 procedure Replace_Element
1359 (Container : in out Map;
1360 Position : Cursor;
1361 New_Item : Element_Type)
1363 begin
1364 if Checks and then Position.Node = null then
1365 raise Constraint_Error with
1366 "Position cursor of Replace_Element equals No_Element";
1367 end if;
1369 if Checks and then Position.Container /= Container'Unrestricted_Access
1370 then
1371 raise Program_Error with
1372 "Position cursor of Replace_Element designates wrong map";
1373 end if;
1375 TE_Check (Container.Tree.TC);
1377 pragma Assert (Vet (Container.Tree, Position.Node),
1378 "Position cursor of Replace_Element is bad");
1380 Position.Node.Element := New_Item;
1381 end Replace_Element;
1383 ---------------------
1384 -- Reverse_Iterate --
1385 ---------------------
1387 procedure Reverse_Iterate
1388 (Container : Map;
1389 Process : not null access procedure (Position : Cursor))
1391 procedure Process_Node (Node : Node_Access);
1392 pragma Inline (Process_Node);
1394 procedure Local_Reverse_Iterate is
1395 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1397 ------------------
1398 -- Process_Node --
1399 ------------------
1401 procedure Process_Node (Node : Node_Access) is
1402 begin
1403 Process (Cursor'(Container'Unrestricted_Access, Node));
1404 end Process_Node;
1406 Busy : With_Busy (Container.Tree.TC'Unrestricted_Access);
1408 -- Start of processing for Reverse_Iterate
1410 begin
1411 Local_Reverse_Iterate (Container.Tree);
1412 end Reverse_Iterate;
1414 -----------
1415 -- Right --
1416 -----------
1418 function Right (Node : Node_Access) return Node_Access is
1419 begin
1420 return Node.Right;
1421 end Right;
1423 ---------------
1424 -- Set_Color --
1425 ---------------
1427 procedure Set_Color
1428 (Node : Node_Access;
1429 Color : Color_Type)
1431 begin
1432 Node.Color := Color;
1433 end Set_Color;
1435 --------------
1436 -- Set_Left --
1437 --------------
1439 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1440 begin
1441 Node.Left := Left;
1442 end Set_Left;
1444 ----------------
1445 -- Set_Parent --
1446 ----------------
1448 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1449 begin
1450 Node.Parent := Parent;
1451 end Set_Parent;
1453 ---------------
1454 -- Set_Right --
1455 ---------------
1457 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1458 begin
1459 Node.Right := Right;
1460 end Set_Right;
1462 --------------------
1463 -- Update_Element --
1464 --------------------
1466 procedure Update_Element
1467 (Container : in out Map;
1468 Position : Cursor;
1469 Process : not null access procedure (Key : Key_Type;
1470 Element : in out Element_Type))
1472 begin
1473 if Checks and then Position.Node = null then
1474 raise Constraint_Error with
1475 "Position cursor of Update_Element equals No_Element";
1476 end if;
1478 if Checks and then Position.Container /= Container'Unrestricted_Access
1479 then
1480 raise Program_Error with
1481 "Position cursor of Update_Element designates wrong map";
1482 end if;
1484 pragma Assert (Vet (Container.Tree, Position.Node),
1485 "Position cursor of Update_Element is bad");
1487 declare
1488 T : Tree_Type renames Container.Tree;
1489 Lock : With_Lock (T.TC'Unrestricted_Access);
1490 K : Key_Type renames Position.Node.Key;
1491 E : Element_Type renames Position.Node.Element;
1492 begin
1493 Process (K, E);
1494 end;
1495 end Update_Element;
1497 -----------
1498 -- Write --
1499 -----------
1501 procedure Write
1502 (Stream : not null access Root_Stream_Type'Class;
1503 Container : Map)
1505 procedure Write_Node
1506 (Stream : not null access Root_Stream_Type'Class;
1507 Node : Node_Access);
1508 pragma Inline (Write_Node);
1510 procedure Write is
1511 new Tree_Operations.Generic_Write (Write_Node);
1513 ----------------
1514 -- Write_Node --
1515 ----------------
1517 procedure Write_Node
1518 (Stream : not null access Root_Stream_Type'Class;
1519 Node : Node_Access)
1521 begin
1522 Key_Type'Write (Stream, Node.Key);
1523 Element_Type'Write (Stream, Node.Element);
1524 end Write_Node;
1526 -- Start of processing for Write
1528 begin
1529 Write (Stream, Container.Tree);
1530 end Write;
1532 procedure Write
1533 (Stream : not null access Root_Stream_Type'Class;
1534 Item : Cursor)
1536 begin
1537 raise Program_Error with "attempt to stream map cursor";
1538 end Write;
1540 procedure Write
1541 (Stream : not null access Root_Stream_Type'Class;
1542 Item : Reference_Type)
1544 begin
1545 raise Program_Error with "attempt to stream reference";
1546 end Write;
1548 procedure Write
1549 (Stream : not null access Root_Stream_Type'Class;
1550 Item : Constant_Reference_Type)
1552 begin
1553 raise Program_Error with "attempt to stream reference";
1554 end Write;
1556 end Ada.Containers.Ordered_Maps;