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 _ S E T S --
9 -- Copyright (C) 2004-2011, 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 with Ada.Containers.Prime_Numbers;
40 with System; use type System.Address;
42 package body Ada.Containers.Hashed_Sets is
44 type Iterator is limited new
45 Set_Iterator_Interfaces.Forward_Iterator with record
46 Container : Set_Access;
49 overriding function First (Object : Iterator) return Cursor;
51 overriding function Next
53 Position : Cursor) return Cursor;
55 -----------------------
56 -- Local Subprograms --
57 -----------------------
59 procedure Assign (Node : Node_Access; Item : Element_Type);
60 pragma Inline (Assign);
62 function Copy_Node (Source : Node_Access) return Node_Access;
63 pragma Inline (Copy_Node);
65 function Equivalent_Keys
67 Node : Node_Access) return Boolean;
68 pragma Inline (Equivalent_Keys);
70 function Find_Equal_Key
71 (R_HT : Hash_Table_Type;
72 L_Node : Node_Access) return Boolean;
74 function Find_Equivalent_Key
75 (R_HT : Hash_Table_Type;
76 L_Node : Node_Access) return Boolean;
78 procedure Free (X : in out Node_Access);
80 function Hash_Node (Node : Node_Access) return Hash_Type;
81 pragma Inline (Hash_Node);
84 (HT : in out Hash_Table_Type;
85 New_Item : Element_Type;
86 Node : out Node_Access;
87 Inserted : out Boolean);
90 (HT : Hash_Table_Type;
91 Key : Node_Access) return Boolean;
92 pragma Inline (Is_In);
94 function Next (Node : Node_Access) return Node_Access;
97 function Read_Node (Stream : not null access Root_Stream_Type'Class)
99 pragma Inline (Read_Node);
101 procedure Set_Next (Node : Node_Access; Next : Node_Access);
102 pragma Inline (Set_Next);
104 function Vet (Position : Cursor) return Boolean;
107 (Stream : not null access Root_Stream_Type'Class;
109 pragma Inline (Write_Node);
111 --------------------------
112 -- Local Instantiations --
113 --------------------------
115 package HT_Ops is new Hash_Tables.Generic_Operations
116 (HT_Types => HT_Types,
117 Hash_Node => Hash_Node,
119 Set_Next => Set_Next,
120 Copy_Node => Copy_Node,
123 package Element_Keys is new Hash_Tables.Generic_Keys
124 (HT_Types => HT_Types,
126 Set_Next => Set_Next,
127 Key_Type => Element_Type,
129 Equivalent_Keys => Equivalent_Keys);
132 new HT_Ops.Generic_Equal (Find_Equal_Key);
134 function Is_Equivalent is
135 new HT_Ops.Generic_Equal (Find_Equivalent_Key);
137 procedure Read_Nodes is
138 new HT_Ops.Generic_Read (Read_Node);
140 procedure Replace_Element is
141 new Element_Keys.Generic_Replace_Element (Hash_Node, Assign);
143 procedure Write_Nodes is
144 new HT_Ops.Generic_Write (Write_Node);
150 function "=" (Left, Right : Set) return Boolean is
152 return Is_Equal (Left.HT, Right.HT);
159 procedure Adjust (Container : in out Set) is
161 HT_Ops.Adjust (Container.HT);
168 procedure Assign (Node : Node_Access; Item : Element_Type) is
170 Node.Element := Item;
173 procedure Assign (Target : in out Set; Source : Set) is
175 if Target'Address = Source'Address then
180 Target.Union (Source);
187 function Capacity (Container : Set) return Count_Type is
189 return HT_Ops.Capacity (Container.HT);
196 procedure Clear (Container : in out Set) is
198 HT_Ops.Clear (Container.HT);
201 ------------------------
202 -- Constant_Reference --
203 ------------------------
205 function Constant_Reference
206 (Container : aliased Set;
207 Position : Cursor) return Constant_Reference_Type
210 if Position.Container = null then
211 raise Constraint_Error with "Position cursor has no element";
214 if Position.Container /= Container'Unrestricted_Access then
215 raise Program_Error with
216 "Position cursor designates wrong container";
219 pragma Assert (Vet (Position), "bad cursor in Constant_Reference");
221 return (Element => Position.Node.Element'Access);
222 end Constant_Reference;
228 function Contains (Container : Set; Item : Element_Type) return Boolean is
230 return Find (Container, Item) /= No_Element;
239 Capacity : Count_Type := 0) return Set
247 elsif Capacity >= Source.Length then
252 with "Requested capacity is less than Source length";
255 return Target : Set do
256 Target.Reserve_Capacity (C);
257 Target.Assign (Source);
265 function Copy_Node (Source : Node_Access) return Node_Access is
267 return new Node_Type'(Element => Source.Element, Next => null);
275 (Container : in out Set;
281 Element_Keys.Delete_Key_Sans_Free (Container.HT, Item, X);
284 raise Constraint_Error with "attempt to delete element not in set";
291 (Container : in out Set;
292 Position : in out Cursor)
295 if Position.Node = null then
296 raise Constraint_Error with "Position cursor equals No_Element";
299 if Position.Container /= Container'Unrestricted_Access then
300 raise Program_Error with "Position cursor designates wrong set";
303 if Container.HT.Busy > 0 then
304 raise Program_Error with
305 "attempt to tamper with cursors (set is busy)";
308 pragma Assert (Vet (Position), "bad cursor in Delete");
310 HT_Ops.Delete_Node_Sans_Free (Container.HT, Position.Node);
312 Free (Position.Node);
313 Position.Container := null;
321 (Target : in out Set;
324 Tgt_Node : Node_Access;
327 if Target'Address = Source'Address then
332 if Source.HT.Length = 0 then
336 if Target.HT.Busy > 0 then
337 raise Program_Error with
338 "attempt to tamper with cursors (set is busy)";
341 if Source.HT.Length < Target.HT.Length then
343 Src_Node : Node_Access;
346 Src_Node := HT_Ops.First (Source.HT);
347 while Src_Node /= null loop
348 Tgt_Node := Element_Keys.Find (Target.HT, Src_Node.Element);
350 if Tgt_Node /= null then
351 HT_Ops.Delete_Node_Sans_Free (Target.HT, Tgt_Node);
355 Src_Node := HT_Ops.Next (Source.HT, Src_Node);
360 Tgt_Node := HT_Ops.First (Target.HT);
361 while Tgt_Node /= null loop
362 if Is_In (Source.HT, Tgt_Node) then
364 X : Node_Access := Tgt_Node;
366 Tgt_Node := HT_Ops.Next (Target.HT, Tgt_Node);
367 HT_Ops.Delete_Node_Sans_Free (Target.HT, X);
372 Tgt_Node := HT_Ops.Next (Target.HT, Tgt_Node);
378 function Difference (Left, Right : Set) return Set is
379 Buckets : HT_Types.Buckets_Access;
383 if Left'Address = Right'Address then
387 if Left.HT.Length = 0 then
391 if Right.HT.Length = 0 then
396 Size : constant Hash_Type := Prime_Numbers.To_Prime (Left.Length);
398 Buckets := HT_Ops.New_Buckets (Length => Size);
403 Iterate_Left : declare
404 procedure Process (L_Node : Node_Access);
407 new HT_Ops.Generic_Iteration (Process);
413 procedure Process (L_Node : Node_Access) is
415 if not Is_In (Right.HT, L_Node) then
417 J : constant Hash_Type :=
418 Hash (L_Node.Element) mod Buckets'Length;
420 Bucket : Node_Access renames Buckets (J);
423 Bucket := new Node_Type'(L_Node.Element, Bucket);
426 Length := Length + 1;
430 -- Start of processing for Iterate_Left
436 HT_Ops.Free_Hash_Table (Buckets);
440 return (Controlled with HT => (Buckets, Length, 0, 0));
447 function Element (Position : Cursor) return Element_Type is
449 if Position.Node = null then
450 raise Constraint_Error with "Position cursor equals No_Element";
453 pragma Assert (Vet (Position), "bad cursor in function Element");
455 return Position.Node.Element;
458 ---------------------
459 -- Equivalent_Sets --
460 ---------------------
462 function Equivalent_Sets (Left, Right : Set) return Boolean is
464 return Is_Equivalent (Left.HT, Right.HT);
467 -------------------------
468 -- Equivalent_Elements --
469 -------------------------
471 function Equivalent_Elements (Left, Right : Cursor)
474 if Left.Node = null then
475 raise Constraint_Error with
476 "Left cursor of Equivalent_Elements equals No_Element";
479 if Right.Node = null then
480 raise Constraint_Error with
481 "Right cursor of Equivalent_Elements equals No_Element";
484 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Elements");
485 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Elements");
487 return Equivalent_Elements (Left.Node.Element, Right.Node.Element);
488 end Equivalent_Elements;
490 function Equivalent_Elements (Left : Cursor; Right : Element_Type)
493 if Left.Node = null then
494 raise Constraint_Error with
495 "Left cursor of Equivalent_Elements equals No_Element";
498 pragma Assert (Vet (Left), "Left cursor in Equivalent_Elements is bad");
500 return Equivalent_Elements (Left.Node.Element, Right);
501 end Equivalent_Elements;
503 function Equivalent_Elements (Left : Element_Type; Right : Cursor)
506 if Right.Node = null then
507 raise Constraint_Error with
508 "Right cursor of Equivalent_Elements equals No_Element";
513 "Right cursor of Equivalent_Elements is bad");
515 return Equivalent_Elements (Left, Right.Node.Element);
516 end Equivalent_Elements;
518 ---------------------
519 -- Equivalent_Keys --
520 ---------------------
522 function Equivalent_Keys (Key : Element_Type; Node : Node_Access)
525 return Equivalent_Elements (Key, Node.Element);
533 (Container : in out Set;
538 Element_Keys.Delete_Key_Sans_Free (Container.HT, Item, X);
546 procedure Finalize (Container : in out Set) is
548 HT_Ops.Finalize (Container.HT);
557 Item : Element_Type) return Cursor
559 Node : constant Node_Access := Element_Keys.Find (Container.HT, Item);
566 return Cursor'(Container'Unrestricted_Access, Node);
573 function Find_Equal_Key
574 (R_HT : Hash_Table_Type;
575 L_Node : Node_Access) return Boolean
577 R_Index : constant Hash_Type :=
578 Element_Keys.Index (R_HT, L_Node.Element);
580 R_Node : Node_Access := R_HT.Buckets (R_Index);
584 if R_Node = null then
588 if L_Node.Element = R_Node.Element then
592 R_Node := Next (R_Node);
596 -------------------------
597 -- Find_Equivalent_Key --
598 -------------------------
600 function Find_Equivalent_Key
601 (R_HT : Hash_Table_Type;
602 L_Node : Node_Access) return Boolean
604 R_Index : constant Hash_Type :=
605 Element_Keys.Index (R_HT, L_Node.Element);
607 R_Node : Node_Access := R_HT.Buckets (R_Index);
611 if R_Node = null then
615 if Equivalent_Elements (L_Node.Element, R_Node.Element) then
619 R_Node := Next (R_Node);
621 end Find_Equivalent_Key;
627 function First (Container : Set) return Cursor is
628 Node : constant Node_Access := HT_Ops.First (Container.HT);
635 return Cursor'(Container'Unrestricted_Access, Node);
638 function First (Object : Iterator) return Cursor is
640 return Object.Container.First;
647 procedure Free (X : in out Node_Access) is
648 procedure Deallocate is
649 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
653 X.Next := X; -- detect mischief (in Vet)
662 function Has_Element (Position : Cursor) return Boolean is
664 pragma Assert (Vet (Position), "bad cursor in Has_Element");
665 return Position.Node /= null;
672 function Hash_Node (Node : Node_Access) return Hash_Type is
674 return Hash (Node.Element);
682 (Container : in out Set;
683 New_Item : Element_Type)
689 Insert (Container, New_Item, Position, Inserted);
692 if Container.HT.Lock > 0 then
693 raise Program_Error with
694 "attempt to tamper with elements (set is locked)";
697 Position.Node.Element := New_Item;
706 (Container : in out Set;
707 New_Item : Element_Type;
708 Position : out Cursor;
709 Inserted : out Boolean)
712 Insert (Container.HT, New_Item, Position.Node, Inserted);
713 Position.Container := Container'Unchecked_Access;
717 (Container : in out Set;
718 New_Item : Element_Type)
721 pragma Unreferenced (Position);
726 Insert (Container, New_Item, Position, Inserted);
729 raise Constraint_Error with
730 "attempt to insert element already in set";
735 (HT : in out Hash_Table_Type;
736 New_Item : Element_Type;
737 Node : out Node_Access;
738 Inserted : out Boolean)
740 function New_Node (Next : Node_Access) return Node_Access;
741 pragma Inline (New_Node);
743 procedure Local_Insert is
744 new Element_Keys.Generic_Conditional_Insert (New_Node);
750 function New_Node (Next : Node_Access) return Node_Access is
752 return new Node_Type'(New_Item, Next);
755 -- Start of processing for Insert
758 if HT_Ops.Capacity (HT) = 0 then
759 HT_Ops.Reserve_Capacity (HT, 1);
762 Local_Insert (HT, New_Item, Node, Inserted);
765 and then HT.Length > HT_Ops.Capacity (HT)
767 HT_Ops.Reserve_Capacity (HT, HT.Length);
775 procedure Intersection
776 (Target : in out Set;
779 Tgt_Node : Node_Access;
782 if Target'Address = Source'Address then
786 if Source.HT.Length = 0 then
791 if Target.HT.Busy > 0 then
792 raise Program_Error with
793 "attempt to tamper with cursors (set is busy)";
796 Tgt_Node := HT_Ops.First (Target.HT);
797 while Tgt_Node /= null loop
798 if Is_In (Source.HT, Tgt_Node) then
799 Tgt_Node := HT_Ops.Next (Target.HT, Tgt_Node);
803 X : Node_Access := Tgt_Node;
805 Tgt_Node := HT_Ops.Next (Target.HT, Tgt_Node);
806 HT_Ops.Delete_Node_Sans_Free (Target.HT, X);
813 function Intersection (Left, Right : Set) return Set is
814 Buckets : HT_Types.Buckets_Access;
818 if Left'Address = Right'Address then
822 Length := Count_Type'Min (Left.Length, Right.Length);
829 Size : constant Hash_Type := Prime_Numbers.To_Prime (Length);
831 Buckets := HT_Ops.New_Buckets (Length => Size);
836 Iterate_Left : declare
837 procedure Process (L_Node : Node_Access);
840 new HT_Ops.Generic_Iteration (Process);
846 procedure Process (L_Node : Node_Access) is
848 if Is_In (Right.HT, L_Node) then
850 J : constant Hash_Type :=
851 Hash (L_Node.Element) mod Buckets'Length;
853 Bucket : Node_Access renames Buckets (J);
856 Bucket := new Node_Type'(L_Node.Element, Bucket);
859 Length := Length + 1;
863 -- Start of processing for Iterate_Left
869 HT_Ops.Free_Hash_Table (Buckets);
873 return (Controlled with HT => (Buckets, Length, 0, 0));
880 function Is_Empty (Container : Set) return Boolean is
882 return Container.HT.Length = 0;
889 function Is_In (HT : Hash_Table_Type; Key : Node_Access) return Boolean is
891 return Element_Keys.Find (HT, Key.Element) /= null;
898 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
899 Subset_Node : Node_Access;
902 if Subset'Address = Of_Set'Address then
906 if Subset.Length > Of_Set.Length then
910 Subset_Node := HT_Ops.First (Subset.HT);
911 while Subset_Node /= null loop
912 if not Is_In (Of_Set.HT, Subset_Node) then
915 Subset_Node := HT_Ops.Next (Subset.HT, Subset_Node);
927 Process : not null access procedure (Position : Cursor))
929 procedure Process_Node (Node : Node_Access);
930 pragma Inline (Process_Node);
933 new HT_Ops.Generic_Iteration (Process_Node);
939 procedure Process_Node (Node : Node_Access) is
941 Process (Cursor'(Container'Unrestricted_Access, Node));
944 B : Natural renames Container'Unrestricted_Access.HT.Busy;
946 -- Start of processing for Iterate
952 Iterate (Container.HT);
963 (Container : Set) return Set_Iterator_Interfaces.Forward_Iterator'Class
966 return Iterator'(Container => Container'Unrestricted_Access);
973 function Length (Container : Set) return Count_Type is
975 return Container.HT.Length;
982 procedure Move (Target : in out Set; Source : in out Set) is
984 HT_Ops.Move (Target => Target.HT, Source => Source.HT);
991 function Next (Node : Node_Access) return Node_Access is
996 function Next (Position : Cursor) return Cursor is
998 if Position.Node = null then
1002 pragma Assert (Vet (Position), "bad cursor in Next");
1005 HT : Hash_Table_Type renames Position.Container.HT;
1006 Node : constant Node_Access := HT_Ops.Next (HT, Position.Node);
1013 return Cursor'(Position.Container, Node);
1017 procedure Next (Position : in out Cursor) is
1019 Position := Next (Position);
1024 Position : Cursor) return Cursor
1027 if Position.Container = null then
1031 if Position.Container /= Object.Container then
1032 raise Program_Error with
1033 "Position cursor of Next designates wrong set";
1036 return Next (Position);
1043 function Overlap (Left, Right : Set) return Boolean is
1044 Left_Node : Node_Access;
1047 if Right.Length = 0 then
1051 if Left'Address = Right'Address then
1055 Left_Node := HT_Ops.First (Left.HT);
1056 while Left_Node /= null loop
1057 if Is_In (Right.HT, Left_Node) then
1060 Left_Node := HT_Ops.Next (Left.HT, Left_Node);
1070 procedure Query_Element
1072 Process : not null access procedure (Element : Element_Type))
1075 if Position.Node = null then
1076 raise Constraint_Error with
1077 "Position cursor of Query_Element equals No_Element";
1080 pragma Assert (Vet (Position), "bad cursor in Query_Element");
1083 HT : Hash_Table_Type renames Position.Container.HT;
1085 B : Natural renames HT.Busy;
1086 L : Natural renames HT.Lock;
1093 Process (Position.Node.Element);
1111 (Stream : not null access Root_Stream_Type'Class;
1112 Container : out Set)
1115 Read_Nodes (Stream, Container.HT);
1119 (Stream : not null access Root_Stream_Type'Class;
1123 raise Program_Error with "attempt to stream set cursor";
1127 (Stream : not null access Root_Stream_Type'Class;
1128 Item : out Constant_Reference_Type)
1131 raise Program_Error with "attempt to stream reference";
1138 function Read_Node (Stream : not null access Root_Stream_Type'Class)
1141 Node : Node_Access := new Node_Type;
1144 Element_Type'Read (Stream, Node.Element);
1157 (Container : in out Set;
1158 New_Item : Element_Type)
1160 Node : constant Node_Access :=
1161 Element_Keys.Find (Container.HT, New_Item);
1165 raise Constraint_Error with
1166 "attempt to replace element not in set";
1169 if Container.HT.Lock > 0 then
1170 raise Program_Error with
1171 "attempt to tamper with elements (set is locked)";
1174 Node.Element := New_Item;
1177 procedure Replace_Element
1178 (Container : in out Set;
1180 New_Item : Element_Type)
1183 if Position.Node = null then
1184 raise Constraint_Error with
1185 "Position cursor equals No_Element";
1188 if Position.Container /= Container'Unrestricted_Access then
1189 raise Program_Error with
1190 "Position cursor designates wrong set";
1193 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
1195 Replace_Element (Container.HT, Position.Node, New_Item);
1196 end Replace_Element;
1198 ----------------------
1199 -- Reserve_Capacity --
1200 ----------------------
1202 procedure Reserve_Capacity
1203 (Container : in out Set;
1204 Capacity : Count_Type)
1207 HT_Ops.Reserve_Capacity (Container.HT, Capacity);
1208 end Reserve_Capacity;
1214 procedure Set_Next (Node : Node_Access; Next : Node_Access) is
1219 --------------------------
1220 -- Symmetric_Difference --
1221 --------------------------
1223 procedure Symmetric_Difference
1224 (Target : in out Set;
1228 if Target'Address = Source'Address then
1233 if Target.HT.Busy > 0 then
1234 raise Program_Error with
1235 "attempt to tamper with cursors (set is busy)";
1239 N : constant Count_Type := Target.Length + Source.Length;
1241 if N > HT_Ops.Capacity (Target.HT) then
1242 HT_Ops.Reserve_Capacity (Target.HT, N);
1246 if Target.Length = 0 then
1247 Iterate_Source_When_Empty_Target : declare
1248 procedure Process (Src_Node : Node_Access);
1250 procedure Iterate is
1251 new HT_Ops.Generic_Iteration (Process);
1257 procedure Process (Src_Node : Node_Access) is
1258 E : Element_Type renames Src_Node.Element;
1259 B : Buckets_Type renames Target.HT.Buckets.all;
1260 J : constant Hash_Type := Hash (E) mod B'Length;
1261 N : Count_Type renames Target.HT.Length;
1264 B (J) := new Node_Type'(E, B (J));
1268 -- Start of processing for Iterate_Source_When_Empty_Target
1271 Iterate (Source.HT);
1272 end Iterate_Source_When_Empty_Target;
1275 Iterate_Source : declare
1276 procedure Process (Src_Node : Node_Access);
1278 procedure Iterate is
1279 new HT_Ops.Generic_Iteration (Process);
1285 procedure Process (Src_Node : Node_Access) is
1286 E : Element_Type renames Src_Node.Element;
1287 B : Buckets_Type renames Target.HT.Buckets.all;
1288 J : constant Hash_Type := Hash (E) mod B'Length;
1289 N : Count_Type renames Target.HT.Length;
1292 if B (J) = null then
1293 B (J) := new Node_Type'(E, null);
1296 elsif Equivalent_Elements (E, B (J).Element) then
1298 X : Node_Access := B (J);
1300 B (J) := B (J).Next;
1307 Prev : Node_Access := B (J);
1308 Curr : Node_Access := Prev.Next;
1311 while Curr /= null loop
1312 if Equivalent_Elements (E, Curr.Element) then
1313 Prev.Next := Curr.Next;
1323 B (J) := new Node_Type'(E, B (J));
1329 -- Start of processing for Iterate_Source
1332 Iterate (Source.HT);
1335 end Symmetric_Difference;
1337 function Symmetric_Difference (Left, Right : Set) return Set is
1338 Buckets : HT_Types.Buckets_Access;
1339 Length : Count_Type;
1342 if Left'Address = Right'Address then
1346 if Right.Length = 0 then
1350 if Left.Length = 0 then
1355 Size : constant Hash_Type :=
1356 Prime_Numbers.To_Prime (Left.Length + Right.Length);
1358 Buckets := HT_Ops.New_Buckets (Length => Size);
1363 Iterate_Left : declare
1364 procedure Process (L_Node : Node_Access);
1366 procedure Iterate is
1367 new HT_Ops.Generic_Iteration (Process);
1373 procedure Process (L_Node : Node_Access) is
1375 if not Is_In (Right.HT, L_Node) then
1377 E : Element_Type renames L_Node.Element;
1378 J : constant Hash_Type := Hash (E) mod Buckets'Length;
1381 Buckets (J) := new Node_Type'(E, Buckets (J));
1382 Length := Length + 1;
1387 -- Start of processing for Iterate_Left
1393 HT_Ops.Free_Hash_Table (Buckets);
1397 Iterate_Right : declare
1398 procedure Process (R_Node : Node_Access);
1400 procedure Iterate is
1401 new HT_Ops.Generic_Iteration (Process);
1407 procedure Process (R_Node : Node_Access) is
1409 if not Is_In (Left.HT, R_Node) then
1411 E : Element_Type renames R_Node.Element;
1412 J : constant Hash_Type := Hash (E) mod Buckets'Length;
1415 Buckets (J) := new Node_Type'(E, Buckets (J));
1416 Length := Length + 1;
1421 -- Start of processing for Iterate_Right
1427 HT_Ops.Free_Hash_Table (Buckets);
1431 return (Controlled with HT => (Buckets, Length, 0, 0));
1432 end Symmetric_Difference;
1438 function To_Set (New_Item : Element_Type) return Set is
1439 HT : Hash_Table_Type;
1443 pragma Unreferenced (Node, Inserted);
1446 Insert (HT, New_Item, Node, Inserted);
1447 return Set'(Controlled with HT);
1455 (Target : in out Set;
1458 procedure Process (Src_Node : Node_Access);
1460 procedure Iterate is
1461 new HT_Ops.Generic_Iteration (Process);
1467 procedure Process (Src_Node : Node_Access) is
1468 function New_Node (Next : Node_Access) return Node_Access;
1469 pragma Inline (New_Node);
1472 new Element_Keys.Generic_Conditional_Insert (New_Node);
1478 function New_Node (Next : Node_Access) return Node_Access is
1479 Node : constant Node_Access :=
1480 new Node_Type'(Src_Node.Element, Next);
1485 Tgt_Node : Node_Access;
1487 pragma Unreferenced (Tgt_Node, Success);
1489 -- Start of processing for Process
1492 Insert (Target.HT, Src_Node.Element, Tgt_Node, Success);
1495 -- Start of processing for Union
1498 if Target'Address = Source'Address then
1502 if Target.HT.Busy > 0 then
1503 raise Program_Error with
1504 "attempt to tamper with cursors (set is busy)";
1508 N : constant Count_Type := Target.Length + Source.Length;
1510 if N > HT_Ops.Capacity (Target.HT) then
1511 HT_Ops.Reserve_Capacity (Target.HT, N);
1515 Iterate (Source.HT);
1518 function Union (Left, Right : Set) return Set is
1519 Buckets : HT_Types.Buckets_Access;
1520 Length : Count_Type;
1523 if Left'Address = Right'Address then
1527 if Right.Length = 0 then
1531 if Left.Length = 0 then
1536 Size : constant Hash_Type :=
1537 Prime_Numbers.To_Prime (Left.Length + Right.Length);
1539 Buckets := HT_Ops.New_Buckets (Length => Size);
1542 Iterate_Left : declare
1543 procedure Process (L_Node : Node_Access);
1545 procedure Iterate is
1546 new HT_Ops.Generic_Iteration (Process);
1552 procedure Process (L_Node : Node_Access) is
1553 J : constant Hash_Type :=
1554 Hash (L_Node.Element) mod Buckets'Length;
1557 Buckets (J) := new Node_Type'(L_Node.Element, Buckets (J));
1560 -- Start of processing for Iterate_Left
1566 HT_Ops.Free_Hash_Table (Buckets);
1570 Length := Left.Length;
1572 Iterate_Right : declare
1573 procedure Process (Src_Node : Node_Access);
1575 procedure Iterate is
1576 new HT_Ops.Generic_Iteration (Process);
1582 procedure Process (Src_Node : Node_Access) is
1583 J : constant Hash_Type :=
1584 Hash (Src_Node.Element) mod Buckets'Length;
1586 Tgt_Node : Node_Access := Buckets (J);
1589 while Tgt_Node /= null loop
1590 if Equivalent_Elements (Src_Node.Element, Tgt_Node.Element) then
1594 Tgt_Node := Next (Tgt_Node);
1597 Buckets (J) := new Node_Type'(Src_Node.Element, Buckets (J));
1598 Length := Length + 1;
1601 -- Start of processing for Iterate_Right
1607 HT_Ops.Free_Hash_Table (Buckets);
1611 return (Controlled with HT => (Buckets, Length, 0, 0));
1618 function Vet (Position : Cursor) return Boolean is
1620 if Position.Node = null then
1621 return Position.Container = null;
1624 if Position.Container = null then
1628 if Position.Node.Next = Position.Node then
1633 HT : Hash_Table_Type renames Position.Container.HT;
1637 if HT.Length = 0 then
1641 if HT.Buckets = null
1642 or else HT.Buckets'Length = 0
1647 X := HT.Buckets (Element_Keys.Index (HT, Position.Node.Element));
1649 for J in 1 .. HT.Length loop
1650 if X = Position.Node then
1658 if X = X.Next then -- to prevent unnecessary looping
1674 (Stream : not null access Root_Stream_Type'Class;
1678 Write_Nodes (Stream, Container.HT);
1682 (Stream : not null access Root_Stream_Type'Class;
1686 raise Program_Error with "attempt to stream set cursor";
1690 (Stream : not null access Root_Stream_Type'Class;
1691 Item : Constant_Reference_Type)
1694 raise Program_Error with "attempt to stream reference";
1701 procedure Write_Node
1702 (Stream : not null access Root_Stream_Type'Class;
1706 Element_Type'Write (Stream, Node.Element);
1709 package body Generic_Keys is
1711 -----------------------
1712 -- Local Subprograms --
1713 -----------------------
1715 function Equivalent_Key_Node
1717 Node : Node_Access) return Boolean;
1718 pragma Inline (Equivalent_Key_Node);
1720 --------------------------
1721 -- Local Instantiations --
1722 --------------------------
1725 new Hash_Tables.Generic_Keys
1726 (HT_Types => HT_Types,
1728 Set_Next => Set_Next,
1729 Key_Type => Key_Type,
1731 Equivalent_Keys => Equivalent_Key_Node);
1733 ------------------------
1734 -- Constant_Reference --
1735 ------------------------
1737 function Constant_Reference
1738 (Container : aliased Set;
1739 Key : Key_Type) return Constant_Reference_Type
1741 Node : constant Node_Access :=
1742 Key_Keys.Find (Container.HT, Key);
1746 raise Constraint_Error with "Key not in set";
1749 return (Element => Node.Element'Access);
1750 end Constant_Reference;
1758 Key : Key_Type) return Boolean
1761 return Find (Container, Key) /= No_Element;
1769 (Container : in out Set;
1775 Key_Keys.Delete_Key_Sans_Free (Container.HT, Key, X);
1778 raise Constraint_Error with "attempt to delete key not in set";
1790 Key : Key_Type) return Element_Type
1792 Node : constant Node_Access := Key_Keys.Find (Container.HT, Key);
1796 raise Constraint_Error with "key not in map"; -- ??? "set"
1799 return Node.Element;
1802 -------------------------
1803 -- Equivalent_Key_Node --
1804 -------------------------
1806 function Equivalent_Key_Node
1808 Node : Node_Access) return Boolean
1811 return Equivalent_Keys (Key, Generic_Keys.Key (Node.Element));
1812 end Equivalent_Key_Node;
1819 (Container : in out Set;
1824 Key_Keys.Delete_Key_Sans_Free (Container.HT, Key, X);
1834 Key : Key_Type) return Cursor
1836 Node : constant Node_Access :=
1837 Key_Keys.Find (Container.HT, Key);
1844 return Cursor'(Container'Unrestricted_Access, Node);
1851 function Key (Position : Cursor) return Key_Type is
1853 if Position.Node = null then
1854 raise Constraint_Error with
1855 "Position cursor equals No_Element";
1858 pragma Assert (Vet (Position), "bad cursor in function Key");
1860 return Key (Position.Node.Element);
1868 (Stream : not null access Root_Stream_Type'Class;
1869 Item : out Reference_Type)
1872 raise Program_Error with "attempt to stream reference";
1875 ------------------------------
1876 -- Reference_Preserving_Key --
1877 ------------------------------
1879 function Reference_Preserving_Key
1880 (Container : aliased in out Set;
1881 Position : Cursor) return Reference_Type
1884 if Position.Container = null then
1885 raise Constraint_Error with "Position cursor has no element";
1888 if Position.Container /= Container'Unrestricted_Access then
1889 raise Program_Error with
1890 "Position cursor designates wrong container";
1895 "bad cursor in function Reference_Preserving_Key");
1897 -- Some form of finalization will be required in order to actually
1898 -- check that the key-part of the element designated by Position has
1901 return (Element => Position.Node.Element'Access);
1902 end Reference_Preserving_Key;
1904 function Reference_Preserving_Key
1905 (Container : aliased in out Set;
1906 Key : Key_Type) return Reference_Type
1908 Node : constant Node_Access :=
1909 Key_Keys.Find (Container.HT, Key);
1913 raise Constraint_Error with "Key not in set";
1916 -- Some form of finalization will be required in order to actually
1917 -- check that the key-part of the element designated by Key has not
1920 return (Element => Node.Element'Access);
1921 end Reference_Preserving_Key;
1928 (Container : in out Set;
1930 New_Item : Element_Type)
1932 Node : constant Node_Access :=
1933 Key_Keys.Find (Container.HT, Key);
1937 raise Constraint_Error with
1938 "attempt to replace key not in set";
1941 Replace_Element (Container.HT, Node, New_Item);
1944 -----------------------------------
1945 -- Update_Element_Preserving_Key --
1946 -----------------------------------
1948 procedure Update_Element_Preserving_Key
1949 (Container : in out Set;
1951 Process : not null access
1952 procedure (Element : in out Element_Type))
1954 HT : Hash_Table_Type renames Container.HT;
1958 if Position.Node = null then
1959 raise Constraint_Error with
1960 "Position cursor equals No_Element";
1963 if Position.Container /= Container'Unrestricted_Access then
1964 raise Program_Error with
1965 "Position cursor designates wrong set";
1968 if HT.Buckets = null
1969 or else HT.Buckets'Length = 0
1970 or else HT.Length = 0
1971 or else Position.Node.Next = Position.Node
1973 raise Program_Error with "Position cursor is bad (set is empty)";
1978 "bad cursor in Update_Element_Preserving_Key");
1980 Indx := HT_Ops.Index (HT, Position.Node);
1983 E : Element_Type renames Position.Node.Element;
1984 K : constant Key_Type := Key (E);
1986 B : Natural renames HT.Busy;
1987 L : Natural renames HT.Lock;
2005 if Equivalent_Keys (K, Key (E)) then
2006 pragma Assert (Hash (K) = Hash (E));
2011 if HT.Buckets (Indx) = Position.Node then
2012 HT.Buckets (Indx) := Position.Node.Next;
2016 Prev : Node_Access := HT.Buckets (Indx);
2019 while Prev.Next /= Position.Node loop
2023 raise Program_Error with
2024 "Position cursor is bad (node not found)";
2028 Prev.Next := Position.Node.Next;
2032 HT.Length := HT.Length - 1;
2035 X : Node_Access := Position.Node;
2041 raise Program_Error with "key was modified";
2042 end Update_Element_Preserving_Key;
2049 (Stream : not null access Root_Stream_Type'Class;
2050 Item : Reference_Type)
2053 raise Program_Error with "attempt to stream reference";
2058 end Ada.Containers.Hashed_Sets;