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-2005 Free Software Foundation, Inc. --
11 -- This specification is derived from the Ada Reference Manual for use with --
12 -- GNAT. The copyright notice above, and the license provisions that follow --
13 -- apply solely to the contents of the part following the private keyword. --
15 -- GNAT is free software; you can redistribute it and/or modify it under --
16 -- terms of the GNU General Public License as published by the Free Soft- --
17 -- ware Foundation; either version 2, or (at your option) any later ver- --
18 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
19 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
20 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
21 -- for more details. You should have received a copy of the GNU General --
22 -- Public License distributed with GNAT; see file COPYING. If not, write --
23 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
24 -- Boston, MA 02110-1301, USA. --
26 -- As a special exception, if other files instantiate generics from this --
27 -- unit, or you link this unit with other files to produce an executable, --
28 -- this unit does not by itself cause the resulting executable to be --
29 -- covered by the GNU General Public License. This exception does not --
30 -- however invalidate any other reasons why the executable file might be --
31 -- covered by the GNU Public License. --
33 -- This unit was originally developed by Matthew J Heaney. --
34 ------------------------------------------------------------------------------
36 with Ada
.Unchecked_Deallocation
;
38 with Ada
.Containers
.Hash_Tables
.Generic_Operations
;
39 pragma Elaborate_All
(Ada
.Containers
.Hash_Tables
.Generic_Operations
);
41 with Ada
.Containers
.Hash_Tables
.Generic_Keys
;
42 pragma Elaborate_All
(Ada
.Containers
.Hash_Tables
.Generic_Keys
);
44 package body Ada
.Containers
.Hashed_Maps
is
46 -----------------------
47 -- Local Subprograms --
48 -----------------------
51 (Source
: Node_Access
) return Node_Access
;
52 pragma Inline
(Copy_Node
);
54 function Equivalent_Key_Node
56 Node
: Node_Access
) return Boolean;
57 pragma Inline
(Equivalent_Key_Node
);
59 procedure Free
(X
: in out Node_Access
);
61 function Find_Equal_Key
62 (R_HT
: Hash_Table_Type
;
63 L_Node
: Node_Access
) return Boolean;
65 function Hash_Node
(Node
: Node_Access
) return Hash_Type
;
66 pragma Inline
(Hash_Node
);
68 function Next
(Node
: Node_Access
) return Node_Access
;
72 (Stream
: access Root_Stream_Type
'Class) return Node_Access
;
73 pragma Inline
(Read_Node
);
75 procedure Set_Next
(Node
: Node_Access
; Next
: Node_Access
);
76 pragma Inline
(Set_Next
);
78 function Vet
(Position
: Cursor
) return Boolean;
81 (Stream
: access Root_Stream_Type
'Class;
83 pragma Inline
(Write_Node
);
85 --------------------------
86 -- Local Instantiations --
87 --------------------------
90 new Hash_Tables
.Generic_Operations
91 (HT_Types
=> HT_Types
,
92 Hash_Node
=> Hash_Node
,
95 Copy_Node
=> Copy_Node
,
99 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 function Capacity
(Container
: Map
) return Count_Type
is
136 return HT_Ops
.Capacity
(Container
.HT
);
143 procedure Clear
(Container
: in out Map
) is
145 HT_Ops
.Clear
(Container
.HT
);
152 function Contains
(Container
: Map
; Key
: Key_Type
) return Boolean is
154 return Find
(Container
, Key
) /= No_Element
;
162 (Source
: Node_Access
) return Node_Access
164 Target
: constant Node_Access
:=
165 new Node_Type
'(Key => Source.Key,
166 Element => Source.Element,
176 procedure Delete (Container : in out Map; Key : Key_Type) is
180 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
183 raise Constraint_Error;
189 procedure Delete (Container : in out Map; Position : in out Cursor) is
191 pragma Assert (Vet (Position), "bad cursor in Delete");
193 if Position.Node = null then
194 raise Constraint_Error;
197 if Position.Container /= Container'Unrestricted_Access then
201 if Container.HT.Busy > 0 then
205 HT_Ops.Delete_Node_Sans_Free (Container.HT, Position.Node);
207 Free (Position.Node);
208 Position.Container := null;
215 function Element (Container : Map; Key : Key_Type) return Element_Type is
216 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
220 raise Constraint_Error;
226 function Element (Position : Cursor) return Element_Type is
228 pragma Assert (Vet (Position), "bad cursor in function Element");
230 if Position.Node = null then
231 raise Constraint_Error;
234 return Position.Node.Element;
237 -------------------------
238 -- Equivalent_Key_Node --
239 -------------------------
241 function Equivalent_Key_Node
243 Node : Node_Access) return Boolean is
245 return Equivalent_Keys (Key, Node.Key);
246 end Equivalent_Key_Node;
248 ---------------------
249 -- Equivalent_Keys --
250 ---------------------
252 function Equivalent_Keys (Left, Right : Cursor)
255 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Keys");
256 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Keys");
259 or else Right.Node = null
261 raise Constraint_Error;
264 return Equivalent_Keys (Left.Node.Key, Right.Node.Key);
267 function Equivalent_Keys (Left : Cursor; Right : Key_Type) return Boolean is
269 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Keys");
271 if Left.Node = null then
272 raise Constraint_Error;
275 return Equivalent_Keys (Left.Node.Key, Right);
278 function Equivalent_Keys (Left : Key_Type; Right : Cursor) return Boolean is
280 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Keys");
282 if Right.Node = null then
283 raise Constraint_Error;
286 return Equivalent_Keys (Left, Right.Node.Key);
293 procedure Exclude (Container : in out Map; Key : Key_Type) is
296 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
304 procedure Finalize (Container : in out Map) is
306 HT_Ops.Finalize (Container.HT);
313 function Find (Container : Map; Key : Key_Type) return Cursor is
314 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
321 return Cursor'(Container
'Unchecked_Access, Node
);
328 function Find_Equal_Key
329 (R_HT
: Hash_Table_Type
;
330 L_Node
: Node_Access
) return Boolean
332 R_Index
: constant Hash_Type
:= Key_Ops
.Index
(R_HT
, L_Node
.Key
);
333 R_Node
: Node_Access
:= R_HT
.Buckets
(R_Index
);
336 while R_Node
/= null loop
337 if Equivalent_Keys
(L_Node
.Key
, R_Node
.Key
) then
338 return L_Node
.Element
= R_Node
.Element
;
341 R_Node
:= R_Node
.Next
;
351 function First
(Container
: Map
) return Cursor
is
352 Node
: constant Node_Access
:= HT_Ops
.First
(Container
.HT
);
359 return Cursor
'(Container'Unchecked_Access, Node);
366 procedure Free (X : in out Node_Access) is
367 procedure Deallocate is
368 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
371 X.Next := X; -- detect mischief (in Vet)
380 function Has_Element (Position : Cursor) return Boolean is
382 pragma Assert (Vet (Position), "bad cursor in Has_Element");
383 return Position.Node /= null;
390 function Hash_Node (Node : Node_Access) return Hash_Type is
392 return Hash (Node.Key);
400 (Container : in out Map;
402 New_Item : Element_Type)
408 Insert (Container, Key, New_Item, Position, Inserted);
411 if Container.HT.Lock > 0 then
415 Position.Node.Key := Key;
416 Position.Node.Element := New_Item;
425 (Container : in out Map;
427 Position : out Cursor;
428 Inserted : out Boolean)
430 function New_Node (Next : Node_Access) return Node_Access;
431 pragma Inline (New_Node);
433 procedure Local_Insert is
434 new Key_Ops.Generic_Conditional_Insert (New_Node);
440 function New_Node (Next : Node_Access) return Node_Access is
441 Node : Node_Access := new Node_Type; -- Ada 2005 aggregate possible?
455 HT : Hash_Table_Type renames Container.HT;
457 -- Start of processing for Insert
460 if HT_Ops.Capacity (HT) = 0 then
461 HT_Ops.Reserve_Capacity (HT, 1);
464 Local_Insert (HT, Key, Position.Node, Inserted);
467 and then HT.Length > HT_Ops.Capacity (HT)
469 HT_Ops.Reserve_Capacity (HT, HT.Length);
472 Position.Container := Container'Unchecked_Access;
476 (Container : in out Map;
478 New_Item : Element_Type;
479 Position : out Cursor;
480 Inserted : out Boolean)
482 function New_Node (Next : Node_Access) return Node_Access;
483 pragma Inline (New_Node);
485 procedure Local_Insert is
486 new Key_Ops.Generic_Conditional_Insert (New_Node);
492 function New_Node (Next : Node_Access) return Node_Access is
493 Node : constant Node_Access := new Node_Type'(Key
, New_Item
, Next
);
498 HT
: Hash_Table_Type
renames Container
.HT
;
500 -- Start of processing for Insert
503 if HT_Ops
.Capacity
(HT
) = 0 then
504 HT_Ops
.Reserve_Capacity
(HT
, 1);
507 Local_Insert
(HT
, Key
, Position
.Node
, Inserted
);
510 and then HT
.Length
> HT_Ops
.Capacity
(HT
)
512 HT_Ops
.Reserve_Capacity
(HT
, HT
.Length
);
515 Position
.Container
:= Container
'Unchecked_Access;
519 (Container
: in out Map
;
521 New_Item
: Element_Type
)
527 Insert
(Container
, Key
, New_Item
, Position
, Inserted
);
530 raise Constraint_Error
;
538 function Is_Empty
(Container
: Map
) return Boolean is
540 return Container
.HT
.Length
= 0;
549 Process
: not null access procedure (Position
: Cursor
))
551 procedure Process_Node
(Node
: Node_Access
);
552 pragma Inline
(Process_Node
);
554 procedure Local_Iterate
is new HT_Ops
.Generic_Iteration
(Process_Node
);
560 procedure Process_Node
(Node
: Node_Access
) is
562 Process
(Cursor
'(Container'Unchecked_Access, Node));
565 -- Start of processing for Iterate
568 Local_Iterate (Container.HT);
575 function Key (Position : Cursor) return Key_Type is
577 pragma Assert (Vet (Position), "bad cursor in function Key");
579 if Position.Node = null then
580 raise Constraint_Error;
583 return Position.Node.Key;
590 function Length (Container : Map) return Count_Type is
592 return Container.HT.Length;
600 (Target : in out Map;
604 HT_Ops.Move (Target => Target.HT, Source => Source.HT);
611 function Next (Node : Node_Access) return Node_Access is
616 function Next (Position : Cursor) return Cursor is
618 pragma Assert (Vet (Position), "bad cursor in function Next");
620 if Position.Node = null then
625 HT : Hash_Table_Type renames Position.Container.HT;
626 Node : constant Node_Access := HT_Ops.Next (HT, Position.Node);
632 return Cursor'(Position
.Container
, Node
);
636 procedure Next
(Position
: in out Cursor
) is
638 Position
:= Next
(Position
);
645 procedure Query_Element
647 Process
: not null access
648 procedure (Key
: Key_Type
; Element
: Element_Type
))
651 pragma Assert
(Vet
(Position
), "bad cursor in Query_Element");
653 if Position
.Node
= null then
654 raise Constraint_Error
;
658 M
: Map
renames Position
.Container
.all;
659 HT
: Hash_Table_Type
renames M
.HT
'Unrestricted_Access.all;
661 B
: Natural renames HT
.Busy
;
662 L
: Natural renames HT
.Lock
;
669 K
: Key_Type
renames Position
.Node
.Key
;
670 E
: Element_Type
renames Position
.Node
.Element
;
691 (Stream
: access Root_Stream_Type
'Class;
695 Read_Nodes
(Stream
, Container
.HT
);
703 (Stream
: access Root_Stream_Type
'Class) return Node_Access
705 Node
: Node_Access
:= new Node_Type
;
708 Key_Type
'Read (Stream
, Node
.Key
);
709 Element_Type
'Read (Stream
, Node
.Element
);
723 (Container
: in out Map
;
725 New_Item
: Element_Type
)
727 Node
: constant Node_Access
:= Key_Ops
.Find
(Container
.HT
, Key
);
731 raise Constraint_Error
;
734 if Container
.HT
.Lock
> 0 then
739 Node
.Element
:= New_Item
;
742 ---------------------
743 -- Replace_Element --
744 ---------------------
746 procedure Replace_Element
(Position
: Cursor
; By
: Element_Type
) is
748 pragma Assert
(Vet
(Position
), "bad cursor in Replace_Element");
750 if Position
.Node
= null then
751 raise Constraint_Error
;
754 if Position
.Container
.HT
.Lock
> 0 then
758 Position
.Node
.Element
:= By
;
761 ----------------------
762 -- Reserve_Capacity --
763 ----------------------
765 procedure Reserve_Capacity
766 (Container
: in out Map
;
767 Capacity
: Count_Type
)
770 HT_Ops
.Reserve_Capacity
(Container
.HT
, Capacity
);
771 end Reserve_Capacity
;
777 procedure Set_Next
(Node
: Node_Access
; Next
: Node_Access
) is
786 procedure Update_Element
788 Process
: not null access procedure (Key
: Key_Type
;
789 Element
: in out Element_Type
))
792 pragma Assert
(Vet
(Position
), "bad cursor in Update_Element");
794 if Position
.Node
= null then
795 raise Constraint_Error
;
799 M
: Map
renames Position
.Container
.all;
800 HT
: Hash_Table_Type
renames M
.HT
'Unrestricted_Access.all;
802 B
: Natural renames HT
.Busy
;
803 L
: Natural renames HT
.Lock
;
810 K
: Key_Type
renames Position
.Node
.Key
;
811 E
: Element_Type
renames Position
.Node
.Element
;
831 function Vet
(Position
: Cursor
) return Boolean is
833 if Position
.Node
= null then
834 return Position
.Container
= null;
837 if Position
.Container
= null then
841 if Position
.Node
.Next
= Position
.Node
then
846 HT
: Hash_Table_Type
renames Position
.Container
.HT
;
850 if HT
.Length
= 0 then
855 or else HT
.Buckets
'Length = 0
860 X
:= HT
.Buckets
(Key_Ops
.Index
(HT
, Position
.Node
.Key
));
862 for J
in 1 .. HT
.Length
loop
863 if X
= Position
.Node
then
871 if X
= X
.Next
then -- to prevent endless loop
887 (Stream
: access Root_Stream_Type
'Class;
891 Write_Nodes
(Stream
, Container
.HT
);
899 (Stream
: access Root_Stream_Type
'Class;
903 Key_Type
'Write (Stream
, Node
.Key
);
904 Element_Type
'Write (Stream
, Node
.Element
);
907 end Ada
.Containers
.Hashed_Maps
;