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-2005, Free Software Foundation, Inc. --
11 -- This specification is derived from the Ada Reference Manual for use with --
12 -- GNAT. The copyright notice above, and the license provisions that follow --
13 -- apply solely to the contents of the part following the private keyword. --
15 -- GNAT is free software; you can redistribute it and/or modify it under --
16 -- terms of the GNU General Public License as published by the Free Soft- --
17 -- ware Foundation; either version 2, or (at your option) any later ver- --
18 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
19 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
20 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
21 -- for more details. You should have received a copy of the GNU General --
22 -- Public License distributed with GNAT; see file COPYING. If not, write --
23 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
24 -- Boston, MA 02110-1301, USA. --
26 -- As a special exception, if other files instantiate generics from this --
27 -- unit, or you link this unit with other files to produce an executable, --
28 -- this unit does not by itself cause the resulting executable to be --
29 -- covered by the GNU General Public License. This exception does not --
30 -- however invalidate any other reasons why the executable file might be --
31 -- covered by the GNU Public License. --
33 -- This unit was originally developed by Matthew J Heaney. --
34 ------------------------------------------------------------------------------
36 with Ada.Unchecked_Deallocation;
38 with Ada.Containers.Red_Black_Trees.Generic_Operations;
39 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Operations);
41 with Ada.Containers.Red_Black_Trees.Generic_Keys;
42 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Keys);
44 with Ada.Containers.Red_Black_Trees.Generic_Set_Operations;
45 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Set_Operations);
47 package body Ada.Containers.Ordered_Sets is
49 ------------------------------
50 -- Access to Fields of Node --
51 ------------------------------
53 -- These subprograms provide functional notation for access to fields
54 -- of a node, and procedural notation for modifiying these fields.
56 function Color (Node : Node_Access) return Color_Type;
57 pragma Inline (Color);
59 function Left (Node : Node_Access) return Node_Access;
62 function Parent (Node : Node_Access) return Node_Access;
63 pragma Inline (Parent);
65 function Right (Node : Node_Access) return Node_Access;
66 pragma Inline (Right);
68 procedure Set_Color (Node : Node_Access; Color : Color_Type);
69 pragma Inline (Set_Color);
71 procedure Set_Left (Node : Node_Access; Left : Node_Access);
72 pragma Inline (Set_Left);
74 procedure Set_Right (Node : Node_Access; Right : Node_Access);
75 pragma Inline (Set_Right);
77 procedure Set_Parent (Node : Node_Access; Parent : Node_Access);
78 pragma Inline (Set_Parent);
80 -----------------------
81 -- Local Subprograms --
82 -----------------------
84 function Copy_Node (Source : Node_Access) return Node_Access;
85 pragma Inline (Copy_Node);
87 procedure Free (X : in out Node_Access);
89 procedure Insert_Sans_Hint
90 (Tree : in out Tree_Type;
91 New_Item : Element_Type;
92 Node : out Node_Access;
93 Inserted : out Boolean);
95 procedure Insert_With_Hint
96 (Dst_Tree : in out Tree_Type;
97 Dst_Hint : Node_Access;
98 Src_Node : Node_Access;
99 Dst_Node : out Node_Access);
101 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean;
102 pragma Inline (Is_Equal_Node_Node);
104 function Is_Greater_Element_Node
105 (Left : Element_Type;
106 Right : Node_Access) return Boolean;
107 pragma Inline (Is_Greater_Element_Node);
109 function Is_Less_Element_Node
110 (Left : Element_Type;
111 Right : Node_Access) return Boolean;
112 pragma Inline (Is_Less_Element_Node);
114 function Is_Less_Node_Node (L, R : Node_Access) return Boolean;
115 pragma Inline (Is_Less_Node_Node);
117 procedure Replace_Element
118 (Tree : in out Tree_Type;
120 Item : Element_Type);
122 --------------------------
123 -- Local Instantiations --
124 --------------------------
126 package Tree_Operations is
127 new Red_Black_Trees.Generic_Operations (Tree_Types);
129 procedure Delete_Tree is
130 new Tree_Operations.Generic_Delete_Tree (Free);
132 function Copy_Tree is
133 new Tree_Operations.Generic_Copy_Tree (Copy_Node, Delete_Tree);
138 new Tree_Operations.Generic_Equal (Is_Equal_Node_Node);
140 package Element_Keys is
141 new Red_Black_Trees.Generic_Keys
142 (Tree_Operations => Tree_Operations,
143 Key_Type => Element_Type,
144 Is_Less_Key_Node => Is_Less_Element_Node,
145 Is_Greater_Key_Node => Is_Greater_Element_Node);
148 new Generic_Set_Operations
149 (Tree_Operations => Tree_Operations,
150 Insert_With_Hint => Insert_With_Hint,
151 Copy_Tree => Copy_Tree,
152 Delete_Tree => Delete_Tree,
153 Is_Less => Is_Less_Node_Node,
160 function "<" (Left, Right : Cursor) return Boolean is
163 or else Right.Node = null
165 raise Constraint_Error;
168 pragma Assert (Vet (Left.Container.Tree, Left.Node),
169 "bad Left cursor in ""<""");
171 pragma Assert (Vet (Right.Container.Tree, Right.Node),
172 "bad Right cursor in ""<""");
174 return Left.Node.Element < Right.Node.Element;
177 function "<" (Left : Cursor; Right : Element_Type) return Boolean is
179 if Left.Node = null then
180 raise Constraint_Error;
183 pragma Assert (Vet (Left.Container.Tree, Left.Node),
184 "bad Left cursor in ""<""");
186 return Left.Node.Element < Right;
189 function "<" (Left : Element_Type; Right : Cursor) return Boolean is
191 if Right.Node = null then
192 raise Constraint_Error;
195 pragma Assert (Vet (Right.Container.Tree, Right.Node),
196 "bad Right cursor in ""<""");
198 return Left < Right.Node.Element;
205 function "=" (Left, Right : Set) return Boolean is
207 return Is_Equal (Left.Tree, Right.Tree);
214 function ">" (Left, Right : Cursor) return Boolean is
217 or else Right.Node = null
219 raise Constraint_Error;
222 pragma Assert (Vet (Left.Container.Tree, Left.Node),
223 "bad Left cursor in "">""");
225 pragma Assert (Vet (Right.Container.Tree, Right.Node),
226 "bad Right cursor in "">""");
228 -- L > R same as R < L
230 return Right.Node.Element < Left.Node.Element;
233 function ">" (Left : Element_Type; Right : Cursor) return Boolean is
235 if Right.Node = null then
236 raise Constraint_Error;
239 pragma Assert (Vet (Right.Container.Tree, Right.Node),
240 "bad Right cursor in "">""");
242 return Right.Node.Element < Left;
245 function ">" (Left : Cursor; Right : Element_Type) return Boolean is
247 if Left.Node = null then
248 raise Constraint_Error;
251 pragma Assert (Vet (Left.Container.Tree, Left.Node),
252 "bad Left cursor in "">""");
254 return Right < Left.Node.Element;
262 new Tree_Operations.Generic_Adjust (Copy_Tree);
264 procedure Adjust (Container : in out Set) is
266 Adjust (Container.Tree);
273 function Ceiling (Container : Set; Item : Element_Type) return Cursor is
274 Node : constant Node_Access :=
275 Element_Keys.Ceiling (Container.Tree, Item);
282 return Cursor'(Container'Unrestricted_Access, Node);
290 new Tree_Operations.Generic_Clear (Delete_Tree);
292 procedure Clear (Container : in out Set) is
294 Clear (Container.Tree);
301 function Color (Node : Node_Access) return Color_Type is
312 Item : Element_Type) return Boolean
315 return Find (Container, Item) /= No_Element;
322 function Copy_Node (Source : Node_Access) return Node_Access is
323 Target : constant Node_Access :=
324 new Node_Type'(Parent => null,
327 Color => Source.Color,
328 Element => Source.Element);
337 procedure Delete (Container : in out Set; Position : in out Cursor) is
339 if Position.Node = null then
340 raise Constraint_Error;
343 if Position.Container /= Container'Unrestricted_Access then
347 pragma Assert (Vet (Container.Tree, Position.Node),
348 "bad cursor in Delete");
350 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
351 Free (Position.Node);
352 Position.Container := null;
355 procedure Delete (Container : in out Set; Item : Element_Type) is
356 X : Node_Access := Element_Keys.Find (Container.Tree, Item);
360 raise Constraint_Error;
363 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
371 procedure Delete_First (Container : in out Set) is
372 Tree : Tree_Type renames Container.Tree;
373 X : Node_Access := Tree.First;
377 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
386 procedure Delete_Last (Container : in out Set) is
387 Tree : Tree_Type renames Container.Tree;
388 X : Node_Access := Tree.Last;
392 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
401 procedure Difference (Target : in out Set; Source : Set) is
403 Set_Ops.Difference (Target.Tree, Source.Tree);
406 function Difference (Left, Right : Set) return Set is
407 Tree : constant Tree_Type :=
408 Set_Ops.Difference (Left.Tree, Right.Tree);
410 return Set'(Controlled with Tree);
417 function Element (Position : Cursor) return Element_Type is
419 if Position.Node = null then
420 raise Constraint_Error;
423 pragma Assert (Vet (Position.Container.Tree, Position.Node),
424 "bad cursor in Element");
426 return Position.Node.Element;
429 -------------------------
430 -- Equivalent_Elements --
431 -------------------------
433 function Equivalent_Elements (Left, Right : Element_Type) return Boolean is
442 end Equivalent_Elements;
444 ---------------------
445 -- Equivalent_Sets --
446 ---------------------
448 function Equivalent_Sets (Left, Right : Set) return Boolean is
449 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean;
450 pragma Inline (Is_Equivalent_Node_Node);
452 function Is_Equivalent is
453 new Tree_Operations.Generic_Equal (Is_Equivalent_Node_Node);
455 -----------------------------
456 -- Is_Equivalent_Node_Node --
457 -----------------------------
459 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean is
461 if L.Element < R.Element then
463 elsif R.Element < L.Element then
468 end Is_Equivalent_Node_Node;
470 -- Start of processing for Equivalent_Sets
473 return Is_Equivalent (Left.Tree, Right.Tree);
480 procedure Exclude (Container : in out Set; Item : Element_Type) is
481 X : Node_Access := Element_Keys.Find (Container.Tree, Item);
485 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
494 function Find (Container : Set; Item : Element_Type) return Cursor is
495 Node : constant Node_Access :=
496 Element_Keys.Find (Container.Tree, Item);
503 return Cursor'(Container'Unrestricted_Access, Node);
510 function First (Container : Set) return Cursor is
512 if Container.Tree.First = null then
516 return Cursor'(Container'Unrestricted_Access, Container.Tree.First);
523 function First_Element (Container : Set) return Element_Type is
525 if Container.Tree.First = null then
526 raise Constraint_Error;
529 return Container.Tree.First.Element;
536 function Floor (Container : Set; Item : Element_Type) return Cursor is
537 Node : constant Node_Access :=
538 Element_Keys.Floor (Container.Tree, Item);
545 return Cursor'(Container'Unrestricted_Access, Node);
552 procedure Free (X : in out Node_Access) is
553 procedure Deallocate is
554 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
570 package body Generic_Keys is
572 -----------------------
573 -- Local Subprograms --
574 -----------------------
576 function Is_Greater_Key_Node
578 Right : Node_Access) return Boolean;
579 pragma Inline (Is_Greater_Key_Node);
581 function Is_Less_Key_Node
583 Right : Node_Access) return Boolean;
584 pragma Inline (Is_Less_Key_Node);
586 --------------------------
587 -- Local Instantiations --
588 --------------------------
591 new Red_Black_Trees.Generic_Keys
592 (Tree_Operations => Tree_Operations,
593 Key_Type => Key_Type,
594 Is_Less_Key_Node => Is_Less_Key_Node,
595 Is_Greater_Key_Node => Is_Greater_Key_Node);
601 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
602 Node : constant Node_Access :=
603 Key_Keys.Ceiling (Container.Tree, Key);
610 return Cursor'(Container'Unrestricted_Access, Node);
617 function Contains (Container : Set; Key : Key_Type) return Boolean is
619 return Find (Container, Key) /= No_Element;
626 procedure Delete (Container : in out Set; Key : Key_Type) is
627 X : Node_Access := Key_Keys.Find (Container.Tree, Key);
631 raise Constraint_Error;
634 Delete_Node_Sans_Free (Container.Tree, X);
642 function Element (Container : Set; Key : Key_Type) return Element_Type is
643 Node : constant Node_Access :=
644 Key_Keys.Find (Container.Tree, Key);
648 raise Constraint_Error;
654 ---------------------
655 -- Equivalent_Keys --
656 ---------------------
658 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
673 procedure Exclude (Container : in out Set; Key : Key_Type) is
674 X : Node_Access := Key_Keys.Find (Container.Tree, Key);
678 Delete_Node_Sans_Free (Container.Tree, X);
687 function Find (Container : Set; Key : Key_Type) return Cursor is
688 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
695 return Cursor'(Container'Unrestricted_Access, Node);
702 function Floor (Container : Set; Key : Key_Type) return Cursor is
703 Node : constant Node_Access := Key_Keys.Floor (Container.Tree, Key);
710 return Cursor'(Container'Unrestricted_Access, Node);
713 -------------------------
714 -- Is_Greater_Key_Node --
715 -------------------------
717 function Is_Greater_Key_Node
719 Right : Node_Access) return Boolean
722 return Key (Right.Element) < Left;
723 end Is_Greater_Key_Node;
725 ----------------------
726 -- Is_Less_Key_Node --
727 ----------------------
729 function Is_Less_Key_Node
731 Right : Node_Access) return Boolean
734 return Left < Key (Right.Element);
735 end Is_Less_Key_Node;
741 function Key (Position : Cursor) return Key_Type is
743 if Position.Node = null then
744 raise Constraint_Error;
747 pragma Assert (Vet (Position.Container.Tree, Position.Node),
748 "bad cursor in Key");
750 return Key (Position.Node.Element);
758 (Container : in out Set;
760 New_Item : Element_Type)
762 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
766 raise Constraint_Error;
769 Replace_Element (Container.Tree, Node, New_Item);
772 -----------------------------------
773 -- Update_Element_Preserving_Key --
774 -----------------------------------
776 procedure Update_Element_Preserving_Key
777 (Container : in out Set;
779 Process : not null access procedure (Element : in out Element_Type))
781 Tree : Tree_Type renames Container.Tree;
784 if Position.Node = null then
785 raise Constraint_Error;
788 if Position.Container /= Container'Unrestricted_Access then
792 pragma Assert (Vet (Container.Tree, Position.Node),
793 "bad cursor in Update_Element_Preserving_Key");
796 E : Element_Type renames Position.Node.Element;
797 K : constant Key_Type := Key (E);
799 B : Natural renames Tree.Busy;
800 L : Natural renames Tree.Lock;
818 if Equivalent_Keys (K, Key (E)) then
824 X : Node_Access := Position.Node;
826 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
831 end Update_Element_Preserving_Key;
839 function Has_Element (Position : Cursor) return Boolean is
841 return Position /= No_Element;
848 procedure Include (Container : in out Set; New_Item : Element_Type) is
853 Insert (Container, New_Item, Position, Inserted);
856 if Container.Tree.Lock > 0 then
860 Position.Node.Element := New_Item;
869 (Container : in out Set;
870 New_Item : Element_Type;
871 Position : out Cursor;
872 Inserted : out Boolean)
881 Position.Container := Container'Unrestricted_Access;
885 (Container : in out Set;
886 New_Item : Element_Type)
892 Insert (Container, New_Item, Position, Inserted);
895 raise Constraint_Error;
899 ----------------------
900 -- Insert_Sans_Hint --
901 ----------------------
903 procedure Insert_Sans_Hint
904 (Tree : in out Tree_Type;
905 New_Item : Element_Type;
906 Node : out Node_Access;
907 Inserted : out Boolean)
909 function New_Node return Node_Access;
910 pragma Inline (New_Node);
912 procedure Insert_Post is
913 new Element_Keys.Generic_Insert_Post (New_Node);
915 procedure Conditional_Insert_Sans_Hint is
916 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
922 function New_Node return Node_Access is
924 return new Node_Type'(Parent => null,
927 Color => Red_Black_Trees.Red,
928 Element => New_Item);
931 -- Start of processing for Insert_Sans_Hint
934 Conditional_Insert_Sans_Hint
939 end Insert_Sans_Hint;
941 ----------------------
942 -- Insert_With_Hint --
943 ----------------------
945 procedure Insert_With_Hint
946 (Dst_Tree : in out Tree_Type;
947 Dst_Hint : Node_Access;
948 Src_Node : Node_Access;
949 Dst_Node : out Node_Access)
953 function New_Node return Node_Access;
954 pragma Inline (New_Node);
956 procedure Insert_Post is
957 new Element_Keys.Generic_Insert_Post (New_Node);
959 procedure Insert_Sans_Hint is
960 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
962 procedure Local_Insert_With_Hint is
963 new Element_Keys.Generic_Conditional_Insert_With_Hint
971 function New_Node return Node_Access is
972 Node : constant Node_Access :=
973 new Node_Type'(Parent => null,
977 Element => Src_Node.Element);
982 -- Start of processing for Insert_With_Hint
985 Local_Insert_With_Hint
991 end Insert_With_Hint;
997 procedure Intersection (Target : in out Set; Source : Set) is
999 Set_Ops.Intersection (Target.Tree, Source.Tree);
1002 function Intersection (Left, Right : Set) return Set is
1003 Tree : constant Tree_Type :=
1004 Set_Ops.Intersection (Left.Tree, Right.Tree);
1006 return Set'(Controlled with Tree);
1013 function Is_Empty (Container : Set) return Boolean is
1015 return Container.Tree.Length = 0;
1018 ------------------------
1019 -- Is_Equal_Node_Node --
1020 ------------------------
1022 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
1024 return L.Element = R.Element;
1025 end Is_Equal_Node_Node;
1027 -----------------------------
1028 -- Is_Greater_Element_Node --
1029 -----------------------------
1031 function Is_Greater_Element_Node
1032 (Left : Element_Type;
1033 Right : Node_Access) return Boolean
1036 -- Compute e > node same as node < e
1038 return Right.Element < Left;
1039 end Is_Greater_Element_Node;
1041 --------------------------
1042 -- Is_Less_Element_Node --
1043 --------------------------
1045 function Is_Less_Element_Node
1046 (Left : Element_Type;
1047 Right : Node_Access) return Boolean
1050 return Left < Right.Element;
1051 end Is_Less_Element_Node;
1053 -----------------------
1054 -- Is_Less_Node_Node --
1055 -----------------------
1057 function Is_Less_Node_Node (L, R : Node_Access) return Boolean is
1059 return L.Element < R.Element;
1060 end Is_Less_Node_Node;
1066 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1068 return Set_Ops.Is_Subset (Subset => Subset.Tree, Of_Set => Of_Set.Tree);
1077 Process : not null access procedure (Position : Cursor))
1079 procedure Process_Node (Node : Node_Access);
1080 pragma Inline (Process_Node);
1082 procedure Local_Iterate is
1083 new Tree_Operations.Generic_Iteration (Process_Node);
1089 procedure Process_Node (Node : Node_Access) is
1091 Process (Cursor'(Container'Unrestricted_Access, Node));
1094 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1095 B : Natural renames T.Busy;
1097 -- Start of prccessing for Iterate
1117 function Last (Container : Set) return Cursor is
1119 if Container.Tree.Last = null then
1123 return Cursor'(Container'Unrestricted_Access, Container.Tree.Last);
1130 function Last_Element (Container : Set) return Element_Type is
1132 if Container.Tree.Last = null then
1133 raise Constraint_Error;
1136 return Container.Tree.Last.Element;
1143 function Left (Node : Node_Access) return Node_Access is
1152 function Length (Container : Set) return Count_Type is
1154 return Container.Tree.Length;
1162 new Tree_Operations.Generic_Move (Clear);
1164 procedure Move (Target : in out Set; Source : in out Set) is
1166 Move (Target => Target.Tree, Source => Source.Tree);
1173 function Next (Position : Cursor) return Cursor is
1175 if Position = No_Element then
1179 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1180 "bad cursor in Next");
1183 Node : constant Node_Access :=
1184 Tree_Operations.Next (Position.Node);
1191 return Cursor'(Position.Container, Node);
1195 procedure Next (Position : in out Cursor) is
1197 Position := Next (Position);
1204 function Overlap (Left, Right : Set) return Boolean is
1206 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1213 function Parent (Node : Node_Access) return Node_Access is
1222 function Previous (Position : Cursor) return Cursor is
1224 if Position = No_Element then
1228 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1229 "bad cursor in Previous");
1232 Node : constant Node_Access :=
1233 Tree_Operations.Previous (Position.Node);
1240 return Cursor'(Position.Container, Node);
1244 procedure Previous (Position : in out Cursor) is
1246 Position := Previous (Position);
1253 procedure Query_Element
1255 Process : not null access procedure (Element : Element_Type))
1258 if Position.Node = null then
1259 raise Constraint_Error;
1262 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1263 "bad cursor in Query_Element");
1266 T : Tree_Type renames Position.Container.Tree;
1268 B : Natural renames T.Busy;
1269 L : Natural renames T.Lock;
1276 Process (Position.Node.Element);
1294 (Stream : access Root_Stream_Type'Class;
1295 Container : out Set)
1298 (Stream : access Root_Stream_Type'Class) return Node_Access;
1299 pragma Inline (Read_Node);
1302 new Tree_Operations.Generic_Read (Clear, Read_Node);
1309 (Stream : access Root_Stream_Type'Class) return Node_Access
1311 Node : Node_Access := new Node_Type;
1314 Element_Type'Read (Stream, Node.Element);
1323 -- Start of processing for Read
1326 Read (Stream, Container.Tree);
1330 (Stream : access Root_Stream_Type'Class;
1334 raise Program_Error;
1341 procedure Replace (Container : in out Set; New_Item : Element_Type) is
1342 Node : constant Node_Access :=
1343 Element_Keys.Find (Container.Tree, New_Item);
1347 raise Constraint_Error;
1350 if Container.Tree.Lock > 0 then
1351 raise Program_Error;
1354 Node.Element := New_Item;
1357 ---------------------
1358 -- Replace_Element --
1359 ---------------------
1361 procedure Replace_Element
1362 (Tree : in out Tree_Type;
1364 Item : Element_Type)
1367 if Item < Node.Element
1368 or else Node.Element < Item
1372 if Tree.Lock > 0 then
1373 raise Program_Error;
1376 Node.Element := Item;
1380 Tree_Operations.Delete_Node_Sans_Free (Tree, Node); -- Checks busy-bit
1382 Insert_New_Item : declare
1383 function New_Node return Node_Access;
1384 pragma Inline (New_Node);
1386 procedure Insert_Post is
1387 new Element_Keys.Generic_Insert_Post (New_Node);
1390 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1396 function New_Node return Node_Access is
1398 Node.Element := Item;
1400 Node.Parent := null;
1407 Result : Node_Access;
1410 -- Start of processing for Insert_New_Item
1417 Success => Inserted); -- TODO: change param name
1420 pragma Assert (Result = Node);
1425 null; -- Assignment must have failed
1426 end Insert_New_Item;
1428 Reinsert_Old_Element : declare
1429 function New_Node return Node_Access;
1430 pragma Inline (New_Node);
1432 procedure Insert_Post is
1433 new Element_Keys.Generic_Insert_Post (New_Node);
1436 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1442 function New_Node return Node_Access is
1445 Node.Parent := null;
1452 Result : Node_Access;
1455 -- Start of processing for Reinsert_Old_Element
1460 Key => Node.Element,
1462 Success => Inserted); -- TODO: change param name
1465 null; -- Assignment must have failed
1466 end Reinsert_Old_Element;
1468 raise Program_Error;
1469 end Replace_Element;
1471 procedure Replace_Element
1472 (Container : in out Set;
1474 New_Item : Element_Type)
1477 if Position.Node = null then
1478 raise Constraint_Error;
1481 if Position.Container /= Container'Unrestricted_Access then
1482 raise Program_Error;
1485 pragma Assert (Vet (Container.Tree, Position.Node),
1486 "bad cursor in Replace_Element");
1488 Replace_Element (Container.Tree, Position.Node, New_Item);
1489 end Replace_Element;
1491 ---------------------
1492 -- Reverse_Iterate --
1493 ---------------------
1495 procedure Reverse_Iterate
1497 Process : not null access procedure (Position : Cursor))
1499 procedure Process_Node (Node : Node_Access);
1500 pragma Inline (Process_Node);
1502 procedure Local_Reverse_Iterate is
1503 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1509 procedure Process_Node (Node : Node_Access) is
1511 Process (Cursor'(Container'Unrestricted_Access, Node));
1514 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1515 B : Natural renames T.Busy;
1517 -- Start of processing for Reverse_Iterate
1523 Local_Reverse_Iterate (T);
1531 end Reverse_Iterate;
1537 function Right (Node : Node_Access) return Node_Access is
1546 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1548 Node.Color := Color;
1555 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1564 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1566 Node.Parent := Parent;
1573 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1575 Node.Right := Right;
1578 --------------------------
1579 -- Symmetric_Difference --
1580 --------------------------
1582 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1584 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
1585 end Symmetric_Difference;
1587 function Symmetric_Difference (Left, Right : Set) return Set is
1588 Tree : constant Tree_Type :=
1589 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
1591 return Set'(Controlled with Tree);
1592 end Symmetric_Difference;
1598 function To_Set (New_Item : Element_Type) return Set is
1604 Insert_Sans_Hint (Tree, New_Item, Node, Inserted);
1605 return Set'(Controlled with Tree);
1612 procedure Union (Target : in out Set; Source : Set) is
1614 Set_Ops.Union (Target.Tree, Source.Tree);
1617 function Union (Left, Right : Set) return Set is
1618 Tree : constant Tree_Type :=
1619 Set_Ops.Union (Left.Tree, Right.Tree);
1621 return Set'(Controlled with Tree);
1629 (Stream : access Root_Stream_Type'Class;
1632 procedure Write_Node
1633 (Stream : access Root_Stream_Type'Class;
1634 Node : Node_Access);
1635 pragma Inline (Write_Node);
1638 new Tree_Operations.Generic_Write (Write_Node);
1644 procedure Write_Node
1645 (Stream : access Root_Stream_Type'Class;
1649 Element_Type'Write (Stream, Node.Element);
1652 -- Start of processing for Write
1655 Write (Stream, Container.Tree);
1659 (Stream : access Root_Stream_Type'Class;
1663 raise Program_Error;
1666 end Ada.Containers.Ordered_Sets;