1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.INDEFINITE_ORDERED_SETS --
9 -- Copyright (C) 2004-2007, 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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- This unit was originally developed by Matthew J Heaney. --
30 ------------------------------------------------------------------------------
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 Ada.Unchecked_Deallocation;
43 package body Ada.Containers.Indefinite_Ordered_Sets is
45 -----------------------
46 -- Local Subprograms --
47 -----------------------
49 function Color (Node : Node_Access) return Color_Type;
50 pragma Inline (Color);
52 function Copy_Node (Source : Node_Access) return Node_Access;
53 pragma Inline (Copy_Node);
55 procedure Free (X : in out Node_Access);
57 procedure Insert_Sans_Hint
58 (Tree : in out Tree_Type;
59 New_Item : Element_Type;
60 Node : out Node_Access;
61 Inserted : out Boolean);
63 procedure Insert_With_Hint
64 (Dst_Tree : in out Tree_Type;
65 Dst_Hint : Node_Access;
66 Src_Node : Node_Access;
67 Dst_Node : out Node_Access);
69 function Is_Greater_Element_Node
71 Right : Node_Access) return Boolean;
72 pragma Inline (Is_Greater_Element_Node);
74 function Is_Less_Element_Node
76 Right : Node_Access) return Boolean;
77 pragma Inline (Is_Less_Element_Node);
79 function Is_Less_Node_Node (L, R : Node_Access) return Boolean;
80 pragma Inline (Is_Less_Node_Node);
82 function Left (Node : Node_Access) return Node_Access;
85 function Parent (Node : Node_Access) return Node_Access;
86 pragma Inline (Parent);
88 procedure Replace_Element
89 (Tree : in out Tree_Type;
93 function Right (Node : Node_Access) return Node_Access;
94 pragma Inline (Right);
96 procedure Set_Color (Node : Node_Access; Color : Color_Type);
97 pragma Inline (Set_Color);
99 procedure Set_Left (Node : Node_Access; Left : Node_Access);
100 pragma Inline (Set_Left);
102 procedure Set_Parent (Node : Node_Access; Parent : Node_Access);
103 pragma Inline (Set_Parent);
105 procedure Set_Right (Node : Node_Access; Right : Node_Access);
106 pragma Inline (Set_Right);
108 --------------------------
109 -- Local Instantiations --
110 --------------------------
112 procedure Free_Element is
113 new Ada.Unchecked_Deallocation (Element_Type, Element_Access);
115 package Tree_Operations is
116 new Red_Black_Trees.Generic_Operations (Tree_Types);
118 procedure Delete_Tree is
119 new Tree_Operations.Generic_Delete_Tree (Free);
121 function Copy_Tree is
122 new Tree_Operations.Generic_Copy_Tree (Copy_Node, Delete_Tree);
126 package Element_Keys is
127 new Red_Black_Trees.Generic_Keys
128 (Tree_Operations => Tree_Operations,
129 Key_Type => Element_Type,
130 Is_Less_Key_Node => Is_Less_Element_Node,
131 Is_Greater_Key_Node => Is_Greater_Element_Node);
134 new Generic_Set_Operations
135 (Tree_Operations => Tree_Operations,
136 Insert_With_Hint => Insert_With_Hint,
137 Copy_Tree => Copy_Tree,
138 Delete_Tree => Delete_Tree,
139 Is_Less => Is_Less_Node_Node,
146 function "<" (Left, Right : Cursor) return Boolean is
148 if Left.Node = null then
149 raise Constraint_Error with "Left cursor equals No_Element";
152 if Right.Node = null then
153 raise Constraint_Error with "Right cursor equals No_Element";
156 if Left.Node.Element = null then
157 raise Program_Error with "Left cursor is bad";
160 if Right.Node.Element = null then
161 raise Program_Error with "Right cursor is bad";
164 pragma Assert (Vet (Left.Container.Tree, Left.Node),
165 "bad Left cursor in ""<""");
167 pragma Assert (Vet (Right.Container.Tree, Right.Node),
168 "bad Right cursor in ""<""");
170 return Left.Node.Element.all < Right.Node.Element.all;
173 function "<" (Left : Cursor; Right : Element_Type) return Boolean is
175 if Left.Node = null then
176 raise Constraint_Error with "Left cursor equals No_Element";
179 if Left.Node.Element = null then
180 raise Program_Error with "Left cursor is bad";
183 pragma Assert (Vet (Left.Container.Tree, Left.Node),
184 "bad Left cursor in ""<""");
186 return Left.Node.Element.all < Right;
189 function "<" (Left : Element_Type; Right : Cursor) return Boolean is
191 if Right.Node = null then
192 raise Constraint_Error with "Right cursor equals No_Element";
195 if Right.Node.Element = null then
196 raise Program_Error with "Right cursor is bad";
199 pragma Assert (Vet (Right.Container.Tree, Right.Node),
200 "bad Right cursor in ""<""");
202 return Left < Right.Node.Element.all;
209 function "=" (Left, Right : Set) return Boolean is
211 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean;
212 pragma Inline (Is_Equal_Node_Node);
215 new Tree_Operations.Generic_Equal (Is_Equal_Node_Node);
217 ------------------------
218 -- Is_Equal_Node_Node --
219 ------------------------
221 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
223 return L.Element.all = R.Element.all;
224 end Is_Equal_Node_Node;
226 -- Start of processing for "="
229 return Is_Equal (Left.Tree, Right.Tree);
236 function ">" (Left, Right : Cursor) return Boolean is
238 if Left.Node = null then
239 raise Constraint_Error with "Left cursor equals No_Element";
242 if Right.Node = null then
243 raise Constraint_Error with "Right cursor equals No_Element";
246 if Left.Node.Element = null then
247 raise Program_Error with "Left cursor is bad";
250 if Right.Node.Element = null then
251 raise Program_Error with "Right cursor is bad";
254 pragma Assert (Vet (Left.Container.Tree, Left.Node),
255 "bad Left cursor in "">""");
257 pragma Assert (Vet (Right.Container.Tree, Right.Node),
258 "bad Right cursor in "">""");
260 -- L > R same as R < L
262 return Right.Node.Element.all < Left.Node.Element.all;
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 if Left.Node.Element = null then
272 raise Program_Error with "Left cursor is bad";
275 pragma Assert (Vet (Left.Container.Tree, Left.Node),
276 "bad Left cursor in "">""");
278 return Right < Left.Node.Element.all;
281 function ">" (Left : Element_Type; Right : Cursor) return Boolean is
283 if Right.Node = null then
284 raise Constraint_Error with "Right cursor equals No_Element";
287 if Right.Node.Element = null then
288 raise Program_Error with "Right cursor is bad";
291 pragma Assert (Vet (Right.Container.Tree, Right.Node),
292 "bad Right cursor in "">""");
294 return Right.Node.Element.all < Left;
302 new Tree_Operations.Generic_Adjust (Copy_Tree);
304 procedure Adjust (Container : in out Set) is
306 Adjust (Container.Tree);
313 function Ceiling (Container : Set; Item : Element_Type) return Cursor is
314 Node : constant Node_Access :=
315 Element_Keys.Ceiling (Container.Tree, Item);
322 return Cursor'(Container'Unrestricted_Access, Node);
330 new Tree_Operations.Generic_Clear (Delete_Tree);
332 procedure Clear (Container : in out Set) is
334 Clear (Container.Tree);
341 function Color (Node : Node_Access) return Color_Type is
350 function Contains (Container : Set; Item : Element_Type) return Boolean is
352 return Find (Container, Item) /= No_Element;
359 function Copy_Node (Source : Node_Access) return Node_Access is
360 Element : Element_Access := new Element_Type'(Source.Element.all);
363 return new Node_Type'(Parent => null,
366 Color => Source.Color,
370 Free_Element (Element);
378 procedure Delete (Container : in out Set; Position : in out Cursor) is
380 if Position.Node = null then
381 raise Constraint_Error with "Position cursor equals No_Element";
384 if Position.Node.Element = null then
385 raise Program_Error with "Position cursor is bad";
388 if Position.Container /= Container'Unrestricted_Access then
389 raise Program_Error with "Position cursor designates wrong set";
392 pragma Assert (Vet (Container.Tree, Position.Node),
393 "bad cursor in Delete");
395 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
396 Free (Position.Node);
397 Position.Container := null;
400 procedure Delete (Container : in out Set; Item : Element_Type) is
402 Element_Keys.Find (Container.Tree, Item);
406 raise Constraint_Error with "attempt to delete element not in set";
409 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
417 procedure Delete_First (Container : in out Set) is
418 Tree : Tree_Type renames Container.Tree;
419 X : Node_Access := Tree.First;
423 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
432 procedure Delete_Last (Container : in out Set) is
433 Tree : Tree_Type renames Container.Tree;
434 X : Node_Access := Tree.Last;
438 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
447 procedure Difference (Target : in out Set; Source : Set) is
449 Set_Ops.Difference (Target.Tree, Source.Tree);
452 function Difference (Left, Right : Set) return Set is
453 Tree : constant Tree_Type :=
454 Set_Ops.Difference (Left.Tree, Right.Tree);
456 return Set'(Controlled with Tree);
463 function Element (Position : Cursor) return Element_Type is
465 if Position.Node = null then
466 raise Constraint_Error with "Position cursor equals No_Element";
469 if Position.Node.Element = null then
470 raise Program_Error with "Position cursor is bad";
473 pragma Assert (Vet (Position.Container.Tree, Position.Node),
474 "bad cursor in Element");
476 return Position.Node.Element.all;
479 -------------------------
480 -- Equivalent_Elements --
481 -------------------------
483 function Equivalent_Elements (Left, Right : Element_Type) return Boolean is
492 end Equivalent_Elements;
494 ---------------------
495 -- Equivalent_Sets --
496 ---------------------
498 function Equivalent_Sets (Left, Right : Set) return Boolean is
500 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean;
501 pragma Inline (Is_Equivalent_Node_Node);
503 function Is_Equivalent is
504 new Tree_Operations.Generic_Equal (Is_Equivalent_Node_Node);
506 -----------------------------
507 -- Is_Equivalent_Node_Node --
508 -----------------------------
510 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean is
512 if L.Element.all < R.Element.all then
514 elsif R.Element.all < L.Element.all then
519 end Is_Equivalent_Node_Node;
521 -- Start of processing for Equivalent_Sets
524 return Is_Equivalent (Left.Tree, Right.Tree);
531 procedure Exclude (Container : in out Set; Item : Element_Type) is
533 Element_Keys.Find (Container.Tree, Item);
537 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
546 function Find (Container : Set; Item : Element_Type) return Cursor is
547 Node : constant Node_Access :=
548 Element_Keys.Find (Container.Tree, Item);
555 return Cursor'(Container'Unrestricted_Access, Node);
562 function First (Container : Set) return Cursor is
564 if Container.Tree.First = null then
568 return Cursor'(Container'Unrestricted_Access, Container.Tree.First);
575 function First_Element (Container : Set) return Element_Type is
577 if Container.Tree.First = null then
578 raise Constraint_Error with "set is empty";
581 return Container.Tree.First.Element.all;
588 function Floor (Container : Set; Item : Element_Type) return Cursor is
589 Node : constant Node_Access :=
590 Element_Keys.Floor (Container.Tree, Item);
597 return Cursor'(Container'Unrestricted_Access, Node);
604 procedure Free (X : in out Node_Access) is
605 procedure Deallocate is
606 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
618 Free_Element (X.Element);
633 package body Generic_Keys is
635 -----------------------
636 -- Local Subprograms --
637 -----------------------
639 function Is_Greater_Key_Node
641 Right : Node_Access) return Boolean;
642 pragma Inline (Is_Greater_Key_Node);
644 function Is_Less_Key_Node
646 Right : Node_Access) return Boolean;
647 pragma Inline (Is_Less_Key_Node);
649 --------------------------
650 -- Local Instantiations --
651 --------------------------
654 new Red_Black_Trees.Generic_Keys
655 (Tree_Operations => Tree_Operations,
656 Key_Type => Key_Type,
657 Is_Less_Key_Node => Is_Less_Key_Node,
658 Is_Greater_Key_Node => Is_Greater_Key_Node);
664 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
665 Node : constant Node_Access :=
666 Key_Keys.Ceiling (Container.Tree, Key);
673 return Cursor'(Container'Unrestricted_Access, Node);
680 function Contains (Container : Set; Key : Key_Type) return Boolean is
682 return Find (Container, Key) /= No_Element;
689 procedure Delete (Container : in out Set; Key : Key_Type) is
690 X : Node_Access := Key_Keys.Find (Container.Tree, Key);
694 raise Constraint_Error with "attempt to delete key not in set";
697 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
705 function Element (Container : Set; Key : Key_Type) return Element_Type is
706 Node : constant Node_Access :=
707 Key_Keys.Find (Container.Tree, Key);
711 raise Constraint_Error with "key not in set";
714 return Node.Element.all;
717 ---------------------
718 -- Equivalent_Keys --
719 ---------------------
721 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
736 procedure Exclude (Container : in out Set; Key : Key_Type) is
737 X : Node_Access := Key_Keys.Find (Container.Tree, Key);
741 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
750 function Find (Container : Set; Key : Key_Type) return Cursor is
751 Node : constant Node_Access :=
752 Key_Keys.Find (Container.Tree, Key);
759 return Cursor'(Container'Unrestricted_Access, Node);
766 function Floor (Container : Set; Key : Key_Type) return Cursor is
767 Node : constant Node_Access :=
768 Key_Keys.Floor (Container.Tree, Key);
775 return Cursor'(Container'Unrestricted_Access, Node);
778 -------------------------
779 -- Is_Greater_Key_Node --
780 -------------------------
782 function Is_Greater_Key_Node
784 Right : Node_Access) return Boolean is
786 return Key (Right.Element.all) < Left;
787 end Is_Greater_Key_Node;
789 ----------------------
790 -- Is_Less_Key_Node --
791 ----------------------
793 function Is_Less_Key_Node
795 Right : Node_Access) return Boolean is
797 return Left < Key (Right.Element.all);
798 end Is_Less_Key_Node;
804 function Key (Position : Cursor) return Key_Type is
806 if Position.Node = null then
807 raise Constraint_Error with
808 "Position cursor equals No_Element";
811 if Position.Node.Element = null then
812 raise Program_Error with
813 "Position cursor is bad";
816 pragma Assert (Vet (Position.Container.Tree, Position.Node),
817 "bad cursor in Key");
819 return Key (Position.Node.Element.all);
827 (Container : in out Set;
829 New_Item : Element_Type)
831 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
835 raise Constraint_Error with
836 "attempt to replace key not in set";
839 Replace_Element (Container.Tree, Node, New_Item);
842 -----------------------------------
843 -- Update_Element_Preserving_Key --
844 -----------------------------------
846 procedure Update_Element_Preserving_Key
847 (Container : in out Set;
849 Process : not null access
850 procedure (Element : in out Element_Type))
852 Tree : Tree_Type renames Container.Tree;
855 if Position.Node = null then
856 raise Constraint_Error with "Position cursor equals No_Element";
859 if Position.Node.Element = null then
860 raise Program_Error with "Position cursor is bad";
863 if Position.Container /= Container'Unrestricted_Access then
864 raise Program_Error with "Position cursor designates wrong set";
867 pragma Assert (Vet (Container.Tree, Position.Node),
868 "bad cursor in Update_Element_Preserving_Key");
871 E : Element_Type renames Position.Node.Element.all;
872 K : constant Key_Type := Key (E);
874 B : Natural renames Tree.Busy;
875 L : Natural renames Tree.Lock;
893 if Equivalent_Keys (K, Key (E)) then
899 X : Node_Access := Position.Node;
901 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
905 raise Program_Error with "key was modified";
906 end Update_Element_Preserving_Key;
914 function Has_Element (Position : Cursor) return Boolean is
916 return Position /= No_Element;
923 procedure Include (Container : in out Set; New_Item : Element_Type) is
930 Insert (Container, New_Item, Position, Inserted);
933 if Container.Tree.Lock > 0 then
934 raise Program_Error with
935 "attempt to tamper with cursors (set is locked)";
938 X := Position.Node.Element;
939 Position.Node.Element := new Element_Type'(New_Item);
949 (Container : in out Set;
950 New_Item : Element_Type;
951 Position : out Cursor;
952 Inserted : out Boolean)
961 Position.Container := Container'Unrestricted_Access;
964 procedure Insert (Container : in out Set; New_Item : Element_Type) is
966 pragma Unreferenced (Position);
971 Insert (Container, New_Item, Position, Inserted);
974 raise Constraint_Error with
975 "attempt to insert element already in set";
979 ----------------------
980 -- Insert_Sans_Hint --
981 ----------------------
983 procedure Insert_Sans_Hint
984 (Tree : in out Tree_Type;
985 New_Item : Element_Type;
986 Node : out Node_Access;
987 Inserted : out Boolean)
989 function New_Node return Node_Access;
990 pragma Inline (New_Node);
992 procedure Insert_Post is
993 new Element_Keys.Generic_Insert_Post (New_Node);
995 procedure Conditional_Insert_Sans_Hint is
996 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1002 function New_Node return Node_Access is
1003 Element : Element_Access := new Element_Type'(New_Item);
1006 return new Node_Type'(Parent => null,
1009 Color => Red_Black_Trees.Red,
1010 Element => Element);
1013 Free_Element (Element);
1017 -- Start of processing for Insert_Sans_Hint
1020 Conditional_Insert_Sans_Hint
1025 end Insert_Sans_Hint;
1027 ----------------------
1028 -- Insert_With_Hint --
1029 ----------------------
1031 procedure Insert_With_Hint
1032 (Dst_Tree : in out Tree_Type;
1033 Dst_Hint : Node_Access;
1034 Src_Node : Node_Access;
1035 Dst_Node : out Node_Access)
1038 pragma Unreferenced (Success);
1040 function New_Node return Node_Access;
1042 procedure Insert_Post is
1043 new Element_Keys.Generic_Insert_Post (New_Node);
1045 procedure Insert_Sans_Hint is
1046 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1048 procedure Insert_With_Hint is
1049 new Element_Keys.Generic_Conditional_Insert_With_Hint
1057 function New_Node return Node_Access is
1058 Element : Element_Access :=
1059 new Element_Type'(Src_Node.Element.all);
1064 Node := new Node_Type;
1067 Free_Element (Element);
1071 Node.Element := Element;
1075 -- Start of processing for Insert_With_Hint
1081 Src_Node.Element.all,
1084 end Insert_With_Hint;
1090 procedure Intersection (Target : in out Set; Source : Set) is
1092 Set_Ops.Intersection (Target.Tree, Source.Tree);
1095 function Intersection (Left, Right : Set) return Set is
1096 Tree : constant Tree_Type :=
1097 Set_Ops.Intersection (Left.Tree, Right.Tree);
1099 return Set'(Controlled with Tree);
1106 function Is_Empty (Container : Set) return Boolean is
1108 return Container.Tree.Length = 0;
1111 -----------------------------
1112 -- Is_Greater_Element_Node --
1113 -----------------------------
1115 function Is_Greater_Element_Node
1116 (Left : Element_Type;
1117 Right : Node_Access) return Boolean is
1119 -- e > node same as node < e
1121 return Right.Element.all < Left;
1122 end Is_Greater_Element_Node;
1124 --------------------------
1125 -- Is_Less_Element_Node --
1126 --------------------------
1128 function Is_Less_Element_Node
1129 (Left : Element_Type;
1130 Right : Node_Access) return Boolean is
1132 return Left < Right.Element.all;
1133 end Is_Less_Element_Node;
1135 -----------------------
1136 -- Is_Less_Node_Node --
1137 -----------------------
1139 function Is_Less_Node_Node (L, R : Node_Access) return Boolean is
1141 return L.Element.all < R.Element.all;
1142 end Is_Less_Node_Node;
1148 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1150 return Set_Ops.Is_Subset (Subset => Subset.Tree, Of_Set => Of_Set.Tree);
1159 Process : not null access procedure (Position : Cursor))
1161 procedure Process_Node (Node : Node_Access);
1162 pragma Inline (Process_Node);
1164 procedure Local_Iterate is
1165 new Tree_Operations.Generic_Iteration (Process_Node);
1171 procedure Process_Node (Node : Node_Access) is
1173 Process (Cursor'(Container'Unrestricted_Access, Node));
1176 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1177 B : Natural renames T.Busy;
1179 -- Start of processing for Iterate
1199 function Last (Container : Set) return Cursor is
1201 if Container.Tree.Last = null then
1205 return Cursor'(Container'Unrestricted_Access, Container.Tree.Last);
1212 function Last_Element (Container : Set) return Element_Type is
1214 if Container.Tree.Last = null then
1215 raise Constraint_Error with "set is empty";
1218 return Container.Tree.Last.Element.all;
1225 function Left (Node : Node_Access) return Node_Access is
1234 function Length (Container : Set) return Count_Type is
1236 return Container.Tree.Length;
1244 new Tree_Operations.Generic_Move (Clear);
1246 procedure Move (Target : in out Set; Source : in out Set) is
1248 Move (Target => Target.Tree, Source => Source.Tree);
1255 procedure Next (Position : in out Cursor) is
1257 Position := Next (Position);
1260 function Next (Position : Cursor) return Cursor is
1262 if Position = No_Element then
1266 if Position.Node.Element = null then
1267 raise Program_Error with "Position cursor is bad";
1270 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1271 "bad cursor in Next");
1274 Node : constant Node_Access :=
1275 Tree_Operations.Next (Position.Node);
1282 return Cursor'(Position.Container, Node);
1290 function Overlap (Left, Right : Set) return Boolean is
1292 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1299 function Parent (Node : Node_Access) return Node_Access is
1308 procedure Previous (Position : in out Cursor) is
1310 Position := Previous (Position);
1313 function Previous (Position : Cursor) return Cursor is
1315 if Position = No_Element then
1319 if Position.Node.Element = null then
1320 raise Program_Error with "Position cursor is bad";
1323 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1324 "bad cursor in Previous");
1327 Node : constant Node_Access :=
1328 Tree_Operations.Previous (Position.Node);
1335 return Cursor'(Position.Container, Node);
1343 procedure Query_Element
1345 Process : not null access procedure (Element : Element_Type))
1348 if Position.Node = null then
1349 raise Constraint_Error with "Position cursor equals No_Element";
1352 if Position.Node.Element = null then
1353 raise Program_Error with "Position cursor is bad";
1356 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1357 "bad cursor in Query_Element");
1360 T : Tree_Type renames Position.Container.Tree;
1362 B : Natural renames T.Busy;
1363 L : Natural renames T.Lock;
1370 Process (Position.Node.Element.all);
1388 (Stream : not null access Root_Stream_Type'Class;
1389 Container : out Set)
1392 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1393 pragma Inline (Read_Node);
1396 new Tree_Operations.Generic_Read (Clear, Read_Node);
1403 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1405 Node : Node_Access := new Node_Type;
1408 Node.Element := new Element_Type'(Element_Type'Input (Stream));
1413 Free (Node); -- Note that Free deallocates elem too
1417 -- Start of processing for Read
1420 Read (Stream, Container.Tree);
1424 (Stream : not null access Root_Stream_Type'Class;
1428 raise Program_Error with "attempt to stream set cursor";
1435 procedure Replace (Container : in out Set; New_Item : Element_Type) is
1436 Node : constant Node_Access :=
1437 Element_Keys.Find (Container.Tree, New_Item);
1440 pragma Warnings (Off, X);
1444 raise Constraint_Error with "attempt to replace element not in set";
1447 if Container.Tree.Lock > 0 then
1448 raise Program_Error with
1449 "attempt to tamper with cursors (set is locked)";
1453 Node.Element := new Element_Type'(New_Item);
1457 ---------------------
1458 -- Replace_Element --
1459 ---------------------
1461 procedure Replace_Element
1462 (Tree : in out Tree_Type;
1464 Item : Element_Type)
1466 pragma Assert (Node /= null);
1467 pragma Assert (Node.Element /= null);
1469 function New_Node return Node_Access;
1470 pragma Inline (New_Node);
1472 procedure Local_Insert_Post is
1473 new Element_Keys.Generic_Insert_Post (New_Node);
1475 procedure Local_Insert_Sans_Hint is
1476 new Element_Keys.Generic_Conditional_Insert (Local_Insert_Post);
1478 procedure Local_Insert_With_Hint is
1479 new Element_Keys.Generic_Conditional_Insert_With_Hint
1481 Local_Insert_Sans_Hint);
1487 function New_Node return Node_Access is
1489 Node.Element := new Element_Type'(Item); -- OK if fails
1491 Node.Parent := null;
1499 Result : Node_Access;
1502 X : Element_Access := Node.Element;
1504 -- Start of processing for Insert
1507 if Item < Node.Element.all
1508 or else Node.Element.all < Item
1513 if Tree.Lock > 0 then
1514 raise Program_Error with
1515 "attempt to tamper with cursors (set is locked)";
1518 Node.Element := new Element_Type'(Item);
1524 Hint := Element_Keys.Ceiling (Tree, Item);
1529 elsif Item < Hint.Element.all then
1531 if Tree.Lock > 0 then
1532 raise Program_Error with
1533 "attempt to tamper with cursors (set is locked)";
1536 Node.Element := new Element_Type'(Item);
1543 pragma Assert (not (Hint.Element.all < Item));
1544 raise Program_Error with "attempt to replace existing element";
1547 Tree_Operations.Delete_Node_Sans_Free (Tree, Node); -- Checks busy-bit
1549 Local_Insert_With_Hint
1554 Inserted => Inserted);
1556 pragma Assert (Inserted);
1557 pragma Assert (Result = Node);
1560 end Replace_Element;
1562 procedure Replace_Element
1563 (Container : in out Set;
1565 New_Item : Element_Type)
1568 if Position.Node = null then
1569 raise Constraint_Error with "Position cursor equals No_Element";
1572 if Position.Node.Element = null then
1573 raise Program_Error with "Position cursor is bad";
1576 if Position.Container /= Container'Unrestricted_Access then
1577 raise Program_Error with "Position cursor designates wrong set";
1580 pragma Assert (Vet (Container.Tree, Position.Node),
1581 "bad cursor in Replace_Element");
1583 Replace_Element (Container.Tree, Position.Node, New_Item);
1584 end Replace_Element;
1586 ---------------------
1587 -- Reverse_Iterate --
1588 ---------------------
1590 procedure Reverse_Iterate
1592 Process : not null access procedure (Position : Cursor))
1594 procedure Process_Node (Node : Node_Access);
1595 pragma Inline (Process_Node);
1597 procedure Local_Reverse_Iterate is
1598 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1604 procedure Process_Node (Node : Node_Access) is
1606 Process (Cursor'(Container'Unrestricted_Access, Node));
1609 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1610 B : Natural renames T.Busy;
1612 -- Start of processing for Reverse_Iterate
1618 Local_Reverse_Iterate (T);
1626 end Reverse_Iterate;
1632 function Right (Node : Node_Access) return Node_Access is
1641 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1643 Node.Color := Color;
1650 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1659 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1661 Node.Parent := Parent;
1668 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1670 Node.Right := Right;
1673 --------------------------
1674 -- Symmetric_Difference --
1675 --------------------------
1677 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1679 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
1680 end Symmetric_Difference;
1682 function Symmetric_Difference (Left, Right : Set) return Set is
1683 Tree : constant Tree_Type :=
1684 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
1686 return Set'(Controlled with Tree);
1687 end Symmetric_Difference;
1693 function To_Set (New_Item : Element_Type) return Set is
1698 pragma Unreferenced (Node, Inserted);
1701 Insert_Sans_Hint (Tree, New_Item, Node, Inserted);
1702 return Set'(Controlled with Tree);
1709 procedure Union (Target : in out Set; Source : Set) is
1711 Set_Ops.Union (Target.Tree, Source.Tree);
1714 function Union (Left, Right : Set) return Set is
1715 Tree : constant Tree_Type :=
1716 Set_Ops.Union (Left.Tree, Right.Tree);
1718 return Set'(Controlled with Tree);
1726 (Stream : not null access Root_Stream_Type'Class;
1729 procedure Write_Node
1730 (Stream : not null access Root_Stream_Type'Class;
1731 Node : Node_Access);
1732 pragma Inline (Write_Node);
1735 new Tree_Operations.Generic_Write (Write_Node);
1741 procedure Write_Node
1742 (Stream : not null access Root_Stream_Type'Class;
1746 Element_Type'Output (Stream, Node.Element.all);
1749 -- Start of processing for Write
1752 Write (Stream, Container.Tree);
1756 (Stream : not null access Root_Stream_Type'Class;
1760 raise Program_Error with "attempt to stream set cursor";
1763 end Ada.Containers.Indefinite_Ordered_Sets;