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-2015, 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 Annotate
(CodePeer
, Skip_Analysis
);
38 -----------------------
39 -- Local Subprograms --
40 -----------------------
42 procedure Free
(X
: in out Node_Access
);
44 procedure Insert_Internal
45 (Container
: in out List
;
47 New_Node
: Node_Access
);
49 procedure Splice_Internal
50 (Target
: in out List
;
52 Source
: in out List
);
54 procedure Splice_Internal
55 (Target
: in out List
;
58 Position
: Node_Access
);
60 function Vet
(Position
: Cursor
) return Boolean;
61 -- Checks invariants of the cursor and its designated container, as a
62 -- simple way of detecting dangling references (see operation Free for a
63 -- description of the detection mechanism), returning True if all checks
64 -- pass. Invocations of Vet are used here as the argument of pragma Assert,
65 -- so the checks are performed only when assertions are enabled.
71 function "=" (Left
, Right
: List
) return Boolean is
72 BL
: Natural renames Left
'Unrestricted_Access.Busy
;
73 LL
: Natural renames Left
'Unrestricted_Access.Lock
;
75 BR
: Natural renames Right
'Unrestricted_Access.Busy
;
76 LR
: Natural renames Right
'Unrestricted_Access.Lock
;
83 if Left
'Address = Right
'Address then
87 if Left
.Length
/= Right
.Length
then
91 -- Per AI05-0022, the container implementation is required to detect
92 -- element tampering by a generic actual subprogram.
103 for J
in 1 .. Left
.Length
loop
104 if L
.Element
/= R
.Element
then
136 procedure Adjust
(Container
: in out List
) is
137 Src
: Node_Access
:= Container
.First
;
141 pragma Assert
(Container
.Last
= null);
142 pragma Assert
(Container
.Length
= 0);
143 pragma Assert
(Container
.Busy
= 0);
144 pragma Assert
(Container
.Lock
= 0);
148 pragma Assert
(Container
.First
.Prev
= null);
149 pragma Assert
(Container
.Last
.Next
= null);
150 pragma Assert
(Container
.Length
> 0);
152 Container
.First
:= null;
153 Container
.Last
:= null;
154 Container
.Length
:= 0;
158 Container
.First
:= new Node_Type
'(Src.Element, null, null);
159 Container.Last := Container.First;
160 Container.Length := 1;
163 while Src /= null loop
164 Container.Last.Next := new Node_Type'(Element
=> Src
.Element
,
165 Prev
=> Container
.Last
,
167 Container
.Last
:= Container
.Last
.Next
;
168 Container
.Length
:= Container
.Length
+ 1;
174 procedure Adjust
(Control
: in out Reference_Control_Type
) is
176 if Control
.Container
/= null then
178 C
: List
renames Control
.Container
.all;
179 B
: Natural renames C
.Busy
;
180 L
: Natural renames C
.Lock
;
193 (Container
: in out List
;
194 New_Item
: Element_Type
;
195 Count
: Count_Type
:= 1)
198 Insert
(Container
, No_Element
, New_Item
, Count
);
205 procedure Assign
(Target
: in out List
; Source
: List
) is
209 if Target
'Address = Source
'Address then
215 Node
:= Source
.First
;
216 while Node
/= null loop
217 Target
.Append
(Node
.Element
);
226 procedure Clear
(Container
: in out List
) is
230 if Container
.Length
= 0 then
231 pragma Assert
(Container
.First
= null);
232 pragma Assert
(Container
.Last
= null);
233 pragma Assert
(Container
.Busy
= 0);
234 pragma Assert
(Container
.Lock
= 0);
238 pragma Assert
(Container
.First
.Prev
= null);
239 pragma Assert
(Container
.Last
.Next
= null);
241 if Container
.Busy
> 0 then
242 raise Program_Error
with
243 "attempt to tamper with cursors (list is busy)";
246 while Container
.Length
> 1 loop
247 X
:= Container
.First
;
248 pragma Assert
(X
.Next
.Prev
= Container
.First
);
250 Container
.First
:= X
.Next
;
251 Container
.First
.Prev
:= null;
253 Container
.Length
:= Container
.Length
- 1;
258 X
:= Container
.First
;
259 pragma Assert
(X
= Container
.Last
);
261 Container
.First
:= null;
262 Container
.Last
:= null;
263 Container
.Length
:= 0;
265 pragma Warnings
(Off
);
267 pragma Warnings
(On
);
270 ------------------------
271 -- Constant_Reference --
272 ------------------------
274 function Constant_Reference
275 (Container
: aliased List
;
276 Position
: Cursor
) return Constant_Reference_Type
279 if Position
.Container
= null then
280 raise Constraint_Error
with "Position cursor has no element";
283 if Position
.Container
/= Container
'Unrestricted_Access then
284 raise Program_Error
with
285 "Position cursor designates wrong container";
288 pragma Assert
(Vet
(Position
), "bad cursor in Constant_Reference");
291 C
: List
renames Position
.Container
.all;
292 B
: Natural renames C
.Busy
;
293 L
: Natural renames C
.Lock
;
295 return R
: constant Constant_Reference_Type
:=
296 (Element
=> Position
.Node
.Element
'Access,
297 Control
=> (Controlled
with Container
'Unrestricted_Access))
303 end Constant_Reference
;
311 Item
: Element_Type
) return Boolean
314 return Find
(Container
, Item
) /= No_Element
;
321 function Copy
(Source
: List
) return List
is
323 return Target
: List
do
324 Target
.Assign
(Source
);
333 (Container
: in out List
;
334 Position
: in out Cursor
;
335 Count
: Count_Type
:= 1)
340 if Position
.Node
= null then
341 raise Constraint_Error
with
342 "Position cursor has no element";
345 if Position
.Container
/= Container
'Unrestricted_Access then
346 raise Program_Error
with
347 "Position cursor designates wrong container";
350 pragma Assert
(Vet
(Position
), "bad cursor in Delete");
352 if Position
.Node
= Container
.First
then
353 Delete_First
(Container
, Count
);
354 Position
:= No_Element
; -- Post-York behavior
359 Position
:= No_Element
; -- Post-York behavior
363 if Container
.Busy
> 0 then
364 raise Program_Error
with
365 "attempt to tamper with cursors (list is busy)";
368 for Index
in 1 .. Count
loop
370 Container
.Length
:= Container
.Length
- 1;
372 if X
= Container
.Last
then
373 Position
:= No_Element
;
375 Container
.Last
:= X
.Prev
;
376 Container
.Last
.Next
:= null;
382 Position
.Node
:= X
.Next
;
384 X
.Next
.Prev
:= X
.Prev
;
385 X
.Prev
.Next
:= X
.Next
;
390 -- The following comment is unacceptable, more detail needed ???
392 Position
:= No_Element
; -- Post-York behavior
399 procedure Delete_First
400 (Container
: in out List
;
401 Count
: Count_Type
:= 1)
406 if Count
>= Container
.Length
then
415 if Container
.Busy
> 0 then
416 raise Program_Error
with
417 "attempt to tamper with cursors (list is busy)";
420 for J
in 1 .. Count
loop
421 X
:= Container
.First
;
422 pragma Assert
(X
.Next
.Prev
= Container
.First
);
424 Container
.First
:= X
.Next
;
425 Container
.First
.Prev
:= null;
427 Container
.Length
:= Container
.Length
- 1;
437 procedure Delete_Last
438 (Container
: in out List
;
439 Count
: Count_Type
:= 1)
444 if Count
>= Container
.Length
then
453 if Container
.Busy
> 0 then
454 raise Program_Error
with
455 "attempt to tamper with cursors (list is busy)";
458 for J
in 1 .. Count
loop
460 pragma Assert
(X
.Prev
.Next
= Container
.Last
);
462 Container
.Last
:= X
.Prev
;
463 Container
.Last
.Next
:= null;
465 Container
.Length
:= Container
.Length
- 1;
475 function Element
(Position
: Cursor
) return Element_Type
is
477 if Position
.Node
= null then
478 raise Constraint_Error
with
479 "Position cursor has no element";
481 pragma Assert
(Vet
(Position
), "bad cursor in Element");
483 return Position
.Node
.Element
;
491 procedure Finalize
(Object
: in out Iterator
) is
493 if Object
.Container
/= null then
495 B
: Natural renames Object
.Container
.all.Busy
;
502 procedure Finalize
(Control
: in out Reference_Control_Type
) is
504 if Control
.Container
/= null then
506 C
: List
renames Control
.Container
.all;
507 B
: Natural renames C
.Busy
;
508 L
: Natural renames C
.Lock
;
514 Control
.Container
:= null;
525 Position
: Cursor
:= No_Element
) return Cursor
527 Node
: Node_Access
:= Position
.Node
;
531 Node
:= Container
.First
;
534 if Position
.Container
/= Container
'Unrestricted_Access then
535 raise Program_Error
with
536 "Position cursor designates wrong container";
538 pragma Assert
(Vet
(Position
), "bad cursor in Find");
542 -- Per AI05-0022, the container implementation is required to detect
543 -- element tampering by a generic actual subprogram.
546 B
: Natural renames Container
'Unrestricted_Access.Busy
;
547 L
: Natural renames Container
'Unrestricted_Access.Lock
;
549 Result
: Node_Access
;
555 pragma Warnings
(Off
);
556 -- Deal with junk infinite loop warning from below loop
559 while Node
/= null loop
560 if Node
.Element
= Item
then
568 pragma Warnings
(On
);
569 -- End of section dealing with junk infinite loop warning
574 if Result
= null then
577 return Cursor
'(Container'Unrestricted_Access, Result);
592 function First (Container : List) return Cursor is
594 if Container.First = null then
597 return Cursor'(Container
'Unrestricted_Access, Container
.First
);
601 function First
(Object
: Iterator
) return Cursor
is
603 -- The value of the iterator object's Node component influences the
604 -- behavior of the First (and Last) selector function.
606 -- When the Node component is null, this means the iterator object was
607 -- constructed without a start expression, in which case the (forward)
608 -- iteration starts from the (logical) beginning of the entire sequence
609 -- of items (corresponding to Container.First, for a forward iterator).
611 -- Otherwise, this is iteration over a partial sequence of items. When
612 -- the Node component is non-null, the iterator object was constructed
613 -- with a start expression, that specifies the position from which the
614 -- (forward) partial iteration begins.
616 if Object
.Node
= null then
617 return Doubly_Linked_Lists
.First
(Object
.Container
.all);
619 return Cursor
'(Object.Container, Object.Node);
627 function First_Element (Container : List) return Element_Type is
629 if Container.First = null then
630 raise Constraint_Error with "list is empty";
632 return Container.First.Element;
640 procedure Free (X : in out Node_Access) is
641 procedure Deallocate is
642 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
645 -- While a node is in use, as an active link in a list, its Previous and
646 -- Next components must be null, or designate a different node; this is
647 -- a node invariant. Before actually deallocating the node, we set both
648 -- access value components of the node to point to the node itself, thus
649 -- falsifying the node invariant. Subprogram Vet inspects the value of
650 -- the node components when interrogating the node, in order to detect
651 -- whether the cursor's node access value is dangling.
653 -- Note that we have no guarantee that the storage for the node isn't
654 -- modified when it is deallocated, but there are other tests that Vet
655 -- does if node invariants appear to be satisifed. However, in practice
656 -- this simple test works well enough, detecting dangling references
657 -- immediately, without needing further interrogation.
665 ---------------------
666 -- Generic_Sorting --
667 ---------------------
669 package body Generic_Sorting is
675 function Is_Sorted (Container : List) return Boolean is
676 B : Natural renames Container'Unrestricted_Access.Busy;
677 L : Natural renames Container'Unrestricted_Access.Lock;
683 -- Per AI05-0022, the container implementation is required to detect
684 -- element tampering by a generic actual subprogram.
689 Node := Container.First;
691 for Idx in 2 .. Container.Length loop
692 if Node.Next.Element < Node.Element then
717 (Target : in out List;
718 Source : in out List)
721 -- The semantics of Merge changed slightly per AI05-0021. It was
722 -- originally the case that if Target and Source denoted the same
723 -- container object, then the GNAT implementation of Merge did
724 -- nothing. However, it was argued that RM05 did not precisely
725 -- specify the semantics for this corner case. The decision of the
726 -- ARG was that if Target and Source denote the same non-empty
727 -- container object, then Program_Error is raised.
729 if Source.Is_Empty then
733 if Target'Address = Source'Address then
734 raise Program_Error with
735 "Target and Source denote same non-empty container";
738 if Target.Length > Count_Type'Last - Source.Length then
739 raise Constraint_Error with "new length exceeds maximum";
742 if Target.Busy > 0 then
743 raise Program_Error with
744 "attempt to tamper with cursors of Target (list is busy)";
747 if Source.Busy > 0 then
748 raise Program_Error with
749 "attempt to tamper with cursors of Source (list is busy)";
752 -- Per AI05-0022, the container implementation is required to detect
753 -- element tampering by a generic actual subprogram.
756 TB : Natural renames Target.Busy;
757 TL : Natural renames Target.Lock;
759 SB : Natural renames Source.Busy;
760 SL : Natural renames Source.Lock;
762 LI, RI, RJ : Node_Access;
773 while RI /= null loop
774 pragma Assert (RI.Next = null
775 or else not (RI.Next.Element < RI.Element));
778 Splice_Internal (Target, null, Source);
782 pragma Assert (LI.Next = null
783 or else not (LI.Next.Element < LI.Element));
785 if RI.Element < LI.Element then
788 Splice_Internal (Target, LI, Source, RJ);
817 procedure Sort (Container : in out List) is
819 procedure Partition (Pivot : Node_Access; Back : Node_Access);
821 procedure Sort (Front, Back : Node_Access);
827 procedure Partition (Pivot : Node_Access; Back : Node_Access) is
832 while Node /= Back loop
833 if Node.Element < Pivot.Element then
835 Prev : constant Node_Access := Node.Prev;
836 Next : constant Node_Access := Node.Next;
842 Container.Last := Prev;
848 Node.Prev := Pivot.Prev;
852 if Node.Prev = null then
853 Container.First := Node;
855 Node.Prev.Next := Node;
871 procedure Sort (Front, Back : Node_Access) is
872 Pivot : constant Node_Access :=
873 (if Front = null then Container.First else Front.Next);
875 if Pivot /= Back then
876 Partition (Pivot, Back);
882 -- Start of processing for Sort
885 if Container.Length <= 1 then
889 pragma Assert (Container.First.Prev = null);
890 pragma Assert (Container.Last.Next = null);
892 if Container.Busy > 0 then
893 raise Program_Error with
894 "attempt to tamper with cursors (list is busy)";
897 -- Per AI05-0022, the container implementation is required to detect
898 -- element tampering by a generic actual subprogram.
901 B : Natural renames Container.Busy;
902 L : Natural renames Container.Lock;
908 Sort (Front => null, Back => null);
920 pragma Assert (Container.First.Prev = null);
921 pragma Assert (Container.Last.Next = null);
926 ------------------------
927 -- Get_Element_Access --
928 ------------------------
930 function Get_Element_Access
931 (Position : Cursor) return not null Element_Access is
933 return Position.Node.Element'Access;
934 end Get_Element_Access;
940 function Has_Element (Position : Cursor) return Boolean is
942 pragma Assert (Vet (Position), "bad cursor in Has_Element");
943 return Position.Node /= null;
951 (Container : in out List;
953 New_Item : Element_Type;
954 Position : out Cursor;
955 Count : Count_Type := 1)
957 First_Node : Node_Access;
958 New_Node : Node_Access;
961 if Before.Container /= null then
962 if Before.Container /= Container'Unrestricted_Access then
963 raise Program_Error with
964 "Before cursor designates wrong list";
966 pragma Assert (Vet (Before), "bad cursor in Insert");
974 elsif Container.Length > Count_Type'Last - Count then
975 raise Constraint_Error with "new length exceeds maximum";
977 elsif Container.Busy > 0 then
978 raise Program_Error with
979 "attempt to tamper with cursors (list is busy)";
982 New_Node := new Node_Type'(New_Item
, null, null);
983 First_Node
:= New_Node
;
984 Insert_Internal
(Container
, Before
.Node
, New_Node
);
986 for J
in 2 .. Count
loop
987 New_Node
:= new Node_Type
'(New_Item, null, null);
988 Insert_Internal (Container, Before.Node, New_Node);
991 Position := Cursor'(Container
'Unchecked_Access, First_Node
);
996 (Container
: in out List
;
998 New_Item
: Element_Type
;
999 Count
: Count_Type
:= 1)
1002 pragma Unreferenced
(Position
);
1004 Insert
(Container
, Before
, New_Item
, Position
, Count
);
1008 (Container
: in out List
;
1010 Position
: out Cursor
;
1011 Count
: Count_Type
:= 1)
1013 First_Node
: Node_Access
;
1014 New_Node
: Node_Access
;
1017 if Before
.Container
/= null then
1018 if Before
.Container
/= Container
'Unrestricted_Access then
1019 raise Program_Error
with
1020 "Before cursor designates wrong list";
1022 pragma Assert
(Vet
(Before
), "bad cursor in Insert");
1031 if Container
.Length
> Count_Type
'Last - Count
then
1032 raise Constraint_Error
with "new length exceeds maximum";
1034 elsif Container
.Busy
> 0 then
1035 raise Program_Error
with
1036 "attempt to tamper with cursors (list is busy)";
1039 New_Node
:= new Node_Type
;
1040 First_Node
:= New_Node
;
1041 Insert_Internal
(Container
, Before
.Node
, New_Node
);
1043 for J
in 2 .. Count
loop
1044 New_Node
:= new Node_Type
;
1045 Insert_Internal
(Container
, Before
.Node
, New_Node
);
1048 Position
:= Cursor
'(Container'Unchecked_Access, First_Node);
1052 ---------------------
1053 -- Insert_Internal --
1054 ---------------------
1056 procedure Insert_Internal
1057 (Container : in out List;
1058 Before : Node_Access;
1059 New_Node : Node_Access)
1062 if Container.Length = 0 then
1063 pragma Assert (Before = null);
1064 pragma Assert (Container.First = null);
1065 pragma Assert (Container.Last = null);
1067 Container.First := New_Node;
1068 Container.Last := New_Node;
1070 elsif Before = null then
1071 pragma Assert (Container.Last.Next = null);
1073 Container.Last.Next := New_Node;
1074 New_Node.Prev := Container.Last;
1076 Container.Last := New_Node;
1078 elsif Before = Container.First then
1079 pragma Assert (Container.First.Prev = null);
1081 Container.First.Prev := New_Node;
1082 New_Node.Next := Container.First;
1084 Container.First := New_Node;
1087 pragma Assert (Container.First.Prev = null);
1088 pragma Assert (Container.Last.Next = null);
1090 New_Node.Next := Before;
1091 New_Node.Prev := Before.Prev;
1093 Before.Prev.Next := New_Node;
1094 Before.Prev := New_Node;
1097 Container.Length := Container.Length + 1;
1098 end Insert_Internal;
1104 function Is_Empty (Container : List) return Boolean is
1106 return Container.Length = 0;
1115 Process : not null access procedure (Position : Cursor))
1117 B : Natural renames Container'Unrestricted_Access.all.Busy;
1118 Node : Node_Access := Container.First;
1124 while Node /= null loop
1125 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1137 function Iterate
(Container
: List
)
1138 return List_Iterator_Interfaces
.Reversible_Iterator
'Class
1140 B
: Natural renames Container
'Unrestricted_Access.all.Busy
;
1143 -- The value of the Node component influences the behavior of the First
1144 -- and Last selector functions of the iterator object. When the Node
1145 -- component is null (as is the case here), this means the iterator
1146 -- object was constructed without a start expression. This is a
1147 -- complete iterator, meaning that the iteration starts from the
1148 -- (logical) beginning of the sequence of items.
1150 -- Note: For a forward iterator, Container.First is the beginning, and
1151 -- for a reverse iterator, Container.Last is the beginning.
1153 return It
: constant Iterator
:=
1154 Iterator
'(Limited_Controlled with
1155 Container => Container'Unrestricted_Access,
1162 function Iterate (Container : List; Start : Cursor)
1163 return List_Iterator_Interfaces.Reversible_Iterator'Class
1165 B : Natural renames Container'Unrestricted_Access.all.Busy;
1168 -- It was formerly the case that when Start = No_Element, the partial
1169 -- iterator was defined to behave the same as for a complete iterator,
1170 -- and iterate over the entire sequence of items. However, those
1171 -- semantics were unintuitive and arguably error-prone (it is too easy
1172 -- to accidentally create an endless loop), and so they were changed,
1173 -- per the ARG meeting in Denver on 2011/11. However, there was no
1174 -- consensus about what positive meaning this corner case should have,
1175 -- and so it was decided to simply raise an exception. This does imply,
1176 -- however, that it is not possible to use a partial iterator to specify
1177 -- an empty sequence of items.
1179 if Start = No_Element then
1180 raise Constraint_Error with
1181 "Start position for iterator equals No_Element";
1183 elsif Start.Container /= Container'Unrestricted_Access then
1184 raise Program_Error with
1185 "Start cursor of Iterate designates wrong list";
1188 pragma Assert (Vet (Start), "Start cursor of Iterate is bad");
1190 -- The value of the Node component influences the behavior of the
1191 -- First and Last selector functions of the iterator object. When
1192 -- the Node component is non-null (as is the case here), it means
1193 -- that this is a partial iteration, over a subset of the complete
1194 -- sequence of items. The iterator object was constructed with
1195 -- a start expression, indicating the position from which the
1196 -- iteration begins. Note that the start position has the same value
1197 -- irrespective of whether this is a forward or reverse iteration.
1199 return It : constant Iterator :=
1200 Iterator'(Limited_Controlled
with
1201 Container
=> Container
'Unrestricted_Access,
1213 function Last
(Container
: List
) return Cursor
is
1215 if Container
.Last
= null then
1218 return Cursor
'(Container'Unrestricted_Access, Container.Last);
1222 function Last (Object : Iterator) return Cursor is
1224 -- The value of the iterator object's Node component influences the
1225 -- behavior of the Last (and First) selector function.
1227 -- When the Node component is null, this means the iterator object was
1228 -- constructed without a start expression, in which case the (reverse)
1229 -- iteration starts from the (logical) beginning of the entire sequence
1230 -- (corresponding to Container.Last, for a reverse iterator).
1232 -- Otherwise, this is iteration over a partial sequence of items. When
1233 -- the Node component is non-null, the iterator object was constructed
1234 -- with a start expression, that specifies the position from which the
1235 -- (reverse) partial iteration begins.
1237 if Object.Node = null then
1238 return Doubly_Linked_Lists.Last (Object.Container.all);
1240 return Cursor'(Object
.Container
, Object
.Node
);
1248 function Last_Element
(Container
: List
) return Element_Type
is
1250 if Container
.Last
= null then
1251 raise Constraint_Error
with "list is empty";
1253 return Container
.Last
.Element
;
1261 function Length
(Container
: List
) return Count_Type
is
1263 return Container
.Length
;
1271 (Target
: in out List
;
1272 Source
: in out List
)
1275 if Target
'Address = Source
'Address then
1278 elsif Source
.Busy
> 0 then
1279 raise Program_Error
with
1280 "attempt to tamper with cursors of Source (list is busy)";
1285 Target
.First
:= Source
.First
;
1286 Source
.First
:= null;
1288 Target
.Last
:= Source
.Last
;
1289 Source
.Last
:= null;
1291 Target
.Length
:= Source
.Length
;
1300 procedure Next
(Position
: in out Cursor
) is
1302 Position
:= Next
(Position
);
1305 function Next
(Position
: Cursor
) return Cursor
is
1307 if Position
.Node
= null then
1311 pragma Assert
(Vet
(Position
), "bad cursor in Next");
1314 Next_Node
: constant Node_Access
:= Position
.Node
.Next
;
1316 if Next_Node
= null then
1319 return Cursor
'(Position.Container, Next_Node);
1327 Position : Cursor) return Cursor
1330 if Position.Container = null then
1332 elsif Position.Container /= Object.Container then
1333 raise Program_Error with
1334 "Position cursor of Next designates wrong list";
1336 return Next (Position);
1345 (Container : in out List;
1346 New_Item : Element_Type;
1347 Count : Count_Type := 1)
1350 Insert (Container, First (Container), New_Item, Count);
1357 procedure Previous (Position : in out Cursor) is
1359 Position := Previous (Position);
1362 function Previous (Position : Cursor) return Cursor is
1364 if Position.Node = null then
1368 pragma Assert (Vet (Position), "bad cursor in Previous");
1371 Prev_Node : constant Node_Access := Position.Node.Prev;
1373 if Prev_Node = null then
1376 return Cursor'(Position
.Container
, Prev_Node
);
1384 Position
: Cursor
) return Cursor
1387 if Position
.Container
= null then
1389 elsif Position
.Container
/= Object
.Container
then
1390 raise Program_Error
with
1391 "Position cursor of Previous designates wrong list";
1393 return Previous
(Position
);
1397 ----------------------
1398 -- Pseudo_Reference --
1399 ----------------------
1401 function Pseudo_Reference
1402 (Container
: aliased List
'Class) return Reference_Control_Type
1404 C
: constant List_Access
:= Container
'Unrestricted_Access;
1405 B
: Natural renames C
.Busy
;
1406 L
: Natural renames C
.Lock
;
1408 return R
: constant Reference_Control_Type
:=
1414 end Pseudo_Reference
;
1420 procedure Query_Element
1422 Process
: not null access procedure (Element
: Element_Type
))
1425 if Position
.Node
= null then
1426 raise Constraint_Error
with
1427 "Position cursor has no element";
1430 pragma Assert
(Vet
(Position
), "bad cursor in Query_Element");
1433 C
: List
renames Position
.Container
.all'Unrestricted_Access.all;
1434 B
: Natural renames C
.Busy
;
1435 L
: Natural renames C
.Lock
;
1442 Process
(Position
.Node
.Element
);
1460 (Stream
: not null access Root_Stream_Type
'Class;
1463 N
: Count_Type
'Base;
1468 Count_Type
'Base'Read (Stream, N);
1477 Element_Type'Read (Stream, X.Element);
1488 Item.Length := Item.Length + 1;
1489 exit when Item.Length = N;
1494 Element_Type'Read (Stream, X.Element);
1501 X.Prev := Item.Last;
1502 Item.Last.Next := X;
1508 (Stream : not null access Root_Stream_Type'Class;
1512 raise Program_Error with "attempt to stream list cursor";
1516 (Stream : not null access Root_Stream_Type'Class;
1517 Item : out Reference_Type)
1520 raise Program_Error with "attempt to stream reference";
1524 (Stream : not null access Root_Stream_Type'Class;
1525 Item : out Constant_Reference_Type)
1528 raise Program_Error with "attempt to stream reference";
1536 (Container : aliased in out List;
1537 Position : Cursor) return Reference_Type
1540 if Position.Container = null then
1541 raise Constraint_Error with "Position cursor has no element";
1543 elsif Position.Container /= Container'Unchecked_Access then
1544 raise Program_Error with
1545 "Position cursor designates wrong container";
1548 pragma Assert (Vet (Position), "bad cursor in function Reference");
1551 C : List renames Position.Container.all;
1552 B : Natural renames C.Busy;
1553 L : Natural renames C.Lock;
1555 return R : constant Reference_Type :=
1556 (Element => Position.Node.Element'Access,
1557 Control => (Controlled with Position.Container))
1566 ---------------------
1567 -- Replace_Element --
1568 ---------------------
1570 procedure Replace_Element
1571 (Container : in out List;
1573 New_Item : Element_Type)
1576 if Position.Container = null then
1577 raise Constraint_Error with "Position cursor has no element";
1579 elsif Position.Container /= Container'Unchecked_Access then
1580 raise Program_Error with
1581 "Position cursor designates wrong container";
1583 elsif Container.Lock > 0 then
1584 raise Program_Error with
1585 "attempt to tamper with elements (list is locked)";
1588 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
1590 Position.Node.Element := New_Item;
1592 end Replace_Element;
1594 ----------------------
1595 -- Reverse_Elements --
1596 ----------------------
1598 procedure Reverse_Elements (Container : in out List) is
1599 I : Node_Access := Container.First;
1600 J : Node_Access := Container.Last;
1602 procedure Swap (L, R : Node_Access);
1608 procedure Swap (L, R : Node_Access) is
1609 LN : constant Node_Access := L.Next;
1610 LP : constant Node_Access := L.Prev;
1612 RN : constant Node_Access := R.Next;
1613 RP : constant Node_Access := R.Prev;
1628 pragma Assert (RP = L);
1642 -- Start of processing for Reverse_Elements
1645 if Container.Length <= 1 then
1649 pragma Assert (Container.First.Prev = null);
1650 pragma Assert (Container.Last.Next = null);
1652 if Container.Busy > 0 then
1653 raise Program_Error with
1654 "attempt to tamper with cursors (list is busy)";
1657 Container.First := J;
1658 Container.Last := I;
1660 Swap (L => I, R => J);
1668 Swap (L => J, R => I);
1677 pragma Assert (Container.First.Prev = null);
1678 pragma Assert (Container.Last.Next = null);
1679 end Reverse_Elements;
1685 function Reverse_Find
1687 Item : Element_Type;
1688 Position : Cursor := No_Element) return Cursor
1690 Node : Node_Access := Position.Node;
1694 Node := Container.Last;
1697 if Position.Container /= Container'Unrestricted_Access then
1698 raise Program_Error with
1699 "Position cursor designates wrong container";
1701 pragma Assert (Vet (Position), "bad cursor in Reverse_Find");
1705 -- Per AI05-0022, the container implementation is required to detect
1706 -- element tampering by a generic actual subprogram.
1709 B : Natural renames Container'Unrestricted_Access.Busy;
1710 L : Natural renames Container'Unrestricted_Access.Lock;
1712 Result : Node_Access;
1719 while Node /= null loop
1720 if Node.Element = Item then
1731 if Result = null then
1734 return Cursor'(Container
'Unrestricted_Access, Result
);
1745 ---------------------
1746 -- Reverse_Iterate --
1747 ---------------------
1749 procedure Reverse_Iterate
1751 Process
: not null access procedure (Position
: Cursor
))
1753 C
: List
renames Container
'Unrestricted_Access.all;
1754 B
: Natural renames C
.Busy
;
1756 Node
: Node_Access
:= Container
.Last
;
1762 while Node
/= null loop
1763 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1773 end Reverse_Iterate;
1780 (Target : in out List;
1782 Source : in out List)
1785 if Before.Container /= null then
1786 if Before.Container /= Target'Unrestricted_Access then
1787 raise Program_Error with
1788 "Before cursor designates wrong container";
1790 pragma Assert (Vet (Before), "bad cursor in Splice");
1794 if Target'Address = Source'Address or else Source.Length = 0 then
1797 elsif Target.Length > Count_Type'Last - Source.Length then
1798 raise Constraint_Error with "new length exceeds maximum";
1800 elsif Target.Busy > 0 then
1801 raise Program_Error with
1802 "attempt to tamper with cursors of Target (list is busy)";
1804 elsif Source.Busy > 0 then
1805 raise Program_Error with
1806 "attempt to tamper with cursors of Source (list is busy)";
1809 Splice_Internal (Target, Before.Node, Source);
1814 (Container : in out List;
1819 if Before.Container /= null then
1820 if Before.Container /= Container'Unchecked_Access then
1821 raise Program_Error with
1822 "Before cursor designates wrong container";
1824 pragma Assert (Vet (Before), "bad Before cursor in Splice");
1828 if Position.Node = null then
1829 raise Constraint_Error with "Position cursor has no element";
1832 if Position.Container /= Container'Unrestricted_Access then
1833 raise Program_Error with
1834 "Position cursor designates wrong container";
1837 pragma Assert (Vet (Position), "bad Position cursor in Splice");
1839 if Position.Node = Before.Node
1840 or else Position.Node.Next = Before.Node
1845 pragma Assert (Container.Length >= 2);
1847 if Container.Busy > 0 then
1848 raise Program_Error with
1849 "attempt to tamper with cursors (list is busy)";
1852 if Before.Node = null then
1853 pragma Assert (Position.Node /= Container.Last);
1855 if Position.Node = Container.First then
1856 Container.First := Position.Node.Next;
1857 Container.First.Prev := null;
1859 Position.Node.Prev.Next := Position.Node.Next;
1860 Position.Node.Next.Prev := Position.Node.Prev;
1863 Container.Last.Next := Position.Node;
1864 Position.Node.Prev := Container.Last;
1866 Container.Last := Position.Node;
1867 Container.Last.Next := null;
1872 if Before.Node = Container.First then
1873 pragma Assert (Position.Node /= Container.First);
1875 if Position.Node = Container.Last then
1876 Container.Last := Position.Node.Prev;
1877 Container.Last.Next := null;
1879 Position.Node.Prev.Next := Position.Node.Next;
1880 Position.Node.Next.Prev := Position.Node.Prev;
1883 Container.First.Prev := Position.Node;
1884 Position.Node.Next := Container.First;
1886 Container.First := Position.Node;
1887 Container.First.Prev := null;
1892 if Position.Node = Container.First then
1893 Container.First := Position.Node.Next;
1894 Container.First.Prev := null;
1896 elsif Position.Node = Container.Last then
1897 Container.Last := Position.Node.Prev;
1898 Container.Last.Next := null;
1901 Position.Node.Prev.Next := Position.Node.Next;
1902 Position.Node.Next.Prev := Position.Node.Prev;
1905 Before.Node.Prev.Next := Position.Node;
1906 Position.Node.Prev := Before.Node.Prev;
1908 Before.Node.Prev := Position.Node;
1909 Position.Node.Next := Before.Node;
1911 pragma Assert (Container.First.Prev = null);
1912 pragma Assert (Container.Last.Next = null);
1916 (Target : in out List;
1918 Source : in out List;
1919 Position : in out Cursor)
1922 if Target'Address = Source'Address then
1923 Splice (Target, Before, Position);
1927 if Before.Container /= null then
1928 if Before.Container /= Target'Unrestricted_Access then
1929 raise Program_Error with
1930 "Before cursor designates wrong container";
1932 pragma Assert (Vet (Before), "bad Before cursor in Splice");
1936 if Position.Node = null then
1937 raise Constraint_Error with "Position cursor has no element";
1939 elsif Position.Container /= Source'Unrestricted_Access then
1940 raise Program_Error with
1941 "Position cursor designates wrong container";
1944 pragma Assert (Vet (Position), "bad Position cursor in Splice");
1946 if Target.Length = Count_Type'Last then
1947 raise Constraint_Error with "Target is full";
1949 elsif Target.Busy > 0 then
1950 raise Program_Error with
1951 "attempt to tamper with cursors of Target (list is busy)";
1953 elsif Source.Busy > 0 then
1954 raise Program_Error with
1955 "attempt to tamper with cursors of Source (list is busy)";
1958 Splice_Internal (Target, Before.Node, Source, Position.Node);
1959 Position.Container := Target'Unchecked_Access;
1964 ---------------------
1965 -- Splice_Internal --
1966 ---------------------
1968 procedure Splice_Internal
1969 (Target : in out List;
1970 Before : Node_Access;
1971 Source : in out List)
1974 -- This implements the corresponding Splice operation, after the
1975 -- parameters have been vetted, and corner-cases disposed of.
1977 pragma Assert (Target'Address /= Source'Address);
1978 pragma Assert (Source.Length > 0);
1979 pragma Assert (Source.First /= null);
1980 pragma Assert (Source.First.Prev = null);
1981 pragma Assert (Source.Last /= null);
1982 pragma Assert (Source.Last.Next = null);
1983 pragma Assert (Target.Length <= Count_Type'Last - Source.Length);
1985 if Target.Length = 0 then
1986 pragma Assert (Target.First = null);
1987 pragma Assert (Target.Last = null);
1988 pragma Assert (Before = null);
1990 Target.First := Source.First;
1991 Target.Last := Source.Last;
1993 elsif Before = null then
1994 pragma Assert (Target.Last.Next = null);
1996 Target.Last.Next := Source.First;
1997 Source.First.Prev := Target.Last;
1999 Target.Last := Source.Last;
2001 elsif Before = Target.First then
2002 pragma Assert (Target.First.Prev = null);
2004 Source.Last.Next := Target.First;
2005 Target.First.Prev := Source.Last;
2007 Target.First := Source.First;
2010 pragma Assert (Target.Length >= 2);
2012 Before.Prev.Next := Source.First;
2013 Source.First.Prev := Before.Prev;
2015 Before.Prev := Source.Last;
2016 Source.Last.Next := Before;
2019 Source.First := null;
2020 Source.Last := null;
2022 Target.Length := Target.Length + Source.Length;
2024 end Splice_Internal;
2026 procedure Splice_Internal
2027 (Target : in out List;
2028 Before : Node_Access; -- node of Target
2029 Source : in out List;
2030 Position : Node_Access) -- node of Source
2033 -- This implements the corresponding Splice operation, after the
2034 -- parameters have been vetted.
2036 pragma Assert (Target'Address /= Source'Address);
2037 pragma Assert (Target.Length < Count_Type'Last);
2038 pragma Assert (Source.Length > 0);
2039 pragma Assert (Source.First /= null);
2040 pragma Assert (Source.First.Prev = null);
2041 pragma Assert (Source.Last /= null);
2042 pragma Assert (Source.Last.Next = null);
2043 pragma Assert (Position /= null);
2045 if Position = Source.First then
2046 Source.First := Position.Next;
2048 if Position = Source.Last then
2049 pragma Assert (Source.First = null);
2050 pragma Assert (Source.Length = 1);
2051 Source.Last := null;
2054 Source.First.Prev := null;
2057 elsif Position = Source.Last then
2058 pragma Assert (Source.Length >= 2);
2059 Source.Last := Position.Prev;
2060 Source.Last.Next := null;
2063 pragma Assert (Source.Length >= 3);
2064 Position.Prev.Next := Position.Next;
2065 Position.Next.Prev := Position.Prev;
2068 if Target.Length = 0 then
2069 pragma Assert (Target.First = null);
2070 pragma Assert (Target.Last = null);
2071 pragma Assert (Before = null);
2073 Target.First := Position;
2074 Target.Last := Position;
2076 Target.First.Prev := null;
2077 Target.Last.Next := null;
2079 elsif Before = null then
2080 pragma Assert (Target.Last.Next = null);
2081 Target.Last.Next := Position;
2082 Position.Prev := Target.Last;
2084 Target.Last := Position;
2085 Target.Last.Next := null;
2087 elsif Before = Target.First then
2088 pragma Assert (Target.First.Prev = null);
2089 Target.First.Prev := Position;
2090 Position.Next := Target.First;
2092 Target.First := Position;
2093 Target.First.Prev := null;
2096 pragma Assert (Target.Length >= 2);
2097 Before.Prev.Next := Position;
2098 Position.Prev := Before.Prev;
2100 Before.Prev := Position;
2101 Position.Next := Before;
2104 Target.Length := Target.Length + 1;
2105 Source.Length := Source.Length - 1;
2106 end Splice_Internal;
2113 (Container : in out List;
2117 if I.Node = null then
2118 raise Constraint_Error with "I cursor has no element";
2121 if J.Node = null then
2122 raise Constraint_Error with "J cursor has no element";
2125 if I.Container /= Container'Unchecked_Access then
2126 raise Program_Error with "I cursor designates wrong container";
2129 if J.Container /= Container'Unchecked_Access then
2130 raise Program_Error with "J cursor designates wrong container";
2133 if I.Node = J.Node then
2137 if Container.Lock > 0 then
2138 raise Program_Error with
2139 "attempt to tamper with elements (list is locked)";
2142 pragma Assert (Vet (I), "bad I cursor in Swap");
2143 pragma Assert (Vet (J), "bad J cursor in Swap");
2146 EI : Element_Type renames I.Node.Element;
2147 EJ : Element_Type renames J.Node.Element;
2149 EI_Copy : constant Element_Type := EI;
2161 procedure Swap_Links
2162 (Container : in out List;
2166 if I.Node = null then
2167 raise Constraint_Error with "I cursor has no element";
2170 if J.Node = null then
2171 raise Constraint_Error with "J cursor has no element";
2174 if I.Container /= Container'Unrestricted_Access then
2175 raise Program_Error with "I cursor designates wrong container";
2178 if J.Container /= Container'Unrestricted_Access then
2179 raise Program_Error with "J cursor designates wrong container";
2182 if I.Node = J.Node then
2186 if Container.Busy > 0 then
2187 raise Program_Error with
2188 "attempt to tamper with cursors (list is busy)";
2191 pragma Assert (Vet (I), "bad I cursor in Swap_Links");
2192 pragma Assert (Vet (J), "bad J cursor in Swap_Links");
2195 I_Next : constant Cursor := Next (I);
2199 Splice (Container, Before => I, Position => J);
2203 J_Next : constant Cursor := Next (J);
2207 Splice (Container, Before => J, Position => I);
2210 pragma Assert (Container.Length >= 3);
2212 Splice (Container, Before => I_Next, Position => J);
2213 Splice (Container, Before => J_Next, Position => I);
2220 --------------------
2221 -- Update_Element --
2222 --------------------
2224 procedure Update_Element
2225 (Container : in out List;
2227 Process : not null access procedure (Element : in out Element_Type))
2230 if Position.Node = null then
2231 raise Constraint_Error with "Position cursor has no element";
2233 elsif Position.Container /= Container'Unchecked_Access then
2234 raise Program_Error with
2235 "Position cursor designates wrong container";
2238 pragma Assert (Vet (Position), "bad cursor in Update_Element");
2241 B : Natural renames Container.Busy;
2242 L : Natural renames Container.Lock;
2249 Process (Position.Node.Element);
2267 function Vet (Position : Cursor) return Boolean is
2269 if Position.Node = null then
2270 return Position.Container = null;
2273 if Position.Container = null then
2277 -- An invariant of a node is that its Previous and Next components can
2278 -- be null, or designate a different node. Operation Free sets the
2279 -- access value components of the node to designate the node itself
2280 -- before actually deallocating the node, thus deliberately violating
2281 -- the node invariant. This gives us a simple way to detect a dangling
2282 -- reference to a node.
2284 if Position.Node.Next = Position.Node then
2288 if Position.Node.Prev = Position.Node then
2292 -- In practice the tests above will detect most instances of a dangling
2293 -- reference. If we get here, it means that the invariants of the
2294 -- designated node are satisfied (they at least appear to be satisfied),
2295 -- so we perform some more tests, to determine whether invariants of the
2296 -- designated list are satisfied too.
2299 L : List renames Position.Container.all;
2302 if L.Length = 0 then
2306 if L.First = null then
2310 if L.Last = null then
2314 if L.First.Prev /= null then
2318 if L.Last.Next /= null then
2322 if Position.Node.Prev = null and then Position.Node /= L.First then
2327 (Position.Node.Prev /= null or else Position.Node = L.First);
2329 if Position.Node.Next = null and then Position.Node /= L.Last then
2334 (Position.Node.Next /= null
2335 or else Position.Node = L.Last);
2337 if L.Length = 1 then
2338 return L.First = L.Last;
2341 if L.First = L.Last then
2345 if L.First.Next = null then
2349 if L.Last.Prev = null then
2353 if L.First.Next.Prev /= L.First then
2357 if L.Last.Prev.Next /= L.Last then
2361 if L.Length = 2 then
2362 if L.First.Next /= L.Last then
2364 elsif L.Last.Prev /= L.First then
2371 if L.First.Next = L.Last then
2375 if L.Last.Prev = L.First then
2379 -- Eliminate earlier possibility
2381 if Position.Node = L.First then
2385 pragma Assert (Position.Node.Prev /= null);
2387 -- Eliminate earlier possibility
2389 if Position.Node = L.Last then
2393 pragma Assert (Position.Node.Next /= null);
2395 if Position.Node.Next.Prev /= Position.Node then
2399 if Position.Node.Prev.Next /= Position.Node then
2403 if L.Length = 3 then
2404 if L.First.Next /= Position.Node then
2406 elsif L.Last.Prev /= Position.Node then
2420 (Stream : not null access Root_Stream_Type'Class;
2426 Count_Type'Base'Write
(Stream
, Item
.Length
);
2429 while Node
/= null loop
2430 Element_Type
'Write (Stream
, Node
.Element
);
2436 (Stream
: not null access Root_Stream_Type
'Class;
2440 raise Program_Error
with "attempt to stream list cursor";
2444 (Stream
: not null access Root_Stream_Type
'Class;
2445 Item
: Reference_Type
)
2448 raise Program_Error
with "attempt to stream reference";
2452 (Stream
: not null access Root_Stream_Type
'Class;
2453 Item
: Constant_Reference_Type
)
2456 raise Program_Error
with "attempt to stream reference";
2459 end Ada
.Containers
.Doubly_Linked_Lists
;