1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . D O U B L Y _ L I N K E D _ L I S T S --
9 -- Copyright (C) 2004-2017, 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
.Doubly_Linked_Lists
is
36 pragma Warnings
(Off
, "variable ""Busy*"" is not referenced");
37 pragma Warnings
(Off
, "variable ""Lock*"" is not referenced");
38 -- See comment in Ada.Containers.Helpers
40 -----------------------
41 -- Local Subprograms --
42 -----------------------
44 procedure Free
(X
: in out Node_Access
);
46 procedure Insert_Internal
47 (Container
: in out List
;
49 New_Node
: Node_Access
);
51 procedure Splice_Internal
52 (Target
: in out List
;
54 Source
: in out List
);
56 procedure Splice_Internal
57 (Target
: in out List
;
60 Position
: Node_Access
);
62 function Vet
(Position
: Cursor
) return Boolean;
63 -- Checks invariants of the cursor and its designated container, as a
64 -- simple way of detecting dangling references (see operation Free for a
65 -- description of the detection mechanism), returning True if all checks
66 -- pass. Invocations of Vet are used here as the argument of pragma Assert,
67 -- so the checks are performed only when assertions are enabled.
73 function "=" (Left
, Right
: List
) return Boolean is
75 if Left
.Length
/= Right
.Length
then
79 if Left
.Length
= 0 then
84 -- Per AI05-0022, the container implementation is required to detect
85 -- element tampering by a generic actual subprogram.
87 Lock_Left
: With_Lock
(Left
.TC
'Unrestricted_Access);
88 Lock_Right
: With_Lock
(Right
.TC
'Unrestricted_Access);
90 L
: Node_Access
:= Left
.First
;
91 R
: Node_Access
:= Right
.First
;
93 for J
in 1 .. Left
.Length
loop
94 if L
.Element
/= R
.Element
then
110 procedure Adjust
(Container
: in out List
) is
111 Src
: Node_Access
:= Container
.First
;
114 -- If the counts are nonzero, execution is technically erroneous, but
115 -- it seems friendly to allow things like concurrent "=" on shared
118 Zero_Counts
(Container
.TC
);
121 pragma Assert
(Container
.Last
= null);
122 pragma Assert
(Container
.Length
= 0);
126 pragma Assert
(Container
.First
.Prev
= null);
127 pragma Assert
(Container
.Last
.Next
= null);
128 pragma Assert
(Container
.Length
> 0);
130 Container
.First
:= null;
131 Container
.Last
:= null;
132 Container
.Length
:= 0;
133 Zero_Counts
(Container
.TC
);
135 Container
.First
:= new Node_Type
'(Src.Element, null, null);
136 Container.Last := Container.First;
137 Container.Length := 1;
140 while Src /= null loop
141 Container.Last.Next := new Node_Type'(Element
=> Src
.Element
,
142 Prev
=> Container
.Last
,
144 Container
.Last
:= Container
.Last
.Next
;
145 Container
.Length
:= Container
.Length
+ 1;
156 (Container
: in out List
;
157 New_Item
: Element_Type
;
158 Count
: Count_Type
:= 1)
161 Insert
(Container
, No_Element
, New_Item
, Count
);
168 procedure Assign
(Target
: in out List
; Source
: List
) is
172 if Target
'Address = Source
'Address then
178 Node
:= Source
.First
;
179 while Node
/= null loop
180 Target
.Append
(Node
.Element
);
189 procedure Clear
(Container
: in out List
) is
193 if Container
.Length
= 0 then
194 pragma Assert
(Container
.First
= null);
195 pragma Assert
(Container
.Last
= null);
196 pragma Assert
(Container
.TC
= (Busy
=> 0, Lock
=> 0));
200 pragma Assert
(Container
.First
.Prev
= null);
201 pragma Assert
(Container
.Last
.Next
= null);
203 TC_Check
(Container
.TC
);
205 while Container
.Length
> 1 loop
206 X
:= Container
.First
;
207 pragma Assert
(X
.Next
.Prev
= Container
.First
);
209 Container
.First
:= X
.Next
;
210 Container
.First
.Prev
:= null;
212 Container
.Length
:= Container
.Length
- 1;
217 X
:= Container
.First
;
218 pragma Assert
(X
= Container
.Last
);
220 Container
.First
:= null;
221 Container
.Last
:= null;
222 Container
.Length
:= 0;
224 pragma Warnings
(Off
);
226 pragma Warnings
(On
);
229 ------------------------
230 -- Constant_Reference --
231 ------------------------
233 function Constant_Reference
234 (Container
: aliased List
;
235 Position
: Cursor
) return Constant_Reference_Type
238 if Checks
and then Position
.Container
= null then
239 raise Constraint_Error
with "Position cursor has no element";
242 if Checks
and then Position
.Container
/= Container
'Unrestricted_Access
244 raise Program_Error
with
245 "Position cursor designates wrong container";
248 pragma Assert
(Vet
(Position
), "bad cursor in Constant_Reference");
251 TC
: constant Tamper_Counts_Access
:=
252 Container
.TC
'Unrestricted_Access;
254 return R
: constant Constant_Reference_Type
:=
255 (Element
=> Position
.Node
.Element
'Access,
256 Control
=> (Controlled
with TC
))
261 end Constant_Reference
;
269 Item
: Element_Type
) return Boolean
272 return Find
(Container
, Item
) /= No_Element
;
279 function Copy
(Source
: List
) return List
is
281 return Target
: List
do
282 Target
.Assign
(Source
);
291 (Container
: in out List
;
292 Position
: in out Cursor
;
293 Count
: Count_Type
:= 1)
298 if Checks
and then Position
.Node
= null then
299 raise Constraint_Error
with
300 "Position cursor has no element";
303 if Checks
and then Position
.Container
/= Container
'Unrestricted_Access
305 raise Program_Error
with
306 "Position cursor designates wrong container";
309 pragma Assert
(Vet
(Position
), "bad cursor in Delete");
311 if Position
.Node
= Container
.First
then
312 Delete_First
(Container
, Count
);
313 Position
:= No_Element
; -- Post-York behavior
318 Position
:= No_Element
; -- Post-York behavior
322 TC_Check
(Container
.TC
);
324 for Index
in 1 .. Count
loop
326 Container
.Length
:= Container
.Length
- 1;
328 if X
= Container
.Last
then
329 Position
:= No_Element
;
331 Container
.Last
:= X
.Prev
;
332 Container
.Last
.Next
:= null;
338 Position
.Node
:= X
.Next
;
340 X
.Next
.Prev
:= X
.Prev
;
341 X
.Prev
.Next
:= X
.Next
;
346 -- The following comment is unacceptable, more detail needed ???
348 Position
:= No_Element
; -- Post-York behavior
355 procedure Delete_First
356 (Container
: in out List
;
357 Count
: Count_Type
:= 1)
362 if Count
>= Container
.Length
then
371 TC_Check
(Container
.TC
);
373 for J
in 1 .. Count
loop
374 X
:= Container
.First
;
375 pragma Assert
(X
.Next
.Prev
= Container
.First
);
377 Container
.First
:= X
.Next
;
378 Container
.First
.Prev
:= null;
380 Container
.Length
:= Container
.Length
- 1;
390 procedure Delete_Last
391 (Container
: in out List
;
392 Count
: Count_Type
:= 1)
397 if Count
>= Container
.Length
then
406 TC_Check
(Container
.TC
);
408 for J
in 1 .. Count
loop
410 pragma Assert
(X
.Prev
.Next
= Container
.Last
);
412 Container
.Last
:= X
.Prev
;
413 Container
.Last
.Next
:= null;
415 Container
.Length
:= Container
.Length
- 1;
425 function Element
(Position
: Cursor
) return Element_Type
is
427 if Checks
and then Position
.Node
= null then
428 raise Constraint_Error
with
429 "Position cursor has no element";
432 pragma Assert
(Vet
(Position
), "bad cursor in Element");
434 return Position
.Node
.Element
;
441 procedure Finalize
(Object
: in out Iterator
) is
443 if Object
.Container
/= null then
444 Unbusy
(Object
.Container
.TC
);
455 Position
: Cursor
:= No_Element
) return Cursor
457 Node
: Node_Access
:= Position
.Node
;
461 Node
:= Container
.First
;
464 if Checks
and then Position
.Container
/= Container
'Unrestricted_Access
466 raise Program_Error
with
467 "Position cursor designates wrong container";
470 pragma Assert
(Vet
(Position
), "bad cursor in Find");
473 -- Per AI05-0022, the container implementation is required to detect
474 -- element tampering by a generic actual subprogram.
477 Lock
: With_Lock
(Container
.TC
'Unrestricted_Access);
479 while Node
/= null loop
480 if Node
.Element
= Item
then
481 return Cursor
'(Container'Unrestricted_Access, Node);
495 function First (Container : List) return Cursor is
497 if Container.First = null then
500 return Cursor'(Container
'Unrestricted_Access, Container
.First
);
504 function First
(Object
: Iterator
) return Cursor
is
506 -- The value of the iterator object's Node component influences the
507 -- behavior of the First (and Last) selector function.
509 -- When the Node component is null, this means the iterator object was
510 -- constructed without a start expression, in which case the (forward)
511 -- iteration starts from the (logical) beginning of the entire sequence
512 -- of items (corresponding to Container.First, for a forward iterator).
514 -- Otherwise, this is iteration over a partial sequence of items. When
515 -- the Node component is non-null, the iterator object was constructed
516 -- with a start expression, that specifies the position from which the
517 -- (forward) partial iteration begins.
519 if Object
.Node
= null then
520 return Doubly_Linked_Lists
.First
(Object
.Container
.all);
522 return Cursor
'(Object.Container, Object.Node);
530 function First_Element (Container : List) return Element_Type is
532 if Checks and then Container.First = null then
533 raise Constraint_Error with "list is empty";
536 return Container.First.Element;
543 procedure Free (X : in out Node_Access) is
544 procedure Deallocate is
545 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
548 -- While a node is in use, as an active link in a list, its Previous and
549 -- Next components must be null, or designate a different node; this is
550 -- a node invariant. Before actually deallocating the node, we set both
551 -- access value components of the node to point to the node itself, thus
552 -- falsifying the node invariant. Subprogram Vet inspects the value of
553 -- the node components when interrogating the node, in order to detect
554 -- whether the cursor's node access value is dangling.
556 -- Note that we have no guarantee that the storage for the node isn't
557 -- modified when it is deallocated, but there are other tests that Vet
558 -- does if node invariants appear to be satisifed. However, in practice
559 -- this simple test works well enough, detecting dangling references
560 -- immediately, without needing further interrogation.
568 ---------------------
569 -- Generic_Sorting --
570 ---------------------
572 package body Generic_Sorting is
578 function Is_Sorted (Container : List) return Boolean is
579 -- Per AI05-0022, the container implementation is required to detect
580 -- element tampering by a generic actual subprogram.
582 Lock : With_Lock (Container.TC'Unrestricted_Access);
586 Node := Container.First;
587 for Idx in 2 .. Container.Length loop
588 if Node.Next.Element < Node.Element then
603 (Target : in out List;
604 Source : in out List)
607 -- The semantics of Merge changed slightly per AI05-0021. It was
608 -- originally the case that if Target and Source denoted the same
609 -- container object, then the GNAT implementation of Merge did
610 -- nothing. However, it was argued that RM05 did not precisely
611 -- specify the semantics for this corner case. The decision of the
612 -- ARG was that if Target and Source denote the same non-empty
613 -- container object, then Program_Error is raised.
615 if Source.Is_Empty then
619 if Checks and then Target'Address = Source'Address then
620 raise Program_Error with
621 "Target and Source denote same non-empty container";
624 if Checks and then Target.Length > Count_Type'Last - Source.Length
626 raise Constraint_Error with "new length exceeds maximum";
629 TC_Check (Target.TC);
630 TC_Check (Source.TC);
632 -- Per AI05-0022, the container implementation is required to detect
633 -- element tampering by a generic actual subprogram.
636 Lock_Target : With_Lock (Target.TC'Unchecked_Access);
637 Lock_Source : With_Lock (Source.TC'Unchecked_Access);
639 LI, RI, RJ : Node_Access;
644 while RI /= null loop
645 pragma Assert (RI.Next = null
646 or else not (RI.Next.Element < RI.Element));
649 Splice_Internal (Target, null, Source);
653 pragma Assert (LI.Next = null
654 or else not (LI.Next.Element < LI.Element));
656 if RI.Element < LI.Element then
659 Splice_Internal (Target, LI, Source, RJ);
672 procedure Sort (Container : in out List) is
674 procedure Partition (Pivot : Node_Access; Back : Node_Access);
676 procedure Sort (Front, Back : Node_Access);
682 procedure Partition (Pivot : Node_Access; Back : Node_Access) is
687 while Node /= Back loop
688 if Node.Element < Pivot.Element then
690 Prev : constant Node_Access := Node.Prev;
691 Next : constant Node_Access := Node.Next;
697 Container.Last := Prev;
703 Node.Prev := Pivot.Prev;
707 if Node.Prev = null then
708 Container.First := Node;
710 Node.Prev.Next := Node;
726 procedure Sort (Front, Back : Node_Access) is
727 Pivot : constant Node_Access :=
728 (if Front = null then Container.First else Front.Next);
730 if Pivot /= Back then
731 Partition (Pivot, Back);
737 -- Start of processing for Sort
740 if Container.Length <= 1 then
744 pragma Assert (Container.First.Prev = null);
745 pragma Assert (Container.Last.Next = null);
747 TC_Check (Container.TC);
749 -- Per AI05-0022, the container implementation is required to detect
750 -- element tampering by a generic actual subprogram.
753 Lock : With_Lock (Container.TC'Unchecked_Access);
755 Sort (Front => null, Back => null);
758 pragma Assert (Container.First.Prev = null);
759 pragma Assert (Container.Last.Next = null);
764 ------------------------
765 -- Get_Element_Access --
766 ------------------------
768 function Get_Element_Access
769 (Position : Cursor) return not null Element_Access is
771 return Position.Node.Element'Access;
772 end Get_Element_Access;
778 function Has_Element (Position : Cursor) return Boolean is
780 pragma Assert (Vet (Position), "bad cursor in Has_Element");
781 return Position.Node /= null;
789 (Container : in out List;
791 New_Item : Element_Type;
792 Position : out Cursor;
793 Count : Count_Type := 1)
795 First_Node : Node_Access;
796 New_Node : Node_Access;
799 if Before.Container /= null then
800 if Checks and then Before.Container /= Container'Unrestricted_Access
802 raise Program_Error with
803 "Before cursor designates wrong list";
806 pragma Assert (Vet (Before), "bad cursor in Insert");
814 if Checks and then Container.Length > Count_Type'Last - Count then
815 raise Constraint_Error with "new length exceeds maximum";
818 TC_Check (Container.TC);
820 New_Node := new Node_Type'(New_Item
, null, null);
821 First_Node
:= New_Node
;
822 Insert_Internal
(Container
, Before
.Node
, New_Node
);
824 for J
in 2 .. Count
loop
825 New_Node
:= new Node_Type
'(New_Item, null, null);
826 Insert_Internal (Container, Before.Node, New_Node);
829 Position := Cursor'(Container
'Unchecked_Access, First_Node
);
833 (Container
: in out List
;
835 New_Item
: Element_Type
;
836 Count
: Count_Type
:= 1)
839 pragma Unreferenced
(Position
);
841 Insert
(Container
, Before
, New_Item
, Position
, Count
);
845 (Container
: in out List
;
847 Position
: out Cursor
;
848 Count
: Count_Type
:= 1)
850 First_Node
: Node_Access
;
851 New_Node
: Node_Access
;
854 if Before
.Container
/= null then
855 if Checks
and then Before
.Container
/= Container
'Unrestricted_Access
857 raise Program_Error
with
858 "Before cursor designates wrong list";
861 pragma Assert
(Vet
(Before
), "bad cursor in Insert");
869 if Checks
and then Container
.Length
> Count_Type
'Last - Count
then
870 raise Constraint_Error
with "new length exceeds maximum";
873 TC_Check
(Container
.TC
);
875 New_Node
:= new Node_Type
;
876 First_Node
:= New_Node
;
877 Insert_Internal
(Container
, Before
.Node
, New_Node
);
879 for J
in 2 .. Count
loop
880 New_Node
:= new Node_Type
;
881 Insert_Internal
(Container
, Before
.Node
, New_Node
);
884 Position
:= Cursor
'(Container'Unchecked_Access, First_Node);
887 ---------------------
888 -- Insert_Internal --
889 ---------------------
891 procedure Insert_Internal
892 (Container : in out List;
893 Before : Node_Access;
894 New_Node : Node_Access)
897 if Container.Length = 0 then
898 pragma Assert (Before = null);
899 pragma Assert (Container.First = null);
900 pragma Assert (Container.Last = null);
902 Container.First := New_Node;
903 Container.Last := New_Node;
905 elsif Before = null then
906 pragma Assert (Container.Last.Next = null);
908 Container.Last.Next := New_Node;
909 New_Node.Prev := Container.Last;
911 Container.Last := New_Node;
913 elsif Before = Container.First then
914 pragma Assert (Container.First.Prev = null);
916 Container.First.Prev := New_Node;
917 New_Node.Next := Container.First;
919 Container.First := New_Node;
922 pragma Assert (Container.First.Prev = null);
923 pragma Assert (Container.Last.Next = null);
925 New_Node.Next := Before;
926 New_Node.Prev := Before.Prev;
928 Before.Prev.Next := New_Node;
929 Before.Prev := New_Node;
932 Container.Length := Container.Length + 1;
939 function Is_Empty (Container : List) return Boolean is
941 return Container.Length = 0;
950 Process : not null access procedure (Position : Cursor))
952 Busy : With_Busy (Container.TC'Unrestricted_Access);
953 Node : Node_Access := Container.First;
956 while Node /= null loop
957 Process (Cursor'(Container
'Unrestricted_Access, Node
));
962 function Iterate
(Container
: List
)
963 return List_Iterator_Interfaces
.Reversible_Iterator
'Class
966 -- The value of the Node component influences the behavior of the First
967 -- and Last selector functions of the iterator object. When the Node
968 -- component is null (as is the case here), this means the iterator
969 -- object was constructed without a start expression. This is a
970 -- complete iterator, meaning that the iteration starts from the
971 -- (logical) beginning of the sequence of items.
973 -- Note: For a forward iterator, Container.First is the beginning, and
974 -- for a reverse iterator, Container.Last is the beginning.
976 return It
: constant Iterator
:=
977 Iterator
'(Limited_Controlled with
978 Container => Container'Unrestricted_Access,
981 Busy (Container.TC'Unrestricted_Access.all);
985 function Iterate (Container : List; Start : Cursor)
986 return List_Iterator_Interfaces.Reversible_Iterator'Class
989 -- It was formerly the case that when Start = No_Element, the partial
990 -- iterator was defined to behave the same as for a complete iterator,
991 -- and iterate over the entire sequence of items. However, those
992 -- semantics were unintuitive and arguably error-prone (it is too easy
993 -- to accidentally create an endless loop), and so they were changed,
994 -- per the ARG meeting in Denver on 2011/11. However, there was no
995 -- consensus about what positive meaning this corner case should have,
996 -- and so it was decided to simply raise an exception. This does imply,
997 -- however, that it is not possible to use a partial iterator to specify
998 -- an empty sequence of items.
1000 if Checks and then Start = No_Element then
1001 raise Constraint_Error with
1002 "Start position for iterator equals No_Element";
1005 if Checks and then Start.Container /= Container'Unrestricted_Access then
1006 raise Program_Error with
1007 "Start cursor of Iterate designates wrong list";
1010 pragma Assert (Vet (Start), "Start cursor of Iterate is bad");
1012 -- The value of the Node component influences the behavior of the First
1013 -- and Last selector functions of the iterator object. When the Node
1014 -- component is non-null (as is the case here), it means that this is a
1015 -- partial iteration, over a subset of the complete sequence of items.
1016 -- The iterator object was constructed with a start expression,
1017 -- indicating the position from which the iteration begins. Note that
1018 -- the start position has the same value irrespective of whether this is
1019 -- a forward or reverse iteration.
1021 return It : constant Iterator :=
1022 Iterator'(Limited_Controlled
with
1023 Container
=> Container
'Unrestricted_Access,
1026 Busy
(Container
.TC
'Unrestricted_Access.all);
1034 function Last
(Container
: List
) return Cursor
is
1036 if Container
.Last
= null then
1039 return Cursor
'(Container'Unrestricted_Access, Container.Last);
1043 function Last (Object : Iterator) return Cursor is
1045 -- The value of the iterator object's Node component influences the
1046 -- behavior of the Last (and First) selector function.
1048 -- When the Node component is null, this means the iterator object was
1049 -- constructed without a start expression, in which case the (reverse)
1050 -- iteration starts from the (logical) beginning of the entire sequence
1051 -- (corresponding to Container.Last, for a reverse iterator).
1053 -- Otherwise, this is iteration over a partial sequence of items. When
1054 -- the Node component is non-null, the iterator object was constructed
1055 -- with a start expression, that specifies the position from which the
1056 -- (reverse) partial iteration begins.
1058 if Object.Node = null then
1059 return Doubly_Linked_Lists.Last (Object.Container.all);
1061 return Cursor'(Object
.Container
, Object
.Node
);
1069 function Last_Element
(Container
: List
) return Element_Type
is
1071 if Checks
and then Container
.Last
= null then
1072 raise Constraint_Error
with "list is empty";
1075 return Container
.Last
.Element
;
1082 function Length
(Container
: List
) return Count_Type
is
1084 return Container
.Length
;
1092 (Target
: in out List
;
1093 Source
: in out List
)
1096 if Target
'Address = Source
'Address then
1100 TC_Check
(Source
.TC
);
1104 Target
.First
:= Source
.First
;
1105 Source
.First
:= null;
1107 Target
.Last
:= Source
.Last
;
1108 Source
.Last
:= null;
1110 Target
.Length
:= Source
.Length
;
1118 procedure Next
(Position
: in out Cursor
) is
1120 Position
:= Next
(Position
);
1123 function Next
(Position
: Cursor
) return Cursor
is
1125 if Position
.Node
= null then
1129 pragma Assert
(Vet
(Position
), "bad cursor in Next");
1132 Next_Node
: constant Node_Access
:= Position
.Node
.Next
;
1134 if Next_Node
= null then
1137 return Cursor
'(Position.Container, Next_Node);
1145 Position : Cursor) return Cursor
1148 if Position.Container = null then
1152 if Checks and then Position.Container /= Object.Container then
1153 raise Program_Error with
1154 "Position cursor of Next designates wrong list";
1157 return Next (Position);
1165 (Container : in out List;
1166 New_Item : Element_Type;
1167 Count : Count_Type := 1)
1170 Insert (Container, First (Container), New_Item, Count);
1177 procedure Previous (Position : in out Cursor) is
1179 Position := Previous (Position);
1182 function Previous (Position : Cursor) return Cursor is
1184 if Position.Node = null then
1188 pragma Assert (Vet (Position), "bad cursor in Previous");
1191 Prev_Node : constant Node_Access := Position.Node.Prev;
1193 if Prev_Node = null then
1196 return Cursor'(Position
.Container
, Prev_Node
);
1204 Position
: Cursor
) return Cursor
1207 if Position
.Container
= null then
1211 if Checks
and then Position
.Container
/= Object
.Container
then
1212 raise Program_Error
with
1213 "Position cursor of Previous designates wrong list";
1216 return Previous
(Position
);
1219 ----------------------
1220 -- Pseudo_Reference --
1221 ----------------------
1223 function Pseudo_Reference
1224 (Container
: aliased List
'Class) return Reference_Control_Type
1226 TC
: constant Tamper_Counts_Access
:= Container
.TC
'Unrestricted_Access;
1228 return R
: constant Reference_Control_Type
:= (Controlled
with TC
) do
1231 end Pseudo_Reference
;
1237 procedure Query_Element
1239 Process
: not null access procedure (Element
: Element_Type
))
1242 if Checks
and then Position
.Node
= null then
1243 raise Constraint_Error
with
1244 "Position cursor has no element";
1247 pragma Assert
(Vet
(Position
), "bad cursor in Query_Element");
1250 Lock
: With_Lock
(Position
.Container
.TC
'Unrestricted_Access);
1252 Process
(Position
.Node
.Element
);
1261 (Stream
: not null access Root_Stream_Type
'Class;
1264 N
: Count_Type
'Base;
1269 Count_Type
'Base'Read (Stream, N);
1278 Element_Type'Read (Stream, X.Element);
1289 Item.Length := Item.Length + 1;
1290 exit when Item.Length = N;
1295 Element_Type'Read (Stream, X.Element);
1302 X.Prev := Item.Last;
1303 Item.Last.Next := X;
1309 (Stream : not null access Root_Stream_Type'Class;
1313 raise Program_Error with "attempt to stream list cursor";
1317 (Stream : not null access Root_Stream_Type'Class;
1318 Item : out Reference_Type)
1321 raise Program_Error with "attempt to stream reference";
1325 (Stream : not null access Root_Stream_Type'Class;
1326 Item : out Constant_Reference_Type)
1329 raise Program_Error with "attempt to stream reference";
1337 (Container : aliased in out List;
1338 Position : Cursor) return Reference_Type
1341 if Checks and then Position.Container = null then
1342 raise Constraint_Error with "Position cursor has no element";
1345 if Checks and then Position.Container /= Container'Unchecked_Access then
1346 raise Program_Error with
1347 "Position cursor designates wrong container";
1350 pragma Assert (Vet (Position), "bad cursor in function Reference");
1353 TC : constant Tamper_Counts_Access :=
1354 Container.TC'Unrestricted_Access;
1356 return R : constant Reference_Type :=
1357 (Element => Position.Node.Element'Access,
1358 Control => (Controlled with TC))
1365 ---------------------
1366 -- Replace_Element --
1367 ---------------------
1369 procedure Replace_Element
1370 (Container : in out List;
1372 New_Item : Element_Type)
1375 if Checks and then Position.Container = null then
1376 raise Constraint_Error with "Position cursor has no element";
1379 if Checks and then Position.Container /= Container'Unchecked_Access then
1380 raise Program_Error with
1381 "Position cursor designates wrong container";
1384 TE_Check (Container.TC);
1386 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
1388 Position.Node.Element := New_Item;
1389 end Replace_Element;
1391 ----------------------
1392 -- Reverse_Elements --
1393 ----------------------
1395 procedure Reverse_Elements (Container : in out List) is
1396 I : Node_Access := Container.First;
1397 J : Node_Access := Container.Last;
1399 procedure Swap (L, R : Node_Access);
1405 procedure Swap (L, R : Node_Access) is
1406 LN : constant Node_Access := L.Next;
1407 LP : constant Node_Access := L.Prev;
1409 RN : constant Node_Access := R.Next;
1410 RP : constant Node_Access := R.Prev;
1425 pragma Assert (RP = L);
1439 -- Start of processing for Reverse_Elements
1442 if Container.Length <= 1 then
1446 pragma Assert (Container.First.Prev = null);
1447 pragma Assert (Container.Last.Next = null);
1449 TC_Check (Container.TC);
1451 Container.First := J;
1452 Container.Last := I;
1454 Swap (L => I, R => J);
1462 Swap (L => J, R => I);
1471 pragma Assert (Container.First.Prev = null);
1472 pragma Assert (Container.Last.Next = null);
1473 end Reverse_Elements;
1479 function Reverse_Find
1481 Item : Element_Type;
1482 Position : Cursor := No_Element) return Cursor
1484 Node : Node_Access := Position.Node;
1488 Node := Container.Last;
1491 if Checks and then Position.Container /= Container'Unrestricted_Access
1493 raise Program_Error with
1494 "Position cursor designates wrong container";
1497 pragma Assert (Vet (Position), "bad cursor in Reverse_Find");
1500 -- Per AI05-0022, the container implementation is required to detect
1501 -- element tampering by a generic actual subprogram.
1504 Lock : With_Lock (Container.TC'Unrestricted_Access);
1506 while Node /= null loop
1507 if Node.Element = Item then
1508 return Cursor'(Container
'Unrestricted_Access, Node
);
1518 ---------------------
1519 -- Reverse_Iterate --
1520 ---------------------
1522 procedure Reverse_Iterate
1524 Process
: not null access procedure (Position
: Cursor
))
1526 Busy
: With_Busy
(Container
.TC
'Unrestricted_Access);
1527 Node
: Node_Access
:= Container
.Last
;
1530 while Node
/= null loop
1531 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1534 end Reverse_Iterate;
1541 (Target : in out List;
1543 Source : in out List)
1546 if Before.Container /= null then
1547 if Checks and then Before.Container /= Target'Unrestricted_Access then
1548 raise Program_Error with
1549 "Before cursor designates wrong container";
1552 pragma Assert (Vet (Before), "bad cursor in Splice");
1555 if Target'Address = Source'Address or else Source.Length = 0 then
1559 if Checks and then Target.Length > Count_Type'Last - Source.Length then
1560 raise Constraint_Error with "new length exceeds maximum";
1563 TC_Check (Target.TC);
1564 TC_Check (Source.TC);
1566 Splice_Internal (Target, Before.Node, Source);
1570 (Container : in out List;
1575 if Before.Container /= null then
1576 if Checks and then Before.Container /= Container'Unchecked_Access then
1577 raise Program_Error with
1578 "Before cursor designates wrong container";
1581 pragma Assert (Vet (Before), "bad Before cursor in Splice");
1584 if Checks and then Position.Node = null then
1585 raise Constraint_Error with "Position cursor has no element";
1588 if Checks and then Position.Container /= Container'Unrestricted_Access
1590 raise Program_Error with
1591 "Position cursor designates wrong container";
1594 pragma Assert (Vet (Position), "bad Position cursor in Splice");
1596 if Position.Node = Before.Node
1597 or else Position.Node.Next = Before.Node
1602 pragma Assert (Container.Length >= 2);
1604 TC_Check (Container.TC);
1606 if Before.Node = null then
1607 pragma Assert (Position.Node /= Container.Last);
1609 if Position.Node = Container.First then
1610 Container.First := Position.Node.Next;
1611 Container.First.Prev := null;
1613 Position.Node.Prev.Next := Position.Node.Next;
1614 Position.Node.Next.Prev := Position.Node.Prev;
1617 Container.Last.Next := Position.Node;
1618 Position.Node.Prev := Container.Last;
1620 Container.Last := Position.Node;
1621 Container.Last.Next := null;
1626 if Before.Node = Container.First then
1627 pragma Assert (Position.Node /= Container.First);
1629 if Position.Node = Container.Last then
1630 Container.Last := Position.Node.Prev;
1631 Container.Last.Next := null;
1633 Position.Node.Prev.Next := Position.Node.Next;
1634 Position.Node.Next.Prev := Position.Node.Prev;
1637 Container.First.Prev := Position.Node;
1638 Position.Node.Next := Container.First;
1640 Container.First := Position.Node;
1641 Container.First.Prev := null;
1646 if Position.Node = Container.First then
1647 Container.First := Position.Node.Next;
1648 Container.First.Prev := null;
1650 elsif Position.Node = Container.Last then
1651 Container.Last := Position.Node.Prev;
1652 Container.Last.Next := null;
1655 Position.Node.Prev.Next := Position.Node.Next;
1656 Position.Node.Next.Prev := Position.Node.Prev;
1659 Before.Node.Prev.Next := Position.Node;
1660 Position.Node.Prev := Before.Node.Prev;
1662 Before.Node.Prev := Position.Node;
1663 Position.Node.Next := Before.Node;
1665 pragma Assert (Container.First.Prev = null);
1666 pragma Assert (Container.Last.Next = null);
1670 (Target : in out List;
1672 Source : in out List;
1673 Position : in out Cursor)
1676 if Target'Address = Source'Address then
1677 Splice (Target, Before, Position);
1681 if Before.Container /= null then
1682 if Checks and then Before.Container /= Target'Unrestricted_Access then
1683 raise Program_Error with
1684 "Before cursor designates wrong container";
1687 pragma Assert (Vet (Before), "bad Before cursor in Splice");
1690 if Checks and then Position.Node = null then
1691 raise Constraint_Error with "Position cursor has no element";
1694 if Checks and then Position.Container /= Source'Unrestricted_Access then
1695 raise Program_Error with
1696 "Position cursor designates wrong container";
1699 pragma Assert (Vet (Position), "bad Position cursor in Splice");
1701 if Checks and then Target.Length = Count_Type'Last then
1702 raise Constraint_Error with "Target is full";
1705 TC_Check (Target.TC);
1706 TC_Check (Source.TC);
1708 Splice_Internal (Target, Before.Node, Source, Position.Node);
1709 Position.Container := Target'Unchecked_Access;
1712 ---------------------
1713 -- Splice_Internal --
1714 ---------------------
1716 procedure Splice_Internal
1717 (Target : in out List;
1718 Before : Node_Access;
1719 Source : in out List)
1722 -- This implements the corresponding Splice operation, after the
1723 -- parameters have been vetted, and corner-cases disposed of.
1725 pragma Assert (Target'Address /= Source'Address);
1726 pragma Assert (Source.Length > 0);
1727 pragma Assert (Source.First /= null);
1728 pragma Assert (Source.First.Prev = null);
1729 pragma Assert (Source.Last /= null);
1730 pragma Assert (Source.Last.Next = null);
1731 pragma Assert (Target.Length <= Count_Type'Last - Source.Length);
1733 if Target.Length = 0 then
1734 pragma Assert (Target.First = null);
1735 pragma Assert (Target.Last = null);
1736 pragma Assert (Before = null);
1738 Target.First := Source.First;
1739 Target.Last := Source.Last;
1741 elsif Before = null then
1742 pragma Assert (Target.Last.Next = null);
1744 Target.Last.Next := Source.First;
1745 Source.First.Prev := Target.Last;
1747 Target.Last := Source.Last;
1749 elsif Before = Target.First then
1750 pragma Assert (Target.First.Prev = null);
1752 Source.Last.Next := Target.First;
1753 Target.First.Prev := Source.Last;
1755 Target.First := Source.First;
1758 pragma Assert (Target.Length >= 2);
1760 Before.Prev.Next := Source.First;
1761 Source.First.Prev := Before.Prev;
1763 Before.Prev := Source.Last;
1764 Source.Last.Next := Before;
1767 Source.First := null;
1768 Source.Last := null;
1770 Target.Length := Target.Length + Source.Length;
1772 end Splice_Internal;
1774 procedure Splice_Internal
1775 (Target : in out List;
1776 Before : Node_Access; -- node of Target
1777 Source : in out List;
1778 Position : Node_Access) -- node of Source
1781 -- This implements the corresponding Splice operation, after the
1782 -- parameters have been vetted.
1784 pragma Assert (Target'Address /= Source'Address);
1785 pragma Assert (Target.Length < Count_Type'Last);
1786 pragma Assert (Source.Length > 0);
1787 pragma Assert (Source.First /= null);
1788 pragma Assert (Source.First.Prev = null);
1789 pragma Assert (Source.Last /= null);
1790 pragma Assert (Source.Last.Next = null);
1791 pragma Assert (Position /= null);
1793 if Position = Source.First then
1794 Source.First := Position.Next;
1796 if Position = Source.Last then
1797 pragma Assert (Source.First = null);
1798 pragma Assert (Source.Length = 1);
1799 Source.Last := null;
1802 Source.First.Prev := null;
1805 elsif Position = Source.Last then
1806 pragma Assert (Source.Length >= 2);
1807 Source.Last := Position.Prev;
1808 Source.Last.Next := null;
1811 pragma Assert (Source.Length >= 3);
1812 Position.Prev.Next := Position.Next;
1813 Position.Next.Prev := Position.Prev;
1816 if Target.Length = 0 then
1817 pragma Assert (Target.First = null);
1818 pragma Assert (Target.Last = null);
1819 pragma Assert (Before = null);
1821 Target.First := Position;
1822 Target.Last := Position;
1824 Target.First.Prev := null;
1825 Target.Last.Next := null;
1827 elsif Before = null then
1828 pragma Assert (Target.Last.Next = null);
1829 Target.Last.Next := Position;
1830 Position.Prev := Target.Last;
1832 Target.Last := Position;
1833 Target.Last.Next := null;
1835 elsif Before = Target.First then
1836 pragma Assert (Target.First.Prev = null);
1837 Target.First.Prev := Position;
1838 Position.Next := Target.First;
1840 Target.First := Position;
1841 Target.First.Prev := null;
1844 pragma Assert (Target.Length >= 2);
1845 Before.Prev.Next := Position;
1846 Position.Prev := Before.Prev;
1848 Before.Prev := Position;
1849 Position.Next := Before;
1852 Target.Length := Target.Length + 1;
1853 Source.Length := Source.Length - 1;
1854 end Splice_Internal;
1861 (Container : in out List;
1865 if Checks and then I.Node = null then
1866 raise Constraint_Error with "I cursor has no element";
1869 if Checks and then J.Node = null then
1870 raise Constraint_Error with "J cursor has no element";
1873 if Checks and then I.Container /= Container'Unchecked_Access then
1874 raise Program_Error with "I cursor designates wrong container";
1877 if Checks and then J.Container /= Container'Unchecked_Access then
1878 raise Program_Error with "J cursor designates wrong container";
1881 if I.Node = J.Node then
1885 TE_Check (Container.TC);
1887 pragma Assert (Vet (I), "bad I cursor in Swap");
1888 pragma Assert (Vet (J), "bad J cursor in Swap");
1891 EI : Element_Type renames I.Node.Element;
1892 EJ : Element_Type renames J.Node.Element;
1894 EI_Copy : constant Element_Type := EI;
1906 procedure Swap_Links
1907 (Container : in out List;
1911 if Checks and then I.Node = null then
1912 raise Constraint_Error with "I cursor has no element";
1915 if Checks and then J.Node = null then
1916 raise Constraint_Error with "J cursor has no element";
1919 if Checks and then I.Container /= Container'Unrestricted_Access then
1920 raise Program_Error with "I cursor designates wrong container";
1923 if Checks and then J.Container /= Container'Unrestricted_Access then
1924 raise Program_Error with "J cursor designates wrong container";
1927 if I.Node = J.Node then
1931 TC_Check (Container.TC);
1933 pragma Assert (Vet (I), "bad I cursor in Swap_Links");
1934 pragma Assert (Vet (J), "bad J cursor in Swap_Links");
1937 I_Next : constant Cursor := Next (I);
1941 Splice (Container, Before => I, Position => J);
1945 J_Next : constant Cursor := Next (J);
1949 Splice (Container, Before => J, Position => I);
1952 pragma Assert (Container.Length >= 3);
1954 Splice (Container, Before => I_Next, Position => J);
1955 Splice (Container, Before => J_Next, Position => I);
1962 --------------------
1963 -- Update_Element --
1964 --------------------
1966 procedure Update_Element
1967 (Container : in out List;
1969 Process : not null access procedure (Element : in out Element_Type))
1972 if Checks and then Position.Node = null then
1973 raise Constraint_Error with "Position cursor has no element";
1976 if Checks and then Position.Container /= Container'Unchecked_Access then
1977 raise Program_Error with
1978 "Position cursor designates wrong container";
1981 pragma Assert (Vet (Position), "bad cursor in Update_Element");
1984 Lock : With_Lock (Container.TC'Unchecked_Access);
1986 Process (Position.Node.Element);
1994 function Vet (Position : Cursor) return Boolean is
1996 if Position.Node = null then
1997 return Position.Container = null;
2000 if Position.Container = null then
2004 -- An invariant of a node is that its Previous and Next components can
2005 -- be null, or designate a different node. Operation Free sets the
2006 -- access value components of the node to designate the node itself
2007 -- before actually deallocating the node, thus deliberately violating
2008 -- the node invariant. This gives us a simple way to detect a dangling
2009 -- reference to a node.
2011 if Position.Node.Next = Position.Node then
2015 if Position.Node.Prev = Position.Node then
2019 -- In practice the tests above will detect most instances of a dangling
2020 -- reference. If we get here, it means that the invariants of the
2021 -- designated node are satisfied (they at least appear to be satisfied),
2022 -- so we perform some more tests, to determine whether invariants of the
2023 -- designated list are satisfied too.
2026 L : List renames Position.Container.all;
2029 if L.Length = 0 then
2033 if L.First = null then
2037 if L.Last = null then
2041 if L.First.Prev /= null then
2045 if L.Last.Next /= null then
2049 if Position.Node.Prev = null and then Position.Node /= L.First then
2054 (Position.Node.Prev /= null or else Position.Node = L.First);
2056 if Position.Node.Next = null and then Position.Node /= L.Last then
2061 (Position.Node.Next /= null
2062 or else Position.Node = L.Last);
2064 if L.Length = 1 then
2065 return L.First = L.Last;
2068 if L.First = L.Last then
2072 if L.First.Next = null then
2076 if L.Last.Prev = null then
2080 if L.First.Next.Prev /= L.First then
2084 if L.Last.Prev.Next /= L.Last then
2088 if L.Length = 2 then
2089 if L.First.Next /= L.Last then
2091 elsif L.Last.Prev /= L.First then
2098 if L.First.Next = L.Last then
2102 if L.Last.Prev = L.First then
2106 -- Eliminate earlier possibility
2108 if Position.Node = L.First then
2112 pragma Assert (Position.Node.Prev /= null);
2114 -- Eliminate earlier possibility
2116 if Position.Node = L.Last then
2120 pragma Assert (Position.Node.Next /= null);
2122 if Position.Node.Next.Prev /= Position.Node then
2126 if Position.Node.Prev.Next /= Position.Node then
2130 if L.Length = 3 then
2131 if L.First.Next /= Position.Node then
2133 elsif L.Last.Prev /= Position.Node then
2147 (Stream : not null access Root_Stream_Type'Class;
2153 Count_Type'Base'Write
(Stream
, Item
.Length
);
2156 while Node
/= null loop
2157 Element_Type
'Write (Stream
, Node
.Element
);
2163 (Stream
: not null access Root_Stream_Type
'Class;
2167 raise Program_Error
with "attempt to stream list cursor";
2171 (Stream
: not null access Root_Stream_Type
'Class;
2172 Item
: Reference_Type
)
2175 raise Program_Error
with "attempt to stream reference";
2179 (Stream
: not null access Root_Stream_Type
'Class;
2180 Item
: Constant_Reference_Type
)
2183 raise Program_Error
with "attempt to stream reference";
2186 end Ada
.Containers
.Doubly_Linked_Lists
;