1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . B O U N D E D _ O R D E R E D _ S E T S --
9 -- Copyright (C) 2004-2024, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- This unit was originally developed by Matthew J Heaney. --
28 ------------------------------------------------------------------------------
30 with Ada.Containers.Helpers; use Ada.Containers.Helpers;
32 with Ada.Containers.Red_Black_Trees.Generic_Bounded_Operations;
34 (Ada.Containers.Red_Black_Trees.Generic_Bounded_Operations);
36 with Ada.Containers.Red_Black_Trees.Generic_Bounded_Keys;
37 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Bounded_Keys);
39 with Ada.Containers.Red_Black_Trees.Generic_Bounded_Set_Operations;
41 (Ada.Containers.Red_Black_Trees.Generic_Bounded_Set_Operations);
43 with Ada.Unchecked_Deallocation;
45 with System; use type System.Address;
46 with System.Put_Images;
48 package body Ada.Containers.Bounded_Ordered_Sets with
52 pragma Warnings (Off, "variable ""Busy*"" is not referenced");
53 pragma Warnings (Off, "variable ""Lock*"" is not referenced");
54 -- See comment in Ada.Containers.Helpers
56 ------------------------------
57 -- Access to Fields of Node --
58 ------------------------------
60 -- These subprograms provide functional notation for access to fields
61 -- of a node, and procedural notation for modifying these fields.
63 function Color (Node : Node_Type) return Red_Black_Trees.Color_Type;
64 pragma Inline (Color);
66 function Left (Node : Node_Type) return Count_Type;
69 function Parent (Node : Node_Type) return Count_Type;
70 pragma Inline (Parent);
72 function Right (Node : Node_Type) return Count_Type;
73 pragma Inline (Right);
76 (Node : in out Node_Type;
77 Color : Red_Black_Trees.Color_Type);
78 pragma Inline (Set_Color);
80 procedure Set_Left (Node : in out Node_Type; Left : Count_Type);
81 pragma Inline (Set_Left);
83 procedure Set_Right (Node : in out Node_Type; Right : Count_Type);
84 pragma Inline (Set_Right);
86 procedure Set_Parent (Node : in out Node_Type; Parent : Count_Type);
87 pragma Inline (Set_Parent);
89 -----------------------
90 -- Local Subprograms --
91 -----------------------
93 procedure Insert_Sans_Hint
94 (Container : in out Set;
95 New_Item : Element_Type;
96 Node : out Count_Type;
97 Inserted : out Boolean);
99 procedure Insert_With_Hint
100 (Dst_Set : in out Set;
101 Dst_Hint : Count_Type;
102 Src_Node : Node_Type;
103 Dst_Node : out Count_Type);
105 function Is_Greater_Element_Node
106 (Left : Element_Type;
107 Right : Node_Type) return Boolean;
108 pragma Inline (Is_Greater_Element_Node);
110 function Is_Less_Element_Node
111 (Left : Element_Type;
112 Right : Node_Type) return Boolean;
113 pragma Inline (Is_Less_Element_Node);
115 function Is_Less_Node_Node (L, R : Node_Type) return Boolean;
116 pragma Inline (Is_Less_Node_Node);
118 procedure Replace_Element
119 (Container : in out Set;
121 Item : Element_Type);
123 --------------------------
124 -- Local Instantiations --
125 --------------------------
127 package Tree_Operations is
128 new Red_Black_Trees.Generic_Bounded_Operations (Tree_Types);
132 package Element_Keys is
133 new Red_Black_Trees.Generic_Bounded_Keys
134 (Tree_Operations => Tree_Operations,
135 Key_Type => Element_Type,
136 Is_Less_Key_Node => Is_Less_Element_Node,
137 Is_Greater_Key_Node => Is_Greater_Element_Node);
140 new Red_Black_Trees.Generic_Bounded_Set_Operations
141 (Tree_Operations => Tree_Operations,
144 Insert_With_Hint => Insert_With_Hint,
145 Is_Less => Is_Less_Node_Node);
151 function "<" (Left, Right : Cursor) return Boolean is
153 if Checks and then Left.Node = 0 then
154 raise Constraint_Error with "Left cursor equals No_Element";
157 if Checks and then Right.Node = 0 then
158 raise Constraint_Error with "Right cursor equals No_Element";
161 pragma Assert (Vet (Left.Container.all, Left.Node),
162 "bad Left cursor in ""<""");
164 pragma Assert (Vet (Right.Container.all, Right.Node),
165 "bad Right cursor in ""<""");
168 LN : Nodes_Type renames Left.Container.Nodes;
169 RN : Nodes_Type renames Right.Container.Nodes;
171 return LN (Left.Node).Element < RN (Right.Node).Element;
175 function "<" (Left : Cursor; Right : Element_Type) return Boolean is
177 if Checks and then Left.Node = 0 then
178 raise Constraint_Error with "Left cursor equals No_Element";
181 pragma Assert (Vet (Left.Container.all, Left.Node),
182 "bad Left cursor in ""<""");
184 return Left.Container.Nodes (Left.Node).Element < Right;
187 function "<" (Left : Element_Type; Right : Cursor) return Boolean is
189 if Checks and then Right.Node = 0 then
190 raise Constraint_Error with "Right cursor equals No_Element";
193 pragma Assert (Vet (Right.Container.all, Right.Node),
194 "bad Right cursor in ""<""");
196 return Left < Right.Container.Nodes (Right.Node).Element;
203 function "=" (Left, Right : Set) return Boolean is
204 function Is_Equal_Node_Node (L, R : Node_Type) return Boolean;
205 pragma Inline (Is_Equal_Node_Node);
208 new Tree_Operations.Generic_Equal (Is_Equal_Node_Node);
210 ------------------------
211 -- Is_Equal_Node_Node --
212 ------------------------
214 function Is_Equal_Node_Node (L, R : Node_Type) return Boolean is
216 return L.Element = R.Element;
217 end Is_Equal_Node_Node;
219 -- Start of processing for Is_Equal
222 return Is_Equal (Left, Right);
229 function ">" (Left, Right : Cursor) return Boolean is
231 if Checks and then Left.Node = 0 then
232 raise Constraint_Error with "Left cursor equals No_Element";
235 if Checks and then Right.Node = 0 then
236 raise Constraint_Error with "Right cursor equals No_Element";
239 pragma Assert (Vet (Left.Container.all, Left.Node),
240 "bad Left cursor in "">""");
242 pragma Assert (Vet (Right.Container.all, Right.Node),
243 "bad Right cursor in "">""");
245 -- L > R same as R < L
248 LN : Nodes_Type renames Left.Container.Nodes;
249 RN : Nodes_Type renames Right.Container.Nodes;
251 return RN (Right.Node).Element < LN (Left.Node).Element;
255 function ">" (Left : Element_Type; Right : Cursor) return Boolean is
257 if Checks and then Right.Node = 0 then
258 raise Constraint_Error with "Right cursor equals No_Element";
261 pragma Assert (Vet (Right.Container.all, Right.Node),
262 "bad Right cursor in "">""");
264 return Right.Container.Nodes (Right.Node).Element < Left;
267 function ">" (Left : Cursor; Right : Element_Type) return Boolean is
269 if Checks and then Left.Node = 0 then
270 raise Constraint_Error with "Left cursor equals No_Element";
273 pragma Assert (Vet (Left.Container.all, Left.Node),
274 "bad Left cursor in "">""");
276 return Right < Left.Container.Nodes (Left.Node).Element;
283 procedure Assign (Target : in out Set; Source : Set) is
284 procedure Append_Element (Source_Node : Count_Type);
286 procedure Append_Elements is
287 new Tree_Operations.Generic_Iteration (Append_Element);
293 procedure Append_Element (Source_Node : Count_Type) is
294 SN : Node_Type renames Source.Nodes (Source_Node);
296 procedure Set_Element (Node : in out Node_Type);
297 pragma Inline (Set_Element);
299 function New_Node return Count_Type;
300 pragma Inline (New_Node);
302 procedure Insert_Post is
303 new Element_Keys.Generic_Insert_Post (New_Node);
305 procedure Unconditional_Insert_Sans_Hint is
306 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
308 procedure Unconditional_Insert_Avec_Hint is
309 new Element_Keys.Generic_Unconditional_Insert_With_Hint
311 Unconditional_Insert_Sans_Hint);
313 procedure Allocate is
314 new Tree_Operations.Generic_Allocate (Set_Element);
320 function New_Node return Count_Type is
323 Allocate (Target, Result);
331 procedure Set_Element (Node : in out Node_Type) is
333 Node.Element := SN.Element;
336 Target_Node : Count_Type;
338 -- Start of processing for Append_Element
341 Unconditional_Insert_Avec_Hint
345 Node => Target_Node);
348 -- Start of processing for Assign
351 if Target'Address = Source'Address then
355 if Checks and then Target.Capacity < Source.Length then
357 with "Target capacity is less than Source length";
361 Append_Elements (Source);
368 function Ceiling (Container : Set; Item : Element_Type) return Cursor is
369 Node : constant Count_Type :=
370 Element_Keys.Ceiling (Container, Item);
372 return (if Node = 0 then No_Element
373 else Cursor'(Container'Unrestricted_Access, Node));
380 procedure Clear (Container : in out Set) is
382 while not Container.Is_Empty loop
383 Container.Delete_Last;
391 function Color (Node : Node_Type) return Red_Black_Trees.Color_Type is
396 ------------------------
397 -- Constant_Reference --
398 ------------------------
400 function Constant_Reference
401 (Container : aliased Set;
402 Position : Cursor) return Constant_Reference_Type
405 if Checks and then Position.Container = null then
406 raise Constraint_Error with "Position cursor has no element";
409 if Checks and then Position.Container /= Container'Unrestricted_Access
411 raise Program_Error with
412 "Position cursor designates wrong container";
416 (Vet (Container, Position.Node),
417 "bad cursor in Constant_Reference");
420 N : Node_Type renames Container.Nodes (Position.Node);
421 TC : constant Tamper_Counts_Access :=
422 Container.TC'Unrestricted_Access;
424 return R : constant Constant_Reference_Type :=
425 (Element => N.Element'Unchecked_Access,
426 Control => (Controlled with TC))
431 end Constant_Reference;
439 Item : Element_Type) return Boolean
442 return Find (Container, Item) /= No_Element;
449 function Copy (Source : Set; Capacity : Count_Type := 0) return Set is
450 C : constant Count_Type :=
451 (if Capacity = 0 then Source.Length
454 if Checks and then C < Source.Length then
455 raise Capacity_Error with "Capacity too small";
458 return Target : Set (Capacity => C) do
459 Assign (Target => Target, Source => Source);
467 procedure Delete (Container : in out Set; Position : in out Cursor) is
469 TC_Check (Container.TC);
471 if Checks and then Position.Node = 0 then
472 raise Constraint_Error with "Position cursor equals No_Element";
475 if Checks and then Position.Container /= Container'Unrestricted_Access
477 raise Program_Error with "Position cursor designates wrong set";
480 pragma Assert (Vet (Container, Position.Node),
481 "bad cursor in Delete");
483 Tree_Operations.Delete_Node_Sans_Free (Container, Position.Node);
484 Tree_Operations.Free (Container, Position.Node);
486 Position := No_Element;
489 procedure Delete (Container : in out Set; Item : Element_Type) is
490 X : constant Count_Type := Element_Keys.Find (Container, Item);
493 Tree_Operations.Delete_Node_Sans_Free (Container, X);
495 if Checks and then X = 0 then
496 raise Constraint_Error with "attempt to delete element not in set";
499 Tree_Operations.Free (Container, X);
506 procedure Delete_First (Container : in out Set) is
507 X : constant Count_Type := Container.First;
510 Tree_Operations.Delete_Node_Sans_Free (Container, X);
511 Tree_Operations.Free (Container, X);
519 procedure Delete_Last (Container : in out Set) is
520 X : constant Count_Type := Container.Last;
523 Tree_Operations.Delete_Node_Sans_Free (Container, X);
524 Tree_Operations.Free (Container, X);
532 procedure Difference (Target : in out Set; Source : Set)
533 renames Set_Ops.Set_Difference;
535 function Difference (Left, Right : Set) return Set
536 renames Set_Ops.Set_Difference;
542 function Element (Position : Cursor) return Element_Type is
544 if Checks and then Position.Node = 0 then
545 raise Constraint_Error with "Position cursor equals No_Element";
548 pragma Assert (Vet (Position.Container.all, Position.Node),
549 "bad cursor in Element");
551 return Position.Container.Nodes (Position.Node).Element;
558 function Empty (Capacity : Count_Type := 10) return Set is
560 return Result : Set (Capacity) do
565 -------------------------
566 -- Equivalent_Elements --
567 -------------------------
569 function Equivalent_Elements (Left, Right : Element_Type) return Boolean is
571 return (if Left < Right or else Right < Left then False else True);
572 end Equivalent_Elements;
574 ---------------------
575 -- Equivalent_Sets --
576 ---------------------
578 function Equivalent_Sets (Left, Right : Set) return Boolean is
579 function Is_Equivalent_Node_Node (L, R : Node_Type) return Boolean;
580 pragma Inline (Is_Equivalent_Node_Node);
582 function Is_Equivalent is
583 new Tree_Operations.Generic_Equal (Is_Equivalent_Node_Node);
585 -----------------------------
586 -- Is_Equivalent_Node_Node --
587 -----------------------------
589 function Is_Equivalent_Node_Node (L, R : Node_Type) return Boolean is
591 return (if L.Element < R.Element then False
592 elsif R.Element < L.Element then False
594 end Is_Equivalent_Node_Node;
596 -- Start of processing for Equivalent_Sets
599 return Is_Equivalent (Left, Right);
606 procedure Exclude (Container : in out Set; Item : Element_Type) is
607 X : constant Count_Type := Element_Keys.Find (Container, Item);
610 Tree_Operations.Delete_Node_Sans_Free (Container, X);
611 Tree_Operations.Free (Container, X);
619 procedure Finalize (Object : in out Iterator) is
621 if Object.Container /= null then
622 Unbusy (Object.Container.TC);
630 function Find (Container : Set; Item : Element_Type) return Cursor is
631 Node : constant Count_Type := Element_Keys.Find (Container, Item);
633 return (if Node = 0 then No_Element
634 else Cursor'(Container'Unrestricted_Access, Node));
641 function First (Container : Set) return Cursor is
643 return (if Container.First = 0 then No_Element
644 else Cursor'(Container'Unrestricted_Access, Container.First));
647 function First (Object : Iterator) return Cursor is
649 -- The value of the iterator object's Node component influences the
650 -- behavior of the First (and Last) selector function.
652 -- When the Node component is 0, this means the iterator object was
653 -- constructed without a start expression, in which case the (forward)
654 -- iteration starts from the (logical) beginning of the entire sequence
655 -- of items (corresponding to Container.First, for a forward iterator).
657 -- Otherwise, this is iteration over a partial sequence of items. When
658 -- the Node component is positive, the iterator object was constructed
659 -- with a start expression, that specifies the position from which the
660 -- (forward) partial iteration begins.
662 if Object.Node = 0 then
663 return Bounded_Ordered_Sets.First (Object.Container.all);
665 return Cursor'(Object.Container, Object.Node);
673 function First_Element (Container : Set) return Element_Type is
675 if Checks and then Container.First = 0 then
676 raise Constraint_Error with "set is empty";
679 return Container.Nodes (Container.First).Element;
686 function Floor (Container : Set; Item : Element_Type) return Cursor is
687 Node : constant Count_Type := Element_Keys.Floor (Container, Item);
689 return (if Node = 0 then No_Element
690 else Cursor'(Container'Unrestricted_Access, Node));
693 -- Ada 2022 features:
695 function Has_Element (Container : Set; Position : Cursor) return Boolean is
698 (Position.Container = null or else Vet (Container, Position.Node),
699 "bad cursor in Has_Element");
700 pragma Assert ((Position.Container = null) = (Position.Node = 0),
701 "bad nullity in Has_Element");
702 return Position.Container = Container'Unrestricted_Access;
705 function Tampering_With_Cursors_Prohibited
706 (Container : Set) return Boolean
709 return Is_Busy (Container.TC);
710 end Tampering_With_Cursors_Prohibited;
712 function Element (Container : Set; Position : Cursor) return Element_Type is
714 if Checks and then not Has_Element (Container, Position) then
715 raise Program_Error with "Position for wrong Container";
718 return Element (Position);
721 procedure Query_Element
724 Process : not null access procedure (Element : Element_Type)) is
726 if Checks and then not Has_Element (Container, Position) then
727 raise Program_Error with "Position for wrong Container";
730 Query_Element (Position, Process);
733 function Next (Container : Set; Position : Cursor) return Cursor is
736 not (Position = No_Element or else Has_Element (Container, Position))
738 raise Program_Error with "Position for wrong Container";
741 return Next (Position);
744 procedure Next (Container : Set; Position : in out Cursor) is
746 Position := Next (Container, Position);
753 package body Generic_Keys is
755 -----------------------
756 -- Local Subprograms --
757 -----------------------
759 function Is_Greater_Key_Node
761 Right : Node_Type) return Boolean;
762 pragma Inline (Is_Greater_Key_Node);
764 function Is_Less_Key_Node
766 Right : Node_Type) return Boolean;
767 pragma Inline (Is_Less_Key_Node);
769 --------------------------
770 -- Local Instantiations --
771 --------------------------
774 new Red_Black_Trees.Generic_Bounded_Keys
775 (Tree_Operations => Tree_Operations,
776 Key_Type => Key_Type,
777 Is_Less_Key_Node => Is_Less_Key_Node,
778 Is_Greater_Key_Node => Is_Greater_Key_Node);
784 procedure Adjust (Control : in out Reference_Control_Type) is
786 Impl.Reference_Control_Type (Control).Adjust;
787 if Control.Old_Key /= null then
788 Control.Old_Key := new Key_Type'(Control.Old_Key.all);
796 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
797 Node : constant Count_Type :=
798 Key_Keys.Ceiling (Container, Key);
800 return (if Node = 0 then No_Element
801 else Cursor'(Container'Unrestricted_Access, Node));
804 ------------------------
805 -- Constant_Reference --
806 ------------------------
808 function Constant_Reference
809 (Container : aliased Set;
810 Key : Key_Type) return Constant_Reference_Type
812 Position : constant Cursor := Find (Container, Key);
815 if Checks and then Position = No_Element then
816 raise Constraint_Error with "key not in set";
819 return Constant_Reference (Container, Position);
820 end Constant_Reference;
826 function Contains (Container : Set; Key : Key_Type) return Boolean is
828 return Find (Container, Key) /= No_Element;
835 procedure Delete (Container : in out Set; Key : Key_Type) is
836 X : constant Count_Type := Key_Keys.Find (Container, Key);
839 if Checks and then X = 0 then
840 raise Constraint_Error with "attempt to delete key not in set";
843 Tree_Operations.Delete_Node_Sans_Free (Container, X);
844 Tree_Operations.Free (Container, X);
851 function Element (Container : Set; Key : Key_Type) return Element_Type is
852 Node : constant Count_Type := Key_Keys.Find (Container, Key);
855 if Checks and then Node = 0 then
856 raise Constraint_Error with "key not in set";
859 return Container.Nodes (Node).Element;
862 ---------------------
863 -- Equivalent_Keys --
864 ---------------------
866 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
868 return (if Left < Right or else Right < Left then False else True);
875 procedure Exclude (Container : in out Set; Key : Key_Type) is
876 X : constant Count_Type := Key_Keys.Find (Container, Key);
879 Tree_Operations.Delete_Node_Sans_Free (Container, X);
880 Tree_Operations.Free (Container, X);
888 procedure Finalize (Control : in out Reference_Control_Type) is
889 procedure Deallocate is
890 new Ada.Unchecked_Deallocation (Key_Type, Key_Access);
892 if Control.Container /= null then
893 Impl.Reference_Control_Type (Control).Finalize;
895 if Checks and then not (Key (Control.Pos) = Control.Old_Key.all)
897 Delete (Control.Container.all, Key (Control.Pos));
901 Control.Container := null;
902 Deallocate (Control.Old_Key);
910 function Find (Container : Set; Key : Key_Type) return Cursor is
911 Node : constant Count_Type := Key_Keys.Find (Container, Key);
913 return (if Node = 0 then No_Element
914 else Cursor'(Container'Unrestricted_Access, Node));
921 function Floor (Container : Set; Key : Key_Type) return Cursor is
922 Node : constant Count_Type := Key_Keys.Floor (Container, Key);
924 return (if Node = 0 then No_Element
925 else Cursor'(Container'Unrestricted_Access, Node));
928 -------------------------
929 -- Is_Greater_Key_Node --
930 -------------------------
932 function Is_Greater_Key_Node
934 Right : Node_Type) return Boolean
937 return Key (Right.Element) < Left;
938 end Is_Greater_Key_Node;
940 ----------------------
941 -- Is_Less_Key_Node --
942 ----------------------
944 function Is_Less_Key_Node
946 Right : Node_Type) return Boolean
949 return Left < Key (Right.Element);
950 end Is_Less_Key_Node;
956 function Key (Position : Cursor) return Key_Type is
958 if Checks and then Position.Node = 0 then
959 raise Constraint_Error with
960 "Position cursor equals No_Element";
963 pragma Assert (Vet (Position.Container.all, Position.Node),
964 "bad cursor in Key");
966 return Key (Position.Container.Nodes (Position.Node).Element);
974 (Stream : not null access Root_Stream_Type'Class;
975 Item : out Reference_Type)
978 raise Program_Error with "attempt to stream reference";
981 ------------------------------
982 -- Reference_Preserving_Key --
983 ------------------------------
985 function Reference_Preserving_Key
986 (Container : aliased in out Set;
987 Position : Cursor) return Reference_Type
990 if Checks and then Position.Container = null then
991 raise Constraint_Error with "Position cursor has no element";
994 if Checks and then Position.Container /= Container'Unrestricted_Access
996 raise Program_Error with
997 "Position cursor designates wrong container";
1001 (Vet (Container, Position.Node),
1002 "bad cursor in function Reference_Preserving_Key");
1005 N : Node_Type renames Container.Nodes (Position.Node);
1007 return R : constant Reference_Type :=
1008 (Element => N.Element'Unchecked_Access,
1011 Container.TC'Unrestricted_Access,
1012 Container => Container'Unchecked_Access,
1014 Old_Key => new Key_Type'(Key (Position))))
1016 Busy (Container.TC);
1019 end Reference_Preserving_Key;
1021 function Reference_Preserving_Key
1022 (Container : aliased in out Set;
1023 Key : Key_Type) return Reference_Type
1025 Position : constant Cursor := Find (Container, Key);
1028 if Checks and then Position = No_Element then
1029 raise Constraint_Error with "key not in set";
1032 return Reference_Preserving_Key (Container, Position);
1033 end Reference_Preserving_Key;
1040 (Container : in out Set;
1042 New_Item : Element_Type)
1044 Node : constant Count_Type := Key_Keys.Find (Container, Key);
1047 if Checks and then Node = 0 then
1048 raise Constraint_Error with
1049 "attempt to replace key not in set";
1052 Replace_Element (Container, Node, New_Item);
1055 -----------------------------------
1056 -- Update_Element_Preserving_Key --
1057 -----------------------------------
1059 procedure Update_Element_Preserving_Key
1060 (Container : in out Set;
1062 Process : not null access procedure (Element : in out Element_Type))
1065 if Checks and then Position.Node = 0 then
1066 raise Constraint_Error with
1067 "Position cursor equals No_Element";
1070 if Checks and then Position.Container /= Container'Unrestricted_Access
1072 raise Program_Error with
1073 "Position cursor designates wrong set";
1076 pragma Assert (Vet (Container, Position.Node),
1077 "bad cursor in Update_Element_Preserving_Key");
1079 -- Per AI05-0022, the container implementation is required to detect
1080 -- element tampering by a generic actual subprogram.
1083 N : Node_Type renames Container.Nodes (Position.Node);
1084 E : Element_Type renames N.Element;
1085 K : constant Key_Type := Key (E);
1086 Lock : With_Lock (Container.TC'Unrestricted_Access);
1089 if Equivalent_Keys (K, Key (E)) then
1094 Tree_Operations.Delete_Node_Sans_Free (Container, Position.Node);
1095 Tree_Operations.Free (Container, Position.Node);
1097 raise Program_Error with "key was modified";
1098 end Update_Element_Preserving_Key;
1105 (Stream : not null access Root_Stream_Type'Class;
1106 Item : Reference_Type)
1109 raise Program_Error with "attempt to stream reference";
1113 ------------------------
1114 -- Get_Element_Access --
1115 ------------------------
1117 function Get_Element_Access
1118 (Position : Cursor) return not null Element_Access is
1120 return Position.Container.Nodes (Position.Node).Element'Access;
1121 end Get_Element_Access;
1127 function Has_Element (Position : Cursor) return Boolean is
1129 return Position /= No_Element;
1136 procedure Include (Container : in out Set; New_Item : Element_Type) is
1141 Insert (Container, New_Item, Position, Inserted);
1143 if not Inserted then
1144 TE_Check (Container.TC);
1146 Container.Nodes (Position.Node).Element := New_Item;
1155 (Container : in out Set;
1156 New_Item : Element_Type;
1157 Position : out Cursor;
1158 Inserted : out Boolean)
1167 Position.Container := Container'Unrestricted_Access;
1171 (Container : in out Set;
1172 New_Item : Element_Type)
1178 Insert (Container, New_Item, Position, Inserted);
1180 if Checks and then not Inserted then
1181 raise Constraint_Error with
1182 "attempt to insert element already in set";
1186 ----------------------
1187 -- Insert_Sans_Hint --
1188 ----------------------
1190 procedure Insert_Sans_Hint
1191 (Container : in out Set;
1192 New_Item : Element_Type;
1193 Node : out Count_Type;
1194 Inserted : out Boolean)
1196 procedure Set_Element (Node : in out Node_Type);
1197 pragma Inline (Set_Element);
1199 function New_Node return Count_Type;
1200 pragma Inline (New_Node);
1202 procedure Insert_Post is
1203 new Element_Keys.Generic_Insert_Post (New_Node);
1205 procedure Conditional_Insert_Sans_Hint is
1206 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1208 procedure Allocate is
1209 new Tree_Operations.Generic_Allocate (Set_Element);
1215 function New_Node return Count_Type is
1216 Result : Count_Type;
1218 Allocate (Container, Result);
1226 procedure Set_Element (Node : in out Node_Type) is
1228 Node.Element := New_Item;
1231 -- Start of processing for Insert_Sans_Hint
1234 TC_Check (Container.TC);
1236 Conditional_Insert_Sans_Hint
1241 end Insert_Sans_Hint;
1243 ----------------------
1244 -- Insert_With_Hint --
1245 ----------------------
1247 procedure Insert_With_Hint
1248 (Dst_Set : in out Set;
1249 Dst_Hint : Count_Type;
1250 Src_Node : Node_Type;
1251 Dst_Node : out Count_Type)
1255 procedure Set_Element (Node : in out Node_Type);
1256 pragma Inline (Set_Element);
1258 function New_Node return Count_Type;
1259 pragma Inline (New_Node);
1261 procedure Insert_Post is
1262 new Element_Keys.Generic_Insert_Post (New_Node);
1264 procedure Insert_Sans_Hint is
1265 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1267 procedure Local_Insert_With_Hint is
1268 new Element_Keys.Generic_Conditional_Insert_With_Hint
1272 procedure Allocate is
1273 new Tree_Operations.Generic_Allocate (Set_Element);
1279 function New_Node return Count_Type is
1280 Result : Count_Type;
1282 Allocate (Dst_Set, Result);
1290 procedure Set_Element (Node : in out Node_Type) is
1292 Node.Element := Src_Node.Element;
1295 -- Start of processing for Insert_With_Hint
1298 Local_Insert_With_Hint
1304 end Insert_With_Hint;
1310 procedure Intersection (Target : in out Set; Source : Set)
1311 renames Set_Ops.Set_Intersection;
1313 function Intersection (Left, Right : Set) return Set
1314 renames Set_Ops.Set_Intersection;
1320 function Is_Empty (Container : Set) return Boolean is
1322 return Container.Length = 0;
1325 -----------------------------
1326 -- Is_Greater_Element_Node --
1327 -----------------------------
1329 function Is_Greater_Element_Node
1330 (Left : Element_Type;
1331 Right : Node_Type) return Boolean
1334 -- Compute e > node same as node < e
1336 return Right.Element < Left;
1337 end Is_Greater_Element_Node;
1339 --------------------------
1340 -- Is_Less_Element_Node --
1341 --------------------------
1343 function Is_Less_Element_Node
1344 (Left : Element_Type;
1345 Right : Node_Type) return Boolean
1348 return Left < Right.Element;
1349 end Is_Less_Element_Node;
1351 -----------------------
1352 -- Is_Less_Node_Node --
1353 -----------------------
1355 function Is_Less_Node_Node (L, R : Node_Type) return Boolean is
1357 return L.Element < R.Element;
1358 end Is_Less_Node_Node;
1364 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean
1365 renames Set_Ops.Set_Subset;
1373 Process : not null access procedure (Position : Cursor))
1375 procedure Process_Node (Node : Count_Type);
1376 pragma Inline (Process_Node);
1378 procedure Local_Iterate is
1379 new Tree_Operations.Generic_Iteration (Process_Node);
1385 procedure Process_Node (Node : Count_Type) is
1387 Process (Cursor'(Container'Unrestricted_Access, Node));
1390 S : Set renames Container'Unrestricted_Access.all;
1391 Busy : With_Busy (S.TC'Unrestricted_Access);
1393 -- Start of processing for Iterate
1399 function Iterate (Container : Set)
1400 return Set_Iterator_Interfaces.Reversible_Iterator'class
1403 -- The value of the Node component influences the behavior of the First
1404 -- and Last selector functions of the iterator object. When the Node
1405 -- component is 0 (as is the case here), this means the iterator object
1406 -- was constructed without a start expression. This is a complete
1407 -- iterator, meaning that the iteration starts from the (logical)
1408 -- beginning of the sequence of items.
1410 -- Note: For a forward iterator, Container.First is the beginning, and
1411 -- for a reverse iterator, Container.Last is the beginning.
1413 return It : constant Iterator :=
1414 Iterator'(Limited_Controlled with
1415 Container => Container'Unrestricted_Access,
1418 Busy (Container.TC'Unrestricted_Access.all);
1422 function Iterate (Container : Set; Start : Cursor)
1423 return Set_Iterator_Interfaces.Reversible_Iterator'class
1426 -- It was formerly the case that when Start = No_Element, the partial
1427 -- iterator was defined to behave the same as for a complete iterator,
1428 -- and iterate over the entire sequence of items. However, those
1429 -- semantics were unintuitive and arguably error-prone (it is too easy
1430 -- to accidentally create an endless loop), and so they were changed,
1431 -- per the ARG meeting in Denver on 2011/11. However, there was no
1432 -- consensus about what positive meaning this corner case should have,
1433 -- and so it was decided to simply raise an exception. This does imply,
1434 -- however, that it is not possible to use a partial iterator to specify
1435 -- an empty sequence of items.
1437 if Checks and then Start = No_Element then
1438 raise Constraint_Error with
1439 "Start position for iterator equals No_Element";
1442 if Checks and then Start.Container /= Container'Unrestricted_Access then
1443 raise Program_Error with
1444 "Start cursor of Iterate designates wrong set";
1447 pragma Assert (Vet (Container, Start.Node),
1448 "Start cursor of Iterate is bad");
1450 -- The value of the Node component influences the behavior of the First
1451 -- and Last selector functions of the iterator object. When the Node
1452 -- component is positive (as is the case here), it means that this
1453 -- is a partial iteration, over a subset of the complete sequence of
1454 -- items. The iterator object was constructed with a start expression,
1455 -- indicating the position from which the iteration begins. (Note that
1456 -- the start position has the same value irrespective of whether this
1457 -- is a forward or reverse iteration.)
1459 return It : constant Iterator :=
1460 Iterator'(Limited_Controlled with
1461 Container => Container'Unrestricted_Access,
1464 Busy (Container.TC'Unrestricted_Access.all);
1472 function Last (Container : Set) return Cursor is
1474 return (if Container.Last = 0 then No_Element
1475 else Cursor'(Container'Unrestricted_Access, Container.Last));
1478 function Last (Object : Iterator) return Cursor is
1480 -- The value of the iterator object's Node component influences the
1481 -- behavior of the Last (and First) selector function.
1483 -- When the Node component is 0, this means the iterator object was
1484 -- constructed without a start expression, in which case the (reverse)
1485 -- iteration starts from the (logical) beginning of the entire sequence
1486 -- (corresponding to Container.Last, for a reverse iterator).
1488 -- Otherwise, this is iteration over a partial sequence of items. When
1489 -- the Node component is positive, the iterator object was constructed
1490 -- with a start expression, that specifies the position from which the
1491 -- (reverse) partial iteration begins.
1493 if Object.Node = 0 then
1494 return Bounded_Ordered_Sets.Last (Object.Container.all);
1496 return Cursor'(Object.Container, Object.Node);
1504 function Last_Element (Container : Set) return Element_Type is
1506 if Checks and then Container.Last = 0 then
1507 raise Constraint_Error with "set is empty";
1510 return Container.Nodes (Container.Last).Element;
1517 function Left (Node : Node_Type) return Count_Type is
1526 function Length (Container : Set) return Count_Type is
1528 return Container.Length;
1535 procedure Move (Target : in out Set; Source : in out Set) is
1537 if Target'Address = Source'Address then
1541 TC_Check (Source.TC);
1543 Target.Assign (Source);
1551 function Next (Position : Cursor) return Cursor is
1553 if Position = No_Element then
1557 pragma Assert (Vet (Position.Container.all, Position.Node),
1558 "bad cursor in Next");
1561 Node : constant Count_Type :=
1562 Tree_Operations.Next (Position.Container.all, Position.Node);
1569 return Cursor'(Position.Container, Node);
1573 procedure Next (Position : in out Cursor) is
1575 Position := Next (Position);
1578 function Next (Object : Iterator; Position : Cursor) return Cursor is
1580 if Position.Container = null then
1584 if Checks and then Position.Container /= Object.Container then
1585 raise Program_Error with
1586 "Position cursor of Next designates wrong set";
1589 return Next (Position);
1596 function Overlap (Left, Right : Set) return Boolean
1597 renames Set_Ops.Set_Overlap;
1603 function Parent (Node : Node_Type) return Count_Type is
1612 function Previous (Position : Cursor) return Cursor is
1614 if Position = No_Element then
1618 pragma Assert (Vet (Position.Container.all, Position.Node),
1619 "bad cursor in Previous");
1622 Node : constant Count_Type :=
1623 Tree_Operations.Previous (Position.Container.all, Position.Node);
1625 return (if Node = 0 then No_Element
1626 else Cursor'(Position.Container, Node));
1630 procedure Previous (Position : in out Cursor) is
1632 Position := Previous (Position);
1635 function Previous (Object : Iterator; Position : Cursor) return Cursor is
1637 if Position.Container = null then
1641 if Checks and then Position.Container /= Object.Container then
1642 raise Program_Error with
1643 "Position cursor of Previous designates wrong set";
1646 return Previous (Position);
1649 ----------------------
1650 -- Pseudo_Reference --
1651 ----------------------
1653 function Pseudo_Reference
1654 (Container : aliased Set'Class) return Reference_Control_Type
1656 TC : constant Tamper_Counts_Access :=
1657 Container.TC'Unrestricted_Access;
1659 return R : constant Reference_Control_Type := (Controlled with TC) do
1662 end Pseudo_Reference;
1668 procedure Query_Element
1670 Process : not null access procedure (Element : Element_Type))
1673 if Checks and then Position.Node = 0 then
1674 raise Constraint_Error with "Position cursor equals No_Element";
1677 pragma Assert (Vet (Position.Container.all, Position.Node),
1678 "bad cursor in Query_Element");
1681 S : Set renames Position.Container.all;
1682 Lock : With_Lock (S.TC'Unrestricted_Access);
1684 Process (S.Nodes (Position.Node).Element);
1693 (S : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class; V : Set)
1695 First_Time : Boolean := True;
1696 use System.Put_Images;
1702 First_Time := False;
1704 Simple_Array_Between (S);
1707 Element_Type'Put_Image (S, X);
1718 (Stream : not null access Root_Stream_Type'Class;
1719 Container : out Set)
1721 procedure Read_Element (Node : in out Node_Type);
1722 pragma Inline (Read_Element);
1724 procedure Allocate is
1725 new Tree_Operations.Generic_Allocate (Read_Element);
1727 procedure Read_Elements is
1728 new Tree_Operations.Generic_Read (Allocate);
1734 procedure Read_Element (Node : in out Node_Type) is
1736 Element_Type'Read (Stream, Node.Element);
1739 -- Start of processing for Read
1742 Read_Elements (Stream, Container);
1746 (Stream : not null access Root_Stream_Type'Class;
1750 raise Program_Error with "attempt to stream set cursor";
1754 (Stream : not null access Root_Stream_Type'Class;
1755 Item : out Constant_Reference_Type)
1758 raise Program_Error with "attempt to stream reference";
1765 procedure Replace (Container : in out Set; New_Item : Element_Type) is
1766 Node : constant Count_Type := Element_Keys.Find (Container, New_Item);
1769 TE_Check (Container.TC);
1771 if Checks and then Node = 0 then
1772 raise Constraint_Error with
1773 "attempt to replace element not in set";
1776 Container.Nodes (Node).Element := New_Item;
1779 ---------------------
1780 -- Replace_Element --
1781 ---------------------
1783 procedure Replace_Element
1784 (Container : in out Set;
1786 Item : Element_Type)
1788 pragma Assert (Index /= 0);
1790 function New_Node return Count_Type;
1791 pragma Inline (New_Node);
1793 procedure Local_Insert_Post is
1794 new Element_Keys.Generic_Insert_Post (New_Node);
1796 procedure Local_Insert_Sans_Hint is
1797 new Element_Keys.Generic_Conditional_Insert (Local_Insert_Post);
1799 procedure Local_Insert_With_Hint is
1800 new Element_Keys.Generic_Conditional_Insert_With_Hint
1802 Local_Insert_Sans_Hint);
1804 Nodes : Nodes_Type renames Container.Nodes;
1805 Node : Node_Type renames Nodes (Index);
1811 function New_Node return Count_Type is
1813 Node.Element := Item;
1814 Node.Color := Red_Black_Trees.Red;
1822 Result : Count_Type;
1826 -- Start of processing for Replace_Element
1829 -- Replace_Element assigns value Item to the element designated by Node,
1830 -- per certain semantic constraints, described as follows.
1832 -- If Item is equivalent to the element, then element is replaced and
1833 -- there's nothing else to do. This is the easy case.
1835 -- If Item is not equivalent, then the node will (possibly) have to move
1836 -- to some other place in the tree. This is slighly more complicated,
1837 -- because we must ensure that Item is not equivalent to some other
1838 -- element in the tree (in which case, the replacement is not allowed).
1840 -- Determine whether Item is equivalent to element on the specified
1844 Lock : With_Lock (Container.TC'Unrestricted_Access);
1846 Compare := (if Item < Node.Element then False
1847 elsif Node.Element < Item then False
1853 -- Item is equivalent to the node's element, so we will not have to
1856 TE_Check (Container.TC);
1858 Node.Element := Item;
1862 -- The replacement Item is not equivalent to the element on the
1863 -- specified node, which means that it will need to be re-inserted in a
1864 -- different position in the tree. We must now determine whether Item is
1865 -- equivalent to some other element in the tree (which would prohibit
1866 -- the assignment and hence the move).
1868 -- Ceiling returns the smallest element equivalent or greater than the
1869 -- specified Item; if there is no such element, then it returns 0.
1871 Hint := Element_Keys.Ceiling (Container, Item);
1873 if Hint /= 0 then -- Item <= Nodes (Hint).Element
1875 Lock : With_Lock (Container.TC'Unrestricted_Access);
1877 Compare := Item < Nodes (Hint).Element;
1880 -- Item is equivalent to Nodes (Hint).Element
1882 if Checks and then not Compare then
1884 -- Ceiling returns an element that is equivalent or greater than
1885 -- Item. If Item is "not less than" the element, then by
1886 -- elimination we know that Item is equivalent to the element.
1888 -- But this means that it is not possible to assign the value of
1889 -- Item to the specified element (on Node), because a different
1890 -- element (on Hint) equivalent to Item already exsits. (Were we
1891 -- to change Node's element value, we would have to move Node, but
1892 -- we would be unable to move the Node, because its new position
1893 -- in the tree is already occupied by an equivalent element.)
1895 raise Program_Error with "attempt to replace existing element";
1898 -- Item is not equivalent to any other element in the tree
1899 -- (specifically, it is less than Nodes (Hint).Element), so it is
1900 -- safe to assign the value of Item to Node.Element. This means that
1901 -- the node will have to move to a different position in the tree
1902 -- (because its element will have a different value).
1904 -- The nearest (greater) neighbor of Item is Hint. This will be the
1905 -- insertion position of Node (because its element will have Item as
1908 -- If Node equals Hint, the relative position of Node does not
1909 -- change. This allows us to perform an optimization: we need not
1910 -- remove Node from the tree and then reinsert it with its new value,
1911 -- because it would only be placed in the exact same position.
1913 if Hint = Index then
1914 TE_Check (Container.TC);
1916 Node.Element := Item;
1921 -- If we get here, it is because Item was greater than all elements in
1922 -- the tree (Hint = 0), or because Item was less than some element at a
1923 -- different place in the tree (Item < Nodes (Hint).Element and Hint /=
1924 -- Index). In either case, we remove Node from the tree and then insert
1925 -- Item into the tree, onto the same Node.
1927 Tree_Operations.Delete_Node_Sans_Free (Container, Index);
1929 Local_Insert_With_Hint
1934 Inserted => Inserted);
1936 pragma Assert (Inserted);
1937 pragma Assert (Result = Index);
1938 end Replace_Element;
1940 procedure Replace_Element
1941 (Container : in out Set;
1943 New_Item : Element_Type)
1946 if Checks and then Position.Node = 0 then
1947 raise Constraint_Error with
1948 "Position cursor equals No_Element";
1951 if Checks and then Position.Container /= Container'Unrestricted_Access
1953 raise Program_Error with
1954 "Position cursor designates wrong set";
1957 pragma Assert (Vet (Container, Position.Node),
1958 "bad cursor in Replace_Element");
1960 Replace_Element (Container, Position.Node, New_Item);
1961 end Replace_Element;
1963 ---------------------
1964 -- Reverse_Iterate --
1965 ---------------------
1967 procedure Reverse_Iterate
1969 Process : not null access procedure (Position : Cursor))
1971 procedure Process_Node (Node : Count_Type);
1972 pragma Inline (Process_Node);
1974 procedure Local_Reverse_Iterate is
1975 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1981 procedure Process_Node (Node : Count_Type) is
1983 Process (Cursor'(Container'Unrestricted_Access, Node));
1986 S : Set renames Container'Unrestricted_Access.all;
1987 Busy : With_Busy (S.TC'Unrestricted_Access);
1989 -- Start of processing for Reverse_Iterate
1992 Local_Reverse_Iterate (S);
1993 end Reverse_Iterate;
1999 function Right (Node : Node_Type) return Count_Type is
2009 (Node : in out Node_Type;
2010 Color : Red_Black_Trees.Color_Type)
2013 Node.Color := Color;
2020 procedure Set_Left (Node : in out Node_Type; Left : Count_Type) is
2029 procedure Set_Parent (Node : in out Node_Type; Parent : Count_Type) is
2031 Node.Parent := Parent;
2038 procedure Set_Right (Node : in out Node_Type; Right : Count_Type) is
2040 Node.Right := Right;
2043 --------------------------
2044 -- Symmetric_Difference --
2045 --------------------------
2047 procedure Symmetric_Difference (Target : in out Set; Source : Set)
2048 renames Set_Ops.Set_Symmetric_Difference;
2050 function Symmetric_Difference (Left, Right : Set) return Set
2051 renames Set_Ops.Set_Symmetric_Difference;
2057 function To_Set (New_Item : Element_Type) return Set is
2062 return S : Set (1) do
2063 Insert_Sans_Hint (S, New_Item, Node, Inserted);
2064 pragma Assert (Inserted);
2072 procedure Union (Target : in out Set; Source : Set)
2073 renames Set_Ops.Set_Union;
2075 function Union (Left, Right : Set) return Set
2076 renames Set_Ops.Set_Union;
2083 (Stream : not null access Root_Stream_Type'Class;
2086 procedure Write_Element
2087 (Stream : not null access Root_Stream_Type'Class;
2089 pragma Inline (Write_Element);
2091 procedure Write_Elements is
2092 new Tree_Operations.Generic_Write (Write_Element);
2098 procedure Write_Element
2099 (Stream : not null access Root_Stream_Type'Class;
2103 Element_Type'Write (Stream, Node.Element);
2106 -- Start of processing for Write
2109 Write_Elements (Stream, Container);
2113 (Stream : not null access Root_Stream_Type'Class;
2117 raise Program_Error with "attempt to stream set cursor";
2121 (Stream : not null access Root_Stream_Type'Class;
2122 Item : Constant_Reference_Type)
2125 raise Program_Error with "attempt to stream reference";
2128 end Ada.Containers.Bounded_Ordered_Sets;