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-2013, 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 -----------------------
43 -- Local Subprograms --
44 -----------------------
47 (Source
: Node_Access
) return Node_Access
;
48 pragma Inline
(Copy_Node
);
50 function Equivalent_Key_Node
52 Node
: Node_Access
) return Boolean;
53 pragma Inline
(Equivalent_Key_Node
);
55 procedure Free
(X
: in out Node_Access
);
57 function Find_Equal_Key
58 (R_HT
: Hash_Table_Type
;
59 L_Node
: Node_Access
) return Boolean;
61 function Hash_Node
(Node
: Node_Access
) return Hash_Type
;
62 pragma Inline
(Hash_Node
);
64 function Next
(Node
: Node_Access
) return Node_Access
;
68 (Stream
: not null access Root_Stream_Type
'Class) return Node_Access
;
69 pragma Inline
(Read_Node
);
71 procedure Set_Next
(Node
: Node_Access
; Next
: Node_Access
);
72 pragma Inline
(Set_Next
);
74 function Vet
(Position
: Cursor
) return Boolean;
77 (Stream
: not null access Root_Stream_Type
'Class;
79 pragma Inline
(Write_Node
);
81 --------------------------
82 -- Local Instantiations --
83 --------------------------
85 package HT_Ops
is new Hash_Tables
.Generic_Operations
86 (HT_Types
=> HT_Types
,
87 Hash_Node
=> Hash_Node
,
90 Copy_Node
=> Copy_Node
,
93 package Key_Ops
is new Hash_Tables
.Generic_Keys
94 (HT_Types
=> HT_Types
,
99 Equivalent_Keys
=> Equivalent_Key_Node
);
101 function Is_Equal
is new HT_Ops
.Generic_Equal
(Find_Equal_Key
);
103 procedure Read_Nodes
is new HT_Ops
.Generic_Read
(Read_Node
);
104 procedure Write_Nodes
is new HT_Ops
.Generic_Write
(Write_Node
);
110 function "=" (Left
, Right
: Map
) return Boolean is
112 return Is_Equal
(Left
.HT
, Right
.HT
);
119 procedure Adjust
(Container
: in out Map
) is
121 HT_Ops
.Adjust
(Container
.HT
);
124 procedure Adjust
(Control
: in out Reference_Control_Type
) is
126 if Control
.Container
/= null then
128 HT
: Hash_Table_Type
renames Control
.Container
.all.HT
;
129 B
: Natural renames HT
.Busy
;
130 L
: Natural renames HT
.Lock
;
142 procedure Assign
(Target
: in out Map
; Source
: Map
) is
143 procedure Insert_Item
(Node
: Node_Access
);
144 pragma Inline
(Insert_Item
);
146 procedure Insert_Items
is new HT_Ops
.Generic_Iteration
(Insert_Item
);
152 procedure Insert_Item
(Node
: Node_Access
) is
154 Target
.Insert
(Key
=> Node
.Key
, New_Item
=> Node
.Element
);
157 -- Start of processing for Assign
160 if Target
'Address = Source
'Address then
166 if Target
.Capacity
< Source
.Length
then
167 Target
.Reserve_Capacity
(Source
.Length
);
170 Insert_Items
(Target
.HT
);
177 function Capacity
(Container
: Map
) return Count_Type
is
179 return HT_Ops
.Capacity
(Container
.HT
);
186 procedure Clear
(Container
: in out Map
) is
188 HT_Ops
.Clear
(Container
.HT
);
191 ------------------------
192 -- Constant_Reference --
193 ------------------------
195 function Constant_Reference
196 (Container
: aliased Map
;
197 Position
: Cursor
) return Constant_Reference_Type
200 if Position
.Container
= null then
201 raise Constraint_Error
with
202 "Position cursor has no element";
205 if Position
.Container
/= Container
'Unrestricted_Access then
206 raise Program_Error
with
207 "Position cursor designates wrong map";
212 "Position cursor in Constant_Reference is bad");
215 HT
: Hash_Table_Type
renames Container
'Unrestricted_Access.all.HT
;
216 B
: Natural renames HT
.Busy
;
217 L
: Natural renames HT
.Lock
;
219 return R
: constant Constant_Reference_Type
:=
220 (Element
=> Position
.Node
.Element
'Access,
221 Control
=> (Controlled
with Position
.Container
))
227 end Constant_Reference
;
229 function Constant_Reference
230 (Container
: aliased Map
;
231 Key
: Key_Type
) return Constant_Reference_Type
233 Node
: constant Node_Access
:= Key_Ops
.Find
(Container
.HT
, Key
);
237 raise Constraint_Error
with "key not in map";
241 HT
: Hash_Table_Type
renames Container
'Unrestricted_Access.all.HT
;
242 B
: Natural renames HT
.Busy
;
243 L
: Natural renames HT
.Lock
;
245 return R
: constant Constant_Reference_Type
:=
246 (Element
=> Node
.Element
'Access,
247 Control
=> (Controlled
with Container
'Unrestricted_Access))
253 end Constant_Reference
;
259 function Contains
(Container
: Map
; Key
: Key_Type
) return Boolean is
261 return Find
(Container
, Key
) /= No_Element
;
270 Capacity
: Count_Type
:= 0) return Map
278 elsif Capacity
>= Source
.Length
then
283 with "Requested capacity is less than Source length";
286 return Target
: Map
do
287 Target
.Reserve_Capacity
(C
);
288 Target
.Assign
(Source
);
297 (Source
: Node_Access
) return Node_Access
299 Target
: constant Node_Access
:=
300 new Node_Type
'(Key => Source.Key,
301 Element => Source.Element,
311 procedure Delete (Container : in out Map; Key : Key_Type) is
315 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
318 raise Constraint_Error with "attempt to delete key not in map";
324 procedure Delete (Container : in out Map; Position : in out Cursor) is
326 if Position.Node = null then
327 raise Constraint_Error with
328 "Position cursor of Delete equals No_Element";
331 if Position.Container /= Container'Unrestricted_Access then
332 raise Program_Error with
333 "Position cursor of Delete designates wrong map";
336 if Container.HT.Busy > 0 then
337 raise Program_Error with
338 "Delete attempted to tamper with cursors (map is busy)";
341 pragma Assert (Vet (Position), "bad cursor in Delete");
343 HT_Ops.Delete_Node_Sans_Free (Container.HT, Position.Node);
345 Free (Position.Node);
346 Position.Container := null;
353 function Element (Container : Map; Key : Key_Type) return Element_Type is
354 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
358 raise Constraint_Error with
359 "no element available because key not in map";
365 function Element (Position : Cursor) return Element_Type is
367 if Position.Node = null then
368 raise Constraint_Error with
369 "Position cursor of function Element equals No_Element";
372 pragma Assert (Vet (Position), "bad cursor in function Element");
374 return Position.Node.Element;
377 -------------------------
378 -- Equivalent_Key_Node --
379 -------------------------
381 function Equivalent_Key_Node
383 Node : Node_Access) return Boolean is
385 return Equivalent_Keys (Key, Node.Key);
386 end Equivalent_Key_Node;
388 ---------------------
389 -- Equivalent_Keys --
390 ---------------------
392 function Equivalent_Keys (Left, Right : Cursor)
395 if Left.Node = null then
396 raise Constraint_Error with
397 "Left cursor of Equivalent_Keys equals No_Element";
400 if Right.Node = null then
401 raise Constraint_Error with
402 "Right cursor of Equivalent_Keys equals No_Element";
405 pragma Assert (Vet (Left), "Left cursor of Equivalent_Keys is bad");
406 pragma Assert (Vet (Right), "Right cursor of Equivalent_Keys is bad");
408 return Equivalent_Keys (Left.Node.Key, Right.Node.Key);
411 function Equivalent_Keys (Left : Cursor; Right : Key_Type) return Boolean is
413 if Left.Node = null then
414 raise Constraint_Error with
415 "Left cursor of Equivalent_Keys equals No_Element";
418 pragma Assert (Vet (Left), "Left cursor in Equivalent_Keys is bad");
420 return Equivalent_Keys (Left.Node.Key, Right);
423 function Equivalent_Keys (Left : Key_Type; Right : Cursor) return Boolean is
425 if Right.Node = null then
426 raise Constraint_Error with
427 "Right cursor of Equivalent_Keys equals No_Element";
430 pragma Assert (Vet (Right), "Right cursor of Equivalent_Keys is bad");
432 return Equivalent_Keys (Left, Right.Node.Key);
439 procedure Exclude (Container : in out Map; Key : Key_Type) is
442 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
450 procedure Finalize (Container : in out Map) is
452 HT_Ops.Finalize (Container.HT);
455 procedure Finalize (Object : in out Iterator) is
457 if Object.Container /= null then
459 B : Natural renames Object.Container.all.HT.Busy;
466 procedure Finalize (Control : in out Reference_Control_Type) is
468 if Control.Container /= null then
470 HT : Hash_Table_Type renames Control.Container.all.HT;
471 B : Natural renames HT.Busy;
472 L : Natural renames HT.Lock;
478 Control.Container := null;
486 function Find (Container : Map; Key : Key_Type) return Cursor is
487 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
494 return Cursor'(Container
'Unrestricted_Access, Node
);
501 function Find_Equal_Key
502 (R_HT
: Hash_Table_Type
;
503 L_Node
: Node_Access
) return Boolean
505 R_Index
: constant Hash_Type
:= Key_Ops
.Index
(R_HT
, L_Node
.Key
);
506 R_Node
: Node_Access
:= R_HT
.Buckets
(R_Index
);
509 while R_Node
/= null loop
510 if Equivalent_Keys
(L_Node
.Key
, R_Node
.Key
) then
511 return L_Node
.Element
= R_Node
.Element
;
514 R_Node
:= R_Node
.Next
;
524 function First
(Container
: Map
) return Cursor
is
525 Node
: constant Node_Access
:= HT_Ops
.First
(Container
.HT
);
532 return Cursor
'(Container'Unrestricted_Access, Node);
535 function First (Object : Iterator) return Cursor is
537 return Object.Container.First;
544 procedure Free (X : in out Node_Access) is
545 procedure Deallocate is
546 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
549 X.Next := X; -- detect mischief (in Vet)
558 function Has_Element (Position : Cursor) return Boolean is
560 pragma Assert (Vet (Position), "bad cursor in Has_Element");
561 return Position.Node /= null;
568 function Hash_Node (Node : Node_Access) return Hash_Type is
570 return Hash (Node.Key);
578 (Container : in out Map;
580 New_Item : Element_Type)
586 Insert (Container, Key, New_Item, Position, Inserted);
589 if Container.HT.Lock > 0 then
590 raise Program_Error with
591 "Include attempted to tamper with elements (map is locked)";
594 Position.Node.Key := Key;
595 Position.Node.Element := New_Item;
604 (Container : in out Map;
606 Position : out Cursor;
607 Inserted : out Boolean)
609 function New_Node (Next : Node_Access) return Node_Access;
610 pragma Inline (New_Node);
612 procedure Local_Insert is
613 new Key_Ops.Generic_Conditional_Insert (New_Node);
619 function New_Node (Next : Node_Access) return Node_Access is
621 return new Node_Type'(Key
=> Key
,
626 HT
: Hash_Table_Type
renames Container
.HT
;
628 -- Start of processing for Insert
631 if HT_Ops
.Capacity
(HT
) = 0 then
632 HT_Ops
.Reserve_Capacity
(HT
, 1);
635 Local_Insert
(HT
, Key
, Position
.Node
, Inserted
);
638 and then HT
.Length
> HT_Ops
.Capacity
(HT
)
640 HT_Ops
.Reserve_Capacity
(HT
, HT
.Length
);
643 Position
.Container
:= Container
'Unrestricted_Access;
647 (Container
: in out Map
;
649 New_Item
: Element_Type
;
650 Position
: out Cursor
;
651 Inserted
: out Boolean)
653 function New_Node
(Next
: Node_Access
) return Node_Access
;
654 pragma Inline
(New_Node
);
656 procedure Local_Insert
is
657 new Key_Ops
.Generic_Conditional_Insert
(New_Node
);
663 function New_Node
(Next
: Node_Access
) return Node_Access
is
665 return new Node_Type
'(Key, New_Item, Next);
668 HT : Hash_Table_Type renames Container.HT;
670 -- Start of processing for Insert
673 if HT_Ops.Capacity (HT) = 0 then
674 HT_Ops.Reserve_Capacity (HT, 1);
677 Local_Insert (HT, Key, Position.Node, Inserted);
680 and then HT.Length > HT_Ops.Capacity (HT)
682 HT_Ops.Reserve_Capacity (HT, HT.Length);
685 Position.Container := Container'Unrestricted_Access;
689 (Container : in out Map;
691 New_Item : Element_Type)
694 pragma Unreferenced (Position);
699 Insert (Container, Key, New_Item, Position, Inserted);
702 raise Constraint_Error with
703 "attempt to insert key already in map";
711 function Is_Empty (Container : Map) return Boolean is
713 return Container.HT.Length = 0;
722 Process : not null access procedure (Position : Cursor))
724 procedure Process_Node (Node : Node_Access);
725 pragma Inline (Process_Node);
727 procedure Local_Iterate is new HT_Ops.Generic_Iteration (Process_Node);
733 procedure Process_Node (Node : Node_Access) is
735 Process (Cursor'(Container
'Unrestricted_Access, Node
));
738 B
: Natural renames Container
'Unrestricted_Access.all.HT
.Busy
;
740 -- Start of processing for Iterate
746 Local_Iterate
(Container
.HT
);
757 (Container
: Map
) return Map_Iterator_Interfaces
.Forward_Iterator
'Class
759 B
: Natural renames Container
'Unrestricted_Access.all.HT
.Busy
;
761 return It
: constant Iterator
:=
762 (Limited_Controlled
with Container
=> Container
'Unrestricted_Access)
772 function Key
(Position
: Cursor
) return Key_Type
is
774 if Position
.Node
= null then
775 raise Constraint_Error
with
776 "Position cursor of function Key equals No_Element";
779 pragma Assert
(Vet
(Position
), "bad cursor in function Key");
781 return Position
.Node
.Key
;
788 function Length
(Container
: Map
) return Count_Type
is
790 return Container
.HT
.Length
;
798 (Target
: in out Map
;
802 HT_Ops
.Move
(Target
=> Target
.HT
, Source
=> Source
.HT
);
809 function Next
(Node
: Node_Access
) return Node_Access
is
814 function Next
(Position
: Cursor
) return Cursor
is
816 if Position
.Node
= null then
820 pragma Assert
(Vet
(Position
), "bad cursor in function Next");
823 HT
: Hash_Table_Type
renames Position
.Container
.HT
;
824 Node
: constant Node_Access
:= HT_Ops
.Next
(HT
, Position
.Node
);
831 return Cursor
'(Position.Container, Node);
835 procedure Next (Position : in out Cursor) is
837 Position := Next (Position);
842 Position : Cursor) return Cursor
845 if Position.Container = null then
849 if Position.Container /= Object.Container then
850 raise Program_Error with
851 "Position cursor of Next designates wrong map";
854 return Next (Position);
861 procedure Query_Element
863 Process : not null access
864 procedure (Key : Key_Type; Element : Element_Type))
867 if Position.Node = null then
868 raise Constraint_Error with
869 "Position cursor of Query_Element equals No_Element";
872 pragma Assert (Vet (Position), "bad cursor in Query_Element");
875 M : Map renames Position.Container.all;
876 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
878 B : Natural renames HT.Busy;
879 L : Natural renames HT.Lock;
886 K : Key_Type renames Position.Node.Key;
887 E : Element_Type renames Position.Node.Element;
908 (Stream : not null access Root_Stream_Type'Class;
912 Read_Nodes (Stream, Container.HT);
916 (Stream : not null access Root_Stream_Type'Class;
920 raise Program_Error with "attempt to stream map cursor";
924 (Stream : not null access Root_Stream_Type'Class;
925 Item : out Reference_Type)
928 raise Program_Error with "attempt to stream reference";
932 (Stream : not null access Root_Stream_Type'Class;
933 Item : out Constant_Reference_Type)
936 raise Program_Error with "attempt to stream reference";
944 (Container : aliased in out Map;
945 Position : Cursor) return Reference_Type
948 if Position.Container = null then
949 raise Constraint_Error with
950 "Position cursor has no element";
953 if Position.Container /= Container'Unrestricted_Access then
954 raise Program_Error with
955 "Position cursor designates wrong map";
960 "Position cursor in function Reference is bad");
963 HT : Hash_Table_Type renames Container'Unrestricted_Access.all.HT;
964 B : Natural renames HT.Busy;
965 L : Natural renames HT.Lock;
967 return R : constant Reference_Type :=
968 (Element => Position.Node.Element'Access,
969 Control => (Controlled with Position.Container))
978 (Container : aliased in out Map;
979 Key : Key_Type) return Reference_Type
981 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
985 raise Constraint_Error with "key not in map";
989 HT : Hash_Table_Type renames Container'Unrestricted_Access.all.HT;
990 B : Natural renames HT.Busy;
991 L : Natural renames HT.Lock;
993 return R : constant Reference_Type :=
994 (Element => Node.Element'Access,
995 Control => (Controlled with Container'Unrestricted_Access))
1008 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1010 Node : Node_Access := new Node_Type;
1013 Key_Type'Read (Stream, Node.Key);
1014 Element_Type'Read (Stream, Node.Element);
1028 (Container : in out Map;
1030 New_Item : Element_Type)
1032 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
1036 raise Constraint_Error with
1037 "attempt to replace key not in map";
1040 if Container.HT.Lock > 0 then
1041 raise Program_Error with
1042 "Replace attempted to tamper with elements (map is locked)";
1046 Node.Element := New_Item;
1049 ---------------------
1050 -- Replace_Element --
1051 ---------------------
1053 procedure Replace_Element
1054 (Container : in out Map;
1056 New_Item : Element_Type)
1059 if Position.Node = null then
1060 raise Constraint_Error with
1061 "Position cursor of Replace_Element equals No_Element";
1064 if Position.Container /= Container'Unrestricted_Access then
1065 raise Program_Error with
1066 "Position cursor of Replace_Element designates wrong map";
1069 if Position.Container.HT.Lock > 0 then
1070 raise Program_Error with
1071 "Replace_Element attempted to tamper with elements (map is locked)";
1074 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
1076 Position.Node.Element := New_Item;
1077 end Replace_Element;
1079 ----------------------
1080 -- Reserve_Capacity --
1081 ----------------------
1083 procedure Reserve_Capacity
1084 (Container : in out Map;
1085 Capacity : Count_Type)
1088 HT_Ops.Reserve_Capacity (Container.HT, Capacity);
1089 end Reserve_Capacity;
1095 procedure Set_Next (Node : Node_Access; Next : Node_Access) is
1100 --------------------
1101 -- Update_Element --
1102 --------------------
1104 procedure Update_Element
1105 (Container : in out Map;
1107 Process : not null access procedure (Key : Key_Type;
1108 Element : in out Element_Type))
1111 if Position.Node = null then
1112 raise Constraint_Error with
1113 "Position cursor of Update_Element equals No_Element";
1116 if Position.Container /= Container'Unrestricted_Access then
1117 raise Program_Error with
1118 "Position cursor of Update_Element designates wrong map";
1121 pragma Assert (Vet (Position), "bad cursor in Update_Element");
1124 HT : Hash_Table_Type renames Container.HT;
1125 B : Natural renames HT.Busy;
1126 L : Natural renames HT.Lock;
1133 K : Key_Type renames Position.Node.Key;
1134 E : Element_Type renames Position.Node.Element;
1155 function Vet (Position : Cursor) return Boolean is
1157 if Position.Node = null then
1158 return Position.Container = null;
1161 if Position.Container = null then
1165 if Position.Node.Next = Position.Node then
1170 HT : Hash_Table_Type renames Position.Container.HT;
1174 if HT.Length = 0 then
1178 if HT.Buckets = null
1179 or else HT.Buckets'Length = 0
1184 X := HT.Buckets (Key_Ops.Index (HT, Position.Node.Key));
1186 for J in 1 .. HT.Length loop
1187 if X = Position.Node then
1195 if X = X.Next then -- to prevent unnecessary looping
1211 (Stream : not null access Root_Stream_Type'Class;
1215 Write_Nodes (Stream, Container.HT);
1219 (Stream : not null access Root_Stream_Type'Class;
1223 raise Program_Error with "attempt to stream map cursor";
1227 (Stream : not null access Root_Stream_Type'Class;
1228 Item : Reference_Type)
1231 raise Program_Error with "attempt to stream reference";
1235 (Stream : not null access Root_Stream_Type'Class;
1236 Item : Constant_Reference_Type)
1239 raise Program_Error with "attempt to stream reference";
1246 procedure Write_Node
1247 (Stream : not null access Root_Stream_Type'Class;
1251 Key_Type'Write (Stream, Node.Key);
1252 Element_Type'Write (Stream, Node.Element);
1255 end Ada.Containers.Hashed_Maps;