1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.INDEFINITE_ORDERED_MULTISETS --
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
.Unchecked_Deallocation
;
32 with Ada
.Containers
.Red_Black_Trees
.Generic_Operations
;
33 pragma Elaborate_All
(Ada
.Containers
.Red_Black_Trees
.Generic_Operations
);
35 with Ada
.Containers
.Red_Black_Trees
.Generic_Keys
;
36 pragma Elaborate_All
(Ada
.Containers
.Red_Black_Trees
.Generic_Keys
);
38 with Ada
.Containers
.Red_Black_Trees
.Generic_Set_Operations
;
39 pragma Elaborate_All
(Ada
.Containers
.Red_Black_Trees
.Generic_Set_Operations
);
41 with System
; use type System
.Address
;
43 package body Ada
.Containers
.Indefinite_Ordered_Multisets
is
45 pragma Annotate
(CodePeer
, Skip_Analysis
);
47 -----------------------------
48 -- Node Access Subprograms --
49 -----------------------------
51 -- These subprograms provide a functional interface to access fields
52 -- of a node, and a procedural interface for modifying these values.
54 function Color
(Node
: Node_Access
) return Color_Type
;
55 pragma Inline
(Color
);
57 function Left
(Node
: Node_Access
) return Node_Access
;
60 function Parent
(Node
: Node_Access
) return Node_Access
;
61 pragma Inline
(Parent
);
63 function Right
(Node
: Node_Access
) return Node_Access
;
64 pragma Inline
(Right
);
66 procedure Set_Parent
(Node
: Node_Access
; Parent
: Node_Access
);
67 pragma Inline
(Set_Parent
);
69 procedure Set_Left
(Node
: Node_Access
; Left
: Node_Access
);
70 pragma Inline
(Set_Left
);
72 procedure Set_Right
(Node
: Node_Access
; Right
: Node_Access
);
73 pragma Inline
(Set_Right
);
75 procedure Set_Color
(Node
: Node_Access
; Color
: Color_Type
);
76 pragma Inline
(Set_Color
);
78 -----------------------
79 -- Local Subprograms --
80 -----------------------
82 function Copy_Node
(Source
: Node_Access
) return Node_Access
;
83 pragma Inline
(Copy_Node
);
85 procedure Free
(X
: in out Node_Access
);
87 procedure Insert_Sans_Hint
88 (Tree
: in out Tree_Type
;
89 New_Item
: Element_Type
;
90 Node
: out Node_Access
);
92 procedure Insert_With_Hint
93 (Dst_Tree
: in out Tree_Type
;
94 Dst_Hint
: Node_Access
;
95 Src_Node
: Node_Access
;
96 Dst_Node
: out Node_Access
);
98 function Is_Equal_Node_Node
(L
, R
: Node_Access
) return Boolean;
99 pragma Inline
(Is_Equal_Node_Node
);
101 function Is_Greater_Element_Node
102 (Left
: Element_Type
;
103 Right
: Node_Access
) return Boolean;
104 pragma Inline
(Is_Greater_Element_Node
);
106 function Is_Less_Element_Node
107 (Left
: Element_Type
;
108 Right
: Node_Access
) return Boolean;
109 pragma Inline
(Is_Less_Element_Node
);
111 function Is_Less_Node_Node
(L
, R
: Node_Access
) return Boolean;
112 pragma Inline
(Is_Less_Node_Node
);
114 procedure Replace_Element
115 (Tree
: in out Tree_Type
;
117 Item
: Element_Type
);
119 --------------------------
120 -- Local Instantiations --
121 --------------------------
123 package Tree_Operations
is
124 new Red_Black_Trees
.Generic_Operations
(Tree_Types
);
126 procedure Delete_Tree
is
127 new Tree_Operations
.Generic_Delete_Tree
(Free
);
129 function Copy_Tree
is
130 new Tree_Operations
.Generic_Copy_Tree
(Copy_Node
, Delete_Tree
);
134 procedure Free_Element
is
135 new Ada
.Unchecked_Deallocation
(Element_Type
, Element_Access
);
138 new Tree_Operations
.Generic_Equal
(Is_Equal_Node_Node
);
141 new Generic_Set_Operations
142 (Tree_Operations
=> Tree_Operations
,
143 Insert_With_Hint
=> Insert_With_Hint
,
144 Copy_Tree
=> Copy_Tree
,
145 Delete_Tree
=> Delete_Tree
,
146 Is_Less
=> Is_Less_Node_Node
,
149 package Element_Keys
is
150 new Red_Black_Trees
.Generic_Keys
151 (Tree_Operations
=> Tree_Operations
,
152 Key_Type
=> Element_Type
,
153 Is_Less_Key_Node
=> Is_Less_Element_Node
,
154 Is_Greater_Key_Node
=> Is_Greater_Element_Node
);
160 function "<" (Left
, Right
: Cursor
) return Boolean is
162 if Left
.Node
= null then
163 raise Constraint_Error
with "Left cursor equals No_Element";
166 if Right
.Node
= null then
167 raise Constraint_Error
with "Right cursor equals No_Element";
170 if Left
.Node
.Element
= null then
171 raise Program_Error
with "Left cursor is bad";
174 if Right
.Node
.Element
= null then
175 raise Program_Error
with "Right cursor is bad";
178 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
179 "bad Left cursor in ""<""");
181 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
182 "bad Right cursor in ""<""");
184 return Left
.Node
.Element
.all < Right
.Node
.Element
.all;
187 function "<" (Left
: Cursor
; Right
: Element_Type
) return Boolean is
189 if Left
.Node
= null then
190 raise Constraint_Error
with "Left cursor equals No_Element";
193 if Left
.Node
.Element
= null then
194 raise Program_Error
with "Left cursor is bad";
197 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
198 "bad Left cursor in ""<""");
200 return Left
.Node
.Element
.all < Right
;
203 function "<" (Left
: Element_Type
; Right
: Cursor
) return Boolean is
205 if Right
.Node
= null then
206 raise Constraint_Error
with "Right cursor equals No_Element";
209 if Right
.Node
.Element
= null then
210 raise Program_Error
with "Right cursor is bad";
213 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
214 "bad Right cursor in ""<""");
216 return Left
< Right
.Node
.Element
.all;
223 function "=" (Left
, Right
: Set
) return Boolean is
225 return Is_Equal
(Left
.Tree
, Right
.Tree
);
232 function ">" (Left
, Right
: Cursor
) return Boolean is
234 if Left
.Node
= null then
235 raise Constraint_Error
with "Left cursor equals No_Element";
238 if Right
.Node
= null then
239 raise Constraint_Error
with "Right cursor equals No_Element";
242 if Left
.Node
.Element
= null then
243 raise Program_Error
with "Left cursor is bad";
246 if Right
.Node
.Element
= null then
247 raise Program_Error
with "Right cursor is bad";
250 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
251 "bad Left cursor in "">""");
253 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
254 "bad Right cursor in "">""");
256 -- L > R same as R < L
258 return Right
.Node
.Element
.all < Left
.Node
.Element
.all;
261 function ">" (Left
: Cursor
; Right
: Element_Type
) return Boolean is
263 if Left
.Node
= null then
264 raise Constraint_Error
with "Left cursor equals No_Element";
267 if Left
.Node
.Element
= null then
268 raise Program_Error
with "Left cursor is bad";
271 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
272 "bad Left cursor in "">""");
274 return Right
< Left
.Node
.Element
.all;
277 function ">" (Left
: Element_Type
; Right
: Cursor
) return Boolean is
279 if Right
.Node
= null then
280 raise Constraint_Error
with "Right cursor equals No_Element";
283 if Right
.Node
.Element
= null then
284 raise Program_Error
with "Right cursor is bad";
287 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
288 "bad Right cursor in "">""");
290 return Right
.Node
.Element
.all < Left
;
298 new Tree_Operations
.Generic_Adjust
(Copy_Tree
);
300 procedure Adjust
(Container
: in out Set
) is
302 Adjust
(Container
.Tree
);
309 procedure Assign
(Target
: in out Set
; Source
: Set
) is
311 if Target
'Address = Source
'Address then
316 Target
.Union
(Source
);
323 function Ceiling
(Container
: Set
; Item
: Element_Type
) return Cursor
is
324 Node
: constant Node_Access
:=
325 Element_Keys
.Ceiling
(Container
.Tree
, Item
);
332 return Cursor
'(Container'Unrestricted_Access, Node);
340 new Tree_Operations.Generic_Clear (Delete_Tree);
342 procedure Clear (Container : in out Set) is
344 Clear (Container.Tree);
351 function Color (Node : Node_Access) return Color_Type is
356 ------------------------
357 -- Constant_Reference --
358 ------------------------
360 function Constant_Reference
361 (Container : aliased Set;
362 Position : Cursor) return Constant_Reference_Type
365 if Position.Container = null then
366 raise Constraint_Error with "Position cursor has no element";
369 if Position.Container /= Container'Unrestricted_Access then
370 raise Program_Error with
371 "Position cursor designates wrong container";
374 pragma Assert (Vet (Position.Container.Tree, Position.Node),
375 "bad cursor in Constant_Reference");
377 -- Note: in predefined container units, the creation of a reference
378 -- increments the busy bit of the container, and its finalization
379 -- decrements it. In the absence of control machinery, this tampering
380 -- protection is missing.
383 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
384 pragma Unreferenced (T);
386 return R : constant Constant_Reference_Type :=
387 (Element => Position.Node.Element,
388 Control => (Container => Container'Unrestricted_Access))
393 end Constant_Reference;
399 function Contains (Container : Set; Item : Element_Type) return Boolean is
401 return Find (Container, Item) /= No_Element;
408 function Copy (Source : Set) return Set is
410 return Target : Set do
411 Target.Assign (Source);
419 function Copy_Node (Source : Node_Access) return Node_Access is
420 X : Element_Access := new Element_Type'(Source
.Element
.all);
423 return new Node_Type
'(Parent => null,
426 Color => Source.Color,
439 procedure Delete (Container : in out Set; Item : Element_Type) is
440 Tree : Tree_Type renames Container.Tree;
441 Node : Node_Access := Element_Keys.Ceiling (Tree, Item);
442 Done : constant Node_Access := Element_Keys.Upper_Bound (Tree, Item);
447 raise Constraint_Error with "attempt to delete element not in set";
452 Node := Tree_Operations.Next (Node);
453 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
456 exit when Node = Done;
460 procedure Delete (Container : in out Set; Position : in out Cursor) is
462 if Position.Node = null then
463 raise Constraint_Error with "Position cursor equals No_Element";
466 if Position.Node.Element = null then
467 raise Program_Error with "Position cursor is bad";
470 if Position.Container /= Container'Unrestricted_Access then
471 raise Program_Error with "Position cursor designates wrong set";
474 pragma Assert (Vet (Container.Tree, Position.Node),
475 "bad cursor in Delete");
477 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
478 Free (Position.Node);
480 Position.Container := null;
487 procedure Delete_First (Container : in out Set) is
488 Tree : Tree_Type renames Container.Tree;
489 X : Node_Access := Tree.First;
496 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
504 procedure Delete_Last (Container : in out Set) is
505 Tree : Tree_Type renames Container.Tree;
506 X : Node_Access := Tree.Last;
513 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
521 procedure Difference (Target : in out Set; Source : Set) is
523 Set_Ops.Difference (Target.Tree, Source.Tree);
526 function Difference (Left, Right : Set) return Set is
527 Tree : constant Tree_Type := Set_Ops.Difference (Left.Tree, Right.Tree);
529 return Set'(Controlled
with Tree
);
536 function Element
(Position
: Cursor
) return Element_Type
is
538 if Position
.Node
= null then
539 raise Constraint_Error
with "Position cursor equals No_Element";
542 if Position
.Node
.Element
= null then
543 raise Program_Error
with "Position cursor is bad";
546 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
547 "bad cursor in Element");
549 return Position
.Node
.Element
.all;
552 -------------------------
553 -- Equivalent_Elements --
554 -------------------------
556 function Equivalent_Elements
(Left
, Right
: Element_Type
) return Boolean is
565 end Equivalent_Elements
;
567 ---------------------
568 -- Equivalent_Sets --
569 ---------------------
571 function Equivalent_Sets
(Left
, Right
: Set
) return Boolean is
573 function Is_Equivalent_Node_Node
(L
, R
: Node_Access
) return Boolean;
574 pragma Inline
(Is_Equivalent_Node_Node
);
576 function Is_Equivalent
is
577 new Tree_Operations
.Generic_Equal
(Is_Equivalent_Node_Node
);
579 -----------------------------
580 -- Is_Equivalent_Node_Node --
581 -----------------------------
583 function Is_Equivalent_Node_Node
(L
, R
: Node_Access
) return Boolean is
585 if L
.Element
.all < R
.Element
.all then
587 elsif R
.Element
.all < L
.Element
.all then
592 end Is_Equivalent_Node_Node
;
594 -- Start of processing for Equivalent_Sets
597 return Is_Equivalent
(Left
.Tree
, Right
.Tree
);
604 procedure Exclude
(Container
: in out Set
; Item
: Element_Type
) is
605 Tree
: Tree_Type
renames Container
.Tree
;
606 Node
: Node_Access
:= Element_Keys
.Ceiling
(Tree
, Item
);
607 Done
: constant Node_Access
:= Element_Keys
.Upper_Bound
(Tree
, Item
);
611 while Node
/= Done
loop
613 Node
:= Tree_Operations
.Next
(Node
);
614 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, X
);
623 function Find
(Container
: Set
; Item
: Element_Type
) return Cursor
is
624 Node
: constant Node_Access
:= Element_Keys
.Find
(Container
.Tree
, Item
);
631 return Cursor
'(Container'Unrestricted_Access, Node);
638 procedure Finalize (Object : in out Iterator) is
639 B : Natural renames Object.Container.Tree.Busy;
640 pragma Assert (B > 0);
649 function First (Container : Set) return Cursor is
651 if Container.Tree.First = null then
655 return Cursor'(Container
'Unrestricted_Access, Container
.Tree
.First
);
658 function First
(Object
: Iterator
) return Cursor
is
660 -- The value of the iterator object's Node component influences the
661 -- behavior of the First (and Last) selector function.
663 -- When the Node component is null, this means the iterator object was
664 -- constructed without a start expression, in which case the (forward)
665 -- iteration starts from the (logical) beginning of the entire sequence
666 -- of items (corresponding to Container.First, for a forward iterator).
668 -- Otherwise, this is iteration over a partial sequence of items. When
669 -- the Node component is non-null, the iterator object was constructed
670 -- with a start expression, that specifies the position from which the
671 -- (forward) partial iteration begins.
673 if Object
.Node
= null then
674 return Object
.Container
.First
;
676 return Cursor
'(Object.Container, Object.Node);
684 function First_Element (Container : Set) return Element_Type is
686 if Container.Tree.First = null then
687 raise Constraint_Error with "set is empty";
690 pragma Assert (Container.Tree.First.Element /= null);
691 return Container.Tree.First.Element.all;
698 function Floor (Container : Set; Item : Element_Type) return Cursor is
699 Node : constant Node_Access := Element_Keys.Floor (Container.Tree, Item);
706 return Cursor'(Container
'Unrestricted_Access, Node
);
713 procedure Free
(X
: in out Node_Access
) is
714 procedure Deallocate
is
715 new Ada
.Unchecked_Deallocation
(Node_Type
, Node_Access
);
727 Free_Element
(X
.Element
);
742 package body Generic_Keys
is
744 -----------------------
745 -- Local Subprograms --
746 -----------------------
748 function Is_Less_Key_Node
750 Right
: Node_Access
) return Boolean;
751 pragma Inline
(Is_Less_Key_Node
);
753 function Is_Greater_Key_Node
755 Right
: Node_Access
) return Boolean;
756 pragma Inline
(Is_Greater_Key_Node
);
758 --------------------------
759 -- Local Instantiations --
760 --------------------------
763 new Red_Black_Trees
.Generic_Keys
764 (Tree_Operations
=> Tree_Operations
,
765 Key_Type
=> Key_Type
,
766 Is_Less_Key_Node
=> Is_Less_Key_Node
,
767 Is_Greater_Key_Node
=> Is_Greater_Key_Node
);
773 function Ceiling
(Container
: Set
; Key
: Key_Type
) return Cursor
is
774 Node
: constant Node_Access
:= Key_Keys
.Ceiling
(Container
.Tree
, Key
);
781 return Cursor
'(Container'Unrestricted_Access, Node);
788 function Contains (Container : Set; Key : Key_Type) return Boolean is
790 return Find (Container, Key) /= No_Element;
797 procedure Delete (Container : in out Set; Key : Key_Type) is
798 Tree : Tree_Type renames Container.Tree;
799 Node : Node_Access := Key_Keys.Ceiling (Tree, Key);
800 Done : constant Node_Access := Key_Keys.Upper_Bound (Tree, Key);
805 raise Constraint_Error with "attempt to delete key not in set";
810 Node := Tree_Operations.Next (Node);
811 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
814 exit when Node = Done;
822 function Element (Container : Set; Key : Key_Type) return Element_Type is
823 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
827 raise Constraint_Error with "key not in set";
830 return Node.Element.all;
833 ---------------------
834 -- Equivalent_Keys --
835 ---------------------
837 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
852 procedure Exclude (Container : in out Set; Key : Key_Type) is
853 Tree : Tree_Type renames Container.Tree;
854 Node : Node_Access := Key_Keys.Ceiling (Tree, Key);
855 Done : constant Node_Access := Key_Keys.Upper_Bound (Tree, Key);
859 while Node /= Done loop
861 Node := Tree_Operations.Next (Node);
862 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
871 function Find (Container : Set; Key : Key_Type) return Cursor is
872 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
879 return Cursor'(Container
'Unrestricted_Access, Node
);
886 function Floor
(Container
: Set
; Key
: Key_Type
) return Cursor
is
887 Node
: constant Node_Access
:= Key_Keys
.Floor
(Container
.Tree
, Key
);
894 return Cursor
'(Container'Unrestricted_Access, Node);
897 -------------------------
898 -- Is_Greater_Key_Node --
899 -------------------------
901 function Is_Greater_Key_Node
903 Right : Node_Access) return Boolean
906 return Key (Right.Element.all) < Left;
907 end Is_Greater_Key_Node;
909 ----------------------
910 -- Is_Less_Key_Node --
911 ----------------------
913 function Is_Less_Key_Node
915 Right : Node_Access) return Boolean
918 return Left < Key (Right.Element.all);
919 end Is_Less_Key_Node;
928 Process : not null access procedure (Position : Cursor))
930 procedure Process_Node (Node : Node_Access);
931 pragma Inline (Process_Node);
933 procedure Local_Iterate is
934 new Key_Keys.Generic_Iteration (Process_Node);
940 procedure Process_Node (Node : Node_Access) is
942 Process (Cursor'(Container
'Unrestricted_Access, Node
));
945 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
946 B
: Natural renames T
.Busy
;
948 -- Start of processing for Iterate
954 Local_Iterate
(T
, Key
);
968 function Key
(Position
: Cursor
) return Key_Type
is
970 if Position
.Node
= null then
971 raise Constraint_Error
with
972 "Position cursor equals No_Element";
975 if Position
.Node
.Element
= null then
976 raise Program_Error
with
977 "Position cursor is bad";
980 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
981 "bad cursor in Key");
983 return Key
(Position
.Node
.Element
.all);
986 ---------------------
987 -- Reverse_Iterate --
988 ---------------------
990 procedure Reverse_Iterate
993 Process
: not null access procedure (Position
: Cursor
))
995 procedure Process_Node
(Node
: Node_Access
);
996 pragma Inline
(Process_Node
);
1002 procedure Local_Reverse_Iterate
is
1003 new Key_Keys
.Generic_Reverse_Iteration
(Process_Node
);
1009 procedure Process_Node
(Node
: Node_Access
) is
1011 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1014 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1015 B : Natural renames T.Busy;
1017 -- Start of processing for Reverse_Iterate
1023 Local_Reverse_Iterate (T, Key);
1031 end Reverse_Iterate;
1033 --------------------
1034 -- Update_Element --
1035 --------------------
1037 procedure Update_Element
1038 (Container : in out Set;
1040 Process : not null access procedure (Element : in out Element_Type))
1042 Tree : Tree_Type renames Container.Tree;
1043 Node : constant Node_Access := Position.Node;
1047 raise Constraint_Error with "Position cursor equals No_Element";
1050 if Node.Element = null then
1051 raise Program_Error with "Position cursor is bad";
1054 if Position.Container /= Container'Unrestricted_Access then
1055 raise Program_Error with "Position cursor designates wrong set";
1058 pragma Assert (Vet (Tree, Node),
1059 "bad cursor in Update_Element");
1062 E : Element_Type renames Node.Element.all;
1063 K : constant Key_Type := Key (E);
1065 B : Natural renames Tree.Busy;
1066 L : Natural renames Tree.Lock;
1084 if Equivalent_Keys (Left => K, Right => Key (E)) then
1089 -- Delete_Node checks busy-bit
1091 Tree_Operations.Delete_Node_Sans_Free (Tree, Node);
1093 Insert_New_Item : declare
1094 function New_Node return Node_Access;
1095 pragma Inline (New_Node);
1097 procedure Insert_Post is
1098 new Element_Keys.Generic_Insert_Post (New_Node);
1100 procedure Unconditional_Insert is
1101 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1107 function New_Node return Node_Access is
1109 Node.Color := Red_Black_Trees.Red;
1110 Node.Parent := null;
1117 Result : Node_Access;
1119 -- Start of processing for Insert_New_Item
1122 Unconditional_Insert
1124 Key => Node.Element.all,
1127 pragma Assert (Result = Node);
1128 end Insert_New_Item;
1137 function Has_Element (Position : Cursor) return Boolean is
1139 return Position /= No_Element;
1146 procedure Insert (Container : in out Set; New_Item : Element_Type) is
1148 pragma Unreferenced (Position);
1150 Insert (Container, New_Item, Position);
1154 (Container : in out Set;
1155 New_Item : Element_Type;
1156 Position : out Cursor)
1159 Insert_Sans_Hint (Container.Tree, New_Item, Position.Node);
1160 Position.Container := Container'Unrestricted_Access;
1163 ----------------------
1164 -- Insert_Sans_Hint --
1165 ----------------------
1167 procedure Insert_Sans_Hint
1168 (Tree : in out Tree_Type;
1169 New_Item : Element_Type;
1170 Node : out Node_Access)
1172 function New_Node return Node_Access;
1173 pragma Inline (New_Node);
1175 procedure Insert_Post is
1176 new Element_Keys.Generic_Insert_Post (New_Node);
1178 procedure Unconditional_Insert is
1179 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1185 function New_Node return Node_Access is
1186 -- The element allocator may need an accessibility check in the case
1187 -- the actual type is class-wide or has access discriminants (see
1188 -- RM 4.8(10.1) and AI12-0035).
1190 pragma Unsuppress (Accessibility_Check);
1192 Element : Element_Access := new Element_Type'(New_Item
);
1195 return new Node_Type
'(Parent => null,
1198 Color => Red_Black_Trees.Red,
1199 Element => Element);
1203 Free_Element (Element);
1207 -- Start of processing for Insert_Sans_Hint
1210 Unconditional_Insert (Tree, New_Item, Node);
1211 end Insert_Sans_Hint;
1213 ----------------------
1214 -- Insert_With_Hint --
1215 ----------------------
1217 procedure Insert_With_Hint
1218 (Dst_Tree : in out Tree_Type;
1219 Dst_Hint : Node_Access;
1220 Src_Node : Node_Access;
1221 Dst_Node : out Node_Access)
1223 function New_Node return Node_Access;
1224 pragma Inline (New_Node);
1226 procedure Insert_Post is
1227 new Element_Keys.Generic_Insert_Post (New_Node);
1229 procedure Insert_Sans_Hint is
1230 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1232 procedure Local_Insert_With_Hint is
1233 new Element_Keys.Generic_Unconditional_Insert_With_Hint
1241 function New_Node return Node_Access is
1242 X : Element_Access := new Element_Type'(Src_Node
.Element
.all);
1245 return new Node_Type
'(Parent => null,
1257 -- Start of processing for Insert_With_Hint
1260 Local_Insert_With_Hint
1263 Src_Node.Element.all,
1265 end Insert_With_Hint;
1271 procedure Intersection (Target : in out Set; Source : Set) is
1273 Set_Ops.Intersection (Target.Tree, Source.Tree);
1276 function Intersection (Left, Right : Set) return Set is
1277 Tree : constant Tree_Type :=
1278 Set_Ops.Intersection (Left.Tree, Right.Tree);
1280 return Set'(Controlled
with Tree
);
1287 function Is_Empty
(Container
: Set
) return Boolean is
1289 return Container
.Tree
.Length
= 0;
1292 ------------------------
1293 -- Is_Equal_Node_Node --
1294 ------------------------
1296 function Is_Equal_Node_Node
(L
, R
: Node_Access
) return Boolean is
1298 return L
.Element
.all = R
.Element
.all;
1299 end Is_Equal_Node_Node
;
1301 -----------------------------
1302 -- Is_Greater_Element_Node --
1303 -----------------------------
1305 function Is_Greater_Element_Node
1306 (Left
: Element_Type
;
1307 Right
: Node_Access
) return Boolean
1310 -- e > node same as node < e
1312 return Right
.Element
.all < Left
;
1313 end Is_Greater_Element_Node
;
1315 --------------------------
1316 -- Is_Less_Element_Node --
1317 --------------------------
1319 function Is_Less_Element_Node
1320 (Left
: Element_Type
;
1321 Right
: Node_Access
) return Boolean
1324 return Left
< Right
.Element
.all;
1325 end Is_Less_Element_Node
;
1327 -----------------------
1328 -- Is_Less_Node_Node --
1329 -----------------------
1331 function Is_Less_Node_Node
(L
, R
: Node_Access
) return Boolean is
1333 return L
.Element
.all < R
.Element
.all;
1334 end Is_Less_Node_Node
;
1340 function Is_Subset
(Subset
: Set
; Of_Set
: Set
) return Boolean is
1342 return Set_Ops
.Is_Subset
(Subset
=> Subset
.Tree
, Of_Set
=> Of_Set
.Tree
);
1351 Item
: Element_Type
;
1352 Process
: not null access procedure (Position
: Cursor
))
1354 procedure Process_Node
(Node
: Node_Access
);
1355 pragma Inline
(Process_Node
);
1357 procedure Local_Iterate
is
1358 new Element_Keys
.Generic_Iteration
(Process_Node
);
1364 procedure Process_Node
(Node
: Node_Access
) is
1366 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1369 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1370 B : Natural renames T.Busy;
1372 -- Start of processing for Iterate
1378 Local_Iterate (T, Item);
1390 Process : not null access procedure (Position : Cursor))
1392 procedure Process_Node (Node : Node_Access);
1393 pragma Inline (Process_Node);
1395 procedure Local_Iterate is
1396 new Tree_Operations.Generic_Iteration (Process_Node);
1402 procedure Process_Node (Node : Node_Access) is
1404 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1407 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
1408 B
: Natural renames T
.Busy
;
1410 -- Start of processing for Iterate
1426 function Iterate
(Container
: Set
)
1427 return Set_Iterator_Interfaces
.Reversible_Iterator
'Class
1429 S
: constant Set_Access
:= Container
'Unrestricted_Access;
1430 B
: Natural renames S
.Tree
.Busy
;
1433 -- The value of the Node component influences the behavior of the First
1434 -- and Last selector functions of the iterator object. When the Node
1435 -- component is null (as is the case here), this means the iterator
1436 -- object was constructed without a start expression. This is a complete
1437 -- iterator, meaning that the iteration starts from the (logical)
1438 -- beginning of the sequence of items.
1440 -- Note: For a forward iterator, Container.First is the beginning, and
1441 -- for a reverse iterator, Container.Last is the beginning.
1443 return It
: constant Iterator
:= (Limited_Controlled
with S
, null) do
1448 function Iterate
(Container
: Set
; Start
: Cursor
)
1449 return Set_Iterator_Interfaces
.Reversible_Iterator
'Class
1451 S
: constant Set_Access
:= Container
'Unrestricted_Access;
1452 B
: Natural renames S
.Tree
.Busy
;
1455 -- It was formerly the case that when Start = No_Element, the partial
1456 -- iterator was defined to behave the same as for a complete iterator,
1457 -- and iterate over the entire sequence of items. However, those
1458 -- semantics were unintuitive and arguably error-prone (it is too easy
1459 -- to accidentally create an endless loop), and so they were changed,
1460 -- per the ARG meeting in Denver on 2011/11. However, there was no
1461 -- consensus about what positive meaning this corner case should have,
1462 -- and so it was decided to simply raise an exception. This does imply,
1463 -- however, that it is not possible to use a partial iterator to specify
1464 -- an empty sequence of items.
1466 if Start
= No_Element
then
1467 raise Constraint_Error
with
1468 "Start position for iterator equals No_Element";
1471 if Start
.Container
/= Container
'Unrestricted_Access then
1472 raise Program_Error
with
1473 "Start cursor of Iterate designates wrong set";
1476 pragma Assert
(Vet
(Container
.Tree
, Start
.Node
),
1477 "Start cursor of Iterate is bad");
1479 -- The value of the Node component influences the behavior of the First
1480 -- and Last selector functions of the iterator object. When the Node
1481 -- component is non-null (as is the case here), it means that this is a
1482 -- partial iteration, over a subset of the complete sequence of
1483 -- items. The iterator object was constructed with a start expression,
1484 -- indicating the position from which the iteration begins. Note that
1485 -- the start position has the same value irrespective of whether this is
1486 -- a forward or reverse iteration.
1488 return It
: constant Iterator
:=
1489 (Limited_Controlled
with S
, Start
.Node
)
1499 function Last
(Container
: Set
) return Cursor
is
1501 if Container
.Tree
.Last
= null then
1505 return Cursor
'(Container'Unrestricted_Access, Container.Tree.Last);
1508 function Last (Object : Iterator) return Cursor is
1510 -- The value of the iterator object's Node component influences the
1511 -- behavior of the Last (and First) selector function.
1513 -- When the Node component is null, this means the iterator object was
1514 -- constructed without a start expression, in which case the (reverse)
1515 -- iteration starts from the (logical) beginning of the entire sequence
1516 -- (corresponding to Container.Last, for a reverse iterator).
1518 -- Otherwise, this is iteration over a partial sequence of items. When
1519 -- the Node component is non-null, the iterator object was constructed
1520 -- with a start expression, that specifies the position from which the
1521 -- (reverse) partial iteration begins.
1523 if Object.Node = null then
1524 return Object.Container.Last;
1526 return Cursor'(Object
.Container
, Object
.Node
);
1534 function Last_Element
(Container
: Set
) return Element_Type
is
1536 if Container
.Tree
.Last
= null then
1537 raise Constraint_Error
with "set is empty";
1540 pragma Assert
(Container
.Tree
.Last
.Element
/= null);
1541 return Container
.Tree
.Last
.Element
.all;
1548 function Left
(Node
: Node_Access
) return Node_Access
is
1557 function Length
(Container
: Set
) return Count_Type
is
1559 return Container
.Tree
.Length
;
1567 new Tree_Operations
.Generic_Move
(Clear
);
1569 procedure Move
(Target
: in out Set
; Source
: in out Set
) is
1571 Move
(Target
=> Target
.Tree
, Source
=> Source
.Tree
);
1578 function Next
(Position
: Cursor
) return Cursor
is
1580 if Position
= No_Element
then
1584 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
1585 "bad cursor in Next");
1588 Node
: constant Node_Access
:=
1589 Tree_Operations
.Next
(Position
.Node
);
1596 return Cursor
'(Position.Container, Node);
1600 procedure Next (Position : in out Cursor) is
1602 Position := Next (Position);
1605 function Next (Object : Iterator; Position : Cursor) return Cursor is
1607 if Position.Container = null then
1611 if Position.Container /= Object.Container then
1612 raise Program_Error with
1613 "Position cursor of Next designates wrong set";
1616 return Next (Position);
1623 function Overlap (Left, Right : Set) return Boolean is
1625 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1632 function Parent (Node : Node_Access) return Node_Access is
1641 function Previous (Position : Cursor) return Cursor is
1643 if Position = No_Element then
1647 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1648 "bad cursor in Previous");
1651 Node : constant Node_Access :=
1652 Tree_Operations.Previous (Position.Node);
1659 return Cursor'(Position
.Container
, Node
);
1663 procedure Previous
(Position
: in out Cursor
) is
1665 Position
:= Previous
(Position
);
1668 function Previous
(Object
: Iterator
; Position
: Cursor
) return Cursor
is
1670 if Position
.Container
= null then
1674 if Position
.Container
/= Object
.Container
then
1675 raise Program_Error
with
1676 "Position cursor of Previous designates wrong set";
1679 return Previous
(Position
);
1686 procedure Query_Element
1688 Process
: not null access procedure (Element
: Element_Type
))
1691 if Position
.Node
= null then
1692 raise Constraint_Error
with "Position cursor equals No_Element";
1695 if Position
.Node
.Element
= null then
1696 raise Program_Error
with "Position cursor is bad";
1699 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
1700 "bad cursor in Query_Element");
1703 T
: Tree_Type
renames Position
.Container
.Tree
;
1705 B
: Natural renames T
.Busy
;
1706 L
: Natural renames T
.Lock
;
1713 Process
(Position
.Node
.Element
.all);
1731 (Stream
: not null access Root_Stream_Type
'Class;
1732 Container
: out Set
)
1735 (Stream
: not null access Root_Stream_Type
'Class) return Node_Access
;
1736 pragma Inline
(Read_Node
);
1739 new Tree_Operations
.Generic_Read
(Clear
, Read_Node
);
1746 (Stream
: not null access Root_Stream_Type
'Class) return Node_Access
1748 Node
: Node_Access
:= new Node_Type
;
1750 Node
.Element
:= new Element_Type
'(Element_Type'Input (Stream));
1754 Free (Node); -- Note that Free deallocates elem too
1758 -- Start of processing for Read
1761 Read (Stream, Container.Tree);
1765 (Stream : not null access Root_Stream_Type'Class;
1769 raise Program_Error with "attempt to stream set cursor";
1773 (Stream : not null access Root_Stream_Type'Class;
1774 Item : out Constant_Reference_Type)
1777 raise Program_Error with "attempt to stream reference";
1780 ---------------------
1781 -- Replace_Element --
1782 ---------------------
1784 procedure Replace_Element
1785 (Tree : in out Tree_Type;
1787 Item : Element_Type)
1790 if Item < Node.Element.all
1791 or else Node.Element.all < Item
1795 if Tree.Lock > 0 then
1796 raise Program_Error with
1797 "attempt to tamper with elements (set is locked)";
1801 X : Element_Access := Node.Element;
1803 -- The element allocator may need an accessibility check in the
1804 -- case the actual type is class-wide or has access discriminants
1805 -- (see RM 4.8(10.1) and AI12-0035).
1807 pragma Unsuppress (Accessibility_Check);
1810 Node.Element := new Element_Type'(Item
);
1817 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, Node
); -- Checks busy-bit
1819 Insert_New_Item
: declare
1820 function New_Node
return Node_Access
;
1821 pragma Inline
(New_Node
);
1823 procedure Insert_Post
is
1824 new Element_Keys
.Generic_Insert_Post
(New_Node
);
1826 procedure Unconditional_Insert
is
1827 new Element_Keys
.Generic_Unconditional_Insert
(Insert_Post
);
1833 function New_Node
return Node_Access
is
1835 -- The element allocator may need an accessibility check in the
1836 -- case the actual type is class-wide or has access discriminants
1837 -- (see RM 4.8(10.1) and AI12-0035).
1839 pragma Unsuppress
(Accessibility_Check
);
1842 Node
.Element
:= new Element_Type
'(Item); -- OK if fails
1843 Node.Color := Red_Black_Trees.Red;
1844 Node.Parent := null;
1851 Result : Node_Access;
1853 X : Element_Access := Node.Element;
1855 -- Start of processing for Insert_New_Item
1858 Unconditional_Insert
1862 pragma Assert (Result = Node);
1864 Free_Element (X); -- OK if fails
1865 end Insert_New_Item;
1866 end Replace_Element;
1868 procedure Replace_Element
1869 (Container : in out Set;
1871 New_Item : Element_Type)
1874 if Position.Node = null then
1875 raise Constraint_Error with "Position cursor equals No_Element";
1878 if Position.Node.Element = null then
1879 raise Program_Error with "Position cursor is bad";
1882 if Position.Container /= Container'Unrestricted_Access then
1883 raise Program_Error with "Position cursor designates wrong set";
1886 pragma Assert (Vet (Container.Tree, Position.Node),
1887 "bad cursor in Replace_Element");
1889 Replace_Element (Container.Tree, Position.Node, New_Item);
1890 end Replace_Element;
1892 ---------------------
1893 -- Reverse_Iterate --
1894 ---------------------
1896 procedure Reverse_Iterate
1898 Item : Element_Type;
1899 Process : not null access procedure (Position : Cursor))
1901 procedure Process_Node (Node : Node_Access);
1902 pragma Inline (Process_Node);
1904 procedure Local_Reverse_Iterate is
1905 new Element_Keys.Generic_Reverse_Iteration (Process_Node);
1911 procedure Process_Node (Node : Node_Access) is
1913 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1916 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
1917 B
: Natural renames T
.Busy
;
1919 -- Start of processing for Reverse_Iterate
1925 Local_Reverse_Iterate
(T
, Item
);
1933 end Reverse_Iterate
;
1935 procedure Reverse_Iterate
1937 Process
: not null access procedure (Position
: Cursor
))
1939 procedure Process_Node
(Node
: Node_Access
);
1940 pragma Inline
(Process_Node
);
1942 procedure Local_Reverse_Iterate
is
1943 new Tree_Operations
.Generic_Reverse_Iteration
(Process_Node
);
1949 procedure Process_Node
(Node
: Node_Access
) is
1951 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1954 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1955 B : Natural renames T.Busy;
1957 -- Start of processing for Reverse_Iterate
1963 Local_Reverse_Iterate (T);
1971 end Reverse_Iterate;
1977 function Right (Node : Node_Access) return Node_Access is
1986 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1988 Node.Color := Color;
1995 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
2004 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
2006 Node.Parent := Parent;
2013 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
2015 Node.Right := Right;
2018 --------------------------
2019 -- Symmetric_Difference --
2020 --------------------------
2022 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
2024 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
2025 end Symmetric_Difference;
2027 function Symmetric_Difference (Left, Right : Set) return Set is
2028 Tree : constant Tree_Type :=
2029 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
2031 return Set'(Controlled
with Tree
);
2032 end Symmetric_Difference
;
2038 function To_Set
(New_Item
: Element_Type
) return Set
is
2041 pragma Unreferenced
(Node
);
2043 Insert_Sans_Hint
(Tree
, New_Item
, Node
);
2044 return Set
'(Controlled with Tree);
2051 procedure Union (Target : in out Set; Source : Set) is
2053 Set_Ops.Union (Target.Tree, Source.Tree);
2056 function Union (Left, Right : Set) return Set is
2057 Tree : constant Tree_Type :=
2058 Set_Ops.Union (Left.Tree, Right.Tree);
2060 return Set'(Controlled
with Tree
);
2068 (Stream
: not null access Root_Stream_Type
'Class;
2071 procedure Write_Node
2072 (Stream
: not null access Root_Stream_Type
'Class;
2073 Node
: Node_Access
);
2074 pragma Inline
(Write_Node
);
2077 new Tree_Operations
.Generic_Write
(Write_Node
);
2083 procedure Write_Node
2084 (Stream
: not null access Root_Stream_Type
'Class;
2088 Element_Type
'Output (Stream
, Node
.Element
.all);
2091 -- Start of processing for Write
2094 Write
(Stream
, Container
.Tree
);
2098 (Stream
: not null access Root_Stream_Type
'Class;
2102 raise Program_Error
with "attempt to stream set cursor";
2106 (Stream
: not null access Root_Stream_Type
'Class;
2107 Item
: Constant_Reference_Type
)
2110 raise Program_Error
with "attempt to stream reference";
2112 end Ada
.Containers
.Indefinite_Ordered_Multisets
;