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-2007, 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)
894 pragma Unreferenced (Position);
899 Insert (Container, New_Item, Position, Inserted);
902 raise Constraint_Error with
903 "attempt to insert element already in set";
907 ----------------------
908 -- Insert_Sans_Hint --
909 ----------------------
911 procedure Insert_Sans_Hint
912 (Tree : in out Tree_Type;
913 New_Item : Element_Type;
914 Node : out Node_Access;
915 Inserted : out Boolean)
917 function New_Node return Node_Access;
918 pragma Inline (New_Node);
920 procedure Insert_Post is
921 new Element_Keys.Generic_Insert_Post (New_Node);
923 procedure Conditional_Insert_Sans_Hint is
924 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
930 function New_Node return Node_Access is
932 return new Node_Type'(Parent => null,
935 Color => Red_Black_Trees.Red,
936 Element => New_Item);
939 -- Start of processing for Insert_Sans_Hint
942 Conditional_Insert_Sans_Hint
947 end Insert_Sans_Hint;
949 ----------------------
950 -- Insert_With_Hint --
951 ----------------------
953 procedure Insert_With_Hint
954 (Dst_Tree : in out Tree_Type;
955 Dst_Hint : Node_Access;
956 Src_Node : Node_Access;
957 Dst_Node : out Node_Access)
960 pragma Unreferenced (Success);
962 function New_Node return Node_Access;
963 pragma Inline (New_Node);
965 procedure Insert_Post is
966 new Element_Keys.Generic_Insert_Post (New_Node);
968 procedure Insert_Sans_Hint is
969 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
971 procedure Local_Insert_With_Hint is
972 new Element_Keys.Generic_Conditional_Insert_With_Hint
980 function New_Node return Node_Access is
981 Node : constant Node_Access :=
982 new Node_Type'(Parent => null,
986 Element => Src_Node.Element);
991 -- Start of processing for Insert_With_Hint
994 Local_Insert_With_Hint
1000 end Insert_With_Hint;
1006 procedure Intersection (Target : in out Set; Source : Set) is
1008 Set_Ops.Intersection (Target.Tree, Source.Tree);
1011 function Intersection (Left, Right : Set) return Set is
1012 Tree : constant Tree_Type :=
1013 Set_Ops.Intersection (Left.Tree, Right.Tree);
1015 return Set'(Controlled with Tree);
1022 function Is_Empty (Container : Set) return Boolean is
1024 return Container.Tree.Length = 0;
1027 ------------------------
1028 -- Is_Equal_Node_Node --
1029 ------------------------
1031 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
1033 return L.Element = R.Element;
1034 end Is_Equal_Node_Node;
1036 -----------------------------
1037 -- Is_Greater_Element_Node --
1038 -----------------------------
1040 function Is_Greater_Element_Node
1041 (Left : Element_Type;
1042 Right : Node_Access) return Boolean
1045 -- Compute e > node same as node < e
1047 return Right.Element < Left;
1048 end Is_Greater_Element_Node;
1050 --------------------------
1051 -- Is_Less_Element_Node --
1052 --------------------------
1054 function Is_Less_Element_Node
1055 (Left : Element_Type;
1056 Right : Node_Access) return Boolean
1059 return Left < Right.Element;
1060 end Is_Less_Element_Node;
1062 -----------------------
1063 -- Is_Less_Node_Node --
1064 -----------------------
1066 function Is_Less_Node_Node (L, R : Node_Access) return Boolean is
1068 return L.Element < R.Element;
1069 end Is_Less_Node_Node;
1075 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1077 return Set_Ops.Is_Subset (Subset => Subset.Tree, Of_Set => Of_Set.Tree);
1086 Process : not null access procedure (Position : Cursor))
1088 procedure Process_Node (Node : Node_Access);
1089 pragma Inline (Process_Node);
1091 procedure Local_Iterate is
1092 new Tree_Operations.Generic_Iteration (Process_Node);
1098 procedure Process_Node (Node : Node_Access) is
1100 Process (Cursor'(Container'Unrestricted_Access, Node));
1103 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1104 B : Natural renames T.Busy;
1106 -- Start of prccessing for Iterate
1126 function Last (Container : Set) return Cursor is
1128 if Container.Tree.Last = null then
1132 return Cursor'(Container'Unrestricted_Access, Container.Tree.Last);
1139 function Last_Element (Container : Set) return Element_Type is
1141 if Container.Tree.Last = null then
1142 raise Constraint_Error with "set is empty";
1145 return Container.Tree.Last.Element;
1152 function Left (Node : Node_Access) return Node_Access is
1161 function Length (Container : Set) return Count_Type is
1163 return Container.Tree.Length;
1171 new Tree_Operations.Generic_Move (Clear);
1173 procedure Move (Target : in out Set; Source : in out Set) is
1175 Move (Target => Target.Tree, Source => Source.Tree);
1182 function Next (Position : Cursor) return Cursor is
1184 if Position = No_Element then
1188 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1189 "bad cursor in Next");
1192 Node : constant Node_Access :=
1193 Tree_Operations.Next (Position.Node);
1200 return Cursor'(Position.Container, Node);
1204 procedure Next (Position : in out Cursor) is
1206 Position := Next (Position);
1213 function Overlap (Left, Right : Set) return Boolean is
1215 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1222 function Parent (Node : Node_Access) return Node_Access is
1231 function Previous (Position : Cursor) return Cursor is
1233 if Position = No_Element then
1237 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1238 "bad cursor in Previous");
1241 Node : constant Node_Access :=
1242 Tree_Operations.Previous (Position.Node);
1249 return Cursor'(Position.Container, Node);
1253 procedure Previous (Position : in out Cursor) is
1255 Position := Previous (Position);
1262 procedure Query_Element
1264 Process : not null access procedure (Element : Element_Type))
1267 if Position.Node = null then
1268 raise Constraint_Error with "Position cursor equals No_Element";
1271 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1272 "bad cursor in Query_Element");
1275 T : Tree_Type renames Position.Container.Tree;
1277 B : Natural renames T.Busy;
1278 L : Natural renames T.Lock;
1285 Process (Position.Node.Element);
1303 (Stream : not null access Root_Stream_Type'Class;
1304 Container : out Set)
1307 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1308 pragma Inline (Read_Node);
1311 new Tree_Operations.Generic_Read (Clear, Read_Node);
1318 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1320 Node : Node_Access := new Node_Type;
1323 Element_Type'Read (Stream, Node.Element);
1332 -- Start of processing for Read
1335 Read (Stream, Container.Tree);
1339 (Stream : not null access Root_Stream_Type'Class;
1343 raise Program_Error with "attempt to stream set cursor";
1350 procedure Replace (Container : in out Set; New_Item : Element_Type) is
1351 Node : constant Node_Access :=
1352 Element_Keys.Find (Container.Tree, New_Item);
1356 raise Constraint_Error with
1357 "attempt to replace element not in set";
1360 if Container.Tree.Lock > 0 then
1361 raise Program_Error with
1362 "attempt to tamper with cursors (set is locked)";
1365 Node.Element := New_Item;
1368 ---------------------
1369 -- Replace_Element --
1370 ---------------------
1372 procedure Replace_Element
1373 (Tree : in out Tree_Type;
1375 Item : Element_Type)
1377 pragma Assert (Node /= null);
1379 function New_Node return Node_Access;
1380 pragma Inline (New_Node);
1382 procedure Local_Insert_Post is
1383 new Element_Keys.Generic_Insert_Post (New_Node);
1385 procedure Local_Insert_Sans_Hint is
1386 new Element_Keys.Generic_Conditional_Insert (Local_Insert_Post);
1388 procedure Local_Insert_With_Hint is
1389 new Element_Keys.Generic_Conditional_Insert_With_Hint
1391 Local_Insert_Sans_Hint);
1397 function New_Node return Node_Access is
1399 Node.Element := Item;
1401 Node.Parent := null;
1409 Result : Node_Access;
1412 -- Start of processing for Insert
1415 if Item < Node.Element
1416 or else Node.Element < Item
1421 if Tree.Lock > 0 then
1422 raise Program_Error with
1423 "attempt to tamper with cursors (set is locked)";
1426 Node.Element := Item;
1430 Hint := Element_Keys.Ceiling (Tree, Item);
1435 elsif Item < Hint.Element then
1437 if Tree.Lock > 0 then
1438 raise Program_Error with
1439 "attempt to tamper with cursors (set is locked)";
1442 Node.Element := Item;
1447 pragma Assert (not (Hint.Element < Item));
1448 raise Program_Error with "attempt to replace existing element";
1451 Tree_Operations.Delete_Node_Sans_Free (Tree, Node); -- Checks busy-bit
1453 Local_Insert_With_Hint
1458 Inserted => Inserted);
1460 pragma Assert (Inserted);
1461 pragma Assert (Result = Node);
1462 end Replace_Element;
1464 procedure Replace_Element
1465 (Container : in out Set;
1467 New_Item : Element_Type)
1470 if Position.Node = null then
1471 raise Constraint_Error with
1472 "Position cursor equals No_Element";
1475 if Position.Container /= Container'Unrestricted_Access then
1476 raise Program_Error with
1477 "Position cursor designates wrong set";
1480 pragma Assert (Vet (Container.Tree, Position.Node),
1481 "bad cursor in Replace_Element");
1483 Replace_Element (Container.Tree, Position.Node, New_Item);
1484 end Replace_Element;
1486 ---------------------
1487 -- Reverse_Iterate --
1488 ---------------------
1490 procedure Reverse_Iterate
1492 Process : not null access procedure (Position : Cursor))
1494 procedure Process_Node (Node : Node_Access);
1495 pragma Inline (Process_Node);
1497 procedure Local_Reverse_Iterate is
1498 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1504 procedure Process_Node (Node : Node_Access) is
1506 Process (Cursor'(Container'Unrestricted_Access, Node));
1509 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1510 B : Natural renames T.Busy;
1512 -- Start of processing for Reverse_Iterate
1518 Local_Reverse_Iterate (T);
1526 end Reverse_Iterate;
1532 function Right (Node : Node_Access) return Node_Access is
1541 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1543 Node.Color := Color;
1550 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1559 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1561 Node.Parent := Parent;
1568 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1570 Node.Right := Right;
1573 --------------------------
1574 -- Symmetric_Difference --
1575 --------------------------
1577 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1579 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
1580 end Symmetric_Difference;
1582 function Symmetric_Difference (Left, Right : Set) return Set is
1583 Tree : constant Tree_Type :=
1584 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
1586 return Set'(Controlled with Tree);
1587 end Symmetric_Difference;
1593 function To_Set (New_Item : Element_Type) return Set is
1597 pragma Unreferenced (Node, Inserted);
1599 Insert_Sans_Hint (Tree, New_Item, Node, Inserted);
1600 return Set'(Controlled with Tree);
1607 procedure Union (Target : in out Set; Source : Set) is
1609 Set_Ops.Union (Target.Tree, Source.Tree);
1612 function Union (Left, Right : Set) return Set is
1613 Tree : constant Tree_Type :=
1614 Set_Ops.Union (Left.Tree, Right.Tree);
1616 return Set'(Controlled with Tree);
1624 (Stream : not null access Root_Stream_Type'Class;
1627 procedure Write_Node
1628 (Stream : not null access Root_Stream_Type'Class;
1629 Node : Node_Access);
1630 pragma Inline (Write_Node);
1633 new Tree_Operations.Generic_Write (Write_Node);
1639 procedure Write_Node
1640 (Stream : not null access Root_Stream_Type'Class;
1644 Element_Type'Write (Stream, Node.Element);
1647 -- Start of processing for Write
1650 Write (Stream, Container.Tree);
1654 (Stream : not null access Root_Stream_Type'Class;
1658 raise Program_Error with "attempt to stream set cursor";
1661 end Ada.Containers.Ordered_Sets;