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 package body Ada.Containers.Ordered_Sets is
44 Ordered_Set_Iterator_Interfaces.Reversible_Iterator with record
45 Container : access constant Set;
49 overriding function First (Object : Iterator) return Cursor;
51 overriding function Last (Object : Iterator) return Cursor;
53 overriding function Next
55 Position : Cursor) return Cursor;
57 overriding function Previous
59 Position : Cursor) return Cursor;
61 ------------------------------
62 -- Access to Fields of Node --
63 ------------------------------
65 -- These subprograms provide functional notation for access to fields
66 -- of a node, and procedural notation for modifying these fields.
68 function Color (Node : Node_Access) return Color_Type;
69 pragma Inline (Color);
71 function Left (Node : Node_Access) return Node_Access;
74 function Parent (Node : Node_Access) return Node_Access;
75 pragma Inline (Parent);
77 function Right (Node : Node_Access) return Node_Access;
78 pragma Inline (Right);
80 procedure Set_Color (Node : Node_Access; Color : Color_Type);
81 pragma Inline (Set_Color);
83 procedure Set_Left (Node : Node_Access; Left : Node_Access);
84 pragma Inline (Set_Left);
86 procedure Set_Right (Node : Node_Access; Right : Node_Access);
87 pragma Inline (Set_Right);
89 procedure Set_Parent (Node : Node_Access; Parent : Node_Access);
90 pragma Inline (Set_Parent);
92 -----------------------
93 -- Local Subprograms --
94 -----------------------
96 function Copy_Node (Source : Node_Access) return Node_Access;
97 pragma Inline (Copy_Node);
99 procedure Free (X : in out Node_Access);
101 procedure Insert_Sans_Hint
102 (Tree : in out Tree_Type;
103 New_Item : Element_Type;
104 Node : out Node_Access;
105 Inserted : out Boolean);
107 procedure Insert_With_Hint
108 (Dst_Tree : in out Tree_Type;
109 Dst_Hint : Node_Access;
110 Src_Node : Node_Access;
111 Dst_Node : out Node_Access);
113 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean;
114 pragma Inline (Is_Equal_Node_Node);
116 function Is_Greater_Element_Node
117 (Left : Element_Type;
118 Right : Node_Access) return Boolean;
119 pragma Inline (Is_Greater_Element_Node);
121 function Is_Less_Element_Node
122 (Left : Element_Type;
123 Right : Node_Access) return Boolean;
124 pragma Inline (Is_Less_Element_Node);
126 function Is_Less_Node_Node (L, R : Node_Access) return Boolean;
127 pragma Inline (Is_Less_Node_Node);
129 procedure Replace_Element
130 (Tree : in out Tree_Type;
132 Item : Element_Type);
134 --------------------------
135 -- Local Instantiations --
136 --------------------------
138 package Tree_Operations is
139 new Red_Black_Trees.Generic_Operations (Tree_Types);
141 procedure Delete_Tree is
142 new Tree_Operations.Generic_Delete_Tree (Free);
144 function Copy_Tree is
145 new Tree_Operations.Generic_Copy_Tree (Copy_Node, Delete_Tree);
150 new Tree_Operations.Generic_Equal (Is_Equal_Node_Node);
152 package Element_Keys is
153 new Red_Black_Trees.Generic_Keys
154 (Tree_Operations => Tree_Operations,
155 Key_Type => Element_Type,
156 Is_Less_Key_Node => Is_Less_Element_Node,
157 Is_Greater_Key_Node => Is_Greater_Element_Node);
160 new Generic_Set_Operations
161 (Tree_Operations => Tree_Operations,
162 Insert_With_Hint => Insert_With_Hint,
163 Copy_Tree => Copy_Tree,
164 Delete_Tree => Delete_Tree,
165 Is_Less => Is_Less_Node_Node,
172 function "<" (Left, Right : Cursor) return Boolean is
174 if Left.Node = null then
175 raise Constraint_Error with "Left cursor equals No_Element";
178 if Right.Node = null then
179 raise Constraint_Error with "Right cursor equals No_Element";
182 pragma Assert (Vet (Left.Container.Tree, Left.Node),
183 "bad Left cursor in ""<""");
185 pragma Assert (Vet (Right.Container.Tree, Right.Node),
186 "bad Right cursor in ""<""");
188 return Left.Node.Element < Right.Node.Element;
191 function "<" (Left : Cursor; Right : Element_Type) return Boolean is
193 if Left.Node = null then
194 raise Constraint_Error with "Left cursor equals No_Element";
197 pragma Assert (Vet (Left.Container.Tree, Left.Node),
198 "bad Left cursor in ""<""");
200 return Left.Node.Element < Right;
203 function "<" (Left : Element_Type; Right : Cursor) return Boolean is
205 if Right.Node = null then
206 raise Constraint_Error with "Right cursor equals No_Element";
209 pragma Assert (Vet (Right.Container.Tree, Right.Node),
210 "bad Right cursor in ""<""");
212 return Left < Right.Node.Element;
219 function "=" (Left, Right : Set) return Boolean is
221 return Is_Equal (Left.Tree, Right.Tree);
228 function ">" (Left, Right : Cursor) return Boolean is
230 if Left.Node = null then
231 raise Constraint_Error with "Left cursor equals No_Element";
234 if Right.Node = null then
235 raise Constraint_Error with "Right cursor equals No_Element";
238 pragma Assert (Vet (Left.Container.Tree, Left.Node),
239 "bad Left cursor in "">""");
241 pragma Assert (Vet (Right.Container.Tree, Right.Node),
242 "bad Right cursor in "">""");
244 -- L > R same as R < L
246 return Right.Node.Element < Left.Node.Element;
249 function ">" (Left : Element_Type; Right : Cursor) return Boolean is
251 if Right.Node = null then
252 raise Constraint_Error with "Right cursor equals No_Element";
255 pragma Assert (Vet (Right.Container.Tree, Right.Node),
256 "bad Right cursor in "">""");
258 return Right.Node.Element < Left;
261 function ">" (Left : Cursor; Right : Element_Type) return Boolean is
263 if Left.Node = null then
264 raise Constraint_Error with "Left cursor equals No_Element";
267 pragma Assert (Vet (Left.Container.Tree, Left.Node),
268 "bad Left cursor in "">""");
270 return Right < Left.Node.Element;
277 procedure Adjust is new Tree_Operations.Generic_Adjust (Copy_Tree);
279 procedure Adjust (Container : in out Set) is
281 Adjust (Container.Tree);
288 function Ceiling (Container : Set; Item : Element_Type) return Cursor is
289 Node : constant Node_Access :=
290 Element_Keys.Ceiling (Container.Tree, Item);
297 return Cursor'(Container'Unrestricted_Access, Node);
304 procedure Clear is new Tree_Operations.Generic_Clear (Delete_Tree);
306 procedure Clear (Container : in out Set) is
308 Clear (Container.Tree);
315 function Color (Node : Node_Access) return Color_Type is
326 Item : Element_Type) return Boolean
329 return Find (Container, Item) /= No_Element;
336 function Copy_Node (Source : Node_Access) return Node_Access is
337 Target : constant Node_Access :=
338 new Node_Type'(Parent => null,
341 Color => Source.Color,
342 Element => Source.Element);
351 procedure Delete (Container : in out Set; Position : in out Cursor) is
353 if Position.Node = null then
354 raise Constraint_Error with "Position cursor equals No_Element";
357 if Position.Container /= Container'Unrestricted_Access then
358 raise Program_Error with "Position cursor designates wrong set";
361 pragma Assert (Vet (Container.Tree, Position.Node),
362 "bad cursor in Delete");
364 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
365 Free (Position.Node);
366 Position.Container := null;
369 procedure Delete (Container : in out Set; Item : Element_Type) is
370 X : Node_Access := Element_Keys.Find (Container.Tree, Item);
374 raise Constraint_Error with "attempt to delete element not in set";
377 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
385 procedure Delete_First (Container : in out Set) is
386 Tree : Tree_Type renames Container.Tree;
387 X : Node_Access := Tree.First;
391 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
400 procedure Delete_Last (Container : in out Set) is
401 Tree : Tree_Type renames Container.Tree;
402 X : Node_Access := Tree.Last;
406 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
415 procedure Difference (Target : in out Set; Source : Set) is
417 Set_Ops.Difference (Target.Tree, Source.Tree);
420 function Difference (Left, Right : Set) return Set is
421 Tree : constant Tree_Type :=
422 Set_Ops.Difference (Left.Tree, Right.Tree);
424 return Set'(Controlled with Tree);
431 function Element (Position : Cursor) return Element_Type is
433 if Position.Node = null then
434 raise Constraint_Error with "Position cursor equals No_Element";
437 pragma Assert (Vet (Position.Container.Tree, Position.Node),
438 "bad cursor in Element");
440 return Position.Node.Element;
443 -------------------------
444 -- Equivalent_Elements --
445 -------------------------
447 function Equivalent_Elements (Left, Right : Element_Type) return Boolean is
456 end Equivalent_Elements;
458 ---------------------
459 -- Equivalent_Sets --
460 ---------------------
462 function Equivalent_Sets (Left, Right : Set) return Boolean is
463 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean;
464 pragma Inline (Is_Equivalent_Node_Node);
466 function Is_Equivalent is
467 new Tree_Operations.Generic_Equal (Is_Equivalent_Node_Node);
469 -----------------------------
470 -- Is_Equivalent_Node_Node --
471 -----------------------------
473 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean is
475 if L.Element < R.Element then
477 elsif R.Element < L.Element then
482 end Is_Equivalent_Node_Node;
484 -- Start of processing for Equivalent_Sets
487 return Is_Equivalent (Left.Tree, Right.Tree);
494 procedure Exclude (Container : in out Set; Item : Element_Type) is
495 X : Node_Access := Element_Keys.Find (Container.Tree, Item);
499 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
508 function Find (Container : Set; Item : Element_Type) return Cursor is
509 Node : constant Node_Access :=
510 Element_Keys.Find (Container.Tree, Item);
517 return Cursor'(Container'Unrestricted_Access, Node);
524 function First (Container : Set) return Cursor is
526 if Container.Tree.First = null then
530 return Cursor'(Container'Unrestricted_Access, Container.Tree.First);
533 function First (Object : Iterator) return Cursor is
535 if Object.Container = null then
540 Object.Container.all'Unrestricted_Access,
541 Object.Container.Tree.First);
549 function First_Element (Container : Set) return Element_Type is
551 if Container.Tree.First = null then
552 raise Constraint_Error with "set is empty";
555 return Container.Tree.First.Element;
562 function Floor (Container : Set; Item : Element_Type) return Cursor is
563 Node : constant Node_Access :=
564 Element_Keys.Floor (Container.Tree, Item);
571 return Cursor'(Container'Unrestricted_Access, Node);
578 procedure Free (X : in out Node_Access) is
579 procedure Deallocate is
580 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
596 package body Generic_Keys is
598 -----------------------
599 -- Local Subprograms --
600 -----------------------
602 function Is_Greater_Key_Node
604 Right : Node_Access) return Boolean;
605 pragma Inline (Is_Greater_Key_Node);
607 function Is_Less_Key_Node
609 Right : Node_Access) return Boolean;
610 pragma Inline (Is_Less_Key_Node);
612 --------------------------
613 -- Local Instantiations --
614 --------------------------
617 new Red_Black_Trees.Generic_Keys
618 (Tree_Operations => Tree_Operations,
619 Key_Type => Key_Type,
620 Is_Less_Key_Node => Is_Less_Key_Node,
621 Is_Greater_Key_Node => Is_Greater_Key_Node);
627 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
628 Node : constant Node_Access :=
629 Key_Keys.Ceiling (Container.Tree, Key);
636 return Cursor'(Container'Unrestricted_Access, Node);
643 function Contains (Container : Set; Key : Key_Type) return Boolean is
645 return Find (Container, Key) /= No_Element;
652 procedure Delete (Container : in out Set; Key : Key_Type) is
653 X : Node_Access := Key_Keys.Find (Container.Tree, Key);
657 raise Constraint_Error with "attempt to delete key not in set";
660 Delete_Node_Sans_Free (Container.Tree, X);
668 function Element (Container : Set; Key : Key_Type) return Element_Type is
669 Node : constant Node_Access :=
670 Key_Keys.Find (Container.Tree, Key);
674 raise Constraint_Error with "key not in set";
680 ---------------------
681 -- Equivalent_Keys --
682 ---------------------
684 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
699 procedure Exclude (Container : in out Set; Key : Key_Type) is
700 X : Node_Access := Key_Keys.Find (Container.Tree, Key);
704 Delete_Node_Sans_Free (Container.Tree, X);
713 function Find (Container : Set; Key : Key_Type) return Cursor is
714 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
721 return Cursor'(Container'Unrestricted_Access, Node);
728 function Floor (Container : Set; Key : Key_Type) return Cursor is
729 Node : constant Node_Access := Key_Keys.Floor (Container.Tree, Key);
736 return Cursor'(Container'Unrestricted_Access, Node);
739 -------------------------
740 -- Is_Greater_Key_Node --
741 -------------------------
743 function Is_Greater_Key_Node
745 Right : Node_Access) return Boolean
748 return Key (Right.Element) < Left;
749 end Is_Greater_Key_Node;
751 ----------------------
752 -- Is_Less_Key_Node --
753 ----------------------
755 function Is_Less_Key_Node
757 Right : Node_Access) return Boolean
760 return Left < Key (Right.Element);
761 end Is_Less_Key_Node;
767 function Key (Position : Cursor) return Key_Type is
769 if Position.Node = null then
770 raise Constraint_Error with
771 "Position cursor equals No_Element";
774 pragma Assert (Vet (Position.Container.Tree, Position.Node),
775 "bad cursor in Key");
777 return Key (Position.Node.Element);
785 (Container : in out Set;
787 New_Item : Element_Type)
789 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
793 raise Constraint_Error with
794 "attempt to replace key not in set";
797 Replace_Element (Container.Tree, Node, New_Item);
800 -----------------------------------
801 -- Update_Element_Preserving_Key --
802 -----------------------------------
804 procedure Update_Element_Preserving_Key
805 (Container : in out Set;
807 Process : not null access procedure (Element : in out Element_Type))
809 Tree : Tree_Type renames Container.Tree;
812 if Position.Node = null then
813 raise Constraint_Error with
814 "Position cursor equals No_Element";
817 if Position.Container /= Container'Unrestricted_Access then
818 raise Program_Error with
819 "Position cursor designates wrong set";
822 pragma Assert (Vet (Container.Tree, Position.Node),
823 "bad cursor in Update_Element_Preserving_Key");
826 E : Element_Type renames Position.Node.Element;
827 K : constant Key_Type := Key (E);
829 B : Natural renames Tree.Busy;
830 L : Natural renames Tree.Lock;
848 if Equivalent_Keys (K, Key (E)) then
854 X : Node_Access := Position.Node;
856 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
860 raise Program_Error with "key was modified";
861 end Update_Element_Preserving_Key;
863 function Reference_Preserving_Key
864 (Container : aliased in out Set;
865 Key : Key_Type) return Constant_Reference_Type
867 Position : constant Cursor := Find (Container, Key);
870 if Position.Container = null then
871 raise Constraint_Error with "Position cursor has no element";
874 return (Element => Position.Node.Element'Access);
875 end Reference_Preserving_Key;
877 function Reference_Preserving_Key
878 (Container : aliased in out Set;
879 Key : Key_Type) return Reference_Type
881 Position : constant Cursor := Find (Container, Key);
884 if Position.Container = null then
885 raise Constraint_Error with "Position cursor has no element";
888 return (Element => Position.Node.Element'Access);
889 end Reference_Preserving_Key;
892 (Stream : not null access Root_Stream_Type'Class;
893 Item : out Reference_Type)
896 raise Program_Error with "attempt to stream reference";
900 (Stream : not null access Root_Stream_Type'Class;
901 Item : Reference_Type)
904 raise Program_Error with "attempt to stream reference";
913 function Has_Element (Position : Cursor) return Boolean is
915 return Position /= No_Element;
922 procedure Include (Container : in out Set; New_Item : Element_Type) is
927 Insert (Container, New_Item, Position, Inserted);
930 if Container.Tree.Lock > 0 then
931 raise Program_Error with
932 "attempt to tamper with elements (set is locked)";
935 Position.Node.Element := New_Item;
944 (Container : in out Set;
945 New_Item : Element_Type;
946 Position : out Cursor;
947 Inserted : out Boolean)
956 Position.Container := Container'Unrestricted_Access;
960 (Container : in out Set;
961 New_Item : Element_Type)
964 pragma Unreferenced (Position);
969 Insert (Container, New_Item, Position, Inserted);
972 raise Constraint_Error with
973 "attempt to insert element already in set";
977 ----------------------
978 -- Insert_Sans_Hint --
979 ----------------------
981 procedure Insert_Sans_Hint
982 (Tree : in out Tree_Type;
983 New_Item : Element_Type;
984 Node : out Node_Access;
985 Inserted : out Boolean)
987 function New_Node return Node_Access;
988 pragma Inline (New_Node);
990 procedure Insert_Post is
991 new Element_Keys.Generic_Insert_Post (New_Node);
993 procedure Conditional_Insert_Sans_Hint is
994 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1000 function New_Node return Node_Access is
1002 return new Node_Type'(Parent => null,
1005 Color => Red_Black_Trees.Red,
1006 Element => New_Item);
1009 -- Start of processing for Insert_Sans_Hint
1012 Conditional_Insert_Sans_Hint
1017 end Insert_Sans_Hint;
1019 ----------------------
1020 -- Insert_With_Hint --
1021 ----------------------
1023 procedure Insert_With_Hint
1024 (Dst_Tree : in out Tree_Type;
1025 Dst_Hint : Node_Access;
1026 Src_Node : Node_Access;
1027 Dst_Node : out Node_Access)
1030 pragma Unreferenced (Success);
1032 function New_Node return Node_Access;
1033 pragma Inline (New_Node);
1035 procedure Insert_Post is
1036 new Element_Keys.Generic_Insert_Post (New_Node);
1038 procedure Insert_Sans_Hint is
1039 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1041 procedure Local_Insert_With_Hint is
1042 new Element_Keys.Generic_Conditional_Insert_With_Hint
1050 function New_Node return Node_Access is
1051 Node : constant Node_Access :=
1052 new Node_Type'(Parent => null,
1056 Element => Src_Node.Element);
1061 -- Start of processing for Insert_With_Hint
1064 Local_Insert_With_Hint
1070 end Insert_With_Hint;
1076 procedure Intersection (Target : in out Set; Source : Set) is
1078 Set_Ops.Intersection (Target.Tree, Source.Tree);
1081 function Intersection (Left, Right : Set) return Set is
1082 Tree : constant Tree_Type :=
1083 Set_Ops.Intersection (Left.Tree, Right.Tree);
1085 return Set'(Controlled with Tree);
1092 function Is_Empty (Container : Set) return Boolean is
1094 return Container.Tree.Length = 0;
1097 ------------------------
1098 -- Is_Equal_Node_Node --
1099 ------------------------
1101 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
1103 return L.Element = R.Element;
1104 end Is_Equal_Node_Node;
1106 -----------------------------
1107 -- Is_Greater_Element_Node --
1108 -----------------------------
1110 function Is_Greater_Element_Node
1111 (Left : Element_Type;
1112 Right : Node_Access) return Boolean
1115 -- Compute e > node same as node < e
1117 return Right.Element < Left;
1118 end Is_Greater_Element_Node;
1120 --------------------------
1121 -- Is_Less_Element_Node --
1122 --------------------------
1124 function Is_Less_Element_Node
1125 (Left : Element_Type;
1126 Right : Node_Access) return Boolean
1129 return Left < Right.Element;
1130 end Is_Less_Element_Node;
1132 -----------------------
1133 -- Is_Less_Node_Node --
1134 -----------------------
1136 function Is_Less_Node_Node (L, R : Node_Access) return Boolean is
1138 return L.Element < R.Element;
1139 end Is_Less_Node_Node;
1145 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1147 return Set_Ops.Is_Subset (Subset => Subset.Tree, Of_Set => Of_Set.Tree);
1156 Process : not null access procedure (Position : Cursor))
1158 procedure Process_Node (Node : Node_Access);
1159 pragma Inline (Process_Node);
1161 procedure Local_Iterate is
1162 new Tree_Operations.Generic_Iteration (Process_Node);
1168 procedure Process_Node (Node : Node_Access) is
1170 Process (Cursor'(Container'Unrestricted_Access, Node));
1173 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1174 B : Natural renames T.Busy;
1176 -- Start of processing for Iterate
1192 function Iterate (Container : Set)
1193 return Ordered_Set_Iterator_Interfaces.Reversible_Iterator'class
1196 if Container.Length = 0 then
1197 return Iterator'(null, null);
1199 return Iterator'(Container'Unchecked_Access, Container.Tree.First);
1203 function Iterate (Container : Set; Start : Cursor)
1204 return Ordered_Set_Iterator_Interfaces.Reversible_Iterator'class
1206 It : constant Iterator := (Container'Unchecked_Access, Start.Node);
1215 function Last (Container : Set) return Cursor is
1217 if Container.Tree.Last = null then
1220 return Cursor'(Container'Unrestricted_Access, Container.Tree.Last);
1224 function Last (Object : Iterator) return Cursor is
1226 if Object.Container = null then
1230 Object.Container.all'Unrestricted_Access,
1231 Object.Container.Tree.Last);
1239 function Last_Element (Container : Set) return Element_Type is
1241 if Container.Tree.Last = null then
1242 raise Constraint_Error with "set is empty";
1244 return Container.Tree.Last.Element;
1252 function Left (Node : Node_Access) return Node_Access is
1261 function Length (Container : Set) return Count_Type is
1263 return Container.Tree.Length;
1271 new Tree_Operations.Generic_Move (Clear);
1273 procedure Move (Target : in out Set; Source : in out Set) is
1275 Move (Target => Target.Tree, Source => Source.Tree);
1282 function Next (Position : Cursor) return Cursor is
1284 if Position = No_Element then
1288 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1289 "bad cursor in Next");
1292 Node : constant Node_Access :=
1293 Tree_Operations.Next (Position.Node);
1300 return Cursor'(Position.Container, Node);
1304 procedure Next (Position : in out Cursor) is
1306 Position := Next (Position);
1309 function Next (Object : Iterator; Position : Cursor) return Cursor is
1310 pragma Unreferenced (Object);
1312 return Next (Position);
1319 function Overlap (Left, Right : Set) return Boolean is
1321 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1328 function Parent (Node : Node_Access) return Node_Access is
1337 function Previous (Position : Cursor) return Cursor is
1339 if Position = No_Element then
1343 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1344 "bad cursor in Previous");
1347 Node : constant Node_Access :=
1348 Tree_Operations.Previous (Position.Node);
1353 return Cursor'(Position.Container, Node);
1358 procedure Previous (Position : in out Cursor) is
1360 Position := Previous (Position);
1363 function Previous (Object : Iterator; Position : Cursor) return Cursor is
1364 pragma Unreferenced (Object);
1366 return Previous (Position);
1373 procedure Query_Element
1375 Process : not null access procedure (Element : Element_Type))
1378 if Position.Node = null then
1379 raise Constraint_Error with "Position cursor equals No_Element";
1382 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1383 "bad cursor in Query_Element");
1386 T : Tree_Type renames Position.Container.Tree;
1388 B : Natural renames T.Busy;
1389 L : Natural renames T.Lock;
1396 Process (Position.Node.Element);
1414 (Stream : not null access Root_Stream_Type'Class;
1415 Container : out Set)
1418 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1419 pragma Inline (Read_Node);
1422 new Tree_Operations.Generic_Read (Clear, Read_Node);
1429 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1431 Node : Node_Access := new Node_Type;
1434 Element_Type'Read (Stream, Node.Element);
1443 -- Start of processing for Read
1446 Read (Stream, Container.Tree);
1450 (Stream : not null access Root_Stream_Type'Class;
1454 raise Program_Error with "attempt to stream set cursor";
1458 (Stream : not null access Root_Stream_Type'Class;
1459 Item : out Constant_Reference_Type)
1462 raise Program_Error with "attempt to stream reference";
1469 function Constant_Reference (Container : Set; Position : Cursor)
1470 return Constant_Reference_Type
1472 pragma Unreferenced (Container);
1474 if Position.Container = null then
1475 raise Constraint_Error with "Position cursor has no element";
1478 return (Element => Position.Node.Element'Access);
1479 end Constant_Reference;
1485 procedure Replace (Container : in out Set; New_Item : Element_Type) is
1486 Node : constant Node_Access :=
1487 Element_Keys.Find (Container.Tree, New_Item);
1491 raise Constraint_Error with
1492 "attempt to replace element not in set";
1495 if Container.Tree.Lock > 0 then
1496 raise Program_Error with
1497 "attempt to tamper with elements (set is locked)";
1500 Node.Element := New_Item;
1503 ---------------------
1504 -- Replace_Element --
1505 ---------------------
1507 procedure Replace_Element
1508 (Tree : in out Tree_Type;
1510 Item : Element_Type)
1512 pragma Assert (Node /= null);
1514 function New_Node return Node_Access;
1515 pragma Inline (New_Node);
1517 procedure Local_Insert_Post is
1518 new Element_Keys.Generic_Insert_Post (New_Node);
1520 procedure Local_Insert_Sans_Hint is
1521 new Element_Keys.Generic_Conditional_Insert (Local_Insert_Post);
1523 procedure Local_Insert_With_Hint is
1524 new Element_Keys.Generic_Conditional_Insert_With_Hint
1526 Local_Insert_Sans_Hint);
1532 function New_Node return Node_Access is
1534 Node.Element := Item;
1536 Node.Parent := null;
1544 Result : Node_Access;
1547 -- Start of processing for Replace_Element
1550 if Item < Node.Element
1551 or else Node.Element < Item
1556 if Tree.Lock > 0 then
1557 raise Program_Error with
1558 "attempt to tamper with elements (set is locked)";
1561 Node.Element := Item;
1565 Hint := Element_Keys.Ceiling (Tree, Item);
1570 elsif Item < Hint.Element then
1572 if Tree.Lock > 0 then
1573 raise Program_Error with
1574 "attempt to tamper with elements (set is locked)";
1577 Node.Element := Item;
1582 pragma Assert (not (Hint.Element < Item));
1583 raise Program_Error with "attempt to replace existing element";
1586 Tree_Operations.Delete_Node_Sans_Free (Tree, Node); -- Checks busy-bit
1588 Local_Insert_With_Hint
1593 Inserted => Inserted);
1595 pragma Assert (Inserted);
1596 pragma Assert (Result = Node);
1597 end Replace_Element;
1599 procedure Replace_Element
1600 (Container : in out Set;
1602 New_Item : Element_Type)
1605 if Position.Node = null then
1606 raise Constraint_Error with
1607 "Position cursor equals No_Element";
1610 if Position.Container /= Container'Unrestricted_Access then
1611 raise Program_Error with
1612 "Position cursor designates wrong set";
1615 pragma Assert (Vet (Container.Tree, Position.Node),
1616 "bad cursor in Replace_Element");
1618 Replace_Element (Container.Tree, Position.Node, New_Item);
1619 end Replace_Element;
1621 ---------------------
1622 -- Reverse_Iterate --
1623 ---------------------
1625 procedure Reverse_Iterate
1627 Process : not null access procedure (Position : Cursor))
1629 procedure Process_Node (Node : Node_Access);
1630 pragma Inline (Process_Node);
1632 procedure Local_Reverse_Iterate is
1633 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1639 procedure Process_Node (Node : Node_Access) is
1641 Process (Cursor'(Container'Unrestricted_Access, Node));
1644 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1645 B : Natural renames T.Busy;
1647 -- Start of processing for Reverse_Iterate
1653 Local_Reverse_Iterate (T);
1661 end Reverse_Iterate;
1667 function Right (Node : Node_Access) return Node_Access is
1676 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1678 Node.Color := Color;
1685 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1694 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1696 Node.Parent := Parent;
1703 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1705 Node.Right := Right;
1708 --------------------------
1709 -- Symmetric_Difference --
1710 --------------------------
1712 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1714 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
1715 end Symmetric_Difference;
1717 function Symmetric_Difference (Left, Right : Set) return Set is
1718 Tree : constant Tree_Type :=
1719 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
1721 return Set'(Controlled with Tree);
1722 end Symmetric_Difference;
1728 function To_Set (New_Item : Element_Type) return Set is
1732 pragma Unreferenced (Node, Inserted);
1734 Insert_Sans_Hint (Tree, New_Item, Node, Inserted);
1735 return Set'(Controlled with Tree);
1742 procedure Union (Target : in out Set; Source : Set) is
1744 Set_Ops.Union (Target.Tree, Source.Tree);
1747 function Union (Left, Right : Set) return Set is
1748 Tree : constant Tree_Type :=
1749 Set_Ops.Union (Left.Tree, Right.Tree);
1751 return Set'(Controlled with Tree);
1759 (Stream : not null access Root_Stream_Type'Class;
1762 procedure Write_Node
1763 (Stream : not null access Root_Stream_Type'Class;
1764 Node : Node_Access);
1765 pragma Inline (Write_Node);
1768 new Tree_Operations.Generic_Write (Write_Node);
1774 procedure Write_Node
1775 (Stream : not null access Root_Stream_Type'Class;
1779 Element_Type'Write (Stream, Node.Element);
1782 -- Start of processing for Write
1785 Write (Stream, Container.Tree);
1789 (Stream : not null access Root_Stream_Type'Class;
1793 raise Program_Error with "attempt to stream set cursor";
1797 (Stream : not null access Root_Stream_Type'Class;
1798 Item : Constant_Reference_Type)
1801 raise Program_Error with "attempt to stream reference";
1804 end Ada.Containers.Ordered_Sets;