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-2007, 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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- This unit was originally developed by Matthew J Heaney. --
30 ------------------------------------------------------------------------------
32 with Ada.Unchecked_Deallocation;
34 with Ada.Containers.Hash_Tables.Generic_Operations;
35 pragma Elaborate_All (Ada.Containers.Hash_Tables.Generic_Operations);
37 with Ada.Containers.Hash_Tables.Generic_Keys;
38 pragma Elaborate_All (Ada.Containers.Hash_Tables.Generic_Keys);
40 package body Ada.Containers.Hashed_Maps is
42 -----------------------
43 -- Local Subprograms --
44 -----------------------
47 (Source : Node_Access) return Node_Access;
48 pragma Inline (Copy_Node);
50 function Equivalent_Key_Node
52 Node : Node_Access) return Boolean;
53 pragma Inline (Equivalent_Key_Node);
55 procedure Free (X : in out Node_Access);
57 function Find_Equal_Key
58 (R_HT : Hash_Table_Type;
59 L_Node : Node_Access) return Boolean;
61 function Hash_Node (Node : Node_Access) return Hash_Type;
62 pragma Inline (Hash_Node);
64 function Next (Node : Node_Access) return Node_Access;
68 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
69 pragma Inline (Read_Node);
71 procedure Set_Next (Node : Node_Access; Next : Node_Access);
72 pragma Inline (Set_Next);
74 function Vet (Position : Cursor) return Boolean;
77 (Stream : not null access Root_Stream_Type'Class;
79 pragma Inline (Write_Node);
81 --------------------------
82 -- Local Instantiations --
83 --------------------------
86 new Hash_Tables.Generic_Operations
87 (HT_Types => HT_Types,
88 Hash_Node => Hash_Node,
91 Copy_Node => Copy_Node,
95 new Hash_Tables.Generic_Keys
96 (HT_Types => HT_Types,
101 Equivalent_Keys => Equivalent_Key_Node);
103 function Is_Equal is new HT_Ops.Generic_Equal (Find_Equal_Key);
105 procedure Read_Nodes is new HT_Ops.Generic_Read (Read_Node);
106 procedure Write_Nodes is new HT_Ops.Generic_Write (Write_Node);
112 function "=" (Left, Right : Map) return Boolean is
114 return Is_Equal (Left.HT, Right.HT);
121 procedure Adjust (Container : in out Map) is
123 HT_Ops.Adjust (Container.HT);
130 function Capacity (Container : Map) return Count_Type is
132 return HT_Ops.Capacity (Container.HT);
139 procedure Clear (Container : in out Map) is
141 HT_Ops.Clear (Container.HT);
148 function Contains (Container : Map; Key : Key_Type) return Boolean is
150 return Find (Container, Key) /= No_Element;
158 (Source : Node_Access) return Node_Access
160 Target : constant Node_Access :=
161 new Node_Type'(Key => Source.Key,
162 Element => Source.Element,
172 procedure Delete (Container : in out Map; Key : Key_Type) is
176 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
179 raise Constraint_Error with "attempt to delete key not in map";
185 procedure Delete (Container : in out Map; Position : in out Cursor) is
187 if Position.Node = null then
188 raise Constraint_Error with
189 "Position cursor of Delete equals No_Element";
192 if Position.Container /= Container'Unrestricted_Access then
193 raise Program_Error with
194 "Position cursor of Delete designates wrong map";
197 if Container.HT.Busy > 0 then
198 raise Program_Error with
199 "Delete attempted to tamper with elements (map is busy)";
202 pragma Assert (Vet (Position), "bad cursor in Delete");
204 HT_Ops.Delete_Node_Sans_Free (Container.HT, Position.Node);
206 Free (Position.Node);
207 Position.Container := null;
214 function Element (Container : Map; Key : Key_Type) return Element_Type is
215 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
219 raise Constraint_Error with
220 "no element available because key not in map";
226 function Element (Position : Cursor) return Element_Type is
228 if Position.Node = null then
229 raise Constraint_Error with
230 "Position cursor of function Element equals No_Element";
233 pragma Assert (Vet (Position), "bad cursor in function Element");
235 return Position.Node.Element;
238 -------------------------
239 -- Equivalent_Key_Node --
240 -------------------------
242 function Equivalent_Key_Node
244 Node : Node_Access) return Boolean is
246 return Equivalent_Keys (Key, Node.Key);
247 end Equivalent_Key_Node;
249 ---------------------
250 -- Equivalent_Keys --
251 ---------------------
253 function Equivalent_Keys (Left, Right : Cursor)
256 if Left.Node = null then
257 raise Constraint_Error with
258 "Left cursor of Equivalent_Keys equals No_Element";
261 if Right.Node = null then
262 raise Constraint_Error with
263 "Right cursor of Equivalent_Keys equals No_Element";
266 pragma Assert (Vet (Left), "Left cursor of Equivalent_Keys is bad");
267 pragma Assert (Vet (Right), "Right cursor of Equivalent_Keys is bad");
269 return Equivalent_Keys (Left.Node.Key, Right.Node.Key);
272 function Equivalent_Keys (Left : Cursor; Right : Key_Type) return Boolean is
274 if Left.Node = null then
275 raise Constraint_Error with
276 "Left cursor of Equivalent_Keys equals No_Element";
279 pragma Assert (Vet (Left), "Left cursor in Equivalent_Keys is bad");
281 return Equivalent_Keys (Left.Node.Key, Right);
284 function Equivalent_Keys (Left : Key_Type; Right : Cursor) return Boolean is
286 if Right.Node = null then
287 raise Constraint_Error with
288 "Right cursor of Equivalent_Keys equals No_Element";
291 pragma Assert (Vet (Right), "Right cursor of Equivalent_Keys is bad");
293 return Equivalent_Keys (Left, Right.Node.Key);
300 procedure Exclude (Container : in out Map; Key : Key_Type) is
303 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
311 procedure Finalize (Container : in out Map) is
313 HT_Ops.Finalize (Container.HT);
320 function Find (Container : Map; Key : Key_Type) return Cursor is
321 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
328 return Cursor'(Container'Unchecked_Access, Node);
335 function Find_Equal_Key
336 (R_HT : Hash_Table_Type;
337 L_Node : Node_Access) return Boolean
339 R_Index : constant Hash_Type := Key_Ops.Index (R_HT, L_Node.Key);
340 R_Node : Node_Access := R_HT.Buckets (R_Index);
343 while R_Node /= null loop
344 if Equivalent_Keys (L_Node.Key, R_Node.Key) then
345 return L_Node.Element = R_Node.Element;
348 R_Node := R_Node.Next;
358 function First (Container : Map) return Cursor is
359 Node : constant Node_Access := HT_Ops.First (Container.HT);
366 return Cursor'(Container'Unchecked_Access, Node);
373 procedure Free (X : in out Node_Access) is
374 procedure Deallocate is
375 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
378 X.Next := X; -- detect mischief (in Vet)
387 function Has_Element (Position : Cursor) return Boolean is
389 pragma Assert (Vet (Position), "bad cursor in Has_Element");
390 return Position.Node /= null;
397 function Hash_Node (Node : Node_Access) return Hash_Type is
399 return Hash (Node.Key);
407 (Container : in out Map;
409 New_Item : Element_Type)
415 Insert (Container, Key, New_Item, Position, Inserted);
418 if Container.HT.Lock > 0 then
419 raise Program_Error with
420 "Include attempted to tamper with cursors (map is locked)";
423 Position.Node.Key := Key;
424 Position.Node.Element := New_Item;
433 (Container : in out Map;
435 Position : out Cursor;
436 Inserted : out Boolean)
438 function New_Node (Next : Node_Access) return Node_Access;
439 pragma Inline (New_Node);
441 procedure Local_Insert is
442 new Key_Ops.Generic_Conditional_Insert (New_Node);
448 function New_Node (Next : Node_Access) return Node_Access is
450 return new Node_Type'(Key => Key,
455 HT : Hash_Table_Type renames Container.HT;
457 -- Start of processing for Insert
460 if HT_Ops.Capacity (HT) = 0 then
461 HT_Ops.Reserve_Capacity (HT, 1);
464 Local_Insert (HT, Key, Position.Node, Inserted);
467 and then HT.Length > HT_Ops.Capacity (HT)
469 HT_Ops.Reserve_Capacity (HT, HT.Length);
472 Position.Container := Container'Unchecked_Access;
476 (Container : in out Map;
478 New_Item : Element_Type;
479 Position : out Cursor;
480 Inserted : out Boolean)
482 function New_Node (Next : Node_Access) return Node_Access;
483 pragma Inline (New_Node);
485 procedure Local_Insert is
486 new Key_Ops.Generic_Conditional_Insert (New_Node);
492 function New_Node (Next : Node_Access) return Node_Access is
494 return new Node_Type'(Key, New_Item, Next);
497 HT : Hash_Table_Type renames Container.HT;
499 -- Start of processing for Insert
502 if HT_Ops.Capacity (HT) = 0 then
503 HT_Ops.Reserve_Capacity (HT, 1);
506 Local_Insert (HT, Key, Position.Node, Inserted);
509 and then HT.Length > HT_Ops.Capacity (HT)
511 HT_Ops.Reserve_Capacity (HT, HT.Length);
514 Position.Container := Container'Unchecked_Access;
518 (Container : in out Map;
520 New_Item : Element_Type)
523 pragma Unreferenced (Position);
528 Insert (Container, Key, New_Item, Position, Inserted);
531 raise Constraint_Error with
532 "attempt to insert key already in map";
540 function Is_Empty (Container : Map) return Boolean is
542 return Container.HT.Length = 0;
551 Process : not null access procedure (Position : Cursor))
553 procedure Process_Node (Node : Node_Access);
554 pragma Inline (Process_Node);
556 procedure Local_Iterate is new HT_Ops.Generic_Iteration (Process_Node);
562 procedure Process_Node (Node : Node_Access) is
564 Process (Cursor'(Container'Unchecked_Access, Node));
567 B : Natural renames Container'Unrestricted_Access.HT.Busy;
569 -- Start of processing for Iterate
575 Local_Iterate (Container.HT);
589 function Key (Position : Cursor) return Key_Type is
591 if Position.Node = null then
592 raise Constraint_Error with
593 "Position cursor of function Key equals No_Element";
596 pragma Assert (Vet (Position), "bad cursor in function Key");
598 return Position.Node.Key;
605 function Length (Container : Map) return Count_Type is
607 return Container.HT.Length;
615 (Target : in out Map;
619 HT_Ops.Move (Target => Target.HT, Source => Source.HT);
626 function Next (Node : Node_Access) return Node_Access is
631 function Next (Position : Cursor) return Cursor is
633 if Position.Node = null then
637 pragma Assert (Vet (Position), "bad cursor in function Next");
640 HT : Hash_Table_Type renames Position.Container.HT;
641 Node : constant Node_Access := HT_Ops.Next (HT, Position.Node);
648 return Cursor'(Position.Container, Node);
652 procedure Next (Position : in out Cursor) is
654 Position := Next (Position);
661 procedure Query_Element
663 Process : not null access
664 procedure (Key : Key_Type; Element : Element_Type))
667 if Position.Node = null then
668 raise Constraint_Error with
669 "Position cursor of Query_Element equals No_Element";
672 pragma Assert (Vet (Position), "bad cursor in Query_Element");
675 M : Map renames Position.Container.all;
676 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
678 B : Natural renames HT.Busy;
679 L : Natural renames HT.Lock;
686 K : Key_Type renames Position.Node.Key;
687 E : Element_Type renames Position.Node.Element;
708 (Stream : not null access Root_Stream_Type'Class;
712 Read_Nodes (Stream, Container.HT);
716 (Stream : not null access Root_Stream_Type'Class;
720 raise Program_Error with "attempt to stream map cursor";
728 (Stream : not null access Root_Stream_Type'Class) return Node_Access
730 Node : Node_Access := new Node_Type;
733 Key_Type'Read (Stream, Node.Key);
734 Element_Type'Read (Stream, Node.Element);
748 (Container : in out Map;
750 New_Item : Element_Type)
752 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
756 raise Constraint_Error with
757 "attempt to replace key not in map";
760 if Container.HT.Lock > 0 then
761 raise Program_Error with
762 "Replace attempted to tamper with cursors (map is locked)";
766 Node.Element := New_Item;
769 ---------------------
770 -- Replace_Element --
771 ---------------------
773 procedure Replace_Element
774 (Container : in out Map;
776 New_Item : Element_Type)
779 if Position.Node = null then
780 raise Constraint_Error with
781 "Position cursor of Replace_Element equals No_Element";
784 if Position.Container /= Container'Unrestricted_Access then
785 raise Program_Error with
786 "Position cursor of Replace_Element designates wrong map";
789 if Position.Container.HT.Lock > 0 then
790 raise Program_Error with
791 "Replace_Element attempted to tamper with cursors (map is locked)";
794 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
796 Position.Node.Element := New_Item;
799 ----------------------
800 -- Reserve_Capacity --
801 ----------------------
803 procedure Reserve_Capacity
804 (Container : in out Map;
805 Capacity : Count_Type)
808 HT_Ops.Reserve_Capacity (Container.HT, Capacity);
809 end Reserve_Capacity;
815 procedure Set_Next (Node : Node_Access; Next : Node_Access) is
824 procedure Update_Element
825 (Container : in out Map;
827 Process : not null access procedure (Key : Key_Type;
828 Element : in out Element_Type))
831 if Position.Node = null then
832 raise Constraint_Error with
833 "Position cursor of Update_Element equals No_Element";
836 if Position.Container /= Container'Unrestricted_Access then
837 raise Program_Error with
838 "Position cursor of Update_Element designates wrong map";
841 pragma Assert (Vet (Position), "bad cursor in Update_Element");
844 HT : Hash_Table_Type renames Container.HT;
845 B : Natural renames HT.Busy;
846 L : Natural renames HT.Lock;
853 K : Key_Type renames Position.Node.Key;
854 E : Element_Type renames Position.Node.Element;
875 function Vet (Position : Cursor) return Boolean is
877 if Position.Node = null then
878 return Position.Container = null;
881 if Position.Container = null then
885 if Position.Node.Next = Position.Node then
890 HT : Hash_Table_Type renames Position.Container.HT;
894 if HT.Length = 0 then
899 or else HT.Buckets'Length = 0
904 X := HT.Buckets (Key_Ops.Index (HT, Position.Node.Key));
906 for J in 1 .. HT.Length loop
907 if X = Position.Node then
915 if X = X.Next then -- to prevent endless loop
931 (Stream : not null access Root_Stream_Type'Class;
935 Write_Nodes (Stream, Container.HT);
939 (Stream : not null access Root_Stream_Type'Class;
943 raise Program_Error with "attempt to stream map cursor";
951 (Stream : not null access Root_Stream_Type'Class;
955 Key_Type'Write (Stream, Node.Key);
956 Element_Type'Write (Stream, Node.Element);
959 end Ada.Containers.Hashed_Maps;