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;
54 overriding function Next
56 Position : Cursor) return Cursor;
58 -----------------------
59 -- Local Subprograms --
60 -----------------------
62 function Copy_Node (Node : Node_Access) return Node_Access;
63 pragma Inline (Copy_Node);
65 function Equivalent_Key_Node
67 Node : Node_Access) return Boolean;
68 pragma Inline (Equivalent_Key_Node);
70 function Find_Equal_Key
71 (R_HT : Hash_Table_Type;
72 L_Node : Node_Access) return Boolean;
74 procedure Free (X : in out Node_Access);
75 -- pragma Inline (Free);
77 function Hash_Node (Node : Node_Access) return Hash_Type;
78 pragma Inline (Hash_Node);
80 function Next (Node : Node_Access) return Node_Access;
84 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
86 procedure Set_Next (Node : Node_Access; Next : Node_Access);
87 pragma Inline (Set_Next);
89 function Vet (Position : Cursor) return Boolean;
92 (Stream : not null access Root_Stream_Type'Class;
95 --------------------------
96 -- Local Instantiations --
97 --------------------------
99 package HT_Ops is new Ada.Containers.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);
119 function Is_Equal is new HT_Ops.Generic_Equal (Find_Equal_Key);
121 overriding function "=" (Left, Right : Map) return Boolean is
123 return Is_Equal (Left.HT, Right.HT);
130 procedure Adjust (Container : in out Map) is
132 HT_Ops.Adjust (Container.HT);
139 function Capacity (Container : Map) return Count_Type is
141 return HT_Ops.Capacity (Container.HT);
148 procedure Clear (Container : in out Map) is
150 HT_Ops.Clear (Container.HT);
157 function Contains (Container : Map; Key : Key_Type) return Boolean is
159 return Find (Container, Key) /= No_Element;
166 function Copy_Node (Node : Node_Access) return Node_Access is
167 K : Key_Access := new Key_Type'(Node.Key.all);
171 E := new Element_Type'(Node.Element.all);
172 return new Node_Type'(K, E, null);
185 procedure Delete (Container : in out Map; Key : Key_Type) is
189 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
192 raise Constraint_Error with "attempt to delete key not in map";
198 procedure Delete (Container : in out Map; Position : in out Cursor) is
200 if Position.Node = null then
201 raise Constraint_Error with
202 "Position cursor of Delete equals No_Element";
205 if Position.Container /= Container'Unrestricted_Access then
206 raise Program_Error with
207 "Position cursor of Delete designates wrong map";
210 if Container.HT.Busy > 0 then
211 raise Program_Error with
212 "Delete attempted to tamper with cursors (map is busy)";
215 pragma Assert (Vet (Position), "bad cursor in Delete");
217 HT_Ops.Delete_Node_Sans_Free (Container.HT, Position.Node);
219 Free (Position.Node);
220 Position.Container := null;
227 function Element (Container : Map; Key : Key_Type) return Element_Type is
228 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
232 raise Constraint_Error with
233 "no element available because key not in map";
236 return Node.Element.all;
239 function Element (Position : Cursor) return Element_Type is
241 if Position.Node = null then
242 raise Constraint_Error with
243 "Position cursor of function Element equals No_Element";
246 if Position.Node.Element = null then
247 raise Program_Error with
248 "Position cursor of function Element is bad";
251 pragma Assert (Vet (Position), "bad cursor in function Element");
253 return Position.Node.Element.all;
256 -------------------------
257 -- Equivalent_Key_Node --
258 -------------------------
260 function Equivalent_Key_Node
262 Node : Node_Access) return Boolean
265 return Equivalent_Keys (Key, Node.Key.all);
266 end Equivalent_Key_Node;
268 ---------------------
269 -- Equivalent_Keys --
270 ---------------------
272 function Equivalent_Keys (Left, Right : Cursor) return Boolean is
274 if Left.Node = null then
275 raise Constraint_Error with
276 "Left cursor of Equivalent_Keys equals No_Element";
279 if Right.Node = null then
280 raise Constraint_Error with
281 "Right cursor of Equivalent_Keys equals No_Element";
284 if Left.Node.Key = null then
285 raise Program_Error with
286 "Left cursor of Equivalent_Keys is bad";
289 if Right.Node.Key = null then
290 raise Program_Error with
291 "Right cursor of Equivalent_Keys is bad";
294 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Keys");
295 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Keys");
297 return Equivalent_Keys (Left.Node.Key.all, Right.Node.Key.all);
300 function Equivalent_Keys
302 Right : Key_Type) return Boolean
305 if Left.Node = null then
306 raise Constraint_Error with
307 "Left cursor of Equivalent_Keys equals No_Element";
310 if Left.Node.Key = null then
311 raise Program_Error with
312 "Left cursor of Equivalent_Keys is bad";
315 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Keys");
317 return Equivalent_Keys (Left.Node.Key.all, Right);
320 function Equivalent_Keys
322 Right : Cursor) return Boolean
325 if Right.Node = null then
326 raise Constraint_Error with
327 "Right cursor of Equivalent_Keys equals No_Element";
330 if Right.Node.Key = null then
331 raise Program_Error with
332 "Right cursor of Equivalent_Keys is bad";
335 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Keys");
337 return Equivalent_Keys (Left, Right.Node.Key.all);
344 procedure Exclude (Container : in out Map; Key : Key_Type) is
347 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
355 procedure Finalize (Container : in out Map) is
357 HT_Ops.Finalize (Container.HT);
364 function Find (Container : Map; Key : Key_Type) return Cursor is
365 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
372 return Cursor'(Container'Unchecked_Access, Node);
379 function Find_Equal_Key
380 (R_HT : Hash_Table_Type;
381 L_Node : Node_Access) return Boolean
383 R_Index : constant Hash_Type := Key_Ops.Index (R_HT, L_Node.Key.all);
384 R_Node : Node_Access := R_HT.Buckets (R_Index);
387 while R_Node /= null loop
388 if Equivalent_Keys (L_Node.Key.all, R_Node.Key.all) then
389 return L_Node.Element.all = R_Node.Element.all;
392 R_Node := R_Node.Next;
402 function First (Container : Map) return Cursor is
403 Node : constant Node_Access := HT_Ops.First (Container.HT);
410 return Cursor'(Container'Unchecked_Access, Node);
413 function First (Object : Iterator) return Cursor is
414 M : constant Map_Access := Object.Container;
415 N : constant Node_Access := HT_Ops.First (M.HT);
420 return Cursor'(Object.Container.all'Unchecked_Access, N);
428 procedure Free (X : in out Node_Access) is
429 procedure Deallocate is
430 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
437 X.Next := X; -- detect mischief (in Vet)
446 Free_Element (X.Element);
457 Free_Element (X.Element);
473 function Has_Element (Position : Cursor) return Boolean is
475 pragma Assert (Vet (Position), "bad cursor in Has_Element");
476 return Position.Node /= null;
483 function Hash_Node (Node : Node_Access) return Hash_Type is
485 return Hash (Node.Key.all);
493 (Container : in out Map;
495 New_Item : Element_Type)
504 Insert (Container, Key, New_Item, Position, Inserted);
507 if Container.HT.Lock > 0 then
508 raise Program_Error with
509 "Include attempted to tamper with elements (map is locked)";
512 K := Position.Node.Key;
513 E := Position.Node.Element;
515 Position.Node.Key := new Key_Type'(Key);
518 Position.Node.Element := new Element_Type'(New_Item);
535 (Container : in out Map;
537 New_Item : Element_Type;
538 Position : out Cursor;
539 Inserted : out Boolean)
541 function New_Node (Next : Node_Access) return Node_Access;
543 procedure Local_Insert is
544 new Key_Ops.Generic_Conditional_Insert (New_Node);
550 function New_Node (Next : Node_Access) return Node_Access is
551 K : Key_Access := new Key_Type'(Key);
555 E := new Element_Type'(New_Item);
556 return new Node_Type'(K, E, Next);
564 HT : Hash_Table_Type renames Container.HT;
566 -- Start of processing for Insert
569 if HT_Ops.Capacity (HT) = 0 then
570 HT_Ops.Reserve_Capacity (HT, 1);
573 Local_Insert (HT, Key, Position.Node, Inserted);
576 and then HT.Length > HT_Ops.Capacity (HT)
578 HT_Ops.Reserve_Capacity (HT, HT.Length);
581 Position.Container := Container'Unchecked_Access;
585 (Container : in out Map;
587 New_Item : Element_Type)
590 pragma Unreferenced (Position);
595 Insert (Container, Key, New_Item, Position, Inserted);
598 raise Constraint_Error with
599 "attempt to insert key already in map";
607 function Is_Empty (Container : Map) return Boolean is
609 return Container.HT.Length = 0;
618 Process : not null access procedure (Position : Cursor))
620 procedure Process_Node (Node : Node_Access);
621 pragma Inline (Process_Node);
623 procedure Local_Iterate is
624 new HT_Ops.Generic_Iteration (Process_Node);
630 procedure Process_Node (Node : Node_Access) is
632 Process (Cursor'(Container'Unchecked_Access, Node));
635 B : Natural renames Container'Unrestricted_Access.HT.Busy;
637 -- Start of processing Iterate
643 Local_Iterate (Container.HT);
653 function Iterate (Container : Map)
654 return Map_Iterator_Interfaces.Forward_Iterator'class
656 Node : constant Node_Access := HT_Ops.First (Container.HT);
657 It : constant Iterator := (Container'Unrestricted_Access, Node);
666 function Key (Position : Cursor) return Key_Type is
668 if Position.Node = null then
669 raise Constraint_Error with
670 "Position cursor of function Key equals No_Element";
673 if Position.Node.Key = null then
674 raise Program_Error with
675 "Position cursor of function Key is bad";
678 pragma Assert (Vet (Position), "bad cursor in function Key");
680 return Position.Node.Key.all;
687 function Length (Container : Map) return Count_Type is
689 return Container.HT.Length;
697 (Target : in out Map;
701 HT_Ops.Move (Target => Target.HT, Source => Source.HT);
708 function Next (Node : Node_Access) return Node_Access is
713 procedure Next (Position : in out Cursor) is
715 Position := Next (Position);
718 function Next (Position : Cursor) return Cursor is
720 if Position.Node = null then
724 if Position.Node.Key = null
725 or else Position.Node.Element = null
727 raise Program_Error with "Position cursor of Next is bad";
730 pragma Assert (Vet (Position), "Position cursor of Next is bad");
733 HT : Hash_Table_Type renames Position.Container.HT;
734 Node : constant Node_Access := HT_Ops.Next (HT, Position.Node);
741 return Cursor'(Position.Container, Node);
745 function Next (Object : Iterator; Position : Cursor) return Cursor is
747 if Position.Node = null then
750 return (Object.Container, Next (Position).Node);
758 procedure Query_Element
760 Process : not null access procedure (Key : Key_Type;
761 Element : Element_Type))
764 if Position.Node = null then
765 raise Constraint_Error with
766 "Position cursor of Query_Element equals No_Element";
769 if Position.Node.Key = null
770 or else Position.Node.Element = null
772 raise Program_Error with
773 "Position cursor of Query_Element is bad";
776 pragma Assert (Vet (Position), "bad cursor in Query_Element");
779 M : Map renames Position.Container.all;
780 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
782 B : Natural renames HT.Busy;
783 L : Natural renames HT.Lock;
790 K : Key_Type renames Position.Node.Key.all;
791 E : Element_Type renames Position.Node.Element.all;
811 procedure Read_Nodes is new HT_Ops.Generic_Read (Read_Node);
814 (Stream : not null access Root_Stream_Type'Class;
818 Read_Nodes (Stream, Container.HT);
822 (Stream : not null access Root_Stream_Type'Class;
826 raise Program_Error with "attempt to stream map cursor";
830 (Stream : not null access Root_Stream_Type'Class;
831 Item : out Reference_Type)
834 raise Program_Error with "attempt to stream reference";
838 (Stream : not null access Root_Stream_Type'Class;
839 Item : out Constant_Reference_Type)
842 raise Program_Error with "attempt to stream reference";
850 (Stream : not null access Root_Stream_Type'Class) return Node_Access
852 Node : Node_Access := new Node_Type;
856 Node.Key := new Key_Type'(Key_Type'Input (Stream));
864 Node.Element := new Element_Type'(Element_Type'Input (Stream));
879 function Constant_Reference
881 Key : Key_Type) return Constant_Reference_Type
885 Container.Find (Key).Node.Element.all'Unrestricted_Access);
886 end Constant_Reference;
890 Key : Key_Type) return Reference_Type
894 Container.Find (Key).Node.Element.all'Unrestricted_Access);
902 (Container : in out Map;
904 New_Item : Element_Type)
906 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
913 raise Constraint_Error with
914 "attempt to replace key not in map";
917 if Container.HT.Lock > 0 then
918 raise Program_Error with
919 "Replace attempted to tamper with elements (map is locked)";
925 Node.Key := new Key_Type'(Key);
928 Node.Element := new Element_Type'(New_Item);
939 ---------------------
940 -- Replace_Element --
941 ---------------------
943 procedure Replace_Element
944 (Container : in out Map;
946 New_Item : Element_Type)
949 if Position.Node = null then
950 raise Constraint_Error with
951 "Position cursor of Replace_Element equals No_Element";
954 if Position.Node.Key = null
955 or else Position.Node.Element = null
957 raise Program_Error with
958 "Position cursor of Replace_Element is bad";
961 if Position.Container /= Container'Unrestricted_Access then
962 raise Program_Error with
963 "Position cursor of Replace_Element designates wrong map";
966 if Position.Container.HT.Lock > 0 then
967 raise Program_Error with
968 "Replace_Element attempted to tamper with elements (map is locked)";
971 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
974 X : Element_Access := Position.Node.Element;
977 Position.Node.Element := new Element_Type'(New_Item);
982 ----------------------
983 -- Reserve_Capacity --
984 ----------------------
986 procedure Reserve_Capacity
987 (Container : in out Map;
988 Capacity : Count_Type)
991 HT_Ops.Reserve_Capacity (Container.HT, Capacity);
992 end Reserve_Capacity;
998 procedure Set_Next (Node : Node_Access; Next : Node_Access) is
1003 --------------------
1004 -- Update_Element --
1005 --------------------
1007 procedure Update_Element
1008 (Container : in out Map;
1010 Process : not null access procedure (Key : Key_Type;
1011 Element : in out Element_Type))
1014 if Position.Node = null then
1015 raise Constraint_Error with
1016 "Position cursor of Update_Element equals No_Element";
1019 if Position.Node.Key = null
1020 or else Position.Node.Element = null
1022 raise Program_Error with
1023 "Position cursor of Update_Element is bad";
1026 if Position.Container /= Container'Unrestricted_Access then
1027 raise Program_Error with
1028 "Position cursor of Update_Element designates wrong map";
1031 pragma Assert (Vet (Position), "bad cursor in Update_Element");
1034 HT : Hash_Table_Type renames Container.HT;
1036 B : Natural renames HT.Busy;
1037 L : Natural renames HT.Lock;
1044 K : Key_Type renames Position.Node.Key.all;
1045 E : Element_Type renames Position.Node.Element.all;
1066 function Vet (Position : Cursor) return Boolean is
1068 if Position.Node = null then
1069 return Position.Container = null;
1072 if Position.Container = null then
1076 if Position.Node.Next = Position.Node then
1080 if Position.Node.Key = null then
1084 if Position.Node.Element = null then
1089 HT : Hash_Table_Type renames Position.Container.HT;
1093 if HT.Length = 0 then
1097 if HT.Buckets = null
1098 or else HT.Buckets'Length = 0
1103 X := HT.Buckets (Key_Ops.Index (HT, Position.Node.Key.all));
1105 for J in 1 .. HT.Length loop
1106 if X = Position.Node then
1114 if X = X.Next then -- to prevent unnecessary looping
1129 procedure Write_Nodes is new HT_Ops.Generic_Write (Write_Node);
1132 (Stream : not null access Root_Stream_Type'Class;
1136 Write_Nodes (Stream, Container.HT);
1140 (Stream : not null access Root_Stream_Type'Class;
1144 raise Program_Error with "attempt to stream map cursor";
1148 (Stream : not null access Root_Stream_Type'Class;
1149 Item : Reference_Type)
1152 raise Program_Error with "attempt to stream reference";
1156 (Stream : not null access Root_Stream_Type'Class;
1157 Item : Constant_Reference_Type)
1160 raise Program_Error with "attempt to stream reference";
1167 procedure Write_Node
1168 (Stream : not null access Root_Stream_Type'Class;
1172 Key_Type'Output (Stream, Node.Key.all);
1173 Element_Type'Output (Stream, Node.Element.all);
1176 end Ada.Containers.Indefinite_Hashed_Maps;