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 -----------------------
45 -- Local Subprograms --
46 -----------------------
48 procedure Assign (Node : Node_Access; Item : Element_Type);
49 pragma Inline (Assign);
51 function Copy_Node (Source : Node_Access) return Node_Access;
52 pragma Inline (Copy_Node);
54 function Equivalent_Keys
56 Node : Node_Access) return Boolean;
57 pragma Inline (Equivalent_Keys);
59 function Find_Equal_Key
60 (R_HT : Hash_Table_Type;
61 L_Node : Node_Access) return Boolean;
63 function Find_Equivalent_Key
64 (R_HT : Hash_Table_Type;
65 L_Node : Node_Access) return Boolean;
67 procedure Free (X : in out Node_Access);
69 function Hash_Node (Node : Node_Access) return Hash_Type;
70 pragma Inline (Hash_Node);
73 (HT : in out Hash_Table_Type;
74 New_Item : Element_Type;
75 Node : out Node_Access;
76 Inserted : out Boolean);
79 (HT : Hash_Table_Type;
80 Key : Node_Access) return Boolean;
81 pragma Inline (Is_In);
83 function Next (Node : Node_Access) return Node_Access;
86 function Read_Node (Stream : not null access Root_Stream_Type'Class)
88 pragma Inline (Read_Node);
90 procedure Set_Next (Node : Node_Access; Next : Node_Access);
91 pragma Inline (Set_Next);
93 function Vet (Position : Cursor) return Boolean;
96 (Stream : not null access Root_Stream_Type'Class;
98 pragma Inline (Write_Node);
100 --------------------------
101 -- Local Instantiations --
102 --------------------------
104 package HT_Ops is new Hash_Tables.Generic_Operations
105 (HT_Types => HT_Types,
106 Hash_Node => Hash_Node,
108 Set_Next => Set_Next,
109 Copy_Node => Copy_Node,
112 package Element_Keys is new Hash_Tables.Generic_Keys
113 (HT_Types => HT_Types,
115 Set_Next => Set_Next,
116 Key_Type => Element_Type,
118 Equivalent_Keys => Equivalent_Keys);
121 new HT_Ops.Generic_Equal (Find_Equal_Key);
123 function Is_Equivalent is
124 new HT_Ops.Generic_Equal (Find_Equivalent_Key);
126 procedure Read_Nodes is
127 new HT_Ops.Generic_Read (Read_Node);
129 procedure Replace_Element is
130 new Element_Keys.Generic_Replace_Element (Hash_Node, Assign);
132 procedure Write_Nodes is
133 new HT_Ops.Generic_Write (Write_Node);
139 function "=" (Left, Right : Set) return Boolean is
141 return Is_Equal (Left.HT, Right.HT);
148 procedure Adjust (Container : in out Set) is
150 HT_Ops.Adjust (Container.HT);
157 procedure Assign (Node : Node_Access; Item : Element_Type) is
159 Node.Element := Item;
162 procedure Assign (Target : in out Set; Source : Set) is
164 if Target'Address = Source'Address then
169 Target.Union (Source);
176 function Capacity (Container : Set) return Count_Type is
178 return HT_Ops.Capacity (Container.HT);
185 procedure Clear (Container : in out Set) is
187 HT_Ops.Clear (Container.HT);
194 function Contains (Container : Set; Item : Element_Type) return Boolean is
196 return Find (Container, Item) /= No_Element;
205 Capacity : Count_Type := 0) return Set
213 elsif Capacity >= Source.Length then
218 with "Requested capacity is less than Source length";
221 return Target : Set do
222 Target.Reserve_Capacity (C);
223 Target.Assign (Source);
231 function Copy_Node (Source : Node_Access) return Node_Access is
233 return new Node_Type'(Element => Source.Element, Next => null);
241 (Container : in out Set;
247 Element_Keys.Delete_Key_Sans_Free (Container.HT, Item, X);
250 raise Constraint_Error with "attempt to delete element not in set";
257 (Container : in out Set;
258 Position : in out Cursor)
261 if Position.Node = null then
262 raise Constraint_Error with "Position cursor equals No_Element";
265 if Position.Container /= Container'Unrestricted_Access then
266 raise Program_Error with "Position cursor designates wrong set";
269 if Container.HT.Busy > 0 then
270 raise Program_Error with
271 "attempt to tamper with cursors (set is busy)";
274 pragma Assert (Vet (Position), "bad cursor in Delete");
276 HT_Ops.Delete_Node_Sans_Free (Container.HT, Position.Node);
278 Free (Position.Node);
279 Position.Container := null;
287 (Target : in out Set;
290 Tgt_Node : Node_Access;
293 if Target'Address = Source'Address then
298 if Source.HT.Length = 0 then
302 if Target.HT.Busy > 0 then
303 raise Program_Error with
304 "attempt to tamper with cursors (set is busy)";
307 if Source.HT.Length < Target.HT.Length then
309 Src_Node : Node_Access;
312 Src_Node := HT_Ops.First (Source.HT);
313 while Src_Node /= null loop
314 Tgt_Node := Element_Keys.Find (Target.HT, Src_Node.Element);
316 if Tgt_Node /= null then
317 HT_Ops.Delete_Node_Sans_Free (Target.HT, Tgt_Node);
321 Src_Node := HT_Ops.Next (Source.HT, Src_Node);
326 Tgt_Node := HT_Ops.First (Target.HT);
327 while Tgt_Node /= null loop
328 if Is_In (Source.HT, Tgt_Node) then
330 X : Node_Access := Tgt_Node;
332 Tgt_Node := HT_Ops.Next (Target.HT, Tgt_Node);
333 HT_Ops.Delete_Node_Sans_Free (Target.HT, X);
338 Tgt_Node := HT_Ops.Next (Target.HT, Tgt_Node);
344 function Difference (Left, Right : Set) return Set is
345 Buckets : HT_Types.Buckets_Access;
349 if Left'Address = Right'Address then
353 if Left.HT.Length = 0 then
357 if Right.HT.Length = 0 then
362 Size : constant Hash_Type := Prime_Numbers.To_Prime (Left.Length);
364 Buckets := HT_Ops.New_Buckets (Length => Size);
369 Iterate_Left : declare
370 procedure Process (L_Node : Node_Access);
373 new HT_Ops.Generic_Iteration (Process);
379 procedure Process (L_Node : Node_Access) is
381 if not Is_In (Right.HT, L_Node) then
383 J : constant Hash_Type :=
384 Hash (L_Node.Element) mod Buckets'Length;
386 Bucket : Node_Access renames Buckets (J);
389 Bucket := new Node_Type'(L_Node.Element, Bucket);
392 Length := Length + 1;
396 -- Start of processing for Iterate_Left
402 HT_Ops.Free_Hash_Table (Buckets);
406 return (Controlled with HT => (Buckets, Length, 0, 0));
413 function Element (Position : Cursor) return Element_Type is
415 if Position.Node = null then
416 raise Constraint_Error with "Position cursor equals No_Element";
419 pragma Assert (Vet (Position), "bad cursor in function Element");
421 return Position.Node.Element;
424 ---------------------
425 -- Equivalent_Sets --
426 ---------------------
428 function Equivalent_Sets (Left, Right : Set) return Boolean is
430 return Is_Equivalent (Left.HT, Right.HT);
433 -------------------------
434 -- Equivalent_Elements --
435 -------------------------
437 function Equivalent_Elements (Left, Right : Cursor)
440 if Left.Node = null then
441 raise Constraint_Error with
442 "Left cursor of Equivalent_Elements equals No_Element";
445 if Right.Node = null then
446 raise Constraint_Error with
447 "Right cursor of Equivalent_Elements equals No_Element";
450 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Elements");
451 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Elements");
453 return Equivalent_Elements (Left.Node.Element, Right.Node.Element);
454 end Equivalent_Elements;
456 function Equivalent_Elements (Left : Cursor; Right : Element_Type)
459 if Left.Node = null then
460 raise Constraint_Error with
461 "Left cursor of Equivalent_Elements equals No_Element";
464 pragma Assert (Vet (Left), "Left cursor in Equivalent_Elements is bad");
466 return Equivalent_Elements (Left.Node.Element, Right);
467 end Equivalent_Elements;
469 function Equivalent_Elements (Left : Element_Type; Right : Cursor)
472 if Right.Node = null then
473 raise Constraint_Error with
474 "Right cursor of Equivalent_Elements equals No_Element";
479 "Right cursor of Equivalent_Elements is bad");
481 return Equivalent_Elements (Left, Right.Node.Element);
482 end Equivalent_Elements;
484 ---------------------
485 -- Equivalent_Keys --
486 ---------------------
488 function Equivalent_Keys (Key : Element_Type; Node : Node_Access)
491 return Equivalent_Elements (Key, Node.Element);
499 (Container : in out Set;
504 Element_Keys.Delete_Key_Sans_Free (Container.HT, Item, X);
512 procedure Finalize (Container : in out Set) is
514 HT_Ops.Finalize (Container.HT);
523 Item : Element_Type) return Cursor
525 Node : constant Node_Access := Element_Keys.Find (Container.HT, Item);
532 return Cursor'(Container'Unrestricted_Access, Node);
539 function Find_Equal_Key
540 (R_HT : Hash_Table_Type;
541 L_Node : Node_Access) return Boolean
543 R_Index : constant Hash_Type :=
544 Element_Keys.Index (R_HT, L_Node.Element);
546 R_Node : Node_Access := R_HT.Buckets (R_Index);
550 if R_Node = null then
554 if L_Node.Element = R_Node.Element then
558 R_Node := Next (R_Node);
562 -------------------------
563 -- Find_Equivalent_Key --
564 -------------------------
566 function Find_Equivalent_Key
567 (R_HT : Hash_Table_Type;
568 L_Node : Node_Access) return Boolean
570 R_Index : constant Hash_Type :=
571 Element_Keys.Index (R_HT, L_Node.Element);
573 R_Node : Node_Access := R_HT.Buckets (R_Index);
577 if R_Node = null then
581 if Equivalent_Elements (L_Node.Element, R_Node.Element) then
585 R_Node := Next (R_Node);
587 end Find_Equivalent_Key;
593 function First (Container : Set) return Cursor is
594 Node : constant Node_Access := HT_Ops.First (Container.HT);
601 return Cursor'(Container'Unrestricted_Access, Node);
608 procedure Free (X : in out Node_Access) is
609 procedure Deallocate is
610 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
614 X.Next := X; -- detect mischief (in Vet)
623 function Has_Element (Position : Cursor) return Boolean is
625 pragma Assert (Vet (Position), "bad cursor in Has_Element");
626 return Position.Node /= null;
633 function Hash_Node (Node : Node_Access) return Hash_Type is
635 return Hash (Node.Element);
643 (Container : in out Set;
644 New_Item : Element_Type)
650 Insert (Container, New_Item, Position, Inserted);
653 if Container.HT.Lock > 0 then
654 raise Program_Error with
655 "attempt to tamper with elements (set is locked)";
658 Position.Node.Element := New_Item;
667 (Container : in out Set;
668 New_Item : Element_Type;
669 Position : out Cursor;
670 Inserted : out Boolean)
673 Insert (Container.HT, New_Item, Position.Node, Inserted);
674 Position.Container := Container'Unchecked_Access;
678 (Container : in out Set;
679 New_Item : Element_Type)
682 pragma Unreferenced (Position);
687 Insert (Container, New_Item, Position, Inserted);
690 raise Constraint_Error with
691 "attempt to insert element already in set";
696 (HT : in out Hash_Table_Type;
697 New_Item : Element_Type;
698 Node : out Node_Access;
699 Inserted : out Boolean)
701 function New_Node (Next : Node_Access) return Node_Access;
702 pragma Inline (New_Node);
704 procedure Local_Insert is
705 new Element_Keys.Generic_Conditional_Insert (New_Node);
711 function New_Node (Next : Node_Access) return Node_Access is
713 return new Node_Type'(New_Item, Next);
716 -- Start of processing for Insert
719 if HT_Ops.Capacity (HT) = 0 then
720 HT_Ops.Reserve_Capacity (HT, 1);
723 Local_Insert (HT, New_Item, Node, Inserted);
726 and then HT.Length > HT_Ops.Capacity (HT)
728 HT_Ops.Reserve_Capacity (HT, HT.Length);
736 procedure Intersection
737 (Target : in out Set;
740 Tgt_Node : Node_Access;
743 if Target'Address = Source'Address then
747 if Source.HT.Length = 0 then
752 if Target.HT.Busy > 0 then
753 raise Program_Error with
754 "attempt to tamper with cursors (set is busy)";
757 Tgt_Node := HT_Ops.First (Target.HT);
758 while Tgt_Node /= null loop
759 if Is_In (Source.HT, Tgt_Node) then
760 Tgt_Node := HT_Ops.Next (Target.HT, Tgt_Node);
764 X : Node_Access := Tgt_Node;
766 Tgt_Node := HT_Ops.Next (Target.HT, Tgt_Node);
767 HT_Ops.Delete_Node_Sans_Free (Target.HT, X);
774 function Intersection (Left, Right : Set) return Set is
775 Buckets : HT_Types.Buckets_Access;
779 if Left'Address = Right'Address then
783 Length := Count_Type'Min (Left.Length, Right.Length);
790 Size : constant Hash_Type := Prime_Numbers.To_Prime (Length);
792 Buckets := HT_Ops.New_Buckets (Length => Size);
797 Iterate_Left : declare
798 procedure Process (L_Node : Node_Access);
801 new HT_Ops.Generic_Iteration (Process);
807 procedure Process (L_Node : Node_Access) is
809 if Is_In (Right.HT, L_Node) then
811 J : constant Hash_Type :=
812 Hash (L_Node.Element) mod Buckets'Length;
814 Bucket : Node_Access renames Buckets (J);
817 Bucket := new Node_Type'(L_Node.Element, Bucket);
820 Length := Length + 1;
824 -- Start of processing for Iterate_Left
830 HT_Ops.Free_Hash_Table (Buckets);
834 return (Controlled with HT => (Buckets, Length, 0, 0));
841 function Is_Empty (Container : Set) return Boolean is
843 return Container.HT.Length = 0;
850 function Is_In (HT : Hash_Table_Type; Key : Node_Access) return Boolean is
852 return Element_Keys.Find (HT, Key.Element) /= null;
859 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
860 Subset_Node : Node_Access;
863 if Subset'Address = Of_Set'Address then
867 if Subset.Length > Of_Set.Length then
871 Subset_Node := HT_Ops.First (Subset.HT);
872 while Subset_Node /= null loop
873 if not Is_In (Of_Set.HT, Subset_Node) then
876 Subset_Node := HT_Ops.Next (Subset.HT, Subset_Node);
888 Process : not null access procedure (Position : Cursor))
890 procedure Process_Node (Node : Node_Access);
891 pragma Inline (Process_Node);
894 new HT_Ops.Generic_Iteration (Process_Node);
900 procedure Process_Node (Node : Node_Access) is
902 Process (Cursor'(Container'Unrestricted_Access, Node));
905 B : Natural renames Container'Unrestricted_Access.HT.Busy;
907 -- Start of processing for Iterate
913 Iterate (Container.HT);
927 function Length (Container : Set) return Count_Type is
929 return Container.HT.Length;
936 procedure Move (Target : in out Set; Source : in out Set) is
938 HT_Ops.Move (Target => Target.HT, Source => Source.HT);
945 function Next (Node : Node_Access) return Node_Access is
950 function Next (Position : Cursor) return Cursor is
952 if Position.Node = null then
956 pragma Assert (Vet (Position), "bad cursor in Next");
959 HT : Hash_Table_Type renames Position.Container.HT;
960 Node : constant Node_Access := HT_Ops.Next (HT, Position.Node);
967 return Cursor'(Position.Container, Node);
971 procedure Next (Position : in out Cursor) is
973 Position := Next (Position);
980 function Overlap (Left, Right : Set) return Boolean is
981 Left_Node : Node_Access;
984 if Right.Length = 0 then
988 if Left'Address = Right'Address then
992 Left_Node := HT_Ops.First (Left.HT);
993 while Left_Node /= null loop
994 if Is_In (Right.HT, Left_Node) then
997 Left_Node := HT_Ops.Next (Left.HT, Left_Node);
1007 procedure Query_Element
1009 Process : not null access procedure (Element : Element_Type))
1012 if Position.Node = null then
1013 raise Constraint_Error with
1014 "Position cursor of Query_Element equals No_Element";
1017 pragma Assert (Vet (Position), "bad cursor in Query_Element");
1020 HT : Hash_Table_Type renames Position.Container.HT;
1022 B : Natural renames HT.Busy;
1023 L : Natural renames HT.Lock;
1030 Process (Position.Node.Element);
1048 (Stream : not null access Root_Stream_Type'Class;
1049 Container : out Set)
1052 Read_Nodes (Stream, Container.HT);
1056 (Stream : not null access Root_Stream_Type'Class;
1060 raise Program_Error with "attempt to stream set cursor";
1067 function Read_Node (Stream : not null access Root_Stream_Type'Class)
1070 Node : Node_Access := new Node_Type;
1073 Element_Type'Read (Stream, Node.Element);
1086 (Container : in out Set;
1087 New_Item : Element_Type)
1089 Node : constant Node_Access :=
1090 Element_Keys.Find (Container.HT, New_Item);
1094 raise Constraint_Error with
1095 "attempt to replace element not in set";
1098 if Container.HT.Lock > 0 then
1099 raise Program_Error with
1100 "attempt to tamper with elements (set is locked)";
1103 Node.Element := New_Item;
1106 procedure Replace_Element
1107 (Container : in out Set;
1109 New_Item : Element_Type)
1112 if Position.Node = null then
1113 raise Constraint_Error with
1114 "Position cursor equals No_Element";
1117 if Position.Container /= Container'Unrestricted_Access then
1118 raise Program_Error with
1119 "Position cursor designates wrong set";
1122 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
1124 Replace_Element (Container.HT, Position.Node, New_Item);
1125 end Replace_Element;
1127 ----------------------
1128 -- Reserve_Capacity --
1129 ----------------------
1131 procedure Reserve_Capacity
1132 (Container : in out Set;
1133 Capacity : Count_Type)
1136 HT_Ops.Reserve_Capacity (Container.HT, Capacity);
1137 end Reserve_Capacity;
1143 procedure Set_Next (Node : Node_Access; Next : Node_Access) is
1148 --------------------------
1149 -- Symmetric_Difference --
1150 --------------------------
1152 procedure Symmetric_Difference
1153 (Target : in out Set;
1157 if Target'Address = Source'Address then
1162 if Target.HT.Busy > 0 then
1163 raise Program_Error with
1164 "attempt to tamper with cursors (set is busy)";
1168 N : constant Count_Type := Target.Length + Source.Length;
1170 if N > HT_Ops.Capacity (Target.HT) then
1171 HT_Ops.Reserve_Capacity (Target.HT, N);
1175 if Target.Length = 0 then
1176 Iterate_Source_When_Empty_Target : declare
1177 procedure Process (Src_Node : Node_Access);
1179 procedure Iterate is
1180 new HT_Ops.Generic_Iteration (Process);
1186 procedure Process (Src_Node : Node_Access) is
1187 E : Element_Type renames Src_Node.Element;
1188 B : Buckets_Type renames Target.HT.Buckets.all;
1189 J : constant Hash_Type := Hash (E) mod B'Length;
1190 N : Count_Type renames Target.HT.Length;
1193 B (J) := new Node_Type'(E, B (J));
1197 -- Start of processing for Iterate_Source_When_Empty_Target
1200 Iterate (Source.HT);
1201 end Iterate_Source_When_Empty_Target;
1204 Iterate_Source : declare
1205 procedure Process (Src_Node : Node_Access);
1207 procedure Iterate is
1208 new HT_Ops.Generic_Iteration (Process);
1214 procedure Process (Src_Node : Node_Access) is
1215 E : Element_Type renames Src_Node.Element;
1216 B : Buckets_Type renames Target.HT.Buckets.all;
1217 J : constant Hash_Type := Hash (E) mod B'Length;
1218 N : Count_Type renames Target.HT.Length;
1221 if B (J) = null then
1222 B (J) := new Node_Type'(E, null);
1225 elsif Equivalent_Elements (E, B (J).Element) then
1227 X : Node_Access := B (J);
1229 B (J) := B (J).Next;
1236 Prev : Node_Access := B (J);
1237 Curr : Node_Access := Prev.Next;
1240 while Curr /= null loop
1241 if Equivalent_Elements (E, Curr.Element) then
1242 Prev.Next := Curr.Next;
1252 B (J) := new Node_Type'(E, B (J));
1258 -- Start of processing for Iterate_Source
1261 Iterate (Source.HT);
1264 end Symmetric_Difference;
1266 function Symmetric_Difference (Left, Right : Set) return Set is
1267 Buckets : HT_Types.Buckets_Access;
1268 Length : Count_Type;
1271 if Left'Address = Right'Address then
1275 if Right.Length = 0 then
1279 if Left.Length = 0 then
1284 Size : constant Hash_Type :=
1285 Prime_Numbers.To_Prime (Left.Length + Right.Length);
1287 Buckets := HT_Ops.New_Buckets (Length => Size);
1292 Iterate_Left : declare
1293 procedure Process (L_Node : Node_Access);
1295 procedure Iterate is
1296 new HT_Ops.Generic_Iteration (Process);
1302 procedure Process (L_Node : Node_Access) is
1304 if not Is_In (Right.HT, L_Node) then
1306 E : Element_Type renames L_Node.Element;
1307 J : constant Hash_Type := Hash (E) mod Buckets'Length;
1310 Buckets (J) := new Node_Type'(E, Buckets (J));
1311 Length := Length + 1;
1316 -- Start of processing for Iterate_Left
1322 HT_Ops.Free_Hash_Table (Buckets);
1326 Iterate_Right : declare
1327 procedure Process (R_Node : Node_Access);
1329 procedure Iterate is
1330 new HT_Ops.Generic_Iteration (Process);
1336 procedure Process (R_Node : Node_Access) is
1338 if not Is_In (Left.HT, R_Node) then
1340 E : Element_Type renames R_Node.Element;
1341 J : constant Hash_Type := Hash (E) mod Buckets'Length;
1344 Buckets (J) := new Node_Type'(E, Buckets (J));
1345 Length := Length + 1;
1350 -- Start of processing for Iterate_Right
1356 HT_Ops.Free_Hash_Table (Buckets);
1360 return (Controlled with HT => (Buckets, Length, 0, 0));
1361 end Symmetric_Difference;
1367 function To_Set (New_Item : Element_Type) return Set is
1368 HT : Hash_Table_Type;
1372 pragma Unreferenced (Node, Inserted);
1375 Insert (HT, New_Item, Node, Inserted);
1376 return Set'(Controlled with HT);
1384 (Target : in out Set;
1387 procedure Process (Src_Node : Node_Access);
1389 procedure Iterate is
1390 new HT_Ops.Generic_Iteration (Process);
1396 procedure Process (Src_Node : Node_Access) is
1397 function New_Node (Next : Node_Access) return Node_Access;
1398 pragma Inline (New_Node);
1401 new Element_Keys.Generic_Conditional_Insert (New_Node);
1407 function New_Node (Next : Node_Access) return Node_Access is
1408 Node : constant Node_Access :=
1409 new Node_Type'(Src_Node.Element, Next);
1414 Tgt_Node : Node_Access;
1416 pragma Unreferenced (Tgt_Node, Success);
1418 -- Start of processing for Process
1421 Insert (Target.HT, Src_Node.Element, Tgt_Node, Success);
1424 -- Start of processing for Union
1427 if Target'Address = Source'Address then
1431 if Target.HT.Busy > 0 then
1432 raise Program_Error with
1433 "attempt to tamper with cursors (set is busy)";
1437 N : constant Count_Type := Target.Length + Source.Length;
1439 if N > HT_Ops.Capacity (Target.HT) then
1440 HT_Ops.Reserve_Capacity (Target.HT, N);
1444 Iterate (Source.HT);
1447 function Union (Left, Right : Set) return Set is
1448 Buckets : HT_Types.Buckets_Access;
1449 Length : Count_Type;
1452 if Left'Address = Right'Address then
1456 if Right.Length = 0 then
1460 if Left.Length = 0 then
1465 Size : constant Hash_Type :=
1466 Prime_Numbers.To_Prime (Left.Length + Right.Length);
1468 Buckets := HT_Ops.New_Buckets (Length => Size);
1471 Iterate_Left : declare
1472 procedure Process (L_Node : Node_Access);
1474 procedure Iterate is
1475 new HT_Ops.Generic_Iteration (Process);
1481 procedure Process (L_Node : Node_Access) is
1482 J : constant Hash_Type :=
1483 Hash (L_Node.Element) mod Buckets'Length;
1486 Buckets (J) := new Node_Type'(L_Node.Element, Buckets (J));
1489 -- Start of processing for Iterate_Left
1495 HT_Ops.Free_Hash_Table (Buckets);
1499 Length := Left.Length;
1501 Iterate_Right : declare
1502 procedure Process (Src_Node : Node_Access);
1504 procedure Iterate is
1505 new HT_Ops.Generic_Iteration (Process);
1511 procedure Process (Src_Node : Node_Access) is
1512 J : constant Hash_Type :=
1513 Hash (Src_Node.Element) mod Buckets'Length;
1515 Tgt_Node : Node_Access := Buckets (J);
1518 while Tgt_Node /= null loop
1519 if Equivalent_Elements (Src_Node.Element, Tgt_Node.Element) then
1523 Tgt_Node := Next (Tgt_Node);
1526 Buckets (J) := new Node_Type'(Src_Node.Element, Buckets (J));
1527 Length := Length + 1;
1530 -- Start of processing for Iterate_Right
1536 HT_Ops.Free_Hash_Table (Buckets);
1540 return (Controlled with HT => (Buckets, Length, 0, 0));
1547 function Vet (Position : Cursor) return Boolean is
1549 if Position.Node = null then
1550 return Position.Container = null;
1553 if Position.Container = null then
1557 if Position.Node.Next = Position.Node then
1562 HT : Hash_Table_Type renames Position.Container.HT;
1566 if HT.Length = 0 then
1570 if HT.Buckets = null
1571 or else HT.Buckets'Length = 0
1576 X := HT.Buckets (Element_Keys.Index (HT, Position.Node.Element));
1578 for J in 1 .. HT.Length loop
1579 if X = Position.Node then
1587 if X = X.Next then -- to prevent unnecessary looping
1603 (Stream : not null access Root_Stream_Type'Class;
1607 Write_Nodes (Stream, Container.HT);
1611 (Stream : not null access Root_Stream_Type'Class;
1615 raise Program_Error with "attempt to stream set cursor";
1622 procedure Write_Node
1623 (Stream : not null access Root_Stream_Type'Class;
1627 Element_Type'Write (Stream, Node.Element);
1630 package body Generic_Keys is
1632 -----------------------
1633 -- Local Subprograms --
1634 -----------------------
1636 function Equivalent_Key_Node
1638 Node : Node_Access) return Boolean;
1639 pragma Inline (Equivalent_Key_Node);
1641 --------------------------
1642 -- Local Instantiations --
1643 --------------------------
1646 new Hash_Tables.Generic_Keys
1647 (HT_Types => HT_Types,
1649 Set_Next => Set_Next,
1650 Key_Type => Key_Type,
1652 Equivalent_Keys => Equivalent_Key_Node);
1660 Key : Key_Type) return Boolean
1663 return Find (Container, Key) /= No_Element;
1671 (Container : in out Set;
1677 Key_Keys.Delete_Key_Sans_Free (Container.HT, Key, X);
1680 raise Constraint_Error with "attempt to delete key not in set";
1692 Key : Key_Type) return Element_Type
1694 Node : constant Node_Access := Key_Keys.Find (Container.HT, Key);
1698 raise Constraint_Error with "key not in map";
1701 return Node.Element;
1704 -------------------------
1705 -- Equivalent_Key_Node --
1706 -------------------------
1708 function Equivalent_Key_Node
1710 Node : Node_Access) return Boolean
1713 return Equivalent_Keys (Key, Generic_Keys.Key (Node.Element));
1714 end Equivalent_Key_Node;
1721 (Container : in out Set;
1726 Key_Keys.Delete_Key_Sans_Free (Container.HT, Key, X);
1736 Key : Key_Type) return Cursor
1738 Node : constant Node_Access :=
1739 Key_Keys.Find (Container.HT, Key);
1746 return Cursor'(Container'Unrestricted_Access, Node);
1753 function Key (Position : Cursor) return Key_Type is
1755 if Position.Node = null then
1756 raise Constraint_Error with
1757 "Position cursor equals No_Element";
1760 pragma Assert (Vet (Position), "bad cursor in function Key");
1762 return Key (Position.Node.Element);
1770 (Container : in out Set;
1772 New_Item : Element_Type)
1774 Node : constant Node_Access :=
1775 Key_Keys.Find (Container.HT, Key);
1779 raise Constraint_Error with
1780 "attempt to replace key not in set";
1783 Replace_Element (Container.HT, Node, New_Item);
1786 -----------------------------------
1787 -- Update_Element_Preserving_Key --
1788 -----------------------------------
1790 procedure Update_Element_Preserving_Key
1791 (Container : in out Set;
1793 Process : not null access
1794 procedure (Element : in out Element_Type))
1796 HT : Hash_Table_Type renames Container.HT;
1800 if Position.Node = null then
1801 raise Constraint_Error with
1802 "Position cursor equals No_Element";
1805 if Position.Container /= Container'Unrestricted_Access then
1806 raise Program_Error with
1807 "Position cursor designates wrong set";
1810 if HT.Buckets = null
1811 or else HT.Buckets'Length = 0
1812 or else HT.Length = 0
1813 or else Position.Node.Next = Position.Node
1815 raise Program_Error with "Position cursor is bad (set is empty)";
1820 "bad cursor in Update_Element_Preserving_Key");
1822 Indx := HT_Ops.Index (HT, Position.Node);
1825 E : Element_Type renames Position.Node.Element;
1826 K : constant Key_Type := Key (E);
1828 B : Natural renames HT.Busy;
1829 L : Natural renames HT.Lock;
1847 if Equivalent_Keys (K, Key (E)) then
1848 pragma Assert (Hash (K) = Hash (E));
1853 if HT.Buckets (Indx) = Position.Node then
1854 HT.Buckets (Indx) := Position.Node.Next;
1858 Prev : Node_Access := HT.Buckets (Indx);
1861 while Prev.Next /= Position.Node loop
1865 raise Program_Error with
1866 "Position cursor is bad (node not found)";
1870 Prev.Next := Position.Node.Next;
1874 HT.Length := HT.Length - 1;
1877 X : Node_Access := Position.Node;
1883 raise Program_Error with "key was modified";
1884 end Update_Element_Preserving_Key;
1888 end Ada.Containers.Hashed_Sets;