1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.INDEFINITE_HASHED_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.Containers.Hash_Tables.Generic_Operations;
31 pragma Elaborate_All (Ada.Containers.Hash_Tables.Generic_Operations);
33 with Ada.Containers.Hash_Tables.Generic_Keys;
34 pragma Elaborate_All (Ada.Containers.Hash_Tables.Generic_Keys);
36 with Ada.Unchecked_Deallocation;
38 with System; use type System.Address;
40 package body Ada.Containers.Indefinite_Hashed_Maps is
43 new Ada.Unchecked_Deallocation (Key_Type, Key_Access);
45 procedure Free_Element is
46 new Ada.Unchecked_Deallocation (Element_Type, Element_Access);
48 type Iterator is new Limited_Controlled and
49 Map_Iterator_Interfaces.Forward_Iterator with
51 Container : Map_Access;
54 overriding procedure Finalize (Object : in out Iterator);
56 overriding function First (Object : Iterator) return Cursor;
58 overriding function Next
60 Position : Cursor) return Cursor;
62 -----------------------
63 -- Local Subprograms --
64 -----------------------
66 function Copy_Node (Node : Node_Access) return Node_Access;
67 pragma Inline (Copy_Node);
69 function Equivalent_Key_Node
71 Node : Node_Access) return Boolean;
72 pragma Inline (Equivalent_Key_Node);
74 function Find_Equal_Key
75 (R_HT : Hash_Table_Type;
76 L_Node : Node_Access) return Boolean;
78 procedure Free (X : in out Node_Access);
79 -- pragma Inline (Free);
81 function Hash_Node (Node : Node_Access) return Hash_Type;
82 pragma Inline (Hash_Node);
84 function Next (Node : Node_Access) return Node_Access;
88 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
90 procedure Set_Next (Node : Node_Access; Next : Node_Access);
91 pragma Inline (Set_Next);
93 function Vet (Position : Cursor) return Boolean;
96 (Stream : not null access Root_Stream_Type'Class;
99 --------------------------
100 -- Local Instantiations --
101 --------------------------
103 package HT_Ops is new Ada.Containers.Hash_Tables.Generic_Operations
104 (HT_Types => HT_Types,
105 Hash_Node => Hash_Node,
107 Set_Next => Set_Next,
108 Copy_Node => Copy_Node,
111 package Key_Ops is new Hash_Tables.Generic_Keys
112 (HT_Types => HT_Types,
114 Set_Next => Set_Next,
115 Key_Type => Key_Type,
117 Equivalent_Keys => Equivalent_Key_Node);
123 function Is_Equal is new HT_Ops.Generic_Equal (Find_Equal_Key);
125 overriding function "=" (Left, Right : Map) return Boolean is
127 return Is_Equal (Left.HT, Right.HT);
134 procedure Adjust (Container : in out Map) is
136 HT_Ops.Adjust (Container.HT);
139 procedure Adjust (Control : in out Reference_Control_Type) is
141 if Control.Container /= null then
143 M : Map renames Control.Container.all;
144 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
145 B : Natural renames HT.Busy;
146 L : Natural renames HT.Lock;
158 procedure Assign (Target : in out Map; Source : Map) is
159 procedure Insert_Item (Node : Node_Access);
160 pragma Inline (Insert_Item);
162 procedure Insert_Items is new HT_Ops.Generic_Iteration (Insert_Item);
168 procedure Insert_Item (Node : Node_Access) is
170 Target.Insert (Key => Node.Key.all, New_Item => Node.Element.all);
173 -- Start of processing for Assign
176 if Target'Address = Source'Address then
182 if Target.Capacity < Source.Length then
183 Target.Reserve_Capacity (Source.Length);
186 Insert_Items (Target.HT);
193 function Capacity (Container : Map) return Count_Type is
195 return HT_Ops.Capacity (Container.HT);
202 procedure Clear (Container : in out Map) is
204 HT_Ops.Clear (Container.HT);
207 ------------------------
208 -- Constant_Reference --
209 ------------------------
211 function Constant_Reference
212 (Container : aliased Map;
213 Position : Cursor) return Constant_Reference_Type
216 if Position.Container = null then
217 raise Constraint_Error with
218 "Position cursor has no element";
221 if Position.Container /= Container'Unrestricted_Access then
222 raise Program_Error with
223 "Position cursor designates wrong map";
226 if Position.Node.Element = null then
227 raise Program_Error with
228 "Position cursor has no element";
233 "Position cursor in Constant_Reference is bad");
236 M : Map renames Position.Container.all;
237 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
238 B : Natural renames HT.Busy;
239 L : Natural renames HT.Lock;
241 return R : constant Constant_Reference_Type :=
242 (Element => Position.Node.Element.all'Access,
244 (Controlled with Container'Unrestricted_Access))
250 end Constant_Reference;
252 function Constant_Reference
253 (Container : aliased Map;
254 Key : Key_Type) return Constant_Reference_Type
256 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
260 raise Constraint_Error with "key not in map";
263 if Node.Element = null then
264 raise Program_Error with "key has no element";
268 M : Map renames Container'Unrestricted_Access.all;
269 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
270 B : Natural renames HT.Busy;
271 L : Natural renames HT.Lock;
273 return R : constant Constant_Reference_Type :=
274 (Element => Node.Element.all'Access,
276 (Controlled with Container'Unrestricted_Access))
282 end Constant_Reference;
288 function Contains (Container : Map; Key : Key_Type) return Boolean is
290 return Find (Container, Key) /= No_Element;
299 Capacity : Count_Type := 0) return Map
307 elsif Capacity >= Source.Length then
312 with "Requested capacity is less than Source length";
315 return Target : Map do
316 Target.Reserve_Capacity (C);
317 Target.Assign (Source);
325 function Copy_Node (Node : Node_Access) return Node_Access is
326 K : Key_Access := new Key_Type'(Node.Key.all);
330 E := new Element_Type'(Node.Element.all);
331 return new Node_Type'(K, E, null);
344 procedure Delete (Container : in out Map; Key : Key_Type) is
348 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
351 raise Constraint_Error with "attempt to delete key not in map";
357 procedure Delete (Container : in out Map; Position : in out Cursor) is
359 if Position.Node = null then
360 raise Constraint_Error with
361 "Position cursor of Delete equals No_Element";
364 if Position.Container /= Container'Unrestricted_Access then
365 raise Program_Error with
366 "Position cursor of Delete designates wrong map";
369 if Container.HT.Busy > 0 then
370 raise Program_Error with
371 "Delete attempted to tamper with cursors (map is busy)";
374 pragma Assert (Vet (Position), "bad cursor in Delete");
376 HT_Ops.Delete_Node_Sans_Free (Container.HT, Position.Node);
378 Free (Position.Node);
379 Position.Container := null;
386 function Element (Container : Map; Key : Key_Type) return Element_Type is
387 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
391 raise Constraint_Error with
392 "no element available because key not in map";
395 return Node.Element.all;
398 function Element (Position : Cursor) return Element_Type is
400 if Position.Node = null then
401 raise Constraint_Error with
402 "Position cursor of function Element equals No_Element";
405 if Position.Node.Element = null then
406 raise Program_Error with
407 "Position cursor of function Element is bad";
410 pragma Assert (Vet (Position), "bad cursor in function Element");
412 return Position.Node.Element.all;
415 -------------------------
416 -- Equivalent_Key_Node --
417 -------------------------
419 function Equivalent_Key_Node
421 Node : Node_Access) return Boolean
424 return Equivalent_Keys (Key, Node.Key.all);
425 end Equivalent_Key_Node;
427 ---------------------
428 -- Equivalent_Keys --
429 ---------------------
431 function Equivalent_Keys (Left, Right : Cursor) return Boolean is
433 if Left.Node = null then
434 raise Constraint_Error with
435 "Left cursor of Equivalent_Keys equals No_Element";
438 if Right.Node = null then
439 raise Constraint_Error with
440 "Right cursor of Equivalent_Keys equals No_Element";
443 if Left.Node.Key = null then
444 raise Program_Error with
445 "Left cursor of Equivalent_Keys is bad";
448 if Right.Node.Key = null then
449 raise Program_Error with
450 "Right cursor of Equivalent_Keys is bad";
453 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Keys");
454 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Keys");
456 return Equivalent_Keys (Left.Node.Key.all, Right.Node.Key.all);
459 function Equivalent_Keys
461 Right : Key_Type) return Boolean
464 if Left.Node = null then
465 raise Constraint_Error with
466 "Left cursor of Equivalent_Keys equals No_Element";
469 if Left.Node.Key = null then
470 raise Program_Error with
471 "Left cursor of Equivalent_Keys is bad";
474 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Keys");
476 return Equivalent_Keys (Left.Node.Key.all, Right);
479 function Equivalent_Keys
481 Right : Cursor) return Boolean
484 if Right.Node = null then
485 raise Constraint_Error with
486 "Right cursor of Equivalent_Keys equals No_Element";
489 if Right.Node.Key = null then
490 raise Program_Error with
491 "Right cursor of Equivalent_Keys is bad";
494 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Keys");
496 return Equivalent_Keys (Left, Right.Node.Key.all);
503 procedure Exclude (Container : in out Map; Key : Key_Type) is
506 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
514 procedure Finalize (Container : in out Map) is
516 HT_Ops.Finalize (Container.HT);
519 procedure Finalize (Object : in out Iterator) is
521 if Object.Container /= null then
523 B : Natural renames Object.Container.all.HT.Busy;
530 procedure Finalize (Control : in out Reference_Control_Type) is
532 if Control.Container /= null then
534 M : Map renames Control.Container.all;
535 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
536 B : Natural renames HT.Busy;
537 L : Natural renames HT.Lock;
543 Control.Container := null;
551 function Find (Container : Map; Key : Key_Type) return Cursor is
552 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
559 return Cursor'(Container'Unrestricted_Access, Node);
566 function Find_Equal_Key
567 (R_HT : Hash_Table_Type;
568 L_Node : Node_Access) return Boolean
570 R_Index : constant Hash_Type := Key_Ops.Index (R_HT, L_Node.Key.all);
571 R_Node : Node_Access := R_HT.Buckets (R_Index);
574 while R_Node /= null loop
575 if Equivalent_Keys (L_Node.Key.all, R_Node.Key.all) then
576 return L_Node.Element.all = R_Node.Element.all;
579 R_Node := R_Node.Next;
589 function First (Container : Map) return Cursor is
590 Node : constant Node_Access := HT_Ops.First (Container.HT);
595 return Cursor'(Container'Unrestricted_Access, Node);
599 function First (Object : Iterator) return Cursor is
601 return Object.Container.First;
608 procedure Free (X : in out Node_Access) is
609 procedure Deallocate is
610 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
617 X.Next := X; -- detect mischief (in Vet)
626 Free_Element (X.Element);
637 Free_Element (X.Element);
653 function Has_Element (Position : Cursor) return Boolean is
655 pragma Assert (Vet (Position), "bad cursor in Has_Element");
656 return Position.Node /= null;
663 function Hash_Node (Node : Node_Access) return Hash_Type is
665 return Hash (Node.Key.all);
673 (Container : in out Map;
675 New_Item : Element_Type)
684 Insert (Container, Key, New_Item, Position, Inserted);
687 if Container.HT.Lock > 0 then
688 raise Program_Error with
689 "Include attempted to tamper with elements (map is locked)";
692 K := Position.Node.Key;
693 E := Position.Node.Element;
695 Position.Node.Key := new Key_Type'(Key);
698 Position.Node.Element := new Element_Type'(New_Item);
715 (Container : in out Map;
717 New_Item : Element_Type;
718 Position : out Cursor;
719 Inserted : out Boolean)
721 function New_Node (Next : Node_Access) return Node_Access;
723 procedure Local_Insert is
724 new Key_Ops.Generic_Conditional_Insert (New_Node);
730 function New_Node (Next : Node_Access) return Node_Access is
731 K : Key_Access := new Key_Type'(Key);
735 E := new Element_Type'(New_Item);
736 return new Node_Type'(K, E, Next);
744 HT : Hash_Table_Type renames Container.HT;
746 -- Start of processing for Insert
749 if HT_Ops.Capacity (HT) = 0 then
750 HT_Ops.Reserve_Capacity (HT, 1);
753 Local_Insert (HT, Key, Position.Node, Inserted);
756 and then HT.Length > HT_Ops.Capacity (HT)
758 HT_Ops.Reserve_Capacity (HT, HT.Length);
761 Position.Container := Container'Unchecked_Access;
765 (Container : in out Map;
767 New_Item : Element_Type)
770 pragma Unreferenced (Position);
775 Insert (Container, Key, New_Item, Position, Inserted);
778 raise Constraint_Error with
779 "attempt to insert key already in map";
787 function Is_Empty (Container : Map) return Boolean is
789 return Container.HT.Length = 0;
798 Process : not null access procedure (Position : Cursor))
800 procedure Process_Node (Node : Node_Access);
801 pragma Inline (Process_Node);
803 procedure Local_Iterate is
804 new HT_Ops.Generic_Iteration (Process_Node);
810 procedure Process_Node (Node : Node_Access) is
812 Process (Cursor'(Container'Unrestricted_Access, Node));
815 B : Natural renames Container'Unrestricted_Access.all.HT.Busy;
817 -- Start of processing Iterate
823 Local_Iterate (Container.HT);
834 (Container : Map) return Map_Iterator_Interfaces.Forward_Iterator'Class
836 B : Natural renames Container'Unrestricted_Access.all.HT.Busy;
838 return It : constant Iterator :=
839 (Limited_Controlled with
840 Container => Container'Unrestricted_Access)
850 function Key (Position : Cursor) return Key_Type is
852 if Position.Node = null then
853 raise Constraint_Error with
854 "Position cursor of function Key equals No_Element";
857 if Position.Node.Key = null then
858 raise Program_Error with
859 "Position cursor of function Key is bad";
862 pragma Assert (Vet (Position), "bad cursor in function Key");
864 return Position.Node.Key.all;
871 function Length (Container : Map) return Count_Type is
873 return Container.HT.Length;
881 (Target : in out Map;
885 HT_Ops.Move (Target => Target.HT, Source => Source.HT);
892 function Next (Node : Node_Access) return Node_Access is
897 procedure Next (Position : in out Cursor) is
899 Position := Next (Position);
902 function Next (Position : Cursor) return Cursor is
904 if Position.Node = null then
908 if Position.Node.Key = null
909 or else Position.Node.Element = null
911 raise Program_Error with "Position cursor of Next is bad";
914 pragma Assert (Vet (Position), "Position cursor of Next is bad");
917 HT : Hash_Table_Type renames Position.Container.HT;
918 Node : constant Node_Access := HT_Ops.Next (HT, Position.Node);
923 return Cursor'(Position.Container, Node);
928 function Next (Object : Iterator; Position : Cursor) return Cursor is
930 if Position.Container = null then
934 if Position.Container /= Object.Container then
935 raise Program_Error with
936 "Position cursor of Next designates wrong map";
939 return Next (Position);
946 procedure Query_Element
948 Process : not null access procedure (Key : Key_Type;
949 Element : Element_Type))
952 if Position.Node = null then
953 raise Constraint_Error with
954 "Position cursor of Query_Element equals No_Element";
957 if Position.Node.Key = null
958 or else Position.Node.Element = null
960 raise Program_Error with
961 "Position cursor of Query_Element is bad";
964 pragma Assert (Vet (Position), "bad cursor in Query_Element");
967 M : Map renames Position.Container.all;
968 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
970 B : Natural renames HT.Busy;
971 L : Natural renames HT.Lock;
978 K : Key_Type renames Position.Node.Key.all;
979 E : Element_Type renames Position.Node.Element.all;
999 procedure Read_Nodes is new HT_Ops.Generic_Read (Read_Node);
1002 (Stream : not null access Root_Stream_Type'Class;
1003 Container : out Map)
1006 Read_Nodes (Stream, Container.HT);
1010 (Stream : not null access Root_Stream_Type'Class;
1014 raise Program_Error with "attempt to stream map cursor";
1018 (Stream : not null access Root_Stream_Type'Class;
1019 Item : out Reference_Type)
1022 raise Program_Error with "attempt to stream reference";
1026 (Stream : not null access Root_Stream_Type'Class;
1027 Item : out Constant_Reference_Type)
1030 raise Program_Error with "attempt to stream reference";
1038 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1040 Node : Node_Access := new Node_Type;
1044 Node.Key := new Key_Type'(Key_Type'Input (Stream));
1052 Node.Element := new Element_Type'(Element_Type'Input (Stream));
1055 Free_Key (Node.Key);
1068 (Container : aliased in out Map;
1069 Position : Cursor) return Reference_Type
1072 if Position.Container = null then
1073 raise Constraint_Error with
1074 "Position cursor has no element";
1077 if Position.Container /= Container'Unrestricted_Access then
1078 raise Program_Error with
1079 "Position cursor designates wrong map";
1082 if Position.Node.Element = null then
1083 raise Program_Error with
1084 "Position cursor has no element";
1089 "Position cursor in function Reference is bad");
1092 M : Map renames Position.Container.all;
1093 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
1094 B : Natural renames HT.Busy;
1095 L : Natural renames HT.Lock;
1097 return R : constant Reference_Type :=
1098 (Element => Position.Node.Element.all'Access,
1099 Control => (Controlled with Position.Container))
1108 (Container : aliased in out Map;
1109 Key : Key_Type) return Reference_Type
1111 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
1115 raise Constraint_Error with "key not in map";
1118 if Node.Element = null then
1119 raise Program_Error with "key has no element";
1123 M : Map renames Container'Unrestricted_Access.all;
1124 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
1125 B : Natural renames HT.Busy;
1126 L : Natural renames HT.Lock;
1128 return R : constant Reference_Type :=
1129 (Element => Node.Element.all'Access,
1131 (Controlled with Container'Unrestricted_Access))
1144 (Container : in out Map;
1146 New_Item : Element_Type)
1148 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
1155 raise Constraint_Error with
1156 "attempt to replace key not in map";
1159 if Container.HT.Lock > 0 then
1160 raise Program_Error with
1161 "Replace attempted to tamper with elements (map is locked)";
1167 Node.Key := new Key_Type'(Key);
1170 Node.Element := new Element_Type'(New_Item);
1181 ---------------------
1182 -- Replace_Element --
1183 ---------------------
1185 procedure Replace_Element
1186 (Container : in out Map;
1188 New_Item : Element_Type)
1191 if Position.Node = null then
1192 raise Constraint_Error with
1193 "Position cursor of Replace_Element equals No_Element";
1196 if Position.Node.Key = null
1197 or else Position.Node.Element = null
1199 raise Program_Error with
1200 "Position cursor of Replace_Element is bad";
1203 if Position.Container /= Container'Unrestricted_Access then
1204 raise Program_Error with
1205 "Position cursor of Replace_Element designates wrong map";
1208 if Position.Container.HT.Lock > 0 then
1209 raise Program_Error with
1210 "Replace_Element attempted to tamper with elements (map is locked)";
1213 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
1216 X : Element_Access := Position.Node.Element;
1219 Position.Node.Element := new Element_Type'(New_Item);
1222 end Replace_Element;
1224 ----------------------
1225 -- Reserve_Capacity --
1226 ----------------------
1228 procedure Reserve_Capacity
1229 (Container : in out Map;
1230 Capacity : Count_Type)
1233 HT_Ops.Reserve_Capacity (Container.HT, Capacity);
1234 end Reserve_Capacity;
1240 procedure Set_Next (Node : Node_Access; Next : Node_Access) is
1245 --------------------
1246 -- Update_Element --
1247 --------------------
1249 procedure Update_Element
1250 (Container : in out Map;
1252 Process : not null access procedure (Key : Key_Type;
1253 Element : in out Element_Type))
1256 if Position.Node = null then
1257 raise Constraint_Error with
1258 "Position cursor of Update_Element equals No_Element";
1261 if Position.Node.Key = null
1262 or else Position.Node.Element = null
1264 raise Program_Error with
1265 "Position cursor of Update_Element is bad";
1268 if Position.Container /= Container'Unrestricted_Access then
1269 raise Program_Error with
1270 "Position cursor of Update_Element designates wrong map";
1273 pragma Assert (Vet (Position), "bad cursor in Update_Element");
1276 HT : Hash_Table_Type renames Container.HT;
1278 B : Natural renames HT.Busy;
1279 L : Natural renames HT.Lock;
1286 K : Key_Type renames Position.Node.Key.all;
1287 E : Element_Type renames Position.Node.Element.all;
1308 function Vet (Position : Cursor) return Boolean is
1310 if Position.Node = null then
1311 return Position.Container = null;
1314 if Position.Container = null then
1318 if Position.Node.Next = Position.Node then
1322 if Position.Node.Key = null then
1326 if Position.Node.Element = null then
1331 HT : Hash_Table_Type renames Position.Container.HT;
1335 if HT.Length = 0 then
1339 if HT.Buckets = null
1340 or else HT.Buckets'Length = 0
1345 X := HT.Buckets (Key_Ops.Index (HT, Position.Node.Key.all));
1347 for J in 1 .. HT.Length loop
1348 if X = Position.Node then
1356 if X = X.Next then -- to prevent unnecessary looping
1371 procedure Write_Nodes is new HT_Ops.Generic_Write (Write_Node);
1374 (Stream : not null access Root_Stream_Type'Class;
1378 Write_Nodes (Stream, Container.HT);
1382 (Stream : not null access Root_Stream_Type'Class;
1386 raise Program_Error with "attempt to stream map cursor";
1390 (Stream : not null access Root_Stream_Type'Class;
1391 Item : Reference_Type)
1394 raise Program_Error with "attempt to stream reference";
1398 (Stream : not null access Root_Stream_Type'Class;
1399 Item : Constant_Reference_Type)
1402 raise Program_Error with "attempt to stream reference";
1409 procedure Write_Node
1410 (Stream : not null access Root_Stream_Type'Class;
1414 Key_Type'Output (Stream, Node.Key.all);
1415 Element_Type'Output (Stream, Node.Element.all);
1418 end Ada.Containers.Indefinite_Hashed_Maps;