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-2005, Free Software Foundation, Inc. --
11 -- This specification is derived from the Ada Reference Manual for use with --
12 -- GNAT. The copyright notice above, and the license provisions that follow --
13 -- apply solely to the contents of the part following the private keyword. --
15 -- GNAT is free software; you can redistribute it and/or modify it under --
16 -- terms of the GNU General Public License as published by the Free Soft- --
17 -- ware Foundation; either version 2, or (at your option) any later ver- --
18 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
19 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
20 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
21 -- for more details. You should have received a copy of the GNU General --
22 -- Public License distributed with GNAT; see file COPYING. If not, write --
23 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
24 -- Boston, MA 02110-1301, USA. --
26 -- As a special exception, if other files instantiate generics from this --
27 -- unit, or you link this unit with other files to produce an executable, --
28 -- this unit does not by itself cause the resulting executable to be --
29 -- covered by the GNU General Public License. This exception does not --
30 -- however invalidate any other reasons why the executable file might be --
31 -- covered by the GNU Public License. --
33 -- This unit was originally developed by Matthew J Heaney. --
34 ------------------------------------------------------------------------------
36 with Ada.Unchecked_Deallocation;
38 with Ada.Containers.Hash_Tables.Generic_Operations;
39 pragma Elaborate_All (Ada.Containers.Hash_Tables.Generic_Operations);
41 with Ada.Containers.Hash_Tables.Generic_Keys;
42 pragma Elaborate_All (Ada.Containers.Hash_Tables.Generic_Keys);
44 package body Ada.Containers.Hashed_Maps is
46 -----------------------
47 -- Local Subprograms --
48 -----------------------
51 (Source : Node_Access) return Node_Access;
52 pragma Inline (Copy_Node);
54 function Equivalent_Key_Node
56 Node : Node_Access) return Boolean;
57 pragma Inline (Equivalent_Key_Node);
59 procedure Free (X : in out Node_Access);
61 function Find_Equal_Key
62 (R_HT : Hash_Table_Type;
63 L_Node : Node_Access) return Boolean;
65 function Hash_Node (Node : Node_Access) return Hash_Type;
66 pragma Inline (Hash_Node);
68 function Next (Node : Node_Access) return Node_Access;
72 (Stream : access Root_Stream_Type'Class) return Node_Access;
73 pragma Inline (Read_Node);
75 procedure Set_Next (Node : Node_Access; Next : Node_Access);
76 pragma Inline (Set_Next);
78 function Vet (Position : Cursor) return Boolean;
81 (Stream : access Root_Stream_Type'Class;
83 pragma Inline (Write_Node);
85 --------------------------
86 -- Local Instantiations --
87 --------------------------
90 new Hash_Tables.Generic_Operations
91 (HT_Types => HT_Types,
92 Hash_Node => Hash_Node,
95 Copy_Node => Copy_Node,
99 new Hash_Tables.Generic_Keys
100 (HT_Types => HT_Types,
102 Set_Next => Set_Next,
103 Key_Type => Key_Type,
105 Equivalent_Keys => Equivalent_Key_Node);
107 function Is_Equal is new HT_Ops.Generic_Equal (Find_Equal_Key);
109 procedure Read_Nodes is new HT_Ops.Generic_Read (Read_Node);
110 procedure Write_Nodes is new HT_Ops.Generic_Write (Write_Node);
116 function "=" (Left, Right : Map) return Boolean is
118 return Is_Equal (Left.HT, Right.HT);
125 procedure Adjust (Container : in out Map) is
127 HT_Ops.Adjust (Container.HT);
134 function Capacity (Container : Map) return Count_Type is
136 return HT_Ops.Capacity (Container.HT);
143 procedure Clear (Container : in out Map) is
145 HT_Ops.Clear (Container.HT);
152 function Contains (Container : Map; Key : Key_Type) return Boolean is
154 return Find (Container, Key) /= No_Element;
162 (Source : Node_Access) return Node_Access
164 Target : constant Node_Access :=
165 new Node_Type'(Key => Source.Key,
166 Element => Source.Element,
176 procedure Delete (Container : in out Map; Key : Key_Type) is
180 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
183 raise Constraint_Error with "attempt to delete key not in map";
189 procedure Delete (Container : in out Map; Position : in out Cursor) is
191 if Position.Node = null then
192 raise Constraint_Error with
193 "Position cursor of Delete equals No_Element";
196 if Position.Container /= Container'Unrestricted_Access then
197 raise Program_Error with
198 "Position cursor of Delete designates wrong map";
201 if Container.HT.Busy > 0 then
202 raise Program_Error with
203 "Delete attempted to tamper with elements (map is busy)";
206 pragma Assert (Vet (Position), "bad cursor in Delete");
208 HT_Ops.Delete_Node_Sans_Free (Container.HT, Position.Node);
210 Free (Position.Node);
211 Position.Container := null;
218 function Element (Container : Map; Key : Key_Type) return Element_Type is
219 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
223 raise Constraint_Error with
224 "no element available because key not in map";
230 function Element (Position : Cursor) return Element_Type is
232 if Position.Node = null then
233 raise Constraint_Error with
234 "Position cursor of function Element equals No_Element";
237 pragma Assert (Vet (Position), "bad cursor in function Element");
239 return Position.Node.Element;
242 -------------------------
243 -- Equivalent_Key_Node --
244 -------------------------
246 function Equivalent_Key_Node
248 Node : Node_Access) return Boolean is
250 return Equivalent_Keys (Key, Node.Key);
251 end Equivalent_Key_Node;
253 ---------------------
254 -- Equivalent_Keys --
255 ---------------------
257 function Equivalent_Keys (Left, Right : Cursor)
260 if Left.Node = null then
261 raise Constraint_Error with
262 "Left cursor of Equivalent_Keys equals No_Element";
265 if Right.Node = null then
266 raise Constraint_Error with
267 "Right cursor of Equivalent_Keys equals No_Element";
270 pragma Assert (Vet (Left), "Left cursor of Equivalent_Keys is bad");
271 pragma Assert (Vet (Right), "Right cursor of Equivalent_Keys is bad");
273 return Equivalent_Keys (Left.Node.Key, Right.Node.Key);
276 function Equivalent_Keys (Left : Cursor; Right : Key_Type) return Boolean is
278 if Left.Node = null then
279 raise Constraint_Error with
280 "Left cursor of Equivalent_Keys equals No_Element";
283 pragma Assert (Vet (Left), "Left cursor in Equivalent_Keys is bad");
285 return Equivalent_Keys (Left.Node.Key, Right);
288 function Equivalent_Keys (Left : Key_Type; Right : Cursor) return Boolean is
290 if Right.Node = null then
291 raise Constraint_Error with
292 "Right cursor of Equivalent_Keys equals No_Element";
295 pragma Assert (Vet (Right), "Right cursor of Equivalent_Keys is bad");
297 return Equivalent_Keys (Left, Right.Node.Key);
304 procedure Exclude (Container : in out Map; Key : Key_Type) is
307 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
315 procedure Finalize (Container : in out Map) is
317 HT_Ops.Finalize (Container.HT);
324 function Find (Container : Map; Key : Key_Type) return Cursor is
325 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
332 return Cursor'(Container'Unchecked_Access, Node);
339 function Find_Equal_Key
340 (R_HT : Hash_Table_Type;
341 L_Node : Node_Access) return Boolean
343 R_Index : constant Hash_Type := Key_Ops.Index (R_HT, L_Node.Key);
344 R_Node : Node_Access := R_HT.Buckets (R_Index);
347 while R_Node /= null loop
348 if Equivalent_Keys (L_Node.Key, R_Node.Key) then
349 return L_Node.Element = R_Node.Element;
352 R_Node := R_Node.Next;
362 function First (Container : Map) return Cursor is
363 Node : constant Node_Access := HT_Ops.First (Container.HT);
370 return Cursor'(Container'Unchecked_Access, Node);
377 procedure Free (X : in out Node_Access) is
378 procedure Deallocate is
379 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
382 X.Next := X; -- detect mischief (in Vet)
391 function Has_Element (Position : Cursor) return Boolean is
393 pragma Assert (Vet (Position), "bad cursor in Has_Element");
394 return Position.Node /= null;
401 function Hash_Node (Node : Node_Access) return Hash_Type is
403 return Hash (Node.Key);
411 (Container : in out Map;
413 New_Item : Element_Type)
419 Insert (Container, Key, New_Item, Position, Inserted);
422 if Container.HT.Lock > 0 then
423 raise Program_Error with
424 "Include attempted to tamper with cursors (map is locked)";
427 Position.Node.Key := Key;
428 Position.Node.Element := New_Item;
437 (Container : in out Map;
439 Position : out Cursor;
440 Inserted : out Boolean)
442 function New_Node (Next : Node_Access) return Node_Access;
443 pragma Inline (New_Node);
445 procedure Local_Insert is
446 new Key_Ops.Generic_Conditional_Insert (New_Node);
452 function New_Node (Next : Node_Access) return Node_Access is
454 return new Node_Type'(Key => Key,
459 HT : Hash_Table_Type renames Container.HT;
461 -- Start of processing for Insert
464 if HT_Ops.Capacity (HT) = 0 then
465 HT_Ops.Reserve_Capacity (HT, 1);
468 Local_Insert (HT, Key, Position.Node, Inserted);
471 and then HT.Length > HT_Ops.Capacity (HT)
473 HT_Ops.Reserve_Capacity (HT, HT.Length);
476 Position.Container := Container'Unchecked_Access;
480 (Container : in out Map;
482 New_Item : Element_Type;
483 Position : out Cursor;
484 Inserted : out Boolean)
486 function New_Node (Next : Node_Access) return Node_Access;
487 pragma Inline (New_Node);
489 procedure Local_Insert is
490 new Key_Ops.Generic_Conditional_Insert (New_Node);
496 function New_Node (Next : Node_Access) return Node_Access is
498 return new Node_Type'(Key, New_Item, Next);
501 HT : Hash_Table_Type renames Container.HT;
503 -- Start of processing for Insert
506 if HT_Ops.Capacity (HT) = 0 then
507 HT_Ops.Reserve_Capacity (HT, 1);
510 Local_Insert (HT, Key, Position.Node, Inserted);
513 and then HT.Length > HT_Ops.Capacity (HT)
515 HT_Ops.Reserve_Capacity (HT, HT.Length);
518 Position.Container := Container'Unchecked_Access;
522 (Container : in out Map;
524 New_Item : Element_Type)
530 Insert (Container, Key, New_Item, Position, Inserted);
533 raise Constraint_Error with
534 "attempt to insert key already in map";
542 function Is_Empty (Container : Map) return Boolean is
544 return Container.HT.Length = 0;
553 Process : not null access procedure (Position : Cursor))
555 procedure Process_Node (Node : Node_Access);
556 pragma Inline (Process_Node);
558 procedure Local_Iterate is new HT_Ops.Generic_Iteration (Process_Node);
564 procedure Process_Node (Node : Node_Access) is
566 Process (Cursor'(Container'Unchecked_Access, Node));
569 -- Start of processing for Iterate
572 Local_Iterate (Container.HT);
579 function Key (Position : Cursor) return Key_Type is
581 if Position.Node = null then
582 raise Constraint_Error with
583 "Position cursor of function Key equals No_Element";
586 pragma Assert (Vet (Position), "bad cursor in function Key");
588 return Position.Node.Key;
595 function Length (Container : Map) return Count_Type is
597 return Container.HT.Length;
605 (Target : in out Map;
609 HT_Ops.Move (Target => Target.HT, Source => Source.HT);
616 function Next (Node : Node_Access) return Node_Access is
621 function Next (Position : Cursor) return Cursor is
623 if Position.Node = null then
627 pragma Assert (Vet (Position), "bad cursor in function Next");
630 HT : Hash_Table_Type renames Position.Container.HT;
631 Node : constant Node_Access := HT_Ops.Next (HT, Position.Node);
638 return Cursor'(Position.Container, Node);
642 procedure Next (Position : in out Cursor) is
644 Position := Next (Position);
651 procedure Query_Element
653 Process : not null access
654 procedure (Key : Key_Type; Element : Element_Type))
657 if Position.Node = null then
658 raise Constraint_Error with
659 "Position cursor of Query_Element equals No_Element";
662 pragma Assert (Vet (Position), "bad cursor in Query_Element");
665 M : Map renames Position.Container.all;
666 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
668 B : Natural renames HT.Busy;
669 L : Natural renames HT.Lock;
676 K : Key_Type renames Position.Node.Key;
677 E : Element_Type renames Position.Node.Element;
698 (Stream : access Root_Stream_Type'Class;
702 Read_Nodes (Stream, Container.HT);
706 (Stream : access Root_Stream_Type'Class;
710 raise Program_Error with "attempt to stream map cursor";
718 (Stream : access Root_Stream_Type'Class) return Node_Access
720 Node : Node_Access := new Node_Type;
723 Key_Type'Read (Stream, Node.Key);
724 Element_Type'Read (Stream, Node.Element);
738 (Container : in out Map;
740 New_Item : Element_Type)
742 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
746 raise Constraint_Error with
747 "attempt to replace key not in map";
750 if Container.HT.Lock > 0 then
751 raise Program_Error with
752 "Replace attempted to tamper with cursors (map is locked)";
756 Node.Element := New_Item;
759 ---------------------
760 -- Replace_Element --
761 ---------------------
763 procedure Replace_Element
764 (Container : in out Map;
766 New_Item : Element_Type)
769 if Position.Node = null then
770 raise Constraint_Error with
771 "Position cursor of Replace_Element equals No_Element";
774 if Position.Container /= Container'Unrestricted_Access then
775 raise Program_Error with
776 "Position cursor of Replace_Element designates wrong map";
779 if Position.Container.HT.Lock > 0 then
780 raise Program_Error with
781 "Replace_Element attempted to tamper with cursors (map is locked)";
784 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
786 Position.Node.Element := New_Item;
789 ----------------------
790 -- Reserve_Capacity --
791 ----------------------
793 procedure Reserve_Capacity
794 (Container : in out Map;
795 Capacity : Count_Type)
798 HT_Ops.Reserve_Capacity (Container.HT, Capacity);
799 end Reserve_Capacity;
805 procedure Set_Next (Node : Node_Access; Next : Node_Access) is
814 procedure Update_Element
815 (Container : in out Map;
817 Process : not null access procedure (Key : Key_Type;
818 Element : in out Element_Type))
821 if Position.Node = null then
822 raise Constraint_Error with
823 "Position cursor of Update_Element equals No_Element";
826 if Position.Container /= Container'Unrestricted_Access then
827 raise Program_Error with
828 "Position cursor of Update_Element designates wrong map";
831 pragma Assert (Vet (Position), "bad cursor in Update_Element");
834 HT : Hash_Table_Type renames Container.HT;
835 B : Natural renames HT.Busy;
836 L : Natural renames HT.Lock;
843 K : Key_Type renames Position.Node.Key;
844 E : Element_Type renames Position.Node.Element;
863 function Vet (Position : Cursor) return Boolean is
865 if Position.Node = null then
866 return Position.Container = null;
869 if Position.Container = null then
873 if Position.Node.Next = Position.Node then
878 HT : Hash_Table_Type renames Position.Container.HT;
882 if HT.Length = 0 then
887 or else HT.Buckets'Length = 0
892 X := HT.Buckets (Key_Ops.Index (HT, Position.Node.Key));
894 for J in 1 .. HT.Length loop
895 if X = Position.Node then
903 if X = X.Next then -- to prevent endless loop
919 (Stream : access Root_Stream_Type'Class;
923 Write_Nodes (Stream, Container.HT);
927 (Stream : access Root_Stream_Type'Class;
931 raise Program_Error with "attempt to stream map cursor";
939 (Stream : access Root_Stream_Type'Class;
943 Key_Type'Write (Stream, Node.Key);
944 Element_Type'Write (Stream, Node.Element);
947 end Ada.Containers.Hashed_Maps;