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-2014, 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);
930 function Has_Element (Position : Cursor) return Boolean is
932 pragma Assert (Vet (Position), "bad cursor in Has_Element");
933 return Position.Node /= null;
941 (Container : in out List;
943 New_Item : Element_Type;
944 Position : out Cursor;
945 Count : Count_Type := 1)
947 First_Node : Node_Access;
948 New_Node : Node_Access;
951 if Before.Container /= null then
952 if Before.Container /= Container'Unrestricted_Access then
953 raise Program_Error with
954 "Before cursor designates wrong list";
956 pragma Assert (Vet (Before), "bad cursor in Insert");
964 elsif Container.Length > Count_Type'Last - Count then
965 raise Constraint_Error with "new length exceeds maximum";
967 elsif Container.Busy > 0 then
968 raise Program_Error with
969 "attempt to tamper with cursors (list is busy)";
972 New_Node := new Node_Type'(New_Item
, null, null);
973 First_Node
:= New_Node
;
974 Insert_Internal
(Container
, Before
.Node
, New_Node
);
976 for J
in 2 .. Count
loop
977 New_Node
:= new Node_Type
'(New_Item, null, null);
978 Insert_Internal (Container, Before.Node, New_Node);
981 Position := Cursor'(Container
'Unchecked_Access, First_Node
);
986 (Container
: in out List
;
988 New_Item
: Element_Type
;
989 Count
: Count_Type
:= 1)
992 pragma Unreferenced
(Position
);
994 Insert
(Container
, Before
, New_Item
, Position
, Count
);
998 (Container
: in out List
;
1000 Position
: out Cursor
;
1001 Count
: Count_Type
:= 1)
1003 First_Node
: Node_Access
;
1004 New_Node
: Node_Access
;
1007 if Before
.Container
/= null then
1008 if Before
.Container
/= Container
'Unrestricted_Access then
1009 raise Program_Error
with
1010 "Before cursor designates wrong list";
1012 pragma Assert
(Vet
(Before
), "bad cursor in Insert");
1021 if Container
.Length
> Count_Type
'Last - Count
then
1022 raise Constraint_Error
with "new length exceeds maximum";
1024 elsif Container
.Busy
> 0 then
1025 raise Program_Error
with
1026 "attempt to tamper with cursors (list is busy)";
1029 New_Node
:= new Node_Type
;
1030 First_Node
:= New_Node
;
1031 Insert_Internal
(Container
, Before
.Node
, New_Node
);
1033 for J
in 2 .. Count
loop
1034 New_Node
:= new Node_Type
;
1035 Insert_Internal
(Container
, Before
.Node
, New_Node
);
1038 Position
:= Cursor
'(Container'Unchecked_Access, First_Node);
1042 ---------------------
1043 -- Insert_Internal --
1044 ---------------------
1046 procedure Insert_Internal
1047 (Container : in out List;
1048 Before : Node_Access;
1049 New_Node : Node_Access)
1052 if Container.Length = 0 then
1053 pragma Assert (Before = null);
1054 pragma Assert (Container.First = null);
1055 pragma Assert (Container.Last = null);
1057 Container.First := New_Node;
1058 Container.Last := New_Node;
1060 elsif Before = null then
1061 pragma Assert (Container.Last.Next = null);
1063 Container.Last.Next := New_Node;
1064 New_Node.Prev := Container.Last;
1066 Container.Last := New_Node;
1068 elsif Before = Container.First then
1069 pragma Assert (Container.First.Prev = null);
1071 Container.First.Prev := New_Node;
1072 New_Node.Next := Container.First;
1074 Container.First := New_Node;
1077 pragma Assert (Container.First.Prev = null);
1078 pragma Assert (Container.Last.Next = null);
1080 New_Node.Next := Before;
1081 New_Node.Prev := Before.Prev;
1083 Before.Prev.Next := New_Node;
1084 Before.Prev := New_Node;
1087 Container.Length := Container.Length + 1;
1088 end Insert_Internal;
1094 function Is_Empty (Container : List) return Boolean is
1096 return Container.Length = 0;
1105 Process : not null access procedure (Position : Cursor))
1107 B : Natural renames Container'Unrestricted_Access.all.Busy;
1108 Node : Node_Access := Container.First;
1114 while Node /= null loop
1115 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1127 function Iterate
(Container
: List
)
1128 return List_Iterator_Interfaces
.Reversible_Iterator
'Class
1130 B
: Natural renames Container
'Unrestricted_Access.all.Busy
;
1133 -- The value of the Node component influences the behavior of the First
1134 -- and Last selector functions of the iterator object. When the Node
1135 -- component is null (as is the case here), this means the iterator
1136 -- object was constructed without a start expression. This is a
1137 -- complete iterator, meaning that the iteration starts from the
1138 -- (logical) beginning of the sequence of items.
1140 -- Note: For a forward iterator, Container.First is the beginning, and
1141 -- for a reverse iterator, Container.Last is the beginning.
1143 return It
: constant Iterator
:=
1144 Iterator
'(Limited_Controlled with
1145 Container => Container'Unrestricted_Access,
1152 function Iterate (Container : List; Start : Cursor)
1153 return List_Iterator_Interfaces.Reversible_Iterator'Class
1155 B : Natural renames Container'Unrestricted_Access.all.Busy;
1158 -- It was formerly the case that when Start = No_Element, the partial
1159 -- iterator was defined to behave the same as for a complete iterator,
1160 -- and iterate over the entire sequence of items. However, those
1161 -- semantics were unintuitive and arguably error-prone (it is too easy
1162 -- to accidentally create an endless loop), and so they were changed,
1163 -- per the ARG meeting in Denver on 2011/11. However, there was no
1164 -- consensus about what positive meaning this corner case should have,
1165 -- and so it was decided to simply raise an exception. This does imply,
1166 -- however, that it is not possible to use a partial iterator to specify
1167 -- an empty sequence of items.
1169 if Start = No_Element then
1170 raise Constraint_Error with
1171 "Start position for iterator equals No_Element";
1173 elsif Start.Container /= Container'Unrestricted_Access then
1174 raise Program_Error with
1175 "Start cursor of Iterate designates wrong list";
1178 pragma Assert (Vet (Start), "Start cursor of Iterate is bad");
1180 -- The value of the Node component influences the behavior of the
1181 -- First and Last selector functions of the iterator object. When
1182 -- the Node component is non-null (as is the case here), it means
1183 -- that this is a partial iteration, over a subset of the complete
1184 -- sequence of items. The iterator object was constructed with
1185 -- a start expression, indicating the position from which the
1186 -- iteration begins. Note that the start position has the same value
1187 -- irrespective of whether this is a forward or reverse iteration.
1189 return It : constant Iterator :=
1190 Iterator'(Limited_Controlled
with
1191 Container
=> Container
'Unrestricted_Access,
1203 function Last
(Container
: List
) return Cursor
is
1205 if Container
.Last
= null then
1208 return Cursor
'(Container'Unrestricted_Access, Container.Last);
1212 function Last (Object : Iterator) return Cursor is
1214 -- The value of the iterator object's Node component influences the
1215 -- behavior of the Last (and First) selector function.
1217 -- When the Node component is null, this means the iterator object was
1218 -- constructed without a start expression, in which case the (reverse)
1219 -- iteration starts from the (logical) beginning of the entire sequence
1220 -- (corresponding to Container.Last, for a reverse iterator).
1222 -- Otherwise, this is iteration over a partial sequence of items. When
1223 -- the Node component is non-null, the iterator object was constructed
1224 -- with a start expression, that specifies the position from which the
1225 -- (reverse) partial iteration begins.
1227 if Object.Node = null then
1228 return Doubly_Linked_Lists.Last (Object.Container.all);
1230 return Cursor'(Object
.Container
, Object
.Node
);
1238 function Last_Element
(Container
: List
) return Element_Type
is
1240 if Container
.Last
= null then
1241 raise Constraint_Error
with "list is empty";
1243 return Container
.Last
.Element
;
1251 function Length
(Container
: List
) return Count_Type
is
1253 return Container
.Length
;
1261 (Target
: in out List
;
1262 Source
: in out List
)
1265 if Target
'Address = Source
'Address then
1268 elsif Source
.Busy
> 0 then
1269 raise Program_Error
with
1270 "attempt to tamper with cursors of Source (list is busy)";
1275 Target
.First
:= Source
.First
;
1276 Source
.First
:= null;
1278 Target
.Last
:= Source
.Last
;
1279 Source
.Last
:= null;
1281 Target
.Length
:= Source
.Length
;
1290 procedure Next
(Position
: in out Cursor
) is
1292 Position
:= Next
(Position
);
1295 function Next
(Position
: Cursor
) return Cursor
is
1297 if Position
.Node
= null then
1301 pragma Assert
(Vet
(Position
), "bad cursor in Next");
1304 Next_Node
: constant Node_Access
:= Position
.Node
.Next
;
1306 if Next_Node
= null then
1309 return Cursor
'(Position.Container, Next_Node);
1317 Position : Cursor) return Cursor
1320 if Position.Container = null then
1322 elsif Position.Container /= Object.Container then
1323 raise Program_Error with
1324 "Position cursor of Next designates wrong list";
1326 return Next (Position);
1335 (Container : in out List;
1336 New_Item : Element_Type;
1337 Count : Count_Type := 1)
1340 Insert (Container, First (Container), New_Item, Count);
1347 procedure Previous (Position : in out Cursor) is
1349 Position := Previous (Position);
1352 function Previous (Position : Cursor) return Cursor is
1354 if Position.Node = null then
1358 pragma Assert (Vet (Position), "bad cursor in Previous");
1361 Prev_Node : constant Node_Access := Position.Node.Prev;
1363 if Prev_Node = null then
1366 return Cursor'(Position
.Container
, Prev_Node
);
1374 Position
: Cursor
) return Cursor
1377 if Position
.Container
= null then
1379 elsif Position
.Container
/= Object
.Container
then
1380 raise Program_Error
with
1381 "Position cursor of Previous designates wrong list";
1383 return Previous
(Position
);
1391 procedure Query_Element
1393 Process
: not null access procedure (Element
: Element_Type
))
1396 if Position
.Node
= null then
1397 raise Constraint_Error
with
1398 "Position cursor has no element";
1401 pragma Assert
(Vet
(Position
), "bad cursor in Query_Element");
1404 C
: List
renames Position
.Container
.all'Unrestricted_Access.all;
1405 B
: Natural renames C
.Busy
;
1406 L
: Natural renames C
.Lock
;
1413 Process
(Position
.Node
.Element
);
1431 (Stream
: not null access Root_Stream_Type
'Class;
1434 N
: Count_Type
'Base;
1439 Count_Type
'Base'Read (Stream, N);
1448 Element_Type'Read (Stream, X.Element);
1459 Item.Length := Item.Length + 1;
1460 exit when Item.Length = N;
1465 Element_Type'Read (Stream, X.Element);
1472 X.Prev := Item.Last;
1473 Item.Last.Next := X;
1479 (Stream : not null access Root_Stream_Type'Class;
1483 raise Program_Error with "attempt to stream list cursor";
1487 (Stream : not null access Root_Stream_Type'Class;
1488 Item : out Reference_Type)
1491 raise Program_Error with "attempt to stream reference";
1495 (Stream : not null access Root_Stream_Type'Class;
1496 Item : out Constant_Reference_Type)
1499 raise Program_Error with "attempt to stream reference";
1507 (Container : aliased in out List;
1508 Position : Cursor) return Reference_Type
1511 if Position.Container = null then
1512 raise Constraint_Error with "Position cursor has no element";
1514 elsif Position.Container /= Container'Unchecked_Access then
1515 raise Program_Error with
1516 "Position cursor designates wrong container";
1519 pragma Assert (Vet (Position), "bad cursor in function Reference");
1522 C : List renames Position.Container.all;
1523 B : Natural renames C.Busy;
1524 L : Natural renames C.Lock;
1526 return R : constant Reference_Type :=
1527 (Element => Position.Node.Element'Access,
1528 Control => (Controlled with Position.Container))
1537 ---------------------
1538 -- Replace_Element --
1539 ---------------------
1541 procedure Replace_Element
1542 (Container : in out List;
1544 New_Item : Element_Type)
1547 if Position.Container = null then
1548 raise Constraint_Error with "Position cursor has no element";
1550 elsif Position.Container /= Container'Unchecked_Access then
1551 raise Program_Error with
1552 "Position cursor designates wrong container";
1554 elsif Container.Lock > 0 then
1555 raise Program_Error with
1556 "attempt to tamper with elements (list is locked)";
1559 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
1561 Position.Node.Element := New_Item;
1563 end Replace_Element;
1565 ----------------------
1566 -- Reverse_Elements --
1567 ----------------------
1569 procedure Reverse_Elements (Container : in out List) is
1570 I : Node_Access := Container.First;
1571 J : Node_Access := Container.Last;
1573 procedure Swap (L, R : Node_Access);
1579 procedure Swap (L, R : Node_Access) is
1580 LN : constant Node_Access := L.Next;
1581 LP : constant Node_Access := L.Prev;
1583 RN : constant Node_Access := R.Next;
1584 RP : constant Node_Access := R.Prev;
1599 pragma Assert (RP = L);
1613 -- Start of processing for Reverse_Elements
1616 if Container.Length <= 1 then
1620 pragma Assert (Container.First.Prev = null);
1621 pragma Assert (Container.Last.Next = null);
1623 if Container.Busy > 0 then
1624 raise Program_Error with
1625 "attempt to tamper with cursors (list is busy)";
1628 Container.First := J;
1629 Container.Last := I;
1631 Swap (L => I, R => J);
1639 Swap (L => J, R => I);
1648 pragma Assert (Container.First.Prev = null);
1649 pragma Assert (Container.Last.Next = null);
1650 end Reverse_Elements;
1656 function Reverse_Find
1658 Item : Element_Type;
1659 Position : Cursor := No_Element) return Cursor
1661 Node : Node_Access := Position.Node;
1665 Node := Container.Last;
1668 if Position.Container /= Container'Unrestricted_Access then
1669 raise Program_Error with
1670 "Position cursor designates wrong container";
1672 pragma Assert (Vet (Position), "bad cursor in Reverse_Find");
1676 -- Per AI05-0022, the container implementation is required to detect
1677 -- element tampering by a generic actual subprogram.
1680 B : Natural renames Container'Unrestricted_Access.Busy;
1681 L : Natural renames Container'Unrestricted_Access.Lock;
1683 Result : Node_Access;
1690 while Node /= null loop
1691 if Node.Element = Item then
1702 if Result = null then
1705 return Cursor'(Container
'Unrestricted_Access, Result
);
1716 ---------------------
1717 -- Reverse_Iterate --
1718 ---------------------
1720 procedure Reverse_Iterate
1722 Process
: not null access procedure (Position
: Cursor
))
1724 C
: List
renames Container
'Unrestricted_Access.all;
1725 B
: Natural renames C
.Busy
;
1727 Node
: Node_Access
:= Container
.Last
;
1733 while Node
/= null loop
1734 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1744 end Reverse_Iterate;
1751 (Target : in out List;
1753 Source : in out List)
1756 if Before.Container /= null then
1757 if Before.Container /= Target'Unrestricted_Access then
1758 raise Program_Error with
1759 "Before cursor designates wrong container";
1761 pragma Assert (Vet (Before), "bad cursor in Splice");
1765 if Target'Address = Source'Address or else Source.Length = 0 then
1768 elsif Target.Length > Count_Type'Last - Source.Length then
1769 raise Constraint_Error with "new length exceeds maximum";
1771 elsif Target.Busy > 0 then
1772 raise Program_Error with
1773 "attempt to tamper with cursors of Target (list is busy)";
1775 elsif Source.Busy > 0 then
1776 raise Program_Error with
1777 "attempt to tamper with cursors of Source (list is busy)";
1780 Splice_Internal (Target, Before.Node, Source);
1785 (Container : in out List;
1790 if Before.Container /= null then
1791 if Before.Container /= Container'Unchecked_Access then
1792 raise Program_Error with
1793 "Before cursor designates wrong container";
1795 pragma Assert (Vet (Before), "bad Before cursor in Splice");
1799 if Position.Node = null then
1800 raise Constraint_Error with "Position cursor has no element";
1803 if Position.Container /= Container'Unrestricted_Access then
1804 raise Program_Error with
1805 "Position cursor designates wrong container";
1808 pragma Assert (Vet (Position), "bad Position cursor in Splice");
1810 if Position.Node = Before.Node
1811 or else Position.Node.Next = Before.Node
1816 pragma Assert (Container.Length >= 2);
1818 if Container.Busy > 0 then
1819 raise Program_Error with
1820 "attempt to tamper with cursors (list is busy)";
1823 if Before.Node = null then
1824 pragma Assert (Position.Node /= Container.Last);
1826 if Position.Node = Container.First then
1827 Container.First := Position.Node.Next;
1828 Container.First.Prev := null;
1830 Position.Node.Prev.Next := Position.Node.Next;
1831 Position.Node.Next.Prev := Position.Node.Prev;
1834 Container.Last.Next := Position.Node;
1835 Position.Node.Prev := Container.Last;
1837 Container.Last := Position.Node;
1838 Container.Last.Next := null;
1843 if Before.Node = Container.First then
1844 pragma Assert (Position.Node /= Container.First);
1846 if Position.Node = Container.Last then
1847 Container.Last := Position.Node.Prev;
1848 Container.Last.Next := null;
1850 Position.Node.Prev.Next := Position.Node.Next;
1851 Position.Node.Next.Prev := Position.Node.Prev;
1854 Container.First.Prev := Position.Node;
1855 Position.Node.Next := Container.First;
1857 Container.First := Position.Node;
1858 Container.First.Prev := null;
1863 if Position.Node = Container.First then
1864 Container.First := Position.Node.Next;
1865 Container.First.Prev := null;
1867 elsif Position.Node = Container.Last then
1868 Container.Last := Position.Node.Prev;
1869 Container.Last.Next := null;
1872 Position.Node.Prev.Next := Position.Node.Next;
1873 Position.Node.Next.Prev := Position.Node.Prev;
1876 Before.Node.Prev.Next := Position.Node;
1877 Position.Node.Prev := Before.Node.Prev;
1879 Before.Node.Prev := Position.Node;
1880 Position.Node.Next := Before.Node;
1882 pragma Assert (Container.First.Prev = null);
1883 pragma Assert (Container.Last.Next = null);
1887 (Target : in out List;
1889 Source : in out List;
1890 Position : in out Cursor)
1893 if Target'Address = Source'Address then
1894 Splice (Target, Before, Position);
1898 if Before.Container /= null then
1899 if Before.Container /= Target'Unrestricted_Access then
1900 raise Program_Error with
1901 "Before cursor designates wrong container";
1903 pragma Assert (Vet (Before), "bad Before cursor in Splice");
1907 if Position.Node = null then
1908 raise Constraint_Error with "Position cursor has no element";
1910 elsif Position.Container /= Source'Unrestricted_Access then
1911 raise Program_Error with
1912 "Position cursor designates wrong container";
1915 pragma Assert (Vet (Position), "bad Position cursor in Splice");
1917 if Target.Length = Count_Type'Last then
1918 raise Constraint_Error with "Target is full";
1920 elsif Target.Busy > 0 then
1921 raise Program_Error with
1922 "attempt to tamper with cursors of Target (list is busy)";
1924 elsif Source.Busy > 0 then
1925 raise Program_Error with
1926 "attempt to tamper with cursors of Source (list is busy)";
1929 Splice_Internal (Target, Before.Node, Source, Position.Node);
1930 Position.Container := Target'Unchecked_Access;
1935 ---------------------
1936 -- Splice_Internal --
1937 ---------------------
1939 procedure Splice_Internal
1940 (Target : in out List;
1941 Before : Node_Access;
1942 Source : in out List)
1945 -- This implements the corresponding Splice operation, after the
1946 -- parameters have been vetted, and corner-cases disposed of.
1948 pragma Assert (Target'Address /= Source'Address);
1949 pragma Assert (Source.Length > 0);
1950 pragma Assert (Source.First /= null);
1951 pragma Assert (Source.First.Prev = null);
1952 pragma Assert (Source.Last /= null);
1953 pragma Assert (Source.Last.Next = null);
1954 pragma Assert (Target.Length <= Count_Type'Last - Source.Length);
1956 if Target.Length = 0 then
1957 pragma Assert (Target.First = null);
1958 pragma Assert (Target.Last = null);
1959 pragma Assert (Before = null);
1961 Target.First := Source.First;
1962 Target.Last := Source.Last;
1964 elsif Before = null then
1965 pragma Assert (Target.Last.Next = null);
1967 Target.Last.Next := Source.First;
1968 Source.First.Prev := Target.Last;
1970 Target.Last := Source.Last;
1972 elsif Before = Target.First then
1973 pragma Assert (Target.First.Prev = null);
1975 Source.Last.Next := Target.First;
1976 Target.First.Prev := Source.Last;
1978 Target.First := Source.First;
1981 pragma Assert (Target.Length >= 2);
1983 Before.Prev.Next := Source.First;
1984 Source.First.Prev := Before.Prev;
1986 Before.Prev := Source.Last;
1987 Source.Last.Next := Before;
1990 Source.First := null;
1991 Source.Last := null;
1993 Target.Length := Target.Length + Source.Length;
1995 end Splice_Internal;
1997 procedure Splice_Internal
1998 (Target : in out List;
1999 Before : Node_Access; -- node of Target
2000 Source : in out List;
2001 Position : Node_Access) -- node of Source
2004 -- This implements the corresponding Splice operation, after the
2005 -- parameters have been vetted.
2007 pragma Assert (Target'Address /= Source'Address);
2008 pragma Assert (Target.Length < Count_Type'Last);
2009 pragma Assert (Source.Length > 0);
2010 pragma Assert (Source.First /= null);
2011 pragma Assert (Source.First.Prev = null);
2012 pragma Assert (Source.Last /= null);
2013 pragma Assert (Source.Last.Next = null);
2014 pragma Assert (Position /= null);
2016 if Position = Source.First then
2017 Source.First := Position.Next;
2019 if Position = Source.Last then
2020 pragma Assert (Source.First = null);
2021 pragma Assert (Source.Length = 1);
2022 Source.Last := null;
2025 Source.First.Prev := null;
2028 elsif Position = Source.Last then
2029 pragma Assert (Source.Length >= 2);
2030 Source.Last := Position.Prev;
2031 Source.Last.Next := null;
2034 pragma Assert (Source.Length >= 3);
2035 Position.Prev.Next := Position.Next;
2036 Position.Next.Prev := Position.Prev;
2039 if Target.Length = 0 then
2040 pragma Assert (Target.First = null);
2041 pragma Assert (Target.Last = null);
2042 pragma Assert (Before = null);
2044 Target.First := Position;
2045 Target.Last := Position;
2047 Target.First.Prev := null;
2048 Target.Last.Next := null;
2050 elsif Before = null then
2051 pragma Assert (Target.Last.Next = null);
2052 Target.Last.Next := Position;
2053 Position.Prev := Target.Last;
2055 Target.Last := Position;
2056 Target.Last.Next := null;
2058 elsif Before = Target.First then
2059 pragma Assert (Target.First.Prev = null);
2060 Target.First.Prev := Position;
2061 Position.Next := Target.First;
2063 Target.First := Position;
2064 Target.First.Prev := null;
2067 pragma Assert (Target.Length >= 2);
2068 Before.Prev.Next := Position;
2069 Position.Prev := Before.Prev;
2071 Before.Prev := Position;
2072 Position.Next := Before;
2075 Target.Length := Target.Length + 1;
2076 Source.Length := Source.Length - 1;
2077 end Splice_Internal;
2084 (Container : in out List;
2088 if I.Node = null then
2089 raise Constraint_Error with "I cursor has no element";
2092 if J.Node = null then
2093 raise Constraint_Error with "J cursor has no element";
2096 if I.Container /= Container'Unchecked_Access then
2097 raise Program_Error with "I cursor designates wrong container";
2100 if J.Container /= Container'Unchecked_Access then
2101 raise Program_Error with "J cursor designates wrong container";
2104 if I.Node = J.Node then
2108 if Container.Lock > 0 then
2109 raise Program_Error with
2110 "attempt to tamper with elements (list is locked)";
2113 pragma Assert (Vet (I), "bad I cursor in Swap");
2114 pragma Assert (Vet (J), "bad J cursor in Swap");
2117 EI : Element_Type renames I.Node.Element;
2118 EJ : Element_Type renames J.Node.Element;
2120 EI_Copy : constant Element_Type := EI;
2132 procedure Swap_Links
2133 (Container : in out List;
2137 if I.Node = null then
2138 raise Constraint_Error with "I cursor has no element";
2141 if J.Node = null then
2142 raise Constraint_Error with "J cursor has no element";
2145 if I.Container /= Container'Unrestricted_Access then
2146 raise Program_Error with "I cursor designates wrong container";
2149 if J.Container /= Container'Unrestricted_Access then
2150 raise Program_Error with "J cursor designates wrong container";
2153 if I.Node = J.Node then
2157 if Container.Busy > 0 then
2158 raise Program_Error with
2159 "attempt to tamper with cursors (list is busy)";
2162 pragma Assert (Vet (I), "bad I cursor in Swap_Links");
2163 pragma Assert (Vet (J), "bad J cursor in Swap_Links");
2166 I_Next : constant Cursor := Next (I);
2170 Splice (Container, Before => I, Position => J);
2174 J_Next : constant Cursor := Next (J);
2178 Splice (Container, Before => J, Position => I);
2181 pragma Assert (Container.Length >= 3);
2183 Splice (Container, Before => I_Next, Position => J);
2184 Splice (Container, Before => J_Next, Position => I);
2191 --------------------
2192 -- Update_Element --
2193 --------------------
2195 procedure Update_Element
2196 (Container : in out List;
2198 Process : not null access procedure (Element : in out Element_Type))
2201 if Position.Node = null then
2202 raise Constraint_Error with "Position cursor has no element";
2204 elsif Position.Container /= Container'Unchecked_Access then
2205 raise Program_Error with
2206 "Position cursor designates wrong container";
2209 pragma Assert (Vet (Position), "bad cursor in Update_Element");
2212 B : Natural renames Container.Busy;
2213 L : Natural renames Container.Lock;
2220 Process (Position.Node.Element);
2238 function Vet (Position : Cursor) return Boolean is
2240 if Position.Node = null then
2241 return Position.Container = null;
2244 if Position.Container = null then
2248 -- An invariant of a node is that its Previous and Next components can
2249 -- be null, or designate a different node. Operation Free sets the
2250 -- access value components of the node to designate the node itself
2251 -- before actually deallocating the node, thus deliberately violating
2252 -- the node invariant. This gives us a simple way to detect a dangling
2253 -- reference to a node.
2255 if Position.Node.Next = Position.Node then
2259 if Position.Node.Prev = Position.Node then
2263 -- In practice the tests above will detect most instances of a dangling
2264 -- reference. If we get here, it means that the invariants of the
2265 -- designated node are satisfied (they at least appear to be satisfied),
2266 -- so we perform some more tests, to determine whether invariants of the
2267 -- designated list are satisfied too.
2270 L : List renames Position.Container.all;
2273 if L.Length = 0 then
2277 if L.First = null then
2281 if L.Last = null then
2285 if L.First.Prev /= null then
2289 if L.Last.Next /= null then
2293 if Position.Node.Prev = null and then Position.Node /= L.First then
2298 (Position.Node.Prev /= null or else Position.Node = L.First);
2300 if Position.Node.Next = null and then Position.Node /= L.Last then
2305 (Position.Node.Next /= null
2306 or else Position.Node = L.Last);
2308 if L.Length = 1 then
2309 return L.First = L.Last;
2312 if L.First = L.Last then
2316 if L.First.Next = null then
2320 if L.Last.Prev = null then
2324 if L.First.Next.Prev /= L.First then
2328 if L.Last.Prev.Next /= L.Last then
2332 if L.Length = 2 then
2333 if L.First.Next /= L.Last then
2335 elsif L.Last.Prev /= L.First then
2342 if L.First.Next = L.Last then
2346 if L.Last.Prev = L.First then
2350 -- Eliminate earlier possibility
2352 if Position.Node = L.First then
2356 pragma Assert (Position.Node.Prev /= null);
2358 -- Eliminate earlier possibility
2360 if Position.Node = L.Last then
2364 pragma Assert (Position.Node.Next /= null);
2366 if Position.Node.Next.Prev /= Position.Node then
2370 if Position.Node.Prev.Next /= Position.Node then
2374 if L.Length = 3 then
2375 if L.First.Next /= Position.Node then
2377 elsif L.Last.Prev /= Position.Node then
2391 (Stream : not null access Root_Stream_Type'Class;
2397 Count_Type'Base'Write
(Stream
, Item
.Length
);
2400 while Node
/= null loop
2401 Element_Type
'Write (Stream
, Node
.Element
);
2407 (Stream
: not null access Root_Stream_Type
'Class;
2411 raise Program_Error
with "attempt to stream list cursor";
2415 (Stream
: not null access Root_Stream_Type
'Class;
2416 Item
: Reference_Type
)
2419 raise Program_Error
with "attempt to stream reference";
2423 (Stream
: not null access Root_Stream_Type
'Class;
2424 Item
: Constant_Reference_Type
)
2427 raise Program_Error
with "attempt to stream reference";
2430 end Ada
.Containers
.Doubly_Linked_Lists
;