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-2009, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- This unit was originally developed by Matthew J Heaney. --
28 ------------------------------------------------------------------------------
30 with Ada.Unchecked_Deallocation;
32 with Ada.Containers.Red_Black_Trees.Generic_Operations;
33 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Operations);
35 with Ada.Containers.Red_Black_Trees.Generic_Keys;
36 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Keys);
38 with Ada.Containers.Red_Black_Trees.Generic_Set_Operations;
39 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Set_Operations);
41 package body Ada.Containers.Ordered_Sets is
43 ------------------------------
44 -- Access to Fields of Node --
45 ------------------------------
47 -- These subprograms provide functional notation for access to fields
48 -- of a node, and procedural notation for modifying these fields.
50 function Color (Node : Node_Access) return Color_Type;
51 pragma Inline (Color);
53 function Left (Node : Node_Access) return Node_Access;
56 function Parent (Node : Node_Access) return Node_Access;
57 pragma Inline (Parent);
59 function Right (Node : Node_Access) return Node_Access;
60 pragma Inline (Right);
62 procedure Set_Color (Node : Node_Access; Color : Color_Type);
63 pragma Inline (Set_Color);
65 procedure Set_Left (Node : Node_Access; Left : Node_Access);
66 pragma Inline (Set_Left);
68 procedure Set_Right (Node : Node_Access; Right : Node_Access);
69 pragma Inline (Set_Right);
71 procedure Set_Parent (Node : Node_Access; Parent : Node_Access);
72 pragma Inline (Set_Parent);
74 -----------------------
75 -- Local Subprograms --
76 -----------------------
78 function Copy_Node (Source : Node_Access) return Node_Access;
79 pragma Inline (Copy_Node);
81 procedure Free (X : in out Node_Access);
83 procedure Insert_Sans_Hint
84 (Tree : in out Tree_Type;
85 New_Item : Element_Type;
86 Node : out Node_Access;
87 Inserted : out Boolean);
89 procedure Insert_With_Hint
90 (Dst_Tree : in out Tree_Type;
91 Dst_Hint : Node_Access;
92 Src_Node : Node_Access;
93 Dst_Node : out Node_Access);
95 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean;
96 pragma Inline (Is_Equal_Node_Node);
98 function Is_Greater_Element_Node
100 Right : Node_Access) return Boolean;
101 pragma Inline (Is_Greater_Element_Node);
103 function Is_Less_Element_Node
104 (Left : Element_Type;
105 Right : Node_Access) return Boolean;
106 pragma Inline (Is_Less_Element_Node);
108 function Is_Less_Node_Node (L, R : Node_Access) return Boolean;
109 pragma Inline (Is_Less_Node_Node);
111 procedure Replace_Element
112 (Tree : in out Tree_Type;
114 Item : Element_Type);
116 --------------------------
117 -- Local Instantiations --
118 --------------------------
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);
132 new Tree_Operations.Generic_Equal (Is_Equal_Node_Node);
134 package Element_Keys is
135 new Red_Black_Trees.Generic_Keys
136 (Tree_Operations => Tree_Operations,
137 Key_Type => Element_Type,
138 Is_Less_Key_Node => Is_Less_Element_Node,
139 Is_Greater_Key_Node => Is_Greater_Element_Node);
142 new Generic_Set_Operations
143 (Tree_Operations => Tree_Operations,
144 Insert_With_Hint => Insert_With_Hint,
145 Copy_Tree => Copy_Tree,
146 Delete_Tree => Delete_Tree,
147 Is_Less => Is_Less_Node_Node,
154 function "<" (Left, Right : Cursor) return Boolean is
156 if Left.Node = null then
157 raise Constraint_Error with "Left cursor equals No_Element";
160 if Right.Node = null then
161 raise Constraint_Error with "Right cursor equals No_Element";
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 < Right.Node.Element;
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 pragma Assert (Vet (Left.Container.Tree, Left.Node),
180 "bad Left cursor in ""<""");
182 return Left.Node.Element < Right;
185 function "<" (Left : Element_Type; Right : Cursor) return Boolean is
187 if Right.Node = null then
188 raise Constraint_Error with "Right cursor equals No_Element";
191 pragma Assert (Vet (Right.Container.Tree, Right.Node),
192 "bad Right cursor in ""<""");
194 return Left < Right.Node.Element;
201 function "=" (Left, Right : Set) return Boolean is
203 return Is_Equal (Left.Tree, Right.Tree);
210 function ">" (Left, Right : Cursor) return Boolean is
212 if Left.Node = null then
213 raise Constraint_Error with "Left cursor equals No_Element";
216 if Right.Node = null then
217 raise Constraint_Error with "Right cursor equals No_Element";
220 pragma Assert (Vet (Left.Container.Tree, Left.Node),
221 "bad Left cursor in "">""");
223 pragma Assert (Vet (Right.Container.Tree, Right.Node),
224 "bad Right cursor in "">""");
226 -- L > R same as R < L
228 return Right.Node.Element < Left.Node.Element;
231 function ">" (Left : Element_Type; Right : Cursor) return Boolean is
233 if Right.Node = null then
234 raise Constraint_Error with "Right cursor equals No_Element";
237 pragma Assert (Vet (Right.Container.Tree, Right.Node),
238 "bad Right cursor in "">""");
240 return Right.Node.Element < Left;
243 function ">" (Left : Cursor; Right : Element_Type) return Boolean is
245 if Left.Node = null then
246 raise Constraint_Error with "Left cursor equals No_Element";
249 pragma Assert (Vet (Left.Container.Tree, Left.Node),
250 "bad Left cursor in "">""");
252 return Right < Left.Node.Element;
259 procedure Adjust is new Tree_Operations.Generic_Adjust (Copy_Tree);
261 procedure Adjust (Container : in out Set) is
263 Adjust (Container.Tree);
270 function Ceiling (Container : Set; Item : Element_Type) return Cursor is
271 Node : constant Node_Access :=
272 Element_Keys.Ceiling (Container.Tree, Item);
279 return Cursor'(Container'Unrestricted_Access, Node);
286 procedure Clear is new Tree_Operations.Generic_Clear (Delete_Tree);
288 procedure Clear (Container : in out Set) is
290 Clear (Container.Tree);
297 function Color (Node : Node_Access) return Color_Type is
308 Item : Element_Type) return Boolean
311 return Find (Container, Item) /= No_Element;
318 function Copy_Node (Source : Node_Access) return Node_Access is
319 Target : constant Node_Access :=
320 new Node_Type'(Parent => null,
323 Color => Source.Color,
324 Element => Source.Element);
333 procedure Delete (Container : in out Set; Position : in out Cursor) is
335 if Position.Node = null then
336 raise Constraint_Error with "Position cursor equals No_Element";
339 if Position.Container /= Container'Unrestricted_Access then
340 raise Program_Error with "Position cursor designates wrong set";
343 pragma Assert (Vet (Container.Tree, Position.Node),
344 "bad cursor in Delete");
346 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
347 Free (Position.Node);
348 Position.Container := null;
351 procedure Delete (Container : in out Set; Item : Element_Type) is
352 X : Node_Access := Element_Keys.Find (Container.Tree, Item);
356 raise Constraint_Error with "attempt to delete element not in set";
359 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
367 procedure Delete_First (Container : in out Set) is
368 Tree : Tree_Type renames Container.Tree;
369 X : Node_Access := Tree.First;
373 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
382 procedure Delete_Last (Container : in out Set) is
383 Tree : Tree_Type renames Container.Tree;
384 X : Node_Access := Tree.Last;
388 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
397 procedure Difference (Target : in out Set; Source : Set) is
399 Set_Ops.Difference (Target.Tree, Source.Tree);
402 function Difference (Left, Right : Set) return Set is
403 Tree : constant Tree_Type :=
404 Set_Ops.Difference (Left.Tree, Right.Tree);
406 return Set'(Controlled with Tree);
413 function Element (Position : Cursor) return Element_Type is
415 if Position.Node = null then
416 raise Constraint_Error with "Position cursor equals No_Element";
419 pragma Assert (Vet (Position.Container.Tree, Position.Node),
420 "bad cursor in Element");
422 return Position.Node.Element;
425 -------------------------
426 -- Equivalent_Elements --
427 -------------------------
429 function Equivalent_Elements (Left, Right : Element_Type) return Boolean is
438 end Equivalent_Elements;
440 ---------------------
441 -- Equivalent_Sets --
442 ---------------------
444 function Equivalent_Sets (Left, Right : Set) return Boolean is
445 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean;
446 pragma Inline (Is_Equivalent_Node_Node);
448 function Is_Equivalent is
449 new Tree_Operations.Generic_Equal (Is_Equivalent_Node_Node);
451 -----------------------------
452 -- Is_Equivalent_Node_Node --
453 -----------------------------
455 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean is
457 if L.Element < R.Element then
459 elsif R.Element < L.Element then
464 end Is_Equivalent_Node_Node;
466 -- Start of processing for Equivalent_Sets
469 return Is_Equivalent (Left.Tree, Right.Tree);
476 procedure Exclude (Container : in out Set; Item : Element_Type) is
477 X : Node_Access := Element_Keys.Find (Container.Tree, Item);
481 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
490 function Find (Container : Set; Item : Element_Type) return Cursor is
491 Node : constant Node_Access :=
492 Element_Keys.Find (Container.Tree, Item);
499 return Cursor'(Container'Unrestricted_Access, Node);
506 function First (Container : Set) return Cursor is
508 if Container.Tree.First = null then
512 return Cursor'(Container'Unrestricted_Access, Container.Tree.First);
519 function First_Element (Container : Set) return Element_Type is
521 if Container.Tree.First = null then
522 raise Constraint_Error with "set is empty";
525 return Container.Tree.First.Element;
532 function Floor (Container : Set; Item : Element_Type) return Cursor is
533 Node : constant Node_Access :=
534 Element_Keys.Floor (Container.Tree, Item);
541 return Cursor'(Container'Unrestricted_Access, Node);
548 procedure Free (X : in out Node_Access) is
549 procedure Deallocate is
550 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
566 package body Generic_Keys is
568 -----------------------
569 -- Local Subprograms --
570 -----------------------
572 function Is_Greater_Key_Node
574 Right : Node_Access) return Boolean;
575 pragma Inline (Is_Greater_Key_Node);
577 function Is_Less_Key_Node
579 Right : Node_Access) return Boolean;
580 pragma Inline (Is_Less_Key_Node);
582 --------------------------
583 -- Local Instantiations --
584 --------------------------
587 new Red_Black_Trees.Generic_Keys
588 (Tree_Operations => Tree_Operations,
589 Key_Type => Key_Type,
590 Is_Less_Key_Node => Is_Less_Key_Node,
591 Is_Greater_Key_Node => Is_Greater_Key_Node);
597 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
598 Node : constant Node_Access :=
599 Key_Keys.Ceiling (Container.Tree, Key);
606 return Cursor'(Container'Unrestricted_Access, Node);
613 function Contains (Container : Set; Key : Key_Type) return Boolean is
615 return Find (Container, Key) /= No_Element;
622 procedure Delete (Container : in out Set; Key : Key_Type) is
623 X : Node_Access := Key_Keys.Find (Container.Tree, Key);
627 raise Constraint_Error with "attempt to delete key not in set";
630 Delete_Node_Sans_Free (Container.Tree, X);
638 function Element (Container : Set; Key : Key_Type) return Element_Type is
639 Node : constant Node_Access :=
640 Key_Keys.Find (Container.Tree, Key);
644 raise Constraint_Error with "key not in set";
650 ---------------------
651 -- Equivalent_Keys --
652 ---------------------
654 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
669 procedure Exclude (Container : in out Set; Key : Key_Type) is
670 X : Node_Access := Key_Keys.Find (Container.Tree, Key);
674 Delete_Node_Sans_Free (Container.Tree, X);
683 function Find (Container : Set; Key : Key_Type) return Cursor is
684 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
691 return Cursor'(Container'Unrestricted_Access, Node);
698 function Floor (Container : Set; Key : Key_Type) return Cursor is
699 Node : constant Node_Access := Key_Keys.Floor (Container.Tree, Key);
706 return Cursor'(Container'Unrestricted_Access, Node);
709 -------------------------
710 -- Is_Greater_Key_Node --
711 -------------------------
713 function Is_Greater_Key_Node
715 Right : Node_Access) return Boolean
718 return Key (Right.Element) < Left;
719 end Is_Greater_Key_Node;
721 ----------------------
722 -- Is_Less_Key_Node --
723 ----------------------
725 function Is_Less_Key_Node
727 Right : Node_Access) return Boolean
730 return Left < Key (Right.Element);
731 end Is_Less_Key_Node;
737 function Key (Position : Cursor) return Key_Type is
739 if Position.Node = null then
740 raise Constraint_Error with
741 "Position cursor equals No_Element";
744 pragma Assert (Vet (Position.Container.Tree, Position.Node),
745 "bad cursor in Key");
747 return Key (Position.Node.Element);
755 (Container : in out Set;
757 New_Item : Element_Type)
759 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
763 raise Constraint_Error with
764 "attempt to replace key not in set";
767 Replace_Element (Container.Tree, Node, New_Item);
770 -----------------------------------
771 -- Update_Element_Preserving_Key --
772 -----------------------------------
774 procedure Update_Element_Preserving_Key
775 (Container : in out Set;
777 Process : not null access procedure (Element : in out Element_Type))
779 Tree : Tree_Type renames Container.Tree;
782 if Position.Node = null then
783 raise Constraint_Error with
784 "Position cursor equals No_Element";
787 if Position.Container /= Container'Unrestricted_Access then
788 raise Program_Error with
789 "Position cursor designates wrong set";
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);
830 raise Program_Error with "key was modified";
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
857 raise Program_Error with
858 "attempt to tamper with cursors (set is locked)";
861 Position.Node.Element := New_Item;
870 (Container : in out Set;
871 New_Item : Element_Type;
872 Position : out Cursor;
873 Inserted : out Boolean)
882 Position.Container := Container'Unrestricted_Access;
886 (Container : in out Set;
887 New_Item : Element_Type)
890 pragma Unreferenced (Position);
895 Insert (Container, New_Item, Position, Inserted);
898 raise Constraint_Error with
899 "attempt to insert element already in set";
903 ----------------------
904 -- Insert_Sans_Hint --
905 ----------------------
907 procedure Insert_Sans_Hint
908 (Tree : in out Tree_Type;
909 New_Item : Element_Type;
910 Node : out Node_Access;
911 Inserted : out Boolean)
913 function New_Node return Node_Access;
914 pragma Inline (New_Node);
916 procedure Insert_Post is
917 new Element_Keys.Generic_Insert_Post (New_Node);
919 procedure Conditional_Insert_Sans_Hint is
920 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
926 function New_Node return Node_Access is
928 return new Node_Type'(Parent => null,
931 Color => Red_Black_Trees.Red,
932 Element => New_Item);
935 -- Start of processing for Insert_Sans_Hint
938 Conditional_Insert_Sans_Hint
943 end Insert_Sans_Hint;
945 ----------------------
946 -- Insert_With_Hint --
947 ----------------------
949 procedure Insert_With_Hint
950 (Dst_Tree : in out Tree_Type;
951 Dst_Hint : Node_Access;
952 Src_Node : Node_Access;
953 Dst_Node : out Node_Access)
956 pragma Unreferenced (Success);
958 function New_Node return Node_Access;
959 pragma Inline (New_Node);
961 procedure Insert_Post is
962 new Element_Keys.Generic_Insert_Post (New_Node);
964 procedure Insert_Sans_Hint is
965 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
967 procedure Local_Insert_With_Hint is
968 new Element_Keys.Generic_Conditional_Insert_With_Hint
976 function New_Node return Node_Access is
977 Node : constant Node_Access :=
978 new Node_Type'(Parent => null,
982 Element => Src_Node.Element);
987 -- Start of processing for Insert_With_Hint
990 Local_Insert_With_Hint
996 end Insert_With_Hint;
1002 procedure Intersection (Target : in out Set; Source : Set) is
1004 Set_Ops.Intersection (Target.Tree, Source.Tree);
1007 function Intersection (Left, Right : Set) return Set is
1008 Tree : constant Tree_Type :=
1009 Set_Ops.Intersection (Left.Tree, Right.Tree);
1011 return Set'(Controlled with Tree);
1018 function Is_Empty (Container : Set) return Boolean is
1020 return Container.Tree.Length = 0;
1023 ------------------------
1024 -- Is_Equal_Node_Node --
1025 ------------------------
1027 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
1029 return L.Element = R.Element;
1030 end Is_Equal_Node_Node;
1032 -----------------------------
1033 -- Is_Greater_Element_Node --
1034 -----------------------------
1036 function Is_Greater_Element_Node
1037 (Left : Element_Type;
1038 Right : Node_Access) return Boolean
1041 -- Compute e > node same as node < e
1043 return Right.Element < Left;
1044 end Is_Greater_Element_Node;
1046 --------------------------
1047 -- Is_Less_Element_Node --
1048 --------------------------
1050 function Is_Less_Element_Node
1051 (Left : Element_Type;
1052 Right : Node_Access) return Boolean
1055 return Left < Right.Element;
1056 end Is_Less_Element_Node;
1058 -----------------------
1059 -- Is_Less_Node_Node --
1060 -----------------------
1062 function Is_Less_Node_Node (L, R : Node_Access) return Boolean is
1064 return L.Element < R.Element;
1065 end Is_Less_Node_Node;
1071 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1073 return Set_Ops.Is_Subset (Subset => Subset.Tree, Of_Set => Of_Set.Tree);
1082 Process : not null access procedure (Position : Cursor))
1084 procedure Process_Node (Node : Node_Access);
1085 pragma Inline (Process_Node);
1087 procedure Local_Iterate is
1088 new Tree_Operations.Generic_Iteration (Process_Node);
1094 procedure Process_Node (Node : Node_Access) is
1096 Process (Cursor'(Container'Unrestricted_Access, Node));
1099 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1100 B : Natural renames T.Busy;
1102 -- Start of processing for Iterate
1122 function Last (Container : Set) return Cursor is
1124 if Container.Tree.Last = null then
1128 return Cursor'(Container'Unrestricted_Access, Container.Tree.Last);
1135 function Last_Element (Container : Set) return Element_Type is
1137 if Container.Tree.Last = null then
1138 raise Constraint_Error with "set is empty";
1141 return Container.Tree.Last.Element;
1148 function Left (Node : Node_Access) return Node_Access is
1157 function Length (Container : Set) return Count_Type is
1159 return Container.Tree.Length;
1167 new Tree_Operations.Generic_Move (Clear);
1169 procedure Move (Target : in out Set; Source : in out Set) is
1171 Move (Target => Target.Tree, Source => Source.Tree);
1178 function Next (Position : Cursor) return Cursor is
1180 if Position = No_Element then
1184 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1185 "bad cursor in Next");
1188 Node : constant Node_Access :=
1189 Tree_Operations.Next (Position.Node);
1196 return Cursor'(Position.Container, Node);
1200 procedure Next (Position : in out Cursor) is
1202 Position := Next (Position);
1209 function Overlap (Left, Right : Set) return Boolean is
1211 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1218 function Parent (Node : Node_Access) return Node_Access is
1227 function Previous (Position : Cursor) return Cursor is
1229 if Position = No_Element then
1233 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1234 "bad cursor in Previous");
1237 Node : constant Node_Access :=
1238 Tree_Operations.Previous (Position.Node);
1245 return Cursor'(Position.Container, Node);
1249 procedure Previous (Position : in out Cursor) is
1251 Position := Previous (Position);
1258 procedure Query_Element
1260 Process : not null access procedure (Element : Element_Type))
1263 if Position.Node = null then
1264 raise Constraint_Error with "Position cursor equals No_Element";
1267 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1268 "bad cursor in Query_Element");
1271 T : Tree_Type renames Position.Container.Tree;
1273 B : Natural renames T.Busy;
1274 L : Natural renames T.Lock;
1281 Process (Position.Node.Element);
1299 (Stream : not null access Root_Stream_Type'Class;
1300 Container : out Set)
1303 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1304 pragma Inline (Read_Node);
1307 new Tree_Operations.Generic_Read (Clear, Read_Node);
1314 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1316 Node : Node_Access := new Node_Type;
1319 Element_Type'Read (Stream, Node.Element);
1328 -- Start of processing for Read
1331 Read (Stream, Container.Tree);
1335 (Stream : not null access Root_Stream_Type'Class;
1339 raise Program_Error with "attempt to stream set cursor";
1346 procedure Replace (Container : in out Set; New_Item : Element_Type) is
1347 Node : constant Node_Access :=
1348 Element_Keys.Find (Container.Tree, New_Item);
1352 raise Constraint_Error with
1353 "attempt to replace element not in set";
1356 if Container.Tree.Lock > 0 then
1357 raise Program_Error with
1358 "attempt to tamper with cursors (set is locked)";
1361 Node.Element := New_Item;
1364 ---------------------
1365 -- Replace_Element --
1366 ---------------------
1368 procedure Replace_Element
1369 (Tree : in out Tree_Type;
1371 Item : Element_Type)
1373 pragma Assert (Node /= null);
1375 function New_Node return Node_Access;
1376 pragma Inline (New_Node);
1378 procedure Local_Insert_Post is
1379 new Element_Keys.Generic_Insert_Post (New_Node);
1381 procedure Local_Insert_Sans_Hint is
1382 new Element_Keys.Generic_Conditional_Insert (Local_Insert_Post);
1384 procedure Local_Insert_With_Hint is
1385 new Element_Keys.Generic_Conditional_Insert_With_Hint
1387 Local_Insert_Sans_Hint);
1393 function New_Node return Node_Access is
1395 Node.Element := Item;
1397 Node.Parent := null;
1405 Result : Node_Access;
1408 -- Start of processing for Insert
1411 if Item < Node.Element
1412 or else Node.Element < Item
1417 if Tree.Lock > 0 then
1418 raise Program_Error with
1419 "attempt to tamper with cursors (set is locked)";
1422 Node.Element := Item;
1426 Hint := Element_Keys.Ceiling (Tree, Item);
1431 elsif Item < Hint.Element then
1433 if Tree.Lock > 0 then
1434 raise Program_Error with
1435 "attempt to tamper with cursors (set is locked)";
1438 Node.Element := Item;
1443 pragma Assert (not (Hint.Element < Item));
1444 raise Program_Error with "attempt to replace existing element";
1447 Tree_Operations.Delete_Node_Sans_Free (Tree, Node); -- Checks busy-bit
1449 Local_Insert_With_Hint
1454 Inserted => Inserted);
1456 pragma Assert (Inserted);
1457 pragma Assert (Result = Node);
1458 end Replace_Element;
1460 procedure Replace_Element
1461 (Container : in out Set;
1463 New_Item : Element_Type)
1466 if Position.Node = null then
1467 raise Constraint_Error with
1468 "Position cursor equals No_Element";
1471 if Position.Container /= Container'Unrestricted_Access then
1472 raise Program_Error with
1473 "Position cursor designates wrong set";
1476 pragma Assert (Vet (Container.Tree, Position.Node),
1477 "bad cursor in Replace_Element");
1479 Replace_Element (Container.Tree, Position.Node, New_Item);
1480 end Replace_Element;
1482 ---------------------
1483 -- Reverse_Iterate --
1484 ---------------------
1486 procedure Reverse_Iterate
1488 Process : not null access procedure (Position : Cursor))
1490 procedure Process_Node (Node : Node_Access);
1491 pragma Inline (Process_Node);
1493 procedure Local_Reverse_Iterate is
1494 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1500 procedure Process_Node (Node : Node_Access) is
1502 Process (Cursor'(Container'Unrestricted_Access, Node));
1505 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1506 B : Natural renames T.Busy;
1508 -- Start of processing for Reverse_Iterate
1514 Local_Reverse_Iterate (T);
1522 end Reverse_Iterate;
1528 function Right (Node : Node_Access) return Node_Access is
1537 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1539 Node.Color := Color;
1546 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1555 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1557 Node.Parent := Parent;
1564 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1566 Node.Right := Right;
1569 --------------------------
1570 -- Symmetric_Difference --
1571 --------------------------
1573 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1575 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
1576 end Symmetric_Difference;
1578 function Symmetric_Difference (Left, Right : Set) return Set is
1579 Tree : constant Tree_Type :=
1580 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
1582 return Set'(Controlled with Tree);
1583 end Symmetric_Difference;
1589 function To_Set (New_Item : Element_Type) return Set is
1593 pragma Unreferenced (Node, Inserted);
1595 Insert_Sans_Hint (Tree, New_Item, Node, Inserted);
1596 return Set'(Controlled with Tree);
1603 procedure Union (Target : in out Set; Source : Set) is
1605 Set_Ops.Union (Target.Tree, Source.Tree);
1608 function Union (Left, Right : Set) return Set is
1609 Tree : constant Tree_Type :=
1610 Set_Ops.Union (Left.Tree, Right.Tree);
1612 return Set'(Controlled with Tree);
1620 (Stream : not null access Root_Stream_Type'Class;
1623 procedure Write_Node
1624 (Stream : not null access Root_Stream_Type'Class;
1625 Node : Node_Access);
1626 pragma Inline (Write_Node);
1629 new Tree_Operations.Generic_Write (Write_Node);
1635 procedure Write_Node
1636 (Stream : not null access Root_Stream_Type'Class;
1640 Element_Type'Write (Stream, Node.Element);
1643 -- Start of processing for Write
1646 Write (Stream, Container.Tree);
1650 (Stream : not null access Root_Stream_Type'Class;
1654 raise Program_Error with "attempt to stream set cursor";
1657 end Ada.Containers.Ordered_Sets;