Debian Codegraph @

gcc/ada/libgnat/a-comutr.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- A D A . C O N T A I N E R S . M U L T I W A Y _ T R E E S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2004-2025, Free Software Foundation, Inc. --
10 -- --
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. --
17 -- --
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. --
21 -- --
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/>. --
26 -- --
27 -- This unit was originally developed by Matthew J Heaney. --
28 ------------------------------------------------------------------------------
29
30 with Ada.Unchecked_Conversion;
31 with Ada.Unchecked_Deallocation;
32
33 with System; use type System.Address;
34 with System.Put_Images;
35
36 package body Ada.Containers.Multiway_Trees with
37 SPARK_Mode => Off
38 is
39
40 pragma Warnings (Off, "variable ""Busy*"" is not referenced");
41 pragma Warnings (Off, "variable ""Lock*"" is not referenced");
42 -- See comment in Ada.Containers.Helpers
43
44 -----------------------
45 -- Local Subprograms --
46 -----------------------
47
48 function Root_Node (Container : Tree) return Tree_Node_Access;
49
50 procedure Deallocate_Node is
51 new Ada.Unchecked_Deallocation (Tree_Node_Type, Tree_Node_Access);
52
53 procedure Deallocate_Children
54 (Subtree : Tree_Node_Access;
55 Count : in out Count_Type);
56
57 procedure Deallocate_Subtree
58 (Subtree : in out Tree_Node_Access;
59 Count : in out Count_Type);
60
61 function Equal_Children
62 (Left_Subtree, Right_Subtree : Tree_Node_Access) return Boolean;
63
64 function Equal_Subtree
65 (Left_Subtree, Right_Subtree : Tree_Node_Access) return Boolean;
66
67 procedure Iterate_Children
68 (Container : Tree_Access;
69 Subtree : Tree_Node_Access;
70 Process : not null access procedure (Position : Cursor));
71
72 procedure Iterate_Subtree
73 (Container : Tree_Access;
74 Subtree : Tree_Node_Access;
75 Process : not null access procedure (Position : Cursor));
76
77 procedure Copy_Children
78 (Source : Children_Type;
79 Parent : Tree_Node_Access;
80 Count : in out Count_Type);
81
82 procedure Copy_Subtree
83 (Source : Tree_Node_Access;
84 Parent : Tree_Node_Access;
85 Target : out Tree_Node_Access;
86 Count : in out Count_Type);
87
88 function Find_In_Children
89 (Subtree : Tree_Node_Access;
90 Item : Element_Type) return Tree_Node_Access;
91
92 function Find_In_Subtree
93 (Subtree : Tree_Node_Access;
94 Item : Element_Type) return Tree_Node_Access;
95
96 function Child_Count (Children : Children_Type) return Count_Type;
97
98 function Subtree_Node_Count
99 (Subtree : Tree_Node_Access) return Count_Type;
100
101 function Is_Reachable (From, To : Tree_Node_Access) return Boolean;
102
103 procedure Remove_Subtree (Subtree : Tree_Node_Access);
104
105 procedure Insert_Subtree_Node
106 (Subtree : Tree_Node_Access;
107 Parent : Tree_Node_Access;
108 Before : Tree_Node_Access);
109
110 procedure Insert_Subtree_List
111 (First : Tree_Node_Access;
112 Last : Tree_Node_Access;
113 Parent : Tree_Node_Access;
114 Before : Tree_Node_Access);
115
116 procedure Splice_Children
117 (Target_Parent : Tree_Node_Access;
118 Before : Tree_Node_Access;
119 Source_Parent : Tree_Node_Access);
120
121 ---------
122 -- "=" --
123 ---------
124
125 function "=" (Left, Right : Tree) return Boolean is
126 begin
127 return Equal_Children (Root_Node (Left), Root_Node (Right));
128 end "=";
129
130 ------------
131 -- Adjust --
132 ------------
133
134 procedure Adjust (Container : in out Tree) is
135 Source : constant Children_Type := Container.Root.Children;
136 Source_Count : constant Count_Type := Container.Count;
137 Target_Count : Count_Type;
138
139 begin
140 -- We first restore the target container to its default-initialized
141 -- state, before we attempt any allocation, to ensure that invariants
142 -- are preserved in the event that the allocation fails.
143
144 Container.Root.Children := Children_Type'(others => null);
145 Zero_Counts (Container.TC);
146 Container.Count := 0;
147
148 -- Copy_Children returns a count of the number of nodes that it
149 -- allocates, but it works by incrementing the value that is passed
150 -- in. We must therefore initialize the count value before calling
151 -- Copy_Children.
152
153 Target_Count := 0;
154
155 -- Now we attempt the allocation of subtrees. The invariants are
156 -- satisfied even if the allocation fails.
157
158 Copy_Children (Source, Root_Node (Container), Target_Count);
159 pragma Assert (Target_Count = Source_Count);
160
161 Container.Count := Source_Count;
162 end Adjust;
163
164 -------------------
165 -- Ancestor_Find --
166 -------------------
167
168 function Ancestor_Find
169 (Position : Cursor;
170 Item : Element_Type) return Cursor
171 is
172 R, N : Tree_Node_Access;
173
174 begin
175 if Checks and then Position = No_Element then
176 raise Constraint_Error with "Position cursor has no element";
177 end if;
178
179 -- Commented-out pending official ruling from ARG. ???
180
181 -- if Position.Container /= Container'Unrestricted_Access then
182 -- raise Program_Error with "Position cursor not in container";
183 -- end if;
184
185 -- AI-0136 says to raise PE if Position equals the root node. This does
186 -- not seem correct, as this value is just the limiting condition of the
187 -- search. For now we omit this check, pending a ruling from the ARG.???
188
189 -- if Checks and then Is_Root (Position) then
190 -- raise Program_Error with "Position cursor designates root";
191 -- end if;
192
193 R := Root_Node (Position.Container.all);
194 N := Position.Node;
195 while N /= R loop
196 if N.Element = Item then
197 return Cursor'(Position.Container, N);
198 end if;
199
200 N := N.Parent;
201 end loop;
202
203 return No_Element;
204 end Ancestor_Find;
205
206 ------------------
207 -- Append_Child --
208 ------------------
209
210 procedure Append_Child
211 (Container : in out Tree;
212 Parent : Cursor;
213 New_Item : Element_Type;
214 Count : Count_Type := 1)
215 is
216 First : Tree_Node_Access;
217 Last : Tree_Node_Access;
218
219 begin
220 TC_Check (Container.TC);
221
222 if Checks and then Parent = No_Element then
223 raise Constraint_Error with "Parent cursor has no element";
224 end if;
225
226 if Checks and then Parent.Container /= Container'Unrestricted_Access then
227 raise Program_Error with "Parent cursor not in container";
228 end if;
229
230 if Count = 0 then
231 return;
232 end if;
233
234 First := new Tree_Node_Type'(Parent => Parent.Node,
235 Element => New_Item,
236 others => <>);
237
238 Last := First;
239 for J in Count_Type'(2) .. Count loop
240
241 -- Reclaim other nodes if Storage_Error. ???
242
243 Last.Next := new Tree_Node_Type'(Parent => Parent.Node,
244 Prev => Last,
245 Element => New_Item,
246 others => <>);
247
248 Last := Last.Next;
249 end loop;
250
251 Insert_Subtree_List
252 (First => First,
253 Last => Last,
254 Parent => Parent.Node,
255 Before => null); -- null means "insert at end of list"
256
257 -- In order for operation Node_Count to complete in O(1) time, we cache
258 -- the count value. Here we increment the total count by the number of
259 -- nodes we just inserted.
260
261 Container.Count := Container.Count + Count;
262 end Append_Child;
263
264 ------------
265 -- Assign --
266 ------------
267
268 procedure Assign (Target : in out Tree; Source : Tree) is
269 Source_Count : constant Count_Type := Source.Count;
270 Target_Count : Count_Type;
271
272 begin
273 if Target'Address = Source'Address then
274 return;
275 end if;
276
277 Target.Clear; -- checks busy bit
278
279 -- Copy_Children returns the number of nodes that it allocates, but it
280 -- does this by incrementing the count value passed in, so we must
281 -- initialize the count before calling Copy_Children.
282
283 Target_Count := 0;
284
285 -- Note that Copy_Children inserts the newly-allocated children into
286 -- their parent list only after the allocation of all the children has
287 -- succeeded. This preserves invariants even if the allocation fails.
288
289 Copy_Children (Source.Root.Children, Root_Node (Target), Target_Count);
290 pragma Assert (Target_Count = Source_Count);
291
292 Target.Count := Source_Count;
293 end Assign;
294
295 -----------------
296 -- Child_Count --
297 -----------------
298
299 function Child_Count (Parent : Cursor) return Count_Type is
300 begin
301 return (if Parent = No_Element
302 then 0 else Child_Count (Parent.Node.Children));
303 end Child_Count;
304
305 function Child_Count (Children : Children_Type) return Count_Type is
306 Result : Count_Type;
307 Node : Tree_Node_Access;
308
309 begin
310 Result := 0;
311 Node := Children.First;
312 while Node /= null loop
313 Result := Result + 1;
314 Node := Node.Next;
315 end loop;
316
317 return Result;
318 end Child_Count;
319
320 -----------------
321 -- Child_Depth --
322 -----------------
323
324 function Child_Depth (Parent, Child : Cursor) return Count_Type is
325 Result : Count_Type;
326 N : Tree_Node_Access;
327
328 begin
329 if Checks and then Parent = No_Element then
330 raise Constraint_Error with "Parent cursor has no element";
331 end if;
332
333 if Checks and then Child = No_Element then
334 raise Constraint_Error with "Child cursor has no element";
335 end if;
336
337 if Checks and then Parent.Container /= Child.Container then
338 raise Program_Error with "Parent and Child in different containers";
339 end if;
340
341 Result := 0;
342 N := Child.Node;
343 while N /= Parent.Node loop
344 Result := Result + 1;
345 N := N.Parent;
346
347 if Checks and then N = null then
348 raise Program_Error with "Parent is not ancestor of Child";
349 end if;
350 end loop;
351
352 return Result;
353 end Child_Depth;
354
355 -----------
356 -- Clear --
357 -----------
358
359 procedure Clear (Container : in out Tree) is
360 Container_Count, Children_Count : Count_Type;
361
362 begin
363 TC_Check (Container.TC);
364
365 -- We first set the container count to 0, in order to preserve
366 -- invariants in case the deallocation fails. (This works because
367 -- Deallocate_Children immediately removes the children from their
368 -- parent, and then does the actual deallocation.)
369
370 Container_Count := Container.Count;
371 Container.Count := 0;
372
373 -- Deallocate_Children returns the number of nodes that it deallocates,
374 -- but it does this by incrementing the count value that is passed in,
375 -- so we must first initialize the count return value before calling it.
376
377 Children_Count := 0;
378
379 -- See comment above. Deallocate_Children immediately removes the
380 -- children list from their parent node (here, the root of the tree),
381 -- and only after that does it attempt the actual deallocation. So even
382 -- if the deallocation fails, the representation invariants for the tree
383 -- are preserved.
384
385 Deallocate_Children (Root_Node (Container), Children_Count);
386 pragma Assert (Children_Count = Container_Count);
387 end Clear;
388
389 ------------------------
390 -- Constant_Reference --
391 ------------------------
392
393 function Constant_Reference
394 (Container : aliased Tree;
395 Position : Cursor) return Constant_Reference_Type
396 is
397 begin
398 if Checks and then Position.Container = null then
399 raise Constraint_Error with
400 "Position cursor has no element";
401 end if;
402
403 if Checks and then Position.Container /= Container'Unrestricted_Access
404 then
405 raise Program_Error with
406 "Position cursor designates wrong container";
407 end if;
408
409 if Checks and then Position.Node = Root_Node (Container) then
410 raise Program_Error with "Position cursor designates root";
411 end if;
412
413 -- Implement Vet for multiway tree???
414 -- pragma Assert (Vet (Position),
415 -- "Position cursor in Constant_Reference is bad");
416
417 declare
418 C : Tree renames Position.Container.all;
419 TC : constant Tamper_Counts_Access :=
420 C.TC'Unrestricted_Access;
421 begin
422 return R : constant Constant_Reference_Type :=
423 (Element => Position.Node.Element'Access,
424 Control => (Controlled with TC))
425 do
426 Busy (TC.all);
427 end return;
428 end;
429 end Constant_Reference;
430
431 --------------
432 -- Contains --
433 --------------
434
435 function Contains
436 (Container : Tree;
437 Item : Element_Type) return Boolean
438 is
439 begin
440 return Find (Container, Item) /= No_Element;
441 end Contains;
442
443 ----------
444 -- Copy --
445 ----------
446
447 function Copy (Source : Tree) return Tree is
448 begin
449 return Target : Tree do
450 Copy_Children
451 (Source => Source.Root.Children,
452 Parent => Root_Node (Target),
453 Count => Target.Count);
454
455 pragma Assert (Target.Count = Source.Count);
456 end return;
457 end Copy;
458
459 -------------------
460 -- Copy_Children --
461 -------------------
462
463 procedure Copy_Children
464 (Source : Children_Type;
465 Parent : Tree_Node_Access;
466 Count : in out Count_Type)
467 is
468 pragma Assert (Parent /= null);
469 pragma Assert (Parent.Children.First = null);
470 pragma Assert (Parent.Children.Last = null);
471
472 CC : Children_Type;
473 C : Tree_Node_Access;
474
475 begin
476 -- We special-case the first allocation, in order to establish the
477 -- representation invariants for type Children_Type.
478
479 C := Source.First;
480
481 if C = null then
482 return;
483 end if;
484
485 Copy_Subtree
486 (Source => C,
487 Parent => Parent,
488 Target => CC.First,
489 Count => Count);
490
491 CC.Last := CC.First;
492
493 -- The representation invariants for the Children_Type list have been
494 -- established, so we can now copy the remaining children of Source.
495
496 C := C.Next;
497 while C /= null loop
498 Copy_Subtree
499 (Source => C,
500 Parent => Parent,
501 Target => CC.Last.Next,
502 Count => Count);
503
504 CC.Last.Next.Prev := CC.Last;
505 CC.Last := CC.Last.Next;
506
507 C := C.Next;
508 end loop;
509
510 -- Add the newly-allocated children to their parent list only after the
511 -- allocation has succeeded, so as to preserve invariants of the parent.
512
513 Parent.Children := CC;
514 end Copy_Children;
515
516 ------------------
517 -- Copy_Subtree --
518 ------------------
519
520 procedure Copy_Subtree
521 (Target : in out Tree;
522 Parent : Cursor;
523 Before : Cursor;
524 Source : Cursor)
525 is
526 Target_Subtree : Tree_Node_Access;
527 Target_Count : Count_Type;
528
529 begin
530 if Checks and then Parent = No_Element then
531 raise Constraint_Error with "Parent cursor has no element";
532 end if;
533
534 if Checks and then Parent.Container /= Target'Unrestricted_Access then
535 raise Program_Error with "Parent cursor not in container";
536 end if;
537
538 if Before /= No_Element then
539 if Checks and then Before.Container /= Target'Unrestricted_Access then
540 raise Program_Error with "Before cursor not in container";
541 end if;
542
543 if Checks and then Before.Node.Parent /= Parent.Node then
544 raise Constraint_Error with "Before cursor not child of Parent";
545 end if;
546 end if;
547
548 if Source = No_Element then
549 return;
550 end if;
551
552 if Checks and then Is_Root (Source) then
553 raise Constraint_Error with "Source cursor designates root";
554 end if;
555
556 -- Copy_Subtree returns a count of the number of nodes that it
557 -- allocates, but it works by incrementing the value that is passed
558 -- in. We must therefore initialize the count value before calling
559 -- Copy_Subtree.
560
561 Target_Count := 0;
562
563 Copy_Subtree
564 (Source => Source.Node,
565 Parent => Parent.Node,
566 Target => Target_Subtree,
567 Count => Target_Count);
568
569 pragma Assert (Target_Subtree /= null);
570 pragma Assert (Target_Subtree.Parent = Parent.Node);
571 pragma Assert (Target_Count >= 1);
572
573 Insert_Subtree_Node
574 (Subtree => Target_Subtree,
575 Parent => Parent.Node,
576 Before => Before.Node);
577
578 -- In order for operation Node_Count to complete in O(1) time, we cache
579 -- the count value. Here we increment the total count by the number of
580 -- nodes we just inserted.
581
582 Target.Count := Target.Count + Target_Count;
583 end Copy_Subtree;
584
585 procedure Copy_Subtree
586 (Source : Tree_Node_Access;
587 Parent : Tree_Node_Access;
588 Target : out Tree_Node_Access;
589 Count : in out Count_Type)
590 is
591 begin
592 Target := new Tree_Node_Type'(Element => Source.Element,
593 Parent => Parent,
594 others => <>);
595
596 Count := Count + 1;
597
598 Copy_Children
599 (Source => Source.Children,
600 Parent => Target,
601 Count => Count);
602 end Copy_Subtree;
603
604 -------------------------
605 -- Deallocate_Children --
606 -------------------------
607
608 procedure Deallocate_Children
609 (Subtree : Tree_Node_Access;
610 Count : in out Count_Type)
611 is
612 pragma Assert (Subtree /= null);
613
614 CC : Children_Type := Subtree.Children;
615 C : Tree_Node_Access;
616
617 begin
618 -- We immediately remove the children from their parent, in order to
619 -- preserve invariants in case the deallocation fails.
620
621 Subtree.Children := Children_Type'(others => null);
622
623 while CC.First /= null loop
624 C := CC.First;
625 CC.First := C.Next;
626
627 Deallocate_Subtree (C, Count);
628 end loop;
629 end Deallocate_Children;
630
631 ------------------------
632 -- Deallocate_Subtree --
633 ------------------------
634
635 procedure Deallocate_Subtree
636 (Subtree : in out Tree_Node_Access;
637 Count : in out Count_Type)
638 is
639 begin
640 Deallocate_Children (Subtree, Count);
641 Deallocate_Node (Subtree);
642 Count := Count + 1;
643 end Deallocate_Subtree;
644
645 ---------------------
646 -- Delete_Children --
647 ---------------------
648
649 procedure Delete_Children
650 (Container : in out Tree;
651 Parent : Cursor)
652 is
653 Count : Count_Type;
654
655 begin
656 TC_Check (Container.TC);
657
658 if Checks and then Parent = No_Element then
659 raise Constraint_Error with "Parent cursor has no element";
660 end if;
661
662 if Checks and then Parent.Container /= Container'Unrestricted_Access then
663 raise Program_Error with "Parent cursor not in container";
664 end if;
665
666 -- Deallocate_Children returns a count of the number of nodes that it
667 -- deallocates, but it works by incrementing the value that is passed
668 -- in. We must therefore initialize the count value before calling
669 -- Deallocate_Children.
670
671 Count := 0;
672
673 Deallocate_Children (Parent.Node, Count);
674 pragma Assert (Count <= Container.Count);
675
676 Container.Count := Container.Count - Count;
677 end Delete_Children;
678
679 -----------------
680 -- Delete_Leaf --
681 -----------------
682
683 procedure Delete_Leaf
684 (Container : in out Tree;
685 Position : in out Cursor)
686 is
687 X : Tree_Node_Access;
688
689 begin
690 TC_Check (Container.TC);
691
692 if Checks and then Position = No_Element then
693 raise Constraint_Error with "Position cursor has no element";
694 end if;
695
696 if Checks and then Position.Container /= Container'Unrestricted_Access
697 then
698 raise Program_Error with "Position cursor not in container";
699 end if;
700
701 if Checks and then Is_Root (Position) then
702 raise Program_Error with "Position cursor designates root";
703 end if;
704
705 if Checks and then not Is_Leaf (Position) then
706 raise Constraint_Error with "Position cursor does not designate leaf";
707 end if;
708
709 X := Position.Node;
710 Position := No_Element;
711
712 -- Restore represention invariants before attempting the actual
713 -- deallocation.
714
715 Remove_Subtree (X);
716 Container.Count := Container.Count - 1;
717
718 -- It is now safe to attempt the deallocation. This leaf node has been
719 -- disassociated from the tree, so even if the deallocation fails,
720 -- representation invariants will remain satisfied.
721
722 Deallocate_Node (X);
723 end Delete_Leaf;
724
725 --------------------
726 -- Delete_Subtree --
727 --------------------
728
729 procedure Delete_Subtree
730 (Container : in out Tree;
731 Position : in out Cursor)
732 is
733 X : Tree_Node_Access;
734 Count : Count_Type;
735
736 begin
737 TC_Check (Container.TC);
738
739 if Checks and then Position = No_Element then
740 raise Constraint_Error with "Position cursor has no element";
741 end if;
742
743 if Checks and then Position.Container /= Container'Unrestricted_Access
744 then
745 raise Program_Error with "Position cursor not in container";
746 end if;
747
748 if Checks and then Is_Root (Position) then
749 raise Program_Error with "Position cursor designates root";
750 end if;
751
752 X := Position.Node;
753 Position := No_Element;
754
755 -- Here is one case where a deallocation failure can result in the
756 -- violation of a representation invariant. We disassociate the subtree
757 -- from the tree now, but we only decrement the total node count after
758 -- we attempt the deallocation. However, if the deallocation fails, the
759 -- total node count will not get decremented.
760
761 -- One way around this dilemma is to count the nodes in the subtree
762 -- before attempt to delete the subtree, but that is an O(n) operation,
763 -- so it does not seem worth it.
764
765 -- Perhaps this is much ado about nothing, since the only way
766 -- deallocation can fail is if Controlled Finalization fails: this
767 -- propagates Program_Error so all bets are off anyway. ???
768
769 Remove_Subtree (X);
770
771 -- Deallocate_Subtree returns a count of the number of nodes that it
772 -- deallocates, but it works by incrementing the value that is passed
773 -- in. We must therefore initialize the count value before calling
774 -- Deallocate_Subtree.
775
776 Count := 0;
777
778 Deallocate_Subtree (X, Count);
779 pragma Assert (Count <= Container.Count);
780
781 -- See comments above. We would prefer to do this sooner, but there's no
782 -- way to satisfy that goal without a potentially severe execution
783 -- penalty.
784
785 Container.Count := Container.Count - Count;
786 end Delete_Subtree;
787
788 -----------
789 -- Depth --
790 -----------
791
792 function Depth (Position : Cursor) return Count_Type is
793 Result : Count_Type;
794 N : Tree_Node_Access;
795
796 begin
797 Result := 0;
798 N := Position.Node;
799 while N /= null loop
800 N := N.Parent;
801 Result := Result + 1;
802 end loop;
803
804 return Result;
805 end Depth;
806
807 -------------
808 -- Element --
809 -------------
810
811 function Element (Position : Cursor) return Element_Type is
812 begin
813 if Checks and then Position.Container = null then
814 raise Constraint_Error with "Position cursor has no element";
815 end if;
816
817 if Checks and then Position.Node = Root_Node (Position.Container.all)
818 then
819 raise Program_Error with "Position cursor designates root";
820 end if;
821
822 return Position.Node.Element;
823 end Element;
824
825 --------------------
826 -- Equal_Children --
827 --------------------
828
829 function Equal_Children
830 (Left_Subtree : Tree_Node_Access;
831 Right_Subtree : Tree_Node_Access) return Boolean
832 is
833 Left_Children : Children_Type renames Left_Subtree.Children;
834 Right_Children : Children_Type renames Right_Subtree.Children;
835
836 L, R : Tree_Node_Access;
837
838 begin
839 if Child_Count (Left_Children) /= Child_Count (Right_Children) then
840 return False;
841 end if;
842
843 L := Left_Children.First;
844 R := Right_Children.First;
845 while L /= null loop
846 if not Equal_Subtree (L, R) then
847 return False;
848 end if;
849
850 L := L.Next;
851 R := R.Next;
852 end loop;
853
854 return True;
855 end Equal_Children;
856
857 -------------------
858 -- Equal_Subtree --
859 -------------------
860
861 function Equal_Subtree
862 (Left_Position : Cursor;
863 Right_Position : Cursor) return Boolean
864 is
865 begin
866 if Checks and then Left_Position = No_Element then
867 raise Constraint_Error with "Left cursor has no element";
868 end if;
869
870 if Checks and then Right_Position = No_Element then
871 raise Constraint_Error with "Right cursor has no element";
872 end if;
873
874 if Left_Position = Right_Position then
875 return True;
876 end if;
877
878 if Is_Root (Left_Position) then
879 if not Is_Root (Right_Position) then
880 return False;
881 end if;
882
883 return Equal_Children (Left_Position.Node, Right_Position.Node);
884 end if;
885
886 if Is_Root (Right_Position) then
887 return False;
888 end if;
889
890 return Equal_Subtree (Left_Position.Node, Right_Position.Node);
891 end Equal_Subtree;
892
893 function Equal_Subtree
894 (Left_Subtree : Tree_Node_Access;
895 Right_Subtree : Tree_Node_Access) return Boolean
896 is
897 begin
898 if Left_Subtree.Element /= Right_Subtree.Element then
899 return False;
900 end if;
901
902 return Equal_Children (Left_Subtree, Right_Subtree);
903 end Equal_Subtree;
904
905 --------------
906 -- Finalize --
907 --------------
908
909 procedure Finalize (Object : in out Root_Iterator) is
910 begin
911 Unbusy (Object.Container.TC);
912 end Finalize;
913
914 ----------
915 -- Find --
916 ----------
917
918 function Find
919 (Container : Tree;
920 Item : Element_Type) return Cursor
921 is
922 N : constant Tree_Node_Access :=
923 Find_In_Children (Root_Node (Container), Item);
924 begin
925 if N = null then
926 return No_Element;
927 else
928 return Cursor'(Container'Unrestricted_Access, N);
929 end if;
930 end Find;
931
932 -----------
933 -- First --
934 -----------
935
936 overriding function First (Object : Subtree_Iterator) return Cursor is
937 begin
938 if Object.Subtree = Root_Node (Object.Container.all) then
939 return First_Child (Root (Object.Container.all));
940 else
941 return Cursor'(Object.Container, Object.Subtree);
942 end if;
943 end First;
944
945 overriding function First (Object : Child_Iterator) return Cursor is
946 begin
947 return First_Child (Cursor'(Object.Container, Object.Subtree));
948 end First;
949
950 -----------------
951 -- First_Child --
952 -----------------
953
954 function First_Child (Parent : Cursor) return Cursor is
955 Node : Tree_Node_Access;
956
957 begin
958 if Checks and then Parent = No_Element then
959 raise Constraint_Error with "Parent cursor has no element";
960 end if;
961
962 Node := Parent.Node.Children.First;
963
964 if Node = null then
965 return No_Element;
966 end if;
967
968 return Cursor'(Parent.Container, Node);
969 end First_Child;
970
971 -------------------------
972 -- First_Child_Element --
973 -------------------------
974
975 function First_Child_Element (Parent : Cursor) return Element_Type is
976 begin
977 return Element (First_Child (Parent));
978 end First_Child_Element;
979
980 ----------------------
981 -- Find_In_Children --
982 ----------------------
983
984 function Find_In_Children
985 (Subtree : Tree_Node_Access;
986 Item : Element_Type) return Tree_Node_Access
987 is
988 N, Result : Tree_Node_Access;
989
990 begin
991 N := Subtree.Children.First;
992 while N /= null loop
993 Result := Find_In_Subtree (N, Item);
994
995 if Result /= null then
996 return Result;
997 end if;
998
999 N := N.Next;
1000 end loop;
1001
1002 return null;
1003 end Find_In_Children;
1004
1005 ---------------------
1006 -- Find_In_Subtree --
1007 ---------------------
1008
1009 function Find_In_Subtree
1010 (Position : Cursor;
1011 Item : Element_Type) return Cursor
1012 is
1013 Result : Tree_Node_Access;
1014
1015 begin
1016 if Checks and then Position = No_Element then
1017 raise Constraint_Error with "Position cursor has no element";
1018 end if;
1019
1020 -- Commented out pending official ruling by ARG. ???
1021
1022 -- if Checks and then
1023 -- Position.Container /= Container'Unrestricted_Access
1024 -- then
1025 -- raise Program_Error with "Position cursor not in container";
1026 -- end if;
1027
1028 Result :=
1029 (if Is_Root (Position)
1030 then Find_In_Children (Position.Node, Item)
1031 else Find_In_Subtree (Position.Node, Item));
1032
1033 if Result = null then
1034 return No_Element;
1035 end if;
1036
1037 return Cursor'(Position.Container, Result);
1038 end Find_In_Subtree;
1039
1040 function Find_In_Subtree
1041 (Subtree : Tree_Node_Access;
1042 Item : Element_Type) return Tree_Node_Access
1043 is
1044 begin
1045 if Subtree.Element = Item then
1046 return Subtree;
1047 end if;
1048
1049 return Find_In_Children (Subtree, Item);
1050 end Find_In_Subtree;
1051
1052 ------------------------
1053 -- Get_Element_Access --
1054 ------------------------
1055
1056 function Get_Element_Access
1057 (Position : Cursor) return not null Element_Access is
1058 begin
1059 return Position.Node.Element'Access;
1060 end Get_Element_Access;
1061
1062 -----------------
1063 -- Has_Element --
1064 -----------------
1065
1066 function Has_Element (Position : Cursor) return Boolean is
1067 begin
1068 return (if Position = No_Element then False
1069 else Position.Node.Parent /= null);
1070 end Has_Element;
1071
1072 ------------------
1073 -- Insert_Child --
1074 ------------------
1075
1076 procedure Insert_Child
1077 (Container : in out Tree;
1078 Parent : Cursor;
1079 Before : Cursor;
1080 New_Item : Element_Type;
1081 Count : Count_Type := 1)
1082 is
1083 Position : Cursor;
1084
1085 begin
1086 Insert_Child (Container, Parent, Before, New_Item, Position, Count);
1087 end Insert_Child;
1088
1089 procedure Insert_Child
1090 (Container : in out Tree;
1091 Parent : Cursor;
1092 Before : Cursor;
1093 New_Item : Element_Type;
1094 Position : out Cursor;
1095 Count : Count_Type := 1)
1096 is
1097 First : Tree_Node_Access;
1098 Last : Tree_Node_Access;
1099
1100 begin
1101 TC_Check (Container.TC);
1102
1103 if Checks and then Parent = No_Element then
1104 raise Constraint_Error with "Parent cursor has no element";
1105 end if;
1106
1107 if Checks and then Parent.Container /= Container'Unrestricted_Access then
1108 raise Program_Error with "Parent cursor not in container";
1109 end if;
1110
1111 if Before /= No_Element then
1112 if Checks and then Before.Container /= Container'Unrestricted_Access
1113 then
1114 raise Program_Error with "Before cursor not in container";
1115 end if;
1116
1117 if Checks and then Before.Node.Parent /= Parent.Node then
1118 raise Constraint_Error with "Parent cursor not parent of Before";
1119 end if;
1120 end if;
1121
1122 if Count = 0 then
1123 Position := No_Element; -- Need ruling from ARG ???
1124 return;
1125 end if;
1126
1127 First := new Tree_Node_Type'(Parent => Parent.Node,
1128 Element => New_Item,
1129 others => <>);
1130
1131 Last := First;
1132 for J in Count_Type'(2) .. Count loop
1133
1134 -- Reclaim other nodes if Storage_Error. ???
1135
1136 Last.Next := new Tree_Node_Type'(Parent => Parent.Node,
1137 Prev => Last,
1138 Element => New_Item,
1139 others => <>);
1140
1141 Last := Last.Next;
1142 end loop;
1143
1144 Insert_Subtree_List
1145 (First => First,
1146 Last => Last,
1147 Parent => Parent.Node,
1148 Before => Before.Node);
1149
1150 -- In order for operation Node_Count to complete in O(1) time, we cache
1151 -- the count value. Here we increment the total count by the number of
1152 -- nodes we just inserted.
1153
1154 Container.Count := Container.Count + Count;
1155
1156 Position := Cursor'(Parent.Container, First);
1157 end Insert_Child;
1158
1159 procedure Insert_Child
1160 (Container : in out Tree;
1161 Parent : Cursor;
1162 Before : Cursor;
1163 Position : out Cursor;
1164 Count : Count_Type := 1)
1165 is
1166 First : Tree_Node_Access;
1167 Last : Tree_Node_Access;
1168
1169 begin
1170 TC_Check (Container.TC);
1171
1172 if Checks and then Parent = No_Element then
1173 raise Constraint_Error with "Parent cursor has no element";
1174 end if;
1175
1176 if Checks and then Parent.Container /= Container'Unrestricted_Access then
1177 raise Program_Error with "Parent cursor not in container";
1178 end if;
1179
1180 if Before /= No_Element then
1181 if Checks and then Before.Container /= Container'Unrestricted_Access
1182 then
1183 raise Program_Error with "Before cursor not in container";
1184 end if;
1185
1186 if Checks and then Before.Node.Parent /= Parent.Node then
1187 raise Constraint_Error with "Parent cursor not parent of Before";
1188 end if;
1189 end if;
1190
1191 if Count = 0 then
1192 Position := No_Element; -- Need ruling from ARG ???
1193 return;
1194 end if;
1195
1196 First := new Tree_Node_Type'(Parent => Parent.Node,
1197 Element => <>,
1198 others => <>);
1199
1200 Last := First;
1201 for J in Count_Type'(2) .. Count loop
1202
1203 -- Reclaim other nodes if Storage_Error. ???
1204
1205 Last.Next := new Tree_Node_Type'(Parent => Parent.Node,
1206 Prev => Last,
1207 Element => <>,
1208 others => <>);
1209
1210 Last := Last.Next;
1211 end loop;
1212
1213 Insert_Subtree_List
1214 (First => First,
1215 Last => Last,
1216 Parent => Parent.Node,
1217 Before => Before.Node);
1218
1219 -- In order for operation Node_Count to complete in O(1) time, we cache
1220 -- the count value. Here we increment the total count by the number of
1221 -- nodes we just inserted.
1222
1223 Container.Count := Container.Count + Count;
1224
1225 Position := Cursor'(Parent.Container, First);
1226 end Insert_Child;
1227
1228 -------------------------
1229 -- Insert_Subtree_List --
1230 -------------------------
1231
1232 procedure Insert_Subtree_List
1233 (First : Tree_Node_Access;
1234 Last : Tree_Node_Access;
1235 Parent : Tree_Node_Access;
1236 Before : Tree_Node_Access)
1237 is
1238 pragma Assert (Parent /= null);
1239 C : Children_Type renames Parent.Children;
1240
1241 begin
1242 -- This is a simple utility operation to insert a list of nodes (from
1243 -- First..Last) as children of Parent. The Before node specifies where
1244 -- the new children should be inserted relative to the existing
1245 -- children.
1246
1247 if First = null then
1248 pragma Assert (Last = null);
1249 return;
1250 end if;
1251
1252 pragma Assert (Last /= null);
1253 pragma Assert (Before = null or else Before.Parent = Parent);
1254
1255 if C.First = null then
1256 C.First := First;
1257 C.First.Prev := null;
1258 C.Last := Last;
1259 C.Last.Next := null;
1260
1261 elsif Before = null then -- means "insert after existing nodes"
1262 C.Last.Next := First;
1263 First.Prev := C.Last;
1264 C.Last := Last;
1265 C.Last.Next := null;
1266
1267 elsif Before = C.First then
1268 Last.Next := C.First;
1269 C.First.Prev := Last;
1270 C.First := First;
1271 C.First.Prev := null;
1272
1273 else
1274 Before.Prev.Next := First;
1275 First.Prev := Before.Prev;
1276 Last.Next := Before;
1277 Before.Prev := Last;
1278 end if;
1279 end Insert_Subtree_List;
1280
1281 -------------------------
1282 -- Insert_Subtree_Node --
1283 -------------------------
1284
1285 procedure Insert_Subtree_Node
1286 (Subtree : Tree_Node_Access;
1287 Parent : Tree_Node_Access;
1288 Before : Tree_Node_Access)
1289 is
1290 begin
1291 -- This is a simple wrapper operation to insert a single child into the
1292 -- Parent's children list.
1293
1294 Insert_Subtree_List
1295 (First => Subtree,
1296 Last => Subtree,
1297 Parent => Parent,
1298 Before => Before);
1299 end Insert_Subtree_Node;
1300
1301 --------------
1302 -- Is_Empty --
1303 --------------
1304
1305 function Is_Empty (Container : Tree) return Boolean is
1306 begin
1307 return Container.Root.Children.First = null;
1308 end Is_Empty;
1309
1310 -------------
1311 -- Is_Leaf --
1312 -------------
1313
1314 function Is_Leaf (Position : Cursor) return Boolean is
1315 begin
1316 return (if Position = No_Element then False
1317 else Position.Node.Children.First = null);
1318 end Is_Leaf;
1319
1320 ------------------
1321 -- Is_Reachable --
1322 ------------------
1323
1324 function Is_Reachable (From, To : Tree_Node_Access) return Boolean is
1325 pragma Assert (From /= null);
1326 pragma Assert (To /= null);
1327
1328 N : Tree_Node_Access;
1329
1330 begin
1331 N := From;
1332 while N /= null loop
1333 if N = To then
1334 return True;
1335 end if;
1336
1337 N := N.Parent;
1338 end loop;
1339
1340 return False;
1341 end Is_Reachable;
1342
1343 -------------
1344 -- Is_Root --
1345 -------------
1346
1347 function Is_Root (Position : Cursor) return Boolean is
1348 begin
1349 return (if Position.Container = null then False
1350 else Position = Root (Position.Container.all));
1351 end Is_Root;
1352
1353 -------------
1354 -- Iterate --
1355 -------------
1356
1357 procedure Iterate
1358 (Container : Tree;
1359 Process : not null access procedure (Position : Cursor))
1360 is
1361 Busy : With_Busy (Container.TC'Unrestricted_Access);
1362 begin
1363 Iterate_Children
1364 (Container => Container'Unrestricted_Access,
1365 Subtree => Root_Node (Container),
1366 Process => Process);
1367 end Iterate;
1368
1369 function Iterate (Container : Tree)
1370 return Tree_Iterator_Interfaces.Forward_Iterator'Class
1371 is
1372 begin
1373 return Iterate_Subtree (Root (Container));
1374 end Iterate;
1375
1376 ----------------------
1377 -- Iterate_Children --
1378 ----------------------
1379
1380 procedure Iterate_Children
1381 (Parent : Cursor;
1382 Process : not null access procedure (Position : Cursor))
1383 is
1384 C : Tree_Node_Access;
1385 Busy : With_Busy (Parent.Container.TC'Unrestricted_Access);
1386 begin
1387 if Checks and then Parent = No_Element then
1388 raise Constraint_Error with "Parent cursor has no element";
1389 end if;
1390
1391 C := Parent.Node.Children.First;
1392 while C /= null loop
1393 Process (Position => Cursor'(Parent.Container, Node => C));
1394 C := C.Next;
1395 end loop;
1396 end Iterate_Children;
1397
1398 procedure Iterate_Children
1399 (Container : Tree_Access;
1400 Subtree : Tree_Node_Access;
1401 Process : not null access procedure (Position : Cursor))
1402 is
1403 Node : Tree_Node_Access;
1404
1405 begin
1406 -- This is a helper function to recursively iterate over all the nodes
1407 -- in a subtree, in depth-first fashion. This particular helper just
1408 -- visits the children of this subtree, not the root of the subtree node
1409 -- itself. This is useful when starting from the ultimate root of the
1410 -- entire tree (see Iterate), as that root does not have an element.
1411
1412 Node := Subtree.Children.First;
1413 while Node /= null loop
1414 Iterate_Subtree (Container, Node, Process);
1415 Node := Node.Next;
1416 end loop;
1417 end Iterate_Children;
1418
1419 function Iterate_Children
1420 (Container : Tree;
1421 Parent : Cursor)
1422 return Tree_Iterator_Interfaces.Reversible_Iterator'Class
1423 is
1424 C : constant Tree_Access := Container'Unrestricted_Access;
1425 begin
1426 if Checks and then Parent = No_Element then
1427 raise Constraint_Error with "Parent cursor has no element";
1428 end if;
1429
1430 if Checks and then Parent.Container /= C then
1431 raise Program_Error with "Parent cursor not in container";
1432 end if;
1433
1434 return It : constant Child_Iterator :=
1435 (Limited_Controlled with
1436 Container => C,
1437 Subtree => Parent.Node)
1438 do
1439 Busy (C.TC);
1440 end return;
1441 end Iterate_Children;
1442
1443 ---------------------
1444 -- Iterate_Subtree --
1445 ---------------------
1446
1447 function Iterate_Subtree
1448 (Position : Cursor)
1449 return Tree_Iterator_Interfaces.Forward_Iterator'Class
1450 is
1451 C : constant Tree_Access := Position.Container;
1452 begin
1453 if Checks and then Position = No_Element then
1454 raise Constraint_Error with "Position cursor has no element";
1455 end if;
1456
1457 -- Implement Vet for multiway trees???
1458 -- pragma Assert (Vet (Position), "bad subtree cursor");
1459
1460 return It : constant Subtree_Iterator :=
1461 (Limited_Controlled with
1462 Container => C,
1463 Subtree => Position.Node)
1464 do
1465 Busy (C.TC);
1466 end return;
1467 end Iterate_Subtree;
1468
1469 procedure Iterate_Subtree
1470 (Position : Cursor;
1471 Process : not null access procedure (Position : Cursor))
1472 is
1473 Busy : With_Busy (Position.Container.TC'Unrestricted_Access);
1474 begin
1475 if Checks and then Position = No_Element then
1476 raise Constraint_Error with "Position cursor has no element";
1477 end if;
1478
1479 if Is_Root (Position) then
1480 Iterate_Children (Position.Container, Position.Node, Process);
1481 else
1482 Iterate_Subtree (Position.Container, Position.Node, Process);
1483 end if;
1484 end Iterate_Subtree;
1485
1486 procedure Iterate_Subtree
1487 (Container : Tree_Access;
1488 Subtree : Tree_Node_Access;
1489 Process : not null access procedure (Position : Cursor))
1490 is
1491 begin
1492 -- This is a helper function to recursively iterate over all the nodes
1493 -- in a subtree, in depth-first fashion. It first visits the root of the
1494 -- subtree, then visits its children.
1495
1496 Process (Cursor'(Container, Subtree));
1497 Iterate_Children (Container, Subtree, Process);
1498 end Iterate_Subtree;
1499
1500 ----------
1501 -- Last --
1502 ----------
1503
1504 overriding function Last (Object : Child_Iterator) return Cursor is
1505 begin
1506 return Last_Child (Cursor'(Object.Container, Object.Subtree));
1507 end Last;
1508
1509 ----------------
1510 -- Last_Child --
1511 ----------------
1512
1513 function Last_Child (Parent : Cursor) return Cursor is
1514 Node : Tree_Node_Access;
1515
1516 begin
1517 if Checks and then Parent = No_Element then
1518 raise Constraint_Error with "Parent cursor has no element";
1519 end if;
1520
1521 Node := Parent.Node.Children.Last;
1522
1523 if Node = null then
1524 return No_Element;
1525 end if;
1526
1527 return (Parent.Container, Node);
1528 end Last_Child;
1529
1530 ------------------------
1531 -- Last_Child_Element --
1532 ------------------------
1533
1534 function Last_Child_Element (Parent : Cursor) return Element_Type is
1535 begin
1536 return Element (Last_Child (Parent));
1537 end Last_Child_Element;
1538
1539 ----------
1540 -- Move --
1541 ----------
1542
1543 procedure Move (Target : in out Tree; Source : in out Tree) is
1544 Node : Tree_Node_Access;
1545
1546 begin
1547 if Target'Address = Source'Address then
1548 return;
1549 end if;
1550
1551 TC_Check (Source.TC);
1552
1553 Target.Clear; -- checks busy bit
1554
1555 Target.Root.Children := Source.Root.Children;
1556 Source.Root.Children := Children_Type'(others => null);
1557
1558 Node := Target.Root.Children.First;
1559 while Node /= null loop
1560 Node.Parent := Root_Node (Target);
1561 Node := Node.Next;
1562 end loop;
1563
1564 Target.Count := Source.Count;
1565 Source.Count := 0;
1566 end Move;
1567
1568 ----------
1569 -- Next --
1570 ----------
1571
1572 function Next
1573 (Object : Subtree_Iterator;
1574 Position : Cursor) return Cursor
1575 is
1576 Node : Tree_Node_Access;
1577
1578 begin
1579 if Position.Container = null then
1580 return No_Element;
1581 end if;
1582
1583 if Checks and then Position.Container /= Object.Container then
1584 raise Program_Error with
1585 "Position cursor of Next designates wrong tree";
1586 end if;
1587
1588 Node := Position.Node;
1589
1590 if Node.Children.First /= null then
1591 return Cursor'(Object.Container, Node.Children.First);
1592 end if;
1593
1594 while Node /= Object.Subtree loop
1595 if Node.Next /= null then
1596 return Cursor'(Object.Container, Node.Next);
1597 end if;
1598
1599 Node := Node.Parent;
1600 end loop;
1601
1602 return No_Element;
1603 end Next;
1604
1605 function Next
1606 (Object : Child_Iterator;
1607 Position : Cursor) return Cursor
1608 is
1609 begin
1610 if Position.Container = null then
1611 return No_Element;
1612 end if;
1613
1614 if Checks and then Position.Container /= Object.Container then
1615 raise Program_Error with
1616 "Position cursor of Next designates wrong tree";
1617 end if;
1618
1619 return Next_Sibling (Position);
1620 end Next;
1621
1622 ------------------
1623 -- Next_Sibling --
1624 ------------------
1625
1626 function Next_Sibling (Position : Cursor) return Cursor is
1627 begin
1628 if Position = No_Element then
1629 return No_Element;
1630 end if;
1631
1632 if Position.Node.Next = null then
1633 return No_Element;
1634 end if;
1635
1636 return Cursor'(Position.Container, Position.Node.Next);
1637 end Next_Sibling;
1638
1639 procedure Next_Sibling (Position : in out Cursor) is
1640 begin
1641 Position := Next_Sibling (Position);
1642 end Next_Sibling;
1643
1644 ----------------
1645 -- Node_Count --
1646 ----------------
1647
1648 function Node_Count (Container : Tree) return Count_Type is
1649 begin
1650 -- Container.Count is the number of nodes we have actually allocated. We
1651 -- cache the value specifically so this Node_Count operation can execute
1652 -- in O(1) time, which makes it behave similarly to how the Length
1653 -- selector function behaves for other containers.
1654
1655 -- The cached node count value only describes the nodes we have
1656 -- allocated; the root node itself is not included in that count. The
1657 -- Node_Count operation returns a value that includes the root node
1658 -- (because the RM says so), so we must add 1 to our cached value.
1659
1660 return 1 + Container.Count;
1661 end Node_Count;
1662
1663 ------------
1664 -- Parent --
1665 ------------
1666
1667 function Parent (Position : Cursor) return Cursor is
1668 begin
1669 if Position = No_Element then
1670 return No_Element;
1671 end if;
1672
1673 if Position.Node.Parent = null then
1674 return No_Element;
1675 end if;
1676
1677 return Cursor'(Position.Container, Position.Node.Parent);
1678 end Parent;
1679
1680 -------------------
1681 -- Prepend_Child --
1682 -------------------
1683
1684 procedure Prepend_Child
1685 (Container : in out Tree;
1686 Parent : Cursor;
1687 New_Item : Element_Type;
1688 Count : Count_Type := 1)
1689 is
1690 First, Last : Tree_Node_Access;
1691
1692 begin
1693 TC_Check (Container.TC);
1694
1695 if Checks and then Parent = No_Element then
1696 raise Constraint_Error with "Parent cursor has no element";
1697 end if;
1698
1699 if Checks and then Parent.Container /= Container'Unrestricted_Access then
1700 raise Program_Error with "Parent cursor not in container";
1701 end if;
1702
1703 if Count = 0 then
1704 return;
1705 end if;
1706
1707 First := new Tree_Node_Type'(Parent => Parent.Node,
1708 Element => New_Item,
1709 others => <>);
1710
1711 Last := First;
1712
1713 for J in Count_Type'(2) .. Count loop
1714
1715 -- Reclaim other nodes if Storage_Error???
1716
1717 Last.Next := new Tree_Node_Type'(Parent => Parent.Node,
1718 Prev => Last,
1719 Element => New_Item,
1720 others => <>);
1721
1722 Last := Last.Next;
1723 end loop;
1724
1725 Insert_Subtree_List
1726 (First => First,
1727 Last => Last,
1728 Parent => Parent.Node,
1729 Before => Parent.Node.Children.First);
1730
1731 -- In order for operation Node_Count to complete in O(1) time, we cache
1732 -- the count value. Here we increment the total count by the number of
1733 -- nodes we just inserted.
1734
1735 Container.Count := Container.Count + Count;
1736 end Prepend_Child;
1737
1738 --------------
1739 -- Previous --
1740 --------------
1741
1742 overriding function Previous
1743 (Object : Child_Iterator;
1744 Position : Cursor) return Cursor
1745 is
1746 begin
1747 if Position.Container = null then
1748 return No_Element;
1749 end if;
1750
1751 if Checks and then Position.Container /= Object.Container then
1752 raise Program_Error with
1753 "Position cursor of Previous designates wrong tree";
1754 end if;
1755
1756 return Previous_Sibling (Position);
1757 end Previous;
1758
1759 ----------------------
1760 -- Previous_Sibling --
1761 ----------------------
1762
1763 function Previous_Sibling (Position : Cursor) return Cursor is
1764 begin
1765 return
1766 (if Position = No_Element then No_Element
1767 elsif Position.Node.Prev = null then No_Element
1768 else Cursor'(Position.Container, Position.Node.Prev));
1769 end Previous_Sibling;
1770
1771 procedure Previous_Sibling (Position : in out Cursor) is
1772 begin
1773 Position := Previous_Sibling (Position);
1774 end Previous_Sibling;
1775
1776 ----------------------
1777 -- Pseudo_Reference --
1778 ----------------------
1779
1780 function Pseudo_Reference
1781 (Container : aliased Tree'Class) return Reference_Control_Type
1782 is
1783 TC : constant Tamper_Counts_Access := Container.TC'Unrestricted_Access;
1784 begin
1785 return R : constant Reference_Control_Type := (Controlled with TC) do
1786 Busy (TC.all);
1787 end return;
1788 end Pseudo_Reference;
1789
1790 -------------------
1791 -- Query_Element --
1792 -------------------
1793
1794 procedure Query_Element
1795 (Position : Cursor;
1796 Process : not null access procedure (Element : Element_Type))
1797 is
1798 T : Tree renames Position.Container.all'Unrestricted_Access.all;
1799 Lock : With_Lock (T.TC'Unrestricted_Access);
1800 begin
1801 if Checks and then Position = No_Element then
1802 raise Constraint_Error with "Position cursor has no element";
1803 end if;
1804
1805 if Checks and then Is_Root (Position) then
1806 raise Program_Error with "Position cursor designates root";
1807 end if;
1808
1809 Process (Position.Node.Element);
1810 end Query_Element;
1811
1812 ---------------
1813 -- Put_Image --
1814 ---------------
1815
1816 procedure Put_Image
1817 (S : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class; V : Tree)
1818 is
1819 use System.Put_Images;
1820
1821 procedure Rec (Position : Cursor);
1822 -- Recursive routine operating on cursors
1823
1824 procedure Rec (Position : Cursor) is
1825 First_Time : Boolean := True;
1826 begin
1827 Array_Before (S);
1828
1829 for X in Iterate_Children (V, Position) loop
1830 if First_Time then
1831 First_Time := False;
1832 else
1833 Array_Between (S);
1834 end if;
1835
1836 Element_Type'Put_Image (S, Element (X));
1837 if Child_Count (X) > 0 then
1838 Simple_Array_Between (S);
1839 Rec (X);
1840 end if;
1841 end loop;
1842
1843 Array_After (S);
1844 end Rec;
1845
1846 begin
1847 if First_Child (Root (V)) = No_Element then
1848 Array_Before (S);
1849 Array_After (S);
1850 else
1851 Rec (First_Child (Root (V)));
1852 end if;
1853 end Put_Image;
1854
1855 ----------
1856 -- Read --
1857 ----------
1858
1859 procedure Read
1860 (Stream : not null access Root_Stream_Type'Class;
1861 Container : out Tree)
1862 is
1863 procedure Read_Children (Subtree : Tree_Node_Access);
1864
1865 function Read_Subtree
1866 (Parent : Tree_Node_Access) return Tree_Node_Access;
1867
1868 Total_Count : Count_Type'Base;
1869 -- Value read from the stream that says how many elements follow
1870
1871 Read_Count : Count_Type'Base;
1872 -- Actual number of elements read from the stream
1873
1874 -------------------
1875 -- Read_Children --
1876 -------------------
1877
1878 procedure Read_Children (Subtree : Tree_Node_Access) is
1879 pragma Assert (Subtree /= null);
1880 pragma Assert (Subtree.Children.First = null);
1881 pragma Assert (Subtree.Children.Last = null);
1882
1883 Count : Count_Type'Base;
1884 -- Number of child subtrees
1885
1886 C : Children_Type;
1887
1888 begin
1889 Count_Type'Read (Stream, Count);
1890
1891 if Checks and then Count < 0 then
1892 raise Program_Error with "attempt to read from corrupt stream";
1893 end if;
1894
1895 if Count = 0 then
1896 return;
1897 end if;
1898
1899 C.First := Read_Subtree (Parent => Subtree);
1900 C.Last := C.First;
1901
1902 for J in Count_Type'(2) .. Count loop
1903 C.Last.Next := Read_Subtree (Parent => Subtree);
1904 C.Last.Next.Prev := C.Last;
1905 C.Last := C.Last.Next;
1906 end loop;
1907
1908 -- Now that the allocation and reads have completed successfully, it
1909 -- is safe to link the children to their parent.
1910
1911 Subtree.Children := C;
1912 end Read_Children;
1913
1914 ------------------
1915 -- Read_Subtree --
1916 ------------------
1917
1918 function Read_Subtree
1919 (Parent : Tree_Node_Access) return Tree_Node_Access
1920 is
1921 Subtree : constant Tree_Node_Access :=
1922 new Tree_Node_Type'
1923 (Parent => Parent,
1924 Element => Element_Type'Input (Stream),
1925 others => <>);
1926
1927 begin
1928 Read_Count := Read_Count + 1;
1929
1930 Read_Children (Subtree);
1931
1932 return Subtree;
1933 end Read_Subtree;
1934
1935 -- Start of processing for Read
1936
1937 begin
1938 Container.Clear; -- checks busy bit
1939
1940 Count_Type'Read (Stream, Total_Count);
1941
1942 if Checks and then Total_Count < 0 then
1943 raise Program_Error with "attempt to read from corrupt stream";
1944 end if;
1945
1946 if Total_Count = 0 then
1947 return;
1948 end if;
1949
1950 Read_Count := 0;
1951
1952 Read_Children (Root_Node (Container));
1953
1954 if Checks and then Read_Count /= Total_Count then
1955 raise Program_Error with "attempt to read from corrupt stream";
1956 end if;
1957
1958 Container.Count := Total_Count;
1959 end Read;
1960
1961 procedure Read
1962 (Stream : not null access Root_Stream_Type'Class;
1963 Position : out Cursor)
1964 is
1965 begin
1966 raise Program_Error with "attempt to read tree cursor from stream";
1967 end Read;
1968
1969 procedure Read
1970 (Stream : not null access Root_Stream_Type'Class;
1971 Item : out Reference_Type)
1972 is
1973 begin
1974 raise Program_Error with "attempt to stream reference";
1975 end Read;
1976
1977 procedure Read
1978 (Stream : not null access Root_Stream_Type'Class;
1979 Item : out Constant_Reference_Type)
1980 is
1981 begin
1982 raise Program_Error with "attempt to stream reference";
1983 end Read;
1984
1985 ---------------
1986 -- Reference --
1987 ---------------
1988
1989 function Reference
1990 (Container : aliased in out Tree;
1991 Position : Cursor) return Reference_Type
1992 is
1993 begin
1994 if Checks and then Position.Container = null then
1995 raise Constraint_Error with
1996 "Position cursor has no element";
1997 end if;
1998
1999 if Checks and then Position.Container /= Container'Unrestricted_Access
2000 then
2001 raise Program_Error with
2002 "Position cursor designates wrong container";
2003 end if;
2004
2005 if Checks and then Position.Node = Root_Node (Container) then
2006 raise Program_Error with "Position cursor designates root";
2007 end if;
2008
2009 -- Implement Vet for multiway tree???
2010 -- pragma Assert (Vet (Position),
2011 -- "Position cursor in Constant_Reference is bad");
2012
2013 declare
2014 C : Tree renames Position.Container.all;
2015 TC : constant Tamper_Counts_Access :=
2016 C.TC'Unrestricted_Access;
2017 begin
2018 return R : constant Reference_Type :=
2019 (Element => Position.Node.Element'Access,
2020 Control => (Controlled with TC))
2021 do
2022 Busy (TC.all);
2023 end return;
2024 end;
2025 end Reference;
2026
2027 --------------------
2028 -- Remove_Subtree --
2029 --------------------
2030
2031 procedure Remove_Subtree (Subtree : Tree_Node_Access) is
2032 C : Children_Type renames Subtree.Parent.Children;
2033
2034 begin
2035 -- This is a utility operation to remove a subtree node from its
2036 -- parent's list of children.
2037
2038 if C.First = Subtree then
2039 pragma Assert (Subtree.Prev = null);
2040
2041 if C.Last = Subtree then
2042 pragma Assert (Subtree.Next = null);
2043 C.First := null;
2044 C.Last := null;
2045
2046 else
2047 C.First := Subtree.Next;
2048 C.First.Prev := null;
2049 end if;
2050
2051 elsif C.Last = Subtree then
2052 pragma Assert (Subtree.Next = null);
2053 C.Last := Subtree.Prev;
2054 C.Last.Next := null;
2055
2056 else
2057 Subtree.Prev.Next := Subtree.Next;
2058 Subtree.Next.Prev := Subtree.Prev;
2059 end if;
2060 end Remove_Subtree;
2061
2062 ----------------------
2063 -- Replace_Element --
2064 ----------------------
2065
2066 procedure Replace_Element
2067 (Container : in out Tree;
2068 Position : Cursor;
2069 New_Item : Element_Type)
2070 is
2071 begin
2072 TE_Check (Container.TC);
2073
2074 if Checks and then Position = No_Element then
2075 raise Constraint_Error with "Position cursor has no element";
2076 end if;
2077
2078 if Checks and then Position.Container /= Container'Unrestricted_Access
2079 then
2080 raise Program_Error with "Position cursor not in container";
2081 end if;
2082
2083 if Checks and then Is_Root (Position) then
2084 raise Program_Error with "Position cursor designates root";
2085 end if;
2086
2087 Position.Node.Element := New_Item;
2088 end Replace_Element;
2089
2090 ------------------------------
2091 -- Reverse_Iterate_Children --
2092 ------------------------------
2093
2094 procedure Reverse_Iterate_Children
2095 (Parent : Cursor;
2096 Process : not null access procedure (Position : Cursor))
2097 is
2098 C : Tree_Node_Access;
2099 Busy : With_Busy (Parent.Container.TC'Unrestricted_Access);
2100 begin
2101 if Checks and then Parent = No_Element then
2102 raise Constraint_Error with "Parent cursor has no element";
2103 end if;
2104
2105 C := Parent.Node.Children.Last;
2106 while C /= null loop
2107 Process (Position => Cursor'(Parent.Container, Node => C));
2108 C := C.Prev;
2109 end loop;
2110 end Reverse_Iterate_Children;
2111
2112 ----------
2113 -- Root --
2114 ----------
2115
2116 function Root (Container : Tree) return Cursor is
2117 begin
2118 return (Container'Unrestricted_Access, Root_Node (Container));
2119 end Root;
2120
2121 ---------------
2122 -- Root_Node --
2123 ---------------
2124
2125 function Root_Node (Container : Tree) return Tree_Node_Access is
2126 type Root_Node_Access is access all Root_Node_Type;
2127 for Root_Node_Access'Storage_Size use 0;
2128 pragma Convention (C, Root_Node_Access);
2129
2130 function To_Tree_Node_Access is
2131 new Ada.Unchecked_Conversion (Root_Node_Access, Tree_Node_Access);
2132
2133 -- Start of processing for Root_Node
2134
2135 begin
2136 -- This is a utility function for converting from an access type that
2137 -- designates the distinguished root node to an access type designating
2138 -- a non-root node. The representation of a root node does not have an
2139 -- element, but is otherwise identical to a non-root node, so the
2140 -- conversion itself is safe.
2141
2142 return To_Tree_Node_Access (Container.Root'Unrestricted_Access);
2143 end Root_Node;
2144
2145 ---------------------
2146 -- Splice_Children --
2147 ---------------------
2148
2149 procedure Splice_Children
2150 (Target : in out Tree;
2151 Target_Parent : Cursor;
2152 Before : Cursor;
2153 Source : in out Tree;
2154 Source_Parent : Cursor)
2155 is
2156 Count : Count_Type;
2157
2158 begin
2159 TC_Check (Target.TC);
2160 TC_Check (Source.TC);
2161
2162 if Checks and then Target_Parent = No_Element then
2163 raise Constraint_Error with "Target_Parent cursor has no element";
2164 end if;
2165
2166 if Checks and then Target_Parent.Container /= Target'Unrestricted_Access
2167 then
2168 raise Program_Error
2169 with "Target_Parent cursor not in Target container";
2170 end if;
2171
2172 if Before /= No_Element then
2173 if Checks and then Before.Container /= Target'Unrestricted_Access then
2174 raise Program_Error
2175 with "Before cursor not in Target container";
2176 end if;
2177
2178 if Checks and then Before.Node.Parent /= Target_Parent.Node then
2179 raise Constraint_Error
2180 with "Before cursor not child of Target_Parent";
2181 end if;
2182 end if;
2183
2184 if Checks and then Source_Parent = No_Element then
2185 raise Constraint_Error with "Source_Parent cursor has no element";
2186 end if;
2187
2188 if Checks and then Source_Parent.Container /= Source'Unrestricted_Access
2189 then
2190 raise Program_Error
2191 with "Source_Parent cursor not in Source container";
2192 end if;
2193
2194 if Target'Address = Source'Address then
2195 if Target_Parent = Source_Parent then
2196 return;
2197 end if;
2198
2199 if Checks and then Is_Reachable (From => Target_Parent.Node,
2200 To => Source_Parent.Node)
2201 then
2202 raise Constraint_Error
2203 with "Source_Parent is ancestor of Target_Parent";
2204 end if;
2205
2206 Splice_Children
2207 (Target_Parent => Target_Parent.Node,
2208 Before => Before.Node,
2209 Source_Parent => Source_Parent.Node);
2210
2211 return;
2212 end if;
2213
2214 -- We cache the count of the nodes we have allocated, so that operation
2215 -- Node_Count can execute in O(1) time. But that means we must count the
2216 -- nodes in the subtree we remove from Source and insert into Target, in
2217 -- order to keep the count accurate.
2218
2219 Count := Subtree_Node_Count (Source_Parent.Node);
2220 pragma Assert (Count >= 1);
2221
2222 Count := Count - 1; -- because Source_Parent node does not move
2223
2224 Splice_Children
2225 (Target_Parent => Target_Parent.Node,
2226 Before => Before.Node,
2227 Source_Parent => Source_Parent.Node);
2228
2229 Source.Count := Source.Count - Count;
2230 Target.Count := Target.Count + Count;
2231 end Splice_Children;
2232
2233 procedure Splice_Children
2234 (Container : in out Tree;
2235 Target_Parent : Cursor;
2236 Before : Cursor;
2237 Source_Parent : Cursor)
2238 is
2239 begin
2240 TC_Check (Container.TC);
2241
2242 if Checks and then Target_Parent = No_Element then
2243 raise Constraint_Error with "Target_Parent cursor has no element";
2244 end if;
2245
2246 if Checks and then
2247 Target_Parent.Container /= Container'Unrestricted_Access
2248 then
2249 raise Program_Error
2250 with "Target_Parent cursor not in container";
2251 end if;
2252
2253 if Before /= No_Element then
2254 if Checks and then Before.Container /= Container'Unrestricted_Access
2255 then
2256 raise Program_Error
2257 with "Before cursor not in container";
2258 end if;
2259
2260 if Checks and then Before.Node.Parent /= Target_Parent.Node then
2261 raise Constraint_Error
2262 with "Before cursor not child of Target_Parent";
2263 end if;
2264 end if;
2265
2266 if Checks and then Source_Parent = No_Element then
2267 raise Constraint_Error with "Source_Parent cursor has no element";
2268 end if;
2269
2270 if Checks and then
2271 Source_Parent.Container /= Container'Unrestricted_Access
2272 then
2273 raise Program_Error
2274 with "Source_Parent cursor not in container";
2275 end if;
2276
2277 if Target_Parent = Source_Parent then
2278 return;
2279 end if;
2280
2281 if Checks and then Is_Reachable (From => Target_Parent.Node,
2282 To => Source_Parent.Node)
2283 then
2284 raise Constraint_Error
2285 with "Source_Parent is ancestor of Target_Parent";
2286 end if;
2287
2288 Splice_Children
2289 (Target_Parent => Target_Parent.Node,
2290 Before => Before.Node,
2291 Source_Parent => Source_Parent.Node);
2292 end Splice_Children;
2293
2294 procedure Splice_Children
2295 (Target_Parent : Tree_Node_Access;
2296 Before : Tree_Node_Access;
2297 Source_Parent : Tree_Node_Access)
2298 is
2299 CC : constant Children_Type := Source_Parent.Children;
2300 C : Tree_Node_Access;
2301
2302 begin
2303 -- This is a utility operation to remove the children from
2304 -- Source parent and insert them into Target parent.
2305
2306 Source_Parent.Children := Children_Type'(others => null);
2307
2308 -- Fix up the Parent pointers of each child to designate
2309 -- its new Target parent.
2310
2311 C := CC.First;
2312 while C /= null loop
2313 C.Parent := Target_Parent;
2314 C := C.Next;
2315 end loop;
2316
2317 Insert_Subtree_List
2318 (First => CC.First,
2319 Last => CC.Last,
2320 Parent => Target_Parent,
2321 Before => Before);
2322 end Splice_Children;
2323
2324 --------------------
2325 -- Splice_Subtree --
2326 --------------------
2327
2328 procedure Splice_Subtree
2329 (Target : in out Tree;
2330 Parent : Cursor;
2331 Before : Cursor;
2332 Source : in out Tree;
2333 Position : in out Cursor)
2334 is
2335 Subtree_Count : Count_Type;
2336
2337 begin
2338 TC_Check (Target.TC);
2339 TC_Check (Source.TC);
2340
2341 if Checks and then Parent = No_Element then
2342 raise Constraint_Error with "Parent cursor has no element";
2343 end if;
2344
2345 if Checks and then Parent.Container /= Target'Unrestricted_Access then
2346 raise Program_Error with "Parent cursor not in Target container";
2347 end if;
2348
2349 if Before /= No_Element then
2350 if Checks and then Before.Container /= Target'Unrestricted_Access then
2351 raise Program_Error with "Before cursor not in Target container";
2352 end if;
2353
2354 if Checks and then Before.Node.Parent /= Parent.Node then
2355 raise Constraint_Error with "Before cursor not child of Parent";
2356 end if;
2357 end if;
2358
2359 if Checks and then Position = No_Element then
2360 raise Constraint_Error with "Position cursor has no element";
2361 end if;
2362
2363 if Checks and then Position.Container /= Source'Unrestricted_Access then
2364 raise Program_Error with "Position cursor not in Source container";
2365 end if;
2366
2367 if Checks and then Is_Root (Position) then
2368 raise Program_Error with "Position cursor designates root";
2369 end if;
2370
2371 if Target'Address = Source'Address then
2372 if Position.Node.Parent = Parent.Node then
2373 if Position.Node = Before.Node then
2374 return;
2375 end if;
2376
2377 if Position.Node.Next = Before.Node then
2378 return;
2379 end if;
2380 end if;
2381
2382 if Checks and then
2383 Is_Reachable (From => Parent.Node, To => Position.Node)
2384 then
2385 raise Constraint_Error with "Position is ancestor of Parent";
2386 end if;
2387
2388 Remove_Subtree (Position.Node);
2389
2390 Position.Node.Parent := Parent.Node;
2391 Insert_Subtree_Node (Position.Node, Parent.Node, Before.Node);
2392
2393 return;
2394 end if;
2395
2396 -- This is an unfortunate feature of this API: we must count the nodes
2397 -- in the subtree that we remove from the source tree, which is an O(n)
2398 -- operation. It would have been better if the Tree container did not
2399 -- have a Node_Count selector; a user that wants the number of nodes in
2400 -- the tree could simply call Subtree_Node_Count, with the understanding
2401 -- that such an operation is O(n).
2402
2403 -- Of course, we could choose to implement the Node_Count selector as an
2404 -- O(n) operation, which would turn this splice operation into an O(1)
2405 -- operation. ???
2406
2407 Subtree_Count := Subtree_Node_Count (Position.Node);
2408 pragma Assert (Subtree_Count <= Source.Count);
2409
2410 Remove_Subtree (Position.Node);
2411 Source.Count := Source.Count - Subtree_Count;
2412
2413 Position.Node.Parent := Parent.Node;
2414 Insert_Subtree_Node (Position.Node, Parent.Node, Before.Node);
2415
2416 Target.Count := Target.Count + Subtree_Count;
2417
2418 Position.Container := Target'Unrestricted_Access;
2419 end Splice_Subtree;
2420
2421 procedure Splice_Subtree
2422 (Container : in out Tree;
2423 Parent : Cursor;
2424 Before : Cursor;
2425 Position : Cursor)
2426 is
2427 begin
2428 TC_Check (Container.TC);
2429
2430 if Checks and then Parent = No_Element then
2431 raise Constraint_Error with "Parent cursor has no element";
2432 end if;
2433
2434 if Checks and then Parent.Container /= Container'Unrestricted_Access then
2435 raise Program_Error with "Parent cursor not in container";
2436 end if;
2437
2438 if Before /= No_Element then
2439 if Checks and then Before.Container /= Container'Unrestricted_Access
2440 then
2441 raise Program_Error with "Before cursor not in container";
2442 end if;
2443
2444 if Checks and then Before.Node.Parent /= Parent.Node then
2445 raise Constraint_Error with "Before cursor not child of Parent";
2446 end if;
2447 end if;
2448
2449 if Checks and then Position = No_Element then
2450 raise Constraint_Error with "Position cursor has no element";
2451 end if;
2452
2453 if Checks and then Position.Container /= Container'Unrestricted_Access
2454 then
2455 raise Program_Error with "Position cursor not in container";
2456 end if;
2457
2458 if Checks and then Is_Root (Position) then
2459
2460 -- Should this be PE instead? Need ARG confirmation. ???
2461
2462 raise Constraint_Error with "Position cursor designates root";
2463 end if;
2464
2465 if Position.Node.Parent = Parent.Node then
2466 if Position.Node = Before.Node then
2467 return;
2468 end if;
2469
2470 if Position.Node.Next = Before.Node then
2471 return;
2472 end if;
2473 end if;
2474
2475 if Checks and then
2476 Is_Reachable (From => Parent.Node, To => Position.Node)
2477 then
2478 raise Constraint_Error with "Position is ancestor of Parent";
2479 end if;
2480
2481 Remove_Subtree (Position.Node);
2482
2483 Position.Node.Parent := Parent.Node;
2484 Insert_Subtree_Node (Position.Node, Parent.Node, Before.Node);
2485 end Splice_Subtree;
2486
2487 ------------------------
2488 -- Subtree_Node_Count --
2489 ------------------------
2490
2491 function Subtree_Node_Count (Position : Cursor) return Count_Type is
2492 begin
2493 if Position = No_Element then
2494 return 0;
2495 end if;
2496
2497 return Subtree_Node_Count (Position.Node);
2498 end Subtree_Node_Count;
2499
2500 function Subtree_Node_Count
2501 (Subtree : Tree_Node_Access) return Count_Type
2502 is
2503 Result : Count_Type;
2504 Node : Tree_Node_Access;
2505
2506 begin
2507 Result := 1;
2508 Node := Subtree.Children.First;
2509 while Node /= null loop
2510 Result := Result + Subtree_Node_Count (Node);
2511 Node := Node.Next;
2512 end loop;
2513
2514 return Result;
2515 end Subtree_Node_Count;
2516
2517 ----------
2518 -- Swap --
2519 ----------
2520
2521 procedure Swap
2522 (Container : in out Tree;
2523 I, J : Cursor)
2524 is
2525 begin
2526 TE_Check (Container.TC);
2527
2528 if Checks and then I = No_Element then
2529 raise Constraint_Error with "I cursor has no element";
2530 end if;
2531
2532 if Checks and then I.Container /= Container'Unrestricted_Access then
2533 raise Program_Error with "I cursor not in container";
2534 end if;
2535
2536 if Checks and then Is_Root (I) then
2537 raise Program_Error with "I cursor designates root";
2538 end if;
2539
2540 if I = J then -- make this test sooner???
2541 return;
2542 end if;
2543
2544 if Checks and then J = No_Element then
2545 raise Constraint_Error with "J cursor has no element";
2546 end if;
2547
2548 if Checks and then J.Container /= Container'Unrestricted_Access then
2549 raise Program_Error with "J cursor not in container";
2550 end if;
2551
2552 if Checks and then Is_Root (J) then
2553 raise Program_Error with "J cursor designates root";
2554 end if;
2555
2556 declare
2557 EI : constant Element_Type := I.Node.Element;
2558
2559 begin
2560 I.Node.Element := J.Node.Element;
2561 J.Node.Element := EI;
2562 end;
2563 end Swap;
2564
2565 --------------------
2566 -- Update_Element --
2567 --------------------
2568
2569 procedure Update_Element
2570 (Container : in out Tree;
2571 Position : Cursor;
2572 Process : not null access procedure (Element : in out Element_Type))
2573 is
2574 T : Tree renames Position.Container.all'Unrestricted_Access.all;
2575 Lock : With_Lock (T.TC'Unrestricted_Access);
2576 begin
2577 if Checks and then Position = No_Element then
2578 raise Constraint_Error with "Position cursor has no element";
2579 end if;
2580
2581 if Checks and then Position.Container /= Container'Unrestricted_Access
2582 then
2583 raise Program_Error with "Position cursor not in container";
2584 end if;
2585
2586 if Checks and then Is_Root (Position) then
2587 raise Program_Error with "Position cursor designates root";
2588 end if;
2589
2590 Process (Position.Node.Element);
2591 end Update_Element;
2592
2593 -----------
2594 -- Write --
2595 -----------
2596
2597 procedure Write
2598 (Stream : not null access Root_Stream_Type'Class;
2599 Container : Tree)
2600 is
2601 procedure Write_Children (Subtree : Tree_Node_Access);
2602 procedure Write_Subtree (Subtree : Tree_Node_Access);
2603
2604 --------------------
2605 -- Write_Children --
2606 --------------------
2607
2608 procedure Write_Children (Subtree : Tree_Node_Access) is
2609 CC : Children_Type renames Subtree.Children;
2610 C : Tree_Node_Access;
2611
2612 begin
2613 Count_Type'Write (Stream, Child_Count (CC));
2614
2615 C := CC.First;
2616 while C /= null loop
2617 Write_Subtree (C);
2618 C := C.Next;
2619 end loop;
2620 end Write_Children;
2621
2622 -------------------
2623 -- Write_Subtree --
2624 -------------------
2625
2626 procedure Write_Subtree (Subtree : Tree_Node_Access) is
2627 begin
2628 Element_Type'Output (Stream, Subtree.Element);
2629 Write_Children (Subtree);
2630 end Write_Subtree;
2631
2632 -- Start of processing for Write
2633
2634 begin
2635 Count_Type'Write (Stream, Container.Count);
2636
2637 if Container.Count = 0 then
2638 return;
2639 end if;
2640
2641 Write_Children (Root_Node (Container));
2642 end Write;
2643
2644 procedure Write
2645 (Stream : not null access Root_Stream_Type'Class;
2646 Position : Cursor)
2647 is
2648 begin
2649 raise Program_Error with "attempt to write tree cursor to stream";
2650 end Write;
2651
2652 procedure Write
2653 (Stream : not null access Root_Stream_Type'Class;
2654 Item : Reference_Type)
2655 is
2656 begin
2657 raise Program_Error with "attempt to stream reference";
2658 end Write;
2659
2660 procedure Write
2661 (Stream : not null access Root_Stream_Type'Class;
2662 Item : Constant_Reference_Type)
2663 is
2664 begin
2665 raise Program_Error with "attempt to stream reference";
2666 end Write;
2667
2668 end Ada.Containers.Multiway_Trees;