1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.INDEFINITE_DOUBLY_LINKED_LISTS --
9 -- Copyright (C) 2004-2012, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- This unit was originally developed by Matthew J Heaney. --
28 ------------------------------------------------------------------------------
30 with Ada
.Unchecked_Deallocation
;
32 with System
; use type System
.Address
;
34 package body Ada
.Containers
.Indefinite_Doubly_Linked_Lists
is
37 new Ada
.Unchecked_Deallocation
(Element_Type
, Element_Access
);
39 type Iterator
is new Limited_Controlled
and
40 List_Iterator_Interfaces
.Reversible_Iterator
with
42 Container
: List_Access
;
46 overriding
procedure Finalize
(Object
: in out Iterator
);
48 overriding
function First
(Object
: Iterator
) return Cursor
;
49 overriding
function Last
(Object
: Iterator
) return Cursor
;
51 overriding
function Next
53 Position
: Cursor
) return Cursor
;
55 overriding
function Previous
57 Position
: Cursor
) return Cursor
;
59 -----------------------
60 -- Local Subprograms --
61 -----------------------
63 procedure Free
(X
: in out Node_Access
);
65 procedure Insert_Internal
66 (Container
: in out List
;
68 New_Node
: Node_Access
);
70 function Vet
(Position
: Cursor
) return Boolean;
71 -- Checks invariants of the cursor and its designated container, as a
72 -- simple way of detecting dangling references (see operation Free for a
73 -- description of the detection mechanism), returning True if all checks
74 -- pass. Invocations of Vet are used here as the argument of pragma Assert,
75 -- so the checks are performed only when assertions are enabled.
81 function "=" (Left
, Right
: List
) return Boolean is
86 if Left
'Address = Right
'Address then
90 if Left
.Length
/= Right
.Length
then
96 for J
in 1 .. Left
.Length
loop
97 if L
.Element
.all /= R
.Element
.all then
112 procedure Adjust
(Container
: in out List
) is
113 Src
: Node_Access
:= Container
.First
;
118 pragma Assert
(Container
.Last
= null);
119 pragma Assert
(Container
.Length
= 0);
120 pragma Assert
(Container
.Busy
= 0);
121 pragma Assert
(Container
.Lock
= 0);
125 pragma Assert
(Container
.First
.Prev
= null);
126 pragma Assert
(Container
.Last
.Next
= null);
127 pragma Assert
(Container
.Length
> 0);
129 Container
.First
:= null;
130 Container
.Last
:= null;
131 Container
.Length
:= 0;
136 Element
: Element_Access
:= new Element_Type
'(Src.Element.all);
138 Dst := new Node_Type'(Element
, null, null);
145 Container
.First
:= Dst
;
146 Container
.Last
:= Dst
;
147 Container
.Length
:= 1;
150 while Src
/= null loop
152 Element
: Element_Access
:= new Element_Type
'(Src.Element.all);
154 Dst := new Node_Type'(Element
, null, Prev
=> Container
.Last
);
161 Container
.Last
.Next
:= Dst
;
162 Container
.Last
:= Dst
;
163 Container
.Length
:= Container
.Length
+ 1;
169 procedure Adjust
(Control
: in out Reference_Control_Type
) is
171 if Control
.Container
/= null then
173 C
: List
renames Control
.Container
.all;
174 B
: Natural renames C
.Busy
;
175 L
: Natural renames C
.Lock
;
188 (Container
: in out List
;
189 New_Item
: Element_Type
;
190 Count
: Count_Type
:= 1)
193 Insert
(Container
, No_Element
, New_Item
, Count
);
200 procedure Assign
(Target
: in out List
; Source
: List
) is
204 if Target
'Address = Source
'Address then
210 Node
:= Source
.First
;
211 while Node
/= null loop
212 Target
.Append
(Node
.Element
.all);
221 procedure Clear
(Container
: in out List
) is
223 pragma Warnings
(Off
, X
);
226 if Container
.Length
= 0 then
227 pragma Assert
(Container
.First
= null);
228 pragma Assert
(Container
.Last
= null);
229 pragma Assert
(Container
.Busy
= 0);
230 pragma Assert
(Container
.Lock
= 0);
234 pragma Assert
(Container
.First
.Prev
= null);
235 pragma Assert
(Container
.Last
.Next
= null);
237 if Container
.Busy
> 0 then
238 raise Program_Error
with
239 "attempt to tamper with cursors (list is busy)";
242 while Container
.Length
> 1 loop
243 X
:= Container
.First
;
244 pragma Assert
(X
.Next
.Prev
= Container
.First
);
246 Container
.First
:= X
.Next
;
247 Container
.First
.Prev
:= null;
249 Container
.Length
:= Container
.Length
- 1;
254 X
:= Container
.First
;
255 pragma Assert
(X
= Container
.Last
);
257 Container
.First
:= null;
258 Container
.Last
:= null;
259 Container
.Length
:= 0;
264 ------------------------
265 -- Constant_Reference --
266 ------------------------
268 function Constant_Reference
269 (Container
: aliased List
;
270 Position
: Cursor
) return Constant_Reference_Type
273 if Position
.Container
= null then
274 raise Constraint_Error
with "Position cursor has no element";
277 if Position
.Container
/= Container
'Unrestricted_Access then
278 raise Program_Error
with
279 "Position cursor designates wrong container";
282 if Position
.Node
.Element
= null then
283 raise Program_Error
with "Node has no element";
286 pragma Assert
(Vet
(Position
), "bad cursor in Constant_Reference");
289 C
: List
renames Position
.Container
.all;
290 B
: Natural renames C
.Busy
;
291 L
: Natural renames C
.Lock
;
293 return R
: constant Constant_Reference_Type
:=
294 (Element
=> Position
.Node
.Element
.all'Access,
295 Control
=> (Controlled
with Position
.Container
))
301 end Constant_Reference
;
309 Item
: Element_Type
) return Boolean
312 return Find
(Container
, Item
) /= No_Element
;
319 function Copy
(Source
: List
) return List
is
321 return Target
: List
do
322 Target
.Assign
(Source
);
331 (Container
: in out List
;
332 Position
: in out Cursor
;
333 Count
: Count_Type
:= 1)
338 if Position
.Node
= null then
339 raise Constraint_Error
with
340 "Position cursor has no element";
343 if Position
.Node
.Element
= null then
344 raise Program_Error
with
345 "Position cursor has no element";
348 if Position
.Container
/= Container
'Unrestricted_Access then
349 raise Program_Error
with
350 "Position cursor designates wrong container";
353 pragma Assert
(Vet
(Position
), "bad cursor in Delete");
355 if Position
.Node
= Container
.First
then
356 Delete_First
(Container
, Count
);
357 Position
:= No_Element
; -- Post-York behavior
362 Position
:= No_Element
; -- Post-York behavior
366 if Container
.Busy
> 0 then
367 raise Program_Error
with
368 "attempt to tamper with cursors (list is busy)";
371 for Index
in 1 .. Count
loop
373 Container
.Length
:= Container
.Length
- 1;
375 if X
= Container
.Last
then
376 Position
:= No_Element
;
378 Container
.Last
:= X
.Prev
;
379 Container
.Last
.Next
:= null;
385 Position
.Node
:= X
.Next
;
387 X
.Next
.Prev
:= X
.Prev
;
388 X
.Prev
.Next
:= X
.Next
;
393 Position
:= No_Element
; -- Post-York behavior
400 procedure Delete_First
401 (Container
: in out List
;
402 Count
: Count_Type
:= 1)
407 if Count
>= Container
.Length
then
416 if Container
.Busy
> 0 then
417 raise Program_Error
with
418 "attempt to tamper with cursors (list is busy)";
421 for I
in 1 .. Count
loop
422 X
:= Container
.First
;
423 pragma Assert
(X
.Next
.Prev
= Container
.First
);
425 Container
.First
:= X
.Next
;
426 Container
.First
.Prev
:= null;
428 Container
.Length
:= Container
.Length
- 1;
438 procedure Delete_Last
439 (Container
: in out List
;
440 Count
: Count_Type
:= 1)
445 if Count
>= Container
.Length
then
454 if Container
.Busy
> 0 then
455 raise Program_Error
with
456 "attempt to tamper with cursors (list is busy)";
459 for I
in 1 .. Count
loop
461 pragma Assert
(X
.Prev
.Next
= Container
.Last
);
463 Container
.Last
:= X
.Prev
;
464 Container
.Last
.Next
:= null;
466 Container
.Length
:= Container
.Length
- 1;
476 function Element
(Position
: Cursor
) return Element_Type
is
478 if Position
.Node
= null then
479 raise Constraint_Error
with
480 "Position cursor has no element";
483 if Position
.Node
.Element
= null then
484 raise Program_Error
with
485 "Position cursor has no element";
488 pragma Assert
(Vet
(Position
), "bad cursor in Element");
490 return Position
.Node
.Element
.all;
497 procedure Finalize
(Object
: in out Iterator
) is
499 if Object
.Container
/= null then
501 B
: Natural renames Object
.Container
.all.Busy
;
508 procedure Finalize
(Control
: in out Reference_Control_Type
) is
510 if Control
.Container
/= null then
512 C
: List
renames Control
.Container
.all;
513 B
: Natural renames C
.Busy
;
514 L
: Natural renames C
.Lock
;
520 Control
.Container
:= null;
531 Position
: Cursor
:= No_Element
) return Cursor
533 Node
: Node_Access
:= Position
.Node
;
537 Node
:= Container
.First
;
540 if Node
.Element
= null then
544 if Position
.Container
/= Container
'Unrestricted_Access then
545 raise Program_Error
with
546 "Position cursor designates wrong container";
549 pragma Assert
(Vet
(Position
), "bad cursor in Find");
552 while Node
/= null loop
553 if Node
.Element
.all = Item
then
554 return Cursor
'(Container'Unrestricted_Access, Node);
567 function First (Container : List) return Cursor is
569 if Container.First = null then
573 return Cursor'(Container
'Unrestricted_Access, Container
.First
);
576 function First
(Object
: Iterator
) return Cursor
is
578 -- The value of the iterator object's Node component influences the
579 -- behavior of the First (and Last) selector function.
581 -- When the Node component is null, this means the iterator object was
582 -- constructed without a start expression, in which case the (forward)
583 -- iteration starts from the (logical) beginning of the entire sequence
584 -- of items (corresponding to Container.First, for a forward iterator).
586 -- Otherwise, this is iteration over a partial sequence of items. When
587 -- the Node component is non-null, the iterator object was constructed
588 -- with a start expression, that specifies the position from which the
589 -- (forward) partial iteration begins.
591 if Object
.Node
= null then
592 return Indefinite_Doubly_Linked_Lists
.First
(Object
.Container
.all);
594 return Cursor
'(Object.Container, Object.Node);
602 function First_Element (Container : List) return Element_Type is
604 if Container.First = null then
605 raise Constraint_Error with "list is empty";
608 return Container.First.Element.all;
615 procedure Free (X : in out Node_Access) is
616 procedure Deallocate is
617 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
620 -- While a node is in use, as an active link in a list, its Previous and
621 -- Next components must be null, or designate a different node; this is
622 -- a node invariant. For this indefinite list, there is an additional
623 -- invariant: that the element access value be non-null. Before actually
624 -- deallocating the node, we set the node access value components of the
625 -- node to point to the node itself, and set the element access value to
626 -- null (by deallocating the node's element), thus falsifying the node
627 -- invariant. Subprogram Vet inspects the value of the node components
628 -- when interrogating the node, in order to detect whether the cursor's
629 -- node access value is dangling.
631 -- Note that we have no guarantee that the storage for the node isn't
632 -- modified when it is deallocated, but there are other tests that Vet
633 -- does if node invariants appear to be satisifed. However, in practice
634 -- this simple test works well enough, detecting dangling references
635 -- immediately, without needing further interrogation.
652 ---------------------
653 -- Generic_Sorting --
654 ---------------------
656 package body Generic_Sorting is
662 function Is_Sorted (Container : List) return Boolean is
663 Node : Node_Access := Container.First;
666 for I in 2 .. Container.Length loop
667 if Node.Next.Element.all < Node.Element.all then
682 (Target : in out List;
683 Source : in out List)
689 -- The semantics of Merge changed slightly per AI05-0021. It was
690 -- originally the case that if Target and Source denoted the same
691 -- container object, then the GNAT implementation of Merge did
692 -- nothing. However, it was argued that RM05 did not precisely
693 -- specify the semantics for this corner case. The decision of the
694 -- ARG was that if Target and Source denote the same non-empty
695 -- container object, then Program_Error is raised.
697 if Source.Is_Empty then
701 if Target'Address = Source'Address then
702 raise Program_Error with
703 "Target and Source denote same non-empty container";
706 if Target.Busy > 0 then
707 raise Program_Error with
708 "attempt to tamper with cursors of Target (list is busy)";
711 if Source.Busy > 0 then
712 raise Program_Error with
713 "attempt to tamper with cursors of Source (list is busy)";
716 LI := First (Target);
717 RI := First (Source);
718 while RI.Node /= null loop
719 pragma Assert (RI.Node.Next = null
720 or else not (RI.Node.Next.Element.all <
721 RI.Node.Element.all));
723 if LI.Node = null then
724 Splice (Target, No_Element, Source);
728 pragma Assert (LI.Node.Next = null
729 or else not (LI.Node.Next.Element.all <
730 LI.Node.Element.all));
732 if RI.Node.Element.all < LI.Node.Element.all then
735 pragma Warnings (Off, RJ);
737 RI.Node := RI.Node.Next;
738 Splice (Target, LI, Source, RJ);
742 LI.Node := LI.Node.Next;
751 procedure Sort (Container : in out List) is
752 procedure Partition (Pivot : Node_Access; Back : Node_Access);
754 procedure Sort (Front, Back : Node_Access);
760 procedure Partition (Pivot : Node_Access; Back : Node_Access) is
761 Node : Node_Access := Pivot.Next;
764 while Node /= Back loop
765 if Node.Element.all < Pivot.Element.all then
767 Prev : constant Node_Access := Node.Prev;
768 Next : constant Node_Access := Node.Next;
773 Container.Last := Prev;
779 Node.Prev := Pivot.Prev;
783 if Node.Prev = null then
784 Container.First := Node;
786 Node.Prev.Next := Node;
802 procedure Sort (Front, Back : Node_Access) is
803 Pivot : constant Node_Access :=
804 (if Front = null then Container.First else Front.Next);
806 if Pivot /= Back then
807 Partition (Pivot, Back);
813 -- Start of processing for Sort
816 if Container.Length <= 1 then
820 pragma Assert (Container.First.Prev = null);
821 pragma Assert (Container.Last.Next = null);
823 if Container.Busy > 0 then
824 raise Program_Error with
825 "attempt to tamper with cursors (list is busy)";
828 Sort (Front => null, Back => null);
830 pragma Assert (Container.First.Prev = null);
831 pragma Assert (Container.Last.Next = null);
840 function Has_Element (Position : Cursor) return Boolean is
842 pragma Assert (Vet (Position), "bad cursor in Has_Element");
843 return Position.Node /= null;
851 (Container : in out List;
853 New_Item : Element_Type;
854 Position : out Cursor;
855 Count : Count_Type := 1)
857 New_Node : Node_Access;
860 if Before.Container /= null then
861 if Before.Container /= Container'Unrestricted_Access then
862 raise Program_Error with
863 "attempt to tamper with cursors (list is busy)";
866 if Before.Node = null
867 or else Before.Node.Element = null
869 raise Program_Error with
870 "Before cursor has no element";
873 pragma Assert (Vet (Before), "bad cursor in Insert");
881 if Container.Length > Count_Type'Last - Count then
882 raise Constraint_Error with "new length exceeds maximum";
885 if Container.Busy > 0 then
886 raise Program_Error with
887 "attempt to tamper with cursors (list is busy)";
891 -- The element allocator may need an accessibility check in the case
892 -- the actual type is class-wide or has access discriminants (see
893 -- RM 4.8(10.1) and AI12-0035). We don't unsuppress the check on the
894 -- allocator in the loop below, because the one in this block would
895 -- have failed already.
897 pragma Unsuppress (Accessibility_Check);
899 Element : Element_Access := new Element_Type'(New_Item
);
902 New_Node
:= new Node_Type
'(Element, null, null);
910 Insert_Internal (Container, Before.Node, New_Node);
911 Position := Cursor'(Container
'Unchecked_Access, New_Node
);
913 for J
in Count_Type
'(2) .. Count loop
916 Element : Element_Access := new Element_Type'(New_Item
);
918 New_Node
:= new Node_Type
'(Element, null, null);
925 Insert_Internal (Container, Before.Node, New_Node);
930 (Container : in out List;
932 New_Item : Element_Type;
933 Count : Count_Type := 1)
936 pragma Unreferenced (Position);
938 Insert (Container, Before, New_Item, Position, Count);
941 ---------------------
942 -- Insert_Internal --
943 ---------------------
945 procedure Insert_Internal
946 (Container : in out List;
947 Before : Node_Access;
948 New_Node : Node_Access)
951 if Container.Length = 0 then
952 pragma Assert (Before = null);
953 pragma Assert (Container.First = null);
954 pragma Assert (Container.Last = null);
956 Container.First := New_Node;
957 Container.Last := New_Node;
959 elsif Before = null then
960 pragma Assert (Container.Last.Next = null);
962 Container.Last.Next := New_Node;
963 New_Node.Prev := Container.Last;
965 Container.Last := New_Node;
967 elsif Before = Container.First then
968 pragma Assert (Container.First.Prev = null);
970 Container.First.Prev := New_Node;
971 New_Node.Next := Container.First;
973 Container.First := New_Node;
976 pragma Assert (Container.First.Prev = null);
977 pragma Assert (Container.Last.Next = null);
979 New_Node.Next := Before;
980 New_Node.Prev := Before.Prev;
982 Before.Prev.Next := New_Node;
983 Before.Prev := New_Node;
986 Container.Length := Container.Length + 1;
993 function Is_Empty (Container : List) return Boolean is
995 return Container.Length = 0;
1004 Process : not null access procedure (Position : Cursor))
1006 B : Natural renames Container'Unrestricted_Access.all.Busy;
1007 Node : Node_Access := Container.First;
1013 while Node /= null loop
1014 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1028 return List_Iterator_Interfaces
.Reversible_Iterator
'class
1030 B
: Natural renames Container
'Unrestricted_Access.all.Busy
;
1033 -- The value of the Node component influences the behavior of the First
1034 -- and Last selector functions of the iterator object. When the Node
1035 -- component is null (as is the case here), this means the iterator
1036 -- object was constructed without a start expression. This is a
1037 -- complete iterator, meaning that the iteration starts from the
1038 -- (logical) beginning of the sequence of items.
1040 -- Note: For a forward iterator, Container.First is the beginning, and
1041 -- for a reverse iterator, Container.Last is the beginning.
1043 return It
: constant Iterator
:=
1044 Iterator
'(Limited_Controlled with
1045 Container => Container'Unrestricted_Access,
1055 return List_Iterator_Interfaces.Reversible_Iterator'Class
1057 B : Natural renames Container'Unrestricted_Access.all.Busy;
1060 -- It was formerly the case that when Start = No_Element, the partial
1061 -- iterator was defined to behave the same as for a complete iterator,
1062 -- and iterate over the entire sequence of items. However, those
1063 -- semantics were unintuitive and arguably error-prone (it is too easy
1064 -- to accidentally create an endless loop), and so they were changed,
1065 -- per the ARG meeting in Denver on 2011/11. However, there was no
1066 -- consensus about what positive meaning this corner case should have,
1067 -- and so it was decided to simply raise an exception. This does imply,
1068 -- however, that it is not possible to use a partial iterator to specify
1069 -- an empty sequence of items.
1071 if Start = No_Element then
1072 raise Constraint_Error with
1073 "Start position for iterator equals No_Element";
1076 if Start.Container /= Container'Unrestricted_Access then
1077 raise Program_Error with
1078 "Start cursor of Iterate designates wrong list";
1081 pragma Assert (Vet (Start), "Start cursor of Iterate is bad");
1083 -- The value of the Node component influences the behavior of the First
1084 -- and Last selector functions of the iterator object. When the Node
1085 -- component is non-null (as is the case here), it means that this
1086 -- is a partial iteration, over a subset of the complete sequence of
1087 -- items. The iterator object was constructed with a start expression,
1088 -- indicating the position from which the iteration begins. Note that
1089 -- the start position has the same value irrespective of whether this
1090 -- is a forward or reverse iteration.
1092 return It : constant Iterator :=
1093 Iterator'(Limited_Controlled
with
1094 Container
=> Container
'Unrestricted_Access,
1105 function Last
(Container
: List
) return Cursor
is
1107 if Container
.Last
= null then
1111 return Cursor
'(Container'Unrestricted_Access, Container.Last);
1114 function Last (Object : Iterator) return Cursor is
1116 -- The value of the iterator object's Node component influences the
1117 -- behavior of the Last (and First) selector function.
1119 -- When the Node component is null, this means the iterator object was
1120 -- constructed without a start expression, in which case the (reverse)
1121 -- iteration starts from the (logical) beginning of the entire sequence
1122 -- (corresponding to Container.Last, for a reverse iterator).
1124 -- Otherwise, this is iteration over a partial sequence of items. When
1125 -- the Node component is non-null, the iterator object was constructed
1126 -- with a start expression, that specifies the position from which the
1127 -- (reverse) partial iteration begins.
1129 if Object.Node = null then
1130 return Indefinite_Doubly_Linked_Lists.Last (Object.Container.all);
1132 return Cursor'(Object
.Container
, Object
.Node
);
1140 function Last_Element
(Container
: List
) return Element_Type
is
1142 if Container
.Last
= null then
1143 raise Constraint_Error
with "list is empty";
1146 return Container
.Last
.Element
.all;
1153 function Length
(Container
: List
) return Count_Type
is
1155 return Container
.Length
;
1162 procedure Move
(Target
: in out List
; Source
: in out List
) is
1164 if Target
'Address = Source
'Address then
1168 if Source
.Busy
> 0 then
1169 raise Program_Error
with
1170 "attempt to tamper with cursors of Source (list is busy)";
1175 Target
.First
:= Source
.First
;
1176 Source
.First
:= null;
1178 Target
.Last
:= Source
.Last
;
1179 Source
.Last
:= null;
1181 Target
.Length
:= Source
.Length
;
1189 procedure Next
(Position
: in out Cursor
) is
1191 Position
:= Next
(Position
);
1194 function Next
(Position
: Cursor
) return Cursor
is
1196 if Position
.Node
= null then
1200 pragma Assert
(Vet
(Position
), "bad cursor in Next");
1203 Next_Node
: constant Node_Access
:= Position
.Node
.Next
;
1205 if Next_Node
= null then
1209 return Cursor
'(Position.Container, Next_Node);
1213 function Next (Object : Iterator; Position : Cursor) return Cursor is
1215 if Position.Container = null then
1219 if Position.Container /= Object.Container then
1220 raise Program_Error with
1221 "Position cursor of Next designates wrong list";
1224 return Next (Position);
1232 (Container : in out List;
1233 New_Item : Element_Type;
1234 Count : Count_Type := 1)
1237 Insert (Container, First (Container), New_Item, Count);
1244 procedure Previous (Position : in out Cursor) is
1246 Position := Previous (Position);
1249 function Previous (Position : Cursor) return Cursor is
1251 if Position.Node = null then
1255 pragma Assert (Vet (Position), "bad cursor in Previous");
1258 Prev_Node : constant Node_Access := Position.Node.Prev;
1260 if Prev_Node = null then
1264 return Cursor'(Position
.Container
, Prev_Node
);
1268 function Previous
(Object
: Iterator
; Position
: Cursor
) return Cursor
is
1270 if Position
.Container
= null then
1274 if Position
.Container
/= Object
.Container
then
1275 raise Program_Error
with
1276 "Position cursor of Previous designates wrong list";
1279 return Previous
(Position
);
1286 procedure Query_Element
1288 Process
: not null access procedure (Element
: Element_Type
))
1291 if Position
.Node
= null then
1292 raise Constraint_Error
with
1293 "Position cursor has no element";
1296 if Position
.Node
.Element
= null then
1297 raise Program_Error
with
1298 "Position cursor has no element";
1301 pragma Assert
(Vet
(Position
), "bad cursor in Query_Element");
1304 C
: List
renames Position
.Container
.all'Unrestricted_Access.all;
1305 B
: Natural renames C
.Busy
;
1306 L
: Natural renames C
.Lock
;
1313 Process
(Position
.Node
.Element
.all);
1331 (Stream
: not null access Root_Stream_Type
'Class;
1334 N
: Count_Type
'Base;
1340 Count_Type
'Base'Read (Stream, N);
1347 Element : Element_Access :=
1348 new Element_Type'(Element_Type
'Input (Stream
));
1350 Dst
:= new Node_Type
'(Element, null, null);
1361 while Item.Length < N loop
1363 Element : Element_Access :=
1364 new Element_Type'(Element_Type
'Input (Stream
));
1366 Dst
:= new Node_Type
'(Element, Next => null, Prev => Item.Last);
1373 Item.Last.Next := Dst;
1375 Item.Length := Item.Length + 1;
1380 (Stream : not null access Root_Stream_Type'Class;
1384 raise Program_Error with "attempt to stream list cursor";
1388 (Stream : not null access Root_Stream_Type'Class;
1389 Item : out Reference_Type)
1392 raise Program_Error with "attempt to stream reference";
1396 (Stream : not null access Root_Stream_Type'Class;
1397 Item : out Constant_Reference_Type)
1400 raise Program_Error with "attempt to stream reference";
1408 (Container : aliased in out List;
1409 Position : Cursor) return Reference_Type
1412 if Position.Container = null then
1413 raise Constraint_Error with "Position cursor has no element";
1416 if Position.Container /= Container'Unrestricted_Access then
1417 raise Program_Error with
1418 "Position cursor designates wrong container";
1421 if Position.Node.Element = null then
1422 raise Program_Error with "Node has no element";
1425 pragma Assert (Vet (Position), "bad cursor in function Reference");
1428 C : List renames Position.Container.all;
1429 B : Natural renames C.Busy;
1430 L : Natural renames C.Lock;
1432 return R : constant Reference_Type :=
1433 (Element => Position.Node.Element.all'Access,
1434 Control => (Controlled with Position.Container))
1442 ---------------------
1443 -- Replace_Element --
1444 ---------------------
1446 procedure Replace_Element
1447 (Container : in out List;
1449 New_Item : Element_Type)
1452 if Position.Container = null then
1453 raise Constraint_Error with "Position cursor has no element";
1456 if Position.Container /= Container'Unchecked_Access then
1457 raise Program_Error with
1458 "Position cursor designates wrong container";
1461 if Container.Lock > 0 then
1462 raise Program_Error with
1463 "attempt to tamper with elements (list is locked)";
1466 if Position.Node.Element = null then
1467 raise Program_Error with
1468 "Position cursor has no element";
1471 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
1474 -- The element allocator may need an accessibility check in the case
1475 -- the actual type is class-wide or has access discriminants (see
1476 -- RM 4.8(10.1) and AI12-0035).
1478 pragma Unsuppress (Accessibility_Check);
1480 X : Element_Access := Position.Node.Element;
1483 Position.Node.Element := new Element_Type'(New_Item
);
1486 end Replace_Element
;
1488 ----------------------
1489 -- Reverse_Elements --
1490 ----------------------
1492 procedure Reverse_Elements
(Container
: in out List
) is
1493 I
: Node_Access
:= Container
.First
;
1494 J
: Node_Access
:= Container
.Last
;
1496 procedure Swap
(L
, R
: Node_Access
);
1502 procedure Swap
(L
, R
: Node_Access
) is
1503 LN
: constant Node_Access
:= L
.Next
;
1504 LP
: constant Node_Access
:= L
.Prev
;
1506 RN
: constant Node_Access
:= R
.Next
;
1507 RP
: constant Node_Access
:= R
.Prev
;
1522 pragma Assert
(RP
= L
);
1536 -- Start of processing for Reverse_Elements
1539 if Container
.Length
<= 1 then
1543 pragma Assert
(Container
.First
.Prev
= null);
1544 pragma Assert
(Container
.Last
.Next
= null);
1546 if Container
.Busy
> 0 then
1547 raise Program_Error
with
1548 "attempt to tamper with cursors (list is busy)";
1551 Container
.First
:= J
;
1552 Container
.Last
:= I
;
1554 Swap
(L
=> I
, R
=> J
);
1562 Swap
(L
=> J
, R
=> I
);
1571 pragma Assert
(Container
.First
.Prev
= null);
1572 pragma Assert
(Container
.Last
.Next
= null);
1573 end Reverse_Elements
;
1579 function Reverse_Find
1581 Item
: Element_Type
;
1582 Position
: Cursor
:= No_Element
) return Cursor
1584 Node
: Node_Access
:= Position
.Node
;
1588 Node
:= Container
.Last
;
1591 if Node
.Element
= null then
1592 raise Program_Error
with "Position cursor has no element";
1595 if Position
.Container
/= Container
'Unrestricted_Access then
1596 raise Program_Error
with
1597 "Position cursor designates wrong container";
1600 pragma Assert
(Vet
(Position
), "bad cursor in Reverse_Find");
1603 while Node
/= null loop
1604 if Node
.Element
.all = Item
then
1605 return Cursor
'(Container'Unrestricted_Access, Node);
1614 ---------------------
1615 -- Reverse_Iterate --
1616 ---------------------
1618 procedure Reverse_Iterate
1620 Process : not null access procedure (Position : Cursor))
1622 C : List renames Container'Unrestricted_Access.all;
1623 B : Natural renames C.Busy;
1625 Node : Node_Access := Container.Last;
1631 while Node /= null loop
1632 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1642 end Reverse_Iterate
;
1649 (Target
: in out List
;
1651 Source
: in out List
)
1654 if Before
.Container
/= null then
1655 if Before
.Container
/= Target
'Unrestricted_Access then
1656 raise Program_Error
with
1657 "Before cursor designates wrong container";
1660 if Before
.Node
= null
1661 or else Before
.Node
.Element
= null
1663 raise Program_Error
with
1664 "Before cursor has no element";
1667 pragma Assert
(Vet
(Before
), "bad cursor in Splice");
1670 if Target
'Address = Source
'Address
1671 or else Source
.Length
= 0
1676 pragma Assert
(Source
.First
.Prev
= null);
1677 pragma Assert
(Source
.Last
.Next
= null);
1679 if Target
.Length
> Count_Type
'Last - Source
.Length
then
1680 raise Constraint_Error
with "new length exceeds maximum";
1683 if Target
.Busy
> 0 then
1684 raise Program_Error
with
1685 "attempt to tamper with cursors of Target (list is busy)";
1688 if Source
.Busy
> 0 then
1689 raise Program_Error
with
1690 "attempt to tamper with cursors of Source (list is busy)";
1693 if Target
.Length
= 0 then
1694 pragma Assert
(Before
= No_Element
);
1695 pragma Assert
(Target
.First
= null);
1696 pragma Assert
(Target
.Last
= null);
1698 Target
.First
:= Source
.First
;
1699 Target
.Last
:= Source
.Last
;
1701 elsif Before
.Node
= null then
1702 pragma Assert
(Target
.Last
.Next
= null);
1704 Target
.Last
.Next
:= Source
.First
;
1705 Source
.First
.Prev
:= Target
.Last
;
1707 Target
.Last
:= Source
.Last
;
1709 elsif Before
.Node
= Target
.First
then
1710 pragma Assert
(Target
.First
.Prev
= null);
1712 Source
.Last
.Next
:= Target
.First
;
1713 Target
.First
.Prev
:= Source
.Last
;
1715 Target
.First
:= Source
.First
;
1718 pragma Assert
(Target
.Length
>= 2);
1719 Before
.Node
.Prev
.Next
:= Source
.First
;
1720 Source
.First
.Prev
:= Before
.Node
.Prev
;
1722 Before
.Node
.Prev
:= Source
.Last
;
1723 Source
.Last
.Next
:= Before
.Node
;
1726 Source
.First
:= null;
1727 Source
.Last
:= null;
1729 Target
.Length
:= Target
.Length
+ Source
.Length
;
1734 (Container
: in out List
;
1739 if Before
.Container
/= null then
1740 if Before
.Container
/= Container
'Unchecked_Access then
1741 raise Program_Error
with
1742 "Before cursor designates wrong container";
1745 if Before
.Node
= null
1746 or else Before
.Node
.Element
= null
1748 raise Program_Error
with
1749 "Before cursor has no element";
1752 pragma Assert
(Vet
(Before
), "bad Before cursor in Splice");
1755 if Position
.Node
= null then
1756 raise Constraint_Error
with "Position cursor has no element";
1759 if Position
.Node
.Element
= null then
1760 raise Program_Error
with "Position cursor has no element";
1763 if Position
.Container
/= Container
'Unrestricted_Access then
1764 raise Program_Error
with
1765 "Position cursor designates wrong container";
1768 pragma Assert
(Vet
(Position
), "bad Position cursor in Splice");
1770 if Position
.Node
= Before
.Node
1771 or else Position
.Node
.Next
= Before
.Node
1776 pragma Assert
(Container
.Length
>= 2);
1778 if Container
.Busy
> 0 then
1779 raise Program_Error
with
1780 "attempt to tamper with cursors (list is busy)";
1783 if Before
.Node
= null then
1784 pragma Assert
(Position
.Node
/= Container
.Last
);
1786 if Position
.Node
= Container
.First
then
1787 Container
.First
:= Position
.Node
.Next
;
1788 Container
.First
.Prev
:= null;
1790 Position
.Node
.Prev
.Next
:= Position
.Node
.Next
;
1791 Position
.Node
.Next
.Prev
:= Position
.Node
.Prev
;
1794 Container
.Last
.Next
:= Position
.Node
;
1795 Position
.Node
.Prev
:= Container
.Last
;
1797 Container
.Last
:= Position
.Node
;
1798 Container
.Last
.Next
:= null;
1803 if Before
.Node
= Container
.First
then
1804 pragma Assert
(Position
.Node
/= Container
.First
);
1806 if Position
.Node
= Container
.Last
then
1807 Container
.Last
:= Position
.Node
.Prev
;
1808 Container
.Last
.Next
:= null;
1810 Position
.Node
.Prev
.Next
:= Position
.Node
.Next
;
1811 Position
.Node
.Next
.Prev
:= Position
.Node
.Prev
;
1814 Container
.First
.Prev
:= Position
.Node
;
1815 Position
.Node
.Next
:= Container
.First
;
1817 Container
.First
:= Position
.Node
;
1818 Container
.First
.Prev
:= null;
1823 if Position
.Node
= Container
.First
then
1824 Container
.First
:= Position
.Node
.Next
;
1825 Container
.First
.Prev
:= null;
1827 elsif Position
.Node
= Container
.Last
then
1828 Container
.Last
:= Position
.Node
.Prev
;
1829 Container
.Last
.Next
:= null;
1832 Position
.Node
.Prev
.Next
:= Position
.Node
.Next
;
1833 Position
.Node
.Next
.Prev
:= Position
.Node
.Prev
;
1836 Before
.Node
.Prev
.Next
:= Position
.Node
;
1837 Position
.Node
.Prev
:= Before
.Node
.Prev
;
1839 Before
.Node
.Prev
:= Position
.Node
;
1840 Position
.Node
.Next
:= Before
.Node
;
1842 pragma Assert
(Container
.First
.Prev
= null);
1843 pragma Assert
(Container
.Last
.Next
= null);
1847 (Target
: in out List
;
1849 Source
: in out List
;
1850 Position
: in out Cursor
)
1853 if Target
'Address = Source
'Address then
1854 Splice
(Target
, Before
, Position
);
1858 if Before
.Container
/= null then
1859 if Before
.Container
/= Target
'Unrestricted_Access then
1860 raise Program_Error
with
1861 "Before cursor designates wrong container";
1864 if Before
.Node
= null
1865 or else Before
.Node
.Element
= null
1867 raise Program_Error
with
1868 "Before cursor has no element";
1871 pragma Assert
(Vet
(Before
), "bad Before cursor in Splice");
1874 if Position
.Node
= null then
1875 raise Constraint_Error
with "Position cursor has no element";
1878 if Position
.Node
.Element
= null then
1879 raise Program_Error
with
1880 "Position cursor has no element";
1883 if Position
.Container
/= Source
'Unrestricted_Access then
1884 raise Program_Error
with
1885 "Position cursor designates wrong container";
1888 pragma Assert
(Vet
(Position
), "bad Position cursor in Splice");
1890 if Target
.Length
= Count_Type
'Last then
1891 raise Constraint_Error
with "Target is full";
1894 if Target
.Busy
> 0 then
1895 raise Program_Error
with
1896 "attempt to tamper with cursors of Target (list is busy)";
1899 if Source
.Busy
> 0 then
1900 raise Program_Error
with
1901 "attempt to tamper with cursors of Source (list is busy)";
1904 if Position
.Node
= Source
.First
then
1905 Source
.First
:= Position
.Node
.Next
;
1907 if Position
.Node
= Source
.Last
then
1908 pragma Assert
(Source
.First
= null);
1909 pragma Assert
(Source
.Length
= 1);
1910 Source
.Last
:= null;
1913 Source
.First
.Prev
:= null;
1916 elsif Position
.Node
= Source
.Last
then
1917 pragma Assert
(Source
.Length
>= 2);
1918 Source
.Last
:= Position
.Node
.Prev
;
1919 Source
.Last
.Next
:= null;
1922 pragma Assert
(Source
.Length
>= 3);
1923 Position
.Node
.Prev
.Next
:= Position
.Node
.Next
;
1924 Position
.Node
.Next
.Prev
:= Position
.Node
.Prev
;
1927 if Target
.Length
= 0 then
1928 pragma Assert
(Before
= No_Element
);
1929 pragma Assert
(Target
.First
= null);
1930 pragma Assert
(Target
.Last
= null);
1932 Target
.First
:= Position
.Node
;
1933 Target
.Last
:= Position
.Node
;
1935 Target
.First
.Prev
:= null;
1936 Target
.Last
.Next
:= null;
1938 elsif Before
.Node
= null then
1939 pragma Assert
(Target
.Last
.Next
= null);
1940 Target
.Last
.Next
:= Position
.Node
;
1941 Position
.Node
.Prev
:= Target
.Last
;
1943 Target
.Last
:= Position
.Node
;
1944 Target
.Last
.Next
:= null;
1946 elsif Before
.Node
= Target
.First
then
1947 pragma Assert
(Target
.First
.Prev
= null);
1948 Target
.First
.Prev
:= Position
.Node
;
1949 Position
.Node
.Next
:= Target
.First
;
1951 Target
.First
:= Position
.Node
;
1952 Target
.First
.Prev
:= null;
1955 pragma Assert
(Target
.Length
>= 2);
1956 Before
.Node
.Prev
.Next
:= Position
.Node
;
1957 Position
.Node
.Prev
:= Before
.Node
.Prev
;
1959 Before
.Node
.Prev
:= Position
.Node
;
1960 Position
.Node
.Next
:= Before
.Node
;
1963 Target
.Length
:= Target
.Length
+ 1;
1964 Source
.Length
:= Source
.Length
- 1;
1966 Position
.Container
:= Target
'Unchecked_Access;
1974 (Container
: in out List
;
1978 if I
.Node
= null then
1979 raise Constraint_Error
with "I cursor has no element";
1982 if J
.Node
= null then
1983 raise Constraint_Error
with "J cursor has no element";
1986 if I
.Container
/= Container
'Unchecked_Access then
1987 raise Program_Error
with "I cursor designates wrong container";
1990 if J
.Container
/= Container
'Unchecked_Access then
1991 raise Program_Error
with "J cursor designates wrong container";
1994 if I
.Node
= J
.Node
then
1998 if Container
.Lock
> 0 then
1999 raise Program_Error
with
2000 "attempt to tamper with elements (list is locked)";
2003 pragma Assert
(Vet
(I
), "bad I cursor in Swap");
2004 pragma Assert
(Vet
(J
), "bad J cursor in Swap");
2007 EI_Copy
: constant Element_Access
:= I
.Node
.Element
;
2010 I
.Node
.Element
:= J
.Node
.Element
;
2011 J
.Node
.Element
:= EI_Copy
;
2019 procedure Swap_Links
2020 (Container
: in out List
;
2024 if I
.Node
= null then
2025 raise Constraint_Error
with "I cursor has no element";
2028 if J
.Node
= null then
2029 raise Constraint_Error
with "J cursor has no element";
2032 if I
.Container
/= Container
'Unrestricted_Access then
2033 raise Program_Error
with "I cursor designates wrong container";
2036 if J
.Container
/= Container
'Unrestricted_Access then
2037 raise Program_Error
with "J cursor designates wrong container";
2040 if I
.Node
= J
.Node
then
2044 if Container
.Busy
> 0 then
2045 raise Program_Error
with
2046 "attempt to tamper with cursors (list is busy)";
2049 pragma Assert
(Vet
(I
), "bad I cursor in Swap_Links");
2050 pragma Assert
(Vet
(J
), "bad J cursor in Swap_Links");
2053 I_Next
: constant Cursor
:= Next
(I
);
2057 Splice
(Container
, Before
=> I
, Position
=> J
);
2061 J_Next
: constant Cursor
:= Next
(J
);
2065 Splice
(Container
, Before
=> J
, Position
=> I
);
2068 pragma Assert
(Container
.Length
>= 3);
2070 Splice
(Container
, Before
=> I_Next
, Position
=> J
);
2071 Splice
(Container
, Before
=> J_Next
, Position
=> I
);
2077 pragma Assert
(Container
.First
.Prev
= null);
2078 pragma Assert
(Container
.Last
.Next
= null);
2081 --------------------
2082 -- Update_Element --
2083 --------------------
2085 procedure Update_Element
2086 (Container
: in out List
;
2088 Process
: not null access procedure (Element
: in out Element_Type
))
2091 if Position
.Node
= null then
2092 raise Constraint_Error
with "Position cursor has no element";
2095 if Position
.Node
.Element
= null then
2096 raise Program_Error
with
2097 "Position cursor has no element";
2100 if Position
.Container
/= Container
'Unchecked_Access then
2101 raise Program_Error
with
2102 "Position cursor designates wrong container";
2105 pragma Assert
(Vet
(Position
), "bad cursor in Update_Element");
2108 B
: Natural renames Container
.Busy
;
2109 L
: Natural renames Container
.Lock
;
2116 Process
(Position
.Node
.Element
.all);
2133 function Vet
(Position
: Cursor
) return Boolean is
2135 if Position
.Node
= null then
2136 return Position
.Container
= null;
2139 if Position
.Container
= null then
2143 -- An invariant of a node is that its Previous and Next components can
2144 -- be null, or designate a different node. Also, its element access
2145 -- value must be non-null. Operation Free sets the node access value
2146 -- components of the node to designate the node itself, and the element
2147 -- access value to null, before actually deallocating the node, thus
2148 -- deliberately violating the node invariant. This gives us a simple way
2149 -- to detect a dangling reference to a node.
2151 if Position
.Node
.Next
= Position
.Node
then
2155 if Position
.Node
.Prev
= Position
.Node
then
2159 if Position
.Node
.Element
= null then
2163 -- In practice the tests above will detect most instances of a dangling
2164 -- reference. If we get here, it means that the invariants of the
2165 -- designated node are satisfied (they at least appear to be satisfied),
2166 -- so we perform some more tests, to determine whether invariants of the
2167 -- designated list are satisfied too.
2170 L
: List
renames Position
.Container
.all;
2173 if L
.Length
= 0 then
2177 if L
.First
= null then
2181 if L
.Last
= null then
2185 if L
.First
.Prev
/= null then
2189 if L
.Last
.Next
/= null then
2193 if Position
.Node
.Prev
= null and then Position
.Node
/= L
.First
then
2197 if Position
.Node
.Next
= null and then Position
.Node
/= L
.Last
then
2201 if L
.Length
= 1 then
2202 return L
.First
= L
.Last
;
2205 if L
.First
= L
.Last
then
2209 if L
.First
.Next
= null then
2213 if L
.Last
.Prev
= null then
2217 if L
.First
.Next
.Prev
/= L
.First
then
2221 if L
.Last
.Prev
.Next
/= L
.Last
then
2225 if L
.Length
= 2 then
2226 if L
.First
.Next
/= L
.Last
then
2230 if L
.Last
.Prev
/= L
.First
then
2237 if L
.First
.Next
= L
.Last
then
2241 if L
.Last
.Prev
= L
.First
then
2245 if Position
.Node
= L
.First
then
2249 if Position
.Node
= L
.Last
then
2253 if Position
.Node
.Next
= null then
2257 if Position
.Node
.Prev
= null then
2261 if Position
.Node
.Next
.Prev
/= Position
.Node
then
2265 if Position
.Node
.Prev
.Next
/= Position
.Node
then
2269 if L
.Length
= 3 then
2270 if L
.First
.Next
/= Position
.Node
then
2274 if L
.Last
.Prev
/= Position
.Node
then
2288 (Stream
: not null access Root_Stream_Type
'Class;
2291 Node
: Node_Access
:= Item
.First
;
2294 Count_Type
'Base'Write (Stream, Item.Length);
2296 while Node /= null loop
2297 Element_Type'Output (Stream, Node.Element.all);
2303 (Stream : not null access Root_Stream_Type'Class;
2307 raise Program_Error with "attempt to stream list cursor";
2311 (Stream : not null access Root_Stream_Type'Class;
2312 Item : Reference_Type)
2315 raise Program_Error with "attempt to stream reference";
2319 (Stream : not null access Root_Stream_Type'Class;
2320 Item : Constant_Reference_Type)
2323 raise Program_Error with "attempt to stream reference";
2326 end Ada.Containers.Indefinite_Doubly_Linked_Lists;