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-2012, 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 3, 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. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- This unit was originally developed by Matthew J Heaney. --
28 ------------------------------------------------------------------------------
30 with Ada
.Unchecked_Deallocation
;
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 System
; use type System
.Address
;
40 package body Ada
.Containers
.Hashed_Maps
is
42 type Iterator
is new Limited_Controlled
and
43 Map_Iterator_Interfaces
.Forward_Iterator
with
45 Container
: Map_Access
;
48 overriding
procedure Finalize
(Object
: in out Iterator
);
50 overriding
function First
(Object
: Iterator
) return Cursor
;
52 overriding
function Next
54 Position
: Cursor
) return Cursor
;
56 -----------------------
57 -- Local Subprograms --
58 -----------------------
61 (Source
: Node_Access
) return Node_Access
;
62 pragma Inline
(Copy_Node
);
64 function Equivalent_Key_Node
66 Node
: Node_Access
) return Boolean;
67 pragma Inline
(Equivalent_Key_Node
);
69 procedure Free
(X
: in out Node_Access
);
71 function Find_Equal_Key
72 (R_HT
: Hash_Table_Type
;
73 L_Node
: Node_Access
) return Boolean;
75 function Hash_Node
(Node
: Node_Access
) return Hash_Type
;
76 pragma Inline
(Hash_Node
);
78 function Next
(Node
: Node_Access
) return Node_Access
;
82 (Stream
: not null access Root_Stream_Type
'Class) return Node_Access
;
83 pragma Inline
(Read_Node
);
85 procedure Set_Next
(Node
: Node_Access
; Next
: Node_Access
);
86 pragma Inline
(Set_Next
);
88 function Vet
(Position
: Cursor
) return Boolean;
91 (Stream
: not null access Root_Stream_Type
'Class;
93 pragma Inline
(Write_Node
);
95 --------------------------
96 -- Local Instantiations --
97 --------------------------
99 package HT_Ops
is new Hash_Tables
.Generic_Operations
100 (HT_Types
=> HT_Types
,
101 Hash_Node
=> Hash_Node
,
103 Set_Next
=> Set_Next
,
104 Copy_Node
=> Copy_Node
,
107 package Key_Ops
is new Hash_Tables
.Generic_Keys
108 (HT_Types
=> HT_Types
,
110 Set_Next
=> Set_Next
,
111 Key_Type
=> Key_Type
,
113 Equivalent_Keys
=> Equivalent_Key_Node
);
115 function Is_Equal
is new HT_Ops
.Generic_Equal
(Find_Equal_Key
);
117 procedure Read_Nodes
is new HT_Ops
.Generic_Read
(Read_Node
);
118 procedure Write_Nodes
is new HT_Ops
.Generic_Write
(Write_Node
);
124 function "=" (Left
, Right
: Map
) return Boolean is
126 return Is_Equal
(Left
.HT
, Right
.HT
);
133 procedure Adjust
(Container
: in out Map
) is
135 HT_Ops
.Adjust
(Container
.HT
);
138 procedure Adjust
(Control
: in out Reference_Control_Type
) is
140 if Control
.Container
/= null then
142 HT
: Hash_Table_Type
renames Control
.Container
.all.HT
;
143 B
: Natural renames HT
.Busy
;
144 L
: Natural renames HT
.Lock
;
156 procedure Assign
(Target
: in out Map
; Source
: Map
) is
157 procedure Insert_Item
(Node
: Node_Access
);
158 pragma Inline
(Insert_Item
);
160 procedure Insert_Items
is new HT_Ops
.Generic_Iteration
(Insert_Item
);
166 procedure Insert_Item
(Node
: Node_Access
) is
168 Target
.Insert
(Key
=> Node
.Key
, New_Item
=> Node
.Element
);
171 -- Start of processing for Assign
174 if Target
'Address = Source
'Address then
180 if Target
.Capacity
< Source
.Length
then
181 Target
.Reserve_Capacity
(Source
.Length
);
184 Insert_Items
(Target
.HT
);
191 function Capacity
(Container
: Map
) return Count_Type
is
193 return HT_Ops
.Capacity
(Container
.HT
);
200 procedure Clear
(Container
: in out Map
) is
202 HT_Ops
.Clear
(Container
.HT
);
205 ------------------------
206 -- Constant_Reference --
207 ------------------------
209 function Constant_Reference
210 (Container
: aliased Map
;
211 Position
: Cursor
) return Constant_Reference_Type
214 if Position
.Container
= null then
215 raise Constraint_Error
with
216 "Position cursor has no element";
219 if Position
.Container
/= Container
'Unrestricted_Access then
220 raise Program_Error
with
221 "Position cursor designates wrong map";
226 "Position cursor in Constant_Reference is bad");
229 HT
: Hash_Table_Type
renames Container
'Unrestricted_Access.all.HT
;
230 B
: Natural renames HT
.Busy
;
231 L
: Natural renames HT
.Lock
;
233 return R
: constant Constant_Reference_Type
:=
234 (Element
=> Position
.Node
.Element
'Access,
235 Control
=> (Controlled
with Position
.Container
))
241 end Constant_Reference
;
243 function Constant_Reference
244 (Container
: aliased Map
;
245 Key
: Key_Type
) return Constant_Reference_Type
247 Node
: constant Node_Access
:= Key_Ops
.Find
(Container
.HT
, Key
);
251 raise Constraint_Error
with "key not in map";
255 HT
: Hash_Table_Type
renames Container
'Unrestricted_Access.all.HT
;
256 B
: Natural renames HT
.Busy
;
257 L
: Natural renames HT
.Lock
;
259 return R
: constant Constant_Reference_Type
:=
260 (Element
=> Node
.Element
'Access,
262 (Controlled
with Container
'Unrestricted_Access))
268 end Constant_Reference
;
274 function Contains
(Container
: Map
; Key
: Key_Type
) return Boolean is
276 return Find
(Container
, Key
) /= No_Element
;
285 Capacity
: Count_Type
:= 0) return Map
293 elsif Capacity
>= Source
.Length
then
298 with "Requested capacity is less than Source length";
301 return Target
: Map
do
302 Target
.Reserve_Capacity
(C
);
303 Target
.Assign
(Source
);
312 (Source
: Node_Access
) return Node_Access
314 Target
: constant Node_Access
:=
315 new Node_Type
'(Key => Source.Key,
316 Element => Source.Element,
326 procedure Delete (Container : in out Map; Key : Key_Type) is
330 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
333 raise Constraint_Error with "attempt to delete key not in map";
339 procedure Delete (Container : in out Map; Position : in out Cursor) is
341 if Position.Node = null then
342 raise Constraint_Error with
343 "Position cursor of Delete equals No_Element";
346 if Position.Container /= Container'Unrestricted_Access then
347 raise Program_Error with
348 "Position cursor of Delete designates wrong map";
351 if Container.HT.Busy > 0 then
352 raise Program_Error with
353 "Delete attempted to tamper with cursors (map is busy)";
356 pragma Assert (Vet (Position), "bad cursor in Delete");
358 HT_Ops.Delete_Node_Sans_Free (Container.HT, Position.Node);
360 Free (Position.Node);
361 Position.Container := null;
368 function Element (Container : Map; Key : Key_Type) return Element_Type is
369 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
373 raise Constraint_Error with
374 "no element available because key not in map";
380 function Element (Position : Cursor) return Element_Type is
382 if Position.Node = null then
383 raise Constraint_Error with
384 "Position cursor of function Element equals No_Element";
387 pragma Assert (Vet (Position), "bad cursor in function Element");
389 return Position.Node.Element;
392 -------------------------
393 -- Equivalent_Key_Node --
394 -------------------------
396 function Equivalent_Key_Node
398 Node : Node_Access) return Boolean is
400 return Equivalent_Keys (Key, Node.Key);
401 end Equivalent_Key_Node;
403 ---------------------
404 -- Equivalent_Keys --
405 ---------------------
407 function Equivalent_Keys (Left, Right : Cursor)
410 if Left.Node = null then
411 raise Constraint_Error with
412 "Left cursor of Equivalent_Keys equals No_Element";
415 if Right.Node = null then
416 raise Constraint_Error with
417 "Right cursor of Equivalent_Keys equals No_Element";
420 pragma Assert (Vet (Left), "Left cursor of Equivalent_Keys is bad");
421 pragma Assert (Vet (Right), "Right cursor of Equivalent_Keys is bad");
423 return Equivalent_Keys (Left.Node.Key, Right.Node.Key);
426 function Equivalent_Keys (Left : Cursor; Right : Key_Type) return Boolean is
428 if Left.Node = null then
429 raise Constraint_Error with
430 "Left cursor of Equivalent_Keys equals No_Element";
433 pragma Assert (Vet (Left), "Left cursor in Equivalent_Keys is bad");
435 return Equivalent_Keys (Left.Node.Key, Right);
438 function Equivalent_Keys (Left : Key_Type; Right : Cursor) return Boolean is
440 if Right.Node = null then
441 raise Constraint_Error with
442 "Right cursor of Equivalent_Keys equals No_Element";
445 pragma Assert (Vet (Right), "Right cursor of Equivalent_Keys is bad");
447 return Equivalent_Keys (Left, Right.Node.Key);
454 procedure Exclude (Container : in out Map; Key : Key_Type) is
457 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
465 procedure Finalize (Container : in out Map) is
467 HT_Ops.Finalize (Container.HT);
470 procedure Finalize (Object : in out Iterator) is
472 if Object.Container /= null then
474 B : Natural renames Object.Container.all.HT.Busy;
481 procedure Finalize (Control : in out Reference_Control_Type) is
483 if Control.Container /= null then
485 HT : Hash_Table_Type renames Control.Container.all.HT;
486 B : Natural renames HT.Busy;
487 L : Natural renames HT.Lock;
493 Control.Container := null;
501 function Find (Container : Map; Key : Key_Type) return Cursor is
502 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
509 return Cursor'(Container
'Unrestricted_Access, Node
);
516 function Find_Equal_Key
517 (R_HT
: Hash_Table_Type
;
518 L_Node
: Node_Access
) return Boolean
520 R_Index
: constant Hash_Type
:= Key_Ops
.Index
(R_HT
, L_Node
.Key
);
521 R_Node
: Node_Access
:= R_HT
.Buckets
(R_Index
);
524 while R_Node
/= null loop
525 if Equivalent_Keys
(L_Node
.Key
, R_Node
.Key
) then
526 return L_Node
.Element
= R_Node
.Element
;
529 R_Node
:= R_Node
.Next
;
539 function First
(Container
: Map
) return Cursor
is
540 Node
: constant Node_Access
:= HT_Ops
.First
(Container
.HT
);
547 return Cursor
'(Container'Unrestricted_Access, Node);
550 function First (Object : Iterator) return Cursor is
552 return Object.Container.First;
559 procedure Free (X : in out Node_Access) is
560 procedure Deallocate is
561 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
564 X.Next := X; -- detect mischief (in Vet)
573 function Has_Element (Position : Cursor) return Boolean is
575 pragma Assert (Vet (Position), "bad cursor in Has_Element");
576 return Position.Node /= null;
583 function Hash_Node (Node : Node_Access) return Hash_Type is
585 return Hash (Node.Key);
593 (Container : in out Map;
595 New_Item : Element_Type)
601 Insert (Container, Key, New_Item, Position, Inserted);
604 if Container.HT.Lock > 0 then
605 raise Program_Error with
606 "Include attempted to tamper with elements (map is locked)";
609 Position.Node.Key := Key;
610 Position.Node.Element := New_Item;
619 (Container : in out Map;
621 Position : out Cursor;
622 Inserted : out Boolean)
624 function New_Node (Next : Node_Access) return Node_Access;
625 pragma Inline (New_Node);
627 procedure Local_Insert is
628 new Key_Ops.Generic_Conditional_Insert (New_Node);
634 function New_Node (Next : Node_Access) return Node_Access is
636 return new Node_Type'(Key
=> Key
,
641 HT
: Hash_Table_Type
renames Container
.HT
;
643 -- Start of processing for Insert
646 if HT_Ops
.Capacity
(HT
) = 0 then
647 HT_Ops
.Reserve_Capacity
(HT
, 1);
650 Local_Insert
(HT
, Key
, Position
.Node
, Inserted
);
653 and then HT
.Length
> HT_Ops
.Capacity
(HT
)
655 HT_Ops
.Reserve_Capacity
(HT
, HT
.Length
);
658 Position
.Container
:= Container
'Unrestricted_Access;
662 (Container
: in out Map
;
664 New_Item
: Element_Type
;
665 Position
: out Cursor
;
666 Inserted
: out Boolean)
668 function New_Node
(Next
: Node_Access
) return Node_Access
;
669 pragma Inline
(New_Node
);
671 procedure Local_Insert
is
672 new Key_Ops
.Generic_Conditional_Insert
(New_Node
);
678 function New_Node
(Next
: Node_Access
) return Node_Access
is
680 return new Node_Type
'(Key, New_Item, Next);
683 HT : Hash_Table_Type renames Container.HT;
685 -- Start of processing for Insert
688 if HT_Ops.Capacity (HT) = 0 then
689 HT_Ops.Reserve_Capacity (HT, 1);
692 Local_Insert (HT, Key, Position.Node, Inserted);
695 and then HT.Length > HT_Ops.Capacity (HT)
697 HT_Ops.Reserve_Capacity (HT, HT.Length);
700 Position.Container := Container'Unrestricted_Access;
704 (Container : in out Map;
706 New_Item : Element_Type)
709 pragma Unreferenced (Position);
714 Insert (Container, Key, New_Item, Position, Inserted);
717 raise Constraint_Error with
718 "attempt to insert key already in map";
726 function Is_Empty (Container : Map) return Boolean is
728 return Container.HT.Length = 0;
737 Process : not null access procedure (Position : Cursor))
739 procedure Process_Node (Node : Node_Access);
740 pragma Inline (Process_Node);
742 procedure Local_Iterate is new HT_Ops.Generic_Iteration (Process_Node);
748 procedure Process_Node (Node : Node_Access) is
750 Process (Cursor'(Container
'Unrestricted_Access, Node
));
753 B
: Natural renames Container
'Unrestricted_Access.all.HT
.Busy
;
755 -- Start of processing for Iterate
761 Local_Iterate
(Container
.HT
);
772 (Container
: Map
) return Map_Iterator_Interfaces
.Forward_Iterator
'Class
774 B
: Natural renames Container
'Unrestricted_Access.all.HT
.Busy
;
776 return It
: constant Iterator
:=
777 (Limited_Controlled
with
778 Container
=> Container
'Unrestricted_Access)
788 function Key
(Position
: Cursor
) return Key_Type
is
790 if Position
.Node
= null then
791 raise Constraint_Error
with
792 "Position cursor of function Key equals No_Element";
795 pragma Assert
(Vet
(Position
), "bad cursor in function Key");
797 return Position
.Node
.Key
;
804 function Length
(Container
: Map
) return Count_Type
is
806 return Container
.HT
.Length
;
814 (Target
: in out Map
;
818 HT_Ops
.Move
(Target
=> Target
.HT
, Source
=> Source
.HT
);
825 function Next
(Node
: Node_Access
) return Node_Access
is
830 function Next
(Position
: Cursor
) return Cursor
is
832 if Position
.Node
= null then
836 pragma Assert
(Vet
(Position
), "bad cursor in function Next");
839 HT
: Hash_Table_Type
renames Position
.Container
.HT
;
840 Node
: constant Node_Access
:= HT_Ops
.Next
(HT
, Position
.Node
);
847 return Cursor
'(Position.Container, Node);
851 procedure Next (Position : in out Cursor) is
853 Position := Next (Position);
858 Position : Cursor) return Cursor
861 if Position.Container = null then
865 if Position.Container /= Object.Container then
866 raise Program_Error with
867 "Position cursor of Next designates wrong map";
870 return Next (Position);
877 procedure Query_Element
879 Process : not null access
880 procedure (Key : Key_Type; Element : Element_Type))
883 if Position.Node = null then
884 raise Constraint_Error with
885 "Position cursor of Query_Element equals No_Element";
888 pragma Assert (Vet (Position), "bad cursor in Query_Element");
891 M : Map renames Position.Container.all;
892 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
894 B : Natural renames HT.Busy;
895 L : Natural renames HT.Lock;
902 K : Key_Type renames Position.Node.Key;
903 E : Element_Type renames Position.Node.Element;
924 (Stream : not null access Root_Stream_Type'Class;
928 Read_Nodes (Stream, Container.HT);
932 (Stream : not null access Root_Stream_Type'Class;
936 raise Program_Error with "attempt to stream map cursor";
940 (Stream : not null access Root_Stream_Type'Class;
941 Item : out Reference_Type)
944 raise Program_Error with "attempt to stream reference";
948 (Stream : not null access Root_Stream_Type'Class;
949 Item : out Constant_Reference_Type)
952 raise Program_Error with "attempt to stream reference";
960 (Container : aliased in out Map;
961 Position : Cursor) return Reference_Type
964 if Position.Container = null then
965 raise Constraint_Error with
966 "Position cursor has no element";
969 if Position.Container /= Container'Unrestricted_Access then
970 raise Program_Error with
971 "Position cursor designates wrong map";
976 "Position cursor in function Reference is bad");
979 HT : Hash_Table_Type renames Container'Unrestricted_Access.all.HT;
980 B : Natural renames HT.Busy;
981 L : Natural renames HT.Lock;
983 return R : constant Reference_Type :=
984 (Element => Position.Node.Element'Access,
985 Control => (Controlled with Position.Container))
994 (Container : aliased in out Map;
995 Key : Key_Type) return Reference_Type
997 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
1001 raise Constraint_Error with "key not in map";
1005 HT : Hash_Table_Type renames Container'Unrestricted_Access.all.HT;
1006 B : Natural renames HT.Busy;
1007 L : Natural renames HT.Lock;
1009 return R : constant Reference_Type :=
1010 (Element => Node.Element'Access,
1012 (Controlled with Container'Unrestricted_Access))
1025 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1027 Node : Node_Access := new Node_Type;
1030 Key_Type'Read (Stream, Node.Key);
1031 Element_Type'Read (Stream, Node.Element);
1045 (Container : in out Map;
1047 New_Item : Element_Type)
1049 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
1053 raise Constraint_Error with
1054 "attempt to replace key not in map";
1057 if Container.HT.Lock > 0 then
1058 raise Program_Error with
1059 "Replace attempted to tamper with elements (map is locked)";
1063 Node.Element := New_Item;
1066 ---------------------
1067 -- Replace_Element --
1068 ---------------------
1070 procedure Replace_Element
1071 (Container : in out Map;
1073 New_Item : Element_Type)
1076 if Position.Node = null then
1077 raise Constraint_Error with
1078 "Position cursor of Replace_Element equals No_Element";
1081 if Position.Container /= Container'Unrestricted_Access then
1082 raise Program_Error with
1083 "Position cursor of Replace_Element designates wrong map";
1086 if Position.Container.HT.Lock > 0 then
1087 raise Program_Error with
1088 "Replace_Element attempted to tamper with elements (map is locked)";
1091 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
1093 Position.Node.Element := New_Item;
1094 end Replace_Element;
1096 ----------------------
1097 -- Reserve_Capacity --
1098 ----------------------
1100 procedure Reserve_Capacity
1101 (Container : in out Map;
1102 Capacity : Count_Type)
1105 HT_Ops.Reserve_Capacity (Container.HT, Capacity);
1106 end Reserve_Capacity;
1112 procedure Set_Next (Node : Node_Access; Next : Node_Access) is
1117 --------------------
1118 -- Update_Element --
1119 --------------------
1121 procedure Update_Element
1122 (Container : in out Map;
1124 Process : not null access procedure (Key : Key_Type;
1125 Element : in out Element_Type))
1128 if Position.Node = null then
1129 raise Constraint_Error with
1130 "Position cursor of Update_Element equals No_Element";
1133 if Position.Container /= Container'Unrestricted_Access then
1134 raise Program_Error with
1135 "Position cursor of Update_Element designates wrong map";
1138 pragma Assert (Vet (Position), "bad cursor in Update_Element");
1141 HT : Hash_Table_Type renames Container.HT;
1142 B : Natural renames HT.Busy;
1143 L : Natural renames HT.Lock;
1150 K : Key_Type renames Position.Node.Key;
1151 E : Element_Type renames Position.Node.Element;
1172 function Vet (Position : Cursor) return Boolean is
1174 if Position.Node = null then
1175 return Position.Container = null;
1178 if Position.Container = null then
1182 if Position.Node.Next = Position.Node then
1187 HT : Hash_Table_Type renames Position.Container.HT;
1191 if HT.Length = 0 then
1195 if HT.Buckets = null
1196 or else HT.Buckets'Length = 0
1201 X := HT.Buckets (Key_Ops.Index (HT, Position.Node.Key));
1203 for J in 1 .. HT.Length loop
1204 if X = Position.Node then
1212 if X = X.Next then -- to prevent unnecessary looping
1228 (Stream : not null access Root_Stream_Type'Class;
1232 Write_Nodes (Stream, Container.HT);
1236 (Stream : not null access Root_Stream_Type'Class;
1240 raise Program_Error with "attempt to stream map cursor";
1244 (Stream : not null access Root_Stream_Type'Class;
1245 Item : Reference_Type)
1248 raise Program_Error with "attempt to stream reference";
1252 (Stream : not null access Root_Stream_Type'Class;
1253 Item : Constant_Reference_Type)
1256 raise Program_Error with "attempt to stream reference";
1263 procedure Write_Node
1264 (Stream : not null access Root_Stream_Type'Class;
1268 Key_Type'Write (Stream, Node.Key);
1269 Element_Type'Write (Stream, Node.Element);
1272 end Ada.Containers.Hashed_Maps;