1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.INDEFINITE_ORDERED_MULTISETS --
9 -- Copyright (C) 2004-2011, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- This unit was originally developed by Matthew J Heaney. --
28 ------------------------------------------------------------------------------
30 with Ada
.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 type Iterator
is new Limited_Controlled
and
46 Set_Iterator_Interfaces
.Reversible_Iterator
with
48 Container
: Set_Access
;
52 overriding
procedure Finalize
(Object
: in out Iterator
);
54 overriding
function First
(Object
: Iterator
) return Cursor
;
55 overriding
function Last
(Object
: Iterator
) return Cursor
;
57 overriding
function Next
59 Position
: Cursor
) return Cursor
;
61 overriding
function Previous
63 Position
: Cursor
) return Cursor
;
65 -----------------------------
66 -- Node Access Subprograms --
67 -----------------------------
69 -- These subprograms provide a functional interface to access fields
70 -- of a node, and a procedural interface for modifying these values.
72 function Color
(Node
: Node_Access
) return Color_Type
;
73 pragma Inline
(Color
);
75 function Left
(Node
: Node_Access
) return Node_Access
;
78 function Parent
(Node
: Node_Access
) return Node_Access
;
79 pragma Inline
(Parent
);
81 function Right
(Node
: Node_Access
) return Node_Access
;
82 pragma Inline
(Right
);
84 procedure Set_Parent
(Node
: Node_Access
; Parent
: Node_Access
);
85 pragma Inline
(Set_Parent
);
87 procedure Set_Left
(Node
: Node_Access
; Left
: Node_Access
);
88 pragma Inline
(Set_Left
);
90 procedure Set_Right
(Node
: Node_Access
; Right
: Node_Access
);
91 pragma Inline
(Set_Right
);
93 procedure Set_Color
(Node
: Node_Access
; Color
: Color_Type
);
94 pragma Inline
(Set_Color
);
96 -----------------------
97 -- Local Subprograms --
98 -----------------------
100 function Copy_Node
(Source
: Node_Access
) return Node_Access
;
101 pragma Inline
(Copy_Node
);
103 procedure Free
(X
: in out Node_Access
);
105 procedure Insert_Sans_Hint
106 (Tree
: in out Tree_Type
;
107 New_Item
: Element_Type
;
108 Node
: out Node_Access
);
110 procedure Insert_With_Hint
111 (Dst_Tree
: in out Tree_Type
;
112 Dst_Hint
: Node_Access
;
113 Src_Node
: Node_Access
;
114 Dst_Node
: out Node_Access
);
116 function Is_Equal_Node_Node
(L
, R
: Node_Access
) return Boolean;
117 pragma Inline
(Is_Equal_Node_Node
);
119 function Is_Greater_Element_Node
120 (Left
: Element_Type
;
121 Right
: Node_Access
) return Boolean;
122 pragma Inline
(Is_Greater_Element_Node
);
124 function Is_Less_Element_Node
125 (Left
: Element_Type
;
126 Right
: Node_Access
) return Boolean;
127 pragma Inline
(Is_Less_Element_Node
);
129 function Is_Less_Node_Node
(L
, R
: Node_Access
) return Boolean;
130 pragma Inline
(Is_Less_Node_Node
);
132 procedure Replace_Element
133 (Tree
: in out Tree_Type
;
135 Item
: Element_Type
);
137 --------------------------
138 -- Local Instantiations --
139 --------------------------
141 package Tree_Operations
is
142 new Red_Black_Trees
.Generic_Operations
(Tree_Types
);
144 procedure Delete_Tree
is
145 new Tree_Operations
.Generic_Delete_Tree
(Free
);
147 function Copy_Tree
is
148 new Tree_Operations
.Generic_Copy_Tree
(Copy_Node
, Delete_Tree
);
152 procedure Free_Element
is
153 new Ada
.Unchecked_Deallocation
(Element_Type
, Element_Access
);
156 new Tree_Operations
.Generic_Equal
(Is_Equal_Node_Node
);
159 new Generic_Set_Operations
160 (Tree_Operations
=> Tree_Operations
,
161 Insert_With_Hint
=> Insert_With_Hint
,
162 Copy_Tree
=> Copy_Tree
,
163 Delete_Tree
=> Delete_Tree
,
164 Is_Less
=> Is_Less_Node_Node
,
167 package Element_Keys
is
168 new Red_Black_Trees
.Generic_Keys
169 (Tree_Operations
=> Tree_Operations
,
170 Key_Type
=> Element_Type
,
171 Is_Less_Key_Node
=> Is_Less_Element_Node
,
172 Is_Greater_Key_Node
=> Is_Greater_Element_Node
);
178 function "<" (Left
, Right
: Cursor
) return Boolean is
180 if Left
.Node
= null then
181 raise Constraint_Error
with "Left cursor equals No_Element";
184 if Right
.Node
= null then
185 raise Constraint_Error
with "Right cursor equals No_Element";
188 if Left
.Node
.Element
= null then
189 raise Program_Error
with "Left cursor is bad";
192 if Right
.Node
.Element
= null then
193 raise Program_Error
with "Right cursor is bad";
196 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
197 "bad Left cursor in ""<""");
199 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
200 "bad Right cursor in ""<""");
202 return Left
.Node
.Element
.all < Right
.Node
.Element
.all;
205 function "<" (Left
: Cursor
; Right
: Element_Type
) return Boolean is
207 if Left
.Node
= null then
208 raise Constraint_Error
with "Left cursor equals No_Element";
211 if Left
.Node
.Element
= null then
212 raise Program_Error
with "Left cursor is bad";
215 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
216 "bad Left cursor in ""<""");
218 return Left
.Node
.Element
.all < Right
;
221 function "<" (Left
: Element_Type
; Right
: Cursor
) return Boolean is
223 if Right
.Node
= null then
224 raise Constraint_Error
with "Right cursor equals No_Element";
227 if Right
.Node
.Element
= null then
228 raise Program_Error
with "Right cursor is bad";
231 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
232 "bad Right cursor in ""<""");
234 return Left
< Right
.Node
.Element
.all;
241 function "=" (Left
, Right
: Set
) return Boolean is
243 return Is_Equal
(Left
.Tree
, Right
.Tree
);
250 function ">" (Left
, Right
: Cursor
) return Boolean is
252 if Left
.Node
= null then
253 raise Constraint_Error
with "Left cursor equals No_Element";
256 if Right
.Node
= null then
257 raise Constraint_Error
with "Right cursor equals No_Element";
260 if Left
.Node
.Element
= null then
261 raise Program_Error
with "Left cursor is bad";
264 if Right
.Node
.Element
= null then
265 raise Program_Error
with "Right cursor is bad";
268 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
269 "bad Left cursor in "">""");
271 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
272 "bad Right cursor in "">""");
274 -- L > R same as R < L
276 return Right
.Node
.Element
.all < Left
.Node
.Element
.all;
279 function ">" (Left
: Cursor
; Right
: Element_Type
) return Boolean is
281 if Left
.Node
= null then
282 raise Constraint_Error
with "Left cursor equals No_Element";
285 if Left
.Node
.Element
= null then
286 raise Program_Error
with "Left cursor is bad";
289 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
290 "bad Left cursor in "">""");
292 return Right
< Left
.Node
.Element
.all;
295 function ">" (Left
: Element_Type
; Right
: Cursor
) return Boolean is
297 if Right
.Node
= null then
298 raise Constraint_Error
with "Right cursor equals No_Element";
301 if Right
.Node
.Element
= null then
302 raise Program_Error
with "Right cursor is bad";
305 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
306 "bad Right cursor in "">""");
308 return Right
.Node
.Element
.all < Left
;
316 new Tree_Operations
.Generic_Adjust
(Copy_Tree
);
318 procedure Adjust
(Container
: in out Set
) is
320 Adjust
(Container
.Tree
);
327 procedure Assign
(Target
: in out Set
; Source
: Set
) is
329 if Target
'Address = Source
'Address then
334 Target
.Union
(Source
);
341 function Ceiling
(Container
: Set
; Item
: Element_Type
) return Cursor
is
342 Node
: constant Node_Access
:=
343 Element_Keys
.Ceiling
(Container
.Tree
, Item
);
350 return Cursor
'(Container'Unrestricted_Access, Node);
358 new Tree_Operations.Generic_Clear (Delete_Tree);
360 procedure Clear (Container : in out Set) is
362 Clear (Container.Tree);
369 function Color (Node : Node_Access) return Color_Type is
378 function Contains (Container : Set; Item : Element_Type) return Boolean is
380 return Find (Container, Item) /= No_Element;
387 function Copy (Source : Set) return Set is
389 return Target : Set do
390 Target.Assign (Source);
398 function Copy_Node (Source : Node_Access) return Node_Access is
399 X : Element_Access := new Element_Type'(Source
.Element
.all);
402 return new Node_Type
'(Parent => null,
405 Color => Source.Color,
418 procedure Delete (Container : in out Set; Item : Element_Type) is
419 Tree : Tree_Type renames Container.Tree;
420 Node : Node_Access := Element_Keys.Ceiling (Tree, Item);
421 Done : constant Node_Access := Element_Keys.Upper_Bound (Tree, Item);
426 raise Constraint_Error with "attempt to delete element not in set";
431 Node := Tree_Operations.Next (Node);
432 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
435 exit when Node = Done;
439 procedure Delete (Container : in out Set; Position : in out Cursor) is
441 if Position.Node = null then
442 raise Constraint_Error with "Position cursor equals No_Element";
445 if Position.Node.Element = null then
446 raise Program_Error with "Position cursor is bad";
449 if Position.Container /= Container'Unrestricted_Access then
450 raise Program_Error with "Position cursor designates wrong set";
453 pragma Assert (Vet (Container.Tree, Position.Node),
454 "bad cursor in Delete");
456 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
457 Free (Position.Node);
459 Position.Container := null;
466 procedure Delete_First (Container : in out Set) is
467 Tree : Tree_Type renames Container.Tree;
468 X : Node_Access := Tree.First;
475 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
483 procedure Delete_Last (Container : in out Set) is
484 Tree : Tree_Type renames Container.Tree;
485 X : Node_Access := Tree.Last;
492 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
500 procedure Difference (Target : in out Set; Source : Set) is
502 Set_Ops.Difference (Target.Tree, Source.Tree);
505 function Difference (Left, Right : Set) return Set is
506 Tree : constant Tree_Type :=
507 Set_Ops.Difference (Left.Tree, Right.Tree);
509 return Set'(Controlled
with Tree
);
516 function Element
(Position
: Cursor
) return Element_Type
is
518 if Position
.Node
= null then
519 raise Constraint_Error
with "Position cursor equals No_Element";
522 if Position
.Node
.Element
= null then
523 raise Program_Error
with "Position cursor is bad";
526 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
527 "bad cursor in Element");
529 return Position
.Node
.Element
.all;
532 -------------------------
533 -- Equivalent_Elements --
534 -------------------------
536 function Equivalent_Elements
(Left
, Right
: Element_Type
) return Boolean is
545 end Equivalent_Elements
;
547 ---------------------
548 -- Equivalent_Sets --
549 ---------------------
551 function Equivalent_Sets
(Left
, Right
: Set
) return Boolean is
553 function Is_Equivalent_Node_Node
(L
, R
: Node_Access
) return Boolean;
554 pragma Inline
(Is_Equivalent_Node_Node
);
556 function Is_Equivalent
is
557 new Tree_Operations
.Generic_Equal
(Is_Equivalent_Node_Node
);
559 -----------------------------
560 -- Is_Equivalent_Node_Node --
561 -----------------------------
563 function Is_Equivalent_Node_Node
(L
, R
: Node_Access
) return Boolean is
565 if L
.Element
.all < R
.Element
.all then
567 elsif R
.Element
.all < L
.Element
.all then
572 end Is_Equivalent_Node_Node
;
574 -- Start of processing for Equivalent_Sets
577 return Is_Equivalent
(Left
.Tree
, Right
.Tree
);
584 procedure Exclude
(Container
: in out Set
; Item
: Element_Type
) is
585 Tree
: Tree_Type
renames Container
.Tree
;
586 Node
: Node_Access
:= Element_Keys
.Ceiling
(Tree
, Item
);
587 Done
: constant Node_Access
:= Element_Keys
.Upper_Bound
(Tree
, Item
);
591 while Node
/= Done
loop
593 Node
:= Tree_Operations
.Next
(Node
);
594 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, X
);
603 function Find
(Container
: Set
; Item
: Element_Type
) return Cursor
is
604 Node
: constant Node_Access
:=
605 Element_Keys
.Find
(Container
.Tree
, Item
);
612 return Cursor
'(Container'Unrestricted_Access, Node);
619 procedure Finalize (Object : in out Iterator) is
620 B : Natural renames Object.Container.Tree.Busy;
621 pragma Assert (B > 0);
630 function First (Container : Set) return Cursor is
632 if Container.Tree.First = null then
636 return Cursor'(Container
'Unrestricted_Access, Container
.Tree
.First
);
639 function First
(Object
: Iterator
) return Cursor
is
641 -- The value of the iterator object's Node component influences the
642 -- behavior of the First (and Last) selector function.
644 -- When the Node component is null, this means the iterator object was
645 -- constructed without a start expression, in which case the (forward)
646 -- iteration starts from the (logical) beginning of the entire sequence
647 -- of items (corresponding to Container.First, for a forward iterator).
649 -- Otherwise, this is iteration over a partial sequence of items. When
650 -- the Node component is non-null, the iterator object was constructed
651 -- with a start expression, that specifies the position from which the
652 -- (forward) partial iteration begins.
654 if Object
.Node
= null then
655 return Object
.Container
.First
;
657 return Cursor
'(Object.Container, Object.Node);
665 function First_Element (Container : Set) return Element_Type is
667 if Container.Tree.First = null then
668 raise Constraint_Error with "set is empty";
671 pragma Assert (Container.Tree.First.Element /= null);
672 return Container.Tree.First.Element.all;
679 function Floor (Container : Set; Item : Element_Type) return Cursor is
680 Node : constant Node_Access :=
681 Element_Keys.Floor (Container.Tree, Item);
688 return Cursor'(Container
'Unrestricted_Access, Node
);
695 procedure Free
(X
: in out Node_Access
) is
696 procedure Deallocate
is
697 new Ada
.Unchecked_Deallocation
(Node_Type
, Node_Access
);
709 Free_Element
(X
.Element
);
724 package body Generic_Keys
is
726 -----------------------
727 -- Local Subprograms --
728 -----------------------
730 function Is_Less_Key_Node
732 Right
: Node_Access
) return Boolean;
733 pragma Inline
(Is_Less_Key_Node
);
735 function Is_Greater_Key_Node
737 Right
: Node_Access
) return Boolean;
738 pragma Inline
(Is_Greater_Key_Node
);
740 --------------------------
741 -- Local Instantiations --
742 --------------------------
745 new Red_Black_Trees
.Generic_Keys
746 (Tree_Operations
=> Tree_Operations
,
747 Key_Type
=> Key_Type
,
748 Is_Less_Key_Node
=> Is_Less_Key_Node
,
749 Is_Greater_Key_Node
=> Is_Greater_Key_Node
);
755 function Ceiling
(Container
: Set
; Key
: Key_Type
) return Cursor
is
756 Node
: constant Node_Access
:=
757 Key_Keys
.Ceiling
(Container
.Tree
, Key
);
764 return Cursor
'(Container'Unrestricted_Access, Node);
771 function Contains (Container : Set; Key : Key_Type) return Boolean is
773 return Find (Container, Key) /= No_Element;
780 procedure Delete (Container : in out Set; Key : Key_Type) is
781 Tree : Tree_Type renames Container.Tree;
782 Node : Node_Access := Key_Keys.Ceiling (Tree, Key);
783 Done : constant Node_Access := Key_Keys.Upper_Bound (Tree, Key);
788 raise Constraint_Error with "attempt to delete key not in set";
793 Node := Tree_Operations.Next (Node);
794 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
797 exit when Node = Done;
805 function Element (Container : Set; Key : Key_Type) return Element_Type is
806 Node : constant Node_Access :=
807 Key_Keys.Find (Container.Tree, Key);
811 raise Constraint_Error with "key not in set";
814 return Node.Element.all;
817 ---------------------
818 -- Equivalent_Keys --
819 ---------------------
821 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
836 procedure Exclude (Container : in out Set; Key : Key_Type) is
837 Tree : Tree_Type renames Container.Tree;
838 Node : Node_Access := Key_Keys.Ceiling (Tree, Key);
839 Done : constant Node_Access := Key_Keys.Upper_Bound (Tree, Key);
843 while Node /= Done loop
845 Node := Tree_Operations.Next (Node);
846 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
855 function Find (Container : Set; Key : Key_Type) return Cursor is
856 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
863 return Cursor'(Container
'Unrestricted_Access, Node
);
870 function Floor
(Container
: Set
; Key
: Key_Type
) return Cursor
is
871 Node
: constant Node_Access
:= Key_Keys
.Floor
(Container
.Tree
, Key
);
878 return Cursor
'(Container'Unrestricted_Access, Node);
881 -------------------------
882 -- Is_Greater_Key_Node --
883 -------------------------
885 function Is_Greater_Key_Node
887 Right : Node_Access) return Boolean
890 return Key (Right.Element.all) < Left;
891 end Is_Greater_Key_Node;
893 ----------------------
894 -- Is_Less_Key_Node --
895 ----------------------
897 function Is_Less_Key_Node
899 Right : Node_Access) return Boolean
902 return Left < Key (Right.Element.all);
903 end Is_Less_Key_Node;
912 Process : not null access procedure (Position : Cursor))
914 procedure Process_Node (Node : Node_Access);
915 pragma Inline (Process_Node);
917 procedure Local_Iterate is
918 new Key_Keys.Generic_Iteration (Process_Node);
924 procedure Process_Node (Node : Node_Access) is
926 Process (Cursor'(Container
'Unrestricted_Access, Node
));
929 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
930 B
: Natural renames T
.Busy
;
932 -- Start of processing for Iterate
938 Local_Iterate
(T
, Key
);
952 function Key
(Position
: Cursor
) return Key_Type
is
954 if Position
.Node
= null then
955 raise Constraint_Error
with
956 "Position cursor equals No_Element";
959 if Position
.Node
.Element
= null then
960 raise Program_Error
with
961 "Position cursor is bad";
964 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
965 "bad cursor in Key");
967 return Key
(Position
.Node
.Element
.all);
970 ---------------------
971 -- Reverse_Iterate --
972 ---------------------
974 procedure Reverse_Iterate
977 Process
: not null access procedure (Position
: Cursor
))
979 procedure Process_Node
(Node
: Node_Access
);
980 pragma Inline
(Process_Node
);
986 procedure Local_Reverse_Iterate
is
987 new Key_Keys
.Generic_Reverse_Iteration
(Process_Node
);
993 procedure Process_Node
(Node
: Node_Access
) is
995 Process
(Cursor
'(Container'Unrestricted_Access, Node));
998 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
999 B : Natural renames T.Busy;
1001 -- Start of processing for Reverse_Iterate
1007 Local_Reverse_Iterate (T, Key);
1015 end Reverse_Iterate;
1017 --------------------
1018 -- Update_Element --
1019 --------------------
1021 procedure Update_Element
1022 (Container : in out Set;
1024 Process : not null access procedure (Element : in out Element_Type))
1026 Tree : Tree_Type renames Container.Tree;
1027 Node : constant Node_Access := Position.Node;
1031 raise Constraint_Error with "Position cursor equals No_Element";
1034 if Node.Element = null then
1035 raise Program_Error with "Position cursor is bad";
1038 if Position.Container /= Container'Unrestricted_Access then
1039 raise Program_Error with "Position cursor designates wrong set";
1042 pragma Assert (Vet (Tree, Node),
1043 "bad cursor in Update_Element");
1046 E : Element_Type renames Node.Element.all;
1047 K : constant Key_Type := Key (E);
1049 B : Natural renames Tree.Busy;
1050 L : Natural renames Tree.Lock;
1068 if Equivalent_Keys (Left => K, Right => Key (E)) then
1073 -- Delete_Node checks busy-bit
1075 Tree_Operations.Delete_Node_Sans_Free (Tree, Node);
1077 Insert_New_Item : declare
1078 function New_Node return Node_Access;
1079 pragma Inline (New_Node);
1081 procedure Insert_Post is
1082 new Element_Keys.Generic_Insert_Post (New_Node);
1084 procedure Unconditional_Insert is
1085 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1091 function New_Node return Node_Access is
1093 Node.Color := Red_Black_Trees.Red;
1094 Node.Parent := null;
1101 Result : Node_Access;
1103 -- Start of processing for Insert_New_Item
1106 Unconditional_Insert
1108 Key => Node.Element.all,
1111 pragma Assert (Result = Node);
1112 end Insert_New_Item;
1121 function Has_Element (Position : Cursor) return Boolean is
1123 return Position /= No_Element;
1130 procedure Insert (Container : in out Set; New_Item : Element_Type) is
1132 pragma Unreferenced (Position);
1134 Insert (Container, New_Item, Position);
1138 (Container : in out Set;
1139 New_Item : Element_Type;
1140 Position : out Cursor)
1143 Insert_Sans_Hint (Container.Tree, New_Item, Position.Node);
1144 Position.Container := Container'Unrestricted_Access;
1147 ----------------------
1148 -- Insert_Sans_Hint --
1149 ----------------------
1151 procedure Insert_Sans_Hint
1152 (Tree : in out Tree_Type;
1153 New_Item : Element_Type;
1154 Node : out Node_Access)
1156 function New_Node return Node_Access;
1157 pragma Inline (New_Node);
1159 procedure Insert_Post is
1160 new Element_Keys.Generic_Insert_Post (New_Node);
1162 procedure Unconditional_Insert is
1163 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1169 function New_Node return Node_Access is
1170 Element : Element_Access := new Element_Type'(New_Item
);
1173 return new Node_Type
'(Parent => null,
1176 Color => Red_Black_Trees.Red,
1177 Element => Element);
1180 Free_Element (Element);
1184 -- Start of processing for Insert_Sans_Hint
1187 Unconditional_Insert (Tree, New_Item, Node);
1188 end Insert_Sans_Hint;
1190 ----------------------
1191 -- Insert_With_Hint --
1192 ----------------------
1194 procedure Insert_With_Hint
1195 (Dst_Tree : in out Tree_Type;
1196 Dst_Hint : Node_Access;
1197 Src_Node : Node_Access;
1198 Dst_Node : out Node_Access)
1200 function New_Node return Node_Access;
1201 pragma Inline (New_Node);
1203 procedure Insert_Post is
1204 new Element_Keys.Generic_Insert_Post (New_Node);
1206 procedure Insert_Sans_Hint is
1207 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1209 procedure Local_Insert_With_Hint is
1210 new Element_Keys.Generic_Unconditional_Insert_With_Hint
1218 function New_Node return Node_Access is
1219 X : Element_Access := new Element_Type'(Src_Node
.Element
.all);
1222 return new Node_Type
'(Parent => null,
1234 -- Start of processing for Insert_With_Hint
1237 Local_Insert_With_Hint
1240 Src_Node.Element.all,
1242 end Insert_With_Hint;
1248 procedure Intersection (Target : in out Set; Source : Set) is
1250 Set_Ops.Intersection (Target.Tree, Source.Tree);
1253 function Intersection (Left, Right : Set) return Set is
1254 Tree : constant Tree_Type :=
1255 Set_Ops.Intersection (Left.Tree, Right.Tree);
1257 return Set'(Controlled
with Tree
);
1264 function Is_Empty
(Container
: Set
) return Boolean is
1266 return Container
.Tree
.Length
= 0;
1269 ------------------------
1270 -- Is_Equal_Node_Node --
1271 ------------------------
1273 function Is_Equal_Node_Node
(L
, R
: Node_Access
) return Boolean is
1275 return L
.Element
.all = R
.Element
.all;
1276 end Is_Equal_Node_Node
;
1278 -----------------------------
1279 -- Is_Greater_Element_Node --
1280 -----------------------------
1282 function Is_Greater_Element_Node
1283 (Left
: Element_Type
;
1284 Right
: Node_Access
) return Boolean
1287 -- e > node same as node < e
1289 return Right
.Element
.all < Left
;
1290 end Is_Greater_Element_Node
;
1292 --------------------------
1293 -- Is_Less_Element_Node --
1294 --------------------------
1296 function Is_Less_Element_Node
1297 (Left
: Element_Type
;
1298 Right
: Node_Access
) return Boolean
1301 return Left
< Right
.Element
.all;
1302 end Is_Less_Element_Node
;
1304 -----------------------
1305 -- Is_Less_Node_Node --
1306 -----------------------
1308 function Is_Less_Node_Node
(L
, R
: Node_Access
) return Boolean is
1310 return L
.Element
.all < R
.Element
.all;
1311 end Is_Less_Node_Node
;
1317 function Is_Subset
(Subset
: Set
; Of_Set
: Set
) return Boolean is
1319 return Set_Ops
.Is_Subset
(Subset
=> Subset
.Tree
, Of_Set
=> Of_Set
.Tree
);
1328 Item
: Element_Type
;
1329 Process
: not null access procedure (Position
: Cursor
))
1331 procedure Process_Node
(Node
: Node_Access
);
1332 pragma Inline
(Process_Node
);
1334 procedure Local_Iterate
is
1335 new Element_Keys
.Generic_Iteration
(Process_Node
);
1341 procedure Process_Node
(Node
: Node_Access
) is
1343 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1346 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1347 B : Natural renames T.Busy;
1349 -- Start of processing for Iterate
1355 Local_Iterate (T, Item);
1367 Process : not null access procedure (Position : Cursor))
1369 procedure Process_Node (Node : Node_Access);
1370 pragma Inline (Process_Node);
1372 procedure Local_Iterate is
1373 new Tree_Operations.Generic_Iteration (Process_Node);
1379 procedure Process_Node (Node : Node_Access) is
1381 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1384 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
1385 B
: Natural renames T
.Busy
;
1387 -- Start of processing for Iterate
1403 function Iterate
(Container
: Set
)
1404 return Set_Iterator_Interfaces
.Reversible_Iterator
'Class
1406 S
: constant Set_Access
:= Container
'Unrestricted_Access;
1407 B
: Natural renames S
.Tree
.Busy
;
1410 -- The value of the Node component influences the behavior of the First
1411 -- and Last selector functions of the iterator object. When the Node
1412 -- component is null (as is the case here), this means the iterator
1413 -- object was constructed without a start expression. This is a complete
1414 -- iterator, meaning that the iteration starts from the (logical)
1415 -- beginning of the sequence of items.
1417 -- Note: For a forward iterator, Container.First is the beginning, and
1418 -- for a reverse iterator, Container.Last is the beginning.
1420 return It
: constant Iterator
:= (Limited_Controlled
with S
, null) do
1425 function Iterate
(Container
: Set
; Start
: Cursor
)
1426 return Set_Iterator_Interfaces
.Reversible_Iterator
'Class
1428 S
: constant Set_Access
:= Container
'Unrestricted_Access;
1429 B
: Natural renames S
.Tree
.Busy
;
1432 -- It was formerly the case that when Start = No_Element, the partial
1433 -- iterator was defined to behave the same as for a complete iterator,
1434 -- and iterate over the entire sequence of items. However, those
1435 -- semantics were unintuitive and arguably error-prone (it is too easy
1436 -- to accidentally create an endless loop), and so they were changed,
1437 -- per the ARG meeting in Denver on 2011/11. However, there was no
1438 -- consensus about what positive meaning this corner case should have,
1439 -- and so it was decided to simply raise an exception. This does imply,
1440 -- however, that it is not possible to use a partial iterator to specify
1441 -- an empty sequence of items.
1443 if Start
= No_Element
then
1444 raise Constraint_Error
with
1445 "Start position for iterator equals No_Element";
1448 if Start
.Container
/= Container
'Unrestricted_Access then
1449 raise Program_Error
with
1450 "Start cursor of Iterate designates wrong set";
1453 pragma Assert
(Vet
(Container
.Tree
, Start
.Node
),
1454 "Start cursor of Iterate is bad");
1456 -- The value of the Node component influences the behavior of the First
1457 -- and Last selector functions of the iterator object. When the Node
1458 -- component is non-null (as is the case here), it means that this is a
1459 -- partial iteration, over a subset of the complete sequence of
1460 -- items. The iterator object was constructed with a start expression,
1461 -- indicating the position from which the iteration begins. Note that
1462 -- the start position has the same value irrespective of whether this is
1463 -- a forward or reverse iteration.
1465 return It
: constant Iterator
:=
1466 (Limited_Controlled
with S
, Start
.Node
)
1476 function Last
(Container
: Set
) return Cursor
is
1478 if Container
.Tree
.Last
= null then
1482 return Cursor
'(Container'Unrestricted_Access, Container.Tree.Last);
1485 function Last (Object : Iterator) return Cursor is
1487 -- The value of the iterator object's Node component influences the
1488 -- behavior of the Last (and First) selector function.
1490 -- When the Node component is null, this means the iterator object was
1491 -- constructed without a start expression, in which case the (reverse)
1492 -- iteration starts from the (logical) beginning of the entire sequence
1493 -- (corresponding to Container.Last, for a reverse iterator).
1495 -- Otherwise, this is iteration over a partial sequence of items. When
1496 -- the Node component is non-null, the iterator object was constructed
1497 -- with a start expression, that specifies the position from which the
1498 -- (reverse) partial iteration begins.
1500 if Object.Node = null then
1501 return Object.Container.Last;
1503 return Cursor'(Object
.Container
, Object
.Node
);
1511 function Last_Element
(Container
: Set
) return Element_Type
is
1513 if Container
.Tree
.Last
= null then
1514 raise Constraint_Error
with "set is empty";
1517 pragma Assert
(Container
.Tree
.Last
.Element
/= null);
1518 return Container
.Tree
.Last
.Element
.all;
1525 function Left
(Node
: Node_Access
) return Node_Access
is
1534 function Length
(Container
: Set
) return Count_Type
is
1536 return Container
.Tree
.Length
;
1544 new Tree_Operations
.Generic_Move
(Clear
);
1546 procedure Move
(Target
: in out Set
; Source
: in out Set
) is
1548 Move
(Target
=> Target
.Tree
, Source
=> Source
.Tree
);
1555 function Next
(Position
: Cursor
) return Cursor
is
1557 if Position
= No_Element
then
1561 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
1562 "bad cursor in Next");
1565 Node
: constant Node_Access
:=
1566 Tree_Operations
.Next
(Position
.Node
);
1573 return Cursor
'(Position.Container, Node);
1577 procedure Next (Position : in out Cursor) is
1579 Position := Next (Position);
1582 function Next (Object : Iterator; Position : Cursor) return Cursor is
1584 if Position.Container = null then
1588 if Position.Container /= Object.Container then
1589 raise Program_Error with
1590 "Position cursor of Next designates wrong set";
1593 return Next (Position);
1600 function Overlap (Left, Right : Set) return Boolean is
1602 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1609 function Parent (Node : Node_Access) return Node_Access is
1618 function Previous (Position : Cursor) return Cursor is
1620 if Position = No_Element then
1624 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1625 "bad cursor in Previous");
1628 Node : constant Node_Access :=
1629 Tree_Operations.Previous (Position.Node);
1636 return Cursor'(Position
.Container
, Node
);
1640 procedure Previous
(Position
: in out Cursor
) is
1642 Position
:= Previous
(Position
);
1645 function Previous
(Object
: Iterator
; Position
: Cursor
) return Cursor
is
1647 if Position
.Container
= null then
1651 if Position
.Container
/= Object
.Container
then
1652 raise Program_Error
with
1653 "Position cursor of Previous designates wrong set";
1656 return Previous
(Position
);
1663 procedure Query_Element
1665 Process
: not null access procedure (Element
: Element_Type
))
1668 if Position
.Node
= null then
1669 raise Constraint_Error
with "Position cursor equals No_Element";
1672 if Position
.Node
.Element
= null then
1673 raise Program_Error
with "Position cursor is bad";
1676 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
1677 "bad cursor in Query_Element");
1680 T
: Tree_Type
renames Position
.Container
.Tree
;
1682 B
: Natural renames T
.Busy
;
1683 L
: Natural renames T
.Lock
;
1690 Process
(Position
.Node
.Element
.all);
1708 (Stream
: not null access Root_Stream_Type
'Class;
1709 Container
: out Set
)
1712 (Stream
: not null access Root_Stream_Type
'Class) return Node_Access
;
1713 pragma Inline
(Read_Node
);
1716 new Tree_Operations
.Generic_Read
(Clear
, Read_Node
);
1723 (Stream
: not null access Root_Stream_Type
'Class) return Node_Access
1725 Node
: Node_Access
:= new Node_Type
;
1727 Node
.Element
:= new Element_Type
'(Element_Type'Input (Stream));
1731 Free (Node); -- Note that Free deallocates elem too
1735 -- Start of processing for Read
1738 Read (Stream, Container.Tree);
1742 (Stream : not null access Root_Stream_Type'Class;
1746 raise Program_Error with "attempt to stream set cursor";
1749 ---------------------
1750 -- Replace_Element --
1751 ---------------------
1753 procedure Replace_Element
1754 (Tree : in out Tree_Type;
1756 Item : Element_Type)
1759 if Item < Node.Element.all
1760 or else Node.Element.all < Item
1764 if Tree.Lock > 0 then
1765 raise Program_Error with
1766 "attempt to tamper with elements (set is locked)";
1770 X : Element_Access := Node.Element;
1772 Node.Element := new Element_Type'(Item
);
1779 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, Node
); -- Checks busy-bit
1781 Insert_New_Item
: declare
1782 function New_Node
return Node_Access
;
1783 pragma Inline
(New_Node
);
1785 procedure Insert_Post
is
1786 new Element_Keys
.Generic_Insert_Post
(New_Node
);
1788 procedure Unconditional_Insert
is
1789 new Element_Keys
.Generic_Unconditional_Insert
(Insert_Post
);
1795 function New_Node
return Node_Access
is
1797 Node
.Element
:= new Element_Type
'(Item); -- OK if fails
1798 Node.Color := Red_Black_Trees.Red;
1799 Node.Parent := null;
1806 Result : Node_Access;
1808 X : Element_Access := Node.Element;
1810 -- Start of processing for Insert_New_Item
1813 Unconditional_Insert
1817 pragma Assert (Result = Node);
1819 Free_Element (X); -- OK if fails
1820 end Insert_New_Item;
1821 end Replace_Element;
1823 procedure Replace_Element
1824 (Container : in out Set;
1826 New_Item : Element_Type)
1829 if Position.Node = null then
1830 raise Constraint_Error with "Position cursor equals No_Element";
1833 if Position.Node.Element = null then
1834 raise Program_Error with "Position cursor is bad";
1837 if Position.Container /= Container'Unrestricted_Access then
1838 raise Program_Error with "Position cursor designates wrong set";
1841 pragma Assert (Vet (Container.Tree, Position.Node),
1842 "bad cursor in Replace_Element");
1844 Replace_Element (Container.Tree, Position.Node, New_Item);
1845 end Replace_Element;
1847 ---------------------
1848 -- Reverse_Iterate --
1849 ---------------------
1851 procedure Reverse_Iterate
1853 Item : Element_Type;
1854 Process : not null access procedure (Position : Cursor))
1856 procedure Process_Node (Node : Node_Access);
1857 pragma Inline (Process_Node);
1859 procedure Local_Reverse_Iterate is
1860 new Element_Keys.Generic_Reverse_Iteration (Process_Node);
1866 procedure Process_Node (Node : Node_Access) is
1868 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1871 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
1872 B
: Natural renames T
.Busy
;
1874 -- Start of processing for Reverse_Iterate
1880 Local_Reverse_Iterate
(T
, Item
);
1888 end Reverse_Iterate
;
1890 procedure Reverse_Iterate
1892 Process
: not null access procedure (Position
: Cursor
))
1894 procedure Process_Node
(Node
: Node_Access
);
1895 pragma Inline
(Process_Node
);
1897 procedure Local_Reverse_Iterate
is
1898 new Tree_Operations
.Generic_Reverse_Iteration
(Process_Node
);
1904 procedure Process_Node
(Node
: Node_Access
) is
1906 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1909 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1910 B : Natural renames T.Busy;
1912 -- Start of processing for Reverse_Iterate
1918 Local_Reverse_Iterate (T);
1926 end Reverse_Iterate;
1932 function Right (Node : Node_Access) return Node_Access is
1941 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1943 Node.Color := Color;
1950 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1959 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1961 Node.Parent := Parent;
1968 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1970 Node.Right := Right;
1973 --------------------------
1974 -- Symmetric_Difference --
1975 --------------------------
1977 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1979 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
1980 end Symmetric_Difference;
1982 function Symmetric_Difference (Left, Right : Set) return Set is
1983 Tree : constant Tree_Type :=
1984 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
1986 return Set'(Controlled
with Tree
);
1987 end Symmetric_Difference
;
1993 function To_Set
(New_Item
: Element_Type
) return Set
is
1996 pragma Unreferenced
(Node
);
1998 Insert_Sans_Hint
(Tree
, New_Item
, Node
);
1999 return Set
'(Controlled with Tree);
2006 procedure Union (Target : in out Set; Source : Set) is
2008 Set_Ops.Union (Target.Tree, Source.Tree);
2011 function Union (Left, Right : Set) return Set is
2012 Tree : constant Tree_Type :=
2013 Set_Ops.Union (Left.Tree, Right.Tree);
2015 return Set'(Controlled
with Tree
);
2023 (Stream
: not null access Root_Stream_Type
'Class;
2026 procedure Write_Node
2027 (Stream
: not null access Root_Stream_Type
'Class;
2028 Node
: Node_Access
);
2029 pragma Inline
(Write_Node
);
2032 new Tree_Operations
.Generic_Write
(Write_Node
);
2038 procedure Write_Node
2039 (Stream
: not null access Root_Stream_Type
'Class;
2043 Element_Type
'Output (Stream
, Node
.Element
.all);
2046 -- Start of processing for Write
2049 Write
(Stream
, Container
.Tree
);
2053 (Stream
: not null access Root_Stream_Type
'Class;
2057 raise Program_Error
with "attempt to stream set cursor";
2060 end Ada
.Containers
.Indefinite_Ordered_Multisets
;