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 if Position.Node.Element = null then
244 return Position.Node.Element.all;
247 -------------------------
248 -- Equivalent_Key_Node --
249 -------------------------
251 function Equivalent_Key_Node
253 Node : Node_Access) return Boolean
256 return Equivalent_Keys (Key, Node.Key.all);
257 end Equivalent_Key_Node;
259 ---------------------
260 -- Equivalent_Keys --
261 ---------------------
263 function Equivalent_Keys (Left, Right : Cursor) return Boolean is
265 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Keys");
266 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Keys");
269 or else Right.Node = null
271 raise Constraint_Error;
274 if Left.Node.Key = null
275 or else Right.Node.Key = null
280 return Equivalent_Keys (Left.Node.Key.all, Right.Node.Key.all);
283 function Equivalent_Keys
285 Right : Key_Type) return Boolean
288 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Keys");
290 if Left.Node = null then
291 raise Constraint_Error;
294 if Left.Node.Key = null then
298 return Equivalent_Keys (Left.Node.Key.all, Right);
301 function Equivalent_Keys
303 Right : Cursor) return Boolean
306 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Keys");
308 if Right.Node = null then
309 raise Constraint_Error;
312 if Right.Node.Key = null then
316 return Equivalent_Keys (Left, Right.Node.Key.all);
323 procedure Exclude (Container : in out Map; Key : Key_Type) is
326 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
334 procedure Finalize (Container : in out Map) is
336 HT_Ops.Finalize (Container.HT);
343 function Find (Container : Map; Key : Key_Type) return Cursor is
344 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
351 return Cursor'(Container
'Unchecked_Access, Node
);
358 function Find_Equal_Key
359 (R_HT
: Hash_Table_Type
;
360 L_Node
: Node_Access
) return Boolean
362 R_Index
: constant Hash_Type
:= Key_Ops
.Index
(R_HT
, L_Node
.Key
.all);
363 R_Node
: Node_Access
:= R_HT
.Buckets
(R_Index
);
366 while R_Node
/= null loop
367 if Equivalent_Keys
(L_Node
.Key
.all, R_Node
.Key
.all) then
368 return L_Node
.Element
.all = R_Node
.Element
.all;
371 R_Node
:= R_Node
.Next
;
381 function First
(Container
: Map
) return Cursor
is
382 Node
: constant Node_Access
:= HT_Ops
.First
(Container
.HT
);
389 return Cursor
'(Container'Unchecked_Access, Node);
396 procedure Free (X : in out Node_Access) is
397 procedure Deallocate is
398 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
404 X.Next := X; -- detect mischief (in Vet)
413 Free_Element (X.Element);
424 Free_Element (X.Element);
440 function Has_Element (Position : Cursor) return Boolean is
442 pragma Assert (Vet (Position), "bad cursor in Has_Element");
443 return Position.Node /= null;
450 function Hash_Node (Node : Node_Access) return Hash_Type is
452 return Hash (Node.Key.all);
460 (Container : in out Map;
462 New_Item : Element_Type)
471 Insert (Container, Key, New_Item, Position, Inserted);
474 if Container.HT.Lock > 0 then
478 K := Position.Node.Key;
479 E := Position.Node.Element;
481 Position.Node.Key := new Key_Type'(Key
);
484 Position
.Node
.Element
:= new Element_Type
'(New_Item);
501 (Container : in out Map;
503 New_Item : Element_Type;
504 Position : out Cursor;
505 Inserted : out Boolean)
507 function New_Node (Next : Node_Access) return Node_Access;
509 procedure Local_Insert is
510 new Key_Ops.Generic_Conditional_Insert (New_Node);
516 function New_Node (Next : Node_Access) return Node_Access is
517 K : Key_Access := new Key_Type'(Key
);
521 E
:= new Element_Type
'(New_Item);
522 return new Node_Type'(K
, E
, Next
);
530 HT
: Hash_Table_Type
renames Container
.HT
;
532 -- Start of processing for Insert
535 if HT_Ops
.Capacity
(HT
) = 0 then
536 HT_Ops
.Reserve_Capacity
(HT
, 1);
539 Local_Insert
(HT
, Key
, Position
.Node
, Inserted
);
542 and then HT
.Length
> HT_Ops
.Capacity
(HT
)
544 HT_Ops
.Reserve_Capacity
(HT
, HT
.Length
);
547 Position
.Container
:= Container
'Unchecked_Access;
551 (Container
: in out Map
;
553 New_Item
: Element_Type
)
559 Insert
(Container
, Key
, New_Item
, Position
, Inserted
);
562 raise Constraint_Error
;
570 function Is_Empty
(Container
: Map
) return Boolean is
572 return Container
.HT
.Length
= 0;
581 Process
: not null access procedure (Position
: Cursor
))
583 procedure Process_Node
(Node
: Node_Access
);
584 pragma Inline
(Process_Node
);
587 new HT_Ops
.Generic_Iteration
(Process_Node
);
593 procedure Process_Node
(Node
: Node_Access
) is
595 Process
(Cursor
'(Container'Unchecked_Access, Node));
598 -- Start of processing Iterate
601 Iterate (Container.HT);
608 function Key (Position : Cursor) return Key_Type is
610 pragma Assert (Vet (Position), "bad cursor in function Key");
612 if Position.Node = null then
613 raise Constraint_Error;
616 if Position.Node.Key = null then
620 return Position.Node.Key.all;
627 function Length (Container : Map) return Count_Type is
629 return Container.HT.Length;
637 (Target : in out Map;
641 HT_Ops.Move (Target => Target.HT, Source => Source.HT);
648 function Next (Node : Node_Access) return Node_Access is
653 procedure Next (Position : in out Cursor) is
655 Position := Next (Position);
658 function Next (Position : Cursor) return Cursor is
660 pragma Assert (Vet (Position), "bad cursor in function Next");
662 if Position.Node = null then
666 if Position.Node.Key = null
667 or else Position.Node.Element = null
673 HT : Hash_Table_Type renames Position.Container.HT;
674 Node : constant Node_Access := HT_Ops.Next (HT, Position.Node);
681 return Cursor'(Position
.Container
, Node
);
689 procedure Query_Element
691 Process
: not null access procedure (Key
: Key_Type
;
692 Element
: Element_Type
))
695 pragma Assert
(Vet
(Position
), "bad cursor in Query_Element");
697 if Position
.Node
= null then
698 raise Constraint_Error
;
701 if Position
.Node
.Key
= null
702 or else Position
.Node
.Element
= null
708 M
: Map
renames Position
.Container
.all;
709 HT
: Hash_Table_Type
renames M
.HT
'Unrestricted_Access.all;
711 B
: Natural renames HT
.Busy
;
712 L
: Natural renames HT
.Lock
;
719 K
: Key_Type
renames Position
.Node
.Key
.all;
720 E
: Element_Type
renames Position
.Node
.Element
.all;
740 procedure Read_Nodes
is new HT_Ops
.Generic_Read
(Read_Node
);
743 (Stream
: access Root_Stream_Type
'Class;
747 Read_Nodes
(Stream
, Container
.HT
);
751 (Stream
: access Root_Stream_Type
'Class;
763 (Stream
: access Root_Stream_Type
'Class) return Node_Access
765 Node
: Node_Access
:= new Node_Type
;
769 Node
.Key
:= new Key_Type
'(Key_Type'Input (Stream));
777 Node.Element := new Element_Type'(Element_Type
'Input (Stream
));
793 (Container
: in out Map
;
795 New_Item
: Element_Type
)
797 Node
: constant Node_Access
:= Key_Ops
.Find
(Container
.HT
, Key
);
804 raise Constraint_Error
;
807 if Container
.HT
.Lock
> 0 then
814 Node
.Key
:= new Key_Type
'(Key);
817 Node.Element := new Element_Type'(New_Item
);
828 ---------------------
829 -- Replace_Element --
830 ---------------------
832 procedure Replace_Element
833 (Container
: in out Map
;
835 New_Item
: Element_Type
)
838 pragma Assert
(Vet
(Position
), "bad cursor in Replace_Element");
840 if Position
.Node
= null then
841 raise Constraint_Error
;
844 if Position
.Node
.Key
= null
845 or else Position
.Node
.Element
= null
850 if Position
.Container
/= Container
'Unrestricted_Access then
854 if Position
.Container
.HT
.Lock
> 0 then
859 X
: Element_Access
:= Position
.Node
.Element
;
862 Position
.Node
.Element
:= new Element_Type
'(New_Item);
867 ----------------------
868 -- Reserve_Capacity --
869 ----------------------
871 procedure Reserve_Capacity
872 (Container : in out Map;
873 Capacity : Count_Type)
876 HT_Ops.Reserve_Capacity (Container.HT, Capacity);
877 end Reserve_Capacity;
883 procedure Set_Next (Node : Node_Access; Next : Node_Access) is
892 procedure Update_Element
893 (Container : in out Map;
895 Process : not null access procedure (Key : Key_Type;
896 Element : in out Element_Type))
899 pragma Assert (Vet (Position), "bad cursor in Update_Element");
901 if Position.Node = null then
902 raise Constraint_Error;
905 if Position.Node.Key = null
906 or else Position.Node.Element = null
911 if Position.Container /= Container'Unrestricted_Access then
916 HT : Hash_Table_Type renames Container.HT;
918 B : Natural renames HT.Busy;
919 L : Natural renames HT.Lock;
926 K : Key_Type renames Position.Node.Key.all;
927 E : Element_Type renames Position.Node.Element.all;
946 function Vet (Position : Cursor) return Boolean is
948 if Position.Node = null then
949 return Position.Container = null;
952 if Position.Container = null then
956 if Position.Node.Next = Position.Node then
960 if Position.Node.Key = null then
964 if Position.Node.Element = null then
969 HT : Hash_Table_Type renames Position.Container.HT;
973 if HT.Length = 0 then
978 or else HT.Buckets'Length = 0
983 X := HT.Buckets (Key_Ops.Index (HT, Position.Node.Key.all));
985 for J in 1 .. HT.Length loop
986 if X = Position.Node then
994 if X = X.Next then -- to prevent endless loop
1009 procedure Write_Nodes is new HT_Ops.Generic_Write (Write_Node);
1012 (Stream : access Root_Stream_Type'Class;
1016 Write_Nodes (Stream, Container.HT);
1020 (Stream : access Root_Stream_Type'Class;
1024 raise Program_Error;
1031 procedure Write_Node
1032 (Stream : access Root_Stream_Type'Class;
1036 Key_Type'Output (Stream, Node.Key.all);
1037 Element_Type'Output (Stream, Node.Element.all);
1040 end Ada.Containers.Indefinite_Hashed_Maps;