1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.INDEFINITE_ORDERED_MAPS --
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 with Ada.Unchecked_Deallocation;
32 with Ada.Containers.Red_Black_Trees.Generic_Operations;
33 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Operations);
35 with Ada.Containers.Red_Black_Trees.Generic_Keys;
36 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Keys);
38 with System; use type System.Address;
40 package body Ada.Containers.Indefinite_Ordered_Maps is
41 pragma Suppress (All_Checks);
43 -----------------------------
44 -- Node Access Subprograms --
45 -----------------------------
47 -- These subprograms provide a functional interface to access fields
48 -- of a node, and a procedural interface for modifying these values.
50 function Color (Node : Node_Access) return Color_Type;
51 pragma Inline (Color);
53 function Left (Node : Node_Access) return Node_Access;
56 function Parent (Node : Node_Access) return Node_Access;
57 pragma Inline (Parent);
59 function Right (Node : Node_Access) return Node_Access;
60 pragma Inline (Right);
62 procedure Set_Parent (Node : Node_Access; Parent : Node_Access);
63 pragma Inline (Set_Parent);
65 procedure Set_Left (Node : Node_Access; Left : Node_Access);
66 pragma Inline (Set_Left);
68 procedure Set_Right (Node : Node_Access; Right : Node_Access);
69 pragma Inline (Set_Right);
71 procedure Set_Color (Node : Node_Access; Color : Color_Type);
72 pragma Inline (Set_Color);
74 -----------------------
75 -- Local Subprograms --
76 -----------------------
78 function Copy_Node (Source : Node_Access) return Node_Access;
79 pragma Inline (Copy_Node);
81 procedure Free (X : in out Node_Access);
83 function Is_Equal_Node_Node
84 (L, R : Node_Access) return Boolean;
85 pragma Inline (Is_Equal_Node_Node);
87 function Is_Greater_Key_Node
89 Right : Node_Access) return Boolean;
90 pragma Inline (Is_Greater_Key_Node);
92 function Is_Less_Key_Node
94 Right : Node_Access) return Boolean;
95 pragma Inline (Is_Less_Key_Node);
97 --------------------------
98 -- Local Instantiations --
99 --------------------------
101 package Tree_Operations is
102 new Red_Black_Trees.Generic_Operations (Tree_Types);
104 procedure Delete_Tree is
105 new Tree_Operations.Generic_Delete_Tree (Free);
107 function Copy_Tree is
108 new Tree_Operations.Generic_Copy_Tree (Copy_Node, Delete_Tree);
113 new Red_Black_Trees.Generic_Keys
114 (Tree_Operations => Tree_Operations,
115 Key_Type => Key_Type,
116 Is_Less_Key_Node => Is_Less_Key_Node,
117 Is_Greater_Key_Node => Is_Greater_Key_Node);
119 procedure Free_Key is
120 new Ada.Unchecked_Deallocation (Key_Type, Key_Access);
122 procedure Free_Element is
123 new Ada.Unchecked_Deallocation (Element_Type, Element_Access);
126 new Tree_Operations.Generic_Equal (Is_Equal_Node_Node);
132 function "<" (Left, Right : Cursor) return Boolean is
134 if Left.Node = null then
135 raise Constraint_Error with "Left cursor of ""<"" equals No_Element";
138 if Right.Node = null then
139 raise Constraint_Error with "Right cursor of ""<"" equals No_Element";
142 if Left.Node.Key = null then
143 raise Program_Error with "Left cursor in ""<"" is bad";
146 if Right.Node.Key = null then
147 raise Program_Error with "Right cursor in ""<"" is bad";
150 pragma Assert (Vet (Left.Container.Tree, Left.Node),
151 "Left cursor in ""<"" is bad");
153 pragma Assert (Vet (Right.Container.Tree, Right.Node),
154 "Right cursor in ""<"" is bad");
156 return Left.Node.Key.all < Right.Node.Key.all;
159 function "<" (Left : Cursor; Right : Key_Type) return Boolean is
161 if Left.Node = null then
162 raise Constraint_Error with "Left cursor of ""<"" equals No_Element";
165 if Left.Node.Key = null then
166 raise Program_Error with "Left cursor in ""<"" is bad";
169 pragma Assert (Vet (Left.Container.Tree, Left.Node),
170 "Left cursor in ""<"" is bad");
172 return Left.Node.Key.all < Right;
175 function "<" (Left : Key_Type; Right : Cursor) return Boolean is
177 if Right.Node = null then
178 raise Constraint_Error with "Right cursor of ""<"" equals No_Element";
181 if Right.Node.Key = null then
182 raise Program_Error with "Right cursor in ""<"" is bad";
185 pragma Assert (Vet (Right.Container.Tree, Right.Node),
186 "Right cursor in ""<"" is bad");
188 return Left < Right.Node.Key.all;
195 function "=" (Left, Right : Map) return Boolean is
197 return Is_Equal (Left.Tree, Right.Tree);
204 function ">" (Left, Right : Cursor) return Boolean is
206 if Left.Node = null then
207 raise Constraint_Error with "Left cursor of "">"" equals No_Element";
210 if Right.Node = null then
211 raise Constraint_Error with "Right cursor of "">"" equals No_Element";
214 if Left.Node.Key = null then
215 raise Program_Error with "Left cursor in ""<"" is bad";
218 if Right.Node.Key = null then
219 raise Program_Error with "Right cursor in ""<"" is bad";
222 pragma Assert (Vet (Left.Container.Tree, Left.Node),
223 "Left cursor in "">"" is bad");
225 pragma Assert (Vet (Right.Container.Tree, Right.Node),
226 "Right cursor in "">"" is bad");
228 return Right.Node.Key.all < Left.Node.Key.all;
231 function ">" (Left : Cursor; Right : Key_Type) return Boolean is
233 if Left.Node = null then
234 raise Constraint_Error with "Left cursor of "">"" equals No_Element";
237 if Left.Node.Key = null then
238 raise Program_Error with "Left cursor in ""<"" is bad";
241 pragma Assert (Vet (Left.Container.Tree, Left.Node),
242 "Left cursor in "">"" is bad");
244 return Right < Left.Node.Key.all;
247 function ">" (Left : Key_Type; Right : Cursor) return Boolean is
249 if Right.Node = null then
250 raise Constraint_Error with "Right cursor of "">"" equals No_Element";
253 if Right.Node.Key = null then
254 raise Program_Error with "Right cursor in ""<"" is bad";
257 pragma Assert (Vet (Right.Container.Tree, Right.Node),
258 "Right cursor in "">"" is bad");
260 return Right.Node.Key.all < Left;
267 procedure Adjust is new Tree_Operations.Generic_Adjust (Copy_Tree);
269 procedure Adjust (Container : in out Map) is
271 Adjust (Container.Tree);
274 procedure Adjust (Control : in out Reference_Control_Type) is
276 if Control.Container /= null then
278 T : Tree_Type renames Control.Container.all.Tree;
279 B : Natural renames T.Busy;
280 L : Natural renames T.Lock;
292 procedure Assign (Target : in out Map; Source : Map) is
293 procedure Insert_Item (Node : Node_Access);
294 pragma Inline (Insert_Item);
296 procedure Insert_Items is
297 new Tree_Operations.Generic_Iteration (Insert_Item);
303 procedure Insert_Item (Node : Node_Access) is
305 Target.Insert (Key => Node.Key.all, New_Item => Node.Element.all);
308 -- Start of processing for Assign
311 if Target'Address = Source'Address then
316 Insert_Items (Target.Tree);
323 function Ceiling (Container : Map; Key : Key_Type) return Cursor is
324 Node : constant Node_Access := Key_Ops.Ceiling (Container.Tree, Key);
326 return (if Node = null then No_Element
327 else Cursor'(Container'Unrestricted_Access, Node));
334 procedure Clear is new Tree_Operations.Generic_Clear (Delete_Tree);
336 procedure Clear (Container : in out Map) is
338 Clear (Container.Tree);
345 function Color (Node : Node_Access) return Color_Type is
350 ------------------------
351 -- Constant_Reference --
352 ------------------------
354 function Constant_Reference
355 (Container : aliased Map;
356 Position : Cursor) return Constant_Reference_Type
359 if Position.Container = null then
360 raise Constraint_Error with
361 "Position cursor has no element";
364 if Position.Container /= Container'Unrestricted_Access then
365 raise Program_Error with
366 "Position cursor designates wrong map";
369 if Position.Node.Element = null then
370 raise Program_Error with "Node has no element";
373 pragma Assert (Vet (Container.Tree, Position.Node),
374 "Position cursor in Constant_Reference is bad");
377 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
378 B : Natural renames T.Busy;
379 L : Natural renames T.Lock;
381 return R : constant Constant_Reference_Type :=
382 (Element => Position.Node.Element.all'Access,
383 Control => (Controlled with Container'Unrestricted_Access))
389 end Constant_Reference;
391 function Constant_Reference
392 (Container : aliased Map;
393 Key : Key_Type) return Constant_Reference_Type
395 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
399 raise Constraint_Error with "key not in map";
402 if Node.Element = null then
403 raise Program_Error with "Node has no element";
407 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
408 B : Natural renames T.Busy;
409 L : Natural renames T.Lock;
411 return R : constant Constant_Reference_Type :=
412 (Element => Node.Element.all'Access,
413 Control => (Controlled with Container'Unrestricted_Access))
419 end Constant_Reference;
425 function Contains (Container : Map; Key : Key_Type) return Boolean is
427 return Find (Container, Key) /= No_Element;
434 function Copy (Source : Map) return Map is
436 return Target : Map do
437 Target.Assign (Source);
445 function Copy_Node (Source : Node_Access) return Node_Access is
446 K : Key_Access := new Key_Type'(Source.Key.all);
450 E := new Element_Type'(Source.Element.all);
452 return new Node_Type'(Parent => null,
455 Color => Source.Color,
470 (Container : in out Map;
471 Position : in out Cursor)
474 if Position.Node = null then
475 raise Constraint_Error with
476 "Position cursor of Delete equals No_Element";
479 if Position.Node.Key = null
480 or else Position.Node.Element = null
482 raise Program_Error with "Position cursor of Delete is bad";
485 if Position.Container /= Container'Unrestricted_Access then
486 raise Program_Error with
487 "Position cursor of Delete designates wrong map";
490 pragma Assert (Vet (Container.Tree, Position.Node),
491 "Position cursor of Delete is bad");
493 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
494 Free (Position.Node);
496 Position.Container := null;
499 procedure Delete (Container : in out Map; Key : Key_Type) is
500 X : Node_Access := Key_Ops.Find (Container.Tree, Key);
504 raise Constraint_Error with "key not in map";
507 Delete_Node_Sans_Free (Container.Tree, X);
515 procedure Delete_First (Container : in out Map) is
516 X : Node_Access := Container.Tree.First;
519 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
528 procedure Delete_Last (Container : in out Map) is
529 X : Node_Access := Container.Tree.Last;
532 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
541 function Element (Position : Cursor) return Element_Type is
543 if Position.Node = null then
544 raise Constraint_Error with
545 "Position cursor of function Element equals No_Element";
548 if Position.Node.Element = null then
549 raise Program_Error with
550 "Position cursor of function Element is bad";
553 pragma Assert (Vet (Position.Container.Tree, Position.Node),
554 "Position cursor of function Element is bad");
556 return Position.Node.Element.all;
559 function Element (Container : Map; Key : Key_Type) return Element_Type is
560 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
564 raise Constraint_Error with "key not in map";
567 return Node.Element.all;
570 ---------------------
571 -- Equivalent_Keys --
572 ---------------------
574 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
576 return (if Left < Right or else Right < Left then False else True);
583 procedure Exclude (Container : in out Map; Key : Key_Type) is
584 X : Node_Access := Key_Ops.Find (Container.Tree, Key);
587 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
596 procedure Finalize (Object : in out Iterator) is
598 if Object.Container /= null then
600 B : Natural renames Object.Container.all.Tree.Busy;
607 procedure Finalize (Control : in out Reference_Control_Type) is
609 if Control.Container /= null then
611 T : Tree_Type renames Control.Container.all.Tree;
612 B : Natural renames T.Busy;
613 L : Natural renames T.Lock;
619 Control.Container := null;
627 function Find (Container : Map; Key : Key_Type) return Cursor is
628 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
630 return (if Node = null then No_Element
631 else Cursor'(Container'Unrestricted_Access, Node));
638 function First (Container : Map) return Cursor is
639 T : Tree_Type renames Container.Tree;
641 return (if T.First = null then No_Element
642 else Cursor'(Container'Unrestricted_Access, T.First));
645 function First (Object : Iterator) return Cursor is
647 -- The value of the iterator object's Node component influences the
648 -- behavior of the First (and Last) selector function.
650 -- When the Node component is null, this means the iterator object was
651 -- constructed without a start expression, in which case the (forward)
652 -- iteration starts from the (logical) beginning of the entire sequence
653 -- of items (corresponding to Container.First for a forward iterator).
655 -- Otherwise, this is iteration over a partial sequence of items. When
656 -- the Node component is non-null, the iterator object was constructed
657 -- with a start expression, that specifies the position from which the
658 -- (forward) partial iteration begins.
660 if Object.Node = null then
661 return Object.Container.First;
663 return Cursor'(Object.Container, Object.Node);
671 function First_Element (Container : Map) return Element_Type is
672 T : Tree_Type renames Container.Tree;
674 if T.First = null then
675 raise Constraint_Error with "map is empty";
677 return T.First.Element.all;
685 function First_Key (Container : Map) return Key_Type is
686 T : Tree_Type renames Container.Tree;
688 if T.First = null then
689 raise Constraint_Error with "map is empty";
691 return T.First.Key.all;
699 function Floor (Container : Map; Key : Key_Type) return Cursor is
700 Node : constant Node_Access := Key_Ops.Floor (Container.Tree, Key);
702 return (if Node = null then No_Element
703 else Cursor'(Container'Unrestricted_Access, Node));
710 procedure Free (X : in out Node_Access) is
711 procedure Deallocate is
712 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
731 Free_Element (X.Element);
742 Free_Element (X.Element);
759 function Has_Element (Position : Cursor) return Boolean is
761 return Position /= No_Element;
769 (Container : in out Map;
771 New_Item : Element_Type)
780 Insert (Container, Key, New_Item, Position, Inserted);
783 if Container.Tree.Lock > 0 then
784 raise Program_Error with
785 "attempt to tamper with elements (map is locked)";
788 K := Position.Node.Key;
789 E := Position.Node.Element;
791 Position.Node.Key := new Key_Type'(Key);
794 -- The element allocator may need an accessibility check in the
795 -- case the actual type is class-wide or has access discriminants
796 -- (see RM 4.8(10.1) and AI12-0035).
798 pragma Unsuppress (Accessibility_Check);
801 Position.Node.Element := new Element_Type'(New_Item);
819 (Container : in out Map;
821 New_Item : Element_Type;
822 Position : out Cursor;
823 Inserted : out Boolean)
825 function New_Node return Node_Access;
826 pragma Inline (New_Node);
828 procedure Insert_Post is
829 new Key_Ops.Generic_Insert_Post (New_Node);
831 procedure Insert_Sans_Hint is
832 new Key_Ops.Generic_Conditional_Insert (Insert_Post);
838 function New_Node return Node_Access is
839 Node : Node_Access := new Node_Type;
841 -- The element allocator may need an accessibility check in the case
842 -- the actual type is class-wide or has access discriminants (see
843 -- RM 4.8(10.1) and AI12-0035).
845 pragma Unsuppress (Accessibility_Check);
848 Node.Key := new Key_Type'(Key);
849 Node.Element := new Element_Type'(New_Item);
855 -- On exception, deallocate key and elem. Note that free
856 -- deallocates both the key and the elem.
862 -- Start of processing for Insert
871 Position.Container := Container'Unrestricted_Access;
875 (Container : in out Map;
877 New_Item : Element_Type)
880 pragma Unreferenced (Position);
885 Insert (Container, Key, New_Item, Position, Inserted);
888 raise Constraint_Error with "key already in map";
896 function Is_Empty (Container : Map) return Boolean is
898 return Container.Tree.Length = 0;
901 ------------------------
902 -- Is_Equal_Node_Node --
903 ------------------------
905 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
907 return (if L.Key.all < R.Key.all then False
908 elsif R.Key.all < L.Key.all then False
909 else L.Element.all = R.Element.all);
910 end Is_Equal_Node_Node;
912 -------------------------
913 -- Is_Greater_Key_Node --
914 -------------------------
916 function Is_Greater_Key_Node
918 Right : Node_Access) return Boolean
921 -- k > node same as node < k
923 return Right.Key.all < Left;
924 end Is_Greater_Key_Node;
926 ----------------------
927 -- Is_Less_Key_Node --
928 ----------------------
930 function Is_Less_Key_Node
932 Right : Node_Access) return Boolean is
934 return Left < Right.Key.all;
935 end Is_Less_Key_Node;
943 Process : not null access procedure (Position : Cursor))
945 procedure Process_Node (Node : Node_Access);
946 pragma Inline (Process_Node);
948 procedure Local_Iterate is
949 new Tree_Operations.Generic_Iteration (Process_Node);
955 procedure Process_Node (Node : Node_Access) is
957 Process (Cursor'(Container'Unrestricted_Access, Node));
960 B : Natural renames Container'Unrestricted_Access.all.Tree.Busy;
962 -- Start of processing for Iterate
968 Local_Iterate (Container.Tree);
979 (Container : Map) return Map_Iterator_Interfaces.Reversible_Iterator'Class
981 B : Natural renames Container'Unrestricted_Access.all.Tree.Busy;
984 -- The value of the Node component influences the behavior of the First
985 -- and Last selector functions of the iterator object. When the Node
986 -- component is null (as is the case here), this means the iterator
987 -- object was constructed without a start expression. This is a complete
988 -- iterator, meaning that the iteration starts from the (logical)
989 -- beginning of the sequence of items.
991 -- Note: For a forward iterator, Container.First is the beginning, and
992 -- for a reverse iterator, Container.Last is the beginning.
994 return It : constant Iterator :=
995 (Limited_Controlled with
996 Container => Container'Unrestricted_Access,
1006 return Map_Iterator_Interfaces.Reversible_Iterator'Class
1008 B : Natural renames Container'Unrestricted_Access.all.Tree.Busy;
1011 -- It was formerly the case that when Start = No_Element, the partial
1012 -- iterator was defined to behave the same as for a complete iterator,
1013 -- and iterate over the entire sequence of items. However, those
1014 -- semantics were unintuitive and arguably error-prone (it is too easy
1015 -- to accidentally create an endless loop), and so they were changed,
1016 -- per the ARG meeting in Denver on 2011/11. However, there was no
1017 -- consensus about what positive meaning this corner case should have,
1018 -- and so it was decided to simply raise an exception. This does imply,
1019 -- however, that it is not possible to use a partial iterator to specify
1020 -- an empty sequence of items.
1022 if Start = No_Element then
1023 raise Constraint_Error with
1024 "Start position for iterator equals No_Element";
1027 if Start.Container /= Container'Unrestricted_Access then
1028 raise Program_Error with
1029 "Start cursor of Iterate designates wrong map";
1032 pragma Assert (Vet (Container.Tree, Start.Node),
1033 "Start cursor of Iterate is bad");
1035 -- The value of the Node component influences the behavior of the First
1036 -- and Last selector functions of the iterator object. When the Node
1037 -- component is non-null (as is the case here), it means that this
1038 -- is a partial iteration, over a subset of the complete sequence of
1039 -- items. The iterator object was constructed with a start expression,
1040 -- indicating the position from which the iteration begins. Note that
1041 -- the start position has the same value irrespective of whether this
1042 -- is a forward or reverse iteration.
1044 return It : constant Iterator :=
1045 (Limited_Controlled with
1046 Container => Container'Unrestricted_Access,
1057 function Key (Position : Cursor) return Key_Type is
1059 if Position.Node = null then
1060 raise Constraint_Error with
1061 "Position cursor of function Key equals No_Element";
1064 if Position.Node.Key = null then
1065 raise Program_Error with
1066 "Position cursor of function Key is bad";
1069 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1070 "Position cursor of function Key is bad");
1072 return Position.Node.Key.all;
1079 function Last (Container : Map) return Cursor is
1080 T : Tree_Type renames Container.Tree;
1082 return (if T.Last = null then No_Element
1083 else Cursor'(Container'Unrestricted_Access, T.Last));
1086 function Last (Object : Iterator) return Cursor is
1088 -- The value of the iterator object's Node component influences the
1089 -- behavior of the Last (and First) selector function.
1091 -- When the Node component is null, this means the iterator object was
1092 -- constructed without a start expression, in which case the (reverse)
1093 -- iteration starts from the (logical) beginning of the entire sequence
1094 -- (corresponding to Container.Last, for a reverse iterator).
1096 -- Otherwise, this is iteration over a partial sequence of items. When
1097 -- the Node component is non-null, the iterator object was constructed
1098 -- with a start expression, that specifies the position from which the
1099 -- (reverse) partial iteration begins.
1101 if Object.Node = null then
1102 return Object.Container.Last;
1104 return Cursor'(Object.Container, Object.Node);
1112 function Last_Element (Container : Map) return Element_Type is
1113 T : Tree_Type renames Container.Tree;
1116 if T.Last = null then
1117 raise Constraint_Error with "map is empty";
1120 return T.Last.Element.all;
1127 function Last_Key (Container : Map) return Key_Type is
1128 T : Tree_Type renames Container.Tree;
1131 if T.Last = null then
1132 raise Constraint_Error with "map is empty";
1135 return T.Last.Key.all;
1142 function Left (Node : Node_Access) return Node_Access is
1151 function Length (Container : Map) return Count_Type is
1153 return Container.Tree.Length;
1160 procedure Move is new Tree_Operations.Generic_Move (Clear);
1162 procedure Move (Target : in out Map; Source : in out Map) is
1164 Move (Target => Target.Tree, Source => Source.Tree);
1171 function Next (Position : Cursor) return Cursor is
1173 if Position = No_Element then
1177 pragma Assert (Position.Node /= null);
1178 pragma Assert (Position.Node.Key /= null);
1179 pragma Assert (Position.Node.Element /= null);
1180 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1181 "Position cursor of Next is bad");
1184 Node : constant Node_Access :=
1185 Tree_Operations.Next (Position.Node);
1187 return (if Node = null then No_Element
1188 else Cursor'(Position.Container, Node));
1192 procedure Next (Position : in out Cursor) is
1194 Position := Next (Position);
1199 Position : Cursor) return Cursor
1202 if Position.Container = null then
1206 if Position.Container /= Object.Container then
1207 raise Program_Error with
1208 "Position cursor of Next designates wrong map";
1211 return Next (Position);
1218 function Parent (Node : Node_Access) return Node_Access is
1227 function Previous (Position : Cursor) return Cursor is
1229 if Position = No_Element then
1233 pragma Assert (Position.Node /= null);
1234 pragma Assert (Position.Node.Key /= null);
1235 pragma Assert (Position.Node.Element /= null);
1236 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1237 "Position cursor of Previous is bad");
1240 Node : constant Node_Access :=
1241 Tree_Operations.Previous (Position.Node);
1243 return (if Node = null then No_Element
1244 else Cursor'(Position.Container, Node));
1248 procedure Previous (Position : in out Cursor) is
1250 Position := Previous (Position);
1255 Position : Cursor) return Cursor
1258 if Position.Container = null then
1262 if Position.Container /= Object.Container then
1263 raise Program_Error with
1264 "Position cursor of Previous designates wrong map";
1267 return Previous (Position);
1274 procedure Query_Element
1276 Process : not null access procedure (Key : Key_Type;
1277 Element : Element_Type))
1280 if Position.Node = null then
1281 raise Constraint_Error with
1282 "Position cursor of Query_Element equals No_Element";
1285 if Position.Node.Key = null
1286 or else Position.Node.Element = null
1288 raise Program_Error with
1289 "Position cursor of Query_Element is bad";
1292 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1293 "Position cursor of Query_Element is bad");
1296 T : Tree_Type renames Position.Container.Tree;
1298 B : Natural renames T.Busy;
1299 L : Natural renames T.Lock;
1306 K : Key_Type renames Position.Node.Key.all;
1307 E : Element_Type renames Position.Node.Element.all;
1328 (Stream : not null access Root_Stream_Type'Class;
1329 Container : out Map)
1332 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1333 pragma Inline (Read_Node);
1336 new Tree_Operations.Generic_Read (Clear, Read_Node);
1343 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1345 Node : Node_Access := new Node_Type;
1347 Node.Key := new Key_Type'(Key_Type'Input (Stream));
1348 Node.Element := new Element_Type'(Element_Type'Input (Stream));
1352 Free (Node); -- Note that Free deallocates key and elem too
1356 -- Start of processing for Read
1359 Read (Stream, Container.Tree);
1363 (Stream : not null access Root_Stream_Type'Class;
1367 raise Program_Error with "attempt to stream map cursor";
1371 (Stream : not null access Root_Stream_Type'Class;
1372 Item : out Reference_Type)
1375 raise Program_Error with "attempt to stream reference";
1379 (Stream : not null access Root_Stream_Type'Class;
1380 Item : out Constant_Reference_Type)
1383 raise Program_Error with "attempt to stream reference";
1391 (Container : aliased in out Map;
1392 Position : Cursor) return Reference_Type
1395 if Position.Container = null then
1396 raise Constraint_Error with
1397 "Position cursor has no element";
1400 if Position.Container /= Container'Unrestricted_Access then
1401 raise Program_Error with
1402 "Position cursor designates wrong map";
1405 if Position.Node.Element = null then
1406 raise Program_Error with "Node has no element";
1409 pragma Assert (Vet (Container.Tree, Position.Node),
1410 "Position cursor in function Reference is bad");
1413 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
1414 B : Natural renames T.Busy;
1415 L : Natural renames T.Lock;
1417 return R : constant Reference_Type :=
1418 (Element => Position.Node.Element.all'Access,
1419 Control => (Controlled with Position.Container))
1428 (Container : aliased in out Map;
1429 Key : Key_Type) return Reference_Type
1431 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
1435 raise Constraint_Error with "key not in map";
1438 if Node.Element = null then
1439 raise Program_Error with "Node has no element";
1443 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
1444 B : Natural renames T.Busy;
1445 L : Natural renames T.Lock;
1447 return R : constant Reference_Type :=
1448 (Element => Node.Element.all'Access,
1449 Control => (Controlled with Container'Unrestricted_Access))
1462 (Container : in out Map;
1464 New_Item : Element_Type)
1466 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
1473 raise Constraint_Error with "key not in map";
1476 if Container.Tree.Lock > 0 then
1477 raise Program_Error with
1478 "attempt to tamper with elements (map is locked)";
1484 Node.Key := new Key_Type'(Key);
1487 -- The element allocator may need an accessibility check in the case
1488 -- the actual type is class-wide or has access discriminants (see
1489 -- RM 4.8(10.1) and AI12-0035).
1491 pragma Unsuppress (Accessibility_Check);
1494 Node.Element := new Element_Type'(New_Item);
1506 ---------------------
1507 -- Replace_Element --
1508 ---------------------
1510 procedure Replace_Element
1511 (Container : in out Map;
1513 New_Item : Element_Type)
1516 if Position.Node = null then
1517 raise Constraint_Error with
1518 "Position cursor of Replace_Element equals No_Element";
1521 if Position.Node.Key = null
1522 or else Position.Node.Element = null
1524 raise Program_Error with
1525 "Position cursor of Replace_Element is bad";
1528 if Position.Container /= Container'Unrestricted_Access then
1529 raise Program_Error with
1530 "Position cursor of Replace_Element designates wrong map";
1533 if Container.Tree.Lock > 0 then
1534 raise Program_Error with
1535 "attempt to tamper with elements (map is locked)";
1538 pragma Assert (Vet (Container.Tree, Position.Node),
1539 "Position cursor of Replace_Element is bad");
1542 X : Element_Access := Position.Node.Element;
1544 -- The element allocator may need an accessibility check in the case
1545 -- the actual type is class-wide or has access discriminants (see
1546 -- RM 4.8(10.1) and AI12-0035).
1548 pragma Unsuppress (Accessibility_Check);
1551 Position.Node.Element := new Element_Type'(New_Item);
1554 end Replace_Element;
1556 ---------------------
1557 -- Reverse_Iterate --
1558 ---------------------
1560 procedure Reverse_Iterate
1562 Process : not null access procedure (Position : Cursor))
1564 procedure Process_Node (Node : Node_Access);
1565 pragma Inline (Process_Node);
1567 procedure Local_Reverse_Iterate is
1568 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1574 procedure Process_Node (Node : Node_Access) is
1576 Process (Cursor'(Container'Unrestricted_Access, Node));
1579 B : Natural renames Container.Tree'Unrestricted_Access.all.Busy;
1581 -- Start of processing for Reverse_Iterate
1587 Local_Reverse_Iterate (Container.Tree);
1595 end Reverse_Iterate;
1601 function Right (Node : Node_Access) return Node_Access is
1610 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1612 Node.Color := Color;
1619 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1628 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1630 Node.Parent := Parent;
1637 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1639 Node.Right := Right;
1642 --------------------
1643 -- Update_Element --
1644 --------------------
1646 procedure Update_Element
1647 (Container : in out Map;
1649 Process : not null access procedure (Key : Key_Type;
1650 Element : in out Element_Type))
1653 if Position.Node = null then
1654 raise Constraint_Error with
1655 "Position cursor of Update_Element equals No_Element";
1658 if Position.Node.Key = null
1659 or else Position.Node.Element = null
1661 raise Program_Error with
1662 "Position cursor of Update_Element is bad";
1665 if Position.Container /= Container'Unrestricted_Access then
1666 raise Program_Error with
1667 "Position cursor of Update_Element designates wrong map";
1670 pragma Assert (Vet (Container.Tree, Position.Node),
1671 "Position cursor of Update_Element is bad");
1674 T : Tree_Type renames Position.Container.Tree;
1676 B : Natural renames T.Busy;
1677 L : Natural renames T.Lock;
1684 K : Key_Type renames Position.Node.Key.all;
1685 E : Element_Type renames Position.Node.Element.all;
1707 (Stream : not null access Root_Stream_Type'Class;
1710 procedure Write_Node
1711 (Stream : not null access Root_Stream_Type'Class;
1712 Node : Node_Access);
1713 pragma Inline (Write_Node);
1716 new Tree_Operations.Generic_Write (Write_Node);
1722 procedure Write_Node
1723 (Stream : not null access Root_Stream_Type'Class;
1727 Key_Type'Output (Stream, Node.Key.all);
1728 Element_Type'Output (Stream, Node.Element.all);
1731 -- Start of processing for Write
1734 Write (Stream, Container.Tree);
1738 (Stream : not null access Root_Stream_Type'Class;
1742 raise Program_Error with "attempt to stream map cursor";
1746 (Stream : not null access Root_Stream_Type'Class;
1747 Item : Reference_Type)
1750 raise Program_Error with "attempt to stream reference";
1754 (Stream : not null access Root_Stream_Type'Class;
1755 Item : Constant_Reference_Type)
1758 raise Program_Error with "attempt to stream reference";
1761 end Ada.Containers.Indefinite_Ordered_Maps;