1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . H A S H E D _ M A P S --
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.Hash_Tables.Generic_Operations;
33 pragma Elaborate_All (Ada.Containers.Hash_Tables.Generic_Operations);
35 with Ada.Containers.Hash_Tables.Generic_Keys;
36 pragma Elaborate_All (Ada.Containers.Hash_Tables.Generic_Keys);
38 with System; use type System.Address;
40 package body Ada.Containers.Hashed_Maps is
42 type Iterator is new Limited_Controlled and
43 Map_Iterator_Interfaces.Forward_Iterator with
45 Container : Map_Access;
48 overriding procedure Finalize (Object : in out Iterator);
50 overriding function First (Object : Iterator) return Cursor;
52 overriding function Next
54 Position : Cursor) return Cursor;
56 -----------------------
57 -- Local Subprograms --
58 -----------------------
61 (Source : Node_Access) return Node_Access;
62 pragma Inline (Copy_Node);
64 function Equivalent_Key_Node
66 Node : Node_Access) return Boolean;
67 pragma Inline (Equivalent_Key_Node);
69 procedure Free (X : in out Node_Access);
71 function Find_Equal_Key
72 (R_HT : Hash_Table_Type;
73 L_Node : Node_Access) return Boolean;
75 function Hash_Node (Node : Node_Access) return Hash_Type;
76 pragma Inline (Hash_Node);
78 function Next (Node : Node_Access) return Node_Access;
82 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
83 pragma Inline (Read_Node);
85 procedure Set_Next (Node : Node_Access; Next : Node_Access);
86 pragma Inline (Set_Next);
88 function Vet (Position : Cursor) return Boolean;
91 (Stream : not null access Root_Stream_Type'Class;
93 pragma Inline (Write_Node);
95 --------------------------
96 -- Local Instantiations --
97 --------------------------
99 package HT_Ops is new Hash_Tables.Generic_Operations
100 (HT_Types => HT_Types,
101 Hash_Node => Hash_Node,
103 Set_Next => Set_Next,
104 Copy_Node => Copy_Node,
107 package Key_Ops is new Hash_Tables.Generic_Keys
108 (HT_Types => HT_Types,
110 Set_Next => Set_Next,
111 Key_Type => Key_Type,
113 Equivalent_Keys => Equivalent_Key_Node);
115 function Is_Equal is new HT_Ops.Generic_Equal (Find_Equal_Key);
117 procedure Read_Nodes is new HT_Ops.Generic_Read (Read_Node);
118 procedure Write_Nodes is new HT_Ops.Generic_Write (Write_Node);
124 function "=" (Left, Right : Map) return Boolean is
126 return Is_Equal (Left.HT, Right.HT);
133 procedure Adjust (Container : in out Map) is
135 HT_Ops.Adjust (Container.HT);
138 procedure Adjust (Control : in out Reference_Control_Type) is
140 if Control.Container /= null then
142 HT : Hash_Table_Type renames Control.Container.all.HT;
143 B : Natural renames HT.Busy;
144 L : Natural renames HT.Lock;
156 procedure Assign (Target : in out Map; Source : Map) is
157 procedure Insert_Item (Node : Node_Access);
158 pragma Inline (Insert_Item);
160 procedure Insert_Items is new HT_Ops.Generic_Iteration (Insert_Item);
166 procedure Insert_Item (Node : Node_Access) is
168 Target.Insert (Key => Node.Key, New_Item => Node.Element);
171 -- Start of processing for Assign
174 if Target'Address = Source'Address then
180 if Target.Capacity < Source.Length then
181 Target.Reserve_Capacity (Source.Length);
184 Insert_Items (Target.HT);
191 function Capacity (Container : Map) return Count_Type is
193 return HT_Ops.Capacity (Container.HT);
200 procedure Clear (Container : in out Map) is
202 HT_Ops.Clear (Container.HT);
205 ------------------------
206 -- Constant_Reference --
207 ------------------------
209 function Constant_Reference
210 (Container : aliased Map;
211 Position : Cursor) return Constant_Reference_Type
214 if Position.Container = null then
215 raise Constraint_Error with
216 "Position cursor has no element";
219 if Position.Container /= Container'Unrestricted_Access then
220 raise Program_Error with
221 "Position cursor designates wrong map";
226 "Position cursor in Constant_Reference is bad");
229 HT : Hash_Table_Type renames Container'Unrestricted_Access.all.HT;
230 B : Natural renames HT.Busy;
231 L : Natural renames HT.Lock;
233 return R : constant Constant_Reference_Type :=
234 (Element => Position.Node.Element'Access,
235 Control => (Controlled with Position.Container))
241 end Constant_Reference;
243 function Constant_Reference
244 (Container : aliased Map;
245 Key : Key_Type) return Constant_Reference_Type
247 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
251 raise Constraint_Error with "key not in map";
255 HT : Hash_Table_Type renames Container'Unrestricted_Access.all.HT;
256 B : Natural renames HT.Busy;
257 L : Natural renames HT.Lock;
259 return R : constant Constant_Reference_Type :=
260 (Element => Node.Element'Access,
261 Control => (Controlled with Container'Unrestricted_Access))
267 end Constant_Reference;
273 function Contains (Container : Map; Key : Key_Type) return Boolean is
275 return Find (Container, Key) /= No_Element;
284 Capacity : Count_Type := 0) return Map
292 elsif Capacity >= Source.Length then
297 with "Requested capacity is less than Source length";
300 return Target : Map do
301 Target.Reserve_Capacity (C);
302 Target.Assign (Source);
311 (Source : Node_Access) return Node_Access
313 Target : constant Node_Access :=
314 new Node_Type'(Key => Source.Key,
315 Element => Source.Element,
325 procedure Delete (Container : in out Map; Key : Key_Type) is
329 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
332 raise Constraint_Error with "attempt to delete key not in map";
338 procedure Delete (Container : in out Map; Position : in out Cursor) is
340 if Position.Node = null then
341 raise Constraint_Error with
342 "Position cursor of Delete equals No_Element";
345 if Position.Container /= Container'Unrestricted_Access then
346 raise Program_Error with
347 "Position cursor of Delete designates wrong map";
350 if Container.HT.Busy > 0 then
351 raise Program_Error with
352 "Delete attempted to tamper with cursors (map is busy)";
355 pragma Assert (Vet (Position), "bad cursor in Delete");
357 HT_Ops.Delete_Node_Sans_Free (Container.HT, Position.Node);
359 Free (Position.Node);
360 Position.Container := null;
367 function Element (Container : Map; Key : Key_Type) return Element_Type is
368 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
372 raise Constraint_Error with
373 "no element available because key not in map";
379 function Element (Position : Cursor) return Element_Type is
381 if Position.Node = null then
382 raise Constraint_Error with
383 "Position cursor of function Element equals No_Element";
386 pragma Assert (Vet (Position), "bad cursor in function Element");
388 return Position.Node.Element;
391 -------------------------
392 -- Equivalent_Key_Node --
393 -------------------------
395 function Equivalent_Key_Node
397 Node : Node_Access) return Boolean is
399 return Equivalent_Keys (Key, Node.Key);
400 end Equivalent_Key_Node;
402 ---------------------
403 -- Equivalent_Keys --
404 ---------------------
406 function Equivalent_Keys (Left, Right : Cursor)
409 if Left.Node = null then
410 raise Constraint_Error with
411 "Left cursor of Equivalent_Keys equals No_Element";
414 if Right.Node = null then
415 raise Constraint_Error with
416 "Right cursor of Equivalent_Keys equals No_Element";
419 pragma Assert (Vet (Left), "Left cursor of Equivalent_Keys is bad");
420 pragma Assert (Vet (Right), "Right cursor of Equivalent_Keys is bad");
422 return Equivalent_Keys (Left.Node.Key, Right.Node.Key);
425 function Equivalent_Keys (Left : Cursor; Right : Key_Type) return Boolean is
427 if Left.Node = null then
428 raise Constraint_Error with
429 "Left cursor of Equivalent_Keys equals No_Element";
432 pragma Assert (Vet (Left), "Left cursor in Equivalent_Keys is bad");
434 return Equivalent_Keys (Left.Node.Key, Right);
437 function Equivalent_Keys (Left : Key_Type; Right : Cursor) return Boolean is
439 if Right.Node = null then
440 raise Constraint_Error with
441 "Right cursor of Equivalent_Keys equals No_Element";
444 pragma Assert (Vet (Right), "Right cursor of Equivalent_Keys is bad");
446 return Equivalent_Keys (Left, Right.Node.Key);
453 procedure Exclude (Container : in out Map; Key : Key_Type) is
456 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
464 procedure Finalize (Container : in out Map) is
466 HT_Ops.Finalize (Container.HT);
469 procedure Finalize (Object : in out Iterator) is
471 if Object.Container /= null then
473 B : Natural renames Object.Container.all.HT.Busy;
480 procedure Finalize (Control : in out Reference_Control_Type) is
482 if Control.Container /= null then
484 HT : Hash_Table_Type renames Control.Container.all.HT;
485 B : Natural renames HT.Busy;
486 L : Natural renames HT.Lock;
492 Control.Container := null;
500 function Find (Container : Map; Key : Key_Type) return Cursor is
501 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
508 return Cursor'(Container'Unrestricted_Access, Node);
515 function Find_Equal_Key
516 (R_HT : Hash_Table_Type;
517 L_Node : Node_Access) return Boolean
519 R_Index : constant Hash_Type := Key_Ops.Index (R_HT, L_Node.Key);
520 R_Node : Node_Access := R_HT.Buckets (R_Index);
523 while R_Node /= null loop
524 if Equivalent_Keys (L_Node.Key, R_Node.Key) then
525 return L_Node.Element = R_Node.Element;
528 R_Node := R_Node.Next;
538 function First (Container : Map) return Cursor is
539 Node : constant Node_Access := HT_Ops.First (Container.HT);
546 return Cursor'(Container'Unrestricted_Access, Node);
549 function First (Object : Iterator) return Cursor is
551 return Object.Container.First;
558 procedure Free (X : in out Node_Access) is
559 procedure Deallocate is
560 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
563 X.Next := X; -- detect mischief (in Vet)
572 function Has_Element (Position : Cursor) return Boolean is
574 pragma Assert (Vet (Position), "bad cursor in Has_Element");
575 return Position.Node /= null;
582 function Hash_Node (Node : Node_Access) return Hash_Type is
584 return Hash (Node.Key);
592 (Container : in out Map;
594 New_Item : Element_Type)
600 Insert (Container, Key, New_Item, Position, Inserted);
603 if Container.HT.Lock > 0 then
604 raise Program_Error with
605 "Include attempted to tamper with elements (map is locked)";
608 Position.Node.Key := Key;
609 Position.Node.Element := New_Item;
618 (Container : in out Map;
620 Position : out Cursor;
621 Inserted : out Boolean)
623 function New_Node (Next : Node_Access) return Node_Access;
624 pragma Inline (New_Node);
626 procedure Local_Insert is
627 new Key_Ops.Generic_Conditional_Insert (New_Node);
633 function New_Node (Next : Node_Access) return Node_Access is
635 return new Node_Type'(Key => Key,
640 HT : Hash_Table_Type renames Container.HT;
642 -- Start of processing for Insert
645 if HT_Ops.Capacity (HT) = 0 then
646 HT_Ops.Reserve_Capacity (HT, 1);
649 Local_Insert (HT, Key, Position.Node, Inserted);
652 and then HT.Length > HT_Ops.Capacity (HT)
654 HT_Ops.Reserve_Capacity (HT, HT.Length);
657 Position.Container := Container'Unrestricted_Access;
661 (Container : in out Map;
663 New_Item : Element_Type;
664 Position : out Cursor;
665 Inserted : out Boolean)
667 function New_Node (Next : Node_Access) return Node_Access;
668 pragma Inline (New_Node);
670 procedure Local_Insert is
671 new Key_Ops.Generic_Conditional_Insert (New_Node);
677 function New_Node (Next : Node_Access) return Node_Access is
679 return new Node_Type'(Key, New_Item, Next);
682 HT : Hash_Table_Type renames Container.HT;
684 -- Start of processing for Insert
687 if HT_Ops.Capacity (HT) = 0 then
688 HT_Ops.Reserve_Capacity (HT, 1);
691 Local_Insert (HT, Key, Position.Node, Inserted);
694 and then HT.Length > HT_Ops.Capacity (HT)
696 HT_Ops.Reserve_Capacity (HT, HT.Length);
699 Position.Container := Container'Unrestricted_Access;
703 (Container : in out Map;
705 New_Item : Element_Type)
708 pragma Unreferenced (Position);
713 Insert (Container, Key, New_Item, Position, Inserted);
716 raise Constraint_Error with
717 "attempt to insert key already in map";
725 function Is_Empty (Container : Map) return Boolean is
727 return Container.HT.Length = 0;
736 Process : not null access procedure (Position : Cursor))
738 procedure Process_Node (Node : Node_Access);
739 pragma Inline (Process_Node);
741 procedure Local_Iterate is new HT_Ops.Generic_Iteration (Process_Node);
747 procedure Process_Node (Node : Node_Access) is
749 Process (Cursor'(Container'Unrestricted_Access, Node));
752 B : Natural renames Container'Unrestricted_Access.all.HT.Busy;
754 -- Start of processing for Iterate
760 Local_Iterate (Container.HT);
771 (Container : Map) return Map_Iterator_Interfaces.Forward_Iterator'Class
773 B : Natural renames Container'Unrestricted_Access.all.HT.Busy;
775 return It : constant Iterator :=
776 (Limited_Controlled with Container => Container'Unrestricted_Access)
786 function Key (Position : Cursor) return Key_Type is
788 if Position.Node = null then
789 raise Constraint_Error with
790 "Position cursor of function Key equals No_Element";
793 pragma Assert (Vet (Position), "bad cursor in function Key");
795 return Position.Node.Key;
802 function Length (Container : Map) return Count_Type is
804 return Container.HT.Length;
812 (Target : in out Map;
816 HT_Ops.Move (Target => Target.HT, Source => Source.HT);
823 function Next (Node : Node_Access) return Node_Access is
828 function Next (Position : Cursor) return Cursor is
830 if Position.Node = null then
834 pragma Assert (Vet (Position), "bad cursor in function Next");
837 HT : Hash_Table_Type renames Position.Container.HT;
838 Node : constant Node_Access := HT_Ops.Next (HT, Position.Node);
845 return Cursor'(Position.Container, Node);
849 procedure Next (Position : in out Cursor) is
851 Position := Next (Position);
856 Position : Cursor) return Cursor
859 if Position.Container = null then
863 if Position.Container /= Object.Container then
864 raise Program_Error with
865 "Position cursor of Next designates wrong map";
868 return Next (Position);
875 procedure Query_Element
877 Process : not null access
878 procedure (Key : Key_Type; Element : Element_Type))
881 if Position.Node = null then
882 raise Constraint_Error with
883 "Position cursor of Query_Element equals No_Element";
886 pragma Assert (Vet (Position), "bad cursor in Query_Element");
889 M : Map renames Position.Container.all;
890 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
892 B : Natural renames HT.Busy;
893 L : Natural renames HT.Lock;
900 K : Key_Type renames Position.Node.Key;
901 E : Element_Type renames Position.Node.Element;
922 (Stream : not null access Root_Stream_Type'Class;
926 Read_Nodes (Stream, Container.HT);
930 (Stream : not null access Root_Stream_Type'Class;
934 raise Program_Error with "attempt to stream map cursor";
938 (Stream : not null access Root_Stream_Type'Class;
939 Item : out Reference_Type)
942 raise Program_Error with "attempt to stream reference";
946 (Stream : not null access Root_Stream_Type'Class;
947 Item : out Constant_Reference_Type)
950 raise Program_Error with "attempt to stream reference";
958 (Container : aliased in out Map;
959 Position : Cursor) return Reference_Type
962 if Position.Container = null then
963 raise Constraint_Error with
964 "Position cursor has no element";
967 if Position.Container /= Container'Unrestricted_Access then
968 raise Program_Error with
969 "Position cursor designates wrong map";
974 "Position cursor in function Reference is bad");
977 HT : Hash_Table_Type renames Container'Unrestricted_Access.all.HT;
978 B : Natural renames HT.Busy;
979 L : Natural renames HT.Lock;
981 return R : constant Reference_Type :=
982 (Element => Position.Node.Element'Access,
983 Control => (Controlled with Position.Container))
992 (Container : aliased in out Map;
993 Key : Key_Type) return Reference_Type
995 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
999 raise Constraint_Error with "key not in map";
1003 HT : Hash_Table_Type renames Container'Unrestricted_Access.all.HT;
1004 B : Natural renames HT.Busy;
1005 L : Natural renames HT.Lock;
1007 return R : constant Reference_Type :=
1008 (Element => Node.Element'Access,
1009 Control => (Controlled with Container'Unrestricted_Access))
1022 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1024 Node : Node_Access := new Node_Type;
1027 Key_Type'Read (Stream, Node.Key);
1028 Element_Type'Read (Stream, Node.Element);
1042 (Container : in out Map;
1044 New_Item : Element_Type)
1046 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
1050 raise Constraint_Error with
1051 "attempt to replace key not in map";
1054 if Container.HT.Lock > 0 then
1055 raise Program_Error with
1056 "Replace attempted to tamper with elements (map is locked)";
1060 Node.Element := New_Item;
1063 ---------------------
1064 -- Replace_Element --
1065 ---------------------
1067 procedure Replace_Element
1068 (Container : in out Map;
1070 New_Item : Element_Type)
1073 if Position.Node = null then
1074 raise Constraint_Error with
1075 "Position cursor of Replace_Element equals No_Element";
1078 if Position.Container /= Container'Unrestricted_Access then
1079 raise Program_Error with
1080 "Position cursor of Replace_Element designates wrong map";
1083 if Position.Container.HT.Lock > 0 then
1084 raise Program_Error with
1085 "Replace_Element attempted to tamper with elements (map is locked)";
1088 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
1090 Position.Node.Element := New_Item;
1091 end Replace_Element;
1093 ----------------------
1094 -- Reserve_Capacity --
1095 ----------------------
1097 procedure Reserve_Capacity
1098 (Container : in out Map;
1099 Capacity : Count_Type)
1102 HT_Ops.Reserve_Capacity (Container.HT, Capacity);
1103 end Reserve_Capacity;
1109 procedure Set_Next (Node : Node_Access; Next : Node_Access) is
1114 --------------------
1115 -- Update_Element --
1116 --------------------
1118 procedure Update_Element
1119 (Container : in out Map;
1121 Process : not null access procedure (Key : Key_Type;
1122 Element : in out Element_Type))
1125 if Position.Node = null then
1126 raise Constraint_Error with
1127 "Position cursor of Update_Element equals No_Element";
1130 if Position.Container /= Container'Unrestricted_Access then
1131 raise Program_Error with
1132 "Position cursor of Update_Element designates wrong map";
1135 pragma Assert (Vet (Position), "bad cursor in Update_Element");
1138 HT : Hash_Table_Type renames Container.HT;
1139 B : Natural renames HT.Busy;
1140 L : Natural renames HT.Lock;
1147 K : Key_Type renames Position.Node.Key;
1148 E : Element_Type renames Position.Node.Element;
1169 function Vet (Position : Cursor) return Boolean is
1171 if Position.Node = null then
1172 return Position.Container = null;
1175 if Position.Container = null then
1179 if Position.Node.Next = Position.Node then
1184 HT : Hash_Table_Type renames Position.Container.HT;
1188 if HT.Length = 0 then
1192 if HT.Buckets = null
1193 or else HT.Buckets'Length = 0
1198 X := HT.Buckets (Key_Ops.Index (HT, Position.Node.Key));
1200 for J in 1 .. HT.Length loop
1201 if X = Position.Node then
1209 if X = X.Next then -- to prevent unnecessary looping
1225 (Stream : not null access Root_Stream_Type'Class;
1229 Write_Nodes (Stream, Container.HT);
1233 (Stream : not null access Root_Stream_Type'Class;
1237 raise Program_Error with "attempt to stream map cursor";
1241 (Stream : not null access Root_Stream_Type'Class;
1242 Item : Reference_Type)
1245 raise Program_Error with "attempt to stream reference";
1249 (Stream : not null access Root_Stream_Type'Class;
1250 Item : Constant_Reference_Type)
1253 raise Program_Error with "attempt to stream reference";
1260 procedure Write_Node
1261 (Stream : not null access Root_Stream_Type'Class;
1265 Key_Type'Write (Stream, Node.Key);
1266 Element_Type'Write (Stream, Node.Element);
1269 end Ada.Containers.Hashed_Maps;