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-2009, 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 package body Ada
.Containers
.Hashed_Maps
is
40 -----------------------
41 -- Local Subprograms --
42 -----------------------
45 (Source
: Node_Access
) return Node_Access
;
46 pragma Inline
(Copy_Node
);
48 function Equivalent_Key_Node
50 Node
: Node_Access
) return Boolean;
51 pragma Inline
(Equivalent_Key_Node
);
53 procedure Free
(X
: in out Node_Access
);
55 function Find_Equal_Key
56 (R_HT
: Hash_Table_Type
;
57 L_Node
: Node_Access
) return Boolean;
59 function Hash_Node
(Node
: Node_Access
) return Hash_Type
;
60 pragma Inline
(Hash_Node
);
62 function Next
(Node
: Node_Access
) return Node_Access
;
66 (Stream
: not null access Root_Stream_Type
'Class) return Node_Access
;
67 pragma Inline
(Read_Node
);
69 procedure Set_Next
(Node
: Node_Access
; Next
: Node_Access
);
70 pragma Inline
(Set_Next
);
72 function Vet
(Position
: Cursor
) return Boolean;
75 (Stream
: not null access Root_Stream_Type
'Class;
77 pragma Inline
(Write_Node
);
79 --------------------------
80 -- Local Instantiations --
81 --------------------------
83 package HT_Ops
is new Hash_Tables
.Generic_Operations
84 (HT_Types
=> HT_Types
,
85 Hash_Node
=> Hash_Node
,
88 Copy_Node
=> Copy_Node
,
91 package Key_Ops
is new Hash_Tables
.Generic_Keys
92 (HT_Types
=> HT_Types
,
97 Equivalent_Keys
=> Equivalent_Key_Node
);
99 function Is_Equal
is new HT_Ops
.Generic_Equal
(Find_Equal_Key
);
101 procedure Read_Nodes
is new HT_Ops
.Generic_Read
(Read_Node
);
102 procedure Write_Nodes
is new HT_Ops
.Generic_Write
(Write_Node
);
108 function "=" (Left
, Right
: Map
) return Boolean is
110 return Is_Equal
(Left
.HT
, Right
.HT
);
117 procedure Adjust
(Container
: in out Map
) is
119 HT_Ops
.Adjust
(Container
.HT
);
126 function Capacity
(Container
: Map
) return Count_Type
is
128 return HT_Ops
.Capacity
(Container
.HT
);
135 procedure Clear
(Container
: in out Map
) is
137 HT_Ops
.Clear
(Container
.HT
);
144 function Contains
(Container
: Map
; Key
: Key_Type
) return Boolean is
146 return Find
(Container
, Key
) /= No_Element
;
154 (Source
: Node_Access
) return Node_Access
156 Target
: constant Node_Access
:=
157 new Node_Type
'(Key => Source.Key,
158 Element => Source.Element,
168 procedure Delete (Container : in out Map; Key : Key_Type) is
172 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
175 raise Constraint_Error with "attempt to delete key not in map";
181 procedure Delete (Container : in out Map; Position : in out Cursor) is
183 if Position.Node = null then
184 raise Constraint_Error with
185 "Position cursor of Delete equals No_Element";
188 if Position.Container /= Container'Unrestricted_Access then
189 raise Program_Error with
190 "Position cursor of Delete designates wrong map";
193 if Container.HT.Busy > 0 then
194 raise Program_Error with
195 "Delete attempted to tamper with elements (map is busy)";
198 pragma Assert (Vet (Position), "bad cursor in Delete");
200 HT_Ops.Delete_Node_Sans_Free (Container.HT, Position.Node);
202 Free (Position.Node);
203 Position.Container := null;
210 function Element (Container : Map; Key : Key_Type) return Element_Type is
211 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
215 raise Constraint_Error with
216 "no element available because key not in map";
222 function Element (Position : Cursor) return Element_Type is
224 if Position.Node = null then
225 raise Constraint_Error with
226 "Position cursor of function Element equals No_Element";
229 pragma Assert (Vet (Position), "bad cursor in function Element");
231 return Position.Node.Element;
234 -------------------------
235 -- Equivalent_Key_Node --
236 -------------------------
238 function Equivalent_Key_Node
240 Node : Node_Access) return Boolean is
242 return Equivalent_Keys (Key, Node.Key);
243 end Equivalent_Key_Node;
245 ---------------------
246 -- Equivalent_Keys --
247 ---------------------
249 function Equivalent_Keys (Left, Right : Cursor)
252 if Left.Node = null then
253 raise Constraint_Error with
254 "Left cursor of Equivalent_Keys equals No_Element";
257 if Right.Node = null then
258 raise Constraint_Error with
259 "Right cursor of Equivalent_Keys equals No_Element";
262 pragma Assert (Vet (Left), "Left cursor of Equivalent_Keys is bad");
263 pragma Assert (Vet (Right), "Right cursor of Equivalent_Keys is bad");
265 return Equivalent_Keys (Left.Node.Key, Right.Node.Key);
268 function Equivalent_Keys (Left : Cursor; Right : Key_Type) return Boolean is
270 if Left.Node = null then
271 raise Constraint_Error with
272 "Left cursor of Equivalent_Keys equals No_Element";
275 pragma Assert (Vet (Left), "Left cursor in Equivalent_Keys is bad");
277 return Equivalent_Keys (Left.Node.Key, Right);
280 function Equivalent_Keys (Left : Key_Type; Right : Cursor) return Boolean is
282 if Right.Node = null then
283 raise Constraint_Error with
284 "Right cursor of Equivalent_Keys equals No_Element";
287 pragma Assert (Vet (Right), "Right cursor of Equivalent_Keys is bad");
289 return Equivalent_Keys (Left, Right.Node.Key);
296 procedure Exclude (Container : in out Map; Key : Key_Type) is
299 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
307 procedure Finalize (Container : in out Map) is
309 HT_Ops.Finalize (Container.HT);
316 function Find (Container : Map; Key : Key_Type) return Cursor is
317 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
324 return Cursor'(Container
'Unchecked_Access, Node
);
331 function Find_Equal_Key
332 (R_HT
: Hash_Table_Type
;
333 L_Node
: Node_Access
) return Boolean
335 R_Index
: constant Hash_Type
:= Key_Ops
.Index
(R_HT
, L_Node
.Key
);
336 R_Node
: Node_Access
:= R_HT
.Buckets
(R_Index
);
339 while R_Node
/= null loop
340 if Equivalent_Keys
(L_Node
.Key
, R_Node
.Key
) then
341 return L_Node
.Element
= R_Node
.Element
;
344 R_Node
:= R_Node
.Next
;
354 function First
(Container
: Map
) return Cursor
is
355 Node
: constant Node_Access
:= HT_Ops
.First
(Container
.HT
);
362 return Cursor
'(Container'Unchecked_Access, Node);
369 procedure Free (X : in out Node_Access) is
370 procedure Deallocate is
371 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
374 X.Next := X; -- detect mischief (in Vet)
383 function Has_Element (Position : Cursor) return Boolean is
385 pragma Assert (Vet (Position), "bad cursor in Has_Element");
386 return Position.Node /= null;
393 function Hash_Node (Node : Node_Access) return Hash_Type is
395 return Hash (Node.Key);
403 (Container : in out Map;
405 New_Item : Element_Type)
411 Insert (Container, Key, New_Item, Position, Inserted);
414 if Container.HT.Lock > 0 then
415 raise Program_Error with
416 "Include attempted to tamper with cursors (map is locked)";
419 Position.Node.Key := Key;
420 Position.Node.Element := New_Item;
429 (Container : in out Map;
431 Position : out Cursor;
432 Inserted : out Boolean)
434 function New_Node (Next : Node_Access) return Node_Access;
435 pragma Inline (New_Node);
437 procedure Local_Insert is
438 new Key_Ops.Generic_Conditional_Insert (New_Node);
444 function New_Node (Next : Node_Access) return Node_Access is
446 return new Node_Type'(Key
=> Key
,
451 HT
: Hash_Table_Type
renames Container
.HT
;
453 -- Start of processing for Insert
456 if HT_Ops
.Capacity
(HT
) = 0 then
457 HT_Ops
.Reserve_Capacity
(HT
, 1);
460 Local_Insert
(HT
, Key
, Position
.Node
, Inserted
);
463 and then HT
.Length
> HT_Ops
.Capacity
(HT
)
465 HT_Ops
.Reserve_Capacity
(HT
, HT
.Length
);
468 Position
.Container
:= Container
'Unchecked_Access;
472 (Container
: in out Map
;
474 New_Item
: Element_Type
;
475 Position
: out Cursor
;
476 Inserted
: out Boolean)
478 function New_Node
(Next
: Node_Access
) return Node_Access
;
479 pragma Inline
(New_Node
);
481 procedure Local_Insert
is
482 new Key_Ops
.Generic_Conditional_Insert
(New_Node
);
488 function New_Node
(Next
: Node_Access
) return Node_Access
is
490 return new Node_Type
'(Key, New_Item, Next);
493 HT : Hash_Table_Type renames Container.HT;
495 -- Start of processing for Insert
498 if HT_Ops.Capacity (HT) = 0 then
499 HT_Ops.Reserve_Capacity (HT, 1);
502 Local_Insert (HT, Key, Position.Node, Inserted);
505 and then HT.Length > HT_Ops.Capacity (HT)
507 HT_Ops.Reserve_Capacity (HT, HT.Length);
510 Position.Container := Container'Unchecked_Access;
514 (Container : in out Map;
516 New_Item : Element_Type)
519 pragma Unreferenced (Position);
524 Insert (Container, Key, New_Item, Position, Inserted);
527 raise Constraint_Error with
528 "attempt to insert key already in map";
536 function Is_Empty (Container : Map) return Boolean is
538 return Container.HT.Length = 0;
547 Process : not null access procedure (Position : Cursor))
549 procedure Process_Node (Node : Node_Access);
550 pragma Inline (Process_Node);
552 procedure Local_Iterate is new HT_Ops.Generic_Iteration (Process_Node);
558 procedure Process_Node (Node : Node_Access) is
560 Process (Cursor'(Container
'Unchecked_Access, Node
));
563 B
: Natural renames Container
'Unrestricted_Access.HT
.Busy
;
565 -- Start of processing for Iterate
571 Local_Iterate
(Container
.HT
);
585 function Key
(Position
: Cursor
) return Key_Type
is
587 if Position
.Node
= null then
588 raise Constraint_Error
with
589 "Position cursor of function Key equals No_Element";
592 pragma Assert
(Vet
(Position
), "bad cursor in function Key");
594 return Position
.Node
.Key
;
601 function Length
(Container
: Map
) return Count_Type
is
603 return Container
.HT
.Length
;
611 (Target
: in out Map
;
615 HT_Ops
.Move
(Target
=> Target
.HT
, Source
=> Source
.HT
);
622 function Next
(Node
: Node_Access
) return Node_Access
is
627 function Next
(Position
: Cursor
) return Cursor
is
629 if Position
.Node
= null then
633 pragma Assert
(Vet
(Position
), "bad cursor in function Next");
636 HT
: Hash_Table_Type
renames Position
.Container
.HT
;
637 Node
: constant Node_Access
:= HT_Ops
.Next
(HT
, Position
.Node
);
644 return Cursor
'(Position.Container, Node);
648 procedure Next (Position : in out Cursor) is
650 Position := Next (Position);
657 procedure Query_Element
659 Process : not null access
660 procedure (Key : Key_Type; Element : Element_Type))
663 if Position.Node = null then
664 raise Constraint_Error with
665 "Position cursor of Query_Element equals No_Element";
668 pragma Assert (Vet (Position), "bad cursor in Query_Element");
671 M : Map renames Position.Container.all;
672 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
674 B : Natural renames HT.Busy;
675 L : Natural renames HT.Lock;
682 K : Key_Type renames Position.Node.Key;
683 E : Element_Type renames Position.Node.Element;
704 (Stream : not null access Root_Stream_Type'Class;
708 Read_Nodes (Stream, Container.HT);
712 (Stream : not null access Root_Stream_Type'Class;
716 raise Program_Error with "attempt to stream map cursor";
724 (Stream : not null access Root_Stream_Type'Class) return Node_Access
726 Node : Node_Access := new Node_Type;
729 Key_Type'Read (Stream, Node.Key);
730 Element_Type'Read (Stream, Node.Element);
744 (Container : in out Map;
746 New_Item : Element_Type)
748 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
752 raise Constraint_Error with
753 "attempt to replace key not in map";
756 if Container.HT.Lock > 0 then
757 raise Program_Error with
758 "Replace attempted to tamper with cursors (map is locked)";
762 Node.Element := New_Item;
765 ---------------------
766 -- Replace_Element --
767 ---------------------
769 procedure Replace_Element
770 (Container : in out Map;
772 New_Item : Element_Type)
775 if Position.Node = null then
776 raise Constraint_Error with
777 "Position cursor of Replace_Element equals No_Element";
780 if Position.Container /= Container'Unrestricted_Access then
781 raise Program_Error with
782 "Position cursor of Replace_Element designates wrong map";
785 if Position.Container.HT.Lock > 0 then
786 raise Program_Error with
787 "Replace_Element attempted to tamper with cursors (map is locked)";
790 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
792 Position.Node.Element := New_Item;
795 ----------------------
796 -- Reserve_Capacity --
797 ----------------------
799 procedure Reserve_Capacity
800 (Container : in out Map;
801 Capacity : Count_Type)
804 HT_Ops.Reserve_Capacity (Container.HT, Capacity);
805 end Reserve_Capacity;
811 procedure Set_Next (Node : Node_Access; Next : Node_Access) is
820 procedure Update_Element
821 (Container : in out Map;
823 Process : not null access procedure (Key : Key_Type;
824 Element : in out Element_Type))
827 if Position.Node = null then
828 raise Constraint_Error with
829 "Position cursor of Update_Element equals No_Element";
832 if Position.Container /= Container'Unrestricted_Access then
833 raise Program_Error with
834 "Position cursor of Update_Element designates wrong map";
837 pragma Assert (Vet (Position), "bad cursor in Update_Element");
840 HT : Hash_Table_Type renames Container.HT;
841 B : Natural renames HT.Busy;
842 L : Natural renames HT.Lock;
849 K : Key_Type renames Position.Node.Key;
850 E : Element_Type renames Position.Node.Element;
871 function Vet (Position : Cursor) return Boolean is
873 if Position.Node = null then
874 return Position.Container = null;
877 if Position.Container = null then
881 if Position.Node.Next = Position.Node then
886 HT : Hash_Table_Type renames Position.Container.HT;
890 if HT.Length = 0 then
895 or else HT.Buckets'Length = 0
900 X := HT.Buckets (Key_Ops.Index (HT, Position.Node.Key));
902 for J in 1 .. HT.Length loop
903 if X = Position.Node then
911 if X = X.Next then -- to prevent endless loop
927 (Stream : not null access Root_Stream_Type'Class;
931 Write_Nodes (Stream, Container.HT);
935 (Stream : not null access Root_Stream_Type'Class;
939 raise Program_Error with "attempt to stream map cursor";
947 (Stream : not null access Root_Stream_Type'Class;
951 Key_Type'Write (Stream, Node.Key);
952 Element_Type'Write (Stream, Node.Element);
955 end Ada.Containers.Hashed_Maps;