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-2019, 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 System; use type System.Address;
45 package body Ada.Containers.Bounded_Ordered_Sets is
47 pragma Warnings (Off, "variable ""Busy*"" is not referenced");
48 pragma Warnings (Off, "variable ""Lock*"" is not referenced");
49 -- See comment in Ada.Containers.Helpers
51 ------------------------------
52 -- Access to Fields of Node --
53 ------------------------------
55 -- These subprograms provide functional notation for access to fields
56 -- of a node, and procedural notation for modifying these fields.
58 function Color (Node : Node_Type) return Red_Black_Trees.Color_Type;
59 pragma Inline (Color);
61 function Left (Node : Node_Type) return Count_Type;
64 function Parent (Node : Node_Type) return Count_Type;
65 pragma Inline (Parent);
67 function Right (Node : Node_Type) return Count_Type;
68 pragma Inline (Right);
71 (Node : in out Node_Type;
72 Color : Red_Black_Trees.Color_Type);
73 pragma Inline (Set_Color);
75 procedure Set_Left (Node : in out Node_Type; Left : Count_Type);
76 pragma Inline (Set_Left);
78 procedure Set_Right (Node : in out Node_Type; Right : Count_Type);
79 pragma Inline (Set_Right);
81 procedure Set_Parent (Node : in out Node_Type; Parent : Count_Type);
82 pragma Inline (Set_Parent);
84 -----------------------
85 -- Local Subprograms --
86 -----------------------
88 procedure Insert_Sans_Hint
89 (Container : in out Set;
90 New_Item : Element_Type;
91 Node : out Count_Type;
92 Inserted : out Boolean);
94 procedure Insert_With_Hint
95 (Dst_Set : in out Set;
96 Dst_Hint : Count_Type;
98 Dst_Node : out Count_Type);
100 function Is_Greater_Element_Node
101 (Left : Element_Type;
102 Right : Node_Type) return Boolean;
103 pragma Inline (Is_Greater_Element_Node);
105 function Is_Less_Element_Node
106 (Left : Element_Type;
107 Right : Node_Type) return Boolean;
108 pragma Inline (Is_Less_Element_Node);
110 function Is_Less_Node_Node (L, R : Node_Type) return Boolean;
111 pragma Inline (Is_Less_Node_Node);
113 procedure Replace_Element
114 (Container : in out Set;
116 Item : Element_Type);
118 --------------------------
119 -- Local Instantiations --
120 --------------------------
122 package Tree_Operations is
123 new Red_Black_Trees.Generic_Bounded_Operations (Tree_Types);
127 package Element_Keys is
128 new Red_Black_Trees.Generic_Bounded_Keys
129 (Tree_Operations => Tree_Operations,
130 Key_Type => Element_Type,
131 Is_Less_Key_Node => Is_Less_Element_Node,
132 Is_Greater_Key_Node => Is_Greater_Element_Node);
135 new Red_Black_Trees.Generic_Bounded_Set_Operations
136 (Tree_Operations => Tree_Operations,
139 Insert_With_Hint => Insert_With_Hint,
140 Is_Less => Is_Less_Node_Node);
146 function "<" (Left, Right : Cursor) return Boolean is
148 if Checks and then Left.Node = 0 then
149 raise Constraint_Error with "Left cursor equals No_Element";
152 if Checks and then Right.Node = 0 then
153 raise Constraint_Error with "Right cursor equals No_Element";
156 pragma Assert (Vet (Left.Container.all, Left.Node),
157 "bad Left cursor in ""<""");
159 pragma Assert (Vet (Right.Container.all, Right.Node),
160 "bad Right cursor in ""<""");
163 LN : Nodes_Type renames Left.Container.Nodes;
164 RN : Nodes_Type renames Right.Container.Nodes;
166 return LN (Left.Node).Element < RN (Right.Node).Element;
170 function "<" (Left : Cursor; Right : Element_Type) return Boolean is
172 if Checks and then Left.Node = 0 then
173 raise Constraint_Error with "Left cursor equals No_Element";
176 pragma Assert (Vet (Left.Container.all, Left.Node),
177 "bad Left cursor in ""<""");
179 return Left.Container.Nodes (Left.Node).Element < Right;
182 function "<" (Left : Element_Type; Right : Cursor) return Boolean is
184 if Checks and then Right.Node = 0 then
185 raise Constraint_Error with "Right cursor equals No_Element";
188 pragma Assert (Vet (Right.Container.all, Right.Node),
189 "bad Right cursor in ""<""");
191 return Left < Right.Container.Nodes (Right.Node).Element;
198 function "=" (Left, Right : Set) return Boolean is
199 function Is_Equal_Node_Node (L, R : Node_Type) return Boolean;
200 pragma Inline (Is_Equal_Node_Node);
203 new Tree_Operations.Generic_Equal (Is_Equal_Node_Node);
205 ------------------------
206 -- Is_Equal_Node_Node --
207 ------------------------
209 function Is_Equal_Node_Node (L, R : Node_Type) return Boolean is
211 return L.Element = R.Element;
212 end Is_Equal_Node_Node;
214 -- Start of processing for Is_Equal
217 return Is_Equal (Left, Right);
224 function ">" (Left, Right : Cursor) return Boolean is
226 if Checks and then Left.Node = 0 then
227 raise Constraint_Error with "Left cursor equals No_Element";
230 if Checks and then Right.Node = 0 then
231 raise Constraint_Error with "Right cursor equals No_Element";
234 pragma Assert (Vet (Left.Container.all, Left.Node),
235 "bad Left cursor in "">""");
237 pragma Assert (Vet (Right.Container.all, Right.Node),
238 "bad Right cursor in "">""");
240 -- L > R same as R < L
243 LN : Nodes_Type renames Left.Container.Nodes;
244 RN : Nodes_Type renames Right.Container.Nodes;
246 return RN (Right.Node).Element < LN (Left.Node).Element;
250 function ">" (Left : Element_Type; Right : Cursor) return Boolean is
252 if Checks and then Right.Node = 0 then
253 raise Constraint_Error with "Right cursor equals No_Element";
256 pragma Assert (Vet (Right.Container.all, Right.Node),
257 "bad Right cursor in "">""");
259 return Right.Container.Nodes (Right.Node).Element < Left;
262 function ">" (Left : Cursor; Right : Element_Type) return Boolean is
264 if Checks and then Left.Node = 0 then
265 raise Constraint_Error with "Left cursor equals No_Element";
268 pragma Assert (Vet (Left.Container.all, Left.Node),
269 "bad Left cursor in "">""");
271 return Right < Left.Container.Nodes (Left.Node).Element;
278 procedure Assign (Target : in out Set; Source : Set) is
279 procedure Append_Element (Source_Node : Count_Type);
281 procedure Append_Elements is
282 new Tree_Operations.Generic_Iteration (Append_Element);
288 procedure Append_Element (Source_Node : Count_Type) is
289 SN : Node_Type renames Source.Nodes (Source_Node);
291 procedure Set_Element (Node : in out Node_Type);
292 pragma Inline (Set_Element);
294 function New_Node return Count_Type;
295 pragma Inline (New_Node);
297 procedure Insert_Post is
298 new Element_Keys.Generic_Insert_Post (New_Node);
300 procedure Unconditional_Insert_Sans_Hint is
301 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
303 procedure Unconditional_Insert_Avec_Hint is
304 new Element_Keys.Generic_Unconditional_Insert_With_Hint
306 Unconditional_Insert_Sans_Hint);
308 procedure Allocate is
309 new Tree_Operations.Generic_Allocate (Set_Element);
315 function New_Node return Count_Type is
318 Allocate (Target, Result);
326 procedure Set_Element (Node : in out Node_Type) is
328 Node.Element := SN.Element;
331 Target_Node : Count_Type;
333 -- Start of processing for Append_Element
336 Unconditional_Insert_Avec_Hint
340 Node => Target_Node);
343 -- Start of processing for Assign
346 if Target'Address = Source'Address then
350 if Checks and then Target.Capacity < Source.Length then
352 with "Target capacity is less than Source length";
356 Append_Elements (Source);
363 function Ceiling (Container : Set; Item : Element_Type) return Cursor is
364 Node : constant Count_Type :=
365 Element_Keys.Ceiling (Container, Item);
367 return (if Node = 0 then No_Element
368 else Cursor'(Container'Unrestricted_Access, Node));
375 procedure Clear (Container : in out Set) is
377 while not Container.Is_Empty loop
378 Container.Delete_Last;
386 function Color (Node : Node_Type) return Red_Black_Trees.Color_Type is
391 ------------------------
392 -- Constant_Reference --
393 ------------------------
395 function Constant_Reference
396 (Container : aliased Set;
397 Position : Cursor) return Constant_Reference_Type
400 if Checks and then Position.Container = null then
401 raise Constraint_Error with "Position cursor has no element";
404 if Checks and then Position.Container /= Container'Unrestricted_Access
406 raise Program_Error with
407 "Position cursor designates wrong container";
411 (Vet (Container, Position.Node),
412 "bad cursor in Constant_Reference");
415 N : Node_Type renames Container.Nodes (Position.Node);
416 TC : constant Tamper_Counts_Access :=
417 Container.TC'Unrestricted_Access;
419 return R : constant Constant_Reference_Type :=
420 (Element => N.Element'Access,
421 Control => (Controlled with TC))
426 end Constant_Reference;
434 Item : Element_Type) return Boolean
437 return Find (Container, Item) /= No_Element;
444 function Copy (Source : Set; Capacity : Count_Type := 0) return Set is
450 elsif Capacity >= Source.Length then
453 raise Capacity_Error with "Capacity value too small";
456 return Target : Set (Capacity => C) do
457 Assign (Target => Target, Source => Source);
465 procedure Delete (Container : in out Set; Position : in out Cursor) is
467 if Checks and then Position.Node = 0 then
468 raise Constraint_Error with "Position cursor equals No_Element";
471 if Checks and then Position.Container /= Container'Unrestricted_Access
473 raise Program_Error with "Position cursor designates wrong set";
476 TC_Check (Container.TC);
478 pragma Assert (Vet (Container, Position.Node),
479 "bad cursor in Delete");
481 Tree_Operations.Delete_Node_Sans_Free (Container, Position.Node);
482 Tree_Operations.Free (Container, Position.Node);
484 Position := No_Element;
487 procedure Delete (Container : in out Set; Item : Element_Type) is
488 X : constant Count_Type := Element_Keys.Find (Container, Item);
491 Tree_Operations.Delete_Node_Sans_Free (Container, X);
493 if Checks and then X = 0 then
494 raise Constraint_Error with "attempt to delete element not in set";
497 Tree_Operations.Free (Container, X);
504 procedure Delete_First (Container : in out Set) is
505 X : constant Count_Type := Container.First;
508 Tree_Operations.Delete_Node_Sans_Free (Container, X);
509 Tree_Operations.Free (Container, X);
517 procedure Delete_Last (Container : in out Set) is
518 X : constant Count_Type := Container.Last;
521 Tree_Operations.Delete_Node_Sans_Free (Container, X);
522 Tree_Operations.Free (Container, X);
530 procedure Difference (Target : in out Set; Source : Set)
531 renames Set_Ops.Set_Difference;
533 function Difference (Left, Right : Set) return Set
534 renames Set_Ops.Set_Difference;
540 function Element (Position : Cursor) return Element_Type is
542 if Checks and then Position.Node = 0 then
543 raise Constraint_Error with "Position cursor equals No_Element";
546 pragma Assert (Vet (Position.Container.all, Position.Node),
547 "bad cursor in Element");
549 return Position.Container.Nodes (Position.Node).Element;
552 -------------------------
553 -- Equivalent_Elements --
554 -------------------------
556 function Equivalent_Elements (Left, Right : Element_Type) return Boolean is
558 return (if Left < Right or else Right < Left then False else True);
559 end Equivalent_Elements;
561 ---------------------
562 -- Equivalent_Sets --
563 ---------------------
565 function Equivalent_Sets (Left, Right : Set) return Boolean is
566 function Is_Equivalent_Node_Node (L, R : Node_Type) return Boolean;
567 pragma Inline (Is_Equivalent_Node_Node);
569 function Is_Equivalent is
570 new Tree_Operations.Generic_Equal (Is_Equivalent_Node_Node);
572 -----------------------------
573 -- Is_Equivalent_Node_Node --
574 -----------------------------
576 function Is_Equivalent_Node_Node (L, R : Node_Type) return Boolean is
578 return (if L.Element < R.Element then False
579 elsif R.Element < L.Element then False
581 end Is_Equivalent_Node_Node;
583 -- Start of processing for Equivalent_Sets
586 return Is_Equivalent (Left, Right);
593 procedure Exclude (Container : in out Set; Item : Element_Type) is
594 X : constant Count_Type := Element_Keys.Find (Container, Item);
597 Tree_Operations.Delete_Node_Sans_Free (Container, X);
598 Tree_Operations.Free (Container, X);
606 procedure Finalize (Object : in out Iterator) is
608 if Object.Container /= null then
609 Unbusy (Object.Container.TC);
617 function Find (Container : Set; Item : Element_Type) return Cursor is
618 Node : constant Count_Type := Element_Keys.Find (Container, Item);
620 return (if Node = 0 then No_Element
621 else Cursor'(Container'Unrestricted_Access, Node));
628 function First (Container : Set) return Cursor is
630 return (if Container.First = 0 then No_Element
631 else Cursor'(Container'Unrestricted_Access, Container.First));
634 function First (Object : Iterator) return Cursor is
636 -- The value of the iterator object's Node component influences the
637 -- behavior of the First (and Last) selector function.
639 -- When the Node component is 0, this means the iterator object was
640 -- constructed without a start expression, in which case the (forward)
641 -- iteration starts from the (logical) beginning of the entire sequence
642 -- of items (corresponding to Container.First, for a forward iterator).
644 -- Otherwise, this is iteration over a partial sequence of items. When
645 -- the Node component is positive, the iterator object was constructed
646 -- with a start expression, that specifies the position from which the
647 -- (forward) partial iteration begins.
649 if Object.Node = 0 then
650 return Bounded_Ordered_Sets.First (Object.Container.all);
652 return Cursor'(Object.Container, Object.Node);
660 function First_Element (Container : Set) return Element_Type is
662 if Checks and then Container.First = 0 then
663 raise Constraint_Error with "set is empty";
666 return Container.Nodes (Container.First).Element;
673 function Floor (Container : Set; Item : Element_Type) return Cursor is
674 Node : constant Count_Type := Element_Keys.Floor (Container, Item);
676 return (if Node = 0 then No_Element
677 else Cursor'(Container'Unrestricted_Access, Node));
684 package body Generic_Keys is
686 -----------------------
687 -- Local Subprograms --
688 -----------------------
690 function Is_Greater_Key_Node
692 Right : Node_Type) return Boolean;
693 pragma Inline (Is_Greater_Key_Node);
695 function Is_Less_Key_Node
697 Right : Node_Type) return Boolean;
698 pragma Inline (Is_Less_Key_Node);
700 --------------------------
701 -- Local Instantiations --
702 --------------------------
705 new Red_Black_Trees.Generic_Bounded_Keys
706 (Tree_Operations => Tree_Operations,
707 Key_Type => Key_Type,
708 Is_Less_Key_Node => Is_Less_Key_Node,
709 Is_Greater_Key_Node => Is_Greater_Key_Node);
715 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
716 Node : constant Count_Type :=
717 Key_Keys.Ceiling (Container, Key);
719 return (if Node = 0 then No_Element
720 else Cursor'(Container'Unrestricted_Access, Node));
723 ------------------------
724 -- Constant_Reference --
725 ------------------------
727 function Constant_Reference
728 (Container : aliased Set;
729 Key : Key_Type) return Constant_Reference_Type
731 Node : constant Count_Type := Key_Keys.Find (Container, Key);
734 if Checks and then Node = 0 then
735 raise Constraint_Error with "key not in set";
739 N : Node_Type renames Container.Nodes (Node);
740 TC : constant Tamper_Counts_Access :=
741 Container.TC'Unrestricted_Access;
743 return R : constant Constant_Reference_Type :=
744 (Element => N.Element'Access,
745 Control => (Controlled with TC))
750 end Constant_Reference;
756 function Contains (Container : Set; Key : Key_Type) return Boolean is
758 return Find (Container, Key) /= No_Element;
765 procedure Delete (Container : in out Set; Key : Key_Type) is
766 X : constant Count_Type := Key_Keys.Find (Container, Key);
769 if Checks and then X = 0 then
770 raise Constraint_Error with "attempt to delete key not in set";
773 Tree_Operations.Delete_Node_Sans_Free (Container, X);
774 Tree_Operations.Free (Container, X);
781 function Element (Container : Set; Key : Key_Type) return Element_Type is
782 Node : constant Count_Type := Key_Keys.Find (Container, Key);
785 if Checks and then Node = 0 then
786 raise Constraint_Error with "key not in set";
789 return Container.Nodes (Node).Element;
792 ---------------------
793 -- Equivalent_Keys --
794 ---------------------
796 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
798 return (if Left < Right or else Right < Left then False else True);
805 procedure Exclude (Container : in out Set; Key : Key_Type) is
806 X : constant Count_Type := Key_Keys.Find (Container, Key);
809 Tree_Operations.Delete_Node_Sans_Free (Container, X);
810 Tree_Operations.Free (Container, X);
818 procedure Finalize (Control : in out Reference_Control_Type) is
820 if Control.Container /= null then
821 Impl.Reference_Control_Type (Control).Finalize;
823 if Checks and then not (Key (Control.Pos) = Control.Old_Key.all)
825 Delete (Control.Container.all, Key (Control.Pos));
829 Control.Container := null;
837 function Find (Container : Set; Key : Key_Type) return Cursor is
838 Node : constant Count_Type := Key_Keys.Find (Container, Key);
840 return (if Node = 0 then No_Element
841 else Cursor'(Container'Unrestricted_Access, Node));
848 function Floor (Container : Set; Key : Key_Type) return Cursor is
849 Node : constant Count_Type := Key_Keys.Floor (Container, Key);
851 return (if Node = 0 then No_Element
852 else Cursor'(Container'Unrestricted_Access, Node));
855 -------------------------
856 -- Is_Greater_Key_Node --
857 -------------------------
859 function Is_Greater_Key_Node
861 Right : Node_Type) return Boolean
864 return Key (Right.Element) < Left;
865 end Is_Greater_Key_Node;
867 ----------------------
868 -- Is_Less_Key_Node --
869 ----------------------
871 function Is_Less_Key_Node
873 Right : Node_Type) return Boolean
876 return Left < Key (Right.Element);
877 end Is_Less_Key_Node;
883 function Key (Position : Cursor) return Key_Type is
885 if Checks and then Position.Node = 0 then
886 raise Constraint_Error with
887 "Position cursor equals No_Element";
890 pragma Assert (Vet (Position.Container.all, Position.Node),
891 "bad cursor in Key");
893 return Key (Position.Container.Nodes (Position.Node).Element);
901 (Stream : not null access Root_Stream_Type'Class;
902 Item : out Reference_Type)
905 raise Program_Error with "attempt to stream reference";
908 ------------------------------
909 -- Reference_Preserving_Key --
910 ------------------------------
912 function Reference_Preserving_Key
913 (Container : aliased in out Set;
914 Position : Cursor) return Reference_Type
917 if Checks and then Position.Container = null then
918 raise Constraint_Error with "Position cursor has no element";
921 if Checks and then Position.Container /= Container'Unrestricted_Access
923 raise Program_Error with
924 "Position cursor designates wrong container";
928 (Vet (Container, Position.Node),
929 "bad cursor in function Reference_Preserving_Key");
932 N : Node_Type renames Container.Nodes (Position.Node);
934 return R : constant Reference_Type :=
935 (Element => N.Element'Access,
938 Container.TC'Unrestricted_Access,
939 Container => Container'Access,
941 Old_Key => new Key_Type'(Key (Position))))
946 end Reference_Preserving_Key;
948 function Reference_Preserving_Key
949 (Container : aliased in out Set;
950 Key : Key_Type) return Reference_Type
952 Node : constant Count_Type := Key_Keys.Find (Container, Key);
955 if Checks and then Node = 0 then
956 raise Constraint_Error with "key not in set";
960 N : Node_Type renames Container.Nodes (Node);
962 return R : constant Reference_Type :=
963 (Element => N.Element'Access,
966 Container.TC'Unrestricted_Access,
967 Container => Container'Access,
968 Pos => Find (Container, Key),
969 Old_Key => new Key_Type'(Key)))
974 end Reference_Preserving_Key;
981 (Container : in out Set;
983 New_Item : Element_Type)
985 Node : constant Count_Type := Key_Keys.Find (Container, Key);
988 if Checks and then Node = 0 then
989 raise Constraint_Error with
990 "attempt to replace key not in set";
993 Replace_Element (Container, Node, New_Item);
996 -----------------------------------
997 -- Update_Element_Preserving_Key --
998 -----------------------------------
1000 procedure Update_Element_Preserving_Key
1001 (Container : in out Set;
1003 Process : not null access procedure (Element : in out Element_Type))
1006 if Checks and then Position.Node = 0 then
1007 raise Constraint_Error with
1008 "Position cursor equals No_Element";
1011 if Checks and then Position.Container /= Container'Unrestricted_Access
1013 raise Program_Error with
1014 "Position cursor designates wrong set";
1017 pragma Assert (Vet (Container, Position.Node),
1018 "bad cursor in Update_Element_Preserving_Key");
1020 -- Per AI05-0022, the container implementation is required to detect
1021 -- element tampering by a generic actual subprogram.
1024 N : Node_Type renames Container.Nodes (Position.Node);
1025 E : Element_Type renames N.Element;
1026 K : constant Key_Type := Key (E);
1027 Lock : With_Lock (Container.TC'Unrestricted_Access);
1030 if Equivalent_Keys (K, Key (E)) then
1035 Tree_Operations.Delete_Node_Sans_Free (Container, Position.Node);
1036 Tree_Operations.Free (Container, Position.Node);
1038 raise Program_Error with "key was modified";
1039 end Update_Element_Preserving_Key;
1046 (Stream : not null access Root_Stream_Type'Class;
1047 Item : Reference_Type)
1050 raise Program_Error with "attempt to stream reference";
1054 ------------------------
1055 -- Get_Element_Access --
1056 ------------------------
1058 function Get_Element_Access
1059 (Position : Cursor) return not null Element_Access is
1061 return Position.Container.Nodes (Position.Node).Element'Access;
1062 end Get_Element_Access;
1068 function Has_Element (Position : Cursor) return Boolean is
1070 return Position /= No_Element;
1077 procedure Include (Container : in out Set; New_Item : Element_Type) is
1082 Insert (Container, New_Item, Position, Inserted);
1084 if not Inserted then
1085 TE_Check (Container.TC);
1087 Container.Nodes (Position.Node).Element := New_Item;
1096 (Container : in out Set;
1097 New_Item : Element_Type;
1098 Position : out Cursor;
1099 Inserted : out Boolean)
1108 Position.Container := Container'Unrestricted_Access;
1112 (Container : in out Set;
1113 New_Item : Element_Type)
1116 pragma Unreferenced (Position);
1121 Insert (Container, New_Item, Position, Inserted);
1123 if Checks and then not Inserted then
1124 raise Constraint_Error with
1125 "attempt to insert element already in set";
1129 ----------------------
1130 -- Insert_Sans_Hint --
1131 ----------------------
1133 procedure Insert_Sans_Hint
1134 (Container : in out Set;
1135 New_Item : Element_Type;
1136 Node : out Count_Type;
1137 Inserted : out Boolean)
1139 procedure Set_Element (Node : in out Node_Type);
1140 pragma Inline (Set_Element);
1142 function New_Node return Count_Type;
1143 pragma Inline (New_Node);
1145 procedure Insert_Post is
1146 new Element_Keys.Generic_Insert_Post (New_Node);
1148 procedure Conditional_Insert_Sans_Hint is
1149 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1151 procedure Allocate is
1152 new Tree_Operations.Generic_Allocate (Set_Element);
1158 function New_Node return Count_Type is
1159 Result : Count_Type;
1161 Allocate (Container, Result);
1169 procedure Set_Element (Node : in out Node_Type) is
1171 Node.Element := New_Item;
1174 -- Start of processing for Insert_Sans_Hint
1177 TC_Check (Container.TC);
1179 Conditional_Insert_Sans_Hint
1184 end Insert_Sans_Hint;
1186 ----------------------
1187 -- Insert_With_Hint --
1188 ----------------------
1190 procedure Insert_With_Hint
1191 (Dst_Set : in out Set;
1192 Dst_Hint : Count_Type;
1193 Src_Node : Node_Type;
1194 Dst_Node : out Count_Type)
1197 pragma Unreferenced (Success);
1199 procedure Set_Element (Node : in out Node_Type);
1200 pragma Inline (Set_Element);
1202 function New_Node return Count_Type;
1203 pragma Inline (New_Node);
1205 procedure Insert_Post is
1206 new Element_Keys.Generic_Insert_Post (New_Node);
1208 procedure Insert_Sans_Hint is
1209 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1211 procedure Local_Insert_With_Hint is
1212 new Element_Keys.Generic_Conditional_Insert_With_Hint
1216 procedure Allocate is
1217 new Tree_Operations.Generic_Allocate (Set_Element);
1223 function New_Node return Count_Type is
1224 Result : Count_Type;
1226 Allocate (Dst_Set, Result);
1234 procedure Set_Element (Node : in out Node_Type) is
1236 Node.Element := Src_Node.Element;
1239 -- Start of processing for Insert_With_Hint
1242 Local_Insert_With_Hint
1248 end Insert_With_Hint;
1254 procedure Intersection (Target : in out Set; Source : Set)
1255 renames Set_Ops.Set_Intersection;
1257 function Intersection (Left, Right : Set) return Set
1258 renames Set_Ops.Set_Intersection;
1264 function Is_Empty (Container : Set) return Boolean is
1266 return Container.Length = 0;
1269 -----------------------------
1270 -- Is_Greater_Element_Node --
1271 -----------------------------
1273 function Is_Greater_Element_Node
1274 (Left : Element_Type;
1275 Right : Node_Type) return Boolean
1278 -- Compute e > node same as node < e
1280 return Right.Element < Left;
1281 end Is_Greater_Element_Node;
1283 --------------------------
1284 -- Is_Less_Element_Node --
1285 --------------------------
1287 function Is_Less_Element_Node
1288 (Left : Element_Type;
1289 Right : Node_Type) return Boolean
1292 return Left < Right.Element;
1293 end Is_Less_Element_Node;
1295 -----------------------
1296 -- Is_Less_Node_Node --
1297 -----------------------
1299 function Is_Less_Node_Node (L, R : Node_Type) return Boolean is
1301 return L.Element < R.Element;
1302 end Is_Less_Node_Node;
1308 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean
1309 renames Set_Ops.Set_Subset;
1317 Process : not null access procedure (Position : Cursor))
1319 procedure Process_Node (Node : Count_Type);
1320 pragma Inline (Process_Node);
1322 procedure Local_Iterate is
1323 new Tree_Operations.Generic_Iteration (Process_Node);
1329 procedure Process_Node (Node : Count_Type) is
1331 Process (Cursor'(Container'Unrestricted_Access, Node));
1334 S : Set renames Container'Unrestricted_Access.all;
1335 Busy : With_Busy (S.TC'Unrestricted_Access);
1337 -- Start of processing for Iterate
1343 function Iterate (Container : Set)
1344 return Set_Iterator_Interfaces.Reversible_Iterator'class
1347 -- The value of the Node component influences the behavior of the First
1348 -- and Last selector functions of the iterator object. When the Node
1349 -- component is 0 (as is the case here), this means the iterator object
1350 -- was constructed without a start expression. This is a complete
1351 -- iterator, meaning that the iteration starts from the (logical)
1352 -- beginning of the sequence of items.
1354 -- Note: For a forward iterator, Container.First is the beginning, and
1355 -- for a reverse iterator, Container.Last is the beginning.
1357 return It : constant Iterator :=
1358 Iterator'(Limited_Controlled with
1359 Container => Container'Unrestricted_Access,
1362 Busy (Container.TC'Unrestricted_Access.all);
1366 function Iterate (Container : Set; Start : Cursor)
1367 return Set_Iterator_Interfaces.Reversible_Iterator'class
1370 -- It was formerly the case that when Start = No_Element, the partial
1371 -- iterator was defined to behave the same as for a complete iterator,
1372 -- and iterate over the entire sequence of items. However, those
1373 -- semantics were unintuitive and arguably error-prone (it is too easy
1374 -- to accidentally create an endless loop), and so they were changed,
1375 -- per the ARG meeting in Denver on 2011/11. However, there was no
1376 -- consensus about what positive meaning this corner case should have,
1377 -- and so it was decided to simply raise an exception. This does imply,
1378 -- however, that it is not possible to use a partial iterator to specify
1379 -- an empty sequence of items.
1381 if Checks and then Start = No_Element then
1382 raise Constraint_Error with
1383 "Start position for iterator equals No_Element";
1386 if Checks and then Start.Container /= Container'Unrestricted_Access then
1387 raise Program_Error with
1388 "Start cursor of Iterate designates wrong set";
1391 pragma Assert (Vet (Container, Start.Node),
1392 "Start cursor of Iterate is bad");
1394 -- The value of the Node component influences the behavior of the First
1395 -- and Last selector functions of the iterator object. When the Node
1396 -- component is positive (as is the case here), it means that this
1397 -- is a partial iteration, over a subset of the complete sequence of
1398 -- items. The iterator object was constructed with a start expression,
1399 -- indicating the position from which the iteration begins. (Note that
1400 -- the start position has the same value irrespective of whether this
1401 -- is a forward or reverse iteration.)
1403 return It : constant Iterator :=
1404 Iterator'(Limited_Controlled with
1405 Container => Container'Unrestricted_Access,
1408 Busy (Container.TC'Unrestricted_Access.all);
1416 function Last (Container : Set) return Cursor is
1418 return (if Container.Last = 0 then No_Element
1419 else Cursor'(Container'Unrestricted_Access, Container.Last));
1422 function Last (Object : Iterator) return Cursor is
1424 -- The value of the iterator object's Node component influences the
1425 -- behavior of the Last (and First) selector function.
1427 -- When the Node component is 0, this means the iterator object was
1428 -- constructed without a start expression, in which case the (reverse)
1429 -- iteration starts from the (logical) beginning of the entire sequence
1430 -- (corresponding to Container.Last, for a reverse iterator).
1432 -- Otherwise, this is iteration over a partial sequence of items. When
1433 -- the Node component is positive, the iterator object was constructed
1434 -- with a start expression, that specifies the position from which the
1435 -- (reverse) partial iteration begins.
1437 if Object.Node = 0 then
1438 return Bounded_Ordered_Sets.Last (Object.Container.all);
1440 return Cursor'(Object.Container, Object.Node);
1448 function Last_Element (Container : Set) return Element_Type is
1450 if Checks and then Container.Last = 0 then
1451 raise Constraint_Error with "set is empty";
1454 return Container.Nodes (Container.Last).Element;
1461 function Left (Node : Node_Type) return Count_Type is
1470 function Length (Container : Set) return Count_Type is
1472 return Container.Length;
1479 procedure Move (Target : in out Set; Source : in out Set) is
1481 if Target'Address = Source'Address then
1485 TC_Check (Source.TC);
1487 Target.Assign (Source);
1495 function Next (Position : Cursor) return Cursor is
1497 if Position = No_Element then
1501 pragma Assert (Vet (Position.Container.all, Position.Node),
1502 "bad cursor in Next");
1505 Node : constant Count_Type :=
1506 Tree_Operations.Next (Position.Container.all, Position.Node);
1513 return Cursor'(Position.Container, Node);
1517 procedure Next (Position : in out Cursor) is
1519 Position := Next (Position);
1522 function Next (Object : Iterator; Position : Cursor) return Cursor is
1524 if Position.Container = null then
1528 if Checks and then Position.Container /= Object.Container then
1529 raise Program_Error with
1530 "Position cursor of Next designates wrong set";
1533 return Next (Position);
1540 function Overlap (Left, Right : Set) return Boolean
1541 renames Set_Ops.Set_Overlap;
1547 function Parent (Node : Node_Type) return Count_Type is
1556 function Previous (Position : Cursor) return Cursor is
1558 if Position = No_Element then
1562 pragma Assert (Vet (Position.Container.all, Position.Node),
1563 "bad cursor in Previous");
1566 Node : constant Count_Type :=
1567 Tree_Operations.Previous (Position.Container.all, Position.Node);
1569 return (if Node = 0 then No_Element
1570 else Cursor'(Position.Container, Node));
1574 procedure Previous (Position : in out Cursor) is
1576 Position := Previous (Position);
1579 function Previous (Object : Iterator; Position : Cursor) return Cursor is
1581 if Position.Container = null then
1585 if Checks and then Position.Container /= Object.Container then
1586 raise Program_Error with
1587 "Position cursor of Previous designates wrong set";
1590 return Previous (Position);
1593 ----------------------
1594 -- Pseudo_Reference --
1595 ----------------------
1597 function Pseudo_Reference
1598 (Container : aliased Set'Class) return Reference_Control_Type
1600 TC : constant Tamper_Counts_Access :=
1601 Container.TC'Unrestricted_Access;
1603 return R : constant Reference_Control_Type := (Controlled with TC) do
1606 end Pseudo_Reference;
1612 procedure Query_Element
1614 Process : not null access procedure (Element : Element_Type))
1617 if Checks and then Position.Node = 0 then
1618 raise Constraint_Error with "Position cursor equals No_Element";
1621 pragma Assert (Vet (Position.Container.all, Position.Node),
1622 "bad cursor in Query_Element");
1625 S : Set renames Position.Container.all;
1626 Lock : With_Lock (S.TC'Unrestricted_Access);
1628 Process (S.Nodes (Position.Node).Element);
1637 (Stream : not null access Root_Stream_Type'Class;
1638 Container : out Set)
1640 procedure Read_Element (Node : in out Node_Type);
1641 pragma Inline (Read_Element);
1643 procedure Allocate is
1644 new Tree_Operations.Generic_Allocate (Read_Element);
1646 procedure Read_Elements is
1647 new Tree_Operations.Generic_Read (Allocate);
1653 procedure Read_Element (Node : in out Node_Type) is
1655 Element_Type'Read (Stream, Node.Element);
1658 -- Start of processing for Read
1661 Read_Elements (Stream, Container);
1665 (Stream : not null access Root_Stream_Type'Class;
1669 raise Program_Error with "attempt to stream set cursor";
1673 (Stream : not null access Root_Stream_Type'Class;
1674 Item : out Constant_Reference_Type)
1677 raise Program_Error with "attempt to stream reference";
1684 procedure Replace (Container : in out Set; New_Item : Element_Type) is
1685 Node : constant Count_Type := Element_Keys.Find (Container, New_Item);
1688 if Checks and then Node = 0 then
1689 raise Constraint_Error with
1690 "attempt to replace element not in set";
1693 TE_Check (Container.TC);
1695 Container.Nodes (Node).Element := New_Item;
1698 ---------------------
1699 -- Replace_Element --
1700 ---------------------
1702 procedure Replace_Element
1703 (Container : in out Set;
1705 Item : Element_Type)
1707 pragma Assert (Index /= 0);
1709 function New_Node return Count_Type;
1710 pragma Inline (New_Node);
1712 procedure Local_Insert_Post is
1713 new Element_Keys.Generic_Insert_Post (New_Node);
1715 procedure Local_Insert_Sans_Hint is
1716 new Element_Keys.Generic_Conditional_Insert (Local_Insert_Post);
1718 procedure Local_Insert_With_Hint is
1719 new Element_Keys.Generic_Conditional_Insert_With_Hint
1721 Local_Insert_Sans_Hint);
1723 Nodes : Nodes_Type renames Container.Nodes;
1724 Node : Node_Type renames Nodes (Index);
1730 function New_Node return Count_Type is
1732 Node.Element := Item;
1733 Node.Color := Red_Black_Trees.Red;
1741 Result : Count_Type;
1745 -- Start of processing for Replace_Element
1748 -- Replace_Element assigns value Item to the element designated by Node,
1749 -- per certain semantic constraints, described as follows.
1751 -- If Item is equivalent to the element, then element is replaced and
1752 -- there's nothing else to do. This is the easy case.
1754 -- If Item is not equivalent, then the node will (possibly) have to move
1755 -- to some other place in the tree. This is slighly more complicated,
1756 -- because we must ensure that Item is not equivalent to some other
1757 -- element in the tree (in which case, the replacement is not allowed).
1759 -- Determine whether Item is equivalent to element on the specified
1763 Lock : With_Lock (Container.TC'Unrestricted_Access);
1765 Compare := (if Item < Node.Element then False
1766 elsif Node.Element < Item then False
1772 -- Item is equivalent to the node's element, so we will not have to
1775 TE_Check (Container.TC);
1777 Node.Element := Item;
1781 -- The replacement Item is not equivalent to the element on the
1782 -- specified node, which means that it will need to be re-inserted in a
1783 -- different position in the tree. We must now determine whether Item is
1784 -- equivalent to some other element in the tree (which would prohibit
1785 -- the assignment and hence the move).
1787 -- Ceiling returns the smallest element equivalent or greater than the
1788 -- specified Item; if there is no such element, then it returns 0.
1790 Hint := Element_Keys.Ceiling (Container, Item);
1792 if Hint /= 0 then -- Item <= Nodes (Hint).Element
1794 Lock : With_Lock (Container.TC'Unrestricted_Access);
1796 Compare := Item < Nodes (Hint).Element;
1799 -- Item is equivalent to Nodes (Hint).Element
1801 if Checks and then not Compare then
1803 -- Ceiling returns an element that is equivalent or greater than
1804 -- Item. If Item is "not less than" the element, then by
1805 -- elimination we know that Item is equivalent to the element.
1807 -- But this means that it is not possible to assign the value of
1808 -- Item to the specified element (on Node), because a different
1809 -- element (on Hint) equivalent to Item already exsits. (Were we
1810 -- to change Node's element value, we would have to move Node, but
1811 -- we would be unable to move the Node, because its new position
1812 -- in the tree is already occupied by an equivalent element.)
1814 raise Program_Error with "attempt to replace existing element";
1817 -- Item is not equivalent to any other element in the tree
1818 -- (specifically, it is less than Nodes (Hint).Element), so it is
1819 -- safe to assign the value of Item to Node.Element. This means that
1820 -- the node will have to move to a different position in the tree
1821 -- (because its element will have a different value).
1823 -- The nearest (greater) neighbor of Item is Hint. This will be the
1824 -- insertion position of Node (because its element will have Item as
1827 -- If Node equals Hint, the relative position of Node does not
1828 -- change. This allows us to perform an optimization: we need not
1829 -- remove Node from the tree and then reinsert it with its new value,
1830 -- because it would only be placed in the exact same position.
1832 if Hint = Index then
1833 TE_Check (Container.TC);
1835 Node.Element := Item;
1840 -- If we get here, it is because Item was greater than all elements in
1841 -- the tree (Hint = 0), or because Item was less than some element at a
1842 -- different place in the tree (Item < Nodes (Hint).Element and Hint /=
1843 -- Index). In either case, we remove Node from the tree and then insert
1844 -- Item into the tree, onto the same Node.
1846 Tree_Operations.Delete_Node_Sans_Free (Container, Index);
1848 Local_Insert_With_Hint
1853 Inserted => Inserted);
1855 pragma Assert (Inserted);
1856 pragma Assert (Result = Index);
1857 end Replace_Element;
1859 procedure Replace_Element
1860 (Container : in out Set;
1862 New_Item : Element_Type)
1865 if Checks and then Position.Node = 0 then
1866 raise Constraint_Error with
1867 "Position cursor equals No_Element";
1870 if Checks and then Position.Container /= Container'Unrestricted_Access
1872 raise Program_Error with
1873 "Position cursor designates wrong set";
1876 pragma Assert (Vet (Container, Position.Node),
1877 "bad cursor in Replace_Element");
1879 Replace_Element (Container, Position.Node, New_Item);
1880 end Replace_Element;
1882 ---------------------
1883 -- Reverse_Iterate --
1884 ---------------------
1886 procedure Reverse_Iterate
1888 Process : not null access procedure (Position : Cursor))
1890 procedure Process_Node (Node : Count_Type);
1891 pragma Inline (Process_Node);
1893 procedure Local_Reverse_Iterate is
1894 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1900 procedure Process_Node (Node : Count_Type) is
1902 Process (Cursor'(Container'Unrestricted_Access, Node));
1905 S : Set renames Container'Unrestricted_Access.all;
1906 Busy : With_Busy (S.TC'Unrestricted_Access);
1908 -- Start of processing for Reverse_Iterate
1911 Local_Reverse_Iterate (S);
1912 end Reverse_Iterate;
1918 function Right (Node : Node_Type) return Count_Type is
1928 (Node : in out Node_Type;
1929 Color : Red_Black_Trees.Color_Type)
1932 Node.Color := Color;
1939 procedure Set_Left (Node : in out Node_Type; Left : Count_Type) is
1948 procedure Set_Parent (Node : in out Node_Type; Parent : Count_Type) is
1950 Node.Parent := Parent;
1957 procedure Set_Right (Node : in out Node_Type; Right : Count_Type) is
1959 Node.Right := Right;
1962 --------------------------
1963 -- Symmetric_Difference --
1964 --------------------------
1966 procedure Symmetric_Difference (Target : in out Set; Source : Set)
1967 renames Set_Ops.Set_Symmetric_Difference;
1969 function Symmetric_Difference (Left, Right : Set) return Set
1970 renames Set_Ops.Set_Symmetric_Difference;
1976 function To_Set (New_Item : Element_Type) return Set is
1980 return S : Set (1) do
1981 Insert_Sans_Hint (S, New_Item, Node, Inserted);
1982 pragma Assert (Inserted);
1990 procedure Union (Target : in out Set; Source : Set)
1991 renames Set_Ops.Set_Union;
1993 function Union (Left, Right : Set) return Set
1994 renames Set_Ops.Set_Union;
2001 (Stream : not null access Root_Stream_Type'Class;
2004 procedure Write_Element
2005 (Stream : not null access Root_Stream_Type'Class;
2007 pragma Inline (Write_Element);
2009 procedure Write_Elements is
2010 new Tree_Operations.Generic_Write (Write_Element);
2016 procedure Write_Element
2017 (Stream : not null access Root_Stream_Type'Class;
2021 Element_Type'Write (Stream, Node.Element);
2024 -- Start of processing for Write
2027 Write_Elements (Stream, Container);
2031 (Stream : not null access Root_Stream_Type'Class;
2035 raise Program_Error with "attempt to stream set cursor";
2039 (Stream : not null access Root_Stream_Type'Class;
2040 Item : Constant_Reference_Type)
2043 raise Program_Error with "attempt to stream reference";
2046 end Ada.Containers.Bounded_Ordered_Sets;