1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . --
6 -- I N D E F I N I T E _ O R D E R E D _ S E T S --
10 -- Copyright (C) 2004-2005, Free Software Foundation, Inc. --
12 -- This specification is derived from the Ada Reference Manual for use with --
13 -- GNAT. The copyright notice above, and the license provisions that follow --
14 -- apply solely to the contents of the part following the private keyword. --
16 -- GNAT is free software; you can redistribute it and/or modify it under --
17 -- terms of the GNU General Public License as published by the Free Soft- --
18 -- ware Foundation; either version 2, or (at your option) any later ver- --
19 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
20 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
21 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
22 -- for more details. You should have received a copy of the GNU General --
23 -- Public License distributed with GNAT; see file COPYING. If not, write --
24 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
25 -- Boston, MA 02110-1301, USA. --
27 -- As a special exception, if other files instantiate generics from this --
28 -- unit, or you link this unit with other files to produce an executable, --
29 -- this unit does not by itself cause the resulting executable to be --
30 -- covered by the GNU General Public License. This exception does not --
31 -- however invalidate any other reasons why the executable file might be --
32 -- covered by the GNU Public License. --
34 -- This unit was originally developed by Matthew J Heaney. --
35 ------------------------------------------------------------------------------
37 with Ada.Containers.Red_Black_Trees.Generic_Operations;
38 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Operations);
40 with Ada.Containers.Red_Black_Trees.Generic_Keys;
41 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Keys);
43 with Ada.Containers.Red_Black_Trees.Generic_Set_Operations;
44 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Set_Operations);
46 with Ada.Unchecked_Deallocation;
48 package body Ada.Containers.Indefinite_Ordered_Sets is
50 -----------------------
51 -- Local Subprograms --
52 -----------------------
54 function Color (Node : Node_Access) return Color_Type;
55 pragma Inline (Color);
57 function Copy_Node (Source : Node_Access) return Node_Access;
58 pragma Inline (Copy_Node);
60 procedure Free (X : in out Node_Access);
62 procedure Insert_Sans_Hint
63 (Tree : in out Tree_Type;
64 New_Item : Element_Type;
65 Node : out Node_Access;
66 Inserted : out Boolean);
68 procedure Insert_With_Hint
69 (Dst_Tree : in out Tree_Type;
70 Dst_Hint : Node_Access;
71 Src_Node : Node_Access;
72 Dst_Node : out Node_Access);
74 function Is_Greater_Element_Node
76 Right : Node_Access) return Boolean;
77 pragma Inline (Is_Greater_Element_Node);
79 function Is_Less_Element_Node
81 Right : Node_Access) return Boolean;
82 pragma Inline (Is_Less_Element_Node);
84 function Is_Less_Node_Node (L, R : Node_Access) return Boolean;
85 pragma Inline (Is_Less_Node_Node);
87 function Left (Node : Node_Access) return Node_Access;
90 function Parent (Node : Node_Access) return Node_Access;
91 pragma Inline (Parent);
93 procedure Replace_Element
94 (Tree : in out Tree_Type;
98 function Right (Node : Node_Access) return Node_Access;
99 pragma Inline (Right);
101 procedure Set_Color (Node : Node_Access; Color : Color_Type);
102 pragma Inline (Set_Color);
104 procedure Set_Left (Node : Node_Access; Left : Node_Access);
105 pragma Inline (Set_Left);
107 procedure Set_Parent (Node : Node_Access; Parent : Node_Access);
108 pragma Inline (Set_Parent);
110 procedure Set_Right (Node : Node_Access; Right : Node_Access);
111 pragma Inline (Set_Right);
113 --------------------------
114 -- Local Instantiations --
115 --------------------------
117 procedure Free_Element is
118 new Ada.Unchecked_Deallocation (Element_Type, Element_Access);
120 package Tree_Operations is
121 new Red_Black_Trees.Generic_Operations (Tree_Types);
123 procedure Delete_Tree is
124 new Tree_Operations.Generic_Delete_Tree (Free);
126 function Copy_Tree is
127 new Tree_Operations.Generic_Copy_Tree (Copy_Node, Delete_Tree);
131 package Element_Keys is
132 new Red_Black_Trees.Generic_Keys
133 (Tree_Operations => Tree_Operations,
134 Key_Type => Element_Type,
135 Is_Less_Key_Node => Is_Less_Element_Node,
136 Is_Greater_Key_Node => Is_Greater_Element_Node);
139 new Generic_Set_Operations
140 (Tree_Operations => Tree_Operations,
141 Insert_With_Hint => Insert_With_Hint,
142 Copy_Tree => Copy_Tree,
143 Delete_Tree => Delete_Tree,
144 Is_Less => Is_Less_Node_Node,
151 function "<" (Left, Right : Cursor) return Boolean is
154 or else Right.Node = null
156 raise Constraint_Error;
159 if Left.Node.Element = null
160 or else Right.Node.Element = null
165 pragma Assert (Vet (Left.Container.Tree, Left.Node),
166 "bad Left cursor in ""<""");
168 pragma Assert (Vet (Right.Container.Tree, Right.Node),
169 "bad Right cursor in ""<""");
171 return Left.Node.Element.all < Right.Node.Element.all;
174 function "<" (Left : Cursor; Right : Element_Type) return Boolean is
176 if Left.Node = null then
177 raise Constraint_Error;
180 if Left.Node.Element = null then
184 pragma Assert (Vet (Left.Container.Tree, Left.Node),
185 "bad Left cursor in ""<""");
187 return Left.Node.Element.all < Right;
190 function "<" (Left : Element_Type; Right : Cursor) return Boolean is
192 if Right.Node = null then
193 raise Constraint_Error;
196 if Right.Node.Element = null then
200 pragma Assert (Vet (Right.Container.Tree, Right.Node),
201 "bad Right cursor in ""<""");
203 return Left < Right.Node.Element.all;
210 function "=" (Left, Right : Set) return Boolean is
212 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean;
213 pragma Inline (Is_Equal_Node_Node);
216 new Tree_Operations.Generic_Equal (Is_Equal_Node_Node);
218 ------------------------
219 -- Is_Equal_Node_Node --
220 ------------------------
222 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
224 return L.Element.all = R.Element.all;
225 end Is_Equal_Node_Node;
227 -- Start of processing for "="
230 return Is_Equal (Left.Tree, Right.Tree);
237 function ">" (Left, Right : Cursor) return Boolean is
240 or else Right.Node = null
242 raise Constraint_Error;
245 if Left.Node.Element = null
246 or else Right.Node.Element = null
251 pragma Assert (Vet (Left.Container.Tree, Left.Node),
252 "bad Left cursor in "">""");
254 pragma Assert (Vet (Right.Container.Tree, Right.Node),
255 "bad Right cursor in "">""");
257 -- L > R same as R < L
259 return Right.Node.Element.all < Left.Node.Element.all;
262 function ">" (Left : Cursor; Right : Element_Type) return Boolean is
264 if Left.Node = null then
265 raise Constraint_Error;
268 if Left.Node.Element = null then
272 pragma Assert (Vet (Left.Container.Tree, Left.Node),
273 "bad Left cursor in "">""");
275 return Right < Left.Node.Element.all;
278 function ">" (Left : Element_Type; Right : Cursor) return Boolean is
280 if Right.Node = null then
281 raise Constraint_Error;
284 if Right.Node.Element = null then
288 pragma Assert (Vet (Right.Container.Tree, Right.Node),
289 "bad Right cursor in "">""");
291 return Right.Node.Element.all < Left;
299 new Tree_Operations.Generic_Adjust (Copy_Tree);
301 procedure Adjust (Container : in out Set) is
303 Adjust (Container.Tree);
310 function Ceiling (Container : Set; Item : Element_Type) return Cursor is
311 Node : constant Node_Access :=
312 Element_Keys.Ceiling (Container.Tree, Item);
319 return Cursor'(Container'Unrestricted_Access, Node);
327 new Tree_Operations.Generic_Clear (Delete_Tree);
329 procedure Clear (Container : in out Set) is
331 Clear (Container.Tree);
338 function Color (Node : Node_Access) return Color_Type is
347 function Contains (Container : Set; Item : Element_Type) return Boolean is
349 return Find (Container, Item) /= No_Element;
356 function Copy_Node (Source : Node_Access) return Node_Access is
357 Element : Element_Access := new Element_Type'(Source.Element.all);
360 return new Node_Type'(Parent => null,
363 Color => Source.Color,
367 Free_Element (Element);
375 procedure Delete (Container : in out Set; Position : in out Cursor) is
377 if Position.Node = null then
378 raise Constraint_Error;
381 if Position.Container /= Container'Unrestricted_Access then
385 pragma Assert (Vet (Container.Tree, Position.Node),
386 "bad cursor in Delete");
388 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
389 Free (Position.Node);
390 Position.Container := null;
393 procedure Delete (Container : in out Set; Item : Element_Type) is
395 Element_Keys.Find (Container.Tree, Item);
399 raise Constraint_Error;
402 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
410 procedure Delete_First (Container : in out Set) is
411 Tree : Tree_Type renames Container.Tree;
412 X : Node_Access := Tree.First;
416 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
425 procedure Delete_Last (Container : in out Set) is
426 Tree : Tree_Type renames Container.Tree;
427 X : Node_Access := Tree.Last;
431 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
440 procedure Difference (Target : in out Set; Source : Set) is
442 Set_Ops.Difference (Target.Tree, Source.Tree);
445 function Difference (Left, Right : Set) return Set is
446 Tree : constant Tree_Type :=
447 Set_Ops.Difference (Left.Tree, Right.Tree);
449 return Set'(Controlled with Tree);
456 function Element (Position : Cursor) return Element_Type is
458 if Position.Node = null then
459 raise Constraint_Error;
462 if Position.Node.Element = null then
466 pragma Assert (Vet (Position.Container.Tree, Position.Node),
467 "bad cursor in Element");
469 return Position.Node.Element.all;
472 -------------------------
473 -- Equivalent_Elements --
474 -------------------------
476 function Equivalent_Elements (Left, Right : Element_Type) return Boolean is
485 end Equivalent_Elements;
487 ---------------------
488 -- Equivalent_Sets --
489 ---------------------
491 function Equivalent_Sets (Left, Right : Set) return Boolean is
493 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean;
494 pragma Inline (Is_Equivalent_Node_Node);
496 function Is_Equivalent is
497 new Tree_Operations.Generic_Equal (Is_Equivalent_Node_Node);
499 -----------------------------
500 -- Is_Equivalent_Node_Node --
501 -----------------------------
503 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean is
505 if L.Element.all < R.Element.all then
507 elsif R.Element.all < L.Element.all then
512 end Is_Equivalent_Node_Node;
514 -- Start of processing for Equivalent_Sets
517 return Is_Equivalent (Left.Tree, Right.Tree);
524 procedure Exclude (Container : in out Set; Item : Element_Type) is
526 Element_Keys.Find (Container.Tree, Item);
530 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
539 function Find (Container : Set; Item : Element_Type) return Cursor is
540 Node : constant Node_Access :=
541 Element_Keys.Find (Container.Tree, Item);
548 return Cursor'(Container'Unrestricted_Access, Node);
555 function First (Container : Set) return Cursor is
557 if Container.Tree.First = null then
561 return Cursor'(Container'Unrestricted_Access, Container.Tree.First);
568 function First_Element (Container : Set) return Element_Type is
570 if Container.Tree.First = null then
571 raise Constraint_Error;
574 return Container.Tree.First.Element.all;
581 function Floor (Container : Set; Item : Element_Type) return Cursor is
582 Node : constant Node_Access :=
583 Element_Keys.Floor (Container.Tree, Item);
590 return Cursor'(Container'Unrestricted_Access, Node);
597 procedure Free (X : in out Node_Access) is
598 procedure Deallocate is
599 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
611 Free_Element (X.Element);
626 package body Generic_Keys is
628 -----------------------
629 -- Local Subprograms --
630 -----------------------
632 function Is_Greater_Key_Node
634 Right : Node_Access) return Boolean;
635 pragma Inline (Is_Greater_Key_Node);
637 function Is_Less_Key_Node
639 Right : Node_Access) return Boolean;
640 pragma Inline (Is_Less_Key_Node);
642 --------------------------
643 -- Local Instantiations --
644 --------------------------
647 new Red_Black_Trees.Generic_Keys
648 (Tree_Operations => Tree_Operations,
649 Key_Type => Key_Type,
650 Is_Less_Key_Node => Is_Less_Key_Node,
651 Is_Greater_Key_Node => Is_Greater_Key_Node);
657 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
658 Node : constant Node_Access :=
659 Key_Keys.Ceiling (Container.Tree, Key);
666 return Cursor'(Container'Unrestricted_Access, Node);
673 function Contains (Container : Set; Key : Key_Type) return Boolean is
675 return Find (Container, Key) /= No_Element;
682 procedure Delete (Container : in out Set; Key : Key_Type) is
683 X : Node_Access := Key_Keys.Find (Container.Tree, Key);
687 raise Constraint_Error;
690 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
698 function Element (Container : Set; Key : Key_Type) return Element_Type is
699 Node : constant Node_Access :=
700 Key_Keys.Find (Container.Tree, Key);
704 raise Constraint_Error;
707 return Node.Element.all;
710 ---------------------
711 -- Equivalent_Keys --
712 ---------------------
714 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
729 procedure Exclude (Container : in out Set; Key : Key_Type) is
730 X : Node_Access := Key_Keys.Find (Container.Tree, Key);
734 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
743 function Find (Container : Set; Key : Key_Type) return Cursor is
744 Node : constant Node_Access :=
745 Key_Keys.Find (Container.Tree, Key);
752 return Cursor'(Container'Unrestricted_Access, Node);
759 function Floor (Container : Set; Key : Key_Type) return Cursor is
760 Node : constant Node_Access :=
761 Key_Keys.Floor (Container.Tree, Key);
768 return Cursor'(Container'Unrestricted_Access, Node);
771 -------------------------
772 -- Is_Greater_Key_Node --
773 -------------------------
775 function Is_Greater_Key_Node
777 Right : Node_Access) return Boolean is
779 return Key (Right.Element.all) < Left;
780 end Is_Greater_Key_Node;
782 ----------------------
783 -- Is_Less_Key_Node --
784 ----------------------
786 function Is_Less_Key_Node
788 Right : Node_Access) return Boolean is
790 return Left < Key (Right.Element.all);
791 end Is_Less_Key_Node;
797 function Key (Position : Cursor) return Key_Type is
799 if Position.Node = null then
800 raise Constraint_Error;
803 if Position.Node.Element = null then
807 pragma Assert (Vet (Position.Container.Tree, Position.Node),
808 "bad cursor in Key");
810 return Key (Position.Node.Element.all);
818 (Container : in out Set;
820 New_Item : Element_Type)
822 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
826 raise Constraint_Error;
829 Replace_Element (Container.Tree, Node, New_Item);
832 -----------------------------------
833 -- Update_Element_Preserving_Key --
834 -----------------------------------
836 procedure Update_Element_Preserving_Key
837 (Container : in out Set;
839 Process : not null access
840 procedure (Element : in out Element_Type))
842 Tree : Tree_Type renames Container.Tree;
845 if Position.Node = null then
846 raise Constraint_Error;
849 if Position.Node.Element = null then
853 if Position.Container /= Container'Unrestricted_Access then
857 pragma Assert (Vet (Container.Tree, Position.Node),
858 "bad cursor in Update_Element_Preserving_Key");
861 E : Element_Type renames Position.Node.Element.all;
862 K : constant Key_Type := Key (E);
864 B : Natural renames Tree.Busy;
865 L : Natural renames Tree.Lock;
883 if Equivalent_Keys (K, Key (E)) then
889 X : Node_Access := Position.Node;
891 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
896 end Update_Element_Preserving_Key;
904 function Has_Element (Position : Cursor) return Boolean is
906 return Position /= No_Element;
913 procedure Include (Container : in out Set; New_Item : Element_Type) is
920 Insert (Container, New_Item, Position, Inserted);
923 if Container.Tree.Lock > 0 then
927 X := Position.Node.Element;
928 Position.Node.Element := new Element_Type'(New_Item);
938 (Container : in out Set;
939 New_Item : Element_Type;
940 Position : out Cursor;
941 Inserted : out Boolean)
950 Position.Container := Container'Unrestricted_Access;
953 procedure Insert (Container : in out Set; New_Item : Element_Type) is
957 Insert (Container, New_Item, Position, Inserted);
960 raise Constraint_Error;
964 ----------------------
965 -- Insert_Sans_Hint --
966 ----------------------
968 procedure Insert_Sans_Hint
969 (Tree : in out Tree_Type;
970 New_Item : Element_Type;
971 Node : out Node_Access;
972 Inserted : out Boolean)
974 function New_Node return Node_Access;
975 pragma Inline (New_Node);
977 procedure Insert_Post is
978 new Element_Keys.Generic_Insert_Post (New_Node);
980 procedure Conditional_Insert_Sans_Hint is
981 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
987 function New_Node return Node_Access is
988 Element : Element_Access := new Element_Type'(New_Item);
991 return new Node_Type'(Parent => null,
994 Color => Red_Black_Trees.Red,
998 Free_Element (Element);
1002 -- Start of processing for Insert_Sans_Hint
1005 Conditional_Insert_Sans_Hint
1010 end Insert_Sans_Hint;
1012 ----------------------
1013 -- Insert_With_Hint --
1014 ----------------------
1016 procedure Insert_With_Hint
1017 (Dst_Tree : in out Tree_Type;
1018 Dst_Hint : Node_Access;
1019 Src_Node : Node_Access;
1020 Dst_Node : out Node_Access)
1024 function New_Node return Node_Access;
1026 procedure Insert_Post is
1027 new Element_Keys.Generic_Insert_Post (New_Node);
1029 procedure Insert_Sans_Hint is
1030 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1032 procedure Insert_With_Hint is
1033 new Element_Keys.Generic_Conditional_Insert_With_Hint
1041 function New_Node return Node_Access is
1042 Element : Element_Access :=
1043 new Element_Type'(Src_Node.Element.all);
1048 Node := new Node_Type;
1051 Free_Element (Element);
1055 Node.Element := Element;
1059 -- Start of processing for Insert_With_Hint
1065 Src_Node.Element.all,
1068 end Insert_With_Hint;
1074 procedure Intersection (Target : in out Set; Source : Set) is
1076 Set_Ops.Intersection (Target.Tree, Source.Tree);
1079 function Intersection (Left, Right : Set) return Set is
1080 Tree : constant Tree_Type :=
1081 Set_Ops.Intersection (Left.Tree, Right.Tree);
1083 return Set'(Controlled with Tree);
1090 function Is_Empty (Container : Set) return Boolean is
1092 return Container.Tree.Length = 0;
1095 -----------------------------
1096 -- Is_Greater_Element_Node --
1097 -----------------------------
1099 function Is_Greater_Element_Node
1100 (Left : Element_Type;
1101 Right : Node_Access) return Boolean is
1103 -- e > node same as node < e
1105 return Right.Element.all < Left;
1106 end Is_Greater_Element_Node;
1108 --------------------------
1109 -- Is_Less_Element_Node --
1110 --------------------------
1112 function Is_Less_Element_Node
1113 (Left : Element_Type;
1114 Right : Node_Access) return Boolean is
1116 return Left < Right.Element.all;
1117 end Is_Less_Element_Node;
1119 -----------------------
1120 -- Is_Less_Node_Node --
1121 -----------------------
1123 function Is_Less_Node_Node (L, R : Node_Access) return Boolean is
1125 return L.Element.all < R.Element.all;
1126 end Is_Less_Node_Node;
1132 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1134 return Set_Ops.Is_Subset (Subset => Subset.Tree, Of_Set => Of_Set.Tree);
1143 Process : not null access procedure (Position : Cursor))
1145 procedure Process_Node (Node : Node_Access);
1146 pragma Inline (Process_Node);
1148 procedure Local_Iterate is
1149 new Tree_Operations.Generic_Iteration (Process_Node);
1155 procedure Process_Node (Node : Node_Access) is
1157 Process (Cursor'(Container'Unrestricted_Access, Node));
1160 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1161 B : Natural renames T.Busy;
1163 -- Start of prccessing for Iterate
1183 function Last (Container : Set) return Cursor is
1185 if Container.Tree.Last = null then
1189 return Cursor'(Container'Unrestricted_Access, Container.Tree.Last);
1196 function Last_Element (Container : Set) return Element_Type is
1198 if Container.Tree.Last = null then
1199 raise Constraint_Error;
1202 return Container.Tree.Last.Element.all;
1209 function Left (Node : Node_Access) return Node_Access is
1218 function Length (Container : Set) return Count_Type is
1220 return Container.Tree.Length;
1228 new Tree_Operations.Generic_Move (Clear);
1230 procedure Move (Target : in out Set; Source : in out Set) is
1232 Move (Target => Target.Tree, Source => Source.Tree);
1239 procedure Next (Position : in out Cursor) is
1241 Position := Next (Position);
1244 function Next (Position : Cursor) return Cursor is
1246 if Position = No_Element then
1250 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1251 "bad cursor in Next");
1254 Node : constant Node_Access :=
1255 Tree_Operations.Next (Position.Node);
1262 return Cursor'(Position.Container, Node);
1270 function Overlap (Left, Right : Set) return Boolean is
1272 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1279 function Parent (Node : Node_Access) return Node_Access is
1288 procedure Previous (Position : in out Cursor) is
1290 Position := Previous (Position);
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);
1311 return Cursor'(Position.Container, Node);
1319 procedure Query_Element
1321 Process : not null access procedure (Element : Element_Type))
1324 if Position.Node = null then
1325 raise Constraint_Error;
1328 if Position.Node.Element = null then
1329 raise Program_Error;
1332 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1333 "bad cursor in Query_Element");
1336 T : Tree_Type renames Position.Container.Tree;
1338 B : Natural renames T.Busy;
1339 L : Natural renames T.Lock;
1346 Process (Position.Node.Element.all);
1364 (Stream : access Root_Stream_Type'Class;
1365 Container : out Set)
1368 (Stream : access Root_Stream_Type'Class) return Node_Access;
1369 pragma Inline (Read_Node);
1372 new Tree_Operations.Generic_Read (Clear, Read_Node);
1379 (Stream : access Root_Stream_Type'Class) return Node_Access
1381 Node : Node_Access := new Node_Type;
1384 Node.Element := new Element_Type'(Element_Type'Input (Stream));
1389 Free (Node); -- Note that Free deallocates elem too
1393 -- Start of processing for Read
1396 Read (Stream, Container.Tree);
1400 (Stream : access Root_Stream_Type'Class;
1404 raise Program_Error;
1411 procedure Replace (Container : in out Set; New_Item : Element_Type) is
1412 Node : constant Node_Access :=
1413 Element_Keys.Find (Container.Tree, New_Item);
1419 raise Constraint_Error;
1422 if Container.Tree.Lock > 0 then
1423 raise Program_Error;
1427 Node.Element := new Element_Type'(New_Item);
1431 ---------------------
1432 -- Replace_Element --
1433 ---------------------
1435 procedure Replace_Element
1436 (Tree : in out Tree_Type;
1438 Item : Element_Type)
1441 if Item < Node.Element.all
1442 or else Node.Element.all < Item
1446 if Tree.Lock > 0 then
1447 raise Program_Error;
1451 X : Element_Access := Node.Element;
1453 Node.Element := new Element_Type'(Item);
1460 Tree_Operations.Delete_Node_Sans_Free (Tree, Node); -- Checks busy-bit
1462 Insert_New_Item : declare
1463 function New_Node return Node_Access;
1464 pragma Inline (New_Node);
1466 procedure Insert_Post is
1467 new Element_Keys.Generic_Insert_Post (New_Node);
1470 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1476 function New_Node return Node_Access is
1478 Node.Element := new Element_Type'(Item); -- OK if fails
1480 Node.Parent := null;
1487 Result : Node_Access;
1490 X : Element_Access := Node.Element;
1492 -- Start of processing for Insert_New_Item
1495 Attempt_Insert : begin
1500 Success => Inserted); -- TODO: change name of formal param
1507 pragma Assert (Result = Node);
1508 Free_Element (X); -- OK if fails
1511 end Insert_New_Item;
1513 Reinsert_Old_Element : declare
1514 function New_Node return Node_Access;
1515 pragma Inline (New_Node);
1517 procedure Insert_Post is
1518 new Element_Keys.Generic_Insert_Post (New_Node);
1521 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1527 function New_Node return Node_Access is
1530 Node.Parent := null;
1537 Result : Node_Access;
1540 -- Start of processing for Reinsert_Old_Element
1545 Key => Node.Element.all,
1547 Success => Inserted); -- TODO: change name of formal param
1551 end Reinsert_Old_Element;
1553 raise Program_Error;
1554 end Replace_Element;
1556 procedure Replace_Element
1557 (Container : in out Set;
1559 New_Item : Element_Type)
1562 if Position.Node = null then
1563 raise Constraint_Error;
1566 if Position.Node.Element = null then
1567 raise Program_Error;
1570 if Position.Container /= Container'Unrestricted_Access then
1571 raise Program_Error;
1574 pragma Assert (Vet (Container.Tree, Position.Node),
1575 "bad cursor in Replace_Element");
1577 Replace_Element (Container.Tree, Position.Node, New_Item);
1578 end Replace_Element;
1580 ---------------------
1581 -- Reverse_Iterate --
1582 ---------------------
1584 procedure Reverse_Iterate
1586 Process : not null access procedure (Position : Cursor))
1588 procedure Process_Node (Node : Node_Access);
1589 pragma Inline (Process_Node);
1591 procedure Local_Reverse_Iterate is
1592 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1598 procedure Process_Node (Node : Node_Access) is
1600 Process (Cursor'(Container'Unrestricted_Access, Node));
1603 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1604 B : Natural renames T.Busy;
1606 -- Start of processing for Reverse_Iterate
1612 Local_Reverse_Iterate (T);
1620 end Reverse_Iterate;
1626 function Right (Node : Node_Access) return Node_Access is
1635 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1637 Node.Color := Color;
1644 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1653 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1655 Node.Parent := Parent;
1662 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1664 Node.Right := Right;
1667 --------------------------
1668 -- Symmetric_Difference --
1669 --------------------------
1671 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1673 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
1674 end Symmetric_Difference;
1676 function Symmetric_Difference (Left, Right : Set) return Set is
1677 Tree : constant Tree_Type :=
1678 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
1680 return Set'(Controlled with Tree);
1681 end Symmetric_Difference;
1687 function To_Set (New_Item : Element_Type) return Set is
1693 Insert_Sans_Hint (Tree, New_Item, Node, Inserted);
1694 return Set'(Controlled with Tree);
1701 procedure Union (Target : in out Set; Source : Set) is
1703 Set_Ops.Union (Target.Tree, Source.Tree);
1706 function Union (Left, Right : Set) return Set is
1707 Tree : constant Tree_Type :=
1708 Set_Ops.Union (Left.Tree, Right.Tree);
1710 return Set'(Controlled with Tree);
1718 (Stream : access Root_Stream_Type'Class;
1721 procedure Write_Node
1722 (Stream : access Root_Stream_Type'Class;
1723 Node : Node_Access);
1724 pragma Inline (Write_Node);
1727 new Tree_Operations.Generic_Write (Write_Node);
1733 procedure Write_Node
1734 (Stream : access Root_Stream_Type'Class;
1738 Element_Type'Output (Stream, Node.Element.all);
1741 -- Start of processing for Write
1744 Write (Stream, Container.Tree);
1748 (Stream : access Root_Stream_Type'Class;
1752 raise Program_Error;
1755 end Ada.Containers.Indefinite_Ordered_Sets;