1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . --
6 -- I N D E F I N I T E _ O R D E R E D _ M A P S --
10 -- Copyright (C) 2004-2005, Free Software Foundation, Inc. --
12 -- This specification is derived from the Ada Reference Manual for use with --
13 -- GNAT. The copyright notice above, and the license provisions that follow --
14 -- apply solely to the contents of the part following the private keyword. --
16 -- GNAT is free software; you can redistribute it and/or modify it under --
17 -- terms of the GNU General Public License as published by the Free Soft- --
18 -- ware Foundation; either version 2, or (at your option) any later ver- --
19 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
20 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
21 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
22 -- for more details. You should have received a copy of the GNU General --
23 -- Public License distributed with GNAT; see file COPYING. If not, write --
24 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
25 -- Boston, MA 02110-1301, USA. --
27 -- As a special exception, if other files instantiate generics from this --
28 -- unit, or you link this unit with other files to produce an executable, --
29 -- this unit does not by itself cause the resulting executable to be --
30 -- covered by the GNU General Public License. This exception does not --
31 -- however invalidate any other reasons why the executable file might be --
32 -- covered by the GNU Public License. --
34 -- This unit was originally developed by Matthew J Heaney. --
35 ------------------------------------------------------------------------------
37 with Ada.Unchecked_Deallocation;
39 with Ada.Containers.Red_Black_Trees.Generic_Operations;
40 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Operations);
42 with Ada.Containers.Red_Black_Trees.Generic_Keys;
43 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Keys);
45 package body Ada.Containers.Indefinite_Ordered_Maps is
47 -----------------------------
48 -- Node Access Subprograms --
49 -----------------------------
51 -- These subprograms provide a functional interface to access fields
52 -- of a node, and a procedural interface for modifying these values.
54 function Color (Node : Node_Access) return Color_Type;
55 pragma Inline (Color);
57 function Left (Node : Node_Access) return Node_Access;
60 function Parent (Node : Node_Access) return Node_Access;
61 pragma Inline (Parent);
63 function Right (Node : Node_Access) return Node_Access;
64 pragma Inline (Right);
66 procedure Set_Parent (Node : Node_Access; Parent : Node_Access);
67 pragma Inline (Set_Parent);
69 procedure Set_Left (Node : Node_Access; Left : Node_Access);
70 pragma Inline (Set_Left);
72 procedure Set_Right (Node : Node_Access; Right : Node_Access);
73 pragma Inline (Set_Right);
75 procedure Set_Color (Node : Node_Access; Color : Color_Type);
76 pragma Inline (Set_Color);
78 -----------------------
79 -- Local Subprograms --
80 -----------------------
82 function Copy_Node (Source : Node_Access) return Node_Access;
83 pragma Inline (Copy_Node);
85 procedure Free (X : in out Node_Access);
87 function Is_Equal_Node_Node
88 (L, R : Node_Access) return Boolean;
89 pragma Inline (Is_Equal_Node_Node);
91 function Is_Greater_Key_Node
93 Right : Node_Access) return Boolean;
94 pragma Inline (Is_Greater_Key_Node);
96 function Is_Less_Key_Node
98 Right : Node_Access) return Boolean;
99 pragma Inline (Is_Less_Key_Node);
101 --------------------------
102 -- Local Instantiations --
103 --------------------------
105 package Tree_Operations is
106 new Red_Black_Trees.Generic_Operations (Tree_Types);
108 procedure Delete_Tree is
109 new Tree_Operations.Generic_Delete_Tree (Free);
111 function Copy_Tree is
112 new Tree_Operations.Generic_Copy_Tree (Copy_Node, Delete_Tree);
117 new Red_Black_Trees.Generic_Keys
118 (Tree_Operations => Tree_Operations,
119 Key_Type => Key_Type,
120 Is_Less_Key_Node => Is_Less_Key_Node,
121 Is_Greater_Key_Node => Is_Greater_Key_Node);
123 procedure Free_Key is
124 new Ada.Unchecked_Deallocation (Key_Type, Key_Access);
126 procedure Free_Element is
127 new Ada.Unchecked_Deallocation (Element_Type, Element_Access);
130 new Tree_Operations.Generic_Equal (Is_Equal_Node_Node);
136 function "<" (Left, Right : Cursor) return Boolean is
138 if Left.Node = null then
139 raise Constraint_Error with "Left cursor of ""<"" equals No_Element";
142 if Right.Node = null then
143 raise Constraint_Error with "Right cursor of ""<"" equals No_Element";
146 if Left.Node.Key = null then
147 raise Program_Error with "Left cursor in ""<"" is bad";
150 if Right.Node.Key = null then
151 raise Program_Error with "Right cursor in ""<"" is bad";
154 pragma Assert (Vet (Left.Container.Tree, Left.Node),
155 "Left cursor in ""<"" is bad");
157 pragma Assert (Vet (Right.Container.Tree, Right.Node),
158 "Right cursor in ""<"" is bad");
160 return Left.Node.Key.all < Right.Node.Key.all;
163 function "<" (Left : Cursor; Right : Key_Type) return Boolean is
165 if Left.Node = null then
166 raise Constraint_Error with "Left cursor of ""<"" equals No_Element";
169 if Left.Node.Key = null then
170 raise Program_Error with "Left cursor in ""<"" is bad";
173 pragma Assert (Vet (Left.Container.Tree, Left.Node),
174 "Left cursor in ""<"" is bad");
176 return Left.Node.Key.all < Right;
179 function "<" (Left : Key_Type; Right : Cursor) return Boolean is
181 if Right.Node = null then
182 raise Constraint_Error with "Right cursor of ""<"" equals No_Element";
185 if Right.Node.Key = null then
186 raise Program_Error with "Right cursor in ""<"" is bad";
189 pragma Assert (Vet (Right.Container.Tree, Right.Node),
190 "Right cursor in ""<"" is bad");
192 return Left < Right.Node.Key.all;
199 function "=" (Left, Right : Map) return Boolean is
201 return Is_Equal (Left.Tree, Right.Tree);
208 function ">" (Left, Right : Cursor) return Boolean is
210 if Left.Node = null then
211 raise Constraint_Error with "Left cursor of "">"" equals No_Element";
214 if Right.Node = null then
215 raise Constraint_Error with "Right cursor of "">"" equals No_Element";
218 if Left.Node.Key = null then
219 raise Program_Error with "Left cursor in ""<"" is bad";
222 if Right.Node.Key = null then
223 raise Program_Error with "Right cursor in ""<"" is bad";
226 pragma Assert (Vet (Left.Container.Tree, Left.Node),
227 "Left cursor in "">"" is bad");
229 pragma Assert (Vet (Right.Container.Tree, Right.Node),
230 "Right cursor in "">"" is bad");
232 return Right.Node.Key.all < Left.Node.Key.all;
235 function ">" (Left : Cursor; Right : Key_Type) return Boolean is
237 if Left.Node = null then
238 raise Constraint_Error with "Left cursor of "">"" equals No_Element";
241 if Left.Node.Key = null then
242 raise Program_Error with "Left cursor in ""<"" is bad";
245 pragma Assert (Vet (Left.Container.Tree, Left.Node),
246 "Left cursor in "">"" is bad");
248 return Right < Left.Node.Key.all;
251 function ">" (Left : Key_Type; Right : Cursor) return Boolean is
253 if Right.Node = null then
254 raise Constraint_Error with "Right cursor of "">"" equals No_Element";
257 if Right.Node.Key = null then
258 raise Program_Error with "Right cursor in ""<"" is bad";
261 pragma Assert (Vet (Right.Container.Tree, Right.Node),
262 "Right cursor in "">"" is bad");
264 return Right.Node.Key.all < Left;
272 new Tree_Operations.Generic_Adjust (Copy_Tree);
274 procedure Adjust (Container : in out Map) is
276 Adjust (Container.Tree);
283 function Ceiling (Container : Map; Key : Key_Type) return Cursor is
284 Node : constant Node_Access := Key_Ops.Ceiling (Container.Tree, Key);
291 return Cursor'(Container'Unrestricted_Access, Node);
299 new Tree_Operations.Generic_Clear (Delete_Tree);
301 procedure Clear (Container : in out Map) is
303 Clear (Container.Tree);
310 function Color (Node : Node_Access) return Color_Type is
319 function Contains (Container : Map; Key : Key_Type) return Boolean is
321 return Find (Container, Key) /= No_Element;
328 function Copy_Node (Source : Node_Access) return Node_Access is
329 K : Key_Access := new Key_Type'(Source.Key.all);
332 E := new Element_Type'(Source.Element.all);
334 return new Node_Type'(Parent => null,
337 Color => Source.Color,
352 (Container : in out Map;
353 Position : in out Cursor)
356 if Position.Node = null then
357 raise Constraint_Error with
358 "Position cursor of Delete equals No_Element";
361 if Position.Node.Key = null
362 or else Position.Node.Element = null
364 raise Program_Error with "Position cursor of Delete is bad";
367 if Position.Container /= Container'Unrestricted_Access then
368 raise Program_Error with
369 "Position cursor of Delete designates wrong map";
372 pragma Assert (Vet (Container.Tree, Position.Node),
373 "Position cursor of Delete is bad");
375 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
376 Free (Position.Node);
378 Position.Container := null;
381 procedure Delete (Container : in out Map; Key : Key_Type) is
382 X : Node_Access := Key_Ops.Find (Container.Tree, Key);
386 raise Constraint_Error with "key not in map";
389 Delete_Node_Sans_Free (Container.Tree, X);
397 procedure Delete_First (Container : in out Map) is
398 X : Node_Access := Container.Tree.First;
402 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
411 procedure Delete_Last (Container : in out Map) is
412 X : Node_Access := Container.Tree.Last;
416 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
425 function Element (Position : Cursor) return Element_Type is
427 if Position.Node = null then
428 raise Constraint_Error with
429 "Position cursor of function Element equals No_Element";
432 if Position.Node.Element = null then
433 raise Program_Error with
434 "Position cursor of function Element is bad";
437 pragma Assert (Vet (Position.Container.Tree, Position.Node),
438 "Position cursor of function Element is bad");
440 return Position.Node.Element.all;
443 function Element (Container : Map; Key : Key_Type) return Element_Type is
444 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
448 raise Constraint_Error with "key not in map";
451 return Node.Element.all;
454 ---------------------
455 -- Equivalent_Keys --
456 ---------------------
458 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
473 procedure Exclude (Container : in out Map; Key : Key_Type) is
474 X : Node_Access := Key_Ops.Find (Container.Tree, Key);
478 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
487 function Find (Container : Map; Key : Key_Type) return Cursor is
488 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
495 return Cursor'(Container'Unrestricted_Access, Node);
502 function First (Container : Map) return Cursor is
503 T : Tree_Type renames Container.Tree;
506 if T.First = null then
510 return Cursor'(Container'Unrestricted_Access, T.First);
517 function First_Element (Container : Map) return Element_Type is
518 T : Tree_Type renames Container.Tree;
521 if T.First = null then
522 raise Constraint_Error with "map is empty";
525 return T.First.Element.all;
532 function First_Key (Container : Map) return Key_Type is
533 T : Tree_Type renames Container.Tree;
536 if T.First = null then
537 raise Constraint_Error with "map is empty";
540 return T.First.Key.all;
547 function Floor (Container : Map; Key : Key_Type) return Cursor is
548 Node : constant Node_Access := Key_Ops.Floor (Container.Tree, Key);
555 return Cursor'(Container'Unrestricted_Access, Node);
562 procedure Free (X : in out Node_Access) is
563 procedure Deallocate is
564 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
582 Free_Element (X.Element);
593 Free_Element (X.Element);
609 function Has_Element (Position : Cursor) return Boolean is
611 return Position /= No_Element;
619 (Container : in out Map;
621 New_Item : Element_Type)
630 Insert (Container, Key, New_Item, Position, Inserted);
633 if Container.Tree.Lock > 0 then
634 raise Program_Error with
635 "attempt to tamper with cursors (map is locked)";
638 K := Position.Node.Key;
639 E := Position.Node.Element;
641 Position.Node.Key := new Key_Type'(Key);
644 Position.Node.Element := new Element_Type'(New_Item);
661 (Container : in out Map;
663 New_Item : Element_Type;
664 Position : out Cursor;
665 Inserted : out Boolean)
667 function New_Node return Node_Access;
668 pragma Inline (New_Node);
670 procedure Insert_Post is
671 new Key_Ops.Generic_Insert_Post (New_Node);
673 procedure Insert_Sans_Hint is
674 new Key_Ops.Generic_Conditional_Insert (Insert_Post);
680 function New_Node return Node_Access is
681 Node : Node_Access := new Node_Type;
684 Node.Key := new Key_Type'(Key);
685 Node.Element := new Element_Type'(New_Item);
691 -- On exception, deallocate key and elem
693 Free (Node); -- Note that Free deallocates key and elem too
697 -- Start of processing for Insert
706 Position.Container := Container'Unrestricted_Access;
710 (Container : in out Map;
712 New_Item : Element_Type)
719 Insert (Container, Key, New_Item, Position, Inserted);
722 raise Constraint_Error with "key already in map";
730 function Is_Empty (Container : Map) return Boolean is
732 return Container.Tree.Length = 0;
735 ------------------------
736 -- Is_Equal_Node_Node --
737 ------------------------
739 function Is_Equal_Node_Node
740 (L, R : Node_Access) return Boolean is
742 if L.Key.all < R.Key.all then
745 elsif R.Key.all < L.Key.all then
749 return L.Element.all = R.Element.all;
751 end Is_Equal_Node_Node;
753 -------------------------
754 -- Is_Greater_Key_Node --
755 -------------------------
757 function Is_Greater_Key_Node
759 Right : Node_Access) return Boolean
762 -- k > node same as node < k
764 return Right.Key.all < Left;
765 end Is_Greater_Key_Node;
767 ----------------------
768 -- Is_Less_Key_Node --
769 ----------------------
771 function Is_Less_Key_Node
773 Right : Node_Access) return Boolean is
775 return Left < Right.Key.all;
776 end Is_Less_Key_Node;
784 Process : not null access procedure (Position : Cursor))
786 procedure Process_Node (Node : Node_Access);
787 pragma Inline (Process_Node);
789 procedure Local_Iterate is
790 new Tree_Operations.Generic_Iteration (Process_Node);
796 procedure Process_Node (Node : Node_Access) is
798 Process (Cursor'(Container'Unrestricted_Access, Node));
801 B : Natural renames Container.Tree'Unrestricted_Access.all.Busy;
803 -- Start of processing for Iterate
809 Local_Iterate (Container.Tree);
823 function Key (Position : Cursor) return Key_Type is
825 if Position.Node = null then
826 raise Constraint_Error with
827 "Position cursor of function Key equals No_Element";
830 if Position.Node.Key = null then
831 raise Program_Error with
832 "Position cursor of function Key is bad";
835 pragma Assert (Vet (Position.Container.Tree, Position.Node),
836 "Position cursor of function Key is bad");
838 return Position.Node.Key.all;
845 function Last (Container : Map) return Cursor is
846 T : Tree_Type renames Container.Tree;
849 if T.Last = null then
853 return Cursor'(Container'Unrestricted_Access, T.Last);
860 function Last_Element (Container : Map) return Element_Type is
861 T : Tree_Type renames Container.Tree;
864 if T.Last = null then
865 raise Constraint_Error with "map is empty";
868 return T.Last.Element.all;
875 function Last_Key (Container : Map) return Key_Type is
876 T : Tree_Type renames Container.Tree;
879 if T.Last = null then
880 raise Constraint_Error with "map is empty";
883 return T.Last.Key.all;
890 function Left (Node : Node_Access) return Node_Access is
899 function Length (Container : Map) return Count_Type is
901 return Container.Tree.Length;
909 new Tree_Operations.Generic_Move (Clear);
911 procedure Move (Target : in out Map; Source : in out Map) is
913 Move (Target => Target.Tree, Source => Source.Tree);
920 function Next (Position : Cursor) return Cursor is
922 if Position = No_Element then
926 pragma Assert (Position.Node /= null);
927 pragma Assert (Position.Node.Key /= null);
928 pragma Assert (Position.Node.Element /= null);
929 pragma Assert (Vet (Position.Container.Tree, Position.Node),
930 "Position cursor of Next is bad");
933 Node : constant Node_Access :=
934 Tree_Operations.Next (Position.Node);
940 return Cursor'(Position.Container, Node);
945 procedure Next (Position : in out Cursor) is
947 Position := Next (Position);
954 function Parent (Node : Node_Access) return Node_Access is
963 function Previous (Position : Cursor) return Cursor is
965 if Position = No_Element then
969 pragma Assert (Position.Node /= null);
970 pragma Assert (Position.Node.Key /= null);
971 pragma Assert (Position.Node.Element /= null);
972 pragma Assert (Vet (Position.Container.Tree, Position.Node),
973 "Position cursor of Previous is bad");
976 Node : constant Node_Access :=
977 Tree_Operations.Previous (Position.Node);
984 return Cursor'(Position.Container, Node);
988 procedure Previous (Position : in out Cursor) is
990 Position := Previous (Position);
997 procedure Query_Element
999 Process : not null access procedure (Key : Key_Type;
1000 Element : Element_Type))
1003 if Position.Node = null then
1004 raise Constraint_Error with
1005 "Position cursor of Query_Element equals No_Element";
1008 if Position.Node.Key = null
1009 or else Position.Node.Element = null
1011 raise Program_Error with
1012 "Position cursor of Query_Element is bad";
1015 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1016 "Position cursor of Query_Element is bad");
1019 T : Tree_Type renames Position.Container.Tree;
1021 B : Natural renames T.Busy;
1022 L : Natural renames T.Lock;
1029 K : Key_Type renames Position.Node.Key.all;
1030 E : Element_Type renames Position.Node.Element.all;
1051 (Stream : not null access Root_Stream_Type'Class;
1052 Container : out Map)
1055 (Stream : access Root_Stream_Type'Class) return Node_Access;
1056 pragma Inline (Read_Node);
1059 new Tree_Operations.Generic_Read (Clear, Read_Node);
1066 (Stream : access Root_Stream_Type'Class) return Node_Access
1068 Node : Node_Access := new Node_Type;
1070 Node.Key := new Key_Type'(Key_Type'Input (Stream));
1071 Node.Element := new Element_Type'(Element_Type'Input (Stream));
1075 Free (Node); -- Note that Free deallocates key and elem too
1079 -- Start of processing for Read
1082 Read (Stream, Container.Tree);
1086 (Stream : not null access Root_Stream_Type'Class;
1090 raise Program_Error with "attempt to stream map cursor";
1098 (Container : in out Map;
1100 New_Item : Element_Type)
1102 Node : constant Node_Access :=
1103 Key_Ops.Find (Container.Tree, Key);
1110 raise Constraint_Error with "key not in map";
1113 if Container.Tree.Lock > 0 then
1114 raise Program_Error with
1115 "attempt to tamper with cursors (map is locked)";
1121 Node.Key := new Key_Type'(Key);
1124 Node.Element := new Element_Type'(New_Item);
1135 ---------------------
1136 -- Replace_Element --
1137 ---------------------
1139 procedure Replace_Element
1140 (Container : in out Map;
1142 New_Item : Element_Type)
1145 if Position.Node = null then
1146 raise Constraint_Error with
1147 "Position cursor of Replace_Element equals No_Element";
1150 if Position.Node.Key = null
1151 or else Position.Node.Element = null
1153 raise Program_Error with
1154 "Position cursor of Replace_Element is bad";
1157 if Position.Container /= Container'Unrestricted_Access then
1158 raise Program_Error with
1159 "Position cursor of Replace_Element designates wrong map";
1162 if Container.Tree.Lock > 0 then
1163 raise Program_Error with
1164 "attempt to tamper with cursors (map is locked)";
1167 pragma Assert (Vet (Container.Tree, Position.Node),
1168 "Position cursor of Replace_Element is bad");
1171 X : Element_Access := Position.Node.Element;
1174 Position.Node.Element := new Element_Type'(New_Item);
1177 end Replace_Element;
1179 ---------------------
1180 -- Reverse_Iterate --
1181 ---------------------
1183 procedure Reverse_Iterate
1185 Process : not null access procedure (Position : Cursor))
1187 procedure Process_Node (Node : Node_Access);
1188 pragma Inline (Process_Node);
1190 procedure Local_Reverse_Iterate is
1191 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1197 procedure Process_Node (Node : Node_Access) is
1199 Process (Cursor'(Container'Unrestricted_Access, Node));
1202 B : Natural renames Container.Tree'Unrestricted_Access.all.Busy;
1204 -- Start of processing for Reverse_Iterate
1210 Local_Reverse_Iterate (Container.Tree);
1218 end Reverse_Iterate;
1224 function Right (Node : Node_Access) return Node_Access is
1233 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1235 Node.Color := Color;
1242 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1251 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1253 Node.Parent := Parent;
1260 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1262 Node.Right := Right;
1265 --------------------
1266 -- Update_Element --
1267 --------------------
1269 procedure Update_Element
1270 (Container : in out Map;
1272 Process : not null access procedure (Key : Key_Type;
1273 Element : in out Element_Type))
1276 if Position.Node = null then
1277 raise Constraint_Error with
1278 "Position cursor of Update_Element equals No_Element";
1281 if Position.Node.Key = null
1282 or else Position.Node.Element = null
1284 raise Program_Error with
1285 "Position cursor of Update_Element is bad";
1288 if Position.Container /= Container'Unrestricted_Access then
1289 raise Program_Error with
1290 "Position cursor of Update_Element designates wrong map";
1293 pragma Assert (Vet (Container.Tree, Position.Node),
1294 "Position cursor of Update_Element is bad");
1297 T : Tree_Type renames Position.Container.Tree;
1299 B : Natural renames T.Busy;
1300 L : Natural renames T.Lock;
1307 K : Key_Type renames Position.Node.Key.all;
1308 E : Element_Type renames Position.Node.Element.all;
1329 (Stream : not null access Root_Stream_Type'Class;
1332 procedure Write_Node
1333 (Stream : access Root_Stream_Type'Class;
1334 Node : Node_Access);
1335 pragma Inline (Write_Node);
1338 new Tree_Operations.Generic_Write (Write_Node);
1344 procedure Write_Node
1345 (Stream : access Root_Stream_Type'Class;
1349 Key_Type'Output (Stream, Node.Key.all);
1350 Element_Type'Output (Stream, Node.Element.all);
1353 -- Start of processing for Write
1356 Write (Stream, Container.Tree);
1360 (Stream : not null access Root_Stream_Type'Class;
1364 raise Program_Error with "attempt to stream map cursor";
1367 end Ada.Containers.Indefinite_Ordered_Maps;