1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.INDEFINITE_MULTIWAY_TREES --
9 -- Copyright (C) 2004-2014, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- This unit was originally developed by Matthew J Heaney. --
28 ------------------------------------------------------------------------------
30 with Ada
.Unchecked_Deallocation
;
32 with System
; use type System
.Address
;
34 package body Ada
.Containers
.Indefinite_Multiway_Trees
is
40 type Root_Iterator
is abstract new Limited_Controlled
and
41 Tree_Iterator_Interfaces
.Forward_Iterator
with
43 Container
: Tree_Access
;
44 Subtree
: Tree_Node_Access
;
47 overriding
procedure Finalize
(Object
: in out Root_Iterator
);
49 -----------------------
50 -- Subtree_Iterator --
51 -----------------------
53 type Subtree_Iterator
is new Root_Iterator
with null record;
55 overriding
function First
(Object
: Subtree_Iterator
) return Cursor
;
57 overriding
function Next
58 (Object
: Subtree_Iterator
;
59 Position
: Cursor
) return Cursor
;
65 type Child_Iterator
is new Root_Iterator
and
66 Tree_Iterator_Interfaces
.Reversible_Iterator
with null record;
68 overriding
function First
(Object
: Child_Iterator
) return Cursor
;
70 overriding
function Next
71 (Object
: Child_Iterator
;
72 Position
: Cursor
) return Cursor
;
74 overriding
function Last
(Object
: Child_Iterator
) return Cursor
;
76 overriding
function Previous
77 (Object
: Child_Iterator
;
78 Position
: Cursor
) return Cursor
;
80 -----------------------
81 -- Local Subprograms --
82 -----------------------
84 function Root_Node
(Container
: Tree
) return Tree_Node_Access
;
86 procedure Free_Element
is
87 new Ada
.Unchecked_Deallocation
(Element_Type
, Element_Access
);
89 procedure Deallocate_Node
(X
: in out Tree_Node_Access
);
91 procedure Deallocate_Children
92 (Subtree
: Tree_Node_Access
;
93 Count
: in out Count_Type
);
95 procedure Deallocate_Subtree
96 (Subtree
: in out Tree_Node_Access
;
97 Count
: in out Count_Type
);
99 function Equal_Children
100 (Left_Subtree
, Right_Subtree
: Tree_Node_Access
) return Boolean;
102 function Equal_Subtree
103 (Left_Subtree
, Right_Subtree
: Tree_Node_Access
) return Boolean;
105 procedure Iterate_Children
106 (Container
: Tree_Access
;
107 Subtree
: Tree_Node_Access
;
108 Process
: not null access procedure (Position
: Cursor
));
110 procedure Iterate_Subtree
111 (Container
: Tree_Access
;
112 Subtree
: Tree_Node_Access
;
113 Process
: not null access procedure (Position
: Cursor
));
115 procedure Copy_Children
116 (Source
: Children_Type
;
117 Parent
: Tree_Node_Access
;
118 Count
: in out Count_Type
);
120 procedure Copy_Subtree
121 (Source
: Tree_Node_Access
;
122 Parent
: Tree_Node_Access
;
123 Target
: out Tree_Node_Access
;
124 Count
: in out Count_Type
);
126 function Find_In_Children
127 (Subtree
: Tree_Node_Access
;
128 Item
: Element_Type
) return Tree_Node_Access
;
130 function Find_In_Subtree
131 (Subtree
: Tree_Node_Access
;
132 Item
: Element_Type
) return Tree_Node_Access
;
134 function Child_Count
(Children
: Children_Type
) return Count_Type
;
136 function Subtree_Node_Count
137 (Subtree
: Tree_Node_Access
) return Count_Type
;
139 function Is_Reachable
(From
, To
: Tree_Node_Access
) return Boolean;
141 procedure Remove_Subtree
(Subtree
: Tree_Node_Access
);
143 procedure Insert_Subtree_Node
144 (Subtree
: Tree_Node_Access
;
145 Parent
: Tree_Node_Access
;
146 Before
: Tree_Node_Access
);
148 procedure Insert_Subtree_List
149 (First
: Tree_Node_Access
;
150 Last
: Tree_Node_Access
;
151 Parent
: Tree_Node_Access
;
152 Before
: Tree_Node_Access
);
154 procedure Splice_Children
155 (Target_Parent
: Tree_Node_Access
;
156 Before
: Tree_Node_Access
;
157 Source_Parent
: Tree_Node_Access
);
163 function "=" (Left
, Right
: Tree
) return Boolean is
165 if Left
'Address = Right
'Address then
169 return Equal_Children
(Root_Node
(Left
), Root_Node
(Right
));
176 procedure Adjust
(Container
: in out Tree
) is
177 Source
: constant Children_Type
:= Container
.Root
.Children
;
178 Source_Count
: constant Count_Type
:= Container
.Count
;
179 Target_Count
: Count_Type
;
182 -- We first restore the target container to its default-initialized
183 -- state, before we attempt any allocation, to ensure that invariants
184 -- are preserved in the event that the allocation fails.
186 Container
.Root
.Children
:= Children_Type
'(others => null);
189 Container.Count := 0;
191 -- Copy_Children returns a count of the number of nodes that it
192 -- allocates, but it works by incrementing the value that is passed in.
193 -- We must therefore initialize the count value before calling
198 -- Now we attempt the allocation of subtrees. The invariants are
199 -- satisfied even if the allocation fails.
201 Copy_Children (Source, Root_Node (Container), Target_Count);
202 pragma Assert (Target_Count = Source_Count);
204 Container.Count := Source_Count;
207 procedure Adjust (Control : in out Reference_Control_Type) is
209 if Control.Container /= null then
211 C : Tree renames Control.Container.all;
212 B : Natural renames C.Busy;
213 L : Natural renames C.Lock;
225 function Ancestor_Find
227 Item : Element_Type) return Cursor
229 R, N : Tree_Node_Access;
232 if Position = No_Element then
233 raise Constraint_Error with "Position cursor has no element";
236 -- Commented-out pending ARG ruling. ???
238 -- if Position.Container /= Container'Unrestricted_Access then
239 -- raise Program_Error with "Position cursor not in container";
242 -- AI-0136 says to raise PE if Position equals the root node. This does
243 -- not seem correct, as this value is just the limiting condition of the
244 -- search. For now we omit this check pending a ruling from the ARG.???
246 -- if Is_Root (Position) then
247 -- raise Program_Error with "Position cursor designates root";
250 R := Root_Node (Position.Container.all);
253 if N.Element.all = Item then
254 return Cursor'(Position
.Container
, N
);
267 procedure Append_Child
268 (Container
: in out Tree
;
270 New_Item
: Element_Type
;
271 Count
: Count_Type
:= 1)
273 First
, Last
: Tree_Node_Access
;
274 Element
: Element_Access
;
277 if Parent
= No_Element
then
278 raise Constraint_Error
with "Parent cursor has no element";
281 if Parent
.Container
/= Container
'Unrestricted_Access then
282 raise Program_Error
with "Parent cursor not in container";
289 if Container
.Busy
> 0 then
291 with "attempt to tamper with cursors (tree is busy)";
295 -- The element allocator may need an accessibility check in the case
296 -- the actual type is class-wide or has access discriminants (see
297 -- RM 4.8(10.1) and AI12-0035). We don't unsuppress the check on the
298 -- allocator in the loop below, because the one in this block would
299 -- have failed already.
301 pragma Unsuppress
(Accessibility_Check
);
304 Element
:= new Element_Type
'(New_Item);
307 First := new Tree_Node_Type'(Parent
=> Parent
.Node
,
313 for J
in Count_Type
'(2) .. Count loop
315 -- Reclaim other nodes if Storage_Error. ???
317 Element := new Element_Type'(New_Item
);
318 Last
.Next
:= new Tree_Node_Type
'(Parent => Parent.Node,
329 Parent => Parent.Node,
330 Before => null); -- null means "insert at end of list"
332 -- In order for operation Node_Count to complete in O(1) time, we cache
333 -- the count value. Here we increment the total count by the number of
334 -- nodes we just inserted.
336 Container.Count := Container.Count + Count;
343 procedure Assign (Target : in out Tree; Source : Tree) is
344 Source_Count : constant Count_Type := Source.Count;
345 Target_Count : Count_Type;
348 if Target'Address = Source'Address then
352 Target.Clear; -- checks busy bit
354 -- Copy_Children returns the number of nodes that it allocates, but it
355 -- does this by incrementing the count value passed in, so we must
356 -- initialize the count before calling Copy_Children.
360 -- Note that Copy_Children inserts the newly-allocated children into
361 -- their parent list only after the allocation of all the children has
362 -- succeeded. This preserves invariants even if the allocation fails.
364 Copy_Children (Source.Root.Children, Root_Node (Target), Target_Count);
365 pragma Assert (Target_Count = Source_Count);
367 Target.Count := Source_Count;
374 function Child_Count (Parent : Cursor) return Count_Type is
376 if Parent = No_Element then
379 return Child_Count (Parent.Node.Children);
383 function Child_Count (Children : Children_Type) return Count_Type is
385 Node : Tree_Node_Access;
389 Node := Children.First;
390 while Node /= null loop
391 Result := Result + 1;
402 function Child_Depth (Parent, Child : Cursor) return Count_Type is
404 N : Tree_Node_Access;
407 if Parent = No_Element then
408 raise Constraint_Error with "Parent cursor has no element";
411 if Child = No_Element then
412 raise Constraint_Error with "Child cursor has no element";
415 if Parent.Container /= Child.Container then
416 raise Program_Error with "Parent and Child in different containers";
421 while N /= Parent.Node loop
422 Result := Result + 1;
426 raise Program_Error with "Parent is not ancestor of Child";
437 procedure Clear (Container : in out Tree) is
438 Container_Count : Count_Type;
439 Children_Count : Count_Type;
442 if Container.Busy > 0 then
444 with "attempt to tamper with cursors (tree is busy)";
447 -- We first set the container count to 0, in order to preserve
448 -- invariants in case the deallocation fails. (This works because
449 -- Deallocate_Children immediately removes the children from their
450 -- parent, and then does the actual deallocation.)
452 Container_Count := Container.Count;
453 Container.Count := 0;
455 -- Deallocate_Children returns the number of nodes that it deallocates,
456 -- but it does this by incrementing the count value that is passed in,
457 -- so we must first initialize the count return value before calling it.
461 -- See comment above. Deallocate_Children immediately removes the
462 -- children list from their parent node (here, the root of the tree),
463 -- and only after that does it attempt the actual deallocation. So even
464 -- if the deallocation fails, the representation invariants
466 Deallocate_Children (Root_Node (Container), Children_Count);
467 pragma Assert (Children_Count = Container_Count);
470 ------------------------
471 -- Constant_Reference --
472 ------------------------
474 function Constant_Reference
475 (Container : aliased Tree;
476 Position : Cursor) return Constant_Reference_Type
479 if Position.Container = null then
480 raise Constraint_Error with
481 "Position cursor has no element";
484 if Position.Container /= Container'Unrestricted_Access then
485 raise Program_Error with
486 "Position cursor designates wrong container";
489 if Position.Node = Root_Node (Container) then
490 raise Program_Error with "Position cursor designates root";
493 if Position.Node.Element = null then
494 raise Program_Error with "Node has no element";
497 -- Implement Vet for multiway tree???
498 -- pragma Assert (Vet (Position),
499 -- "Position cursor in Constant_Reference is bad");
502 C : Tree renames Position.Container.all;
503 B : Natural renames C.Busy;
504 L : Natural renames C.Lock;
506 return R : constant Constant_Reference_Type :=
507 (Element => Position.Node.Element.all'Access,
508 Control => (Controlled with Container'Unrestricted_Access))
514 end Constant_Reference;
522 Item : Element_Type) return Boolean
525 return Find (Container, Item) /= No_Element;
532 function Copy (Source : Tree) return Tree is
534 return Target : Tree do
536 (Source => Source.Root.Children,
537 Parent => Root_Node (Target),
538 Count => Target.Count);
540 pragma Assert (Target.Count = Source.Count);
548 procedure Copy_Children
549 (Source : Children_Type;
550 Parent : Tree_Node_Access;
551 Count : in out Count_Type)
553 pragma Assert (Parent /= null);
554 pragma Assert (Parent.Children.First = null);
555 pragma Assert (Parent.Children.Last = null);
558 C : Tree_Node_Access;
561 -- We special-case the first allocation, in order to establish the
562 -- representation invariants for type Children_Type.
578 -- The representation invariants for the Children_Type list have been
579 -- established, so we can now copy the remaining children of Source.
586 Target => CC.Last.Next,
589 CC.Last.Next.Prev := CC.Last;
590 CC.Last := CC.Last.Next;
595 -- We add the newly-allocated children to their parent list only after
596 -- the allocation has succeeded, in order to preserve invariants of the
599 Parent.Children := CC;
606 procedure Copy_Subtree
607 (Target : in out Tree;
612 Target_Subtree : Tree_Node_Access;
613 Target_Count : Count_Type;
616 if Parent = No_Element then
617 raise Constraint_Error with "Parent cursor has no element";
620 if Parent.Container /= Target'Unrestricted_Access then
621 raise Program_Error with "Parent cursor not in container";
624 if Before /= No_Element then
625 if Before.Container /= Target'Unrestricted_Access then
626 raise Program_Error with "Before cursor not in container";
629 if Before.Node.Parent /= Parent.Node then
630 raise Constraint_Error with "Before cursor not child of Parent";
634 if Source = No_Element then
638 if Is_Root (Source) then
639 raise Constraint_Error with "Source cursor designates root";
642 -- Copy_Subtree returns a count of the number of nodes that it
643 -- allocates, but it works by incrementing the value that is passed in.
644 -- We must therefore initialize the count value before calling
650 (Source => Source.Node,
651 Parent => Parent.Node,
652 Target => Target_Subtree,
653 Count => Target_Count);
655 pragma Assert (Target_Subtree /= null);
656 pragma Assert (Target_Subtree.Parent = Parent.Node);
657 pragma Assert (Target_Count >= 1);
660 (Subtree => Target_Subtree,
661 Parent => Parent.Node,
662 Before => Before.Node);
664 -- In order for operation Node_Count to complete in O(1) time, we cache
665 -- the count value. Here we increment the total count by the number of
666 -- nodes we just inserted.
668 Target.Count := Target.Count + Target_Count;
671 procedure Copy_Subtree
672 (Source : Tree_Node_Access;
673 Parent : Tree_Node_Access;
674 Target : out Tree_Node_Access;
675 Count : in out Count_Type)
677 E : constant Element_Access := new Element_Type'(Source
.Element
.all);
680 Target
:= new Tree_Node_Type
'(Element => E,
687 (Source => Source.Children,
692 -------------------------
693 -- Deallocate_Children --
694 -------------------------
696 procedure Deallocate_Children
697 (Subtree : Tree_Node_Access;
698 Count : in out Count_Type)
700 pragma Assert (Subtree /= null);
702 CC : Children_Type := Subtree.Children;
703 C : Tree_Node_Access;
706 -- We immediately remove the children from their parent, in order to
707 -- preserve invariants in case the deallocation fails.
709 Subtree.Children := Children_Type'(others => null);
711 while CC
.First
/= null loop
715 Deallocate_Subtree
(C
, Count
);
717 end Deallocate_Children
;
719 ---------------------
720 -- Deallocate_Node --
721 ---------------------
723 procedure Deallocate_Node
(X
: in out Tree_Node_Access
) is
724 procedure Free_Node
is
725 new Ada
.Unchecked_Deallocation
(Tree_Node_Type
, Tree_Node_Access
);
727 -- Start of processing for Deallocate_Node
731 Free_Element
(X
.Element
);
736 ------------------------
737 -- Deallocate_Subtree --
738 ------------------------
740 procedure Deallocate_Subtree
741 (Subtree
: in out Tree_Node_Access
;
742 Count
: in out Count_Type
)
745 Deallocate_Children
(Subtree
, Count
);
746 Deallocate_Node
(Subtree
);
748 end Deallocate_Subtree
;
750 ---------------------
751 -- Delete_Children --
752 ---------------------
754 procedure Delete_Children
755 (Container
: in out Tree
;
761 if Parent
= No_Element
then
762 raise Constraint_Error
with "Parent cursor has no element";
765 if Parent
.Container
/= Container
'Unrestricted_Access then
766 raise Program_Error
with "Parent cursor not in container";
769 if Container
.Busy
> 0 then
771 with "attempt to tamper with cursors (tree is busy)";
774 -- Deallocate_Children returns a count of the number of nodes
775 -- that it deallocates, but it works by incrementing the
776 -- value that is passed in. We must therefore initialize
777 -- the count value before calling Deallocate_Children.
781 Deallocate_Children
(Parent
.Node
, Count
);
782 pragma Assert
(Count
<= Container
.Count
);
784 Container
.Count
:= Container
.Count
- Count
;
791 procedure Delete_Leaf
792 (Container
: in out Tree
;
793 Position
: in out Cursor
)
795 X
: Tree_Node_Access
;
798 if Position
= No_Element
then
799 raise Constraint_Error
with "Position cursor has no element";
802 if Position
.Container
/= Container
'Unrestricted_Access then
803 raise Program_Error
with "Position cursor not in container";
806 if Is_Root
(Position
) then
807 raise Program_Error
with "Position cursor designates root";
810 if not Is_Leaf
(Position
) then
811 raise Constraint_Error
with "Position cursor does not designate leaf";
814 if Container
.Busy
> 0 then
816 with "attempt to tamper with cursors (tree is busy)";
820 Position
:= No_Element
;
822 -- Restore represention invariants before attempting the actual
826 Container
.Count
:= Container
.Count
- 1;
828 -- It is now safe to attempt the deallocation. This leaf node has been
829 -- disassociated from the tree, so even if the deallocation fails,
830 -- representation invariants will remain satisfied.
839 procedure Delete_Subtree
840 (Container
: in out Tree
;
841 Position
: in out Cursor
)
843 X
: Tree_Node_Access
;
847 if Position
= No_Element
then
848 raise Constraint_Error
with "Position cursor has no element";
851 if Position
.Container
/= Container
'Unrestricted_Access then
852 raise Program_Error
with "Position cursor not in container";
855 if Is_Root
(Position
) then
856 raise Program_Error
with "Position cursor designates root";
859 if Container
.Busy
> 0 then
861 with "attempt to tamper with cursors (tree is busy)";
865 Position
:= No_Element
;
867 -- Here is one case where a deallocation failure can result in the
868 -- violation of a representation invariant. We disassociate the subtree
869 -- from the tree now, but we only decrement the total node count after
870 -- we attempt the deallocation. However, if the deallocation fails, the
871 -- total node count will not get decremented.
873 -- One way around this dilemma is to count the nodes in the subtree
874 -- before attempt to delete the subtree, but that is an O(n) operation,
875 -- so it does not seem worth it.
877 -- Perhaps this is much ado about nothing, since the only way
878 -- deallocation can fail is if Controlled Finalization fails: this
879 -- propagates Program_Error so all bets are off anyway. ???
883 -- Deallocate_Subtree returns a count of the number of nodes that it
884 -- deallocates, but it works by incrementing the value that is passed
885 -- in. We must therefore initialize the count value before calling
886 -- Deallocate_Subtree.
890 Deallocate_Subtree
(X
, Count
);
891 pragma Assert
(Count
<= Container
.Count
);
893 -- See comments above. We would prefer to do this sooner, but there's no
894 -- way to satisfy that goal without an potentially severe execution
897 Container
.Count
:= Container
.Count
- Count
;
904 function Depth
(Position
: Cursor
) return Count_Type
is
906 N
: Tree_Node_Access
;
913 Result
:= Result
+ 1;
923 function Element
(Position
: Cursor
) return Element_Type
is
925 if Position
.Container
= null then
926 raise Constraint_Error
with "Position cursor has no element";
929 if Position
.Node
= Root_Node
(Position
.Container
.all) then
930 raise Program_Error
with "Position cursor designates root";
933 return Position
.Node
.Element
.all;
940 function Equal_Children
941 (Left_Subtree
: Tree_Node_Access
;
942 Right_Subtree
: Tree_Node_Access
) return Boolean
944 Left_Children
: Children_Type
renames Left_Subtree
.Children
;
945 Right_Children
: Children_Type
renames Right_Subtree
.Children
;
947 L
, R
: Tree_Node_Access
;
950 if Child_Count
(Left_Children
) /= Child_Count
(Right_Children
) then
954 L
:= Left_Children
.First
;
955 R
:= Right_Children
.First
;
957 if not Equal_Subtree
(L
, R
) then
972 function Equal_Subtree
973 (Left_Position
: Cursor
;
974 Right_Position
: Cursor
) return Boolean
977 if Left_Position
= No_Element
then
978 raise Constraint_Error
with "Left cursor has no element";
981 if Right_Position
= No_Element
then
982 raise Constraint_Error
with "Right cursor has no element";
985 if Left_Position
= Right_Position
then
989 if Is_Root
(Left_Position
) then
990 if not Is_Root
(Right_Position
) then
994 return Equal_Children
(Left_Position
.Node
, Right_Position
.Node
);
997 if Is_Root
(Right_Position
) then
1001 return Equal_Subtree
(Left_Position
.Node
, Right_Position
.Node
);
1004 function Equal_Subtree
1005 (Left_Subtree
: Tree_Node_Access
;
1006 Right_Subtree
: Tree_Node_Access
) return Boolean
1009 if Left_Subtree
.Element
.all /= Right_Subtree
.Element
.all then
1013 return Equal_Children
(Left_Subtree
, Right_Subtree
);
1020 procedure Finalize
(Object
: in out Root_Iterator
) is
1021 B
: Natural renames Object
.Container
.Busy
;
1026 procedure Finalize
(Control
: in out Reference_Control_Type
) is
1028 if Control
.Container
/= null then
1030 C
: Tree
renames Control
.Container
.all;
1031 B
: Natural renames C
.Busy
;
1032 L
: Natural renames C
.Lock
;
1038 Control
.Container
:= null;
1048 Item
: Element_Type
) return Cursor
1050 N
: constant Tree_Node_Access
:=
1051 Find_In_Children
(Root_Node
(Container
), Item
);
1058 return Cursor
'(Container'Unrestricted_Access, N);
1065 overriding function First (Object : Subtree_Iterator) return Cursor is
1067 if Object.Subtree = Root_Node (Object.Container.all) then
1068 return First_Child (Root (Object.Container.all));
1070 return Cursor'(Object
.Container
, Object
.Subtree
);
1074 overriding
function First
(Object
: Child_Iterator
) return Cursor
is
1076 return First_Child
(Cursor
'(Object.Container, Object.Subtree));
1083 function First_Child (Parent : Cursor) return Cursor is
1084 Node : Tree_Node_Access;
1087 if Parent = No_Element then
1088 raise Constraint_Error with "Parent cursor has no element";
1091 Node := Parent.Node.Children.First;
1097 return Cursor'(Parent
.Container
, Node
);
1100 -------------------------
1101 -- First_Child_Element --
1102 -------------------------
1104 function First_Child_Element
(Parent
: Cursor
) return Element_Type
is
1106 return Element
(First_Child
(Parent
));
1107 end First_Child_Element
;
1109 ----------------------
1110 -- Find_In_Children --
1111 ----------------------
1113 function Find_In_Children
1114 (Subtree
: Tree_Node_Access
;
1115 Item
: Element_Type
) return Tree_Node_Access
1117 N
, Result
: Tree_Node_Access
;
1120 N
:= Subtree
.Children
.First
;
1121 while N
/= null loop
1122 Result
:= Find_In_Subtree
(N
, Item
);
1124 if Result
/= null then
1132 end Find_In_Children
;
1134 ---------------------
1135 -- Find_In_Subtree --
1136 ---------------------
1138 function Find_In_Subtree
1140 Item
: Element_Type
) return Cursor
1142 Result
: Tree_Node_Access
;
1145 if Position
= No_Element
then
1146 raise Constraint_Error
with "Position cursor has no element";
1149 -- Commented-out pending ruling from ARG. ???
1151 -- if Position.Container /= Container'Unrestricted_Access then
1152 -- raise Program_Error with "Position cursor not in container";
1155 if Is_Root
(Position
) then
1156 Result
:= Find_In_Children
(Position
.Node
, Item
);
1159 Result
:= Find_In_Subtree
(Position
.Node
, Item
);
1162 if Result
= null then
1166 return Cursor
'(Position.Container, Result);
1167 end Find_In_Subtree;
1169 function Find_In_Subtree
1170 (Subtree : Tree_Node_Access;
1171 Item : Element_Type) return Tree_Node_Access
1174 if Subtree.Element.all = Item then
1178 return Find_In_Children (Subtree, Item);
1179 end Find_In_Subtree;
1185 function Has_Element (Position : Cursor) return Boolean is
1187 if Position = No_Element then
1191 return Position.Node.Parent /= null;
1198 procedure Insert_Child
1199 (Container : in out Tree;
1202 New_Item : Element_Type;
1203 Count : Count_Type := 1)
1206 pragma Unreferenced (Position);
1209 Insert_Child (Container, Parent, Before, New_Item, Position, Count);
1212 procedure Insert_Child
1213 (Container : in out Tree;
1216 New_Item : Element_Type;
1217 Position : out Cursor;
1218 Count : Count_Type := 1)
1220 First : Tree_Node_Access;
1221 Last : Tree_Node_Access;
1222 Element : Element_Access;
1225 if Parent = No_Element then
1226 raise Constraint_Error with "Parent cursor has no element";
1229 if Parent.Container /= Container'Unrestricted_Access then
1230 raise Program_Error with "Parent cursor not in container";
1233 if Before /= No_Element then
1234 if Before.Container /= Container'Unrestricted_Access then
1235 raise Program_Error with "Before cursor not in container";
1238 if Before.Node.Parent /= Parent.Node then
1239 raise Constraint_Error with "Parent cursor not parent of Before";
1244 Position := No_Element; -- Need ruling from ARG ???
1248 if Container.Busy > 0 then
1250 with "attempt to tamper with cursors (tree is busy)";
1254 -- The element allocator may need an accessibility check in the case
1255 -- the actual type is class-wide or has access discriminants (see
1256 -- RM 4.8(10.1) and AI12-0035). We don't unsuppress the check on the
1257 -- allocator in the loop below, because the one in this block would
1258 -- have failed already.
1260 pragma Unsuppress (Accessibility_Check);
1263 Element := new Element_Type'(New_Item
);
1266 First
:= new Tree_Node_Type
'(Parent => Parent.Node,
1271 for J in Count_Type'(2) .. Count
loop
1273 -- Reclaim other nodes if Storage_Error. ???
1275 Element
:= new Element_Type
'(New_Item);
1276 Last.Next := new Tree_Node_Type'(Parent
=> Parent
.Node
,
1287 Parent
=> Parent
.Node
,
1288 Before
=> Before
.Node
);
1290 -- In order for operation Node_Count to complete in O(1) time, we cache
1291 -- the count value. Here we increment the total count by the number of
1292 -- nodes we just inserted.
1294 Container
.Count
:= Container
.Count
+ Count
;
1296 Position
:= Cursor
'(Parent.Container, First);
1299 -------------------------
1300 -- Insert_Subtree_List --
1301 -------------------------
1303 procedure Insert_Subtree_List
1304 (First : Tree_Node_Access;
1305 Last : Tree_Node_Access;
1306 Parent : Tree_Node_Access;
1307 Before : Tree_Node_Access)
1309 pragma Assert (Parent /= null);
1310 C : Children_Type renames Parent.Children;
1313 -- This is a simple utility operation to insert a list of nodes (from
1314 -- First..Last) as children of Parent. The Before node specifies where
1315 -- the new children should be inserted relative to the existing
1318 if First = null then
1319 pragma Assert (Last = null);
1323 pragma Assert (Last /= null);
1324 pragma Assert (Before = null or else Before.Parent = Parent);
1326 if C.First = null then
1328 C.First.Prev := null;
1330 C.Last.Next := null;
1332 elsif Before = null then -- means "insert after existing nodes"
1333 C.Last.Next := First;
1334 First.Prev := C.Last;
1336 C.Last.Next := null;
1338 elsif Before = C.First then
1339 Last.Next := C.First;
1340 C.First.Prev := Last;
1342 C.First.Prev := null;
1345 Before.Prev.Next := First;
1346 First.Prev := Before.Prev;
1347 Last.Next := Before;
1348 Before.Prev := Last;
1350 end Insert_Subtree_List;
1352 -------------------------
1353 -- Insert_Subtree_Node --
1354 -------------------------
1356 procedure Insert_Subtree_Node
1357 (Subtree : Tree_Node_Access;
1358 Parent : Tree_Node_Access;
1359 Before : Tree_Node_Access)
1362 -- This is a simple wrapper operation to insert a single child into the
1363 -- Parent's children list.
1370 end Insert_Subtree_Node;
1376 function Is_Empty (Container : Tree) return Boolean is
1378 return Container.Root.Children.First = null;
1385 function Is_Leaf (Position : Cursor) return Boolean is
1387 if Position = No_Element then
1391 return Position.Node.Children.First = null;
1398 function Is_Reachable (From, To : Tree_Node_Access) return Boolean is
1399 pragma Assert (From /= null);
1400 pragma Assert (To /= null);
1402 N : Tree_Node_Access;
1406 while N /= null loop
1421 function Is_Root (Position : Cursor) return Boolean is
1423 if Position.Container = null then
1427 return Position = Root (Position.Container.all);
1436 Process : not null access procedure (Position : Cursor))
1438 B : Natural renames Container'Unrestricted_Access.all.Busy;
1444 (Container => Container'Unrestricted_Access,
1445 Subtree => Root_Node (Container),
1446 Process => Process);
1456 function Iterate (Container : Tree)
1457 return Tree_Iterator_Interfaces.Forward_Iterator'Class
1460 return Iterate_Subtree (Root (Container));
1463 ----------------------
1464 -- Iterate_Children --
1465 ----------------------
1467 procedure Iterate_Children
1469 Process : not null access procedure (Position : Cursor))
1472 if Parent = No_Element then
1473 raise Constraint_Error with "Parent cursor has no element";
1477 B : Natural renames Parent.Container.Busy;
1478 C : Tree_Node_Access;
1483 C := Parent.Node.Children.First;
1484 while C /= null loop
1485 Process (Position => Cursor'(Parent
.Container
, Node
=> C
));
1496 end Iterate_Children
;
1498 procedure Iterate_Children
1499 (Container
: Tree_Access
;
1500 Subtree
: Tree_Node_Access
;
1501 Process
: not null access procedure (Position
: Cursor
))
1503 Node
: Tree_Node_Access
;
1506 -- This is a helper function to recursively iterate over all the nodes
1507 -- in a subtree, in depth-first fashion. This particular helper just
1508 -- visits the children of this subtree, not the root of the subtree node
1509 -- itself. This is useful when starting from the ultimate root of the
1510 -- entire tree (see Iterate), as that root does not have an element.
1512 Node
:= Subtree
.Children
.First
;
1513 while Node
/= null loop
1514 Iterate_Subtree
(Container
, Node
, Process
);
1517 end Iterate_Children
;
1519 function Iterate_Children
1522 return Tree_Iterator_Interfaces
.Reversible_Iterator
'Class
1524 C
: constant Tree_Access
:= Container
'Unrestricted_Access;
1525 B
: Natural renames C
.Busy
;
1528 if Parent
= No_Element
then
1529 raise Constraint_Error
with "Parent cursor has no element";
1532 if Parent
.Container
/= C
then
1533 raise Program_Error
with "Parent cursor not in container";
1536 return It
: constant Child_Iterator
:=
1537 Child_Iterator
'(Limited_Controlled with
1539 Subtree => Parent.Node)
1543 end Iterate_Children;
1545 ---------------------
1546 -- Iterate_Subtree --
1547 ---------------------
1549 function Iterate_Subtree
1551 return Tree_Iterator_Interfaces.Forward_Iterator'Class
1554 if Position = No_Element then
1555 raise Constraint_Error with "Position cursor has no element";
1558 -- Implement Vet for multiway trees???
1559 -- pragma Assert (Vet (Position), "bad subtree cursor");
1562 B : Natural renames Position.Container.Busy;
1564 return It : constant Subtree_Iterator :=
1565 (Limited_Controlled with
1566 Container => Position.Container,
1567 Subtree => Position.Node)
1572 end Iterate_Subtree;
1574 procedure Iterate_Subtree
1576 Process : not null access procedure (Position : Cursor))
1579 if Position = No_Element then
1580 raise Constraint_Error with "Position cursor has no element";
1584 B : Natural renames Position.Container.Busy;
1589 if Is_Root (Position) then
1590 Iterate_Children (Position.Container, Position.Node, Process);
1592 Iterate_Subtree (Position.Container, Position.Node, Process);
1602 end Iterate_Subtree;
1604 procedure Iterate_Subtree
1605 (Container : Tree_Access;
1606 Subtree : Tree_Node_Access;
1607 Process : not null access procedure (Position : Cursor))
1610 -- This is a helper function to recursively iterate over all the nodes
1611 -- in a subtree, in depth-first fashion. It first visits the root of the
1612 -- subtree, then visits its children.
1614 Process (Cursor'(Container
, Subtree
));
1615 Iterate_Children
(Container
, Subtree
, Process
);
1616 end Iterate_Subtree
;
1622 overriding
function Last
(Object
: Child_Iterator
) return Cursor
is
1624 return Last_Child
(Cursor
'(Object.Container, Object.Subtree));
1631 function Last_Child (Parent : Cursor) return Cursor is
1632 Node : Tree_Node_Access;
1635 if Parent = No_Element then
1636 raise Constraint_Error with "Parent cursor has no element";
1639 Node := Parent.Node.Children.Last;
1645 return (Parent.Container, Node);
1648 ------------------------
1649 -- Last_Child_Element --
1650 ------------------------
1652 function Last_Child_Element (Parent : Cursor) return Element_Type is
1654 return Element (Last_Child (Parent));
1655 end Last_Child_Element;
1661 procedure Move (Target : in out Tree; Source : in out Tree) is
1662 Node : Tree_Node_Access;
1665 if Target'Address = Source'Address then
1669 if Source.Busy > 0 then
1671 with "attempt to tamper with cursors of Source (tree is busy)";
1674 Target.Clear; -- checks busy bit
1676 Target.Root.Children := Source.Root.Children;
1677 Source.Root.Children := Children_Type'(others => null);
1679 Node
:= Target
.Root
.Children
.First
;
1680 while Node
/= null loop
1681 Node
.Parent
:= Root_Node
(Target
);
1685 Target
.Count
:= Source
.Count
;
1694 (Object
: Subtree_Iterator
;
1695 Position
: Cursor
) return Cursor
1697 Node
: Tree_Node_Access
;
1700 if Position
.Container
= null then
1704 if Position
.Container
/= Object
.Container
then
1705 raise Program_Error
with
1706 "Position cursor of Next designates wrong tree";
1709 Node
:= Position
.Node
;
1711 if Node
.Children
.First
/= null then
1712 return Cursor
'(Object.Container, Node.Children.First);
1715 while Node /= Object.Subtree loop
1716 if Node.Next /= null then
1717 return Cursor'(Object
.Container
, Node
.Next
);
1720 Node
:= Node
.Parent
;
1727 (Object
: Child_Iterator
;
1728 Position
: Cursor
) return Cursor
1731 if Position
.Container
= null then
1735 if Position
.Container
/= Object
.Container
then
1736 raise Program_Error
with
1737 "Position cursor of Next designates wrong tree";
1740 return Next_Sibling
(Position
);
1747 function Next_Sibling
(Position
: Cursor
) return Cursor
is
1749 if Position
= No_Element
then
1753 if Position
.Node
.Next
= null then
1757 return Cursor
'(Position.Container, Position.Node.Next);
1760 procedure Next_Sibling (Position : in out Cursor) is
1762 Position := Next_Sibling (Position);
1769 function Node_Count (Container : Tree) return Count_Type is
1771 -- Container.Count is the number of nodes we have actually allocated. We
1772 -- cache the value specifically so this Node_Count operation can execute
1773 -- in O(1) time, which makes it behave similarly to how the Length
1774 -- selector function behaves for other containers.
1776 -- The cached node count value only describes the nodes we have
1777 -- allocated; the root node itself is not included in that count. The
1778 -- Node_Count operation returns a value that includes the root node
1779 -- (because the RM says so), so we must add 1 to our cached value.
1781 return 1 + Container.Count;
1788 function Parent (Position : Cursor) return Cursor is
1790 if Position = No_Element then
1794 if Position.Node.Parent = null then
1798 return Cursor'(Position
.Container
, Position
.Node
.Parent
);
1805 procedure Prepend_Child
1806 (Container
: in out Tree
;
1808 New_Item
: Element_Type
;
1809 Count
: Count_Type
:= 1)
1811 First
, Last
: Tree_Node_Access
;
1812 Element
: Element_Access
;
1815 if Parent
= No_Element
then
1816 raise Constraint_Error
with "Parent cursor has no element";
1819 if Parent
.Container
/= Container
'Unrestricted_Access then
1820 raise Program_Error
with "Parent cursor not in container";
1827 if Container
.Busy
> 0 then
1829 with "attempt to tamper with cursors (tree is busy)";
1833 -- The element allocator may need an accessibility check in the case
1834 -- the actual type is class-wide or has access discriminants (see
1835 -- RM 4.8(10.1) and AI12-0035). We don't unsuppress the check on the
1836 -- allocator in the loop below, because the one in this block would
1837 -- have failed already.
1839 pragma Unsuppress
(Accessibility_Check
);
1842 Element
:= new Element_Type
'(New_Item);
1845 First := new Tree_Node_Type'(Parent
=> Parent
.Node
,
1851 for J
in Count_Type
'(2) .. Count loop
1853 -- Reclaim other nodes if Storage_Error. ???
1855 Element := new Element_Type'(New_Item
);
1856 Last
.Next
:= new Tree_Node_Type
'(Parent => Parent.Node,
1867 Parent => Parent.Node,
1868 Before => Parent.Node.Children.First);
1870 -- In order for operation Node_Count to complete in O(1) time, we cache
1871 -- the count value. Here we increment the total count by the number of
1872 -- nodes we just inserted.
1874 Container.Count := Container.Count + Count;
1881 overriding function Previous
1882 (Object : Child_Iterator;
1883 Position : Cursor) return Cursor
1886 if Position.Container = null then
1890 if Position.Container /= Object.Container then
1891 raise Program_Error with
1892 "Position cursor of Previous designates wrong tree";
1895 return Previous_Sibling (Position);
1898 ----------------------
1899 -- Previous_Sibling --
1900 ----------------------
1902 function Previous_Sibling (Position : Cursor) return Cursor is
1904 if Position = No_Element then
1908 if Position.Node.Prev = null then
1912 return Cursor'(Position
.Container
, Position
.Node
.Prev
);
1913 end Previous_Sibling
;
1915 procedure Previous_Sibling
(Position
: in out Cursor
) is
1917 Position
:= Previous_Sibling
(Position
);
1918 end Previous_Sibling
;
1924 procedure Query_Element
1926 Process
: not null access procedure (Element
: Element_Type
))
1929 if Position
= No_Element
then
1930 raise Constraint_Error
with "Position cursor has no element";
1933 if Is_Root
(Position
) then
1934 raise Program_Error
with "Position cursor designates root";
1938 T
: Tree
renames Position
.Container
.all'Unrestricted_Access.all;
1939 B
: Natural renames T
.Busy
;
1940 L
: Natural renames T
.Lock
;
1946 Process
(Position
.Node
.Element
.all);
1964 (Stream
: not null access Root_Stream_Type
'Class;
1965 Container
: out Tree
)
1967 procedure Read_Children
(Subtree
: Tree_Node_Access
);
1969 function Read_Subtree
1970 (Parent
: Tree_Node_Access
) return Tree_Node_Access
;
1972 Total_Count
: Count_Type
'Base;
1973 -- Value read from the stream that says how many elements follow
1975 Read_Count
: Count_Type
'Base;
1976 -- Actual number of elements read from the stream
1982 procedure Read_Children
(Subtree
: Tree_Node_Access
) is
1983 pragma Assert
(Subtree
/= null);
1984 pragma Assert
(Subtree
.Children
.First
= null);
1985 pragma Assert
(Subtree
.Children
.Last
= null);
1987 Count
: Count_Type
'Base;
1988 -- Number of child subtrees
1993 Count_Type
'Read (Stream
, Count
);
1996 raise Program_Error
with "attempt to read from corrupt stream";
2003 C
.First
:= Read_Subtree
(Parent
=> Subtree
);
2006 for J
in Count_Type
'(2) .. Count loop
2007 C.Last.Next := Read_Subtree (Parent => Subtree);
2008 C.Last.Next.Prev := C.Last;
2009 C.Last := C.Last.Next;
2012 -- Now that the allocation and reads have completed successfully, it
2013 -- is safe to link the children to their parent.
2015 Subtree.Children := C;
2022 function Read_Subtree
2023 (Parent : Tree_Node_Access) return Tree_Node_Access
2025 Element : constant Element_Access :=
2026 new Element_Type'(Element_Type
'Input (Stream
));
2028 Subtree
: constant Tree_Node_Access
:=
2030 (Parent => Parent, Element => Element, others => <>);
2033 Read_Count := Read_Count + 1;
2035 Read_Children (Subtree);
2040 -- Start of processing for Read
2043 Container.Clear; -- checks busy bit
2045 Count_Type'Read (Stream, Total_Count);
2047 if Total_Count < 0 then
2048 raise Program_Error with "attempt to read from corrupt stream";
2051 if Total_Count = 0 then
2057 Read_Children (Root_Node (Container));
2059 if Read_Count /= Total_Count then
2060 raise Program_Error with "attempt to read from corrupt stream";
2063 Container.Count := Total_Count;
2067 (Stream : not null access Root_Stream_Type'Class;
2068 Position : out Cursor)
2071 raise Program_Error with "attempt to read tree cursor from stream";
2075 (Stream : not null access Root_Stream_Type'Class;
2076 Item : out Reference_Type)
2079 raise Program_Error with "attempt to stream reference";
2083 (Stream : not null access Root_Stream_Type'Class;
2084 Item : out Constant_Reference_Type)
2087 raise Program_Error with "attempt to stream reference";
2095 (Container : aliased in out Tree;
2096 Position : Cursor) return Reference_Type
2099 if Position.Container = null then
2100 raise Constraint_Error with
2101 "Position cursor has no element";
2104 if Position.Container /= Container'Unrestricted_Access then
2105 raise Program_Error with
2106 "Position cursor designates wrong container";
2109 if Position.Node = Root_Node (Container) then
2110 raise Program_Error with "Position cursor designates root";
2113 if Position.Node.Element = null then
2114 raise Program_Error with "Node has no element";
2117 -- Implement Vet for multiway tree???
2118 -- pragma Assert (Vet (Position),
2119 -- "Position cursor in Constant_Reference is bad");
2122 C : Tree renames Position.Container.all;
2123 B : Natural renames C.Busy;
2124 L : Natural renames C.Lock;
2126 return R : constant Reference_Type :=
2127 (Element => Position.Node.Element.all'Access,
2128 Control => (Controlled with Position.Container))
2136 --------------------
2137 -- Remove_Subtree --
2138 --------------------
2140 procedure Remove_Subtree (Subtree : Tree_Node_Access) is
2141 C : Children_Type renames Subtree.Parent.Children;
2144 -- This is a utility operation to remove a subtree node from its
2145 -- parent's list of children.
2147 if C.First = Subtree then
2148 pragma Assert (Subtree.Prev = null);
2150 if C.Last = Subtree then
2151 pragma Assert (Subtree.Next = null);
2156 C.First := Subtree.Next;
2157 C.First.Prev := null;
2160 elsif C.Last = Subtree then
2161 pragma Assert (Subtree.Next = null);
2162 C.Last := Subtree.Prev;
2163 C.Last.Next := null;
2166 Subtree.Prev.Next := Subtree.Next;
2167 Subtree.Next.Prev := Subtree.Prev;
2171 ----------------------
2172 -- Replace_Element --
2173 ----------------------
2175 procedure Replace_Element
2176 (Container : in out Tree;
2178 New_Item : Element_Type)
2180 E, X : Element_Access;
2183 if Position = No_Element then
2184 raise Constraint_Error with "Position cursor has no element";
2187 if Position.Container /= Container'Unrestricted_Access then
2188 raise Program_Error with "Position cursor not in container";
2191 if Is_Root (Position) then
2192 raise Program_Error with "Position cursor designates root";
2195 if Container.Lock > 0 then
2197 with "attempt to tamper with elements (tree is locked)";
2201 -- The element allocator may need an accessibility check in the case
2202 -- the actual type is class-wide or has access discriminants (see
2203 -- RM 4.8(10.1) and AI12-0035).
2205 pragma Unsuppress (Accessibility_Check);
2208 E := new Element_Type'(New_Item
);
2211 X
:= Position
.Node
.Element
;
2212 Position
.Node
.Element
:= E
;
2215 end Replace_Element
;
2217 ------------------------------
2218 -- Reverse_Iterate_Children --
2219 ------------------------------
2221 procedure Reverse_Iterate_Children
2223 Process
: not null access procedure (Position
: Cursor
))
2226 if Parent
= No_Element
then
2227 raise Constraint_Error
with "Parent cursor has no element";
2231 B
: Natural renames Parent
.Container
.Busy
;
2232 C
: Tree_Node_Access
;
2237 C
:= Parent
.Node
.Children
.Last
;
2238 while C
/= null loop
2239 Process
(Position
=> Cursor
'(Parent.Container, Node => C));
2250 end Reverse_Iterate_Children;
2256 function Root (Container : Tree) return Cursor is
2258 return (Container'Unrestricted_Access, Root_Node (Container));
2265 function Root_Node (Container : Tree) return Tree_Node_Access is
2267 return Container.Root'Unrestricted_Access;
2270 ---------------------
2271 -- Splice_Children --
2272 ---------------------
2274 procedure Splice_Children
2275 (Target : in out Tree;
2276 Target_Parent : Cursor;
2278 Source : in out Tree;
2279 Source_Parent : Cursor)
2284 if Target_Parent = No_Element then
2285 raise Constraint_Error with "Target_Parent cursor has no element";
2288 if Target_Parent.Container /= Target'Unrestricted_Access then
2290 with "Target_Parent cursor not in Target container";
2293 if Before /= No_Element then
2294 if Before.Container /= Target'Unrestricted_Access then
2296 with "Before cursor not in Target container";
2299 if Before.Node.Parent /= Target_Parent.Node then
2300 raise Constraint_Error
2301 with "Before cursor not child of Target_Parent";
2305 if Source_Parent = No_Element then
2306 raise Constraint_Error with "Source_Parent cursor has no element";
2309 if Source_Parent.Container /= Source'Unrestricted_Access then
2311 with "Source_Parent cursor not in Source container";
2314 if Target'Address = Source'Address then
2315 if Target_Parent = Source_Parent then
2319 if Target.Busy > 0 then
2321 with "attempt to tamper with cursors (Target tree is busy)";
2324 if Is_Reachable (From => Target_Parent.Node,
2325 To => Source_Parent.Node)
2327 raise Constraint_Error
2328 with "Source_Parent is ancestor of Target_Parent";
2332 (Target_Parent => Target_Parent.Node,
2333 Before => Before.Node,
2334 Source_Parent => Source_Parent.Node);
2339 if Target.Busy > 0 then
2341 with "attempt to tamper with cursors (Target tree is busy)";
2344 if Source.Busy > 0 then
2346 with "attempt to tamper with cursors (Source tree is busy)";
2349 -- We cache the count of the nodes we have allocated, so that operation
2350 -- Node_Count can execute in O(1) time. But that means we must count the
2351 -- nodes in the subtree we remove from Source and insert into Target, in
2352 -- order to keep the count accurate.
2354 Count := Subtree_Node_Count (Source_Parent.Node);
2355 pragma Assert (Count >= 1);
2357 Count := Count - 1; -- because Source_Parent node does not move
2360 (Target_Parent => Target_Parent.Node,
2361 Before => Before.Node,
2362 Source_Parent => Source_Parent.Node);
2364 Source.Count := Source.Count - Count;
2365 Target.Count := Target.Count + Count;
2366 end Splice_Children;
2368 procedure Splice_Children
2369 (Container : in out Tree;
2370 Target_Parent : Cursor;
2372 Source_Parent : Cursor)
2375 if Target_Parent = No_Element then
2376 raise Constraint_Error with "Target_Parent cursor has no element";
2379 if Target_Parent.Container /= Container'Unrestricted_Access then
2381 with "Target_Parent cursor not in container";
2384 if Before /= No_Element then
2385 if Before.Container /= Container'Unrestricted_Access then
2387 with "Before cursor not in container";
2390 if Before.Node.Parent /= Target_Parent.Node then
2391 raise Constraint_Error
2392 with "Before cursor not child of Target_Parent";
2396 if Source_Parent = No_Element then
2397 raise Constraint_Error with "Source_Parent cursor has no element";
2400 if Source_Parent.Container /= Container'Unrestricted_Access then
2402 with "Source_Parent cursor not in container";
2405 if Target_Parent = Source_Parent then
2409 if Container.Busy > 0 then
2411 with "attempt to tamper with cursors (tree is busy)";
2414 if Is_Reachable (From => Target_Parent.Node,
2415 To => Source_Parent.Node)
2417 raise Constraint_Error
2418 with "Source_Parent is ancestor of Target_Parent";
2422 (Target_Parent => Target_Parent.Node,
2423 Before => Before.Node,
2424 Source_Parent => Source_Parent.Node);
2425 end Splice_Children;
2427 procedure Splice_Children
2428 (Target_Parent : Tree_Node_Access;
2429 Before : Tree_Node_Access;
2430 Source_Parent : Tree_Node_Access)
2432 CC : constant Children_Type := Source_Parent.Children;
2433 C : Tree_Node_Access;
2436 -- This is a utility operation to remove the children from Source parent
2437 -- and insert them into Target parent.
2439 Source_Parent.Children := Children_Type'(others => null);
2441 -- Fix up the Parent pointers of each child to designate its new Target
2445 while C
/= null loop
2446 C
.Parent
:= Target_Parent
;
2453 Parent
=> Target_Parent
,
2455 end Splice_Children
;
2457 --------------------
2458 -- Splice_Subtree --
2459 --------------------
2461 procedure Splice_Subtree
2462 (Target
: in out Tree
;
2465 Source
: in out Tree
;
2466 Position
: in out Cursor
)
2468 Subtree_Count
: Count_Type
;
2471 if Parent
= No_Element
then
2472 raise Constraint_Error
with "Parent cursor has no element";
2475 if Parent
.Container
/= Target
'Unrestricted_Access then
2476 raise Program_Error
with "Parent cursor not in Target container";
2479 if Before
/= No_Element
then
2480 if Before
.Container
/= Target
'Unrestricted_Access then
2481 raise Program_Error
with "Before cursor not in Target container";
2484 if Before
.Node
.Parent
/= Parent
.Node
then
2485 raise Constraint_Error
with "Before cursor not child of Parent";
2489 if Position
= No_Element
then
2490 raise Constraint_Error
with "Position cursor has no element";
2493 if Position
.Container
/= Source
'Unrestricted_Access then
2494 raise Program_Error
with "Position cursor not in Source container";
2497 if Is_Root
(Position
) then
2498 raise Program_Error
with "Position cursor designates root";
2501 if Target
'Address = Source
'Address then
2502 if Position
.Node
.Parent
= Parent
.Node
then
2503 if Position
.Node
= Before
.Node
then
2507 if Position
.Node
.Next
= Before
.Node
then
2512 if Target
.Busy
> 0 then
2514 with "attempt to tamper with cursors (Target tree is busy)";
2517 if Is_Reachable
(From
=> Parent
.Node
, To
=> Position
.Node
) then
2518 raise Constraint_Error
with "Position is ancestor of Parent";
2521 Remove_Subtree
(Position
.Node
);
2523 Position
.Node
.Parent
:= Parent
.Node
;
2524 Insert_Subtree_Node
(Position
.Node
, Parent
.Node
, Before
.Node
);
2529 if Target
.Busy
> 0 then
2531 with "attempt to tamper with cursors (Target tree is busy)";
2534 if Source
.Busy
> 0 then
2536 with "attempt to tamper with cursors (Source tree is busy)";
2539 -- This is an unfortunate feature of this API: we must count the nodes
2540 -- in the subtree that we remove from the source tree, which is an O(n)
2541 -- operation. It would have been better if the Tree container did not
2542 -- have a Node_Count selector; a user that wants the number of nodes in
2543 -- the tree could simply call Subtree_Node_Count, with the understanding
2544 -- that such an operation is O(n).
2546 -- Of course, we could choose to implement the Node_Count selector as an
2547 -- O(n) operation, which would turn this splice operation into an O(1)
2550 Subtree_Count
:= Subtree_Node_Count
(Position
.Node
);
2551 pragma Assert
(Subtree_Count
<= Source
.Count
);
2553 Remove_Subtree
(Position
.Node
);
2554 Source
.Count
:= Source
.Count
- Subtree_Count
;
2556 Position
.Node
.Parent
:= Parent
.Node
;
2557 Insert_Subtree_Node
(Position
.Node
, Parent
.Node
, Before
.Node
);
2559 Target
.Count
:= Target
.Count
+ Subtree_Count
;
2561 Position
.Container
:= Target
'Unrestricted_Access;
2564 procedure Splice_Subtree
2565 (Container
: in out Tree
;
2571 if Parent
= No_Element
then
2572 raise Constraint_Error
with "Parent cursor has no element";
2575 if Parent
.Container
/= Container
'Unrestricted_Access then
2576 raise Program_Error
with "Parent cursor not in container";
2579 if Before
/= No_Element
then
2580 if Before
.Container
/= Container
'Unrestricted_Access then
2581 raise Program_Error
with "Before cursor not in container";
2584 if Before
.Node
.Parent
/= Parent
.Node
then
2585 raise Constraint_Error
with "Before cursor not child of Parent";
2589 if Position
= No_Element
then
2590 raise Constraint_Error
with "Position cursor has no element";
2593 if Position
.Container
/= Container
'Unrestricted_Access then
2594 raise Program_Error
with "Position cursor not in container";
2597 if Is_Root
(Position
) then
2599 -- Should this be PE instead? Need ARG confirmation. ???
2601 raise Constraint_Error
with "Position cursor designates root";
2604 if Position
.Node
.Parent
= Parent
.Node
then
2605 if Position
.Node
= Before
.Node
then
2609 if Position
.Node
.Next
= Before
.Node
then
2614 if Container
.Busy
> 0 then
2616 with "attempt to tamper with cursors (tree is busy)";
2619 if Is_Reachable
(From
=> Parent
.Node
, To
=> Position
.Node
) then
2620 raise Constraint_Error
with "Position is ancestor of Parent";
2623 Remove_Subtree
(Position
.Node
);
2625 Position
.Node
.Parent
:= Parent
.Node
;
2626 Insert_Subtree_Node
(Position
.Node
, Parent
.Node
, Before
.Node
);
2629 ------------------------
2630 -- Subtree_Node_Count --
2631 ------------------------
2633 function Subtree_Node_Count
(Position
: Cursor
) return Count_Type
is
2635 if Position
= No_Element
then
2639 return Subtree_Node_Count
(Position
.Node
);
2640 end Subtree_Node_Count
;
2642 function Subtree_Node_Count
2643 (Subtree
: Tree_Node_Access
) return Count_Type
2645 Result
: Count_Type
;
2646 Node
: Tree_Node_Access
;
2650 Node
:= Subtree
.Children
.First
;
2651 while Node
/= null loop
2652 Result
:= Result
+ Subtree_Node_Count
(Node
);
2657 end Subtree_Node_Count
;
2664 (Container
: in out Tree
;
2668 if I
= No_Element
then
2669 raise Constraint_Error
with "I cursor has no element";
2672 if I
.Container
/= Container
'Unrestricted_Access then
2673 raise Program_Error
with "I cursor not in container";
2677 raise Program_Error
with "I cursor designates root";
2680 if I
= J
then -- make this test sooner???
2684 if J
= No_Element
then
2685 raise Constraint_Error
with "J cursor has no element";
2688 if J
.Container
/= Container
'Unrestricted_Access then
2689 raise Program_Error
with "J cursor not in container";
2693 raise Program_Error
with "J cursor designates root";
2696 if Container
.Lock
> 0 then
2698 with "attempt to tamper with elements (tree is locked)";
2702 EI
: constant Element_Access
:= I
.Node
.Element
;
2705 I
.Node
.Element
:= J
.Node
.Element
;
2706 J
.Node
.Element
:= EI
;
2710 --------------------
2711 -- Update_Element --
2712 --------------------
2714 procedure Update_Element
2715 (Container
: in out Tree
;
2717 Process
: not null access procedure (Element
: in out Element_Type
))
2720 if Position
= No_Element
then
2721 raise Constraint_Error
with "Position cursor has no element";
2724 if Position
.Container
/= Container
'Unrestricted_Access then
2725 raise Program_Error
with "Position cursor not in container";
2728 if Is_Root
(Position
) then
2729 raise Program_Error
with "Position cursor designates root";
2733 T
: Tree
renames Position
.Container
.all'Unrestricted_Access.all;
2734 B
: Natural renames T
.Busy
;
2735 L
: Natural renames T
.Lock
;
2741 Process
(Position
.Node
.Element
.all);
2760 (Stream
: not null access Root_Stream_Type
'Class;
2763 procedure Write_Children
(Subtree
: Tree_Node_Access
);
2764 procedure Write_Subtree
(Subtree
: Tree_Node_Access
);
2766 --------------------
2767 -- Write_Children --
2768 --------------------
2770 procedure Write_Children
(Subtree
: Tree_Node_Access
) is
2771 CC
: Children_Type
renames Subtree
.Children
;
2772 C
: Tree_Node_Access
;
2775 Count_Type
'Write (Stream
, Child_Count
(CC
));
2778 while C
/= null loop
2788 procedure Write_Subtree
(Subtree
: Tree_Node_Access
) is
2790 Element_Type
'Output (Stream
, Subtree
.Element
.all);
2791 Write_Children
(Subtree
);
2794 -- Start of processing for Write
2797 Count_Type
'Write (Stream
, Container
.Count
);
2799 if Container
.Count
= 0 then
2803 Write_Children
(Root_Node
(Container
));
2807 (Stream
: not null access Root_Stream_Type
'Class;
2811 raise Program_Error
with "attempt to write tree cursor to stream";
2815 (Stream
: not null access Root_Stream_Type
'Class;
2816 Item
: Reference_Type
)
2819 raise Program_Error
with "attempt to stream reference";
2823 (Stream
: not null access Root_Stream_Type
'Class;
2824 Item
: Constant_Reference_Type
)
2827 raise Program_Error
with "attempt to stream reference";
2830 end Ada
.Containers
.Indefinite_Multiway_Trees
;