2fedd3c3b64a3e62001f30d7c08c55e1448990fe
[gcc.git] / gcc / ada / a-cdlili.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- A D A . C O N T A I N E R S . D O U B L Y _ L I N K E D _ L I S T S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2004-2013, 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_Deallocation;
31
32 with System; use type System.Address;
33
34 package body Ada.Containers.Doubly_Linked_Lists is
35
36 type Iterator is new Limited_Controlled and
37 List_Iterator_Interfaces.Reversible_Iterator with
38 record
39 Container : List_Access;
40 Node : Node_Access;
41 end record;
42
43 overriding procedure Finalize (Object : in out Iterator);
44
45 overriding function First (Object : Iterator) return Cursor;
46 overriding function Last (Object : Iterator) return Cursor;
47
48 overriding function Next
49 (Object : Iterator;
50 Position : Cursor) return Cursor;
51
52 overriding function Previous
53 (Object : Iterator;
54 Position : Cursor) return Cursor;
55
56 -----------------------
57 -- Local Subprograms --
58 -----------------------
59
60 procedure Free (X : in out Node_Access);
61
62 procedure Insert_Internal
63 (Container : in out List;
64 Before : Node_Access;
65 New_Node : Node_Access);
66
67 procedure Splice_Internal
68 (Target : in out List;
69 Before : Node_Access;
70 Source : in out List);
71
72 procedure Splice_Internal
73 (Target : in out List;
74 Before : Node_Access;
75 Source : in out List;
76 Position : Node_Access);
77
78 function Vet (Position : Cursor) return Boolean;
79 -- Checks invariants of the cursor and its designated container, as a
80 -- simple way of detecting dangling references (see operation Free for a
81 -- description of the detection mechanism), returning True if all checks
82 -- pass. Invocations of Vet are used here as the argument of pragma Assert,
83 -- so the checks are performed only when assertions are enabled.
84
85 ---------
86 -- "=" --
87 ---------
88
89 function "=" (Left, Right : List) return Boolean is
90 BL : Natural renames Left'Unrestricted_Access.Busy;
91 LL : Natural renames Left'Unrestricted_Access.Lock;
92
93 BR : Natural renames Right'Unrestricted_Access.Busy;
94 LR : Natural renames Right'Unrestricted_Access.Lock;
95
96 L : Node_Access;
97 R : Node_Access;
98 Result : Boolean;
99
100 begin
101 if Left'Address = Right'Address then
102 return True;
103 end if;
104
105 if Left.Length /= Right.Length then
106 return False;
107 end if;
108
109 -- Per AI05-0022, the container implementation is required to detect
110 -- element tampering by a generic actual subprogram.
111
112 BL := BL + 1;
113 LL := LL + 1;
114
115 BR := BR + 1;
116 LR := LR + 1;
117
118 L := Left.First;
119 R := Right.First;
120 Result := True;
121 for J in 1 .. Left.Length loop
122 if L.Element /= R.Element then
123 Result := False;
124 exit;
125 end if;
126
127 L := L.Next;
128 R := R.Next;
129 end loop;
130
131 BL := BL - 1;
132 LL := LL - 1;
133
134 BR := BR - 1;
135 LR := LR - 1;
136
137 return Result;
138 exception
139 when others =>
140 BL := BL - 1;
141 LL := LL - 1;
142
143 BR := BR - 1;
144 LR := LR - 1;
145
146 raise;
147 end "=";
148
149 ------------
150 -- Adjust --
151 ------------
152
153 procedure Adjust (Container : in out List) is
154 Src : Node_Access := Container.First;
155
156 begin
157 if Src = null then
158 pragma Assert (Container.Last = null);
159 pragma Assert (Container.Length = 0);
160 pragma Assert (Container.Busy = 0);
161 pragma Assert (Container.Lock = 0);
162 return;
163 end if;
164
165 pragma Assert (Container.First.Prev = null);
166 pragma Assert (Container.Last.Next = null);
167 pragma Assert (Container.Length > 0);
168
169 Container.First := null;
170 Container.Last := null;
171 Container.Length := 0;
172 Container.Busy := 0;
173 Container.Lock := 0;
174
175 Container.First := new Node_Type'(Src.Element, null, null);
176 Container.Last := Container.First;
177 Container.Length := 1;
178
179 Src := Src.Next;
180 while Src /= null loop
181 Container.Last.Next := new Node_Type'(Element => Src.Element,
182 Prev => Container.Last,
183 Next => null);
184 Container.Last := Container.Last.Next;
185 Container.Length := Container.Length + 1;
186
187 Src := Src.Next;
188 end loop;
189 end Adjust;
190
191 procedure Adjust (Control : in out Reference_Control_Type) is
192 begin
193 if Control.Container /= null then
194 declare
195 C : List renames Control.Container.all;
196 B : Natural renames C.Busy;
197 L : Natural renames C.Lock;
198 begin
199 B := B + 1;
200 L := L + 1;
201 end;
202 end if;
203 end Adjust;
204
205 ------------
206 -- Append --
207 ------------
208
209 procedure Append
210 (Container : in out List;
211 New_Item : Element_Type;
212 Count : Count_Type := 1)
213 is
214 begin
215 Insert (Container, No_Element, New_Item, Count);
216 end Append;
217
218 ------------
219 -- Assign --
220 ------------
221
222 procedure Assign (Target : in out List; Source : List) is
223 Node : Node_Access;
224
225 begin
226 if Target'Address = Source'Address then
227 return;
228 end if;
229
230 Target.Clear;
231
232 Node := Source.First;
233 while Node /= null loop
234 Target.Append (Node.Element);
235 Node := Node.Next;
236 end loop;
237 end Assign;
238
239 -----------
240 -- Clear --
241 -----------
242
243 procedure Clear (Container : in out List) is
244 X : Node_Access;
245
246 begin
247 if Container.Length = 0 then
248 pragma Assert (Container.First = null);
249 pragma Assert (Container.Last = null);
250 pragma Assert (Container.Busy = 0);
251 pragma Assert (Container.Lock = 0);
252 return;
253 end if;
254
255 pragma Assert (Container.First.Prev = null);
256 pragma Assert (Container.Last.Next = null);
257
258 if Container.Busy > 0 then
259 raise Program_Error with
260 "attempt to tamper with cursors (list is busy)";
261 end if;
262
263 while Container.Length > 1 loop
264 X := Container.First;
265 pragma Assert (X.Next.Prev = Container.First);
266
267 Container.First := X.Next;
268 Container.First.Prev := null;
269
270 Container.Length := Container.Length - 1;
271
272 Free (X);
273 end loop;
274
275 X := Container.First;
276 pragma Assert (X = Container.Last);
277
278 Container.First := null;
279 Container.Last := null;
280 Container.Length := 0;
281
282 pragma Warnings (Off);
283 Free (X);
284 pragma Warnings (On);
285 end Clear;
286
287 ------------------------
288 -- Constant_Reference --
289 ------------------------
290
291 function Constant_Reference
292 (Container : aliased List;
293 Position : Cursor) return Constant_Reference_Type
294 is
295 begin
296 if Position.Container = null then
297 raise Constraint_Error with "Position cursor has no element";
298 end if;
299
300 if Position.Container /= Container'Unrestricted_Access then
301 raise Program_Error with
302 "Position cursor designates wrong container";
303 end if;
304
305 pragma Assert (Vet (Position), "bad cursor in Constant_Reference");
306
307 declare
308 C : List renames Position.Container.all;
309 B : Natural renames C.Busy;
310 L : Natural renames C.Lock;
311 begin
312 return R : constant Constant_Reference_Type :=
313 (Element => Position.Node.Element'Access,
314 Control => (Controlled with Container'Unrestricted_Access))
315 do
316 B := B + 1;
317 L := L + 1;
318 end return;
319 end;
320 end Constant_Reference;
321
322 --------------
323 -- Contains --
324 --------------
325
326 function Contains
327 (Container : List;
328 Item : Element_Type) return Boolean
329 is
330 begin
331 return Find (Container, Item) /= No_Element;
332 end Contains;
333
334 ----------
335 -- Copy --
336 ----------
337
338 function Copy (Source : List) return List is
339 begin
340 return Target : List do
341 Target.Assign (Source);
342 end return;
343 end Copy;
344
345 ------------
346 -- Delete --
347 ------------
348
349 procedure Delete
350 (Container : in out List;
351 Position : in out Cursor;
352 Count : Count_Type := 1)
353 is
354 X : Node_Access;
355
356 begin
357 if Position.Node = null then
358 raise Constraint_Error with
359 "Position cursor has no element";
360 end if;
361
362 if Position.Container /= Container'Unrestricted_Access then
363 raise Program_Error with
364 "Position cursor designates wrong container";
365 end if;
366
367 pragma Assert (Vet (Position), "bad cursor in Delete");
368
369 if Position.Node = Container.First then
370 Delete_First (Container, Count);
371 Position := No_Element; -- Post-York behavior
372 return;
373 end if;
374
375 if Count = 0 then
376 Position := No_Element; -- Post-York behavior
377 return;
378 end if;
379
380 if Container.Busy > 0 then
381 raise Program_Error with
382 "attempt to tamper with cursors (list is busy)";
383 end if;
384
385 for Index in 1 .. Count loop
386 X := Position.Node;
387 Container.Length := Container.Length - 1;
388
389 if X = Container.Last then
390 Position := No_Element;
391
392 Container.Last := X.Prev;
393 Container.Last.Next := null;
394
395 Free (X);
396 return;
397 end if;
398
399 Position.Node := X.Next;
400
401 X.Next.Prev := X.Prev;
402 X.Prev.Next := X.Next;
403
404 Free (X);
405 end loop;
406
407 Position := No_Element; -- Post-York behavior
408 end Delete;
409
410 ------------------
411 -- Delete_First --
412 ------------------
413
414 procedure Delete_First
415 (Container : in out List;
416 Count : Count_Type := 1)
417 is
418 X : Node_Access;
419
420 begin
421 if Count >= Container.Length then
422 Clear (Container);
423 return;
424 end if;
425
426 if Count = 0 then
427 return;
428 end if;
429
430 if Container.Busy > 0 then
431 raise Program_Error with
432 "attempt to tamper with cursors (list is busy)";
433 end if;
434
435 for I in 1 .. Count loop
436 X := Container.First;
437 pragma Assert (X.Next.Prev = Container.First);
438
439 Container.First := X.Next;
440 Container.First.Prev := null;
441
442 Container.Length := Container.Length - 1;
443
444 Free (X);
445 end loop;
446 end Delete_First;
447
448 -----------------
449 -- Delete_Last --
450 -----------------
451
452 procedure Delete_Last
453 (Container : in out List;
454 Count : Count_Type := 1)
455 is
456 X : Node_Access;
457
458 begin
459 if Count >= Container.Length then
460 Clear (Container);
461 return;
462 end if;
463
464 if Count = 0 then
465 return;
466 end if;
467
468 if Container.Busy > 0 then
469 raise Program_Error with
470 "attempt to tamper with cursors (list is busy)";
471 end if;
472
473 for I in 1 .. Count loop
474 X := Container.Last;
475 pragma Assert (X.Prev.Next = Container.Last);
476
477 Container.Last := X.Prev;
478 Container.Last.Next := null;
479
480 Container.Length := Container.Length - 1;
481
482 Free (X);
483 end loop;
484 end Delete_Last;
485
486 -------------
487 -- Element --
488 -------------
489
490 function Element (Position : Cursor) return Element_Type is
491 begin
492 if Position.Node = null then
493 raise Constraint_Error with
494 "Position cursor has no element";
495 end if;
496
497 pragma Assert (Vet (Position), "bad cursor in Element");
498
499 return Position.Node.Element;
500 end Element;
501
502 --------------
503 -- Finalize --
504 --------------
505
506 procedure Finalize (Object : in out Iterator) is
507 begin
508 if Object.Container /= null then
509 declare
510 B : Natural renames Object.Container.all.Busy;
511 begin
512 B := B - 1;
513 end;
514 end if;
515 end Finalize;
516
517 procedure Finalize (Control : in out Reference_Control_Type) is
518 begin
519 if Control.Container /= null then
520 declare
521 C : List renames Control.Container.all;
522 B : Natural renames C.Busy;
523 L : Natural renames C.Lock;
524 begin
525 B := B - 1;
526 L := L - 1;
527 end;
528
529 Control.Container := null;
530 end if;
531 end Finalize;
532
533 ----------
534 -- Find --
535 ----------
536
537 function Find
538 (Container : List;
539 Item : Element_Type;
540 Position : Cursor := No_Element) return Cursor
541 is
542 Node : Node_Access := Position.Node;
543
544 begin
545 if Node = null then
546 Node := Container.First;
547
548 else
549 if Position.Container /= Container'Unrestricted_Access then
550 raise Program_Error with
551 "Position cursor designates wrong container";
552 end if;
553
554 pragma Assert (Vet (Position), "bad cursor in Find");
555 end if;
556
557 -- Per AI05-0022, the container implementation is required to detect
558 -- element tampering by a generic actual subprogram.
559
560 declare
561 B : Natural renames Container'Unrestricted_Access.Busy;
562 L : Natural renames Container'Unrestricted_Access.Lock;
563
564 Result : Node_Access;
565
566 begin
567 B := B + 1;
568 L := L + 1;
569
570 Result := null;
571 while Node /= null loop
572 if Node.Element = Item then
573 Result := Node;
574 exit;
575 end if;
576
577 Node := Node.Next;
578 end loop;
579
580 B := B - 1;
581 L := L - 1;
582
583 if Result = null then
584 return No_Element;
585 else
586 return Cursor'(Container'Unrestricted_Access, Result);
587 end if;
588 exception
589 when others =>
590 B := B - 1;
591 L := L - 1;
592 raise;
593 end;
594 end Find;
595
596 -----------
597 -- First --
598 -----------
599
600 function First (Container : List) return Cursor is
601 begin
602 if Container.First = null then
603 return No_Element;
604 end if;
605
606 return Cursor'(Container'Unrestricted_Access, Container.First);
607 end First;
608
609 function First (Object : Iterator) return Cursor is
610 begin
611 -- The value of the iterator object's Node component influences the
612 -- behavior of the First (and Last) selector function.
613
614 -- When the Node component is null, this means the iterator object was
615 -- constructed without a start expression, in which case the (forward)
616 -- iteration starts from the (logical) beginning of the entire sequence
617 -- of items (corresponding to Container.First, for a forward iterator).
618
619 -- Otherwise, this is iteration over a partial sequence of items. When
620 -- the Node component is non-null, the iterator object was constructed
621 -- with a start expression, that specifies the position from which the
622 -- (forward) partial iteration begins.
623
624 if Object.Node = null then
625 return Doubly_Linked_Lists.First (Object.Container.all);
626 else
627 return Cursor'(Object.Container, Object.Node);
628 end if;
629 end First;
630
631 -------------------
632 -- First_Element --
633 -------------------
634
635 function First_Element (Container : List) return Element_Type is
636 begin
637 if Container.First = null then
638 raise Constraint_Error with "list is empty";
639 end if;
640
641 return Container.First.Element;
642 end First_Element;
643
644 ----------
645 -- Free --
646 ----------
647
648 procedure Free (X : in out Node_Access) is
649 procedure Deallocate is
650 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
651 begin
652 -- While a node is in use, as an active link in a list, its Previous and
653 -- Next components must be null, or designate a different node; this is
654 -- a node invariant. Before actually deallocating the node, we set both
655 -- access value components of the node to point to the node itself, thus
656 -- falsifying the node invariant. Subprogram Vet inspects the value of
657 -- the node components when interrogating the node, in order to detect
658 -- whether the cursor's node access value is dangling.
659
660 -- Note that we have no guarantee that the storage for the node isn't
661 -- modified when it is deallocated, but there are other tests that Vet
662 -- does if node invariants appear to be satisifed. However, in practice
663 -- this simple test works well enough, detecting dangling references
664 -- immediately, without needing further interrogation.
665
666 X.Prev := X;
667 X.Next := X;
668
669 Deallocate (X);
670 end Free;
671
672 ---------------------
673 -- Generic_Sorting --
674 ---------------------
675
676 package body Generic_Sorting is
677
678 ---------------
679 -- Is_Sorted --
680 ---------------
681
682 function Is_Sorted (Container : List) return Boolean is
683 B : Natural renames Container'Unrestricted_Access.Busy;
684 L : Natural renames Container'Unrestricted_Access.Lock;
685
686 Node : Node_Access;
687 Result : Boolean;
688
689 begin
690 -- Per AI05-0022, the container implementation is required to detect
691 -- element tampering by a generic actual subprogram.
692
693 B := B + 1;
694 L := L + 1;
695
696 Node := Container.First;
697 Result := True;
698 for Idx in 2 .. Container.Length loop
699 if Node.Next.Element < Node.Element then
700 Result := False;
701 exit;
702 end if;
703
704 Node := Node.Next;
705 end loop;
706
707 B := B - 1;
708 L := L - 1;
709
710 return Result;
711 exception
712 when others =>
713 B := B - 1;
714 L := L - 1;
715 raise;
716 end Is_Sorted;
717
718 -----------
719 -- Merge --
720 -----------
721
722 procedure Merge
723 (Target : in out List;
724 Source : in out List)
725 is
726 begin
727 -- The semantics of Merge changed slightly per AI05-0021. It was
728 -- originally the case that if Target and Source denoted the same
729 -- container object, then the GNAT implementation of Merge did
730 -- nothing. However, it was argued that RM05 did not precisely
731 -- specify the semantics for this corner case. The decision of the
732 -- ARG was that if Target and Source denote the same non-empty
733 -- container object, then Program_Error is raised.
734
735 if Source.Is_Empty then
736 return;
737 end if;
738
739 if Target'Address = Source'Address then
740 raise Program_Error with
741 "Target and Source denote same non-empty container";
742 end if;
743
744 if Target.Length > Count_Type'Last - Source.Length then
745 raise Constraint_Error with "new length exceeds maximum";
746 end if;
747
748 if Target.Busy > 0 then
749 raise Program_Error with
750 "attempt to tamper with cursors of Target (list is busy)";
751 end if;
752
753 if Source.Busy > 0 then
754 raise Program_Error with
755 "attempt to tamper with cursors of Source (list is busy)";
756 end if;
757
758 -- Per AI05-0022, the container implementation is required to detect
759 -- element tampering by a generic actual subprogram.
760
761 declare
762 TB : Natural renames Target.Busy;
763 TL : Natural renames Target.Lock;
764
765 SB : Natural renames Source.Busy;
766 SL : Natural renames Source.Lock;
767
768 LI, RI, RJ : Node_Access;
769
770 begin
771 TB := TB + 1;
772 TL := TL + 1;
773
774 SB := SB + 1;
775 SL := SL + 1;
776
777 LI := Target.First;
778 RI := Source.First;
779 while RI /= null loop
780 pragma Assert (RI.Next = null
781 or else not (RI.Next.Element < RI.Element));
782
783 if LI = null then
784 Splice_Internal (Target, null, Source);
785 exit;
786 end if;
787
788 pragma Assert (LI.Next = null
789 or else not (LI.Next.Element < LI.Element));
790
791 if RI.Element < LI.Element then
792 RJ := RI;
793 RI := RI.Next;
794 Splice_Internal (Target, LI, Source, RJ);
795
796 else
797 LI := LI.Next;
798 end if;
799 end loop;
800
801 TB := TB - 1;
802 TL := TL - 1;
803
804 SB := SB - 1;
805 SL := SL - 1;
806 exception
807 when others =>
808 TB := TB - 1;
809 TL := TL - 1;
810
811 SB := SB - 1;
812 SL := SL - 1;
813
814 raise;
815 end;
816 end Merge;
817
818 ----------
819 -- Sort --
820 ----------
821
822 procedure Sort (Container : in out List) is
823
824 procedure Partition (Pivot : Node_Access; Back : Node_Access);
825
826 procedure Sort (Front, Back : Node_Access);
827
828 ---------------
829 -- Partition --
830 ---------------
831
832 procedure Partition (Pivot : Node_Access; Back : Node_Access) is
833 Node : Node_Access := Pivot.Next;
834
835 begin
836 while Node /= Back loop
837 if Node.Element < Pivot.Element then
838 declare
839 Prev : constant Node_Access := Node.Prev;
840 Next : constant Node_Access := Node.Next;
841
842 begin
843 Prev.Next := Next;
844
845 if Next = null then
846 Container.Last := Prev;
847 else
848 Next.Prev := Prev;
849 end if;
850
851 Node.Next := Pivot;
852 Node.Prev := Pivot.Prev;
853
854 Pivot.Prev := Node;
855
856 if Node.Prev = null then
857 Container.First := Node;
858 else
859 Node.Prev.Next := Node;
860 end if;
861
862 Node := Next;
863 end;
864
865 else
866 Node := Node.Next;
867 end if;
868 end loop;
869 end Partition;
870
871 ----------
872 -- Sort --
873 ----------
874
875 procedure Sort (Front, Back : Node_Access) is
876 Pivot : constant Node_Access :=
877 (if Front = null then Container.First else Front.Next);
878 begin
879 if Pivot /= Back then
880 Partition (Pivot, Back);
881 Sort (Front, Pivot);
882 Sort (Pivot, Back);
883 end if;
884 end Sort;
885
886 -- Start of processing for Sort
887
888 begin
889 if Container.Length <= 1 then
890 return;
891 end if;
892
893 pragma Assert (Container.First.Prev = null);
894 pragma Assert (Container.Last.Next = null);
895
896 if Container.Busy > 0 then
897 raise Program_Error with
898 "attempt to tamper with cursors (list is busy)";
899 end if;
900
901 -- Per AI05-0022, the container implementation is required to detect
902 -- element tampering by a generic actual subprogram.
903
904 declare
905 B : Natural renames Container.Busy;
906 L : Natural renames Container.Lock;
907
908 begin
909 B := B + 1;
910 L := L + 1;
911
912 Sort (Front => null, Back => null);
913
914 B := B - 1;
915 L := L - 1;
916 exception
917 when others =>
918 B := B - 1;
919 L := L - 1;
920 raise;
921 end;
922
923 pragma Assert (Container.First.Prev = null);
924 pragma Assert (Container.Last.Next = null);
925 end Sort;
926
927 end Generic_Sorting;
928
929 -----------------
930 -- Has_Element --
931 -----------------
932
933 function Has_Element (Position : Cursor) return Boolean is
934 begin
935 pragma Assert (Vet (Position), "bad cursor in Has_Element");
936 return Position.Node /= null;
937 end Has_Element;
938
939 ------------
940 -- Insert --
941 ------------
942
943 procedure Insert
944 (Container : in out List;
945 Before : Cursor;
946 New_Item : Element_Type;
947 Position : out Cursor;
948 Count : Count_Type := 1)
949 is
950 New_Node : Node_Access;
951
952 begin
953 if Before.Container /= null then
954 if Before.Container /= Container'Unrestricted_Access then
955 raise Program_Error with
956 "Before cursor designates wrong list";
957 end if;
958
959 pragma Assert (Vet (Before), "bad cursor in Insert");
960 end if;
961
962 if Count = 0 then
963 Position := Before;
964 return;
965 end if;
966
967 if Container.Length > Count_Type'Last - Count then
968 raise Constraint_Error with "new length exceeds maximum";
969 end if;
970
971 if Container.Busy > 0 then
972 raise Program_Error with
973 "attempt to tamper with cursors (list is busy)";
974 end if;
975
976 New_Node := new Node_Type'(New_Item, null, null);
977 Insert_Internal (Container, Before.Node, New_Node);
978
979 Position := Cursor'(Container'Unchecked_Access, New_Node);
980
981 for J in Count_Type'(2) .. Count loop
982 New_Node := new Node_Type'(New_Item, null, null);
983 Insert_Internal (Container, Before.Node, New_Node);
984 end loop;
985 end Insert;
986
987 procedure Insert
988 (Container : in out List;
989 Before : Cursor;
990 New_Item : Element_Type;
991 Count : Count_Type := 1)
992 is
993 Position : Cursor;
994 pragma Unreferenced (Position);
995 begin
996 Insert (Container, Before, New_Item, Position, Count);
997 end Insert;
998
999 procedure Insert
1000 (Container : in out List;
1001 Before : Cursor;
1002 Position : out Cursor;
1003 Count : Count_Type := 1)
1004 is
1005 New_Node : Node_Access;
1006
1007 begin
1008 if Before.Container /= null then
1009 if Before.Container /= Container'Unrestricted_Access then
1010 raise Program_Error with
1011 "Before cursor designates wrong list";
1012 end if;
1013
1014 pragma Assert (Vet (Before), "bad cursor in Insert");
1015 end if;
1016
1017 if Count = 0 then
1018 Position := Before;
1019 return;
1020 end if;
1021
1022 if Container.Length > Count_Type'Last - Count then
1023 raise Constraint_Error with "new length exceeds maximum";
1024 end if;
1025
1026 if Container.Busy > 0 then
1027 raise Program_Error with
1028 "attempt to tamper with cursors (list is busy)";
1029 end if;
1030
1031 New_Node := new Node_Type;
1032 Insert_Internal (Container, Before.Node, New_Node);
1033
1034 Position := Cursor'(Container'Unchecked_Access, New_Node);
1035
1036 for J in Count_Type'(2) .. Count loop
1037 New_Node := new Node_Type;
1038 Insert_Internal (Container, Before.Node, New_Node);
1039 end loop;
1040 end Insert;
1041
1042 ---------------------
1043 -- Insert_Internal --
1044 ---------------------
1045
1046 procedure Insert_Internal
1047 (Container : in out List;
1048 Before : Node_Access;
1049 New_Node : Node_Access)
1050 is
1051 begin
1052 if Container.Length = 0 then
1053 pragma Assert (Before = null);
1054 pragma Assert (Container.First = null);
1055 pragma Assert (Container.Last = null);
1056
1057 Container.First := New_Node;
1058 Container.Last := New_Node;
1059
1060 elsif Before = null then
1061 pragma Assert (Container.Last.Next = null);
1062
1063 Container.Last.Next := New_Node;
1064 New_Node.Prev := Container.Last;
1065
1066 Container.Last := New_Node;
1067
1068 elsif Before = Container.First then
1069 pragma Assert (Container.First.Prev = null);
1070
1071 Container.First.Prev := New_Node;
1072 New_Node.Next := Container.First;
1073
1074 Container.First := New_Node;
1075
1076 else
1077 pragma Assert (Container.First.Prev = null);
1078 pragma Assert (Container.Last.Next = null);
1079
1080 New_Node.Next := Before;
1081 New_Node.Prev := Before.Prev;
1082
1083 Before.Prev.Next := New_Node;
1084 Before.Prev := New_Node;
1085 end if;
1086
1087 Container.Length := Container.Length + 1;
1088 end Insert_Internal;
1089
1090 --------------
1091 -- Is_Empty --
1092 --------------
1093
1094 function Is_Empty (Container : List) return Boolean is
1095 begin
1096 return Container.Length = 0;
1097 end Is_Empty;
1098
1099 -------------
1100 -- Iterate --
1101 -------------
1102
1103 procedure Iterate
1104 (Container : List;
1105 Process : not null access procedure (Position : Cursor))
1106 is
1107 B : Natural renames Container'Unrestricted_Access.all.Busy;
1108 Node : Node_Access := Container.First;
1109
1110 begin
1111 B := B + 1;
1112
1113 begin
1114 while Node /= null loop
1115 Process (Cursor'(Container'Unrestricted_Access, Node));
1116 Node := Node.Next;
1117 end loop;
1118 exception
1119 when others =>
1120 B := B - 1;
1121 raise;
1122 end;
1123
1124 B := B - 1;
1125 end Iterate;
1126
1127 function Iterate (Container : List)
1128 return List_Iterator_Interfaces.Reversible_Iterator'Class
1129 is
1130 B : Natural renames Container'Unrestricted_Access.all.Busy;
1131
1132 begin
1133 -- The value of the Node component influences the behavior of the First
1134 -- and Last selector functions of the iterator object. When the Node
1135 -- component is null (as is the case here), this means the iterator
1136 -- object was constructed without a start expression. This is a
1137 -- complete iterator, meaning that the iteration starts from the
1138 -- (logical) beginning of the sequence of items.
1139
1140 -- Note: For a forward iterator, Container.First is the beginning, and
1141 -- for a reverse iterator, Container.Last is the beginning.
1142
1143 return It : constant Iterator :=
1144 Iterator'(Limited_Controlled with
1145 Container => Container'Unrestricted_Access,
1146 Node => null)
1147 do
1148 B := B + 1;
1149 end return;
1150 end Iterate;
1151
1152 function Iterate (Container : List; Start : Cursor)
1153 return List_Iterator_Interfaces.Reversible_Iterator'Class
1154 is
1155 B : Natural renames Container'Unrestricted_Access.all.Busy;
1156
1157 begin
1158 -- It was formerly the case that when Start = No_Element, the partial
1159 -- iterator was defined to behave the same as for a complete iterator,
1160 -- and iterate over the entire sequence of items. However, those
1161 -- semantics were unintuitive and arguably error-prone (it is too easy
1162 -- to accidentally create an endless loop), and so they were changed,
1163 -- per the ARG meeting in Denver on 2011/11. However, there was no
1164 -- consensus about what positive meaning this corner case should have,
1165 -- and so it was decided to simply raise an exception. This does imply,
1166 -- however, that it is not possible to use a partial iterator to specify
1167 -- an empty sequence of items.
1168
1169 if Start = No_Element then
1170 raise Constraint_Error with
1171 "Start position for iterator equals No_Element";
1172 end if;
1173
1174 if Start.Container /= Container'Unrestricted_Access then
1175 raise Program_Error with
1176 "Start cursor of Iterate designates wrong list";
1177 end if;
1178
1179 pragma Assert (Vet (Start), "Start cursor of Iterate is bad");
1180
1181 -- The value of the Node component influences the behavior of the First
1182 -- and Last selector functions of the iterator object. When the Node
1183 -- component is non-null (as is the case here), it means that this
1184 -- is a partial iteration, over a subset of the complete sequence of
1185 -- items. The iterator object was constructed with a start expression,
1186 -- indicating the position from which the iteration begins. Note that
1187 -- the start position has the same value irrespective of whether this
1188 -- is a forward or reverse iteration.
1189
1190 return It : constant Iterator :=
1191 Iterator'(Limited_Controlled with
1192 Container => Container'Unrestricted_Access,
1193 Node => Start.Node)
1194 do
1195 B := B + 1;
1196 end return;
1197 end Iterate;
1198
1199 ----------
1200 -- Last --
1201 ----------
1202
1203 function Last (Container : List) return Cursor is
1204 begin
1205 if Container.Last = null then
1206 return No_Element;
1207 end if;
1208
1209 return Cursor'(Container'Unrestricted_Access, Container.Last);
1210 end Last;
1211
1212 function Last (Object : Iterator) return Cursor is
1213 begin
1214 -- The value of the iterator object's Node component influences the
1215 -- behavior of the Last (and First) selector function.
1216
1217 -- When the Node component is null, this means the iterator object was
1218 -- constructed without a start expression, in which case the (reverse)
1219 -- iteration starts from the (logical) beginning of the entire sequence
1220 -- (corresponding to Container.Last, for a reverse iterator).
1221
1222 -- Otherwise, this is iteration over a partial sequence of items. When
1223 -- the Node component is non-null, the iterator object was constructed
1224 -- with a start expression, that specifies the position from which the
1225 -- (reverse) partial iteration begins.
1226
1227 if Object.Node = null then
1228 return Doubly_Linked_Lists.Last (Object.Container.all);
1229 else
1230 return Cursor'(Object.Container, Object.Node);
1231 end if;
1232 end Last;
1233
1234 ------------------
1235 -- Last_Element --
1236 ------------------
1237
1238 function Last_Element (Container : List) return Element_Type is
1239 begin
1240 if Container.Last = null then
1241 raise Constraint_Error with "list is empty";
1242 end if;
1243
1244 return Container.Last.Element;
1245 end Last_Element;
1246
1247 ------------
1248 -- Length --
1249 ------------
1250
1251 function Length (Container : List) return Count_Type is
1252 begin
1253 return Container.Length;
1254 end Length;
1255
1256 ----------
1257 -- Move --
1258 ----------
1259
1260 procedure Move
1261 (Target : in out List;
1262 Source : in out List)
1263 is
1264 begin
1265 if Target'Address = Source'Address then
1266 return;
1267 end if;
1268
1269 if Source.Busy > 0 then
1270 raise Program_Error with
1271 "attempt to tamper with cursors of Source (list is busy)";
1272 end if;
1273
1274 Clear (Target);
1275
1276 Target.First := Source.First;
1277 Source.First := null;
1278
1279 Target.Last := Source.Last;
1280 Source.Last := null;
1281
1282 Target.Length := Source.Length;
1283 Source.Length := 0;
1284 end Move;
1285
1286 ----------
1287 -- Next --
1288 ----------
1289
1290 procedure Next (Position : in out Cursor) is
1291 begin
1292 Position := Next (Position);
1293 end Next;
1294
1295 function Next (Position : Cursor) return Cursor is
1296 begin
1297 if Position.Node = null then
1298 return No_Element;
1299 end if;
1300
1301 pragma Assert (Vet (Position), "bad cursor in Next");
1302
1303 declare
1304 Next_Node : constant Node_Access := Position.Node.Next;
1305
1306 begin
1307 if Next_Node = null then
1308 return No_Element;
1309 end if;
1310
1311 return Cursor'(Position.Container, Next_Node);
1312 end;
1313 end Next;
1314
1315 function Next
1316 (Object : Iterator;
1317 Position : Cursor) return Cursor
1318 is
1319 begin
1320 if Position.Container = null then
1321 return No_Element;
1322 end if;
1323
1324 if Position.Container /= Object.Container then
1325 raise Program_Error with
1326 "Position cursor of Next designates wrong list";
1327 end if;
1328
1329 return Next (Position);
1330 end Next;
1331
1332 -------------
1333 -- Prepend --
1334 -------------
1335
1336 procedure Prepend
1337 (Container : in out List;
1338 New_Item : Element_Type;
1339 Count : Count_Type := 1)
1340 is
1341 begin
1342 Insert (Container, First (Container), New_Item, Count);
1343 end Prepend;
1344
1345 --------------
1346 -- Previous --
1347 --------------
1348
1349 procedure Previous (Position : in out Cursor) is
1350 begin
1351 Position := Previous (Position);
1352 end Previous;
1353
1354 function Previous (Position : Cursor) return Cursor is
1355 begin
1356 if Position.Node = null then
1357 return No_Element;
1358 end if;
1359
1360 pragma Assert (Vet (Position), "bad cursor in Previous");
1361
1362 declare
1363 Prev_Node : constant Node_Access := Position.Node.Prev;
1364
1365 begin
1366 if Prev_Node = null then
1367 return No_Element;
1368 end if;
1369
1370 return Cursor'(Position.Container, Prev_Node);
1371 end;
1372 end Previous;
1373
1374 function Previous
1375 (Object : Iterator;
1376 Position : Cursor) return Cursor
1377 is
1378 begin
1379 if Position.Container = null then
1380 return No_Element;
1381 end if;
1382
1383 if Position.Container /= Object.Container then
1384 raise Program_Error with
1385 "Position cursor of Previous designates wrong list";
1386 end if;
1387
1388 return Previous (Position);
1389 end Previous;
1390
1391 -------------------
1392 -- Query_Element --
1393 -------------------
1394
1395 procedure Query_Element
1396 (Position : Cursor;
1397 Process : not null access procedure (Element : Element_Type))
1398 is
1399 begin
1400 if Position.Node = null then
1401 raise Constraint_Error with
1402 "Position cursor has no element";
1403 end if;
1404
1405 pragma Assert (Vet (Position), "bad cursor in Query_Element");
1406
1407 declare
1408 C : List renames Position.Container.all'Unrestricted_Access.all;
1409 B : Natural renames C.Busy;
1410 L : Natural renames C.Lock;
1411
1412 begin
1413 B := B + 1;
1414 L := L + 1;
1415
1416 begin
1417 Process (Position.Node.Element);
1418 exception
1419 when others =>
1420 L := L - 1;
1421 B := B - 1;
1422 raise;
1423 end;
1424
1425 L := L - 1;
1426 B := B - 1;
1427 end;
1428 end Query_Element;
1429
1430 ----------
1431 -- Read --
1432 ----------
1433
1434 procedure Read
1435 (Stream : not null access Root_Stream_Type'Class;
1436 Item : out List)
1437 is
1438 N : Count_Type'Base;
1439 X : Node_Access;
1440
1441 begin
1442 Clear (Item);
1443 Count_Type'Base'Read (Stream, N);
1444
1445 if N = 0 then
1446 return;
1447 end if;
1448
1449 X := new Node_Type;
1450
1451 begin
1452 Element_Type'Read (Stream, X.Element);
1453 exception
1454 when others =>
1455 Free (X);
1456 raise;
1457 end;
1458
1459 Item.First := X;
1460 Item.Last := X;
1461
1462 loop
1463 Item.Length := Item.Length + 1;
1464 exit when Item.Length = N;
1465
1466 X := new Node_Type;
1467
1468 begin
1469 Element_Type'Read (Stream, X.Element);
1470 exception
1471 when others =>
1472 Free (X);
1473 raise;
1474 end;
1475
1476 X.Prev := Item.Last;
1477 Item.Last.Next := X;
1478 Item.Last := X;
1479 end loop;
1480 end Read;
1481
1482 procedure Read
1483 (Stream : not null access Root_Stream_Type'Class;
1484 Item : out Cursor)
1485 is
1486 begin
1487 raise Program_Error with "attempt to stream list cursor";
1488 end Read;
1489
1490 procedure Read
1491 (Stream : not null access Root_Stream_Type'Class;
1492 Item : out Reference_Type)
1493 is
1494 begin
1495 raise Program_Error with "attempt to stream reference";
1496 end Read;
1497
1498 procedure Read
1499 (Stream : not null access Root_Stream_Type'Class;
1500 Item : out Constant_Reference_Type)
1501 is
1502 begin
1503 raise Program_Error with "attempt to stream reference";
1504 end Read;
1505
1506 ---------------
1507 -- Reference --
1508 ---------------
1509
1510 function Reference
1511 (Container : aliased in out List;
1512 Position : Cursor) return Reference_Type
1513 is
1514 begin
1515 if Position.Container = null then
1516 raise Constraint_Error with "Position cursor has no element";
1517 end if;
1518
1519 if Position.Container /= Container'Unchecked_Access then
1520 raise Program_Error with
1521 "Position cursor designates wrong container";
1522 end if;
1523
1524 pragma Assert (Vet (Position), "bad cursor in function Reference");
1525
1526 declare
1527 C : List renames Position.Container.all;
1528 B : Natural renames C.Busy;
1529 L : Natural renames C.Lock;
1530 begin
1531 return R : constant Reference_Type :=
1532 (Element => Position.Node.Element'Access,
1533 Control => (Controlled with Position.Container))
1534 do
1535 B := B + 1;
1536 L := L + 1;
1537 end return;
1538 end;
1539 end Reference;
1540
1541 ---------------------
1542 -- Replace_Element --
1543 ---------------------
1544
1545 procedure Replace_Element
1546 (Container : in out List;
1547 Position : Cursor;
1548 New_Item : Element_Type)
1549 is
1550 begin
1551 if Position.Container = null then
1552 raise Constraint_Error with "Position cursor has no element";
1553 end if;
1554
1555 if Position.Container /= Container'Unchecked_Access then
1556 raise Program_Error with
1557 "Position cursor designates wrong container";
1558 end if;
1559
1560 if Container.Lock > 0 then
1561 raise Program_Error with
1562 "attempt to tamper with elements (list is locked)";
1563 end if;
1564
1565 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
1566
1567 Position.Node.Element := New_Item;
1568 end Replace_Element;
1569
1570 ----------------------
1571 -- Reverse_Elements --
1572 ----------------------
1573
1574 procedure Reverse_Elements (Container : in out List) is
1575 I : Node_Access := Container.First;
1576 J : Node_Access := Container.Last;
1577
1578 procedure Swap (L, R : Node_Access);
1579
1580 ----------
1581 -- Swap --
1582 ----------
1583
1584 procedure Swap (L, R : Node_Access) is
1585 LN : constant Node_Access := L.Next;
1586 LP : constant Node_Access := L.Prev;
1587
1588 RN : constant Node_Access := R.Next;
1589 RP : constant Node_Access := R.Prev;
1590
1591 begin
1592 if LP /= null then
1593 LP.Next := R;
1594 end if;
1595
1596 if RN /= null then
1597 RN.Prev := L;
1598 end if;
1599
1600 L.Next := RN;
1601 R.Prev := LP;
1602
1603 if LN = R then
1604 pragma Assert (RP = L);
1605
1606 L.Prev := R;
1607 R.Next := L;
1608
1609 else
1610 L.Prev := RP;
1611 RP.Next := L;
1612
1613 R.Next := LN;
1614 LN.Prev := R;
1615 end if;
1616 end Swap;
1617
1618 -- Start of processing for Reverse_Elements
1619
1620 begin
1621 if Container.Length <= 1 then
1622 return;
1623 end if;
1624
1625 pragma Assert (Container.First.Prev = null);
1626 pragma Assert (Container.Last.Next = null);
1627
1628 if Container.Busy > 0 then
1629 raise Program_Error with
1630 "attempt to tamper with cursors (list is busy)";
1631 end if;
1632
1633 Container.First := J;
1634 Container.Last := I;
1635 loop
1636 Swap (L => I, R => J);
1637
1638 J := J.Next;
1639 exit when I = J;
1640
1641 I := I.Prev;
1642 exit when I = J;
1643
1644 Swap (L => J, R => I);
1645
1646 I := I.Next;
1647 exit when I = J;
1648
1649 J := J.Prev;
1650 exit when I = J;
1651 end loop;
1652
1653 pragma Assert (Container.First.Prev = null);
1654 pragma Assert (Container.Last.Next = null);
1655 end Reverse_Elements;
1656
1657 ------------------
1658 -- Reverse_Find --
1659 ------------------
1660
1661 function Reverse_Find
1662 (Container : List;
1663 Item : Element_Type;
1664 Position : Cursor := No_Element) return Cursor
1665 is
1666 Node : Node_Access := Position.Node;
1667
1668 begin
1669 if Node = null then
1670 Node := Container.Last;
1671
1672 else
1673 if Position.Container /= Container'Unrestricted_Access then
1674 raise Program_Error with
1675 "Position cursor designates wrong container";
1676 end if;
1677
1678 pragma Assert (Vet (Position), "bad cursor in Reverse_Find");
1679 end if;
1680
1681 -- Per AI05-0022, the container implementation is required to detect
1682 -- element tampering by a generic actual subprogram.
1683
1684 declare
1685 B : Natural renames Container'Unrestricted_Access.Busy;
1686 L : Natural renames Container'Unrestricted_Access.Lock;
1687
1688 Result : Node_Access;
1689
1690 begin
1691 B := B + 1;
1692 L := L + 1;
1693
1694 Result := null;
1695 while Node /= null loop
1696 if Node.Element = Item then
1697 Result := Node;
1698 exit;
1699 end if;
1700
1701 Node := Node.Prev;
1702 end loop;
1703
1704 B := B - 1;
1705 L := L - 1;
1706
1707 if Result = null then
1708 return No_Element;
1709 else
1710 return Cursor'(Container'Unrestricted_Access, Result);
1711 end if;
1712 exception
1713 when others =>
1714 B := B - 1;
1715 L := L - 1;
1716 raise;
1717 end;
1718 end Reverse_Find;
1719
1720 ---------------------
1721 -- Reverse_Iterate --
1722 ---------------------
1723
1724 procedure Reverse_Iterate
1725 (Container : List;
1726 Process : not null access procedure (Position : Cursor))
1727 is
1728 C : List renames Container'Unrestricted_Access.all;
1729 B : Natural renames C.Busy;
1730
1731 Node : Node_Access := Container.Last;
1732
1733 begin
1734 B := B + 1;
1735
1736 begin
1737 while Node /= null loop
1738 Process (Cursor'(Container'Unrestricted_Access, Node));
1739 Node := Node.Prev;
1740 end loop;
1741
1742 exception
1743 when others =>
1744 B := B - 1;
1745 raise;
1746 end;
1747
1748 B := B - 1;
1749 end Reverse_Iterate;
1750
1751 ------------
1752 -- Splice --
1753 ------------
1754
1755 procedure Splice
1756 (Target : in out List;
1757 Before : Cursor;
1758 Source : in out List)
1759 is
1760 begin
1761 if Before.Container /= null then
1762 if Before.Container /= Target'Unrestricted_Access then
1763 raise Program_Error with
1764 "Before cursor designates wrong container";
1765 end if;
1766
1767 pragma Assert (Vet (Before), "bad cursor in Splice");
1768 end if;
1769
1770 if Target'Address = Source'Address
1771 or else Source.Length = 0
1772 then
1773 return;
1774 end if;
1775
1776 if Target.Length > Count_Type'Last - Source.Length then
1777 raise Constraint_Error with "new length exceeds maximum";
1778 end if;
1779
1780 if Target.Busy > 0 then
1781 raise Program_Error with
1782 "attempt to tamper with cursors of Target (list is busy)";
1783 end if;
1784
1785 if Source.Busy > 0 then
1786 raise Program_Error with
1787 "attempt to tamper with cursors of Source (list is busy)";
1788 end if;
1789
1790 Splice_Internal (Target, Before.Node, Source);
1791 end Splice;
1792
1793 procedure Splice
1794 (Container : in out List;
1795 Before : Cursor;
1796 Position : Cursor)
1797 is
1798 begin
1799 if Before.Container /= null then
1800 if Before.Container /= Container'Unchecked_Access then
1801 raise Program_Error with
1802 "Before cursor designates wrong container";
1803 end if;
1804
1805 pragma Assert (Vet (Before), "bad Before cursor in Splice");
1806 end if;
1807
1808 if Position.Node = null then
1809 raise Constraint_Error with "Position cursor has no element";
1810 end if;
1811
1812 if Position.Container /= Container'Unrestricted_Access then
1813 raise Program_Error with
1814 "Position cursor designates wrong container";
1815 end if;
1816
1817 pragma Assert (Vet (Position), "bad Position cursor in Splice");
1818
1819 if Position.Node = Before.Node
1820 or else Position.Node.Next = Before.Node
1821 then
1822 return;
1823 end if;
1824
1825 pragma Assert (Container.Length >= 2);
1826
1827 if Container.Busy > 0 then
1828 raise Program_Error with
1829 "attempt to tamper with cursors (list is busy)";
1830 end if;
1831
1832 if Before.Node = null then
1833 pragma Assert (Position.Node /= Container.Last);
1834
1835 if Position.Node = Container.First then
1836 Container.First := Position.Node.Next;
1837 Container.First.Prev := null;
1838 else
1839 Position.Node.Prev.Next := Position.Node.Next;
1840 Position.Node.Next.Prev := Position.Node.Prev;
1841 end if;
1842
1843 Container.Last.Next := Position.Node;
1844 Position.Node.Prev := Container.Last;
1845
1846 Container.Last := Position.Node;
1847 Container.Last.Next := null;
1848
1849 return;
1850 end if;
1851
1852 if Before.Node = Container.First then
1853 pragma Assert (Position.Node /= Container.First);
1854
1855 if Position.Node = Container.Last then
1856 Container.Last := Position.Node.Prev;
1857 Container.Last.Next := null;
1858 else
1859 Position.Node.Prev.Next := Position.Node.Next;
1860 Position.Node.Next.Prev := Position.Node.Prev;
1861 end if;
1862
1863 Container.First.Prev := Position.Node;
1864 Position.Node.Next := Container.First;
1865
1866 Container.First := Position.Node;
1867 Container.First.Prev := null;
1868
1869 return;
1870 end if;
1871
1872 if Position.Node = Container.First then
1873 Container.First := Position.Node.Next;
1874 Container.First.Prev := null;
1875
1876 elsif Position.Node = Container.Last then
1877 Container.Last := Position.Node.Prev;
1878 Container.Last.Next := null;
1879
1880 else
1881 Position.Node.Prev.Next := Position.Node.Next;
1882 Position.Node.Next.Prev := Position.Node.Prev;
1883 end if;
1884
1885 Before.Node.Prev.Next := Position.Node;
1886 Position.Node.Prev := Before.Node.Prev;
1887
1888 Before.Node.Prev := Position.Node;
1889 Position.Node.Next := Before.Node;
1890
1891 pragma Assert (Container.First.Prev = null);
1892 pragma Assert (Container.Last.Next = null);
1893 end Splice;
1894
1895 procedure Splice
1896 (Target : in out List;
1897 Before : Cursor;
1898 Source : in out List;
1899 Position : in out Cursor)
1900 is
1901 begin
1902 if Target'Address = Source'Address then
1903 Splice (Target, Before, Position);
1904 return;
1905 end if;
1906
1907 if Before.Container /= null then
1908 if Before.Container /= Target'Unrestricted_Access then
1909 raise Program_Error with
1910 "Before cursor designates wrong container";
1911 end if;
1912
1913 pragma Assert (Vet (Before), "bad Before cursor in Splice");
1914 end if;
1915
1916 if Position.Node = null then
1917 raise Constraint_Error with "Position cursor has no element";
1918 end if;
1919
1920 if Position.Container /= Source'Unrestricted_Access then
1921 raise Program_Error with
1922 "Position cursor designates wrong container";
1923 end if;
1924
1925 pragma Assert (Vet (Position), "bad Position cursor in Splice");
1926
1927 if Target.Length = Count_Type'Last then
1928 raise Constraint_Error with "Target is full";
1929 end if;
1930
1931 if Target.Busy > 0 then
1932 raise Program_Error with
1933 "attempt to tamper with cursors of Target (list is busy)";
1934 end if;
1935
1936 if Source.Busy > 0 then
1937 raise Program_Error with
1938 "attempt to tamper with cursors of Source (list is busy)";
1939 end if;
1940
1941 Splice_Internal (Target, Before.Node, Source, Position.Node);
1942 Position.Container := Target'Unchecked_Access;
1943 end Splice;
1944
1945 ---------------------
1946 -- Splice_Internal --
1947 ---------------------
1948
1949 procedure Splice_Internal
1950 (Target : in out List;
1951 Before : Node_Access;
1952 Source : in out List)
1953 is
1954 begin
1955 -- This implements the corresponding Splice operation, after the
1956 -- parameters have been vetted, and corner-cases disposed of.
1957
1958 pragma Assert (Target'Address /= Source'Address);
1959 pragma Assert (Source.Length > 0);
1960 pragma Assert (Source.First /= null);
1961 pragma Assert (Source.First.Prev = null);
1962 pragma Assert (Source.Last /= null);
1963 pragma Assert (Source.Last.Next = null);
1964 pragma Assert (Target.Length <= Count_Type'Last - Source.Length);
1965
1966 if Target.Length = 0 then
1967 pragma Assert (Target.First = null);
1968 pragma Assert (Target.Last = null);
1969 pragma Assert (Before = null);
1970
1971 Target.First := Source.First;
1972 Target.Last := Source.Last;
1973
1974 elsif Before = null then
1975 pragma Assert (Target.Last.Next = null);
1976
1977 Target.Last.Next := Source.First;
1978 Source.First.Prev := Target.Last;
1979
1980 Target.Last := Source.Last;
1981
1982 elsif Before = Target.First then
1983 pragma Assert (Target.First.Prev = null);
1984
1985 Source.Last.Next := Target.First;
1986 Target.First.Prev := Source.Last;
1987
1988 Target.First := Source.First;
1989
1990 else
1991 pragma Assert (Target.Length >= 2);
1992
1993 Before.Prev.Next := Source.First;
1994 Source.First.Prev := Before.Prev;
1995
1996 Before.Prev := Source.Last;
1997 Source.Last.Next := Before;
1998 end if;
1999
2000 Source.First := null;
2001 Source.Last := null;
2002
2003 Target.Length := Target.Length + Source.Length;
2004 Source.Length := 0;
2005 end Splice_Internal;
2006
2007 procedure Splice_Internal
2008 (Target : in out List;
2009 Before : Node_Access; -- node of Target
2010 Source : in out List;
2011 Position : Node_Access) -- node of Source
2012 is
2013 begin
2014 -- This implements the corresponding Splice operation, after the
2015 -- parameters have been vetted.
2016
2017 pragma Assert (Target'Address /= Source'Address);
2018 pragma Assert (Target.Length < Count_Type'Last);
2019 pragma Assert (Source.Length > 0);
2020 pragma Assert (Source.First /= null);
2021 pragma Assert (Source.First.Prev = null);
2022 pragma Assert (Source.Last /= null);
2023 pragma Assert (Source.Last.Next = null);
2024 pragma Assert (Position /= null);
2025
2026 if Position = Source.First then
2027 Source.First := Position.Next;
2028
2029 if Position = Source.Last then
2030 pragma Assert (Source.First = null);
2031 pragma Assert (Source.Length = 1);
2032 Source.Last := null;
2033
2034 else
2035 Source.First.Prev := null;
2036 end if;
2037
2038 elsif Position = Source.Last then
2039 pragma Assert (Source.Length >= 2);
2040 Source.Last := Position.Prev;
2041 Source.Last.Next := null;
2042
2043 else
2044 pragma Assert (Source.Length >= 3);
2045 Position.Prev.Next := Position.Next;
2046 Position.Next.Prev := Position.Prev;
2047 end if;
2048
2049 if Target.Length = 0 then
2050 pragma Assert (Target.First = null);
2051 pragma Assert (Target.Last = null);
2052 pragma Assert (Before = null);
2053
2054 Target.First := Position;
2055 Target.Last := Position;
2056
2057 Target.First.Prev := null;
2058 Target.Last.Next := null;
2059
2060 elsif Before = null then
2061 pragma Assert (Target.Last.Next = null);
2062 Target.Last.Next := Position;
2063 Position.Prev := Target.Last;
2064
2065 Target.Last := Position;
2066 Target.Last.Next := null;
2067
2068 elsif Before = Target.First then
2069 pragma Assert (Target.First.Prev = null);
2070 Target.First.Prev := Position;
2071 Position.Next := Target.First;
2072
2073 Target.First := Position;
2074 Target.First.Prev := null;
2075
2076 else
2077 pragma Assert (Target.Length >= 2);
2078 Before.Prev.Next := Position;
2079 Position.Prev := Before.Prev;
2080
2081 Before.Prev := Position;
2082 Position.Next := Before;
2083 end if;
2084
2085 Target.Length := Target.Length + 1;
2086 Source.Length := Source.Length - 1;
2087 end Splice_Internal;
2088
2089 ----------
2090 -- Swap --
2091 ----------
2092
2093 procedure Swap
2094 (Container : in out List;
2095 I, J : Cursor)
2096 is
2097 begin
2098 if I.Node = null then
2099 raise Constraint_Error with "I cursor has no element";
2100 end if;
2101
2102 if J.Node = null then
2103 raise Constraint_Error with "J cursor has no element";
2104 end if;
2105
2106 if I.Container /= Container'Unchecked_Access then
2107 raise Program_Error with "I cursor designates wrong container";
2108 end if;
2109
2110 if J.Container /= Container'Unchecked_Access then
2111 raise Program_Error with "J cursor designates wrong container";
2112 end if;
2113
2114 if I.Node = J.Node then
2115 return;
2116 end if;
2117
2118 if Container.Lock > 0 then
2119 raise Program_Error with
2120 "attempt to tamper with elements (list is locked)";
2121 end if;
2122
2123 pragma Assert (Vet (I), "bad I cursor in Swap");
2124 pragma Assert (Vet (J), "bad J cursor in Swap");
2125
2126 declare
2127 EI : Element_Type renames I.Node.Element;
2128 EJ : Element_Type renames J.Node.Element;
2129
2130 EI_Copy : constant Element_Type := EI;
2131
2132 begin
2133 EI := EJ;
2134 EJ := EI_Copy;
2135 end;
2136 end Swap;
2137
2138 ----------------
2139 -- Swap_Links --
2140 ----------------
2141
2142 procedure Swap_Links
2143 (Container : in out List;
2144 I, J : Cursor)
2145 is
2146 begin
2147 if I.Node = null then
2148 raise Constraint_Error with "I cursor has no element";
2149 end if;
2150
2151 if J.Node = null then
2152 raise Constraint_Error with "J cursor has no element";
2153 end if;
2154
2155 if I.Container /= Container'Unrestricted_Access then
2156 raise Program_Error with "I cursor designates wrong container";
2157 end if;
2158
2159 if J.Container /= Container'Unrestricted_Access then
2160 raise Program_Error with "J cursor designates wrong container";
2161 end if;
2162
2163 if I.Node = J.Node then
2164 return;
2165 end if;
2166
2167 if Container.Busy > 0 then
2168 raise Program_Error with
2169 "attempt to tamper with cursors (list is busy)";
2170 end if;
2171
2172 pragma Assert (Vet (I), "bad I cursor in Swap_Links");
2173 pragma Assert (Vet (J), "bad J cursor in Swap_Links");
2174
2175 declare
2176 I_Next : constant Cursor := Next (I);
2177
2178 begin
2179 if I_Next = J then
2180 Splice (Container, Before => I, Position => J);
2181
2182 else
2183 declare
2184 J_Next : constant Cursor := Next (J);
2185
2186 begin
2187 if J_Next = I then
2188 Splice (Container, Before => J, Position => I);
2189
2190 else
2191 pragma Assert (Container.Length >= 3);
2192
2193 Splice (Container, Before => I_Next, Position => J);
2194 Splice (Container, Before => J_Next, Position => I);
2195 end if;
2196 end;
2197 end if;
2198 end;
2199 end Swap_Links;
2200
2201 --------------------
2202 -- Update_Element --
2203 --------------------
2204
2205 procedure Update_Element
2206 (Container : in out List;
2207 Position : Cursor;
2208 Process : not null access procedure (Element : in out Element_Type))
2209 is
2210 begin
2211 if Position.Node = null then
2212 raise Constraint_Error with "Position cursor has no element";
2213 end if;
2214
2215 if Position.Container /= Container'Unchecked_Access then
2216 raise Program_Error with
2217 "Position cursor designates wrong container";
2218 end if;
2219
2220 pragma Assert (Vet (Position), "bad cursor in Update_Element");
2221
2222 declare
2223 B : Natural renames Container.Busy;
2224 L : Natural renames Container.Lock;
2225
2226 begin
2227 B := B + 1;
2228 L := L + 1;
2229
2230 begin
2231 Process (Position.Node.Element);
2232 exception
2233 when others =>
2234 L := L - 1;
2235 B := B - 1;
2236 raise;
2237 end;
2238
2239 L := L - 1;
2240 B := B - 1;
2241 end;
2242 end Update_Element;
2243
2244 ---------
2245 -- Vet --
2246 ---------
2247
2248 function Vet (Position : Cursor) return Boolean is
2249 begin
2250 if Position.Node = null then
2251 return Position.Container = null;
2252 end if;
2253
2254 if Position.Container = null then
2255 return False;
2256 end if;
2257
2258 -- An invariant of a node is that its Previous and Next components can
2259 -- be null, or designate a different node. Operation Free sets the
2260 -- access value components of the node to designate the node itself
2261 -- before actually deallocating the node, thus deliberately violating
2262 -- the node invariant. This gives us a simple way to detect a dangling
2263 -- reference to a node.
2264
2265 if Position.Node.Next = Position.Node then
2266 return False;
2267 end if;
2268
2269 if Position.Node.Prev = Position.Node then
2270 return False;
2271 end if;
2272
2273 -- In practice the tests above will detect most instances of a dangling
2274 -- reference. If we get here, it means that the invariants of the
2275 -- designated node are satisfied (they at least appear to be satisfied),
2276 -- so we perform some more tests, to determine whether invariants of the
2277 -- designated list are satisfied too.
2278
2279 declare
2280 L : List renames Position.Container.all;
2281
2282 begin
2283 if L.Length = 0 then
2284 return False;
2285 end if;
2286
2287 if L.First = null then
2288 return False;
2289 end if;
2290
2291 if L.Last = null then
2292 return False;
2293 end if;
2294
2295 if L.First.Prev /= null then
2296 return False;
2297 end if;
2298
2299 if L.Last.Next /= null then
2300 return False;
2301 end if;
2302
2303 if Position.Node.Prev = null and then Position.Node /= L.First then
2304 return False;
2305 end if;
2306
2307 pragma Assert
2308 (Position.Node.Prev /= null
2309 or else Position.Node = L.First);
2310
2311 if Position.Node.Next = null and then Position.Node /= L.Last then
2312 return False;
2313 end if;
2314
2315 pragma Assert
2316 (Position.Node.Next /= null
2317 or else Position.Node = L.Last);
2318
2319 if L.Length = 1 then
2320 return L.First = L.Last;
2321 end if;
2322
2323 if L.First = L.Last then
2324 return False;
2325 end if;
2326
2327 if L.First.Next = null then
2328 return False;
2329 end if;
2330
2331 if L.Last.Prev = null then
2332 return False;
2333 end if;
2334
2335 if L.First.Next.Prev /= L.First then
2336 return False;
2337 end if;
2338
2339 if L.Last.Prev.Next /= L.Last then
2340 return False;
2341 end if;
2342
2343 if L.Length = 2 then
2344 if L.First.Next /= L.Last then
2345 return False;
2346 elsif L.Last.Prev /= L.First then
2347 return False;
2348 else
2349 return True;
2350 end if;
2351 end if;
2352
2353 if L.First.Next = L.Last then
2354 return False;
2355 end if;
2356
2357 if L.Last.Prev = L.First then
2358 return False;
2359 end if;
2360
2361 -- Eliminate earlier possibility
2362
2363 if Position.Node = L.First then
2364 return True;
2365 end if;
2366
2367 pragma Assert (Position.Node.Prev /= null);
2368
2369 -- Eliminate earlier possibility
2370
2371 if Position.Node = L.Last then
2372 return True;
2373 end if;
2374
2375 pragma Assert (Position.Node.Next /= null);
2376
2377 if Position.Node.Next.Prev /= Position.Node then
2378 return False;
2379 end if;
2380
2381 if Position.Node.Prev.Next /= Position.Node then
2382 return False;
2383 end if;
2384
2385 if L.Length = 3 then
2386 if L.First.Next /= Position.Node then
2387 return False;
2388 elsif L.Last.Prev /= Position.Node then
2389 return False;
2390 end if;
2391 end if;
2392
2393 return True;
2394 end;
2395 end Vet;
2396
2397 -----------
2398 -- Write --
2399 -----------
2400
2401 procedure Write
2402 (Stream : not null access Root_Stream_Type'Class;
2403 Item : List)
2404 is
2405 Node : Node_Access;
2406
2407 begin
2408 Count_Type'Base'Write (Stream, Item.Length);
2409
2410 Node := Item.First;
2411 while Node /= null loop
2412 Element_Type'Write (Stream, Node.Element);
2413 Node := Node.Next;
2414 end loop;
2415 end Write;
2416
2417 procedure Write
2418 (Stream : not null access Root_Stream_Type'Class;
2419 Item : Cursor)
2420 is
2421 begin
2422 raise Program_Error with "attempt to stream list cursor";
2423 end Write;
2424
2425 procedure Write
2426 (Stream : not null access Root_Stream_Type'Class;
2427 Item : Reference_Type)
2428 is
2429 begin
2430 raise Program_Error with "attempt to stream reference";
2431 end Write;
2432
2433 procedure Write
2434 (Stream : not null access Root_Stream_Type'Class;
2435 Item : Constant_Reference_Type)
2436 is
2437 begin
2438 raise Program_Error with "attempt to stream reference";
2439 end Write;
2440
2441 end Ada.Containers.Doubly_Linked_Lists;