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 _ M U L T I S E T S --
9 -- Copyright (C) 2004-2005, Free Software Foundation, Inc. --
11 -- This specification is derived from the Ada Reference Manual for use with --
12 -- GNAT. The copyright notice above, and the license provisions that follow --
13 -- apply solely to the contents of the part following the private keyword. --
15 -- GNAT is free software; you can redistribute it and/or modify it under --
16 -- terms of the GNU General Public License as published by the Free Soft- --
17 -- ware Foundation; either version 2, or (at your option) any later ver- --
18 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
19 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
20 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
21 -- for more details. You should have received a copy of the GNU General --
22 -- Public License distributed with GNAT; see file COPYING. If not, write --
23 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
24 -- Boston, MA 02110-1301, USA. --
26 -- As a special exception, if other files instantiate generics from this --
27 -- unit, or you link this unit with other files to produce an executable, --
28 -- this unit does not by itself cause the resulting executable to be --
29 -- covered by the GNU General Public License. This exception does not --
30 -- however invalidate any other reasons why the executable file might be --
31 -- covered by the GNU Public License. --
33 -- This unit was originally developed by Matthew J Heaney. --
34 ------------------------------------------------------------------------------
36 with Ada.Unchecked_Deallocation;
38 with Ada.Containers.Red_Black_Trees.Generic_Operations;
39 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Operations);
41 with Ada.Containers.Red_Black_Trees.Generic_Keys;
42 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Keys);
44 with Ada.Containers.Red_Black_Trees.Generic_Set_Operations;
45 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Set_Operations);
47 package body Ada.Containers.Ordered_Multisets is
49 -----------------------------
50 -- Node Access Subprograms --
51 -----------------------------
53 -- These subprograms provide a functional interface to access fields
54 -- of a node, and a procedural interface for modifying these values.
56 function Color (Node : Node_Access) return Color_Type;
57 pragma Inline (Color);
59 function Left (Node : Node_Access) return Node_Access;
62 function Parent (Node : Node_Access) return Node_Access;
63 pragma Inline (Parent);
65 function Right (Node : Node_Access) return Node_Access;
66 pragma Inline (Right);
68 procedure Set_Parent (Node : Node_Access; Parent : Node_Access);
69 pragma Inline (Set_Parent);
71 procedure Set_Left (Node : Node_Access; Left : Node_Access);
72 pragma Inline (Set_Left);
74 procedure Set_Right (Node : Node_Access; Right : Node_Access);
75 pragma Inline (Set_Right);
77 procedure Set_Color (Node : Node_Access; Color : Color_Type);
78 pragma Inline (Set_Color);
80 -----------------------
81 -- Local Subprograms --
82 -----------------------
84 function Copy_Node (Source : Node_Access) return Node_Access;
85 pragma Inline (Copy_Node);
87 procedure Free (X : in out Node_Access);
89 procedure Insert_Sans_Hint
90 (Tree : in out Tree_Type;
91 New_Item : Element_Type;
92 Node : out Node_Access);
94 procedure Insert_With_Hint
95 (Dst_Tree : in out Tree_Type;
96 Dst_Hint : Node_Access;
97 Src_Node : Node_Access;
98 Dst_Node : out Node_Access);
100 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean;
101 pragma Inline (Is_Equal_Node_Node);
103 function Is_Greater_Element_Node
104 (Left : Element_Type;
105 Right : Node_Access) return Boolean;
106 pragma Inline (Is_Greater_Element_Node);
108 function Is_Less_Element_Node
109 (Left : Element_Type;
110 Right : Node_Access) return Boolean;
111 pragma Inline (Is_Less_Element_Node);
113 function Is_Less_Node_Node (L, R : Node_Access) return Boolean;
114 pragma Inline (Is_Less_Node_Node);
116 procedure Replace_Element
117 (Tree : in out Tree_Type;
119 Item : Element_Type);
121 --------------------------
122 -- Local Instantiations --
123 --------------------------
125 package Tree_Operations is
126 new Red_Black_Trees.Generic_Operations (Tree_Types);
128 procedure Delete_Tree is
129 new Tree_Operations.Generic_Delete_Tree (Free);
131 function Copy_Tree is
132 new Tree_Operations.Generic_Copy_Tree (Copy_Node, Delete_Tree);
137 new Tree_Operations.Generic_Equal (Is_Equal_Node_Node);
139 package Element_Keys is
140 new Red_Black_Trees.Generic_Keys
141 (Tree_Operations => Tree_Operations,
142 Key_Type => Element_Type,
143 Is_Less_Key_Node => Is_Less_Element_Node,
144 Is_Greater_Key_Node => Is_Greater_Element_Node);
147 new Generic_Set_Operations
148 (Tree_Operations => Tree_Operations,
149 Insert_With_Hint => Insert_With_Hint,
150 Copy_Tree => Copy_Tree,
151 Delete_Tree => Delete_Tree,
152 Is_Less => Is_Less_Node_Node,
159 function "<" (Left, Right : Cursor) return Boolean is
162 or else Right.Node = null
164 raise Constraint_Error;
167 pragma Assert (Vet (Left.Container.Tree, Left.Node),
168 "bad Left cursor in ""<""");
170 pragma Assert (Vet (Right.Container.Tree, Right.Node),
171 "bad Right cursor in ""<""");
173 return Left.Node.Element < Right.Node.Element;
176 function "<" (Left : Cursor; Right : Element_Type)
179 if Left.Node = null then
180 raise Constraint_Error;
183 pragma Assert (Vet (Left.Container.Tree, Left.Node),
184 "bad Left cursor in ""<""");
186 return Left.Node.Element < Right;
189 function "<" (Left : Element_Type; Right : Cursor)
192 if Right.Node = null then
193 raise Constraint_Error;
196 pragma Assert (Vet (Right.Container.Tree, Right.Node),
197 "bad Right cursor in ""<""");
199 return Left < Right.Node.Element;
206 function "=" (Left, Right : Set) return Boolean is
208 return Is_Equal (Left.Tree, Right.Tree);
215 function ">" (Left, Right : Cursor) return Boolean is
218 or else Right.Node = null
220 raise Constraint_Error;
223 pragma Assert (Vet (Left.Container.Tree, Left.Node),
224 "bad Left cursor in "">""");
226 pragma Assert (Vet (Right.Container.Tree, Right.Node),
227 "bad Right cursor in "">""");
229 -- L > R same as R < L
231 return Right.Node.Element < Left.Node.Element;
234 function ">" (Left : Cursor; Right : Element_Type)
237 if Left.Node = null then
238 raise Constraint_Error;
241 pragma Assert (Vet (Left.Container.Tree, Left.Node),
242 "bad Left cursor in "">""");
244 return Right < Left.Node.Element;
247 function ">" (Left : Element_Type; Right : Cursor)
250 if Right.Node = null then
251 raise Constraint_Error;
254 pragma Assert (Vet (Right.Container.Tree, Right.Node),
255 "bad Right cursor in "">""");
257 return Right.Node.Element < Left;
265 new Tree_Operations.Generic_Adjust (Copy_Tree);
267 procedure Adjust (Container : in out Set) is
269 Adjust (Container.Tree);
276 function Ceiling (Container : Set; Item : Element_Type) return Cursor is
277 Node : constant Node_Access :=
278 Element_Keys.Ceiling (Container.Tree, Item);
285 return Cursor'(Container'Unrestricted_Access, Node);
293 new Tree_Operations.Generic_Clear (Delete_Tree);
295 procedure Clear (Container : in out Set) is
297 Clear (Container.Tree);
304 function Color (Node : Node_Access) return Color_Type is
313 function Contains (Container : Set; Item : Element_Type) return Boolean is
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; Item : Element_Type) is
338 Tree : Tree_Type renames Container.Tree;
339 Node : Node_Access := Element_Keys.Ceiling (Tree, Item);
340 Done : constant Node_Access := Element_Keys.Upper_Bound (Tree, Item);
345 raise Constraint_Error;
350 Node := Tree_Operations.Next (Node);
351 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
354 exit when Node = Done;
358 procedure Delete (Container : in out Set; Position : in out Cursor) is
360 if Position.Node = null then
361 raise Constraint_Error;
364 if Position.Container /= Container'Unrestricted_Access then
368 pragma Assert (Vet (Container.Tree, Position.Node),
369 "bad cursor in Delete");
371 Delete_Node_Sans_Free (Container.Tree, Position.Node);
372 Free (Position.Node);
374 Position.Container := null;
381 procedure Delete_First (Container : in out Set) is
382 Tree : Tree_Type renames Container.Tree;
383 X : Node_Access := Tree.First;
390 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
398 procedure Delete_Last (Container : in out Set) is
399 Tree : Tree_Type renames Container.Tree;
400 X : Node_Access := Tree.Last;
407 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
415 procedure Difference (Target : in out Set; Source : Set) is
417 Set_Ops.Difference (Target.Tree, Source.Tree);
420 function Difference (Left, Right : Set) return Set is
421 Tree : constant Tree_Type :=
422 Set_Ops.Difference (Left.Tree, Right.Tree);
424 return Set'(Controlled with Tree);
431 function Element (Position : Cursor) return Element_Type is
433 if Position.Node = null then
434 raise Constraint_Error;
437 pragma Assert (Vet (Position.Container.Tree, Position.Node),
438 "bad cursor in Element");
440 return Position.Node.Element;
443 -------------------------
444 -- Equivalent_Elements --
445 -------------------------
447 function Equivalent_Elements (Left, Right : Element_Type) return Boolean is
456 end Equivalent_Elements;
458 ---------------------
459 -- Equivalent_Sets --
460 ---------------------
462 function Equivalent_Sets (Left, Right : Set) return Boolean is
464 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean;
465 pragma Inline (Is_Equivalent_Node_Node);
467 function Is_Equivalent is
468 new Tree_Operations.Generic_Equal (Is_Equivalent_Node_Node);
470 -----------------------------
471 -- Is_Equivalent_Node_Node --
472 -----------------------------
474 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean is
476 if L.Element < R.Element then
478 elsif R.Element < L.Element then
483 end Is_Equivalent_Node_Node;
485 -- Start of processing for Equivalent_Sets
488 return Is_Equivalent (Left.Tree, Right.Tree);
495 procedure Exclude (Container : in out Set; Item : Element_Type) is
496 Tree : Tree_Type renames Container.Tree;
497 Node : Node_Access := Element_Keys.Ceiling (Tree, Item);
498 Done : constant Node_Access := Element_Keys.Upper_Bound (Tree, Item);
501 while Node /= Done loop
503 Node := Tree_Operations.Next (Node);
504 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
513 function Find (Container : Set; Item : Element_Type) return Cursor is
514 Node : constant Node_Access :=
515 Element_Keys.Find (Container.Tree, Item);
522 return Cursor'(Container'Unrestricted_Access, Node);
529 function First (Container : Set) return Cursor is
531 if Container.Tree.First = null then
535 return Cursor'(Container'Unrestricted_Access, Container.Tree.First);
542 function First_Element (Container : Set) return Element_Type is
544 if Container.Tree.First = null then
545 raise Constraint_Error;
548 return Container.Tree.First.Element;
555 function Floor (Container : Set; Item : Element_Type) return Cursor is
556 Node : constant Node_Access :=
557 Element_Keys.Floor (Container.Tree, Item);
564 return Cursor'(Container'Unrestricted_Access, Node);
571 procedure Free (X : in out Node_Access) is
572 procedure Deallocate is
573 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
589 package body Generic_Keys is
591 -----------------------
592 -- Local Subprograms --
593 -----------------------
595 function Is_Greater_Key_Node
597 Right : Node_Access) return Boolean;
598 pragma Inline (Is_Greater_Key_Node);
600 function Is_Less_Key_Node
602 Right : Node_Access) return Boolean;
603 pragma Inline (Is_Less_Key_Node);
605 --------------------------
606 -- Local_Instantiations --
607 --------------------------
610 new Red_Black_Trees.Generic_Keys
611 (Tree_Operations => Tree_Operations,
612 Key_Type => Key_Type,
613 Is_Less_Key_Node => Is_Less_Key_Node,
614 Is_Greater_Key_Node => Is_Greater_Key_Node);
620 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
621 Node : constant Node_Access :=
622 Key_Keys.Ceiling (Container.Tree, Key);
629 return Cursor'(Container'Unrestricted_Access, Node);
636 function Contains (Container : Set; Key : Key_Type) return Boolean is
638 return Find (Container, Key) /= No_Element;
645 procedure Delete (Container : in out Set; Key : Key_Type) is
646 Tree : Tree_Type renames Container.Tree;
647 Node : Node_Access := Key_Keys.Ceiling (Tree, Key);
648 Done : constant Node_Access := Key_Keys.Upper_Bound (Tree, Key);
653 raise Constraint_Error;
658 Node := Tree_Operations.Next (Node);
659 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
662 exit when Node = Done;
670 function Element (Container : Set; Key : Key_Type) return Element_Type is
671 Node : constant Node_Access :=
672 Key_Keys.Find (Container.Tree, Key);
675 raise Constraint_Error;
681 ---------------------
682 -- Equivalent_Keys --
683 ---------------------
685 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
700 procedure Exclude (Container : in out Set; Key : Key_Type) is
701 Tree : Tree_Type renames Container.Tree;
702 Node : Node_Access := Key_Keys.Ceiling (Tree, Key);
703 Done : constant Node_Access := Key_Keys.Upper_Bound (Tree, Key);
707 while Node /= Done loop
709 Node := Tree_Operations.Next (Node);
710 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
719 function Find (Container : Set; Key : Key_Type) return Cursor is
720 Node : constant Node_Access :=
721 Key_Keys.Find (Container.Tree, Key);
728 return Cursor'(Container'Unrestricted_Access, Node);
735 function Floor (Container : Set; Key : Key_Type) return Cursor is
736 Node : constant Node_Access :=
737 Key_Keys.Floor (Container.Tree, Key);
744 return Cursor'(Container'Unrestricted_Access, Node);
747 -------------------------
748 -- Is_Greater_Key_Node --
749 -------------------------
751 function Is_Greater_Key_Node
753 Right : Node_Access) return Boolean is
755 return Key (Right.Element) < Left;
756 end Is_Greater_Key_Node;
758 ----------------------
759 -- Is_Less_Key_Node --
760 ----------------------
762 function Is_Less_Key_Node
764 Right : Node_Access) return Boolean is
766 return Left < Key (Right.Element);
767 end Is_Less_Key_Node;
776 Process : not null access procedure (Position : Cursor))
778 procedure Process_Node (Node : Node_Access);
779 pragma Inline (Process_Node);
781 procedure Local_Iterate is
782 new Key_Keys.Generic_Iteration (Process_Node);
788 procedure Process_Node (Node : Node_Access) is
790 Process (Cursor'(Container'Unrestricted_Access, Node));
793 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
794 B : Natural renames T.Busy;
796 -- Start of processing for Iterate
802 Local_Iterate (T, Key);
816 function Key (Position : Cursor) return Key_Type is
818 if Position.Node = null then
819 raise Constraint_Error;
822 pragma Assert (Vet (Position.Container.Tree, Position.Node),
823 "bad cursor in Key");
825 return Key (Position.Node.Element);
828 ---------------------
829 -- Reverse_Iterate --
830 ---------------------
832 procedure Reverse_Iterate
835 Process : not null access procedure (Position : Cursor))
837 procedure Process_Node (Node : Node_Access);
838 pragma Inline (Process_Node);
840 procedure Local_Reverse_Iterate is
841 new Key_Keys.Generic_Reverse_Iteration (Process_Node);
847 procedure Process_Node (Node : Node_Access) is
849 Process (Cursor'(Container'Unrestricted_Access, Node));
852 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
853 B : Natural renames T.Busy;
855 -- Start of processing for Reverse_Iterate
861 Local_Reverse_Iterate (T, Key);
871 -----------------------------------
872 -- Update_Element_Preserving_Key --
873 -----------------------------------
875 procedure Update_Element_Preserving_Key
876 (Container : in out Set;
878 Process : not null access procedure (Element : in out Element_Type))
880 Tree : Tree_Type renames Container.Tree;
883 if Position.Node = null then
884 raise Constraint_Error;
887 if Position.Container /= Container'Unrestricted_Access then
891 pragma Assert (Vet (Container.Tree, Position.Node),
892 "bad cursor in Update_Element_Preserving_Key");
895 E : Element_Type renames Position.Node.Element;
896 K : constant Key_Type := Key (E);
898 B : Natural renames Tree.Busy;
899 L : Natural renames Tree.Lock;
917 if Equivalent_Keys (Left => K, Right => Key (E)) then
923 X : Node_Access := Position.Node;
925 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
930 end Update_Element_Preserving_Key;
938 function Has_Element (Position : Cursor) return Boolean is
940 return Position /= No_Element;
947 procedure Insert (Container : in out Set; New_Item : Element_Type) is
950 Insert (Container, New_Item, Position);
954 (Container : in out Set;
955 New_Item : Element_Type;
956 Position : out Cursor)
964 Position.Container := Container'Unrestricted_Access;
967 ----------------------
968 -- Insert_Sans_Hint --
969 ----------------------
971 procedure Insert_Sans_Hint
972 (Tree : in out Tree_Type;
973 New_Item : Element_Type;
974 Node : out Node_Access)
976 function New_Node return Node_Access;
977 pragma Inline (New_Node);
979 procedure Insert_Post is
980 new Element_Keys.Generic_Insert_Post (New_Node);
982 procedure Unconditional_Insert_Sans_Hint is
983 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
989 function New_Node return Node_Access is
990 Node : constant Node_Access :=
991 new Node_Type'(Parent => null,
994 Color => Red_Black_Trees.Red,
995 Element => New_Item);
1000 -- Start of processing for Insert_Sans_Hint
1003 Unconditional_Insert_Sans_Hint
1007 end Insert_Sans_Hint;
1009 ----------------------
1010 -- Insert_With_Hint --
1011 ----------------------
1013 procedure Insert_With_Hint
1014 (Dst_Tree : in out Tree_Type;
1015 Dst_Hint : Node_Access;
1016 Src_Node : Node_Access;
1017 Dst_Node : out Node_Access)
1019 function New_Node return Node_Access;
1020 pragma Inline (New_Node);
1022 procedure Insert_Post is
1023 new Element_Keys.Generic_Insert_Post (New_Node);
1025 procedure Insert_Sans_Hint is
1026 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1028 procedure Local_Insert_With_Hint is
1029 new Element_Keys.Generic_Unconditional_Insert_With_Hint
1037 function New_Node return Node_Access is
1038 Node : constant Node_Access :=
1039 new Node_Type'(Parent => null,
1043 Element => Src_Node.Element);
1048 -- Start of processing for Insert_With_Hint
1051 Local_Insert_With_Hint
1056 end Insert_With_Hint;
1062 procedure Intersection (Target : in out Set; Source : Set) is
1064 Set_Ops.Intersection (Target.Tree, Source.Tree);
1067 function Intersection (Left, Right : Set) return Set is
1068 Tree : constant Tree_Type :=
1069 Set_Ops.Intersection (Left.Tree, Right.Tree);
1071 return Set'(Controlled with Tree);
1078 function Is_Empty (Container : Set) return Boolean is
1080 return Container.Tree.Length = 0;
1083 ------------------------
1084 -- Is_Equal_Node_Node --
1085 ------------------------
1087 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
1089 return L.Element = R.Element;
1090 end Is_Equal_Node_Node;
1092 -----------------------------
1093 -- Is_Greater_Element_Node --
1094 -----------------------------
1096 function Is_Greater_Element_Node
1097 (Left : Element_Type;
1098 Right : Node_Access) return Boolean
1101 -- e > node same as node < e
1103 return Right.Element < Left;
1104 end Is_Greater_Element_Node;
1106 --------------------------
1107 -- Is_Less_Element_Node --
1108 --------------------------
1110 function Is_Less_Element_Node
1111 (Left : Element_Type;
1112 Right : Node_Access) return Boolean
1115 return Left < Right.Element;
1116 end Is_Less_Element_Node;
1118 -----------------------
1119 -- Is_Less_Node_Node --
1120 -----------------------
1122 function Is_Less_Node_Node (L, R : Node_Access) return Boolean is
1124 return L.Element < R.Element;
1125 end Is_Less_Node_Node;
1131 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1133 return Set_Ops.Is_Subset (Subset => Subset.Tree, Of_Set => Of_Set.Tree);
1142 Process : not null access procedure (Position : Cursor))
1144 procedure Process_Node (Node : Node_Access);
1145 pragma Inline (Process_Node);
1147 procedure Local_Iterate is
1148 new Tree_Operations.Generic_Iteration (Process_Node);
1154 procedure Process_Node (Node : Node_Access) is
1156 Process (Cursor'(Container'Unrestricted_Access, Node));
1159 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1160 B : Natural renames T.Busy;
1162 -- Start of processing for Iterate
1180 Item : Element_Type;
1181 Process : not null access procedure (Position : Cursor))
1183 procedure Process_Node (Node : Node_Access);
1184 pragma Inline (Process_Node);
1186 procedure Local_Iterate is
1187 new Element_Keys.Generic_Iteration (Process_Node);
1193 procedure Process_Node (Node : Node_Access) is
1195 Process (Cursor'(Container'Unrestricted_Access, Node));
1198 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1199 B : Natural renames T.Busy;
1201 -- Start of processing for Iterate
1207 Local_Iterate (T, Item);
1221 function Last (Container : Set) return Cursor is
1223 if Container.Tree.Last = null then
1227 return Cursor'(Container'Unrestricted_Access, Container.Tree.Last);
1234 function Last_Element (Container : Set) return Element_Type is
1236 if Container.Tree.Last = null then
1237 raise Constraint_Error;
1240 return Container.Tree.Last.Element;
1247 function Left (Node : Node_Access) return Node_Access is
1256 function Length (Container : Set) return Count_Type is
1258 return Container.Tree.Length;
1266 new Tree_Operations.Generic_Move (Clear);
1268 procedure Move (Target : in out Set; Source : in out Set) is
1270 Move (Target => Target.Tree, Source => Source.Tree);
1277 procedure Next (Position : in out Cursor)
1280 Position := Next (Position);
1283 function Next (Position : Cursor) return Cursor is
1285 if Position = No_Element then
1289 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1290 "bad cursor in Next");
1293 Node : constant Node_Access :=
1294 Tree_Operations.Next (Position.Node);
1300 return Cursor'(Position.Container, Node);
1308 function Overlap (Left, Right : Set) return Boolean is
1310 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1317 function Parent (Node : Node_Access) return Node_Access is
1326 procedure Previous (Position : in out Cursor)
1329 Position := Previous (Position);
1332 function Previous (Position : Cursor) return Cursor is
1334 if Position = No_Element then
1338 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1339 "bad cursor in Previous");
1342 Node : constant Node_Access :=
1343 Tree_Operations.Previous (Position.Node);
1349 return Cursor'(Position.Container, Node);
1357 procedure Query_Element
1359 Process : not null access procedure (Element : Element_Type))
1362 if Position.Node = null then
1363 raise Constraint_Error;
1366 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1367 "bad cursor in Query_Element");
1370 T : Tree_Type renames Position.Container.Tree;
1372 B : Natural renames T.Busy;
1373 L : Natural renames T.Lock;
1380 Process (Position.Node.Element);
1398 (Stream : access Root_Stream_Type'Class;
1399 Container : out Set)
1402 (Stream : access Root_Stream_Type'Class) return Node_Access;
1403 pragma Inline (Read_Node);
1406 new Tree_Operations.Generic_Read (Clear, Read_Node);
1413 (Stream : access Root_Stream_Type'Class) return Node_Access
1415 Node : Node_Access := new Node_Type;
1417 Element_Type'Read (Stream, Node.Element);
1421 Free (Node); -- Note that Free deallocates elem too
1425 -- Start of processing for Read
1428 Read (Stream, Container.Tree);
1432 (Stream : access Root_Stream_Type'Class;
1436 raise Program_Error;
1439 ---------------------
1440 -- Replace_Element --
1441 ---------------------
1443 procedure Replace_Element
1444 (Tree : in out Tree_Type;
1446 Item : Element_Type)
1449 if Item < Node.Element
1450 or else Node.Element < Item
1454 if Tree.Lock > 0 then
1455 raise Program_Error;
1458 Node.Element := Item;
1462 Tree_Operations.Delete_Node_Sans_Free (Tree, Node); -- Checks busy-bit
1464 Insert_New_Item : declare
1465 function New_Node return Node_Access;
1466 pragma Inline (New_Node);
1468 procedure Insert_Post is
1469 new Element_Keys.Generic_Insert_Post (New_Node);
1471 procedure Unconditional_Insert is
1472 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1478 function New_Node return Node_Access is
1480 Node.Element := Item;
1481 Node.Color := Red_Black_Trees.Red;
1482 Node.Parent := null;
1489 Result : Node_Access;
1491 -- Start of processing for Insert_New_Item
1494 Unconditional_Insert
1499 pragma Assert (Result = Node);
1500 end Insert_New_Item;
1501 end Replace_Element;
1503 procedure Replace_Element
1504 (Container : in out Set;
1506 New_Item : Element_Type)
1509 if Position.Node = null then
1510 raise Constraint_Error;
1513 if Position.Container /= Container'Unrestricted_Access then
1514 raise Program_Error;
1517 pragma Assert (Vet (Container.Tree, Position.Node),
1518 "bad cursor in Replace_Element");
1520 Replace_Element (Container.Tree, Position.Node, New_Item);
1521 end Replace_Element;
1523 ---------------------
1524 -- Reverse_Iterate --
1525 ---------------------
1527 procedure Reverse_Iterate
1529 Process : not null access procedure (Position : Cursor))
1531 procedure Process_Node (Node : Node_Access);
1532 pragma Inline (Process_Node);
1534 procedure Local_Reverse_Iterate is
1535 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1541 procedure Process_Node (Node : Node_Access) is
1543 Process (Cursor'(Container'Unrestricted_Access, Node));
1546 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1547 B : Natural renames T.Busy;
1549 -- Start of processing for Reverse_Iterate
1555 Local_Reverse_Iterate (T);
1563 end Reverse_Iterate;
1565 procedure Reverse_Iterate
1567 Item : Element_Type;
1568 Process : not null access procedure (Position : Cursor))
1570 procedure Process_Node (Node : Node_Access);
1571 pragma Inline (Process_Node);
1573 procedure Local_Reverse_Iterate is
1574 new Element_Keys.Generic_Reverse_Iteration (Process_Node);
1580 procedure Process_Node (Node : Node_Access) is
1582 Process (Cursor'(Container'Unrestricted_Access, Node));
1585 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1586 B : Natural renames T.Busy;
1588 -- Start of processing for Reverse_Iterate
1594 Local_Reverse_Iterate (T, Item);
1602 end Reverse_Iterate;
1608 function Right (Node : Node_Access) return Node_Access is
1617 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1619 Node.Color := Color;
1626 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1635 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1637 Node.Parent := Parent;
1644 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1646 Node.Right := Right;
1649 --------------------------
1650 -- Symmetric_Difference --
1651 --------------------------
1653 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1655 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
1656 end Symmetric_Difference;
1658 function Symmetric_Difference (Left, Right : Set) return Set is
1659 Tree : constant Tree_Type :=
1660 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
1662 return Set'(Controlled with Tree);
1663 end Symmetric_Difference;
1669 function To_Set (New_Item : Element_Type) return Set is
1674 Insert_Sans_Hint (Tree, New_Item, Node);
1675 return Set'(Controlled with Tree);
1682 procedure Union (Target : in out Set; Source : Set) is
1684 Set_Ops.Union (Target.Tree, Source.Tree);
1687 function Union (Left, Right : Set) return Set is
1688 Tree : constant Tree_Type :=
1689 Set_Ops.Union (Left.Tree, Right.Tree);
1691 return Set'(Controlled with Tree);
1699 (Stream : access Root_Stream_Type'Class;
1702 procedure Write_Node
1703 (Stream : access Root_Stream_Type'Class;
1704 Node : Node_Access);
1705 pragma Inline (Write_Node);
1708 new Tree_Operations.Generic_Write (Write_Node);
1714 procedure Write_Node
1715 (Stream : access Root_Stream_Type'Class;
1719 Element_Type'Write (Stream, Node.Element);
1722 -- Start of processing for Write
1725 Write (Stream, Container.Tree);
1729 (Stream : access Root_Stream_Type'Class;
1733 raise Program_Error;
1736 end Ada.Containers.Ordered_Multisets;