1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . --
6 -- I N D E F I N I T E _ H A S H E D _ M A P S --
10 -- Copyright (C) 2004-2005 Free Software Foundation, Inc. --
12 -- This specification is derived from the Ada Reference Manual for use with --
13 -- GNAT. The copyright notice above, and the license provisions that follow --
14 -- apply solely to the contents of the part following the private keyword. --
16 -- GNAT is free software; you can redistribute it and/or modify it under --
17 -- terms of the GNU General Public License as published by the Free Soft- --
18 -- ware Foundation; either version 2, or (at your option) any later ver- --
19 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
20 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
21 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
22 -- for more details. You should have received a copy of the GNU General --
23 -- Public License distributed with GNAT; see file COPYING. If not, write --
24 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
25 -- Boston, MA 02110-1301, USA. --
27 -- As a special exception, if other files instantiate generics from this --
28 -- unit, or you link this unit with other files to produce an executable, --
29 -- this unit does not by itself cause the resulting executable to be --
30 -- covered by the GNU General Public License. This exception does not --
31 -- however invalidate any other reasons why the executable file might be --
32 -- covered by the GNU Public License. --
34 -- This unit has originally being developed by Matthew J Heaney. --
35 ------------------------------------------------------------------------------
37 with Ada.Containers.Hash_Tables.Generic_Operations;
38 pragma Elaborate_All (Ada.Containers.Hash_Tables.Generic_Operations);
40 with Ada.Containers.Hash_Tables.Generic_Keys;
41 pragma Elaborate_All (Ada.Containers.Hash_Tables.Generic_Keys);
43 with Ada.Unchecked_Deallocation;
45 package body Ada.Containers.Indefinite_Hashed_Maps is
48 new Ada.Unchecked_Deallocation (Key_Type, Key_Access);
50 procedure Free_Element is
51 new Ada.Unchecked_Deallocation (Element_Type, Element_Access);
53 -----------------------
54 -- Local Subprograms --
55 -----------------------
57 function Copy_Node (Node : 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 function Find_Equal_Key
66 (R_HT : Hash_Table_Type;
67 L_Node : Node_Access) return Boolean;
69 procedure Free (X : in out Node_Access);
70 -- pragma Inline (Free);
72 function Hash_Node (Node : Node_Access) return Hash_Type;
73 pragma Inline (Hash_Node);
75 function Next (Node : Node_Access) return Node_Access;
79 (Stream : access Root_Stream_Type'Class) return Node_Access;
81 procedure Set_Next (Node : Node_Access; Next : Node_Access);
82 pragma Inline (Set_Next);
84 function Vet (Position : Cursor) return Boolean;
87 (Stream : access Root_Stream_Type'Class;
90 --------------------------
91 -- Local Instantiations --
92 --------------------------
95 new Ada.Containers.Hash_Tables.Generic_Operations
96 (HT_Types => HT_Types,
97 Hash_Node => Hash_Node,
100 Copy_Node => Copy_Node,
104 new Hash_Tables.Generic_Keys
105 (HT_Types => HT_Types,
107 Set_Next => Set_Next,
108 Key_Type => Key_Type,
110 Equivalent_Keys => Equivalent_Key_Node);
116 function Is_Equal is new HT_Ops.Generic_Equal (Find_Equal_Key);
118 function "=" (Left, Right : Map) return Boolean is
120 return Is_Equal (Left.HT, Right.HT);
127 procedure Adjust (Container : in out Map) is
129 HT_Ops.Adjust (Container.HT);
136 function Capacity (Container : Map) return Count_Type is
138 return HT_Ops.Capacity (Container.HT);
145 procedure Clear (Container : in out Map) is
147 HT_Ops.Clear (Container.HT);
154 function Contains (Container : Map; Key : Key_Type) return Boolean is
156 return Find (Container, Key) /= No_Element;
163 function Copy_Node (Node : Node_Access) return Node_Access is
164 K : Key_Access := new Key_Type'(Node.Key.all);
168 E := new Element_Type'(Node.Element.all);
169 return new Node_Type'(K, E, null);
182 procedure Delete (Container : in out Map; Key : Key_Type) is
186 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
189 raise Constraint_Error;
195 procedure Delete (Container : in out Map; Position : in out Cursor) is
197 pragma Assert (Vet (Position), "bad cursor in Delete");
199 if Position.Node = null then
200 raise Constraint_Error;
203 if Position.Container /= Container'Unrestricted_Access then
207 if Container.HT.Busy > 0 then
211 HT_Ops.Delete_Node_Sans_Free (Container.HT, Position.Node);
213 Free (Position.Node);
214 Position.Container := null;
221 function Element (Container : Map; Key : Key_Type) return Element_Type is
222 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
226 raise Constraint_Error;
229 return Node.Element.all;
232 function Element (Position : Cursor) return Element_Type is
234 pragma Assert (Vet (Position), "bad cursor in function Element");
236 if Position.Node = null then
237 raise Constraint_Error;
240 return Position.Node.Element.all;
243 -------------------------
244 -- Equivalent_Key_Node --
245 -------------------------
247 function Equivalent_Key_Node
249 Node : Node_Access) return Boolean
252 return Equivalent_Keys (Key, Node.Key.all);
253 end Equivalent_Key_Node;
255 ---------------------
256 -- Equivalent_Keys --
257 ---------------------
259 function Equivalent_Keys (Left, Right : Cursor) return Boolean is
261 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Keys");
262 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Keys");
265 or else Right.Node = null
267 raise Constraint_Error;
270 return Equivalent_Keys (Left.Node.Key.all, Right.Node.Key.all);
273 function Equivalent_Keys
275 Right : Key_Type) return Boolean
278 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Keys");
280 if Left.Node = null then
281 raise Constraint_Error;
284 return Equivalent_Keys (Left.Node.Key.all, Right);
287 function Equivalent_Keys
289 Right : Cursor) return Boolean
292 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Keys");
294 if Right.Node = null then
295 raise Constraint_Error;
298 return Equivalent_Keys (Left, Right.Node.Key.all);
305 procedure Exclude (Container : in out Map; Key : Key_Type) is
308 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
316 procedure Finalize (Container : in out Map) is
318 HT_Ops.Finalize (Container.HT);
325 function Find (Container : Map; Key : Key_Type) return Cursor is
326 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
333 return Cursor'(Container'Unchecked_Access, Node);
340 function Find_Equal_Key
341 (R_HT : Hash_Table_Type;
342 L_Node : Node_Access) return Boolean
344 R_Index : constant Hash_Type := Key_Ops.Index (R_HT, L_Node.Key.all);
345 R_Node : Node_Access := R_HT.Buckets (R_Index);
348 while R_Node /= null loop
349 if Equivalent_Keys (L_Node.Key.all, R_Node.Key.all) then
350 return L_Node.Element.all = R_Node.Element.all;
353 R_Node := R_Node.Next;
363 function First (Container : Map) return Cursor is
364 Node : constant Node_Access := HT_Ops.First (Container.HT);
371 return Cursor'(Container'Unchecked_Access, Node);
378 procedure Free (X : in out Node_Access) is
379 procedure Deallocate is
380 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
386 X.Next := X; -- detect mischief (in Vet)
395 Free_Element (X.Element);
406 Free_Element (X.Element);
422 function Has_Element (Position : Cursor) return Boolean is
424 pragma Assert (Vet (Position), "bad cursor in Has_Element");
425 return Position.Node /= null;
432 function Hash_Node (Node : Node_Access) return Hash_Type is
434 return Hash (Node.Key.all);
442 (Container : in out Map;
444 New_Item : Element_Type)
453 Insert (Container, Key, New_Item, Position, Inserted);
456 if Container.HT.Lock > 0 then
460 K := Position.Node.Key;
461 E := Position.Node.Element;
463 Position.Node.Key := new Key_Type'(Key);
466 Position.Node.Element := new Element_Type'(New_Item);
483 (Container : in out Map;
485 New_Item : Element_Type;
486 Position : out Cursor;
487 Inserted : out Boolean)
489 function New_Node (Next : Node_Access) return Node_Access;
491 procedure Local_Insert is
492 new Key_Ops.Generic_Conditional_Insert (New_Node);
498 function New_Node (Next : Node_Access) return Node_Access is
499 K : Key_Access := new Key_Type'(Key);
503 E := new Element_Type'(New_Item);
504 return new Node_Type'(K, E, Next);
512 HT : Hash_Table_Type renames Container.HT;
514 -- Start of processing for Insert
517 if HT_Ops.Capacity (HT) = 0 then
518 HT_Ops.Reserve_Capacity (HT, 1);
521 Local_Insert (HT, Key, Position.Node, Inserted);
524 and then HT.Length > HT_Ops.Capacity (HT)
526 HT_Ops.Reserve_Capacity (HT, HT.Length);
529 Position.Container := Container'Unchecked_Access;
533 (Container : in out Map;
535 New_Item : Element_Type)
541 Insert (Container, Key, New_Item, Position, Inserted);
544 raise Constraint_Error;
552 function Is_Empty (Container : Map) return Boolean is
554 return Container.HT.Length = 0;
563 Process : not null access procedure (Position : Cursor))
565 procedure Process_Node (Node : Node_Access);
566 pragma Inline (Process_Node);
569 new HT_Ops.Generic_Iteration (Process_Node);
575 procedure Process_Node (Node : Node_Access) is
577 Process (Cursor'(Container'Unchecked_Access, Node));
580 -- Start of processing Iterate
583 Iterate (Container.HT);
590 function Key (Position : Cursor) return Key_Type is
592 pragma Assert (Vet (Position), "bad cursor in function Key");
594 if Position.Node = null then
595 raise Constraint_Error;
598 return Position.Node.Key.all;
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 procedure Next (Position : in out Cursor) is
633 Position := Next (Position);
636 function Next (Position : Cursor) return Cursor is
638 pragma Assert (Vet (Position), "bad cursor in function Next");
640 if Position.Node = null then
645 HT : Hash_Table_Type renames Position.Container.HT;
646 Node : constant Node_Access := HT_Ops.Next (HT, Position.Node);
653 return Cursor'(Position.Container, Node);
661 procedure Query_Element
663 Process : not null access procedure (Key : Key_Type;
664 Element : Element_Type))
667 pragma Assert (Vet (Position), "bad cursor in Query_Element");
669 if Position.Node = null then
670 raise Constraint_Error;
674 M : Map renames Position.Container.all;
675 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
677 B : Natural renames HT.Busy;
678 L : Natural renames HT.Lock;
685 K : Key_Type renames Position.Node.Key.all;
686 E : Element_Type renames Position.Node.Element.all;
706 procedure Read_Nodes is new HT_Ops.Generic_Read (Read_Node);
709 (Stream : access Root_Stream_Type'Class;
713 Read_Nodes (Stream, Container.HT);
721 (Stream : access Root_Stream_Type'Class) return Node_Access
723 Node : Node_Access := new Node_Type;
727 Node.Key := new Key_Type'(Key_Type'Input (Stream));
735 Node.Element := new Element_Type'(Element_Type'Input (Stream));
751 (Container : in out Map;
753 New_Item : Element_Type)
755 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
762 raise Constraint_Error;
765 if Container.HT.Lock > 0 then
772 Node.Key := new Key_Type'(Key);
775 Node.Element := new Element_Type'(New_Item);
786 ---------------------
787 -- Replace_Element --
788 ---------------------
790 procedure Replace_Element (Position : Cursor; By : Element_Type) is
792 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
794 if Position.Node = null then
795 raise Constraint_Error;
798 if Position.Container.HT.Lock > 0 then
803 X : Element_Access := Position.Node.Element;
806 Position.Node.Element := new Element_Type'(By);
811 ----------------------
812 -- Reserve_Capacity --
813 ----------------------
815 procedure Reserve_Capacity
816 (Container : in out Map;
817 Capacity : Count_Type)
820 HT_Ops.Reserve_Capacity (Container.HT, Capacity);
821 end Reserve_Capacity;
827 procedure Set_Next (Node : Node_Access; Next : Node_Access) is
836 procedure Update_Element
838 Process : not null access procedure (Key : Key_Type;
839 Element : in out Element_Type))
842 pragma Assert (Vet (Position), "bad cursor in Update_Element");
844 if Position.Node = null then
845 raise Constraint_Error;
849 M : Map renames Position.Container.all;
850 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
852 B : Natural renames HT.Busy;
853 L : Natural renames HT.Lock;
860 K : Key_Type renames Position.Node.Key.all;
861 E : Element_Type renames Position.Node.Element.all;
881 function Vet (Position : Cursor) return Boolean is
883 if Position.Node = null then
884 return Position.Container = null;
887 if Position.Container = null then
891 if Position.Node.Next = Position.Node then
895 if Position.Node.Key = null then
899 if Position.Node.Element = null then
904 HT : Hash_Table_Type renames Position.Container.HT;
908 if HT.Length = 0 then
913 or else HT.Buckets'Length = 0
918 X := HT.Buckets (Key_Ops.Index (HT, Position.Node.Key.all));
920 for J in 1 .. HT.Length loop
921 if X = Position.Node then
929 if X = X.Next then -- to prevent endless loop
944 procedure Write_Nodes is new HT_Ops.Generic_Write (Write_Node);
947 (Stream : access Root_Stream_Type'Class;
951 Write_Nodes (Stream, Container.HT);
959 (Stream : access Root_Stream_Type'Class;
963 Key_Type'Output (Stream, Node.Key.all);
964 Element_Type'Output (Stream, Node.Element.all);
967 end Ada.Containers.Indefinite_Hashed_Maps;