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-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_Multisets is
45 -----------------------------
46 -- Node Access Subprograms --
47 -----------------------------
49 -- These subprograms provide a functional interface to access fields
50 -- of a node, and a procedural interface for modifying these values.
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_Parent (Node : Node_Access; Parent : Node_Access);
65 pragma Inline (Set_Parent);
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_Color (Node : Node_Access; Color : Color_Type);
74 pragma Inline (Set_Color);
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);
90 procedure Insert_With_Hint
91 (Dst_Tree : in out Tree_Type;
92 Dst_Hint : Node_Access;
93 Src_Node : Node_Access;
94 Dst_Node : out Node_Access);
96 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean;
97 pragma Inline (Is_Equal_Node_Node);
99 function Is_Greater_Element_Node
100 (Left : Element_Type;
101 Right : Node_Access) return Boolean;
102 pragma Inline (Is_Greater_Element_Node);
104 function Is_Less_Element_Node
105 (Left : Element_Type;
106 Right : Node_Access) return Boolean;
107 pragma Inline (Is_Less_Element_Node);
109 function Is_Less_Node_Node (L, R : Node_Access) return Boolean;
110 pragma Inline (Is_Less_Node_Node);
112 procedure Replace_Element
113 (Tree : in out Tree_Type;
115 Item : Element_Type);
117 --------------------------
118 -- Local Instantiations --
119 --------------------------
121 package Tree_Operations is
122 new Red_Black_Trees.Generic_Operations (Tree_Types);
124 procedure Delete_Tree is
125 new Tree_Operations.Generic_Delete_Tree (Free);
127 function Copy_Tree is
128 new Tree_Operations.Generic_Copy_Tree (Copy_Node, Delete_Tree);
133 new Tree_Operations.Generic_Equal (Is_Equal_Node_Node);
135 package Element_Keys is
136 new Red_Black_Trees.Generic_Keys
137 (Tree_Operations => Tree_Operations,
138 Key_Type => Element_Type,
139 Is_Less_Key_Node => Is_Less_Element_Node,
140 Is_Greater_Key_Node => Is_Greater_Element_Node);
143 new Generic_Set_Operations
144 (Tree_Operations => Tree_Operations,
145 Insert_With_Hint => Insert_With_Hint,
146 Copy_Tree => Copy_Tree,
147 Delete_Tree => Delete_Tree,
148 Is_Less => Is_Less_Node_Node,
155 function "<" (Left, Right : Cursor) return Boolean is
157 if Left.Node = null then
158 raise Constraint_Error with "Left cursor equals No_Element";
161 if Right.Node = null then
162 raise Constraint_Error with "Right cursor equals No_Element";
165 pragma Assert (Vet (Left.Container.Tree, Left.Node),
166 "bad Left cursor in ""<""");
168 pragma Assert (Vet (Right.Container.Tree, Right.Node),
169 "bad Right cursor in ""<""");
171 return Left.Node.Element < Right.Node.Element;
174 function "<" (Left : Cursor; Right : Element_Type)
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)
190 if Right.Node = null then
191 raise Constraint_Error with "Right cursor equals No_Element";
194 pragma Assert (Vet (Right.Container.Tree, Right.Node),
195 "bad Right cursor in ""<""");
197 return Left < Right.Node.Element;
204 function "=" (Left, Right : Set) return Boolean is
206 return Is_Equal (Left.Tree, Right.Tree);
213 function ">" (Left, Right : Cursor) return Boolean is
215 if Left.Node = null then
216 raise Constraint_Error with "Left cursor equals No_Element";
219 if Right.Node = null then
220 raise Constraint_Error with "Right cursor equals No_Element";
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 with "Left cursor equals No_Element";
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 with "Right cursor equals No_Element";
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 with
346 "attempt to delete element not in set";
351 Node := Tree_Operations.Next (Node);
352 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
355 exit when Node = Done;
359 procedure Delete (Container : in out Set; Position : in out Cursor) is
361 if Position.Node = null then
362 raise Constraint_Error with "Position cursor equals No_Element";
365 if Position.Container /= Container'Unrestricted_Access then
366 raise Program_Error with "Position cursor designates wrong set";
369 pragma Assert (Vet (Container.Tree, Position.Node),
370 "bad cursor in Delete");
372 Delete_Node_Sans_Free (Container.Tree, Position.Node);
373 Free (Position.Node);
375 Position.Container := null;
382 procedure Delete_First (Container : in out Set) is
383 Tree : Tree_Type renames Container.Tree;
384 X : Node_Access := Tree.First;
391 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
399 procedure Delete_Last (Container : in out Set) is
400 Tree : Tree_Type renames Container.Tree;
401 X : Node_Access := Tree.Last;
408 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
416 procedure Difference (Target : in out Set; Source : Set) is
418 Set_Ops.Difference (Target.Tree, Source.Tree);
421 function Difference (Left, Right : Set) return Set is
422 Tree : constant Tree_Type :=
423 Set_Ops.Difference (Left.Tree, Right.Tree);
425 return Set'(Controlled with Tree);
432 function Element (Position : Cursor) return Element_Type is
434 if Position.Node = null then
435 raise Constraint_Error with "Position cursor equals No_Element";
438 pragma Assert (Vet (Position.Container.Tree, Position.Node),
439 "bad cursor in Element");
441 return Position.Node.Element;
444 -------------------------
445 -- Equivalent_Elements --
446 -------------------------
448 function Equivalent_Elements (Left, Right : Element_Type) return Boolean is
457 end Equivalent_Elements;
459 ---------------------
460 -- Equivalent_Sets --
461 ---------------------
463 function Equivalent_Sets (Left, Right : Set) return Boolean is
465 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean;
466 pragma Inline (Is_Equivalent_Node_Node);
468 function Is_Equivalent is
469 new Tree_Operations.Generic_Equal (Is_Equivalent_Node_Node);
471 -----------------------------
472 -- Is_Equivalent_Node_Node --
473 -----------------------------
475 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean is
477 if L.Element < R.Element then
479 elsif R.Element < L.Element then
484 end Is_Equivalent_Node_Node;
486 -- Start of processing for Equivalent_Sets
489 return Is_Equivalent (Left.Tree, Right.Tree);
496 procedure Exclude (Container : in out Set; Item : Element_Type) is
497 Tree : Tree_Type renames Container.Tree;
498 Node : Node_Access := Element_Keys.Ceiling (Tree, Item);
499 Done : constant Node_Access := Element_Keys.Upper_Bound (Tree, Item);
502 while Node /= Done loop
504 Node := Tree_Operations.Next (Node);
505 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
514 function Find (Container : Set; Item : Element_Type) return Cursor is
515 Node : constant Node_Access :=
516 Element_Keys.Find (Container.Tree, Item);
523 return Cursor'(Container'Unrestricted_Access, Node);
530 function First (Container : Set) return Cursor is
532 if Container.Tree.First = null then
536 return Cursor'(Container'Unrestricted_Access, Container.Tree.First);
543 function First_Element (Container : Set) return Element_Type is
545 if Container.Tree.First = null then
546 raise Constraint_Error with "set is empty";
549 return Container.Tree.First.Element;
556 function Floor (Container : Set; Item : Element_Type) return Cursor is
557 Node : constant Node_Access :=
558 Element_Keys.Floor (Container.Tree, Item);
565 return Cursor'(Container'Unrestricted_Access, Node);
572 procedure Free (X : in out Node_Access) is
573 procedure Deallocate is
574 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
590 package body Generic_Keys is
592 -----------------------
593 -- Local Subprograms --
594 -----------------------
596 function Is_Greater_Key_Node
598 Right : Node_Access) return Boolean;
599 pragma Inline (Is_Greater_Key_Node);
601 function Is_Less_Key_Node
603 Right : Node_Access) return Boolean;
604 pragma Inline (Is_Less_Key_Node);
606 --------------------------
607 -- Local_Instantiations --
608 --------------------------
611 new Red_Black_Trees.Generic_Keys
612 (Tree_Operations => Tree_Operations,
613 Key_Type => Key_Type,
614 Is_Less_Key_Node => Is_Less_Key_Node,
615 Is_Greater_Key_Node => Is_Greater_Key_Node);
621 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
622 Node : constant Node_Access :=
623 Key_Keys.Ceiling (Container.Tree, Key);
630 return Cursor'(Container'Unrestricted_Access, Node);
637 function Contains (Container : Set; Key : Key_Type) return Boolean is
639 return Find (Container, Key) /= No_Element;
646 procedure Delete (Container : in out Set; Key : Key_Type) is
647 Tree : Tree_Type renames Container.Tree;
648 Node : Node_Access := Key_Keys.Ceiling (Tree, Key);
649 Done : constant Node_Access := Key_Keys.Upper_Bound (Tree, Key);
654 raise Constraint_Error with "attempt to delete key not in set";
659 Node := Tree_Operations.Next (Node);
660 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
663 exit when Node = Done;
671 function Element (Container : Set; Key : Key_Type) return Element_Type is
672 Node : constant Node_Access :=
673 Key_Keys.Find (Container.Tree, Key);
676 raise Constraint_Error with "key not in set";
682 ---------------------
683 -- Equivalent_Keys --
684 ---------------------
686 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
701 procedure Exclude (Container : in out Set; Key : Key_Type) is
702 Tree : Tree_Type renames Container.Tree;
703 Node : Node_Access := Key_Keys.Ceiling (Tree, Key);
704 Done : constant Node_Access := Key_Keys.Upper_Bound (Tree, Key);
708 while Node /= Done loop
710 Node := Tree_Operations.Next (Node);
711 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
720 function Find (Container : Set; Key : Key_Type) return Cursor is
721 Node : constant Node_Access :=
722 Key_Keys.Find (Container.Tree, Key);
729 return Cursor'(Container'Unrestricted_Access, Node);
736 function Floor (Container : Set; Key : Key_Type) return Cursor is
737 Node : constant Node_Access :=
738 Key_Keys.Floor (Container.Tree, Key);
745 return Cursor'(Container'Unrestricted_Access, Node);
748 -------------------------
749 -- Is_Greater_Key_Node --
750 -------------------------
752 function Is_Greater_Key_Node
754 Right : Node_Access) return Boolean is
756 return Key (Right.Element) < Left;
757 end Is_Greater_Key_Node;
759 ----------------------
760 -- Is_Less_Key_Node --
761 ----------------------
763 function Is_Less_Key_Node
765 Right : Node_Access) return Boolean is
767 return Left < Key (Right.Element);
768 end Is_Less_Key_Node;
777 Process : not null access procedure (Position : Cursor))
779 procedure Process_Node (Node : Node_Access);
780 pragma Inline (Process_Node);
782 procedure Local_Iterate is
783 new Key_Keys.Generic_Iteration (Process_Node);
789 procedure Process_Node (Node : Node_Access) is
791 Process (Cursor'(Container'Unrestricted_Access, Node));
794 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
795 B : Natural renames T.Busy;
797 -- Start of processing for Iterate
803 Local_Iterate (T, Key);
817 function Key (Position : Cursor) return Key_Type is
819 if Position.Node = null then
820 raise Constraint_Error with
821 "Position cursor equals No_Element";
824 pragma Assert (Vet (Position.Container.Tree, Position.Node),
825 "bad cursor in Key");
827 return Key (Position.Node.Element);
830 ---------------------
831 -- Reverse_Iterate --
832 ---------------------
834 procedure Reverse_Iterate
837 Process : not null access procedure (Position : Cursor))
839 procedure Process_Node (Node : Node_Access);
840 pragma Inline (Process_Node);
842 procedure Local_Reverse_Iterate is
843 new Key_Keys.Generic_Reverse_Iteration (Process_Node);
849 procedure Process_Node (Node : Node_Access) is
851 Process (Cursor'(Container'Unrestricted_Access, Node));
854 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
855 B : Natural renames T.Busy;
857 -- Start of processing for Reverse_Iterate
863 Local_Reverse_Iterate (T, Key);
877 procedure Update_Element
878 (Container : in out Set;
880 Process : not null access procedure (Element : in out Element_Type))
882 Tree : Tree_Type renames Container.Tree;
883 Node : constant Node_Access := Position.Node;
887 raise Constraint_Error with
888 "Position cursor equals No_Element";
891 if Position.Container /= Container'Unrestricted_Access then
892 raise Program_Error with
893 "Position cursor designates wrong set";
896 pragma Assert (Vet (Tree, Node),
897 "bad cursor in Update_Element");
900 E : Element_Type renames Node.Element;
901 K : constant Key_Type := Key (E);
903 B : Natural renames Tree.Busy;
904 L : Natural renames Tree.Lock;
922 if Equivalent_Keys (Left => K, Right => Key (E)) then
927 -- Delete_Node checks busy-bit
929 Tree_Operations.Delete_Node_Sans_Free (Tree, Node);
931 Insert_New_Item : declare
932 function New_Node return Node_Access;
933 pragma Inline (New_Node);
935 procedure Insert_Post is
936 new Element_Keys.Generic_Insert_Post (New_Node);
938 procedure Unconditional_Insert is
939 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
945 function New_Node return Node_Access is
947 Node.Color := Red_Black_Trees.Red;
955 Result : Node_Access;
957 -- Start of processing for Insert_New_Item
965 pragma Assert (Result = Node);
975 function Has_Element (Position : Cursor) return Boolean is
977 return Position /= No_Element;
984 procedure Insert (Container : in out Set; New_Item : Element_Type) is
986 pragma Unreferenced (Position);
988 Insert (Container, New_Item, Position);
992 (Container : in out Set;
993 New_Item : Element_Type;
994 Position : out Cursor)
997 Insert_Sans_Hint (Container.Tree, New_Item, Position.Node);
998 Position.Container := Container'Unrestricted_Access;
1001 ----------------------
1002 -- Insert_Sans_Hint --
1003 ----------------------
1005 procedure Insert_Sans_Hint
1006 (Tree : in out Tree_Type;
1007 New_Item : Element_Type;
1008 Node : out Node_Access)
1010 function New_Node return Node_Access;
1011 pragma Inline (New_Node);
1013 procedure Insert_Post is
1014 new Element_Keys.Generic_Insert_Post (New_Node);
1016 procedure Unconditional_Insert is
1017 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1023 function New_Node return Node_Access is
1024 Node : constant Node_Access :=
1025 new Node_Type'(Parent => null,
1028 Color => Red_Black_Trees.Red,
1029 Element => New_Item);
1034 -- Start of processing for Insert_Sans_Hint
1037 Unconditional_Insert (Tree, New_Item, Node);
1038 end Insert_Sans_Hint;
1040 ----------------------
1041 -- Insert_With_Hint --
1042 ----------------------
1044 procedure Insert_With_Hint
1045 (Dst_Tree : in out Tree_Type;
1046 Dst_Hint : Node_Access;
1047 Src_Node : Node_Access;
1048 Dst_Node : out Node_Access)
1050 function New_Node return Node_Access;
1051 pragma Inline (New_Node);
1053 procedure Insert_Post is
1054 new Element_Keys.Generic_Insert_Post (New_Node);
1056 procedure Insert_Sans_Hint is
1057 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1059 procedure Local_Insert_With_Hint is
1060 new Element_Keys.Generic_Unconditional_Insert_With_Hint
1068 function New_Node return Node_Access is
1069 Node : constant Node_Access :=
1070 new Node_Type'(Parent => null,
1074 Element => Src_Node.Element);
1079 -- Start of processing for Insert_With_Hint
1082 Local_Insert_With_Hint
1087 end Insert_With_Hint;
1093 procedure Intersection (Target : in out Set; Source : Set) is
1095 Set_Ops.Intersection (Target.Tree, Source.Tree);
1098 function Intersection (Left, Right : Set) return Set is
1099 Tree : constant Tree_Type :=
1100 Set_Ops.Intersection (Left.Tree, Right.Tree);
1102 return Set'(Controlled with Tree);
1109 function Is_Empty (Container : Set) return Boolean is
1111 return Container.Tree.Length = 0;
1114 ------------------------
1115 -- Is_Equal_Node_Node --
1116 ------------------------
1118 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
1120 return L.Element = R.Element;
1121 end Is_Equal_Node_Node;
1123 -----------------------------
1124 -- Is_Greater_Element_Node --
1125 -----------------------------
1127 function Is_Greater_Element_Node
1128 (Left : Element_Type;
1129 Right : Node_Access) return Boolean
1132 -- e > node same as node < e
1134 return Right.Element < Left;
1135 end Is_Greater_Element_Node;
1137 --------------------------
1138 -- Is_Less_Element_Node --
1139 --------------------------
1141 function Is_Less_Element_Node
1142 (Left : Element_Type;
1143 Right : Node_Access) return Boolean
1146 return Left < Right.Element;
1147 end Is_Less_Element_Node;
1149 -----------------------
1150 -- Is_Less_Node_Node --
1151 -----------------------
1153 function Is_Less_Node_Node (L, R : Node_Access) return Boolean is
1155 return L.Element < R.Element;
1156 end Is_Less_Node_Node;
1162 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1164 return Set_Ops.Is_Subset (Subset => Subset.Tree, Of_Set => Of_Set.Tree);
1173 Process : not null access procedure (Position : Cursor))
1175 procedure Process_Node (Node : Node_Access);
1176 pragma Inline (Process_Node);
1178 procedure Local_Iterate is
1179 new Tree_Operations.Generic_Iteration (Process_Node);
1185 procedure Process_Node (Node : Node_Access) is
1187 Process (Cursor'(Container'Unrestricted_Access, Node));
1190 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1191 B : Natural renames T.Busy;
1193 -- Start of processing for Iterate
1211 Item : Element_Type;
1212 Process : not null access procedure (Position : Cursor))
1214 procedure Process_Node (Node : Node_Access);
1215 pragma Inline (Process_Node);
1217 procedure Local_Iterate is
1218 new Element_Keys.Generic_Iteration (Process_Node);
1224 procedure Process_Node (Node : Node_Access) is
1226 Process (Cursor'(Container'Unrestricted_Access, Node));
1229 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1230 B : Natural renames T.Busy;
1232 -- Start of processing for Iterate
1238 Local_Iterate (T, Item);
1252 function Last (Container : Set) return Cursor is
1254 if Container.Tree.Last = null then
1258 return Cursor'(Container'Unrestricted_Access, Container.Tree.Last);
1265 function Last_Element (Container : Set) return Element_Type is
1267 if Container.Tree.Last = null then
1268 raise Constraint_Error with "set is empty";
1271 return Container.Tree.Last.Element;
1278 function Left (Node : Node_Access) return Node_Access is
1287 function Length (Container : Set) return Count_Type is
1289 return Container.Tree.Length;
1297 new Tree_Operations.Generic_Move (Clear);
1299 procedure Move (Target : in out Set; Source : in out Set) is
1301 Move (Target => Target.Tree, Source => Source.Tree);
1308 procedure Next (Position : in out Cursor)
1311 Position := Next (Position);
1314 function Next (Position : Cursor) return Cursor is
1316 if Position = No_Element then
1320 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1321 "bad cursor in Next");
1324 Node : constant Node_Access :=
1325 Tree_Operations.Next (Position.Node);
1331 return Cursor'(Position.Container, Node);
1339 function Overlap (Left, Right : Set) return Boolean is
1341 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1348 function Parent (Node : Node_Access) return Node_Access is
1357 procedure Previous (Position : in out Cursor)
1360 Position := Previous (Position);
1363 function Previous (Position : Cursor) return Cursor is
1365 if Position = No_Element then
1369 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1370 "bad cursor in Previous");
1373 Node : constant Node_Access :=
1374 Tree_Operations.Previous (Position.Node);
1380 return Cursor'(Position.Container, Node);
1388 procedure Query_Element
1390 Process : not null access procedure (Element : Element_Type))
1393 if Position.Node = null then
1394 raise Constraint_Error with "Position cursor equals No_Element";
1397 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1398 "bad cursor in Query_Element");
1401 T : Tree_Type renames Position.Container.Tree;
1403 B : Natural renames T.Busy;
1404 L : Natural renames T.Lock;
1411 Process (Position.Node.Element);
1429 (Stream : not null access Root_Stream_Type'Class;
1430 Container : out Set)
1433 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1434 pragma Inline (Read_Node);
1437 new Tree_Operations.Generic_Read (Clear, Read_Node);
1444 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1446 Node : Node_Access := new Node_Type;
1448 Element_Type'Read (Stream, Node.Element);
1452 Free (Node); -- Note that Free deallocates elem too
1456 -- Start of processing for Read
1459 Read (Stream, Container.Tree);
1463 (Stream : not null access Root_Stream_Type'Class;
1467 raise Program_Error with "attempt to stream set cursor";
1470 ---------------------
1471 -- Replace_Element --
1472 ---------------------
1474 procedure Replace_Element
1475 (Tree : in out Tree_Type;
1477 Item : Element_Type)
1480 if Item < Node.Element
1481 or else Node.Element < Item
1485 if Tree.Lock > 0 then
1486 raise Program_Error with
1487 "attempt to tamper with cursors (set is locked)";
1490 Node.Element := Item;
1494 Tree_Operations.Delete_Node_Sans_Free (Tree, Node); -- Checks busy-bit
1496 Insert_New_Item : declare
1497 function New_Node return Node_Access;
1498 pragma Inline (New_Node);
1500 procedure Insert_Post is
1501 new Element_Keys.Generic_Insert_Post (New_Node);
1503 procedure Unconditional_Insert is
1504 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1510 function New_Node return Node_Access is
1512 Node.Element := Item;
1513 Node.Color := Red_Black_Trees.Red;
1514 Node.Parent := null;
1521 Result : Node_Access;
1523 -- Start of processing for Insert_New_Item
1526 Unconditional_Insert
1531 pragma Assert (Result = Node);
1532 end Insert_New_Item;
1533 end Replace_Element;
1535 procedure Replace_Element
1536 (Container : in out Set;
1538 New_Item : Element_Type)
1541 if Position.Node = null then
1542 raise Constraint_Error with
1543 "Position cursor equals No_Element";
1546 if Position.Container /= Container'Unrestricted_Access then
1547 raise Program_Error with
1548 "Position cursor designates wrong set";
1551 pragma Assert (Vet (Container.Tree, Position.Node),
1552 "bad cursor in Replace_Element");
1554 Replace_Element (Container.Tree, Position.Node, New_Item);
1555 end Replace_Element;
1557 ---------------------
1558 -- Reverse_Iterate --
1559 ---------------------
1561 procedure Reverse_Iterate
1563 Process : not null access procedure (Position : Cursor))
1565 procedure Process_Node (Node : Node_Access);
1566 pragma Inline (Process_Node);
1568 procedure Local_Reverse_Iterate is
1569 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1575 procedure Process_Node (Node : Node_Access) is
1577 Process (Cursor'(Container'Unrestricted_Access, Node));
1580 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1581 B : Natural renames T.Busy;
1583 -- Start of processing for Reverse_Iterate
1589 Local_Reverse_Iterate (T);
1597 end Reverse_Iterate;
1599 procedure Reverse_Iterate
1601 Item : Element_Type;
1602 Process : not null access procedure (Position : Cursor))
1604 procedure Process_Node (Node : Node_Access);
1605 pragma Inline (Process_Node);
1607 procedure Local_Reverse_Iterate is
1608 new Element_Keys.Generic_Reverse_Iteration (Process_Node);
1614 procedure Process_Node (Node : Node_Access) is
1616 Process (Cursor'(Container'Unrestricted_Access, Node));
1619 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1620 B : Natural renames T.Busy;
1622 -- Start of processing for Reverse_Iterate
1628 Local_Reverse_Iterate (T, Item);
1636 end Reverse_Iterate;
1642 function Right (Node : Node_Access) return Node_Access is
1651 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1653 Node.Color := Color;
1660 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1669 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1671 Node.Parent := Parent;
1678 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1680 Node.Right := Right;
1683 --------------------------
1684 -- Symmetric_Difference --
1685 --------------------------
1687 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1689 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
1690 end Symmetric_Difference;
1692 function Symmetric_Difference (Left, Right : Set) return Set is
1693 Tree : constant Tree_Type :=
1694 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
1696 return Set'(Controlled with Tree);
1697 end Symmetric_Difference;
1703 function To_Set (New_Item : Element_Type) return Set is
1706 pragma Unreferenced (Node);
1708 Insert_Sans_Hint (Tree, New_Item, Node);
1709 return Set'(Controlled with Tree);
1716 procedure Union (Target : in out Set; Source : Set) is
1718 Set_Ops.Union (Target.Tree, Source.Tree);
1721 function Union (Left, Right : Set) return Set is
1722 Tree : constant Tree_Type :=
1723 Set_Ops.Union (Left.Tree, Right.Tree);
1725 return Set'(Controlled with Tree);
1733 (Stream : not null access Root_Stream_Type'Class;
1736 procedure Write_Node
1737 (Stream : not null access Root_Stream_Type'Class;
1738 Node : Node_Access);
1739 pragma Inline (Write_Node);
1742 new Tree_Operations.Generic_Write (Write_Node);
1748 procedure Write_Node
1749 (Stream : not null access Root_Stream_Type'Class;
1753 Element_Type'Write (Stream, Node.Element);
1756 -- Start of processing for Write
1759 Write (Stream, Container.Tree);
1763 (Stream : not null access Root_Stream_Type'Class;
1767 raise Program_Error with "attempt to stream set cursor";
1770 end Ada.Containers.Ordered_Multisets;