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-2020, 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 3, 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. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- This unit was originally developed by Matthew J Heaney. --
28 ------------------------------------------------------------------------------
30 with Ada.Unchecked_Deallocation;
32 with Ada.Containers.Red_Black_Trees.Generic_Operations;
33 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Operations);
35 with Ada.Containers.Red_Black_Trees.Generic_Keys;
36 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Keys);
38 with Ada.Containers.Red_Black_Trees.Generic_Set_Operations;
39 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Set_Operations);
41 with System; use type System.Address;
43 package body Ada.Containers.Ordered_Multisets is
45 pragma Warnings (Off, "variable ""Busy*"" is not referenced");
46 pragma Warnings (Off, "variable ""Lock*"" is not referenced");
47 -- See comment in Ada.Containers.Helpers
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;
268 procedure Adjust is new Tree_Operations.Generic_Adjust (Copy_Tree);
270 procedure Adjust (Container : in out Set) is
272 Adjust (Container.Tree);
279 procedure Assign (Target : in out Set; Source : Set) is
281 if Target'Address = Source'Address then
286 Target.Union (Source);
293 function Ceiling (Container : Set; Item : Element_Type) return Cursor is
294 Node : constant Node_Access :=
295 Element_Keys.Ceiling (Container.Tree, Item);
302 return Cursor'(Container'Unrestricted_Access, Node);
310 new Tree_Operations.Generic_Clear (Delete_Tree);
312 procedure Clear (Container : in out Set) is
314 Clear (Container.Tree);
321 function Color (Node : Node_Access) return Color_Type is
326 ------------------------
327 -- Constant_Reference --
328 ------------------------
330 function Constant_Reference
331 (Container : aliased Set;
332 Position : Cursor) return Constant_Reference_Type
335 if Position.Container = null then
336 raise Constraint_Error with "Position cursor has no element";
339 if Position.Container /= Container'Unrestricted_Access then
340 raise Program_Error with
341 "Position cursor designates wrong container";
344 pragma Assert (Vet (Position.Container.Tree, Position.Node),
345 "bad cursor in Constant_Reference");
347 -- Note: in predefined container units, the creation of a reference
348 -- increments the busy bit of the container, and its finalization
349 -- decrements it. In the absence of control machinery, this tampering
350 -- protection is missing.
353 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
354 pragma Unreferenced (T);
356 return R : constant Constant_Reference_Type :=
357 (Element => Position.Node.Element'Unrestricted_Access,
358 Control => (Container => Container'Unrestricted_Access))
363 end Constant_Reference;
369 function Contains (Container : Set; Item : Element_Type) return Boolean is
371 return Find (Container, Item) /= No_Element;
378 function Copy (Source : Set) return Set is
380 return Target : Set do
381 Target.Assign (Source);
389 function Copy_Node (Source : Node_Access) return Node_Access is
390 Target : constant Node_Access :=
391 new Node_Type'(Parent => null,
394 Color => Source.Color,
395 Element => Source.Element);
404 procedure Delete (Container : in out Set; Item : Element_Type) is
405 Tree : Tree_Type renames Container.Tree;
406 Node : Node_Access := Element_Keys.Ceiling (Tree, Item);
407 Done : constant Node_Access := Element_Keys.Upper_Bound (Tree, Item);
412 raise Constraint_Error with
413 "attempt to delete element not in set";
418 Node := Tree_Operations.Next (Node);
419 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
422 exit when Node = Done;
426 procedure Delete (Container : in out Set; Position : in out Cursor) is
428 if Position.Node = null then
429 raise Constraint_Error with "Position cursor equals No_Element";
432 if Position.Container /= Container'Unrestricted_Access then
433 raise Program_Error with "Position cursor designates wrong set";
436 pragma Assert (Vet (Container.Tree, Position.Node),
437 "bad cursor in Delete");
439 Delete_Node_Sans_Free (Container.Tree, Position.Node);
440 Free (Position.Node);
442 Position.Container := null;
449 procedure Delete_First (Container : in out Set) is
450 Tree : Tree_Type renames Container.Tree;
451 X : Node_Access := Tree.First;
458 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
466 procedure Delete_Last (Container : in out Set) is
467 Tree : Tree_Type renames Container.Tree;
468 X : Node_Access := Tree.Last;
475 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
483 procedure Difference (Target : in out Set; Source : Set) is
485 Set_Ops.Difference (Target.Tree, Source.Tree);
488 function Difference (Left, Right : Set) return Set is
489 Tree : constant Tree_Type :=
490 Set_Ops.Difference (Left.Tree, Right.Tree);
492 return Set'(Controlled with Tree);
499 function Element (Position : Cursor) return Element_Type is
501 if Position.Node = null then
502 raise Constraint_Error with "Position cursor equals No_Element";
506 and then (Left (Position.Node) = Position.Node
508 Right (Position.Node) = Position.Node)
510 raise Program_Error with "dangling cursor";
513 pragma Assert (Vet (Position.Container.Tree, Position.Node),
514 "bad cursor in Element");
516 return Position.Node.Element;
519 -------------------------
520 -- Equivalent_Elements --
521 -------------------------
523 function Equivalent_Elements (Left, Right : Element_Type) return Boolean is
532 end Equivalent_Elements;
534 ---------------------
535 -- Equivalent_Sets --
536 ---------------------
538 function Equivalent_Sets (Left, Right : Set) return Boolean is
540 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean;
541 pragma Inline (Is_Equivalent_Node_Node);
543 function Is_Equivalent is
544 new Tree_Operations.Generic_Equal (Is_Equivalent_Node_Node);
546 -----------------------------
547 -- Is_Equivalent_Node_Node --
548 -----------------------------
550 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean is
552 if L.Element < R.Element then
554 elsif R.Element < L.Element then
559 end Is_Equivalent_Node_Node;
561 -- Start of processing for Equivalent_Sets
564 return Is_Equivalent (Left.Tree, Right.Tree);
571 procedure Exclude (Container : in out Set; Item : Element_Type) is
572 Tree : Tree_Type renames Container.Tree;
573 Node : Node_Access := Element_Keys.Ceiling (Tree, Item);
574 Done : constant Node_Access := Element_Keys.Upper_Bound (Tree, Item);
577 while Node /= Done loop
579 Node := Tree_Operations.Next (Node);
580 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
589 procedure Finalize (Object : in out Iterator) is
591 Unbusy (Object.Container.Tree.TC);
598 function Find (Container : Set; Item : Element_Type) return Cursor is
599 Node : constant Node_Access :=
600 Element_Keys.Find (Container.Tree, Item);
607 return Cursor'(Container'Unrestricted_Access, Node);
614 function First (Container : Set) return Cursor is
616 if Container.Tree.First = null then
620 return Cursor'(Container'Unrestricted_Access, Container.Tree.First);
623 function First (Object : Iterator) return Cursor is
625 -- The value of the iterator object's Node component influences the
626 -- behavior of the First (and Last) selector function.
628 -- When the Node component is null, this means the iterator object was
629 -- constructed without a start expression, in which case the (forward)
630 -- iteration starts from the (logical) beginning of the entire sequence
631 -- of items (corresponding to Container.First, for a forward iterator).
633 -- Otherwise, this is iteration over a partial sequence of items. When
634 -- the Node component is non-null, the iterator object was constructed
635 -- with a start expression, that specifies the position from which the
636 -- (forward) partial iteration begins.
638 if Object.Node = null then
639 return Object.Container.First;
641 return Cursor'(Object.Container, Object.Node);
649 function First_Element (Container : Set) return Element_Type is
651 if Container.Tree.First = null then
652 raise Constraint_Error with "set is empty";
655 return Container.Tree.First.Element;
662 function Floor (Container : Set; Item : Element_Type) return Cursor is
663 Node : constant Node_Access :=
664 Element_Keys.Floor (Container.Tree, Item);
671 return Cursor'(Container'Unrestricted_Access, Node);
678 procedure Free (X : in out Node_Access) is
679 procedure Deallocate is
680 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
696 package body Generic_Keys is
698 -----------------------
699 -- Local Subprograms --
700 -----------------------
702 function Is_Greater_Key_Node
704 Right : Node_Access) return Boolean;
705 pragma Inline (Is_Greater_Key_Node);
707 function Is_Less_Key_Node
709 Right : Node_Access) return Boolean;
710 pragma Inline (Is_Less_Key_Node);
712 --------------------------
713 -- Local_Instantiations --
714 --------------------------
717 new Red_Black_Trees.Generic_Keys
718 (Tree_Operations => Tree_Operations,
719 Key_Type => Key_Type,
720 Is_Less_Key_Node => Is_Less_Key_Node,
721 Is_Greater_Key_Node => Is_Greater_Key_Node);
727 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
728 Node : constant Node_Access :=
729 Key_Keys.Ceiling (Container.Tree, Key);
736 return Cursor'(Container'Unrestricted_Access, Node);
743 function Contains (Container : Set; Key : Key_Type) return Boolean is
745 return Find (Container, Key) /= No_Element;
752 procedure Delete (Container : in out Set; Key : Key_Type) is
753 Tree : Tree_Type renames Container.Tree;
754 Node : Node_Access := Key_Keys.Ceiling (Tree, Key);
755 Done : constant Node_Access := Key_Keys.Upper_Bound (Tree, Key);
760 raise Constraint_Error with "attempt to delete key not in set";
765 Node := Tree_Operations.Next (Node);
766 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
769 exit when Node = Done;
777 function Element (Container : Set; Key : Key_Type) return Element_Type is
778 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
781 raise Constraint_Error with "key not in set";
787 ---------------------
788 -- Equivalent_Keys --
789 ---------------------
791 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
806 procedure Exclude (Container : in out Set; Key : Key_Type) is
807 Tree : Tree_Type renames Container.Tree;
808 Node : Node_Access := Key_Keys.Ceiling (Tree, Key);
809 Done : constant Node_Access := Key_Keys.Upper_Bound (Tree, Key);
813 while Node /= Done loop
815 Node := Tree_Operations.Next (Node);
816 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
825 function Find (Container : Set; Key : Key_Type) return Cursor is
826 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
833 return Cursor'(Container'Unrestricted_Access, Node);
840 function Floor (Container : Set; Key : Key_Type) return Cursor is
841 Node : constant Node_Access := Key_Keys.Floor (Container.Tree, Key);
848 return Cursor'(Container'Unrestricted_Access, Node);
851 -------------------------
852 -- Is_Greater_Key_Node --
853 -------------------------
855 function Is_Greater_Key_Node
857 Right : Node_Access) return Boolean is
859 return Key (Right.Element) < Left;
860 end Is_Greater_Key_Node;
862 ----------------------
863 -- Is_Less_Key_Node --
864 ----------------------
866 function Is_Less_Key_Node
868 Right : Node_Access) return Boolean is
870 return Left < Key (Right.Element);
871 end Is_Less_Key_Node;
880 Process : not null access procedure (Position : Cursor))
882 procedure Process_Node (Node : Node_Access);
883 pragma Inline (Process_Node);
885 procedure Local_Iterate is
886 new Key_Keys.Generic_Iteration (Process_Node);
892 procedure Process_Node (Node : Node_Access) is
894 Process (Cursor'(Container'Unrestricted_Access, Node));
897 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
898 Busy : With_Busy (T.TC'Unrestricted_Access);
900 -- Start of processing for Iterate
903 Local_Iterate (T, Key);
910 function Key (Position : Cursor) return Key_Type is
912 if Position.Node = null then
913 raise Constraint_Error with
914 "Position cursor equals No_Element";
917 pragma Assert (Vet (Position.Container.Tree, Position.Node),
918 "bad cursor in Key");
920 return Key (Position.Node.Element);
923 ---------------------
924 -- Reverse_Iterate --
925 ---------------------
927 procedure Reverse_Iterate
930 Process : not null access procedure (Position : Cursor))
932 procedure Process_Node (Node : Node_Access);
933 pragma Inline (Process_Node);
935 procedure Local_Reverse_Iterate is
936 new Key_Keys.Generic_Reverse_Iteration (Process_Node);
942 procedure Process_Node (Node : Node_Access) is
944 Process (Cursor'(Container'Unrestricted_Access, Node));
947 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
948 Busy : With_Busy (T.TC'Unrestricted_Access);
950 -- Start of processing for Reverse_Iterate
953 Local_Reverse_Iterate (T, Key);
960 procedure Update_Element
961 (Container : in out Set;
963 Process : not null access procedure (Element : in out Element_Type))
965 Tree : Tree_Type renames Container.Tree;
966 Node : constant Node_Access := Position.Node;
970 raise Constraint_Error with
971 "Position cursor equals No_Element";
974 if Position.Container /= Container'Unrestricted_Access then
975 raise Program_Error with
976 "Position cursor designates wrong set";
979 pragma Assert (Vet (Tree, Node),
980 "bad cursor in Update_Element");
983 E : Element_Type renames Node.Element;
984 K : constant Key_Type := Key (E);
985 Lock : With_Lock (Tree.TC'Unrestricted_Access);
989 if Equivalent_Keys (Left => K, Right => Key (E)) then
994 -- Delete_Node checks busy-bit
996 Tree_Operations.Delete_Node_Sans_Free (Tree, Node);
998 Insert_New_Item : declare
999 function New_Node return Node_Access;
1000 pragma Inline (New_Node);
1002 procedure Insert_Post is
1003 new Element_Keys.Generic_Insert_Post (New_Node);
1005 procedure Unconditional_Insert is
1006 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1012 function New_Node return Node_Access is
1014 Node.Color := Red_Black_Trees.Red;
1015 Node.Parent := null;
1022 Result : Node_Access;
1024 -- Start of processing for Insert_New_Item
1027 Unconditional_Insert
1029 Key => Node.Element,
1032 pragma Assert (Result = Node);
1033 end Insert_New_Item;
1042 function Has_Element (Position : Cursor) return Boolean is
1044 return Position /= No_Element;
1051 procedure Insert (Container : in out Set; New_Item : Element_Type) is
1053 pragma Unreferenced (Position);
1055 Insert (Container, New_Item, Position);
1059 (Container : in out Set;
1060 New_Item : Element_Type;
1061 Position : out Cursor)
1064 Insert_Sans_Hint (Container.Tree, New_Item, Position.Node);
1065 Position.Container := Container'Unrestricted_Access;
1068 ----------------------
1069 -- Insert_Sans_Hint --
1070 ----------------------
1072 procedure Insert_Sans_Hint
1073 (Tree : in out Tree_Type;
1074 New_Item : Element_Type;
1075 Node : out Node_Access)
1077 function New_Node return Node_Access;
1078 pragma Inline (New_Node);
1080 procedure Insert_Post is
1081 new Element_Keys.Generic_Insert_Post (New_Node);
1083 procedure Unconditional_Insert is
1084 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1090 function New_Node return Node_Access is
1091 Node : constant Node_Access :=
1092 new Node_Type'(Parent => null,
1095 Color => Red_Black_Trees.Red,
1096 Element => New_Item);
1101 -- Start of processing for Insert_Sans_Hint
1104 Unconditional_Insert (Tree, New_Item, Node);
1105 end Insert_Sans_Hint;
1107 ----------------------
1108 -- Insert_With_Hint --
1109 ----------------------
1111 procedure Insert_With_Hint
1112 (Dst_Tree : in out Tree_Type;
1113 Dst_Hint : Node_Access;
1114 Src_Node : Node_Access;
1115 Dst_Node : out Node_Access)
1117 function New_Node return Node_Access;
1118 pragma Inline (New_Node);
1120 procedure Insert_Post is
1121 new Element_Keys.Generic_Insert_Post (New_Node);
1123 procedure Insert_Sans_Hint is
1124 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1126 procedure Local_Insert_With_Hint is
1127 new Element_Keys.Generic_Unconditional_Insert_With_Hint
1135 function New_Node return Node_Access is
1136 Node : constant Node_Access :=
1137 new Node_Type'(Parent => null,
1141 Element => Src_Node.Element);
1146 -- Start of processing for Insert_With_Hint
1149 Local_Insert_With_Hint
1154 end Insert_With_Hint;
1160 procedure Intersection (Target : in out Set; Source : Set) is
1162 Set_Ops.Intersection (Target.Tree, Source.Tree);
1165 function Intersection (Left, Right : Set) return Set is
1166 Tree : constant Tree_Type :=
1167 Set_Ops.Intersection (Left.Tree, Right.Tree);
1169 return Set'(Controlled with Tree);
1176 function Is_Empty (Container : Set) return Boolean is
1178 return Container.Tree.Length = 0;
1181 ------------------------
1182 -- Is_Equal_Node_Node --
1183 ------------------------
1185 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
1187 return L.Element = R.Element;
1188 end Is_Equal_Node_Node;
1190 -----------------------------
1191 -- Is_Greater_Element_Node --
1192 -----------------------------
1194 function Is_Greater_Element_Node
1195 (Left : Element_Type;
1196 Right : Node_Access) return Boolean
1199 -- e > node same as node < e
1201 return Right.Element < Left;
1202 end Is_Greater_Element_Node;
1204 --------------------------
1205 -- Is_Less_Element_Node --
1206 --------------------------
1208 function Is_Less_Element_Node
1209 (Left : Element_Type;
1210 Right : Node_Access) return Boolean
1213 return Left < Right.Element;
1214 end Is_Less_Element_Node;
1216 -----------------------
1217 -- Is_Less_Node_Node --
1218 -----------------------
1220 function Is_Less_Node_Node (L, R : Node_Access) return Boolean is
1222 return L.Element < R.Element;
1223 end Is_Less_Node_Node;
1229 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1231 return Set_Ops.Is_Subset (Subset => Subset.Tree, Of_Set => Of_Set.Tree);
1240 Process : not null access procedure (Position : Cursor))
1242 procedure Process_Node (Node : Node_Access);
1243 pragma Inline (Process_Node);
1245 procedure Local_Iterate is
1246 new Tree_Operations.Generic_Iteration (Process_Node);
1252 procedure Process_Node (Node : Node_Access) is
1254 Process (Cursor'(Container'Unrestricted_Access, Node));
1257 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1258 Busy : With_Busy (T.TC'Unrestricted_Access);
1260 -- Start of processing for Iterate
1268 Item : Element_Type;
1269 Process : not null access procedure (Position : Cursor))
1271 procedure Process_Node (Node : Node_Access);
1272 pragma Inline (Process_Node);
1274 procedure Local_Iterate is
1275 new Element_Keys.Generic_Iteration (Process_Node);
1281 procedure Process_Node (Node : Node_Access) is
1283 Process (Cursor'(Container'Unrestricted_Access, Node));
1286 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1287 Busy : With_Busy (T.TC'Unrestricted_Access);
1289 -- Start of processing for Iterate
1292 Local_Iterate (T, Item);
1295 function Iterate (Container : Set)
1296 return Set_Iterator_Interfaces.Reversible_Iterator'Class
1298 S : constant Set_Access := Container'Unrestricted_Access;
1300 -- The value of the Node component influences the behavior of the First
1301 -- and Last selector functions of the iterator object. When the Node
1302 -- component is null (as is the case here), this means the iterator
1303 -- object was constructed without a start expression. This is a complete
1304 -- iterator, meaning that the iteration starts from the (logical)
1305 -- beginning of the sequence of items.
1307 -- Note: For a forward iterator, Container.First is the beginning, and
1308 -- for a reverse iterator, Container.Last is the beginning.
1310 return It : constant Iterator := (Limited_Controlled with S, null) do
1315 function Iterate (Container : Set; Start : Cursor)
1316 return Set_Iterator_Interfaces.Reversible_Iterator'Class
1318 S : constant Set_Access := Container'Unrestricted_Access;
1320 -- It was formerly the case that when Start = No_Element, the partial
1321 -- iterator was defined to behave the same as for a complete iterator,
1322 -- and iterate over the entire sequence of items. However, those
1323 -- semantics were unintuitive and arguably error-prone (it is too easy
1324 -- to accidentally create an endless loop), and so they were changed,
1325 -- per the ARG meeting in Denver on 2011/11. However, there was no
1326 -- consensus about what positive meaning this corner case should have,
1327 -- and so it was decided to simply raise an exception. This does imply,
1328 -- however, that it is not possible to use a partial iterator to specify
1329 -- an empty sequence of items.
1331 if Start = No_Element then
1332 raise Constraint_Error with
1333 "Start position for iterator equals No_Element";
1336 if Start.Container /= Container'Unrestricted_Access then
1337 raise Program_Error with
1338 "Start cursor of Iterate designates wrong set";
1341 pragma Assert (Vet (Container.Tree, Start.Node),
1342 "Start cursor of Iterate is bad");
1344 -- The value of the Node component influences the behavior of the First
1345 -- and Last selector functions of the iterator object. When the Node
1346 -- component is non-null (as is the case here), it means that this is a
1347 -- partial iteration, over a subset of the complete sequence of
1348 -- items. The iterator object was constructed with a start expression,
1349 -- indicating the position from which the iteration begins. Note that
1350 -- the start position has the same value irrespective of whether this is
1351 -- a forward or reverse iteration.
1353 return It : constant Iterator :=
1354 (Limited_Controlled with S, Start.Node)
1364 function Last (Container : Set) return Cursor is
1366 if Container.Tree.Last = null then
1370 return Cursor'(Container'Unrestricted_Access, Container.Tree.Last);
1373 function Last (Object : Iterator) return Cursor is
1375 -- The value of the iterator object's Node component influences the
1376 -- behavior of the Last (and First) selector function.
1378 -- When the Node component is null, this means the iterator object was
1379 -- constructed without a start expression, in which case the (reverse)
1380 -- iteration starts from the (logical) beginning of the entire sequence
1381 -- (corresponding to Container.Last, for a reverse iterator).
1383 -- Otherwise, this is iteration over a partial sequence of items. When
1384 -- the Node component is non-null, the iterator object was constructed
1385 -- with a start expression, that specifies the position from which the
1386 -- (reverse) partial iteration begins.
1388 if Object.Node = null then
1389 return Object.Container.Last;
1391 return Cursor'(Object.Container, Object.Node);
1399 function Last_Element (Container : Set) return Element_Type is
1401 if Container.Tree.Last = null then
1402 raise Constraint_Error with "set is empty";
1405 return Container.Tree.Last.Element;
1412 function Left (Node : Node_Access) return Node_Access is
1421 function Length (Container : Set) return Count_Type is
1423 return Container.Tree.Length;
1431 new Tree_Operations.Generic_Move (Clear);
1433 procedure Move (Target : in out Set; Source : in out Set) is
1435 Move (Target => Target.Tree, Source => Source.Tree);
1442 procedure Next (Position : in out Cursor)
1445 Position := Next (Position);
1448 function Next (Position : Cursor) return Cursor is
1450 if Position = No_Element then
1454 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1455 "bad cursor in Next");
1458 Node : constant Node_Access := Tree_Operations.Next (Position.Node);
1464 return Cursor'(Position.Container, Node);
1468 function Next (Object : Iterator; Position : Cursor) return Cursor is
1470 if Position.Container = null then
1474 if Position.Container /= Object.Container then
1475 raise Program_Error with
1476 "Position cursor of Next designates wrong set";
1479 return Next (Position);
1486 function Overlap (Left, Right : Set) return Boolean is
1488 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1495 function Parent (Node : Node_Access) return Node_Access is
1504 procedure Previous (Position : in out Cursor)
1507 Position := Previous (Position);
1510 function Previous (Position : Cursor) return Cursor is
1512 if Position = No_Element then
1516 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1517 "bad cursor in Previous");
1520 Node : constant Node_Access :=
1521 Tree_Operations.Previous (Position.Node);
1523 return (if Node = null then No_Element
1524 else Cursor'(Position.Container, Node));
1528 function Previous (Object : Iterator; Position : Cursor) return Cursor is
1530 if Position.Container = null then
1534 if Position.Container /= Object.Container then
1535 raise Program_Error with
1536 "Position cursor of Previous designates wrong set";
1539 return Previous (Position);
1546 procedure Query_Element
1548 Process : not null access procedure (Element : Element_Type))
1551 if Position.Node = null then
1552 raise Constraint_Error with "Position cursor equals No_Element";
1555 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1556 "bad cursor in Query_Element");
1559 T : Tree_Type renames Position.Container.Tree;
1560 Lock : With_Lock (T.TC'Unrestricted_Access);
1562 Process (Position.Node.Element);
1571 (Stream : not null access Root_Stream_Type'Class;
1572 Container : out Set)
1575 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1576 pragma Inline (Read_Node);
1579 new Tree_Operations.Generic_Read (Clear, Read_Node);
1586 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1588 Node : Node_Access := new Node_Type;
1590 Element_Type'Read (Stream, Node.Element);
1594 Free (Node); -- Note that Free deallocates elem too
1598 -- Start of processing for Read
1601 Read (Stream, Container.Tree);
1605 (Stream : not null access Root_Stream_Type'Class;
1609 raise Program_Error with "attempt to stream set cursor";
1613 (Stream : not null access Root_Stream_Type'Class;
1614 Item : out Constant_Reference_Type)
1617 raise Program_Error with "attempt to stream reference";
1620 ---------------------
1621 -- Replace_Element --
1622 ---------------------
1624 procedure Replace_Element
1625 (Tree : in out Tree_Type;
1627 Item : Element_Type)
1630 if Item < Node.Element
1631 or else Node.Element < Item
1637 Node.Element := Item;
1641 Tree_Operations.Delete_Node_Sans_Free (Tree, Node); -- Checks busy-bit
1643 Insert_New_Item : declare
1644 function New_Node return Node_Access;
1645 pragma Inline (New_Node);
1647 procedure Insert_Post is
1648 new Element_Keys.Generic_Insert_Post (New_Node);
1650 procedure Unconditional_Insert is
1651 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1657 function New_Node return Node_Access is
1659 Node.Element := Item;
1660 Node.Color := Red_Black_Trees.Red;
1661 Node.Parent := null;
1668 Result : Node_Access;
1670 -- Start of processing for Insert_New_Item
1673 Unconditional_Insert
1678 pragma Assert (Result = Node);
1679 end Insert_New_Item;
1680 end Replace_Element;
1682 procedure Replace_Element
1683 (Container : in out Set;
1685 New_Item : Element_Type)
1688 if Position.Node = null then
1689 raise Constraint_Error with
1690 "Position cursor equals No_Element";
1693 if Position.Container /= Container'Unrestricted_Access then
1694 raise Program_Error with
1695 "Position cursor designates wrong set";
1698 pragma Assert (Vet (Container.Tree, Position.Node),
1699 "bad cursor in Replace_Element");
1701 Replace_Element (Container.Tree, Position.Node, New_Item);
1702 end Replace_Element;
1704 ---------------------
1705 -- Reverse_Iterate --
1706 ---------------------
1708 procedure Reverse_Iterate
1710 Process : not null access procedure (Position : Cursor))
1712 procedure Process_Node (Node : Node_Access);
1713 pragma Inline (Process_Node);
1715 procedure Local_Reverse_Iterate is
1716 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1722 procedure Process_Node (Node : Node_Access) is
1724 Process (Cursor'(Container'Unrestricted_Access, Node));
1727 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1728 Busy : With_Busy (T.TC'Unrestricted_Access);
1730 -- Start of processing for Reverse_Iterate
1733 Local_Reverse_Iterate (T);
1734 end Reverse_Iterate;
1736 procedure Reverse_Iterate
1738 Item : Element_Type;
1739 Process : not null access procedure (Position : Cursor))
1741 procedure Process_Node (Node : Node_Access);
1742 pragma Inline (Process_Node);
1744 procedure Local_Reverse_Iterate is
1745 new Element_Keys.Generic_Reverse_Iteration (Process_Node);
1751 procedure Process_Node (Node : Node_Access) is
1753 Process (Cursor'(Container'Unrestricted_Access, Node));
1756 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1757 Busy : With_Busy (T.TC'Unrestricted_Access);
1759 -- Start of processing for Reverse_Iterate
1762 Local_Reverse_Iterate (T, Item);
1763 end Reverse_Iterate;
1769 function Right (Node : Node_Access) return Node_Access is
1778 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1780 Node.Color := Color;
1787 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1796 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1798 Node.Parent := Parent;
1805 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1807 Node.Right := Right;
1810 --------------------------
1811 -- Symmetric_Difference --
1812 --------------------------
1814 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1816 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
1817 end Symmetric_Difference;
1819 function Symmetric_Difference (Left, Right : Set) return Set is
1820 Tree : constant Tree_Type :=
1821 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
1823 return Set'(Controlled with Tree);
1824 end Symmetric_Difference;
1830 function To_Set (New_Item : Element_Type) return Set is
1833 pragma Unreferenced (Node);
1835 Insert_Sans_Hint (Tree, New_Item, Node);
1836 return Set'(Controlled with Tree);
1843 procedure Union (Target : in out Set; Source : Set) is
1845 Set_Ops.Union (Target.Tree, Source.Tree);
1848 function Union (Left, Right : Set) return Set is
1849 Tree : constant Tree_Type := Set_Ops.Union (Left.Tree, Right.Tree);
1851 return Set'(Controlled with Tree);
1859 (Stream : not null access Root_Stream_Type'Class;
1862 procedure Write_Node
1863 (Stream : not null access Root_Stream_Type'Class;
1864 Node : Node_Access);
1865 pragma Inline (Write_Node);
1868 new Tree_Operations.Generic_Write (Write_Node);
1874 procedure Write_Node
1875 (Stream : not null access Root_Stream_Type'Class;
1879 Element_Type'Write (Stream, Node.Element);
1882 -- Start of processing for Write
1885 Write (Stream, Container.Tree);
1889 (Stream : not null access Root_Stream_Type'Class;
1893 raise Program_Error with "attempt to stream set cursor";
1897 (Stream : not null access Root_Stream_Type'Class;
1898 Item : Constant_Reference_Type)
1901 raise Program_Error with "attempt to stream reference";
1903 end Ada.Containers.Ordered_Multisets;