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-2008, 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.Unchecked_Deallocation;
34 with Ada.Containers.Red_Black_Trees.Generic_Operations;
35 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Operations);
37 with Ada.Containers.Red_Black_Trees.Generic_Keys;
38 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Keys);
40 with Ada.Containers.Red_Black_Trees.Generic_Set_Operations;
41 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Set_Operations);
43 package body Ada.Containers.Ordered_Sets is
45 ------------------------------
46 -- Access to Fields of Node --
47 ------------------------------
49 -- These subprograms provide functional notation for access to fields
50 -- of a node, and procedural notation for modifying these fields.
52 function Color (Node : Node_Access) return Color_Type;
53 pragma Inline (Color);
55 function Left (Node : Node_Access) return Node_Access;
58 function Parent (Node : Node_Access) return Node_Access;
59 pragma Inline (Parent);
61 function Right (Node : Node_Access) return Node_Access;
62 pragma Inline (Right);
64 procedure Set_Color (Node : Node_Access; Color : Color_Type);
65 pragma Inline (Set_Color);
67 procedure Set_Left (Node : Node_Access; Left : Node_Access);
68 pragma Inline (Set_Left);
70 procedure Set_Right (Node : Node_Access; Right : Node_Access);
71 pragma Inline (Set_Right);
73 procedure Set_Parent (Node : Node_Access; Parent : Node_Access);
74 pragma Inline (Set_Parent);
76 -----------------------
77 -- Local Subprograms --
78 -----------------------
80 function Copy_Node (Source : Node_Access) return Node_Access;
81 pragma Inline (Copy_Node);
83 procedure Free (X : in out Node_Access);
85 procedure Insert_Sans_Hint
86 (Tree : in out Tree_Type;
87 New_Item : Element_Type;
88 Node : out Node_Access;
89 Inserted : out Boolean);
91 procedure Insert_With_Hint
92 (Dst_Tree : in out Tree_Type;
93 Dst_Hint : Node_Access;
94 Src_Node : Node_Access;
95 Dst_Node : out Node_Access);
97 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean;
98 pragma Inline (Is_Equal_Node_Node);
100 function Is_Greater_Element_Node
101 (Left : Element_Type;
102 Right : Node_Access) return Boolean;
103 pragma Inline (Is_Greater_Element_Node);
105 function Is_Less_Element_Node
106 (Left : Element_Type;
107 Right : Node_Access) return Boolean;
108 pragma Inline (Is_Less_Element_Node);
110 function Is_Less_Node_Node (L, R : Node_Access) return Boolean;
111 pragma Inline (Is_Less_Node_Node);
113 procedure Replace_Element
114 (Tree : in out Tree_Type;
116 Item : Element_Type);
118 --------------------------
119 -- Local Instantiations --
120 --------------------------
122 package Tree_Operations is
123 new Red_Black_Trees.Generic_Operations (Tree_Types);
125 procedure Delete_Tree is
126 new Tree_Operations.Generic_Delete_Tree (Free);
128 function Copy_Tree is
129 new Tree_Operations.Generic_Copy_Tree (Copy_Node, Delete_Tree);
134 new Tree_Operations.Generic_Equal (Is_Equal_Node_Node);
136 package Element_Keys is
137 new Red_Black_Trees.Generic_Keys
138 (Tree_Operations => Tree_Operations,
139 Key_Type => Element_Type,
140 Is_Less_Key_Node => Is_Less_Element_Node,
141 Is_Greater_Key_Node => Is_Greater_Element_Node);
144 new Generic_Set_Operations
145 (Tree_Operations => Tree_Operations,
146 Insert_With_Hint => Insert_With_Hint,
147 Copy_Tree => Copy_Tree,
148 Delete_Tree => Delete_Tree,
149 Is_Less => Is_Less_Node_Node,
156 function "<" (Left, Right : Cursor) return Boolean is
158 if Left.Node = null then
159 raise Constraint_Error with "Left cursor equals No_Element";
162 if Right.Node = null then
163 raise Constraint_Error with "Right cursor equals No_Element";
166 pragma Assert (Vet (Left.Container.Tree, Left.Node),
167 "bad Left cursor in ""<""");
169 pragma Assert (Vet (Right.Container.Tree, Right.Node),
170 "bad Right cursor in ""<""");
172 return Left.Node.Element < Right.Node.Element;
175 function "<" (Left : Cursor; Right : Element_Type) return Boolean is
177 if Left.Node = null then
178 raise Constraint_Error with "Left cursor equals No_Element";
181 pragma Assert (Vet (Left.Container.Tree, Left.Node),
182 "bad Left cursor in ""<""");
184 return Left.Node.Element < Right;
187 function "<" (Left : Element_Type; Right : Cursor) return Boolean is
189 if Right.Node = null then
190 raise Constraint_Error with "Right cursor equals No_Element";
193 pragma Assert (Vet (Right.Container.Tree, Right.Node),
194 "bad Right cursor in ""<""");
196 return Left < Right.Node.Element;
203 function "=" (Left, Right : Set) return Boolean is
205 return Is_Equal (Left.Tree, Right.Tree);
212 function ">" (Left, Right : Cursor) return Boolean is
214 if Left.Node = null then
215 raise Constraint_Error with "Left cursor equals No_Element";
218 if Right.Node = null then
219 raise Constraint_Error with "Right cursor equals No_Element";
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 with "Right cursor equals No_Element";
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 with "Left cursor equals No_Element";
251 pragma Assert (Vet (Left.Container.Tree, Left.Node),
252 "bad Left cursor in "">""");
254 return Right < Left.Node.Element;
261 procedure Adjust is new Tree_Operations.Generic_Adjust (Copy_Tree);
263 procedure Adjust (Container : in out Set) is
265 Adjust (Container.Tree);
272 function Ceiling (Container : Set; Item : Element_Type) return Cursor is
273 Node : constant Node_Access :=
274 Element_Keys.Ceiling (Container.Tree, Item);
281 return Cursor'(Container'Unrestricted_Access, Node);
288 procedure Clear is new Tree_Operations.Generic_Clear (Delete_Tree);
290 procedure Clear (Container : in out Set) is
292 Clear (Container.Tree);
299 function Color (Node : Node_Access) return Color_Type is
310 Item : Element_Type) return Boolean
313 return Find (Container, Item) /= No_Element;
320 function Copy_Node (Source : Node_Access) return Node_Access is
321 Target : constant Node_Access :=
322 new Node_Type'(Parent => null,
325 Color => Source.Color,
326 Element => Source.Element);
335 procedure Delete (Container : in out Set; Position : in out Cursor) is
337 if Position.Node = null then
338 raise Constraint_Error with "Position cursor equals No_Element";
341 if Position.Container /= Container'Unrestricted_Access then
342 raise Program_Error with "Position cursor designates wrong set";
345 pragma Assert (Vet (Container.Tree, Position.Node),
346 "bad cursor in Delete");
348 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
349 Free (Position.Node);
350 Position.Container := null;
353 procedure Delete (Container : in out Set; Item : Element_Type) is
354 X : Node_Access := Element_Keys.Find (Container.Tree, Item);
358 raise Constraint_Error with "attempt to delete element not in set";
361 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
369 procedure Delete_First (Container : in out Set) is
370 Tree : Tree_Type renames Container.Tree;
371 X : Node_Access := Tree.First;
375 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
384 procedure Delete_Last (Container : in out Set) is
385 Tree : Tree_Type renames Container.Tree;
386 X : Node_Access := Tree.Last;
390 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
399 procedure Difference (Target : in out Set; Source : Set) is
401 Set_Ops.Difference (Target.Tree, Source.Tree);
404 function Difference (Left, Right : Set) return Set is
405 Tree : constant Tree_Type :=
406 Set_Ops.Difference (Left.Tree, Right.Tree);
408 return Set'(Controlled with Tree);
415 function Element (Position : Cursor) return Element_Type is
417 if Position.Node = null then
418 raise Constraint_Error with "Position cursor equals No_Element";
421 pragma Assert (Vet (Position.Container.Tree, Position.Node),
422 "bad cursor in Element");
424 return Position.Node.Element;
427 -------------------------
428 -- Equivalent_Elements --
429 -------------------------
431 function Equivalent_Elements (Left, Right : Element_Type) return Boolean is
440 end Equivalent_Elements;
442 ---------------------
443 -- Equivalent_Sets --
444 ---------------------
446 function Equivalent_Sets (Left, Right : Set) return Boolean is
447 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean;
448 pragma Inline (Is_Equivalent_Node_Node);
450 function Is_Equivalent is
451 new Tree_Operations.Generic_Equal (Is_Equivalent_Node_Node);
453 -----------------------------
454 -- Is_Equivalent_Node_Node --
455 -----------------------------
457 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean is
459 if L.Element < R.Element then
461 elsif R.Element < L.Element then
466 end Is_Equivalent_Node_Node;
468 -- Start of processing for Equivalent_Sets
471 return Is_Equivalent (Left.Tree, Right.Tree);
478 procedure Exclude (Container : in out Set; Item : Element_Type) is
479 X : Node_Access := Element_Keys.Find (Container.Tree, Item);
483 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
492 function Find (Container : Set; Item : Element_Type) return Cursor is
493 Node : constant Node_Access :=
494 Element_Keys.Find (Container.Tree, Item);
501 return Cursor'(Container'Unrestricted_Access, Node);
508 function First (Container : Set) return Cursor is
510 if Container.Tree.First = null then
514 return Cursor'(Container'Unrestricted_Access, Container.Tree.First);
521 function First_Element (Container : Set) return Element_Type is
523 if Container.Tree.First = null then
524 raise Constraint_Error with "set is empty";
527 return Container.Tree.First.Element;
534 function Floor (Container : Set; Item : Element_Type) return Cursor is
535 Node : constant Node_Access :=
536 Element_Keys.Floor (Container.Tree, Item);
543 return Cursor'(Container'Unrestricted_Access, Node);
550 procedure Free (X : in out Node_Access) is
551 procedure Deallocate is
552 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
568 package body Generic_Keys is
570 -----------------------
571 -- Local Subprograms --
572 -----------------------
574 function Is_Greater_Key_Node
576 Right : Node_Access) return Boolean;
577 pragma Inline (Is_Greater_Key_Node);
579 function Is_Less_Key_Node
581 Right : Node_Access) return Boolean;
582 pragma Inline (Is_Less_Key_Node);
584 --------------------------
585 -- Local Instantiations --
586 --------------------------
589 new Red_Black_Trees.Generic_Keys
590 (Tree_Operations => Tree_Operations,
591 Key_Type => Key_Type,
592 Is_Less_Key_Node => Is_Less_Key_Node,
593 Is_Greater_Key_Node => Is_Greater_Key_Node);
599 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
600 Node : constant Node_Access :=
601 Key_Keys.Ceiling (Container.Tree, Key);
608 return Cursor'(Container'Unrestricted_Access, Node);
615 function Contains (Container : Set; Key : Key_Type) return Boolean is
617 return Find (Container, Key) /= No_Element;
624 procedure Delete (Container : in out Set; Key : Key_Type) is
625 X : Node_Access := Key_Keys.Find (Container.Tree, Key);
629 raise Constraint_Error with "attempt to delete key not in set";
632 Delete_Node_Sans_Free (Container.Tree, X);
640 function Element (Container : Set; Key : Key_Type) return Element_Type is
641 Node : constant Node_Access :=
642 Key_Keys.Find (Container.Tree, Key);
646 raise Constraint_Error with "key not in set";
652 ---------------------
653 -- Equivalent_Keys --
654 ---------------------
656 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
671 procedure Exclude (Container : in out Set; Key : Key_Type) is
672 X : Node_Access := Key_Keys.Find (Container.Tree, Key);
676 Delete_Node_Sans_Free (Container.Tree, X);
685 function Find (Container : Set; Key : Key_Type) return Cursor is
686 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
693 return Cursor'(Container'Unrestricted_Access, Node);
700 function Floor (Container : Set; Key : Key_Type) return Cursor is
701 Node : constant Node_Access := Key_Keys.Floor (Container.Tree, Key);
708 return Cursor'(Container'Unrestricted_Access, Node);
711 -------------------------
712 -- Is_Greater_Key_Node --
713 -------------------------
715 function Is_Greater_Key_Node
717 Right : Node_Access) return Boolean
720 return Key (Right.Element) < Left;
721 end Is_Greater_Key_Node;
723 ----------------------
724 -- Is_Less_Key_Node --
725 ----------------------
727 function Is_Less_Key_Node
729 Right : Node_Access) return Boolean
732 return Left < Key (Right.Element);
733 end Is_Less_Key_Node;
739 function Key (Position : Cursor) return Key_Type is
741 if Position.Node = null then
742 raise Constraint_Error with
743 "Position cursor equals No_Element";
746 pragma Assert (Vet (Position.Container.Tree, Position.Node),
747 "bad cursor in Key");
749 return Key (Position.Node.Element);
757 (Container : in out Set;
759 New_Item : Element_Type)
761 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
765 raise Constraint_Error with
766 "attempt to replace key not in set";
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 with
786 "Position cursor equals No_Element";
789 if Position.Container /= Container'Unrestricted_Access then
790 raise Program_Error with
791 "Position cursor designates wrong set";
794 pragma Assert (Vet (Container.Tree, Position.Node),
795 "bad cursor in Update_Element_Preserving_Key");
798 E : Element_Type renames Position.Node.Element;
799 K : constant Key_Type := Key (E);
801 B : Natural renames Tree.Busy;
802 L : Natural renames Tree.Lock;
820 if Equivalent_Keys (K, Key (E)) then
826 X : Node_Access := Position.Node;
828 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
832 raise Program_Error with "key was modified";
833 end Update_Element_Preserving_Key;
841 function Has_Element (Position : Cursor) return Boolean is
843 return Position /= No_Element;
850 procedure Include (Container : in out Set; New_Item : Element_Type) is
855 Insert (Container, New_Item, Position, Inserted);
858 if Container.Tree.Lock > 0 then
859 raise Program_Error with
860 "attempt to tamper with cursors (set is locked)";
863 Position.Node.Element := New_Item;
872 (Container : in out Set;
873 New_Item : Element_Type;
874 Position : out Cursor;
875 Inserted : out Boolean)
884 Position.Container := Container'Unrestricted_Access;
888 (Container : in out Set;
889 New_Item : Element_Type)
892 pragma Unreferenced (Position);
897 Insert (Container, New_Item, Position, Inserted);
900 raise Constraint_Error with
901 "attempt to insert element already in set";
905 ----------------------
906 -- Insert_Sans_Hint --
907 ----------------------
909 procedure Insert_Sans_Hint
910 (Tree : in out Tree_Type;
911 New_Item : Element_Type;
912 Node : out Node_Access;
913 Inserted : out Boolean)
915 function New_Node return Node_Access;
916 pragma Inline (New_Node);
918 procedure Insert_Post is
919 new Element_Keys.Generic_Insert_Post (New_Node);
921 procedure Conditional_Insert_Sans_Hint is
922 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
928 function New_Node return Node_Access is
930 return new Node_Type'(Parent => null,
933 Color => Red_Black_Trees.Red,
934 Element => New_Item);
937 -- Start of processing for Insert_Sans_Hint
940 Conditional_Insert_Sans_Hint
945 end Insert_Sans_Hint;
947 ----------------------
948 -- Insert_With_Hint --
949 ----------------------
951 procedure Insert_With_Hint
952 (Dst_Tree : in out Tree_Type;
953 Dst_Hint : Node_Access;
954 Src_Node : Node_Access;
955 Dst_Node : out Node_Access)
958 pragma Unreferenced (Success);
960 function New_Node return Node_Access;
961 pragma Inline (New_Node);
963 procedure Insert_Post is
964 new Element_Keys.Generic_Insert_Post (New_Node);
966 procedure Insert_Sans_Hint is
967 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
969 procedure Local_Insert_With_Hint is
970 new Element_Keys.Generic_Conditional_Insert_With_Hint
978 function New_Node return Node_Access is
979 Node : constant Node_Access :=
980 new Node_Type'(Parent => null,
984 Element => Src_Node.Element);
989 -- Start of processing for Insert_With_Hint
992 Local_Insert_With_Hint
998 end Insert_With_Hint;
1004 procedure Intersection (Target : in out Set; Source : Set) is
1006 Set_Ops.Intersection (Target.Tree, Source.Tree);
1009 function Intersection (Left, Right : Set) return Set is
1010 Tree : constant Tree_Type :=
1011 Set_Ops.Intersection (Left.Tree, Right.Tree);
1013 return Set'(Controlled with Tree);
1020 function Is_Empty (Container : Set) return Boolean is
1022 return Container.Tree.Length = 0;
1025 ------------------------
1026 -- Is_Equal_Node_Node --
1027 ------------------------
1029 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
1031 return L.Element = R.Element;
1032 end Is_Equal_Node_Node;
1034 -----------------------------
1035 -- Is_Greater_Element_Node --
1036 -----------------------------
1038 function Is_Greater_Element_Node
1039 (Left : Element_Type;
1040 Right : Node_Access) return Boolean
1043 -- Compute e > node same as node < e
1045 return Right.Element < Left;
1046 end Is_Greater_Element_Node;
1048 --------------------------
1049 -- Is_Less_Element_Node --
1050 --------------------------
1052 function Is_Less_Element_Node
1053 (Left : Element_Type;
1054 Right : Node_Access) return Boolean
1057 return Left < Right.Element;
1058 end Is_Less_Element_Node;
1060 -----------------------
1061 -- Is_Less_Node_Node --
1062 -----------------------
1064 function Is_Less_Node_Node (L, R : Node_Access) return Boolean is
1066 return L.Element < R.Element;
1067 end Is_Less_Node_Node;
1073 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1075 return Set_Ops.Is_Subset (Subset => Subset.Tree, Of_Set => Of_Set.Tree);
1084 Process : not null access procedure (Position : Cursor))
1086 procedure Process_Node (Node : Node_Access);
1087 pragma Inline (Process_Node);
1089 procedure Local_Iterate is
1090 new Tree_Operations.Generic_Iteration (Process_Node);
1096 procedure Process_Node (Node : Node_Access) is
1098 Process (Cursor'(Container'Unrestricted_Access, Node));
1101 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1102 B : Natural renames T.Busy;
1104 -- Start of processing for Iterate
1124 function Last (Container : Set) return Cursor is
1126 if Container.Tree.Last = null then
1130 return Cursor'(Container'Unrestricted_Access, Container.Tree.Last);
1137 function Last_Element (Container : Set) return Element_Type is
1139 if Container.Tree.Last = null then
1140 raise Constraint_Error with "set is empty";
1143 return Container.Tree.Last.Element;
1150 function Left (Node : Node_Access) return Node_Access is
1159 function Length (Container : Set) return Count_Type is
1161 return Container.Tree.Length;
1169 new Tree_Operations.Generic_Move (Clear);
1171 procedure Move (Target : in out Set; Source : in out Set) is
1173 Move (Target => Target.Tree, Source => Source.Tree);
1180 function Next (Position : Cursor) return Cursor is
1182 if Position = No_Element then
1186 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1187 "bad cursor in Next");
1190 Node : constant Node_Access :=
1191 Tree_Operations.Next (Position.Node);
1198 return Cursor'(Position.Container, Node);
1202 procedure Next (Position : in out Cursor) is
1204 Position := Next (Position);
1211 function Overlap (Left, Right : Set) return Boolean is
1213 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1220 function Parent (Node : Node_Access) return Node_Access is
1229 function Previous (Position : Cursor) return Cursor is
1231 if Position = No_Element then
1235 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1236 "bad cursor in Previous");
1239 Node : constant Node_Access :=
1240 Tree_Operations.Previous (Position.Node);
1247 return Cursor'(Position.Container, Node);
1251 procedure Previous (Position : in out Cursor) is
1253 Position := Previous (Position);
1260 procedure Query_Element
1262 Process : not null access procedure (Element : Element_Type))
1265 if Position.Node = null then
1266 raise Constraint_Error with "Position cursor equals No_Element";
1269 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1270 "bad cursor in Query_Element");
1273 T : Tree_Type renames Position.Container.Tree;
1275 B : Natural renames T.Busy;
1276 L : Natural renames T.Lock;
1283 Process (Position.Node.Element);
1301 (Stream : not null access Root_Stream_Type'Class;
1302 Container : out Set)
1305 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1306 pragma Inline (Read_Node);
1309 new Tree_Operations.Generic_Read (Clear, Read_Node);
1316 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1318 Node : Node_Access := new Node_Type;
1321 Element_Type'Read (Stream, Node.Element);
1330 -- Start of processing for Read
1333 Read (Stream, Container.Tree);
1337 (Stream : not null access Root_Stream_Type'Class;
1341 raise Program_Error with "attempt to stream set cursor";
1348 procedure Replace (Container : in out Set; New_Item : Element_Type) is
1349 Node : constant Node_Access :=
1350 Element_Keys.Find (Container.Tree, New_Item);
1354 raise Constraint_Error with
1355 "attempt to replace element not in set";
1358 if Container.Tree.Lock > 0 then
1359 raise Program_Error with
1360 "attempt to tamper with cursors (set is locked)";
1363 Node.Element := New_Item;
1366 ---------------------
1367 -- Replace_Element --
1368 ---------------------
1370 procedure Replace_Element
1371 (Tree : in out Tree_Type;
1373 Item : Element_Type)
1375 pragma Assert (Node /= null);
1377 function New_Node return Node_Access;
1378 pragma Inline (New_Node);
1380 procedure Local_Insert_Post is
1381 new Element_Keys.Generic_Insert_Post (New_Node);
1383 procedure Local_Insert_Sans_Hint is
1384 new Element_Keys.Generic_Conditional_Insert (Local_Insert_Post);
1386 procedure Local_Insert_With_Hint is
1387 new Element_Keys.Generic_Conditional_Insert_With_Hint
1389 Local_Insert_Sans_Hint);
1395 function New_Node return Node_Access is
1397 Node.Element := Item;
1399 Node.Parent := null;
1407 Result : Node_Access;
1410 -- Start of processing for Insert
1413 if Item < Node.Element
1414 or else Node.Element < Item
1419 if Tree.Lock > 0 then
1420 raise Program_Error with
1421 "attempt to tamper with cursors (set is locked)";
1424 Node.Element := Item;
1428 Hint := Element_Keys.Ceiling (Tree, Item);
1433 elsif Item < Hint.Element then
1435 if Tree.Lock > 0 then
1436 raise Program_Error with
1437 "attempt to tamper with cursors (set is locked)";
1440 Node.Element := Item;
1445 pragma Assert (not (Hint.Element < Item));
1446 raise Program_Error with "attempt to replace existing element";
1449 Tree_Operations.Delete_Node_Sans_Free (Tree, Node); -- Checks busy-bit
1451 Local_Insert_With_Hint
1456 Inserted => Inserted);
1458 pragma Assert (Inserted);
1459 pragma Assert (Result = Node);
1460 end Replace_Element;
1462 procedure Replace_Element
1463 (Container : in out Set;
1465 New_Item : Element_Type)
1468 if Position.Node = null then
1469 raise Constraint_Error with
1470 "Position cursor equals No_Element";
1473 if Position.Container /= Container'Unrestricted_Access then
1474 raise Program_Error with
1475 "Position cursor designates wrong set";
1478 pragma Assert (Vet (Container.Tree, Position.Node),
1479 "bad cursor in Replace_Element");
1481 Replace_Element (Container.Tree, Position.Node, New_Item);
1482 end Replace_Element;
1484 ---------------------
1485 -- Reverse_Iterate --
1486 ---------------------
1488 procedure Reverse_Iterate
1490 Process : not null access procedure (Position : Cursor))
1492 procedure Process_Node (Node : Node_Access);
1493 pragma Inline (Process_Node);
1495 procedure Local_Reverse_Iterate is
1496 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1502 procedure Process_Node (Node : Node_Access) is
1504 Process (Cursor'(Container'Unrestricted_Access, Node));
1507 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1508 B : Natural renames T.Busy;
1510 -- Start of processing for Reverse_Iterate
1516 Local_Reverse_Iterate (T);
1524 end Reverse_Iterate;
1530 function Right (Node : Node_Access) return Node_Access is
1539 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1541 Node.Color := Color;
1548 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1557 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1559 Node.Parent := Parent;
1566 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1568 Node.Right := Right;
1571 --------------------------
1572 -- Symmetric_Difference --
1573 --------------------------
1575 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1577 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
1578 end Symmetric_Difference;
1580 function Symmetric_Difference (Left, Right : Set) return Set is
1581 Tree : constant Tree_Type :=
1582 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
1584 return Set'(Controlled with Tree);
1585 end Symmetric_Difference;
1591 function To_Set (New_Item : Element_Type) return Set is
1595 pragma Unreferenced (Node, Inserted);
1597 Insert_Sans_Hint (Tree, New_Item, Node, Inserted);
1598 return Set'(Controlled with Tree);
1605 procedure Union (Target : in out Set; Source : Set) is
1607 Set_Ops.Union (Target.Tree, Source.Tree);
1610 function Union (Left, Right : Set) return Set is
1611 Tree : constant Tree_Type :=
1612 Set_Ops.Union (Left.Tree, Right.Tree);
1614 return Set'(Controlled with Tree);
1622 (Stream : not null access Root_Stream_Type'Class;
1625 procedure Write_Node
1626 (Stream : not null access Root_Stream_Type'Class;
1627 Node : Node_Access);
1628 pragma Inline (Write_Node);
1631 new Tree_Operations.Generic_Write (Write_Node);
1637 procedure Write_Node
1638 (Stream : not null access Root_Stream_Type'Class;
1642 Element_Type'Write (Stream, Node.Element);
1645 -- Start of processing for Write
1648 Write (Stream, Container.Tree);
1652 (Stream : not null access Root_Stream_Type'Class;
1656 raise Program_Error with "attempt to stream set cursor";
1659 end Ada.Containers.Ordered_Sets;