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-2005 Free Software Foundation, Inc. --
12 -- This specification is derived from the Ada Reference Manual for use with --
13 -- GNAT. The copyright notice above, and the license provisions that follow --
14 -- apply solely to the contents of the part following the private keyword. --
16 -- GNAT is free software; you can redistribute it and/or modify it under --
17 -- terms of the GNU General Public License as published by the Free Soft- --
18 -- ware Foundation; either version 2, or (at your option) any later ver- --
19 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
20 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
21 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
22 -- for more details. You should have received a copy of the GNU General --
23 -- Public License distributed with GNAT; see file COPYING. If not, write --
24 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
25 -- Boston, MA 02110-1301, USA. --
27 -- As a special exception, if other files instantiate generics from this --
28 -- unit, or you link this unit with other files to produce an executable, --
29 -- this unit does not by itself cause the resulting executable to be --
30 -- covered by the GNU General Public License. This exception does not --
31 -- however invalidate any other reasons why the executable file might be --
32 -- covered by the GNU Public License. --
34 -- This unit was originally developed by Matthew J Heaney. --
35 ------------------------------------------------------------------------------
37 with Ada
.Unchecked_Deallocation
;
39 with Ada
.Containers
.Red_Black_Trees
.Generic_Operations
;
40 pragma Elaborate_All
(Ada
.Containers
.Red_Black_Trees
.Generic_Operations
);
42 with Ada
.Containers
.Red_Black_Trees
.Generic_Keys
;
43 pragma Elaborate_All
(Ada
.Containers
.Red_Black_Trees
.Generic_Keys
);
45 with Ada
.Containers
.Red_Black_Trees
.Generic_Set_Operations
;
46 pragma Elaborate_All
(Ada
.Containers
.Red_Black_Trees
.Generic_Set_Operations
);
48 package body Ada
.Containers
.Indefinite_Ordered_Multisets
is
50 -----------------------------
51 -- Node Access Subprograms --
52 -----------------------------
54 -- These subprograms provide a functional interface to access fields
55 -- of a node, and a procedural interface for modifying these values.
57 function Color
(Node
: Node_Access
) return Color_Type
;
58 pragma Inline
(Color
);
60 function Left
(Node
: Node_Access
) return Node_Access
;
63 function Parent
(Node
: Node_Access
) return Node_Access
;
64 pragma Inline
(Parent
);
66 function Right
(Node
: Node_Access
) return Node_Access
;
67 pragma Inline
(Right
);
69 procedure Set_Parent
(Node
: Node_Access
; Parent
: Node_Access
);
70 pragma Inline
(Set_Parent
);
72 procedure Set_Left
(Node
: Node_Access
; Left
: Node_Access
);
73 pragma Inline
(Set_Left
);
75 procedure Set_Right
(Node
: Node_Access
; Right
: Node_Access
);
76 pragma Inline
(Set_Right
);
78 procedure Set_Color
(Node
: Node_Access
; Color
: Color_Type
);
79 pragma Inline
(Set_Color
);
81 -----------------------
82 -- Local Subprograms --
83 -----------------------
85 function Copy_Node
(Source
: Node_Access
) return Node_Access
;
86 pragma Inline
(Copy_Node
);
88 procedure Free
(X
: in 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 return Left
.Node
.Element
.all < Right
.Node
.Element
.all;
163 function "<" (Left
: Cursor
; Right
: Element_Type
) return Boolean is
165 return Left
.Node
.Element
.all < Right
;
168 function "<" (Left
: Element_Type
; Right
: Cursor
) return Boolean is
170 return Left
< Right
.Node
.Element
.all;
177 function "=" (Left
, Right
: Set
) return Boolean is
179 return Is_Equal
(Left
.Tree
, Right
.Tree
);
186 function ">" (Left
: Cursor
; Right
: Element_Type
) return Boolean is
188 return Right
< Left
.Node
.Element
.all;
191 function ">" (Left
, Right
: Cursor
) return Boolean is
193 -- L > R same as R < L
195 return Right
.Node
.Element
.all < Left
.Node
.Element
.all;
198 function ">" (Left
: Element_Type
; Right
: Cursor
) return Boolean is
200 return Right
.Node
.Element
.all < Left
;
208 new Tree_Operations
.Generic_Adjust
(Copy_Tree
);
210 procedure Adjust
(Container
: in out Set
) is
212 Adjust
(Container
.Tree
);
219 function Ceiling
(Container
: Set
; Item
: Element_Type
) return Cursor
is
220 Node
: constant Node_Access
:=
221 Element_Keys
.Ceiling
(Container
.Tree
, Item
);
228 return Cursor
'(Container'Unrestricted_Access, Node);
236 new Tree_Operations.Generic_Clear (Delete_Tree);
238 procedure Clear (Container : in out Set) is
240 Clear (Container.Tree);
247 function Color (Node : Node_Access) return Color_Type is
256 function Contains (Container : Set; Item : Element_Type) return Boolean is
258 return Find (Container, Item) /= No_Element;
265 function Copy_Node (Source : Node_Access) return Node_Access is
266 X : Element_Access := new Element_Type'(Source
.Element
.all);
269 return new Node_Type
'(Parent => null,
272 Color => Source.Color,
285 procedure Delete (Container : in out Set; Item : Element_Type) is
286 Tree : Tree_Type renames Container.Tree;
287 Node : Node_Access := Element_Keys.Ceiling (Tree, Item);
288 Done : constant Node_Access := Element_Keys.Upper_Bound (Tree, Item);
293 raise Constraint_Error;
298 Node := Tree_Operations.Next (Node);
299 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
302 exit when Node = Done;
306 procedure Delete (Container : in out Set; Position : in out Cursor) is
308 if Position.Node = null then
309 raise Constraint_Error;
312 if Position.Container /= Container'Unrestricted_Access then
316 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
317 Free (Position.Node);
319 Position.Container := null;
326 procedure Delete_First (Container : in out Set) is
327 Tree : Tree_Type renames Container.Tree;
328 X : Node_Access := Tree.First;
335 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
343 procedure Delete_Last (Container : in out Set) is
344 Tree : Tree_Type renames Container.Tree;
345 X : Node_Access := Tree.Last;
352 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
360 procedure Difference (Target : in out Set; Source : Set) is
362 Set_Ops.Difference (Target.Tree, Source.Tree);
365 function Difference (Left, Right : Set) return Set is
366 Tree : constant Tree_Type :=
367 Set_Ops.Difference (Left.Tree, Right.Tree);
369 return Set'(Controlled
with Tree
);
376 function Element
(Position
: Cursor
) return Element_Type
is
378 return Position
.Node
.Element
.all;
381 ---------------------
382 -- Equivalent_Sets --
383 ---------------------
385 function Equivalent_Sets
(Left
, Right
: Set
) return Boolean is
387 function Is_Equivalent_Node_Node
(L
, R
: Node_Access
) return Boolean;
388 pragma Inline
(Is_Equivalent_Node_Node
);
390 function Is_Equivalent
is
391 new Tree_Operations
.Generic_Equal
(Is_Equivalent_Node_Node
);
393 -----------------------------
394 -- Is_Equivalent_Node_Node --
395 -----------------------------
397 function Is_Equivalent_Node_Node
(L
, R
: Node_Access
) return Boolean is
399 if L
.Element
.all < R
.Element
.all then
401 elsif R
.Element
.all < L
.Element
.all then
406 end Is_Equivalent_Node_Node
;
408 -- Start of processing for Equivalent_Sets
411 return Is_Equivalent
(Left
.Tree
, Right
.Tree
);
418 procedure Exclude
(Container
: in out Set
; Item
: Element_Type
) is
419 Tree
: Tree_Type
renames Container
.Tree
;
420 Node
: Node_Access
:= Element_Keys
.Ceiling
(Tree
, Item
);
421 Done
: constant Node_Access
:= Element_Keys
.Upper_Bound
(Tree
, Item
);
424 while Node
/= Done
loop
426 Node
:= Tree_Operations
.Next
(Node
);
427 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, X
);
436 function Find
(Container
: Set
; Item
: Element_Type
) return Cursor
is
437 Node
: constant Node_Access
:=
438 Element_Keys
.Find
(Container
.Tree
, Item
);
445 return Cursor
'(Container'Unrestricted_Access, Node);
452 function First (Container : Set) return Cursor is
454 if Container.Tree.First = null then
458 return Cursor'(Container
'Unrestricted_Access, Container
.Tree
.First
);
465 function First_Element
(Container
: Set
) return Element_Type
is
467 return Container
.Tree
.First
.Element
.all;
474 function Floor
(Container
: Set
; Item
: Element_Type
) return Cursor
is
475 Node
: constant Node_Access
:=
476 Element_Keys
.Floor
(Container
.Tree
, Item
);
483 return Cursor
'(Container'Unrestricted_Access, Node);
490 procedure Free (X : in out Node_Access) is
491 procedure Deallocate is
492 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
499 Free_Element (X.Element);
514 package body Generic_Keys is
516 -----------------------
517 -- Local Subprograms --
518 -----------------------
520 function Is_Less_Key_Node
522 Right : Node_Access) return Boolean;
523 pragma Inline (Is_Less_Key_Node);
525 function Is_Greater_Key_Node
527 Right : Node_Access) return Boolean;
528 pragma Inline (Is_Greater_Key_Node);
530 --------------------------
531 -- Local Instantiations --
532 --------------------------
535 new Red_Black_Trees.Generic_Keys
536 (Tree_Operations => Tree_Operations,
537 Key_Type => Key_Type,
538 Is_Less_Key_Node => Is_Less_Key_Node,
539 Is_Greater_Key_Node => Is_Greater_Key_Node);
545 function "<" (Left : Key_Type; Right : Cursor) return Boolean is
547 return Left < Right.Node.Element.all;
550 function "<" (Left : Cursor; Right : Key_Type) return Boolean is
552 return Right > Left.Node.Element.all;
559 function ">" (Left : Key_Type; Right : Cursor) return Boolean is
561 return Left > Right.Node.Element.all;
564 function ">" (Left : Cursor; Right : Key_Type) return Boolean is
566 return Right < Left.Node.Element.all;
573 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
574 Node : constant Node_Access :=
575 Key_Keys.Ceiling (Container.Tree, Key);
582 return Cursor'(Container
'Unrestricted_Access, Node
);
589 function Contains
(Container
: Set
; Key
: Key_Type
) return Boolean is
591 return Find
(Container
, Key
) /= No_Element
;
598 procedure Delete
(Container
: in out Set
; Key
: Key_Type
) is
599 Tree
: Tree_Type
renames Container
.Tree
;
600 Node
: Node_Access
:= Key_Keys
.Ceiling
(Tree
, Key
);
601 Done
: constant Node_Access
:= Key_Keys
.Upper_Bound
(Tree
, Key
);
606 raise Constraint_Error
;
611 Node
:= Tree_Operations
.Next
(Node
);
612 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, X
);
615 exit when Node
= Done
;
623 function Element
(Container
: Set
; Key
: Key_Type
) return Element_Type
is
624 Node
: constant Node_Access
:= Key_Keys
.Find
(Container
.Tree
, Key
);
626 return Node
.Element
.all;
633 procedure Exclude
(Container
: in out Set
; Key
: Key_Type
) is
634 Tree
: Tree_Type
renames Container
.Tree
;
635 Node
: Node_Access
:= Key_Keys
.Ceiling
(Tree
, Key
);
636 Done
: constant Node_Access
:= Key_Keys
.Upper_Bound
(Tree
, Key
);
640 while Node
/= Done
loop
642 Node
:= Tree_Operations
.Next
(Node
);
643 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, X
);
652 function Find
(Container
: Set
; Key
: Key_Type
) return Cursor
is
653 Node
: constant Node_Access
:= Key_Keys
.Find
(Container
.Tree
, Key
);
660 return Cursor
'(Container'Unrestricted_Access, Node);
667 function Floor (Container : Set; Key : Key_Type) return Cursor is
668 Node : constant Node_Access := Key_Keys.Floor (Container.Tree, Key);
675 return Cursor'(Container
'Unrestricted_Access, Node
);
678 -------------------------
679 -- Is_Greater_Key_Node --
680 -------------------------
682 function Is_Greater_Key_Node
684 Right
: Node_Access
) return Boolean is
686 return Left
> Right
.Element
.all;
687 end Is_Greater_Key_Node
;
689 ----------------------
690 -- Is_Less_Key_Node --
691 ----------------------
693 function Is_Less_Key_Node
695 Right
: Node_Access
) return Boolean is
697 return Left
< Right
.Element
.all;
698 end Is_Less_Key_Node
;
707 Process
: not null access procedure (Position
: Cursor
))
709 procedure Process_Node
(Node
: Node_Access
);
710 pragma Inline
(Process_Node
);
712 procedure Local_Iterate
is
713 new Key_Keys
.Generic_Iteration
(Process_Node
);
719 procedure Process_Node
(Node
: Node_Access
) is
721 Process
(Cursor
'(Container'Unrestricted_Access, Node));
724 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
725 B : Natural renames T.Busy;
727 -- Start of processing for Iterate
733 Local_Iterate (T, Key);
747 function Key (Position : Cursor) return Key_Type is
749 return Key (Position.Node.Element.all);
752 ---------------------
753 -- Reverse_Iterate --
754 ---------------------
756 procedure Reverse_Iterate
759 Process : not null access procedure (Position : Cursor))
761 procedure Process_Node (Node : Node_Access);
762 pragma Inline (Process_Node);
768 procedure Local_Reverse_Iterate is
769 new Key_Keys.Generic_Reverse_Iteration (Process_Node);
775 procedure Process_Node (Node : Node_Access) is
777 Process (Cursor'(Container
'Unrestricted_Access, Node
));
780 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
781 B
: Natural renames T
.Busy
;
783 -- Start of processing for Reverse_Iterate
789 Local_Reverse_Iterate
(T
, Key
);
799 -----------------------------------
800 -- Update_Element_Preserving_Key --
801 -----------------------------------
803 procedure Update_Element_Preserving_Key
804 (Container
: in out Set
;
806 Process
: not null access procedure (Element
: in out Element_Type
))
808 Tree
: Tree_Type
renames Container
.Tree
;
811 if Position
.Node
= null then
812 raise Constraint_Error
;
815 if Position
.Container
/= Container
'Unrestricted_Access then
820 E
: Element_Type
renames Position
.Node
.Element
.all;
821 K
: Key_Type
renames Key
(E
);
823 B
: Natural renames Tree
.Busy
;
824 L
: Natural renames Tree
.Lock
;
852 X
: Node_Access
:= Position
.Node
;
854 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, X
);
859 end Update_Element_Preserving_Key
;
867 function Has_Element
(Position
: Cursor
) return Boolean is
869 return Position
/= No_Element
;
876 procedure Insert
(Container
: in out Set
; New_Item
: Element_Type
) is
879 Insert
(Container
, New_Item
, Position
);
883 (Container
: in out Set
;
884 New_Item
: Element_Type
;
885 Position
: out Cursor
)
887 function New_Node
return Node_Access
;
888 pragma Inline
(New_Node
);
890 procedure Insert_Post
is
891 new Element_Keys
.Generic_Insert_Post
(New_Node
);
893 procedure Unconditional_Insert_Sans_Hint
is
894 new Element_Keys
.Generic_Unconditional_Insert
(Insert_Post
);
900 function New_Node
return Node_Access
is
901 X
: Element_Access
:= new Element_Type
'(New_Item);
904 return new Node_Type'(Parent
=> null,
916 -- Start of processing for Insert
919 Unconditional_Insert_Sans_Hint
924 Position
.Container
:= Container
'Unrestricted_Access;
927 ----------------------
928 -- Insert_With_Hint --
929 ----------------------
931 procedure Insert_With_Hint
932 (Dst_Tree
: in out Tree_Type
;
933 Dst_Hint
: Node_Access
;
934 Src_Node
: Node_Access
;
935 Dst_Node
: out Node_Access
)
937 function New_Node
return Node_Access
;
938 pragma Inline
(New_Node
);
940 procedure Insert_Post
is
941 new Element_Keys
.Generic_Insert_Post
(New_Node
);
943 procedure Insert_Sans_Hint
is
944 new Element_Keys
.Generic_Unconditional_Insert
(Insert_Post
);
946 procedure Local_Insert_With_Hint
is
947 new Element_Keys
.Generic_Unconditional_Insert_With_Hint
955 function New_Node
return Node_Access
is
956 X
: Element_Access
:= new Element_Type
'(Src_Node.Element.all);
959 return new Node_Type'(Parent
=> null,
971 -- Start of processing for Insert_With_Hint
974 Local_Insert_With_Hint
977 Src_Node
.Element
.all,
979 end Insert_With_Hint
;
985 procedure Intersection
(Target
: in out Set
; Source
: Set
) is
987 Set_Ops
.Intersection
(Target
.Tree
, Source
.Tree
);
990 function Intersection
(Left
, Right
: Set
) return Set
is
991 Tree
: constant Tree_Type
:=
992 Set_Ops
.Intersection
(Left
.Tree
, Right
.Tree
);
994 return Set
'(Controlled with Tree);
1001 function Is_Empty (Container : Set) return Boolean is
1003 return Container.Tree.Length = 0;
1006 ------------------------
1007 -- Is_Equal_Node_Node --
1008 ------------------------
1010 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
1012 return L.Element.all = R.Element.all;
1013 end Is_Equal_Node_Node;
1015 -----------------------------
1016 -- Is_Greater_Element_Node --
1017 -----------------------------
1019 function Is_Greater_Element_Node
1020 (Left : Element_Type;
1021 Right : Node_Access) return Boolean
1024 -- e > node same as node < e
1026 return Right.Element.all < Left;
1027 end Is_Greater_Element_Node;
1029 --------------------------
1030 -- Is_Less_Element_Node --
1031 --------------------------
1033 function Is_Less_Element_Node
1034 (Left : Element_Type;
1035 Right : Node_Access) return Boolean
1038 return Left < Right.Element.all;
1039 end Is_Less_Element_Node;
1041 -----------------------
1042 -- Is_Less_Node_Node --
1043 -----------------------
1045 function Is_Less_Node_Node (L, R : Node_Access) return Boolean is
1047 return L.Element.all < R.Element.all;
1048 end Is_Less_Node_Node;
1054 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1056 return Set_Ops.Is_Subset (Subset => Subset.Tree, Of_Set => Of_Set.Tree);
1065 Item : Element_Type;
1066 Process : not null access procedure (Position : Cursor))
1068 procedure Process_Node (Node : Node_Access);
1069 pragma Inline (Process_Node);
1071 procedure Local_Iterate is
1072 new Element_Keys.Generic_Iteration (Process_Node);
1078 procedure Process_Node (Node : Node_Access) is
1080 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1083 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
1084 B
: Natural renames T
.Busy
;
1086 -- Start of processing for Iterate
1092 Local_Iterate
(T
, Item
);
1104 Process
: not null access procedure (Position
: Cursor
))
1106 procedure Process_Node
(Node
: Node_Access
);
1107 pragma Inline
(Process_Node
);
1109 procedure Local_Iterate
is
1110 new Tree_Operations
.Generic_Iteration
(Process_Node
);
1116 procedure Process_Node
(Node
: Node_Access
) is
1118 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1121 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1122 B : Natural renames T.Busy;
1124 -- Start of processing for Iterate
1144 function Last (Container : Set) return Cursor is
1146 if Container.Tree.Last = null then
1150 return Cursor'(Container
'Unrestricted_Access, Container
.Tree
.Last
);
1157 function Last_Element
(Container
: Set
) return Element_Type
is
1159 return Container
.Tree
.Last
.Element
.all;
1166 function Left
(Node
: Node_Access
) return Node_Access
is
1175 function Length
(Container
: Set
) return Count_Type
is
1177 return Container
.Tree
.Length
;
1185 new Tree_Operations
.Generic_Move
(Clear
);
1187 procedure Move
(Target
: in out Set
; Source
: in out Set
) is
1189 Move
(Target
=> Target
.Tree
, Source
=> Source
.Tree
);
1196 function Next
(Position
: Cursor
) return Cursor
is
1198 if Position
= No_Element
then
1203 Node
: constant Node_Access
:=
1204 Tree_Operations
.Next
(Position
.Node
);
1211 return Cursor
'(Position.Container, Node);
1215 procedure Next (Position : in out Cursor) is
1217 Position := Next (Position);
1224 function Overlap (Left, Right : Set) return Boolean is
1226 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1233 function Parent (Node : Node_Access) return Node_Access is
1242 function Previous (Position : Cursor) return Cursor is
1244 if Position = No_Element then
1249 Node : constant Node_Access :=
1250 Tree_Operations.Previous (Position.Node);
1257 return Cursor'(Position
.Container
, Node
);
1261 procedure Previous
(Position
: in out Cursor
) is
1263 Position
:= Previous
(Position
);
1270 procedure Query_Element
1272 Process
: not null access procedure (Element
: Element_Type
))
1274 E
: Element_Type
renames Position
.Node
.Element
.all;
1276 S
: Set
renames Position
.Container
.all;
1277 T
: Tree_Type
renames S
.Tree
'Unrestricted_Access.all;
1279 B
: Natural renames T
.Busy
;
1280 L
: Natural renames T
.Lock
;
1304 (Stream
: access Root_Stream_Type
'Class;
1305 Container
: out Set
)
1308 (Stream
: access Root_Stream_Type
'Class) return Node_Access
;
1309 pragma Inline
(Read_Node
);
1312 new Tree_Operations
.Generic_Read
(Clear
, Read_Node
);
1319 (Stream
: access Root_Stream_Type
'Class) return Node_Access
1321 Node
: Node_Access
:= new Node_Type
;
1323 Node
.Element
:= new Element_Type
'(Element_Type'Input (Stream));
1327 Free (Node); -- Note that Free deallocates elem too
1331 -- Start of processing for Read
1334 Read (Stream, Container.Tree);
1337 ---------------------
1338 -- Replace_Element --
1339 ---------------------
1341 procedure Replace_Element
1342 (Tree : in out Tree_Type;
1344 Item : Element_Type)
1347 if Item < Node.Element.all
1348 or else Node.Element.all < Item
1352 if Tree.Lock > 0 then
1353 raise Program_Error;
1357 X : Element_Access := Node.Element;
1359 Node.Element := new Element_Type'(Item
);
1366 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, Node
); -- Checks busy-bit
1368 Insert_New_Item
: declare
1369 function New_Node
return Node_Access
;
1370 pragma Inline
(New_Node
);
1372 procedure Insert_Post
is
1373 new Element_Keys
.Generic_Insert_Post
(New_Node
);
1375 procedure Unconditional_Insert
is
1376 new Element_Keys
.Generic_Unconditional_Insert
(Insert_Post
);
1382 function New_Node
return Node_Access
is
1384 Node
.Element
:= new Element_Type
'(Item); -- OK if fails
1388 Result : Node_Access;
1390 X : Element_Access := Node.Element;
1392 -- Start of processing for Insert_New_Item
1395 Unconditional_Insert
1399 pragma Assert (Result = Node);
1401 Free_Element (X); -- OK if fails
1402 end Insert_New_Item;
1403 end Replace_Element;
1405 procedure Replace_Element
1410 Tree : Tree_Type renames Position.Container.Tree'Unrestricted_Access.all;
1413 if Position.Node = null then
1414 raise Constraint_Error;
1417 if Position.Container /= Container'Unrestricted_Access then
1418 raise Program_Error;
1421 Replace_Element (Tree, Position.Node, By);
1422 end Replace_Element;
1424 ---------------------
1425 -- Reverse_Iterate --
1426 ---------------------
1428 procedure Reverse_Iterate
1430 Item : Element_Type;
1431 Process : not null access procedure (Position : Cursor))
1433 procedure Process_Node (Node : Node_Access);
1434 pragma Inline (Process_Node);
1436 procedure Local_Reverse_Iterate is
1437 new Element_Keys.Generic_Reverse_Iteration (Process_Node);
1443 procedure Process_Node (Node : Node_Access) is
1445 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1448 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
1449 B
: Natural renames T
.Busy
;
1451 -- Start of processing for Reverse_Iterate
1457 Local_Reverse_Iterate
(T
, Item
);
1465 end Reverse_Iterate
;
1467 procedure Reverse_Iterate
1469 Process
: not null access procedure (Position
: Cursor
))
1471 procedure Process_Node
(Node
: Node_Access
);
1472 pragma Inline
(Process_Node
);
1474 procedure Local_Reverse_Iterate
is
1475 new Tree_Operations
.Generic_Reverse_Iteration
(Process_Node
);
1481 procedure Process_Node
(Node
: Node_Access
) is
1483 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1486 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1487 B : Natural renames T.Busy;
1489 -- Start of processing for Reverse_Iterate
1495 Local_Reverse_Iterate (T);
1503 end Reverse_Iterate;
1509 function Right (Node : Node_Access) return Node_Access is
1518 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1520 Node.Color := Color;
1527 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1536 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1538 Node.Parent := Parent;
1545 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1547 Node.Right := Right;
1550 --------------------------
1551 -- Symmetric_Difference --
1552 --------------------------
1554 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1556 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
1557 end Symmetric_Difference;
1559 function Symmetric_Difference (Left, Right : Set) return Set is
1560 Tree : constant Tree_Type :=
1561 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
1563 return Set'(Controlled
with Tree
);
1564 end Symmetric_Difference
;
1570 procedure Union
(Target
: in out Set
; Source
: Set
) is
1572 Set_Ops
.Union
(Target
.Tree
, Source
.Tree
);
1575 function Union
(Left
, Right
: Set
) return Set
is
1576 Tree
: constant Tree_Type
:=
1577 Set_Ops
.Union
(Left
.Tree
, Right
.Tree
);
1579 return Set
'(Controlled with Tree);
1587 (Stream : access Root_Stream_Type'Class;
1590 procedure Write_Node
1591 (Stream : access Root_Stream_Type'Class;
1592 Node : Node_Access);
1593 pragma Inline (Write_Node);
1596 new Tree_Operations.Generic_Write (Write_Node);
1602 procedure Write_Node
1603 (Stream : access Root_Stream_Type'Class;
1607 Element_Type'Output (Stream, Node.Element.all);
1610 -- Start of processing for Write
1613 Write (Stream, Container.Tree);
1616 end Ada.Containers.Indefinite_Ordered_Multisets;