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 -----------------------
37 -- Local Subprograms --
38 -----------------------
40 procedure Free
(X
: in out Node_Access
);
42 procedure Insert_Internal
43 (Container
: in out List
;
45 New_Node
: Node_Access
);
47 procedure Splice_Internal
48 (Target
: in out List
;
50 Source
: in out List
);
52 procedure Splice_Internal
53 (Target
: in out List
;
56 Position
: Node_Access
);
58 function Vet
(Position
: Cursor
) return Boolean;
59 -- Checks invariants of the cursor and its designated container, as a
60 -- simple way of detecting dangling references (see operation Free for a
61 -- description of the detection mechanism), returning True if all checks
62 -- pass. Invocations of Vet are used here as the argument of pragma Assert,
63 -- so the checks are performed only when assertions are enabled.
69 function "=" (Left
, Right
: List
) return Boolean is
70 BL
: Natural renames Left
'Unrestricted_Access.Busy
;
71 LL
: Natural renames Left
'Unrestricted_Access.Lock
;
73 BR
: Natural renames Right
'Unrestricted_Access.Busy
;
74 LR
: Natural renames Right
'Unrestricted_Access.Lock
;
81 if Left
'Address = Right
'Address then
85 if Left
.Length
/= Right
.Length
then
89 -- Per AI05-0022, the container implementation is required to detect
90 -- element tampering by a generic actual subprogram.
101 for J
in 1 .. Left
.Length
loop
102 if L
.Element
/= R
.Element
then
134 procedure Adjust
(Container
: in out List
) is
135 Src
: Node_Access
:= Container
.First
;
139 pragma Assert
(Container
.Last
= null);
140 pragma Assert
(Container
.Length
= 0);
141 pragma Assert
(Container
.Busy
= 0);
142 pragma Assert
(Container
.Lock
= 0);
146 pragma Assert
(Container
.First
.Prev
= null);
147 pragma Assert
(Container
.Last
.Next
= null);
148 pragma Assert
(Container
.Length
> 0);
150 Container
.First
:= null;
151 Container
.Last
:= null;
152 Container
.Length
:= 0;
156 Container
.First
:= new Node_Type
'(Src.Element, null, null);
157 Container.Last := Container.First;
158 Container.Length := 1;
161 while Src /= null loop
162 Container.Last.Next := new Node_Type'(Element
=> Src
.Element
,
163 Prev
=> Container
.Last
,
165 Container
.Last
:= Container
.Last
.Next
;
166 Container
.Length
:= Container
.Length
+ 1;
172 procedure Adjust
(Control
: in out Reference_Control_Type
) is
174 if Control
.Container
/= null then
176 C
: List
renames Control
.Container
.all;
177 B
: Natural renames C
.Busy
;
178 L
: Natural renames C
.Lock
;
191 (Container
: in out List
;
192 New_Item
: Element_Type
;
193 Count
: Count_Type
:= 1)
196 Insert
(Container
, No_Element
, New_Item
, Count
);
203 procedure Assign
(Target
: in out List
; Source
: List
) is
207 if Target
'Address = Source
'Address then
213 Node
:= Source
.First
;
214 while Node
/= null loop
215 Target
.Append
(Node
.Element
);
224 procedure Clear
(Container
: in out List
) is
228 if Container
.Length
= 0 then
229 pragma Assert
(Container
.First
= null);
230 pragma Assert
(Container
.Last
= null);
231 pragma Assert
(Container
.Busy
= 0);
232 pragma Assert
(Container
.Lock
= 0);
236 pragma Assert
(Container
.First
.Prev
= null);
237 pragma Assert
(Container
.Last
.Next
= null);
239 if Container
.Busy
> 0 then
240 raise Program_Error
with
241 "attempt to tamper with cursors (list is busy)";
244 while Container
.Length
> 1 loop
245 X
:= Container
.First
;
246 pragma Assert
(X
.Next
.Prev
= Container
.First
);
248 Container
.First
:= X
.Next
;
249 Container
.First
.Prev
:= null;
251 Container
.Length
:= Container
.Length
- 1;
256 X
:= Container
.First
;
257 pragma Assert
(X
= Container
.Last
);
259 Container
.First
:= null;
260 Container
.Last
:= null;
261 Container
.Length
:= 0;
263 pragma Warnings
(Off
);
265 pragma Warnings
(On
);
268 ------------------------
269 -- Constant_Reference --
270 ------------------------
272 function Constant_Reference
273 (Container
: aliased List
;
274 Position
: Cursor
) return Constant_Reference_Type
277 if Position
.Container
= null then
278 raise Constraint_Error
with "Position cursor has no element";
281 if Position
.Container
/= Container
'Unrestricted_Access then
282 raise Program_Error
with
283 "Position cursor designates wrong container";
286 pragma Assert
(Vet
(Position
), "bad cursor in Constant_Reference");
289 C
: List
renames Position
.Container
.all;
290 B
: Natural renames C
.Busy
;
291 L
: Natural renames C
.Lock
;
293 return R
: constant Constant_Reference_Type
:=
294 (Element
=> Position
.Node
.Element
'Access,
295 Control
=> (Controlled
with Container
'Unrestricted_Access))
301 end Constant_Reference
;
309 Item
: Element_Type
) return Boolean
312 return Find
(Container
, Item
) /= No_Element
;
319 function Copy
(Source
: List
) return List
is
321 return Target
: List
do
322 Target
.Assign
(Source
);
331 (Container
: in out List
;
332 Position
: in out Cursor
;
333 Count
: Count_Type
:= 1)
338 if Position
.Node
= null then
339 raise Constraint_Error
with
340 "Position cursor has no element";
343 if Position
.Container
/= Container
'Unrestricted_Access then
344 raise Program_Error
with
345 "Position cursor designates wrong container";
348 pragma Assert
(Vet
(Position
), "bad cursor in Delete");
350 if Position
.Node
= Container
.First
then
351 Delete_First
(Container
, Count
);
352 Position
:= No_Element
; -- Post-York behavior
357 Position
:= No_Element
; -- Post-York behavior
361 if Container
.Busy
> 0 then
362 raise Program_Error
with
363 "attempt to tamper with cursors (list is busy)";
366 for Index
in 1 .. Count
loop
368 Container
.Length
:= Container
.Length
- 1;
370 if X
= Container
.Last
then
371 Position
:= No_Element
;
373 Container
.Last
:= X
.Prev
;
374 Container
.Last
.Next
:= null;
380 Position
.Node
:= X
.Next
;
382 X
.Next
.Prev
:= X
.Prev
;
383 X
.Prev
.Next
:= X
.Next
;
388 -- The following comment is unacceptable, more detail needed ???
390 Position
:= No_Element
; -- Post-York behavior
397 procedure Delete_First
398 (Container
: in out List
;
399 Count
: Count_Type
:= 1)
404 if Count
>= Container
.Length
then
413 if Container
.Busy
> 0 then
414 raise Program_Error
with
415 "attempt to tamper with cursors (list is busy)";
418 for J
in 1 .. Count
loop
419 X
:= Container
.First
;
420 pragma Assert
(X
.Next
.Prev
= Container
.First
);
422 Container
.First
:= X
.Next
;
423 Container
.First
.Prev
:= null;
425 Container
.Length
:= Container
.Length
- 1;
435 procedure Delete_Last
436 (Container
: in out List
;
437 Count
: Count_Type
:= 1)
442 if Count
>= Container
.Length
then
451 if Container
.Busy
> 0 then
452 raise Program_Error
with
453 "attempt to tamper with cursors (list is busy)";
456 for J
in 1 .. Count
loop
458 pragma Assert
(X
.Prev
.Next
= Container
.Last
);
460 Container
.Last
:= X
.Prev
;
461 Container
.Last
.Next
:= null;
463 Container
.Length
:= Container
.Length
- 1;
473 function Element
(Position
: Cursor
) return Element_Type
is
475 if Position
.Node
= null then
476 raise Constraint_Error
with
477 "Position cursor has no element";
479 pragma Assert
(Vet
(Position
), "bad cursor in Element");
481 return Position
.Node
.Element
;
489 procedure Finalize
(Object
: in out Iterator
) is
491 if Object
.Container
/= null then
493 B
: Natural renames Object
.Container
.all.Busy
;
500 procedure Finalize
(Control
: in out Reference_Control_Type
) is
502 if Control
.Container
/= null then
504 C
: List
renames Control
.Container
.all;
505 B
: Natural renames C
.Busy
;
506 L
: Natural renames C
.Lock
;
512 Control
.Container
:= null;
523 Position
: Cursor
:= No_Element
) return Cursor
525 Node
: Node_Access
:= Position
.Node
;
529 Node
:= Container
.First
;
532 if Position
.Container
/= Container
'Unrestricted_Access then
533 raise Program_Error
with
534 "Position cursor designates wrong container";
536 pragma Assert
(Vet
(Position
), "bad cursor in Find");
540 -- Per AI05-0022, the container implementation is required to detect
541 -- element tampering by a generic actual subprogram.
544 B
: Natural renames Container
'Unrestricted_Access.Busy
;
545 L
: Natural renames Container
'Unrestricted_Access.Lock
;
547 Result
: Node_Access
;
553 pragma Warnings
(Off
);
554 -- Deal with junk infinite loop warning from below loop
557 while Node
/= null loop
558 if Node
.Element
= Item
then
566 pragma Warnings
(On
);
567 -- End of section dealing with junk infinite loop warning
572 if Result
= null then
575 return Cursor
'(Container'Unrestricted_Access, Result);
590 function First (Container : List) return Cursor is
592 if Container.First = null then
595 return Cursor'(Container
'Unrestricted_Access, Container
.First
);
599 function First
(Object
: Iterator
) return Cursor
is
601 -- The value of the iterator object's Node component influences the
602 -- behavior of the First (and Last) selector function.
604 -- When the Node component is null, this means the iterator object was
605 -- constructed without a start expression, in which case the (forward)
606 -- iteration starts from the (logical) beginning of the entire sequence
607 -- of items (corresponding to Container.First, for a forward iterator).
609 -- Otherwise, this is iteration over a partial sequence of items. When
610 -- the Node component is non-null, the iterator object was constructed
611 -- with a start expression, that specifies the position from which the
612 -- (forward) partial iteration begins.
614 if Object
.Node
= null then
615 return Doubly_Linked_Lists
.First
(Object
.Container
.all);
617 return Cursor
'(Object.Container, Object.Node);
625 function First_Element (Container : List) return Element_Type is
627 if Container.First = null then
628 raise Constraint_Error with "list is empty";
630 return Container.First.Element;
638 procedure Free (X : in out Node_Access) is
639 procedure Deallocate is
640 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
643 -- While a node is in use, as an active link in a list, its Previous and
644 -- Next components must be null, or designate a different node; this is
645 -- a node invariant. Before actually deallocating the node, we set both
646 -- access value components of the node to point to the node itself, thus
647 -- falsifying the node invariant. Subprogram Vet inspects the value of
648 -- the node components when interrogating the node, in order to detect
649 -- whether the cursor's node access value is dangling.
651 -- Note that we have no guarantee that the storage for the node isn't
652 -- modified when it is deallocated, but there are other tests that Vet
653 -- does if node invariants appear to be satisifed. However, in practice
654 -- this simple test works well enough, detecting dangling references
655 -- immediately, without needing further interrogation.
663 ---------------------
664 -- Generic_Sorting --
665 ---------------------
667 package body Generic_Sorting is
673 function Is_Sorted (Container : List) return Boolean is
674 B : Natural renames Container'Unrestricted_Access.Busy;
675 L : Natural renames Container'Unrestricted_Access.Lock;
681 -- Per AI05-0022, the container implementation is required to detect
682 -- element tampering by a generic actual subprogram.
687 Node := Container.First;
689 for Idx in 2 .. Container.Length loop
690 if Node.Next.Element < Node.Element then
715 (Target : in out List;
716 Source : in out List)
719 -- The semantics of Merge changed slightly per AI05-0021. It was
720 -- originally the case that if Target and Source denoted the same
721 -- container object, then the GNAT implementation of Merge did
722 -- nothing. However, it was argued that RM05 did not precisely
723 -- specify the semantics for this corner case. The decision of the
724 -- ARG was that if Target and Source denote the same non-empty
725 -- container object, then Program_Error is raised.
727 if Source.Is_Empty then
731 if Target'Address = Source'Address then
732 raise Program_Error with
733 "Target and Source denote same non-empty container";
736 if Target.Length > Count_Type'Last - Source.Length then
737 raise Constraint_Error with "new length exceeds maximum";
740 if Target.Busy > 0 then
741 raise Program_Error with
742 "attempt to tamper with cursors of Target (list is busy)";
745 if Source.Busy > 0 then
746 raise Program_Error with
747 "attempt to tamper with cursors of Source (list is busy)";
750 -- Per AI05-0022, the container implementation is required to detect
751 -- element tampering by a generic actual subprogram.
754 TB : Natural renames Target.Busy;
755 TL : Natural renames Target.Lock;
757 SB : Natural renames Source.Busy;
758 SL : Natural renames Source.Lock;
760 LI, RI, RJ : Node_Access;
771 while RI /= null loop
772 pragma Assert (RI.Next = null
773 or else not (RI.Next.Element < RI.Element));
776 Splice_Internal (Target, null, Source);
780 pragma Assert (LI.Next = null
781 or else not (LI.Next.Element < LI.Element));
783 if RI.Element < LI.Element then
786 Splice_Internal (Target, LI, Source, RJ);
815 procedure Sort (Container : in out List) is
817 procedure Partition (Pivot : Node_Access; Back : Node_Access);
819 procedure Sort (Front, Back : Node_Access);
825 procedure Partition (Pivot : Node_Access; Back : Node_Access) is
830 while Node /= Back loop
831 if Node.Element < Pivot.Element then
833 Prev : constant Node_Access := Node.Prev;
834 Next : constant Node_Access := Node.Next;
840 Container.Last := Prev;
846 Node.Prev := Pivot.Prev;
850 if Node.Prev = null then
851 Container.First := Node;
853 Node.Prev.Next := Node;
869 procedure Sort (Front, Back : Node_Access) is
870 Pivot : constant Node_Access :=
871 (if Front = null then Container.First else Front.Next);
873 if Pivot /= Back then
874 Partition (Pivot, Back);
880 -- Start of processing for Sort
883 if Container.Length <= 1 then
887 pragma Assert (Container.First.Prev = null);
888 pragma Assert (Container.Last.Next = null);
890 if Container.Busy > 0 then
891 raise Program_Error with
892 "attempt to tamper with cursors (list is busy)";
895 -- Per AI05-0022, the container implementation is required to detect
896 -- element tampering by a generic actual subprogram.
899 B : Natural renames Container.Busy;
900 L : Natural renames Container.Lock;
906 Sort (Front => null, Back => null);
918 pragma Assert (Container.First.Prev = null);
919 pragma Assert (Container.Last.Next = null);
928 function Has_Element (Position : Cursor) return Boolean is
930 pragma Assert (Vet (Position), "bad cursor in Has_Element");
931 return Position.Node /= null;
939 (Container : in out List;
941 New_Item : Element_Type;
942 Position : out Cursor;
943 Count : Count_Type := 1)
945 First_Node : Node_Access;
946 New_Node : Node_Access;
949 if Before.Container /= null then
950 if Before.Container /= Container'Unrestricted_Access then
951 raise Program_Error with
952 "Before cursor designates wrong list";
954 pragma Assert (Vet (Before), "bad cursor in Insert");
962 elsif Container.Length > Count_Type'Last - Count then
963 raise Constraint_Error with "new length exceeds maximum";
965 elsif Container.Busy > 0 then
966 raise Program_Error with
967 "attempt to tamper with cursors (list is busy)";
970 New_Node := new Node_Type'(New_Item
, null, null);
971 First_Node
:= New_Node
;
972 Insert_Internal
(Container
, Before
.Node
, New_Node
);
974 for J
in 2 .. Count
loop
975 New_Node
:= new Node_Type
'(New_Item, null, null);
976 Insert_Internal (Container, Before.Node, New_Node);
979 Position := Cursor'(Container
'Unchecked_Access, First_Node
);
984 (Container
: in out List
;
986 New_Item
: Element_Type
;
987 Count
: Count_Type
:= 1)
990 pragma Unreferenced
(Position
);
992 Insert
(Container
, Before
, New_Item
, Position
, Count
);
996 (Container
: in out List
;
998 Position
: out Cursor
;
999 Count
: Count_Type
:= 1)
1001 First_Node
: Node_Access
;
1002 New_Node
: Node_Access
;
1005 if Before
.Container
/= null then
1006 if Before
.Container
/= Container
'Unrestricted_Access then
1007 raise Program_Error
with
1008 "Before cursor designates wrong list";
1010 pragma Assert
(Vet
(Before
), "bad cursor in Insert");
1019 if Container
.Length
> Count_Type
'Last - Count
then
1020 raise Constraint_Error
with "new length exceeds maximum";
1022 elsif Container
.Busy
> 0 then
1023 raise Program_Error
with
1024 "attempt to tamper with cursors (list is busy)";
1027 New_Node
:= new Node_Type
;
1028 First_Node
:= New_Node
;
1029 Insert_Internal
(Container
, Before
.Node
, New_Node
);
1031 for J
in 2 .. Count
loop
1032 New_Node
:= new Node_Type
;
1033 Insert_Internal
(Container
, Before
.Node
, New_Node
);
1036 Position
:= Cursor
'(Container'Unchecked_Access, First_Node);
1040 ---------------------
1041 -- Insert_Internal --
1042 ---------------------
1044 procedure Insert_Internal
1045 (Container : in out List;
1046 Before : Node_Access;
1047 New_Node : Node_Access)
1050 if Container.Length = 0 then
1051 pragma Assert (Before = null);
1052 pragma Assert (Container.First = null);
1053 pragma Assert (Container.Last = null);
1055 Container.First := New_Node;
1056 Container.Last := New_Node;
1058 elsif Before = null then
1059 pragma Assert (Container.Last.Next = null);
1061 Container.Last.Next := New_Node;
1062 New_Node.Prev := Container.Last;
1064 Container.Last := New_Node;
1066 elsif Before = Container.First then
1067 pragma Assert (Container.First.Prev = null);
1069 Container.First.Prev := New_Node;
1070 New_Node.Next := Container.First;
1072 Container.First := New_Node;
1075 pragma Assert (Container.First.Prev = null);
1076 pragma Assert (Container.Last.Next = null);
1078 New_Node.Next := Before;
1079 New_Node.Prev := Before.Prev;
1081 Before.Prev.Next := New_Node;
1082 Before.Prev := New_Node;
1085 Container.Length := Container.Length + 1;
1086 end Insert_Internal;
1092 function Is_Empty (Container : List) return Boolean is
1094 return Container.Length = 0;
1103 Process : not null access procedure (Position : Cursor))
1105 B : Natural renames Container'Unrestricted_Access.all.Busy;
1106 Node : Node_Access := Container.First;
1112 while Node /= null loop
1113 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1125 function Iterate
(Container
: List
)
1126 return List_Iterator_Interfaces
.Reversible_Iterator
'Class
1128 B
: Natural renames Container
'Unrestricted_Access.all.Busy
;
1131 -- The value of the Node component influences the behavior of the First
1132 -- and Last selector functions of the iterator object. When the Node
1133 -- component is null (as is the case here), this means the iterator
1134 -- object was constructed without a start expression. This is a
1135 -- complete iterator, meaning that the iteration starts from the
1136 -- (logical) beginning of the sequence of items.
1138 -- Note: For a forward iterator, Container.First is the beginning, and
1139 -- for a reverse iterator, Container.Last is the beginning.
1141 return It
: constant Iterator
:=
1142 Iterator
'(Limited_Controlled with
1143 Container => Container'Unrestricted_Access,
1150 function Iterate (Container : List; Start : Cursor)
1151 return List_Iterator_Interfaces.Reversible_Iterator'Class
1153 B : Natural renames Container'Unrestricted_Access.all.Busy;
1156 -- It was formerly the case that when Start = No_Element, the partial
1157 -- iterator was defined to behave the same as for a complete iterator,
1158 -- and iterate over the entire sequence of items. However, those
1159 -- semantics were unintuitive and arguably error-prone (it is too easy
1160 -- to accidentally create an endless loop), and so they were changed,
1161 -- per the ARG meeting in Denver on 2011/11. However, there was no
1162 -- consensus about what positive meaning this corner case should have,
1163 -- and so it was decided to simply raise an exception. This does imply,
1164 -- however, that it is not possible to use a partial iterator to specify
1165 -- an empty sequence of items.
1167 if Start = No_Element then
1168 raise Constraint_Error with
1169 "Start position for iterator equals No_Element";
1171 elsif Start.Container /= Container'Unrestricted_Access then
1172 raise Program_Error with
1173 "Start cursor of Iterate designates wrong list";
1176 pragma Assert (Vet (Start), "Start cursor of Iterate is bad");
1178 -- The value of the Node component influences the behavior of the
1179 -- First and Last selector functions of the iterator object. When
1180 -- the Node component is non-null (as is the case here), it means
1181 -- that this is a partial iteration, over a subset of the complete
1182 -- sequence of items. The iterator object was constructed with
1183 -- a start expression, indicating the position from which the
1184 -- iteration begins. Note that the start position has the same value
1185 -- irrespective of whether this is a forward or reverse iteration.
1187 return It : constant Iterator :=
1188 Iterator'(Limited_Controlled
with
1189 Container
=> Container
'Unrestricted_Access,
1201 function Last
(Container
: List
) return Cursor
is
1203 if Container
.Last
= null then
1206 return Cursor
'(Container'Unrestricted_Access, Container.Last);
1210 function Last (Object : Iterator) return Cursor is
1212 -- The value of the iterator object's Node component influences the
1213 -- behavior of the Last (and First) selector function.
1215 -- When the Node component is null, this means the iterator object was
1216 -- constructed without a start expression, in which case the (reverse)
1217 -- iteration starts from the (logical) beginning of the entire sequence
1218 -- (corresponding to Container.Last, for a reverse iterator).
1220 -- Otherwise, this is iteration over a partial sequence of items. When
1221 -- the Node component is non-null, the iterator object was constructed
1222 -- with a start expression, that specifies the position from which the
1223 -- (reverse) partial iteration begins.
1225 if Object.Node = null then
1226 return Doubly_Linked_Lists.Last (Object.Container.all);
1228 return Cursor'(Object
.Container
, Object
.Node
);
1236 function Last_Element
(Container
: List
) return Element_Type
is
1238 if Container
.Last
= null then
1239 raise Constraint_Error
with "list is empty";
1241 return Container
.Last
.Element
;
1249 function Length
(Container
: List
) return Count_Type
is
1251 return Container
.Length
;
1259 (Target
: in out List
;
1260 Source
: in out List
)
1263 if Target
'Address = Source
'Address then
1266 elsif Source
.Busy
> 0 then
1267 raise Program_Error
with
1268 "attempt to tamper with cursors of Source (list is busy)";
1273 Target
.First
:= Source
.First
;
1274 Source
.First
:= null;
1276 Target
.Last
:= Source
.Last
;
1277 Source
.Last
:= null;
1279 Target
.Length
:= Source
.Length
;
1288 procedure Next
(Position
: in out Cursor
) is
1290 Position
:= Next
(Position
);
1293 function Next
(Position
: Cursor
) return Cursor
is
1295 if Position
.Node
= null then
1299 pragma Assert
(Vet
(Position
), "bad cursor in Next");
1302 Next_Node
: constant Node_Access
:= Position
.Node
.Next
;
1304 if Next_Node
= null then
1307 return Cursor
'(Position.Container, Next_Node);
1315 Position : Cursor) return Cursor
1318 if Position.Container = null then
1320 elsif Position.Container /= Object.Container then
1321 raise Program_Error with
1322 "Position cursor of Next designates wrong list";
1324 return Next (Position);
1333 (Container : in out List;
1334 New_Item : Element_Type;
1335 Count : Count_Type := 1)
1338 Insert (Container, First (Container), New_Item, Count);
1345 procedure Previous (Position : in out Cursor) is
1347 Position := Previous (Position);
1350 function Previous (Position : Cursor) return Cursor is
1352 if Position.Node = null then
1356 pragma Assert (Vet (Position), "bad cursor in Previous");
1359 Prev_Node : constant Node_Access := Position.Node.Prev;
1361 if Prev_Node = null then
1364 return Cursor'(Position
.Container
, Prev_Node
);
1372 Position
: Cursor
) return Cursor
1375 if Position
.Container
= null then
1377 elsif Position
.Container
/= Object
.Container
then
1378 raise Program_Error
with
1379 "Position cursor of Previous designates wrong list";
1381 return Previous
(Position
);
1389 procedure Query_Element
1391 Process
: not null access procedure (Element
: Element_Type
))
1394 if Position
.Node
= null then
1395 raise Constraint_Error
with
1396 "Position cursor has no element";
1399 pragma Assert
(Vet
(Position
), "bad cursor in Query_Element");
1402 C
: List
renames Position
.Container
.all'Unrestricted_Access.all;
1403 B
: Natural renames C
.Busy
;
1404 L
: Natural renames C
.Lock
;
1411 Process
(Position
.Node
.Element
);
1429 (Stream
: not null access Root_Stream_Type
'Class;
1432 N
: Count_Type
'Base;
1437 Count_Type
'Base'Read (Stream, N);
1446 Element_Type'Read (Stream, X.Element);
1457 Item.Length := Item.Length + 1;
1458 exit when Item.Length = N;
1463 Element_Type'Read (Stream, X.Element);
1470 X.Prev := Item.Last;
1471 Item.Last.Next := X;
1477 (Stream : not null access Root_Stream_Type'Class;
1481 raise Program_Error with "attempt to stream list cursor";
1485 (Stream : not null access Root_Stream_Type'Class;
1486 Item : out Reference_Type)
1489 raise Program_Error with "attempt to stream reference";
1493 (Stream : not null access Root_Stream_Type'Class;
1494 Item : out Constant_Reference_Type)
1497 raise Program_Error with "attempt to stream reference";
1505 (Container : aliased in out List;
1506 Position : Cursor) return Reference_Type
1509 if Position.Container = null then
1510 raise Constraint_Error with "Position cursor has no element";
1512 elsif Position.Container /= Container'Unchecked_Access then
1513 raise Program_Error with
1514 "Position cursor designates wrong container";
1517 pragma Assert (Vet (Position), "bad cursor in function Reference");
1520 C : List renames Position.Container.all;
1521 B : Natural renames C.Busy;
1522 L : Natural renames C.Lock;
1524 return R : constant Reference_Type :=
1525 (Element => Position.Node.Element'Access,
1526 Control => (Controlled with Position.Container))
1535 ---------------------
1536 -- Replace_Element --
1537 ---------------------
1539 procedure Replace_Element
1540 (Container : in out List;
1542 New_Item : Element_Type)
1545 if Position.Container = null then
1546 raise Constraint_Error with "Position cursor has no element";
1548 elsif Position.Container /= Container'Unchecked_Access then
1549 raise Program_Error with
1550 "Position cursor designates wrong container";
1552 elsif Container.Lock > 0 then
1553 raise Program_Error with
1554 "attempt to tamper with elements (list is locked)";
1557 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
1559 Position.Node.Element := New_Item;
1561 end Replace_Element;
1563 ----------------------
1564 -- Reverse_Elements --
1565 ----------------------
1567 procedure Reverse_Elements (Container : in out List) is
1568 I : Node_Access := Container.First;
1569 J : Node_Access := Container.Last;
1571 procedure Swap (L, R : Node_Access);
1577 procedure Swap (L, R : Node_Access) is
1578 LN : constant Node_Access := L.Next;
1579 LP : constant Node_Access := L.Prev;
1581 RN : constant Node_Access := R.Next;
1582 RP : constant Node_Access := R.Prev;
1597 pragma Assert (RP = L);
1611 -- Start of processing for Reverse_Elements
1614 if Container.Length <= 1 then
1618 pragma Assert (Container.First.Prev = null);
1619 pragma Assert (Container.Last.Next = null);
1621 if Container.Busy > 0 then
1622 raise Program_Error with
1623 "attempt to tamper with cursors (list is busy)";
1626 Container.First := J;
1627 Container.Last := I;
1629 Swap (L => I, R => J);
1637 Swap (L => J, R => I);
1646 pragma Assert (Container.First.Prev = null);
1647 pragma Assert (Container.Last.Next = null);
1648 end Reverse_Elements;
1654 function Reverse_Find
1656 Item : Element_Type;
1657 Position : Cursor := No_Element) return Cursor
1659 Node : Node_Access := Position.Node;
1663 Node := Container.Last;
1666 if Position.Container /= Container'Unrestricted_Access then
1667 raise Program_Error with
1668 "Position cursor designates wrong container";
1670 pragma Assert (Vet (Position), "bad cursor in Reverse_Find");
1674 -- Per AI05-0022, the container implementation is required to detect
1675 -- element tampering by a generic actual subprogram.
1678 B : Natural renames Container'Unrestricted_Access.Busy;
1679 L : Natural renames Container'Unrestricted_Access.Lock;
1681 Result : Node_Access;
1688 while Node /= null loop
1689 if Node.Element = Item then
1700 if Result = null then
1703 return Cursor'(Container
'Unrestricted_Access, Result
);
1714 ---------------------
1715 -- Reverse_Iterate --
1716 ---------------------
1718 procedure Reverse_Iterate
1720 Process
: not null access procedure (Position
: Cursor
))
1722 C
: List
renames Container
'Unrestricted_Access.all;
1723 B
: Natural renames C
.Busy
;
1725 Node
: Node_Access
:= Container
.Last
;
1731 while Node
/= null loop
1732 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1742 end Reverse_Iterate;
1749 (Target : in out List;
1751 Source : in out List)
1754 if Before.Container /= null then
1755 if Before.Container /= Target'Unrestricted_Access then
1756 raise Program_Error with
1757 "Before cursor designates wrong container";
1759 pragma Assert (Vet (Before), "bad cursor in Splice");
1763 if Target'Address = Source'Address or else Source.Length = 0 then
1766 elsif Target.Length > Count_Type'Last - Source.Length then
1767 raise Constraint_Error with "new length exceeds maximum";
1769 elsif Target.Busy > 0 then
1770 raise Program_Error with
1771 "attempt to tamper with cursors of Target (list is busy)";
1773 elsif Source.Busy > 0 then
1774 raise Program_Error with
1775 "attempt to tamper with cursors of Source (list is busy)";
1778 Splice_Internal (Target, Before.Node, Source);
1783 (Container : in out List;
1788 if Before.Container /= null then
1789 if Before.Container /= Container'Unchecked_Access then
1790 raise Program_Error with
1791 "Before cursor designates wrong container";
1793 pragma Assert (Vet (Before), "bad Before cursor in Splice");
1797 if Position.Node = null then
1798 raise Constraint_Error with "Position cursor has no element";
1801 if Position.Container /= Container'Unrestricted_Access then
1802 raise Program_Error with
1803 "Position cursor designates wrong container";
1806 pragma Assert (Vet (Position), "bad Position cursor in Splice");
1808 if Position.Node = Before.Node
1809 or else Position.Node.Next = Before.Node
1814 pragma Assert (Container.Length >= 2);
1816 if Container.Busy > 0 then
1817 raise Program_Error with
1818 "attempt to tamper with cursors (list is busy)";
1821 if Before.Node = null then
1822 pragma Assert (Position.Node /= Container.Last);
1824 if Position.Node = Container.First then
1825 Container.First := Position.Node.Next;
1826 Container.First.Prev := null;
1828 Position.Node.Prev.Next := Position.Node.Next;
1829 Position.Node.Next.Prev := Position.Node.Prev;
1832 Container.Last.Next := Position.Node;
1833 Position.Node.Prev := Container.Last;
1835 Container.Last := Position.Node;
1836 Container.Last.Next := null;
1841 if Before.Node = Container.First then
1842 pragma Assert (Position.Node /= Container.First);
1844 if Position.Node = Container.Last then
1845 Container.Last := Position.Node.Prev;
1846 Container.Last.Next := null;
1848 Position.Node.Prev.Next := Position.Node.Next;
1849 Position.Node.Next.Prev := Position.Node.Prev;
1852 Container.First.Prev := Position.Node;
1853 Position.Node.Next := Container.First;
1855 Container.First := Position.Node;
1856 Container.First.Prev := null;
1861 if Position.Node = Container.First then
1862 Container.First := Position.Node.Next;
1863 Container.First.Prev := null;
1865 elsif Position.Node = Container.Last then
1866 Container.Last := Position.Node.Prev;
1867 Container.Last.Next := null;
1870 Position.Node.Prev.Next := Position.Node.Next;
1871 Position.Node.Next.Prev := Position.Node.Prev;
1874 Before.Node.Prev.Next := Position.Node;
1875 Position.Node.Prev := Before.Node.Prev;
1877 Before.Node.Prev := Position.Node;
1878 Position.Node.Next := Before.Node;
1880 pragma Assert (Container.First.Prev = null);
1881 pragma Assert (Container.Last.Next = null);
1885 (Target : in out List;
1887 Source : in out List;
1888 Position : in out Cursor)
1891 if Target'Address = Source'Address then
1892 Splice (Target, Before, Position);
1896 if Before.Container /= null then
1897 if Before.Container /= Target'Unrestricted_Access then
1898 raise Program_Error with
1899 "Before cursor designates wrong container";
1901 pragma Assert (Vet (Before), "bad Before cursor in Splice");
1905 if Position.Node = null then
1906 raise Constraint_Error with "Position cursor has no element";
1908 elsif Position.Container /= Source'Unrestricted_Access then
1909 raise Program_Error with
1910 "Position cursor designates wrong container";
1913 pragma Assert (Vet (Position), "bad Position cursor in Splice");
1915 if Target.Length = Count_Type'Last then
1916 raise Constraint_Error with "Target is full";
1918 elsif Target.Busy > 0 then
1919 raise Program_Error with
1920 "attempt to tamper with cursors of Target (list is busy)";
1922 elsif Source.Busy > 0 then
1923 raise Program_Error with
1924 "attempt to tamper with cursors of Source (list is busy)";
1927 Splice_Internal (Target, Before.Node, Source, Position.Node);
1928 Position.Container := Target'Unchecked_Access;
1933 ---------------------
1934 -- Splice_Internal --
1935 ---------------------
1937 procedure Splice_Internal
1938 (Target : in out List;
1939 Before : Node_Access;
1940 Source : in out List)
1943 -- This implements the corresponding Splice operation, after the
1944 -- parameters have been vetted, and corner-cases disposed of.
1946 pragma Assert (Target'Address /= Source'Address);
1947 pragma Assert (Source.Length > 0);
1948 pragma Assert (Source.First /= null);
1949 pragma Assert (Source.First.Prev = null);
1950 pragma Assert (Source.Last /= null);
1951 pragma Assert (Source.Last.Next = null);
1952 pragma Assert (Target.Length <= Count_Type'Last - Source.Length);
1954 if Target.Length = 0 then
1955 pragma Assert (Target.First = null);
1956 pragma Assert (Target.Last = null);
1957 pragma Assert (Before = null);
1959 Target.First := Source.First;
1960 Target.Last := Source.Last;
1962 elsif Before = null then
1963 pragma Assert (Target.Last.Next = null);
1965 Target.Last.Next := Source.First;
1966 Source.First.Prev := Target.Last;
1968 Target.Last := Source.Last;
1970 elsif Before = Target.First then
1971 pragma Assert (Target.First.Prev = null);
1973 Source.Last.Next := Target.First;
1974 Target.First.Prev := Source.Last;
1976 Target.First := Source.First;
1979 pragma Assert (Target.Length >= 2);
1981 Before.Prev.Next := Source.First;
1982 Source.First.Prev := Before.Prev;
1984 Before.Prev := Source.Last;
1985 Source.Last.Next := Before;
1988 Source.First := null;
1989 Source.Last := null;
1991 Target.Length := Target.Length + Source.Length;
1993 end Splice_Internal;
1995 procedure Splice_Internal
1996 (Target : in out List;
1997 Before : Node_Access; -- node of Target
1998 Source : in out List;
1999 Position : Node_Access) -- node of Source
2002 -- This implements the corresponding Splice operation, after the
2003 -- parameters have been vetted.
2005 pragma Assert (Target'Address /= Source'Address);
2006 pragma Assert (Target.Length < Count_Type'Last);
2007 pragma Assert (Source.Length > 0);
2008 pragma Assert (Source.First /= null);
2009 pragma Assert (Source.First.Prev = null);
2010 pragma Assert (Source.Last /= null);
2011 pragma Assert (Source.Last.Next = null);
2012 pragma Assert (Position /= null);
2014 if Position = Source.First then
2015 Source.First := Position.Next;
2017 if Position = Source.Last then
2018 pragma Assert (Source.First = null);
2019 pragma Assert (Source.Length = 1);
2020 Source.Last := null;
2023 Source.First.Prev := null;
2026 elsif Position = Source.Last then
2027 pragma Assert (Source.Length >= 2);
2028 Source.Last := Position.Prev;
2029 Source.Last.Next := null;
2032 pragma Assert (Source.Length >= 3);
2033 Position.Prev.Next := Position.Next;
2034 Position.Next.Prev := Position.Prev;
2037 if Target.Length = 0 then
2038 pragma Assert (Target.First = null);
2039 pragma Assert (Target.Last = null);
2040 pragma Assert (Before = null);
2042 Target.First := Position;
2043 Target.Last := Position;
2045 Target.First.Prev := null;
2046 Target.Last.Next := null;
2048 elsif Before = null then
2049 pragma Assert (Target.Last.Next = null);
2050 Target.Last.Next := Position;
2051 Position.Prev := Target.Last;
2053 Target.Last := Position;
2054 Target.Last.Next := null;
2056 elsif Before = Target.First then
2057 pragma Assert (Target.First.Prev = null);
2058 Target.First.Prev := Position;
2059 Position.Next := Target.First;
2061 Target.First := Position;
2062 Target.First.Prev := null;
2065 pragma Assert (Target.Length >= 2);
2066 Before.Prev.Next := Position;
2067 Position.Prev := Before.Prev;
2069 Before.Prev := Position;
2070 Position.Next := Before;
2073 Target.Length := Target.Length + 1;
2074 Source.Length := Source.Length - 1;
2075 end Splice_Internal;
2082 (Container : in out List;
2086 if I.Node = null then
2087 raise Constraint_Error with "I cursor has no element";
2090 if J.Node = null then
2091 raise Constraint_Error with "J cursor has no element";
2094 if I.Container /= Container'Unchecked_Access then
2095 raise Program_Error with "I cursor designates wrong container";
2098 if J.Container /= Container'Unchecked_Access then
2099 raise Program_Error with "J cursor designates wrong container";
2102 if I.Node = J.Node then
2106 if Container.Lock > 0 then
2107 raise Program_Error with
2108 "attempt to tamper with elements (list is locked)";
2111 pragma Assert (Vet (I), "bad I cursor in Swap");
2112 pragma Assert (Vet (J), "bad J cursor in Swap");
2115 EI : Element_Type renames I.Node.Element;
2116 EJ : Element_Type renames J.Node.Element;
2118 EI_Copy : constant Element_Type := EI;
2130 procedure Swap_Links
2131 (Container : in out List;
2135 if I.Node = null then
2136 raise Constraint_Error with "I cursor has no element";
2139 if J.Node = null then
2140 raise Constraint_Error with "J cursor has no element";
2143 if I.Container /= Container'Unrestricted_Access then
2144 raise Program_Error with "I cursor designates wrong container";
2147 if J.Container /= Container'Unrestricted_Access then
2148 raise Program_Error with "J cursor designates wrong container";
2151 if I.Node = J.Node then
2155 if Container.Busy > 0 then
2156 raise Program_Error with
2157 "attempt to tamper with cursors (list is busy)";
2160 pragma Assert (Vet (I), "bad I cursor in Swap_Links");
2161 pragma Assert (Vet (J), "bad J cursor in Swap_Links");
2164 I_Next : constant Cursor := Next (I);
2168 Splice (Container, Before => I, Position => J);
2172 J_Next : constant Cursor := Next (J);
2176 Splice (Container, Before => J, Position => I);
2179 pragma Assert (Container.Length >= 3);
2181 Splice (Container, Before => I_Next, Position => J);
2182 Splice (Container, Before => J_Next, Position => I);
2189 --------------------
2190 -- Update_Element --
2191 --------------------
2193 procedure Update_Element
2194 (Container : in out List;
2196 Process : not null access procedure (Element : in out Element_Type))
2199 if Position.Node = null then
2200 raise Constraint_Error with "Position cursor has no element";
2202 elsif Position.Container /= Container'Unchecked_Access then
2203 raise Program_Error with
2204 "Position cursor designates wrong container";
2207 pragma Assert (Vet (Position), "bad cursor in Update_Element");
2210 B : Natural renames Container.Busy;
2211 L : Natural renames Container.Lock;
2218 Process (Position.Node.Element);
2236 function Vet (Position : Cursor) return Boolean is
2238 if Position.Node = null then
2239 return Position.Container = null;
2242 if Position.Container = null then
2246 -- An invariant of a node is that its Previous and Next components can
2247 -- be null, or designate a different node. Operation Free sets the
2248 -- access value components of the node to designate the node itself
2249 -- before actually deallocating the node, thus deliberately violating
2250 -- the node invariant. This gives us a simple way to detect a dangling
2251 -- reference to a node.
2253 if Position.Node.Next = Position.Node then
2257 if Position.Node.Prev = Position.Node then
2261 -- In practice the tests above will detect most instances of a dangling
2262 -- reference. If we get here, it means that the invariants of the
2263 -- designated node are satisfied (they at least appear to be satisfied),
2264 -- so we perform some more tests, to determine whether invariants of the
2265 -- designated list are satisfied too.
2268 L : List renames Position.Container.all;
2271 if L.Length = 0 then
2275 if L.First = null then
2279 if L.Last = null then
2283 if L.First.Prev /= null then
2287 if L.Last.Next /= null then
2291 if Position.Node.Prev = null and then Position.Node /= L.First then
2296 (Position.Node.Prev /= null or else Position.Node = L.First);
2298 if Position.Node.Next = null and then Position.Node /= L.Last then
2303 (Position.Node.Next /= null
2304 or else Position.Node = L.Last);
2306 if L.Length = 1 then
2307 return L.First = L.Last;
2310 if L.First = L.Last then
2314 if L.First.Next = null then
2318 if L.Last.Prev = null then
2322 if L.First.Next.Prev /= L.First then
2326 if L.Last.Prev.Next /= L.Last then
2330 if L.Length = 2 then
2331 if L.First.Next /= L.Last then
2333 elsif L.Last.Prev /= L.First then
2340 if L.First.Next = L.Last then
2344 if L.Last.Prev = L.First then
2348 -- Eliminate earlier possibility
2350 if Position.Node = L.First then
2354 pragma Assert (Position.Node.Prev /= null);
2356 -- Eliminate earlier possibility
2358 if Position.Node = L.Last then
2362 pragma Assert (Position.Node.Next /= null);
2364 if Position.Node.Next.Prev /= Position.Node then
2368 if Position.Node.Prev.Next /= Position.Node then
2372 if L.Length = 3 then
2373 if L.First.Next /= Position.Node then
2375 elsif L.Last.Prev /= Position.Node then
2389 (Stream : not null access Root_Stream_Type'Class;
2395 Count_Type'Base'Write
(Stream
, Item
.Length
);
2398 while Node
/= null loop
2399 Element_Type
'Write (Stream
, Node
.Element
);
2405 (Stream
: not null access Root_Stream_Type
'Class;
2409 raise Program_Error
with "attempt to stream list cursor";
2413 (Stream
: not null access Root_Stream_Type
'Class;
2414 Item
: Reference_Type
)
2417 raise Program_Error
with "attempt to stream reference";
2421 (Stream
: not null access Root_Stream_Type
'Class;
2422 Item
: Constant_Reference_Type
)
2425 raise Program_Error
with "attempt to stream reference";
2428 end Ada
.Containers
.Doubly_Linked_Lists
;