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-2006, 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
161 if Left.Node = null then
162 raise Constraint_Error with "Left cursor equals No_Element";
165 if Right.Node = null then
166 raise Constraint_Error with "Right cursor equals No_Element";
169 pragma Assert (Vet (Left.Container.Tree, Left.Node),
170 "bad Left cursor in ""<""");
172 pragma Assert (Vet (Right.Container.Tree, Right.Node),
173 "bad Right cursor in ""<""");
175 return Left.Node.Element < Right.Node.Element;
178 function "<" (Left : Cursor; Right : Element_Type)
181 if Left.Node = null then
182 raise Constraint_Error with "Left cursor equals No_Element";
185 pragma Assert (Vet (Left.Container.Tree, Left.Node),
186 "bad Left cursor in ""<""");
188 return Left.Node.Element < Right;
191 function "<" (Left : Element_Type; Right : Cursor)
194 if Right.Node = null then
195 raise Constraint_Error with "Right cursor equals No_Element";
198 pragma Assert (Vet (Right.Container.Tree, Right.Node),
199 "bad Right cursor in ""<""");
201 return Left < Right.Node.Element;
208 function "=" (Left, Right : Set) return Boolean is
210 return Is_Equal (Left.Tree, Right.Tree);
217 function ">" (Left, Right : Cursor) return Boolean is
219 if Left.Node = null then
220 raise Constraint_Error with "Left cursor equals No_Element";
223 if Right.Node = null then
224 raise Constraint_Error with "Right cursor equals No_Element";
227 pragma Assert (Vet (Left.Container.Tree, Left.Node),
228 "bad Left cursor in "">""");
230 pragma Assert (Vet (Right.Container.Tree, Right.Node),
231 "bad Right cursor in "">""");
233 -- L > R same as R < L
235 return Right.Node.Element < Left.Node.Element;
238 function ">" (Left : Cursor; Right : Element_Type)
241 if Left.Node = null then
242 raise Constraint_Error with "Left cursor equals No_Element";
245 pragma Assert (Vet (Left.Container.Tree, Left.Node),
246 "bad Left cursor in "">""");
248 return Right < Left.Node.Element;
251 function ">" (Left : Element_Type; Right : Cursor)
254 if Right.Node = null then
255 raise Constraint_Error with "Right cursor equals No_Element";
258 pragma Assert (Vet (Right.Container.Tree, Right.Node),
259 "bad Right cursor in "">""");
261 return Right.Node.Element < Left;
269 new Tree_Operations.Generic_Adjust (Copy_Tree);
271 procedure Adjust (Container : in out Set) is
273 Adjust (Container.Tree);
280 function Ceiling (Container : Set; Item : Element_Type) return Cursor is
281 Node : constant Node_Access :=
282 Element_Keys.Ceiling (Container.Tree, Item);
289 return Cursor'(Container'Unrestricted_Access, Node);
297 new Tree_Operations.Generic_Clear (Delete_Tree);
299 procedure Clear (Container : in out Set) is
301 Clear (Container.Tree);
308 function Color (Node : Node_Access) return Color_Type is
317 function Contains (Container : Set; Item : Element_Type) return Boolean is
319 return Find (Container, Item) /= No_Element;
326 function Copy_Node (Source : Node_Access) return Node_Access is
327 Target : constant Node_Access :=
328 new Node_Type'(Parent => null,
331 Color => Source.Color,
332 Element => Source.Element);
341 procedure Delete (Container : in out Set; Item : Element_Type) is
342 Tree : Tree_Type renames Container.Tree;
343 Node : Node_Access := Element_Keys.Ceiling (Tree, Item);
344 Done : constant Node_Access := Element_Keys.Upper_Bound (Tree, Item);
349 raise Constraint_Error with
350 "attempt to delete element not in set";
355 Node := Tree_Operations.Next (Node);
356 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
359 exit when Node = Done;
363 procedure Delete (Container : in out Set; Position : in out Cursor) is
365 if Position.Node = null then
366 raise Constraint_Error with "Position cursor equals No_Element";
369 if Position.Container /= Container'Unrestricted_Access then
370 raise Program_Error with "Position cursor designates wrong set";
373 pragma Assert (Vet (Container.Tree, Position.Node),
374 "bad cursor in Delete");
376 Delete_Node_Sans_Free (Container.Tree, Position.Node);
377 Free (Position.Node);
379 Position.Container := null;
386 procedure Delete_First (Container : in out Set) is
387 Tree : Tree_Type renames Container.Tree;
388 X : Node_Access := Tree.First;
395 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
403 procedure Delete_Last (Container : in out Set) is
404 Tree : Tree_Type renames Container.Tree;
405 X : Node_Access := Tree.Last;
412 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
420 procedure Difference (Target : in out Set; Source : Set) is
422 Set_Ops.Difference (Target.Tree, Source.Tree);
425 function Difference (Left, Right : Set) return Set is
426 Tree : constant Tree_Type :=
427 Set_Ops.Difference (Left.Tree, Right.Tree);
429 return Set'(Controlled with Tree);
436 function Element (Position : Cursor) return Element_Type is
438 if Position.Node = null then
439 raise Constraint_Error with "Position cursor equals No_Element";
442 pragma Assert (Vet (Position.Container.Tree, Position.Node),
443 "bad cursor in Element");
445 return Position.Node.Element;
448 -------------------------
449 -- Equivalent_Elements --
450 -------------------------
452 function Equivalent_Elements (Left, Right : Element_Type) return Boolean is
461 end Equivalent_Elements;
463 ---------------------
464 -- Equivalent_Sets --
465 ---------------------
467 function Equivalent_Sets (Left, Right : Set) return Boolean is
469 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean;
470 pragma Inline (Is_Equivalent_Node_Node);
472 function Is_Equivalent is
473 new Tree_Operations.Generic_Equal (Is_Equivalent_Node_Node);
475 -----------------------------
476 -- Is_Equivalent_Node_Node --
477 -----------------------------
479 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean is
481 if L.Element < R.Element then
483 elsif R.Element < L.Element then
488 end Is_Equivalent_Node_Node;
490 -- Start of processing for Equivalent_Sets
493 return Is_Equivalent (Left.Tree, Right.Tree);
500 procedure Exclude (Container : in out Set; Item : Element_Type) is
501 Tree : Tree_Type renames Container.Tree;
502 Node : Node_Access := Element_Keys.Ceiling (Tree, Item);
503 Done : constant Node_Access := Element_Keys.Upper_Bound (Tree, Item);
506 while Node /= Done loop
508 Node := Tree_Operations.Next (Node);
509 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
518 function Find (Container : Set; Item : Element_Type) return Cursor is
519 Node : constant Node_Access :=
520 Element_Keys.Find (Container.Tree, Item);
527 return Cursor'(Container'Unrestricted_Access, Node);
534 function First (Container : Set) return Cursor is
536 if Container.Tree.First = null then
540 return Cursor'(Container'Unrestricted_Access, Container.Tree.First);
547 function First_Element (Container : Set) return Element_Type is
549 if Container.Tree.First = null then
550 raise Constraint_Error with "set is empty";
553 return Container.Tree.First.Element;
560 function Floor (Container : Set; Item : Element_Type) return Cursor is
561 Node : constant Node_Access :=
562 Element_Keys.Floor (Container.Tree, Item);
569 return Cursor'(Container'Unrestricted_Access, Node);
576 procedure Free (X : in out Node_Access) is
577 procedure Deallocate is
578 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
594 package body Generic_Keys is
596 -----------------------
597 -- Local Subprograms --
598 -----------------------
600 function Is_Greater_Key_Node
602 Right : Node_Access) return Boolean;
603 pragma Inline (Is_Greater_Key_Node);
605 function Is_Less_Key_Node
607 Right : Node_Access) return Boolean;
608 pragma Inline (Is_Less_Key_Node);
610 --------------------------
611 -- Local_Instantiations --
612 --------------------------
615 new Red_Black_Trees.Generic_Keys
616 (Tree_Operations => Tree_Operations,
617 Key_Type => Key_Type,
618 Is_Less_Key_Node => Is_Less_Key_Node,
619 Is_Greater_Key_Node => Is_Greater_Key_Node);
625 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
626 Node : constant Node_Access :=
627 Key_Keys.Ceiling (Container.Tree, Key);
634 return Cursor'(Container'Unrestricted_Access, Node);
641 function Contains (Container : Set; Key : Key_Type) return Boolean is
643 return Find (Container, Key) /= No_Element;
650 procedure Delete (Container : in out Set; Key : Key_Type) is
651 Tree : Tree_Type renames Container.Tree;
652 Node : Node_Access := Key_Keys.Ceiling (Tree, Key);
653 Done : constant Node_Access := Key_Keys.Upper_Bound (Tree, Key);
658 raise Constraint_Error with "attempt to delete key not in set";
663 Node := Tree_Operations.Next (Node);
664 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
667 exit when Node = Done;
675 function Element (Container : Set; Key : Key_Type) return Element_Type is
676 Node : constant Node_Access :=
677 Key_Keys.Find (Container.Tree, Key);
680 raise Constraint_Error with "key not in set";
686 ---------------------
687 -- Equivalent_Keys --
688 ---------------------
690 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
705 procedure Exclude (Container : in out Set; Key : Key_Type) is
706 Tree : Tree_Type renames Container.Tree;
707 Node : Node_Access := Key_Keys.Ceiling (Tree, Key);
708 Done : constant Node_Access := Key_Keys.Upper_Bound (Tree, Key);
712 while Node /= Done loop
714 Node := Tree_Operations.Next (Node);
715 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
724 function Find (Container : Set; Key : Key_Type) return Cursor is
725 Node : constant Node_Access :=
726 Key_Keys.Find (Container.Tree, Key);
733 return Cursor'(Container'Unrestricted_Access, Node);
740 function Floor (Container : Set; Key : Key_Type) return Cursor is
741 Node : constant Node_Access :=
742 Key_Keys.Floor (Container.Tree, Key);
749 return Cursor'(Container'Unrestricted_Access, Node);
752 -------------------------
753 -- Is_Greater_Key_Node --
754 -------------------------
756 function Is_Greater_Key_Node
758 Right : Node_Access) return Boolean is
760 return Key (Right.Element) < Left;
761 end Is_Greater_Key_Node;
763 ----------------------
764 -- Is_Less_Key_Node --
765 ----------------------
767 function Is_Less_Key_Node
769 Right : Node_Access) return Boolean is
771 return Left < Key (Right.Element);
772 end Is_Less_Key_Node;
781 Process : not null access procedure (Position : Cursor))
783 procedure Process_Node (Node : Node_Access);
784 pragma Inline (Process_Node);
786 procedure Local_Iterate is
787 new Key_Keys.Generic_Iteration (Process_Node);
793 procedure Process_Node (Node : Node_Access) is
795 Process (Cursor'(Container'Unrestricted_Access, Node));
798 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
799 B : Natural renames T.Busy;
801 -- Start of processing for Iterate
807 Local_Iterate (T, Key);
821 function Key (Position : Cursor) return Key_Type is
823 if Position.Node = null then
824 raise Constraint_Error with
825 "Position cursor equals No_Element";
828 pragma Assert (Vet (Position.Container.Tree, Position.Node),
829 "bad cursor in Key");
831 return Key (Position.Node.Element);
834 ---------------------
835 -- Reverse_Iterate --
836 ---------------------
838 procedure Reverse_Iterate
841 Process : not null access procedure (Position : Cursor))
843 procedure Process_Node (Node : Node_Access);
844 pragma Inline (Process_Node);
846 procedure Local_Reverse_Iterate is
847 new Key_Keys.Generic_Reverse_Iteration (Process_Node);
853 procedure Process_Node (Node : Node_Access) is
855 Process (Cursor'(Container'Unrestricted_Access, Node));
858 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
859 B : Natural renames T.Busy;
861 -- Start of processing for Reverse_Iterate
867 Local_Reverse_Iterate (T, Key);
881 procedure Update_Element
882 (Container : in out Set;
884 Process : not null access procedure (Element : in out Element_Type))
886 Tree : Tree_Type renames Container.Tree;
887 Node : constant Node_Access := Position.Node;
891 raise Constraint_Error with
892 "Position cursor equals No_Element";
895 if Position.Container /= Container'Unrestricted_Access then
896 raise Program_Error with
897 "Position cursor designates wrong set";
900 pragma Assert (Vet (Tree, Node),
901 "bad cursor in Update_Element");
904 E : Element_Type renames Node.Element;
905 K : constant Key_Type := Key (E);
907 B : Natural renames Tree.Busy;
908 L : Natural renames Tree.Lock;
926 if Equivalent_Keys (Left => K, Right => Key (E)) then
931 -- Delete_Node checks busy-bit
933 Tree_Operations.Delete_Node_Sans_Free (Tree, Node);
935 Insert_New_Item : declare
936 function New_Node return Node_Access;
937 pragma Inline (New_Node);
939 procedure Insert_Post is
940 new Element_Keys.Generic_Insert_Post (New_Node);
942 procedure Unconditional_Insert is
943 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
949 function New_Node return Node_Access is
951 Node.Color := Red_Black_Trees.Red;
959 Result : Node_Access;
961 -- Start of processing for Insert_New_Item
969 pragma Assert (Result = Node);
979 function Has_Element (Position : Cursor) return Boolean is
981 return Position /= No_Element;
988 procedure Insert (Container : in out Set; New_Item : Element_Type) is
991 Insert (Container, New_Item, Position);
995 (Container : in out Set;
996 New_Item : Element_Type;
997 Position : out Cursor)
1000 Insert_Sans_Hint (Container.Tree, New_Item, Position.Node);
1001 Position.Container := Container'Unrestricted_Access;
1004 ----------------------
1005 -- Insert_Sans_Hint --
1006 ----------------------
1008 procedure Insert_Sans_Hint
1009 (Tree : in out Tree_Type;
1010 New_Item : Element_Type;
1011 Node : out Node_Access)
1013 function New_Node return Node_Access;
1014 pragma Inline (New_Node);
1016 procedure Insert_Post is
1017 new Element_Keys.Generic_Insert_Post (New_Node);
1019 procedure Unconditional_Insert is
1020 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1026 function New_Node return Node_Access is
1027 Node : constant Node_Access :=
1028 new Node_Type'(Parent => null,
1031 Color => Red_Black_Trees.Red,
1032 Element => New_Item);
1037 -- Start of processing for Insert_Sans_Hint
1040 Unconditional_Insert (Tree, New_Item, Node);
1041 end Insert_Sans_Hint;
1043 ----------------------
1044 -- Insert_With_Hint --
1045 ----------------------
1047 procedure Insert_With_Hint
1048 (Dst_Tree : in out Tree_Type;
1049 Dst_Hint : Node_Access;
1050 Src_Node : Node_Access;
1051 Dst_Node : out Node_Access)
1053 function New_Node return Node_Access;
1054 pragma Inline (New_Node);
1056 procedure Insert_Post is
1057 new Element_Keys.Generic_Insert_Post (New_Node);
1059 procedure Insert_Sans_Hint is
1060 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1062 procedure Local_Insert_With_Hint is
1063 new Element_Keys.Generic_Unconditional_Insert_With_Hint
1071 function New_Node return Node_Access is
1072 Node : constant Node_Access :=
1073 new Node_Type'(Parent => null,
1077 Element => Src_Node.Element);
1082 -- Start of processing for Insert_With_Hint
1085 Local_Insert_With_Hint
1090 end Insert_With_Hint;
1096 procedure Intersection (Target : in out Set; Source : Set) is
1098 Set_Ops.Intersection (Target.Tree, Source.Tree);
1101 function Intersection (Left, Right : Set) return Set is
1102 Tree : constant Tree_Type :=
1103 Set_Ops.Intersection (Left.Tree, Right.Tree);
1105 return Set'(Controlled with Tree);
1112 function Is_Empty (Container : Set) return Boolean is
1114 return Container.Tree.Length = 0;
1117 ------------------------
1118 -- Is_Equal_Node_Node --
1119 ------------------------
1121 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
1123 return L.Element = R.Element;
1124 end Is_Equal_Node_Node;
1126 -----------------------------
1127 -- Is_Greater_Element_Node --
1128 -----------------------------
1130 function Is_Greater_Element_Node
1131 (Left : Element_Type;
1132 Right : Node_Access) return Boolean
1135 -- e > node same as node < e
1137 return Right.Element < Left;
1138 end Is_Greater_Element_Node;
1140 --------------------------
1141 -- Is_Less_Element_Node --
1142 --------------------------
1144 function Is_Less_Element_Node
1145 (Left : Element_Type;
1146 Right : Node_Access) return Boolean
1149 return Left < Right.Element;
1150 end Is_Less_Element_Node;
1152 -----------------------
1153 -- Is_Less_Node_Node --
1154 -----------------------
1156 function Is_Less_Node_Node (L, R : Node_Access) return Boolean is
1158 return L.Element < R.Element;
1159 end Is_Less_Node_Node;
1165 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1167 return Set_Ops.Is_Subset (Subset => Subset.Tree, Of_Set => Of_Set.Tree);
1176 Process : not null access procedure (Position : Cursor))
1178 procedure Process_Node (Node : Node_Access);
1179 pragma Inline (Process_Node);
1181 procedure Local_Iterate is
1182 new Tree_Operations.Generic_Iteration (Process_Node);
1188 procedure Process_Node (Node : Node_Access) is
1190 Process (Cursor'(Container'Unrestricted_Access, Node));
1193 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1194 B : Natural renames T.Busy;
1196 -- Start of processing for Iterate
1214 Item : Element_Type;
1215 Process : not null access procedure (Position : Cursor))
1217 procedure Process_Node (Node : Node_Access);
1218 pragma Inline (Process_Node);
1220 procedure Local_Iterate is
1221 new Element_Keys.Generic_Iteration (Process_Node);
1227 procedure Process_Node (Node : Node_Access) is
1229 Process (Cursor'(Container'Unrestricted_Access, Node));
1232 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1233 B : Natural renames T.Busy;
1235 -- Start of processing for Iterate
1241 Local_Iterate (T, Item);
1255 function Last (Container : Set) return Cursor is
1257 if Container.Tree.Last = null then
1261 return Cursor'(Container'Unrestricted_Access, Container.Tree.Last);
1268 function Last_Element (Container : Set) return Element_Type is
1270 if Container.Tree.Last = null then
1271 raise Constraint_Error with "set is empty";
1274 return Container.Tree.Last.Element;
1281 function Left (Node : Node_Access) return Node_Access is
1290 function Length (Container : Set) return Count_Type is
1292 return Container.Tree.Length;
1300 new Tree_Operations.Generic_Move (Clear);
1302 procedure Move (Target : in out Set; Source : in out Set) is
1304 Move (Target => Target.Tree, Source => Source.Tree);
1311 procedure Next (Position : in out Cursor)
1314 Position := Next (Position);
1317 function Next (Position : Cursor) return Cursor is
1319 if Position = No_Element then
1323 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1324 "bad cursor in Next");
1327 Node : constant Node_Access :=
1328 Tree_Operations.Next (Position.Node);
1334 return Cursor'(Position.Container, Node);
1342 function Overlap (Left, Right : Set) return Boolean is
1344 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1351 function Parent (Node : Node_Access) return Node_Access is
1360 procedure Previous (Position : in out Cursor)
1363 Position := Previous (Position);
1366 function Previous (Position : Cursor) return Cursor is
1368 if Position = No_Element then
1372 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1373 "bad cursor in Previous");
1376 Node : constant Node_Access :=
1377 Tree_Operations.Previous (Position.Node);
1383 return Cursor'(Position.Container, Node);
1391 procedure Query_Element
1393 Process : not null access procedure (Element : Element_Type))
1396 if Position.Node = null then
1397 raise Constraint_Error with "Position cursor equals No_Element";
1400 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1401 "bad cursor in Query_Element");
1404 T : Tree_Type renames Position.Container.Tree;
1406 B : Natural renames T.Busy;
1407 L : Natural renames T.Lock;
1414 Process (Position.Node.Element);
1432 (Stream : access Root_Stream_Type'Class;
1433 Container : out Set)
1436 (Stream : access Root_Stream_Type'Class) return Node_Access;
1437 pragma Inline (Read_Node);
1440 new Tree_Operations.Generic_Read (Clear, Read_Node);
1447 (Stream : access Root_Stream_Type'Class) return Node_Access
1449 Node : Node_Access := new Node_Type;
1451 Element_Type'Read (Stream, Node.Element);
1455 Free (Node); -- Note that Free deallocates elem too
1459 -- Start of processing for Read
1462 Read (Stream, Container.Tree);
1466 (Stream : access Root_Stream_Type'Class;
1470 raise Program_Error with "attempt to stream set cursor";
1473 ---------------------
1474 -- Replace_Element --
1475 ---------------------
1477 procedure Replace_Element
1478 (Tree : in out Tree_Type;
1480 Item : Element_Type)
1483 if Item < Node.Element
1484 or else Node.Element < Item
1488 if Tree.Lock > 0 then
1489 raise Program_Error with
1490 "attempt to tamper with cursors (set is locked)";
1493 Node.Element := Item;
1497 Tree_Operations.Delete_Node_Sans_Free (Tree, Node); -- Checks busy-bit
1499 Insert_New_Item : declare
1500 function New_Node return Node_Access;
1501 pragma Inline (New_Node);
1503 procedure Insert_Post is
1504 new Element_Keys.Generic_Insert_Post (New_Node);
1506 procedure Unconditional_Insert is
1507 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1513 function New_Node return Node_Access is
1515 Node.Element := Item;
1516 Node.Color := Red_Black_Trees.Red;
1517 Node.Parent := null;
1524 Result : Node_Access;
1526 -- Start of processing for Insert_New_Item
1529 Unconditional_Insert
1534 pragma Assert (Result = Node);
1535 end Insert_New_Item;
1536 end Replace_Element;
1538 procedure Replace_Element
1539 (Container : in out Set;
1541 New_Item : Element_Type)
1544 if Position.Node = null then
1545 raise Constraint_Error with
1546 "Position cursor equals No_Element";
1549 if Position.Container /= Container'Unrestricted_Access then
1550 raise Program_Error with
1551 "Position cursor designates wrong set";
1554 pragma Assert (Vet (Container.Tree, Position.Node),
1555 "bad cursor in Replace_Element");
1557 Replace_Element (Container.Tree, Position.Node, New_Item);
1558 end Replace_Element;
1560 ---------------------
1561 -- Reverse_Iterate --
1562 ---------------------
1564 procedure Reverse_Iterate
1566 Process : not null access procedure (Position : Cursor))
1568 procedure Process_Node (Node : Node_Access);
1569 pragma Inline (Process_Node);
1571 procedure Local_Reverse_Iterate is
1572 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1578 procedure Process_Node (Node : Node_Access) is
1580 Process (Cursor'(Container'Unrestricted_Access, Node));
1583 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1584 B : Natural renames T.Busy;
1586 -- Start of processing for Reverse_Iterate
1592 Local_Reverse_Iterate (T);
1600 end Reverse_Iterate;
1602 procedure Reverse_Iterate
1604 Item : Element_Type;
1605 Process : not null access procedure (Position : Cursor))
1607 procedure Process_Node (Node : Node_Access);
1608 pragma Inline (Process_Node);
1610 procedure Local_Reverse_Iterate is
1611 new Element_Keys.Generic_Reverse_Iteration (Process_Node);
1617 procedure Process_Node (Node : Node_Access) is
1619 Process (Cursor'(Container'Unrestricted_Access, Node));
1622 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1623 B : Natural renames T.Busy;
1625 -- Start of processing for Reverse_Iterate
1631 Local_Reverse_Iterate (T, Item);
1639 end Reverse_Iterate;
1645 function Right (Node : Node_Access) return Node_Access is
1654 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1656 Node.Color := Color;
1663 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1672 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1674 Node.Parent := Parent;
1681 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1683 Node.Right := Right;
1686 --------------------------
1687 -- Symmetric_Difference --
1688 --------------------------
1690 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1692 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
1693 end Symmetric_Difference;
1695 function Symmetric_Difference (Left, Right : Set) return Set is
1696 Tree : constant Tree_Type :=
1697 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
1699 return Set'(Controlled with Tree);
1700 end Symmetric_Difference;
1706 function To_Set (New_Item : Element_Type) return Set is
1711 Insert_Sans_Hint (Tree, New_Item, Node);
1712 return Set'(Controlled with Tree);
1719 procedure Union (Target : in out Set; Source : Set) is
1721 Set_Ops.Union (Target.Tree, Source.Tree);
1724 function Union (Left, Right : Set) return Set is
1725 Tree : constant Tree_Type :=
1726 Set_Ops.Union (Left.Tree, Right.Tree);
1728 return Set'(Controlled with Tree);
1736 (Stream : access Root_Stream_Type'Class;
1739 procedure Write_Node
1740 (Stream : access Root_Stream_Type'Class;
1741 Node : Node_Access);
1742 pragma Inline (Write_Node);
1745 new Tree_Operations.Generic_Write (Write_Node);
1751 procedure Write_Node
1752 (Stream : access Root_Stream_Type'Class;
1756 Element_Type'Write (Stream, Node.Element);
1759 -- Start of processing for Write
1762 Write (Stream, Container.Tree);
1766 (Stream : access Root_Stream_Type'Class;
1770 raise Program_Error with "attempt to stream set cursor";
1773 end Ada.Containers.Ordered_Multisets;