1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.INDEFINITE_ORDERED_SETS --
9 -- Copyright (C) 2004-2015, 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.Helpers; use Ada.Containers.Helpers;
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 Ada.Unchecked_Deallocation;
43 with System; use type System.Address;
45 package body Ada.Containers.Indefinite_Ordered_Sets is
47 pragma Warnings (Off, "variable ""Busy*"" is not referenced");
48 pragma Warnings (Off, "variable ""Lock*"" is not referenced");
49 -- See comment in Ada.Containers.Helpers
51 -----------------------
52 -- Local Subprograms --
53 -----------------------
55 function Color (Node : Node_Access) return Color_Type;
56 pragma Inline (Color);
58 function Copy_Node (Source : Node_Access) return Node_Access;
59 pragma Inline (Copy_Node);
61 procedure Free (X : in out Node_Access);
63 procedure Insert_Sans_Hint
64 (Tree : in out Tree_Type;
65 New_Item : Element_Type;
66 Node : out Node_Access;
67 Inserted : out Boolean);
69 procedure Insert_With_Hint
70 (Dst_Tree : in out Tree_Type;
71 Dst_Hint : Node_Access;
72 Src_Node : Node_Access;
73 Dst_Node : out Node_Access);
75 function Is_Greater_Element_Node
77 Right : Node_Access) return Boolean;
78 pragma Inline (Is_Greater_Element_Node);
80 function Is_Less_Element_Node
82 Right : Node_Access) return Boolean;
83 pragma Inline (Is_Less_Element_Node);
85 function Is_Less_Node_Node (L, R : Node_Access) return Boolean;
86 pragma Inline (Is_Less_Node_Node);
88 function Left (Node : Node_Access) return Node_Access;
91 function Parent (Node : Node_Access) return Node_Access;
92 pragma Inline (Parent);
94 procedure Replace_Element
95 (Tree : in out Tree_Type;
99 function Right (Node : Node_Access) return Node_Access;
100 pragma Inline (Right);
102 procedure Set_Color (Node : Node_Access; Color : Color_Type);
103 pragma Inline (Set_Color);
105 procedure Set_Left (Node : Node_Access; Left : Node_Access);
106 pragma Inline (Set_Left);
108 procedure Set_Parent (Node : Node_Access; Parent : Node_Access);
109 pragma Inline (Set_Parent);
111 procedure Set_Right (Node : Node_Access; Right : Node_Access);
112 pragma Inline (Set_Right);
114 --------------------------
115 -- Local Instantiations --
116 --------------------------
118 procedure Free_Element is
119 new Ada.Unchecked_Deallocation (Element_Type, Element_Access);
121 package Tree_Operations is
122 new Red_Black_Trees.Generic_Operations (Tree_Types);
124 procedure Delete_Tree is
125 new Tree_Operations.Generic_Delete_Tree (Free);
127 function Copy_Tree is
128 new Tree_Operations.Generic_Copy_Tree (Copy_Node, Delete_Tree);
132 package Element_Keys is
133 new Red_Black_Trees.Generic_Keys
134 (Tree_Operations => Tree_Operations,
135 Key_Type => Element_Type,
136 Is_Less_Key_Node => Is_Less_Element_Node,
137 Is_Greater_Key_Node => Is_Greater_Element_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,
152 function "<" (Left, Right : Cursor) return Boolean is
154 if Checks and then Left.Node = null then
155 raise Constraint_Error with "Left cursor equals No_Element";
158 if Checks and then Right.Node = null then
159 raise Constraint_Error with "Right cursor equals No_Element";
162 if Checks and then Left.Node.Element = null then
163 raise Program_Error with "Left cursor is bad";
166 if Checks and then Right.Node.Element = null then
167 raise Program_Error with "Right cursor is bad";
170 pragma Assert (Vet (Left.Container.Tree, Left.Node),
171 "bad Left cursor in ""<""");
173 pragma Assert (Vet (Right.Container.Tree, Right.Node),
174 "bad Right cursor in ""<""");
176 return Left.Node.Element.all < Right.Node.Element.all;
179 function "<" (Left : Cursor; Right : Element_Type) return Boolean is
181 if Checks and then Left.Node = null then
182 raise Constraint_Error with "Left cursor equals No_Element";
185 if Checks and then Left.Node.Element = null then
186 raise Program_Error with "Left cursor is bad";
189 pragma Assert (Vet (Left.Container.Tree, Left.Node),
190 "bad Left cursor in ""<""");
192 return Left.Node.Element.all < Right;
195 function "<" (Left : Element_Type; Right : Cursor) return Boolean is
197 if Checks and then Right.Node = null then
198 raise Constraint_Error with "Right cursor equals No_Element";
201 if Checks and then Right.Node.Element = null then
202 raise Program_Error with "Right cursor is bad";
205 pragma Assert (Vet (Right.Container.Tree, Right.Node),
206 "bad Right cursor in ""<""");
208 return Left < Right.Node.Element.all;
215 function "=" (Left, Right : Set) return Boolean is
217 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean;
218 pragma Inline (Is_Equal_Node_Node);
221 new Tree_Operations.Generic_Equal (Is_Equal_Node_Node);
223 ------------------------
224 -- Is_Equal_Node_Node --
225 ------------------------
227 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
229 return L.Element.all = R.Element.all;
230 end Is_Equal_Node_Node;
232 -- Start of processing for "="
235 return Is_Equal (Left.Tree, Right.Tree);
242 function ">" (Left, Right : Cursor) return Boolean is
244 if Checks and then Left.Node = null then
245 raise Constraint_Error with "Left cursor equals No_Element";
248 if Checks and then Right.Node = null then
249 raise Constraint_Error with "Right cursor equals No_Element";
252 if Checks and then Left.Node.Element = null then
253 raise Program_Error with "Left cursor is bad";
256 if Checks and then Right.Node.Element = null then
257 raise Program_Error with "Right cursor is bad";
260 pragma Assert (Vet (Left.Container.Tree, Left.Node),
261 "bad Left cursor in "">""");
263 pragma Assert (Vet (Right.Container.Tree, Right.Node),
264 "bad Right cursor in "">""");
266 -- L > R same as R < L
268 return Right.Node.Element.all < Left.Node.Element.all;
271 function ">" (Left : Cursor; Right : Element_Type) return Boolean is
273 if Checks and then Left.Node = null then
274 raise Constraint_Error with "Left cursor equals No_Element";
277 if Checks and then Left.Node.Element = null then
278 raise Program_Error with "Left cursor is bad";
281 pragma Assert (Vet (Left.Container.Tree, Left.Node),
282 "bad Left cursor in "">""");
284 return Right < Left.Node.Element.all;
287 function ">" (Left : Element_Type; Right : Cursor) return Boolean is
289 if Checks and then Right.Node = null then
290 raise Constraint_Error with "Right cursor equals No_Element";
293 if Checks and then Right.Node.Element = null then
294 raise Program_Error with "Right cursor is bad";
297 pragma Assert (Vet (Right.Container.Tree, Right.Node),
298 "bad Right cursor in "">""");
300 return Right.Node.Element.all < Left;
307 procedure Adjust is new Tree_Operations.Generic_Adjust (Copy_Tree);
309 procedure Adjust (Container : in out Set) is
311 Adjust (Container.Tree);
318 procedure Assign (Target : in out Set; Source : Set) is
320 if Target'Address = Source'Address then
325 Target.Union (Source);
332 function Ceiling (Container : Set; Item : Element_Type) return Cursor is
333 Node : constant Node_Access :=
334 Element_Keys.Ceiling (Container.Tree, Item);
336 return (if Node = null then No_Element
337 else Cursor'(Container'Unrestricted_Access, Node));
345 new Tree_Operations.Generic_Clear (Delete_Tree);
347 procedure Clear (Container : in out Set) is
349 Clear (Container.Tree);
356 function Color (Node : Node_Access) return Color_Type is
361 ------------------------
362 -- Constant_Reference --
363 ------------------------
365 function Constant_Reference
366 (Container : aliased Set;
367 Position : Cursor) return Constant_Reference_Type
370 if Checks and then Position.Container = null then
371 raise Constraint_Error with "Position cursor has no element";
374 if Checks and then Position.Container /= Container'Unrestricted_Access
376 raise Program_Error with
377 "Position cursor designates wrong container";
380 if Checks and then Position.Node.Element = null then
381 raise Program_Error with "Node has no element";
385 (Vet (Container.Tree, Position.Node),
386 "bad cursor in Constant_Reference");
389 Tree : Tree_Type renames Position.Container.all.Tree;
390 TC : constant Tamper_Counts_Access :=
391 Tree.TC'Unrestricted_Access;
393 return R : constant Constant_Reference_Type :=
394 (Element => Position.Node.Element.all'Access,
395 Control => (Controlled with TC))
400 end Constant_Reference;
406 function Contains (Container : Set; Item : Element_Type) return Boolean is
408 return Find (Container, Item) /= No_Element;
415 function Copy (Source : Set) return Set is
417 return Target : Set do
418 Target.Assign (Source);
426 function Copy_Node (Source : Node_Access) return Node_Access is
427 Element : Element_Access := new Element_Type'(Source.Element.all);
430 return new Node_Type'(Parent => null,
433 Color => Source.Color,
438 Free_Element (Element);
446 procedure Delete (Container : in out Set; Position : in out Cursor) is
448 if Checks and then Position.Node = null then
449 raise Constraint_Error with "Position cursor equals No_Element";
452 if Checks and then Position.Node.Element = null then
453 raise Program_Error with "Position cursor is bad";
456 if Checks and then Position.Container /= Container'Unrestricted_Access
458 raise Program_Error with "Position cursor designates wrong set";
461 pragma Assert (Vet (Container.Tree, Position.Node),
462 "bad cursor in Delete");
464 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
465 Free (Position.Node);
466 Position.Container := null;
469 procedure Delete (Container : in out Set; Item : Element_Type) is
470 X : Node_Access := Element_Keys.Find (Container.Tree, Item);
472 if Checks and then X = null then
473 raise Constraint_Error with "attempt to delete element not in set";
476 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
484 procedure Delete_First (Container : in out Set) is
485 Tree : Tree_Type renames Container.Tree;
486 X : Node_Access := Tree.First;
489 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
498 procedure Delete_Last (Container : in out Set) is
499 Tree : Tree_Type renames Container.Tree;
500 X : Node_Access := Tree.Last;
503 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
512 procedure Difference (Target : in out Set; Source : Set) is
514 Set_Ops.Difference (Target.Tree, Source.Tree);
517 function Difference (Left, Right : Set) return Set is
518 Tree : constant Tree_Type := Set_Ops.Difference (Left.Tree, Right.Tree);
520 return Set'(Controlled with Tree);
527 function Element (Position : Cursor) return Element_Type is
529 if Checks and then Position.Node = null then
530 raise Constraint_Error with "Position cursor equals No_Element";
533 if Checks and then Position.Node.Element = null then
534 raise Program_Error with "Position cursor is bad";
537 pragma Assert (Vet (Position.Container.Tree, Position.Node),
538 "bad cursor in Element");
540 return Position.Node.Element.all;
543 -------------------------
544 -- Equivalent_Elements --
545 -------------------------
547 function Equivalent_Elements (Left, Right : Element_Type) return Boolean is
549 if Left < Right or else Right < Left then
554 end Equivalent_Elements;
556 ---------------------
557 -- Equivalent_Sets --
558 ---------------------
560 function Equivalent_Sets (Left, Right : Set) return Boolean is
562 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean;
563 pragma Inline (Is_Equivalent_Node_Node);
565 function Is_Equivalent is
566 new Tree_Operations.Generic_Equal (Is_Equivalent_Node_Node);
568 -----------------------------
569 -- Is_Equivalent_Node_Node --
570 -----------------------------
572 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean is
574 if L.Element.all < R.Element.all then
576 elsif R.Element.all < L.Element.all then
581 end Is_Equivalent_Node_Node;
583 -- Start of processing for Equivalent_Sets
586 return Is_Equivalent (Left.Tree, Right.Tree);
593 procedure Exclude (Container : in out Set; Item : Element_Type) is
594 X : Node_Access := Element_Keys.Find (Container.Tree, Item);
597 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
606 procedure Finalize (Object : in out Iterator) is
608 if Object.Container /= null then
609 Unbusy (Object.Container.Tree.TC);
617 function Find (Container : Set; Item : Element_Type) return Cursor is
618 Node : constant Node_Access := Element_Keys.Find (Container.Tree, Item);
623 return Cursor'(Container'Unrestricted_Access, Node);
631 function First (Container : Set) return Cursor is
634 (if Container.Tree.First = null then No_Element
635 else Cursor'(Container'Unrestricted_Access, Container.Tree.First));
638 function First (Object : Iterator) return Cursor is
640 -- The value of the iterator object's Node component influences the
641 -- behavior of the First (and Last) selector function.
643 -- When the Node component is null, this means the iterator object was
644 -- constructed without a start expression, in which case the (forward)
645 -- iteration starts from the (logical) beginning of the entire sequence
646 -- of items (corresponding to Container.First, for a forward iterator).
648 -- Otherwise, this is iteration over a partial sequence of items. When
649 -- the Node component is non-null, the iterator object was constructed
650 -- with a start expression, that specifies the position from which the
651 -- (forward) partial iteration begins.
653 if Object.Node = null then
654 return Object.Container.First;
656 return Cursor'(Object.Container, Object.Node);
664 function First_Element (Container : Set) return Element_Type is
666 if Checks and then Container.Tree.First = null then
667 raise Constraint_Error with "set is empty";
670 return Container.Tree.First.Element.all;
677 function Floor (Container : Set; Item : Element_Type) return Cursor is
678 Node : constant Node_Access := Element_Keys.Floor (Container.Tree, Item);
680 return (if Node = null then No_Element
681 else Cursor'(Container'Unrestricted_Access, Node));
688 procedure Free (X : in out Node_Access) is
689 procedure Deallocate is
690 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
702 Free_Element (X.Element);
717 package body Generic_Keys is
719 -----------------------
720 -- Local Subprograms --
721 -----------------------
723 function Is_Greater_Key_Node
725 Right : Node_Access) return Boolean;
726 pragma Inline (Is_Greater_Key_Node);
728 function Is_Less_Key_Node
730 Right : Node_Access) return Boolean;
731 pragma Inline (Is_Less_Key_Node);
733 --------------------------
734 -- Local Instantiations --
735 --------------------------
738 new Red_Black_Trees.Generic_Keys
739 (Tree_Operations => Tree_Operations,
740 Key_Type => Key_Type,
741 Is_Less_Key_Node => Is_Less_Key_Node,
742 Is_Greater_Key_Node => Is_Greater_Key_Node);
748 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
749 Node : constant Node_Access := Key_Keys.Ceiling (Container.Tree, Key);
751 return (if Node = null then No_Element
752 else Cursor'(Container'Unrestricted_Access, Node));
755 ------------------------
756 -- Constant_Reference --
757 ------------------------
759 function Constant_Reference
760 (Container : aliased Set;
761 Key : Key_Type) return Constant_Reference_Type
763 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
766 if Checks and then Node = null then
767 raise Constraint_Error with "Key not in set";
770 if Checks and then Node.Element = null then
771 raise Program_Error with "Node has no element";
775 Tree : Tree_Type renames Container'Unrestricted_Access.all.Tree;
776 TC : constant Tamper_Counts_Access :=
777 Tree.TC'Unrestricted_Access;
779 return R : constant Constant_Reference_Type :=
780 (Element => Node.Element.all'Access,
781 Control => (Controlled with TC))
786 end Constant_Reference;
792 function Contains (Container : Set; Key : Key_Type) return Boolean is
794 return Find (Container, Key) /= No_Element;
801 procedure Delete (Container : in out Set; Key : Key_Type) is
802 X : Node_Access := Key_Keys.Find (Container.Tree, Key);
805 if Checks and then X = null then
806 raise Constraint_Error with "attempt to delete key not in set";
809 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
817 function Element (Container : Set; Key : Key_Type) return Element_Type is
818 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
820 if Checks and then Node = null then
821 raise Constraint_Error with "key not in set";
824 return Node.Element.all;
827 ---------------------
828 -- Equivalent_Keys --
829 ---------------------
831 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
833 if Left < Right or else Right < Left then
844 procedure Exclude (Container : in out Set; Key : Key_Type) is
845 X : Node_Access := Key_Keys.Find (Container.Tree, Key);
848 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
857 procedure Finalize (Control : in out Reference_Control_Type) is
859 if Control.Container /= null then
860 Impl.Reference_Control_Type (Control).Finalize;
862 if Checks and then not (Key (Control.Pos) = Control.Old_Key.all)
864 Delete (Control.Container.all, Key (Control.Pos));
868 Control.Container := null;
869 Control.Old_Key := null;
877 function Find (Container : Set; Key : Key_Type) return Cursor is
878 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
880 return (if Node = null then No_Element
881 else Cursor'(Container'Unrestricted_Access, Node));
888 function Floor (Container : Set; Key : Key_Type) return Cursor is
889 Node : constant Node_Access := Key_Keys.Floor (Container.Tree, Key);
891 return (if Node = null then No_Element
892 else Cursor'(Container'Unrestricted_Access, Node));
895 -------------------------
896 -- Is_Greater_Key_Node --
897 -------------------------
899 function Is_Greater_Key_Node
901 Right : Node_Access) return Boolean
904 return Key (Right.Element.all) < Left;
905 end Is_Greater_Key_Node;
907 ----------------------
908 -- Is_Less_Key_Node --
909 ----------------------
911 function Is_Less_Key_Node
913 Right : Node_Access) return Boolean
916 return Left < Key (Right.Element.all);
917 end Is_Less_Key_Node;
923 function Key (Position : Cursor) return Key_Type is
925 if Checks and then Position.Node = null then
926 raise Constraint_Error with
927 "Position cursor equals No_Element";
930 if Checks and then Position.Node.Element = null then
931 raise Program_Error with
932 "Position cursor is bad";
935 pragma Assert (Vet (Position.Container.Tree, Position.Node),
936 "bad cursor in Key");
938 return Key (Position.Node.Element.all);
946 (Container : in out Set;
948 New_Item : Element_Type)
950 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
953 if Checks and then Node = null then
954 raise Constraint_Error with
955 "attempt to replace key not in set";
958 Replace_Element (Container.Tree, Node, New_Item);
966 (Stream : not null access Root_Stream_Type'Class;
967 Item : out Reference_Type)
970 raise Program_Error with "attempt to stream reference";
973 ------------------------------
974 -- Reference_Preserving_Key --
975 ------------------------------
977 function Reference_Preserving_Key
978 (Container : aliased in out Set;
979 Position : Cursor) return Reference_Type
982 if Checks and then Position.Container = null then
983 raise Constraint_Error with "Position cursor has no element";
986 if Checks and then Position.Container /= Container'Unrestricted_Access
988 raise Program_Error with
989 "Position cursor designates wrong container";
992 if Checks and then Position.Node.Element = null then
993 raise Program_Error with "Node has no element";
997 (Vet (Container.Tree, Position.Node),
998 "bad cursor in function Reference_Preserving_Key");
1001 Tree : Tree_Type renames Container.Tree;
1003 return R : constant Reference_Type :=
1004 (Element => Position.Node.Element.all'Unchecked_Access,
1007 Tree.TC'Unrestricted_Access,
1008 Container => Container'Access,
1010 Old_Key => new Key_Type'(Key (Position))))
1015 end Reference_Preserving_Key;
1017 function Reference_Preserving_Key
1018 (Container : aliased in out Set;
1019 Key : Key_Type) return Reference_Type
1021 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
1024 if Checks and then Node = null then
1025 raise Constraint_Error with "Key not in set";
1028 if Checks and then Node.Element = null then
1029 raise Program_Error with "Node has no element";
1033 Tree : Tree_Type renames Container.Tree;
1035 return R : constant Reference_Type :=
1036 (Element => Node.Element.all'Unchecked_Access,
1039 Tree.TC'Unrestricted_Access,
1040 Container => Container'Access,
1041 Pos => Find (Container, Key),
1042 Old_Key => new Key_Type'(Key)))
1047 end Reference_Preserving_Key;
1049 -----------------------------------
1050 -- Update_Element_Preserving_Key --
1051 -----------------------------------
1053 procedure Update_Element_Preserving_Key
1054 (Container : in out Set;
1056 Process : not null access
1057 procedure (Element : in out Element_Type))
1059 Tree : Tree_Type renames Container.Tree;
1062 if Checks and then Position.Node = null then
1063 raise Constraint_Error with "Position cursor equals No_Element";
1066 if Checks and then Position.Node.Element = null then
1067 raise Program_Error with "Position cursor is bad";
1070 if Checks and then Position.Container /= Container'Unrestricted_Access
1072 raise Program_Error with "Position cursor designates wrong set";
1075 pragma Assert (Vet (Container.Tree, Position.Node),
1076 "bad cursor in Update_Element_Preserving_Key");
1079 E : Element_Type renames Position.Node.Element.all;
1080 K : constant Key_Type := Key (E);
1081 Lock : With_Lock (Tree.TC'Unrestricted_Access);
1084 if Equivalent_Keys (K, Key (E)) then
1090 X : Node_Access := Position.Node;
1092 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
1096 raise Program_Error with "key was modified";
1097 end Update_Element_Preserving_Key;
1104 (Stream : not null access Root_Stream_Type'Class;
1105 Item : Reference_Type)
1108 raise Program_Error with "attempt to stream reference";
1113 ------------------------
1114 -- Get_Element_Access --
1115 ------------------------
1117 function Get_Element_Access
1118 (Position : Cursor) return not null Element_Access is
1120 return Position.Node.Element;
1121 end Get_Element_Access;
1127 function Has_Element (Position : Cursor) return Boolean is
1129 return Position /= No_Element;
1136 procedure Include (Container : in out Set; New_Item : Element_Type) is
1143 Insert (Container, New_Item, Position, Inserted);
1145 if not Inserted then
1146 TE_Check (Container.Tree.TC);
1149 -- The element allocator may need an accessibility check in the
1150 -- case the actual type is class-wide or has access discriminants
1151 -- (see RM 4.8(10.1) and AI12-0035).
1153 pragma Unsuppress (Accessibility_Check);
1156 X := Position.Node.Element;
1157 Position.Node.Element := new Element_Type'(New_Item);
1168 (Container : in out Set;
1169 New_Item : Element_Type;
1170 Position : out Cursor;
1171 Inserted : out Boolean)
1180 Position.Container := Container'Unrestricted_Access;
1183 procedure Insert (Container : in out Set; New_Item : Element_Type) is
1185 pragma Unreferenced (Position);
1190 Insert (Container, New_Item, Position, Inserted);
1192 if Checks and then not Inserted then
1193 raise Constraint_Error with
1194 "attempt to insert element already in set";
1198 ----------------------
1199 -- Insert_Sans_Hint --
1200 ----------------------
1202 procedure Insert_Sans_Hint
1203 (Tree : in out Tree_Type;
1204 New_Item : Element_Type;
1205 Node : out Node_Access;
1206 Inserted : out Boolean)
1208 function New_Node return Node_Access;
1209 pragma Inline (New_Node);
1211 procedure Insert_Post is
1212 new Element_Keys.Generic_Insert_Post (New_Node);
1214 procedure Conditional_Insert_Sans_Hint is
1215 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1221 function New_Node return Node_Access is
1222 -- The element allocator may need an accessibility check in the case
1223 -- the actual type is class-wide or has access discriminants (see
1224 -- RM 4.8(10.1) and AI12-0035).
1226 pragma Unsuppress (Accessibility_Check);
1228 Element : Element_Access := new Element_Type'(New_Item);
1231 return new Node_Type'(Parent => null,
1234 Color => Red_Black_Trees.Red,
1235 Element => Element);
1239 Free_Element (Element);
1243 -- Start of processing for Insert_Sans_Hint
1246 Conditional_Insert_Sans_Hint
1251 end Insert_Sans_Hint;
1253 ----------------------
1254 -- Insert_With_Hint --
1255 ----------------------
1257 procedure Insert_With_Hint
1258 (Dst_Tree : in out Tree_Type;
1259 Dst_Hint : Node_Access;
1260 Src_Node : Node_Access;
1261 Dst_Node : out Node_Access)
1264 pragma Unreferenced (Success);
1266 function New_Node return Node_Access;
1268 procedure Insert_Post is
1269 new Element_Keys.Generic_Insert_Post (New_Node);
1271 procedure Insert_Sans_Hint is
1272 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1274 procedure Insert_With_Hint is
1275 new Element_Keys.Generic_Conditional_Insert_With_Hint
1283 function New_Node return Node_Access is
1284 Element : Element_Access := new Element_Type'(Src_Node.Element.all);
1289 Node := new Node_Type;
1292 Free_Element (Element);
1296 Node.Element := Element;
1300 -- Start of processing for Insert_With_Hint
1306 Src_Node.Element.all,
1309 end Insert_With_Hint;
1315 procedure Intersection (Target : in out Set; Source : Set) is
1317 Set_Ops.Intersection (Target.Tree, Source.Tree);
1320 function Intersection (Left, Right : Set) return Set is
1321 Tree : constant Tree_Type :=
1322 Set_Ops.Intersection (Left.Tree, Right.Tree);
1324 return Set'(Controlled with Tree);
1331 function Is_Empty (Container : Set) return Boolean is
1333 return Container.Tree.Length = 0;
1336 -----------------------------
1337 -- Is_Greater_Element_Node --
1338 -----------------------------
1340 function Is_Greater_Element_Node
1341 (Left : Element_Type;
1342 Right : Node_Access) return Boolean
1345 -- e > node same as node < e
1347 return Right.Element.all < Left;
1348 end Is_Greater_Element_Node;
1350 --------------------------
1351 -- Is_Less_Element_Node --
1352 --------------------------
1354 function Is_Less_Element_Node
1355 (Left : Element_Type;
1356 Right : Node_Access) return Boolean
1359 return Left < Right.Element.all;
1360 end Is_Less_Element_Node;
1362 -----------------------
1363 -- Is_Less_Node_Node --
1364 -----------------------
1366 function Is_Less_Node_Node (L, R : Node_Access) return Boolean is
1368 return L.Element.all < R.Element.all;
1369 end Is_Less_Node_Node;
1375 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1377 return Set_Ops.Is_Subset (Subset => Subset.Tree, Of_Set => Of_Set.Tree);
1386 Process : not null access procedure (Position : Cursor))
1388 procedure Process_Node (Node : Node_Access);
1389 pragma Inline (Process_Node);
1391 procedure Local_Iterate is
1392 new Tree_Operations.Generic_Iteration (Process_Node);
1398 procedure Process_Node (Node : Node_Access) is
1400 Process (Cursor'(Container'Unrestricted_Access, Node));
1403 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
1404 Busy : With_Busy (T.TC'Unrestricted_Access);
1406 -- Start of processing for Iterate
1414 return Set_Iterator_Interfaces.Reversible_Iterator'class
1417 -- The value of the Node component influences the behavior of the First
1418 -- and Last selector functions of the iterator object. When the Node
1419 -- component is null (as is the case here), this means the iterator
1420 -- object was constructed without a start expression. This is a complete
1421 -- iterator, meaning that the iteration starts from the (logical)
1422 -- beginning of the sequence of items.
1424 -- Note: For a forward iterator, Container.First is the beginning, and
1425 -- for a reverse iterator, Container.Last is the beginning.
1427 return It : constant Iterator :=
1428 Iterator'(Limited_Controlled with
1429 Container => Container'Unrestricted_Access,
1432 Busy (Container.Tree.TC'Unrestricted_Access.all);
1439 return Set_Iterator_Interfaces.Reversible_Iterator'class
1442 -- It was formerly the case that when Start = No_Element, the partial
1443 -- iterator was defined to behave the same as for a complete iterator,
1444 -- and iterate over the entire sequence of items. However, those
1445 -- semantics were unintuitive and arguably error-prone (it is too easy
1446 -- to accidentally create an endless loop), and so they were changed,
1447 -- per the ARG meeting in Denver on 2011/11. However, there was no
1448 -- consensus about what positive meaning this corner case should have,
1449 -- and so it was decided to simply raise an exception. This does imply,
1450 -- however, that it is not possible to use a partial iterator to specify
1451 -- an empty sequence of items.
1453 if Checks and then Start = No_Element then
1454 raise Constraint_Error with
1455 "Start position for iterator equals No_Element";
1458 if Checks and then Start.Container /= Container'Unrestricted_Access then
1459 raise Program_Error with
1460 "Start cursor of Iterate designates wrong set";
1463 pragma Assert (Vet (Container.Tree, Start.Node),
1464 "Start cursor of Iterate is bad");
1466 -- The value of the Node component influences the behavior of the First
1467 -- and Last selector functions of the iterator object. When the Node
1468 -- component is non-null (as is the case here), it means that this is a
1469 -- partial iteration, over a subset of the complete sequence of
1470 -- items. The iterator object was constructed with a start expression,
1471 -- indicating the position from which the iteration begins. Note that
1472 -- the start position has the same value irrespective of whether this is
1473 -- a forward or reverse iteration.
1475 return It : constant Iterator :=
1476 (Limited_Controlled with
1477 Container => Container'Unrestricted_Access,
1480 Busy (Container.Tree.TC'Unrestricted_Access.all);
1488 function Last (Container : Set) return Cursor is
1491 (if Container.Tree.Last = null then No_Element
1492 else Cursor'(Container'Unrestricted_Access, Container.Tree.Last));
1495 function Last (Object : Iterator) return Cursor is
1497 -- The value of the iterator object's Node component influences the
1498 -- behavior of the Last (and First) selector function.
1500 -- When the Node component is null, this means the iterator object was
1501 -- constructed without a start expression, in which case the (reverse)
1502 -- iteration starts from the (logical) beginning of the entire sequence
1503 -- (corresponding to Container.Last, for a reverse iterator).
1505 -- Otherwise, this is iteration over a partial sequence of items. When
1506 -- the Node component is non-null, the iterator object was constructed
1507 -- with a start expression, that specifies the position from which the
1508 -- (reverse) partial iteration begins.
1510 if Object.Node = null then
1511 return Object.Container.Last;
1513 return Cursor'(Object.Container, Object.Node);
1521 function Last_Element (Container : Set) return Element_Type is
1523 if Checks and then Container.Tree.Last = null then
1524 raise Constraint_Error with "set is empty";
1527 return Container.Tree.Last.Element.all;
1534 function Left (Node : Node_Access) return Node_Access is
1543 function Length (Container : Set) return Count_Type is
1545 return Container.Tree.Length;
1552 procedure Move is new Tree_Operations.Generic_Move (Clear);
1554 procedure Move (Target : in out Set; Source : in out Set) is
1556 Move (Target => Target.Tree, Source => Source.Tree);
1563 procedure Next (Position : in out Cursor) is
1565 Position := Next (Position);
1568 function Next (Position : Cursor) return Cursor is
1570 if Position = No_Element then
1574 if Checks and then Position.Node.Element = null then
1575 raise Program_Error with "Position cursor is bad";
1578 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1579 "bad cursor in Next");
1582 Node : constant Node_Access := Tree_Operations.Next (Position.Node);
1584 return (if Node = null then No_Element
1585 else Cursor'(Position.Container, Node));
1591 Position : Cursor) return Cursor
1594 if Position.Container = null then
1598 if Checks and then Position.Container /= Object.Container then
1599 raise Program_Error with
1600 "Position cursor of Next designates wrong set";
1603 return Next (Position);
1610 function Overlap (Left, Right : Set) return Boolean is
1612 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1619 function Parent (Node : Node_Access) return Node_Access is
1628 procedure Previous (Position : in out Cursor) is
1630 Position := Previous (Position);
1633 function Previous (Position : Cursor) return Cursor is
1635 if Position = No_Element then
1639 if Checks and then Position.Node.Element = null then
1640 raise Program_Error with "Position cursor is bad";
1643 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1644 "bad cursor in Previous");
1647 Node : constant Node_Access :=
1648 Tree_Operations.Previous (Position.Node);
1650 return (if Node = null then No_Element
1651 else Cursor'(Position.Container, Node));
1657 Position : Cursor) return Cursor
1660 if Position.Container = null then
1664 if Checks and then Position.Container /= Object.Container then
1665 raise Program_Error with
1666 "Position cursor of Previous designates wrong set";
1669 return Previous (Position);
1672 ----------------------
1673 -- Pseudo_Reference --
1674 ----------------------
1676 function Pseudo_Reference
1677 (Container : aliased Set'Class) return Reference_Control_Type
1679 TC : constant Tamper_Counts_Access :=
1680 Container.Tree.TC'Unrestricted_Access;
1682 return R : constant Reference_Control_Type := (Controlled with TC) do
1685 end Pseudo_Reference;
1691 procedure Query_Element
1693 Process : not null access procedure (Element : Element_Type))
1696 if Checks and then Position.Node = null then
1697 raise Constraint_Error with "Position cursor equals No_Element";
1700 if Checks and then Position.Node.Element = null then
1701 raise Program_Error with "Position cursor is bad";
1704 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1705 "bad cursor in Query_Element");
1708 T : Tree_Type renames Position.Container.Tree;
1709 Lock : With_Lock (T.TC'Unrestricted_Access);
1711 Process (Position.Node.Element.all);
1720 (Stream : not null access Root_Stream_Type'Class;
1721 Container : out Set)
1724 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1725 pragma Inline (Read_Node);
1728 new Tree_Operations.Generic_Read (Clear, Read_Node);
1735 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1737 Node : Node_Access := new Node_Type;
1740 Node.Element := new Element_Type'(Element_Type'Input (Stream));
1745 Free (Node); -- Note that Free deallocates elem too
1749 -- Start of processing for Read
1752 Read (Stream, Container.Tree);
1756 (Stream : not null access Root_Stream_Type'Class;
1760 raise Program_Error with "attempt to stream set cursor";
1764 (Stream : not null access Root_Stream_Type'Class;
1765 Item : out Constant_Reference_Type)
1768 raise Program_Error with "attempt to stream reference";
1775 procedure Replace (Container : in out Set; New_Item : Element_Type) is
1776 Node : constant Node_Access :=
1777 Element_Keys.Find (Container.Tree, New_Item);
1780 pragma Warnings (Off, X);
1783 if Checks and then Node = null then
1784 raise Constraint_Error with "attempt to replace element not in set";
1787 TE_Check (Container.Tree.TC);
1790 -- The element allocator may need an accessibility check in the case
1791 -- the actual type is class-wide or has access discriminants (see
1792 -- RM 4.8(10.1) and AI12-0035).
1794 pragma Unsuppress (Accessibility_Check);
1798 Node.Element := new Element_Type'(New_Item);
1803 ---------------------
1804 -- Replace_Element --
1805 ---------------------
1807 procedure Replace_Element
1808 (Tree : in out Tree_Type;
1810 Item : Element_Type)
1812 pragma Assert (Node /= null);
1813 pragma Assert (Node.Element /= null);
1815 function New_Node return Node_Access;
1816 pragma Inline (New_Node);
1818 procedure Local_Insert_Post is
1819 new Element_Keys.Generic_Insert_Post (New_Node);
1821 procedure Local_Insert_Sans_Hint is
1822 new Element_Keys.Generic_Conditional_Insert (Local_Insert_Post);
1824 procedure Local_Insert_With_Hint is
1825 new Element_Keys.Generic_Conditional_Insert_With_Hint
1827 Local_Insert_Sans_Hint);
1833 function New_Node return Node_Access is
1835 -- The element allocator may need an accessibility check in the case
1836 -- the actual type is class-wide or has access discriminants (see
1837 -- RM 4.8(10.1) and AI12-0035).
1839 pragma Unsuppress (Accessibility_Check);
1842 Node.Element := new Element_Type'(Item); -- OK if fails
1844 Node.Parent := null;
1851 Result : Node_Access;
1855 X : Element_Access := Node.Element;
1857 -- Start of processing for Replace_Element
1860 -- Replace_Element assigns value Item to the element designated by Node,
1861 -- per certain semantic constraints, described as follows.
1863 -- If Item is equivalent to the element, then element is replaced and
1864 -- there's nothing else to do. This is the easy case.
1866 -- If Item is not equivalent, then the node will (possibly) have to move
1867 -- to some other place in the tree. This is slighly more complicated,
1868 -- because we must ensure that Item is not equivalent to some other
1869 -- element in the tree (in which case, the replacement is not allowed).
1871 -- Determine whether Item is equivalent to element on the specified
1875 Lock : With_Lock (Tree.TC'Unrestricted_Access);
1877 Compare := (if Item < Node.Element.all then False
1878 elsif Node.Element.all < Item then False
1883 -- Item is equivalent to the node's element, so we will not have to
1889 -- The element allocator may need an accessibility check in the
1890 -- case the actual type is class-wide or has access discriminants
1891 -- (see RM 4.8(10.1) and AI12-0035).
1893 pragma Unsuppress (Accessibility_Check);
1896 Node.Element := new Element_Type'(Item);
1903 -- The replacement Item is not equivalent to the element on the
1904 -- specified node, which means that it will need to be re-inserted in a
1905 -- different position in the tree. We must now determine whether Item is
1906 -- equivalent to some other element in the tree (which would prohibit
1907 -- the assignment and hence the move).
1909 -- Ceiling returns the smallest element equivalent or greater than the
1910 -- specified Item; if there is no such element, then it returns null.
1912 Hint := Element_Keys.Ceiling (Tree, Item);
1914 if Hint /= null then
1916 Lock : With_Lock (Tree.TC'Unrestricted_Access);
1918 Compare := Item < Hint.Element.all;
1921 -- Item >= Hint.Element
1923 if Checks and then not Compare then
1925 -- Ceiling returns an element that is equivalent or greater
1926 -- than Item. If Item is "not less than" the element, then
1927 -- by elimination we know that Item is equivalent to the element.
1929 -- But this means that it is not possible to assign the value of
1930 -- Item to the specified element (on Node), because a different
1931 -- element (on Hint) equivalent to Item already exsits. (Were we
1932 -- to change Node's element value, we would have to move Node, but
1933 -- we would be unable to move the Node, because its new position
1934 -- in the tree is already occupied by an equivalent element.)
1936 raise Program_Error with "attempt to replace existing element";
1939 -- Item is not equivalent to any other element in the tree, so it is
1940 -- safe to assign the value of Item to Node.Element. This means that
1941 -- the node will have to move to a different position in the tree
1942 -- (because its element will have a different value).
1944 -- The nearest (greater) neighbor of Item is Hint. This will be the
1945 -- insertion position of Node (because its element will have Item as
1948 -- If Node equals Hint, the relative position of Node does not
1949 -- change. This allows us to perform an optimization: we need not
1950 -- remove Node from the tree and then reinsert it with its new value,
1951 -- because it would only be placed in the exact same position.
1957 -- The element allocator may need an accessibility check in the
1958 -- case actual type is class-wide or has access discriminants
1959 -- (see RM 4.8(10.1) and AI12-0035).
1961 pragma Unsuppress (Accessibility_Check);
1964 Node.Element := new Element_Type'(Item);
1972 -- If we get here, it is because Item was greater than all elements in
1973 -- the tree (Hint = null), or because Item was less than some element at
1974 -- a different place in the tree (Item < Hint.Element.all). In either
1975 -- case, we remove Node from the tree (without actually deallocating
1976 -- it), and then insert Item into the tree, onto the same Node (so no
1977 -- new node is actually allocated).
1979 Tree_Operations.Delete_Node_Sans_Free (Tree, Node); -- Checks busy-bit
1981 Local_Insert_With_Hint
1986 Inserted => Inserted);
1988 pragma Assert (Inserted);
1989 pragma Assert (Result = Node);
1992 end Replace_Element;
1994 procedure Replace_Element
1995 (Container : in out Set;
1997 New_Item : Element_Type)
2000 if Checks and then Position.Node = null then
2001 raise Constraint_Error with "Position cursor equals No_Element";
2004 if Checks and then Position.Node.Element = null then
2005 raise Program_Error with "Position cursor is bad";
2008 if Checks and then Position.Container /= Container'Unrestricted_Access
2010 raise Program_Error with "Position cursor designates wrong set";
2013 pragma Assert (Vet (Container.Tree, Position.Node),
2014 "bad cursor in Replace_Element");
2016 Replace_Element (Container.Tree, Position.Node, New_Item);
2017 end Replace_Element;
2019 ---------------------
2020 -- Reverse_Iterate --
2021 ---------------------
2023 procedure Reverse_Iterate
2025 Process : not null access procedure (Position : Cursor))
2027 procedure Process_Node (Node : Node_Access);
2028 pragma Inline (Process_Node);
2030 procedure Local_Reverse_Iterate is
2031 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
2037 procedure Process_Node (Node : Node_Access) is
2039 Process (Cursor'(Container'Unrestricted_Access, Node));
2042 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
2043 Busy : With_Busy (T.TC'Unrestricted_Access);
2045 -- Start of processing for Reverse_Iterate
2048 Local_Reverse_Iterate (T);
2049 end Reverse_Iterate;
2055 function Right (Node : Node_Access) return Node_Access is
2064 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
2066 Node.Color := Color;
2073 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
2082 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
2084 Node.Parent := Parent;
2091 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
2093 Node.Right := Right;
2096 --------------------------
2097 -- Symmetric_Difference --
2098 --------------------------
2100 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
2102 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
2103 end Symmetric_Difference;
2105 function Symmetric_Difference (Left, Right : Set) return Set is
2106 Tree : constant Tree_Type :=
2107 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
2109 return Set'(Controlled with Tree);
2110 end Symmetric_Difference;
2116 function To_Set (New_Item : Element_Type) return Set is
2120 pragma Unreferenced (Node, Inserted);
2122 Insert_Sans_Hint (Tree, New_Item, Node, Inserted);
2123 return Set'(Controlled with Tree);
2130 procedure Union (Target : in out Set; Source : Set) is
2132 Set_Ops.Union (Target.Tree, Source.Tree);
2135 function Union (Left, Right : Set) return Set is
2136 Tree : constant Tree_Type := Set_Ops.Union (Left.Tree, Right.Tree);
2138 return Set'(Controlled with Tree);
2146 (Stream : not null access Root_Stream_Type'Class;
2149 procedure Write_Node
2150 (Stream : not null access Root_Stream_Type'Class;
2151 Node : Node_Access);
2152 pragma Inline (Write_Node);
2155 new Tree_Operations.Generic_Write (Write_Node);
2161 procedure Write_Node
2162 (Stream : not null access Root_Stream_Type'Class;
2166 Element_Type'Output (Stream, Node.Element.all);
2169 -- Start of processing for Write
2172 Write (Stream, Container.Tree);
2176 (Stream : not null access Root_Stream_Type'Class;
2180 raise Program_Error with "attempt to stream set cursor";
2184 (Stream : not null access Root_Stream_Type'Class;
2185 Item : Constant_Reference_Type)
2188 raise Program_Error with "attempt to stream reference";
2191 end Ada.Containers.Indefinite_Ordered_Sets;