1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.INDEFINITE_ORDERED_MAPS --
9 -- Copyright (C) 2004-2011, 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);
298 procedure Assign (Target : in out Map; Source : Map) is
299 procedure Insert_Item (Node : Node_Access);
300 pragma Inline (Insert_Item);
302 procedure Insert_Items is
303 new Tree_Operations.Generic_Iteration (Insert_Item);
309 procedure Insert_Item (Node : Node_Access) is
311 Target.Insert (Key => Node.Key.all, New_Item => Node.Element.all);
314 -- Start of processing for Assign
317 if Target'Address = Source'Address then
322 Insert_Items (Target.Tree);
329 function Ceiling (Container : Map; Key : Key_Type) return Cursor is
330 Node : constant Node_Access := Key_Ops.Ceiling (Container.Tree, Key);
332 return (if Node = null then No_Element
333 else Cursor'(Container'Unrestricted_Access, Node));
340 procedure Clear is new Tree_Operations.Generic_Clear (Delete_Tree);
342 procedure Clear (Container : in out Map) is
344 Clear (Container.Tree);
351 function Color (Node : Node_Access) return Color_Type is
356 ------------------------
357 -- Constant_Reference --
358 ------------------------
360 function Constant_Reference
361 (Container : aliased Map;
362 Position : Cursor) return Constant_Reference_Type
365 if Position.Container = null then
366 raise Constraint_Error with
367 "Position cursor has no element";
370 if Position.Container /= Container'Unrestricted_Access then
371 raise Program_Error with
372 "Position cursor designates wrong map";
375 if Position.Node.Element = null then
376 raise Program_Error with "Node has no element";
379 pragma Assert (Vet (Container.Tree, Position.Node),
380 "Position cursor in Constant_Reference is bad");
382 return (Element => Position.Node.Element.all'Access);
383 end Constant_Reference;
385 function Constant_Reference
387 Key : Key_Type) return Constant_Reference_Type
389 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
393 raise Constraint_Error with "key not in map";
396 if Node.Element = null then
397 raise Program_Error with "Node has no element";
400 return (Element => Node.Element.all'Access);
401 end Constant_Reference;
407 function Contains (Container : Map; Key : Key_Type) return Boolean is
409 return Find (Container, Key) /= No_Element;
416 function Copy (Source : Map) return Map is
418 return Target : Map do
419 Target.Assign (Source);
427 function Copy_Node (Source : Node_Access) return Node_Access is
428 K : Key_Access := new Key_Type'(Source.Key.all);
432 E := new Element_Type'(Source.Element.all);
434 return new Node_Type'(Parent => null,
437 Color => Source.Color,
452 (Container : in out Map;
453 Position : in out Cursor)
456 if Position.Node = null then
457 raise Constraint_Error with
458 "Position cursor of Delete equals No_Element";
461 if Position.Node.Key = null
462 or else Position.Node.Element = null
464 raise Program_Error with "Position cursor of Delete is bad";
467 if Position.Container /= Container'Unrestricted_Access then
468 raise Program_Error with
469 "Position cursor of Delete designates wrong map";
472 pragma Assert (Vet (Container.Tree, Position.Node),
473 "Position cursor of Delete is bad");
475 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
476 Free (Position.Node);
478 Position.Container := null;
481 procedure Delete (Container : in out Map; Key : Key_Type) is
482 X : Node_Access := Key_Ops.Find (Container.Tree, Key);
486 raise Constraint_Error with "key not in map";
489 Delete_Node_Sans_Free (Container.Tree, X);
497 procedure Delete_First (Container : in out Map) is
498 X : Node_Access := Container.Tree.First;
501 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
510 procedure Delete_Last (Container : in out Map) is
511 X : Node_Access := Container.Tree.Last;
514 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
523 function Element (Position : Cursor) return Element_Type is
525 if Position.Node = null then
526 raise Constraint_Error with
527 "Position cursor of function Element equals No_Element";
530 if Position.Node.Element = null then
531 raise Program_Error with
532 "Position cursor of function Element is bad";
535 pragma Assert (Vet (Position.Container.Tree, Position.Node),
536 "Position cursor of function Element is bad");
538 return Position.Node.Element.all;
541 function Element (Container : Map; Key : Key_Type) return Element_Type is
542 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
546 raise Constraint_Error with "key not in map";
549 return Node.Element.all;
552 ---------------------
553 -- Equivalent_Keys --
554 ---------------------
556 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
558 return (if Left < Right or else Right < Left then False else True);
565 procedure Exclude (Container : in out Map; Key : Key_Type) is
566 X : Node_Access := Key_Ops.Find (Container.Tree, Key);
569 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
578 procedure Finalize (Object : in out Iterator) is
580 if Object.Container /= null then
582 B : Natural renames Object.Container.all.Tree.Busy;
593 function Find (Container : Map; Key : Key_Type) return Cursor is
594 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
596 return (if Node = null then No_Element
597 else Cursor'(Container'Unrestricted_Access, Node));
604 function First (Container : Map) return Cursor is
605 T : Tree_Type renames Container.Tree;
607 return (if T.First = null then No_Element
608 else Cursor'(Container'Unrestricted_Access, T.First));
611 function First (Object : Iterator) return Cursor is
613 -- The value of the iterator object's Node component influences the
614 -- behavior of the First (and Last) selector function.
616 -- When the Node component is null, this means the iterator object was
617 -- constructed without a start expression, in which case the (forward)
618 -- iteration starts from the (logical) beginning of the entire sequence
619 -- of items (corresponding to Container.First for a forward iterator).
621 -- Otherwise, this is iteration over a partial sequence of items. When
622 -- the Node component is non-null, the iterator object was constructed
623 -- with a start expression, that specifies the position from which the
624 -- (forward) partial iteration begins.
626 if Object.Node = null then
627 return Object.Container.First;
629 return Cursor'(Object.Container, Object.Node);
637 function First_Element (Container : Map) return Element_Type is
638 T : Tree_Type renames Container.Tree;
640 if T.First = null then
641 raise Constraint_Error with "map is empty";
643 return T.First.Element.all;
651 function First_Key (Container : Map) return Key_Type is
652 T : Tree_Type renames Container.Tree;
654 if T.First = null then
655 raise Constraint_Error with "map is empty";
657 return T.First.Key.all;
665 function Floor (Container : Map; Key : Key_Type) return Cursor is
666 Node : constant Node_Access := Key_Ops.Floor (Container.Tree, Key);
668 return (if Node = null then No_Element
669 else Cursor'(Container'Unrestricted_Access, Node));
676 procedure Free (X : in out Node_Access) is
677 procedure Deallocate is
678 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
697 Free_Element (X.Element);
708 Free_Element (X.Element);
725 function Has_Element (Position : Cursor) return Boolean is
727 return Position /= No_Element;
735 (Container : in out Map;
737 New_Item : Element_Type)
746 Insert (Container, Key, New_Item, Position, Inserted);
749 if Container.Tree.Lock > 0 then
750 raise Program_Error with
751 "attempt to tamper with elements (map is locked)";
754 K := Position.Node.Key;
755 E := Position.Node.Element;
757 Position.Node.Key := new Key_Type'(Key);
760 Position.Node.Element := new Element_Type'(New_Item);
777 (Container : in out Map;
779 New_Item : Element_Type;
780 Position : out Cursor;
781 Inserted : out Boolean)
783 function New_Node return Node_Access;
784 pragma Inline (New_Node);
786 procedure Insert_Post is
787 new Key_Ops.Generic_Insert_Post (New_Node);
789 procedure Insert_Sans_Hint is
790 new Key_Ops.Generic_Conditional_Insert (Insert_Post);
796 function New_Node return Node_Access is
797 Node : Node_Access := new Node_Type;
800 Node.Key := new Key_Type'(Key);
801 Node.Element := new Element_Type'(New_Item);
807 -- On exception, deallocate key and elem
809 Free (Node); -- Note that Free deallocates key and elem too
813 -- Start of processing for Insert
822 Position.Container := Container'Unrestricted_Access;
826 (Container : in out Map;
828 New_Item : Element_Type)
831 pragma Unreferenced (Position);
836 Insert (Container, Key, New_Item, Position, Inserted);
839 raise Constraint_Error with "key already in map";
847 function Is_Empty (Container : Map) return Boolean is
849 return Container.Tree.Length = 0;
852 ------------------------
853 -- Is_Equal_Node_Node --
854 ------------------------
856 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
858 return (if L.Key.all < R.Key.all then False
859 elsif R.Key.all < L.Key.all then False
860 else L.Element.all = R.Element.all);
861 end Is_Equal_Node_Node;
863 -------------------------
864 -- Is_Greater_Key_Node --
865 -------------------------
867 function Is_Greater_Key_Node
869 Right : Node_Access) return Boolean
872 -- k > node same as node < k
874 return Right.Key.all < Left;
875 end Is_Greater_Key_Node;
877 ----------------------
878 -- Is_Less_Key_Node --
879 ----------------------
881 function Is_Less_Key_Node
883 Right : Node_Access) return Boolean is
885 return Left < Right.Key.all;
886 end Is_Less_Key_Node;
894 Process : not null access procedure (Position : Cursor))
896 procedure Process_Node (Node : Node_Access);
897 pragma Inline (Process_Node);
899 procedure Local_Iterate is
900 new Tree_Operations.Generic_Iteration (Process_Node);
906 procedure Process_Node (Node : Node_Access) is
908 Process (Cursor'(Container'Unrestricted_Access, Node));
911 B : Natural renames Container'Unrestricted_Access.all.Tree.Busy;
913 -- Start of processing for Iterate
919 Local_Iterate (Container.Tree);
930 (Container : Map) return Map_Iterator_Interfaces.Reversible_Iterator'Class
932 B : Natural renames Container'Unrestricted_Access.all.Tree.Busy;
935 -- The value of the Node component influences the behavior of the First
936 -- and Last selector functions of the iterator object. When the Node
937 -- component is null (as is the case here), this means the iterator
938 -- object was constructed without a start expression. This is a complete
939 -- iterator, meaning that the iteration starts from the (logical)
940 -- beginning of the sequence of items.
942 -- Note: For a forward iterator, Container.First is the beginning, and
943 -- for a reverse iterator, Container.Last is the beginning.
945 return It : constant Iterator :=
946 (Limited_Controlled with
947 Container => Container'Unrestricted_Access,
957 return Map_Iterator_Interfaces.Reversible_Iterator'Class
959 B : Natural renames Container'Unrestricted_Access.all.Tree.Busy;
962 -- It was formerly the case that when Start = No_Element, the partial
963 -- iterator was defined to behave the same as for a complete iterator,
964 -- and iterate over the entire sequence of items. However, those
965 -- semantics were unintuitive and arguably error-prone (it is too easy
966 -- to accidentally create an endless loop), and so they were changed,
967 -- per the ARG meeting in Denver on 2011/11. However, there was no
968 -- consensus about what positive meaning this corner case should have,
969 -- and so it was decided to simply raise an exception. This does imply,
970 -- however, that it is not possible to use a partial iterator to specify
971 -- an empty sequence of items.
973 if Start = No_Element then
974 raise Constraint_Error with
975 "Start position for iterator equals No_Element";
978 if Start.Container /= Container'Unrestricted_Access then
979 raise Program_Error with
980 "Start cursor of Iterate designates wrong map";
983 pragma Assert (Vet (Container.Tree, Start.Node),
984 "Start cursor of Iterate is bad");
986 -- The value of the Node component influences the behavior of the First
987 -- and Last selector functions of the iterator object. When the Node
988 -- component is non-null (as is the case here), it means that this
989 -- is a partial iteration, over a subset of the complete sequence of
990 -- items. The iterator object was constructed with a start expression,
991 -- indicating the position from which the iteration begins. Note that
992 -- the start position has the same value irrespective of whether this
993 -- is a forward or reverse iteration.
995 return It : constant Iterator :=
996 (Limited_Controlled with
997 Container => Container'Unrestricted_Access,
1008 function Key (Position : Cursor) return Key_Type is
1010 if Position.Node = null then
1011 raise Constraint_Error with
1012 "Position cursor of function Key equals No_Element";
1015 if Position.Node.Key = null then
1016 raise Program_Error with
1017 "Position cursor of function Key is bad";
1020 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1021 "Position cursor of function Key is bad");
1023 return Position.Node.Key.all;
1030 function Last (Container : Map) return Cursor is
1031 T : Tree_Type renames Container.Tree;
1033 return (if T.Last = null then No_Element
1034 else Cursor'(Container'Unrestricted_Access, T.Last));
1037 function Last (Object : Iterator) return Cursor is
1039 -- The value of the iterator object's Node component influences the
1040 -- behavior of the Last (and First) selector function.
1042 -- When the Node component is null, this means the iterator object was
1043 -- constructed without a start expression, in which case the (reverse)
1044 -- iteration starts from the (logical) beginning of the entire sequence
1045 -- (corresponding to Container.Last, for a reverse iterator).
1047 -- Otherwise, this is iteration over a partial sequence of items. When
1048 -- the Node component is non-null, the iterator object was constructed
1049 -- with a start expression, that specifies the position from which the
1050 -- (reverse) partial iteration begins.
1052 if Object.Node = null then
1053 return Object.Container.Last;
1055 return Cursor'(Object.Container, Object.Node);
1063 function Last_Element (Container : Map) return Element_Type is
1064 T : Tree_Type renames Container.Tree;
1067 if T.Last = null then
1068 raise Constraint_Error with "map is empty";
1071 return T.Last.Element.all;
1078 function Last_Key (Container : Map) return Key_Type is
1079 T : Tree_Type renames Container.Tree;
1082 if T.Last = null then
1083 raise Constraint_Error with "map is empty";
1086 return T.Last.Key.all;
1093 function Left (Node : Node_Access) return Node_Access is
1102 function Length (Container : Map) return Count_Type is
1104 return Container.Tree.Length;
1111 procedure Move is new Tree_Operations.Generic_Move (Clear);
1113 procedure Move (Target : in out Map; Source : in out Map) is
1115 Move (Target => Target.Tree, Source => Source.Tree);
1122 function Next (Position : Cursor) return Cursor is
1124 if Position = No_Element then
1128 pragma Assert (Position.Node /= null);
1129 pragma Assert (Position.Node.Key /= null);
1130 pragma Assert (Position.Node.Element /= null);
1131 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1132 "Position cursor of Next is bad");
1135 Node : constant Node_Access :=
1136 Tree_Operations.Next (Position.Node);
1138 return (if Node = null then No_Element
1139 else Cursor'(Position.Container, Node));
1143 procedure Next (Position : in out Cursor) is
1145 Position := Next (Position);
1150 Position : Cursor) return Cursor
1153 if Position.Container = null then
1157 if Position.Container /= Object.Container then
1158 raise Program_Error with
1159 "Position cursor of Next designates wrong map";
1162 return Next (Position);
1169 function Parent (Node : Node_Access) return Node_Access is
1178 function Previous (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 Previous is bad");
1191 Node : constant Node_Access :=
1192 Tree_Operations.Previous (Position.Node);
1194 return (if Node = null then No_Element
1195 else Cursor'(Position.Container, Node));
1199 procedure Previous (Position : in out Cursor) is
1201 Position := Previous (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 Previous designates wrong map";
1218 return Previous (Position);
1225 procedure Query_Element
1227 Process : not null access procedure (Key : Key_Type;
1228 Element : Element_Type))
1231 if Position.Node = null then
1232 raise Constraint_Error with
1233 "Position cursor of Query_Element equals No_Element";
1236 if Position.Node.Key = null
1237 or else Position.Node.Element = null
1239 raise Program_Error with
1240 "Position cursor of Query_Element is bad";
1243 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1244 "Position cursor of Query_Element is bad");
1247 T : Tree_Type renames Position.Container.Tree;
1249 B : Natural renames T.Busy;
1250 L : Natural renames T.Lock;
1257 K : Key_Type renames Position.Node.Key.all;
1258 E : Element_Type renames Position.Node.Element.all;
1279 (Stream : not null access Root_Stream_Type'Class;
1280 Container : out Map)
1283 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1284 pragma Inline (Read_Node);
1287 new Tree_Operations.Generic_Read (Clear, Read_Node);
1294 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1296 Node : Node_Access := new Node_Type;
1298 Node.Key := new Key_Type'(Key_Type'Input (Stream));
1299 Node.Element := new Element_Type'(Element_Type'Input (Stream));
1303 Free (Node); -- Note that Free deallocates key and elem too
1307 -- Start of processing for Read
1310 Read (Stream, Container.Tree);
1314 (Stream : not null access Root_Stream_Type'Class;
1318 raise Program_Error with "attempt to stream map cursor";
1322 (Stream : not null access Root_Stream_Type'Class;
1323 Item : out Reference_Type)
1326 raise Program_Error with "attempt to stream reference";
1330 (Stream : not null access Root_Stream_Type'Class;
1331 Item : out Constant_Reference_Type)
1334 raise Program_Error with "attempt to stream reference";
1342 (Container : aliased in out Map;
1343 Position : Cursor) return Reference_Type
1346 if Position.Container = null then
1347 raise Constraint_Error with
1348 "Position cursor has no element";
1351 if Position.Container /= Container'Unrestricted_Access then
1352 raise Program_Error with
1353 "Position cursor designates wrong map";
1356 if Position.Node.Element = null then
1357 raise Program_Error with "Node has no element";
1360 pragma Assert (Vet (Container.Tree, Position.Node),
1361 "Position cursor in function Reference is bad");
1363 return (Element => Position.Node.Element.all'Access);
1367 (Container : aliased in out Map;
1368 Key : Key_Type) return Reference_Type
1370 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
1374 raise Constraint_Error with "key not in map";
1377 if Node.Element = null then
1378 raise Program_Error with "Node has no element";
1381 return (Element => Node.Element.all'Access);
1389 (Container : in out Map;
1391 New_Item : Element_Type)
1393 Node : constant Node_Access :=
1394 Key_Ops.Find (Container.Tree, Key);
1401 raise Constraint_Error with "key not in map";
1404 if Container.Tree.Lock > 0 then
1405 raise Program_Error with
1406 "attempt to tamper with elements (map is locked)";
1412 Node.Key := new Key_Type'(Key);
1415 Node.Element := new Element_Type'(New_Item);
1426 ---------------------
1427 -- Replace_Element --
1428 ---------------------
1430 procedure Replace_Element
1431 (Container : in out Map;
1433 New_Item : Element_Type)
1436 if Position.Node = null then
1437 raise Constraint_Error with
1438 "Position cursor of Replace_Element equals No_Element";
1441 if Position.Node.Key = null
1442 or else Position.Node.Element = null
1444 raise Program_Error with
1445 "Position cursor of Replace_Element is bad";
1448 if Position.Container /= Container'Unrestricted_Access then
1449 raise Program_Error with
1450 "Position cursor of Replace_Element designates wrong map";
1453 if Container.Tree.Lock > 0 then
1454 raise Program_Error with
1455 "attempt to tamper with elements (map is locked)";
1458 pragma Assert (Vet (Container.Tree, Position.Node),
1459 "Position cursor of Replace_Element is bad");
1462 X : Element_Access := Position.Node.Element;
1465 Position.Node.Element := new Element_Type'(New_Item);
1468 end Replace_Element;
1470 ---------------------
1471 -- Reverse_Iterate --
1472 ---------------------
1474 procedure Reverse_Iterate
1476 Process : not null access procedure (Position : Cursor))
1478 procedure Process_Node (Node : Node_Access);
1479 pragma Inline (Process_Node);
1481 procedure Local_Reverse_Iterate is
1482 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1488 procedure Process_Node (Node : Node_Access) is
1490 Process (Cursor'(Container'Unrestricted_Access, Node));
1493 B : Natural renames Container.Tree'Unrestricted_Access.all.Busy;
1495 -- Start of processing for Reverse_Iterate
1501 Local_Reverse_Iterate (Container.Tree);
1509 end Reverse_Iterate;
1515 function Right (Node : Node_Access) return Node_Access is
1524 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1526 Node.Color := Color;
1533 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1542 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1544 Node.Parent := Parent;
1551 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1553 Node.Right := Right;
1556 --------------------
1557 -- Update_Element --
1558 --------------------
1560 procedure Update_Element
1561 (Container : in out Map;
1563 Process : not null access procedure (Key : Key_Type;
1564 Element : in out Element_Type))
1567 if Position.Node = null then
1568 raise Constraint_Error with
1569 "Position cursor of Update_Element equals No_Element";
1572 if Position.Node.Key = null
1573 or else Position.Node.Element = null
1575 raise Program_Error with
1576 "Position cursor of Update_Element is bad";
1579 if Position.Container /= Container'Unrestricted_Access then
1580 raise Program_Error with
1581 "Position cursor of Update_Element designates wrong map";
1584 pragma Assert (Vet (Container.Tree, Position.Node),
1585 "Position cursor of Update_Element is bad");
1588 T : Tree_Type renames Position.Container.Tree;
1590 B : Natural renames T.Busy;
1591 L : Natural renames T.Lock;
1598 K : Key_Type renames Position.Node.Key.all;
1599 E : Element_Type renames Position.Node.Element.all;
1621 (Stream : not null access Root_Stream_Type'Class;
1624 procedure Write_Node
1625 (Stream : not null access Root_Stream_Type'Class;
1626 Node : Node_Access);
1627 pragma Inline (Write_Node);
1630 new Tree_Operations.Generic_Write (Write_Node);
1636 procedure Write_Node
1637 (Stream : not null access Root_Stream_Type'Class;
1641 Key_Type'Output (Stream, Node.Key.all);
1642 Element_Type'Output (Stream, Node.Element.all);
1645 -- Start of processing for Write
1648 Write (Stream, Container.Tree);
1652 (Stream : not null access Root_Stream_Type'Class;
1656 raise Program_Error with "attempt to stream map cursor";
1660 (Stream : not null access Root_Stream_Type'Class;
1661 Item : Reference_Type)
1664 raise Program_Error with "attempt to stream reference";
1668 (Stream : not null access Root_Stream_Type'Class;
1669 Item : Constant_Reference_Type)
1672 raise Program_Error with "attempt to stream reference";
1675 end Ada.Containers.Indefinite_Ordered_Maps;