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