1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.INDEFINITE_HASHED_MAPS --
9 -- Copyright (C) 2004-2008, 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.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 with Ada.Unchecked_Deallocation;
40 package body Ada.Containers.Indefinite_Hashed_Maps is
43 new Ada.Unchecked_Deallocation (Key_Type, Key_Access);
45 procedure Free_Element is
46 new Ada.Unchecked_Deallocation (Element_Type, Element_Access);
48 -----------------------
49 -- Local Subprograms --
50 -----------------------
52 function Copy_Node (Node : Node_Access) return Node_Access;
53 pragma Inline (Copy_Node);
55 function Equivalent_Key_Node
57 Node : Node_Access) return Boolean;
58 pragma Inline (Equivalent_Key_Node);
60 function Find_Equal_Key
61 (R_HT : Hash_Table_Type;
62 L_Node : Node_Access) return Boolean;
64 procedure Free (X : in out Node_Access);
65 -- pragma Inline (Free);
67 function Hash_Node (Node : Node_Access) return Hash_Type;
68 pragma Inline (Hash_Node);
70 function Next (Node : Node_Access) return Node_Access;
74 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
76 procedure Set_Next (Node : Node_Access; Next : Node_Access);
77 pragma Inline (Set_Next);
79 function Vet (Position : Cursor) return Boolean;
82 (Stream : not null access Root_Stream_Type'Class;
85 --------------------------
86 -- Local Instantiations --
87 --------------------------
89 package HT_Ops is new Ada.Containers.Hash_Tables.Generic_Operations
90 (HT_Types => HT_Types,
91 Hash_Node => Hash_Node,
94 Copy_Node => Copy_Node,
97 package Key_Ops is new Hash_Tables.Generic_Keys
98 (HT_Types => HT_Types,
100 Set_Next => Set_Next,
101 Key_Type => Key_Type,
103 Equivalent_Keys => Equivalent_Key_Node);
109 function Is_Equal is new HT_Ops.Generic_Equal (Find_Equal_Key);
111 overriding function "=" (Left, Right : Map) return Boolean is
113 return Is_Equal (Left.HT, Right.HT);
120 procedure Adjust (Container : in out Map) is
122 HT_Ops.Adjust (Container.HT);
129 function Capacity (Container : Map) return Count_Type is
131 return HT_Ops.Capacity (Container.HT);
138 procedure Clear (Container : in out Map) is
140 HT_Ops.Clear (Container.HT);
147 function Contains (Container : Map; Key : Key_Type) return Boolean is
149 return Find (Container, Key) /= No_Element;
156 function Copy_Node (Node : Node_Access) return Node_Access is
157 K : Key_Access := new Key_Type'(Node.Key.all);
161 E := new Element_Type'(Node.Element.all);
162 return new Node_Type'(K, E, null);
175 procedure Delete (Container : in out Map; Key : Key_Type) is
179 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
182 raise Constraint_Error with "attempt to delete key not in map";
188 procedure Delete (Container : in out Map; Position : in out Cursor) is
190 if Position.Node = null then
191 raise Constraint_Error with
192 "Position cursor of Delete equals No_Element";
195 if Position.Container /= Container'Unrestricted_Access then
196 raise Program_Error with
197 "Position cursor of Delete designates wrong map";
200 if Container.HT.Busy > 0 then
201 raise Program_Error with
202 "Delete attempted to tamper with elements (map is busy)";
205 pragma Assert (Vet (Position), "bad cursor in Delete");
207 HT_Ops.Delete_Node_Sans_Free (Container.HT, Position.Node);
209 Free (Position.Node);
210 Position.Container := null;
217 function Element (Container : Map; Key : Key_Type) return Element_Type is
218 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
222 raise Constraint_Error with
223 "no element available because key not in map";
226 return Node.Element.all;
229 function Element (Position : Cursor) return Element_Type is
231 if Position.Node = null then
232 raise Constraint_Error with
233 "Position cursor of function Element equals No_Element";
236 if Position.Node.Element = null then
237 raise Program_Error with
238 "Position cursor of function Element is bad";
241 pragma Assert (Vet (Position), "bad cursor in function Element");
243 return Position.Node.Element.all;
246 -------------------------
247 -- Equivalent_Key_Node --
248 -------------------------
250 function Equivalent_Key_Node
252 Node : Node_Access) return Boolean
255 return Equivalent_Keys (Key, Node.Key.all);
256 end Equivalent_Key_Node;
258 ---------------------
259 -- Equivalent_Keys --
260 ---------------------
262 function Equivalent_Keys (Left, Right : Cursor) return Boolean is
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 if Left.Node.Key = null then
275 raise Program_Error with
276 "Left cursor of Equivalent_Keys is bad";
279 if Right.Node.Key = null then
280 raise Program_Error with
281 "Right cursor of Equivalent_Keys is bad";
284 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Keys");
285 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Keys");
287 return Equivalent_Keys (Left.Node.Key.all, Right.Node.Key.all);
290 function Equivalent_Keys
292 Right : Key_Type) return Boolean
295 if Left.Node = null then
296 raise Constraint_Error with
297 "Left cursor of Equivalent_Keys equals No_Element";
300 if Left.Node.Key = null then
301 raise Program_Error with
302 "Left cursor of Equivalent_Keys is bad";
305 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Keys");
307 return Equivalent_Keys (Left.Node.Key.all, Right);
310 function Equivalent_Keys
312 Right : Cursor) return Boolean
315 if Right.Node = null then
316 raise Constraint_Error with
317 "Right cursor of Equivalent_Keys equals No_Element";
320 if Right.Node.Key = null then
321 raise Program_Error with
322 "Right cursor of Equivalent_Keys is bad";
325 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Keys");
327 return Equivalent_Keys (Left, Right.Node.Key.all);
334 procedure Exclude (Container : in out Map; Key : Key_Type) is
337 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
345 procedure Finalize (Container : in out Map) is
347 HT_Ops.Finalize (Container.HT);
354 function Find (Container : Map; Key : Key_Type) return Cursor is
355 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
362 return Cursor'(Container'Unchecked_Access, Node);
369 function Find_Equal_Key
370 (R_HT : Hash_Table_Type;
371 L_Node : Node_Access) return Boolean
373 R_Index : constant Hash_Type := Key_Ops.Index (R_HT, L_Node.Key.all);
374 R_Node : Node_Access := R_HT.Buckets (R_Index);
377 while R_Node /= null loop
378 if Equivalent_Keys (L_Node.Key.all, R_Node.Key.all) then
379 return L_Node.Element.all = R_Node.Element.all;
382 R_Node := R_Node.Next;
392 function First (Container : Map) return Cursor is
393 Node : constant Node_Access := HT_Ops.First (Container.HT);
400 return Cursor'(Container'Unchecked_Access, Node);
407 procedure Free (X : in out Node_Access) is
408 procedure Deallocate is
409 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
415 X.Next := X; -- detect mischief (in Vet)
424 Free_Element (X.Element);
435 Free_Element (X.Element);
451 function Has_Element (Position : Cursor) return Boolean is
453 pragma Assert (Vet (Position), "bad cursor in Has_Element");
454 return Position.Node /= null;
461 function Hash_Node (Node : Node_Access) return Hash_Type is
463 return Hash (Node.Key.all);
471 (Container : in out Map;
473 New_Item : Element_Type)
482 Insert (Container, Key, New_Item, Position, Inserted);
485 if Container.HT.Lock > 0 then
486 raise Program_Error with
487 "Include attempted to tamper with cursors (map is locked)";
490 K := Position.Node.Key;
491 E := Position.Node.Element;
493 Position.Node.Key := new Key_Type'(Key);
496 Position.Node.Element := new Element_Type'(New_Item);
513 (Container : in out Map;
515 New_Item : Element_Type;
516 Position : out Cursor;
517 Inserted : out Boolean)
519 function New_Node (Next : Node_Access) return Node_Access;
521 procedure Local_Insert is
522 new Key_Ops.Generic_Conditional_Insert (New_Node);
528 function New_Node (Next : Node_Access) return Node_Access is
529 K : Key_Access := new Key_Type'(Key);
533 E := new Element_Type'(New_Item);
534 return new Node_Type'(K, E, Next);
542 HT : Hash_Table_Type renames Container.HT;
544 -- Start of processing for Insert
547 if HT_Ops.Capacity (HT) = 0 then
548 HT_Ops.Reserve_Capacity (HT, 1);
551 Local_Insert (HT, Key, Position.Node, Inserted);
554 and then HT.Length > HT_Ops.Capacity (HT)
556 HT_Ops.Reserve_Capacity (HT, HT.Length);
559 Position.Container := Container'Unchecked_Access;
563 (Container : in out Map;
565 New_Item : Element_Type)
568 pragma Unreferenced (Position);
573 Insert (Container, Key, New_Item, Position, Inserted);
576 raise Constraint_Error with
577 "attempt to insert key already in map";
585 function Is_Empty (Container : Map) return Boolean is
587 return Container.HT.Length = 0;
596 Process : not null access procedure (Position : Cursor))
598 procedure Process_Node (Node : Node_Access);
599 pragma Inline (Process_Node);
601 procedure Local_Iterate is
602 new HT_Ops.Generic_Iteration (Process_Node);
608 procedure Process_Node (Node : Node_Access) is
610 Process (Cursor'(Container'Unchecked_Access, Node));
613 B : Natural renames Container'Unrestricted_Access.HT.Busy;
615 -- Start of processing Iterate
621 Local_Iterate (Container.HT);
635 function Key (Position : Cursor) return Key_Type is
637 if Position.Node = null then
638 raise Constraint_Error with
639 "Position cursor of function Key equals No_Element";
642 if Position.Node.Key = null then
643 raise Program_Error with
644 "Position cursor of function Key is bad";
647 pragma Assert (Vet (Position), "bad cursor in function Key");
649 return Position.Node.Key.all;
656 function Length (Container : Map) return Count_Type is
658 return Container.HT.Length;
666 (Target : in out Map;
670 HT_Ops.Move (Target => Target.HT, Source => Source.HT);
677 function Next (Node : Node_Access) return Node_Access is
682 procedure Next (Position : in out Cursor) is
684 Position := Next (Position);
687 function Next (Position : Cursor) return Cursor is
689 if Position.Node = null then
693 if Position.Node.Key = null
694 or else Position.Node.Element = null
696 raise Program_Error with "Position cursor of Next is bad";
699 pragma Assert (Vet (Position), "Position cursor of Next is bad");
702 HT : Hash_Table_Type renames Position.Container.HT;
703 Node : constant Node_Access := HT_Ops.Next (HT, Position.Node);
710 return Cursor'(Position.Container, Node);
718 procedure Query_Element
720 Process : not null access procedure (Key : Key_Type;
721 Element : Element_Type))
724 if Position.Node = null then
725 raise Constraint_Error with
726 "Position cursor of Query_Element equals No_Element";
729 if Position.Node.Key = null
730 or else Position.Node.Element = null
732 raise Program_Error with
733 "Position cursor of Query_Element is bad";
736 pragma Assert (Vet (Position), "bad cursor in Query_Element");
739 M : Map renames Position.Container.all;
740 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
742 B : Natural renames HT.Busy;
743 L : Natural renames HT.Lock;
750 K : Key_Type renames Position.Node.Key.all;
751 E : Element_Type renames Position.Node.Element.all;
771 procedure Read_Nodes is new HT_Ops.Generic_Read (Read_Node);
774 (Stream : not null access Root_Stream_Type'Class;
778 Read_Nodes (Stream, Container.HT);
782 (Stream : not null access Root_Stream_Type'Class;
786 raise Program_Error with "attempt to stream map cursor";
794 (Stream : not null access Root_Stream_Type'Class) return Node_Access
796 Node : Node_Access := new Node_Type;
800 Node.Key := new Key_Type'(Key_Type'Input (Stream));
808 Node.Element := new Element_Type'(Element_Type'Input (Stream));
824 (Container : in out Map;
826 New_Item : Element_Type)
828 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
835 raise Constraint_Error with
836 "attempt to replace key not in map";
839 if Container.HT.Lock > 0 then
840 raise Program_Error with
841 "Replace attempted to tamper with cursors (map is locked)";
847 Node.Key := new Key_Type'(Key);
850 Node.Element := new Element_Type'(New_Item);
861 ---------------------
862 -- Replace_Element --
863 ---------------------
865 procedure Replace_Element
866 (Container : in out Map;
868 New_Item : Element_Type)
871 if Position.Node = null then
872 raise Constraint_Error with
873 "Position cursor of Replace_Element equals No_Element";
876 if Position.Node.Key = null
877 or else Position.Node.Element = null
879 raise Program_Error with
880 "Position cursor of Replace_Element is bad";
883 if Position.Container /= Container'Unrestricted_Access then
884 raise Program_Error with
885 "Position cursor of Replace_Element designates wrong map";
888 if Position.Container.HT.Lock > 0 then
889 raise Program_Error with
890 "Replace_Element attempted to tamper with cursors (map is locked)";
893 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
896 X : Element_Access := Position.Node.Element;
899 Position.Node.Element := new Element_Type'(New_Item);
904 ----------------------
905 -- Reserve_Capacity --
906 ----------------------
908 procedure Reserve_Capacity
909 (Container : in out Map;
910 Capacity : Count_Type)
913 HT_Ops.Reserve_Capacity (Container.HT, Capacity);
914 end Reserve_Capacity;
920 procedure Set_Next (Node : Node_Access; Next : Node_Access) is
929 procedure Update_Element
930 (Container : in out Map;
932 Process : not null access procedure (Key : Key_Type;
933 Element : in out Element_Type))
936 if Position.Node = null then
937 raise Constraint_Error with
938 "Position cursor of Update_Element equals No_Element";
941 if Position.Node.Key = null
942 or else Position.Node.Element = null
944 raise Program_Error with
945 "Position cursor of Update_Element is bad";
948 if Position.Container /= Container'Unrestricted_Access then
949 raise Program_Error with
950 "Position cursor of Update_Element designates wrong map";
953 pragma Assert (Vet (Position), "bad cursor in Update_Element");
956 HT : Hash_Table_Type renames Container.HT;
958 B : Natural renames HT.Busy;
959 L : Natural renames HT.Lock;
966 K : Key_Type renames Position.Node.Key.all;
967 E : Element_Type renames Position.Node.Element.all;
988 function Vet (Position : Cursor) return Boolean is
990 if Position.Node = null then
991 return Position.Container = null;
994 if Position.Container = null then
998 if Position.Node.Next = Position.Node then
1002 if Position.Node.Key = null then
1006 if Position.Node.Element = null then
1011 HT : Hash_Table_Type renames Position.Container.HT;
1015 if HT.Length = 0 then
1019 if HT.Buckets = null
1020 or else HT.Buckets'Length = 0
1025 X := HT.Buckets (Key_Ops.Index (HT, Position.Node.Key.all));
1027 for J in 1 .. HT.Length loop
1028 if X = Position.Node then
1036 if X = X.Next then -- to prevent endless loop
1051 procedure Write_Nodes is new HT_Ops.Generic_Write (Write_Node);
1054 (Stream : not null access Root_Stream_Type'Class;
1058 Write_Nodes (Stream, Container.HT);
1062 (Stream : not null access Root_Stream_Type'Class;
1066 raise Program_Error with "attempt to stream map cursor";
1073 procedure Write_Node
1074 (Stream : not null access Root_Stream_Type'Class;
1078 Key_Type'Output (Stream, Node.Key.all);
1079 Element_Type'Output (Stream, Node.Element.all);
1082 end Ada.Containers.Indefinite_Hashed_Maps;