1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . --
6 -- I N D E F I N I T E _ O R D E R E D _ M U L T I S E T S --
10 -- Copyright (C) 2004-2007, Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
21 -- Boston, MA 02110-1301, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- This unit was originally developed by Matthew J Heaney. --
31 ------------------------------------------------------------------------------
33 with Ada
.Unchecked_Deallocation
;
35 with Ada
.Containers
.Red_Black_Trees
.Generic_Operations
;
36 pragma Elaborate_All
(Ada
.Containers
.Red_Black_Trees
.Generic_Operations
);
38 with Ada
.Containers
.Red_Black_Trees
.Generic_Keys
;
39 pragma Elaborate_All
(Ada
.Containers
.Red_Black_Trees
.Generic_Keys
);
41 with Ada
.Containers
.Red_Black_Trees
.Generic_Set_Operations
;
42 pragma Elaborate_All
(Ada
.Containers
.Red_Black_Trees
.Generic_Set_Operations
);
44 package body Ada
.Containers
.Indefinite_Ordered_Multisets
is
46 -----------------------------
47 -- Node Access Subprograms --
48 -----------------------------
50 -- These subprograms provide a functional interface to access fields
51 -- of a node, and a procedural interface for modifying these values.
53 function Color
(Node
: Node_Access
) return Color_Type
;
54 pragma Inline
(Color
);
56 function Left
(Node
: Node_Access
) return Node_Access
;
59 function Parent
(Node
: Node_Access
) return Node_Access
;
60 pragma Inline
(Parent
);
62 function Right
(Node
: Node_Access
) return Node_Access
;
63 pragma Inline
(Right
);
65 procedure Set_Parent
(Node
: Node_Access
; Parent
: Node_Access
);
66 pragma Inline
(Set_Parent
);
68 procedure Set_Left
(Node
: Node_Access
; Left
: Node_Access
);
69 pragma Inline
(Set_Left
);
71 procedure Set_Right
(Node
: Node_Access
; Right
: Node_Access
);
72 pragma Inline
(Set_Right
);
74 procedure Set_Color
(Node
: Node_Access
; Color
: Color_Type
);
75 pragma Inline
(Set_Color
);
77 -----------------------
78 -- Local Subprograms --
79 -----------------------
81 function Copy_Node
(Source
: Node_Access
) return Node_Access
;
82 pragma Inline
(Copy_Node
);
84 procedure Free
(X
: in out Node_Access
);
86 procedure Insert_Sans_Hint
87 (Tree
: in out Tree_Type
;
88 New_Item
: Element_Type
;
89 Node
: out Node_Access
);
91 procedure Insert_With_Hint
92 (Dst_Tree
: in out Tree_Type
;
93 Dst_Hint
: Node_Access
;
94 Src_Node
: Node_Access
;
95 Dst_Node
: out Node_Access
);
97 function Is_Equal_Node_Node
(L
, R
: Node_Access
) return Boolean;
98 pragma Inline
(Is_Equal_Node_Node
);
100 function Is_Greater_Element_Node
101 (Left
: Element_Type
;
102 Right
: Node_Access
) return Boolean;
103 pragma Inline
(Is_Greater_Element_Node
);
105 function Is_Less_Element_Node
106 (Left
: Element_Type
;
107 Right
: Node_Access
) return Boolean;
108 pragma Inline
(Is_Less_Element_Node
);
110 function Is_Less_Node_Node
(L
, R
: Node_Access
) return Boolean;
111 pragma Inline
(Is_Less_Node_Node
);
113 procedure Replace_Element
114 (Tree
: in out Tree_Type
;
116 Item
: Element_Type
);
118 --------------------------
119 -- Local Instantiations --
120 --------------------------
122 package Tree_Operations
is
123 new Red_Black_Trees
.Generic_Operations
(Tree_Types
);
125 procedure Delete_Tree
is
126 new Tree_Operations
.Generic_Delete_Tree
(Free
);
128 function Copy_Tree
is
129 new Tree_Operations
.Generic_Copy_Tree
(Copy_Node
, Delete_Tree
);
133 procedure Free_Element
is
134 new Ada
.Unchecked_Deallocation
(Element_Type
, Element_Access
);
137 new Tree_Operations
.Generic_Equal
(Is_Equal_Node_Node
);
140 new Generic_Set_Operations
141 (Tree_Operations
=> Tree_Operations
,
142 Insert_With_Hint
=> Insert_With_Hint
,
143 Copy_Tree
=> Copy_Tree
,
144 Delete_Tree
=> Delete_Tree
,
145 Is_Less
=> Is_Less_Node_Node
,
148 package Element_Keys
is
149 new Red_Black_Trees
.Generic_Keys
150 (Tree_Operations
=> Tree_Operations
,
151 Key_Type
=> Element_Type
,
152 Is_Less_Key_Node
=> Is_Less_Element_Node
,
153 Is_Greater_Key_Node
=> Is_Greater_Element_Node
);
159 function "<" (Left
, Right
: Cursor
) return Boolean is
161 if Left
.Node
= null then
162 raise Constraint_Error
with "Left cursor equals No_Element";
165 if Right
.Node
= null then
166 raise Constraint_Error
with "Right cursor equals No_Element";
169 if Left
.Node
.Element
= null then
170 raise Program_Error
with "Left cursor is bad";
173 if Right
.Node
.Element
= null then
174 raise Program_Error
with "Right cursor is bad";
177 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
178 "bad Left cursor in ""<""");
180 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
181 "bad Right cursor in ""<""");
183 return Left
.Node
.Element
.all < Right
.Node
.Element
.all;
186 function "<" (Left
: Cursor
; Right
: Element_Type
) return Boolean is
188 if Left
.Node
= null then
189 raise Constraint_Error
with "Left cursor equals No_Element";
192 if Left
.Node
.Element
= null then
193 raise Program_Error
with "Left cursor is bad";
196 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
197 "bad Left cursor in ""<""");
199 return Left
.Node
.Element
.all < Right
;
202 function "<" (Left
: Element_Type
; Right
: Cursor
) return Boolean is
204 if Right
.Node
= null then
205 raise Constraint_Error
with "Right cursor equals No_Element";
208 if Right
.Node
.Element
= null then
209 raise Program_Error
with "Right cursor is bad";
212 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
213 "bad Right cursor in ""<""");
215 return Left
< Right
.Node
.Element
.all;
222 function "=" (Left
, Right
: Set
) return Boolean is
224 return Is_Equal
(Left
.Tree
, Right
.Tree
);
231 function ">" (Left
, Right
: Cursor
) return Boolean is
233 if Left
.Node
= null then
234 raise Constraint_Error
with "Left cursor equals No_Element";
237 if Right
.Node
= null then
238 raise Constraint_Error
with "Right cursor equals No_Element";
241 if Left
.Node
.Element
= null then
242 raise Program_Error
with "Left cursor is bad";
245 if Right
.Node
.Element
= null then
246 raise Program_Error
with "Right cursor is bad";
249 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
250 "bad Left cursor in "">""");
252 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
253 "bad Right cursor in "">""");
255 -- L > R same as R < L
257 return Right
.Node
.Element
.all < Left
.Node
.Element
.all;
260 function ">" (Left
: Cursor
; Right
: Element_Type
) return Boolean is
262 if Left
.Node
= null then
263 raise Constraint_Error
with "Left cursor equals No_Element";
266 if Left
.Node
.Element
= null then
267 raise Program_Error
with "Left cursor is bad";
270 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
271 "bad Left cursor in "">""");
273 return Right
< Left
.Node
.Element
.all;
276 function ">" (Left
: Element_Type
; Right
: Cursor
) return Boolean is
278 if Right
.Node
= null then
279 raise Constraint_Error
with "Right cursor equals No_Element";
282 if Right
.Node
.Element
= null then
283 raise Program_Error
with "Right cursor is bad";
286 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
287 "bad Right cursor in "">""");
289 return Right
.Node
.Element
.all < Left
;
297 new Tree_Operations
.Generic_Adjust
(Copy_Tree
);
299 procedure Adjust
(Container
: in out Set
) is
301 Adjust
(Container
.Tree
);
308 function Ceiling
(Container
: Set
; Item
: Element_Type
) return Cursor
is
309 Node
: constant Node_Access
:=
310 Element_Keys
.Ceiling
(Container
.Tree
, Item
);
317 return Cursor
'(Container'Unrestricted_Access, Node);
325 new Tree_Operations.Generic_Clear (Delete_Tree);
327 procedure Clear (Container : in out Set) is
329 Clear (Container.Tree);
336 function Color (Node : Node_Access) return Color_Type is
345 function Contains (Container : Set; Item : Element_Type) return Boolean is
347 return Find (Container, Item) /= No_Element;
354 function Copy_Node (Source : Node_Access) return Node_Access is
355 X : Element_Access := new Element_Type'(Source
.Element
.all);
358 return new Node_Type
'(Parent => null,
361 Color => Source.Color,
374 procedure Delete (Container : in out Set; Item : Element_Type) is
375 Tree : Tree_Type renames Container.Tree;
376 Node : Node_Access := Element_Keys.Ceiling (Tree, Item);
377 Done : constant Node_Access := Element_Keys.Upper_Bound (Tree, Item);
382 raise Constraint_Error with "attempt to delete element not in set";
387 Node := Tree_Operations.Next (Node);
388 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
391 exit when Node = Done;
395 procedure Delete (Container : in out Set; Position : in out Cursor) is
397 if Position.Node = null then
398 raise Constraint_Error with "Position cursor equals No_Element";
401 if Position.Node.Element = null then
402 raise Program_Error with "Position cursor is bad";
405 if Position.Container /= Container'Unrestricted_Access then
406 raise Program_Error with "Position cursor designates wrong set";
409 pragma Assert (Vet (Container.Tree, Position.Node),
410 "bad cursor in Delete");
412 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
413 Free (Position.Node);
415 Position.Container := null;
422 procedure Delete_First (Container : in out Set) is
423 Tree : Tree_Type renames Container.Tree;
424 X : Node_Access := Tree.First;
431 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
439 procedure Delete_Last (Container : in out Set) is
440 Tree : Tree_Type renames Container.Tree;
441 X : Node_Access := Tree.Last;
448 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
456 procedure Difference (Target : in out Set; Source : Set) is
458 Set_Ops.Difference (Target.Tree, Source.Tree);
461 function Difference (Left, Right : Set) return Set is
462 Tree : constant Tree_Type :=
463 Set_Ops.Difference (Left.Tree, Right.Tree);
465 return Set'(Controlled
with Tree
);
472 function Element
(Position
: Cursor
) return Element_Type
is
474 if Position
.Node
= null then
475 raise Constraint_Error
with "Position cursor equals No_Element";
478 if Position
.Node
.Element
= null then
479 raise Program_Error
with "Position cursor is bad";
482 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
483 "bad cursor in Element");
485 return Position
.Node
.Element
.all;
488 -------------------------
489 -- Equivalent_Elements --
490 -------------------------
492 function Equivalent_Elements
(Left
, Right
: Element_Type
) return Boolean is
501 end Equivalent_Elements
;
503 ---------------------
504 -- Equivalent_Sets --
505 ---------------------
507 function Equivalent_Sets
(Left
, Right
: Set
) return Boolean is
509 function Is_Equivalent_Node_Node
(L
, R
: Node_Access
) return Boolean;
510 pragma Inline
(Is_Equivalent_Node_Node
);
512 function Is_Equivalent
is
513 new Tree_Operations
.Generic_Equal
(Is_Equivalent_Node_Node
);
515 -----------------------------
516 -- Is_Equivalent_Node_Node --
517 -----------------------------
519 function Is_Equivalent_Node_Node
(L
, R
: Node_Access
) return Boolean is
521 if L
.Element
.all < R
.Element
.all then
523 elsif R
.Element
.all < L
.Element
.all then
528 end Is_Equivalent_Node_Node
;
530 -- Start of processing for Equivalent_Sets
533 return Is_Equivalent
(Left
.Tree
, Right
.Tree
);
540 procedure Exclude
(Container
: in out Set
; Item
: Element_Type
) is
541 Tree
: Tree_Type
renames Container
.Tree
;
542 Node
: Node_Access
:= Element_Keys
.Ceiling
(Tree
, Item
);
543 Done
: constant Node_Access
:= Element_Keys
.Upper_Bound
(Tree
, Item
);
547 while Node
/= Done
loop
549 Node
:= Tree_Operations
.Next
(Node
);
550 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, X
);
559 function Find
(Container
: Set
; Item
: Element_Type
) return Cursor
is
560 Node
: constant Node_Access
:=
561 Element_Keys
.Find
(Container
.Tree
, Item
);
568 return Cursor
'(Container'Unrestricted_Access, Node);
575 function First (Container : Set) return Cursor is
577 if Container.Tree.First = null then
581 return Cursor'(Container
'Unrestricted_Access, Container
.Tree
.First
);
588 function First_Element
(Container
: Set
) return Element_Type
is
590 if Container
.Tree
.First
= null then
591 raise Constraint_Error
with "set is empty";
594 pragma Assert
(Container
.Tree
.First
.Element
/= null);
595 return Container
.Tree
.First
.Element
.all;
602 function Floor
(Container
: Set
; Item
: Element_Type
) return Cursor
is
603 Node
: constant Node_Access
:=
604 Element_Keys
.Floor
(Container
.Tree
, Item
);
611 return Cursor
'(Container'Unrestricted_Access, Node);
618 procedure Free (X : in out Node_Access) is
619 procedure Deallocate is
620 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
632 Free_Element (X.Element);
647 package body Generic_Keys is
649 -----------------------
650 -- Local Subprograms --
651 -----------------------
653 function Is_Less_Key_Node
655 Right : Node_Access) return Boolean;
656 pragma Inline (Is_Less_Key_Node);
658 function Is_Greater_Key_Node
660 Right : Node_Access) return Boolean;
661 pragma Inline (Is_Greater_Key_Node);
663 --------------------------
664 -- Local Instantiations --
665 --------------------------
668 new Red_Black_Trees.Generic_Keys
669 (Tree_Operations => Tree_Operations,
670 Key_Type => Key_Type,
671 Is_Less_Key_Node => Is_Less_Key_Node,
672 Is_Greater_Key_Node => Is_Greater_Key_Node);
678 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
679 Node : constant Node_Access :=
680 Key_Keys.Ceiling (Container.Tree, Key);
687 return Cursor'(Container
'Unrestricted_Access, Node
);
694 function Contains
(Container
: Set
; Key
: Key_Type
) return Boolean is
696 return Find
(Container
, Key
) /= No_Element
;
703 procedure Delete
(Container
: in out Set
; Key
: Key_Type
) is
704 Tree
: Tree_Type
renames Container
.Tree
;
705 Node
: Node_Access
:= Key_Keys
.Ceiling
(Tree
, Key
);
706 Done
: constant Node_Access
:= Key_Keys
.Upper_Bound
(Tree
, Key
);
711 raise Constraint_Error
with "attempt to delete key not in set";
716 Node
:= Tree_Operations
.Next
(Node
);
717 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, X
);
720 exit when Node
= Done
;
728 function Element
(Container
: Set
; Key
: Key_Type
) return Element_Type
is
729 Node
: constant Node_Access
:=
730 Key_Keys
.Find
(Container
.Tree
, Key
);
734 raise Constraint_Error
with "key not in set";
737 return Node
.Element
.all;
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
813 return Key
(Right
.Element
.all) < Left
;
814 end Is_Greater_Key_Node
;
816 ----------------------
817 -- Is_Less_Key_Node --
818 ----------------------
820 function Is_Less_Key_Node
822 Right
: Node_Access
) return Boolean
825 return Left
< Key
(Right
.Element
.all);
826 end Is_Less_Key_Node
;
835 Process
: not null access procedure (Position
: Cursor
))
837 procedure Process_Node
(Node
: Node_Access
);
838 pragma Inline
(Process_Node
);
840 procedure Local_Iterate
is
841 new Key_Keys
.Generic_Iteration
(Process_Node
);
847 procedure Process_Node
(Node
: Node_Access
) is
849 Process
(Cursor
'(Container'Unrestricted_Access, Node));
852 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
853 B : Natural renames T.Busy;
855 -- Start of processing for Iterate
861 Local_Iterate (T, Key);
875 function Key (Position : Cursor) return Key_Type is
877 if Position.Node = null then
878 raise Constraint_Error with
879 "Position cursor equals No_Element";
882 if Position.Node.Element = null then
883 raise Program_Error with
884 "Position cursor is bad";
887 pragma Assert (Vet (Position.Container.Tree, Position.Node),
888 "bad cursor in Key");
890 return Key (Position.Node.Element.all);
893 ---------------------
894 -- Reverse_Iterate --
895 ---------------------
897 procedure Reverse_Iterate
900 Process : not null access procedure (Position : Cursor))
902 procedure Process_Node (Node : Node_Access);
903 pragma Inline (Process_Node);
909 procedure Local_Reverse_Iterate is
910 new Key_Keys.Generic_Reverse_Iteration (Process_Node);
916 procedure Process_Node (Node : Node_Access) is
918 Process (Cursor'(Container
'Unrestricted_Access, Node
));
921 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
922 B
: Natural renames T
.Busy
;
924 -- Start of processing for Reverse_Iterate
930 Local_Reverse_Iterate
(T
, Key
);
944 procedure Update_Element
945 (Container
: in out Set
;
947 Process
: not null access procedure (Element
: in out Element_Type
))
949 Tree
: Tree_Type
renames Container
.Tree
;
950 Node
: constant Node_Access
:= Position
.Node
;
954 raise Constraint_Error
with "Position cursor equals No_Element";
957 if Node
.Element
= null then
958 raise Program_Error
with "Position cursor is bad";
961 if Position
.Container
/= Container
'Unrestricted_Access then
962 raise Program_Error
with "Position cursor designates wrong set";
965 pragma Assert
(Vet
(Tree
, Node
),
966 "bad cursor in Update_Element");
969 E
: Element_Type
renames Node
.Element
.all;
970 K
: constant Key_Type
:= Key
(E
);
972 B
: Natural renames Tree
.Busy
;
973 L
: Natural renames Tree
.Lock
;
991 if Equivalent_Keys
(Left
=> K
, Right
=> Key
(E
)) then
996 -- Delete_Node checks busy-bit
998 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, Node
);
1000 Insert_New_Item
: declare
1001 function New_Node
return Node_Access
;
1002 pragma Inline
(New_Node
);
1004 procedure Insert_Post
is
1005 new Element_Keys
.Generic_Insert_Post
(New_Node
);
1007 procedure Unconditional_Insert
is
1008 new Element_Keys
.Generic_Unconditional_Insert
(Insert_Post
);
1014 function New_Node
return Node_Access
is
1016 Node
.Color
:= Red_Black_Trees
.Red
;
1017 Node
.Parent
:= null;
1024 Result
: Node_Access
;
1026 -- Start of processing for Insert_New_Item
1029 Unconditional_Insert
1031 Key
=> Node
.Element
.all,
1034 pragma Assert
(Result
= Node
);
1035 end Insert_New_Item
;
1044 function Has_Element
(Position
: Cursor
) return Boolean is
1046 return Position
/= No_Element
;
1053 procedure Insert
(Container
: in out Set
; New_Item
: Element_Type
) is
1055 pragma Unreferenced
(Position
);
1057 Insert
(Container
, New_Item
, Position
);
1061 (Container
: in out Set
;
1062 New_Item
: Element_Type
;
1063 Position
: out Cursor
)
1066 Insert_Sans_Hint
(Container
.Tree
, New_Item
, Position
.Node
);
1067 Position
.Container
:= Container
'Unrestricted_Access;
1070 ----------------------
1071 -- Insert_Sans_Hint --
1072 ----------------------
1074 procedure Insert_Sans_Hint
1075 (Tree
: in out Tree_Type
;
1076 New_Item
: Element_Type
;
1077 Node
: out Node_Access
)
1079 function New_Node
return Node_Access
;
1080 pragma Inline
(New_Node
);
1082 procedure Insert_Post
is
1083 new Element_Keys
.Generic_Insert_Post
(New_Node
);
1085 procedure Unconditional_Insert
is
1086 new Element_Keys
.Generic_Unconditional_Insert
(Insert_Post
);
1092 function New_Node
return Node_Access
is
1093 Element
: Element_Access
:= new Element_Type
'(New_Item);
1096 return new Node_Type'(Parent
=> null,
1099 Color
=> Red_Black_Trees
.Red
,
1100 Element
=> Element
);
1103 Free_Element
(Element
);
1107 -- Start of processing for Insert_Sans_Hint
1110 Unconditional_Insert
(Tree
, New_Item
, Node
);
1111 end Insert_Sans_Hint
;
1113 ----------------------
1114 -- Insert_With_Hint --
1115 ----------------------
1117 procedure Insert_With_Hint
1118 (Dst_Tree
: in out Tree_Type
;
1119 Dst_Hint
: Node_Access
;
1120 Src_Node
: Node_Access
;
1121 Dst_Node
: out Node_Access
)
1123 function New_Node
return Node_Access
;
1124 pragma Inline
(New_Node
);
1126 procedure Insert_Post
is
1127 new Element_Keys
.Generic_Insert_Post
(New_Node
);
1129 procedure Insert_Sans_Hint
is
1130 new Element_Keys
.Generic_Unconditional_Insert
(Insert_Post
);
1132 procedure Local_Insert_With_Hint
is
1133 new Element_Keys
.Generic_Unconditional_Insert_With_Hint
1141 function New_Node
return Node_Access
is
1142 X
: Element_Access
:= new Element_Type
'(Src_Node.Element.all);
1145 return new Node_Type'(Parent
=> null,
1157 -- Start of processing for Insert_With_Hint
1160 Local_Insert_With_Hint
1163 Src_Node
.Element
.all,
1165 end Insert_With_Hint
;
1171 procedure Intersection
(Target
: in out Set
; Source
: Set
) is
1173 Set_Ops
.Intersection
(Target
.Tree
, Source
.Tree
);
1176 function Intersection
(Left
, Right
: Set
) return Set
is
1177 Tree
: constant Tree_Type
:=
1178 Set_Ops
.Intersection
(Left
.Tree
, Right
.Tree
);
1180 return Set
'(Controlled with Tree);
1187 function Is_Empty (Container : Set) return Boolean is
1189 return Container.Tree.Length = 0;
1192 ------------------------
1193 -- Is_Equal_Node_Node --
1194 ------------------------
1196 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
1198 return L.Element.all = R.Element.all;
1199 end Is_Equal_Node_Node;
1201 -----------------------------
1202 -- Is_Greater_Element_Node --
1203 -----------------------------
1205 function Is_Greater_Element_Node
1206 (Left : Element_Type;
1207 Right : Node_Access) return Boolean
1210 -- e > node same as node < e
1212 return Right.Element.all < Left;
1213 end Is_Greater_Element_Node;
1215 --------------------------
1216 -- Is_Less_Element_Node --
1217 --------------------------
1219 function Is_Less_Element_Node
1220 (Left : Element_Type;
1221 Right : Node_Access) return Boolean
1224 return Left < Right.Element.all;
1225 end Is_Less_Element_Node;
1227 -----------------------
1228 -- Is_Less_Node_Node --
1229 -----------------------
1231 function Is_Less_Node_Node (L, R : Node_Access) return Boolean is
1233 return L.Element.all < R.Element.all;
1234 end Is_Less_Node_Node;
1240 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1242 return Set_Ops.Is_Subset (Subset => Subset.Tree, Of_Set => Of_Set.Tree);
1251 Item : Element_Type;
1252 Process : not null access procedure (Position : Cursor))
1254 procedure Process_Node (Node : Node_Access);
1255 pragma Inline (Process_Node);
1257 procedure Local_Iterate is
1258 new Element_Keys.Generic_Iteration (Process_Node);
1264 procedure Process_Node (Node : Node_Access) is
1266 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1269 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
1270 B
: Natural renames T
.Busy
;
1272 -- Start of processing for Iterate
1278 Local_Iterate
(T
, Item
);
1290 Process
: not null access procedure (Position
: Cursor
))
1292 procedure Process_Node
(Node
: Node_Access
);
1293 pragma Inline
(Process_Node
);
1295 procedure Local_Iterate
is
1296 new Tree_Operations
.Generic_Iteration
(Process_Node
);
1302 procedure Process_Node
(Node
: Node_Access
) is
1304 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1307 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1308 B : Natural renames T.Busy;
1310 -- Start of processing for Iterate
1330 function Last (Container : Set) return Cursor is
1332 if Container.Tree.Last = null then
1336 return Cursor'(Container
'Unrestricted_Access, Container
.Tree
.Last
);
1343 function Last_Element
(Container
: Set
) return Element_Type
is
1345 if Container
.Tree
.Last
= null then
1346 raise Constraint_Error
with "set is empty";
1349 pragma Assert
(Container
.Tree
.Last
.Element
/= null);
1350 return Container
.Tree
.Last
.Element
.all;
1357 function Left
(Node
: Node_Access
) return Node_Access
is
1366 function Length
(Container
: Set
) return Count_Type
is
1368 return Container
.Tree
.Length
;
1376 new Tree_Operations
.Generic_Move
(Clear
);
1378 procedure Move
(Target
: in out Set
; Source
: in out Set
) is
1380 Move
(Target
=> Target
.Tree
, Source
=> Source
.Tree
);
1387 function Next
(Position
: Cursor
) return Cursor
is
1389 if Position
= No_Element
then
1393 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
1394 "bad cursor in Next");
1397 Node
: constant Node_Access
:=
1398 Tree_Operations
.Next
(Position
.Node
);
1405 return Cursor
'(Position.Container, Node);
1409 procedure Next (Position : in out Cursor) is
1411 Position := Next (Position);
1418 function Overlap (Left, Right : Set) return Boolean is
1420 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1427 function Parent (Node : Node_Access) return Node_Access is
1436 function Previous (Position : Cursor) return Cursor is
1438 if Position = No_Element then
1442 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1443 "bad cursor in Previous");
1446 Node : constant Node_Access :=
1447 Tree_Operations.Previous (Position.Node);
1454 return Cursor'(Position
.Container
, Node
);
1458 procedure Previous
(Position
: in out Cursor
) is
1460 Position
:= Previous
(Position
);
1467 procedure Query_Element
1469 Process
: not null access procedure (Element
: Element_Type
))
1472 if Position
.Node
= null then
1473 raise Constraint_Error
with "Position cursor equals No_Element";
1476 if Position
.Node
.Element
= null then
1477 raise Program_Error
with "Position cursor is bad";
1480 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
1481 "bad cursor in Query_Element");
1484 T
: Tree_Type
renames Position
.Container
.Tree
;
1486 B
: Natural renames T
.Busy
;
1487 L
: Natural renames T
.Lock
;
1494 Process
(Position
.Node
.Element
.all);
1512 (Stream
: not null access Root_Stream_Type
'Class;
1513 Container
: out Set
)
1516 (Stream
: not null access Root_Stream_Type
'Class) return Node_Access
;
1517 pragma Inline
(Read_Node
);
1520 new Tree_Operations
.Generic_Read
(Clear
, Read_Node
);
1527 (Stream
: not null access Root_Stream_Type
'Class) return Node_Access
1529 Node
: Node_Access
:= new Node_Type
;
1531 Node
.Element
:= new Element_Type
'(Element_Type'Input (Stream));
1535 Free (Node); -- Note that Free deallocates elem too
1539 -- Start of processing for Read
1542 Read (Stream, Container.Tree);
1546 (Stream : not null access Root_Stream_Type'Class;
1550 raise Program_Error with "attempt to stream set cursor";
1553 ---------------------
1554 -- Replace_Element --
1555 ---------------------
1557 procedure Replace_Element
1558 (Tree : in out Tree_Type;
1560 Item : Element_Type)
1563 if Item < Node.Element.all
1564 or else Node.Element.all < Item
1568 if Tree.Lock > 0 then
1569 raise Program_Error with
1570 "attempt to tamper with cursors (set is locked)";
1574 X : Element_Access := Node.Element;
1576 Node.Element := new Element_Type'(Item
);
1583 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, Node
); -- Checks busy-bit
1585 Insert_New_Item
: declare
1586 function New_Node
return Node_Access
;
1587 pragma Inline
(New_Node
);
1589 procedure Insert_Post
is
1590 new Element_Keys
.Generic_Insert_Post
(New_Node
);
1592 procedure Unconditional_Insert
is
1593 new Element_Keys
.Generic_Unconditional_Insert
(Insert_Post
);
1599 function New_Node
return Node_Access
is
1601 Node
.Element
:= new Element_Type
'(Item); -- OK if fails
1602 Node.Color := Red_Black_Trees.Red;
1603 Node.Parent := null;
1610 Result : Node_Access;
1612 X : Element_Access := Node.Element;
1614 -- Start of processing for Insert_New_Item
1617 Unconditional_Insert
1621 pragma Assert (Result = Node);
1623 Free_Element (X); -- OK if fails
1624 end Insert_New_Item;
1625 end Replace_Element;
1627 procedure Replace_Element
1628 (Container : in out Set;
1630 New_Item : Element_Type)
1633 if Position.Node = null then
1634 raise Constraint_Error with "Position cursor equals No_Element";
1637 if Position.Node.Element = null then
1638 raise Program_Error with "Position cursor is bad";
1641 if Position.Container /= Container'Unrestricted_Access then
1642 raise Program_Error with "Position cursor designates wrong set";
1645 pragma Assert (Vet (Container.Tree, Position.Node),
1646 "bad cursor in Replace_Element");
1648 Replace_Element (Container.Tree, Position.Node, New_Item);
1649 end Replace_Element;
1651 ---------------------
1652 -- Reverse_Iterate --
1653 ---------------------
1655 procedure Reverse_Iterate
1657 Item : Element_Type;
1658 Process : not null access procedure (Position : Cursor))
1660 procedure Process_Node (Node : Node_Access);
1661 pragma Inline (Process_Node);
1663 procedure Local_Reverse_Iterate is
1664 new Element_Keys.Generic_Reverse_Iteration (Process_Node);
1670 procedure Process_Node (Node : Node_Access) is
1672 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1675 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
1676 B
: Natural renames T
.Busy
;
1678 -- Start of processing for Reverse_Iterate
1684 Local_Reverse_Iterate
(T
, Item
);
1692 end Reverse_Iterate
;
1694 procedure Reverse_Iterate
1696 Process
: not null access procedure (Position
: Cursor
))
1698 procedure Process_Node
(Node
: Node_Access
);
1699 pragma Inline
(Process_Node
);
1701 procedure Local_Reverse_Iterate
is
1702 new Tree_Operations
.Generic_Reverse_Iteration
(Process_Node
);
1708 procedure Process_Node
(Node
: Node_Access
) is
1710 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1713 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1714 B : Natural renames T.Busy;
1716 -- Start of processing for Reverse_Iterate
1722 Local_Reverse_Iterate (T);
1730 end Reverse_Iterate;
1736 function Right (Node : Node_Access) return Node_Access is
1745 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1747 Node.Color := Color;
1754 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1763 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1765 Node.Parent := Parent;
1772 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1774 Node.Right := Right;
1777 --------------------------
1778 -- Symmetric_Difference --
1779 --------------------------
1781 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1783 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
1784 end Symmetric_Difference;
1786 function Symmetric_Difference (Left, Right : Set) return Set is
1787 Tree : constant Tree_Type :=
1788 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
1790 return Set'(Controlled
with Tree
);
1791 end Symmetric_Difference
;
1797 function To_Set
(New_Item
: Element_Type
) return Set
is
1800 pragma Unreferenced
(Node
);
1802 Insert_Sans_Hint
(Tree
, New_Item
, Node
);
1803 return Set
'(Controlled with Tree);
1810 procedure Union (Target : in out Set; Source : Set) is
1812 Set_Ops.Union (Target.Tree, Source.Tree);
1815 function Union (Left, Right : Set) return Set is
1816 Tree : constant Tree_Type :=
1817 Set_Ops.Union (Left.Tree, Right.Tree);
1819 return Set'(Controlled
with Tree
);
1827 (Stream
: not null access Root_Stream_Type
'Class;
1830 procedure Write_Node
1831 (Stream
: not null access Root_Stream_Type
'Class;
1832 Node
: Node_Access
);
1833 pragma Inline
(Write_Node
);
1836 new Tree_Operations
.Generic_Write
(Write_Node
);
1842 procedure Write_Node
1843 (Stream
: not null access Root_Stream_Type
'Class;
1847 Element_Type
'Output (Stream
, Node
.Element
.all);
1850 -- Start of processing for Write
1853 Write
(Stream
, Container
.Tree
);
1857 (Stream
: not null access Root_Stream_Type
'Class;
1861 raise Program_Error
with "attempt to stream set cursor";
1864 end Ada
.Containers
.Indefinite_Ordered_Multisets
;