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-2014, 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 pragma Annotate
(CodePeer
, Skip_Analysis
);
44 -----------------------
45 -- Local Subprograms --
46 -----------------------
49 (Source
: Node_Access
) return Node_Access
;
50 pragma Inline
(Copy_Node
);
52 function Equivalent_Key_Node
54 Node
: Node_Access
) return Boolean;
55 pragma Inline
(Equivalent_Key_Node
);
57 procedure Free
(X
: in out Node_Access
);
59 function Find_Equal_Key
60 (R_HT
: Hash_Table_Type
;
61 L_Node
: Node_Access
) return Boolean;
63 function Hash_Node
(Node
: Node_Access
) return Hash_Type
;
64 pragma Inline
(Hash_Node
);
66 function Next
(Node
: Node_Access
) return Node_Access
;
70 (Stream
: not null access Root_Stream_Type
'Class) return Node_Access
;
71 pragma Inline
(Read_Node
);
73 procedure Set_Next
(Node
: Node_Access
; Next
: Node_Access
);
74 pragma Inline
(Set_Next
);
76 function Vet
(Position
: Cursor
) return Boolean;
79 (Stream
: not null access Root_Stream_Type
'Class;
81 pragma Inline
(Write_Node
);
83 --------------------------
84 -- Local Instantiations --
85 --------------------------
87 package HT_Ops
is new Hash_Tables
.Generic_Operations
88 (HT_Types
=> HT_Types
,
89 Hash_Node
=> Hash_Node
,
92 Copy_Node
=> Copy_Node
,
95 package Key_Ops
is new Hash_Tables
.Generic_Keys
96 (HT_Types
=> HT_Types
,
101 Equivalent_Keys
=> Equivalent_Key_Node
);
103 function Is_Equal
is new HT_Ops
.Generic_Equal
(Find_Equal_Key
);
105 procedure Read_Nodes
is new HT_Ops
.Generic_Read
(Read_Node
);
106 procedure Write_Nodes
is new HT_Ops
.Generic_Write
(Write_Node
);
112 function "=" (Left
, Right
: Map
) return Boolean is
114 return Is_Equal
(Left
.HT
, Right
.HT
);
121 procedure Adjust
(Container
: in out Map
) is
123 HT_Ops
.Adjust
(Container
.HT
);
126 procedure Adjust
(Control
: in out Reference_Control_Type
) is
128 if Control
.Container
/= null then
130 HT
: Hash_Table_Type
renames Control
.Container
.all.HT
;
131 B
: Natural renames HT
.Busy
;
132 L
: Natural renames HT
.Lock
;
144 procedure Assign
(Target
: in out Map
; Source
: Map
) is
145 procedure Insert_Item
(Node
: Node_Access
);
146 pragma Inline
(Insert_Item
);
148 procedure Insert_Items
is new HT_Ops
.Generic_Iteration
(Insert_Item
);
154 procedure Insert_Item
(Node
: Node_Access
) is
156 Target
.Insert
(Key
=> Node
.Key
, New_Item
=> Node
.Element
);
159 -- Start of processing for Assign
162 if Target
'Address = Source
'Address then
168 if Target
.Capacity
< Source
.Length
then
169 Target
.Reserve_Capacity
(Source
.Length
);
172 Insert_Items
(Source
.HT
);
179 function Capacity
(Container
: Map
) return Count_Type
is
181 return HT_Ops
.Capacity
(Container
.HT
);
188 procedure Clear
(Container
: in out Map
) is
190 HT_Ops
.Clear
(Container
.HT
);
193 ------------------------
194 -- Constant_Reference --
195 ------------------------
197 function Constant_Reference
198 (Container
: aliased Map
;
199 Position
: Cursor
) return Constant_Reference_Type
202 if Position
.Container
= null then
203 raise Constraint_Error
with
204 "Position cursor has no element";
207 if Position
.Container
/= Container
'Unrestricted_Access then
208 raise Program_Error
with
209 "Position cursor designates wrong map";
214 "Position cursor in Constant_Reference is bad");
217 HT
: Hash_Table_Type
renames Container
'Unrestricted_Access.all.HT
;
218 B
: Natural renames HT
.Busy
;
219 L
: Natural renames HT
.Lock
;
221 return R
: constant Constant_Reference_Type
:=
222 (Element
=> Position
.Node
.Element
'Access,
223 Control
=> (Controlled
with Position
.Container
))
229 end Constant_Reference
;
231 function Constant_Reference
232 (Container
: aliased Map
;
233 Key
: Key_Type
) return Constant_Reference_Type
235 HT
: Hash_Table_Type
renames Container
'Unrestricted_Access.HT
;
236 Node
: constant Node_Access
:= Key_Ops
.Find
(HT
, Key
);
240 raise Constraint_Error
with "key not in map";
244 B
: Natural renames HT
.Busy
;
245 L
: Natural renames HT
.Lock
;
247 return R
: constant Constant_Reference_Type
:=
248 (Element
=> Node
.Element
'Access,
249 Control
=> (Controlled
with Container
'Unrestricted_Access))
255 end Constant_Reference
;
261 function Contains
(Container
: Map
; Key
: Key_Type
) return Boolean is
263 return Find
(Container
, Key
) /= No_Element
;
272 Capacity
: Count_Type
:= 0) return Map
280 elsif Capacity
>= Source
.Length
then
285 with "Requested capacity is less than Source length";
288 return Target
: Map
do
289 Target
.Reserve_Capacity
(C
);
290 Target
.Assign
(Source
);
299 (Source
: Node_Access
) return Node_Access
301 Target
: constant Node_Access
:=
302 new Node_Type
'(Key => Source.Key,
303 Element => Source.Element,
313 procedure Delete (Container : in out Map; Key : Key_Type) is
317 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
320 raise Constraint_Error with "attempt to delete key not in map";
326 procedure Delete (Container : in out Map; Position : in out Cursor) is
328 if Position.Node = null then
329 raise Constraint_Error with
330 "Position cursor of Delete equals No_Element";
333 if Position.Container /= Container'Unrestricted_Access then
334 raise Program_Error with
335 "Position cursor of Delete designates wrong map";
338 if Container.HT.Busy > 0 then
339 raise Program_Error with
340 "Delete attempted to tamper with cursors (map is busy)";
343 pragma Assert (Vet (Position), "bad cursor in Delete");
345 HT_Ops.Delete_Node_Sans_Free (Container.HT, Position.Node);
347 Free (Position.Node);
348 Position.Container := null;
355 function Element (Container : Map; Key : Key_Type) return Element_Type is
356 HT : Hash_Table_Type renames Container'Unrestricted_Access.HT;
357 Node : constant Node_Access := Key_Ops.Find (HT, Key);
361 raise Constraint_Error with
362 "no element available because key not in map";
368 function Element (Position : Cursor) return Element_Type is
370 if Position.Node = null then
371 raise Constraint_Error with
372 "Position cursor of function Element equals No_Element";
375 pragma Assert (Vet (Position), "bad cursor in function Element");
377 return Position.Node.Element;
380 -------------------------
381 -- Equivalent_Key_Node --
382 -------------------------
384 function Equivalent_Key_Node
386 Node : Node_Access) return Boolean is
388 return Equivalent_Keys (Key, Node.Key);
389 end Equivalent_Key_Node;
391 ---------------------
392 -- Equivalent_Keys --
393 ---------------------
395 function Equivalent_Keys (Left, Right : Cursor)
398 if Left.Node = null then
399 raise Constraint_Error with
400 "Left cursor of Equivalent_Keys equals No_Element";
403 if Right.Node = null then
404 raise Constraint_Error with
405 "Right cursor of Equivalent_Keys equals No_Element";
408 pragma Assert (Vet (Left), "Left cursor of Equivalent_Keys is bad");
409 pragma Assert (Vet (Right), "Right cursor of Equivalent_Keys is bad");
411 return Equivalent_Keys (Left.Node.Key, Right.Node.Key);
414 function Equivalent_Keys (Left : Cursor; Right : Key_Type) return Boolean is
416 if Left.Node = null then
417 raise Constraint_Error with
418 "Left cursor of Equivalent_Keys equals No_Element";
421 pragma Assert (Vet (Left), "Left cursor in Equivalent_Keys is bad");
423 return Equivalent_Keys (Left.Node.Key, Right);
426 function Equivalent_Keys (Left : Key_Type; Right : Cursor) return Boolean is
428 if Right.Node = null then
429 raise Constraint_Error with
430 "Right cursor of Equivalent_Keys equals No_Element";
433 pragma Assert (Vet (Right), "Right cursor of Equivalent_Keys is bad");
435 return Equivalent_Keys (Left, Right.Node.Key);
442 procedure Exclude (Container : in out Map; Key : Key_Type) is
445 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
453 procedure Finalize (Container : in out Map) is
455 HT_Ops.Finalize (Container.HT);
458 procedure Finalize (Object : in out Iterator) is
460 if Object.Container /= null then
462 B : Natural renames Object.Container.all.HT.Busy;
469 procedure Finalize (Control : in out Reference_Control_Type) is
471 if Control.Container /= null then
473 HT : Hash_Table_Type renames Control.Container.all.HT;
474 B : Natural renames HT.Busy;
475 L : Natural renames HT.Lock;
481 Control.Container := null;
489 function Find (Container : Map; Key : Key_Type) return Cursor is
490 HT : Hash_Table_Type renames Container'Unrestricted_Access.HT;
491 Node : constant Node_Access := Key_Ops.Find (HT, Key);
498 return Cursor'(Container
'Unrestricted_Access, Node
);
505 function Find_Equal_Key
506 (R_HT
: Hash_Table_Type
;
507 L_Node
: Node_Access
) return Boolean
509 R_Index
: constant Hash_Type
:= Key_Ops
.Index
(R_HT
, L_Node
.Key
);
510 R_Node
: Node_Access
:= R_HT
.Buckets
(R_Index
);
513 while R_Node
/= null loop
514 if Equivalent_Keys
(L_Node
.Key
, R_Node
.Key
) then
515 return L_Node
.Element
= R_Node
.Element
;
518 R_Node
:= R_Node
.Next
;
528 function First
(Container
: Map
) return Cursor
is
529 Node
: constant Node_Access
:= HT_Ops
.First
(Container
.HT
);
536 return Cursor
'(Container'Unrestricted_Access, Node);
539 function First (Object : Iterator) return Cursor is
541 return Object.Container.First;
548 procedure Free (X : in out Node_Access) is
549 procedure Deallocate is
550 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
553 X.Next := X; -- detect mischief (in Vet)
562 function Has_Element (Position : Cursor) return Boolean is
564 pragma Assert (Vet (Position), "bad cursor in Has_Element");
565 return Position.Node /= null;
572 function Hash_Node (Node : Node_Access) return Hash_Type is
574 return Hash (Node.Key);
582 (Container : in out Map;
584 New_Item : Element_Type)
590 Insert (Container, Key, New_Item, Position, Inserted);
593 if Container.HT.Lock > 0 then
594 raise Program_Error with
595 "Include attempted to tamper with elements (map is locked)";
598 Position.Node.Key := Key;
599 Position.Node.Element := New_Item;
608 (Container : in out Map;
610 Position : out Cursor;
611 Inserted : out Boolean)
613 function New_Node (Next : Node_Access) return Node_Access;
614 pragma Inline (New_Node);
616 procedure Local_Insert is
617 new Key_Ops.Generic_Conditional_Insert (New_Node);
623 function New_Node (Next : Node_Access) return Node_Access is
625 return new Node_Type'(Key
=> Key
,
630 HT
: Hash_Table_Type
renames Container
.HT
;
632 -- Start of processing for Insert
635 if HT_Ops
.Capacity
(HT
) = 0 then
636 HT_Ops
.Reserve_Capacity
(HT
, 1);
639 Local_Insert
(HT
, Key
, Position
.Node
, Inserted
);
642 and then HT
.Length
> HT_Ops
.Capacity
(HT
)
644 HT_Ops
.Reserve_Capacity
(HT
, HT
.Length
);
647 Position
.Container
:= Container
'Unrestricted_Access;
651 (Container
: in out Map
;
653 New_Item
: Element_Type
;
654 Position
: out Cursor
;
655 Inserted
: out Boolean)
657 function New_Node
(Next
: Node_Access
) return Node_Access
;
658 pragma Inline
(New_Node
);
660 procedure Local_Insert
is
661 new Key_Ops
.Generic_Conditional_Insert
(New_Node
);
667 function New_Node
(Next
: Node_Access
) return Node_Access
is
669 return new Node_Type
'(Key, New_Item, Next);
672 HT : Hash_Table_Type renames Container.HT;
674 -- Start of processing for Insert
677 if HT_Ops.Capacity (HT) = 0 then
678 HT_Ops.Reserve_Capacity (HT, 1);
681 Local_Insert (HT, Key, Position.Node, Inserted);
684 and then HT.Length > HT_Ops.Capacity (HT)
686 HT_Ops.Reserve_Capacity (HT, HT.Length);
689 Position.Container := Container'Unrestricted_Access;
693 (Container : in out Map;
695 New_Item : Element_Type)
698 pragma Unreferenced (Position);
703 Insert (Container, Key, New_Item, Position, Inserted);
706 raise Constraint_Error with
707 "attempt to insert key already in map";
715 function Is_Empty (Container : Map) return Boolean is
717 return Container.HT.Length = 0;
726 Process : not null access procedure (Position : Cursor))
728 procedure Process_Node (Node : Node_Access);
729 pragma Inline (Process_Node);
731 procedure Local_Iterate is new HT_Ops.Generic_Iteration (Process_Node);
737 procedure Process_Node (Node : Node_Access) is
739 Process (Cursor'(Container
'Unrestricted_Access, Node
));
742 B
: Natural renames Container
'Unrestricted_Access.all.HT
.Busy
;
744 -- Start of processing for Iterate
750 Local_Iterate
(Container
.HT
);
761 (Container
: Map
) return Map_Iterator_Interfaces
.Forward_Iterator
'Class
763 B
: Natural renames Container
'Unrestricted_Access.all.HT
.Busy
;
765 return It
: constant Iterator
:=
766 (Limited_Controlled
with Container
=> Container
'Unrestricted_Access)
776 function Key
(Position
: Cursor
) return Key_Type
is
778 if Position
.Node
= null then
779 raise Constraint_Error
with
780 "Position cursor of function Key equals No_Element";
783 pragma Assert
(Vet
(Position
), "bad cursor in function Key");
785 return Position
.Node
.Key
;
792 function Length
(Container
: Map
) return Count_Type
is
794 return Container
.HT
.Length
;
802 (Target
: in out Map
;
806 HT_Ops
.Move
(Target
=> Target
.HT
, Source
=> Source
.HT
);
813 function Next
(Node
: Node_Access
) return Node_Access
is
818 function Next
(Position
: Cursor
) return Cursor
is
820 if Position
.Node
= null then
824 pragma Assert
(Vet
(Position
), "bad cursor in function Next");
827 HT
: Hash_Table_Type
renames Position
.Container
.HT
;
828 Node
: constant Node_Access
:= HT_Ops
.Next
(HT
, Position
.Node
);
835 return Cursor
'(Position.Container, Node);
839 procedure Next (Position : in out Cursor) is
841 Position := Next (Position);
846 Position : Cursor) return Cursor
849 if Position.Container = null then
853 if Position.Container /= Object.Container then
854 raise Program_Error with
855 "Position cursor of Next designates wrong map";
858 return Next (Position);
865 procedure Query_Element
867 Process : not null access
868 procedure (Key : Key_Type; Element : Element_Type))
871 if Position.Node = null then
872 raise Constraint_Error with
873 "Position cursor of Query_Element equals No_Element";
876 pragma Assert (Vet (Position), "bad cursor in Query_Element");
879 M : Map renames Position.Container.all;
880 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
882 B : Natural renames HT.Busy;
883 L : Natural renames HT.Lock;
890 K : Key_Type renames Position.Node.Key;
891 E : Element_Type renames Position.Node.Element;
911 (Stream : not null access Root_Stream_Type'Class;
915 Read_Nodes (Stream, Container.HT);
919 (Stream : not null access Root_Stream_Type'Class;
923 raise Program_Error with "attempt to stream map cursor";
927 (Stream : not null access Root_Stream_Type'Class;
928 Item : out Reference_Type)
931 raise Program_Error with "attempt to stream reference";
935 (Stream : not null access Root_Stream_Type'Class;
936 Item : out Constant_Reference_Type)
939 raise Program_Error with "attempt to stream reference";
947 (Container : aliased in out Map;
948 Position : Cursor) return Reference_Type
951 if Position.Container = null then
952 raise Constraint_Error with
953 "Position cursor has no element";
956 if Position.Container /= Container'Unrestricted_Access then
957 raise Program_Error with
958 "Position cursor designates wrong map";
963 "Position cursor in function Reference is bad");
966 HT : Hash_Table_Type renames Container'Unrestricted_Access.all.HT;
967 B : Natural renames HT.Busy;
968 L : Natural renames HT.Lock;
970 return R : constant Reference_Type :=
971 (Element => Position.Node.Element'Access,
972 Control => (Controlled with Position.Container))
981 (Container : aliased in out Map;
982 Key : Key_Type) return Reference_Type
984 HT : Hash_Table_Type renames Container.HT;
985 Node : constant Node_Access := Key_Ops.Find (HT, Key);
989 raise Constraint_Error with "key not in map";
993 B : Natural renames HT.Busy;
994 L : Natural renames HT.Lock;
996 return R : constant Reference_Type :=
997 (Element => Node.Element'Access,
998 Control => (Controlled with Container'Unrestricted_Access))
1011 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1013 Node : Node_Access := new Node_Type;
1016 Key_Type'Read (Stream, Node.Key);
1017 Element_Type'Read (Stream, Node.Element);
1031 (Container : in out Map;
1033 New_Item : Element_Type)
1035 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
1039 raise Constraint_Error with
1040 "attempt to replace key not in map";
1043 if Container.HT.Lock > 0 then
1044 raise Program_Error with
1045 "Replace attempted to tamper with elements (map is locked)";
1049 Node.Element := New_Item;
1052 ---------------------
1053 -- Replace_Element --
1054 ---------------------
1056 procedure Replace_Element
1057 (Container : in out Map;
1059 New_Item : Element_Type)
1062 if Position.Node = null then
1063 raise Constraint_Error with
1064 "Position cursor of Replace_Element equals No_Element";
1067 if Position.Container /= Container'Unrestricted_Access then
1068 raise Program_Error with
1069 "Position cursor of Replace_Element designates wrong map";
1072 if Position.Container.HT.Lock > 0 then
1073 raise Program_Error with
1074 "Replace_Element attempted to tamper with elements (map is locked)";
1077 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
1079 Position.Node.Element := New_Item;
1080 end Replace_Element;
1082 ----------------------
1083 -- Reserve_Capacity --
1084 ----------------------
1086 procedure Reserve_Capacity
1087 (Container : in out Map;
1088 Capacity : Count_Type)
1091 HT_Ops.Reserve_Capacity (Container.HT, Capacity);
1092 end Reserve_Capacity;
1098 procedure Set_Next (Node : Node_Access; Next : Node_Access) is
1103 --------------------
1104 -- Update_Element --
1105 --------------------
1107 procedure Update_Element
1108 (Container : in out Map;
1110 Process : not null access procedure (Key : Key_Type;
1111 Element : in out Element_Type))
1114 if Position.Node = null then
1115 raise Constraint_Error with
1116 "Position cursor of Update_Element equals No_Element";
1119 if Position.Container /= Container'Unrestricted_Access then
1120 raise Program_Error with
1121 "Position cursor of Update_Element designates wrong map";
1124 pragma Assert (Vet (Position), "bad cursor in Update_Element");
1127 HT : Hash_Table_Type renames Container.HT;
1128 B : Natural renames HT.Busy;
1129 L : Natural renames HT.Lock;
1136 K : Key_Type renames Position.Node.Key;
1137 E : Element_Type renames Position.Node.Element;
1156 function Vet (Position : Cursor) return Boolean is
1158 if Position.Node = null then
1159 return Position.Container = null;
1162 if Position.Container = null then
1166 if Position.Node.Next = Position.Node then
1171 HT : Hash_Table_Type renames Position.Container.HT;
1175 if HT.Length = 0 then
1179 if HT.Buckets = null
1180 or else HT.Buckets'Length = 0
1185 X := HT.Buckets (Key_Ops.Checked_Index (HT, Position.Node.Key));
1187 for J in 1 .. HT.Length loop
1188 if X = Position.Node then
1196 if X = X.Next then -- to prevent unnecessary looping
1212 (Stream : not null access Root_Stream_Type'Class;
1216 Write_Nodes (Stream, Container.HT);
1220 (Stream : not null access Root_Stream_Type'Class;
1224 raise Program_Error with "attempt to stream map cursor";
1228 (Stream : not null access Root_Stream_Type'Class;
1229 Item : Reference_Type)
1232 raise Program_Error with "attempt to stream reference";
1236 (Stream : not null access Root_Stream_Type'Class;
1237 Item : Constant_Reference_Type)
1240 raise Program_Error with "attempt to stream reference";
1247 procedure Write_Node
1248 (Stream : not null access Root_Stream_Type'Class;
1252 Key_Type'Write (Stream, Node.Key);
1253 Element_Type'Write (Stream, Node.Element);
1256 end Ada.Containers.Hashed_Maps;