Implement -mmemcpy-strategy= and -mmemset-strategy= options
[official-gcc.git] / gcc / ada / a-cohase.adb
blobf9e1b2aa8c09a9d88569b247223e92625a277329
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- A D A . C O N T A I N E R S . H A S H E D _ S E T S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2004-2013, Free Software Foundation, Inc. --
10 -- --
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. --
17 -- --
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. --
21 -- --
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/>. --
26 -- --
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
55 (Key : Element_Type;
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);
72 procedure Insert
73 (HT : in out Hash_Table_Type;
74 New_Item : Element_Type;
75 Node : out Node_Access;
76 Inserted : out Boolean);
78 function Is_In
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;
84 pragma Inline (Next);
86 function Read_Node (Stream : not null access Root_Stream_Type'Class)
87 return Node_Access;
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;
95 procedure Write_Node
96 (Stream : not null access Root_Stream_Type'Class;
97 Node : Node_Access);
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,
107 Next => Next,
108 Set_Next => Set_Next,
109 Copy_Node => Copy_Node,
110 Free => Free);
112 package Element_Keys is new Hash_Tables.Generic_Keys
113 (HT_Types => HT_Types,
114 Next => Next,
115 Set_Next => Set_Next,
116 Key_Type => Element_Type,
117 Hash => Hash,
118 Equivalent_Keys => Equivalent_Keys);
120 function Is_Equal is
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);
135 ---------
136 -- "=" --
137 ---------
139 function "=" (Left, Right : Set) return Boolean is
140 begin
141 return Is_Equal (Left.HT, Right.HT);
142 end "=";
144 ------------
145 -- Adjust --
146 ------------
148 procedure Adjust (Container : in out Set) is
149 begin
150 HT_Ops.Adjust (Container.HT);
151 end Adjust;
153 procedure Adjust (Control : in out Reference_Control_Type) is
154 begin
155 if Control.Container /= null then
156 declare
157 HT : Hash_Table_Type renames Control.Container.all.HT;
158 B : Natural renames HT.Busy;
159 L : Natural renames HT.Lock;
160 begin
161 B := B + 1;
162 L := L + 1;
163 end;
164 end if;
165 end Adjust;
167 ------------
168 -- Assign --
169 ------------
171 procedure Assign (Node : Node_Access; Item : Element_Type) is
172 begin
173 Node.Element := Item;
174 end Assign;
176 procedure Assign (Target : in out Set; Source : Set) is
177 begin
178 if Target'Address = Source'Address then
179 return;
180 end if;
182 Target.Clear;
183 Target.Union (Source);
184 end Assign;
186 --------------
187 -- Capacity --
188 --------------
190 function Capacity (Container : Set) return Count_Type is
191 begin
192 return HT_Ops.Capacity (Container.HT);
193 end Capacity;
195 -----------
196 -- Clear --
197 -----------
199 procedure Clear (Container : in out Set) is
200 begin
201 HT_Ops.Clear (Container.HT);
202 end Clear;
204 ------------------------
205 -- Constant_Reference --
206 ------------------------
208 function Constant_Reference
209 (Container : aliased Set;
210 Position : Cursor) return Constant_Reference_Type
212 begin
213 if Position.Container = null then
214 raise Constraint_Error with "Position cursor has no element";
215 end if;
217 if Position.Container /= Container'Unrestricted_Access then
218 raise Program_Error with
219 "Position cursor designates wrong container";
220 end if;
222 pragma Assert (Vet (Position), "bad cursor in Constant_Reference");
224 declare
225 HT : Hash_Table_Type renames Position.Container.all.HT;
226 B : Natural renames HT.Busy;
227 L : Natural renames HT.Lock;
228 begin
229 return R : constant Constant_Reference_Type :=
230 (Element => Position.Node.Element'Access,
231 Control => (Controlled with Container'Unrestricted_Access))
233 B := B + 1;
234 L := L + 1;
235 end return;
236 end;
237 end Constant_Reference;
239 --------------
240 -- Contains --
241 --------------
243 function Contains (Container : Set; Item : Element_Type) return Boolean is
244 begin
245 return Find (Container, Item) /= No_Element;
246 end Contains;
248 ----------
249 -- Copy --
250 ----------
252 function Copy
253 (Source : Set;
254 Capacity : Count_Type := 0) return Set
256 C : Count_Type;
258 begin
259 if Capacity = 0 then
260 C := Source.Length;
262 elsif Capacity >= Source.Length then
263 C := Capacity;
265 else
266 raise Capacity_Error
267 with "Requested capacity is less than Source length";
268 end if;
270 return Target : Set do
271 Target.Reserve_Capacity (C);
272 Target.Assign (Source);
273 end return;
274 end Copy;
276 ---------------
277 -- Copy_Node --
278 ---------------
280 function Copy_Node (Source : Node_Access) return Node_Access is
281 begin
282 return new Node_Type'(Element => Source.Element, Next => null);
283 end Copy_Node;
285 ------------
286 -- Delete --
287 ------------
289 procedure Delete
290 (Container : in out Set;
291 Item : Element_Type)
293 X : Node_Access;
295 begin
296 Element_Keys.Delete_Key_Sans_Free (Container.HT, Item, X);
298 if X = null then
299 raise Constraint_Error with "attempt to delete element not in set";
300 end if;
302 Free (X);
303 end Delete;
305 procedure Delete
306 (Container : in out Set;
307 Position : in out Cursor)
309 begin
310 if Position.Node = null then
311 raise Constraint_Error with "Position cursor equals No_Element";
312 end if;
314 if Position.Container /= Container'Unrestricted_Access then
315 raise Program_Error with "Position cursor designates wrong set";
316 end if;
318 if Container.HT.Busy > 0 then
319 raise Program_Error with
320 "attempt to tamper with cursors (set is busy)";
321 end if;
323 pragma Assert (Vet (Position), "bad cursor in Delete");
325 HT_Ops.Delete_Node_Sans_Free (Container.HT, Position.Node);
327 Free (Position.Node);
328 Position.Container := null;
329 end Delete;
331 ----------------
332 -- Difference --
333 ----------------
335 procedure Difference
336 (Target : in out Set;
337 Source : Set)
339 Tgt_Node : Node_Access;
341 begin
342 if Target'Address = Source'Address then
343 Clear (Target);
344 return;
345 end if;
347 if Source.HT.Length = 0 then
348 return;
349 end if;
351 if Target.HT.Busy > 0 then
352 raise Program_Error with
353 "attempt to tamper with cursors (set is busy)";
354 end if;
356 if Source.HT.Length < Target.HT.Length then
357 declare
358 Src_Node : Node_Access;
360 begin
361 Src_Node := HT_Ops.First (Source.HT);
362 while Src_Node /= null loop
363 Tgt_Node := Element_Keys.Find (Target.HT, Src_Node.Element);
365 if Tgt_Node /= null then
366 HT_Ops.Delete_Node_Sans_Free (Target.HT, Tgt_Node);
367 Free (Tgt_Node);
368 end if;
370 Src_Node := HT_Ops.Next (Source.HT, Src_Node);
371 end loop;
372 end;
374 else
375 Tgt_Node := HT_Ops.First (Target.HT);
376 while Tgt_Node /= null loop
377 if Is_In (Source.HT, Tgt_Node) then
378 declare
379 X : Node_Access := Tgt_Node;
380 begin
381 Tgt_Node := HT_Ops.Next (Target.HT, Tgt_Node);
382 HT_Ops.Delete_Node_Sans_Free (Target.HT, X);
383 Free (X);
384 end;
386 else
387 Tgt_Node := HT_Ops.Next (Target.HT, Tgt_Node);
388 end if;
389 end loop;
390 end if;
391 end Difference;
393 function Difference (Left, Right : Set) return Set is
394 Buckets : HT_Types.Buckets_Access;
395 Length : Count_Type;
397 begin
398 if Left'Address = Right'Address then
399 return Empty_Set;
400 end if;
402 if Left.HT.Length = 0 then
403 return Empty_Set;
404 end if;
406 if Right.HT.Length = 0 then
407 return Left;
408 end if;
410 declare
411 Size : constant Hash_Type := Prime_Numbers.To_Prime (Left.Length);
412 begin
413 Buckets := HT_Ops.New_Buckets (Length => Size);
414 end;
416 Length := 0;
418 Iterate_Left : declare
419 procedure Process (L_Node : Node_Access);
421 procedure Iterate is
422 new HT_Ops.Generic_Iteration (Process);
424 -------------
425 -- Process --
426 -------------
428 procedure Process (L_Node : Node_Access) is
429 begin
430 if not Is_In (Right.HT, L_Node) then
431 declare
432 J : constant Hash_Type :=
433 Hash (L_Node.Element) mod Buckets'Length;
435 Bucket : Node_Access renames Buckets (J);
437 begin
438 Bucket := new Node_Type'(L_Node.Element, Bucket);
439 end;
441 Length := Length + 1;
442 end if;
443 end Process;
445 -- Start of processing for Iterate_Left
447 begin
448 Iterate (Left.HT);
449 exception
450 when others =>
451 HT_Ops.Free_Hash_Table (Buckets);
452 raise;
453 end Iterate_Left;
455 return (Controlled with HT => (Buckets, Length, 0, 0));
456 end Difference;
458 -------------
459 -- Element --
460 -------------
462 function Element (Position : Cursor) return Element_Type is
463 begin
464 if Position.Node = null then
465 raise Constraint_Error with "Position cursor equals No_Element";
466 end if;
468 pragma Assert (Vet (Position), "bad cursor in function Element");
470 return Position.Node.Element;
471 end Element;
473 ---------------------
474 -- Equivalent_Sets --
475 ---------------------
477 function Equivalent_Sets (Left, Right : Set) return Boolean is
478 begin
479 return Is_Equivalent (Left.HT, Right.HT);
480 end Equivalent_Sets;
482 -------------------------
483 -- Equivalent_Elements --
484 -------------------------
486 function Equivalent_Elements (Left, Right : Cursor)
487 return Boolean is
488 begin
489 if Left.Node = null then
490 raise Constraint_Error with
491 "Left cursor of Equivalent_Elements equals No_Element";
492 end if;
494 if Right.Node = null then
495 raise Constraint_Error with
496 "Right cursor of Equivalent_Elements equals No_Element";
497 end if;
499 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Elements");
500 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Elements");
502 return Equivalent_Elements (Left.Node.Element, Right.Node.Element);
503 end Equivalent_Elements;
505 function Equivalent_Elements (Left : Cursor; Right : Element_Type)
506 return Boolean is
507 begin
508 if Left.Node = null then
509 raise Constraint_Error with
510 "Left cursor of Equivalent_Elements equals No_Element";
511 end if;
513 pragma Assert (Vet (Left), "Left cursor in Equivalent_Elements is bad");
515 return Equivalent_Elements (Left.Node.Element, Right);
516 end Equivalent_Elements;
518 function Equivalent_Elements (Left : Element_Type; Right : Cursor)
519 return Boolean is
520 begin
521 if Right.Node = null then
522 raise Constraint_Error with
523 "Right cursor of Equivalent_Elements equals No_Element";
524 end if;
526 pragma Assert
527 (Vet (Right),
528 "Right cursor of Equivalent_Elements is bad");
530 return Equivalent_Elements (Left, Right.Node.Element);
531 end Equivalent_Elements;
533 ---------------------
534 -- Equivalent_Keys --
535 ---------------------
537 function Equivalent_Keys (Key : Element_Type; Node : Node_Access)
538 return Boolean is
539 begin
540 return Equivalent_Elements (Key, Node.Element);
541 end Equivalent_Keys;
543 -------------
544 -- Exclude --
545 -------------
547 procedure Exclude
548 (Container : in out Set;
549 Item : Element_Type)
551 X : Node_Access;
552 begin
553 Element_Keys.Delete_Key_Sans_Free (Container.HT, Item, X);
554 Free (X);
555 end Exclude;
557 --------------
558 -- Finalize --
559 --------------
561 procedure Finalize (Container : in out Set) is
562 begin
563 HT_Ops.Finalize (Container.HT);
564 end Finalize;
566 procedure Finalize (Control : in out Reference_Control_Type) is
567 begin
568 if Control.Container /= null then
569 declare
570 HT : Hash_Table_Type renames Control.Container.all.HT;
571 B : Natural renames HT.Busy;
572 L : Natural renames HT.Lock;
573 begin
574 B := B - 1;
575 L := L - 1;
576 end;
578 Control.Container := null;
579 end if;
580 end Finalize;
582 ----------
583 -- Find --
584 ----------
586 function Find
587 (Container : Set;
588 Item : Element_Type) return Cursor
590 Node : constant Node_Access := Element_Keys.Find (Container.HT, Item);
592 begin
593 if Node = null then
594 return No_Element;
595 end if;
597 return Cursor'(Container'Unrestricted_Access, Node);
598 end Find;
600 --------------------
601 -- Find_Equal_Key --
602 --------------------
604 function Find_Equal_Key
605 (R_HT : Hash_Table_Type;
606 L_Node : Node_Access) return Boolean
608 R_Index : constant Hash_Type :=
609 Element_Keys.Index (R_HT, L_Node.Element);
611 R_Node : Node_Access := R_HT.Buckets (R_Index);
613 begin
614 loop
615 if R_Node = null then
616 return False;
617 end if;
619 if L_Node.Element = R_Node.Element then
620 return True;
621 end if;
623 R_Node := Next (R_Node);
624 end loop;
625 end Find_Equal_Key;
627 -------------------------
628 -- Find_Equivalent_Key --
629 -------------------------
631 function Find_Equivalent_Key
632 (R_HT : Hash_Table_Type;
633 L_Node : Node_Access) return Boolean
635 R_Index : constant Hash_Type :=
636 Element_Keys.Index (R_HT, L_Node.Element);
638 R_Node : Node_Access := R_HT.Buckets (R_Index);
640 begin
641 loop
642 if R_Node = null then
643 return False;
644 end if;
646 if Equivalent_Elements (L_Node.Element, R_Node.Element) then
647 return True;
648 end if;
650 R_Node := Next (R_Node);
651 end loop;
652 end Find_Equivalent_Key;
654 -----------
655 -- First --
656 -----------
658 function First (Container : Set) return Cursor is
659 Node : constant Node_Access := HT_Ops.First (Container.HT);
661 begin
662 if Node = null then
663 return No_Element;
664 end if;
666 return Cursor'(Container'Unrestricted_Access, Node);
667 end First;
669 function First (Object : Iterator) return Cursor is
670 begin
671 return Object.Container.First;
672 end First;
674 ----------
675 -- Free --
676 ----------
678 procedure Free (X : in out Node_Access) is
679 procedure Deallocate is
680 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
682 begin
683 if X /= null then
684 X.Next := X; -- detect mischief (in Vet)
685 Deallocate (X);
686 end if;
687 end Free;
689 -----------------
690 -- Has_Element --
691 -----------------
693 function Has_Element (Position : Cursor) return Boolean is
694 begin
695 pragma Assert (Vet (Position), "bad cursor in Has_Element");
696 return Position.Node /= null;
697 end Has_Element;
699 ---------------
700 -- Hash_Node --
701 ---------------
703 function Hash_Node (Node : Node_Access) return Hash_Type is
704 begin
705 return Hash (Node.Element);
706 end Hash_Node;
708 -------------
709 -- Include --
710 -------------
712 procedure Include
713 (Container : in out Set;
714 New_Item : Element_Type)
716 Position : Cursor;
717 Inserted : Boolean;
719 begin
720 Insert (Container, New_Item, Position, Inserted);
722 if not Inserted then
723 if Container.HT.Lock > 0 then
724 raise Program_Error with
725 "attempt to tamper with elements (set is locked)";
726 end if;
728 Position.Node.Element := New_Item;
729 end if;
730 end Include;
732 ------------
733 -- Insert --
734 ------------
736 procedure Insert
737 (Container : in out Set;
738 New_Item : Element_Type;
739 Position : out Cursor;
740 Inserted : out Boolean)
742 begin
743 Insert (Container.HT, New_Item, Position.Node, Inserted);
744 Position.Container := Container'Unchecked_Access;
745 end Insert;
747 procedure Insert
748 (Container : in out Set;
749 New_Item : Element_Type)
751 Position : Cursor;
752 pragma Unreferenced (Position);
754 Inserted : Boolean;
756 begin
757 Insert (Container, New_Item, Position, Inserted);
759 if not Inserted then
760 raise Constraint_Error with
761 "attempt to insert element already in set";
762 end if;
763 end Insert;
765 procedure Insert
766 (HT : in out Hash_Table_Type;
767 New_Item : Element_Type;
768 Node : out Node_Access;
769 Inserted : out Boolean)
771 function New_Node (Next : Node_Access) return Node_Access;
772 pragma Inline (New_Node);
774 procedure Local_Insert is
775 new Element_Keys.Generic_Conditional_Insert (New_Node);
777 --------------
778 -- New_Node --
779 --------------
781 function New_Node (Next : Node_Access) return Node_Access is
782 begin
783 return new Node_Type'(New_Item, Next);
784 end New_Node;
786 -- Start of processing for Insert
788 begin
789 if HT_Ops.Capacity (HT) = 0 then
790 HT_Ops.Reserve_Capacity (HT, 1);
791 end if;
793 Local_Insert (HT, New_Item, Node, Inserted);
795 if Inserted
796 and then HT.Length > HT_Ops.Capacity (HT)
797 then
798 HT_Ops.Reserve_Capacity (HT, HT.Length);
799 end if;
800 end Insert;
802 ------------------
803 -- Intersection --
804 ------------------
806 procedure Intersection
807 (Target : in out Set;
808 Source : Set)
810 Tgt_Node : Node_Access;
812 begin
813 if Target'Address = Source'Address then
814 return;
815 end if;
817 if Source.HT.Length = 0 then
818 Clear (Target);
819 return;
820 end if;
822 if Target.HT.Busy > 0 then
823 raise Program_Error with
824 "attempt to tamper with cursors (set is busy)";
825 end if;
827 Tgt_Node := HT_Ops.First (Target.HT);
828 while Tgt_Node /= null loop
829 if Is_In (Source.HT, Tgt_Node) then
830 Tgt_Node := HT_Ops.Next (Target.HT, Tgt_Node);
832 else
833 declare
834 X : Node_Access := Tgt_Node;
835 begin
836 Tgt_Node := HT_Ops.Next (Target.HT, Tgt_Node);
837 HT_Ops.Delete_Node_Sans_Free (Target.HT, X);
838 Free (X);
839 end;
840 end if;
841 end loop;
842 end Intersection;
844 function Intersection (Left, Right : Set) return Set is
845 Buckets : HT_Types.Buckets_Access;
846 Length : Count_Type;
848 begin
849 if Left'Address = Right'Address then
850 return Left;
851 end if;
853 Length := Count_Type'Min (Left.Length, Right.Length);
855 if Length = 0 then
856 return Empty_Set;
857 end if;
859 declare
860 Size : constant Hash_Type := Prime_Numbers.To_Prime (Length);
861 begin
862 Buckets := HT_Ops.New_Buckets (Length => Size);
863 end;
865 Length := 0;
867 Iterate_Left : declare
868 procedure Process (L_Node : Node_Access);
870 procedure Iterate is
871 new HT_Ops.Generic_Iteration (Process);
873 -------------
874 -- Process --
875 -------------
877 procedure Process (L_Node : Node_Access) is
878 begin
879 if Is_In (Right.HT, L_Node) then
880 declare
881 J : constant Hash_Type :=
882 Hash (L_Node.Element) mod Buckets'Length;
884 Bucket : Node_Access renames Buckets (J);
886 begin
887 Bucket := new Node_Type'(L_Node.Element, Bucket);
888 end;
890 Length := Length + 1;
891 end if;
892 end Process;
894 -- Start of processing for Iterate_Left
896 begin
897 Iterate (Left.HT);
898 exception
899 when others =>
900 HT_Ops.Free_Hash_Table (Buckets);
901 raise;
902 end Iterate_Left;
904 return (Controlled with HT => (Buckets, Length, 0, 0));
905 end Intersection;
907 --------------
908 -- Is_Empty --
909 --------------
911 function Is_Empty (Container : Set) return Boolean is
912 begin
913 return Container.HT.Length = 0;
914 end Is_Empty;
916 -----------
917 -- Is_In --
918 -----------
920 function Is_In (HT : Hash_Table_Type; Key : Node_Access) return Boolean is
921 begin
922 return Element_Keys.Find (HT, Key.Element) /= null;
923 end Is_In;
925 ---------------
926 -- Is_Subset --
927 ---------------
929 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
930 Subset_Node : Node_Access;
932 begin
933 if Subset'Address = Of_Set'Address then
934 return True;
935 end if;
937 if Subset.Length > Of_Set.Length then
938 return False;
939 end if;
941 Subset_Node := HT_Ops.First (Subset.HT);
942 while Subset_Node /= null loop
943 if not Is_In (Of_Set.HT, Subset_Node) then
944 return False;
945 end if;
946 Subset_Node := HT_Ops.Next (Subset.HT, Subset_Node);
947 end loop;
949 return True;
950 end Is_Subset;
952 -------------
953 -- Iterate --
954 -------------
956 procedure Iterate
957 (Container : Set;
958 Process : not null access procedure (Position : Cursor))
960 procedure Process_Node (Node : Node_Access);
961 pragma Inline (Process_Node);
963 procedure Iterate is
964 new HT_Ops.Generic_Iteration (Process_Node);
966 ------------------
967 -- Process_Node --
968 ------------------
970 procedure Process_Node (Node : Node_Access) is
971 begin
972 Process (Cursor'(Container'Unrestricted_Access, Node));
973 end Process_Node;
975 B : Natural renames Container'Unrestricted_Access.HT.Busy;
977 -- Start of processing for Iterate
979 begin
980 B := B + 1;
982 begin
983 Iterate (Container.HT);
984 exception
985 when others =>
986 B := B - 1;
987 raise;
988 end;
990 B := B - 1;
991 end Iterate;
993 function Iterate
994 (Container : Set) return Set_Iterator_Interfaces.Forward_Iterator'Class
996 begin
997 return Iterator'(Container => Container'Unrestricted_Access);
998 end Iterate;
1000 ------------
1001 -- Length --
1002 ------------
1004 function Length (Container : Set) return Count_Type is
1005 begin
1006 return Container.HT.Length;
1007 end Length;
1009 ----------
1010 -- Move --
1011 ----------
1013 procedure Move (Target : in out Set; Source : in out Set) is
1014 begin
1015 HT_Ops.Move (Target => Target.HT, Source => Source.HT);
1016 end Move;
1018 ----------
1019 -- Next --
1020 ----------
1022 function Next (Node : Node_Access) return Node_Access is
1023 begin
1024 return Node.Next;
1025 end Next;
1027 function Next (Position : Cursor) return Cursor is
1028 begin
1029 if Position.Node = null then
1030 return No_Element;
1031 end if;
1033 pragma Assert (Vet (Position), "bad cursor in Next");
1035 declare
1036 HT : Hash_Table_Type renames Position.Container.HT;
1037 Node : constant Node_Access := HT_Ops.Next (HT, Position.Node);
1039 begin
1040 if Node = null then
1041 return No_Element;
1042 end if;
1044 return Cursor'(Position.Container, Node);
1045 end;
1046 end Next;
1048 procedure Next (Position : in out Cursor) is
1049 begin
1050 Position := Next (Position);
1051 end Next;
1053 function Next
1054 (Object : Iterator;
1055 Position : Cursor) return Cursor
1057 begin
1058 if Position.Container = null then
1059 return No_Element;
1060 end if;
1062 if Position.Container /= Object.Container then
1063 raise Program_Error with
1064 "Position cursor of Next designates wrong set";
1065 end if;
1067 return Next (Position);
1068 end Next;
1070 -------------
1071 -- Overlap --
1072 -------------
1074 function Overlap (Left, Right : Set) return Boolean is
1075 Left_Node : Node_Access;
1077 begin
1078 if Right.Length = 0 then
1079 return False;
1080 end if;
1082 if Left'Address = Right'Address then
1083 return True;
1084 end if;
1086 Left_Node := HT_Ops.First (Left.HT);
1087 while Left_Node /= null loop
1088 if Is_In (Right.HT, Left_Node) then
1089 return True;
1090 end if;
1091 Left_Node := HT_Ops.Next (Left.HT, Left_Node);
1092 end loop;
1094 return False;
1095 end Overlap;
1097 -------------------
1098 -- Query_Element --
1099 -------------------
1101 procedure Query_Element
1102 (Position : Cursor;
1103 Process : not null access procedure (Element : Element_Type))
1105 begin
1106 if Position.Node = null then
1107 raise Constraint_Error with
1108 "Position cursor of Query_Element equals No_Element";
1109 end if;
1111 pragma Assert (Vet (Position), "bad cursor in Query_Element");
1113 declare
1114 HT : Hash_Table_Type renames Position.Container.HT;
1116 B : Natural renames HT.Busy;
1117 L : Natural renames HT.Lock;
1119 begin
1120 B := B + 1;
1121 L := L + 1;
1123 begin
1124 Process (Position.Node.Element);
1125 exception
1126 when others =>
1127 L := L - 1;
1128 B := B - 1;
1129 raise;
1130 end;
1132 L := L - 1;
1133 B := B - 1;
1134 end;
1135 end Query_Element;
1137 ----------
1138 -- Read --
1139 ----------
1141 procedure Read
1142 (Stream : not null access Root_Stream_Type'Class;
1143 Container : out Set)
1145 begin
1146 Read_Nodes (Stream, Container.HT);
1147 end Read;
1149 procedure Read
1150 (Stream : not null access Root_Stream_Type'Class;
1151 Item : out Cursor)
1153 begin
1154 raise Program_Error with "attempt to stream set cursor";
1155 end Read;
1157 procedure Read
1158 (Stream : not null access Root_Stream_Type'Class;
1159 Item : out Constant_Reference_Type)
1161 begin
1162 raise Program_Error with "attempt to stream reference";
1163 end Read;
1165 ---------------
1166 -- Read_Node --
1167 ---------------
1169 function Read_Node (Stream : not null access Root_Stream_Type'Class)
1170 return Node_Access
1172 Node : Node_Access := new Node_Type;
1174 begin
1175 Element_Type'Read (Stream, Node.Element);
1176 return Node;
1177 exception
1178 when others =>
1179 Free (Node);
1180 raise;
1181 end Read_Node;
1183 -------------
1184 -- Replace --
1185 -------------
1187 procedure Replace
1188 (Container : in out Set;
1189 New_Item : Element_Type)
1191 Node : constant Node_Access :=
1192 Element_Keys.Find (Container.HT, New_Item);
1194 begin
1195 if Node = null then
1196 raise Constraint_Error with
1197 "attempt to replace element not in set";
1198 end if;
1200 if Container.HT.Lock > 0 then
1201 raise Program_Error with
1202 "attempt to tamper with elements (set is locked)";
1203 end if;
1205 Node.Element := New_Item;
1206 end Replace;
1208 procedure Replace_Element
1209 (Container : in out Set;
1210 Position : Cursor;
1211 New_Item : Element_Type)
1213 begin
1214 if Position.Node = null then
1215 raise Constraint_Error with
1216 "Position cursor equals No_Element";
1217 end if;
1219 if Position.Container /= Container'Unrestricted_Access then
1220 raise Program_Error with
1221 "Position cursor designates wrong set";
1222 end if;
1224 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
1226 Replace_Element (Container.HT, Position.Node, New_Item);
1227 end Replace_Element;
1229 ----------------------
1230 -- Reserve_Capacity --
1231 ----------------------
1233 procedure Reserve_Capacity
1234 (Container : in out Set;
1235 Capacity : Count_Type)
1237 begin
1238 HT_Ops.Reserve_Capacity (Container.HT, Capacity);
1239 end Reserve_Capacity;
1241 --------------
1242 -- Set_Next --
1243 --------------
1245 procedure Set_Next (Node : Node_Access; Next : Node_Access) is
1246 begin
1247 Node.Next := Next;
1248 end Set_Next;
1250 --------------------------
1251 -- Symmetric_Difference --
1252 --------------------------
1254 procedure Symmetric_Difference
1255 (Target : in out Set;
1256 Source : Set)
1258 begin
1259 if Target'Address = Source'Address then
1260 Clear (Target);
1261 return;
1262 end if;
1264 if Target.HT.Busy > 0 then
1265 raise Program_Error with
1266 "attempt to tamper with cursors (set is busy)";
1267 end if;
1269 declare
1270 N : constant Count_Type := Target.Length + Source.Length;
1271 begin
1272 if N > HT_Ops.Capacity (Target.HT) then
1273 HT_Ops.Reserve_Capacity (Target.HT, N);
1274 end if;
1275 end;
1277 if Target.Length = 0 then
1278 Iterate_Source_When_Empty_Target : declare
1279 procedure Process (Src_Node : Node_Access);
1281 procedure Iterate is
1282 new HT_Ops.Generic_Iteration (Process);
1284 -------------
1285 -- Process --
1286 -------------
1288 procedure Process (Src_Node : Node_Access) is
1289 E : Element_Type renames Src_Node.Element;
1290 B : Buckets_Type renames Target.HT.Buckets.all;
1291 J : constant Hash_Type := Hash (E) mod B'Length;
1292 N : Count_Type renames Target.HT.Length;
1294 begin
1295 B (J) := new Node_Type'(E, B (J));
1296 N := N + 1;
1297 end Process;
1299 -- Start of processing for Iterate_Source_When_Empty_Target
1301 begin
1302 Iterate (Source.HT);
1303 end Iterate_Source_When_Empty_Target;
1305 else
1306 Iterate_Source : declare
1307 procedure Process (Src_Node : Node_Access);
1309 procedure Iterate is
1310 new HT_Ops.Generic_Iteration (Process);
1312 -------------
1313 -- Process --
1314 -------------
1316 procedure Process (Src_Node : Node_Access) is
1317 E : Element_Type renames Src_Node.Element;
1318 B : Buckets_Type renames Target.HT.Buckets.all;
1319 J : constant Hash_Type := Hash (E) mod B'Length;
1320 N : Count_Type renames Target.HT.Length;
1322 begin
1323 if B (J) = null then
1324 B (J) := new Node_Type'(E, null);
1325 N := N + 1;
1327 elsif Equivalent_Elements (E, B (J).Element) then
1328 declare
1329 X : Node_Access := B (J);
1330 begin
1331 B (J) := B (J).Next;
1332 N := N - 1;
1333 Free (X);
1334 end;
1336 else
1337 declare
1338 Prev : Node_Access := B (J);
1339 Curr : Node_Access := Prev.Next;
1341 begin
1342 while Curr /= null loop
1343 if Equivalent_Elements (E, Curr.Element) then
1344 Prev.Next := Curr.Next;
1345 N := N - 1;
1346 Free (Curr);
1347 return;
1348 end if;
1350 Prev := Curr;
1351 Curr := Prev.Next;
1352 end loop;
1354 B (J) := new Node_Type'(E, B (J));
1355 N := N + 1;
1356 end;
1357 end if;
1358 end Process;
1360 -- Start of processing for Iterate_Source
1362 begin
1363 Iterate (Source.HT);
1364 end Iterate_Source;
1365 end if;
1366 end Symmetric_Difference;
1368 function Symmetric_Difference (Left, Right : Set) return Set is
1369 Buckets : HT_Types.Buckets_Access;
1370 Length : Count_Type;
1372 begin
1373 if Left'Address = Right'Address then
1374 return Empty_Set;
1375 end if;
1377 if Right.Length = 0 then
1378 return Left;
1379 end if;
1381 if Left.Length = 0 then
1382 return Right;
1383 end if;
1385 declare
1386 Size : constant Hash_Type :=
1387 Prime_Numbers.To_Prime (Left.Length + Right.Length);
1388 begin
1389 Buckets := HT_Ops.New_Buckets (Length => Size);
1390 end;
1392 Length := 0;
1394 Iterate_Left : declare
1395 procedure Process (L_Node : Node_Access);
1397 procedure Iterate is
1398 new HT_Ops.Generic_Iteration (Process);
1400 -------------
1401 -- Process --
1402 -------------
1404 procedure Process (L_Node : Node_Access) is
1405 begin
1406 if not Is_In (Right.HT, L_Node) then
1407 declare
1408 E : Element_Type renames L_Node.Element;
1409 J : constant Hash_Type := Hash (E) mod Buckets'Length;
1411 begin
1412 Buckets (J) := new Node_Type'(E, Buckets (J));
1413 Length := Length + 1;
1414 end;
1415 end if;
1416 end Process;
1418 -- Start of processing for Iterate_Left
1420 begin
1421 Iterate (Left.HT);
1422 exception
1423 when others =>
1424 HT_Ops.Free_Hash_Table (Buckets);
1425 raise;
1426 end Iterate_Left;
1428 Iterate_Right : declare
1429 procedure Process (R_Node : Node_Access);
1431 procedure Iterate is
1432 new HT_Ops.Generic_Iteration (Process);
1434 -------------
1435 -- Process --
1436 -------------
1438 procedure Process (R_Node : Node_Access) is
1439 begin
1440 if not Is_In (Left.HT, R_Node) then
1441 declare
1442 E : Element_Type renames R_Node.Element;
1443 J : constant Hash_Type := Hash (E) mod Buckets'Length;
1445 begin
1446 Buckets (J) := new Node_Type'(E, Buckets (J));
1447 Length := Length + 1;
1448 end;
1449 end if;
1450 end Process;
1452 -- Start of processing for Iterate_Right
1454 begin
1455 Iterate (Right.HT);
1456 exception
1457 when others =>
1458 HT_Ops.Free_Hash_Table (Buckets);
1459 raise;
1460 end Iterate_Right;
1462 return (Controlled with HT => (Buckets, Length, 0, 0));
1463 end Symmetric_Difference;
1465 ------------
1466 -- To_Set --
1467 ------------
1469 function To_Set (New_Item : Element_Type) return Set is
1470 HT : Hash_Table_Type;
1472 Node : Node_Access;
1473 Inserted : Boolean;
1474 pragma Unreferenced (Node, Inserted);
1476 begin
1477 Insert (HT, New_Item, Node, Inserted);
1478 return Set'(Controlled with HT);
1479 end To_Set;
1481 -----------
1482 -- Union --
1483 -----------
1485 procedure Union
1486 (Target : in out Set;
1487 Source : Set)
1489 procedure Process (Src_Node : Node_Access);
1491 procedure Iterate is
1492 new HT_Ops.Generic_Iteration (Process);
1494 -------------
1495 -- Process --
1496 -------------
1498 procedure Process (Src_Node : Node_Access) is
1499 function New_Node (Next : Node_Access) return Node_Access;
1500 pragma Inline (New_Node);
1502 procedure Insert is
1503 new Element_Keys.Generic_Conditional_Insert (New_Node);
1505 --------------
1506 -- New_Node --
1507 --------------
1509 function New_Node (Next : Node_Access) return Node_Access is
1510 Node : constant Node_Access :=
1511 new Node_Type'(Src_Node.Element, Next);
1512 begin
1513 return Node;
1514 end New_Node;
1516 Tgt_Node : Node_Access;
1517 Success : Boolean;
1518 pragma Unreferenced (Tgt_Node, Success);
1520 -- Start of processing for Process
1522 begin
1523 Insert (Target.HT, Src_Node.Element, Tgt_Node, Success);
1524 end Process;
1526 -- Start of processing for Union
1528 begin
1529 if Target'Address = Source'Address then
1530 return;
1531 end if;
1533 if Target.HT.Busy > 0 then
1534 raise Program_Error with
1535 "attempt to tamper with cursors (set is busy)";
1536 end if;
1538 declare
1539 N : constant Count_Type := Target.Length + Source.Length;
1540 begin
1541 if N > HT_Ops.Capacity (Target.HT) then
1542 HT_Ops.Reserve_Capacity (Target.HT, N);
1543 end if;
1544 end;
1546 Iterate (Source.HT);
1547 end Union;
1549 function Union (Left, Right : Set) return Set is
1550 Buckets : HT_Types.Buckets_Access;
1551 Length : Count_Type;
1553 begin
1554 if Left'Address = Right'Address then
1555 return Left;
1556 end if;
1558 if Right.Length = 0 then
1559 return Left;
1560 end if;
1562 if Left.Length = 0 then
1563 return Right;
1564 end if;
1566 declare
1567 Size : constant Hash_Type :=
1568 Prime_Numbers.To_Prime (Left.Length + Right.Length);
1569 begin
1570 Buckets := HT_Ops.New_Buckets (Length => Size);
1571 end;
1573 Iterate_Left : declare
1574 procedure Process (L_Node : Node_Access);
1576 procedure Iterate is
1577 new HT_Ops.Generic_Iteration (Process);
1579 -------------
1580 -- Process --
1581 -------------
1583 procedure Process (L_Node : Node_Access) is
1584 J : constant Hash_Type :=
1585 Hash (L_Node.Element) mod Buckets'Length;
1587 begin
1588 Buckets (J) := new Node_Type'(L_Node.Element, Buckets (J));
1589 end Process;
1591 -- Start of processing for Iterate_Left
1593 begin
1594 Iterate (Left.HT);
1595 exception
1596 when others =>
1597 HT_Ops.Free_Hash_Table (Buckets);
1598 raise;
1599 end Iterate_Left;
1601 Length := Left.Length;
1603 Iterate_Right : declare
1604 procedure Process (Src_Node : Node_Access);
1606 procedure Iterate is
1607 new HT_Ops.Generic_Iteration (Process);
1609 -------------
1610 -- Process --
1611 -------------
1613 procedure Process (Src_Node : Node_Access) is
1614 J : constant Hash_Type :=
1615 Hash (Src_Node.Element) mod Buckets'Length;
1617 Tgt_Node : Node_Access := Buckets (J);
1619 begin
1620 while Tgt_Node /= null loop
1621 if Equivalent_Elements (Src_Node.Element, Tgt_Node.Element) then
1622 return;
1623 end if;
1625 Tgt_Node := Next (Tgt_Node);
1626 end loop;
1628 Buckets (J) := new Node_Type'(Src_Node.Element, Buckets (J));
1629 Length := Length + 1;
1630 end Process;
1632 -- Start of processing for Iterate_Right
1634 begin
1635 Iterate (Right.HT);
1636 exception
1637 when others =>
1638 HT_Ops.Free_Hash_Table (Buckets);
1639 raise;
1640 end Iterate_Right;
1642 return (Controlled with HT => (Buckets, Length, 0, 0));
1643 end Union;
1645 ---------
1646 -- Vet --
1647 ---------
1649 function Vet (Position : Cursor) return Boolean is
1650 begin
1651 if Position.Node = null then
1652 return Position.Container = null;
1653 end if;
1655 if Position.Container = null then
1656 return False;
1657 end if;
1659 if Position.Node.Next = Position.Node then
1660 return False;
1661 end if;
1663 declare
1664 HT : Hash_Table_Type renames Position.Container.HT;
1665 X : Node_Access;
1667 begin
1668 if HT.Length = 0 then
1669 return False;
1670 end if;
1672 if HT.Buckets = null
1673 or else HT.Buckets'Length = 0
1674 then
1675 return False;
1676 end if;
1678 X := HT.Buckets (Element_Keys.Index (HT, Position.Node.Element));
1680 for J in 1 .. HT.Length loop
1681 if X = Position.Node then
1682 return True;
1683 end if;
1685 if X = null then
1686 return False;
1687 end if;
1689 if X = X.Next then -- to prevent unnecessary looping
1690 return False;
1691 end if;
1693 X := X.Next;
1694 end loop;
1696 return False;
1697 end;
1698 end Vet;
1700 -----------
1701 -- Write --
1702 -----------
1704 procedure Write
1705 (Stream : not null access Root_Stream_Type'Class;
1706 Container : Set)
1708 begin
1709 Write_Nodes (Stream, Container.HT);
1710 end Write;
1712 procedure Write
1713 (Stream : not null access Root_Stream_Type'Class;
1714 Item : Cursor)
1716 begin
1717 raise Program_Error with "attempt to stream set cursor";
1718 end Write;
1720 procedure Write
1721 (Stream : not null access Root_Stream_Type'Class;
1722 Item : Constant_Reference_Type)
1724 begin
1725 raise Program_Error with "attempt to stream reference";
1726 end Write;
1728 ----------------
1729 -- Write_Node --
1730 ----------------
1732 procedure Write_Node
1733 (Stream : not null access Root_Stream_Type'Class;
1734 Node : Node_Access)
1736 begin
1737 Element_Type'Write (Stream, Node.Element);
1738 end Write_Node;
1740 package body Generic_Keys is
1742 -----------------------
1743 -- Local Subprograms --
1744 -----------------------
1746 function Equivalent_Key_Node
1747 (Key : Key_Type;
1748 Node : Node_Access) return Boolean;
1749 pragma Inline (Equivalent_Key_Node);
1751 --------------------------
1752 -- Local Instantiations --
1753 --------------------------
1755 package Key_Keys is
1756 new Hash_Tables.Generic_Keys
1757 (HT_Types => HT_Types,
1758 Next => Next,
1759 Set_Next => Set_Next,
1760 Key_Type => Key_Type,
1761 Hash => Hash,
1762 Equivalent_Keys => Equivalent_Key_Node);
1764 ------------------------
1765 -- Constant_Reference --
1766 ------------------------
1768 function Constant_Reference
1769 (Container : aliased Set;
1770 Key : Key_Type) return Constant_Reference_Type
1772 Node : constant Node_Access := Key_Keys.Find (Container.HT, Key);
1774 begin
1775 if Node = null then
1776 raise Constraint_Error with "Key not in set";
1777 end if;
1779 declare
1780 HT : Hash_Table_Type renames Container'Unrestricted_Access.all.HT;
1781 B : Natural renames HT.Busy;
1782 L : Natural renames HT.Lock;
1783 begin
1784 return R : constant Constant_Reference_Type :=
1785 (Element => Node.Element'Access,
1786 Control => (Controlled with Container'Unrestricted_Access))
1788 B := B + 1;
1789 L := L + 1;
1790 end return;
1791 end;
1792 end Constant_Reference;
1794 --------------
1795 -- Contains --
1796 --------------
1798 function Contains
1799 (Container : Set;
1800 Key : Key_Type) return Boolean
1802 begin
1803 return Find (Container, Key) /= No_Element;
1804 end Contains;
1806 ------------
1807 -- Delete --
1808 ------------
1810 procedure Delete
1811 (Container : in out Set;
1812 Key : Key_Type)
1814 X : Node_Access;
1816 begin
1817 Key_Keys.Delete_Key_Sans_Free (Container.HT, Key, X);
1819 if X = null then
1820 raise Constraint_Error with "attempt to delete key not in set";
1821 end if;
1823 Free (X);
1824 end Delete;
1826 -------------
1827 -- Element --
1828 -------------
1830 function Element
1831 (Container : Set;
1832 Key : Key_Type) return Element_Type
1834 Node : constant Node_Access := Key_Keys.Find (Container.HT, Key);
1836 begin
1837 if Node = null then
1838 raise Constraint_Error with "key not in map"; -- ??? "set"
1839 end if;
1841 return Node.Element;
1842 end Element;
1844 -------------------------
1845 -- Equivalent_Key_Node --
1846 -------------------------
1848 function Equivalent_Key_Node
1849 (Key : Key_Type;
1850 Node : Node_Access) return Boolean
1852 begin
1853 return Equivalent_Keys (Key, Generic_Keys.Key (Node.Element));
1854 end Equivalent_Key_Node;
1856 -------------
1857 -- Exclude --
1858 -------------
1860 procedure Exclude
1861 (Container : in out Set;
1862 Key : Key_Type)
1864 X : Node_Access;
1865 begin
1866 Key_Keys.Delete_Key_Sans_Free (Container.HT, Key, X);
1867 Free (X);
1868 end Exclude;
1870 ----------
1871 -- Find --
1872 ----------
1874 function Find
1875 (Container : Set;
1876 Key : Key_Type) return Cursor
1878 Node : constant Node_Access := Key_Keys.Find (Container.HT, Key);
1880 begin
1881 if Node = null then
1882 return No_Element;
1883 end if;
1885 return Cursor'(Container'Unrestricted_Access, Node);
1886 end Find;
1888 ---------
1889 -- Key --
1890 ---------
1892 function Key (Position : Cursor) return Key_Type is
1893 begin
1894 if Position.Node = null then
1895 raise Constraint_Error with
1896 "Position cursor equals No_Element";
1897 end if;
1899 pragma Assert (Vet (Position), "bad cursor in function Key");
1901 return Key (Position.Node.Element);
1902 end Key;
1904 ----------
1905 -- Read --
1906 ----------
1908 procedure Read
1909 (Stream : not null access Root_Stream_Type'Class;
1910 Item : out Reference_Type)
1912 begin
1913 raise Program_Error with "attempt to stream reference";
1914 end Read;
1916 ------------------------------
1917 -- Reference_Preserving_Key --
1918 ------------------------------
1920 function Reference_Preserving_Key
1921 (Container : aliased in out Set;
1922 Position : Cursor) return Reference_Type
1924 begin
1925 if Position.Container = null then
1926 raise Constraint_Error with "Position cursor has no element";
1927 end if;
1929 if Position.Container /= Container'Unrestricted_Access then
1930 raise Program_Error with
1931 "Position cursor designates wrong container";
1932 end if;
1934 pragma Assert
1935 (Vet (Position),
1936 "bad cursor in function Reference_Preserving_Key");
1938 -- Some form of finalization will be required in order to actually
1939 -- check that the key-part of the element designated by Position has
1940 -- not changed. ???
1942 return (Element => Position.Node.Element'Access);
1943 end Reference_Preserving_Key;
1945 function Reference_Preserving_Key
1946 (Container : aliased in out Set;
1947 Key : Key_Type) return Reference_Type
1949 Node : constant Node_Access := Key_Keys.Find (Container.HT, Key);
1951 begin
1952 if Node = null then
1953 raise Constraint_Error with "Key not in set";
1954 end if;
1956 -- Some form of finalization will be required in order to actually
1957 -- check that the key-part of the element designated by Key has not
1958 -- changed. ???
1960 return (Element => Node.Element'Access);
1961 end Reference_Preserving_Key;
1963 -------------
1964 -- Replace --
1965 -------------
1967 procedure Replace
1968 (Container : in out Set;
1969 Key : Key_Type;
1970 New_Item : Element_Type)
1972 Node : constant Node_Access := Key_Keys.Find (Container.HT, Key);
1974 begin
1975 if Node = null then
1976 raise Constraint_Error with
1977 "attempt to replace key not in set";
1978 end if;
1980 Replace_Element (Container.HT, Node, New_Item);
1981 end Replace;
1983 -----------------------------------
1984 -- Update_Element_Preserving_Key --
1985 -----------------------------------
1987 procedure Update_Element_Preserving_Key
1988 (Container : in out Set;
1989 Position : Cursor;
1990 Process : not null access
1991 procedure (Element : in out Element_Type))
1993 HT : Hash_Table_Type renames Container.HT;
1994 Indx : Hash_Type;
1996 begin
1997 if Position.Node = null then
1998 raise Constraint_Error with
1999 "Position cursor equals No_Element";
2000 end if;
2002 if Position.Container /= Container'Unrestricted_Access then
2003 raise Program_Error with
2004 "Position cursor designates wrong set";
2005 end if;
2007 if HT.Buckets = null
2008 or else HT.Buckets'Length = 0
2009 or else HT.Length = 0
2010 or else Position.Node.Next = Position.Node
2011 then
2012 raise Program_Error with "Position cursor is bad (set is empty)";
2013 end if;
2015 pragma Assert
2016 (Vet (Position),
2017 "bad cursor in Update_Element_Preserving_Key");
2019 Indx := HT_Ops.Index (HT, Position.Node);
2021 declare
2022 E : Element_Type renames Position.Node.Element;
2023 K : constant Key_Type := Key (E);
2025 B : Natural renames HT.Busy;
2026 L : Natural renames HT.Lock;
2028 begin
2029 B := B + 1;
2030 L := L + 1;
2032 begin
2033 Process (E);
2034 exception
2035 when others =>
2036 L := L - 1;
2037 B := B - 1;
2038 raise;
2039 end;
2041 L := L - 1;
2042 B := B - 1;
2044 if Equivalent_Keys (K, Key (E)) then
2045 pragma Assert (Hash (K) = Hash (E));
2046 return;
2047 end if;
2048 end;
2050 if HT.Buckets (Indx) = Position.Node then
2051 HT.Buckets (Indx) := Position.Node.Next;
2053 else
2054 declare
2055 Prev : Node_Access := HT.Buckets (Indx);
2057 begin
2058 while Prev.Next /= Position.Node loop
2059 Prev := Prev.Next;
2061 if Prev = null then
2062 raise Program_Error with
2063 "Position cursor is bad (node not found)";
2064 end if;
2065 end loop;
2067 Prev.Next := Position.Node.Next;
2068 end;
2069 end if;
2071 HT.Length := HT.Length - 1;
2073 declare
2074 X : Node_Access := Position.Node;
2076 begin
2077 Free (X);
2078 end;
2080 raise Program_Error with "key was modified";
2081 end Update_Element_Preserving_Key;
2083 -----------
2084 -- Write --
2085 -----------
2087 procedure Write
2088 (Stream : not null access Root_Stream_Type'Class;
2089 Item : Reference_Type)
2091 begin
2092 raise Program_Error with "attempt to stream reference";
2093 end Write;
2095 end Generic_Keys;
2097 end Ada.Containers.Hashed_Sets;