1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.INDEFINITE_ORDERED_MULTISETS --
9 -- Copyright (C) 2004-2013, 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 -----------------------------
46 -- Node Access Subprograms --
47 -----------------------------
49 -- These subprograms provide a functional interface to access fields
50 -- of a node, and a procedural interface for modifying these values.
52 function Color
(Node
: Node_Access
) return Color_Type
;
53 pragma Inline
(Color
);
55 function Left
(Node
: Node_Access
) return Node_Access
;
58 function Parent
(Node
: Node_Access
) return Node_Access
;
59 pragma Inline
(Parent
);
61 function Right
(Node
: Node_Access
) return Node_Access
;
62 pragma Inline
(Right
);
64 procedure Set_Parent
(Node
: Node_Access
; Parent
: Node_Access
);
65 pragma Inline
(Set_Parent
);
67 procedure Set_Left
(Node
: Node_Access
; Left
: Node_Access
);
68 pragma Inline
(Set_Left
);
70 procedure Set_Right
(Node
: Node_Access
; Right
: Node_Access
);
71 pragma Inline
(Set_Right
);
73 procedure Set_Color
(Node
: Node_Access
; Color
: Color_Type
);
74 pragma Inline
(Set_Color
);
76 -----------------------
77 -- Local Subprograms --
78 -----------------------
80 function Copy_Node
(Source
: Node_Access
) return Node_Access
;
81 pragma Inline
(Copy_Node
);
83 procedure Free
(X
: in out Node_Access
);
85 procedure Insert_Sans_Hint
86 (Tree
: in out Tree_Type
;
87 New_Item
: Element_Type
;
88 Node
: out Node_Access
);
90 procedure Insert_With_Hint
91 (Dst_Tree
: in out Tree_Type
;
92 Dst_Hint
: Node_Access
;
93 Src_Node
: Node_Access
;
94 Dst_Node
: out Node_Access
);
96 function Is_Equal_Node_Node
(L
, R
: Node_Access
) return Boolean;
97 pragma Inline
(Is_Equal_Node_Node
);
99 function Is_Greater_Element_Node
100 (Left
: Element_Type
;
101 Right
: Node_Access
) return Boolean;
102 pragma Inline
(Is_Greater_Element_Node
);
104 function Is_Less_Element_Node
105 (Left
: Element_Type
;
106 Right
: Node_Access
) return Boolean;
107 pragma Inline
(Is_Less_Element_Node
);
109 function Is_Less_Node_Node
(L
, R
: Node_Access
) return Boolean;
110 pragma Inline
(Is_Less_Node_Node
);
112 procedure Replace_Element
113 (Tree
: in out Tree_Type
;
115 Item
: Element_Type
);
117 --------------------------
118 -- Local Instantiations --
119 --------------------------
121 package Tree_Operations
is
122 new Red_Black_Trees
.Generic_Operations
(Tree_Types
);
124 procedure Delete_Tree
is
125 new Tree_Operations
.Generic_Delete_Tree
(Free
);
127 function Copy_Tree
is
128 new Tree_Operations
.Generic_Copy_Tree
(Copy_Node
, Delete_Tree
);
132 procedure Free_Element
is
133 new Ada
.Unchecked_Deallocation
(Element_Type
, Element_Access
);
136 new Tree_Operations
.Generic_Equal
(Is_Equal_Node_Node
);
139 new Generic_Set_Operations
140 (Tree_Operations
=> Tree_Operations
,
141 Insert_With_Hint
=> Insert_With_Hint
,
142 Copy_Tree
=> Copy_Tree
,
143 Delete_Tree
=> Delete_Tree
,
144 Is_Less
=> Is_Less_Node_Node
,
147 package Element_Keys
is
148 new Red_Black_Trees
.Generic_Keys
149 (Tree_Operations
=> Tree_Operations
,
150 Key_Type
=> Element_Type
,
151 Is_Less_Key_Node
=> Is_Less_Element_Node
,
152 Is_Greater_Key_Node
=> Is_Greater_Element_Node
);
158 function "<" (Left
, Right
: Cursor
) return Boolean is
160 if Left
.Node
= null then
161 raise Constraint_Error
with "Left cursor equals No_Element";
164 if Right
.Node
= null then
165 raise Constraint_Error
with "Right cursor equals No_Element";
168 if Left
.Node
.Element
= null then
169 raise Program_Error
with "Left cursor is bad";
172 if Right
.Node
.Element
= null then
173 raise Program_Error
with "Right cursor is bad";
176 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
177 "bad Left cursor in ""<""");
179 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
180 "bad Right cursor in ""<""");
182 return Left
.Node
.Element
.all < Right
.Node
.Element
.all;
185 function "<" (Left
: Cursor
; Right
: Element_Type
) return Boolean is
187 if Left
.Node
= null then
188 raise Constraint_Error
with "Left cursor equals No_Element";
191 if Left
.Node
.Element
= null then
192 raise Program_Error
with "Left cursor is bad";
195 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
196 "bad Left cursor in ""<""");
198 return Left
.Node
.Element
.all < Right
;
201 function "<" (Left
: Element_Type
; Right
: Cursor
) return Boolean is
203 if Right
.Node
= null then
204 raise Constraint_Error
with "Right cursor equals No_Element";
207 if Right
.Node
.Element
= null then
208 raise Program_Error
with "Right cursor is bad";
211 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
212 "bad Right cursor in ""<""");
214 return Left
< Right
.Node
.Element
.all;
221 function "=" (Left
, Right
: Set
) return Boolean is
223 return Is_Equal
(Left
.Tree
, Right
.Tree
);
230 function ">" (Left
, Right
: Cursor
) return Boolean is
232 if Left
.Node
= null then
233 raise Constraint_Error
with "Left cursor equals No_Element";
236 if Right
.Node
= null then
237 raise Constraint_Error
with "Right cursor equals No_Element";
240 if Left
.Node
.Element
= null then
241 raise Program_Error
with "Left cursor is bad";
244 if Right
.Node
.Element
= null then
245 raise Program_Error
with "Right cursor is bad";
248 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
249 "bad Left cursor in "">""");
251 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
252 "bad Right cursor in "">""");
254 -- L > R same as R < L
256 return Right
.Node
.Element
.all < Left
.Node
.Element
.all;
259 function ">" (Left
: Cursor
; Right
: Element_Type
) return Boolean is
261 if Left
.Node
= null then
262 raise Constraint_Error
with "Left cursor equals No_Element";
265 if Left
.Node
.Element
= null then
266 raise Program_Error
with "Left cursor is bad";
269 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
270 "bad Left cursor in "">""");
272 return Right
< Left
.Node
.Element
.all;
275 function ">" (Left
: Element_Type
; Right
: Cursor
) return Boolean is
277 if Right
.Node
= null then
278 raise Constraint_Error
with "Right cursor equals No_Element";
281 if Right
.Node
.Element
= null then
282 raise Program_Error
with "Right cursor is bad";
285 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
286 "bad Right cursor in "">""");
288 return Right
.Node
.Element
.all < Left
;
296 new Tree_Operations
.Generic_Adjust
(Copy_Tree
);
298 procedure Adjust
(Container
: in out Set
) is
300 Adjust
(Container
.Tree
);
307 procedure Assign
(Target
: in out Set
; Source
: Set
) is
309 if Target
'Address = Source
'Address then
314 Target
.Union
(Source
);
321 function Ceiling
(Container
: Set
; Item
: Element_Type
) return Cursor
is
322 Node
: constant Node_Access
:=
323 Element_Keys
.Ceiling
(Container
.Tree
, Item
);
330 return Cursor
'(Container'Unrestricted_Access, Node);
338 new Tree_Operations.Generic_Clear (Delete_Tree);
340 procedure Clear (Container : in out Set) is
342 Clear (Container.Tree);
349 function Color (Node : Node_Access) return Color_Type is
358 function Contains (Container : Set; Item : Element_Type) return Boolean is
360 return Find (Container, Item) /= No_Element;
367 function Copy (Source : Set) return Set is
369 return Target : Set do
370 Target.Assign (Source);
378 function Copy_Node (Source : Node_Access) return Node_Access is
379 X : Element_Access := new Element_Type'(Source
.Element
.all);
382 return new Node_Type
'(Parent => null,
385 Color => Source.Color,
398 procedure Delete (Container : in out Set; Item : Element_Type) is
399 Tree : Tree_Type renames Container.Tree;
400 Node : Node_Access := Element_Keys.Ceiling (Tree, Item);
401 Done : constant Node_Access := Element_Keys.Upper_Bound (Tree, Item);
406 raise Constraint_Error with "attempt to delete element not in set";
411 Node := Tree_Operations.Next (Node);
412 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
415 exit when Node = Done;
419 procedure Delete (Container : in out Set; Position : in out Cursor) is
421 if Position.Node = null then
422 raise Constraint_Error with "Position cursor equals No_Element";
425 if Position.Node.Element = null then
426 raise Program_Error with "Position cursor is bad";
429 if Position.Container /= Container'Unrestricted_Access then
430 raise Program_Error with "Position cursor designates wrong set";
433 pragma Assert (Vet (Container.Tree, Position.Node),
434 "bad cursor in Delete");
436 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
437 Free (Position.Node);
439 Position.Container := null;
446 procedure Delete_First (Container : in out Set) is
447 Tree : Tree_Type renames Container.Tree;
448 X : Node_Access := Tree.First;
455 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
463 procedure Delete_Last (Container : in out Set) is
464 Tree : Tree_Type renames Container.Tree;
465 X : Node_Access := Tree.Last;
472 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
480 procedure Difference (Target : in out Set; Source : Set) is
482 Set_Ops.Difference (Target.Tree, Source.Tree);
485 function Difference (Left, Right : Set) return Set is
486 Tree : constant Tree_Type := Set_Ops.Difference (Left.Tree, Right.Tree);
488 return Set'(Controlled
with Tree
);
495 function Element
(Position
: Cursor
) return Element_Type
is
497 if Position
.Node
= null then
498 raise Constraint_Error
with "Position cursor equals No_Element";
501 if Position
.Node
.Element
= null then
502 raise Program_Error
with "Position cursor is bad";
505 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
506 "bad cursor in Element");
508 return Position
.Node
.Element
.all;
511 -------------------------
512 -- Equivalent_Elements --
513 -------------------------
515 function Equivalent_Elements
(Left
, Right
: Element_Type
) return Boolean is
524 end Equivalent_Elements
;
526 ---------------------
527 -- Equivalent_Sets --
528 ---------------------
530 function Equivalent_Sets
(Left
, Right
: Set
) return Boolean is
532 function Is_Equivalent_Node_Node
(L
, R
: Node_Access
) return Boolean;
533 pragma Inline
(Is_Equivalent_Node_Node
);
535 function Is_Equivalent
is
536 new Tree_Operations
.Generic_Equal
(Is_Equivalent_Node_Node
);
538 -----------------------------
539 -- Is_Equivalent_Node_Node --
540 -----------------------------
542 function Is_Equivalent_Node_Node
(L
, R
: Node_Access
) return Boolean is
544 if L
.Element
.all < R
.Element
.all then
546 elsif R
.Element
.all < L
.Element
.all then
551 end Is_Equivalent_Node_Node
;
553 -- Start of processing for Equivalent_Sets
556 return Is_Equivalent
(Left
.Tree
, Right
.Tree
);
563 procedure Exclude
(Container
: in out Set
; Item
: Element_Type
) is
564 Tree
: Tree_Type
renames Container
.Tree
;
565 Node
: Node_Access
:= Element_Keys
.Ceiling
(Tree
, Item
);
566 Done
: constant Node_Access
:= Element_Keys
.Upper_Bound
(Tree
, Item
);
570 while Node
/= Done
loop
572 Node
:= Tree_Operations
.Next
(Node
);
573 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, X
);
582 function Find
(Container
: Set
; Item
: Element_Type
) return Cursor
is
583 Node
: constant Node_Access
:= Element_Keys
.Find
(Container
.Tree
, Item
);
590 return Cursor
'(Container'Unrestricted_Access, Node);
597 procedure Finalize (Object : in out Iterator) is
598 B : Natural renames Object.Container.Tree.Busy;
599 pragma Assert (B > 0);
608 function First (Container : Set) return Cursor is
610 if Container.Tree.First = null then
614 return Cursor'(Container
'Unrestricted_Access, Container
.Tree
.First
);
617 function First
(Object
: Iterator
) return Cursor
is
619 -- The value of the iterator object's Node component influences the
620 -- behavior of the First (and Last) selector function.
622 -- When the Node component is null, this means the iterator object was
623 -- constructed without a start expression, in which case the (forward)
624 -- iteration starts from the (logical) beginning of the entire sequence
625 -- of items (corresponding to Container.First, for a forward iterator).
627 -- Otherwise, this is iteration over a partial sequence of items. When
628 -- the Node component is non-null, the iterator object was constructed
629 -- with a start expression, that specifies the position from which the
630 -- (forward) partial iteration begins.
632 if Object
.Node
= null then
633 return Object
.Container
.First
;
635 return Cursor
'(Object.Container, Object.Node);
643 function First_Element (Container : Set) return Element_Type is
645 if Container.Tree.First = null then
646 raise Constraint_Error with "set is empty";
649 pragma Assert (Container.Tree.First.Element /= null);
650 return Container.Tree.First.Element.all;
657 function Floor (Container : Set; Item : Element_Type) return Cursor is
658 Node : constant Node_Access := Element_Keys.Floor (Container.Tree, Item);
665 return Cursor'(Container
'Unrestricted_Access, Node
);
672 procedure Free
(X
: in out Node_Access
) is
673 procedure Deallocate
is
674 new Ada
.Unchecked_Deallocation
(Node_Type
, Node_Access
);
686 Free_Element
(X
.Element
);
701 package body Generic_Keys
is
703 -----------------------
704 -- Local Subprograms --
705 -----------------------
707 function Is_Less_Key_Node
709 Right
: Node_Access
) return Boolean;
710 pragma Inline
(Is_Less_Key_Node
);
712 function Is_Greater_Key_Node
714 Right
: Node_Access
) return Boolean;
715 pragma Inline
(Is_Greater_Key_Node
);
717 --------------------------
718 -- Local Instantiations --
719 --------------------------
722 new Red_Black_Trees
.Generic_Keys
723 (Tree_Operations
=> Tree_Operations
,
724 Key_Type
=> Key_Type
,
725 Is_Less_Key_Node
=> Is_Less_Key_Node
,
726 Is_Greater_Key_Node
=> Is_Greater_Key_Node
);
732 function Ceiling
(Container
: Set
; Key
: Key_Type
) return Cursor
is
733 Node
: constant Node_Access
:= Key_Keys
.Ceiling
(Container
.Tree
, Key
);
740 return Cursor
'(Container'Unrestricted_Access, Node);
747 function Contains (Container : Set; Key : Key_Type) return Boolean is
749 return Find (Container, Key) /= No_Element;
756 procedure Delete (Container : in out Set; Key : Key_Type) is
757 Tree : Tree_Type renames Container.Tree;
758 Node : Node_Access := Key_Keys.Ceiling (Tree, Key);
759 Done : constant Node_Access := Key_Keys.Upper_Bound (Tree, Key);
764 raise Constraint_Error with "attempt to delete key not in set";
769 Node := Tree_Operations.Next (Node);
770 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
773 exit when Node = Done;
781 function Element (Container : Set; Key : Key_Type) return Element_Type is
782 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
786 raise Constraint_Error with "key not in set";
789 return Node.Element.all;
792 ---------------------
793 -- Equivalent_Keys --
794 ---------------------
796 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
811 procedure Exclude (Container : in out Set; Key : Key_Type) is
812 Tree : Tree_Type renames Container.Tree;
813 Node : Node_Access := Key_Keys.Ceiling (Tree, Key);
814 Done : constant Node_Access := Key_Keys.Upper_Bound (Tree, Key);
818 while Node /= Done loop
820 Node := Tree_Operations.Next (Node);
821 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
830 function Find (Container : Set; Key : Key_Type) return Cursor is
831 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
838 return Cursor'(Container
'Unrestricted_Access, Node
);
845 function Floor
(Container
: Set
; Key
: Key_Type
) return Cursor
is
846 Node
: constant Node_Access
:= Key_Keys
.Floor
(Container
.Tree
, Key
);
853 return Cursor
'(Container'Unrestricted_Access, Node);
856 -------------------------
857 -- Is_Greater_Key_Node --
858 -------------------------
860 function Is_Greater_Key_Node
862 Right : Node_Access) return Boolean
865 return Key (Right.Element.all) < Left;
866 end Is_Greater_Key_Node;
868 ----------------------
869 -- Is_Less_Key_Node --
870 ----------------------
872 function Is_Less_Key_Node
874 Right : Node_Access) return Boolean
877 return Left < Key (Right.Element.all);
878 end Is_Less_Key_Node;
887 Process : not null access procedure (Position : Cursor))
889 procedure Process_Node (Node : Node_Access);
890 pragma Inline (Process_Node);
892 procedure Local_Iterate is
893 new Key_Keys.Generic_Iteration (Process_Node);
899 procedure Process_Node (Node : Node_Access) is
901 Process (Cursor'(Container
'Unrestricted_Access, Node
));
904 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
905 B
: Natural renames T
.Busy
;
907 -- Start of processing for Iterate
913 Local_Iterate
(T
, Key
);
927 function Key
(Position
: Cursor
) return Key_Type
is
929 if Position
.Node
= null then
930 raise Constraint_Error
with
931 "Position cursor equals No_Element";
934 if Position
.Node
.Element
= null then
935 raise Program_Error
with
936 "Position cursor is bad";
939 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
940 "bad cursor in Key");
942 return Key
(Position
.Node
.Element
.all);
945 ---------------------
946 -- Reverse_Iterate --
947 ---------------------
949 procedure Reverse_Iterate
952 Process
: not null access procedure (Position
: Cursor
))
954 procedure Process_Node
(Node
: Node_Access
);
955 pragma Inline
(Process_Node
);
961 procedure Local_Reverse_Iterate
is
962 new Key_Keys
.Generic_Reverse_Iteration
(Process_Node
);
968 procedure Process_Node
(Node
: Node_Access
) is
970 Process
(Cursor
'(Container'Unrestricted_Access, Node));
973 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
974 B : Natural renames T.Busy;
976 -- Start of processing for Reverse_Iterate
982 Local_Reverse_Iterate (T, Key);
996 procedure Update_Element
997 (Container : in out Set;
999 Process : not null access procedure (Element : in out Element_Type))
1001 Tree : Tree_Type renames Container.Tree;
1002 Node : constant Node_Access := Position.Node;
1006 raise Constraint_Error with "Position cursor equals No_Element";
1009 if Node.Element = null then
1010 raise Program_Error with "Position cursor is bad";
1013 if Position.Container /= Container'Unrestricted_Access then
1014 raise Program_Error with "Position cursor designates wrong set";
1017 pragma Assert (Vet (Tree, Node),
1018 "bad cursor in Update_Element");
1021 E : Element_Type renames Node.Element.all;
1022 K : constant Key_Type := Key (E);
1024 B : Natural renames Tree.Busy;
1025 L : Natural renames Tree.Lock;
1043 if Equivalent_Keys (Left => K, Right => Key (E)) then
1048 -- Delete_Node checks busy-bit
1050 Tree_Operations.Delete_Node_Sans_Free (Tree, Node);
1052 Insert_New_Item : declare
1053 function New_Node return Node_Access;
1054 pragma Inline (New_Node);
1056 procedure Insert_Post is
1057 new Element_Keys.Generic_Insert_Post (New_Node);
1059 procedure Unconditional_Insert is
1060 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1066 function New_Node return Node_Access is
1068 Node.Color := Red_Black_Trees.Red;
1069 Node.Parent := null;
1076 Result : Node_Access;
1078 -- Start of processing for Insert_New_Item
1081 Unconditional_Insert
1083 Key => Node.Element.all,
1086 pragma Assert (Result = Node);
1087 end Insert_New_Item;
1096 function Has_Element (Position : Cursor) return Boolean is
1098 return Position /= No_Element;
1105 procedure Insert (Container : in out Set; New_Item : Element_Type) is
1107 pragma Unreferenced (Position);
1109 Insert (Container, New_Item, Position);
1113 (Container : in out Set;
1114 New_Item : Element_Type;
1115 Position : out Cursor)
1118 Insert_Sans_Hint (Container.Tree, New_Item, Position.Node);
1119 Position.Container := Container'Unrestricted_Access;
1122 ----------------------
1123 -- Insert_Sans_Hint --
1124 ----------------------
1126 procedure Insert_Sans_Hint
1127 (Tree : in out Tree_Type;
1128 New_Item : Element_Type;
1129 Node : out Node_Access)
1131 function New_Node return Node_Access;
1132 pragma Inline (New_Node);
1134 procedure Insert_Post is
1135 new Element_Keys.Generic_Insert_Post (New_Node);
1137 procedure Unconditional_Insert is
1138 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1144 function New_Node return Node_Access is
1145 -- The element allocator may need an accessibility check in the case
1146 -- the actual type is class-wide or has access discriminants (see
1147 -- RM 4.8(10.1) and AI12-0035).
1149 pragma Unsuppress (Accessibility_Check);
1151 Element : Element_Access := new Element_Type'(New_Item
);
1154 return new Node_Type
'(Parent => null,
1157 Color => Red_Black_Trees.Red,
1158 Element => Element);
1162 Free_Element (Element);
1166 -- Start of processing for Insert_Sans_Hint
1169 Unconditional_Insert (Tree, New_Item, Node);
1170 end Insert_Sans_Hint;
1172 ----------------------
1173 -- Insert_With_Hint --
1174 ----------------------
1176 procedure Insert_With_Hint
1177 (Dst_Tree : in out Tree_Type;
1178 Dst_Hint : Node_Access;
1179 Src_Node : Node_Access;
1180 Dst_Node : out Node_Access)
1182 function New_Node return Node_Access;
1183 pragma Inline (New_Node);
1185 procedure Insert_Post is
1186 new Element_Keys.Generic_Insert_Post (New_Node);
1188 procedure Insert_Sans_Hint is
1189 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1191 procedure Local_Insert_With_Hint is
1192 new Element_Keys.Generic_Unconditional_Insert_With_Hint
1200 function New_Node return Node_Access is
1201 X : Element_Access := new Element_Type'(Src_Node
.Element
.all);
1204 return new Node_Type
'(Parent => null,
1216 -- Start of processing for Insert_With_Hint
1219 Local_Insert_With_Hint
1222 Src_Node.Element.all,
1224 end Insert_With_Hint;
1230 procedure Intersection (Target : in out Set; Source : Set) is
1232 Set_Ops.Intersection (Target.Tree, Source.Tree);
1235 function Intersection (Left, Right : Set) return Set is
1236 Tree : constant Tree_Type :=
1237 Set_Ops.Intersection (Left.Tree, Right.Tree);
1239 return Set'(Controlled
with Tree
);
1246 function Is_Empty
(Container
: Set
) return Boolean is
1248 return Container
.Tree
.Length
= 0;
1251 ------------------------
1252 -- Is_Equal_Node_Node --
1253 ------------------------
1255 function Is_Equal_Node_Node
(L
, R
: Node_Access
) return Boolean is
1257 return L
.Element
.all = R
.Element
.all;
1258 end Is_Equal_Node_Node
;
1260 -----------------------------
1261 -- Is_Greater_Element_Node --
1262 -----------------------------
1264 function Is_Greater_Element_Node
1265 (Left
: Element_Type
;
1266 Right
: Node_Access
) return Boolean
1269 -- e > node same as node < e
1271 return Right
.Element
.all < Left
;
1272 end Is_Greater_Element_Node
;
1274 --------------------------
1275 -- Is_Less_Element_Node --
1276 --------------------------
1278 function Is_Less_Element_Node
1279 (Left
: Element_Type
;
1280 Right
: Node_Access
) return Boolean
1283 return Left
< Right
.Element
.all;
1284 end Is_Less_Element_Node
;
1286 -----------------------
1287 -- Is_Less_Node_Node --
1288 -----------------------
1290 function Is_Less_Node_Node
(L
, R
: Node_Access
) return Boolean is
1292 return L
.Element
.all < R
.Element
.all;
1293 end Is_Less_Node_Node
;
1299 function Is_Subset
(Subset
: Set
; Of_Set
: Set
) return Boolean is
1301 return Set_Ops
.Is_Subset
(Subset
=> Subset
.Tree
, Of_Set
=> Of_Set
.Tree
);
1310 Item
: Element_Type
;
1311 Process
: not null access procedure (Position
: Cursor
))
1313 procedure Process_Node
(Node
: Node_Access
);
1314 pragma Inline
(Process_Node
);
1316 procedure Local_Iterate
is
1317 new Element_Keys
.Generic_Iteration
(Process_Node
);
1323 procedure Process_Node
(Node
: Node_Access
) is
1325 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1328 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1329 B : Natural renames T.Busy;
1331 -- Start of processing for Iterate
1337 Local_Iterate (T, Item);
1349 Process : not null access procedure (Position : Cursor))
1351 procedure Process_Node (Node : Node_Access);
1352 pragma Inline (Process_Node);
1354 procedure Local_Iterate is
1355 new Tree_Operations.Generic_Iteration (Process_Node);
1361 procedure Process_Node (Node : Node_Access) is
1363 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1366 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
1367 B
: Natural renames T
.Busy
;
1369 -- Start of processing for Iterate
1385 function Iterate
(Container
: Set
)
1386 return Set_Iterator_Interfaces
.Reversible_Iterator
'Class
1388 S
: constant Set_Access
:= Container
'Unrestricted_Access;
1389 B
: Natural renames S
.Tree
.Busy
;
1392 -- The value of the Node component influences the behavior of the First
1393 -- and Last selector functions of the iterator object. When the Node
1394 -- component is null (as is the case here), this means the iterator
1395 -- object was constructed without a start expression. This is a complete
1396 -- iterator, meaning that the iteration starts from the (logical)
1397 -- beginning of the sequence of items.
1399 -- Note: For a forward iterator, Container.First is the beginning, and
1400 -- for a reverse iterator, Container.Last is the beginning.
1402 return It
: constant Iterator
:= (Limited_Controlled
with S
, null) do
1407 function Iterate
(Container
: Set
; Start
: Cursor
)
1408 return Set_Iterator_Interfaces
.Reversible_Iterator
'Class
1410 S
: constant Set_Access
:= Container
'Unrestricted_Access;
1411 B
: Natural renames S
.Tree
.Busy
;
1414 -- It was formerly the case that when Start = No_Element, the partial
1415 -- iterator was defined to behave the same as for a complete iterator,
1416 -- and iterate over the entire sequence of items. However, those
1417 -- semantics were unintuitive and arguably error-prone (it is too easy
1418 -- to accidentally create an endless loop), and so they were changed,
1419 -- per the ARG meeting in Denver on 2011/11. However, there was no
1420 -- consensus about what positive meaning this corner case should have,
1421 -- and so it was decided to simply raise an exception. This does imply,
1422 -- however, that it is not possible to use a partial iterator to specify
1423 -- an empty sequence of items.
1425 if Start
= No_Element
then
1426 raise Constraint_Error
with
1427 "Start position for iterator equals No_Element";
1430 if Start
.Container
/= Container
'Unrestricted_Access then
1431 raise Program_Error
with
1432 "Start cursor of Iterate designates wrong set";
1435 pragma Assert
(Vet
(Container
.Tree
, Start
.Node
),
1436 "Start cursor of Iterate is bad");
1438 -- The value of the Node component influences the behavior of the First
1439 -- and Last selector functions of the iterator object. When the Node
1440 -- component is non-null (as is the case here), it means that this is a
1441 -- partial iteration, over a subset of the complete sequence of
1442 -- items. The iterator object was constructed with a start expression,
1443 -- indicating the position from which the iteration begins. Note that
1444 -- the start position has the same value irrespective of whether this is
1445 -- a forward or reverse iteration.
1447 return It
: constant Iterator
:=
1448 (Limited_Controlled
with S
, Start
.Node
)
1458 function Last
(Container
: Set
) return Cursor
is
1460 if Container
.Tree
.Last
= null then
1464 return Cursor
'(Container'Unrestricted_Access, Container.Tree.Last);
1467 function Last (Object : Iterator) return Cursor is
1469 -- The value of the iterator object's Node component influences the
1470 -- behavior of the Last (and First) selector function.
1472 -- When the Node component is null, this means the iterator object was
1473 -- constructed without a start expression, in which case the (reverse)
1474 -- iteration starts from the (logical) beginning of the entire sequence
1475 -- (corresponding to Container.Last, for a reverse iterator).
1477 -- Otherwise, this is iteration over a partial sequence of items. When
1478 -- the Node component is non-null, the iterator object was constructed
1479 -- with a start expression, that specifies the position from which the
1480 -- (reverse) partial iteration begins.
1482 if Object.Node = null then
1483 return Object.Container.Last;
1485 return Cursor'(Object
.Container
, Object
.Node
);
1493 function Last_Element
(Container
: Set
) return Element_Type
is
1495 if Container
.Tree
.Last
= null then
1496 raise Constraint_Error
with "set is empty";
1499 pragma Assert
(Container
.Tree
.Last
.Element
/= null);
1500 return Container
.Tree
.Last
.Element
.all;
1507 function Left
(Node
: Node_Access
) return Node_Access
is
1516 function Length
(Container
: Set
) return Count_Type
is
1518 return Container
.Tree
.Length
;
1526 new Tree_Operations
.Generic_Move
(Clear
);
1528 procedure Move
(Target
: in out Set
; Source
: in out Set
) is
1530 Move
(Target
=> Target
.Tree
, Source
=> Source
.Tree
);
1537 function Next
(Position
: Cursor
) return Cursor
is
1539 if Position
= No_Element
then
1543 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
1544 "bad cursor in Next");
1547 Node
: constant Node_Access
:=
1548 Tree_Operations
.Next
(Position
.Node
);
1555 return Cursor
'(Position.Container, Node);
1559 procedure Next (Position : in out Cursor) is
1561 Position := Next (Position);
1564 function Next (Object : Iterator; Position : Cursor) return Cursor is
1566 if Position.Container = null then
1570 if Position.Container /= Object.Container then
1571 raise Program_Error with
1572 "Position cursor of Next designates wrong set";
1575 return Next (Position);
1582 function Overlap (Left, Right : Set) return Boolean is
1584 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1591 function Parent (Node : Node_Access) return Node_Access is
1600 function Previous (Position : Cursor) return Cursor is
1602 if Position = No_Element then
1606 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1607 "bad cursor in Previous");
1610 Node : constant Node_Access :=
1611 Tree_Operations.Previous (Position.Node);
1618 return Cursor'(Position
.Container
, Node
);
1622 procedure Previous
(Position
: in out Cursor
) is
1624 Position
:= Previous
(Position
);
1627 function Previous
(Object
: Iterator
; Position
: Cursor
) return Cursor
is
1629 if Position
.Container
= null then
1633 if Position
.Container
/= Object
.Container
then
1634 raise Program_Error
with
1635 "Position cursor of Previous designates wrong set";
1638 return Previous
(Position
);
1645 procedure Query_Element
1647 Process
: not null access procedure (Element
: Element_Type
))
1650 if Position
.Node
= null then
1651 raise Constraint_Error
with "Position cursor equals No_Element";
1654 if Position
.Node
.Element
= null then
1655 raise Program_Error
with "Position cursor is bad";
1658 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
1659 "bad cursor in Query_Element");
1662 T
: Tree_Type
renames Position
.Container
.Tree
;
1664 B
: Natural renames T
.Busy
;
1665 L
: Natural renames T
.Lock
;
1672 Process
(Position
.Node
.Element
.all);
1690 (Stream
: not null access Root_Stream_Type
'Class;
1691 Container
: out Set
)
1694 (Stream
: not null access Root_Stream_Type
'Class) return Node_Access
;
1695 pragma Inline
(Read_Node
);
1698 new Tree_Operations
.Generic_Read
(Clear
, Read_Node
);
1705 (Stream
: not null access Root_Stream_Type
'Class) return Node_Access
1707 Node
: Node_Access
:= new Node_Type
;
1709 Node
.Element
:= new Element_Type
'(Element_Type'Input (Stream));
1713 Free (Node); -- Note that Free deallocates elem too
1717 -- Start of processing for Read
1720 Read (Stream, Container.Tree);
1724 (Stream : not null access Root_Stream_Type'Class;
1728 raise Program_Error with "attempt to stream set cursor";
1731 ---------------------
1732 -- Replace_Element --
1733 ---------------------
1735 procedure Replace_Element
1736 (Tree : in out Tree_Type;
1738 Item : Element_Type)
1741 if Item < Node.Element.all
1742 or else Node.Element.all < Item
1746 if Tree.Lock > 0 then
1747 raise Program_Error with
1748 "attempt to tamper with elements (set is locked)";
1752 X : Element_Access := Node.Element;
1754 -- The element allocator may need an accessibility check in the
1755 -- case the actual type is class-wide or has access discriminants
1756 -- (see RM 4.8(10.1) and AI12-0035).
1758 pragma Unsuppress (Accessibility_Check);
1761 Node.Element := new Element_Type'(Item
);
1768 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, Node
); -- Checks busy-bit
1770 Insert_New_Item
: declare
1771 function New_Node
return Node_Access
;
1772 pragma Inline
(New_Node
);
1774 procedure Insert_Post
is
1775 new Element_Keys
.Generic_Insert_Post
(New_Node
);
1777 procedure Unconditional_Insert
is
1778 new Element_Keys
.Generic_Unconditional_Insert
(Insert_Post
);
1784 function New_Node
return Node_Access
is
1786 -- The element allocator may need an accessibility check in the
1787 -- case the actual type is class-wide or has access discriminants
1788 -- (see RM 4.8(10.1) and AI12-0035).
1790 pragma Unsuppress
(Accessibility_Check
);
1793 Node
.Element
:= new Element_Type
'(Item); -- OK if fails
1794 Node.Color := Red_Black_Trees.Red;
1795 Node.Parent := null;
1802 Result : Node_Access;
1804 X : Element_Access := Node.Element;
1806 -- Start of processing for Insert_New_Item
1809 Unconditional_Insert
1813 pragma Assert (Result = Node);
1815 Free_Element (X); -- OK if fails
1816 end Insert_New_Item;
1817 end Replace_Element;
1819 procedure Replace_Element
1820 (Container : in out Set;
1822 New_Item : Element_Type)
1825 if Position.Node = null then
1826 raise Constraint_Error with "Position cursor equals No_Element";
1829 if Position.Node.Element = null then
1830 raise Program_Error with "Position cursor is bad";
1833 if Position.Container /= Container'Unrestricted_Access then
1834 raise Program_Error with "Position cursor designates wrong set";
1837 pragma Assert (Vet (Container.Tree, Position.Node),
1838 "bad cursor in Replace_Element");
1840 Replace_Element (Container.Tree, Position.Node, New_Item);
1841 end Replace_Element;
1843 ---------------------
1844 -- Reverse_Iterate --
1845 ---------------------
1847 procedure Reverse_Iterate
1849 Item : Element_Type;
1850 Process : not null access procedure (Position : Cursor))
1852 procedure Process_Node (Node : Node_Access);
1853 pragma Inline (Process_Node);
1855 procedure Local_Reverse_Iterate is
1856 new Element_Keys.Generic_Reverse_Iteration (Process_Node);
1862 procedure Process_Node (Node : Node_Access) is
1864 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1867 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
1868 B
: Natural renames T
.Busy
;
1870 -- Start of processing for Reverse_Iterate
1876 Local_Reverse_Iterate
(T
, Item
);
1884 end Reverse_Iterate
;
1886 procedure Reverse_Iterate
1888 Process
: not null access procedure (Position
: Cursor
))
1890 procedure Process_Node
(Node
: Node_Access
);
1891 pragma Inline
(Process_Node
);
1893 procedure Local_Reverse_Iterate
is
1894 new Tree_Operations
.Generic_Reverse_Iteration
(Process_Node
);
1900 procedure Process_Node
(Node
: Node_Access
) is
1902 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1905 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1906 B : Natural renames T.Busy;
1908 -- Start of processing for Reverse_Iterate
1914 Local_Reverse_Iterate (T);
1922 end Reverse_Iterate;
1928 function Right (Node : Node_Access) return Node_Access is
1937 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1939 Node.Color := Color;
1946 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1955 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1957 Node.Parent := Parent;
1964 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1966 Node.Right := Right;
1969 --------------------------
1970 -- Symmetric_Difference --
1971 --------------------------
1973 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1975 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
1976 end Symmetric_Difference;
1978 function Symmetric_Difference (Left, Right : Set) return Set is
1979 Tree : constant Tree_Type :=
1980 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
1982 return Set'(Controlled
with Tree
);
1983 end Symmetric_Difference
;
1989 function To_Set
(New_Item
: Element_Type
) return Set
is
1992 pragma Unreferenced
(Node
);
1994 Insert_Sans_Hint
(Tree
, New_Item
, Node
);
1995 return Set
'(Controlled with Tree);
2002 procedure Union (Target : in out Set; Source : Set) is
2004 Set_Ops.Union (Target.Tree, Source.Tree);
2007 function Union (Left, Right : Set) return Set is
2008 Tree : constant Tree_Type :=
2009 Set_Ops.Union (Left.Tree, Right.Tree);
2011 return Set'(Controlled
with Tree
);
2019 (Stream
: not null access Root_Stream_Type
'Class;
2022 procedure Write_Node
2023 (Stream
: not null access Root_Stream_Type
'Class;
2024 Node
: Node_Access
);
2025 pragma Inline
(Write_Node
);
2028 new Tree_Operations
.Generic_Write
(Write_Node
);
2034 procedure Write_Node
2035 (Stream
: not null access Root_Stream_Type
'Class;
2039 Element_Type
'Output (Stream
, Node
.Element
.all);
2042 -- Start of processing for Write
2045 Write
(Stream
, Container
.Tree
);
2049 (Stream
: not null access Root_Stream_Type
'Class;
2053 raise Program_Error
with "attempt to stream set cursor";
2056 end Ada
.Containers
.Indefinite_Ordered_Multisets
;