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 Warnings
(Off
, "variable ""Busy*"" is not referenced");
46 pragma Warnings
(Off
, "variable ""Lock*"" is not referenced");
47 -- See comment in Ada.Containers.Helpers
49 -----------------------------
50 -- Node Access Subprograms --
51 -----------------------------
53 -- These subprograms provide a functional interface to access fields
54 -- of a node, and a procedural interface for modifying these values.
56 function Color
(Node
: Node_Access
) return Color_Type
;
57 pragma Inline
(Color
);
59 function Left
(Node
: Node_Access
) return Node_Access
;
62 function Parent
(Node
: Node_Access
) return Node_Access
;
63 pragma Inline
(Parent
);
65 function Right
(Node
: Node_Access
) return Node_Access
;
66 pragma Inline
(Right
);
68 procedure Set_Parent
(Node
: Node_Access
; Parent
: Node_Access
);
69 pragma Inline
(Set_Parent
);
71 procedure Set_Left
(Node
: Node_Access
; Left
: Node_Access
);
72 pragma Inline
(Set_Left
);
74 procedure Set_Right
(Node
: Node_Access
; Right
: Node_Access
);
75 pragma Inline
(Set_Right
);
77 procedure Set_Color
(Node
: Node_Access
; Color
: Color_Type
);
78 pragma Inline
(Set_Color
);
80 -----------------------
81 -- Local Subprograms --
82 -----------------------
84 function Copy_Node
(Source
: Node_Access
) return Node_Access
;
85 pragma Inline
(Copy_Node
);
87 procedure Free
(X
: in out Node_Access
);
89 procedure Insert_Sans_Hint
90 (Tree
: in out Tree_Type
;
91 New_Item
: Element_Type
;
92 Node
: out Node_Access
);
94 procedure Insert_With_Hint
95 (Dst_Tree
: in out Tree_Type
;
96 Dst_Hint
: Node_Access
;
97 Src_Node
: Node_Access
;
98 Dst_Node
: out Node_Access
);
100 function Is_Equal_Node_Node
(L
, R
: Node_Access
) return Boolean;
101 pragma Inline
(Is_Equal_Node_Node
);
103 function Is_Greater_Element_Node
104 (Left
: Element_Type
;
105 Right
: Node_Access
) return Boolean;
106 pragma Inline
(Is_Greater_Element_Node
);
108 function Is_Less_Element_Node
109 (Left
: Element_Type
;
110 Right
: Node_Access
) return Boolean;
111 pragma Inline
(Is_Less_Element_Node
);
113 function Is_Less_Node_Node
(L
, R
: Node_Access
) return Boolean;
114 pragma Inline
(Is_Less_Node_Node
);
116 procedure Replace_Element
117 (Tree
: in out Tree_Type
;
119 Item
: Element_Type
);
121 --------------------------
122 -- Local Instantiations --
123 --------------------------
125 package Tree_Operations
is
126 new Red_Black_Trees
.Generic_Operations
(Tree_Types
);
128 procedure Delete_Tree
is
129 new Tree_Operations
.Generic_Delete_Tree
(Free
);
131 function Copy_Tree
is
132 new Tree_Operations
.Generic_Copy_Tree
(Copy_Node
, Delete_Tree
);
136 procedure Free_Element
is
137 new Ada
.Unchecked_Deallocation
(Element_Type
, Element_Access
);
140 new Tree_Operations
.Generic_Equal
(Is_Equal_Node_Node
);
143 new Generic_Set_Operations
144 (Tree_Operations
=> Tree_Operations
,
145 Insert_With_Hint
=> Insert_With_Hint
,
146 Copy_Tree
=> Copy_Tree
,
147 Delete_Tree
=> Delete_Tree
,
148 Is_Less
=> Is_Less_Node_Node
,
151 package Element_Keys
is
152 new Red_Black_Trees
.Generic_Keys
153 (Tree_Operations
=> Tree_Operations
,
154 Key_Type
=> Element_Type
,
155 Is_Less_Key_Node
=> Is_Less_Element_Node
,
156 Is_Greater_Key_Node
=> Is_Greater_Element_Node
);
162 function "<" (Left
, Right
: Cursor
) return Boolean is
164 if Left
.Node
= null then
165 raise Constraint_Error
with "Left cursor equals No_Element";
168 if Right
.Node
= null then
169 raise Constraint_Error
with "Right cursor equals No_Element";
172 if Left
.Node
.Element
= null then
173 raise Program_Error
with "Left cursor is bad";
176 if Right
.Node
.Element
= null then
177 raise Program_Error
with "Right cursor is bad";
180 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
181 "bad Left cursor in ""<""");
183 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
184 "bad Right cursor in ""<""");
186 return Left
.Node
.Element
.all < Right
.Node
.Element
.all;
189 function "<" (Left
: Cursor
; Right
: Element_Type
) return Boolean is
191 if Left
.Node
= null then
192 raise Constraint_Error
with "Left cursor equals No_Element";
195 if Left
.Node
.Element
= null then
196 raise Program_Error
with "Left cursor is bad";
199 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
200 "bad Left cursor in ""<""");
202 return Left
.Node
.Element
.all < Right
;
205 function "<" (Left
: Element_Type
; Right
: Cursor
) return Boolean is
207 if Right
.Node
= null then
208 raise Constraint_Error
with "Right cursor equals No_Element";
211 if Right
.Node
.Element
= null then
212 raise Program_Error
with "Right cursor is bad";
215 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
216 "bad Right cursor in ""<""");
218 return Left
< Right
.Node
.Element
.all;
225 function "=" (Left
, Right
: Set
) return Boolean is
227 return Is_Equal
(Left
.Tree
, Right
.Tree
);
234 function ">" (Left
, Right
: Cursor
) return Boolean is
236 if Left
.Node
= null then
237 raise Constraint_Error
with "Left cursor equals No_Element";
240 if Right
.Node
= null then
241 raise Constraint_Error
with "Right cursor equals No_Element";
244 if Left
.Node
.Element
= null then
245 raise Program_Error
with "Left cursor is bad";
248 if Right
.Node
.Element
= null then
249 raise Program_Error
with "Right cursor is bad";
252 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
253 "bad Left cursor in "">""");
255 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
256 "bad Right cursor in "">""");
258 -- L > R same as R < L
260 return Right
.Node
.Element
.all < Left
.Node
.Element
.all;
263 function ">" (Left
: Cursor
; Right
: Element_Type
) return Boolean is
265 if Left
.Node
= null then
266 raise Constraint_Error
with "Left cursor equals No_Element";
269 if Left
.Node
.Element
= null then
270 raise Program_Error
with "Left cursor is bad";
273 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
274 "bad Left cursor in "">""");
276 return Right
< Left
.Node
.Element
.all;
279 function ">" (Left
: Element_Type
; Right
: Cursor
) return Boolean is
281 if Right
.Node
= null then
282 raise Constraint_Error
with "Right cursor equals No_Element";
285 if Right
.Node
.Element
= null then
286 raise Program_Error
with "Right cursor is bad";
289 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
290 "bad Right cursor in "">""");
292 return Right
.Node
.Element
.all < Left
;
300 new Tree_Operations
.Generic_Adjust
(Copy_Tree
);
302 procedure Adjust
(Container
: in out Set
) is
304 Adjust
(Container
.Tree
);
311 procedure Assign
(Target
: in out Set
; Source
: Set
) is
313 if Target
'Address = Source
'Address then
318 Target
.Union
(Source
);
325 function Ceiling
(Container
: Set
; Item
: Element_Type
) return Cursor
is
326 Node
: constant Node_Access
:=
327 Element_Keys
.Ceiling
(Container
.Tree
, Item
);
334 return Cursor
'(Container'Unrestricted_Access, Node);
342 new Tree_Operations.Generic_Clear (Delete_Tree);
344 procedure Clear (Container : in out Set) is
346 Clear (Container.Tree);
353 function Color (Node : Node_Access) return Color_Type is
358 ------------------------
359 -- Constant_Reference --
360 ------------------------
362 function Constant_Reference
363 (Container : aliased Set;
364 Position : Cursor) return Constant_Reference_Type
367 if Position.Container = null then
368 raise Constraint_Error with "Position cursor has no element";
371 if Position.Container /= Container'Unrestricted_Access then
372 raise Program_Error with
373 "Position cursor designates wrong container";
376 pragma Assert (Vet (Position.Container.Tree, Position.Node),
377 "bad cursor in Constant_Reference");
379 -- Note: in predefined container units, the creation of a reference
380 -- increments the busy bit of the container, and its finalization
381 -- decrements it. In the absence of control machinery, this tampering
382 -- protection is missing.
385 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
386 pragma Unreferenced (T);
388 return R : constant Constant_Reference_Type :=
389 (Element => Position.Node.Element,
390 Control => (Container => Container'Unrestricted_Access))
395 end Constant_Reference;
401 function Contains (Container : Set; Item : Element_Type) return Boolean is
403 return Find (Container, Item) /= No_Element;
410 function Copy (Source : Set) return Set is
412 return Target : Set do
413 Target.Assign (Source);
421 function Copy_Node (Source : Node_Access) return Node_Access is
422 X : Element_Access := new Element_Type'(Source
.Element
.all);
425 return new Node_Type
'(Parent => null,
428 Color => Source.Color,
441 procedure Delete (Container : in out Set; Item : Element_Type) is
442 Tree : Tree_Type renames Container.Tree;
443 Node : Node_Access := Element_Keys.Ceiling (Tree, Item);
444 Done : constant Node_Access := Element_Keys.Upper_Bound (Tree, Item);
449 raise Constraint_Error with "attempt to delete element not in set";
454 Node := Tree_Operations.Next (Node);
455 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
458 exit when Node = Done;
462 procedure Delete (Container : in out Set; Position : in out Cursor) is
464 if Position.Node = null then
465 raise Constraint_Error with "Position cursor equals No_Element";
468 if Position.Node.Element = null then
469 raise Program_Error with "Position cursor is bad";
472 if Position.Container /= Container'Unrestricted_Access then
473 raise Program_Error with "Position cursor designates wrong set";
476 pragma Assert (Vet (Container.Tree, Position.Node),
477 "bad cursor in Delete");
479 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
480 Free (Position.Node);
482 Position.Container := null;
489 procedure Delete_First (Container : in out Set) is
490 Tree : Tree_Type renames Container.Tree;
491 X : Node_Access := Tree.First;
498 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
506 procedure Delete_Last (Container : in out Set) is
507 Tree : Tree_Type renames Container.Tree;
508 X : Node_Access := Tree.Last;
515 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
523 procedure Difference (Target : in out Set; Source : Set) is
525 Set_Ops.Difference (Target.Tree, Source.Tree);
528 function Difference (Left, Right : Set) return Set is
529 Tree : constant Tree_Type := Set_Ops.Difference (Left.Tree, Right.Tree);
531 return Set'(Controlled
with Tree
);
538 function Element
(Position
: Cursor
) return Element_Type
is
540 if Position
.Node
= null then
541 raise Constraint_Error
with "Position cursor equals No_Element";
544 if Position
.Node
.Element
= null then
545 raise Program_Error
with "Position cursor is bad";
548 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
549 "bad cursor in Element");
551 return Position
.Node
.Element
.all;
554 -------------------------
555 -- Equivalent_Elements --
556 -------------------------
558 function Equivalent_Elements
(Left
, Right
: Element_Type
) return Boolean is
567 end Equivalent_Elements
;
569 ---------------------
570 -- Equivalent_Sets --
571 ---------------------
573 function Equivalent_Sets
(Left
, Right
: Set
) return Boolean is
575 function Is_Equivalent_Node_Node
(L
, R
: Node_Access
) return Boolean;
576 pragma Inline
(Is_Equivalent_Node_Node
);
578 function Is_Equivalent
is
579 new Tree_Operations
.Generic_Equal
(Is_Equivalent_Node_Node
);
581 -----------------------------
582 -- Is_Equivalent_Node_Node --
583 -----------------------------
585 function Is_Equivalent_Node_Node
(L
, R
: Node_Access
) return Boolean is
587 if L
.Element
.all < R
.Element
.all then
589 elsif R
.Element
.all < L
.Element
.all then
594 end Is_Equivalent_Node_Node
;
596 -- Start of processing for Equivalent_Sets
599 return Is_Equivalent
(Left
.Tree
, Right
.Tree
);
606 procedure Exclude
(Container
: in out Set
; Item
: Element_Type
) is
607 Tree
: Tree_Type
renames Container
.Tree
;
608 Node
: Node_Access
:= Element_Keys
.Ceiling
(Tree
, Item
);
609 Done
: constant Node_Access
:= Element_Keys
.Upper_Bound
(Tree
, Item
);
613 while Node
/= Done
loop
615 Node
:= Tree_Operations
.Next
(Node
);
616 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, X
);
625 function Find
(Container
: Set
; Item
: Element_Type
) return Cursor
is
626 Node
: constant Node_Access
:= Element_Keys
.Find
(Container
.Tree
, Item
);
633 return Cursor
'(Container'Unrestricted_Access, Node);
640 procedure Finalize (Object : in out Iterator) is
642 Unbusy (Object.Container.Tree.TC);
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 Busy
: With_Busy
(T
.TC
'Unrestricted_Access);
948 -- Start of processing for Iterate
951 Local_Iterate
(T
, Key
);
958 function Key
(Position
: Cursor
) return Key_Type
is
960 if Position
.Node
= null then
961 raise Constraint_Error
with
962 "Position cursor equals No_Element";
965 if Position
.Node
.Element
= null then
966 raise Program_Error
with
967 "Position cursor is bad";
970 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
971 "bad cursor in Key");
973 return Key
(Position
.Node
.Element
.all);
976 ---------------------
977 -- Reverse_Iterate --
978 ---------------------
980 procedure Reverse_Iterate
983 Process
: not null access procedure (Position
: Cursor
))
985 procedure Process_Node
(Node
: Node_Access
);
986 pragma Inline
(Process_Node
);
992 procedure Local_Reverse_Iterate
is
993 new Key_Keys
.Generic_Reverse_Iteration
(Process_Node
);
999 procedure Process_Node
(Node
: Node_Access
) is
1001 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1004 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1005 Busy : With_Busy (T.TC'Unrestricted_Access);
1007 -- Start of processing for Reverse_Iterate
1010 Local_Reverse_Iterate (T, Key);
1011 end Reverse_Iterate;
1013 --------------------
1014 -- Update_Element --
1015 --------------------
1017 procedure Update_Element
1018 (Container : in out Set;
1020 Process : not null access procedure (Element : in out Element_Type))
1022 Tree : Tree_Type renames Container.Tree;
1023 Node : constant Node_Access := Position.Node;
1027 raise Constraint_Error with "Position cursor equals No_Element";
1030 if Node.Element = null then
1031 raise Program_Error with "Position cursor is bad";
1034 if Position.Container /= Container'Unrestricted_Access then
1035 raise Program_Error with "Position cursor designates wrong set";
1038 pragma Assert (Vet (Tree, Node),
1039 "bad cursor in Update_Element");
1042 E : Element_Type renames Node.Element.all;
1043 K : constant Key_Type := Key (E);
1044 Lock : With_Lock (Tree.TC'Unrestricted_Access);
1048 if Equivalent_Keys (Left => K, Right => Key (E)) then
1053 -- Delete_Node checks busy-bit
1055 Tree_Operations.Delete_Node_Sans_Free (Tree, Node);
1057 Insert_New_Item : declare
1058 function New_Node return Node_Access;
1059 pragma Inline (New_Node);
1061 procedure Insert_Post is
1062 new Element_Keys.Generic_Insert_Post (New_Node);
1064 procedure Unconditional_Insert is
1065 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1071 function New_Node return Node_Access is
1073 Node.Color := Red_Black_Trees.Red;
1074 Node.Parent := null;
1081 Result : Node_Access;
1083 -- Start of processing for Insert_New_Item
1086 Unconditional_Insert
1088 Key => Node.Element.all,
1091 pragma Assert (Result = Node);
1092 end Insert_New_Item;
1101 function Has_Element (Position : Cursor) return Boolean is
1103 return Position /= No_Element;
1110 procedure Insert (Container : in out Set; New_Item : Element_Type) is
1112 pragma Unreferenced (Position);
1114 Insert (Container, New_Item, Position);
1118 (Container : in out Set;
1119 New_Item : Element_Type;
1120 Position : out Cursor)
1123 Insert_Sans_Hint (Container.Tree, New_Item, Position.Node);
1124 Position.Container := Container'Unrestricted_Access;
1127 ----------------------
1128 -- Insert_Sans_Hint --
1129 ----------------------
1131 procedure Insert_Sans_Hint
1132 (Tree : in out Tree_Type;
1133 New_Item : Element_Type;
1134 Node : out Node_Access)
1136 function New_Node return Node_Access;
1137 pragma Inline (New_Node);
1139 procedure Insert_Post is
1140 new Element_Keys.Generic_Insert_Post (New_Node);
1142 procedure Unconditional_Insert is
1143 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1149 function New_Node return Node_Access is
1150 -- The element allocator may need an accessibility check in the case
1151 -- the actual type is class-wide or has access discriminants (see
1152 -- RM 4.8(10.1) and AI12-0035).
1154 pragma Unsuppress (Accessibility_Check);
1156 Element : Element_Access := new Element_Type'(New_Item
);
1159 return new Node_Type
'(Parent => null,
1162 Color => Red_Black_Trees.Red,
1163 Element => Element);
1167 Free_Element (Element);
1171 -- Start of processing for Insert_Sans_Hint
1174 Unconditional_Insert (Tree, New_Item, Node);
1175 end Insert_Sans_Hint;
1177 ----------------------
1178 -- Insert_With_Hint --
1179 ----------------------
1181 procedure Insert_With_Hint
1182 (Dst_Tree : in out Tree_Type;
1183 Dst_Hint : Node_Access;
1184 Src_Node : Node_Access;
1185 Dst_Node : out Node_Access)
1187 function New_Node return Node_Access;
1188 pragma Inline (New_Node);
1190 procedure Insert_Post is
1191 new Element_Keys.Generic_Insert_Post (New_Node);
1193 procedure Insert_Sans_Hint is
1194 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1196 procedure Local_Insert_With_Hint is
1197 new Element_Keys.Generic_Unconditional_Insert_With_Hint
1205 function New_Node return Node_Access is
1206 X : Element_Access := new Element_Type'(Src_Node
.Element
.all);
1209 return new Node_Type
'(Parent => null,
1221 -- Start of processing for Insert_With_Hint
1224 Local_Insert_With_Hint
1227 Src_Node.Element.all,
1229 end Insert_With_Hint;
1235 procedure Intersection (Target : in out Set; Source : Set) is
1237 Set_Ops.Intersection (Target.Tree, Source.Tree);
1240 function Intersection (Left, Right : Set) return Set is
1241 Tree : constant Tree_Type :=
1242 Set_Ops.Intersection (Left.Tree, Right.Tree);
1244 return Set'(Controlled
with Tree
);
1251 function Is_Empty
(Container
: Set
) return Boolean is
1253 return Container
.Tree
.Length
= 0;
1256 ------------------------
1257 -- Is_Equal_Node_Node --
1258 ------------------------
1260 function Is_Equal_Node_Node
(L
, R
: Node_Access
) return Boolean is
1262 return L
.Element
.all = R
.Element
.all;
1263 end Is_Equal_Node_Node
;
1265 -----------------------------
1266 -- Is_Greater_Element_Node --
1267 -----------------------------
1269 function Is_Greater_Element_Node
1270 (Left
: Element_Type
;
1271 Right
: Node_Access
) return Boolean
1274 -- e > node same as node < e
1276 return Right
.Element
.all < Left
;
1277 end Is_Greater_Element_Node
;
1279 --------------------------
1280 -- Is_Less_Element_Node --
1281 --------------------------
1283 function Is_Less_Element_Node
1284 (Left
: Element_Type
;
1285 Right
: Node_Access
) return Boolean
1288 return Left
< Right
.Element
.all;
1289 end Is_Less_Element_Node
;
1291 -----------------------
1292 -- Is_Less_Node_Node --
1293 -----------------------
1295 function Is_Less_Node_Node
(L
, R
: Node_Access
) return Boolean is
1297 return L
.Element
.all < R
.Element
.all;
1298 end Is_Less_Node_Node
;
1304 function Is_Subset
(Subset
: Set
; Of_Set
: Set
) return Boolean is
1306 return Set_Ops
.Is_Subset
(Subset
=> Subset
.Tree
, Of_Set
=> Of_Set
.Tree
);
1315 Item
: Element_Type
;
1316 Process
: not null access procedure (Position
: Cursor
))
1318 procedure Process_Node
(Node
: Node_Access
);
1319 pragma Inline
(Process_Node
);
1321 procedure Local_Iterate
is
1322 new Element_Keys
.Generic_Iteration
(Process_Node
);
1328 procedure Process_Node
(Node
: Node_Access
) is
1330 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1333 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1334 Busy : With_Busy (T.TC'Unrestricted_Access);
1336 -- Start of processing for Iterate
1339 Local_Iterate (T, Item);
1344 Process : not null access procedure (Position : Cursor))
1346 procedure Process_Node (Node : Node_Access);
1347 pragma Inline (Process_Node);
1349 procedure Local_Iterate is
1350 new Tree_Operations.Generic_Iteration (Process_Node);
1356 procedure Process_Node (Node : Node_Access) is
1358 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1361 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
1362 Busy
: With_Busy
(T
.TC
'Unrestricted_Access);
1364 -- Start of processing for Iterate
1370 function Iterate
(Container
: Set
)
1371 return Set_Iterator_Interfaces
.Reversible_Iterator
'Class
1373 S
: constant Set_Access
:= Container
'Unrestricted_Access;
1375 -- The value of the Node component influences the behavior of the First
1376 -- and Last selector functions of the iterator object. When the Node
1377 -- component is null (as is the case here), this means the iterator
1378 -- object was constructed without a start expression. This is a complete
1379 -- iterator, meaning that the iteration starts from the (logical)
1380 -- beginning of the sequence of items.
1382 -- Note: For a forward iterator, Container.First is the beginning, and
1383 -- for a reverse iterator, Container.Last is the beginning.
1385 return It
: constant Iterator
:= (Limited_Controlled
with S
, null) do
1390 function Iterate
(Container
: Set
; Start
: Cursor
)
1391 return Set_Iterator_Interfaces
.Reversible_Iterator
'Class
1393 S
: constant Set_Access
:= Container
'Unrestricted_Access;
1395 -- It was formerly the case that when Start = No_Element, the partial
1396 -- iterator was defined to behave the same as for a complete iterator,
1397 -- and iterate over the entire sequence of items. However, those
1398 -- semantics were unintuitive and arguably error-prone (it is too easy
1399 -- to accidentally create an endless loop), and so they were changed,
1400 -- per the ARG meeting in Denver on 2011/11. However, there was no
1401 -- consensus about what positive meaning this corner case should have,
1402 -- and so it was decided to simply raise an exception. This does imply,
1403 -- however, that it is not possible to use a partial iterator to specify
1404 -- an empty sequence of items.
1406 if Start
= No_Element
then
1407 raise Constraint_Error
with
1408 "Start position for iterator equals No_Element";
1411 if Start
.Container
/= Container
'Unrestricted_Access then
1412 raise Program_Error
with
1413 "Start cursor of Iterate designates wrong set";
1416 pragma Assert
(Vet
(Container
.Tree
, Start
.Node
),
1417 "Start cursor of Iterate is bad");
1419 -- The value of the Node component influences the behavior of the First
1420 -- and Last selector functions of the iterator object. When the Node
1421 -- component is non-null (as is the case here), it means that this is a
1422 -- partial iteration, over a subset of the complete sequence of
1423 -- items. The iterator object was constructed with a start expression,
1424 -- indicating the position from which the iteration begins. Note that
1425 -- the start position has the same value irrespective of whether this is
1426 -- a forward or reverse iteration.
1428 return It
: constant Iterator
:=
1429 (Limited_Controlled
with S
, Start
.Node
)
1439 function Last
(Container
: Set
) return Cursor
is
1441 if Container
.Tree
.Last
= null then
1445 return Cursor
'(Container'Unrestricted_Access, Container.Tree.Last);
1448 function Last (Object : Iterator) return Cursor is
1450 -- The value of the iterator object's Node component influences the
1451 -- behavior of the Last (and First) selector function.
1453 -- When the Node component is null, this means the iterator object was
1454 -- constructed without a start expression, in which case the (reverse)
1455 -- iteration starts from the (logical) beginning of the entire sequence
1456 -- (corresponding to Container.Last, for a reverse iterator).
1458 -- Otherwise, this is iteration over a partial sequence of items. When
1459 -- the Node component is non-null, the iterator object was constructed
1460 -- with a start expression, that specifies the position from which the
1461 -- (reverse) partial iteration begins.
1463 if Object.Node = null then
1464 return Object.Container.Last;
1466 return Cursor'(Object
.Container
, Object
.Node
);
1474 function Last_Element
(Container
: Set
) return Element_Type
is
1476 if Container
.Tree
.Last
= null then
1477 raise Constraint_Error
with "set is empty";
1480 pragma Assert
(Container
.Tree
.Last
.Element
/= null);
1481 return Container
.Tree
.Last
.Element
.all;
1488 function Left
(Node
: Node_Access
) return Node_Access
is
1497 function Length
(Container
: Set
) return Count_Type
is
1499 return Container
.Tree
.Length
;
1507 new Tree_Operations
.Generic_Move
(Clear
);
1509 procedure Move
(Target
: in out Set
; Source
: in out Set
) is
1511 Move
(Target
=> Target
.Tree
, Source
=> Source
.Tree
);
1518 function Next
(Position
: Cursor
) return Cursor
is
1520 if Position
= No_Element
then
1524 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
1525 "bad cursor in Next");
1528 Node
: constant Node_Access
:=
1529 Tree_Operations
.Next
(Position
.Node
);
1536 return Cursor
'(Position.Container, Node);
1540 procedure Next (Position : in out Cursor) is
1542 Position := Next (Position);
1545 function Next (Object : Iterator; Position : Cursor) return Cursor is
1547 if Position.Container = null then
1551 if Position.Container /= Object.Container then
1552 raise Program_Error with
1553 "Position cursor of Next designates wrong set";
1556 return Next (Position);
1563 function Overlap (Left, Right : Set) return Boolean is
1565 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1572 function Parent (Node : Node_Access) return Node_Access is
1581 function Previous (Position : Cursor) return Cursor is
1583 if Position = No_Element then
1587 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1588 "bad cursor in Previous");
1591 Node : constant Node_Access :=
1592 Tree_Operations.Previous (Position.Node);
1599 return Cursor'(Position
.Container
, Node
);
1603 procedure Previous
(Position
: in out Cursor
) is
1605 Position
:= Previous
(Position
);
1608 function Previous
(Object
: Iterator
; Position
: Cursor
) return Cursor
is
1610 if Position
.Container
= null then
1614 if Position
.Container
/= Object
.Container
then
1615 raise Program_Error
with
1616 "Position cursor of Previous designates wrong set";
1619 return Previous
(Position
);
1626 procedure Query_Element
1628 Process
: not null access procedure (Element
: Element_Type
))
1631 if Position
.Node
= null then
1632 raise Constraint_Error
with "Position cursor equals No_Element";
1635 if Position
.Node
.Element
= null then
1636 raise Program_Error
with "Position cursor is bad";
1639 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
1640 "bad cursor in Query_Element");
1643 T
: Tree_Type
renames Position
.Container
.Tree
;
1644 Lock
: With_Lock
(T
.TC
'Unrestricted_Access);
1646 Process
(Position
.Node
.Element
.all);
1655 (Stream
: not null access Root_Stream_Type
'Class;
1656 Container
: out Set
)
1659 (Stream
: not null access Root_Stream_Type
'Class) return Node_Access
;
1660 pragma Inline
(Read_Node
);
1663 new Tree_Operations
.Generic_Read
(Clear
, Read_Node
);
1670 (Stream
: not null access Root_Stream_Type
'Class) return Node_Access
1672 Node
: Node_Access
:= new Node_Type
;
1674 Node
.Element
:= new Element_Type
'(Element_Type'Input (Stream));
1678 Free (Node); -- Note that Free deallocates elem too
1682 -- Start of processing for Read
1685 Read (Stream, Container.Tree);
1689 (Stream : not null access Root_Stream_Type'Class;
1693 raise Program_Error with "attempt to stream set cursor";
1697 (Stream : not null access Root_Stream_Type'Class;
1698 Item : out Constant_Reference_Type)
1701 raise Program_Error with "attempt to stream reference";
1704 ---------------------
1705 -- Replace_Element --
1706 ---------------------
1708 procedure Replace_Element
1709 (Tree : in out Tree_Type;
1711 Item : Element_Type)
1714 if Item < Node.Element.all
1715 or else Node.Element.all < Item
1722 X : Element_Access := Node.Element;
1724 -- The element allocator may need an accessibility check in the
1725 -- case the actual type is class-wide or has access discriminants
1726 -- (see RM 4.8(10.1) and AI12-0035).
1728 pragma Unsuppress (Accessibility_Check);
1731 Node.Element := new Element_Type'(Item
);
1738 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, Node
); -- Checks busy-bit
1740 Insert_New_Item
: declare
1741 function New_Node
return Node_Access
;
1742 pragma Inline
(New_Node
);
1744 procedure Insert_Post
is
1745 new Element_Keys
.Generic_Insert_Post
(New_Node
);
1747 procedure Unconditional_Insert
is
1748 new Element_Keys
.Generic_Unconditional_Insert
(Insert_Post
);
1754 function New_Node
return Node_Access
is
1756 -- The element allocator may need an accessibility check in the
1757 -- case the actual type is class-wide or has access discriminants
1758 -- (see RM 4.8(10.1) and AI12-0035).
1760 pragma Unsuppress
(Accessibility_Check
);
1763 Node
.Element
:= new Element_Type
'(Item); -- OK if fails
1764 Node.Color := Red_Black_Trees.Red;
1765 Node.Parent := null;
1772 Result : Node_Access;
1774 X : Element_Access := Node.Element;
1776 -- Start of processing for Insert_New_Item
1779 Unconditional_Insert
1783 pragma Assert (Result = Node);
1785 Free_Element (X); -- OK if fails
1786 end Insert_New_Item;
1787 end Replace_Element;
1789 procedure Replace_Element
1790 (Container : in out Set;
1792 New_Item : Element_Type)
1795 if Position.Node = null then
1796 raise Constraint_Error with "Position cursor equals No_Element";
1799 if Position.Node.Element = null then
1800 raise Program_Error with "Position cursor is bad";
1803 if Position.Container /= Container'Unrestricted_Access then
1804 raise Program_Error with "Position cursor designates wrong set";
1807 pragma Assert (Vet (Container.Tree, Position.Node),
1808 "bad cursor in Replace_Element");
1810 Replace_Element (Container.Tree, Position.Node, New_Item);
1811 end Replace_Element;
1813 ---------------------
1814 -- Reverse_Iterate --
1815 ---------------------
1817 procedure Reverse_Iterate
1819 Item : Element_Type;
1820 Process : not null access procedure (Position : Cursor))
1822 procedure Process_Node (Node : Node_Access);
1823 pragma Inline (Process_Node);
1825 procedure Local_Reverse_Iterate is
1826 new Element_Keys.Generic_Reverse_Iteration (Process_Node);
1832 procedure Process_Node (Node : Node_Access) is
1834 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1837 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
1838 Busy
: With_Busy
(T
.TC
'Unrestricted_Access);
1840 -- Start of processing for Reverse_Iterate
1843 Local_Reverse_Iterate
(T
, Item
);
1844 end Reverse_Iterate
;
1846 procedure Reverse_Iterate
1848 Process
: not null access procedure (Position
: Cursor
))
1850 procedure Process_Node
(Node
: Node_Access
);
1851 pragma Inline
(Process_Node
);
1853 procedure Local_Reverse_Iterate
is
1854 new Tree_Operations
.Generic_Reverse_Iteration
(Process_Node
);
1860 procedure Process_Node
(Node
: Node_Access
) is
1862 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1865 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1866 Busy : With_Busy (T.TC'Unrestricted_Access);
1868 -- Start of processing for Reverse_Iterate
1871 Local_Reverse_Iterate (T);
1872 end Reverse_Iterate;
1878 function Right (Node : Node_Access) return Node_Access is
1887 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1889 Node.Color := Color;
1896 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1905 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1907 Node.Parent := Parent;
1914 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1916 Node.Right := Right;
1919 --------------------------
1920 -- Symmetric_Difference --
1921 --------------------------
1923 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1925 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
1926 end Symmetric_Difference;
1928 function Symmetric_Difference (Left, Right : Set) return Set is
1929 Tree : constant Tree_Type :=
1930 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
1932 return Set'(Controlled
with Tree
);
1933 end Symmetric_Difference
;
1939 function To_Set
(New_Item
: Element_Type
) return Set
is
1942 pragma Unreferenced
(Node
);
1944 Insert_Sans_Hint
(Tree
, New_Item
, Node
);
1945 return Set
'(Controlled with Tree);
1952 procedure Union (Target : in out Set; Source : Set) is
1954 Set_Ops.Union (Target.Tree, Source.Tree);
1957 function Union (Left, Right : Set) return Set is
1958 Tree : constant Tree_Type :=
1959 Set_Ops.Union (Left.Tree, Right.Tree);
1961 return Set'(Controlled
with Tree
);
1969 (Stream
: not null access Root_Stream_Type
'Class;
1972 procedure Write_Node
1973 (Stream
: not null access Root_Stream_Type
'Class;
1974 Node
: Node_Access
);
1975 pragma Inline
(Write_Node
);
1978 new Tree_Operations
.Generic_Write
(Write_Node
);
1984 procedure Write_Node
1985 (Stream
: not null access Root_Stream_Type
'Class;
1989 Element_Type
'Output (Stream
, Node
.Element
.all);
1992 -- Start of processing for Write
1995 Write
(Stream
, Container
.Tree
);
1999 (Stream
: not null access Root_Stream_Type
'Class;
2003 raise Program_Error
with "attempt to stream set cursor";
2007 (Stream
: not null access Root_Stream_Type
'Class;
2008 Item
: Constant_Reference_Type
)
2011 raise Program_Error
with "attempt to stream reference";
2013 end Ada
.Containers
.Indefinite_Ordered_Multisets
;