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-2006, 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
: 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
: 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
)
574 Insert
(Container
, Key
, New_Item
, Position
, Inserted
);
577 raise Constraint_Error
with
578 "attempt to insert key already in map";
586 function Is_Empty
(Container
: Map
) return Boolean is
588 return Container
.HT
.Length
= 0;
597 Process
: not null access procedure (Position
: Cursor
))
599 procedure Process_Node
(Node
: Node_Access
);
600 pragma Inline
(Process_Node
);
602 procedure Local_Iterate
is
603 new HT_Ops
.Generic_Iteration
(Process_Node
);
609 procedure Process_Node
(Node
: Node_Access
) is
611 Process
(Cursor
'(Container'Unchecked_Access, Node));
614 B : Natural renames Container'Unrestricted_Access.HT.Busy;
616 -- Start of processing Iterate
622 Local_Iterate (Container.HT);
636 function Key (Position : Cursor) return Key_Type is
638 if Position.Node = null then
639 raise Constraint_Error with
640 "Position cursor of function Key equals No_Element";
643 if Position.Node.Key = null then
644 raise Program_Error with
645 "Position cursor of function Key is bad";
648 pragma Assert (Vet (Position), "bad cursor in function Key");
650 return Position.Node.Key.all;
657 function Length (Container : Map) return Count_Type is
659 return Container.HT.Length;
667 (Target : in out Map;
671 HT_Ops.Move (Target => Target.HT, Source => Source.HT);
678 function Next (Node : Node_Access) return Node_Access is
683 procedure Next (Position : in out Cursor) is
685 Position := Next (Position);
688 function Next (Position : Cursor) return Cursor is
690 if Position.Node = null then
694 if Position.Node.Key = null
695 or else Position.Node.Element = null
697 raise Program_Error with "Position cursor of Next is bad";
700 pragma Assert (Vet (Position), "Position cursor of Next is bad");
703 HT : Hash_Table_Type renames Position.Container.HT;
704 Node : constant Node_Access := HT_Ops.Next (HT, Position.Node);
711 return Cursor'(Position
.Container
, Node
);
719 procedure Query_Element
721 Process
: not null access procedure (Key
: Key_Type
;
722 Element
: Element_Type
))
725 if Position
.Node
= null then
726 raise Constraint_Error
with
727 "Position cursor of Query_Element equals No_Element";
730 if Position
.Node
.Key
= null
731 or else Position
.Node
.Element
= null
733 raise Program_Error
with
734 "Position cursor of Query_Element is bad";
737 pragma Assert
(Vet
(Position
), "bad cursor in Query_Element");
740 M
: Map
renames Position
.Container
.all;
741 HT
: Hash_Table_Type
renames M
.HT
'Unrestricted_Access.all;
743 B
: Natural renames HT
.Busy
;
744 L
: Natural renames HT
.Lock
;
751 K
: Key_Type
renames Position
.Node
.Key
.all;
752 E
: Element_Type
renames Position
.Node
.Element
.all;
772 procedure Read_Nodes
is new HT_Ops
.Generic_Read
(Read_Node
);
775 (Stream
: access Root_Stream_Type
'Class;
779 Read_Nodes
(Stream
, Container
.HT
);
783 (Stream
: access Root_Stream_Type
'Class;
787 raise Program_Error
with "attempt to stream map cursor";
795 (Stream
: access Root_Stream_Type
'Class) return Node_Access
797 Node
: Node_Access
:= new Node_Type
;
801 Node
.Key
:= new Key_Type
'(Key_Type'Input (Stream));
809 Node.Element := new Element_Type'(Element_Type
'Input (Stream
));
825 (Container
: in out Map
;
827 New_Item
: Element_Type
)
829 Node
: constant Node_Access
:= Key_Ops
.Find
(Container
.HT
, Key
);
836 raise Constraint_Error
with
837 "attempt to replace key not in map";
840 if Container
.HT
.Lock
> 0 then
841 raise Program_Error
with
842 "Replace attempted to tamper with cursors (map is locked)";
848 Node
.Key
:= new Key_Type
'(Key);
851 Node.Element := new Element_Type'(New_Item
);
862 ---------------------
863 -- Replace_Element --
864 ---------------------
866 procedure Replace_Element
867 (Container
: in out Map
;
869 New_Item
: Element_Type
)
872 if Position
.Node
= null then
873 raise Constraint_Error
with
874 "Position cursor of Replace_Element equals No_Element";
877 if Position
.Node
.Key
= null
878 or else Position
.Node
.Element
= null
880 raise Program_Error
with
881 "Position cursor of Replace_Element is bad";
884 if Position
.Container
/= Container
'Unrestricted_Access then
885 raise Program_Error
with
886 "Position cursor of Replace_Element designates wrong map";
889 if Position
.Container
.HT
.Lock
> 0 then
890 raise Program_Error
with
891 "Replace_Element attempted to tamper with cursors (map is locked)";
894 pragma Assert
(Vet
(Position
), "bad cursor in Replace_Element");
897 X
: Element_Access
:= Position
.Node
.Element
;
900 Position
.Node
.Element
:= new Element_Type
'(New_Item);
905 ----------------------
906 -- Reserve_Capacity --
907 ----------------------
909 procedure Reserve_Capacity
910 (Container : in out Map;
911 Capacity : Count_Type)
914 HT_Ops.Reserve_Capacity (Container.HT, Capacity);
915 end Reserve_Capacity;
921 procedure Set_Next (Node : Node_Access; Next : Node_Access) is
930 procedure Update_Element
931 (Container : in out Map;
933 Process : not null access procedure (Key : Key_Type;
934 Element : in out Element_Type))
937 if Position.Node = null then
938 raise Constraint_Error with
939 "Position cursor of Update_Element equals No_Element";
942 if Position.Node.Key = null
943 or else Position.Node.Element = null
945 raise Program_Error with
946 "Position cursor of Update_Element is bad";
949 if Position.Container /= Container'Unrestricted_Access then
950 raise Program_Error with
951 "Position cursor of Update_Element designates wrong map";
954 pragma Assert (Vet (Position), "bad cursor in Update_Element");
957 HT : Hash_Table_Type renames Container.HT;
959 B : Natural renames HT.Busy;
960 L : Natural renames HT.Lock;
967 K : Key_Type renames Position.Node.Key.all;
968 E : Element_Type renames Position.Node.Element.all;
987 function Vet (Position : Cursor) return Boolean is
989 if Position.Node = null then
990 return Position.Container = null;
993 if Position.Container = null then
997 if Position.Node.Next = Position.Node then
1001 if Position.Node.Key = null then
1005 if Position.Node.Element = null then
1010 HT : Hash_Table_Type renames Position.Container.HT;
1014 if HT.Length = 0 then
1018 if HT.Buckets = null
1019 or else HT.Buckets'Length = 0
1024 X := HT.Buckets (Key_Ops.Index (HT, Position.Node.Key.all));
1026 for J in 1 .. HT.Length loop
1027 if X = Position.Node then
1035 if X = X.Next then -- to prevent endless loop
1050 procedure Write_Nodes is new HT_Ops.Generic_Write (Write_Node);
1053 (Stream : access Root_Stream_Type'Class;
1057 Write_Nodes (Stream, Container.HT);
1061 (Stream : access Root_Stream_Type'Class;
1065 raise Program_Error with "attempt to stream map cursor";
1072 procedure Write_Node
1073 (Stream : access Root_Stream_Type'Class;
1077 Key_Type'Output (Stream, Node.Key.all);
1078 Element_Type'Output (Stream, Node.Element.all);
1081 end Ada.Containers.Indefinite_Hashed_Maps;