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-2010, 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 package body Ada.Containers.Hashed_Maps is
41 Map_Iterator_Interfaces.Forward_Iterator with record
42 Container : Map_Access;
46 overriding function First (Object : Iterator) return Cursor;
48 overriding function Next
50 Position : Cursor) return Cursor;
52 -----------------------
53 -- Local Subprograms --
54 -----------------------
57 (Source : Node_Access) return Node_Access;
58 pragma Inline (Copy_Node);
60 function Equivalent_Key_Node
62 Node : Node_Access) return Boolean;
63 pragma Inline (Equivalent_Key_Node);
65 procedure Free (X : in out Node_Access);
67 function Find_Equal_Key
68 (R_HT : Hash_Table_Type;
69 L_Node : Node_Access) return Boolean;
71 function Hash_Node (Node : Node_Access) return Hash_Type;
72 pragma Inline (Hash_Node);
74 function Next (Node : Node_Access) return Node_Access;
78 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
79 pragma Inline (Read_Node);
81 procedure Set_Next (Node : Node_Access; Next : Node_Access);
82 pragma Inline (Set_Next);
84 function Vet (Position : Cursor) return Boolean;
87 (Stream : not null access Root_Stream_Type'Class;
89 pragma Inline (Write_Node);
91 --------------------------
92 -- Local Instantiations --
93 --------------------------
95 package HT_Ops is new Hash_Tables.Generic_Operations
96 (HT_Types => HT_Types,
97 Hash_Node => Hash_Node,
100 Copy_Node => Copy_Node,
103 package Key_Ops is new Hash_Tables.Generic_Keys
104 (HT_Types => HT_Types,
106 Set_Next => Set_Next,
107 Key_Type => Key_Type,
109 Equivalent_Keys => Equivalent_Key_Node);
111 function Is_Equal is new HT_Ops.Generic_Equal (Find_Equal_Key);
113 procedure Read_Nodes is new HT_Ops.Generic_Read (Read_Node);
114 procedure Write_Nodes is new HT_Ops.Generic_Write (Write_Node);
120 function "=" (Left, Right : Map) return Boolean is
122 return Is_Equal (Left.HT, Right.HT);
129 procedure Adjust (Container : in out Map) is
131 HT_Ops.Adjust (Container.HT);
138 function Capacity (Container : Map) return Count_Type is
140 return HT_Ops.Capacity (Container.HT);
147 procedure Clear (Container : in out Map) is
149 HT_Ops.Clear (Container.HT);
156 function Contains (Container : Map; Key : Key_Type) return Boolean is
158 return Find (Container, Key) /= No_Element;
166 (Source : Node_Access) return Node_Access
168 Target : constant Node_Access :=
169 new Node_Type'(Key => Source.Key,
170 Element => Source.Element,
180 procedure Delete (Container : in out Map; Key : Key_Type) is
184 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
187 raise Constraint_Error with "attempt to delete key not in map";
193 procedure Delete (Container : in out Map; Position : in out Cursor) is
195 if Position.Node = null then
196 raise Constraint_Error with
197 "Position cursor of Delete equals No_Element";
200 if Position.Container /= Container'Unrestricted_Access then
201 raise Program_Error with
202 "Position cursor of Delete designates wrong map";
205 if Container.HT.Busy > 0 then
206 raise Program_Error with
207 "Delete attempted to tamper with cursors (map is busy)";
210 pragma Assert (Vet (Position), "bad cursor in Delete");
212 HT_Ops.Delete_Node_Sans_Free (Container.HT, Position.Node);
214 Free (Position.Node);
215 Position.Container := null;
222 function Element (Container : Map; Key : Key_Type) return Element_Type is
223 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
227 raise Constraint_Error with
228 "no element available because key not in map";
234 function Element (Position : Cursor) return Element_Type is
236 if Position.Node = null then
237 raise Constraint_Error with
238 "Position cursor of function Element equals No_Element";
241 pragma Assert (Vet (Position), "bad cursor in function Element");
243 return Position.Node.Element;
246 -------------------------
247 -- Equivalent_Key_Node --
248 -------------------------
250 function Equivalent_Key_Node
252 Node : Node_Access) return Boolean is
254 return Equivalent_Keys (Key, Node.Key);
255 end Equivalent_Key_Node;
257 ---------------------
258 -- Equivalent_Keys --
259 ---------------------
261 function Equivalent_Keys (Left, Right : Cursor)
264 if Left.Node = null then
265 raise Constraint_Error with
266 "Left cursor of Equivalent_Keys equals No_Element";
269 if Right.Node = null then
270 raise Constraint_Error with
271 "Right cursor of Equivalent_Keys equals No_Element";
274 pragma Assert (Vet (Left), "Left cursor of Equivalent_Keys is bad");
275 pragma Assert (Vet (Right), "Right cursor of Equivalent_Keys is bad");
277 return Equivalent_Keys (Left.Node.Key, Right.Node.Key);
280 function Equivalent_Keys (Left : Cursor; Right : Key_Type) return Boolean is
282 if Left.Node = null then
283 raise Constraint_Error with
284 "Left cursor of Equivalent_Keys equals No_Element";
287 pragma Assert (Vet (Left), "Left cursor in Equivalent_Keys is bad");
289 return Equivalent_Keys (Left.Node.Key, Right);
292 function Equivalent_Keys (Left : Key_Type; Right : Cursor) return Boolean is
294 if Right.Node = null then
295 raise Constraint_Error with
296 "Right cursor of Equivalent_Keys equals No_Element";
299 pragma Assert (Vet (Right), "Right cursor of Equivalent_Keys is bad");
301 return Equivalent_Keys (Left, Right.Node.Key);
308 procedure Exclude (Container : in out Map; Key : Key_Type) is
311 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
319 procedure Finalize (Container : in out Map) is
321 HT_Ops.Finalize (Container.HT);
328 function Find (Container : Map; Key : Key_Type) return Cursor is
329 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
336 return Cursor'(Container'Unchecked_Access, Node);
343 function Find_Equal_Key
344 (R_HT : Hash_Table_Type;
345 L_Node : Node_Access) return Boolean
347 R_Index : constant Hash_Type := Key_Ops.Index (R_HT, L_Node.Key);
348 R_Node : Node_Access := R_HT.Buckets (R_Index);
351 while R_Node /= null loop
352 if Equivalent_Keys (L_Node.Key, R_Node.Key) then
353 return L_Node.Element = R_Node.Element;
356 R_Node := R_Node.Next;
366 function First (Container : Map) return Cursor is
367 Node : constant Node_Access := HT_Ops.First (Container.HT);
374 return Cursor'(Container'Unchecked_Access, Node);
377 function First (Object : Iterator) return Cursor is
378 M : constant Map_Access := Object.Container;
379 N : constant Node_Access := HT_Ops.First (M.HT);
385 return Cursor'(Object.Container.all'Unchecked_Access, N);
392 procedure Free (X : in out Node_Access) is
393 procedure Deallocate is
394 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
397 X.Next := X; -- detect mischief (in Vet)
406 function Has_Element (Position : Cursor) return Boolean is
408 pragma Assert (Vet (Position), "bad cursor in Has_Element");
409 return Position.Node /= null;
416 function Hash_Node (Node : Node_Access) return Hash_Type is
418 return Hash (Node.Key);
426 (Container : in out Map;
428 New_Item : Element_Type)
434 Insert (Container, Key, New_Item, Position, Inserted);
437 if Container.HT.Lock > 0 then
438 raise Program_Error with
439 "Include attempted to tamper with elements (map is locked)";
442 Position.Node.Key := Key;
443 Position.Node.Element := New_Item;
452 (Container : in out Map;
454 Position : out Cursor;
455 Inserted : out Boolean)
457 function New_Node (Next : Node_Access) return Node_Access;
458 pragma Inline (New_Node);
460 procedure Local_Insert is
461 new Key_Ops.Generic_Conditional_Insert (New_Node);
467 function New_Node (Next : Node_Access) return Node_Access is
469 return new Node_Type'(Key => Key,
474 HT : Hash_Table_Type renames Container.HT;
476 -- Start of processing for Insert
479 if HT_Ops.Capacity (HT) = 0 then
480 HT_Ops.Reserve_Capacity (HT, 1);
483 Local_Insert (HT, Key, Position.Node, Inserted);
486 and then HT.Length > HT_Ops.Capacity (HT)
488 HT_Ops.Reserve_Capacity (HT, HT.Length);
491 Position.Container := Container'Unchecked_Access;
495 (Container : in out Map;
497 New_Item : Element_Type;
498 Position : out Cursor;
499 Inserted : out Boolean)
501 function New_Node (Next : Node_Access) return Node_Access;
502 pragma Inline (New_Node);
504 procedure Local_Insert is
505 new Key_Ops.Generic_Conditional_Insert (New_Node);
511 function New_Node (Next : Node_Access) return Node_Access is
513 return new Node_Type'(Key, New_Item, Next);
516 HT : Hash_Table_Type renames Container.HT;
518 -- Start of processing for Insert
521 if HT_Ops.Capacity (HT) = 0 then
522 HT_Ops.Reserve_Capacity (HT, 1);
525 Local_Insert (HT, Key, Position.Node, Inserted);
528 and then HT.Length > HT_Ops.Capacity (HT)
530 HT_Ops.Reserve_Capacity (HT, HT.Length);
533 Position.Container := Container'Unchecked_Access;
537 (Container : in out Map;
539 New_Item : Element_Type)
542 pragma Unreferenced (Position);
547 Insert (Container, Key, New_Item, Position, Inserted);
550 raise Constraint_Error with
551 "attempt to insert key already in map";
559 function Is_Empty (Container : Map) return Boolean is
561 return Container.HT.Length = 0;
570 Process : not null access procedure (Position : Cursor))
572 procedure Process_Node (Node : Node_Access);
573 pragma Inline (Process_Node);
575 procedure Local_Iterate is new HT_Ops.Generic_Iteration (Process_Node);
581 procedure Process_Node (Node : Node_Access) is
583 Process (Cursor'(Container'Unchecked_Access, Node));
586 B : Natural renames Container'Unrestricted_Access.HT.Busy;
588 -- Start of processing for Iterate
594 Local_Iterate (Container.HT);
605 (Container : Map) return Map_Iterator_Interfaces.Forward_Iterator'class
607 Node : constant Node_Access := HT_Ops.First (Container.HT);
608 It : constant Iterator := (Container'Unrestricted_Access, Node);
617 function Key (Position : Cursor) return Key_Type is
619 if Position.Node = null then
620 raise Constraint_Error with
621 "Position cursor of function Key equals No_Element";
624 pragma Assert (Vet (Position), "bad cursor in function Key");
626 return Position.Node.Key;
633 function Length (Container : Map) return Count_Type is
635 return Container.HT.Length;
643 (Target : in out Map;
647 HT_Ops.Move (Target => Target.HT, Source => Source.HT);
654 function Next (Node : Node_Access) return Node_Access is
659 function Next (Position : Cursor) return Cursor is
661 if Position.Node = null then
665 pragma Assert (Vet (Position), "bad cursor in function Next");
668 HT : Hash_Table_Type renames Position.Container.HT;
669 Node : constant Node_Access := HT_Ops.Next (HT, Position.Node);
676 return Cursor'(Position.Container, Node);
680 procedure Next (Position : in out Cursor) is
682 Position := Next (Position);
687 Position : Cursor) return Cursor
690 if Position.Node = null then
693 return (Object.Container, Next (Position).Node);
701 procedure Query_Element
703 Process : not null access
704 procedure (Key : Key_Type; Element : Element_Type))
707 if Position.Node = null then
708 raise Constraint_Error with
709 "Position cursor of Query_Element equals No_Element";
712 pragma Assert (Vet (Position), "bad cursor in Query_Element");
715 M : Map renames Position.Container.all;
716 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
718 B : Natural renames HT.Busy;
719 L : Natural renames HT.Lock;
726 K : Key_Type renames Position.Node.Key;
727 E : Element_Type renames Position.Node.Element;
748 (Stream : not null access Root_Stream_Type'Class;
752 Read_Nodes (Stream, Container.HT);
756 (Stream : not null access Root_Stream_Type'Class;
760 raise Program_Error with "attempt to stream map cursor";
764 (Stream : not null access Root_Stream_Type'Class;
765 Item : out Reference_Type)
768 raise Program_Error with "attempt to stream reference";
772 (Stream : not null access Root_Stream_Type'Class;
773 Item : out Constant_Reference_Type)
776 raise Program_Error with "attempt to stream reference";
783 function Constant_Reference (Container : Map; Key : Key_Type)
784 return Constant_Reference_Type is
786 return (Element => Container.Element (Key)'Unrestricted_Access);
787 end Constant_Reference;
789 function Reference (Container : Map; Key : Key_Type)
790 return Reference_Type is
792 return (Element => Container.Element (Key)'Unrestricted_Access);
800 (Stream : not null access Root_Stream_Type'Class) return Node_Access
802 Node : Node_Access := new Node_Type;
805 Key_Type'Read (Stream, Node.Key);
806 Element_Type'Read (Stream, Node.Element);
820 (Container : in out Map;
822 New_Item : Element_Type)
824 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
828 raise Constraint_Error with
829 "attempt to replace key not in map";
832 if Container.HT.Lock > 0 then
833 raise Program_Error with
834 "Replace attempted to tamper with elements (map is locked)";
838 Node.Element := New_Item;
841 ---------------------
842 -- Replace_Element --
843 ---------------------
845 procedure Replace_Element
846 (Container : in out Map;
848 New_Item : Element_Type)
851 if Position.Node = null then
852 raise Constraint_Error with
853 "Position cursor of Replace_Element equals No_Element";
856 if Position.Container /= Container'Unrestricted_Access then
857 raise Program_Error with
858 "Position cursor of Replace_Element designates wrong map";
861 if Position.Container.HT.Lock > 0 then
862 raise Program_Error with
863 "Replace_Element attempted to tamper with elements (map is locked)";
866 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
868 Position.Node.Element := New_Item;
871 ----------------------
872 -- Reserve_Capacity --
873 ----------------------
875 procedure Reserve_Capacity
876 (Container : in out Map;
877 Capacity : Count_Type)
880 HT_Ops.Reserve_Capacity (Container.HT, Capacity);
881 end Reserve_Capacity;
887 procedure Set_Next (Node : Node_Access; Next : Node_Access) is
896 procedure Update_Element
897 (Container : in out Map;
899 Process : not null access procedure (Key : Key_Type;
900 Element : in out Element_Type))
903 if Position.Node = null then
904 raise Constraint_Error with
905 "Position cursor of Update_Element equals No_Element";
908 if Position.Container /= Container'Unrestricted_Access then
909 raise Program_Error with
910 "Position cursor of Update_Element designates wrong map";
913 pragma Assert (Vet (Position), "bad cursor in Update_Element");
916 HT : Hash_Table_Type renames Container.HT;
917 B : Natural renames HT.Busy;
918 L : Natural renames HT.Lock;
925 K : Key_Type renames Position.Node.Key;
926 E : Element_Type renames Position.Node.Element;
947 function Vet (Position : Cursor) return Boolean is
949 if Position.Node = null then
950 return Position.Container = null;
953 if Position.Container = null then
957 if Position.Node.Next = Position.Node then
962 HT : Hash_Table_Type renames Position.Container.HT;
966 if HT.Length = 0 then
971 or else HT.Buckets'Length = 0
976 X := HT.Buckets (Key_Ops.Index (HT, Position.Node.Key));
978 for J in 1 .. HT.Length loop
979 if X = Position.Node then
987 if X = X.Next then -- to prevent unnecessary looping
1003 (Stream : not null access Root_Stream_Type'Class;
1007 Write_Nodes (Stream, Container.HT);
1011 (Stream : not null access Root_Stream_Type'Class;
1015 raise Program_Error with "attempt to stream map cursor";
1019 (Stream : not null access Root_Stream_Type'Class;
1020 Item : Reference_Type)
1023 raise Program_Error with "attempt to stream reference";
1027 (Stream : not null access Root_Stream_Type'Class;
1028 Item : Constant_Reference_Type)
1031 raise Program_Error with "attempt to stream reference";
1038 procedure Write_Node
1039 (Stream : not null access Root_Stream_Type'Class;
1043 Key_Type'Write (Stream, Node.Key);
1044 Element_Type'Write (Stream, Node.Element);
1047 end Ada.Containers.Hashed_Maps;