1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.INDEFINITE_ORDERED_SETS --
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.Containers.Red_Black_Trees.Generic_Operations;
31 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Operations);
33 with Ada.Containers.Red_Black_Trees.Generic_Keys;
34 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Keys);
36 with Ada.Containers.Red_Black_Trees.Generic_Set_Operations;
37 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Set_Operations);
39 with Ada.Unchecked_Deallocation;
41 with System; use type System.Address;
43 package body Ada.Containers.Indefinite_Ordered_Sets is
46 Ordered_Set_Iterator_Interfaces.Reversible_Iterator with record
47 Container : access constant Set;
51 overriding function First (Object : Iterator) return Cursor;
53 overriding function Last (Object : Iterator) return Cursor;
55 overriding function Next
57 Position : Cursor) return Cursor;
59 overriding function Previous
61 Position : Cursor) return Cursor;
63 -----------------------
64 -- Local Subprograms --
65 -----------------------
67 function Color (Node : Node_Access) return Color_Type;
68 pragma Inline (Color);
70 function Copy_Node (Source : Node_Access) return Node_Access;
71 pragma Inline (Copy_Node);
73 procedure Free (X : in out Node_Access);
75 procedure Insert_Sans_Hint
76 (Tree : in out Tree_Type;
77 New_Item : Element_Type;
78 Node : out Node_Access;
79 Inserted : out Boolean);
81 procedure Insert_With_Hint
82 (Dst_Tree : in out Tree_Type;
83 Dst_Hint : Node_Access;
84 Src_Node : Node_Access;
85 Dst_Node : out Node_Access);
87 function Is_Greater_Element_Node
89 Right : Node_Access) return Boolean;
90 pragma Inline (Is_Greater_Element_Node);
92 function Is_Less_Element_Node
94 Right : Node_Access) return Boolean;
95 pragma Inline (Is_Less_Element_Node);
97 function Is_Less_Node_Node (L, R : Node_Access) return Boolean;
98 pragma Inline (Is_Less_Node_Node);
100 function Left (Node : Node_Access) return Node_Access;
101 pragma Inline (Left);
103 function Parent (Node : Node_Access) return Node_Access;
104 pragma Inline (Parent);
106 procedure Replace_Element
107 (Tree : in out Tree_Type;
109 Item : Element_Type);
111 function Right (Node : Node_Access) return Node_Access;
112 pragma Inline (Right);
114 procedure Set_Color (Node : Node_Access; Color : Color_Type);
115 pragma Inline (Set_Color);
117 procedure Set_Left (Node : Node_Access; Left : Node_Access);
118 pragma Inline (Set_Left);
120 procedure Set_Parent (Node : Node_Access; Parent : Node_Access);
121 pragma Inline (Set_Parent);
123 procedure Set_Right (Node : Node_Access; Right : Node_Access);
124 pragma Inline (Set_Right);
126 --------------------------
127 -- Local Instantiations --
128 --------------------------
130 procedure Free_Element is
131 new Ada.Unchecked_Deallocation (Element_Type, Element_Access);
133 package Tree_Operations is
134 new Red_Black_Trees.Generic_Operations (Tree_Types);
136 procedure Delete_Tree is
137 new Tree_Operations.Generic_Delete_Tree (Free);
139 function Copy_Tree is
140 new Tree_Operations.Generic_Copy_Tree (Copy_Node, Delete_Tree);
144 package Element_Keys is
145 new Red_Black_Trees.Generic_Keys
146 (Tree_Operations => Tree_Operations,
147 Key_Type => Element_Type,
148 Is_Less_Key_Node => Is_Less_Element_Node,
149 Is_Greater_Key_Node => Is_Greater_Element_Node);
152 new Generic_Set_Operations
153 (Tree_Operations => Tree_Operations,
154 Insert_With_Hint => Insert_With_Hint,
155 Copy_Tree => Copy_Tree,
156 Delete_Tree => Delete_Tree,
157 Is_Less => Is_Less_Node_Node,
164 function "<" (Left, Right : Cursor) return Boolean is
166 if Left.Node = null then
167 raise Constraint_Error with "Left cursor equals No_Element";
170 if Right.Node = null then
171 raise Constraint_Error with "Right cursor equals No_Element";
174 if Left.Node.Element = null then
175 raise Program_Error with "Left cursor is bad";
178 if Right.Node.Element = null then
179 raise Program_Error with "Right cursor is bad";
182 pragma Assert (Vet (Left.Container.Tree, Left.Node),
183 "bad Left cursor in ""<""");
185 pragma Assert (Vet (Right.Container.Tree, Right.Node),
186 "bad Right cursor in ""<""");
188 return Left.Node.Element.all < Right.Node.Element.all;
191 function "<" (Left : Cursor; Right : Element_Type) return Boolean is
193 if Left.Node = null then
194 raise Constraint_Error with "Left cursor equals No_Element";
197 if Left.Node.Element = null then
198 raise Program_Error with "Left cursor is bad";
201 pragma Assert (Vet (Left.Container.Tree, Left.Node),
202 "bad Left cursor in ""<""");
204 return Left.Node.Element.all < Right;
207 function "<" (Left : Element_Type; Right : Cursor) return Boolean is
209 if Right.Node = null then
210 raise Constraint_Error with "Right cursor equals No_Element";
213 if Right.Node.Element = null then
214 raise Program_Error with "Right cursor is bad";
217 pragma Assert (Vet (Right.Container.Tree, Right.Node),
218 "bad Right cursor in ""<""");
220 return Left < Right.Node.Element.all;
227 function "=" (Left, Right : Set) return Boolean is
229 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean;
230 pragma Inline (Is_Equal_Node_Node);
233 new Tree_Operations.Generic_Equal (Is_Equal_Node_Node);
235 ------------------------
236 -- Is_Equal_Node_Node --
237 ------------------------
239 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
241 return L.Element.all = R.Element.all;
242 end Is_Equal_Node_Node;
244 -- Start of processing for "="
247 return Is_Equal (Left.Tree, Right.Tree);
254 function ">" (Left, Right : Cursor) return Boolean is
256 if Left.Node = null then
257 raise Constraint_Error with "Left cursor equals No_Element";
260 if Right.Node = null then
261 raise Constraint_Error with "Right cursor equals No_Element";
264 if Left.Node.Element = null then
265 raise Program_Error with "Left cursor is bad";
268 if Right.Node.Element = null then
269 raise Program_Error with "Right cursor is bad";
272 pragma Assert (Vet (Left.Container.Tree, Left.Node),
273 "bad Left cursor in "">""");
275 pragma Assert (Vet (Right.Container.Tree, Right.Node),
276 "bad Right cursor in "">""");
278 -- L > R same as R < L
280 return Right.Node.Element.all < Left.Node.Element.all;
283 function ">" (Left : Cursor; Right : Element_Type) return Boolean is
285 if Left.Node = null then
286 raise Constraint_Error with "Left cursor equals No_Element";
289 if Left.Node.Element = null then
290 raise Program_Error with "Left cursor is bad";
293 pragma Assert (Vet (Left.Container.Tree, Left.Node),
294 "bad Left cursor in "">""");
296 return Right < Left.Node.Element.all;
299 function ">" (Left : Element_Type; Right : Cursor) return Boolean is
301 if Right.Node = null then
302 raise Constraint_Error with "Right cursor equals No_Element";
305 if Right.Node.Element = null then
306 raise Program_Error with "Right cursor is bad";
309 pragma Assert (Vet (Right.Container.Tree, Right.Node),
310 "bad Right cursor in "">""");
312 return Right.Node.Element.all < Left;
319 procedure Adjust is new Tree_Operations.Generic_Adjust (Copy_Tree);
321 procedure Adjust (Container : in out Set) is
323 Adjust (Container.Tree);
330 procedure Assign (Target : in out Set; Source : Set) is
332 if Target'Address = Source'Address then
337 Target.Union (Source);
344 function Ceiling (Container : Set; Item : Element_Type) return Cursor is
345 Node : constant Node_Access :=
346 Element_Keys.Ceiling (Container.Tree, Item);
348 return (if Node = null then No_Element
349 else Cursor'(Container'Unrestricted_Access, Node));
357 new Tree_Operations.Generic_Clear (Delete_Tree);
359 procedure Clear (Container : in out Set) is
361 Clear (Container.Tree);
368 function Color (Node : Node_Access) return Color_Type is
377 function Contains (Container : Set; Item : Element_Type) return Boolean is
379 return Find (Container, Item) /= No_Element;
386 function Copy (Source : Set) return Set is
388 return Target : Set do
389 Target.Assign (Source);
397 function Copy_Node (Source : Node_Access) return Node_Access is
398 Element : Element_Access := new Element_Type'(Source.Element.all);
401 return new Node_Type'(Parent => null,
404 Color => Source.Color,
408 Free_Element (Element);
416 procedure Delete (Container : in out Set; Position : in out Cursor) is
418 if Position.Node = null then
419 raise Constraint_Error with "Position cursor equals No_Element";
422 if Position.Node.Element = null then
423 raise Program_Error with "Position cursor is bad";
426 if Position.Container /= Container'Unrestricted_Access then
427 raise Program_Error with "Position cursor designates wrong set";
430 pragma Assert (Vet (Container.Tree, Position.Node),
431 "bad cursor in Delete");
433 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
434 Free (Position.Node);
435 Position.Container := null;
438 procedure Delete (Container : in out Set; Item : Element_Type) is
440 Element_Keys.Find (Container.Tree, Item);
444 raise Constraint_Error with "attempt to delete element not in set";
447 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
455 procedure Delete_First (Container : in out Set) is
456 Tree : Tree_Type renames Container.Tree;
457 X : Node_Access := Tree.First;
460 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
469 procedure Delete_Last (Container : in out Set) is
470 Tree : Tree_Type renames Container.Tree;
471 X : Node_Access := Tree.Last;
474 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 := Set_Ops.Difference (Left.Tree, Right.Tree);
491 return Set'(Controlled with Tree);
498 function Element (Position : Cursor) return Element_Type is
500 if Position.Node = null then
501 raise Constraint_Error with "Position cursor equals No_Element";
504 if Position.Node.Element = null then
505 raise Program_Error with "Position cursor is bad";
508 pragma Assert (Vet (Position.Container.Tree, Position.Node),
509 "bad cursor in Element");
511 return Position.Node.Element.all;
514 -------------------------
515 -- Equivalent_Elements --
516 -------------------------
518 function Equivalent_Elements (Left, Right : Element_Type) return Boolean is
520 if Left < Right or else Right < Left then
525 end Equivalent_Elements;
527 ---------------------
528 -- Equivalent_Sets --
529 ---------------------
531 function Equivalent_Sets (Left, Right : Set) return Boolean is
533 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean;
534 pragma Inline (Is_Equivalent_Node_Node);
536 function Is_Equivalent is
537 new Tree_Operations.Generic_Equal (Is_Equivalent_Node_Node);
539 -----------------------------
540 -- Is_Equivalent_Node_Node --
541 -----------------------------
543 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean is
545 if L.Element.all < R.Element.all then
547 elsif R.Element.all < L.Element.all then
552 end Is_Equivalent_Node_Node;
554 -- Start of processing for Equivalent_Sets
557 return Is_Equivalent (Left.Tree, Right.Tree);
564 procedure Exclude (Container : in out Set; Item : Element_Type) is
566 Element_Keys.Find (Container.Tree, Item);
569 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
578 function Find (Container : Set; Item : Element_Type) return Cursor is
579 Node : constant Node_Access :=
580 Element_Keys.Find (Container.Tree, Item);
587 return Cursor'(Container'Unrestricted_Access, Node);
594 function First (Container : Set) return Cursor is
597 (if Container.Tree.First = null then No_Element
598 else Cursor'(Container'Unrestricted_Access, Container.Tree.First));
601 function First (Object : Iterator) return Cursor is
604 Object.Container.all'Unrestricted_Access, Object.Container.Tree.First);
611 function First_Element (Container : Set) return Element_Type is
613 if Container.Tree.First = null then
614 raise Constraint_Error with "set is empty";
616 return Container.Tree.First.Element.all;
624 function Floor (Container : Set; Item : Element_Type) return Cursor is
625 Node : constant Node_Access :=
626 Element_Keys.Floor (Container.Tree, Item);
628 return (if Node = null then No_Element
629 else Cursor'(Container'Unrestricted_Access, Node));
636 procedure Free (X : in out Node_Access) is
637 procedure Deallocate is
638 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
650 Free_Element (X.Element);
665 package body Generic_Keys is
667 -----------------------
668 -- Local Subprograms --
669 -----------------------
671 function Is_Greater_Key_Node
673 Right : Node_Access) return Boolean;
674 pragma Inline (Is_Greater_Key_Node);
676 function Is_Less_Key_Node
678 Right : Node_Access) return Boolean;
679 pragma Inline (Is_Less_Key_Node);
681 --------------------------
682 -- Local Instantiations --
683 --------------------------
686 new Red_Black_Trees.Generic_Keys
687 (Tree_Operations => Tree_Operations,
688 Key_Type => Key_Type,
689 Is_Less_Key_Node => Is_Less_Key_Node,
690 Is_Greater_Key_Node => Is_Greater_Key_Node);
696 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
697 Node : constant Node_Access :=
698 Key_Keys.Ceiling (Container.Tree, Key);
700 return (if Node = null then No_Element
701 else Cursor'(Container'Unrestricted_Access, Node));
708 function Contains (Container : Set; Key : Key_Type) return Boolean is
710 return Find (Container, Key) /= No_Element;
717 procedure Delete (Container : in out Set; Key : Key_Type) is
718 X : Node_Access := Key_Keys.Find (Container.Tree, Key);
722 raise Constraint_Error with "attempt to delete key not in set";
725 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
733 function Element (Container : Set; Key : Key_Type) return Element_Type is
734 Node : constant Node_Access :=
735 Key_Keys.Find (Container.Tree, Key);
739 raise Constraint_Error with "key not in set";
742 return Node.Element.all;
745 ---------------------
746 -- Equivalent_Keys --
747 ---------------------
749 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
751 if Left < Right or else Right < Left then
762 procedure Exclude (Container : in out Set; Key : Key_Type) is
763 X : Node_Access := Key_Keys.Find (Container.Tree, Key);
766 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
775 function Find (Container : Set; Key : Key_Type) return Cursor is
776 Node : constant Node_Access :=
777 Key_Keys.Find (Container.Tree, Key);
779 return (if Node = null then No_Element
780 else Cursor'(Container'Unrestricted_Access, Node));
787 function Floor (Container : Set; Key : Key_Type) return Cursor is
788 Node : constant Node_Access :=
789 Key_Keys.Floor (Container.Tree, Key);
791 return (if Node = null then No_Element
792 else Cursor'(Container'Unrestricted_Access, Node));
795 -------------------------
796 -- Is_Greater_Key_Node --
797 -------------------------
799 function Is_Greater_Key_Node
801 Right : Node_Access) return Boolean
804 return Key (Right.Element.all) < Left;
805 end Is_Greater_Key_Node;
807 ----------------------
808 -- Is_Less_Key_Node --
809 ----------------------
811 function Is_Less_Key_Node
813 Right : Node_Access) return Boolean
816 return Left < Key (Right.Element.all);
817 end Is_Less_Key_Node;
823 function Key (Position : Cursor) return Key_Type is
825 if Position.Node = null then
826 raise Constraint_Error with
827 "Position cursor equals No_Element";
830 if Position.Node.Element = null then
831 raise Program_Error with
832 "Position cursor is bad";
835 pragma Assert (Vet (Position.Container.Tree, Position.Node),
836 "bad cursor in Key");
838 return Key (Position.Node.Element.all);
846 (Container : in out Set;
848 New_Item : Element_Type)
850 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
854 raise Constraint_Error with
855 "attempt to replace key not in set";
858 Replace_Element (Container.Tree, Node, New_Item);
861 -----------------------------------
862 -- Update_Element_Preserving_Key --
863 -----------------------------------
865 procedure Update_Element_Preserving_Key
866 (Container : in out Set;
868 Process : not null access
869 procedure (Element : in out Element_Type))
871 Tree : Tree_Type renames Container.Tree;
874 if Position.Node = null then
875 raise Constraint_Error with "Position cursor equals No_Element";
878 if Position.Node.Element = null then
879 raise Program_Error with "Position cursor is bad";
882 if Position.Container /= Container'Unrestricted_Access then
883 raise Program_Error with "Position cursor designates wrong set";
886 pragma Assert (Vet (Container.Tree, Position.Node),
887 "bad cursor in Update_Element_Preserving_Key");
890 E : Element_Type renames Position.Node.Element.all;
891 K : constant Key_Type := Key (E);
893 B : Natural renames Tree.Busy;
894 L : Natural renames Tree.Lock;
912 if Equivalent_Keys (K, Key (E)) then
918 X : Node_Access := Position.Node;
920 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
924 raise Program_Error with "key was modified";
925 end Update_Element_Preserving_Key;
927 function Reference_Preserving_Key
928 (Container : aliased in out Set;
929 Key : Key_Type) return Constant_Reference_Type
931 Position : constant Cursor := Find (Container, Key);
934 if Position.Container = null then
935 raise Constraint_Error with "Position cursor has no element";
938 return (Element => Position.Node.Element);
939 end Reference_Preserving_Key;
941 function Reference_Preserving_Key
942 (Container : aliased in out Set;
943 Key : Key_Type) return Reference_Type
945 Position : constant Cursor := Find (Container, Key);
948 if Position.Container = null then
949 raise Constraint_Error with "Position cursor has no element";
952 return (Element => Position.Node.Element);
953 end Reference_Preserving_Key;
956 (Stream : not null access Root_Stream_Type'Class;
957 Item : out Reference_Type)
960 raise Program_Error with "attempt to stream reference";
964 (Stream : not null access Root_Stream_Type'Class;
965 Item : Reference_Type)
968 raise Program_Error with "attempt to stream reference";
977 function Has_Element (Position : Cursor) return Boolean is
979 return Position /= No_Element;
986 procedure Include (Container : in out Set; New_Item : Element_Type) is
993 Insert (Container, New_Item, Position, Inserted);
996 if Container.Tree.Lock > 0 then
997 raise Program_Error with
998 "attempt to tamper with elements (set is locked)";
1001 X := Position.Node.Element;
1002 Position.Node.Element := new Element_Type'(New_Item);
1012 (Container : in out Set;
1013 New_Item : Element_Type;
1014 Position : out Cursor;
1015 Inserted : out Boolean)
1024 Position.Container := Container'Unrestricted_Access;
1027 procedure Insert (Container : in out Set; New_Item : Element_Type) is
1029 pragma Unreferenced (Position);
1034 Insert (Container, New_Item, Position, Inserted);
1036 if not Inserted then
1037 raise Constraint_Error with
1038 "attempt to insert element already in set";
1042 ----------------------
1043 -- Insert_Sans_Hint --
1044 ----------------------
1046 procedure Insert_Sans_Hint
1047 (Tree : in out Tree_Type;
1048 New_Item : Element_Type;
1049 Node : out Node_Access;
1050 Inserted : out Boolean)
1052 function New_Node return Node_Access;
1053 pragma Inline (New_Node);
1055 procedure Insert_Post is
1056 new Element_Keys.Generic_Insert_Post (New_Node);
1058 procedure Conditional_Insert_Sans_Hint is
1059 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1065 function New_Node return Node_Access is
1066 Element : Element_Access := new Element_Type'(New_Item);
1069 return new Node_Type'(Parent => null,
1072 Color => Red_Black_Trees.Red,
1073 Element => Element);
1076 Free_Element (Element);
1080 -- Start of processing for Insert_Sans_Hint
1083 Conditional_Insert_Sans_Hint
1088 end Insert_Sans_Hint;
1090 ----------------------
1091 -- Insert_With_Hint --
1092 ----------------------
1094 procedure Insert_With_Hint
1095 (Dst_Tree : in out Tree_Type;
1096 Dst_Hint : Node_Access;
1097 Src_Node : Node_Access;
1098 Dst_Node : out Node_Access)
1101 pragma Unreferenced (Success);
1103 function New_Node return Node_Access;
1105 procedure Insert_Post is
1106 new Element_Keys.Generic_Insert_Post (New_Node);
1108 procedure Insert_Sans_Hint is
1109 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1111 procedure Insert_With_Hint is
1112 new Element_Keys.Generic_Conditional_Insert_With_Hint
1120 function New_Node return Node_Access is
1121 Element : Element_Access :=
1122 new Element_Type'(Src_Node.Element.all);
1127 Node := new Node_Type;
1130 Free_Element (Element);
1134 Node.Element := Element;
1138 -- Start of processing for Insert_With_Hint
1144 Src_Node.Element.all,
1147 end Insert_With_Hint;
1153 procedure Intersection (Target : in out Set; Source : Set) is
1155 Set_Ops.Intersection (Target.Tree, Source.Tree);
1158 function Intersection (Left, Right : Set) return Set is
1159 Tree : constant Tree_Type :=
1160 Set_Ops.Intersection (Left.Tree, Right.Tree);
1162 return Set'(Controlled with Tree);
1169 function Is_Empty (Container : Set) return Boolean is
1171 return Container.Tree.Length = 0;
1174 -----------------------------
1175 -- Is_Greater_Element_Node --
1176 -----------------------------
1178 function Is_Greater_Element_Node
1179 (Left : Element_Type;
1180 Right : Node_Access) return Boolean
1183 -- e > node same as node < e
1185 return Right.Element.all < Left;
1186 end Is_Greater_Element_Node;
1188 --------------------------
1189 -- Is_Less_Element_Node --
1190 --------------------------
1192 function Is_Less_Element_Node
1193 (Left : Element_Type;
1194 Right : Node_Access) return Boolean
1197 return Left < Right.Element.all;
1198 end Is_Less_Element_Node;
1200 -----------------------
1201 -- Is_Less_Node_Node --
1202 -----------------------
1204 function Is_Less_Node_Node (L, R : Node_Access) return Boolean is
1206 return L.Element.all < R.Element.all;
1207 end Is_Less_Node_Node;
1213 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1215 return Set_Ops.Is_Subset (Subset => Subset.Tree, Of_Set => Of_Set.Tree);
1224 Process : not null access procedure (Position : Cursor))
1226 procedure Process_Node (Node : Node_Access);
1227 pragma Inline (Process_Node);
1229 procedure Local_Iterate is
1230 new Tree_Operations.Generic_Iteration (Process_Node);
1236 procedure Process_Node (Node : Node_Access) is
1238 Process (Cursor'(Container'Unrestricted_Access, Node));
1241 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1242 B : Natural renames T.Busy;
1244 -- Start of processing for Iterate
1262 return Ordered_Set_Iterator_Interfaces.Reversible_Iterator'class
1264 It : constant Iterator :=
1265 (Container'Unchecked_Access, Container.Tree.First);
1273 return Ordered_Set_Iterator_Interfaces.Reversible_Iterator'class
1275 It : constant Iterator := (Container'Unchecked_Access, Start.Node);
1284 function Last (Container : Set) return Cursor is
1287 (if Container.Tree.Last = null then No_Element
1288 else Cursor'(Container'Unrestricted_Access, Container.Tree.Last));
1291 function Last (Object : Iterator) return Cursor is
1293 return (if Object.Container.Tree.Last = null then No_Element
1294 else Cursor'(Object.Container.all'Unrestricted_Access,
1295 Object.Container.Tree.Last));
1302 function Last_Element (Container : Set) return Element_Type is
1304 if Container.Tree.Last = null then
1305 raise Constraint_Error with "set is empty";
1307 return Container.Tree.Last.Element.all;
1315 function Left (Node : Node_Access) return Node_Access is
1324 function Length (Container : Set) return Count_Type is
1326 return Container.Tree.Length;
1333 procedure Move is new Tree_Operations.Generic_Move (Clear);
1335 procedure Move (Target : in out Set; Source : in out Set) is
1337 Move (Target => Target.Tree, Source => Source.Tree);
1344 procedure Next (Position : in out Cursor) is
1346 Position := Next (Position);
1349 function Next (Position : Cursor) return Cursor is
1351 if Position = No_Element then
1355 if Position.Node.Element = null then
1356 raise Program_Error with "Position cursor is bad";
1359 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1360 "bad cursor in Next");
1363 Node : constant Node_Access :=
1364 Tree_Operations.Next (Position.Node);
1366 return (if Node = null then No_Element
1367 else Cursor'(Position.Container, Node));
1373 Position : Cursor) return Cursor
1375 pragma Unreferenced (Object);
1377 return Next (Position);
1384 function Overlap (Left, Right : Set) return Boolean is
1386 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1393 function Parent (Node : Node_Access) return Node_Access is
1402 procedure Previous (Position : in out Cursor) is
1404 Position := Previous (Position);
1407 function Previous (Position : Cursor) return Cursor is
1409 if Position = No_Element then
1413 if Position.Node.Element = null then
1414 raise Program_Error with "Position cursor is bad";
1417 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1418 "bad cursor in Previous");
1421 Node : constant Node_Access :=
1422 Tree_Operations.Previous (Position.Node);
1424 return (if Node = null then No_Element
1425 else Cursor'(Position.Container, Node));
1431 Position : Cursor) return Cursor
1433 pragma Unreferenced (Object);
1435 return Previous (Position);
1442 procedure Query_Element
1444 Process : not null access procedure (Element : Element_Type))
1447 if Position.Node = null then
1448 raise Constraint_Error with "Position cursor equals No_Element";
1451 if Position.Node.Element = null then
1452 raise Program_Error with "Position cursor is bad";
1455 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1456 "bad cursor in Query_Element");
1459 T : Tree_Type renames Position.Container.Tree;
1461 B : Natural renames T.Busy;
1462 L : Natural renames T.Lock;
1469 Process (Position.Node.Element.all);
1487 (Stream : not null access Root_Stream_Type'Class;
1488 Container : out Set)
1491 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1492 pragma Inline (Read_Node);
1495 new Tree_Operations.Generic_Read (Clear, Read_Node);
1502 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1504 Node : Node_Access := new Node_Type;
1507 Node.Element := new Element_Type'(Element_Type'Input (Stream));
1512 Free (Node); -- Note that Free deallocates elem too
1516 -- Start of processing for Read
1519 Read (Stream, Container.Tree);
1523 (Stream : not null access Root_Stream_Type'Class;
1527 raise Program_Error with "attempt to stream set cursor";
1531 (Stream : not null access Root_Stream_Type'Class;
1532 Item : out Constant_Reference_Type)
1535 raise Program_Error with "attempt to stream reference";
1542 function Constant_Reference (Container : Set; Position : Cursor)
1543 return Constant_Reference_Type
1545 pragma Unreferenced (Container);
1547 if Position.Container = null then
1548 raise Constraint_Error with "Position cursor has no element";
1551 return (Element => Position.Node.Element.all'Access);
1552 end Constant_Reference;
1558 procedure Replace (Container : in out Set; New_Item : Element_Type) is
1559 Node : constant Node_Access :=
1560 Element_Keys.Find (Container.Tree, New_Item);
1563 pragma Warnings (Off, X);
1567 raise Constraint_Error with "attempt to replace element not in set";
1570 if Container.Tree.Lock > 0 then
1571 raise Program_Error with
1572 "attempt to tamper with elements (set is locked)";
1576 Node.Element := new Element_Type'(New_Item);
1580 ---------------------
1581 -- Replace_Element --
1582 ---------------------
1584 procedure Replace_Element
1585 (Tree : in out Tree_Type;
1587 Item : Element_Type)
1589 pragma Assert (Node /= null);
1590 pragma Assert (Node.Element /= null);
1592 function New_Node return Node_Access;
1593 pragma Inline (New_Node);
1595 procedure Local_Insert_Post is
1596 new Element_Keys.Generic_Insert_Post (New_Node);
1598 procedure Local_Insert_Sans_Hint is
1599 new Element_Keys.Generic_Conditional_Insert (Local_Insert_Post);
1601 procedure Local_Insert_With_Hint is
1602 new Element_Keys.Generic_Conditional_Insert_With_Hint
1604 Local_Insert_Sans_Hint);
1610 function New_Node return Node_Access is
1612 Node.Element := new Element_Type'(Item); -- OK if fails
1614 Node.Parent := null;
1621 Result : Node_Access;
1624 X : Element_Access := Node.Element;
1626 -- Start of processing for Replace_Element
1629 if Item < Node.Element.all
1630 or else Node.Element.all < Item
1635 if Tree.Lock > 0 then
1636 raise Program_Error with
1637 "attempt to tamper with elements (set is locked)";
1640 Node.Element := new Element_Type'(Item);
1646 Hint := Element_Keys.Ceiling (Tree, Item);
1651 elsif Item < Hint.Element.all then
1653 if Tree.Lock > 0 then
1654 raise Program_Error with
1655 "attempt to tamper with elements (set is locked)";
1658 Node.Element := new Element_Type'(Item);
1665 pragma Assert (not (Hint.Element.all < Item));
1666 raise Program_Error with "attempt to replace existing element";
1669 Tree_Operations.Delete_Node_Sans_Free (Tree, Node); -- Checks busy-bit
1671 Local_Insert_With_Hint
1676 Inserted => Inserted);
1678 pragma Assert (Inserted);
1679 pragma Assert (Result = Node);
1682 end Replace_Element;
1684 procedure Replace_Element
1685 (Container : in out Set;
1687 New_Item : Element_Type)
1690 if Position.Node = null then
1691 raise Constraint_Error with "Position cursor equals No_Element";
1694 if Position.Node.Element = null then
1695 raise Program_Error with "Position cursor is bad";
1698 if Position.Container /= Container'Unrestricted_Access then
1699 raise Program_Error with "Position cursor designates wrong set";
1702 pragma Assert (Vet (Container.Tree, Position.Node),
1703 "bad cursor in Replace_Element");
1705 Replace_Element (Container.Tree, Position.Node, New_Item);
1706 end Replace_Element;
1708 ---------------------
1709 -- Reverse_Iterate --
1710 ---------------------
1712 procedure Reverse_Iterate
1714 Process : not null access procedure (Position : Cursor))
1716 procedure Process_Node (Node : Node_Access);
1717 pragma Inline (Process_Node);
1719 procedure Local_Reverse_Iterate is
1720 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1726 procedure Process_Node (Node : Node_Access) is
1728 Process (Cursor'(Container'Unrestricted_Access, Node));
1731 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1732 B : Natural renames T.Busy;
1734 -- Start of processing for Reverse_Iterate
1740 Local_Reverse_Iterate (T);
1748 end Reverse_Iterate;
1754 function Right (Node : Node_Access) return Node_Access is
1763 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1765 Node.Color := Color;
1772 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1781 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1783 Node.Parent := Parent;
1790 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1792 Node.Right := Right;
1795 --------------------------
1796 -- Symmetric_Difference --
1797 --------------------------
1799 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1801 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
1802 end Symmetric_Difference;
1804 function Symmetric_Difference (Left, Right : Set) return Set is
1805 Tree : constant Tree_Type :=
1806 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
1808 return Set'(Controlled with Tree);
1809 end Symmetric_Difference;
1815 function To_Set (New_Item : Element_Type) return Set is
1819 pragma Unreferenced (Node, Inserted);
1821 Insert_Sans_Hint (Tree, New_Item, Node, Inserted);
1822 return Set'(Controlled with Tree);
1829 procedure Union (Target : in out Set; Source : Set) is
1831 Set_Ops.Union (Target.Tree, Source.Tree);
1834 function Union (Left, Right : Set) return Set is
1835 Tree : constant Tree_Type :=
1836 Set_Ops.Union (Left.Tree, Right.Tree);
1838 return Set'(Controlled with Tree);
1846 (Stream : not null access Root_Stream_Type'Class;
1849 procedure Write_Node
1850 (Stream : not null access Root_Stream_Type'Class;
1851 Node : Node_Access);
1852 pragma Inline (Write_Node);
1855 new Tree_Operations.Generic_Write (Write_Node);
1861 procedure Write_Node
1862 (Stream : not null access Root_Stream_Type'Class;
1866 Element_Type'Output (Stream, Node.Element.all);
1869 -- Start of processing for Write
1872 Write (Stream, Container.Tree);
1876 (Stream : not null access Root_Stream_Type'Class;
1880 raise Program_Error with "attempt to stream set cursor";
1884 (Stream : not null access Root_Stream_Type'Class;
1885 Item : Constant_Reference_Type)
1888 raise Program_Error with "attempt to stream reference";
1891 end Ada.Containers.Indefinite_Ordered_Sets;