1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.INDEFINITE_HASHED_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.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 package body Ada.Containers.Indefinite_Hashed_Maps is
41 new Ada.Unchecked_Deallocation (Key_Type, Key_Access);
43 procedure Free_Element is
44 new Ada.Unchecked_Deallocation (Element_Type, Element_Access);
47 Map_Iterator_Interfaces.Forward_Iterator with record
48 Container : Map_Access;
52 overriding function First (Object : Iterator) return Cursor;
53 overriding function Next (Object : Iterator; Position : Cursor)
56 -----------------------
57 -- Local Subprograms --
58 -----------------------
60 function Copy_Node (Node : Node_Access) return Node_Access;
61 pragma Inline (Copy_Node);
63 function Equivalent_Key_Node
65 Node : Node_Access) return Boolean;
66 pragma Inline (Equivalent_Key_Node);
68 function Find_Equal_Key
69 (R_HT : Hash_Table_Type;
70 L_Node : Node_Access) return Boolean;
72 procedure Free (X : in out Node_Access);
73 -- pragma Inline (Free);
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;
84 procedure Set_Next (Node : Node_Access; Next : Node_Access);
85 pragma Inline (Set_Next);
87 function Vet (Position : Cursor) return Boolean;
90 (Stream : not null access Root_Stream_Type'Class;
93 --------------------------
94 -- Local Instantiations --
95 --------------------------
97 package HT_Ops is new Ada.Containers.Hash_Tables.Generic_Operations
98 (HT_Types => HT_Types,
99 Hash_Node => Hash_Node,
101 Set_Next => Set_Next,
102 Copy_Node => Copy_Node,
105 package Key_Ops is new Hash_Tables.Generic_Keys
106 (HT_Types => HT_Types,
108 Set_Next => Set_Next,
109 Key_Type => Key_Type,
111 Equivalent_Keys => Equivalent_Key_Node);
117 function Is_Equal is new HT_Ops.Generic_Equal (Find_Equal_Key);
119 overriding function "=" (Left, Right : Map) return Boolean is
121 return Is_Equal (Left.HT, Right.HT);
128 procedure Adjust (Container : in out Map) is
130 HT_Ops.Adjust (Container.HT);
137 function Capacity (Container : Map) return Count_Type is
139 return HT_Ops.Capacity (Container.HT);
146 procedure Clear (Container : in out Map) is
148 HT_Ops.Clear (Container.HT);
155 function Contains (Container : Map; Key : Key_Type) return Boolean is
157 return Find (Container, Key) /= No_Element;
164 function Copy_Node (Node : Node_Access) return Node_Access is
165 K : Key_Access := new Key_Type'(Node.Key.all);
169 E := new Element_Type'(Node.Element.all);
170 return new Node_Type'(K, E, null);
183 procedure Delete (Container : in out Map; Key : Key_Type) is
187 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
190 raise Constraint_Error with "attempt to delete key not in map";
196 procedure Delete (Container : in out Map; Position : in out Cursor) is
198 if Position.Node = null then
199 raise Constraint_Error with
200 "Position cursor of Delete equals No_Element";
203 if Position.Container /= Container'Unrestricted_Access then
204 raise Program_Error with
205 "Position cursor of Delete designates wrong map";
208 if Container.HT.Busy > 0 then
209 raise Program_Error with
210 "Delete attempted to tamper with cursors (map is busy)";
213 pragma Assert (Vet (Position), "bad cursor in Delete");
215 HT_Ops.Delete_Node_Sans_Free (Container.HT, Position.Node);
217 Free (Position.Node);
218 Position.Container := null;
225 function Element (Container : Map; Key : Key_Type) return Element_Type is
226 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
230 raise Constraint_Error with
231 "no element available because key not in map";
234 return Node.Element.all;
237 function Element (Position : Cursor) return Element_Type is
239 if Position.Node = null then
240 raise Constraint_Error with
241 "Position cursor of function Element equals No_Element";
244 if Position.Node.Element = null then
245 raise Program_Error with
246 "Position cursor of function Element is bad";
249 pragma Assert (Vet (Position), "bad cursor in function Element");
251 return Position.Node.Element.all;
254 -------------------------
255 -- Equivalent_Key_Node --
256 -------------------------
258 function Equivalent_Key_Node
260 Node : Node_Access) return Boolean
263 return Equivalent_Keys (Key, Node.Key.all);
264 end Equivalent_Key_Node;
266 ---------------------
267 -- Equivalent_Keys --
268 ---------------------
270 function Equivalent_Keys (Left, Right : Cursor) return Boolean is
272 if Left.Node = null then
273 raise Constraint_Error with
274 "Left cursor of Equivalent_Keys equals No_Element";
277 if Right.Node = null then
278 raise Constraint_Error with
279 "Right cursor of Equivalent_Keys equals No_Element";
282 if Left.Node.Key = null then
283 raise Program_Error with
284 "Left cursor of Equivalent_Keys is bad";
287 if Right.Node.Key = null then
288 raise Program_Error with
289 "Right cursor of Equivalent_Keys is bad";
292 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Keys");
293 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Keys");
295 return Equivalent_Keys (Left.Node.Key.all, Right.Node.Key.all);
298 function Equivalent_Keys
300 Right : Key_Type) return Boolean
303 if Left.Node = null then
304 raise Constraint_Error with
305 "Left cursor of Equivalent_Keys equals No_Element";
308 if Left.Node.Key = null then
309 raise Program_Error with
310 "Left cursor of Equivalent_Keys is bad";
313 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Keys");
315 return Equivalent_Keys (Left.Node.Key.all, Right);
318 function Equivalent_Keys
320 Right : Cursor) return Boolean
323 if Right.Node = null then
324 raise Constraint_Error with
325 "Right cursor of Equivalent_Keys equals No_Element";
328 if Right.Node.Key = null then
329 raise Program_Error with
330 "Right cursor of Equivalent_Keys is bad";
333 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Keys");
335 return Equivalent_Keys (Left, Right.Node.Key.all);
342 procedure Exclude (Container : in out Map; Key : Key_Type) is
345 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
353 procedure Finalize (Container : in out Map) is
355 HT_Ops.Finalize (Container.HT);
362 function Find (Container : Map; Key : Key_Type) return Cursor is
363 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
370 return Cursor'(Container'Unchecked_Access, Node);
377 function Find_Equal_Key
378 (R_HT : Hash_Table_Type;
379 L_Node : Node_Access) return Boolean
381 R_Index : constant Hash_Type := Key_Ops.Index (R_HT, L_Node.Key.all);
382 R_Node : Node_Access := R_HT.Buckets (R_Index);
385 while R_Node /= null loop
386 if Equivalent_Keys (L_Node.Key.all, R_Node.Key.all) then
387 return L_Node.Element.all = R_Node.Element.all;
390 R_Node := R_Node.Next;
400 function First (Container : Map) return Cursor is
401 Node : constant Node_Access := HT_Ops.First (Container.HT);
408 return Cursor'(Container'Unchecked_Access, Node);
411 function First (Object : Iterator) return Cursor is
412 M : constant Map_Access := Object.Container;
413 N : constant Node_Access := HT_Ops.First (M.HT);
419 return Cursor'(Object.Container.all'Unchecked_Access, N);
426 procedure Free (X : in out Node_Access) is
427 procedure Deallocate is
428 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
434 X.Next := X; -- detect mischief (in Vet)
443 Free_Element (X.Element);
454 Free_Element (X.Element);
470 function Has_Element (Position : Cursor) return Boolean is
472 pragma Assert (Vet (Position), "bad cursor in Has_Element");
473 return Position.Node /= null;
480 function Hash_Node (Node : Node_Access) return Hash_Type is
482 return Hash (Node.Key.all);
490 (Container : in out Map;
492 New_Item : Element_Type)
501 Insert (Container, Key, New_Item, Position, Inserted);
504 if Container.HT.Lock > 0 then
505 raise Program_Error with
506 "Include attempted to tamper with elements (map is locked)";
509 K := Position.Node.Key;
510 E := Position.Node.Element;
512 Position.Node.Key := new Key_Type'(Key);
515 Position.Node.Element := new Element_Type'(New_Item);
532 (Container : in out Map;
534 New_Item : Element_Type;
535 Position : out Cursor;
536 Inserted : out Boolean)
538 function New_Node (Next : Node_Access) return Node_Access;
540 procedure Local_Insert is
541 new Key_Ops.Generic_Conditional_Insert (New_Node);
547 function New_Node (Next : Node_Access) return Node_Access is
548 K : Key_Access := new Key_Type'(Key);
552 E := new Element_Type'(New_Item);
553 return new Node_Type'(K, E, Next);
561 HT : Hash_Table_Type renames Container.HT;
563 -- Start of processing for Insert
566 if HT_Ops.Capacity (HT) = 0 then
567 HT_Ops.Reserve_Capacity (HT, 1);
570 Local_Insert (HT, Key, Position.Node, Inserted);
573 and then HT.Length > HT_Ops.Capacity (HT)
575 HT_Ops.Reserve_Capacity (HT, HT.Length);
578 Position.Container := Container'Unchecked_Access;
582 (Container : in out Map;
584 New_Item : Element_Type)
587 pragma Unreferenced (Position);
592 Insert (Container, Key, New_Item, Position, Inserted);
595 raise Constraint_Error with
596 "attempt to insert key already in map";
604 function Is_Empty (Container : Map) return Boolean is
606 return Container.HT.Length = 0;
615 Process : not null access procedure (Position : Cursor))
617 procedure Process_Node (Node : Node_Access);
618 pragma Inline (Process_Node);
620 procedure Local_Iterate is
621 new HT_Ops.Generic_Iteration (Process_Node);
627 procedure Process_Node (Node : Node_Access) is
629 Process (Cursor'(Container'Unchecked_Access, Node));
632 B : Natural renames Container'Unrestricted_Access.HT.Busy;
634 -- Start of processing Iterate
640 Local_Iterate (Container.HT);
650 function Iterate (Container : Map)
651 return Map_Iterator_Interfaces.Forward_Iterator'class
653 Node : constant Node_Access := HT_Ops.First (Container.HT);
654 It : constant Iterator := (Container'Unrestricted_Access, Node);
663 function Key (Position : Cursor) return Key_Type is
665 if Position.Node = null then
666 raise Constraint_Error with
667 "Position cursor of function Key equals No_Element";
670 if Position.Node.Key = null then
671 raise Program_Error with
672 "Position cursor of function Key is bad";
675 pragma Assert (Vet (Position), "bad cursor in function Key");
677 return Position.Node.Key.all;
684 function Length (Container : Map) return Count_Type is
686 return Container.HT.Length;
694 (Target : in out Map;
698 HT_Ops.Move (Target => Target.HT, Source => Source.HT);
705 function Next (Node : Node_Access) return Node_Access is
710 procedure Next (Position : in out Cursor) is
712 Position := Next (Position);
715 function Next (Position : Cursor) return Cursor is
717 if Position.Node = null then
721 if Position.Node.Key = null
722 or else Position.Node.Element = null
724 raise Program_Error with "Position cursor of Next is bad";
727 pragma Assert (Vet (Position), "Position cursor of Next is bad");
730 HT : Hash_Table_Type renames Position.Container.HT;
731 Node : constant Node_Access := HT_Ops.Next (HT, Position.Node);
738 return Cursor'(Position.Container, Node);
742 function Next (Object : Iterator; Position : Cursor) return Cursor is
744 if Position.Node = null then
748 return (Object.Container, Next (Position).Node);
756 procedure Query_Element
758 Process : not null access procedure (Key : Key_Type;
759 Element : Element_Type))
762 if Position.Node = null then
763 raise Constraint_Error with
764 "Position cursor of Query_Element equals No_Element";
767 if Position.Node.Key = null
768 or else Position.Node.Element = null
770 raise Program_Error with
771 "Position cursor of Query_Element is bad";
774 pragma Assert (Vet (Position), "bad cursor in Query_Element");
777 M : Map renames Position.Container.all;
778 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
780 B : Natural renames HT.Busy;
781 L : Natural renames HT.Lock;
788 K : Key_Type renames Position.Node.Key.all;
789 E : Element_Type renames Position.Node.Element.all;
809 procedure Read_Nodes is new HT_Ops.Generic_Read (Read_Node);
812 (Stream : not null access Root_Stream_Type'Class;
816 Read_Nodes (Stream, Container.HT);
820 (Stream : not null access Root_Stream_Type'Class;
824 raise Program_Error with "attempt to stream map cursor";
828 (Stream : not null access Root_Stream_Type'Class;
829 Item : out Reference_Type)
832 raise Program_Error with "attempt to stream reference";
836 (Stream : not null access Root_Stream_Type'Class;
837 Item : out Constant_Reference_Type)
840 raise Program_Error with "attempt to stream reference";
848 (Stream : not null access Root_Stream_Type'Class) return Node_Access
850 Node : Node_Access := new Node_Type;
854 Node.Key := new Key_Type'(Key_Type'Input (Stream));
862 Node.Element := new Element_Type'(Element_Type'Input (Stream));
877 function Constant_Reference (Container : Map; Key : Key_Type)
878 return Constant_Reference_Type is
881 Container.Find (Key).Node.Element.all'Unrestricted_Access);
882 end Constant_Reference;
884 function Reference (Container : Map; Key : Key_Type)
885 return Reference_Type is
888 Container.Find (Key).Node.Element.all'Unrestricted_Access);
896 (Container : in out Map;
898 New_Item : Element_Type)
900 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
907 raise Constraint_Error with
908 "attempt to replace key not in map";
911 if Container.HT.Lock > 0 then
912 raise Program_Error with
913 "Replace attempted to tamper with elements (map is locked)";
919 Node.Key := new Key_Type'(Key);
922 Node.Element := new Element_Type'(New_Item);
933 ---------------------
934 -- Replace_Element --
935 ---------------------
937 procedure Replace_Element
938 (Container : in out Map;
940 New_Item : Element_Type)
943 if Position.Node = null then
944 raise Constraint_Error with
945 "Position cursor of Replace_Element equals No_Element";
948 if Position.Node.Key = null
949 or else Position.Node.Element = null
951 raise Program_Error with
952 "Position cursor of Replace_Element is bad";
955 if Position.Container /= Container'Unrestricted_Access then
956 raise Program_Error with
957 "Position cursor of Replace_Element designates wrong map";
960 if Position.Container.HT.Lock > 0 then
961 raise Program_Error with
962 "Replace_Element attempted to tamper with elements (map is locked)";
965 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
968 X : Element_Access := Position.Node.Element;
971 Position.Node.Element := new Element_Type'(New_Item);
976 ----------------------
977 -- Reserve_Capacity --
978 ----------------------
980 procedure Reserve_Capacity
981 (Container : in out Map;
982 Capacity : Count_Type)
985 HT_Ops.Reserve_Capacity (Container.HT, Capacity);
986 end Reserve_Capacity;
992 procedure Set_Next (Node : Node_Access; Next : Node_Access) is
1001 procedure Update_Element
1002 (Container : in out Map;
1004 Process : not null access procedure (Key : Key_Type;
1005 Element : in out Element_Type))
1008 if Position.Node = null then
1009 raise Constraint_Error with
1010 "Position cursor of Update_Element equals No_Element";
1013 if Position.Node.Key = null
1014 or else Position.Node.Element = null
1016 raise Program_Error with
1017 "Position cursor of Update_Element is bad";
1020 if Position.Container /= Container'Unrestricted_Access then
1021 raise Program_Error with
1022 "Position cursor of Update_Element designates wrong map";
1025 pragma Assert (Vet (Position), "bad cursor in Update_Element");
1028 HT : Hash_Table_Type renames Container.HT;
1030 B : Natural renames HT.Busy;
1031 L : Natural renames HT.Lock;
1038 K : Key_Type renames Position.Node.Key.all;
1039 E : Element_Type renames Position.Node.Element.all;
1060 function Vet (Position : Cursor) return Boolean is
1062 if Position.Node = null then
1063 return Position.Container = null;
1066 if Position.Container = null then
1070 if Position.Node.Next = Position.Node then
1074 if Position.Node.Key = null then
1078 if Position.Node.Element = null then
1083 HT : Hash_Table_Type renames Position.Container.HT;
1087 if HT.Length = 0 then
1091 if HT.Buckets = null
1092 or else HT.Buckets'Length = 0
1097 X := HT.Buckets (Key_Ops.Index (HT, Position.Node.Key.all));
1099 for J in 1 .. HT.Length loop
1100 if X = Position.Node then
1108 if X = X.Next then -- to prevent unnecessary looping
1123 procedure Write_Nodes is new HT_Ops.Generic_Write (Write_Node);
1126 (Stream : not null access Root_Stream_Type'Class;
1130 Write_Nodes (Stream, Container.HT);
1134 (Stream : not null access Root_Stream_Type'Class;
1138 raise Program_Error with "attempt to stream map cursor";
1142 (Stream : not null access Root_Stream_Type'Class;
1143 Item : Reference_Type)
1146 raise Program_Error with "attempt to stream reference";
1150 (Stream : not null access Root_Stream_Type'Class;
1151 Item : Constant_Reference_Type)
1154 raise Program_Error with "attempt to stream reference";
1161 procedure Write_Node
1162 (Stream : not null access Root_Stream_Type'Class;
1166 Key_Type'Output (Stream, Node.Key.all);
1167 Element_Type'Output (Stream, Node.Element.all);
1170 end Ada.Containers.Indefinite_Hashed_Maps;