OSDN Git Service

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