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 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);
143 procedure Assign (Target : in out Map; Source : Map) is
144 procedure Insert_Item (Node : Node_Access);
145 pragma Inline (Insert_Item);
147 procedure Insert_Items is new HT_Ops.Generic_Iteration (Insert_Item);
153 procedure Insert_Item (Node : Node_Access) is
155 Target.Insert (Key => Node.Key.all, New_Item => Node.Element.all);
158 -- Start of processing for Assign
161 if Target'Address = Source'Address then
167 if Target.Capacity < Source.Length then
168 Target.Reserve_Capacity (Source.Length);
171 Insert_Items (Target.HT);
178 function Capacity (Container : Map) return Count_Type is
180 return HT_Ops.Capacity (Container.HT);
187 procedure Clear (Container : in out Map) is
189 HT_Ops.Clear (Container.HT);
192 ------------------------
193 -- Constant_Reference --
194 ------------------------
196 function Constant_Reference
197 (Container : aliased Map;
198 Position : Cursor) return Constant_Reference_Type
201 if Position.Container = null then
202 raise Constraint_Error with
203 "Position cursor has no element";
206 if Position.Container /= Container'Unrestricted_Access then
207 raise Program_Error with
208 "Position cursor designates wrong map";
211 if Position.Node.Element = null then
212 raise Program_Error with
213 "Position cursor has no element";
218 "Position cursor in Constant_Reference is bad");
220 return (Element => Position.Node.Element.all'Access);
221 end Constant_Reference;
223 function Constant_Reference
224 (Container : aliased Map;
225 Key : Key_Type) return Constant_Reference_Type
227 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
231 raise Constraint_Error with "key not in map";
234 if Node.Element = null then
235 raise Program_Error with "key has no element";
238 return (Element => Node.Element.all'Access);
239 end Constant_Reference;
245 function Contains (Container : Map; Key : Key_Type) return Boolean is
247 return Find (Container, Key) /= No_Element;
256 Capacity : Count_Type := 0) return Map
264 elsif Capacity >= Source.Length then
269 with "Requested capacity is less than Source length";
272 return Target : Map do
273 Target.Reserve_Capacity (C);
274 Target.Assign (Source);
282 function Copy_Node (Node : Node_Access) return Node_Access is
283 K : Key_Access := new Key_Type'(Node.Key.all);
287 E := new Element_Type'(Node.Element.all);
288 return new Node_Type'(K, E, null);
301 procedure Delete (Container : in out Map; Key : Key_Type) is
305 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
308 raise Constraint_Error with "attempt to delete key not in map";
314 procedure Delete (Container : in out Map; Position : in out Cursor) is
316 if Position.Node = null then
317 raise Constraint_Error with
318 "Position cursor of Delete equals No_Element";
321 if Position.Container /= Container'Unrestricted_Access then
322 raise Program_Error with
323 "Position cursor of Delete designates wrong map";
326 if Container.HT.Busy > 0 then
327 raise Program_Error with
328 "Delete attempted to tamper with cursors (map is busy)";
331 pragma Assert (Vet (Position), "bad cursor in Delete");
333 HT_Ops.Delete_Node_Sans_Free (Container.HT, Position.Node);
335 Free (Position.Node);
336 Position.Container := null;
343 function Element (Container : Map; Key : Key_Type) return Element_Type is
344 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
348 raise Constraint_Error with
349 "no element available because key not in map";
352 return Node.Element.all;
355 function Element (Position : Cursor) return Element_Type is
357 if Position.Node = null then
358 raise Constraint_Error with
359 "Position cursor of function Element equals No_Element";
362 if Position.Node.Element = null then
363 raise Program_Error with
364 "Position cursor of function Element is bad";
367 pragma Assert (Vet (Position), "bad cursor in function Element");
369 return Position.Node.Element.all;
372 -------------------------
373 -- Equivalent_Key_Node --
374 -------------------------
376 function Equivalent_Key_Node
378 Node : Node_Access) return Boolean
381 return Equivalent_Keys (Key, Node.Key.all);
382 end Equivalent_Key_Node;
384 ---------------------
385 -- Equivalent_Keys --
386 ---------------------
388 function Equivalent_Keys (Left, Right : Cursor) return Boolean is
390 if Left.Node = null then
391 raise Constraint_Error with
392 "Left cursor of Equivalent_Keys equals No_Element";
395 if Right.Node = null then
396 raise Constraint_Error with
397 "Right cursor of Equivalent_Keys equals No_Element";
400 if Left.Node.Key = null then
401 raise Program_Error with
402 "Left cursor of Equivalent_Keys is bad";
405 if Right.Node.Key = null then
406 raise Program_Error with
407 "Right cursor of Equivalent_Keys is bad";
410 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Keys");
411 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Keys");
413 return Equivalent_Keys (Left.Node.Key.all, Right.Node.Key.all);
416 function Equivalent_Keys
418 Right : Key_Type) return Boolean
421 if Left.Node = null then
422 raise Constraint_Error with
423 "Left cursor of Equivalent_Keys equals No_Element";
426 if Left.Node.Key = null then
427 raise Program_Error with
428 "Left cursor of Equivalent_Keys is bad";
431 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Keys");
433 return Equivalent_Keys (Left.Node.Key.all, Right);
436 function Equivalent_Keys
438 Right : Cursor) return Boolean
441 if Right.Node = null then
442 raise Constraint_Error with
443 "Right cursor of Equivalent_Keys equals No_Element";
446 if Right.Node.Key = null then
447 raise Program_Error with
448 "Right cursor of Equivalent_Keys is bad";
451 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Keys");
453 return Equivalent_Keys (Left, Right.Node.Key.all);
460 procedure Exclude (Container : in out Map; Key : Key_Type) is
463 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
471 procedure Finalize (Container : in out Map) is
473 HT_Ops.Finalize (Container.HT);
476 procedure Finalize (Object : in out Iterator) is
478 if Object.Container /= null then
480 B : Natural renames Object.Container.all.HT.Busy;
491 function Find (Container : Map; Key : Key_Type) return Cursor is
492 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
499 return Cursor'(Container'Unrestricted_Access, Node);
506 function Find_Equal_Key
507 (R_HT : Hash_Table_Type;
508 L_Node : Node_Access) return Boolean
510 R_Index : constant Hash_Type := Key_Ops.Index (R_HT, L_Node.Key.all);
511 R_Node : Node_Access := R_HT.Buckets (R_Index);
514 while R_Node /= null loop
515 if Equivalent_Keys (L_Node.Key.all, R_Node.Key.all) then
516 return L_Node.Element.all = R_Node.Element.all;
519 R_Node := R_Node.Next;
529 function First (Container : Map) return Cursor is
530 Node : constant Node_Access := HT_Ops.First (Container.HT);
535 return Cursor'(Container'Unrestricted_Access, Node);
539 function First (Object : Iterator) return Cursor is
541 return Object.Container.First;
548 procedure Free (X : in out Node_Access) is
549 procedure Deallocate is
550 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
557 X.Next := X; -- detect mischief (in Vet)
566 Free_Element (X.Element);
577 Free_Element (X.Element);
593 function Has_Element (Position : Cursor) return Boolean is
595 pragma Assert (Vet (Position), "bad cursor in Has_Element");
596 return Position.Node /= null;
603 function Hash_Node (Node : Node_Access) return Hash_Type is
605 return Hash (Node.Key.all);
613 (Container : in out Map;
615 New_Item : Element_Type)
624 Insert (Container, Key, New_Item, Position, Inserted);
627 if Container.HT.Lock > 0 then
628 raise Program_Error with
629 "Include attempted to tamper with elements (map is locked)";
632 K := Position.Node.Key;
633 E := Position.Node.Element;
635 Position.Node.Key := new Key_Type'(Key);
638 Position.Node.Element := new Element_Type'(New_Item);
655 (Container : in out Map;
657 New_Item : Element_Type;
658 Position : out Cursor;
659 Inserted : out Boolean)
661 function New_Node (Next : Node_Access) return Node_Access;
663 procedure Local_Insert is
664 new Key_Ops.Generic_Conditional_Insert (New_Node);
670 function New_Node (Next : Node_Access) return Node_Access is
671 K : Key_Access := new Key_Type'(Key);
675 E := new Element_Type'(New_Item);
676 return new Node_Type'(K, E, Next);
684 HT : Hash_Table_Type renames Container.HT;
686 -- Start of processing for Insert
689 if HT_Ops.Capacity (HT) = 0 then
690 HT_Ops.Reserve_Capacity (HT, 1);
693 Local_Insert (HT, Key, Position.Node, Inserted);
696 and then HT.Length > HT_Ops.Capacity (HT)
698 HT_Ops.Reserve_Capacity (HT, HT.Length);
701 Position.Container := Container'Unchecked_Access;
705 (Container : in out Map;
707 New_Item : Element_Type)
710 pragma Unreferenced (Position);
715 Insert (Container, Key, New_Item, Position, Inserted);
718 raise Constraint_Error with
719 "attempt to insert key already in map";
727 function Is_Empty (Container : Map) return Boolean is
729 return Container.HT.Length = 0;
738 Process : not null access procedure (Position : Cursor))
740 procedure Process_Node (Node : Node_Access);
741 pragma Inline (Process_Node);
743 procedure Local_Iterate is
744 new HT_Ops.Generic_Iteration (Process_Node);
750 procedure Process_Node (Node : Node_Access) is
752 Process (Cursor'(Container'Unrestricted_Access, Node));
755 B : Natural renames Container'Unrestricted_Access.all.HT.Busy;
757 -- Start of processing Iterate
763 Local_Iterate (Container.HT);
774 (Container : Map) return Map_Iterator_Interfaces.Forward_Iterator'Class
776 B : Natural renames Container'Unrestricted_Access.all.HT.Busy;
778 return It : constant Iterator :=
779 (Limited_Controlled with
780 Container => Container'Unrestricted_Access)
790 function Key (Position : Cursor) return Key_Type is
792 if Position.Node = null then
793 raise Constraint_Error with
794 "Position cursor of function Key equals No_Element";
797 if Position.Node.Key = null then
798 raise Program_Error with
799 "Position cursor of function Key is bad";
802 pragma Assert (Vet (Position), "bad cursor in function Key");
804 return Position.Node.Key.all;
811 function Length (Container : Map) return Count_Type is
813 return Container.HT.Length;
821 (Target : in out Map;
825 HT_Ops.Move (Target => Target.HT, Source => Source.HT);
832 function Next (Node : Node_Access) return Node_Access is
837 procedure Next (Position : in out Cursor) is
839 Position := Next (Position);
842 function Next (Position : Cursor) return Cursor is
844 if Position.Node = null then
848 if Position.Node.Key = null
849 or else Position.Node.Element = null
851 raise Program_Error with "Position cursor of Next is bad";
854 pragma Assert (Vet (Position), "Position cursor of Next is bad");
857 HT : Hash_Table_Type renames Position.Container.HT;
858 Node : constant Node_Access := HT_Ops.Next (HT, Position.Node);
863 return Cursor'(Position.Container, Node);
868 function Next (Object : Iterator; Position : Cursor) return Cursor is
870 if Position.Container = null then
874 if Position.Container /= Object.Container then
875 raise Program_Error with
876 "Position cursor of Next designates wrong map";
879 return Next (Position);
886 procedure Query_Element
888 Process : not null access procedure (Key : Key_Type;
889 Element : Element_Type))
892 if Position.Node = null then
893 raise Constraint_Error with
894 "Position cursor of Query_Element equals No_Element";
897 if Position.Node.Key = null
898 or else Position.Node.Element = null
900 raise Program_Error with
901 "Position cursor of Query_Element is bad";
904 pragma Assert (Vet (Position), "bad cursor in Query_Element");
907 M : Map renames Position.Container.all;
908 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
910 B : Natural renames HT.Busy;
911 L : Natural renames HT.Lock;
918 K : Key_Type renames Position.Node.Key.all;
919 E : Element_Type renames Position.Node.Element.all;
939 procedure Read_Nodes is new HT_Ops.Generic_Read (Read_Node);
942 (Stream : not null access Root_Stream_Type'Class;
946 Read_Nodes (Stream, Container.HT);
950 (Stream : not null access Root_Stream_Type'Class;
954 raise Program_Error with "attempt to stream map cursor";
958 (Stream : not null access Root_Stream_Type'Class;
959 Item : out Reference_Type)
962 raise Program_Error with "attempt to stream reference";
966 (Stream : not null access Root_Stream_Type'Class;
967 Item : out Constant_Reference_Type)
970 raise Program_Error with "attempt to stream reference";
978 (Stream : not null access Root_Stream_Type'Class) return Node_Access
980 Node : Node_Access := new Node_Type;
984 Node.Key := new Key_Type'(Key_Type'Input (Stream));
992 Node.Element := new Element_Type'(Element_Type'Input (Stream));
1008 (Container : aliased in out Map;
1009 Position : Cursor) return Reference_Type
1012 if Position.Container = null then
1013 raise Constraint_Error with
1014 "Position cursor has no element";
1017 if Position.Container /= Container'Unrestricted_Access then
1018 raise Program_Error with
1019 "Position cursor designates wrong map";
1022 if Position.Node.Element = null then
1023 raise Program_Error with
1024 "Position cursor has no element";
1029 "Position cursor in function Reference is bad");
1031 return (Element => Position.Node.Element.all'Access);
1035 (Container : aliased in out Map;
1036 Key : Key_Type) return Reference_Type
1038 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
1042 raise Constraint_Error with "key not in map";
1045 if Node.Element = null then
1046 raise Program_Error with "key has no element";
1049 return (Element => Node.Element.all'Access);
1057 (Container : in out Map;
1059 New_Item : Element_Type)
1061 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
1068 raise Constraint_Error with
1069 "attempt to replace key not in map";
1072 if Container.HT.Lock > 0 then
1073 raise Program_Error with
1074 "Replace attempted to tamper with elements (map is locked)";
1080 Node.Key := new Key_Type'(Key);
1083 Node.Element := new Element_Type'(New_Item);
1094 ---------------------
1095 -- Replace_Element --
1096 ---------------------
1098 procedure Replace_Element
1099 (Container : in out Map;
1101 New_Item : Element_Type)
1104 if Position.Node = null then
1105 raise Constraint_Error with
1106 "Position cursor of Replace_Element equals No_Element";
1109 if Position.Node.Key = null
1110 or else Position.Node.Element = null
1112 raise Program_Error with
1113 "Position cursor of Replace_Element is bad";
1116 if Position.Container /= Container'Unrestricted_Access then
1117 raise Program_Error with
1118 "Position cursor of Replace_Element designates wrong map";
1121 if Position.Container.HT.Lock > 0 then
1122 raise Program_Error with
1123 "Replace_Element attempted to tamper with elements (map is locked)";
1126 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
1129 X : Element_Access := Position.Node.Element;
1132 Position.Node.Element := new Element_Type'(New_Item);
1135 end Replace_Element;
1137 ----------------------
1138 -- Reserve_Capacity --
1139 ----------------------
1141 procedure Reserve_Capacity
1142 (Container : in out Map;
1143 Capacity : Count_Type)
1146 HT_Ops.Reserve_Capacity (Container.HT, Capacity);
1147 end Reserve_Capacity;
1153 procedure Set_Next (Node : Node_Access; Next : Node_Access) is
1158 --------------------
1159 -- Update_Element --
1160 --------------------
1162 procedure Update_Element
1163 (Container : in out Map;
1165 Process : not null access procedure (Key : Key_Type;
1166 Element : in out Element_Type))
1169 if Position.Node = null then
1170 raise Constraint_Error with
1171 "Position cursor of Update_Element equals No_Element";
1174 if Position.Node.Key = null
1175 or else Position.Node.Element = null
1177 raise Program_Error with
1178 "Position cursor of Update_Element is bad";
1181 if Position.Container /= Container'Unrestricted_Access then
1182 raise Program_Error with
1183 "Position cursor of Update_Element designates wrong map";
1186 pragma Assert (Vet (Position), "bad cursor in Update_Element");
1189 HT : Hash_Table_Type renames Container.HT;
1191 B : Natural renames HT.Busy;
1192 L : Natural renames HT.Lock;
1199 K : Key_Type renames Position.Node.Key.all;
1200 E : Element_Type renames Position.Node.Element.all;
1221 function Vet (Position : Cursor) return Boolean is
1223 if Position.Node = null then
1224 return Position.Container = null;
1227 if Position.Container = null then
1231 if Position.Node.Next = Position.Node then
1235 if Position.Node.Key = null then
1239 if Position.Node.Element = null then
1244 HT : Hash_Table_Type renames Position.Container.HT;
1248 if HT.Length = 0 then
1252 if HT.Buckets = null
1253 or else HT.Buckets'Length = 0
1258 X := HT.Buckets (Key_Ops.Index (HT, Position.Node.Key.all));
1260 for J in 1 .. HT.Length loop
1261 if X = Position.Node then
1269 if X = X.Next then -- to prevent unnecessary looping
1284 procedure Write_Nodes is new HT_Ops.Generic_Write (Write_Node);
1287 (Stream : not null access Root_Stream_Type'Class;
1291 Write_Nodes (Stream, Container.HT);
1295 (Stream : not null access Root_Stream_Type'Class;
1299 raise Program_Error with "attempt to stream map cursor";
1303 (Stream : not null access Root_Stream_Type'Class;
1304 Item : Reference_Type)
1307 raise Program_Error with "attempt to stream reference";
1311 (Stream : not null access Root_Stream_Type'Class;
1312 Item : Constant_Reference_Type)
1315 raise Program_Error with "attempt to stream reference";
1322 procedure Write_Node
1323 (Stream : not null access Root_Stream_Type'Class;
1327 Key_Type'Output (Stream, Node.Key.all);
1328 Element_Type'Output (Stream, Node.Element.all);
1331 end Ada.Containers.Indefinite_Hashed_Maps;