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-2011, 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 type Iterator is new Limited_Controlled and
46 Set_Iterator_Interfaces.Reversible_Iterator with
48 Container : Set_Access;
52 overriding procedure Finalize (Object : in out Iterator);
54 overriding function First (Object : Iterator) return Cursor;
55 overriding function Last (Object : Iterator) return Cursor;
57 overriding function Next
59 Position : Cursor) return Cursor;
61 overriding function Previous
63 Position : Cursor) return Cursor;
65 -----------------------------
66 -- Node Access Subprograms --
67 -----------------------------
69 -- These subprograms provide a functional interface to access fields
70 -- of a node, and a procedural interface for modifying these values.
72 function Color (Node : Node_Access) return Color_Type;
73 pragma Inline (Color);
75 function Left (Node : Node_Access) return Node_Access;
78 function Parent (Node : Node_Access) return Node_Access;
79 pragma Inline (Parent);
81 function Right (Node : Node_Access) return Node_Access;
82 pragma Inline (Right);
84 procedure Set_Parent (Node : Node_Access; Parent : Node_Access);
85 pragma Inline (Set_Parent);
87 procedure Set_Left (Node : Node_Access; Left : Node_Access);
88 pragma Inline (Set_Left);
90 procedure Set_Right (Node : Node_Access; Right : Node_Access);
91 pragma Inline (Set_Right);
93 procedure Set_Color (Node : Node_Access; Color : Color_Type);
94 pragma Inline (Set_Color);
96 -----------------------
97 -- Local Subprograms --
98 -----------------------
100 function Copy_Node (Source : Node_Access) return Node_Access;
101 pragma Inline (Copy_Node);
103 procedure Free (X : in out Node_Access);
105 procedure Insert_Sans_Hint
106 (Tree : in out Tree_Type;
107 New_Item : Element_Type;
108 Node : out Node_Access);
110 procedure Insert_With_Hint
111 (Dst_Tree : in out Tree_Type;
112 Dst_Hint : Node_Access;
113 Src_Node : Node_Access;
114 Dst_Node : out Node_Access);
116 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean;
117 pragma Inline (Is_Equal_Node_Node);
119 function Is_Greater_Element_Node
120 (Left : Element_Type;
121 Right : Node_Access) return Boolean;
122 pragma Inline (Is_Greater_Element_Node);
124 function Is_Less_Element_Node
125 (Left : Element_Type;
126 Right : Node_Access) return Boolean;
127 pragma Inline (Is_Less_Element_Node);
129 function Is_Less_Node_Node (L, R : Node_Access) return Boolean;
130 pragma Inline (Is_Less_Node_Node);
132 procedure Replace_Element
133 (Tree : in out Tree_Type;
135 Item : Element_Type);
137 --------------------------
138 -- Local Instantiations --
139 --------------------------
141 package Tree_Operations is
142 new Red_Black_Trees.Generic_Operations (Tree_Types);
144 procedure Delete_Tree is
145 new Tree_Operations.Generic_Delete_Tree (Free);
147 function Copy_Tree is
148 new Tree_Operations.Generic_Copy_Tree (Copy_Node, Delete_Tree);
153 new Tree_Operations.Generic_Equal (Is_Equal_Node_Node);
155 package Element_Keys is
156 new Red_Black_Trees.Generic_Keys
157 (Tree_Operations => Tree_Operations,
158 Key_Type => Element_Type,
159 Is_Less_Key_Node => Is_Less_Element_Node,
160 Is_Greater_Key_Node => Is_Greater_Element_Node);
163 new Generic_Set_Operations
164 (Tree_Operations => Tree_Operations,
165 Insert_With_Hint => Insert_With_Hint,
166 Copy_Tree => Copy_Tree,
167 Delete_Tree => Delete_Tree,
168 Is_Less => Is_Less_Node_Node,
175 function "<" (Left, Right : Cursor) return Boolean is
177 if Left.Node = null then
178 raise Constraint_Error with "Left cursor equals No_Element";
181 if Right.Node = null then
182 raise Constraint_Error with "Right cursor equals No_Element";
185 pragma Assert (Vet (Left.Container.Tree, Left.Node),
186 "bad Left cursor in ""<""");
188 pragma Assert (Vet (Right.Container.Tree, Right.Node),
189 "bad Right cursor in ""<""");
191 return Left.Node.Element < Right.Node.Element;
194 function "<" (Left : Cursor; Right : Element_Type)
197 if Left.Node = null then
198 raise Constraint_Error with "Left cursor equals No_Element";
201 pragma Assert (Vet (Left.Container.Tree, Left.Node),
202 "bad Left cursor in ""<""");
204 return Left.Node.Element < Right;
207 function "<" (Left : Element_Type; Right : Cursor)
210 if Right.Node = null then
211 raise Constraint_Error with "Right cursor equals No_Element";
214 pragma Assert (Vet (Right.Container.Tree, Right.Node),
215 "bad Right cursor in ""<""");
217 return Left < Right.Node.Element;
224 function "=" (Left, Right : Set) return Boolean is
226 return Is_Equal (Left.Tree, Right.Tree);
233 function ">" (Left, Right : Cursor) return Boolean is
235 if Left.Node = null then
236 raise Constraint_Error with "Left cursor equals No_Element";
239 if Right.Node = null then
240 raise Constraint_Error with "Right cursor equals No_Element";
243 pragma Assert (Vet (Left.Container.Tree, Left.Node),
244 "bad Left cursor in "">""");
246 pragma Assert (Vet (Right.Container.Tree, Right.Node),
247 "bad Right cursor in "">""");
249 -- L > R same as R < L
251 return Right.Node.Element < Left.Node.Element;
254 function ">" (Left : Cursor; Right : Element_Type)
257 if Left.Node = null then
258 raise Constraint_Error with "Left cursor equals No_Element";
261 pragma Assert (Vet (Left.Container.Tree, Left.Node),
262 "bad Left cursor in "">""");
264 return Right < Left.Node.Element;
267 function ">" (Left : Element_Type; Right : Cursor)
270 if Right.Node = null then
271 raise Constraint_Error with "Right cursor equals No_Element";
274 pragma Assert (Vet (Right.Container.Tree, Right.Node),
275 "bad Right cursor in "">""");
277 return Right.Node.Element < Left;
284 procedure Adjust is new Tree_Operations.Generic_Adjust (Copy_Tree);
286 procedure Adjust (Container : in out Set) is
288 Adjust (Container.Tree);
295 procedure Assign (Target : in out Set; Source : Set) is
297 if Target'Address = Source'Address then
302 Target.Union (Source);
309 function Ceiling (Container : Set; Item : Element_Type) return Cursor is
310 Node : constant Node_Access :=
311 Element_Keys.Ceiling (Container.Tree, Item);
318 return Cursor'(Container'Unrestricted_Access, Node);
326 new Tree_Operations.Generic_Clear (Delete_Tree);
328 procedure Clear (Container : in out Set) is
330 Clear (Container.Tree);
337 function Color (Node : Node_Access) return Color_Type is
346 function Contains (Container : Set; Item : Element_Type) return Boolean is
348 return Find (Container, Item) /= No_Element;
355 function Copy (Source : Set) return Set is
357 return Target : Set do
358 Target.Assign (Source);
366 function Copy_Node (Source : Node_Access) return Node_Access is
367 Target : constant Node_Access :=
368 new Node_Type'(Parent => null,
371 Color => Source.Color,
372 Element => Source.Element);
381 procedure Delete (Container : in out Set; Item : Element_Type) is
382 Tree : Tree_Type renames Container.Tree;
383 Node : Node_Access := Element_Keys.Ceiling (Tree, Item);
384 Done : constant Node_Access := Element_Keys.Upper_Bound (Tree, Item);
389 raise Constraint_Error with
390 "attempt to delete element not in set";
395 Node := Tree_Operations.Next (Node);
396 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
399 exit when Node = Done;
403 procedure Delete (Container : in out Set; Position : in out Cursor) is
405 if Position.Node = null then
406 raise Constraint_Error with "Position cursor equals No_Element";
409 if Position.Container /= Container'Unrestricted_Access then
410 raise Program_Error with "Position cursor designates wrong set";
413 pragma Assert (Vet (Container.Tree, Position.Node),
414 "bad cursor in Delete");
416 Delete_Node_Sans_Free (Container.Tree, Position.Node);
417 Free (Position.Node);
419 Position.Container := null;
426 procedure Delete_First (Container : in out Set) is
427 Tree : Tree_Type renames Container.Tree;
428 X : Node_Access := Tree.First;
435 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
443 procedure Delete_Last (Container : in out Set) is
444 Tree : Tree_Type renames Container.Tree;
445 X : Node_Access := Tree.Last;
452 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
460 procedure Difference (Target : in out Set; Source : Set) is
462 Set_Ops.Difference (Target.Tree, Source.Tree);
465 function Difference (Left, Right : Set) return Set is
466 Tree : constant Tree_Type :=
467 Set_Ops.Difference (Left.Tree, Right.Tree);
469 return Set'(Controlled with Tree);
476 function Element (Position : Cursor) return Element_Type is
478 if Position.Node = null then
479 raise Constraint_Error with "Position cursor equals No_Element";
482 pragma Assert (Vet (Position.Container.Tree, Position.Node),
483 "bad cursor in Element");
485 return Position.Node.Element;
488 -------------------------
489 -- Equivalent_Elements --
490 -------------------------
492 function Equivalent_Elements (Left, Right : Element_Type) return Boolean is
501 end Equivalent_Elements;
503 ---------------------
504 -- Equivalent_Sets --
505 ---------------------
507 function Equivalent_Sets (Left, Right : Set) return Boolean is
509 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean;
510 pragma Inline (Is_Equivalent_Node_Node);
512 function Is_Equivalent is
513 new Tree_Operations.Generic_Equal (Is_Equivalent_Node_Node);
515 -----------------------------
516 -- Is_Equivalent_Node_Node --
517 -----------------------------
519 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean is
521 if L.Element < R.Element then
523 elsif R.Element < L.Element then
528 end Is_Equivalent_Node_Node;
530 -- Start of processing for Equivalent_Sets
533 return Is_Equivalent (Left.Tree, Right.Tree);
540 procedure Exclude (Container : in out Set; Item : Element_Type) is
541 Tree : Tree_Type renames Container.Tree;
542 Node : Node_Access := Element_Keys.Ceiling (Tree, Item);
543 Done : constant Node_Access := Element_Keys.Upper_Bound (Tree, Item);
546 while Node /= Done loop
548 Node := Tree_Operations.Next (Node);
549 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
558 procedure Finalize (Object : in out Iterator) is
559 B : Natural renames Object.Container.Tree.Busy;
560 pragma Assert (B > 0);
569 function Find (Container : Set; Item : Element_Type) return Cursor is
570 Node : constant Node_Access :=
571 Element_Keys.Find (Container.Tree, Item);
578 return Cursor'(Container'Unrestricted_Access, Node);
585 function First (Container : Set) return Cursor is
587 if Container.Tree.First = null then
591 return Cursor'(Container'Unrestricted_Access, Container.Tree.First);
594 function First (Object : Iterator) return Cursor is
596 -- The value of the iterator object's Node component influences the
597 -- behavior of the First (and Last) selector function.
599 -- When the Node component is null, this means the iterator object was
600 -- constructed without a start expression, in which case the (forward)
601 -- iteration starts from the (logical) beginning of the entire sequence
602 -- of items (corresponding to Container.First, for a forward iterator).
604 -- Otherwise, this is iteration over a partial sequence of items. When
605 -- the Node component is non-null, the iterator object was constructed
606 -- with a start expression, that specifies the position from which the
607 -- (forward) partial iteration begins.
609 if Object.Node = null then
610 return Object.Container.First;
612 return Cursor'(Object.Container, Object.Node);
620 function First_Element (Container : Set) return Element_Type is
622 if Container.Tree.First = null then
623 raise Constraint_Error with "set is empty";
626 return Container.Tree.First.Element;
633 function Floor (Container : Set; Item : Element_Type) return Cursor is
634 Node : constant Node_Access :=
635 Element_Keys.Floor (Container.Tree, Item);
642 return Cursor'(Container'Unrestricted_Access, Node);
649 procedure Free (X : in out Node_Access) is
650 procedure Deallocate is
651 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
667 package body Generic_Keys is
669 -----------------------
670 -- Local Subprograms --
671 -----------------------
673 function Is_Greater_Key_Node
675 Right : Node_Access) return Boolean;
676 pragma Inline (Is_Greater_Key_Node);
678 function Is_Less_Key_Node
680 Right : Node_Access) return Boolean;
681 pragma Inline (Is_Less_Key_Node);
683 --------------------------
684 -- Local_Instantiations --
685 --------------------------
688 new Red_Black_Trees.Generic_Keys
689 (Tree_Operations => Tree_Operations,
690 Key_Type => Key_Type,
691 Is_Less_Key_Node => Is_Less_Key_Node,
692 Is_Greater_Key_Node => Is_Greater_Key_Node);
698 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
699 Node : constant Node_Access :=
700 Key_Keys.Ceiling (Container.Tree, Key);
707 return Cursor'(Container'Unrestricted_Access, Node);
714 function Contains (Container : Set; Key : Key_Type) return Boolean is
716 return Find (Container, Key) /= No_Element;
723 procedure Delete (Container : in out Set; Key : Key_Type) is
724 Tree : Tree_Type renames Container.Tree;
725 Node : Node_Access := Key_Keys.Ceiling (Tree, Key);
726 Done : constant Node_Access := Key_Keys.Upper_Bound (Tree, Key);
731 raise Constraint_Error with "attempt to delete key not in set";
736 Node := Tree_Operations.Next (Node);
737 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
740 exit when Node = Done;
748 function Element (Container : Set; Key : Key_Type) return Element_Type is
749 Node : constant Node_Access :=
750 Key_Keys.Find (Container.Tree, Key);
753 raise Constraint_Error with "key not in set";
759 ---------------------
760 -- Equivalent_Keys --
761 ---------------------
763 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
778 procedure Exclude (Container : in out Set; Key : Key_Type) is
779 Tree : Tree_Type renames Container.Tree;
780 Node : Node_Access := Key_Keys.Ceiling (Tree, Key);
781 Done : constant Node_Access := Key_Keys.Upper_Bound (Tree, Key);
785 while Node /= Done loop
787 Node := Tree_Operations.Next (Node);
788 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
797 function Find (Container : Set; Key : Key_Type) return Cursor is
798 Node : constant Node_Access :=
799 Key_Keys.Find (Container.Tree, Key);
806 return Cursor'(Container'Unrestricted_Access, Node);
813 function Floor (Container : Set; Key : Key_Type) return Cursor is
814 Node : constant Node_Access :=
815 Key_Keys.Floor (Container.Tree, Key);
822 return Cursor'(Container'Unrestricted_Access, Node);
825 -------------------------
826 -- Is_Greater_Key_Node --
827 -------------------------
829 function Is_Greater_Key_Node
831 Right : Node_Access) return Boolean is
833 return Key (Right.Element) < Left;
834 end Is_Greater_Key_Node;
836 ----------------------
837 -- Is_Less_Key_Node --
838 ----------------------
840 function Is_Less_Key_Node
842 Right : Node_Access) return Boolean is
844 return Left < Key (Right.Element);
845 end Is_Less_Key_Node;
854 Process : not null access procedure (Position : Cursor))
856 procedure Process_Node (Node : Node_Access);
857 pragma Inline (Process_Node);
859 procedure Local_Iterate is
860 new Key_Keys.Generic_Iteration (Process_Node);
866 procedure Process_Node (Node : Node_Access) is
868 Process (Cursor'(Container'Unrestricted_Access, Node));
871 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
872 B : Natural renames T.Busy;
874 -- Start of processing for Iterate
880 Local_Iterate (T, Key);
894 function Key (Position : Cursor) return Key_Type is
896 if Position.Node = null then
897 raise Constraint_Error with
898 "Position cursor equals No_Element";
901 pragma Assert (Vet (Position.Container.Tree, Position.Node),
902 "bad cursor in Key");
904 return Key (Position.Node.Element);
907 ---------------------
908 -- Reverse_Iterate --
909 ---------------------
911 procedure Reverse_Iterate
914 Process : not null access procedure (Position : Cursor))
916 procedure Process_Node (Node : Node_Access);
917 pragma Inline (Process_Node);
919 procedure Local_Reverse_Iterate is
920 new Key_Keys.Generic_Reverse_Iteration (Process_Node);
926 procedure Process_Node (Node : Node_Access) is
928 Process (Cursor'(Container'Unrestricted_Access, Node));
931 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
932 B : Natural renames T.Busy;
934 -- Start of processing for Reverse_Iterate
940 Local_Reverse_Iterate (T, Key);
954 procedure Update_Element
955 (Container : in out Set;
957 Process : not null access procedure (Element : in out Element_Type))
959 Tree : Tree_Type renames Container.Tree;
960 Node : constant Node_Access := Position.Node;
964 raise Constraint_Error with
965 "Position cursor equals No_Element";
968 if Position.Container /= Container'Unrestricted_Access then
969 raise Program_Error with
970 "Position cursor designates wrong set";
973 pragma Assert (Vet (Tree, Node),
974 "bad cursor in Update_Element");
977 E : Element_Type renames Node.Element;
978 K : constant Key_Type := Key (E);
980 B : Natural renames Tree.Busy;
981 L : Natural renames Tree.Lock;
999 if Equivalent_Keys (Left => K, Right => Key (E)) then
1004 -- Delete_Node checks busy-bit
1006 Tree_Operations.Delete_Node_Sans_Free (Tree, Node);
1008 Insert_New_Item : declare
1009 function New_Node return Node_Access;
1010 pragma Inline (New_Node);
1012 procedure Insert_Post is
1013 new Element_Keys.Generic_Insert_Post (New_Node);
1015 procedure Unconditional_Insert is
1016 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1022 function New_Node return Node_Access is
1024 Node.Color := Red_Black_Trees.Red;
1025 Node.Parent := null;
1032 Result : Node_Access;
1034 -- Start of processing for Insert_New_Item
1037 Unconditional_Insert
1039 Key => Node.Element,
1042 pragma Assert (Result = Node);
1043 end Insert_New_Item;
1052 function Has_Element (Position : Cursor) return Boolean is
1054 return Position /= No_Element;
1061 procedure Insert (Container : in out Set; New_Item : Element_Type) is
1063 pragma Unreferenced (Position);
1065 Insert (Container, New_Item, Position);
1069 (Container : in out Set;
1070 New_Item : Element_Type;
1071 Position : out Cursor)
1074 Insert_Sans_Hint (Container.Tree, New_Item, Position.Node);
1075 Position.Container := Container'Unrestricted_Access;
1078 ----------------------
1079 -- Insert_Sans_Hint --
1080 ----------------------
1082 procedure Insert_Sans_Hint
1083 (Tree : in out Tree_Type;
1084 New_Item : Element_Type;
1085 Node : out Node_Access)
1087 function New_Node return Node_Access;
1088 pragma Inline (New_Node);
1090 procedure Insert_Post is
1091 new Element_Keys.Generic_Insert_Post (New_Node);
1093 procedure Unconditional_Insert is
1094 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1100 function New_Node return Node_Access is
1101 Node : constant Node_Access :=
1102 new Node_Type'(Parent => null,
1105 Color => Red_Black_Trees.Red,
1106 Element => New_Item);
1111 -- Start of processing for Insert_Sans_Hint
1114 Unconditional_Insert (Tree, New_Item, Node);
1115 end Insert_Sans_Hint;
1117 ----------------------
1118 -- Insert_With_Hint --
1119 ----------------------
1121 procedure Insert_With_Hint
1122 (Dst_Tree : in out Tree_Type;
1123 Dst_Hint : Node_Access;
1124 Src_Node : Node_Access;
1125 Dst_Node : out Node_Access)
1127 function New_Node return Node_Access;
1128 pragma Inline (New_Node);
1130 procedure Insert_Post is
1131 new Element_Keys.Generic_Insert_Post (New_Node);
1133 procedure Insert_Sans_Hint is
1134 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1136 procedure Local_Insert_With_Hint is
1137 new Element_Keys.Generic_Unconditional_Insert_With_Hint
1145 function New_Node return Node_Access is
1146 Node : constant Node_Access :=
1147 new Node_Type'(Parent => null,
1151 Element => Src_Node.Element);
1156 -- Start of processing for Insert_With_Hint
1159 Local_Insert_With_Hint
1164 end Insert_With_Hint;
1170 procedure Intersection (Target : in out Set; Source : Set) is
1172 Set_Ops.Intersection (Target.Tree, Source.Tree);
1175 function Intersection (Left, Right : Set) return Set is
1176 Tree : constant Tree_Type :=
1177 Set_Ops.Intersection (Left.Tree, Right.Tree);
1179 return Set'(Controlled with Tree);
1186 function Is_Empty (Container : Set) return Boolean is
1188 return Container.Tree.Length = 0;
1191 ------------------------
1192 -- Is_Equal_Node_Node --
1193 ------------------------
1195 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
1197 return L.Element = R.Element;
1198 end Is_Equal_Node_Node;
1200 -----------------------------
1201 -- Is_Greater_Element_Node --
1202 -----------------------------
1204 function Is_Greater_Element_Node
1205 (Left : Element_Type;
1206 Right : Node_Access) return Boolean
1209 -- e > node same as node < e
1211 return Right.Element < Left;
1212 end Is_Greater_Element_Node;
1214 --------------------------
1215 -- Is_Less_Element_Node --
1216 --------------------------
1218 function Is_Less_Element_Node
1219 (Left : Element_Type;
1220 Right : Node_Access) return Boolean
1223 return Left < Right.Element;
1224 end Is_Less_Element_Node;
1226 -----------------------
1227 -- Is_Less_Node_Node --
1228 -----------------------
1230 function Is_Less_Node_Node (L, R : Node_Access) return Boolean is
1232 return L.Element < R.Element;
1233 end Is_Less_Node_Node;
1239 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1241 return Set_Ops.Is_Subset (Subset => Subset.Tree, Of_Set => Of_Set.Tree);
1250 Process : not null access procedure (Position : Cursor))
1252 procedure Process_Node (Node : Node_Access);
1253 pragma Inline (Process_Node);
1255 procedure Local_Iterate is
1256 new Tree_Operations.Generic_Iteration (Process_Node);
1262 procedure Process_Node (Node : Node_Access) is
1264 Process (Cursor'(Container'Unrestricted_Access, Node));
1267 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1268 B : Natural renames T.Busy;
1270 -- Start of processing for Iterate
1288 Item : Element_Type;
1289 Process : not null access procedure (Position : Cursor))
1291 procedure Process_Node (Node : Node_Access);
1292 pragma Inline (Process_Node);
1294 procedure Local_Iterate is
1295 new Element_Keys.Generic_Iteration (Process_Node);
1301 procedure Process_Node (Node : Node_Access) is
1303 Process (Cursor'(Container'Unrestricted_Access, Node));
1306 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1307 B : Natural renames T.Busy;
1309 -- Start of processing for Iterate
1315 Local_Iterate (T, Item);
1325 function Iterate (Container : Set)
1326 return Set_Iterator_Interfaces.Reversible_Iterator'Class
1328 S : constant Set_Access := Container'Unrestricted_Access;
1329 B : Natural renames S.Tree.Busy;
1332 -- The value of the Node component influences the behavior of the First
1333 -- and Last selector functions of the iterator object. When the Node
1334 -- component is null (as is the case here), this means the iterator
1335 -- object was constructed without a start expression. This is a complete
1336 -- iterator, meaning that the iteration starts from the (logical)
1337 -- beginning of the sequence of items.
1339 -- Note: For a forward iterator, Container.First is the beginning, and
1340 -- for a reverse iterator, Container.Last is the beginning.
1342 return It : constant Iterator := (Limited_Controlled with S, null) do
1347 function Iterate (Container : Set; Start : Cursor)
1348 return Set_Iterator_Interfaces.Reversible_Iterator'Class
1350 S : constant Set_Access := Container'Unrestricted_Access;
1351 B : Natural renames S.Tree.Busy;
1354 -- It was formerly the case that when Start = No_Element, the partial
1355 -- iterator was defined to behave the same as for a complete iterator,
1356 -- and iterate over the entire sequence of items. However, those
1357 -- semantics were unintuitive and arguably error-prone (it is too easy
1358 -- to accidentally create an endless loop), and so they were changed,
1359 -- per the ARG meeting in Denver on 2011/11. However, there was no
1360 -- consensus about what positive meaning this corner case should have,
1361 -- and so it was decided to simply raise an exception. This does imply,
1362 -- however, that it is not possible to use a partial iterator to specify
1363 -- an empty sequence of items.
1365 if Start = No_Element then
1366 raise Constraint_Error with
1367 "Start position for iterator equals No_Element";
1370 if Start.Container /= Container'Unrestricted_Access then
1371 raise Program_Error with
1372 "Start cursor of Iterate designates wrong set";
1375 pragma Assert (Vet (Container.Tree, Start.Node),
1376 "Start cursor of Iterate is bad");
1378 -- The value of the Node component influences the behavior of the First
1379 -- and Last selector functions of the iterator object. When the Node
1380 -- component is non-null (as is the case here), it means that this is a
1381 -- partial iteration, over a subset of the complete sequence of
1382 -- items. The iterator object was constructed with a start expression,
1383 -- indicating the position from which the iteration begins. Note that
1384 -- the start position has the same value irrespective of whether this is
1385 -- a forward or reverse iteration.
1387 return It : constant Iterator :=
1388 (Limited_Controlled with S, Start.Node)
1398 function Last (Container : Set) return Cursor is
1400 if Container.Tree.Last = null then
1404 return Cursor'(Container'Unrestricted_Access, Container.Tree.Last);
1407 function Last (Object : Iterator) return Cursor is
1409 -- The value of the iterator object's Node component influences the
1410 -- behavior of the Last (and First) selector function.
1412 -- When the Node component is null, this means the iterator object was
1413 -- constructed without a start expression, in which case the (reverse)
1414 -- iteration starts from the (logical) beginning of the entire sequence
1415 -- (corresponding to Container.Last, for a reverse iterator).
1417 -- Otherwise, this is iteration over a partial sequence of items. When
1418 -- the Node component is non-null, the iterator object was constructed
1419 -- with a start expression, that specifies the position from which the
1420 -- (reverse) partial iteration begins.
1422 if Object.Node = null then
1423 return Object.Container.Last;
1425 return Cursor'(Object.Container, Object.Node);
1433 function Last_Element (Container : Set) return Element_Type is
1435 if Container.Tree.Last = null then
1436 raise Constraint_Error with "set is empty";
1439 return Container.Tree.Last.Element;
1446 function Left (Node : Node_Access) return Node_Access is
1455 function Length (Container : Set) return Count_Type is
1457 return Container.Tree.Length;
1465 new Tree_Operations.Generic_Move (Clear);
1467 procedure Move (Target : in out Set; Source : in out Set) is
1469 Move (Target => Target.Tree, Source => Source.Tree);
1476 procedure Next (Position : in out Cursor)
1479 Position := Next (Position);
1482 function Next (Position : Cursor) return Cursor is
1484 if Position = No_Element then
1488 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1489 "bad cursor in Next");
1492 Node : constant Node_Access :=
1493 Tree_Operations.Next (Position.Node);
1499 return Cursor'(Position.Container, Node);
1503 function Next (Object : Iterator; Position : Cursor) return Cursor is
1505 if Position.Container = null then
1509 if Position.Container /= Object.Container then
1510 raise Program_Error with
1511 "Position cursor of Next designates wrong set";
1514 return Next (Position);
1521 function Overlap (Left, Right : Set) return Boolean is
1523 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1530 function Parent (Node : Node_Access) return Node_Access is
1539 procedure Previous (Position : in out Cursor)
1542 Position := Previous (Position);
1545 function Previous (Position : Cursor) return Cursor is
1547 if Position = No_Element then
1551 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1552 "bad cursor in Previous");
1555 Node : constant Node_Access :=
1556 Tree_Operations.Previous (Position.Node);
1558 return (if Node = null then No_Element
1559 else Cursor'(Position.Container, Node));
1563 function Previous (Object : Iterator; Position : Cursor) return Cursor is
1565 if Position.Container = null then
1569 if Position.Container /= Object.Container then
1570 raise Program_Error with
1571 "Position cursor of Previous designates wrong set";
1574 return Previous (Position);
1581 procedure Query_Element
1583 Process : not null access procedure (Element : Element_Type))
1586 if Position.Node = null then
1587 raise Constraint_Error with "Position cursor equals No_Element";
1590 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1591 "bad cursor in Query_Element");
1594 T : Tree_Type renames Position.Container.Tree;
1596 B : Natural renames T.Busy;
1597 L : Natural renames T.Lock;
1604 Process (Position.Node.Element);
1622 (Stream : not null access Root_Stream_Type'Class;
1623 Container : out Set)
1626 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1627 pragma Inline (Read_Node);
1630 new Tree_Operations.Generic_Read (Clear, Read_Node);
1637 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1639 Node : Node_Access := new Node_Type;
1641 Element_Type'Read (Stream, Node.Element);
1645 Free (Node); -- Note that Free deallocates elem too
1649 -- Start of processing for Read
1652 Read (Stream, Container.Tree);
1656 (Stream : not null access Root_Stream_Type'Class;
1660 raise Program_Error with "attempt to stream set cursor";
1663 ---------------------
1664 -- Replace_Element --
1665 ---------------------
1667 procedure Replace_Element
1668 (Tree : in out Tree_Type;
1670 Item : Element_Type)
1673 if Item < Node.Element
1674 or else Node.Element < Item
1678 if Tree.Lock > 0 then
1679 raise Program_Error with
1680 "attempt to tamper with elements (set is locked)";
1683 Node.Element := Item;
1687 Tree_Operations.Delete_Node_Sans_Free (Tree, Node); -- Checks busy-bit
1689 Insert_New_Item : declare
1690 function New_Node return Node_Access;
1691 pragma Inline (New_Node);
1693 procedure Insert_Post is
1694 new Element_Keys.Generic_Insert_Post (New_Node);
1696 procedure Unconditional_Insert is
1697 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1703 function New_Node return Node_Access is
1705 Node.Element := Item;
1706 Node.Color := Red_Black_Trees.Red;
1707 Node.Parent := null;
1714 Result : Node_Access;
1716 -- Start of processing for Insert_New_Item
1719 Unconditional_Insert
1724 pragma Assert (Result = Node);
1725 end Insert_New_Item;
1726 end Replace_Element;
1728 procedure Replace_Element
1729 (Container : in out Set;
1731 New_Item : Element_Type)
1734 if Position.Node = null then
1735 raise Constraint_Error with
1736 "Position cursor equals No_Element";
1739 if Position.Container /= Container'Unrestricted_Access then
1740 raise Program_Error with
1741 "Position cursor designates wrong set";
1744 pragma Assert (Vet (Container.Tree, Position.Node),
1745 "bad cursor in Replace_Element");
1747 Replace_Element (Container.Tree, Position.Node, New_Item);
1748 end Replace_Element;
1750 ---------------------
1751 -- Reverse_Iterate --
1752 ---------------------
1754 procedure Reverse_Iterate
1756 Process : not null access procedure (Position : Cursor))
1758 procedure Process_Node (Node : Node_Access);
1759 pragma Inline (Process_Node);
1761 procedure Local_Reverse_Iterate is
1762 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1768 procedure Process_Node (Node : Node_Access) is
1770 Process (Cursor'(Container'Unrestricted_Access, Node));
1773 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1774 B : Natural renames T.Busy;
1776 -- Start of processing for Reverse_Iterate
1782 Local_Reverse_Iterate (T);
1790 end Reverse_Iterate;
1792 procedure Reverse_Iterate
1794 Item : Element_Type;
1795 Process : not null access procedure (Position : Cursor))
1797 procedure Process_Node (Node : Node_Access);
1798 pragma Inline (Process_Node);
1800 procedure Local_Reverse_Iterate is
1801 new Element_Keys.Generic_Reverse_Iteration (Process_Node);
1807 procedure Process_Node (Node : Node_Access) is
1809 Process (Cursor'(Container'Unrestricted_Access, Node));
1812 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1813 B : Natural renames T.Busy;
1815 -- Start of processing for Reverse_Iterate
1821 Local_Reverse_Iterate (T, Item);
1829 end Reverse_Iterate;
1835 function Right (Node : Node_Access) return Node_Access is
1844 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1846 Node.Color := Color;
1853 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1862 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1864 Node.Parent := Parent;
1871 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1873 Node.Right := Right;
1876 --------------------------
1877 -- Symmetric_Difference --
1878 --------------------------
1880 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1882 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
1883 end Symmetric_Difference;
1885 function Symmetric_Difference (Left, Right : Set) return Set is
1886 Tree : constant Tree_Type :=
1887 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
1889 return Set'(Controlled with Tree);
1890 end Symmetric_Difference;
1896 function To_Set (New_Item : Element_Type) return Set is
1899 pragma Unreferenced (Node);
1901 Insert_Sans_Hint (Tree, New_Item, Node);
1902 return Set'(Controlled with Tree);
1909 procedure Union (Target : in out Set; Source : Set) is
1911 Set_Ops.Union (Target.Tree, Source.Tree);
1914 function Union (Left, Right : Set) return Set is
1915 Tree : constant Tree_Type :=
1916 Set_Ops.Union (Left.Tree, Right.Tree);
1918 return Set'(Controlled with Tree);
1926 (Stream : not null access Root_Stream_Type'Class;
1929 procedure Write_Node
1930 (Stream : not null access Root_Stream_Type'Class;
1931 Node : Node_Access);
1932 pragma Inline (Write_Node);
1935 new Tree_Operations.Generic_Write (Write_Node);
1941 procedure Write_Node
1942 (Stream : not null access Root_Stream_Type'Class;
1946 Element_Type'Write (Stream, Node.Element);
1949 -- Start of processing for Write
1952 Write (Stream, Container.Tree);
1956 (Stream : not null access Root_Stream_Type'Class;
1960 raise Program_Error with "attempt to stream set cursor";
1963 end Ada.Containers.Ordered_Multisets;