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-2006, 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 modifiying 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;
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 with "Position cursor equals No_Element";
343 if Position.Container /= Container'Unrestricted_Access then
344 raise Program_Error with "Position cursor designates wrong set";
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 with "attempt to delete element not in set";
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 with "Position cursor equals No_Element";
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 with "set is empty";
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 with "attempt to delete key not in set";
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 with "key not in set";
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 with
745 "Position cursor equals No_Element";
748 pragma Assert (Vet (Position.Container.Tree, Position.Node),
749 "bad cursor in Key");
751 return Key (Position.Node.Element);
759 (Container : in out Set;
761 New_Item : Element_Type)
763 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
767 raise Constraint_Error with
768 "attempt to replace key not in set";
771 Replace_Element (Container.Tree, Node, New_Item);
774 -----------------------------------
775 -- Update_Element_Preserving_Key --
776 -----------------------------------
778 procedure Update_Element_Preserving_Key
779 (Container : in out Set;
781 Process : not null access procedure (Element : in out Element_Type))
783 Tree : Tree_Type renames Container.Tree;
786 if Position.Node = null then
787 raise Constraint_Error with
788 "Position cursor equals No_Element";
791 if Position.Container /= Container'Unrestricted_Access then
792 raise Program_Error with
793 "Position cursor designates wrong set";
796 pragma Assert (Vet (Container.Tree, Position.Node),
797 "bad cursor in Update_Element_Preserving_Key");
800 E : Element_Type renames Position.Node.Element;
801 K : constant Key_Type := Key (E);
803 B : Natural renames Tree.Busy;
804 L : Natural renames Tree.Lock;
822 if Equivalent_Keys (K, Key (E)) then
828 X : Node_Access := Position.Node;
830 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
834 raise Program_Error with "key was modified";
835 end Update_Element_Preserving_Key;
843 function Has_Element (Position : Cursor) return Boolean is
845 return Position /= No_Element;
852 procedure Include (Container : in out Set; New_Item : Element_Type) is
857 Insert (Container, New_Item, Position, Inserted);
860 if Container.Tree.Lock > 0 then
861 raise Program_Error with
862 "attempt to tamper with cursors (set is locked)";
865 Position.Node.Element := New_Item;
874 (Container : in out Set;
875 New_Item : Element_Type;
876 Position : out Cursor;
877 Inserted : out Boolean)
886 Position.Container := Container'Unrestricted_Access;
890 (Container : in out Set;
891 New_Item : Element_Type)
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)
959 function New_Node return Node_Access;
960 pragma Inline (New_Node);
962 procedure Insert_Post is
963 new Element_Keys.Generic_Insert_Post (New_Node);
965 procedure Insert_Sans_Hint is
966 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
968 procedure Local_Insert_With_Hint is
969 new Element_Keys.Generic_Conditional_Insert_With_Hint
977 function New_Node return Node_Access is
978 Node : constant Node_Access :=
979 new Node_Type'(Parent => null,
983 Element => Src_Node.Element);
988 -- Start of processing for Insert_With_Hint
991 Local_Insert_With_Hint
997 end Insert_With_Hint;
1003 procedure Intersection (Target : in out Set; Source : Set) is
1005 Set_Ops.Intersection (Target.Tree, Source.Tree);
1008 function Intersection (Left, Right : Set) return Set is
1009 Tree : constant Tree_Type :=
1010 Set_Ops.Intersection (Left.Tree, Right.Tree);
1012 return Set'(Controlled with Tree);
1019 function Is_Empty (Container : Set) return Boolean is
1021 return Container.Tree.Length = 0;
1024 ------------------------
1025 -- Is_Equal_Node_Node --
1026 ------------------------
1028 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
1030 return L.Element = R.Element;
1031 end Is_Equal_Node_Node;
1033 -----------------------------
1034 -- Is_Greater_Element_Node --
1035 -----------------------------
1037 function Is_Greater_Element_Node
1038 (Left : Element_Type;
1039 Right : Node_Access) return Boolean
1042 -- Compute e > node same as node < e
1044 return Right.Element < Left;
1045 end Is_Greater_Element_Node;
1047 --------------------------
1048 -- Is_Less_Element_Node --
1049 --------------------------
1051 function Is_Less_Element_Node
1052 (Left : Element_Type;
1053 Right : Node_Access) return Boolean
1056 return Left < Right.Element;
1057 end Is_Less_Element_Node;
1059 -----------------------
1060 -- Is_Less_Node_Node --
1061 -----------------------
1063 function Is_Less_Node_Node (L, R : Node_Access) return Boolean is
1065 return L.Element < R.Element;
1066 end Is_Less_Node_Node;
1072 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1074 return Set_Ops.Is_Subset (Subset => Subset.Tree, Of_Set => Of_Set.Tree);
1083 Process : not null access procedure (Position : Cursor))
1085 procedure Process_Node (Node : Node_Access);
1086 pragma Inline (Process_Node);
1088 procedure Local_Iterate is
1089 new Tree_Operations.Generic_Iteration (Process_Node);
1095 procedure Process_Node (Node : Node_Access) is
1097 Process (Cursor'(Container'Unrestricted_Access, Node));
1100 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1101 B : Natural renames T.Busy;
1103 -- Start of prccessing for Iterate
1123 function Last (Container : Set) return Cursor is
1125 if Container.Tree.Last = null then
1129 return Cursor'(Container'Unrestricted_Access, Container.Tree.Last);
1136 function Last_Element (Container : Set) return Element_Type is
1138 if Container.Tree.Last = null then
1139 raise Constraint_Error with "set is empty";
1142 return Container.Tree.Last.Element;
1149 function Left (Node : Node_Access) return Node_Access is
1158 function Length (Container : Set) return Count_Type is
1160 return Container.Tree.Length;
1168 new Tree_Operations.Generic_Move (Clear);
1170 procedure Move (Target : in out Set; Source : in out Set) is
1172 Move (Target => Target.Tree, Source => Source.Tree);
1179 function Next (Position : Cursor) return Cursor is
1181 if Position = No_Element then
1185 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1186 "bad cursor in Next");
1189 Node : constant Node_Access :=
1190 Tree_Operations.Next (Position.Node);
1197 return Cursor'(Position.Container, Node);
1201 procedure Next (Position : in out Cursor) is
1203 Position := Next (Position);
1210 function Overlap (Left, Right : Set) return Boolean is
1212 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1219 function Parent (Node : Node_Access) return Node_Access is
1228 function Previous (Position : Cursor) return Cursor is
1230 if Position = No_Element then
1234 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1235 "bad cursor in Previous");
1238 Node : constant Node_Access :=
1239 Tree_Operations.Previous (Position.Node);
1246 return Cursor'(Position.Container, Node);
1250 procedure Previous (Position : in out Cursor) is
1252 Position := Previous (Position);
1259 procedure Query_Element
1261 Process : not null access procedure (Element : Element_Type))
1264 if Position.Node = null then
1265 raise Constraint_Error with "Position cursor equals No_Element";
1268 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1269 "bad cursor in Query_Element");
1272 T : Tree_Type renames Position.Container.Tree;
1274 B : Natural renames T.Busy;
1275 L : Natural renames T.Lock;
1282 Process (Position.Node.Element);
1300 (Stream : access Root_Stream_Type'Class;
1301 Container : out Set)
1304 (Stream : access Root_Stream_Type'Class) return Node_Access;
1305 pragma Inline (Read_Node);
1308 new Tree_Operations.Generic_Read (Clear, Read_Node);
1315 (Stream : access Root_Stream_Type'Class) return Node_Access
1317 Node : Node_Access := new Node_Type;
1320 Element_Type'Read (Stream, Node.Element);
1329 -- Start of processing for Read
1332 Read (Stream, Container.Tree);
1336 (Stream : access Root_Stream_Type'Class;
1340 raise Program_Error with "attempt to stream set cursor";
1347 procedure Replace (Container : in out Set; New_Item : Element_Type) is
1348 Node : constant Node_Access :=
1349 Element_Keys.Find (Container.Tree, New_Item);
1353 raise Constraint_Error with
1354 "attempt to replace element not in set";
1357 if Container.Tree.Lock > 0 then
1358 raise Program_Error with
1359 "attempt to tamper with cursors (set is locked)";
1362 Node.Element := New_Item;
1365 ---------------------
1366 -- Replace_Element --
1367 ---------------------
1369 procedure Replace_Element
1370 (Tree : in out Tree_Type;
1372 Item : Element_Type)
1374 pragma Assert (Node /= null);
1376 function New_Node return Node_Access;
1377 pragma Inline (New_Node);
1379 procedure Local_Insert_Post is
1380 new Element_Keys.Generic_Insert_Post (New_Node);
1382 procedure Local_Insert_Sans_Hint is
1383 new Element_Keys.Generic_Conditional_Insert (Local_Insert_Post);
1385 procedure Local_Insert_With_Hint is
1386 new Element_Keys.Generic_Conditional_Insert_With_Hint
1388 Local_Insert_Sans_Hint);
1394 function New_Node return Node_Access is
1396 Node.Element := Item;
1398 Node.Parent := null;
1406 Result : Node_Access;
1409 -- Start of processing for Insert
1412 if Item < Node.Element
1413 or else Node.Element < Item
1418 if Tree.Lock > 0 then
1419 raise Program_Error with
1420 "attempt to tamper with cursors (set is locked)";
1423 Node.Element := Item;
1427 Hint := Element_Keys.Ceiling (Tree, Item);
1432 elsif Item < Hint.Element then
1434 if Tree.Lock > 0 then
1435 raise Program_Error with
1436 "attempt to tamper with cursors (set is locked)";
1439 Node.Element := Item;
1444 pragma Assert (not (Hint.Element < Item));
1445 raise Program_Error with "attempt to replace existing element";
1448 Tree_Operations.Delete_Node_Sans_Free (Tree, Node); -- Checks busy-bit
1450 Local_Insert_With_Hint
1455 Inserted => Inserted);
1457 pragma Assert (Inserted);
1458 pragma Assert (Result = Node);
1459 end Replace_Element;
1461 procedure Replace_Element
1462 (Container : in out Set;
1464 New_Item : Element_Type)
1467 if Position.Node = null then
1468 raise Constraint_Error with
1469 "Position cursor equals No_Element";
1472 if Position.Container /= Container'Unrestricted_Access then
1473 raise Program_Error with
1474 "Position cursor designates wrong set";
1477 pragma Assert (Vet (Container.Tree, Position.Node),
1478 "bad cursor in Replace_Element");
1480 Replace_Element (Container.Tree, Position.Node, New_Item);
1481 end Replace_Element;
1483 ---------------------
1484 -- Reverse_Iterate --
1485 ---------------------
1487 procedure Reverse_Iterate
1489 Process : not null access procedure (Position : Cursor))
1491 procedure Process_Node (Node : Node_Access);
1492 pragma Inline (Process_Node);
1494 procedure Local_Reverse_Iterate is
1495 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1501 procedure Process_Node (Node : Node_Access) is
1503 Process (Cursor'(Container'Unrestricted_Access, Node));
1506 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1507 B : Natural renames T.Busy;
1509 -- Start of processing for Reverse_Iterate
1515 Local_Reverse_Iterate (T);
1523 end Reverse_Iterate;
1529 function Right (Node : Node_Access) return Node_Access is
1538 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1540 Node.Color := Color;
1547 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1556 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1558 Node.Parent := Parent;
1565 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1567 Node.Right := Right;
1570 --------------------------
1571 -- Symmetric_Difference --
1572 --------------------------
1574 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1576 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
1577 end Symmetric_Difference;
1579 function Symmetric_Difference (Left, Right : Set) return Set is
1580 Tree : constant Tree_Type :=
1581 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
1583 return Set'(Controlled with Tree);
1584 end Symmetric_Difference;
1590 function To_Set (New_Item : Element_Type) return Set is
1596 Insert_Sans_Hint (Tree, New_Item, Node, Inserted);
1597 return Set'(Controlled with Tree);
1604 procedure Union (Target : in out Set; Source : Set) is
1606 Set_Ops.Union (Target.Tree, Source.Tree);
1609 function Union (Left, Right : Set) return Set is
1610 Tree : constant Tree_Type :=
1611 Set_Ops.Union (Left.Tree, Right.Tree);
1613 return Set'(Controlled with Tree);
1621 (Stream : access Root_Stream_Type'Class;
1624 procedure Write_Node
1625 (Stream : access Root_Stream_Type'Class;
1626 Node : Node_Access);
1627 pragma Inline (Write_Node);
1630 new Tree_Operations.Generic_Write (Write_Node);
1636 procedure Write_Node
1637 (Stream : access Root_Stream_Type'Class;
1641 Element_Type'Write (Stream, Node.Element);
1644 -- Start of processing for Write
1647 Write (Stream, Container.Tree);
1651 (Stream : access Root_Stream_Type'Class;
1655 raise Program_Error with "attempt to stream set cursor";
1658 end Ada.Containers.Ordered_Sets;