1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.INDEFINITE_ORDERED_MAPS --
9 -- Copyright (C) 2004-2012, 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 type Iterator is new Limited_Controlled and
44 Map_Iterator_Interfaces.Reversible_Iterator with
46 Container : Map_Access;
50 overriding procedure Finalize (Object : in out Iterator);
52 overriding function First (Object : Iterator) return Cursor;
53 overriding function Last (Object : Iterator) return Cursor;
55 overriding function Next
57 Position : Cursor) return Cursor;
59 overriding function Previous
61 Position : Cursor) return Cursor;
63 -----------------------------
64 -- Node Access Subprograms --
65 -----------------------------
67 -- These subprograms provide a functional interface to access fields
68 -- of a node, and a procedural interface for modifying these values.
70 function Color (Node : Node_Access) return Color_Type;
71 pragma Inline (Color);
73 function Left (Node : Node_Access) return Node_Access;
76 function Parent (Node : Node_Access) return Node_Access;
77 pragma Inline (Parent);
79 function Right (Node : Node_Access) return Node_Access;
80 pragma Inline (Right);
82 procedure Set_Parent (Node : Node_Access; Parent : Node_Access);
83 pragma Inline (Set_Parent);
85 procedure Set_Left (Node : Node_Access; Left : Node_Access);
86 pragma Inline (Set_Left);
88 procedure Set_Right (Node : Node_Access; Right : Node_Access);
89 pragma Inline (Set_Right);
91 procedure Set_Color (Node : Node_Access; Color : Color_Type);
92 pragma Inline (Set_Color);
94 -----------------------
95 -- Local Subprograms --
96 -----------------------
98 function Copy_Node (Source : Node_Access) return Node_Access;
99 pragma Inline (Copy_Node);
101 procedure Free (X : in out Node_Access);
103 function Is_Equal_Node_Node
104 (L, R : Node_Access) return Boolean;
105 pragma Inline (Is_Equal_Node_Node);
107 function Is_Greater_Key_Node
109 Right : Node_Access) return Boolean;
110 pragma Inline (Is_Greater_Key_Node);
112 function Is_Less_Key_Node
114 Right : Node_Access) return Boolean;
115 pragma Inline (Is_Less_Key_Node);
117 --------------------------
118 -- Local Instantiations --
119 --------------------------
121 package Tree_Operations is
122 new Red_Black_Trees.Generic_Operations (Tree_Types);
124 procedure Delete_Tree is
125 new Tree_Operations.Generic_Delete_Tree (Free);
127 function Copy_Tree is
128 new Tree_Operations.Generic_Copy_Tree (Copy_Node, Delete_Tree);
133 new Red_Black_Trees.Generic_Keys
134 (Tree_Operations => Tree_Operations,
135 Key_Type => Key_Type,
136 Is_Less_Key_Node => Is_Less_Key_Node,
137 Is_Greater_Key_Node => Is_Greater_Key_Node);
139 procedure Free_Key is
140 new Ada.Unchecked_Deallocation (Key_Type, Key_Access);
142 procedure Free_Element is
143 new Ada.Unchecked_Deallocation (Element_Type, Element_Access);
146 new Tree_Operations.Generic_Equal (Is_Equal_Node_Node);
152 function "<" (Left, Right : Cursor) return Boolean is
154 if Left.Node = null then
155 raise Constraint_Error with "Left cursor of ""<"" equals No_Element";
158 if Right.Node = null then
159 raise Constraint_Error with "Right cursor of ""<"" equals No_Element";
162 if Left.Node.Key = null then
163 raise Program_Error with "Left cursor in ""<"" is bad";
166 if Right.Node.Key = null then
167 raise Program_Error with "Right cursor in ""<"" is bad";
170 pragma Assert (Vet (Left.Container.Tree, Left.Node),
171 "Left cursor in ""<"" is bad");
173 pragma Assert (Vet (Right.Container.Tree, Right.Node),
174 "Right cursor in ""<"" is bad");
176 return Left.Node.Key.all < Right.Node.Key.all;
179 function "<" (Left : Cursor; Right : Key_Type) return Boolean is
181 if Left.Node = null then
182 raise Constraint_Error with "Left cursor of ""<"" equals No_Element";
185 if Left.Node.Key = null then
186 raise Program_Error with "Left cursor in ""<"" is bad";
189 pragma Assert (Vet (Left.Container.Tree, Left.Node),
190 "Left cursor in ""<"" is bad");
192 return Left.Node.Key.all < Right;
195 function "<" (Left : Key_Type; Right : Cursor) return Boolean is
197 if Right.Node = null then
198 raise Constraint_Error with "Right cursor of ""<"" equals No_Element";
201 if Right.Node.Key = null then
202 raise Program_Error with "Right cursor in ""<"" is bad";
205 pragma Assert (Vet (Right.Container.Tree, Right.Node),
206 "Right cursor in ""<"" is bad");
208 return Left < Right.Node.Key.all;
215 function "=" (Left, Right : Map) return Boolean is
217 return Is_Equal (Left.Tree, Right.Tree);
224 function ">" (Left, Right : Cursor) return Boolean is
226 if Left.Node = null then
227 raise Constraint_Error with "Left cursor of "">"" equals No_Element";
230 if Right.Node = null then
231 raise Constraint_Error with "Right cursor of "">"" equals No_Element";
234 if Left.Node.Key = null then
235 raise Program_Error with "Left cursor in ""<"" is bad";
238 if Right.Node.Key = null then
239 raise Program_Error with "Right cursor in ""<"" is bad";
242 pragma Assert (Vet (Left.Container.Tree, Left.Node),
243 "Left cursor in "">"" is bad");
245 pragma Assert (Vet (Right.Container.Tree, Right.Node),
246 "Right cursor in "">"" is bad");
248 return Right.Node.Key.all < Left.Node.Key.all;
251 function ">" (Left : Cursor; Right : Key_Type) return Boolean is
253 if Left.Node = null then
254 raise Constraint_Error with "Left cursor of "">"" equals No_Element";
257 if Left.Node.Key = null then
258 raise Program_Error with "Left cursor in ""<"" is bad";
261 pragma Assert (Vet (Left.Container.Tree, Left.Node),
262 "Left cursor in "">"" is bad");
264 return Right < Left.Node.Key.all;
267 function ">" (Left : Key_Type; Right : Cursor) return Boolean is
269 if Right.Node = null then
270 raise Constraint_Error with "Right cursor of "">"" equals No_Element";
273 if Right.Node.Key = null then
274 raise Program_Error with "Right cursor in ""<"" is bad";
277 pragma Assert (Vet (Right.Container.Tree, Right.Node),
278 "Right cursor in "">"" is bad");
280 return Right.Node.Key.all < Left;
287 procedure Adjust is new Tree_Operations.Generic_Adjust (Copy_Tree);
289 procedure Adjust (Container : in out Map) is
291 Adjust (Container.Tree);
294 procedure Adjust (Control : in out Reference_Control_Type) is
296 if Control.Container /= null then
298 T : Tree_Type renames Control.Container.all.Tree;
299 B : Natural renames T.Busy;
300 L : Natural renames T.Lock;
312 procedure Assign (Target : in out Map; Source : Map) is
313 procedure Insert_Item (Node : Node_Access);
314 pragma Inline (Insert_Item);
316 procedure Insert_Items is
317 new Tree_Operations.Generic_Iteration (Insert_Item);
323 procedure Insert_Item (Node : Node_Access) is
325 Target.Insert (Key => Node.Key.all, New_Item => Node.Element.all);
328 -- Start of processing for Assign
331 if Target'Address = Source'Address then
336 Insert_Items (Target.Tree);
343 function Ceiling (Container : Map; Key : Key_Type) return Cursor is
344 Node : constant Node_Access := Key_Ops.Ceiling (Container.Tree, Key);
346 return (if Node = null then No_Element
347 else Cursor'(Container'Unrestricted_Access, Node));
354 procedure Clear is new Tree_Operations.Generic_Clear (Delete_Tree);
356 procedure Clear (Container : in out Map) is
358 Clear (Container.Tree);
365 function Color (Node : Node_Access) return Color_Type is
370 ------------------------
371 -- Constant_Reference --
372 ------------------------
374 function Constant_Reference
375 (Container : aliased Map;
376 Position : Cursor) return Constant_Reference_Type
379 if Position.Container = null then
380 raise Constraint_Error with
381 "Position cursor has no element";
384 if Position.Container /= Container'Unrestricted_Access then
385 raise Program_Error with
386 "Position cursor designates wrong map";
389 if Position.Node.Element = null then
390 raise Program_Error with "Node has no element";
393 pragma Assert (Vet (Container.Tree, Position.Node),
394 "Position cursor in Constant_Reference is bad");
397 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
398 B : Natural renames T.Busy;
399 L : Natural renames T.Lock;
401 return R : constant Constant_Reference_Type :=
402 (Element => Position.Node.Element.all'Access,
404 (Controlled with Container'Unrestricted_Access))
410 end Constant_Reference;
412 function Constant_Reference
414 Key : Key_Type) return Constant_Reference_Type
416 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
420 raise Constraint_Error with "key not in map";
423 if Node.Element = null then
424 raise Program_Error with "Node has no element";
428 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
429 B : Natural renames T.Busy;
430 L : Natural renames T.Lock;
432 return R : constant Constant_Reference_Type :=
433 (Element => Node.Element.all'Access,
435 (Controlled with Container'Unrestricted_Access))
441 end Constant_Reference;
447 function Contains (Container : Map; Key : Key_Type) return Boolean is
449 return Find (Container, Key) /= No_Element;
456 function Copy (Source : Map) return Map is
458 return Target : Map do
459 Target.Assign (Source);
467 function Copy_Node (Source : Node_Access) return Node_Access is
468 K : Key_Access := new Key_Type'(Source.Key.all);
472 E := new Element_Type'(Source.Element.all);
474 return new Node_Type'(Parent => null,
477 Color => Source.Color,
492 (Container : in out Map;
493 Position : in out Cursor)
496 if Position.Node = null then
497 raise Constraint_Error with
498 "Position cursor of Delete equals No_Element";
501 if Position.Node.Key = null
502 or else Position.Node.Element = null
504 raise Program_Error with "Position cursor of Delete is bad";
507 if Position.Container /= Container'Unrestricted_Access then
508 raise Program_Error with
509 "Position cursor of Delete designates wrong map";
512 pragma Assert (Vet (Container.Tree, Position.Node),
513 "Position cursor of Delete is bad");
515 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
516 Free (Position.Node);
518 Position.Container := null;
521 procedure Delete (Container : in out Map; Key : Key_Type) is
522 X : Node_Access := Key_Ops.Find (Container.Tree, Key);
526 raise Constraint_Error with "key not in map";
529 Delete_Node_Sans_Free (Container.Tree, X);
537 procedure Delete_First (Container : in out Map) is
538 X : Node_Access := Container.Tree.First;
541 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
550 procedure Delete_Last (Container : in out Map) is
551 X : Node_Access := Container.Tree.Last;
554 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
563 function Element (Position : Cursor) return Element_Type is
565 if Position.Node = null then
566 raise Constraint_Error with
567 "Position cursor of function Element equals No_Element";
570 if Position.Node.Element = null then
571 raise Program_Error with
572 "Position cursor of function Element is bad";
575 pragma Assert (Vet (Position.Container.Tree, Position.Node),
576 "Position cursor of function Element is bad");
578 return Position.Node.Element.all;
581 function Element (Container : Map; Key : Key_Type) return Element_Type is
582 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
586 raise Constraint_Error with "key not in map";
589 return Node.Element.all;
592 ---------------------
593 -- Equivalent_Keys --
594 ---------------------
596 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
598 return (if Left < Right or else Right < Left then False else True);
605 procedure Exclude (Container : in out Map; Key : Key_Type) is
606 X : Node_Access := Key_Ops.Find (Container.Tree, Key);
609 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
618 procedure Finalize (Object : in out Iterator) is
620 if Object.Container /= null then
622 B : Natural renames Object.Container.all.Tree.Busy;
629 procedure Finalize (Control : in out Reference_Control_Type) is
631 if Control.Container /= null then
633 T : Tree_Type renames Control.Container.all.Tree;
634 B : Natural renames T.Busy;
635 L : Natural renames T.Lock;
641 Control.Container := null;
649 function Find (Container : Map; Key : Key_Type) return Cursor is
650 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
652 return (if Node = null then No_Element
653 else Cursor'(Container'Unrestricted_Access, Node));
660 function First (Container : Map) return Cursor is
661 T : Tree_Type renames Container.Tree;
663 return (if T.First = null then No_Element
664 else Cursor'(Container'Unrestricted_Access, T.First));
667 function First (Object : Iterator) return Cursor is
669 -- The value of the iterator object's Node component influences the
670 -- behavior of the First (and Last) selector function.
672 -- When the Node component is null, this means the iterator object was
673 -- constructed without a start expression, in which case the (forward)
674 -- iteration starts from the (logical) beginning of the entire sequence
675 -- of items (corresponding to Container.First for a forward iterator).
677 -- Otherwise, this is iteration over a partial sequence of items. When
678 -- the Node component is non-null, the iterator object was constructed
679 -- with a start expression, that specifies the position from which the
680 -- (forward) partial iteration begins.
682 if Object.Node = null then
683 return Object.Container.First;
685 return Cursor'(Object.Container, Object.Node);
693 function First_Element (Container : Map) return Element_Type is
694 T : Tree_Type renames Container.Tree;
696 if T.First = null then
697 raise Constraint_Error with "map is empty";
699 return T.First.Element.all;
707 function First_Key (Container : Map) return Key_Type is
708 T : Tree_Type renames Container.Tree;
710 if T.First = null then
711 raise Constraint_Error with "map is empty";
713 return T.First.Key.all;
721 function Floor (Container : Map; Key : Key_Type) return Cursor is
722 Node : constant Node_Access := Key_Ops.Floor (Container.Tree, Key);
724 return (if Node = null then No_Element
725 else Cursor'(Container'Unrestricted_Access, Node));
732 procedure Free (X : in out Node_Access) is
733 procedure Deallocate is
734 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
753 Free_Element (X.Element);
764 Free_Element (X.Element);
781 function Has_Element (Position : Cursor) return Boolean is
783 return Position /= No_Element;
791 (Container : in out Map;
793 New_Item : Element_Type)
802 Insert (Container, Key, New_Item, Position, Inserted);
805 if Container.Tree.Lock > 0 then
806 raise Program_Error with
807 "attempt to tamper with elements (map is locked)";
810 K := Position.Node.Key;
811 E := Position.Node.Element;
813 Position.Node.Key := new Key_Type'(Key);
816 Position.Node.Element := new Element_Type'(New_Item);
833 (Container : in out Map;
835 New_Item : Element_Type;
836 Position : out Cursor;
837 Inserted : out Boolean)
839 function New_Node return Node_Access;
840 pragma Inline (New_Node);
842 procedure Insert_Post is
843 new Key_Ops.Generic_Insert_Post (New_Node);
845 procedure Insert_Sans_Hint is
846 new Key_Ops.Generic_Conditional_Insert (Insert_Post);
852 function New_Node return Node_Access is
853 Node : Node_Access := new Node_Type;
856 Node.Key := new Key_Type'(Key);
857 Node.Element := new Element_Type'(New_Item);
863 -- On exception, deallocate key and elem
865 Free (Node); -- Note that Free deallocates key and elem too
869 -- Start of processing for Insert
878 Position.Container := Container'Unrestricted_Access;
882 (Container : in out Map;
884 New_Item : Element_Type)
887 pragma Unreferenced (Position);
892 Insert (Container, Key, New_Item, Position, Inserted);
895 raise Constraint_Error with "key already in map";
903 function Is_Empty (Container : Map) return Boolean is
905 return Container.Tree.Length = 0;
908 ------------------------
909 -- Is_Equal_Node_Node --
910 ------------------------
912 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
914 return (if L.Key.all < R.Key.all then False
915 elsif R.Key.all < L.Key.all then False
916 else L.Element.all = R.Element.all);
917 end Is_Equal_Node_Node;
919 -------------------------
920 -- Is_Greater_Key_Node --
921 -------------------------
923 function Is_Greater_Key_Node
925 Right : Node_Access) return Boolean
928 -- k > node same as node < k
930 return Right.Key.all < Left;
931 end Is_Greater_Key_Node;
933 ----------------------
934 -- Is_Less_Key_Node --
935 ----------------------
937 function Is_Less_Key_Node
939 Right : Node_Access) return Boolean is
941 return Left < Right.Key.all;
942 end Is_Less_Key_Node;
950 Process : not null access procedure (Position : Cursor))
952 procedure Process_Node (Node : Node_Access);
953 pragma Inline (Process_Node);
955 procedure Local_Iterate is
956 new Tree_Operations.Generic_Iteration (Process_Node);
962 procedure Process_Node (Node : Node_Access) is
964 Process (Cursor'(Container'Unrestricted_Access, Node));
967 B : Natural renames Container'Unrestricted_Access.all.Tree.Busy;
969 -- Start of processing for Iterate
975 Local_Iterate (Container.Tree);
986 (Container : Map) return Map_Iterator_Interfaces.Reversible_Iterator'Class
988 B : Natural renames Container'Unrestricted_Access.all.Tree.Busy;
991 -- The value of the Node component influences the behavior of the First
992 -- and Last selector functions of the iterator object. When the Node
993 -- component is null (as is the case here), this means the iterator
994 -- object was constructed without a start expression. This is a complete
995 -- iterator, meaning that the iteration starts from the (logical)
996 -- beginning of the sequence of items.
998 -- Note: For a forward iterator, Container.First is the beginning, and
999 -- for a reverse iterator, Container.Last is the beginning.
1001 return It : constant Iterator :=
1002 (Limited_Controlled with
1003 Container => Container'Unrestricted_Access,
1013 return Map_Iterator_Interfaces.Reversible_Iterator'Class
1015 B : Natural renames Container'Unrestricted_Access.all.Tree.Busy;
1018 -- It was formerly the case that when Start = No_Element, the partial
1019 -- iterator was defined to behave the same as for a complete iterator,
1020 -- and iterate over the entire sequence of items. However, those
1021 -- semantics were unintuitive and arguably error-prone (it is too easy
1022 -- to accidentally create an endless loop), and so they were changed,
1023 -- per the ARG meeting in Denver on 2011/11. However, there was no
1024 -- consensus about what positive meaning this corner case should have,
1025 -- and so it was decided to simply raise an exception. This does imply,
1026 -- however, that it is not possible to use a partial iterator to specify
1027 -- an empty sequence of items.
1029 if Start = No_Element then
1030 raise Constraint_Error with
1031 "Start position for iterator equals No_Element";
1034 if Start.Container /= Container'Unrestricted_Access then
1035 raise Program_Error with
1036 "Start cursor of Iterate designates wrong map";
1039 pragma Assert (Vet (Container.Tree, Start.Node),
1040 "Start cursor of Iterate is bad");
1042 -- The value of the Node component influences the behavior of the First
1043 -- and Last selector functions of the iterator object. When the Node
1044 -- component is non-null (as is the case here), it means that this
1045 -- is a partial iteration, over a subset of the complete sequence of
1046 -- items. The iterator object was constructed with a start expression,
1047 -- indicating the position from which the iteration begins. Note that
1048 -- the start position has the same value irrespective of whether this
1049 -- is a forward or reverse iteration.
1051 return It : constant Iterator :=
1052 (Limited_Controlled with
1053 Container => Container'Unrestricted_Access,
1064 function Key (Position : Cursor) return Key_Type is
1066 if Position.Node = null then
1067 raise Constraint_Error with
1068 "Position cursor of function Key equals No_Element";
1071 if Position.Node.Key = null then
1072 raise Program_Error with
1073 "Position cursor of function Key is bad";
1076 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1077 "Position cursor of function Key is bad");
1079 return Position.Node.Key.all;
1086 function Last (Container : Map) return Cursor is
1087 T : Tree_Type renames Container.Tree;
1089 return (if T.Last = null then No_Element
1090 else Cursor'(Container'Unrestricted_Access, T.Last));
1093 function Last (Object : Iterator) return Cursor is
1095 -- The value of the iterator object's Node component influences the
1096 -- behavior of the Last (and First) selector function.
1098 -- When the Node component is null, this means the iterator object was
1099 -- constructed without a start expression, in which case the (reverse)
1100 -- iteration starts from the (logical) beginning of the entire sequence
1101 -- (corresponding to Container.Last, for a reverse iterator).
1103 -- Otherwise, this is iteration over a partial sequence of items. When
1104 -- the Node component is non-null, the iterator object was constructed
1105 -- with a start expression, that specifies the position from which the
1106 -- (reverse) partial iteration begins.
1108 if Object.Node = null then
1109 return Object.Container.Last;
1111 return Cursor'(Object.Container, Object.Node);
1119 function Last_Element (Container : Map) return Element_Type is
1120 T : Tree_Type renames Container.Tree;
1123 if T.Last = null then
1124 raise Constraint_Error with "map is empty";
1127 return T.Last.Element.all;
1134 function Last_Key (Container : Map) return Key_Type is
1135 T : Tree_Type renames Container.Tree;
1138 if T.Last = null then
1139 raise Constraint_Error with "map is empty";
1142 return T.Last.Key.all;
1149 function Left (Node : Node_Access) return Node_Access is
1158 function Length (Container : Map) return Count_Type is
1160 return Container.Tree.Length;
1167 procedure Move is new Tree_Operations.Generic_Move (Clear);
1169 procedure Move (Target : in out Map; Source : in out Map) is
1171 Move (Target => Target.Tree, Source => Source.Tree);
1178 function Next (Position : Cursor) return Cursor is
1180 if Position = No_Element then
1184 pragma Assert (Position.Node /= null);
1185 pragma Assert (Position.Node.Key /= null);
1186 pragma Assert (Position.Node.Element /= null);
1187 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1188 "Position cursor of Next is bad");
1191 Node : constant Node_Access :=
1192 Tree_Operations.Next (Position.Node);
1194 return (if Node = null then No_Element
1195 else Cursor'(Position.Container, Node));
1199 procedure Next (Position : in out Cursor) is
1201 Position := Next (Position);
1206 Position : Cursor) return Cursor
1209 if Position.Container = null then
1213 if Position.Container /= Object.Container then
1214 raise Program_Error with
1215 "Position cursor of Next designates wrong map";
1218 return Next (Position);
1225 function Parent (Node : Node_Access) return Node_Access is
1234 function Previous (Position : Cursor) return Cursor is
1236 if Position = No_Element then
1240 pragma Assert (Position.Node /= null);
1241 pragma Assert (Position.Node.Key /= null);
1242 pragma Assert (Position.Node.Element /= null);
1243 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1244 "Position cursor of Previous is bad");
1247 Node : constant Node_Access :=
1248 Tree_Operations.Previous (Position.Node);
1250 return (if Node = null then No_Element
1251 else Cursor'(Position.Container, Node));
1255 procedure Previous (Position : in out Cursor) is
1257 Position := Previous (Position);
1262 Position : Cursor) return Cursor
1265 if Position.Container = null then
1269 if Position.Container /= Object.Container then
1270 raise Program_Error with
1271 "Position cursor of Previous designates wrong map";
1274 return Previous (Position);
1281 procedure Query_Element
1283 Process : not null access procedure (Key : Key_Type;
1284 Element : Element_Type))
1287 if Position.Node = null then
1288 raise Constraint_Error with
1289 "Position cursor of Query_Element equals No_Element";
1292 if Position.Node.Key = null
1293 or else Position.Node.Element = null
1295 raise Program_Error with
1296 "Position cursor of Query_Element is bad";
1299 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1300 "Position cursor of Query_Element is bad");
1303 T : Tree_Type renames Position.Container.Tree;
1305 B : Natural renames T.Busy;
1306 L : Natural renames T.Lock;
1313 K : Key_Type renames Position.Node.Key.all;
1314 E : Element_Type renames Position.Node.Element.all;
1335 (Stream : not null access Root_Stream_Type'Class;
1336 Container : out Map)
1339 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1340 pragma Inline (Read_Node);
1343 new Tree_Operations.Generic_Read (Clear, Read_Node);
1350 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1352 Node : Node_Access := new Node_Type;
1354 Node.Key := new Key_Type'(Key_Type'Input (Stream));
1355 Node.Element := new Element_Type'(Element_Type'Input (Stream));
1359 Free (Node); -- Note that Free deallocates key and elem too
1363 -- Start of processing for Read
1366 Read (Stream, Container.Tree);
1370 (Stream : not null access Root_Stream_Type'Class;
1374 raise Program_Error with "attempt to stream map cursor";
1378 (Stream : not null access Root_Stream_Type'Class;
1379 Item : out Reference_Type)
1382 raise Program_Error with "attempt to stream reference";
1386 (Stream : not null access Root_Stream_Type'Class;
1387 Item : out Constant_Reference_Type)
1390 raise Program_Error with "attempt to stream reference";
1398 (Container : aliased in out Map;
1399 Position : Cursor) return Reference_Type
1402 if Position.Container = null then
1403 raise Constraint_Error with
1404 "Position cursor has no element";
1407 if Position.Container /= Container'Unrestricted_Access then
1408 raise Program_Error with
1409 "Position cursor designates wrong map";
1412 if Position.Node.Element = null then
1413 raise Program_Error with "Node has no element";
1416 pragma Assert (Vet (Container.Tree, Position.Node),
1417 "Position cursor in function Reference is bad");
1420 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
1421 B : Natural renames T.Busy;
1422 L : Natural renames T.Lock;
1424 return R : constant Reference_Type :=
1425 (Element => Position.Node.Element.all'Access,
1426 Control => (Controlled with Position.Container))
1435 (Container : aliased in out Map;
1436 Key : Key_Type) return Reference_Type
1438 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
1442 raise Constraint_Error with "key not in map";
1445 if Node.Element = null then
1446 raise Program_Error with "Node has no element";
1450 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
1451 B : Natural renames T.Busy;
1452 L : Natural renames T.Lock;
1454 return R : constant Reference_Type :=
1455 (Element => Node.Element.all'Access,
1457 (Controlled with Container'Unrestricted_Access))
1470 (Container : in out Map;
1472 New_Item : Element_Type)
1474 Node : constant Node_Access :=
1475 Key_Ops.Find (Container.Tree, Key);
1482 raise Constraint_Error with "key not in map";
1485 if Container.Tree.Lock > 0 then
1486 raise Program_Error with
1487 "attempt to tamper with elements (map is locked)";
1493 Node.Key := new Key_Type'(Key);
1496 Node.Element := new Element_Type'(New_Item);
1507 ---------------------
1508 -- Replace_Element --
1509 ---------------------
1511 procedure Replace_Element
1512 (Container : in out Map;
1514 New_Item : Element_Type)
1517 if Position.Node = null then
1518 raise Constraint_Error with
1519 "Position cursor of Replace_Element equals No_Element";
1522 if Position.Node.Key = null
1523 or else Position.Node.Element = null
1525 raise Program_Error with
1526 "Position cursor of Replace_Element is bad";
1529 if Position.Container /= Container'Unrestricted_Access then
1530 raise Program_Error with
1531 "Position cursor of Replace_Element designates wrong map";
1534 if Container.Tree.Lock > 0 then
1535 raise Program_Error with
1536 "attempt to tamper with elements (map is locked)";
1539 pragma Assert (Vet (Container.Tree, Position.Node),
1540 "Position cursor of Replace_Element is bad");
1543 X : Element_Access := Position.Node.Element;
1546 Position.Node.Element := new Element_Type'(New_Item);
1549 end Replace_Element;
1551 ---------------------
1552 -- Reverse_Iterate --
1553 ---------------------
1555 procedure Reverse_Iterate
1557 Process : not null access procedure (Position : Cursor))
1559 procedure Process_Node (Node : Node_Access);
1560 pragma Inline (Process_Node);
1562 procedure Local_Reverse_Iterate is
1563 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1569 procedure Process_Node (Node : Node_Access) is
1571 Process (Cursor'(Container'Unrestricted_Access, Node));
1574 B : Natural renames Container.Tree'Unrestricted_Access.all.Busy;
1576 -- Start of processing for Reverse_Iterate
1582 Local_Reverse_Iterate (Container.Tree);
1590 end Reverse_Iterate;
1596 function Right (Node : Node_Access) return Node_Access is
1605 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1607 Node.Color := Color;
1614 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1623 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1625 Node.Parent := Parent;
1632 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1634 Node.Right := Right;
1637 --------------------
1638 -- Update_Element --
1639 --------------------
1641 procedure Update_Element
1642 (Container : in out Map;
1644 Process : not null access procedure (Key : Key_Type;
1645 Element : in out Element_Type))
1648 if Position.Node = null then
1649 raise Constraint_Error with
1650 "Position cursor of Update_Element equals No_Element";
1653 if Position.Node.Key = null
1654 or else Position.Node.Element = null
1656 raise Program_Error with
1657 "Position cursor of Update_Element is bad";
1660 if Position.Container /= Container'Unrestricted_Access then
1661 raise Program_Error with
1662 "Position cursor of Update_Element designates wrong map";
1665 pragma Assert (Vet (Container.Tree, Position.Node),
1666 "Position cursor of Update_Element is bad");
1669 T : Tree_Type renames Position.Container.Tree;
1671 B : Natural renames T.Busy;
1672 L : Natural renames T.Lock;
1679 K : Key_Type renames Position.Node.Key.all;
1680 E : Element_Type renames Position.Node.Element.all;
1702 (Stream : not null access Root_Stream_Type'Class;
1705 procedure Write_Node
1706 (Stream : not null access Root_Stream_Type'Class;
1707 Node : Node_Access);
1708 pragma Inline (Write_Node);
1711 new Tree_Operations.Generic_Write (Write_Node);
1717 procedure Write_Node
1718 (Stream : not null access Root_Stream_Type'Class;
1722 Key_Type'Output (Stream, Node.Key.all);
1723 Element_Type'Output (Stream, Node.Element.all);
1726 -- Start of processing for Write
1729 Write (Stream, Container.Tree);
1733 (Stream : not null access Root_Stream_Type'Class;
1737 raise Program_Error with "attempt to stream map cursor";
1741 (Stream : not null access Root_Stream_Type'Class;
1742 Item : Reference_Type)
1745 raise Program_Error with "attempt to stream reference";
1749 (Stream : not null access Root_Stream_Type'Class;
1750 Item : Constant_Reference_Type)
1753 raise Program_Error with "attempt to stream reference";
1756 end Ada.Containers.Indefinite_Ordered_Maps;