1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . --
6 -- I N D E F I N I T E _ H A S H E D _ M A P S --
10 -- Copyright (C) 2004-2007, Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
21 -- Boston, MA 02110-1301, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- This unit has originally being developed by Matthew J Heaney. --
31 ------------------------------------------------------------------------------
33 with Ada
.Containers
.Hash_Tables
.Generic_Operations
;
34 pragma Elaborate_All
(Ada
.Containers
.Hash_Tables
.Generic_Operations
);
36 with Ada
.Containers
.Hash_Tables
.Generic_Keys
;
37 pragma Elaborate_All
(Ada
.Containers
.Hash_Tables
.Generic_Keys
);
39 with Ada
.Unchecked_Deallocation
;
41 package body Ada
.Containers
.Indefinite_Hashed_Maps
is
44 new Ada
.Unchecked_Deallocation
(Key_Type
, Key_Access
);
46 procedure Free_Element
is
47 new Ada
.Unchecked_Deallocation
(Element_Type
, Element_Access
);
49 -----------------------
50 -- Local Subprograms --
51 -----------------------
53 function Copy_Node
(Node
: 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 function Find_Equal_Key
62 (R_HT
: Hash_Table_Type
;
63 L_Node
: Node_Access
) return Boolean;
65 procedure Free
(X
: in out Node_Access
);
66 -- pragma Inline (Free);
68 function Hash_Node
(Node
: Node_Access
) return Hash_Type
;
69 pragma Inline
(Hash_Node
);
71 function Next
(Node
: Node_Access
) return Node_Access
;
75 (Stream
: not null access Root_Stream_Type
'Class) return Node_Access
;
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;
86 --------------------------
87 -- Local Instantiations --
88 --------------------------
91 new Ada
.Containers
.Hash_Tables
.Generic_Operations
92 (HT_Types
=> HT_Types
,
93 Hash_Node
=> Hash_Node
,
96 Copy_Node
=> Copy_Node
,
100 new Hash_Tables
.Generic_Keys
101 (HT_Types
=> HT_Types
,
103 Set_Next
=> Set_Next
,
104 Key_Type
=> Key_Type
,
106 Equivalent_Keys
=> Equivalent_Key_Node
);
112 function Is_Equal
is new HT_Ops
.Generic_Equal
(Find_Equal_Key
);
114 function "=" (Left
, Right
: Map
) return Boolean is
116 return Is_Equal
(Left
.HT
, Right
.HT
);
123 procedure Adjust
(Container
: in out Map
) is
125 HT_Ops
.Adjust
(Container
.HT
);
132 function Capacity
(Container
: Map
) return Count_Type
is
134 return HT_Ops
.Capacity
(Container
.HT
);
141 procedure Clear
(Container
: in out Map
) is
143 HT_Ops
.Clear
(Container
.HT
);
150 function Contains
(Container
: Map
; Key
: Key_Type
) return Boolean is
152 return Find
(Container
, Key
) /= No_Element
;
159 function Copy_Node
(Node
: Node_Access
) return Node_Access
is
160 K
: Key_Access
:= new Key_Type
'(Node.Key.all);
164 E := new Element_Type'(Node
.Element
.all);
165 return new Node_Type
'(K, E, null);
178 procedure Delete (Container : in out Map; Key : Key_Type) is
182 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
185 raise Constraint_Error with "attempt to delete key not in map";
191 procedure Delete (Container : in out Map; Position : in out Cursor) is
193 if Position.Node = null then
194 raise Constraint_Error with
195 "Position cursor of Delete equals No_Element";
198 if Position.Container /= Container'Unrestricted_Access then
199 raise Program_Error with
200 "Position cursor of Delete designates wrong map";
203 if Container.HT.Busy > 0 then
204 raise Program_Error with
205 "Delete attempted to tamper with elements (map is busy)";
208 pragma Assert (Vet (Position), "bad cursor in Delete");
210 HT_Ops.Delete_Node_Sans_Free (Container.HT, Position.Node);
212 Free (Position.Node);
213 Position.Container := null;
220 function Element (Container : Map; Key : Key_Type) return Element_Type is
221 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
225 raise Constraint_Error with
226 "no element available because key not in map";
229 return Node.Element.all;
232 function Element (Position : Cursor) return Element_Type is
234 if Position.Node = null then
235 raise Constraint_Error with
236 "Position cursor of function Element equals No_Element";
239 if Position.Node.Element = null then
240 raise Program_Error with
241 "Position cursor of function Element is bad";
244 pragma Assert (Vet (Position), "bad cursor in function Element");
246 return Position.Node.Element.all;
249 -------------------------
250 -- Equivalent_Key_Node --
251 -------------------------
253 function Equivalent_Key_Node
255 Node : Node_Access) return Boolean
258 return Equivalent_Keys (Key, Node.Key.all);
259 end Equivalent_Key_Node;
261 ---------------------
262 -- Equivalent_Keys --
263 ---------------------
265 function Equivalent_Keys (Left, Right : Cursor) return Boolean is
267 if Left.Node = null then
268 raise Constraint_Error with
269 "Left cursor of Equivalent_Keys equals No_Element";
272 if Right.Node = null then
273 raise Constraint_Error with
274 "Right cursor of Equivalent_Keys equals No_Element";
277 if Left.Node.Key = null then
278 raise Program_Error with
279 "Left cursor of Equivalent_Keys is bad";
282 if Right.Node.Key = null then
283 raise Program_Error with
284 "Right cursor of Equivalent_Keys is bad";
287 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Keys");
288 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Keys");
290 return Equivalent_Keys (Left.Node.Key.all, Right.Node.Key.all);
293 function Equivalent_Keys
295 Right : Key_Type) return Boolean
298 if Left.Node = null then
299 raise Constraint_Error with
300 "Left cursor of Equivalent_Keys equals No_Element";
303 if Left.Node.Key = null then
304 raise Program_Error with
305 "Left cursor of Equivalent_Keys is bad";
308 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Keys");
310 return Equivalent_Keys (Left.Node.Key.all, Right);
313 function Equivalent_Keys
315 Right : Cursor) return Boolean
318 if Right.Node = null then
319 raise Constraint_Error with
320 "Right cursor of Equivalent_Keys equals No_Element";
323 if Right.Node.Key = null then
324 raise Program_Error with
325 "Right cursor of Equivalent_Keys is bad";
328 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Keys");
330 return Equivalent_Keys (Left, Right.Node.Key.all);
337 procedure Exclude (Container : in out Map; Key : Key_Type) is
340 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
348 procedure Finalize (Container : in out Map) is
350 HT_Ops.Finalize (Container.HT);
357 function Find (Container : Map; Key : Key_Type) return Cursor is
358 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
365 return Cursor'(Container
'Unchecked_Access, Node
);
372 function Find_Equal_Key
373 (R_HT
: Hash_Table_Type
;
374 L_Node
: Node_Access
) return Boolean
376 R_Index
: constant Hash_Type
:= Key_Ops
.Index
(R_HT
, L_Node
.Key
.all);
377 R_Node
: Node_Access
:= R_HT
.Buckets
(R_Index
);
380 while R_Node
/= null loop
381 if Equivalent_Keys
(L_Node
.Key
.all, R_Node
.Key
.all) then
382 return L_Node
.Element
.all = R_Node
.Element
.all;
385 R_Node
:= R_Node
.Next
;
395 function First
(Container
: Map
) return Cursor
is
396 Node
: constant Node_Access
:= HT_Ops
.First
(Container
.HT
);
403 return Cursor
'(Container'Unchecked_Access, Node);
410 procedure Free (X : in out Node_Access) is
411 procedure Deallocate is
412 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
418 X.Next := X; -- detect mischief (in Vet)
427 Free_Element (X.Element);
438 Free_Element (X.Element);
454 function Has_Element (Position : Cursor) return Boolean is
456 pragma Assert (Vet (Position), "bad cursor in Has_Element");
457 return Position.Node /= null;
464 function Hash_Node (Node : Node_Access) return Hash_Type is
466 return Hash (Node.Key.all);
474 (Container : in out Map;
476 New_Item : Element_Type)
485 Insert (Container, Key, New_Item, Position, Inserted);
488 if Container.HT.Lock > 0 then
489 raise Program_Error with
490 "Include attempted to tamper with cursors (map is locked)";
493 K := Position.Node.Key;
494 E := Position.Node.Element;
496 Position.Node.Key := new Key_Type'(Key
);
499 Position
.Node
.Element
:= new Element_Type
'(New_Item);
516 (Container : in out Map;
518 New_Item : Element_Type;
519 Position : out Cursor;
520 Inserted : out Boolean)
522 function New_Node (Next : Node_Access) return Node_Access;
524 procedure Local_Insert is
525 new Key_Ops.Generic_Conditional_Insert (New_Node);
531 function New_Node (Next : Node_Access) return Node_Access is
532 K : Key_Access := new Key_Type'(Key
);
536 E
:= new Element_Type
'(New_Item);
537 return new Node_Type'(K
, E
, Next
);
545 HT
: Hash_Table_Type
renames Container
.HT
;
547 -- Start of processing for Insert
550 if HT_Ops
.Capacity
(HT
) = 0 then
551 HT_Ops
.Reserve_Capacity
(HT
, 1);
554 Local_Insert
(HT
, Key
, Position
.Node
, Inserted
);
557 and then HT
.Length
> HT_Ops
.Capacity
(HT
)
559 HT_Ops
.Reserve_Capacity
(HT
, HT
.Length
);
562 Position
.Container
:= Container
'Unchecked_Access;
566 (Container
: in out Map
;
568 New_Item
: Element_Type
)
571 pragma Unreferenced
(Position
);
576 Insert
(Container
, Key
, New_Item
, Position
, Inserted
);
579 raise Constraint_Error
with
580 "attempt to insert key already in map";
588 function Is_Empty
(Container
: Map
) return Boolean is
590 return Container
.HT
.Length
= 0;
599 Process
: not null access procedure (Position
: Cursor
))
601 procedure Process_Node
(Node
: Node_Access
);
602 pragma Inline
(Process_Node
);
604 procedure Local_Iterate
is
605 new HT_Ops
.Generic_Iteration
(Process_Node
);
611 procedure Process_Node
(Node
: Node_Access
) is
613 Process
(Cursor
'(Container'Unchecked_Access, Node));
616 B : Natural renames Container'Unrestricted_Access.HT.Busy;
618 -- Start of processing Iterate
624 Local_Iterate (Container.HT);
638 function Key (Position : Cursor) return Key_Type is
640 if Position.Node = null then
641 raise Constraint_Error with
642 "Position cursor of function Key equals No_Element";
645 if Position.Node.Key = null then
646 raise Program_Error with
647 "Position cursor of function Key is bad";
650 pragma Assert (Vet (Position), "bad cursor in function Key");
652 return Position.Node.Key.all;
659 function Length (Container : Map) return Count_Type is
661 return Container.HT.Length;
669 (Target : in out Map;
673 HT_Ops.Move (Target => Target.HT, Source => Source.HT);
680 function Next (Node : Node_Access) return Node_Access is
685 procedure Next (Position : in out Cursor) is
687 Position := Next (Position);
690 function Next (Position : Cursor) return Cursor is
692 if Position.Node = null then
696 if Position.Node.Key = null
697 or else Position.Node.Element = null
699 raise Program_Error with "Position cursor of Next is bad";
702 pragma Assert (Vet (Position), "Position cursor of Next is bad");
705 HT : Hash_Table_Type renames Position.Container.HT;
706 Node : constant Node_Access := HT_Ops.Next (HT, Position.Node);
713 return Cursor'(Position
.Container
, Node
);
721 procedure Query_Element
723 Process
: not null access procedure (Key
: Key_Type
;
724 Element
: Element_Type
))
727 if Position
.Node
= null then
728 raise Constraint_Error
with
729 "Position cursor of Query_Element equals No_Element";
732 if Position
.Node
.Key
= null
733 or else Position
.Node
.Element
= null
735 raise Program_Error
with
736 "Position cursor of Query_Element is bad";
739 pragma Assert
(Vet
(Position
), "bad cursor in Query_Element");
742 M
: Map
renames Position
.Container
.all;
743 HT
: Hash_Table_Type
renames M
.HT
'Unrestricted_Access.all;
745 B
: Natural renames HT
.Busy
;
746 L
: Natural renames HT
.Lock
;
753 K
: Key_Type
renames Position
.Node
.Key
.all;
754 E
: Element_Type
renames Position
.Node
.Element
.all;
774 procedure Read_Nodes
is new HT_Ops
.Generic_Read
(Read_Node
);
777 (Stream
: not null access Root_Stream_Type
'Class;
781 Read_Nodes
(Stream
, Container
.HT
);
785 (Stream
: not null access Root_Stream_Type
'Class;
789 raise Program_Error
with "attempt to stream map cursor";
797 (Stream
: not null access Root_Stream_Type
'Class) return Node_Access
799 Node
: Node_Access
:= new Node_Type
;
803 Node
.Key
:= new Key_Type
'(Key_Type'Input (Stream));
811 Node.Element := new Element_Type'(Element_Type
'Input (Stream
));
827 (Container
: in out Map
;
829 New_Item
: Element_Type
)
831 Node
: constant Node_Access
:= Key_Ops
.Find
(Container
.HT
, Key
);
838 raise Constraint_Error
with
839 "attempt to replace key not in map";
842 if Container
.HT
.Lock
> 0 then
843 raise Program_Error
with
844 "Replace attempted to tamper with cursors (map is locked)";
850 Node
.Key
:= new Key_Type
'(Key);
853 Node.Element := new Element_Type'(New_Item
);
864 ---------------------
865 -- Replace_Element --
866 ---------------------
868 procedure Replace_Element
869 (Container
: in out Map
;
871 New_Item
: Element_Type
)
874 if Position
.Node
= null then
875 raise Constraint_Error
with
876 "Position cursor of Replace_Element equals No_Element";
879 if Position
.Node
.Key
= null
880 or else Position
.Node
.Element
= null
882 raise Program_Error
with
883 "Position cursor of Replace_Element is bad";
886 if Position
.Container
/= Container
'Unrestricted_Access then
887 raise Program_Error
with
888 "Position cursor of Replace_Element designates wrong map";
891 if Position
.Container
.HT
.Lock
> 0 then
892 raise Program_Error
with
893 "Replace_Element attempted to tamper with cursors (map is locked)";
896 pragma Assert
(Vet
(Position
), "bad cursor in Replace_Element");
899 X
: Element_Access
:= Position
.Node
.Element
;
902 Position
.Node
.Element
:= new Element_Type
'(New_Item);
907 ----------------------
908 -- Reserve_Capacity --
909 ----------------------
911 procedure Reserve_Capacity
912 (Container : in out Map;
913 Capacity : Count_Type)
916 HT_Ops.Reserve_Capacity (Container.HT, Capacity);
917 end Reserve_Capacity;
923 procedure Set_Next (Node : Node_Access; Next : Node_Access) is
932 procedure Update_Element
933 (Container : in out Map;
935 Process : not null access procedure (Key : Key_Type;
936 Element : in out Element_Type))
939 if Position.Node = null then
940 raise Constraint_Error with
941 "Position cursor of Update_Element equals No_Element";
944 if Position.Node.Key = null
945 or else Position.Node.Element = null
947 raise Program_Error with
948 "Position cursor of Update_Element is bad";
951 if Position.Container /= Container'Unrestricted_Access then
952 raise Program_Error with
953 "Position cursor of Update_Element designates wrong map";
956 pragma Assert (Vet (Position), "bad cursor in Update_Element");
959 HT : Hash_Table_Type renames Container.HT;
961 B : Natural renames HT.Busy;
962 L : Natural renames HT.Lock;
969 K : Key_Type renames Position.Node.Key.all;
971 E : Element_Type renames Position.Node.Element.all;
972 pragma Unreferenced (E);
993 function Vet (Position : Cursor) return Boolean is
995 if Position.Node = null then
996 return Position.Container = null;
999 if Position.Container = null then
1003 if Position.Node.Next = Position.Node then
1007 if Position.Node.Key = null then
1011 if Position.Node.Element = null then
1016 HT : Hash_Table_Type renames Position.Container.HT;
1020 if HT.Length = 0 then
1024 if HT.Buckets = null
1025 or else HT.Buckets'Length = 0
1030 X := HT.Buckets (Key_Ops.Index (HT, Position.Node.Key.all));
1032 for J in 1 .. HT.Length loop
1033 if X = Position.Node then
1041 if X = X.Next then -- to prevent endless loop
1056 procedure Write_Nodes is new HT_Ops.Generic_Write (Write_Node);
1059 (Stream : not null access Root_Stream_Type'Class;
1063 Write_Nodes (Stream, Container.HT);
1067 (Stream : not null access Root_Stream_Type'Class;
1071 raise Program_Error with "attempt to stream map cursor";
1078 procedure Write_Node
1079 (Stream : not null access Root_Stream_Type'Class;
1083 Key_Type'Output (Stream, Node.Key.all);
1084 Element_Type'Output (Stream, Node.Element.all);
1087 end Ada.Containers.Indefinite_Hashed_Maps;