1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . --
6 -- I N D E F I N I T E _ O R D E R E D _ M U L T I S E T S --
10 -- Copyright (C) 2004-2006, Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
21 -- Boston, MA 02110-1301, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- This unit was originally developed by Matthew J Heaney. --
31 ------------------------------------------------------------------------------
33 with Ada.Unchecked_Deallocation;
35 with Ada.Containers.Red_Black_Trees.Generic_Operations;
36 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Operations);
38 with Ada.Containers.Red_Black_Trees.Generic_Keys;
39 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Keys);
41 with Ada.Containers.Red_Black_Trees.Generic_Set_Operations;
42 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Set_Operations);
44 package body Ada.Containers.Indefinite_Ordered_Multisets is
46 -----------------------------
47 -- Node Access Subprograms --
48 -----------------------------
50 -- These subprograms provide a functional interface to access fields
51 -- of a node, and a procedural interface for modifying these values.
53 function Color (Node : Node_Access) return Color_Type;
54 pragma Inline (Color);
56 function Left (Node : Node_Access) return Node_Access;
59 function Parent (Node : Node_Access) return Node_Access;
60 pragma Inline (Parent);
62 function Right (Node : Node_Access) return Node_Access;
63 pragma Inline (Right);
65 procedure Set_Parent (Node : Node_Access; Parent : Node_Access);
66 pragma Inline (Set_Parent);
68 procedure Set_Left (Node : Node_Access; Left : Node_Access);
69 pragma Inline (Set_Left);
71 procedure Set_Right (Node : Node_Access; Right : Node_Access);
72 pragma Inline (Set_Right);
74 procedure Set_Color (Node : Node_Access; Color : Color_Type);
75 pragma Inline (Set_Color);
77 -----------------------
78 -- Local Subprograms --
79 -----------------------
81 function Copy_Node (Source : Node_Access) return Node_Access;
82 pragma Inline (Copy_Node);
84 procedure Free (X : in out Node_Access);
86 procedure Insert_Sans_Hint
87 (Tree : in out Tree_Type;
88 New_Item : Element_Type;
89 Node : out Node_Access);
91 procedure Insert_With_Hint
92 (Dst_Tree : in out Tree_Type;
93 Dst_Hint : Node_Access;
94 Src_Node : Node_Access;
95 Dst_Node : out Node_Access);
97 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean;
98 pragma Inline (Is_Equal_Node_Node);
100 function Is_Greater_Element_Node
101 (Left : Element_Type;
102 Right : Node_Access) return Boolean;
103 pragma Inline (Is_Greater_Element_Node);
105 function Is_Less_Element_Node
106 (Left : Element_Type;
107 Right : Node_Access) return Boolean;
108 pragma Inline (Is_Less_Element_Node);
110 function Is_Less_Node_Node (L, R : Node_Access) return Boolean;
111 pragma Inline (Is_Less_Node_Node);
113 procedure Replace_Element
114 (Tree : in out Tree_Type;
116 Item : Element_Type);
118 --------------------------
119 -- Local Instantiations --
120 --------------------------
122 package Tree_Operations is
123 new Red_Black_Trees.Generic_Operations (Tree_Types);
125 procedure Delete_Tree is
126 new Tree_Operations.Generic_Delete_Tree (Free);
128 function Copy_Tree is
129 new Tree_Operations.Generic_Copy_Tree (Copy_Node, Delete_Tree);
133 procedure Free_Element is
134 new Ada.Unchecked_Deallocation (Element_Type, Element_Access);
137 new Tree_Operations.Generic_Equal (Is_Equal_Node_Node);
140 new Generic_Set_Operations
141 (Tree_Operations => Tree_Operations,
142 Insert_With_Hint => Insert_With_Hint,
143 Copy_Tree => Copy_Tree,
144 Delete_Tree => Delete_Tree,
145 Is_Less => Is_Less_Node_Node,
148 package Element_Keys is
149 new Red_Black_Trees.Generic_Keys
150 (Tree_Operations => Tree_Operations,
151 Key_Type => Element_Type,
152 Is_Less_Key_Node => Is_Less_Element_Node,
153 Is_Greater_Key_Node => Is_Greater_Element_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 if Left.Node.Element = null then
170 raise Program_Error with "Left cursor is bad";
173 if Right.Node.Element = null then
174 raise Program_Error with "Right cursor is bad";
177 pragma Assert (Vet (Left.Container.Tree, Left.Node),
178 "bad Left cursor in ""<""");
180 pragma Assert (Vet (Right.Container.Tree, Right.Node),
181 "bad Right cursor in ""<""");
183 return Left.Node.Element.all < Right.Node.Element.all;
186 function "<" (Left : Cursor; Right : Element_Type) return Boolean is
188 if Left.Node = null then
189 raise Constraint_Error with "Left cursor equals No_Element";
192 if Left.Node.Element = null then
193 raise Program_Error with "Left cursor is bad";
196 pragma Assert (Vet (Left.Container.Tree, Left.Node),
197 "bad Left cursor in ""<""");
199 return Left.Node.Element.all < Right;
202 function "<" (Left : Element_Type; Right : Cursor) return Boolean is
204 if Right.Node = null then
205 raise Constraint_Error with "Right cursor equals No_Element";
208 if Right.Node.Element = null then
209 raise Program_Error with "Right cursor is bad";
212 pragma Assert (Vet (Right.Container.Tree, Right.Node),
213 "bad Right cursor in ""<""");
215 return Left < Right.Node.Element.all;
222 function "=" (Left, Right : Set) return Boolean is
224 return Is_Equal (Left.Tree, Right.Tree);
231 function ">" (Left, Right : Cursor) return Boolean is
233 if Left.Node = null then
234 raise Constraint_Error with "Left cursor equals No_Element";
237 if Right.Node = null then
238 raise Constraint_Error with "Right cursor equals No_Element";
241 if Left.Node.Element = null then
242 raise Program_Error with "Left cursor is bad";
245 if Right.Node.Element = null then
246 raise Program_Error with "Right cursor is bad";
249 pragma Assert (Vet (Left.Container.Tree, Left.Node),
250 "bad Left cursor in "">""");
252 pragma Assert (Vet (Right.Container.Tree, Right.Node),
253 "bad Right cursor in "">""");
255 -- L > R same as R < L
257 return Right.Node.Element.all < Left.Node.Element.all;
260 function ">" (Left : Cursor; Right : Element_Type) return Boolean is
262 if Left.Node = null then
263 raise Constraint_Error with "Left cursor equals No_Element";
266 if Left.Node.Element = null then
267 raise Program_Error with "Left cursor is bad";
270 pragma Assert (Vet (Left.Container.Tree, Left.Node),
271 "bad Left cursor in "">""");
273 return Right < Left.Node.Element.all;
276 function ">" (Left : Element_Type; Right : Cursor) return Boolean is
278 if Right.Node = null then
279 raise Constraint_Error with "Right cursor equals No_Element";
282 if Right.Node.Element = null then
283 raise Program_Error with "Right cursor is bad";
286 pragma Assert (Vet (Right.Container.Tree, Right.Node),
287 "bad Right cursor in "">""");
289 return Right.Node.Element.all < Left;
297 new Tree_Operations.Generic_Adjust (Copy_Tree);
299 procedure Adjust (Container : in out Set) is
301 Adjust (Container.Tree);
308 function Ceiling (Container : Set; Item : Element_Type) return Cursor is
309 Node : constant Node_Access :=
310 Element_Keys.Ceiling (Container.Tree, Item);
317 return Cursor'(Container'Unrestricted_Access, Node);
325 new Tree_Operations.Generic_Clear (Delete_Tree);
327 procedure Clear (Container : in out Set) is
329 Clear (Container.Tree);
336 function Color (Node : Node_Access) return Color_Type is
345 function Contains (Container : Set; Item : Element_Type) return Boolean is
347 return Find (Container, Item) /= No_Element;
354 function Copy_Node (Source : Node_Access) return Node_Access is
355 X : Element_Access := new Element_Type'(Source.Element.all);
358 return new Node_Type'(Parent => null,
361 Color => Source.Color,
374 procedure Delete (Container : in out Set; Item : Element_Type) is
375 Tree : Tree_Type renames Container.Tree;
376 Node : Node_Access := Element_Keys.Ceiling (Tree, Item);
377 Done : constant Node_Access := Element_Keys.Upper_Bound (Tree, Item);
382 raise Constraint_Error with "attempt to delete element not in set";
387 Node := Tree_Operations.Next (Node);
388 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
391 exit when Node = Done;
395 procedure Delete (Container : in out Set; Position : in out Cursor) is
397 if Position.Node = null then
398 raise Constraint_Error with "Position cursor equals No_Element";
401 if Position.Node.Element = null then
402 raise Program_Error with "Position cursor is bad";
405 if Position.Container /= Container'Unrestricted_Access then
406 raise Program_Error with "Position cursor designates wrong set";
409 pragma Assert (Vet (Container.Tree, Position.Node),
410 "bad cursor in Delete");
412 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
413 Free (Position.Node);
415 Position.Container := null;
422 procedure Delete_First (Container : in out Set) is
423 Tree : Tree_Type renames Container.Tree;
424 X : Node_Access := Tree.First;
431 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
439 procedure Delete_Last (Container : in out Set) is
440 Tree : Tree_Type renames Container.Tree;
441 X : Node_Access := Tree.Last;
448 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
456 procedure Difference (Target : in out Set; Source : Set) is
458 Set_Ops.Difference (Target.Tree, Source.Tree);
461 function Difference (Left, Right : Set) return Set is
462 Tree : constant Tree_Type :=
463 Set_Ops.Difference (Left.Tree, Right.Tree);
465 return Set'(Controlled with Tree);
472 function Element (Position : Cursor) return Element_Type is
474 if Position.Node = null then
475 raise Constraint_Error with "Position cursor equals No_Element";
478 if Position.Node.Element = null then
479 raise Program_Error with "Position cursor is bad";
482 pragma Assert (Vet (Position.Container.Tree, Position.Node),
483 "bad cursor in Element");
485 return Position.Node.Element.all;
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.all < R.Element.all then
523 elsif R.Element.all < L.Element.all 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);
547 while Node /= Done loop
549 Node := Tree_Operations.Next (Node);
550 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
559 function Find (Container : Set; Item : Element_Type) return Cursor is
560 Node : constant Node_Access :=
561 Element_Keys.Find (Container.Tree, Item);
568 return Cursor'(Container'Unrestricted_Access, Node);
575 function First (Container : Set) return Cursor is
577 if Container.Tree.First = null then
581 return Cursor'(Container'Unrestricted_Access, Container.Tree.First);
588 function First_Element (Container : Set) return Element_Type is
590 if Container.Tree.First = null then
591 raise Constraint_Error with "set is empty";
594 pragma Assert (Container.Tree.First.Element /= null);
595 return Container.Tree.First.Element.all;
602 function Floor (Container : Set; Item : Element_Type) return Cursor is
603 Node : constant Node_Access :=
604 Element_Keys.Floor (Container.Tree, Item);
611 return Cursor'(Container'Unrestricted_Access, Node);
618 procedure Free (X : in out Node_Access) is
619 procedure Deallocate is
620 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
632 Free_Element (X.Element);
647 package body Generic_Keys is
649 -----------------------
650 -- Local Subprograms --
651 -----------------------
653 function Is_Less_Key_Node
655 Right : Node_Access) return Boolean;
656 pragma Inline (Is_Less_Key_Node);
658 function Is_Greater_Key_Node
660 Right : Node_Access) return Boolean;
661 pragma Inline (Is_Greater_Key_Node);
663 --------------------------
664 -- Local Instantiations --
665 --------------------------
668 new Red_Black_Trees.Generic_Keys
669 (Tree_Operations => Tree_Operations,
670 Key_Type => Key_Type,
671 Is_Less_Key_Node => Is_Less_Key_Node,
672 Is_Greater_Key_Node => Is_Greater_Key_Node);
678 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
679 Node : constant Node_Access :=
680 Key_Keys.Ceiling (Container.Tree, Key);
687 return Cursor'(Container'Unrestricted_Access, Node);
694 function Contains (Container : Set; Key : Key_Type) return Boolean is
696 return Find (Container, Key) /= No_Element;
703 procedure Delete (Container : in out Set; Key : Key_Type) is
704 Tree : Tree_Type renames Container.Tree;
705 Node : Node_Access := Key_Keys.Ceiling (Tree, Key);
706 Done : constant Node_Access := Key_Keys.Upper_Bound (Tree, Key);
711 raise Constraint_Error with "attempt to delete key not in set";
716 Node := Tree_Operations.Next (Node);
717 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
720 exit when Node = Done;
728 function Element (Container : Set; Key : Key_Type) return Element_Type is
729 Node : constant Node_Access :=
730 Key_Keys.Find (Container.Tree, Key);
734 raise Constraint_Error with "key not in set";
737 return Node.Element.all;
740 ---------------------
741 -- Equivalent_Keys --
742 ---------------------
744 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
759 procedure Exclude (Container : in out Set; Key : Key_Type) is
760 Tree : Tree_Type renames Container.Tree;
761 Node : Node_Access := Key_Keys.Ceiling (Tree, Key);
762 Done : constant Node_Access := Key_Keys.Upper_Bound (Tree, Key);
766 while Node /= Done loop
768 Node := Tree_Operations.Next (Node);
769 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
778 function Find (Container : Set; Key : Key_Type) return Cursor is
779 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
786 return Cursor'(Container'Unrestricted_Access, Node);
793 function Floor (Container : Set; Key : Key_Type) return Cursor is
794 Node : constant Node_Access := Key_Keys.Floor (Container.Tree, Key);
801 return Cursor'(Container'Unrestricted_Access, Node);
804 -------------------------
805 -- Is_Greater_Key_Node --
806 -------------------------
808 function Is_Greater_Key_Node
810 Right : Node_Access) return Boolean
813 return Key (Right.Element.all) < Left;
814 end Is_Greater_Key_Node;
816 ----------------------
817 -- Is_Less_Key_Node --
818 ----------------------
820 function Is_Less_Key_Node
822 Right : Node_Access) return Boolean
825 return Left < Key (Right.Element.all);
826 end Is_Less_Key_Node;
835 Process : not null access procedure (Position : Cursor))
837 procedure Process_Node (Node : Node_Access);
838 pragma Inline (Process_Node);
840 procedure Local_Iterate is
841 new Key_Keys.Generic_Iteration (Process_Node);
847 procedure Process_Node (Node : Node_Access) is
849 Process (Cursor'(Container'Unrestricted_Access, Node));
852 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
853 B : Natural renames T.Busy;
855 -- Start of processing for Iterate
861 Local_Iterate (T, Key);
875 function Key (Position : Cursor) return Key_Type is
877 if Position.Node = null then
878 raise Constraint_Error with
879 "Position cursor equals No_Element";
882 if Position.Node.Element = null then
883 raise Program_Error with
884 "Position cursor is bad";
887 pragma Assert (Vet (Position.Container.Tree, Position.Node),
888 "bad cursor in Key");
890 return Key (Position.Node.Element.all);
893 ---------------------
894 -- Reverse_Iterate --
895 ---------------------
897 procedure Reverse_Iterate
900 Process : not null access procedure (Position : Cursor))
902 procedure Process_Node (Node : Node_Access);
903 pragma Inline (Process_Node);
909 procedure Local_Reverse_Iterate is
910 new Key_Keys.Generic_Reverse_Iteration (Process_Node);
916 procedure Process_Node (Node : Node_Access) is
918 Process (Cursor'(Container'Unrestricted_Access, Node));
921 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
922 B : Natural renames T.Busy;
924 -- Start of processing for Reverse_Iterate
930 Local_Reverse_Iterate (T, Key);
944 procedure Update_Element
945 (Container : in out Set;
947 Process : not null access procedure (Element : in out Element_Type))
949 Tree : Tree_Type renames Container.Tree;
950 Node : constant Node_Access := Position.Node;
954 raise Constraint_Error with "Position cursor equals No_Element";
957 if Node.Element = null then
958 raise Program_Error with "Position cursor is bad";
961 if Position.Container /= Container'Unrestricted_Access then
962 raise Program_Error with "Position cursor designates wrong set";
965 pragma Assert (Vet (Tree, Node),
966 "bad cursor in Update_Element");
969 E : Element_Type renames Node.Element.all;
970 K : constant Key_Type := Key (E);
972 B : Natural renames Tree.Busy;
973 L : Natural renames Tree.Lock;
991 if Equivalent_Keys (Left => K, Right => Key (E)) then
996 -- Delete_Node checks busy-bit
998 Tree_Operations.Delete_Node_Sans_Free (Tree, Node);
1000 Insert_New_Item : declare
1001 function New_Node return Node_Access;
1002 pragma Inline (New_Node);
1004 procedure Insert_Post is
1005 new Element_Keys.Generic_Insert_Post (New_Node);
1007 procedure Unconditional_Insert is
1008 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1014 function New_Node return Node_Access is
1016 Node.Color := Red_Black_Trees.Red;
1017 Node.Parent := null;
1024 Result : Node_Access;
1026 -- Start of processing for Insert_New_Item
1029 Unconditional_Insert
1031 Key => Node.Element.all,
1034 pragma Assert (Result = Node);
1035 end Insert_New_Item;
1044 function Has_Element (Position : Cursor) return Boolean is
1046 return Position /= No_Element;
1053 procedure Insert (Container : in out Set; New_Item : Element_Type) is
1056 Insert (Container, New_Item, Position);
1060 (Container : in out Set;
1061 New_Item : Element_Type;
1062 Position : out Cursor)
1065 Insert_Sans_Hint (Container.Tree, New_Item, Position.Node);
1066 Position.Container := Container'Unrestricted_Access;
1069 ----------------------
1070 -- Insert_Sans_Hint --
1071 ----------------------
1073 procedure Insert_Sans_Hint
1074 (Tree : in out Tree_Type;
1075 New_Item : Element_Type;
1076 Node : out Node_Access)
1078 function New_Node return Node_Access;
1079 pragma Inline (New_Node);
1081 procedure Insert_Post is
1082 new Element_Keys.Generic_Insert_Post (New_Node);
1084 procedure Unconditional_Insert is
1085 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1091 function New_Node return Node_Access is
1092 Element : Element_Access := new Element_Type'(New_Item);
1095 return new Node_Type'(Parent => null,
1098 Color => Red_Black_Trees.Red,
1099 Element => Element);
1102 Free_Element (Element);
1106 -- Start of processing for Insert_Sans_Hint
1109 Unconditional_Insert (Tree, New_Item, Node);
1110 end Insert_Sans_Hint;
1112 ----------------------
1113 -- Insert_With_Hint --
1114 ----------------------
1116 procedure Insert_With_Hint
1117 (Dst_Tree : in out Tree_Type;
1118 Dst_Hint : Node_Access;
1119 Src_Node : Node_Access;
1120 Dst_Node : out Node_Access)
1122 function New_Node return Node_Access;
1123 pragma Inline (New_Node);
1125 procedure Insert_Post is
1126 new Element_Keys.Generic_Insert_Post (New_Node);
1128 procedure Insert_Sans_Hint is
1129 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1131 procedure Local_Insert_With_Hint is
1132 new Element_Keys.Generic_Unconditional_Insert_With_Hint
1140 function New_Node return Node_Access is
1141 X : Element_Access := new Element_Type'(Src_Node.Element.all);
1144 return new Node_Type'(Parent => null,
1156 -- Start of processing for Insert_With_Hint
1159 Local_Insert_With_Hint
1162 Src_Node.Element.all,
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.all = R.Element.all;
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.all < 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.all;
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.all < R.Element.all;
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 Item : Element_Type;
1251 Process : not null access procedure (Position : Cursor))
1253 procedure Process_Node (Node : Node_Access);
1254 pragma Inline (Process_Node);
1256 procedure Local_Iterate is
1257 new Element_Keys.Generic_Iteration (Process_Node);
1263 procedure Process_Node (Node : Node_Access) is
1265 Process (Cursor'(Container'Unrestricted_Access, Node));
1268 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1269 B : Natural renames T.Busy;
1271 -- Start of processing for Iterate
1277 Local_Iterate (T, Item);
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 Tree_Operations.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
1329 function Last (Container : Set) return Cursor is
1331 if Container.Tree.Last = null then
1335 return Cursor'(Container'Unrestricted_Access, Container.Tree.Last);
1342 function Last_Element (Container : Set) return Element_Type is
1344 if Container.Tree.Last = null then
1345 raise Constraint_Error with "set is empty";
1348 pragma Assert (Container.Tree.Last.Element /= null);
1349 return Container.Tree.Last.Element.all;
1356 function Left (Node : Node_Access) return Node_Access is
1365 function Length (Container : Set) return Count_Type is
1367 return Container.Tree.Length;
1375 new Tree_Operations.Generic_Move (Clear);
1377 procedure Move (Target : in out Set; Source : in out Set) is
1379 Move (Target => Target.Tree, Source => Source.Tree);
1386 function Next (Position : Cursor) return Cursor is
1388 if Position = No_Element then
1392 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1393 "bad cursor in Next");
1396 Node : constant Node_Access :=
1397 Tree_Operations.Next (Position.Node);
1404 return Cursor'(Position.Container, Node);
1408 procedure Next (Position : in out Cursor) is
1410 Position := Next (Position);
1417 function Overlap (Left, Right : Set) return Boolean is
1419 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1426 function Parent (Node : Node_Access) return Node_Access is
1435 function Previous (Position : Cursor) return Cursor is
1437 if Position = No_Element then
1441 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1442 "bad cursor in Previous");
1445 Node : constant Node_Access :=
1446 Tree_Operations.Previous (Position.Node);
1453 return Cursor'(Position.Container, Node);
1457 procedure Previous (Position : in out Cursor) is
1459 Position := Previous (Position);
1466 procedure Query_Element
1468 Process : not null access procedure (Element : Element_Type))
1471 if Position.Node = null then
1472 raise Constraint_Error with "Position cursor equals No_Element";
1475 if Position.Node.Element = null then
1476 raise Program_Error with "Position cursor is bad";
1479 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1480 "bad cursor in Query_Element");
1483 T : Tree_Type renames Position.Container.Tree;
1485 B : Natural renames T.Busy;
1486 L : Natural renames T.Lock;
1493 Process (Position.Node.Element.all);
1511 (Stream : not null access Root_Stream_Type'Class;
1512 Container : out Set)
1515 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1516 pragma Inline (Read_Node);
1519 new Tree_Operations.Generic_Read (Clear, Read_Node);
1526 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1528 Node : Node_Access := new Node_Type;
1530 Node.Element := new Element_Type'(Element_Type'Input (Stream));
1534 Free (Node); -- Note that Free deallocates elem too
1538 -- Start of processing for Read
1541 Read (Stream, Container.Tree);
1545 (Stream : not null access Root_Stream_Type'Class;
1549 raise Program_Error with "attempt to stream set cursor";
1552 ---------------------
1553 -- Replace_Element --
1554 ---------------------
1556 procedure Replace_Element
1557 (Tree : in out Tree_Type;
1559 Item : Element_Type)
1562 if Item < Node.Element.all
1563 or else Node.Element.all < Item
1567 if Tree.Lock > 0 then
1568 raise Program_Error with
1569 "attempt to tamper with cursors (set is locked)";
1573 X : Element_Access := Node.Element;
1575 Node.Element := new Element_Type'(Item);
1582 Tree_Operations.Delete_Node_Sans_Free (Tree, Node); -- Checks busy-bit
1584 Insert_New_Item : declare
1585 function New_Node return Node_Access;
1586 pragma Inline (New_Node);
1588 procedure Insert_Post is
1589 new Element_Keys.Generic_Insert_Post (New_Node);
1591 procedure Unconditional_Insert is
1592 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1598 function New_Node return Node_Access is
1600 Node.Element := new Element_Type'(Item); -- OK if fails
1601 Node.Color := Red_Black_Trees.Red;
1602 Node.Parent := null;
1609 Result : Node_Access;
1611 X : Element_Access := Node.Element;
1613 -- Start of processing for Insert_New_Item
1616 Unconditional_Insert
1620 pragma Assert (Result = Node);
1622 Free_Element (X); -- OK if fails
1623 end Insert_New_Item;
1624 end Replace_Element;
1626 procedure Replace_Element
1627 (Container : in out Set;
1629 New_Item : Element_Type)
1632 if Position.Node = null then
1633 raise Constraint_Error with "Position cursor equals No_Element";
1636 if Position.Node.Element = null then
1637 raise Program_Error with "Position cursor is bad";
1640 if Position.Container /= Container'Unrestricted_Access then
1641 raise Program_Error with "Position cursor designates wrong set";
1644 pragma Assert (Vet (Container.Tree, Position.Node),
1645 "bad cursor in Replace_Element");
1647 Replace_Element (Container.Tree, Position.Node, New_Item);
1648 end Replace_Element;
1650 ---------------------
1651 -- Reverse_Iterate --
1652 ---------------------
1654 procedure Reverse_Iterate
1656 Item : Element_Type;
1657 Process : not null access procedure (Position : Cursor))
1659 procedure Process_Node (Node : Node_Access);
1660 pragma Inline (Process_Node);
1662 procedure Local_Reverse_Iterate is
1663 new Element_Keys.Generic_Reverse_Iteration (Process_Node);
1669 procedure Process_Node (Node : Node_Access) is
1671 Process (Cursor'(Container'Unrestricted_Access, Node));
1674 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1675 B : Natural renames T.Busy;
1677 -- Start of processing for Reverse_Iterate
1683 Local_Reverse_Iterate (T, Item);
1691 end Reverse_Iterate;
1693 procedure Reverse_Iterate
1695 Process : not null access procedure (Position : Cursor))
1697 procedure Process_Node (Node : Node_Access);
1698 pragma Inline (Process_Node);
1700 procedure Local_Reverse_Iterate is
1701 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1707 procedure Process_Node (Node : Node_Access) is
1709 Process (Cursor'(Container'Unrestricted_Access, Node));
1712 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1713 B : Natural renames T.Busy;
1715 -- Start of processing for Reverse_Iterate
1721 Local_Reverse_Iterate (T);
1729 end Reverse_Iterate;
1735 function Right (Node : Node_Access) return Node_Access is
1744 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1746 Node.Color := Color;
1753 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1762 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1764 Node.Parent := Parent;
1771 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1773 Node.Right := Right;
1776 --------------------------
1777 -- Symmetric_Difference --
1778 --------------------------
1780 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1782 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
1783 end Symmetric_Difference;
1785 function Symmetric_Difference (Left, Right : Set) return Set is
1786 Tree : constant Tree_Type :=
1787 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
1789 return Set'(Controlled with Tree);
1790 end Symmetric_Difference;
1796 function To_Set (New_Item : Element_Type) return Set is
1801 Insert_Sans_Hint (Tree, New_Item, Node);
1802 return Set'(Controlled with Tree);
1809 procedure Union (Target : in out Set; Source : Set) is
1811 Set_Ops.Union (Target.Tree, Source.Tree);
1814 function Union (Left, Right : Set) return Set is
1815 Tree : constant Tree_Type :=
1816 Set_Ops.Union (Left.Tree, Right.Tree);
1818 return Set'(Controlled with Tree);
1826 (Stream : not null access Root_Stream_Type'Class;
1829 procedure Write_Node
1830 (Stream : not null access Root_Stream_Type'Class;
1831 Node : Node_Access);
1832 pragma Inline (Write_Node);
1835 new Tree_Operations.Generic_Write (Write_Node);
1841 procedure Write_Node
1842 (Stream : not null access Root_Stream_Type'Class;
1846 Element_Type'Output (Stream, Node.Element.all);
1849 -- Start of processing for Write
1852 Write (Stream, Container.Tree);
1856 (Stream : not null access Root_Stream_Type'Class;
1860 raise Program_Error with "attempt to stream set cursor";
1863 end Ada.Containers.Indefinite_Ordered_Multisets;