1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.INDEFINITE_HASHED_MAPS --
9 -- Copyright (C) 2004-2010, 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
.Containers
.Hash_Tables
.Generic_Operations
;
31 pragma Elaborate_All
(Ada
.Containers
.Hash_Tables
.Generic_Operations
);
33 with Ada
.Containers
.Hash_Tables
.Generic_Keys
;
34 pragma Elaborate_All
(Ada
.Containers
.Hash_Tables
.Generic_Keys
);
36 with Ada
.Unchecked_Deallocation
;
38 package body Ada
.Containers
.Indefinite_Hashed_Maps
is
41 new Ada
.Unchecked_Deallocation
(Key_Type
, Key_Access
);
43 procedure Free_Element
is
44 new Ada
.Unchecked_Deallocation
(Element_Type
, Element_Access
);
46 -----------------------
47 -- Local Subprograms --
48 -----------------------
50 function Copy_Node
(Node
: Node_Access
) return Node_Access
;
51 pragma Inline
(Copy_Node
);
53 function Equivalent_Key_Node
55 Node
: Node_Access
) return Boolean;
56 pragma Inline
(Equivalent_Key_Node
);
58 function Find_Equal_Key
59 (R_HT
: Hash_Table_Type
;
60 L_Node
: Node_Access
) return Boolean;
62 procedure Free
(X
: in out Node_Access
);
63 -- pragma Inline (Free);
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
: not null access Root_Stream_Type
'Class) return Node_Access
;
74 procedure Set_Next
(Node
: Node_Access
; Next
: Node_Access
);
75 pragma Inline
(Set_Next
);
77 function Vet
(Position
: Cursor
) return Boolean;
80 (Stream
: not null access Root_Stream_Type
'Class;
83 --------------------------
84 -- Local Instantiations --
85 --------------------------
87 package HT_Ops
is new Ada
.Containers
.Hash_Tables
.Generic_Operations
88 (HT_Types
=> HT_Types
,
89 Hash_Node
=> Hash_Node
,
92 Copy_Node
=> Copy_Node
,
95 package Key_Ops
is new Hash_Tables
.Generic_Keys
96 (HT_Types
=> HT_Types
,
101 Equivalent_Keys
=> Equivalent_Key_Node
);
107 function Is_Equal
is new HT_Ops
.Generic_Equal
(Find_Equal_Key
);
109 overriding
function "=" (Left
, Right
: Map
) return Boolean is
111 return Is_Equal
(Left
.HT
, Right
.HT
);
118 procedure Adjust
(Container
: in out Map
) is
120 HT_Ops
.Adjust
(Container
.HT
);
127 function Capacity
(Container
: Map
) return Count_Type
is
129 return HT_Ops
.Capacity
(Container
.HT
);
136 procedure Clear
(Container
: in out Map
) is
138 HT_Ops
.Clear
(Container
.HT
);
145 function Contains
(Container
: Map
; Key
: Key_Type
) return Boolean is
147 return Find
(Container
, Key
) /= No_Element
;
154 function Copy_Node
(Node
: Node_Access
) return Node_Access
is
155 K
: Key_Access
:= new Key_Type
'(Node.Key.all);
159 E := new Element_Type'(Node
.Element
.all);
160 return new Node_Type
'(K, E, null);
173 procedure Delete (Container : in out Map; Key : Key_Type) is
177 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
180 raise Constraint_Error with "attempt to delete key not in map";
186 procedure Delete (Container : in out Map; Position : in out Cursor) is
188 if Position.Node = null then
189 raise Constraint_Error with
190 "Position cursor of Delete equals No_Element";
193 if Position.Container /= Container'Unrestricted_Access then
194 raise Program_Error with
195 "Position cursor of Delete designates wrong map";
198 if Container.HT.Busy > 0 then
199 raise Program_Error with
200 "Delete attempted to tamper with cursors (map is busy)";
203 pragma Assert (Vet (Position), "bad cursor in Delete");
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 with
221 "no element available because key not in map";
224 return Node.Element.all;
227 function Element (Position : Cursor) return Element_Type is
229 if Position.Node = null then
230 raise Constraint_Error with
231 "Position cursor of function Element equals No_Element";
234 if Position.Node.Element = null then
235 raise Program_Error with
236 "Position cursor of function Element is bad";
239 pragma Assert (Vet (Position), "bad cursor in function Element");
241 return Position.Node.Element.all;
244 -------------------------
245 -- Equivalent_Key_Node --
246 -------------------------
248 function Equivalent_Key_Node
250 Node : Node_Access) return Boolean
253 return Equivalent_Keys (Key, Node.Key.all);
254 end Equivalent_Key_Node;
256 ---------------------
257 -- Equivalent_Keys --
258 ---------------------
260 function Equivalent_Keys (Left, Right : Cursor) return Boolean is
262 if Left.Node = null then
263 raise Constraint_Error with
264 "Left cursor of Equivalent_Keys equals No_Element";
267 if Right.Node = null then
268 raise Constraint_Error with
269 "Right cursor of Equivalent_Keys equals No_Element";
272 if Left.Node.Key = null then
273 raise Program_Error with
274 "Left cursor of Equivalent_Keys is bad";
277 if Right.Node.Key = null then
278 raise Program_Error with
279 "Right cursor of Equivalent_Keys is bad";
282 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Keys");
283 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Keys");
285 return Equivalent_Keys (Left.Node.Key.all, Right.Node.Key.all);
288 function Equivalent_Keys
290 Right : Key_Type) return Boolean
293 if Left.Node = null then
294 raise Constraint_Error with
295 "Left cursor of Equivalent_Keys equals No_Element";
298 if Left.Node.Key = null then
299 raise Program_Error with
300 "Left cursor of Equivalent_Keys is bad";
303 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Keys");
305 return Equivalent_Keys (Left.Node.Key.all, Right);
308 function Equivalent_Keys
310 Right : Cursor) return Boolean
313 if Right.Node = null then
314 raise Constraint_Error with
315 "Right cursor of Equivalent_Keys equals No_Element";
318 if Right.Node.Key = null then
319 raise Program_Error with
320 "Right cursor of Equivalent_Keys is bad";
323 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Keys");
325 return Equivalent_Keys (Left, Right.Node.Key.all);
332 procedure Exclude (Container : in out Map; Key : Key_Type) is
335 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
343 procedure Finalize (Container : in out Map) is
345 HT_Ops.Finalize (Container.HT);
352 function Find (Container : Map; Key : Key_Type) return Cursor is
353 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
360 return Cursor'(Container
'Unchecked_Access, Node
);
367 function Find_Equal_Key
368 (R_HT
: Hash_Table_Type
;
369 L_Node
: Node_Access
) return Boolean
371 R_Index
: constant Hash_Type
:= Key_Ops
.Index
(R_HT
, L_Node
.Key
.all);
372 R_Node
: Node_Access
:= R_HT
.Buckets
(R_Index
);
375 while R_Node
/= null loop
376 if Equivalent_Keys
(L_Node
.Key
.all, R_Node
.Key
.all) then
377 return L_Node
.Element
.all = R_Node
.Element
.all;
380 R_Node
:= R_Node
.Next
;
390 function First
(Container
: Map
) return Cursor
is
391 Node
: constant Node_Access
:= HT_Ops
.First
(Container
.HT
);
398 return Cursor
'(Container'Unchecked_Access, Node);
405 procedure Free (X : in out Node_Access) is
406 procedure Deallocate is
407 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
413 X.Next := X; -- detect mischief (in Vet)
422 Free_Element (X.Element);
433 Free_Element (X.Element);
449 function Has_Element (Position : Cursor) return Boolean is
451 pragma Assert (Vet (Position), "bad cursor in Has_Element");
452 return Position.Node /= null;
459 function Hash_Node (Node : Node_Access) return Hash_Type is
461 return Hash (Node.Key.all);
469 (Container : in out Map;
471 New_Item : Element_Type)
480 Insert (Container, Key, New_Item, Position, Inserted);
483 if Container.HT.Lock > 0 then
484 raise Program_Error with
485 "Include attempted to tamper with elements (map is locked)";
488 K := Position.Node.Key;
489 E := Position.Node.Element;
491 Position.Node.Key := new Key_Type'(Key
);
494 Position
.Node
.Element
:= new Element_Type
'(New_Item);
511 (Container : in out Map;
513 New_Item : Element_Type;
514 Position : out Cursor;
515 Inserted : out Boolean)
517 function New_Node (Next : Node_Access) return Node_Access;
519 procedure Local_Insert is
520 new Key_Ops.Generic_Conditional_Insert (New_Node);
526 function New_Node (Next : Node_Access) return Node_Access is
527 K : Key_Access := new Key_Type'(Key
);
531 E
:= new Element_Type
'(New_Item);
532 return new Node_Type'(K
, E
, Next
);
540 HT
: Hash_Table_Type
renames Container
.HT
;
542 -- Start of processing for Insert
545 if HT_Ops
.Capacity
(HT
) = 0 then
546 HT_Ops
.Reserve_Capacity
(HT
, 1);
549 Local_Insert
(HT
, Key
, Position
.Node
, Inserted
);
552 and then HT
.Length
> HT_Ops
.Capacity
(HT
)
554 HT_Ops
.Reserve_Capacity
(HT
, HT
.Length
);
557 Position
.Container
:= Container
'Unchecked_Access;
561 (Container
: in out Map
;
563 New_Item
: Element_Type
)
566 pragma Unreferenced
(Position
);
571 Insert
(Container
, Key
, New_Item
, Position
, Inserted
);
574 raise Constraint_Error
with
575 "attempt to insert key already in map";
583 function Is_Empty
(Container
: Map
) return Boolean is
585 return Container
.HT
.Length
= 0;
594 Process
: not null access procedure (Position
: Cursor
))
596 procedure Process_Node
(Node
: Node_Access
);
597 pragma Inline
(Process_Node
);
599 procedure Local_Iterate
is
600 new HT_Ops
.Generic_Iteration
(Process_Node
);
606 procedure Process_Node
(Node
: Node_Access
) is
608 Process
(Cursor
'(Container'Unchecked_Access, Node));
611 B : Natural renames Container'Unrestricted_Access.HT.Busy;
613 -- Start of processing Iterate
619 Local_Iterate (Container.HT);
633 function Key (Position : Cursor) return Key_Type is
635 if Position.Node = null then
636 raise Constraint_Error with
637 "Position cursor of function Key equals No_Element";
640 if Position.Node.Key = null then
641 raise Program_Error with
642 "Position cursor of function Key is bad";
645 pragma Assert (Vet (Position), "bad cursor in function Key");
647 return Position.Node.Key.all;
654 function Length (Container : Map) return Count_Type is
656 return Container.HT.Length;
664 (Target : in out Map;
668 HT_Ops.Move (Target => Target.HT, Source => Source.HT);
675 function Next (Node : Node_Access) return Node_Access is
680 procedure Next (Position : in out Cursor) is
682 Position := Next (Position);
685 function Next (Position : Cursor) return Cursor is
687 if Position.Node = null then
691 if Position.Node.Key = null
692 or else Position.Node.Element = null
694 raise Program_Error with "Position cursor of Next is bad";
697 pragma Assert (Vet (Position), "Position cursor of Next is bad");
700 HT : Hash_Table_Type renames Position.Container.HT;
701 Node : constant Node_Access := HT_Ops.Next (HT, Position.Node);
708 return Cursor'(Position
.Container
, Node
);
716 procedure Query_Element
718 Process
: not null access procedure (Key
: Key_Type
;
719 Element
: Element_Type
))
722 if Position
.Node
= null then
723 raise Constraint_Error
with
724 "Position cursor of Query_Element equals No_Element";
727 if Position
.Node
.Key
= null
728 or else Position
.Node
.Element
= null
730 raise Program_Error
with
731 "Position cursor of Query_Element is bad";
734 pragma Assert
(Vet
(Position
), "bad cursor in Query_Element");
737 M
: Map
renames Position
.Container
.all;
738 HT
: Hash_Table_Type
renames M
.HT
'Unrestricted_Access.all;
740 B
: Natural renames HT
.Busy
;
741 L
: Natural renames HT
.Lock
;
748 K
: Key_Type
renames Position
.Node
.Key
.all;
749 E
: Element_Type
renames Position
.Node
.Element
.all;
769 procedure Read_Nodes
is new HT_Ops
.Generic_Read
(Read_Node
);
772 (Stream
: not null access Root_Stream_Type
'Class;
776 Read_Nodes
(Stream
, Container
.HT
);
780 (Stream
: not null access Root_Stream_Type
'Class;
784 raise Program_Error
with "attempt to stream map cursor";
792 (Stream
: not null access Root_Stream_Type
'Class) return Node_Access
794 Node
: Node_Access
:= new Node_Type
;
798 Node
.Key
:= new Key_Type
'(Key_Type'Input (Stream));
806 Node.Element := new Element_Type'(Element_Type
'Input (Stream
));
822 (Container
: in out Map
;
824 New_Item
: Element_Type
)
826 Node
: constant Node_Access
:= Key_Ops
.Find
(Container
.HT
, Key
);
833 raise Constraint_Error
with
834 "attempt to replace key not in map";
837 if Container
.HT
.Lock
> 0 then
838 raise Program_Error
with
839 "Replace attempted to tamper with elements (map is locked)";
845 Node
.Key
:= new Key_Type
'(Key);
848 Node.Element := new Element_Type'(New_Item
);
859 ---------------------
860 -- Replace_Element --
861 ---------------------
863 procedure Replace_Element
864 (Container
: in out Map
;
866 New_Item
: Element_Type
)
869 if Position
.Node
= null then
870 raise Constraint_Error
with
871 "Position cursor of Replace_Element equals No_Element";
874 if Position
.Node
.Key
= null
875 or else Position
.Node
.Element
= null
877 raise Program_Error
with
878 "Position cursor of Replace_Element is bad";
881 if Position
.Container
/= Container
'Unrestricted_Access then
882 raise Program_Error
with
883 "Position cursor of Replace_Element designates wrong map";
886 if Position
.Container
.HT
.Lock
> 0 then
887 raise Program_Error
with
888 "Replace_Element attempted to tamper with elements (map is locked)";
891 pragma Assert
(Vet
(Position
), "bad cursor in Replace_Element");
894 X
: Element_Access
:= Position
.Node
.Element
;
897 Position
.Node
.Element
:= new Element_Type
'(New_Item);
902 ----------------------
903 -- Reserve_Capacity --
904 ----------------------
906 procedure Reserve_Capacity
907 (Container : in out Map;
908 Capacity : Count_Type)
911 HT_Ops.Reserve_Capacity (Container.HT, Capacity);
912 end Reserve_Capacity;
918 procedure Set_Next (Node : Node_Access; Next : Node_Access) is
927 procedure Update_Element
928 (Container : in out Map;
930 Process : not null access procedure (Key : Key_Type;
931 Element : in out Element_Type))
934 if Position.Node = null then
935 raise Constraint_Error with
936 "Position cursor of Update_Element equals No_Element";
939 if Position.Node.Key = null
940 or else Position.Node.Element = null
942 raise Program_Error with
943 "Position cursor of Update_Element is bad";
946 if Position.Container /= Container'Unrestricted_Access then
947 raise Program_Error with
948 "Position cursor of Update_Element designates wrong map";
951 pragma Assert (Vet (Position), "bad cursor in Update_Element");
954 HT : Hash_Table_Type renames Container.HT;
956 B : Natural renames HT.Busy;
957 L : Natural renames HT.Lock;
964 K : Key_Type renames Position.Node.Key.all;
965 E : Element_Type renames Position.Node.Element.all;
986 function Vet (Position : Cursor) return Boolean is
988 if Position.Node = null then
989 return Position.Container = null;
992 if Position.Container = null then
996 if Position.Node.Next = Position.Node then
1000 if Position.Node.Key = null then
1004 if Position.Node.Element = null then
1009 HT : Hash_Table_Type renames Position.Container.HT;
1013 if HT.Length = 0 then
1017 if HT.Buckets = null
1018 or else HT.Buckets'Length = 0
1023 X := HT.Buckets (Key_Ops.Index (HT, Position.Node.Key.all));
1025 for J in 1 .. HT.Length loop
1026 if X = Position.Node then
1034 if X = X.Next then -- to prevent unnecessary looping
1049 procedure Write_Nodes is new HT_Ops.Generic_Write (Write_Node);
1052 (Stream : not null access Root_Stream_Type'Class;
1056 Write_Nodes (Stream, Container.HT);
1060 (Stream : not null access Root_Stream_Type'Class;
1064 raise Program_Error with "attempt to stream map cursor";
1071 procedure Write_Node
1072 (Stream : not null access Root_Stream_Type'Class;
1076 Key_Type'Output (Stream, Node.Key.all);
1077 Element_Type'Output (Stream, Node.Element.all);
1080 end Ada.Containers.Indefinite_Hashed_Maps;