1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.INDEFINITE_HASHED_MAPS --
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 has originally being 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 --------------------------
90 new Ada.Containers.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);
111 function Is_Equal is new HT_Ops.Generic_Equal (Find_Equal_Key);
113 function "=" (Left, Right : Map) return Boolean is
115 return Is_Equal (Left.HT, Right.HT);
122 procedure Adjust (Container : in out Map) is
124 HT_Ops.Adjust (Container.HT);
131 function Capacity (Container : Map) return Count_Type is
133 return HT_Ops.Capacity (Container.HT);
140 procedure Clear (Container : in out Map) is
142 HT_Ops.Clear (Container.HT);
149 function Contains (Container : Map; Key : Key_Type) return Boolean is
151 return Find (Container, Key) /= No_Element;
158 function Copy_Node (Node : Node_Access) return Node_Access is
159 K : Key_Access := new Key_Type'(Node.Key.all);
163 E := new Element_Type'(Node.Element.all);
164 return new Node_Type'(K, E, null);
177 procedure Delete (Container : in out Map; Key : Key_Type) is
181 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
184 raise Constraint_Error with "attempt to delete key not in map";
190 procedure Delete (Container : in out Map; Position : in out Cursor) is
192 if Position.Node = null then
193 raise Constraint_Error with
194 "Position cursor of Delete equals No_Element";
197 if Position.Container /= Container'Unrestricted_Access then
198 raise Program_Error with
199 "Position cursor of Delete designates wrong map";
202 if Container.HT.Busy > 0 then
203 raise Program_Error with
204 "Delete attempted to tamper with elements (map is busy)";
207 pragma Assert (Vet (Position), "bad cursor in Delete");
209 HT_Ops.Delete_Node_Sans_Free (Container.HT, Position.Node);
211 Free (Position.Node);
212 Position.Container := null;
219 function Element (Container : Map; Key : Key_Type) return Element_Type is
220 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
224 raise Constraint_Error with
225 "no element available because key not in map";
228 return Node.Element.all;
231 function Element (Position : Cursor) return Element_Type is
233 if Position.Node = null then
234 raise Constraint_Error with
235 "Position cursor of function Element equals No_Element";
238 if Position.Node.Element = null then
239 raise Program_Error with
240 "Position cursor of function Element is bad";
243 pragma Assert (Vet (Position), "bad cursor in function Element");
245 return Position.Node.Element.all;
248 -------------------------
249 -- Equivalent_Key_Node --
250 -------------------------
252 function Equivalent_Key_Node
254 Node : Node_Access) return Boolean
257 return Equivalent_Keys (Key, Node.Key.all);
258 end Equivalent_Key_Node;
260 ---------------------
261 -- Equivalent_Keys --
262 ---------------------
264 function Equivalent_Keys (Left, Right : Cursor) return Boolean is
266 if Left.Node = null then
267 raise Constraint_Error with
268 "Left cursor of Equivalent_Keys equals No_Element";
271 if Right.Node = null then
272 raise Constraint_Error with
273 "Right cursor of Equivalent_Keys equals No_Element";
276 if Left.Node.Key = null then
277 raise Program_Error with
278 "Left cursor of Equivalent_Keys is bad";
281 if Right.Node.Key = null then
282 raise Program_Error with
283 "Right cursor of Equivalent_Keys is bad";
286 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Keys");
287 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Keys");
289 return Equivalent_Keys (Left.Node.Key.all, Right.Node.Key.all);
292 function Equivalent_Keys
294 Right : Key_Type) return Boolean
297 if Left.Node = null then
298 raise Constraint_Error with
299 "Left cursor of Equivalent_Keys equals No_Element";
302 if Left.Node.Key = null then
303 raise Program_Error with
304 "Left cursor of Equivalent_Keys is bad";
307 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Keys");
309 return Equivalent_Keys (Left.Node.Key.all, Right);
312 function Equivalent_Keys
314 Right : Cursor) return Boolean
317 if Right.Node = null then
318 raise Constraint_Error with
319 "Right cursor of Equivalent_Keys equals No_Element";
322 if Right.Node.Key = null then
323 raise Program_Error with
324 "Right cursor of Equivalent_Keys is bad";
327 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Keys");
329 return Equivalent_Keys (Left, Right.Node.Key.all);
336 procedure Exclude (Container : in out Map; Key : Key_Type) is
339 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
347 procedure Finalize (Container : in out Map) is
349 HT_Ops.Finalize (Container.HT);
356 function Find (Container : Map; Key : Key_Type) return Cursor is
357 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
364 return Cursor'(Container'Unchecked_Access, Node);
371 function Find_Equal_Key
372 (R_HT : Hash_Table_Type;
373 L_Node : Node_Access) return Boolean
375 R_Index : constant Hash_Type := Key_Ops.Index (R_HT, L_Node.Key.all);
376 R_Node : Node_Access := R_HT.Buckets (R_Index);
379 while R_Node /= null loop
380 if Equivalent_Keys (L_Node.Key.all, R_Node.Key.all) then
381 return L_Node.Element.all = R_Node.Element.all;
384 R_Node := R_Node.Next;
394 function First (Container : Map) return Cursor is
395 Node : constant Node_Access := HT_Ops.First (Container.HT);
402 return Cursor'(Container'Unchecked_Access, Node);
409 procedure Free (X : in out Node_Access) is
410 procedure Deallocate is
411 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
417 X.Next := X; -- detect mischief (in Vet)
426 Free_Element (X.Element);
437 Free_Element (X.Element);
453 function Has_Element (Position : Cursor) return Boolean is
455 pragma Assert (Vet (Position), "bad cursor in Has_Element");
456 return Position.Node /= null;
463 function Hash_Node (Node : Node_Access) return Hash_Type is
465 return Hash (Node.Key.all);
473 (Container : in out Map;
475 New_Item : Element_Type)
484 Insert (Container, Key, New_Item, Position, Inserted);
487 if Container.HT.Lock > 0 then
488 raise Program_Error with
489 "Include attempted to tamper with cursors (map is locked)";
492 K := Position.Node.Key;
493 E := Position.Node.Element;
495 Position.Node.Key := new Key_Type'(Key);
498 Position.Node.Element := new Element_Type'(New_Item);
515 (Container : in out Map;
517 New_Item : Element_Type;
518 Position : out Cursor;
519 Inserted : out Boolean)
521 function New_Node (Next : Node_Access) return Node_Access;
523 procedure Local_Insert is
524 new Key_Ops.Generic_Conditional_Insert (New_Node);
530 function New_Node (Next : Node_Access) return Node_Access is
531 K : Key_Access := new Key_Type'(Key);
535 E := new Element_Type'(New_Item);
536 return new Node_Type'(K, E, Next);
544 HT : Hash_Table_Type renames Container.HT;
546 -- Start of processing for Insert
549 if HT_Ops.Capacity (HT) = 0 then
550 HT_Ops.Reserve_Capacity (HT, 1);
553 Local_Insert (HT, Key, Position.Node, Inserted);
556 and then HT.Length > HT_Ops.Capacity (HT)
558 HT_Ops.Reserve_Capacity (HT, HT.Length);
561 Position.Container := Container'Unchecked_Access;
565 (Container : in out Map;
567 New_Item : Element_Type)
570 pragma Unreferenced (Position);
575 Insert (Container, Key, New_Item, Position, Inserted);
578 raise Constraint_Error with
579 "attempt to insert key already in map";
587 function Is_Empty (Container : Map) return Boolean is
589 return Container.HT.Length = 0;
598 Process : not null access procedure (Position : Cursor))
600 procedure Process_Node (Node : Node_Access);
601 pragma Inline (Process_Node);
603 procedure Local_Iterate is
604 new HT_Ops.Generic_Iteration (Process_Node);
610 procedure Process_Node (Node : Node_Access) is
612 Process (Cursor'(Container'Unchecked_Access, Node));
615 B : Natural renames Container'Unrestricted_Access.HT.Busy;
617 -- Start of processing Iterate
623 Local_Iterate (Container.HT);
637 function Key (Position : Cursor) return Key_Type is
639 if Position.Node = null then
640 raise Constraint_Error with
641 "Position cursor of function Key equals No_Element";
644 if Position.Node.Key = null then
645 raise Program_Error with
646 "Position cursor of function Key is bad";
649 pragma Assert (Vet (Position), "bad cursor in function Key");
651 return Position.Node.Key.all;
658 function Length (Container : Map) return Count_Type is
660 return Container.HT.Length;
668 (Target : in out Map;
672 HT_Ops.Move (Target => Target.HT, Source => Source.HT);
679 function Next (Node : Node_Access) return Node_Access is
684 procedure Next (Position : in out Cursor) is
686 Position := Next (Position);
689 function Next (Position : Cursor) return Cursor is
691 if Position.Node = null then
695 if Position.Node.Key = null
696 or else Position.Node.Element = null
698 raise Program_Error with "Position cursor of Next is bad";
701 pragma Assert (Vet (Position), "Position cursor of Next is bad");
704 HT : Hash_Table_Type renames Position.Container.HT;
705 Node : constant Node_Access := HT_Ops.Next (HT, Position.Node);
712 return Cursor'(Position.Container, Node);
720 procedure Query_Element
722 Process : not null access procedure (Key : Key_Type;
723 Element : Element_Type))
726 if Position.Node = null then
727 raise Constraint_Error with
728 "Position cursor of Query_Element equals No_Element";
731 if Position.Node.Key = null
732 or else Position.Node.Element = null
734 raise Program_Error with
735 "Position cursor of Query_Element is bad";
738 pragma Assert (Vet (Position), "bad cursor in Query_Element");
741 M : Map renames Position.Container.all;
742 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
744 B : Natural renames HT.Busy;
745 L : Natural renames HT.Lock;
752 K : Key_Type renames Position.Node.Key.all;
753 E : Element_Type renames Position.Node.Element.all;
773 procedure Read_Nodes is new HT_Ops.Generic_Read (Read_Node);
776 (Stream : not null access Root_Stream_Type'Class;
780 Read_Nodes (Stream, Container.HT);
784 (Stream : not null access Root_Stream_Type'Class;
788 raise Program_Error with "attempt to stream map cursor";
796 (Stream : not null access Root_Stream_Type'Class) return Node_Access
798 Node : Node_Access := new Node_Type;
802 Node.Key := new Key_Type'(Key_Type'Input (Stream));
810 Node.Element := new Element_Type'(Element_Type'Input (Stream));
826 (Container : in out Map;
828 New_Item : Element_Type)
830 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
837 raise Constraint_Error with
838 "attempt to replace key not in map";
841 if Container.HT.Lock > 0 then
842 raise Program_Error with
843 "Replace attempted to tamper with cursors (map is locked)";
849 Node.Key := new Key_Type'(Key);
852 Node.Element := new Element_Type'(New_Item);
863 ---------------------
864 -- Replace_Element --
865 ---------------------
867 procedure Replace_Element
868 (Container : in out Map;
870 New_Item : Element_Type)
873 if Position.Node = null then
874 raise Constraint_Error with
875 "Position cursor of Replace_Element equals No_Element";
878 if Position.Node.Key = null
879 or else Position.Node.Element = null
881 raise Program_Error with
882 "Position cursor of Replace_Element is bad";
885 if Position.Container /= Container'Unrestricted_Access then
886 raise Program_Error with
887 "Position cursor of Replace_Element designates wrong map";
890 if Position.Container.HT.Lock > 0 then
891 raise Program_Error with
892 "Replace_Element attempted to tamper with cursors (map is locked)";
895 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
898 X : Element_Access := Position.Node.Element;
901 Position.Node.Element := new Element_Type'(New_Item);
906 ----------------------
907 -- Reserve_Capacity --
908 ----------------------
910 procedure Reserve_Capacity
911 (Container : in out Map;
912 Capacity : Count_Type)
915 HT_Ops.Reserve_Capacity (Container.HT, Capacity);
916 end Reserve_Capacity;
922 procedure Set_Next (Node : Node_Access; Next : Node_Access) is
931 procedure Update_Element
932 (Container : in out Map;
934 Process : not null access procedure (Key : Key_Type;
935 Element : in out Element_Type))
938 if Position.Node = null then
939 raise Constraint_Error with
940 "Position cursor of Update_Element equals No_Element";
943 if Position.Node.Key = null
944 or else Position.Node.Element = null
946 raise Program_Error with
947 "Position cursor of Update_Element is bad";
950 if Position.Container /= Container'Unrestricted_Access then
951 raise Program_Error with
952 "Position cursor of Update_Element designates wrong map";
955 pragma Assert (Vet (Position), "bad cursor in Update_Element");
958 HT : Hash_Table_Type renames Container.HT;
960 B : Natural renames HT.Busy;
961 L : Natural renames HT.Lock;
968 K : Key_Type renames Position.Node.Key.all;
969 E : Element_Type renames Position.Node.Element.all;
990 function Vet (Position : Cursor) return Boolean is
992 if Position.Node = null then
993 return Position.Container = null;
996 if Position.Container = null then
1000 if Position.Node.Next = Position.Node then
1004 if Position.Node.Key = null then
1008 if Position.Node.Element = null then
1013 HT : Hash_Table_Type renames Position.Container.HT;
1017 if HT.Length = 0 then
1021 if HT.Buckets = null
1022 or else HT.Buckets'Length = 0
1027 X := HT.Buckets (Key_Ops.Index (HT, Position.Node.Key.all));
1029 for J in 1 .. HT.Length loop
1030 if X = Position.Node then
1038 if X = X.Next then -- to prevent endless loop
1053 procedure Write_Nodes is new HT_Ops.Generic_Write (Write_Node);
1056 (Stream : not null access Root_Stream_Type'Class;
1060 Write_Nodes (Stream, Container.HT);
1064 (Stream : not null access Root_Stream_Type'Class;
1068 raise Program_Error with "attempt to stream map cursor";
1075 procedure Write_Node
1076 (Stream : not null access Root_Stream_Type'Class;
1080 Key_Type'Output (Stream, Node.Key.all);
1081 Element_Type'Output (Stream, Node.Element.all);
1084 end Ada.Containers.Indefinite_Hashed_Maps;