1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.INDEFINITE_ORDERED_MULTISETS --
9 -- Copyright (C) 2004-2008, 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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- This unit was originally developed by Matthew J Heaney. --
30 ------------------------------------------------------------------------------
32 with Ada
.Unchecked_Deallocation
;
34 with Ada
.Containers
.Red_Black_Trees
.Generic_Operations
;
35 pragma Elaborate_All
(Ada
.Containers
.Red_Black_Trees
.Generic_Operations
);
37 with Ada
.Containers
.Red_Black_Trees
.Generic_Keys
;
38 pragma Elaborate_All
(Ada
.Containers
.Red_Black_Trees
.Generic_Keys
);
40 with Ada
.Containers
.Red_Black_Trees
.Generic_Set_Operations
;
41 pragma Elaborate_All
(Ada
.Containers
.Red_Black_Trees
.Generic_Set_Operations
);
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 function Ceiling
(Container
: Set
; Item
: Element_Type
) return Cursor
is
308 Node
: constant Node_Access
:=
309 Element_Keys
.Ceiling
(Container
.Tree
, Item
);
316 return Cursor
'(Container'Unrestricted_Access, Node);
324 new Tree_Operations.Generic_Clear (Delete_Tree);
326 procedure Clear (Container : in out Set) is
328 Clear (Container.Tree);
335 function Color (Node : Node_Access) return Color_Type is
344 function Contains (Container : Set; Item : Element_Type) return Boolean is
346 return Find (Container, Item) /= No_Element;
353 function Copy_Node (Source : Node_Access) return Node_Access is
354 X : Element_Access := new Element_Type'(Source
.Element
.all);
357 return new Node_Type
'(Parent => null,
360 Color => Source.Color,
373 procedure Delete (Container : in out Set; Item : Element_Type) is
374 Tree : Tree_Type renames Container.Tree;
375 Node : Node_Access := Element_Keys.Ceiling (Tree, Item);
376 Done : constant Node_Access := Element_Keys.Upper_Bound (Tree, Item);
381 raise Constraint_Error with "attempt to delete element not in set";
386 Node := Tree_Operations.Next (Node);
387 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
390 exit when Node = Done;
394 procedure Delete (Container : in out Set; Position : in out Cursor) is
396 if Position.Node = null then
397 raise Constraint_Error with "Position cursor equals No_Element";
400 if Position.Node.Element = null then
401 raise Program_Error with "Position cursor is bad";
404 if Position.Container /= Container'Unrestricted_Access then
405 raise Program_Error with "Position cursor designates wrong set";
408 pragma Assert (Vet (Container.Tree, Position.Node),
409 "bad cursor in Delete");
411 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
412 Free (Position.Node);
414 Position.Container := null;
421 procedure Delete_First (Container : in out Set) is
422 Tree : Tree_Type renames Container.Tree;
423 X : Node_Access := Tree.First;
430 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
438 procedure Delete_Last (Container : in out Set) is
439 Tree : Tree_Type renames Container.Tree;
440 X : Node_Access := Tree.Last;
447 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
455 procedure Difference (Target : in out Set; Source : Set) is
457 Set_Ops.Difference (Target.Tree, Source.Tree);
460 function Difference (Left, Right : Set) return Set is
461 Tree : constant Tree_Type :=
462 Set_Ops.Difference (Left.Tree, Right.Tree);
464 return Set'(Controlled
with Tree
);
471 function Element
(Position
: Cursor
) return Element_Type
is
473 if Position
.Node
= null then
474 raise Constraint_Error
with "Position cursor equals No_Element";
477 if Position
.Node
.Element
= null then
478 raise Program_Error
with "Position cursor is bad";
481 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
482 "bad cursor in Element");
484 return Position
.Node
.Element
.all;
487 -------------------------
488 -- Equivalent_Elements --
489 -------------------------
491 function Equivalent_Elements
(Left
, Right
: Element_Type
) return Boolean is
500 end Equivalent_Elements
;
502 ---------------------
503 -- Equivalent_Sets --
504 ---------------------
506 function Equivalent_Sets
(Left
, Right
: Set
) return Boolean is
508 function Is_Equivalent_Node_Node
(L
, R
: Node_Access
) return Boolean;
509 pragma Inline
(Is_Equivalent_Node_Node
);
511 function Is_Equivalent
is
512 new Tree_Operations
.Generic_Equal
(Is_Equivalent_Node_Node
);
514 -----------------------------
515 -- Is_Equivalent_Node_Node --
516 -----------------------------
518 function Is_Equivalent_Node_Node
(L
, R
: Node_Access
) return Boolean is
520 if L
.Element
.all < R
.Element
.all then
522 elsif R
.Element
.all < L
.Element
.all then
527 end Is_Equivalent_Node_Node
;
529 -- Start of processing for Equivalent_Sets
532 return Is_Equivalent
(Left
.Tree
, Right
.Tree
);
539 procedure Exclude
(Container
: in out Set
; Item
: Element_Type
) is
540 Tree
: Tree_Type
renames Container
.Tree
;
541 Node
: Node_Access
:= Element_Keys
.Ceiling
(Tree
, Item
);
542 Done
: constant Node_Access
:= Element_Keys
.Upper_Bound
(Tree
, Item
);
546 while Node
/= Done
loop
548 Node
:= Tree_Operations
.Next
(Node
);
549 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, X
);
558 function Find
(Container
: Set
; Item
: Element_Type
) return Cursor
is
559 Node
: constant Node_Access
:=
560 Element_Keys
.Find
(Container
.Tree
, Item
);
567 return Cursor
'(Container'Unrestricted_Access, Node);
574 function First (Container : Set) return Cursor is
576 if Container.Tree.First = null then
580 return Cursor'(Container
'Unrestricted_Access, Container
.Tree
.First
);
587 function First_Element
(Container
: Set
) return Element_Type
is
589 if Container
.Tree
.First
= null then
590 raise Constraint_Error
with "set is empty";
593 pragma Assert
(Container
.Tree
.First
.Element
/= null);
594 return Container
.Tree
.First
.Element
.all;
601 function Floor
(Container
: Set
; Item
: Element_Type
) return Cursor
is
602 Node
: constant Node_Access
:=
603 Element_Keys
.Floor
(Container
.Tree
, Item
);
610 return Cursor
'(Container'Unrestricted_Access, Node);
617 procedure Free (X : in out Node_Access) is
618 procedure Deallocate is
619 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
631 Free_Element (X.Element);
646 package body Generic_Keys is
648 -----------------------
649 -- Local Subprograms --
650 -----------------------
652 function Is_Less_Key_Node
654 Right : Node_Access) return Boolean;
655 pragma Inline (Is_Less_Key_Node);
657 function Is_Greater_Key_Node
659 Right : Node_Access) return Boolean;
660 pragma Inline (Is_Greater_Key_Node);
662 --------------------------
663 -- Local Instantiations --
664 --------------------------
667 new Red_Black_Trees.Generic_Keys
668 (Tree_Operations => Tree_Operations,
669 Key_Type => Key_Type,
670 Is_Less_Key_Node => Is_Less_Key_Node,
671 Is_Greater_Key_Node => Is_Greater_Key_Node);
677 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
678 Node : constant Node_Access :=
679 Key_Keys.Ceiling (Container.Tree, Key);
686 return Cursor'(Container
'Unrestricted_Access, Node
);
693 function Contains
(Container
: Set
; Key
: Key_Type
) return Boolean is
695 return Find
(Container
, Key
) /= No_Element
;
702 procedure Delete
(Container
: in out Set
; Key
: Key_Type
) is
703 Tree
: Tree_Type
renames Container
.Tree
;
704 Node
: Node_Access
:= Key_Keys
.Ceiling
(Tree
, Key
);
705 Done
: constant Node_Access
:= Key_Keys
.Upper_Bound
(Tree
, Key
);
710 raise Constraint_Error
with "attempt to delete key not in set";
715 Node
:= Tree_Operations
.Next
(Node
);
716 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, X
);
719 exit when Node
= Done
;
727 function Element
(Container
: Set
; Key
: Key_Type
) return Element_Type
is
728 Node
: constant Node_Access
:=
729 Key_Keys
.Find
(Container
.Tree
, Key
);
733 raise Constraint_Error
with "key not in set";
736 return Node
.Element
.all;
739 ---------------------
740 -- Equivalent_Keys --
741 ---------------------
743 function Equivalent_Keys
(Left
, Right
: Key_Type
) return Boolean is
758 procedure Exclude
(Container
: in out Set
; Key
: Key_Type
) is
759 Tree
: Tree_Type
renames Container
.Tree
;
760 Node
: Node_Access
:= Key_Keys
.Ceiling
(Tree
, Key
);
761 Done
: constant Node_Access
:= Key_Keys
.Upper_Bound
(Tree
, Key
);
765 while Node
/= Done
loop
767 Node
:= Tree_Operations
.Next
(Node
);
768 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, X
);
777 function Find
(Container
: Set
; Key
: Key_Type
) return Cursor
is
778 Node
: constant Node_Access
:= Key_Keys
.Find
(Container
.Tree
, Key
);
785 return Cursor
'(Container'Unrestricted_Access, Node);
792 function Floor (Container : Set; Key : Key_Type) return Cursor is
793 Node : constant Node_Access := Key_Keys.Floor (Container.Tree, Key);
800 return Cursor'(Container
'Unrestricted_Access, Node
);
803 -------------------------
804 -- Is_Greater_Key_Node --
805 -------------------------
807 function Is_Greater_Key_Node
809 Right
: Node_Access
) return Boolean
812 return Key
(Right
.Element
.all) < 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
824 return Left
< Key
(Right
.Element
.all);
825 end Is_Less_Key_Node
;
834 Process
: not null access procedure (Position
: Cursor
))
836 procedure Process_Node
(Node
: Node_Access
);
837 pragma Inline
(Process_Node
);
839 procedure Local_Iterate
is
840 new Key_Keys
.Generic_Iteration
(Process_Node
);
846 procedure Process_Node
(Node
: Node_Access
) is
848 Process
(Cursor
'(Container'Unrestricted_Access, Node));
851 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
852 B : Natural renames T.Busy;
854 -- Start of processing for Iterate
860 Local_Iterate (T, Key);
874 function Key (Position : Cursor) return Key_Type is
876 if Position.Node = null then
877 raise Constraint_Error with
878 "Position cursor equals No_Element";
881 if Position.Node.Element = null then
882 raise Program_Error with
883 "Position cursor is bad";
886 pragma Assert (Vet (Position.Container.Tree, Position.Node),
887 "bad cursor in Key");
889 return Key (Position.Node.Element.all);
892 ---------------------
893 -- Reverse_Iterate --
894 ---------------------
896 procedure Reverse_Iterate
899 Process : not null access procedure (Position : Cursor))
901 procedure Process_Node (Node : Node_Access);
902 pragma Inline (Process_Node);
908 procedure Local_Reverse_Iterate is
909 new Key_Keys.Generic_Reverse_Iteration (Process_Node);
915 procedure Process_Node (Node : Node_Access) is
917 Process (Cursor'(Container
'Unrestricted_Access, Node
));
920 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
921 B
: Natural renames T
.Busy
;
923 -- Start of processing for Reverse_Iterate
929 Local_Reverse_Iterate
(T
, Key
);
943 procedure Update_Element
944 (Container
: in out Set
;
946 Process
: not null access procedure (Element
: in out Element_Type
))
948 Tree
: Tree_Type
renames Container
.Tree
;
949 Node
: constant Node_Access
:= Position
.Node
;
953 raise Constraint_Error
with "Position cursor equals No_Element";
956 if Node
.Element
= null then
957 raise Program_Error
with "Position cursor is bad";
960 if Position
.Container
/= Container
'Unrestricted_Access then
961 raise Program_Error
with "Position cursor designates wrong set";
964 pragma Assert
(Vet
(Tree
, Node
),
965 "bad cursor in Update_Element");
968 E
: Element_Type
renames Node
.Element
.all;
969 K
: constant Key_Type
:= Key
(E
);
971 B
: Natural renames Tree
.Busy
;
972 L
: Natural renames Tree
.Lock
;
990 if Equivalent_Keys
(Left
=> K
, Right
=> Key
(E
)) then
995 -- Delete_Node checks busy-bit
997 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, Node
);
999 Insert_New_Item
: declare
1000 function New_Node
return Node_Access
;
1001 pragma Inline
(New_Node
);
1003 procedure Insert_Post
is
1004 new Element_Keys
.Generic_Insert_Post
(New_Node
);
1006 procedure Unconditional_Insert
is
1007 new Element_Keys
.Generic_Unconditional_Insert
(Insert_Post
);
1013 function New_Node
return Node_Access
is
1015 Node
.Color
:= Red_Black_Trees
.Red
;
1016 Node
.Parent
:= null;
1023 Result
: Node_Access
;
1025 -- Start of processing for Insert_New_Item
1028 Unconditional_Insert
1030 Key
=> Node
.Element
.all,
1033 pragma Assert
(Result
= Node
);
1034 end Insert_New_Item
;
1043 function Has_Element
(Position
: Cursor
) return Boolean is
1045 return Position
/= No_Element
;
1052 procedure Insert
(Container
: in out Set
; New_Item
: Element_Type
) is
1054 pragma Unreferenced
(Position
);
1056 Insert
(Container
, New_Item
, Position
);
1060 (Container
: in out Set
;
1061 New_Item
: Element_Type
;
1062 Position
: out Cursor
)
1065 Insert_Sans_Hint
(Container
.Tree
, New_Item
, Position
.Node
);
1066 Position
.Container
:= Container
'Unrestricted_Access;
1069 ----------------------
1070 -- Insert_Sans_Hint --
1071 ----------------------
1073 procedure Insert_Sans_Hint
1074 (Tree
: in out Tree_Type
;
1075 New_Item
: Element_Type
;
1076 Node
: out Node_Access
)
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
1092 Element
: Element_Access
:= new Element_Type
'(New_Item);
1095 return new Node_Type'(Parent
=> null,
1098 Color
=> Red_Black_Trees
.Red
,
1099 Element
=> Element
);
1102 Free_Element
(Element
);
1106 -- Start of processing for Insert_Sans_Hint
1109 Unconditional_Insert
(Tree
, New_Item
, Node
);
1110 end Insert_Sans_Hint
;
1112 ----------------------
1113 -- Insert_With_Hint --
1114 ----------------------
1116 procedure Insert_With_Hint
1117 (Dst_Tree
: in out Tree_Type
;
1118 Dst_Hint
: Node_Access
;
1119 Src_Node
: Node_Access
;
1120 Dst_Node
: out Node_Access
)
1122 function New_Node
return Node_Access
;
1123 pragma Inline
(New_Node
);
1125 procedure Insert_Post
is
1126 new Element_Keys
.Generic_Insert_Post
(New_Node
);
1128 procedure Insert_Sans_Hint
is
1129 new Element_Keys
.Generic_Unconditional_Insert
(Insert_Post
);
1131 procedure Local_Insert_With_Hint
is
1132 new Element_Keys
.Generic_Unconditional_Insert_With_Hint
1140 function New_Node
return Node_Access
is
1141 X
: Element_Access
:= new Element_Type
'(Src_Node.Element.all);
1144 return new Node_Type'(Parent
=> null,
1156 -- Start of processing for Insert_With_Hint
1159 Local_Insert_With_Hint
1162 Src_Node
.Element
.all,
1164 end Insert_With_Hint
;
1170 procedure Intersection
(Target
: in out Set
; Source
: Set
) is
1172 Set_Ops
.Intersection
(Target
.Tree
, Source
.Tree
);
1175 function Intersection
(Left
, Right
: Set
) return Set
is
1176 Tree
: constant Tree_Type
:=
1177 Set_Ops
.Intersection
(Left
.Tree
, Right
.Tree
);
1179 return Set
'(Controlled with Tree);
1186 function Is_Empty (Container : Set) return Boolean is
1188 return Container.Tree.Length = 0;
1191 ------------------------
1192 -- Is_Equal_Node_Node --
1193 ------------------------
1195 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
1197 return L.Element.all = R.Element.all;
1198 end Is_Equal_Node_Node;
1200 -----------------------------
1201 -- Is_Greater_Element_Node --
1202 -----------------------------
1204 function Is_Greater_Element_Node
1205 (Left : Element_Type;
1206 Right : Node_Access) return Boolean
1209 -- e > node same as node < e
1211 return Right.Element.all < Left;
1212 end Is_Greater_Element_Node;
1214 --------------------------
1215 -- Is_Less_Element_Node --
1216 --------------------------
1218 function Is_Less_Element_Node
1219 (Left : Element_Type;
1220 Right : Node_Access) return Boolean
1223 return Left < Right.Element.all;
1224 end Is_Less_Element_Node;
1226 -----------------------
1227 -- Is_Less_Node_Node --
1228 -----------------------
1230 function Is_Less_Node_Node (L, R : Node_Access) return Boolean is
1232 return L.Element.all < R.Element.all;
1233 end Is_Less_Node_Node;
1239 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1241 return Set_Ops.Is_Subset (Subset => Subset.Tree, Of_Set => Of_Set.Tree);
1250 Item : Element_Type;
1251 Process : not null access procedure (Position : Cursor))
1253 procedure Process_Node (Node : Node_Access);
1254 pragma Inline (Process_Node);
1256 procedure Local_Iterate is
1257 new Element_Keys.Generic_Iteration (Process_Node);
1263 procedure Process_Node (Node : Node_Access) is
1265 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1268 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
1269 B
: Natural renames T
.Busy
;
1271 -- Start of processing for Iterate
1277 Local_Iterate
(T
, Item
);
1289 Process
: not null access procedure (Position
: Cursor
))
1291 procedure Process_Node
(Node
: Node_Access
);
1292 pragma Inline
(Process_Node
);
1294 procedure Local_Iterate
is
1295 new Tree_Operations
.Generic_Iteration
(Process_Node
);
1301 procedure Process_Node
(Node
: Node_Access
) is
1303 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1306 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1307 B : Natural renames T.Busy;
1309 -- Start of processing for Iterate
1329 function Last (Container : Set) return Cursor is
1331 if Container.Tree.Last = null then
1335 return Cursor'(Container
'Unrestricted_Access, Container
.Tree
.Last
);
1342 function Last_Element
(Container
: Set
) return Element_Type
is
1344 if Container
.Tree
.Last
= null then
1345 raise Constraint_Error
with "set is empty";
1348 pragma Assert
(Container
.Tree
.Last
.Element
/= null);
1349 return Container
.Tree
.Last
.Element
.all;
1356 function Left
(Node
: Node_Access
) return Node_Access
is
1365 function Length
(Container
: Set
) return Count_Type
is
1367 return Container
.Tree
.Length
;
1375 new Tree_Operations
.Generic_Move
(Clear
);
1377 procedure Move
(Target
: in out Set
; Source
: in out Set
) is
1379 Move
(Target
=> Target
.Tree
, Source
=> Source
.Tree
);
1386 function Next
(Position
: Cursor
) return Cursor
is
1388 if Position
= No_Element
then
1392 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
1393 "bad cursor in Next");
1396 Node
: constant Node_Access
:=
1397 Tree_Operations
.Next
(Position
.Node
);
1404 return Cursor
'(Position.Container, Node);
1408 procedure Next (Position : in out Cursor) is
1410 Position := Next (Position);
1417 function Overlap (Left, Right : Set) return Boolean is
1419 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1426 function Parent (Node : Node_Access) return Node_Access is
1435 function Previous (Position : Cursor) return Cursor is
1437 if Position = No_Element then
1441 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1442 "bad cursor in Previous");
1445 Node : constant Node_Access :=
1446 Tree_Operations.Previous (Position.Node);
1453 return Cursor'(Position
.Container
, Node
);
1457 procedure Previous
(Position
: in out Cursor
) is
1459 Position
:= Previous
(Position
);
1466 procedure Query_Element
1468 Process
: not null access procedure (Element
: Element_Type
))
1471 if Position
.Node
= null then
1472 raise Constraint_Error
with "Position cursor equals No_Element";
1475 if Position
.Node
.Element
= null then
1476 raise Program_Error
with "Position cursor is bad";
1479 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
1480 "bad cursor in Query_Element");
1483 T
: Tree_Type
renames Position
.Container
.Tree
;
1485 B
: Natural renames T
.Busy
;
1486 L
: Natural renames T
.Lock
;
1493 Process
(Position
.Node
.Element
.all);
1511 (Stream
: not null access Root_Stream_Type
'Class;
1512 Container
: out Set
)
1515 (Stream
: not null access Root_Stream_Type
'Class) return Node_Access
;
1516 pragma Inline
(Read_Node
);
1519 new Tree_Operations
.Generic_Read
(Clear
, Read_Node
);
1526 (Stream
: not null access Root_Stream_Type
'Class) return Node_Access
1528 Node
: Node_Access
:= new Node_Type
;
1530 Node
.Element
:= new Element_Type
'(Element_Type'Input (Stream));
1534 Free (Node); -- Note that Free deallocates elem too
1538 -- Start of processing for Read
1541 Read (Stream, Container.Tree);
1545 (Stream : not null access Root_Stream_Type'Class;
1549 raise Program_Error with "attempt to stream set cursor";
1552 ---------------------
1553 -- Replace_Element --
1554 ---------------------
1556 procedure Replace_Element
1557 (Tree : in out Tree_Type;
1559 Item : Element_Type)
1562 if Item < Node.Element.all
1563 or else Node.Element.all < Item
1567 if Tree.Lock > 0 then
1568 raise Program_Error with
1569 "attempt to tamper with cursors (set is locked)";
1573 X : Element_Access := Node.Element;
1575 Node.Element := new Element_Type'(Item
);
1582 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, Node
); -- Checks busy-bit
1584 Insert_New_Item
: declare
1585 function New_Node
return Node_Access
;
1586 pragma Inline
(New_Node
);
1588 procedure Insert_Post
is
1589 new Element_Keys
.Generic_Insert_Post
(New_Node
);
1591 procedure Unconditional_Insert
is
1592 new Element_Keys
.Generic_Unconditional_Insert
(Insert_Post
);
1598 function New_Node
return Node_Access
is
1600 Node
.Element
:= new Element_Type
'(Item); -- OK if fails
1601 Node.Color := Red_Black_Trees.Red;
1602 Node.Parent := null;
1609 Result : Node_Access;
1611 X : Element_Access := Node.Element;
1613 -- Start of processing for Insert_New_Item
1616 Unconditional_Insert
1620 pragma Assert (Result = Node);
1622 Free_Element (X); -- OK if fails
1623 end Insert_New_Item;
1624 end Replace_Element;
1626 procedure Replace_Element
1627 (Container : in out Set;
1629 New_Item : Element_Type)
1632 if Position.Node = null then
1633 raise Constraint_Error with "Position cursor equals No_Element";
1636 if Position.Node.Element = null then
1637 raise Program_Error with "Position cursor is bad";
1640 if Position.Container /= Container'Unrestricted_Access then
1641 raise Program_Error with "Position cursor designates wrong set";
1644 pragma Assert (Vet (Container.Tree, Position.Node),
1645 "bad cursor in Replace_Element");
1647 Replace_Element (Container.Tree, Position.Node, New_Item);
1648 end Replace_Element;
1650 ---------------------
1651 -- Reverse_Iterate --
1652 ---------------------
1654 procedure Reverse_Iterate
1656 Item : Element_Type;
1657 Process : not null access procedure (Position : Cursor))
1659 procedure Process_Node (Node : Node_Access);
1660 pragma Inline (Process_Node);
1662 procedure Local_Reverse_Iterate is
1663 new Element_Keys.Generic_Reverse_Iteration (Process_Node);
1669 procedure Process_Node (Node : Node_Access) is
1671 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1674 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
1675 B
: Natural renames T
.Busy
;
1677 -- Start of processing for Reverse_Iterate
1683 Local_Reverse_Iterate
(T
, Item
);
1691 end Reverse_Iterate
;
1693 procedure Reverse_Iterate
1695 Process
: not null access procedure (Position
: Cursor
))
1697 procedure Process_Node
(Node
: Node_Access
);
1698 pragma Inline
(Process_Node
);
1700 procedure Local_Reverse_Iterate
is
1701 new Tree_Operations
.Generic_Reverse_Iteration
(Process_Node
);
1707 procedure Process_Node
(Node
: Node_Access
) is
1709 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1712 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1713 B : Natural renames T.Busy;
1715 -- Start of processing for Reverse_Iterate
1721 Local_Reverse_Iterate (T);
1729 end Reverse_Iterate;
1735 function Right (Node : Node_Access) return Node_Access is
1744 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1746 Node.Color := Color;
1753 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1762 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1764 Node.Parent := Parent;
1771 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1773 Node.Right := Right;
1776 --------------------------
1777 -- Symmetric_Difference --
1778 --------------------------
1780 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1782 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
1783 end Symmetric_Difference;
1785 function Symmetric_Difference (Left, Right : Set) return Set is
1786 Tree : constant Tree_Type :=
1787 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
1789 return Set'(Controlled
with Tree
);
1790 end Symmetric_Difference
;
1796 function To_Set
(New_Item
: Element_Type
) return Set
is
1799 pragma Unreferenced
(Node
);
1801 Insert_Sans_Hint
(Tree
, New_Item
, Node
);
1802 return Set
'(Controlled with Tree);
1809 procedure Union (Target : in out Set; Source : Set) is
1811 Set_Ops.Union (Target.Tree, Source.Tree);
1814 function Union (Left, Right : Set) return Set is
1815 Tree : constant Tree_Type :=
1816 Set_Ops.Union (Left.Tree, Right.Tree);
1818 return Set'(Controlled
with Tree
);
1826 (Stream
: not null access Root_Stream_Type
'Class;
1829 procedure Write_Node
1830 (Stream
: not null access Root_Stream_Type
'Class;
1831 Node
: Node_Access
);
1832 pragma Inline
(Write_Node
);
1835 new Tree_Operations
.Generic_Write
(Write_Node
);
1841 procedure Write_Node
1842 (Stream
: not null access Root_Stream_Type
'Class;
1846 Element_Type
'Output (Stream
, Node
.Element
.all);
1849 -- Start of processing for Write
1852 Write
(Stream
, Container
.Tree
);
1856 (Stream
: not null access Root_Stream_Type
'Class;
1860 raise Program_Error
with "attempt to stream set cursor";
1863 end Ada
.Containers
.Indefinite_Ordered_Multisets
;