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;
869 function Has_Element (Position : Cursor) return Boolean is
871 return Position /= No_Element;
878 procedure Include (Container : in out Set; New_Item : Element_Type) is
883 Insert (Container, New_Item, Position, Inserted);
886 if Container.Tree.Lock > 0 then
887 raise Program_Error with
888 "attempt to tamper with elements (set is locked)";
891 Position.Node.Element := New_Item;
900 (Container : in out Set;
901 New_Item : Element_Type;
902 Position : out Cursor;
903 Inserted : out Boolean)
912 Position.Container := Container'Unrestricted_Access;
916 (Container : in out Set;
917 New_Item : Element_Type)
920 pragma Unreferenced (Position);
925 Insert (Container, New_Item, Position, Inserted);
928 raise Constraint_Error with
929 "attempt to insert element already in set";
933 ----------------------
934 -- Insert_Sans_Hint --
935 ----------------------
937 procedure Insert_Sans_Hint
938 (Tree : in out Tree_Type;
939 New_Item : Element_Type;
940 Node : out Node_Access;
941 Inserted : out Boolean)
943 function New_Node return Node_Access;
944 pragma Inline (New_Node);
946 procedure Insert_Post is
947 new Element_Keys.Generic_Insert_Post (New_Node);
949 procedure Conditional_Insert_Sans_Hint is
950 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
956 function New_Node return Node_Access is
958 return new Node_Type'(Parent => null,
961 Color => Red_Black_Trees.Red,
962 Element => New_Item);
965 -- Start of processing for Insert_Sans_Hint
968 Conditional_Insert_Sans_Hint
973 end Insert_Sans_Hint;
975 ----------------------
976 -- Insert_With_Hint --
977 ----------------------
979 procedure Insert_With_Hint
980 (Dst_Tree : in out Tree_Type;
981 Dst_Hint : Node_Access;
982 Src_Node : Node_Access;
983 Dst_Node : out Node_Access)
986 pragma Unreferenced (Success);
988 function New_Node return Node_Access;
989 pragma Inline (New_Node);
991 procedure Insert_Post is
992 new Element_Keys.Generic_Insert_Post (New_Node);
994 procedure Insert_Sans_Hint is
995 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
997 procedure Local_Insert_With_Hint is
998 new Element_Keys.Generic_Conditional_Insert_With_Hint
1006 function New_Node return Node_Access is
1007 Node : constant Node_Access :=
1008 new Node_Type'(Parent => null,
1012 Element => Src_Node.Element);
1017 -- Start of processing for Insert_With_Hint
1020 Local_Insert_With_Hint
1026 end Insert_With_Hint;
1032 procedure Intersection (Target : in out Set; Source : Set) is
1034 Set_Ops.Intersection (Target.Tree, Source.Tree);
1037 function Intersection (Left, Right : Set) return Set is
1038 Tree : constant Tree_Type :=
1039 Set_Ops.Intersection (Left.Tree, Right.Tree);
1041 return Set'(Controlled with Tree);
1048 function Is_Empty (Container : Set) return Boolean is
1050 return Container.Tree.Length = 0;
1053 ------------------------
1054 -- Is_Equal_Node_Node --
1055 ------------------------
1057 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
1059 return L.Element = R.Element;
1060 end Is_Equal_Node_Node;
1062 -----------------------------
1063 -- Is_Greater_Element_Node --
1064 -----------------------------
1066 function Is_Greater_Element_Node
1067 (Left : Element_Type;
1068 Right : Node_Access) return Boolean
1071 -- Compute e > node same as node < e
1073 return Right.Element < Left;
1074 end Is_Greater_Element_Node;
1076 --------------------------
1077 -- Is_Less_Element_Node --
1078 --------------------------
1080 function Is_Less_Element_Node
1081 (Left : Element_Type;
1082 Right : Node_Access) return Boolean
1085 return Left < Right.Element;
1086 end Is_Less_Element_Node;
1088 -----------------------
1089 -- Is_Less_Node_Node --
1090 -----------------------
1092 function Is_Less_Node_Node (L, R : Node_Access) return Boolean is
1094 return L.Element < R.Element;
1095 end Is_Less_Node_Node;
1101 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1103 return Set_Ops.Is_Subset (Subset => Subset.Tree, Of_Set => Of_Set.Tree);
1112 Process : not null access procedure (Position : Cursor))
1114 procedure Process_Node (Node : Node_Access);
1115 pragma Inline (Process_Node);
1117 procedure Local_Iterate is
1118 new Tree_Operations.Generic_Iteration (Process_Node);
1124 procedure Process_Node (Node : Node_Access) is
1126 Process (Cursor'(Container'Unrestricted_Access, Node));
1129 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1130 B : Natural renames T.Busy;
1132 -- Start of processing for Iterate
1148 function Iterate (Container : Set)
1149 return Ordered_Set_Iterator_Interfaces.Reversible_Iterator'class
1152 if Container.Length = 0 then
1153 return Iterator'(null, null);
1155 return Iterator'(Container'Unchecked_Access, Container.Tree.First);
1159 function Iterate (Container : Set; Start : Cursor)
1160 return Ordered_Set_Iterator_Interfaces.Reversible_Iterator'class
1162 It : constant Iterator := (Container'Unchecked_Access, Start.Node);
1171 function Last (Container : Set) return Cursor is
1173 if Container.Tree.Last = null then
1176 return Cursor'(Container'Unrestricted_Access, Container.Tree.Last);
1180 function Last (Object : Iterator) return Cursor is
1182 if Object.Container = null then
1186 Object.Container.all'Unrestricted_Access,
1187 Object.Container.Tree.Last);
1195 function Last_Element (Container : Set) return Element_Type is
1197 if Container.Tree.Last = null then
1198 raise Constraint_Error with "set is empty";
1200 return Container.Tree.Last.Element;
1208 function Left (Node : Node_Access) return Node_Access is
1217 function Length (Container : Set) return Count_Type is
1219 return Container.Tree.Length;
1227 new Tree_Operations.Generic_Move (Clear);
1229 procedure Move (Target : in out Set; Source : in out Set) is
1231 Move (Target => Target.Tree, Source => Source.Tree);
1238 function Next (Position : Cursor) return Cursor is
1240 if Position = No_Element then
1244 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1245 "bad cursor in Next");
1248 Node : constant Node_Access :=
1249 Tree_Operations.Next (Position.Node);
1256 return Cursor'(Position.Container, Node);
1260 procedure Next (Position : in out Cursor) is
1262 Position := Next (Position);
1265 function Next (Object : Iterator; Position : Cursor) return Cursor is
1266 pragma Unreferenced (Object);
1268 return Next (Position);
1275 function Overlap (Left, Right : Set) return Boolean is
1277 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1284 function Parent (Node : Node_Access) return Node_Access is
1293 function Previous (Position : Cursor) return Cursor is
1295 if Position = No_Element then
1299 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1300 "bad cursor in Previous");
1303 Node : constant Node_Access :=
1304 Tree_Operations.Previous (Position.Node);
1309 return Cursor'(Position.Container, Node);
1314 procedure Previous (Position : in out Cursor) is
1316 Position := Previous (Position);
1319 function Previous (Object : Iterator; Position : Cursor) return Cursor is
1320 pragma Unreferenced (Object);
1322 return Previous (Position);
1329 procedure Query_Element
1331 Process : not null access procedure (Element : Element_Type))
1334 if Position.Node = null then
1335 raise Constraint_Error with "Position cursor equals No_Element";
1338 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1339 "bad cursor in Query_Element");
1342 T : Tree_Type renames Position.Container.Tree;
1344 B : Natural renames T.Busy;
1345 L : Natural renames T.Lock;
1352 Process (Position.Node.Element);
1370 (Stream : not null access Root_Stream_Type'Class;
1371 Container : out Set)
1374 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1375 pragma Inline (Read_Node);
1378 new Tree_Operations.Generic_Read (Clear, Read_Node);
1385 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1387 Node : Node_Access := new Node_Type;
1390 Element_Type'Read (Stream, Node.Element);
1399 -- Start of processing for Read
1402 Read (Stream, Container.Tree);
1406 (Stream : not null access Root_Stream_Type'Class;
1410 raise Program_Error with "attempt to stream set cursor";
1414 (Stream : not null access Root_Stream_Type'Class;
1415 Item : out Reference_Type)
1418 raise Program_Error with "attempt to stream reference";
1422 (Stream : not null access Root_Stream_Type'Class;
1423 Item : out Constant_Reference_Type)
1426 raise Program_Error with "attempt to stream reference";
1433 function Constant_Reference (Container : Set; Position : Cursor)
1434 return Constant_Reference_Type
1436 pragma Unreferenced (Container);
1438 if Position.Container = null then
1439 raise Constraint_Error with "Position cursor has no element";
1442 return (Element => Position.Node.Element'Access);
1443 end Constant_Reference;
1445 function Reference (Container : Set; Position : Cursor)
1446 return Reference_Type
1448 pragma Unreferenced (Container);
1450 if Position.Container = null then
1451 raise Constraint_Error with "Position cursor has no element";
1454 return (Element => Position.Node.Element'Access);
1461 procedure Replace (Container : in out Set; New_Item : Element_Type) is
1462 Node : constant Node_Access :=
1463 Element_Keys.Find (Container.Tree, New_Item);
1467 raise Constraint_Error with
1468 "attempt to replace element not in set";
1471 if Container.Tree.Lock > 0 then
1472 raise Program_Error with
1473 "attempt to tamper with elements (set is locked)";
1476 Node.Element := New_Item;
1479 ---------------------
1480 -- Replace_Element --
1481 ---------------------
1483 procedure Replace_Element
1484 (Tree : in out Tree_Type;
1486 Item : Element_Type)
1488 pragma Assert (Node /= null);
1490 function New_Node return Node_Access;
1491 pragma Inline (New_Node);
1493 procedure Local_Insert_Post is
1494 new Element_Keys.Generic_Insert_Post (New_Node);
1496 procedure Local_Insert_Sans_Hint is
1497 new Element_Keys.Generic_Conditional_Insert (Local_Insert_Post);
1499 procedure Local_Insert_With_Hint is
1500 new Element_Keys.Generic_Conditional_Insert_With_Hint
1502 Local_Insert_Sans_Hint);
1508 function New_Node return Node_Access is
1510 Node.Element := Item;
1512 Node.Parent := null;
1520 Result : Node_Access;
1523 -- Start of processing for Replace_Element
1526 if Item < Node.Element
1527 or else Node.Element < Item
1532 if Tree.Lock > 0 then
1533 raise Program_Error with
1534 "attempt to tamper with elements (set is locked)";
1537 Node.Element := Item;
1541 Hint := Element_Keys.Ceiling (Tree, Item);
1546 elsif Item < Hint.Element then
1548 if Tree.Lock > 0 then
1549 raise Program_Error with
1550 "attempt to tamper with elements (set is locked)";
1553 Node.Element := Item;
1558 pragma Assert (not (Hint.Element < Item));
1559 raise Program_Error with "attempt to replace existing element";
1562 Tree_Operations.Delete_Node_Sans_Free (Tree, Node); -- Checks busy-bit
1564 Local_Insert_With_Hint
1569 Inserted => Inserted);
1571 pragma Assert (Inserted);
1572 pragma Assert (Result = Node);
1573 end Replace_Element;
1575 procedure Replace_Element
1576 (Container : in out Set;
1578 New_Item : Element_Type)
1581 if Position.Node = null then
1582 raise Constraint_Error with
1583 "Position cursor equals No_Element";
1586 if Position.Container /= Container'Unrestricted_Access then
1587 raise Program_Error with
1588 "Position cursor designates wrong set";
1591 pragma Assert (Vet (Container.Tree, Position.Node),
1592 "bad cursor in Replace_Element");
1594 Replace_Element (Container.Tree, Position.Node, New_Item);
1595 end Replace_Element;
1597 ---------------------
1598 -- Reverse_Iterate --
1599 ---------------------
1601 procedure Reverse_Iterate
1603 Process : not null access procedure (Position : Cursor))
1605 procedure Process_Node (Node : Node_Access);
1606 pragma Inline (Process_Node);
1608 procedure Local_Reverse_Iterate is
1609 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1615 procedure Process_Node (Node : Node_Access) is
1617 Process (Cursor'(Container'Unrestricted_Access, Node));
1620 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1621 B : Natural renames T.Busy;
1623 -- Start of processing for Reverse_Iterate
1629 Local_Reverse_Iterate (T);
1637 end Reverse_Iterate;
1643 function Right (Node : Node_Access) return Node_Access is
1652 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1654 Node.Color := Color;
1661 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1670 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1672 Node.Parent := Parent;
1679 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1681 Node.Right := Right;
1684 --------------------------
1685 -- Symmetric_Difference --
1686 --------------------------
1688 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1690 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
1691 end Symmetric_Difference;
1693 function Symmetric_Difference (Left, Right : Set) return Set is
1694 Tree : constant Tree_Type :=
1695 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
1697 return Set'(Controlled with Tree);
1698 end Symmetric_Difference;
1704 function To_Set (New_Item : Element_Type) return Set is
1708 pragma Unreferenced (Node, Inserted);
1710 Insert_Sans_Hint (Tree, New_Item, Node, Inserted);
1711 return Set'(Controlled with Tree);
1718 procedure Union (Target : in out Set; Source : Set) is
1720 Set_Ops.Union (Target.Tree, Source.Tree);
1723 function Union (Left, Right : Set) return Set is
1724 Tree : constant Tree_Type :=
1725 Set_Ops.Union (Left.Tree, Right.Tree);
1727 return Set'(Controlled with Tree);
1735 (Stream : not null access Root_Stream_Type'Class;
1738 procedure Write_Node
1739 (Stream : not null access Root_Stream_Type'Class;
1740 Node : Node_Access);
1741 pragma Inline (Write_Node);
1744 new Tree_Operations.Generic_Write (Write_Node);
1750 procedure Write_Node
1751 (Stream : not null access Root_Stream_Type'Class;
1755 Element_Type'Write (Stream, Node.Element);
1758 -- Start of processing for Write
1761 Write (Stream, Container.Tree);
1765 (Stream : not null access Root_Stream_Type'Class;
1769 raise Program_Error with "attempt to stream set cursor";
1773 (Stream : not null access Root_Stream_Type'Class;
1774 Item : Reference_Type)
1777 raise Program_Error with "attempt to stream reference";
1781 (Stream : not null access Root_Stream_Type'Class;
1782 Item : Constant_Reference_Type)
1785 raise Program_Error with "attempt to stream reference";
1788 end Ada.Containers.Ordered_Sets;