1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . O R D E R E D _ M U L T I S E T S --
9 -- Copyright (C) 2004-2014, 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
.Ordered_Multisets
is
45 pragma Annotate
(CodePeer
, Skip_Analysis
);
47 -----------------------------
48 -- Node Access Subprograms --
49 -----------------------------
51 -- These subprograms provide a functional interface to access fields
52 -- of a node, and a procedural interface for modifying these values.
54 function Color
(Node
: Node_Access
) return Color_Type
;
55 pragma Inline
(Color
);
57 function Left
(Node
: Node_Access
) return Node_Access
;
60 function Parent
(Node
: Node_Access
) return Node_Access
;
61 pragma Inline
(Parent
);
63 function Right
(Node
: Node_Access
) return Node_Access
;
64 pragma Inline
(Right
);
66 procedure Set_Parent
(Node
: Node_Access
; Parent
: Node_Access
);
67 pragma Inline
(Set_Parent
);
69 procedure Set_Left
(Node
: Node_Access
; Left
: Node_Access
);
70 pragma Inline
(Set_Left
);
72 procedure Set_Right
(Node
: Node_Access
; Right
: Node_Access
);
73 pragma Inline
(Set_Right
);
75 procedure Set_Color
(Node
: Node_Access
; Color
: Color_Type
);
76 pragma Inline
(Set_Color
);
78 -----------------------
79 -- Local Subprograms --
80 -----------------------
82 function Copy_Node
(Source
: Node_Access
) return Node_Access
;
83 pragma Inline
(Copy_Node
);
85 procedure Free
(X
: in out Node_Access
);
87 procedure Insert_Sans_Hint
88 (Tree
: in out Tree_Type
;
89 New_Item
: Element_Type
;
90 Node
: out Node_Access
);
92 procedure Insert_With_Hint
93 (Dst_Tree
: in out Tree_Type
;
94 Dst_Hint
: Node_Access
;
95 Src_Node
: Node_Access
;
96 Dst_Node
: out Node_Access
);
98 function Is_Equal_Node_Node
(L
, R
: Node_Access
) return Boolean;
99 pragma Inline
(Is_Equal_Node_Node
);
101 function Is_Greater_Element_Node
102 (Left
: Element_Type
;
103 Right
: Node_Access
) return Boolean;
104 pragma Inline
(Is_Greater_Element_Node
);
106 function Is_Less_Element_Node
107 (Left
: Element_Type
;
108 Right
: Node_Access
) return Boolean;
109 pragma Inline
(Is_Less_Element_Node
);
111 function Is_Less_Node_Node
(L
, R
: Node_Access
) return Boolean;
112 pragma Inline
(Is_Less_Node_Node
);
114 procedure Replace_Element
115 (Tree
: in out Tree_Type
;
117 Item
: Element_Type
);
119 --------------------------
120 -- Local Instantiations --
121 --------------------------
123 package Tree_Operations
is
124 new Red_Black_Trees
.Generic_Operations
(Tree_Types
);
126 procedure Delete_Tree
is
127 new Tree_Operations
.Generic_Delete_Tree
(Free
);
129 function Copy_Tree
is
130 new Tree_Operations
.Generic_Copy_Tree
(Copy_Node
, Delete_Tree
);
135 new Tree_Operations
.Generic_Equal
(Is_Equal_Node_Node
);
137 package Element_Keys
is
138 new Red_Black_Trees
.Generic_Keys
139 (Tree_Operations
=> Tree_Operations
,
140 Key_Type
=> Element_Type
,
141 Is_Less_Key_Node
=> Is_Less_Element_Node
,
142 Is_Greater_Key_Node
=> Is_Greater_Element_Node
);
145 new Generic_Set_Operations
146 (Tree_Operations
=> Tree_Operations
,
147 Insert_With_Hint
=> Insert_With_Hint
,
148 Copy_Tree
=> Copy_Tree
,
149 Delete_Tree
=> Delete_Tree
,
150 Is_Less
=> Is_Less_Node_Node
,
157 function "<" (Left
, Right
: Cursor
) return Boolean is
159 if Left
.Node
= null then
160 raise Constraint_Error
with "Left cursor equals No_Element";
163 if Right
.Node
= null then
164 raise Constraint_Error
with "Right cursor equals No_Element";
167 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
168 "bad Left cursor in ""<""");
170 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
171 "bad Right cursor in ""<""");
173 return Left
.Node
.Element
< Right
.Node
.Element
;
176 function "<" (Left
: Cursor
; Right
: Element_Type
)
179 if Left
.Node
= null then
180 raise Constraint_Error
with "Left cursor equals No_Element";
183 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
184 "bad Left cursor in ""<""");
186 return Left
.Node
.Element
< Right
;
189 function "<" (Left
: Element_Type
; Right
: Cursor
)
192 if Right
.Node
= null then
193 raise Constraint_Error
with "Right cursor equals No_Element";
196 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
197 "bad Right cursor in ""<""");
199 return Left
< Right
.Node
.Element
;
206 function "=" (Left
, Right
: Set
) return Boolean is
208 return Is_Equal
(Left
.Tree
, Right
.Tree
);
215 function ">" (Left
, Right
: Cursor
) return Boolean is
217 if Left
.Node
= null then
218 raise Constraint_Error
with "Left cursor equals No_Element";
221 if Right
.Node
= null then
222 raise Constraint_Error
with "Right cursor equals No_Element";
225 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
226 "bad Left cursor in "">""");
228 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
229 "bad Right cursor in "">""");
231 -- L > R same as R < L
233 return Right
.Node
.Element
< Left
.Node
.Element
;
236 function ">" (Left
: Cursor
; Right
: Element_Type
)
239 if Left
.Node
= null then
240 raise Constraint_Error
with "Left cursor equals No_Element";
243 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
244 "bad Left cursor in "">""");
246 return Right
< Left
.Node
.Element
;
249 function ">" (Left
: Element_Type
; Right
: Cursor
)
252 if Right
.Node
= null then
253 raise Constraint_Error
with "Right cursor equals No_Element";
256 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
257 "bad Right cursor in "">""");
259 return Right
.Node
.Element
< Left
;
266 procedure Adjust
is new Tree_Operations
.Generic_Adjust
(Copy_Tree
);
268 procedure Adjust
(Container
: in out Set
) is
270 Adjust
(Container
.Tree
);
277 procedure Assign
(Target
: in out Set
; Source
: Set
) is
279 if Target
'Address = Source
'Address then
284 Target
.Union
(Source
);
291 function Ceiling
(Container
: Set
; Item
: Element_Type
) return Cursor
is
292 Node
: constant Node_Access
:=
293 Element_Keys
.Ceiling
(Container
.Tree
, Item
);
300 return Cursor
'(Container'Unrestricted_Access, Node);
308 new Tree_Operations.Generic_Clear (Delete_Tree);
310 procedure Clear (Container : in out Set) is
312 Clear (Container.Tree);
319 function Color (Node : Node_Access) return Color_Type is
328 function Contains (Container : Set; Item : Element_Type) return Boolean is
330 return Find (Container, Item) /= No_Element;
337 function Copy (Source : Set) return Set is
339 return Target : Set do
340 Target.Assign (Source);
348 function Copy_Node (Source : Node_Access) return Node_Access is
349 Target : constant Node_Access :=
350 new Node_Type'(Parent
=> null,
353 Color
=> Source
.Color
,
354 Element
=> Source
.Element
);
363 procedure Delete
(Container
: in out Set
; Item
: Element_Type
) is
364 Tree
: Tree_Type
renames Container
.Tree
;
365 Node
: Node_Access
:= Element_Keys
.Ceiling
(Tree
, Item
);
366 Done
: constant Node_Access
:= Element_Keys
.Upper_Bound
(Tree
, Item
);
371 raise Constraint_Error
with
372 "attempt to delete element not in set";
377 Node
:= Tree_Operations
.Next
(Node
);
378 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, X
);
381 exit when Node
= Done
;
385 procedure Delete
(Container
: in out Set
; Position
: in out Cursor
) is
387 if Position
.Node
= null then
388 raise Constraint_Error
with "Position cursor equals No_Element";
391 if Position
.Container
/= Container
'Unrestricted_Access then
392 raise Program_Error
with "Position cursor designates wrong set";
395 pragma Assert
(Vet
(Container
.Tree
, Position
.Node
),
396 "bad cursor in Delete");
398 Delete_Node_Sans_Free
(Container
.Tree
, Position
.Node
);
399 Free
(Position
.Node
);
401 Position
.Container
:= null;
408 procedure Delete_First
(Container
: in out Set
) is
409 Tree
: Tree_Type
renames Container
.Tree
;
410 X
: Node_Access
:= Tree
.First
;
417 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, X
);
425 procedure Delete_Last
(Container
: in out Set
) is
426 Tree
: Tree_Type
renames Container
.Tree
;
427 X
: Node_Access
:= Tree
.Last
;
434 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, X
);
442 procedure Difference
(Target
: in out Set
; Source
: Set
) is
444 Set_Ops
.Difference
(Target
.Tree
, Source
.Tree
);
447 function Difference
(Left
, Right
: Set
) return Set
is
448 Tree
: constant Tree_Type
:=
449 Set_Ops
.Difference
(Left
.Tree
, Right
.Tree
);
451 return Set
'(Controlled with Tree);
458 function Element (Position : Cursor) return Element_Type is
460 if Position.Node = null then
461 raise Constraint_Error with "Position cursor equals No_Element";
464 pragma Assert (Vet (Position.Container.Tree, Position.Node),
465 "bad cursor in Element");
467 return Position.Node.Element;
470 -------------------------
471 -- Equivalent_Elements --
472 -------------------------
474 function Equivalent_Elements (Left, Right : Element_Type) return Boolean is
483 end Equivalent_Elements;
485 ---------------------
486 -- Equivalent_Sets --
487 ---------------------
489 function Equivalent_Sets (Left, Right : Set) return Boolean is
491 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean;
492 pragma Inline (Is_Equivalent_Node_Node);
494 function Is_Equivalent is
495 new Tree_Operations.Generic_Equal (Is_Equivalent_Node_Node);
497 -----------------------------
498 -- Is_Equivalent_Node_Node --
499 -----------------------------
501 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean is
503 if L.Element < R.Element then
505 elsif R.Element < L.Element then
510 end Is_Equivalent_Node_Node;
512 -- Start of processing for Equivalent_Sets
515 return Is_Equivalent (Left.Tree, Right.Tree);
522 procedure Exclude (Container : in out Set; Item : Element_Type) is
523 Tree : Tree_Type renames Container.Tree;
524 Node : Node_Access := Element_Keys.Ceiling (Tree, Item);
525 Done : constant Node_Access := Element_Keys.Upper_Bound (Tree, Item);
528 while Node /= Done loop
530 Node := Tree_Operations.Next (Node);
531 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
540 procedure Finalize (Object : in out Iterator) is
541 B : Natural renames Object.Container.Tree.Busy;
542 pragma Assert (B > 0);
551 function Find (Container : Set; Item : Element_Type) return Cursor is
552 Node : constant Node_Access :=
553 Element_Keys.Find (Container.Tree, Item);
560 return Cursor'(Container
'Unrestricted_Access, Node
);
567 function First
(Container
: Set
) return Cursor
is
569 if Container
.Tree
.First
= null then
573 return Cursor
'(Container'Unrestricted_Access, Container.Tree.First);
576 function First (Object : Iterator) return Cursor is
578 -- The value of the iterator object's Node component influences the
579 -- behavior of the First (and Last) selector function.
581 -- When the Node component is null, this means the iterator object was
582 -- constructed without a start expression, in which case the (forward)
583 -- iteration starts from the (logical) beginning of the entire sequence
584 -- of items (corresponding to Container.First, for a forward iterator).
586 -- Otherwise, this is iteration over a partial sequence of items. When
587 -- the Node component is non-null, the iterator object was constructed
588 -- with a start expression, that specifies the position from which the
589 -- (forward) partial iteration begins.
591 if Object.Node = null then
592 return Object.Container.First;
594 return Cursor'(Object
.Container
, Object
.Node
);
602 function First_Element
(Container
: Set
) return Element_Type
is
604 if Container
.Tree
.First
= null then
605 raise Constraint_Error
with "set is empty";
608 return Container
.Tree
.First
.Element
;
615 function Floor
(Container
: Set
; Item
: Element_Type
) return Cursor
is
616 Node
: constant Node_Access
:=
617 Element_Keys
.Floor
(Container
.Tree
, Item
);
624 return Cursor
'(Container'Unrestricted_Access, Node);
631 procedure Free (X : in out Node_Access) is
632 procedure Deallocate is
633 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
649 package body Generic_Keys is
651 -----------------------
652 -- Local Subprograms --
653 -----------------------
655 function Is_Greater_Key_Node
657 Right : Node_Access) return Boolean;
658 pragma Inline (Is_Greater_Key_Node);
660 function Is_Less_Key_Node
662 Right : Node_Access) return Boolean;
663 pragma Inline (Is_Less_Key_Node);
665 --------------------------
666 -- Local_Instantiations --
667 --------------------------
670 new Red_Black_Trees.Generic_Keys
671 (Tree_Operations => Tree_Operations,
672 Key_Type => Key_Type,
673 Is_Less_Key_Node => Is_Less_Key_Node,
674 Is_Greater_Key_Node => Is_Greater_Key_Node);
680 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
681 Node : constant Node_Access :=
682 Key_Keys.Ceiling (Container.Tree, Key);
689 return Cursor'(Container
'Unrestricted_Access, Node
);
696 function Contains
(Container
: Set
; Key
: Key_Type
) return Boolean is
698 return Find
(Container
, Key
) /= No_Element
;
705 procedure Delete
(Container
: in out Set
; Key
: Key_Type
) is
706 Tree
: Tree_Type
renames Container
.Tree
;
707 Node
: Node_Access
:= Key_Keys
.Ceiling
(Tree
, Key
);
708 Done
: constant Node_Access
:= Key_Keys
.Upper_Bound
(Tree
, Key
);
713 raise Constraint_Error
with "attempt to delete key not in set";
718 Node
:= Tree_Operations
.Next
(Node
);
719 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, X
);
722 exit when Node
= Done
;
730 function Element
(Container
: Set
; Key
: Key_Type
) return Element_Type
is
731 Node
: constant Node_Access
:= Key_Keys
.Find
(Container
.Tree
, Key
);
734 raise Constraint_Error
with "key not in set";
740 ---------------------
741 -- Equivalent_Keys --
742 ---------------------
744 function Equivalent_Keys
(Left
, Right
: Key_Type
) return Boolean is
759 procedure Exclude
(Container
: in out Set
; Key
: Key_Type
) is
760 Tree
: Tree_Type
renames Container
.Tree
;
761 Node
: Node_Access
:= Key_Keys
.Ceiling
(Tree
, Key
);
762 Done
: constant Node_Access
:= Key_Keys
.Upper_Bound
(Tree
, Key
);
766 while Node
/= Done
loop
768 Node
:= Tree_Operations
.Next
(Node
);
769 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, X
);
778 function Find
(Container
: Set
; Key
: Key_Type
) return Cursor
is
779 Node
: constant Node_Access
:= Key_Keys
.Find
(Container
.Tree
, Key
);
786 return Cursor
'(Container'Unrestricted_Access, Node);
793 function Floor (Container : Set; Key : Key_Type) return Cursor is
794 Node : constant Node_Access := Key_Keys.Floor (Container.Tree, Key);
801 return Cursor'(Container
'Unrestricted_Access, Node
);
804 -------------------------
805 -- Is_Greater_Key_Node --
806 -------------------------
808 function Is_Greater_Key_Node
810 Right
: Node_Access
) return Boolean is
812 return Key
(Right
.Element
) < Left
;
813 end Is_Greater_Key_Node
;
815 ----------------------
816 -- Is_Less_Key_Node --
817 ----------------------
819 function Is_Less_Key_Node
821 Right
: Node_Access
) return Boolean is
823 return Left
< Key
(Right
.Element
);
824 end Is_Less_Key_Node
;
833 Process
: not null access procedure (Position
: Cursor
))
835 procedure Process_Node
(Node
: Node_Access
);
836 pragma Inline
(Process_Node
);
838 procedure Local_Iterate
is
839 new Key_Keys
.Generic_Iteration
(Process_Node
);
845 procedure Process_Node
(Node
: Node_Access
) is
847 Process
(Cursor
'(Container'Unrestricted_Access, Node));
850 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
851 B : Natural renames T.Busy;
853 -- Start of processing for Iterate
859 Local_Iterate (T, Key);
873 function Key (Position : Cursor) return Key_Type is
875 if Position.Node = null then
876 raise Constraint_Error with
877 "Position cursor equals No_Element";
880 pragma Assert (Vet (Position.Container.Tree, Position.Node),
881 "bad cursor in Key");
883 return Key (Position.Node.Element);
886 ---------------------
887 -- Reverse_Iterate --
888 ---------------------
890 procedure Reverse_Iterate
893 Process : not null access procedure (Position : Cursor))
895 procedure Process_Node (Node : Node_Access);
896 pragma Inline (Process_Node);
898 procedure Local_Reverse_Iterate is
899 new Key_Keys.Generic_Reverse_Iteration (Process_Node);
905 procedure Process_Node (Node : Node_Access) is
907 Process (Cursor'(Container
'Unrestricted_Access, Node
));
910 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
911 B
: Natural renames T
.Busy
;
913 -- Start of processing for Reverse_Iterate
919 Local_Reverse_Iterate
(T
, Key
);
933 procedure Update_Element
934 (Container
: in out Set
;
936 Process
: not null access procedure (Element
: in out Element_Type
))
938 Tree
: Tree_Type
renames Container
.Tree
;
939 Node
: constant Node_Access
:= Position
.Node
;
943 raise Constraint_Error
with
944 "Position cursor equals No_Element";
947 if Position
.Container
/= Container
'Unrestricted_Access then
948 raise Program_Error
with
949 "Position cursor designates wrong set";
952 pragma Assert
(Vet
(Tree
, Node
),
953 "bad cursor in Update_Element");
956 E
: Element_Type
renames Node
.Element
;
957 K
: constant Key_Type
:= Key
(E
);
959 B
: Natural renames Tree
.Busy
;
960 L
: Natural renames Tree
.Lock
;
978 if Equivalent_Keys
(Left
=> K
, Right
=> Key
(E
)) then
983 -- Delete_Node checks busy-bit
985 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, Node
);
987 Insert_New_Item
: declare
988 function New_Node
return Node_Access
;
989 pragma Inline
(New_Node
);
991 procedure Insert_Post
is
992 new Element_Keys
.Generic_Insert_Post
(New_Node
);
994 procedure Unconditional_Insert
is
995 new Element_Keys
.Generic_Unconditional_Insert
(Insert_Post
);
1001 function New_Node
return Node_Access
is
1003 Node
.Color
:= Red_Black_Trees
.Red
;
1004 Node
.Parent
:= null;
1011 Result
: Node_Access
;
1013 -- Start of processing for Insert_New_Item
1016 Unconditional_Insert
1018 Key
=> Node
.Element
,
1021 pragma Assert
(Result
= Node
);
1022 end Insert_New_Item
;
1031 function Has_Element
(Position
: Cursor
) return Boolean is
1033 return Position
/= No_Element
;
1040 procedure Insert
(Container
: in out Set
; New_Item
: Element_Type
) is
1042 pragma Unreferenced
(Position
);
1044 Insert
(Container
, New_Item
, Position
);
1048 (Container
: in out Set
;
1049 New_Item
: Element_Type
;
1050 Position
: out Cursor
)
1053 Insert_Sans_Hint
(Container
.Tree
, New_Item
, Position
.Node
);
1054 Position
.Container
:= Container
'Unrestricted_Access;
1057 ----------------------
1058 -- Insert_Sans_Hint --
1059 ----------------------
1061 procedure Insert_Sans_Hint
1062 (Tree
: in out Tree_Type
;
1063 New_Item
: Element_Type
;
1064 Node
: out Node_Access
)
1066 function New_Node
return Node_Access
;
1067 pragma Inline
(New_Node
);
1069 procedure Insert_Post
is
1070 new Element_Keys
.Generic_Insert_Post
(New_Node
);
1072 procedure Unconditional_Insert
is
1073 new Element_Keys
.Generic_Unconditional_Insert
(Insert_Post
);
1079 function New_Node
return Node_Access
is
1080 Node
: constant Node_Access
:=
1081 new Node_Type
'(Parent => null,
1084 Color => Red_Black_Trees.Red,
1085 Element => New_Item);
1090 -- Start of processing for Insert_Sans_Hint
1093 Unconditional_Insert (Tree, New_Item, Node);
1094 end Insert_Sans_Hint;
1096 ----------------------
1097 -- Insert_With_Hint --
1098 ----------------------
1100 procedure Insert_With_Hint
1101 (Dst_Tree : in out Tree_Type;
1102 Dst_Hint : Node_Access;
1103 Src_Node : Node_Access;
1104 Dst_Node : out Node_Access)
1106 function New_Node return Node_Access;
1107 pragma Inline (New_Node);
1109 procedure Insert_Post is
1110 new Element_Keys.Generic_Insert_Post (New_Node);
1112 procedure Insert_Sans_Hint is
1113 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1115 procedure Local_Insert_With_Hint is
1116 new Element_Keys.Generic_Unconditional_Insert_With_Hint
1124 function New_Node return Node_Access is
1125 Node : constant Node_Access :=
1126 new Node_Type'(Parent
=> null,
1130 Element
=> Src_Node
.Element
);
1135 -- Start of processing for Insert_With_Hint
1138 Local_Insert_With_Hint
1143 end Insert_With_Hint
;
1149 procedure Intersection
(Target
: in out Set
; Source
: Set
) is
1151 Set_Ops
.Intersection
(Target
.Tree
, Source
.Tree
);
1154 function Intersection
(Left
, Right
: Set
) return Set
is
1155 Tree
: constant Tree_Type
:=
1156 Set_Ops
.Intersection
(Left
.Tree
, Right
.Tree
);
1158 return Set
'(Controlled with Tree);
1165 function Is_Empty (Container : Set) return Boolean is
1167 return Container.Tree.Length = 0;
1170 ------------------------
1171 -- Is_Equal_Node_Node --
1172 ------------------------
1174 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
1176 return L.Element = R.Element;
1177 end Is_Equal_Node_Node;
1179 -----------------------------
1180 -- Is_Greater_Element_Node --
1181 -----------------------------
1183 function Is_Greater_Element_Node
1184 (Left : Element_Type;
1185 Right : Node_Access) return Boolean
1188 -- e > node same as node < e
1190 return Right.Element < Left;
1191 end Is_Greater_Element_Node;
1193 --------------------------
1194 -- Is_Less_Element_Node --
1195 --------------------------
1197 function Is_Less_Element_Node
1198 (Left : Element_Type;
1199 Right : Node_Access) return Boolean
1202 return Left < Right.Element;
1203 end Is_Less_Element_Node;
1205 -----------------------
1206 -- Is_Less_Node_Node --
1207 -----------------------
1209 function Is_Less_Node_Node (L, R : Node_Access) return Boolean is
1211 return L.Element < R.Element;
1212 end Is_Less_Node_Node;
1218 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1220 return Set_Ops.Is_Subset (Subset => Subset.Tree, Of_Set => Of_Set.Tree);
1229 Process : not null access procedure (Position : Cursor))
1231 procedure Process_Node (Node : Node_Access);
1232 pragma Inline (Process_Node);
1234 procedure Local_Iterate is
1235 new Tree_Operations.Generic_Iteration (Process_Node);
1241 procedure Process_Node (Node : Node_Access) is
1243 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1246 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
1247 B
: Natural renames T
.Busy
;
1249 -- Start of processing for Iterate
1267 Item
: Element_Type
;
1268 Process
: not null access procedure (Position
: Cursor
))
1270 procedure Process_Node
(Node
: Node_Access
);
1271 pragma Inline
(Process_Node
);
1273 procedure Local_Iterate
is
1274 new Element_Keys
.Generic_Iteration
(Process_Node
);
1280 procedure Process_Node
(Node
: Node_Access
) is
1282 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1285 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1286 B : Natural renames T.Busy;
1288 -- Start of processing for Iterate
1294 Local_Iterate (T, Item);
1304 function Iterate (Container : Set)
1305 return Set_Iterator_Interfaces.Reversible_Iterator'Class
1307 S : constant Set_Access := Container'Unrestricted_Access;
1308 B : Natural renames S.Tree.Busy;
1311 -- The value of the Node component influences the behavior of the First
1312 -- and Last selector functions of the iterator object. When the Node
1313 -- component is null (as is the case here), this means the iterator
1314 -- object was constructed without a start expression. This is a complete
1315 -- iterator, meaning that the iteration starts from the (logical)
1316 -- beginning of the sequence of items.
1318 -- Note: For a forward iterator, Container.First is the beginning, and
1319 -- for a reverse iterator, Container.Last is the beginning.
1321 return It : constant Iterator := (Limited_Controlled with S, null) do
1326 function Iterate (Container : Set; Start : Cursor)
1327 return Set_Iterator_Interfaces.Reversible_Iterator'Class
1329 S : constant Set_Access := Container'Unrestricted_Access;
1330 B : Natural renames S.Tree.Busy;
1333 -- It was formerly the case that when Start = No_Element, the partial
1334 -- iterator was defined to behave the same as for a complete iterator,
1335 -- and iterate over the entire sequence of items. However, those
1336 -- semantics were unintuitive and arguably error-prone (it is too easy
1337 -- to accidentally create an endless loop), and so they were changed,
1338 -- per the ARG meeting in Denver on 2011/11. However, there was no
1339 -- consensus about what positive meaning this corner case should have,
1340 -- and so it was decided to simply raise an exception. This does imply,
1341 -- however, that it is not possible to use a partial iterator to specify
1342 -- an empty sequence of items.
1344 if Start = No_Element then
1345 raise Constraint_Error with
1346 "Start position for iterator equals No_Element";
1349 if Start.Container /= Container'Unrestricted_Access then
1350 raise Program_Error with
1351 "Start cursor of Iterate designates wrong set";
1354 pragma Assert (Vet (Container.Tree, Start.Node),
1355 "Start cursor of Iterate is bad");
1357 -- The value of the Node component influences the behavior of the First
1358 -- and Last selector functions of the iterator object. When the Node
1359 -- component is non-null (as is the case here), it means that this is a
1360 -- partial iteration, over a subset of the complete sequence of
1361 -- items. The iterator object was constructed with a start expression,
1362 -- indicating the position from which the iteration begins. Note that
1363 -- the start position has the same value irrespective of whether this is
1364 -- a forward or reverse iteration.
1366 return It : constant Iterator :=
1367 (Limited_Controlled with S, Start.Node)
1377 function Last (Container : Set) return Cursor is
1379 if Container.Tree.Last = null then
1383 return Cursor'(Container
'Unrestricted_Access, Container
.Tree
.Last
);
1386 function Last
(Object
: Iterator
) return Cursor
is
1388 -- The value of the iterator object's Node component influences the
1389 -- behavior of the Last (and First) selector function.
1391 -- When the Node component is null, this means the iterator object was
1392 -- constructed without a start expression, in which case the (reverse)
1393 -- iteration starts from the (logical) beginning of the entire sequence
1394 -- (corresponding to Container.Last, for a reverse iterator).
1396 -- Otherwise, this is iteration over a partial sequence of items. When
1397 -- the Node component is non-null, the iterator object was constructed
1398 -- with a start expression, that specifies the position from which the
1399 -- (reverse) partial iteration begins.
1401 if Object
.Node
= null then
1402 return Object
.Container
.Last
;
1404 return Cursor
'(Object.Container, Object.Node);
1412 function Last_Element (Container : Set) return Element_Type is
1414 if Container.Tree.Last = null then
1415 raise Constraint_Error with "set is empty";
1418 return Container.Tree.Last.Element;
1425 function Left (Node : Node_Access) return Node_Access is
1434 function Length (Container : Set) return Count_Type is
1436 return Container.Tree.Length;
1444 new Tree_Operations.Generic_Move (Clear);
1446 procedure Move (Target : in out Set; Source : in out Set) is
1448 Move (Target => Target.Tree, Source => Source.Tree);
1455 procedure Next (Position : in out Cursor)
1458 Position := Next (Position);
1461 function Next (Position : Cursor) return Cursor is
1463 if Position = No_Element then
1467 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1468 "bad cursor in Next");
1471 Node : constant Node_Access := Tree_Operations.Next (Position.Node);
1477 return Cursor'(Position
.Container
, Node
);
1481 function Next
(Object
: Iterator
; Position
: Cursor
) return Cursor
is
1483 if Position
.Container
= null then
1487 if Position
.Container
/= Object
.Container
then
1488 raise Program_Error
with
1489 "Position cursor of Next designates wrong set";
1492 return Next
(Position
);
1499 function Overlap
(Left
, Right
: Set
) return Boolean is
1501 return Set_Ops
.Overlap
(Left
.Tree
, Right
.Tree
);
1508 function Parent
(Node
: Node_Access
) return Node_Access
is
1517 procedure Previous
(Position
: in out Cursor
)
1520 Position
:= Previous
(Position
);
1523 function Previous
(Position
: Cursor
) return Cursor
is
1525 if Position
= No_Element
then
1529 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
1530 "bad cursor in Previous");
1533 Node
: constant Node_Access
:=
1534 Tree_Operations
.Previous
(Position
.Node
);
1536 return (if Node
= null then No_Element
1537 else Cursor
'(Position.Container, Node));
1541 function Previous (Object : Iterator; Position : Cursor) return Cursor is
1543 if Position.Container = null then
1547 if Position.Container /= Object.Container then
1548 raise Program_Error with
1549 "Position cursor of Previous designates wrong set";
1552 return Previous (Position);
1559 procedure Query_Element
1561 Process : not null access procedure (Element : Element_Type))
1564 if Position.Node = null then
1565 raise Constraint_Error with "Position cursor equals No_Element";
1568 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1569 "bad cursor in Query_Element");
1572 T : Tree_Type renames Position.Container.Tree;
1574 B : Natural renames T.Busy;
1575 L : Natural renames T.Lock;
1582 Process (Position.Node.Element);
1600 (Stream : not null access Root_Stream_Type'Class;
1601 Container : out Set)
1604 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1605 pragma Inline (Read_Node);
1608 new Tree_Operations.Generic_Read (Clear, Read_Node);
1615 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1617 Node : Node_Access := new Node_Type;
1619 Element_Type'Read (Stream, Node.Element);
1623 Free (Node); -- Note that Free deallocates elem too
1627 -- Start of processing for Read
1630 Read (Stream, Container.Tree);
1634 (Stream : not null access Root_Stream_Type'Class;
1638 raise Program_Error with "attempt to stream set cursor";
1641 ---------------------
1642 -- Replace_Element --
1643 ---------------------
1645 procedure Replace_Element
1646 (Tree : in out Tree_Type;
1648 Item : Element_Type)
1651 if Item < Node.Element
1652 or else Node.Element < Item
1656 if Tree.Lock > 0 then
1657 raise Program_Error with
1658 "attempt to tamper with elements (set is locked)";
1661 Node.Element := Item;
1665 Tree_Operations.Delete_Node_Sans_Free (Tree, Node); -- Checks busy-bit
1667 Insert_New_Item : declare
1668 function New_Node return Node_Access;
1669 pragma Inline (New_Node);
1671 procedure Insert_Post is
1672 new Element_Keys.Generic_Insert_Post (New_Node);
1674 procedure Unconditional_Insert is
1675 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1681 function New_Node return Node_Access is
1683 Node.Element := Item;
1684 Node.Color := Red_Black_Trees.Red;
1685 Node.Parent := null;
1692 Result : Node_Access;
1694 -- Start of processing for Insert_New_Item
1697 Unconditional_Insert
1702 pragma Assert (Result = Node);
1703 end Insert_New_Item;
1704 end Replace_Element;
1706 procedure Replace_Element
1707 (Container : in out Set;
1709 New_Item : Element_Type)
1712 if Position.Node = null then
1713 raise Constraint_Error with
1714 "Position cursor equals No_Element";
1717 if Position.Container /= Container'Unrestricted_Access then
1718 raise Program_Error with
1719 "Position cursor designates wrong set";
1722 pragma Assert (Vet (Container.Tree, Position.Node),
1723 "bad cursor in Replace_Element");
1725 Replace_Element (Container.Tree, Position.Node, New_Item);
1726 end Replace_Element;
1728 ---------------------
1729 -- Reverse_Iterate --
1730 ---------------------
1732 procedure Reverse_Iterate
1734 Process : not null access procedure (Position : Cursor))
1736 procedure Process_Node (Node : Node_Access);
1737 pragma Inline (Process_Node);
1739 procedure Local_Reverse_Iterate is
1740 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1746 procedure Process_Node (Node : Node_Access) is
1748 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1751 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
1752 B
: Natural renames T
.Busy
;
1754 -- Start of processing for Reverse_Iterate
1760 Local_Reverse_Iterate
(T
);
1768 end Reverse_Iterate
;
1770 procedure Reverse_Iterate
1772 Item
: Element_Type
;
1773 Process
: not null access procedure (Position
: Cursor
))
1775 procedure Process_Node
(Node
: Node_Access
);
1776 pragma Inline
(Process_Node
);
1778 procedure Local_Reverse_Iterate
is
1779 new Element_Keys
.Generic_Reverse_Iteration
(Process_Node
);
1785 procedure Process_Node
(Node
: Node_Access
) is
1787 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1790 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1791 B : Natural renames T.Busy;
1793 -- Start of processing for Reverse_Iterate
1799 Local_Reverse_Iterate (T, Item);
1807 end Reverse_Iterate;
1813 function Right (Node : Node_Access) return Node_Access is
1822 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1824 Node.Color := Color;
1831 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1840 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1842 Node.Parent := Parent;
1849 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1851 Node.Right := Right;
1854 --------------------------
1855 -- Symmetric_Difference --
1856 --------------------------
1858 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1860 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
1861 end Symmetric_Difference;
1863 function Symmetric_Difference (Left, Right : Set) return Set is
1864 Tree : constant Tree_Type :=
1865 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
1867 return Set'(Controlled
with Tree
);
1868 end Symmetric_Difference
;
1874 function To_Set
(New_Item
: Element_Type
) return Set
is
1877 pragma Unreferenced
(Node
);
1879 Insert_Sans_Hint
(Tree
, New_Item
, Node
);
1880 return Set
'(Controlled with Tree);
1887 procedure Union (Target : in out Set; Source : Set) is
1889 Set_Ops.Union (Target.Tree, Source.Tree);
1892 function Union (Left, Right : Set) return Set is
1893 Tree : constant Tree_Type := Set_Ops.Union (Left.Tree, Right.Tree);
1895 return Set'(Controlled
with Tree
);
1903 (Stream
: not null access Root_Stream_Type
'Class;
1906 procedure Write_Node
1907 (Stream
: not null access Root_Stream_Type
'Class;
1908 Node
: Node_Access
);
1909 pragma Inline
(Write_Node
);
1912 new Tree_Operations
.Generic_Write
(Write_Node
);
1918 procedure Write_Node
1919 (Stream
: not null access Root_Stream_Type
'Class;
1923 Element_Type
'Write (Stream
, Node
.Element
);
1926 -- Start of processing for Write
1929 Write
(Stream
, Container
.Tree
);
1933 (Stream
: not null access Root_Stream_Type
'Class;
1937 raise Program_Error
with "attempt to stream set cursor";
1940 end Ada
.Containers
.Ordered_Multisets
;