1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . O R D E R E D _ S E T S --
9 -- Copyright (C) 2004-2011, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- This unit was originally developed by Matthew J Heaney. --
28 ------------------------------------------------------------------------------
30 with Ada.Unchecked_Deallocation;
32 with Ada.Containers.Red_Black_Trees.Generic_Operations;
33 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Operations);
35 with Ada.Containers.Red_Black_Trees.Generic_Keys;
36 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Keys);
38 with Ada.Containers.Red_Black_Trees.Generic_Set_Operations;
39 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Set_Operations);
41 with System; use type System.Address;
43 package body Ada.Containers.Ordered_Sets is
45 type Iterator is new Limited_Controlled and
46 Set_Iterator_Interfaces.Reversible_Iterator with
48 Container : Set_Access;
52 overriding procedure Finalize (Object : in out Iterator);
54 overriding function First (Object : Iterator) return Cursor;
55 overriding function Last (Object : Iterator) return Cursor;
57 overriding function Next
59 Position : Cursor) return Cursor;
61 overriding function Previous
63 Position : Cursor) return Cursor;
65 ------------------------------
66 -- Access to Fields of Node --
67 ------------------------------
69 -- These subprograms provide functional notation for access to fields
70 -- of a node, and procedural notation for modifying these fields.
72 function Color (Node : Node_Access) return Color_Type;
73 pragma Inline (Color);
75 function Left (Node : Node_Access) return Node_Access;
78 function Parent (Node : Node_Access) return Node_Access;
79 pragma Inline (Parent);
81 function Right (Node : Node_Access) return Node_Access;
82 pragma Inline (Right);
84 procedure Set_Color (Node : Node_Access; Color : Color_Type);
85 pragma Inline (Set_Color);
87 procedure Set_Left (Node : Node_Access; Left : Node_Access);
88 pragma Inline (Set_Left);
90 procedure Set_Right (Node : Node_Access; Right : Node_Access);
91 pragma Inline (Set_Right);
93 procedure Set_Parent (Node : Node_Access; Parent : Node_Access);
94 pragma Inline (Set_Parent);
96 -----------------------
97 -- Local Subprograms --
98 -----------------------
100 function Copy_Node (Source : Node_Access) return Node_Access;
101 pragma Inline (Copy_Node);
103 procedure Free (X : in out Node_Access);
105 procedure Insert_Sans_Hint
106 (Tree : in out Tree_Type;
107 New_Item : Element_Type;
108 Node : out Node_Access;
109 Inserted : out Boolean);
111 procedure Insert_With_Hint
112 (Dst_Tree : in out Tree_Type;
113 Dst_Hint : Node_Access;
114 Src_Node : Node_Access;
115 Dst_Node : out Node_Access);
117 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean;
118 pragma Inline (Is_Equal_Node_Node);
120 function Is_Greater_Element_Node
121 (Left : Element_Type;
122 Right : Node_Access) return Boolean;
123 pragma Inline (Is_Greater_Element_Node);
125 function Is_Less_Element_Node
126 (Left : Element_Type;
127 Right : Node_Access) return Boolean;
128 pragma Inline (Is_Less_Element_Node);
130 function Is_Less_Node_Node (L, R : Node_Access) return Boolean;
131 pragma Inline (Is_Less_Node_Node);
133 procedure Replace_Element
134 (Tree : in out Tree_Type;
136 Item : Element_Type);
138 --------------------------
139 -- Local Instantiations --
140 --------------------------
142 package Tree_Operations is
143 new Red_Black_Trees.Generic_Operations (Tree_Types);
145 procedure Delete_Tree is
146 new Tree_Operations.Generic_Delete_Tree (Free);
148 function Copy_Tree is
149 new Tree_Operations.Generic_Copy_Tree (Copy_Node, Delete_Tree);
154 new Tree_Operations.Generic_Equal (Is_Equal_Node_Node);
156 package Element_Keys is
157 new Red_Black_Trees.Generic_Keys
158 (Tree_Operations => Tree_Operations,
159 Key_Type => Element_Type,
160 Is_Less_Key_Node => Is_Less_Element_Node,
161 Is_Greater_Key_Node => Is_Greater_Element_Node);
164 new Generic_Set_Operations
165 (Tree_Operations => Tree_Operations,
166 Insert_With_Hint => Insert_With_Hint,
167 Copy_Tree => Copy_Tree,
168 Delete_Tree => Delete_Tree,
169 Is_Less => Is_Less_Node_Node,
176 function "<" (Left, Right : Cursor) return Boolean is
178 if Left.Node = null then
179 raise Constraint_Error with "Left cursor equals No_Element";
182 if Right.Node = null then
183 raise Constraint_Error with "Right cursor equals No_Element";
186 pragma Assert (Vet (Left.Container.Tree, Left.Node),
187 "bad Left cursor in ""<""");
189 pragma Assert (Vet (Right.Container.Tree, Right.Node),
190 "bad Right cursor in ""<""");
192 return Left.Node.Element < Right.Node.Element;
195 function "<" (Left : Cursor; Right : Element_Type) return Boolean is
197 if Left.Node = null then
198 raise Constraint_Error with "Left cursor equals No_Element";
201 pragma Assert (Vet (Left.Container.Tree, Left.Node),
202 "bad Left cursor in ""<""");
204 return Left.Node.Element < Right;
207 function "<" (Left : Element_Type; Right : Cursor) return Boolean is
209 if Right.Node = null then
210 raise Constraint_Error with "Right cursor equals No_Element";
213 pragma Assert (Vet (Right.Container.Tree, Right.Node),
214 "bad Right cursor in ""<""");
216 return Left < Right.Node.Element;
223 function "=" (Left, Right : Set) return Boolean is
225 return Is_Equal (Left.Tree, Right.Tree);
232 function ">" (Left, Right : Cursor) return Boolean is
234 if Left.Node = null then
235 raise Constraint_Error with "Left cursor equals No_Element";
238 if Right.Node = null then
239 raise Constraint_Error with "Right cursor equals No_Element";
242 pragma Assert (Vet (Left.Container.Tree, Left.Node),
243 "bad Left cursor in "">""");
245 pragma Assert (Vet (Right.Container.Tree, Right.Node),
246 "bad Right cursor in "">""");
248 -- L > R same as R < L
250 return Right.Node.Element < Left.Node.Element;
253 function ">" (Left : Element_Type; Right : Cursor) return Boolean is
255 if Right.Node = null then
256 raise Constraint_Error with "Right cursor equals No_Element";
259 pragma Assert (Vet (Right.Container.Tree, Right.Node),
260 "bad Right cursor in "">""");
262 return Right.Node.Element < Left;
265 function ">" (Left : Cursor; Right : Element_Type) return Boolean is
267 if Left.Node = null then
268 raise Constraint_Error with "Left cursor equals No_Element";
271 pragma Assert (Vet (Left.Container.Tree, Left.Node),
272 "bad Left cursor in "">""");
274 return Right < Left.Node.Element;
281 procedure Adjust is new Tree_Operations.Generic_Adjust (Copy_Tree);
283 procedure Adjust (Container : in out Set) is
285 Adjust (Container.Tree);
292 procedure Assign (Target : in out Set; Source : Set) is
294 if Target'Address = Source'Address then
299 Target.Union (Source);
306 function Ceiling (Container : Set; Item : Element_Type) return Cursor is
307 Node : constant Node_Access :=
308 Element_Keys.Ceiling (Container.Tree, Item);
310 return (if Node = null then No_Element
311 else Cursor'(Container'Unrestricted_Access, Node));
318 procedure Clear is new Tree_Operations.Generic_Clear (Delete_Tree);
320 procedure Clear (Container : in out Set) is
322 Clear (Container.Tree);
329 function Color (Node : Node_Access) return Color_Type is
334 ------------------------
335 -- Constant_Reference --
336 ------------------------
338 function Constant_Reference
339 (Container : aliased Set;
340 Position : Cursor) return Constant_Reference_Type
343 if Position.Container = null then
344 raise Constraint_Error with "Position cursor has no element";
347 if Position.Container /= Container'Unrestricted_Access then
348 raise Program_Error with
349 "Position cursor designates wrong container";
353 (Vet (Container.Tree, Position.Node),
354 "bad cursor in Constant_Reference");
356 return (Element => Position.Node.Element'Access);
357 end Constant_Reference;
365 Item : Element_Type) return Boolean
368 return Find (Container, Item) /= No_Element;
375 function Copy (Source : Set) return Set is
377 return Target : Set do
378 Target.Assign (Source);
386 function Copy_Node (Source : Node_Access) return Node_Access is
387 Target : constant Node_Access :=
388 new Node_Type'(Parent => null,
391 Color => Source.Color,
392 Element => Source.Element);
401 procedure Delete (Container : in out Set; Position : in out Cursor) is
403 if Position.Node = null then
404 raise Constraint_Error with "Position cursor equals No_Element";
407 if Position.Container /= Container'Unrestricted_Access then
408 raise Program_Error with "Position cursor designates wrong set";
411 pragma Assert (Vet (Container.Tree, Position.Node),
412 "bad cursor in Delete");
414 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
415 Free (Position.Node);
416 Position.Container := null;
419 procedure Delete (Container : in out Set; Item : Element_Type) is
420 X : Node_Access := Element_Keys.Find (Container.Tree, Item);
424 raise Constraint_Error with "attempt to delete element not in set";
427 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
435 procedure Delete_First (Container : in out Set) is
436 Tree : Tree_Type renames Container.Tree;
437 X : Node_Access := Tree.First;
440 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
449 procedure Delete_Last (Container : in out Set) is
450 Tree : Tree_Type renames Container.Tree;
451 X : Node_Access := Tree.Last;
454 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
463 procedure Difference (Target : in out Set; Source : Set) is
465 Set_Ops.Difference (Target.Tree, Source.Tree);
468 function Difference (Left, Right : Set) return Set is
469 Tree : constant Tree_Type :=
470 Set_Ops.Difference (Left.Tree, Right.Tree);
472 return Set'(Controlled with Tree);
479 function Element (Position : Cursor) return Element_Type is
481 if Position.Node = null then
482 raise Constraint_Error with "Position cursor equals No_Element";
485 pragma Assert (Vet (Position.Container.Tree, Position.Node),
486 "bad cursor in Element");
488 return Position.Node.Element;
491 -------------------------
492 -- Equivalent_Elements --
493 -------------------------
495 function Equivalent_Elements (Left, Right : Element_Type) return Boolean is
497 return (if Left < Right or else Right < Left then False else True);
498 end Equivalent_Elements;
500 ---------------------
501 -- Equivalent_Sets --
502 ---------------------
504 function Equivalent_Sets (Left, Right : Set) return Boolean is
505 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean;
506 pragma Inline (Is_Equivalent_Node_Node);
508 function Is_Equivalent is
509 new Tree_Operations.Generic_Equal (Is_Equivalent_Node_Node);
511 -----------------------------
512 -- Is_Equivalent_Node_Node --
513 -----------------------------
515 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean is
517 return (if L.Element < R.Element then False
518 elsif R.Element < L.Element then False
520 end Is_Equivalent_Node_Node;
522 -- Start of processing for Equivalent_Sets
525 return Is_Equivalent (Left.Tree, Right.Tree);
532 procedure Exclude (Container : in out Set; Item : Element_Type) is
533 X : Node_Access := Element_Keys.Find (Container.Tree, Item);
537 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
546 procedure Finalize (Object : in out Iterator) is
548 if Object.Container /= null then
550 B : Natural renames Object.Container.all.Tree.Busy;
561 function Find (Container : Set; Item : Element_Type) return Cursor is
562 Node : constant Node_Access :=
563 Element_Keys.Find (Container.Tree, Item);
565 return (if Node = null then No_Element
566 else Cursor'(Container'Unrestricted_Access, Node));
573 function First (Container : Set) return Cursor is
576 (if Container.Tree.First = null then No_Element
577 else Cursor'(Container'Unrestricted_Access, Container.Tree.First));
580 function First (Object : Iterator) return Cursor is
582 -- The value of the iterator object's Node component influences the
583 -- behavior of the First (and Last) selector function.
585 -- When the Node component is null, this means the iterator object was
586 -- constructed without a start expression, in which case the (forward)
587 -- iteration starts from the (logical) beginning of the entire sequence
588 -- of items (corresponding to Container.First, for a forward iterator).
590 -- Otherwise, this is iteration over a partial sequence of items. When
591 -- the Node component is non-null, the iterator object was constructed
592 -- with a start expression, that specifies the position from which the
593 -- (forward) partial iteration begins.
595 if Object.Node = null then
596 return Object.Container.First;
598 return Cursor'(Object.Container, Object.Node);
606 function First_Element (Container : Set) return Element_Type is
608 if Container.Tree.First = null then
609 raise Constraint_Error with "set is empty";
612 return Container.Tree.First.Element;
619 function Floor (Container : Set; Item : Element_Type) return Cursor is
620 Node : constant Node_Access :=
621 Element_Keys.Floor (Container.Tree, Item);
623 return (if Node = null then No_Element
624 else Cursor'(Container'Unrestricted_Access, Node));
631 procedure Free (X : in out Node_Access) is
632 procedure Deallocate is
633 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
647 package body Generic_Keys is
649 -----------------------
650 -- Local Subprograms --
651 -----------------------
653 function Is_Greater_Key_Node
655 Right : Node_Access) return Boolean;
656 pragma Inline (Is_Greater_Key_Node);
658 function Is_Less_Key_Node
660 Right : Node_Access) return Boolean;
661 pragma Inline (Is_Less_Key_Node);
663 --------------------------
664 -- Local Instantiations --
665 --------------------------
668 new Red_Black_Trees.Generic_Keys
669 (Tree_Operations => Tree_Operations,
670 Key_Type => Key_Type,
671 Is_Less_Key_Node => Is_Less_Key_Node,
672 Is_Greater_Key_Node => Is_Greater_Key_Node);
678 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
679 Node : constant Node_Access :=
680 Key_Keys.Ceiling (Container.Tree, Key);
682 return (if Node = null then No_Element
683 else Cursor'(Container'Unrestricted_Access, Node));
686 ------------------------
687 -- Constant_Reference --
688 ------------------------
690 function Constant_Reference
691 (Container : aliased Set;
692 Key : Key_Type) return Constant_Reference_Type
694 Node : constant Node_Access :=
695 Key_Keys.Find (Container.Tree, Key);
699 raise Constraint_Error with "key not in set";
702 return (Element => Node.Element'Access);
703 end Constant_Reference;
709 function Contains (Container : Set; Key : Key_Type) return Boolean is
711 return Find (Container, Key) /= No_Element;
718 procedure Delete (Container : in out Set; Key : Key_Type) is
719 X : Node_Access := Key_Keys.Find (Container.Tree, Key);
723 raise Constraint_Error with "attempt to delete key not in set";
726 Delete_Node_Sans_Free (Container.Tree, X);
734 function Element (Container : Set; Key : Key_Type) return Element_Type is
735 Node : constant Node_Access :=
736 Key_Keys.Find (Container.Tree, Key);
740 raise Constraint_Error with "key not in set";
746 ---------------------
747 -- Equivalent_Keys --
748 ---------------------
750 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
752 return (if Left < Right or else Right < Left then False else True);
759 procedure Exclude (Container : in out Set; Key : Key_Type) is
760 X : Node_Access := Key_Keys.Find (Container.Tree, Key);
763 Delete_Node_Sans_Free (Container.Tree, X);
772 function Find (Container : Set; Key : Key_Type) return Cursor is
773 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
775 return (if Node = null then No_Element
776 else Cursor'(Container'Unrestricted_Access, Node));
783 function Floor (Container : Set; Key : Key_Type) return Cursor is
784 Node : constant Node_Access := Key_Keys.Floor (Container.Tree, Key);
786 return (if Node = null then No_Element
787 else Cursor'(Container'Unrestricted_Access, Node));
790 -------------------------
791 -- Is_Greater_Key_Node --
792 -------------------------
794 function Is_Greater_Key_Node
796 Right : Node_Access) return Boolean
799 return Key (Right.Element) < Left;
800 end Is_Greater_Key_Node;
802 ----------------------
803 -- Is_Less_Key_Node --
804 ----------------------
806 function Is_Less_Key_Node
808 Right : Node_Access) return Boolean
811 return Left < Key (Right.Element);
812 end Is_Less_Key_Node;
818 function Key (Position : Cursor) return Key_Type is
820 if Position.Node = null then
821 raise Constraint_Error with
822 "Position cursor equals No_Element";
825 pragma Assert (Vet (Position.Container.Tree, Position.Node),
826 "bad cursor in Key");
828 return Key (Position.Node.Element);
836 (Stream : not null access Root_Stream_Type'Class;
837 Item : out Reference_Type)
840 raise Program_Error with "attempt to stream reference";
843 ------------------------------
844 -- Reference_Preserving_Key --
845 ------------------------------
847 function Reference_Preserving_Key
848 (Container : aliased in out Set;
849 Position : Cursor) return Reference_Type
852 if Position.Container = null then
853 raise Constraint_Error with "Position cursor has no element";
856 if Position.Container /= Container'Unrestricted_Access then
857 raise Program_Error with
858 "Position cursor designates wrong container";
862 (Vet (Container.Tree, Position.Node),
863 "bad cursor in function Reference_Preserving_Key");
865 -- Some form of finalization will be required in order to actually
866 -- check that the key-part of the element designated by Position has
869 return (Element => Position.Node.Element'Access);
870 end Reference_Preserving_Key;
872 function Reference_Preserving_Key
873 (Container : aliased in out Set;
874 Key : Key_Type) return Reference_Type
876 Node : constant Node_Access :=
877 Key_Keys.Find (Container.Tree, Key);
881 raise Constraint_Error with "key not in set";
884 -- Some form of finalization will be required in order to actually
885 -- check that the key-part of the element designated by Position has
888 return (Element => Node.Element'Access);
889 end Reference_Preserving_Key;
896 (Container : in out Set;
898 New_Item : Element_Type)
900 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
904 raise Constraint_Error with
905 "attempt to replace key not in set";
908 Replace_Element (Container.Tree, Node, New_Item);
911 -----------------------------------
912 -- Update_Element_Preserving_Key --
913 -----------------------------------
915 procedure Update_Element_Preserving_Key
916 (Container : in out Set;
918 Process : not null access procedure (Element : in out Element_Type))
920 Tree : Tree_Type renames Container.Tree;
923 if Position.Node = null then
924 raise Constraint_Error with
925 "Position cursor equals No_Element";
928 if Position.Container /= Container'Unrestricted_Access then
929 raise Program_Error with
930 "Position cursor designates wrong set";
933 pragma Assert (Vet (Container.Tree, Position.Node),
934 "bad cursor in Update_Element_Preserving_Key");
937 E : Element_Type renames Position.Node.Element;
938 K : constant Key_Type := Key (E);
940 B : Natural renames Tree.Busy;
941 L : Natural renames Tree.Lock;
959 if Equivalent_Keys (K, Key (E)) then
965 X : Node_Access := Position.Node;
967 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
971 raise Program_Error with "key was modified";
972 end Update_Element_Preserving_Key;
979 (Stream : not null access Root_Stream_Type'Class;
980 Item : Reference_Type)
983 raise Program_Error with "attempt to stream reference";
992 function Has_Element (Position : Cursor) return Boolean is
994 return Position /= No_Element;
1001 procedure Include (Container : in out Set; New_Item : Element_Type) is
1006 Insert (Container, New_Item, Position, Inserted);
1008 if not Inserted then
1009 if Container.Tree.Lock > 0 then
1010 raise Program_Error with
1011 "attempt to tamper with elements (set is locked)";
1014 Position.Node.Element := New_Item;
1023 (Container : in out Set;
1024 New_Item : Element_Type;
1025 Position : out Cursor;
1026 Inserted : out Boolean)
1035 Position.Container := Container'Unrestricted_Access;
1039 (Container : in out Set;
1040 New_Item : Element_Type)
1043 pragma Unreferenced (Position);
1048 Insert (Container, New_Item, Position, Inserted);
1050 if not Inserted then
1051 raise Constraint_Error with
1052 "attempt to insert element already in set";
1056 ----------------------
1057 -- Insert_Sans_Hint --
1058 ----------------------
1060 procedure Insert_Sans_Hint
1061 (Tree : in out Tree_Type;
1062 New_Item : Element_Type;
1063 Node : out Node_Access;
1064 Inserted : out Boolean)
1066 function New_Node return Node_Access;
1067 pragma Inline (New_Node);
1069 procedure Insert_Post is
1070 new Element_Keys.Generic_Insert_Post (New_Node);
1072 procedure Conditional_Insert_Sans_Hint is
1073 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1079 function New_Node return Node_Access is
1081 return new Node_Type'(Parent => null,
1084 Color => Red_Black_Trees.Red,
1085 Element => New_Item);
1088 -- Start of processing for Insert_Sans_Hint
1091 Conditional_Insert_Sans_Hint
1096 end Insert_Sans_Hint;
1098 ----------------------
1099 -- Insert_With_Hint --
1100 ----------------------
1102 procedure Insert_With_Hint
1103 (Dst_Tree : in out Tree_Type;
1104 Dst_Hint : Node_Access;
1105 Src_Node : Node_Access;
1106 Dst_Node : out Node_Access)
1109 pragma Unreferenced (Success);
1111 function New_Node return Node_Access;
1112 pragma Inline (New_Node);
1114 procedure Insert_Post is
1115 new Element_Keys.Generic_Insert_Post (New_Node);
1117 procedure Insert_Sans_Hint is
1118 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1120 procedure Local_Insert_With_Hint is
1121 new Element_Keys.Generic_Conditional_Insert_With_Hint
1129 function New_Node return Node_Access is
1130 Node : constant Node_Access :=
1131 new Node_Type'(Parent => null,
1135 Element => Src_Node.Element);
1140 -- Start of processing for Insert_With_Hint
1143 Local_Insert_With_Hint
1149 end Insert_With_Hint;
1155 procedure Intersection (Target : in out Set; Source : Set) is
1157 Set_Ops.Intersection (Target.Tree, Source.Tree);
1160 function Intersection (Left, Right : Set) return Set is
1161 Tree : constant Tree_Type :=
1162 Set_Ops.Intersection (Left.Tree, Right.Tree);
1164 return Set'(Controlled with Tree);
1171 function Is_Empty (Container : Set) return Boolean is
1173 return Container.Tree.Length = 0;
1176 ------------------------
1177 -- Is_Equal_Node_Node --
1178 ------------------------
1180 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
1182 return L.Element = R.Element;
1183 end Is_Equal_Node_Node;
1185 -----------------------------
1186 -- Is_Greater_Element_Node --
1187 -----------------------------
1189 function Is_Greater_Element_Node
1190 (Left : Element_Type;
1191 Right : Node_Access) return Boolean
1194 -- Compute e > node same as node < e
1196 return Right.Element < Left;
1197 end Is_Greater_Element_Node;
1199 --------------------------
1200 -- Is_Less_Element_Node --
1201 --------------------------
1203 function Is_Less_Element_Node
1204 (Left : Element_Type;
1205 Right : Node_Access) return Boolean
1208 return Left < Right.Element;
1209 end Is_Less_Element_Node;
1211 -----------------------
1212 -- Is_Less_Node_Node --
1213 -----------------------
1215 function Is_Less_Node_Node (L, R : Node_Access) return Boolean is
1217 return L.Element < R.Element;
1218 end Is_Less_Node_Node;
1224 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1226 return Set_Ops.Is_Subset (Subset => Subset.Tree, Of_Set => Of_Set.Tree);
1235 Process : not null access procedure (Position : Cursor))
1237 procedure Process_Node (Node : Node_Access);
1238 pragma Inline (Process_Node);
1240 procedure Local_Iterate is
1241 new Tree_Operations.Generic_Iteration (Process_Node);
1247 procedure Process_Node (Node : Node_Access) is
1249 Process (Cursor'(Container'Unrestricted_Access, Node));
1252 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
1253 B : Natural renames T.Busy;
1255 -- Start of processing for Iterate
1271 function Iterate (Container : Set)
1272 return Set_Iterator_Interfaces.Reversible_Iterator'Class
1274 B : Natural renames Container'Unrestricted_Access.all.Tree.Busy;
1277 -- The value of the Node component influences the behavior of the First
1278 -- and Last selector functions of the iterator object. When the Node
1279 -- component is null (as is the case here), this means the iterator
1280 -- object was constructed without a start expression. This is a complete
1281 -- iterator, meaning that the iteration starts from the (logical)
1282 -- beginning of the sequence of items.
1284 -- Note: For a forward iterator, Container.First is the beginning, and
1285 -- for a reverse iterator, Container.Last is the beginning.
1289 return It : constant Iterator :=
1290 Iterator'(Limited_Controlled with
1291 Container => Container'Unrestricted_Access,
1295 function Iterate (Container : Set; Start : Cursor)
1296 return Set_Iterator_Interfaces.Reversible_Iterator'Class
1298 B : Natural renames Container'Unrestricted_Access.all.Tree.Busy;
1301 -- It was formerly the case that when Start = No_Element, the partial
1302 -- iterator was defined to behave the same as for a complete iterator,
1303 -- and iterate over the entire sequence of items. However, those
1304 -- semantics were unintuitive and arguably error-prone (it is too easy
1305 -- to accidentally create an endless loop), and so they were changed,
1306 -- per the ARG meeting in Denver on 2011/11. However, there was no
1307 -- consensus about what positive meaning this corner case should have,
1308 -- and so it was decided to simply raise an exception. This does imply,
1309 -- however, that it is not possible to use a partial iterator to specify
1310 -- an empty sequence of items.
1312 if Start = No_Element then
1313 raise Constraint_Error with
1314 "Start position for iterator equals No_Element";
1317 if Start.Container /= Container'Unrestricted_Access then
1318 raise Program_Error with
1319 "Start cursor of Iterate designates wrong set";
1322 pragma Assert (Vet (Container.Tree, Start.Node),
1323 "Start cursor of Iterate is bad");
1325 -- The value of the Node component influences the behavior of the First
1326 -- and Last selector functions of the iterator object. When the Node
1327 -- component is non-null (as is the case here), it means that this is a
1328 -- partial iteration, over a subset of the complete sequence of
1329 -- items. The iterator object was constructed with a start expression,
1330 -- indicating the position from which the iteration begins. Note that
1331 -- the start position has the same value irrespective of whether this is
1332 -- a forward or reverse iteration.
1336 return It : constant Iterator :=
1337 Iterator'(Limited_Controlled with
1338 Container => Container'Unrestricted_Access,
1339 Node => Start.Node);
1346 function Last (Container : Set) return Cursor is
1349 (if Container.Tree.Last = null then No_Element
1350 else Cursor'(Container'Unrestricted_Access, Container.Tree.Last));
1353 function Last (Object : Iterator) return Cursor is
1355 -- The value of the iterator object's Node component influences the
1356 -- behavior of the Last (and First) selector function.
1358 -- When the Node component is null, this means the iterator object was
1359 -- constructed without a start expression, in which case the (reverse)
1360 -- iteration starts from the (logical) beginning of the entire sequence
1361 -- (corresponding to Container.Last, for a reverse iterator).
1363 -- Otherwise, this is iteration over a partial sequence of items. When
1364 -- the Node component is non-null, the iterator object was constructed
1365 -- with a start expression, that specifies the position from which the
1366 -- (reverse) partial iteration begins.
1368 if Object.Node = null then
1369 return Object.Container.Last;
1371 return Cursor'(Object.Container, Object.Node);
1379 function Last_Element (Container : Set) return Element_Type is
1381 if Container.Tree.Last = null then
1382 raise Constraint_Error with "set is empty";
1384 return Container.Tree.Last.Element;
1392 function Left (Node : Node_Access) return Node_Access is
1401 function Length (Container : Set) return Count_Type is
1403 return Container.Tree.Length;
1410 procedure Move is new Tree_Operations.Generic_Move (Clear);
1412 procedure Move (Target : in out Set; Source : in out Set) is
1414 Move (Target => Target.Tree, Source => Source.Tree);
1421 function Next (Position : Cursor) return Cursor is
1423 if Position = No_Element then
1427 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1428 "bad cursor in Next");
1431 Node : constant Node_Access :=
1432 Tree_Operations.Next (Position.Node);
1434 return (if Node = null then No_Element
1435 else Cursor'(Position.Container, Node));
1439 procedure Next (Position : in out Cursor) is
1441 Position := Next (Position);
1444 function Next (Object : Iterator; Position : Cursor) return Cursor is
1446 if Position.Container = null then
1450 if Position.Container /= Object.Container then
1451 raise Program_Error with
1452 "Position cursor of Next designates wrong set";
1455 return Next (Position);
1462 function Overlap (Left, Right : Set) return Boolean is
1464 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1471 function Parent (Node : Node_Access) return Node_Access is
1480 function Previous (Position : Cursor) return Cursor is
1482 if Position = No_Element then
1486 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1487 "bad cursor in Previous");
1490 Node : constant Node_Access :=
1491 Tree_Operations.Previous (Position.Node);
1493 return (if Node = null then No_Element
1494 else Cursor'(Position.Container, Node));
1498 procedure Previous (Position : in out Cursor) is
1500 Position := Previous (Position);
1503 function Previous (Object : Iterator; Position : Cursor) return Cursor is
1505 if Position.Container = null then
1509 if Position.Container /= Object.Container then
1510 raise Program_Error with
1511 "Position cursor of Previous designates wrong set";
1514 return Previous (Position);
1521 procedure Query_Element
1523 Process : not null access procedure (Element : Element_Type))
1526 if Position.Node = null then
1527 raise Constraint_Error with "Position cursor equals No_Element";
1530 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1531 "bad cursor in Query_Element");
1534 T : Tree_Type renames Position.Container.Tree;
1536 B : Natural renames T.Busy;
1537 L : Natural renames T.Lock;
1544 Process (Position.Node.Element);
1562 (Stream : not null access Root_Stream_Type'Class;
1563 Container : out Set)
1566 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1567 pragma Inline (Read_Node);
1570 new Tree_Operations.Generic_Read (Clear, Read_Node);
1577 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1579 Node : Node_Access := new Node_Type;
1581 Element_Type'Read (Stream, Node.Element);
1589 -- Start of processing for Read
1592 Read (Stream, Container.Tree);
1596 (Stream : not null access Root_Stream_Type'Class;
1600 raise Program_Error with "attempt to stream set cursor";
1604 (Stream : not null access Root_Stream_Type'Class;
1605 Item : out Constant_Reference_Type)
1608 raise Program_Error with "attempt to stream reference";
1615 procedure Replace (Container : in out Set; New_Item : Element_Type) is
1616 Node : constant Node_Access :=
1617 Element_Keys.Find (Container.Tree, New_Item);
1621 raise Constraint_Error with
1622 "attempt to replace element not in set";
1625 if Container.Tree.Lock > 0 then
1626 raise Program_Error with
1627 "attempt to tamper with elements (set is locked)";
1630 Node.Element := New_Item;
1633 ---------------------
1634 -- Replace_Element --
1635 ---------------------
1637 procedure Replace_Element
1638 (Tree : in out Tree_Type;
1640 Item : Element_Type)
1642 pragma Assert (Node /= null);
1644 function New_Node return Node_Access;
1645 pragma Inline (New_Node);
1647 procedure Local_Insert_Post is
1648 new Element_Keys.Generic_Insert_Post (New_Node);
1650 procedure Local_Insert_Sans_Hint is
1651 new Element_Keys.Generic_Conditional_Insert (Local_Insert_Post);
1653 procedure Local_Insert_With_Hint is
1654 new Element_Keys.Generic_Conditional_Insert_With_Hint
1656 Local_Insert_Sans_Hint);
1662 function New_Node return Node_Access is
1664 Node.Element := Item;
1666 Node.Parent := null;
1673 Result : Node_Access;
1676 -- Start of processing for Replace_Element
1679 if Item < Node.Element or else Node.Element < Item then
1683 if Tree.Lock > 0 then
1684 raise Program_Error with
1685 "attempt to tamper with elements (set is locked)";
1688 Node.Element := Item;
1692 Hint := Element_Keys.Ceiling (Tree, Item);
1697 elsif Item < Hint.Element then
1699 if Tree.Lock > 0 then
1700 raise Program_Error with
1701 "attempt to tamper with elements (set is locked)";
1704 Node.Element := Item;
1709 pragma Assert (not (Hint.Element < Item));
1710 raise Program_Error with "attempt to replace existing element";
1713 Tree_Operations.Delete_Node_Sans_Free (Tree, Node); -- Checks busy-bit
1715 Local_Insert_With_Hint
1720 Inserted => Inserted);
1722 pragma Assert (Inserted);
1723 pragma Assert (Result = Node);
1724 end Replace_Element;
1726 procedure Replace_Element
1727 (Container : in out Set;
1729 New_Item : Element_Type)
1732 if Position.Node = null then
1733 raise Constraint_Error with
1734 "Position cursor equals No_Element";
1737 if Position.Container /= Container'Unrestricted_Access then
1738 raise Program_Error with
1739 "Position cursor designates wrong set";
1742 pragma Assert (Vet (Container.Tree, Position.Node),
1743 "bad cursor in Replace_Element");
1745 Replace_Element (Container.Tree, Position.Node, New_Item);
1746 end Replace_Element;
1748 ---------------------
1749 -- Reverse_Iterate --
1750 ---------------------
1752 procedure Reverse_Iterate
1754 Process : not null access procedure (Position : Cursor))
1756 procedure Process_Node (Node : Node_Access);
1757 pragma Inline (Process_Node);
1759 procedure Local_Reverse_Iterate is
1760 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1766 procedure Process_Node (Node : Node_Access) is
1768 Process (Cursor'(Container'Unrestricted_Access, Node));
1771 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1772 B : Natural renames T.Busy;
1774 -- Start of processing for Reverse_Iterate
1780 Local_Reverse_Iterate (T);
1788 end Reverse_Iterate;
1794 function Right (Node : Node_Access) return Node_Access is
1803 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1805 Node.Color := Color;
1812 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1821 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1823 Node.Parent := Parent;
1830 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1832 Node.Right := Right;
1835 --------------------------
1836 -- Symmetric_Difference --
1837 --------------------------
1839 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1841 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
1842 end Symmetric_Difference;
1844 function Symmetric_Difference (Left, Right : Set) return Set is
1845 Tree : constant Tree_Type :=
1846 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
1848 return Set'(Controlled with Tree);
1849 end Symmetric_Difference;
1855 function To_Set (New_Item : Element_Type) return Set is
1859 pragma Unreferenced (Node, Inserted);
1861 Insert_Sans_Hint (Tree, New_Item, Node, Inserted);
1862 return Set'(Controlled with Tree);
1869 procedure Union (Target : in out Set; Source : Set) is
1871 Set_Ops.Union (Target.Tree, Source.Tree);
1874 function Union (Left, Right : Set) return Set is
1875 Tree : constant Tree_Type :=
1876 Set_Ops.Union (Left.Tree, Right.Tree);
1878 return Set'(Controlled with Tree);
1886 (Stream : not null access Root_Stream_Type'Class;
1889 procedure Write_Node
1890 (Stream : not null access Root_Stream_Type'Class;
1891 Node : Node_Access);
1892 pragma Inline (Write_Node);
1895 new Tree_Operations.Generic_Write (Write_Node);
1901 procedure Write_Node
1902 (Stream : not null access Root_Stream_Type'Class;
1906 Element_Type'Write (Stream, Node.Element);
1909 -- Start of processing for Write
1912 Write (Stream, Container.Tree);
1916 (Stream : not null access Root_Stream_Type'Class;
1920 raise Program_Error with "attempt to stream set cursor";
1924 (Stream : not null access Root_Stream_Type'Class;
1925 Item : Constant_Reference_Type)
1928 raise Program_Error with "attempt to stream reference";
1931 end Ada.Containers.Ordered_Sets;