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-2015, 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 Ada
.Containers
.Helpers
; use Ada
.Containers
.Helpers
;
40 with System
; use type System
.Address
;
42 package body Ada
.Containers
.Hashed_Maps
is
44 pragma Warnings
(Off
, "variable ""Busy*"" is not referenced");
45 pragma Warnings
(Off
, "variable ""Lock*"" is not referenced");
46 -- See comment in Ada.Containers.Helpers
48 -----------------------
49 -- Local Subprograms --
50 -----------------------
53 (Source
: Node_Access
) return Node_Access
;
54 pragma Inline
(Copy_Node
);
56 function Equivalent_Key_Node
58 Node
: Node_Access
) return Boolean;
59 pragma Inline
(Equivalent_Key_Node
);
61 procedure Free
(X
: in out Node_Access
);
63 function Find_Equal_Key
64 (R_HT
: Hash_Table_Type
;
65 L_Node
: Node_Access
) return Boolean;
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
;
75 pragma Inline
(Read_Node
);
77 procedure Set_Next
(Node
: Node_Access
; Next
: Node_Access
);
78 pragma Inline
(Set_Next
);
80 function Vet
(Position
: Cursor
) return Boolean;
83 (Stream
: not null access Root_Stream_Type
'Class;
85 pragma Inline
(Write_Node
);
87 --------------------------
88 -- Local Instantiations --
89 --------------------------
91 package HT_Ops
is new Hash_Tables
.Generic_Operations
92 (HT_Types
=> HT_Types
,
93 Hash_Node
=> Hash_Node
,
96 Copy_Node
=> Copy_Node
,
99 package Key_Ops
is 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
);
107 function Is_Equal
is new HT_Ops
.Generic_Equal
(Find_Equal_Key
);
109 procedure Read_Nodes
is new HT_Ops
.Generic_Read
(Read_Node
);
110 procedure Write_Nodes
is new HT_Ops
.Generic_Write
(Write_Node
);
116 function "=" (Left
, Right
: Map
) return Boolean is
118 return Is_Equal
(Left
.HT
, Right
.HT
);
125 procedure Adjust
(Container
: in out Map
) is
127 HT_Ops
.Adjust
(Container
.HT
);
134 procedure Assign
(Target
: in out Map
; Source
: Map
) is
135 procedure Insert_Item
(Node
: Node_Access
);
136 pragma Inline
(Insert_Item
);
138 procedure Insert_Items
is new HT_Ops
.Generic_Iteration
(Insert_Item
);
144 procedure Insert_Item
(Node
: Node_Access
) is
146 Target
.Insert
(Key
=> Node
.Key
, New_Item
=> Node
.Element
);
149 -- Start of processing for Assign
152 if Target
'Address = Source
'Address then
158 if Target
.Capacity
< Source
.Length
then
159 Target
.Reserve_Capacity
(Source
.Length
);
162 Insert_Items
(Source
.HT
);
169 function Capacity
(Container
: Map
) return Count_Type
is
171 return HT_Ops
.Capacity
(Container
.HT
);
178 procedure Clear
(Container
: in out Map
) is
180 HT_Ops
.Clear
(Container
.HT
);
183 ------------------------
184 -- Constant_Reference --
185 ------------------------
187 function Constant_Reference
188 (Container
: aliased Map
;
189 Position
: Cursor
) return Constant_Reference_Type
192 if Checks
and then Position
.Container
= null then
193 raise Constraint_Error
with
194 "Position cursor has no element";
197 if Checks
and then Position
.Container
/= Container
'Unrestricted_Access
199 raise Program_Error
with
200 "Position cursor designates wrong map";
205 "Position cursor in Constant_Reference is bad");
208 HT
: Hash_Table_Type
renames Container
'Unrestricted_Access.all.HT
;
209 TC
: constant Tamper_Counts_Access
:=
210 HT
.TC
'Unrestricted_Access;
212 return R
: constant Constant_Reference_Type
:=
213 (Element
=> Position
.Node
.Element
'Access,
214 Control
=> (Controlled
with TC
))
219 end Constant_Reference
;
221 function Constant_Reference
222 (Container
: aliased Map
;
223 Key
: Key_Type
) return Constant_Reference_Type
225 HT
: Hash_Table_Type
renames Container
'Unrestricted_Access.HT
;
226 Node
: constant Node_Access
:= Key_Ops
.Find
(HT
, Key
);
229 if Checks
and then Node
= null then
230 raise Constraint_Error
with "key not in map";
234 TC
: constant Tamper_Counts_Access
:=
235 HT
.TC
'Unrestricted_Access;
237 return R
: constant Constant_Reference_Type
:=
238 (Element
=> Node
.Element
'Access,
239 Control
=> (Controlled
with TC
))
244 end Constant_Reference
;
250 function Contains
(Container
: Map
; Key
: Key_Type
) return Boolean is
252 return Find
(Container
, Key
) /= No_Element
;
261 Capacity
: Count_Type
:= 0) return Map
269 elsif Capacity
>= Source
.Length
then
274 with "Requested capacity is less than Source length";
277 return Target
: Map
do
278 Target
.Reserve_Capacity
(C
);
279 Target
.Assign
(Source
);
288 (Source
: Node_Access
) return Node_Access
290 Target
: constant Node_Access
:=
291 new Node_Type
'(Key => Source.Key,
292 Element => Source.Element,
302 procedure Delete (Container : in out Map; Key : Key_Type) is
306 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
308 if Checks and then X = null then
309 raise Constraint_Error with "attempt to delete key not in map";
315 procedure Delete (Container : in out Map; Position : in out Cursor) is
317 if Checks and then Position.Node = null then
318 raise Constraint_Error with
319 "Position cursor of Delete equals No_Element";
322 if Checks and then Position.Container /= Container'Unrestricted_Access
324 raise Program_Error with
325 "Position cursor of Delete designates wrong map";
328 TC_Check (Container.HT.TC);
330 pragma Assert (Vet (Position), "bad cursor in Delete");
332 HT_Ops.Delete_Node_Sans_Free (Container.HT, Position.Node);
334 Free (Position.Node);
335 Position.Container := null;
342 function Element (Container : Map; Key : Key_Type) return Element_Type is
343 HT : Hash_Table_Type renames Container'Unrestricted_Access.HT;
344 Node : constant Node_Access := Key_Ops.Find (HT, Key);
347 if Checks and then Node = null then
348 raise Constraint_Error with
349 "no element available because key not in map";
355 function Element (Position : Cursor) return Element_Type is
357 if Checks and then Position.Node = null then
358 raise Constraint_Error with
359 "Position cursor of function Element equals No_Element";
362 pragma Assert (Vet (Position), "bad cursor in function Element");
364 return Position.Node.Element;
367 -------------------------
368 -- Equivalent_Key_Node --
369 -------------------------
371 function Equivalent_Key_Node
373 Node : Node_Access) return Boolean is
375 return Equivalent_Keys (Key, Node.Key);
376 end Equivalent_Key_Node;
378 ---------------------
379 -- Equivalent_Keys --
380 ---------------------
382 function Equivalent_Keys (Left, Right : Cursor)
385 if Checks and then Left.Node = null then
386 raise Constraint_Error with
387 "Left cursor of Equivalent_Keys equals No_Element";
390 if Checks and then Right.Node = null then
391 raise Constraint_Error with
392 "Right cursor of Equivalent_Keys equals No_Element";
395 pragma Assert (Vet (Left), "Left cursor of Equivalent_Keys is bad");
396 pragma Assert (Vet (Right), "Right cursor of Equivalent_Keys is bad");
398 return Equivalent_Keys (Left.Node.Key, Right.Node.Key);
401 function Equivalent_Keys (Left : Cursor; Right : Key_Type) return Boolean is
403 if Checks and then Left.Node = null then
404 raise Constraint_Error with
405 "Left cursor of Equivalent_Keys equals No_Element";
408 pragma Assert (Vet (Left), "Left cursor in Equivalent_Keys is bad");
410 return Equivalent_Keys (Left.Node.Key, Right);
413 function Equivalent_Keys (Left : Key_Type; Right : Cursor) return Boolean is
415 if Checks and then Right.Node = null then
416 raise Constraint_Error with
417 "Right cursor of Equivalent_Keys equals No_Element";
420 pragma Assert (Vet (Right), "Right cursor of Equivalent_Keys is bad");
422 return Equivalent_Keys (Left, Right.Node.Key);
429 procedure Exclude (Container : in out Map; Key : Key_Type) is
432 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
440 procedure Finalize (Container : in out Map) is
442 HT_Ops.Finalize (Container.HT);
445 procedure Finalize (Object : in out Iterator) is
447 if Object.Container /= null then
448 Unbusy (Object.Container.HT.TC);
456 function Find (Container : Map; Key : Key_Type) return Cursor is
457 HT : Hash_Table_Type renames Container'Unrestricted_Access.HT;
458 Node : constant Node_Access := Key_Ops.Find (HT, Key);
465 return Cursor'(Container
'Unrestricted_Access, Node
);
472 function Find_Equal_Key
473 (R_HT
: Hash_Table_Type
;
474 L_Node
: Node_Access
) return Boolean
476 R_Index
: constant Hash_Type
:= Key_Ops
.Index
(R_HT
, L_Node
.Key
);
477 R_Node
: Node_Access
:= R_HT
.Buckets
(R_Index
);
480 while R_Node
/= null loop
481 if Equivalent_Keys
(L_Node
.Key
, R_Node
.Key
) then
482 return L_Node
.Element
= R_Node
.Element
;
485 R_Node
:= R_Node
.Next
;
495 function First
(Container
: Map
) return Cursor
is
496 Node
: constant Node_Access
:= HT_Ops
.First
(Container
.HT
);
503 return Cursor
'(Container'Unrestricted_Access, Node);
506 function First (Object : Iterator) return Cursor is
508 return Object.Container.First;
515 procedure Free (X : in out Node_Access) is
516 procedure Deallocate is
517 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
520 X.Next := X; -- detect mischief (in Vet)
525 ------------------------
526 -- Get_Element_Access --
527 ------------------------
529 function Get_Element_Access
530 (Position : Cursor) return not null Element_Access is
532 return Position.Node.Element'Access;
533 end Get_Element_Access;
539 function Has_Element (Position : Cursor) return Boolean is
541 pragma Assert (Vet (Position), "bad cursor in Has_Element");
542 return Position.Node /= null;
549 function Hash_Node (Node : Node_Access) return Hash_Type is
551 return Hash (Node.Key);
559 (Container : in out Map;
561 New_Item : Element_Type)
567 Insert (Container, Key, New_Item, Position, Inserted);
570 TE_Check (Container.HT.TC);
572 Position.Node.Key := Key;
573 Position.Node.Element := New_Item;
582 (Container : in out Map;
584 Position : out Cursor;
585 Inserted : out Boolean)
587 function New_Node (Next : Node_Access) return Node_Access;
588 pragma Inline (New_Node);
590 procedure Local_Insert is
591 new Key_Ops.Generic_Conditional_Insert (New_Node);
597 function New_Node (Next : Node_Access) return Node_Access is
599 return new Node_Type'(Key
=> Key
,
604 HT
: Hash_Table_Type
renames Container
.HT
;
606 -- Start of processing for Insert
609 if HT_Ops
.Capacity
(HT
) = 0 then
610 HT_Ops
.Reserve_Capacity
(HT
, 1);
613 Local_Insert
(HT
, Key
, Position
.Node
, Inserted
);
616 and then HT
.Length
> HT_Ops
.Capacity
(HT
)
618 HT_Ops
.Reserve_Capacity
(HT
, HT
.Length
);
621 Position
.Container
:= Container
'Unrestricted_Access;
625 (Container
: in out Map
;
627 New_Item
: Element_Type
;
628 Position
: out Cursor
;
629 Inserted
: out Boolean)
631 function New_Node
(Next
: Node_Access
) return Node_Access
;
632 pragma Inline
(New_Node
);
634 procedure Local_Insert
is
635 new Key_Ops
.Generic_Conditional_Insert
(New_Node
);
641 function New_Node
(Next
: Node_Access
) return Node_Access
is
643 return new Node_Type
'(Key, New_Item, Next);
646 HT : Hash_Table_Type renames Container.HT;
648 -- Start of processing for Insert
651 if HT_Ops.Capacity (HT) = 0 then
652 HT_Ops.Reserve_Capacity (HT, 1);
655 Local_Insert (HT, Key, Position.Node, Inserted);
658 and then HT.Length > HT_Ops.Capacity (HT)
660 HT_Ops.Reserve_Capacity (HT, HT.Length);
663 Position.Container := Container'Unrestricted_Access;
667 (Container : in out Map;
669 New_Item : Element_Type)
672 pragma Unreferenced (Position);
677 Insert (Container, Key, New_Item, Position, Inserted);
679 if Checks and then not Inserted then
680 raise Constraint_Error with
681 "attempt to insert key already in map";
689 function Is_Empty (Container : Map) return Boolean is
691 return Container.HT.Length = 0;
700 Process : not null access procedure (Position : Cursor))
702 procedure Process_Node (Node : Node_Access);
703 pragma Inline (Process_Node);
705 procedure Local_Iterate is new HT_Ops.Generic_Iteration (Process_Node);
711 procedure Process_Node (Node : Node_Access) is
713 Process (Cursor'(Container
'Unrestricted_Access, Node
));
716 Busy
: With_Busy
(Container
.HT
.TC
'Unrestricted_Access);
718 -- Start of processing for Iterate
721 Local_Iterate
(Container
.HT
);
725 (Container
: Map
) return Map_Iterator_Interfaces
.Forward_Iterator
'Class
728 return It
: constant Iterator
:=
729 (Limited_Controlled
with Container
=> Container
'Unrestricted_Access)
731 Busy
(Container
.HT
.TC
'Unrestricted_Access.all);
739 function Key
(Position
: Cursor
) return Key_Type
is
741 if Checks
and then Position
.Node
= null then
742 raise Constraint_Error
with
743 "Position cursor of function Key equals No_Element";
746 pragma Assert
(Vet
(Position
), "bad cursor in function Key");
748 return Position
.Node
.Key
;
755 function Length
(Container
: Map
) return Count_Type
is
757 return Container
.HT
.Length
;
765 (Target
: in out Map
;
769 HT_Ops
.Move
(Target
=> Target
.HT
, Source
=> Source
.HT
);
776 function Next
(Node
: Node_Access
) return Node_Access
is
781 function Next
(Position
: Cursor
) return Cursor
is
783 if Position
.Node
= null then
787 pragma Assert
(Vet
(Position
), "bad cursor in function Next");
790 HT
: Hash_Table_Type
renames Position
.Container
.HT
;
791 Node
: constant Node_Access
:= HT_Ops
.Next
(HT
, Position
.Node
);
798 return Cursor
'(Position.Container, Node);
802 procedure Next (Position : in out Cursor) is
804 Position := Next (Position);
809 Position : Cursor) return Cursor
812 if Position.Container = null then
816 if Checks and then Position.Container /= Object.Container then
817 raise Program_Error with
818 "Position cursor of Next designates wrong map";
821 return Next (Position);
824 ----------------------
825 -- Pseudo_Reference --
826 ----------------------
828 function Pseudo_Reference
829 (Container : aliased Map'Class) return Reference_Control_Type
831 TC : constant Tamper_Counts_Access :=
832 Container.HT.TC'Unrestricted_Access;
834 return R : constant Reference_Control_Type := (Controlled with TC) do
837 end Pseudo_Reference;
843 procedure Query_Element
845 Process : not null access
846 procedure (Key : Key_Type; Element : Element_Type))
849 if Checks and then Position.Node = null then
850 raise Constraint_Error with
851 "Position cursor of Query_Element equals No_Element";
854 pragma Assert (Vet (Position), "bad cursor in Query_Element");
857 M : Map renames Position.Container.all;
858 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
859 Lock : With_Lock (HT.TC'Unrestricted_Access);
860 K : Key_Type renames Position.Node.Key;
861 E : Element_Type renames Position.Node.Element;
872 (Stream : not null access Root_Stream_Type'Class;
876 Read_Nodes (Stream, Container.HT);
880 (Stream : not null access Root_Stream_Type'Class;
884 raise Program_Error with "attempt to stream map cursor";
888 (Stream : not null access Root_Stream_Type'Class;
889 Item : out Reference_Type)
892 raise Program_Error with "attempt to stream reference";
896 (Stream : not null access Root_Stream_Type'Class;
897 Item : out Constant_Reference_Type)
900 raise Program_Error with "attempt to stream reference";
908 (Container : aliased in out Map;
909 Position : Cursor) return Reference_Type
912 if Checks and then Position.Container = null then
913 raise Constraint_Error with
914 "Position cursor has no element";
917 if Checks and then Position.Container /= Container'Unrestricted_Access
919 raise Program_Error with
920 "Position cursor designates wrong map";
925 "Position cursor in function Reference is bad");
928 HT : Hash_Table_Type renames Container'Unrestricted_Access.all.HT;
929 TC : constant Tamper_Counts_Access :=
930 HT.TC'Unrestricted_Access;
932 return R : constant Reference_Type :=
933 (Element => Position.Node.Element'Access,
934 Control => (Controlled with TC))
942 (Container : aliased in out Map;
943 Key : Key_Type) return Reference_Type
945 HT : Hash_Table_Type renames Container.HT;
946 Node : constant Node_Access := Key_Ops.Find (HT, Key);
949 if Checks and then Node = null then
950 raise Constraint_Error with "key not in map";
954 TC : constant Tamper_Counts_Access :=
955 HT.TC'Unrestricted_Access;
957 return R : constant Reference_Type :=
958 (Element => Node.Element'Access,
959 Control => (Controlled with TC))
971 (Stream : not null access Root_Stream_Type'Class) return Node_Access
973 Node : Node_Access := new Node_Type;
976 Key_Type'Read (Stream, Node.Key);
977 Element_Type'Read (Stream, Node.Element);
991 (Container : in out Map;
993 New_Item : Element_Type)
995 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
998 if Checks and then Node = null then
999 raise Constraint_Error with
1000 "attempt to replace key not in map";
1003 TE_Check (Container.HT.TC);
1006 Node.Element := New_Item;
1009 ---------------------
1010 -- Replace_Element --
1011 ---------------------
1013 procedure Replace_Element
1014 (Container : in out Map;
1016 New_Item : Element_Type)
1019 if Checks and then Position.Node = null then
1020 raise Constraint_Error with
1021 "Position cursor of Replace_Element equals No_Element";
1024 if Checks and then Position.Container /= Container'Unrestricted_Access
1026 raise Program_Error with
1027 "Position cursor of Replace_Element designates wrong map";
1030 TE_Check (Position.Container.HT.TC);
1032 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
1034 Position.Node.Element := New_Item;
1035 end Replace_Element;
1037 ----------------------
1038 -- Reserve_Capacity --
1039 ----------------------
1041 procedure Reserve_Capacity
1042 (Container : in out Map;
1043 Capacity : Count_Type)
1046 HT_Ops.Reserve_Capacity (Container.HT, Capacity);
1047 end Reserve_Capacity;
1053 procedure Set_Next (Node : Node_Access; Next : Node_Access) is
1058 --------------------
1059 -- Update_Element --
1060 --------------------
1062 procedure Update_Element
1063 (Container : in out Map;
1065 Process : not null access procedure (Key : Key_Type;
1066 Element : in out Element_Type))
1069 if Checks and then Position.Node = null then
1070 raise Constraint_Error with
1071 "Position cursor of Update_Element equals No_Element";
1074 if Checks and then Position.Container /= Container'Unrestricted_Access
1076 raise Program_Error with
1077 "Position cursor of Update_Element designates wrong map";
1080 pragma Assert (Vet (Position), "bad cursor in Update_Element");
1083 HT : Hash_Table_Type renames Container.HT;
1084 Lock : With_Lock (HT.TC'Unrestricted_Access);
1085 K : Key_Type renames Position.Node.Key;
1086 E : Element_Type renames Position.Node.Element;
1096 function Vet (Position : Cursor) return Boolean is
1098 if Position.Node = null then
1099 return Position.Container = null;
1102 if Position.Container = null then
1106 if Position.Node.Next = Position.Node then
1111 HT : Hash_Table_Type renames Position.Container.HT;
1115 if HT.Length = 0 then
1119 if HT.Buckets = null
1120 or else HT.Buckets'Length = 0
1125 X := HT.Buckets (Key_Ops.Checked_Index (HT, Position.Node.Key));
1127 for J in 1 .. HT.Length loop
1128 if X = Position.Node then
1136 if X = X.Next then -- to prevent unnecessary looping
1152 (Stream : not null access Root_Stream_Type'Class;
1156 Write_Nodes (Stream, Container.HT);
1160 (Stream : not null access Root_Stream_Type'Class;
1164 raise Program_Error with "attempt to stream map cursor";
1168 (Stream : not null access Root_Stream_Type'Class;
1169 Item : Reference_Type)
1172 raise Program_Error with "attempt to stream reference";
1176 (Stream : not null access Root_Stream_Type'Class;
1177 Item : Constant_Reference_Type)
1180 raise Program_Error with "attempt to stream reference";
1187 procedure Write_Node
1188 (Stream : not null access Root_Stream_Type'Class;
1192 Key_Type'Write (Stream, Node.Key);
1193 Element_Type'Write (Stream, Node.Element);
1196 end Ada.Containers.Hashed_Maps;