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-2013, 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 type Iterator is new Limited_Controlled and
37 List_Iterator_Interfaces.Reversible_Iterator with
39 Container : List_Access;
43 overriding procedure Finalize (Object : in out Iterator);
45 overriding function First (Object : Iterator) return Cursor;
46 overriding function Last (Object : Iterator) return Cursor;
48 overriding function Next
50 Position : Cursor) return Cursor;
52 overriding function Previous
54 Position : Cursor) return Cursor;
56 -----------------------
57 -- Local Subprograms --
58 -----------------------
60 procedure Free (X : in out Node_Access);
62 procedure Insert_Internal
63 (Container : in out List;
65 New_Node : Node_Access);
67 procedure Splice_Internal
68 (Target : in out List;
70 Source : in out List);
72 procedure Splice_Internal
73 (Target : in out List;
76 Position : Node_Access);
78 function Vet (Position : Cursor) return Boolean;
79 -- Checks invariants of the cursor and its designated container, as a
80 -- simple way of detecting dangling references (see operation Free for a
81 -- description of the detection mechanism), returning True if all checks
82 -- pass. Invocations of Vet are used here as the argument of pragma Assert,
83 -- so the checks are performed only when assertions are enabled.
89 function "=" (Left, Right : List) return Boolean is
90 BL : Natural renames Left'Unrestricted_Access.Busy;
91 LL : Natural renames Left'Unrestricted_Access.Lock;
93 BR : Natural renames Right'Unrestricted_Access.Busy;
94 LR : Natural renames Right'Unrestricted_Access.Lock;
101 if Left'Address = Right'Address then
105 if Left.Length /= Right.Length then
109 -- Per AI05-0022, the container implementation is required to detect
110 -- element tampering by a generic actual subprogram.
121 for J in 1 .. Left.Length loop
122 if L.Element /= R.Element then
153 procedure Adjust (Container : in out List) is
154 Src : Node_Access := Container.First;
158 pragma Assert (Container.Last = null);
159 pragma Assert (Container.Length = 0);
160 pragma Assert (Container.Busy = 0);
161 pragma Assert (Container.Lock = 0);
165 pragma Assert (Container.First.Prev = null);
166 pragma Assert (Container.Last.Next = null);
167 pragma Assert (Container.Length > 0);
169 Container.First := null;
170 Container.Last := null;
171 Container.Length := 0;
175 Container.First := new Node_Type'(Src.Element, null, null);
176 Container.Last := Container.First;
177 Container.Length := 1;
180 while Src /= null loop
181 Container.Last.Next := new Node_Type'(Element => Src.Element,
182 Prev => Container.Last,
184 Container.Last := Container.Last.Next;
185 Container.Length := Container.Length + 1;
191 procedure Adjust (Control : in out Reference_Control_Type) is
193 if Control.Container /= null then
195 C : List renames Control.Container.all;
196 B : Natural renames C.Busy;
197 L : Natural renames C.Lock;
210 (Container : in out List;
211 New_Item : Element_Type;
212 Count : Count_Type := 1)
215 Insert (Container, No_Element, New_Item, Count);
222 procedure Assign (Target : in out List; Source : List) is
226 if Target'Address = Source'Address then
232 Node := Source.First;
233 while Node /= null loop
234 Target.Append (Node.Element);
243 procedure Clear (Container : in out List) is
247 if Container.Length = 0 then
248 pragma Assert (Container.First = null);
249 pragma Assert (Container.Last = null);
250 pragma Assert (Container.Busy = 0);
251 pragma Assert (Container.Lock = 0);
255 pragma Assert (Container.First.Prev = null);
256 pragma Assert (Container.Last.Next = null);
258 if Container.Busy > 0 then
259 raise Program_Error with
260 "attempt to tamper with cursors (list is busy)";
263 while Container.Length > 1 loop
264 X := Container.First;
265 pragma Assert (X.Next.Prev = Container.First);
267 Container.First := X.Next;
268 Container.First.Prev := null;
270 Container.Length := Container.Length - 1;
275 X := Container.First;
276 pragma Assert (X = Container.Last);
278 Container.First := null;
279 Container.Last := null;
280 Container.Length := 0;
282 pragma Warnings (Off);
284 pragma Warnings (On);
287 ------------------------
288 -- Constant_Reference --
289 ------------------------
291 function Constant_Reference
292 (Container : aliased List;
293 Position : Cursor) return Constant_Reference_Type
296 if Position.Container = null then
297 raise Constraint_Error with "Position cursor has no element";
300 if Position.Container /= Container'Unrestricted_Access then
301 raise Program_Error with
302 "Position cursor designates wrong container";
305 pragma Assert (Vet (Position), "bad cursor in Constant_Reference");
308 C : List renames Position.Container.all;
309 B : Natural renames C.Busy;
310 L : Natural renames C.Lock;
312 return R : constant Constant_Reference_Type :=
313 (Element => Position.Node.Element'Access,
314 Control => (Controlled with Container'Unrestricted_Access))
320 end Constant_Reference;
328 Item : Element_Type) return Boolean
331 return Find (Container, Item) /= No_Element;
338 function Copy (Source : List) return List is
340 return Target : List do
341 Target.Assign (Source);
350 (Container : in out List;
351 Position : in out Cursor;
352 Count : Count_Type := 1)
357 if Position.Node = null then
358 raise Constraint_Error with
359 "Position cursor has no element";
362 if Position.Container /= Container'Unrestricted_Access then
363 raise Program_Error with
364 "Position cursor designates wrong container";
367 pragma Assert (Vet (Position), "bad cursor in Delete");
369 if Position.Node = Container.First then
370 Delete_First (Container, Count);
371 Position := No_Element; -- Post-York behavior
376 Position := No_Element; -- Post-York behavior
380 if Container.Busy > 0 then
381 raise Program_Error with
382 "attempt to tamper with cursors (list is busy)";
385 for Index in 1 .. Count loop
387 Container.Length := Container.Length - 1;
389 if X = Container.Last then
390 Position := No_Element;
392 Container.Last := X.Prev;
393 Container.Last.Next := null;
399 Position.Node := X.Next;
401 X.Next.Prev := X.Prev;
402 X.Prev.Next := X.Next;
407 Position := No_Element; -- Post-York behavior
414 procedure Delete_First
415 (Container : in out List;
416 Count : Count_Type := 1)
421 if Count >= Container.Length then
430 if Container.Busy > 0 then
431 raise Program_Error with
432 "attempt to tamper with cursors (list is busy)";
435 for I in 1 .. Count loop
436 X := Container.First;
437 pragma Assert (X.Next.Prev = Container.First);
439 Container.First := X.Next;
440 Container.First.Prev := null;
442 Container.Length := Container.Length - 1;
452 procedure Delete_Last
453 (Container : in out List;
454 Count : Count_Type := 1)
459 if Count >= Container.Length then
468 if Container.Busy > 0 then
469 raise Program_Error with
470 "attempt to tamper with cursors (list is busy)";
473 for I in 1 .. Count loop
475 pragma Assert (X.Prev.Next = Container.Last);
477 Container.Last := X.Prev;
478 Container.Last.Next := null;
480 Container.Length := Container.Length - 1;
490 function Element (Position : Cursor) return Element_Type is
492 if Position.Node = null then
493 raise Constraint_Error with
494 "Position cursor has no element";
497 pragma Assert (Vet (Position), "bad cursor in Element");
499 return Position.Node.Element;
506 procedure Finalize (Object : in out Iterator) is
508 if Object.Container /= null then
510 B : Natural renames Object.Container.all.Busy;
517 procedure Finalize (Control : in out Reference_Control_Type) is
519 if Control.Container /= null then
521 C : List renames Control.Container.all;
522 B : Natural renames C.Busy;
523 L : Natural renames C.Lock;
529 Control.Container := null;
540 Position : Cursor := No_Element) return Cursor
542 Node : Node_Access := Position.Node;
546 Node := Container.First;
549 if Position.Container /= Container'Unrestricted_Access then
550 raise Program_Error with
551 "Position cursor designates wrong container";
554 pragma Assert (Vet (Position), "bad cursor in Find");
557 -- Per AI05-0022, the container implementation is required to detect
558 -- element tampering by a generic actual subprogram.
561 B : Natural renames Container'Unrestricted_Access.Busy;
562 L : Natural renames Container'Unrestricted_Access.Lock;
564 Result : Node_Access;
571 while Node /= null loop
572 if Node.Element = Item then
583 if Result = null then
586 return Cursor'(Container'Unrestricted_Access, Result);
600 function First (Container : List) return Cursor is
602 if Container.First = null then
606 return Cursor'(Container'Unrestricted_Access, Container.First);
609 function First (Object : Iterator) return Cursor is
611 -- The value of the iterator object's Node component influences the
612 -- behavior of the First (and Last) selector function.
614 -- When the Node component is null, this means the iterator object was
615 -- constructed without a start expression, in which case the (forward)
616 -- iteration starts from the (logical) beginning of the entire sequence
617 -- of items (corresponding to Container.First, for a forward iterator).
619 -- Otherwise, this is iteration over a partial sequence of items. When
620 -- the Node component is non-null, the iterator object was constructed
621 -- with a start expression, that specifies the position from which the
622 -- (forward) partial iteration begins.
624 if Object.Node = null then
625 return Doubly_Linked_Lists.First (Object.Container.all);
627 return Cursor'(Object.Container, Object.Node);
635 function First_Element (Container : List) return Element_Type is
637 if Container.First = null then
638 raise Constraint_Error with "list is empty";
641 return Container.First.Element;
648 procedure Free (X : in out Node_Access) is
649 procedure Deallocate is
650 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
652 -- While a node is in use, as an active link in a list, its Previous and
653 -- Next components must be null, or designate a different node; this is
654 -- a node invariant. Before actually deallocating the node, we set both
655 -- access value components of the node to point to the node itself, thus
656 -- falsifying the node invariant. Subprogram Vet inspects the value of
657 -- the node components when interrogating the node, in order to detect
658 -- whether the cursor's node access value is dangling.
660 -- Note that we have no guarantee that the storage for the node isn't
661 -- modified when it is deallocated, but there are other tests that Vet
662 -- does if node invariants appear to be satisifed. However, in practice
663 -- this simple test works well enough, detecting dangling references
664 -- immediately, without needing further interrogation.
672 ---------------------
673 -- Generic_Sorting --
674 ---------------------
676 package body Generic_Sorting is
682 function Is_Sorted (Container : List) return Boolean is
683 B : Natural renames Container'Unrestricted_Access.Busy;
684 L : Natural renames Container'Unrestricted_Access.Lock;
690 -- Per AI05-0022, the container implementation is required to detect
691 -- element tampering by a generic actual subprogram.
696 Node := Container.First;
698 for Idx in 2 .. Container.Length loop
699 if Node.Next.Element < Node.Element then
723 (Target : in out List;
724 Source : in out List)
727 -- The semantics of Merge changed slightly per AI05-0021. It was
728 -- originally the case that if Target and Source denoted the same
729 -- container object, then the GNAT implementation of Merge did
730 -- nothing. However, it was argued that RM05 did not precisely
731 -- specify the semantics for this corner case. The decision of the
732 -- ARG was that if Target and Source denote the same non-empty
733 -- container object, then Program_Error is raised.
735 if Source.Is_Empty then
739 if Target'Address = Source'Address then
740 raise Program_Error with
741 "Target and Source denote same non-empty container";
744 if Target.Length > Count_Type'Last - Source.Length then
745 raise Constraint_Error with "new length exceeds maximum";
748 if Target.Busy > 0 then
749 raise Program_Error with
750 "attempt to tamper with cursors of Target (list is busy)";
753 if Source.Busy > 0 then
754 raise Program_Error with
755 "attempt to tamper with cursors of Source (list is busy)";
758 -- Per AI05-0022, the container implementation is required to detect
759 -- element tampering by a generic actual subprogram.
762 TB : Natural renames Target.Busy;
763 TL : Natural renames Target.Lock;
765 SB : Natural renames Source.Busy;
766 SL : Natural renames Source.Lock;
768 LI, RI, RJ : Node_Access;
779 while RI /= null loop
780 pragma Assert (RI.Next = null
781 or else not (RI.Next.Element < RI.Element));
784 Splice_Internal (Target, null, Source);
788 pragma Assert (LI.Next = null
789 or else not (LI.Next.Element < LI.Element));
791 if RI.Element < LI.Element then
794 Splice_Internal (Target, LI, Source, RJ);
822 procedure Sort (Container : in out List) is
824 procedure Partition (Pivot : Node_Access; Back : Node_Access);
826 procedure Sort (Front, Back : Node_Access);
832 procedure Partition (Pivot : Node_Access; Back : Node_Access) is
833 Node : Node_Access := Pivot.Next;
836 while Node /= Back loop
837 if Node.Element < Pivot.Element then
839 Prev : constant Node_Access := Node.Prev;
840 Next : constant Node_Access := Node.Next;
846 Container.Last := Prev;
852 Node.Prev := Pivot.Prev;
856 if Node.Prev = null then
857 Container.First := Node;
859 Node.Prev.Next := Node;
875 procedure Sort (Front, Back : Node_Access) is
876 Pivot : constant Node_Access :=
877 (if Front = null then Container.First else Front.Next);
879 if Pivot /= Back then
880 Partition (Pivot, Back);
886 -- Start of processing for Sort
889 if Container.Length <= 1 then
893 pragma Assert (Container.First.Prev = null);
894 pragma Assert (Container.Last.Next = null);
896 if Container.Busy > 0 then
897 raise Program_Error with
898 "attempt to tamper with cursors (list is busy)";
901 -- Per AI05-0022, the container implementation is required to detect
902 -- element tampering by a generic actual subprogram.
905 B : Natural renames Container.Busy;
906 L : Natural renames Container.Lock;
912 Sort (Front => null, Back => null);
923 pragma Assert (Container.First.Prev = null);
924 pragma Assert (Container.Last.Next = null);
933 function Has_Element (Position : Cursor) return Boolean is
935 pragma Assert (Vet (Position), "bad cursor in Has_Element");
936 return Position.Node /= null;
944 (Container : in out List;
946 New_Item : Element_Type;
947 Position : out Cursor;
948 Count : Count_Type := 1)
950 New_Node : Node_Access;
953 if Before.Container /= null then
954 if Before.Container /= Container'Unrestricted_Access then
955 raise Program_Error with
956 "Before cursor designates wrong list";
959 pragma Assert (Vet (Before), "bad cursor in Insert");
967 if Container.Length > Count_Type'Last - Count then
968 raise Constraint_Error with "new length exceeds maximum";
971 if Container.Busy > 0 then
972 raise Program_Error with
973 "attempt to tamper with cursors (list is busy)";
976 New_Node := new Node_Type'(New_Item, null, null);
977 Insert_Internal (Container, Before.Node, New_Node);
979 Position := Cursor'(Container'Unchecked_Access, New_Node);
981 for J in Count_Type'(2) .. Count loop
982 New_Node := new Node_Type'(New_Item, null, null);
983 Insert_Internal (Container, Before.Node, New_Node);
988 (Container : in out List;
990 New_Item : Element_Type;
991 Count : Count_Type := 1)
994 pragma Unreferenced (Position);
996 Insert (Container, Before, New_Item, Position, Count);
1000 (Container : in out List;
1002 Position : out Cursor;
1003 Count : Count_Type := 1)
1005 New_Node : Node_Access;
1008 if Before.Container /= null then
1009 if Before.Container /= Container'Unrestricted_Access then
1010 raise Program_Error with
1011 "Before cursor designates wrong list";
1014 pragma Assert (Vet (Before), "bad cursor in Insert");
1022 if Container.Length > Count_Type'Last - Count then
1023 raise Constraint_Error with "new length exceeds maximum";
1026 if Container.Busy > 0 then
1027 raise Program_Error with
1028 "attempt to tamper with cursors (list is busy)";
1031 New_Node := new Node_Type;
1032 Insert_Internal (Container, Before.Node, New_Node);
1034 Position := Cursor'(Container'Unchecked_Access, New_Node);
1036 for J in Count_Type'(2) .. Count loop
1037 New_Node := new Node_Type;
1038 Insert_Internal (Container, Before.Node, New_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";
1174 if Start.Container /= Container'Unrestricted_Access then
1175 raise Program_Error with
1176 "Start cursor of Iterate designates wrong list";
1179 pragma Assert (Vet (Start), "Start cursor of Iterate is bad");
1181 -- The value of the Node component influences the behavior of the First
1182 -- and Last selector functions of the iterator object. When the Node
1183 -- component is non-null (as is the case here), it means that this
1184 -- is a partial iteration, over a subset of the complete sequence of
1185 -- items. The iterator object was constructed with a start expression,
1186 -- indicating the position from which the iteration begins. Note that
1187 -- the start position has the same value irrespective of whether this
1188 -- is a forward or reverse iteration.
1190 return It : constant Iterator :=
1191 Iterator'(Limited_Controlled with
1192 Container => Container'Unrestricted_Access,
1203 function Last (Container : List) return Cursor is
1205 if Container.Last = null then
1209 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";
1244 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
1269 if Source.Busy > 0 then
1270 raise Program_Error with
1271 "attempt to tamper with cursors of Source (list is busy)";
1276 Target.First := Source.First;
1277 Source.First := null;
1279 Target.Last := Source.Last;
1280 Source.Last := null;
1282 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;
1307 if Next_Node = null then
1311 return Cursor'(Position.Container, Next_Node);
1317 Position : Cursor) return Cursor
1320 if Position.Container = null then
1324 if Position.Container /= Object.Container then
1325 raise Program_Error with
1326 "Position cursor of Next designates wrong list";
1329 return Next (Position);
1337 (Container : in out List;
1338 New_Item : Element_Type;
1339 Count : Count_Type := 1)
1342 Insert (Container, First (Container), New_Item, Count);
1349 procedure Previous (Position : in out Cursor) is
1351 Position := Previous (Position);
1354 function Previous (Position : Cursor) return Cursor is
1356 if Position.Node = null then
1360 pragma Assert (Vet (Position), "bad cursor in Previous");
1363 Prev_Node : constant Node_Access := Position.Node.Prev;
1366 if Prev_Node = null then
1370 return Cursor'(Position.Container, Prev_Node);
1376 Position : Cursor) return Cursor
1379 if Position.Container = null then
1383 if Position.Container /= Object.Container then
1384 raise Program_Error with
1385 "Position cursor of Previous designates wrong list";
1388 return Previous (Position);
1395 procedure Query_Element
1397 Process : not null access procedure (Element : Element_Type))
1400 if Position.Node = null then
1401 raise Constraint_Error with
1402 "Position cursor has no element";
1405 pragma Assert (Vet (Position), "bad cursor in Query_Element");
1408 C : List renames Position.Container.all'Unrestricted_Access.all;
1409 B : Natural renames C.Busy;
1410 L : Natural renames C.Lock;
1417 Process (Position.Node.Element);
1435 (Stream : not null access Root_Stream_Type'Class;
1438 N : Count_Type'Base;
1443 Count_Type'Base'Read (Stream, N);
1452 Element_Type'Read (Stream, X.Element);
1463 Item.Length := Item.Length + 1;
1464 exit when Item.Length = N;
1469 Element_Type'Read (Stream, X.Element);
1476 X.Prev := Item.Last;
1477 Item.Last.Next := X;
1483 (Stream : not null access Root_Stream_Type'Class;
1487 raise Program_Error with "attempt to stream list cursor";
1491 (Stream : not null access Root_Stream_Type'Class;
1492 Item : out Reference_Type)
1495 raise Program_Error with "attempt to stream reference";
1499 (Stream : not null access Root_Stream_Type'Class;
1500 Item : out Constant_Reference_Type)
1503 raise Program_Error with "attempt to stream reference";
1511 (Container : aliased in out List;
1512 Position : Cursor) return Reference_Type
1515 if Position.Container = null then
1516 raise Constraint_Error with "Position cursor has no element";
1519 if Position.Container /= Container'Unchecked_Access then
1520 raise Program_Error with
1521 "Position cursor designates wrong container";
1524 pragma Assert (Vet (Position), "bad cursor in function Reference");
1527 C : List renames Position.Container.all;
1528 B : Natural renames C.Busy;
1529 L : Natural renames C.Lock;
1531 return R : constant Reference_Type :=
1532 (Element => Position.Node.Element'Access,
1533 Control => (Controlled with Position.Container))
1541 ---------------------
1542 -- Replace_Element --
1543 ---------------------
1545 procedure Replace_Element
1546 (Container : in out List;
1548 New_Item : Element_Type)
1551 if Position.Container = null then
1552 raise Constraint_Error with "Position cursor has no element";
1555 if Position.Container /= Container'Unchecked_Access then
1556 raise Program_Error with
1557 "Position cursor designates wrong container";
1560 if Container.Lock > 0 then
1561 raise Program_Error with
1562 "attempt to tamper with elements (list is locked)";
1565 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
1567 Position.Node.Element := New_Item;
1568 end Replace_Element;
1570 ----------------------
1571 -- Reverse_Elements --
1572 ----------------------
1574 procedure Reverse_Elements (Container : in out List) is
1575 I : Node_Access := Container.First;
1576 J : Node_Access := Container.Last;
1578 procedure Swap (L, R : Node_Access);
1584 procedure Swap (L, R : Node_Access) is
1585 LN : constant Node_Access := L.Next;
1586 LP : constant Node_Access := L.Prev;
1588 RN : constant Node_Access := R.Next;
1589 RP : constant Node_Access := R.Prev;
1604 pragma Assert (RP = L);
1618 -- Start of processing for Reverse_Elements
1621 if Container.Length <= 1 then
1625 pragma Assert (Container.First.Prev = null);
1626 pragma Assert (Container.Last.Next = null);
1628 if Container.Busy > 0 then
1629 raise Program_Error with
1630 "attempt to tamper with cursors (list is busy)";
1633 Container.First := J;
1634 Container.Last := I;
1636 Swap (L => I, R => J);
1644 Swap (L => J, R => I);
1653 pragma Assert (Container.First.Prev = null);
1654 pragma Assert (Container.Last.Next = null);
1655 end Reverse_Elements;
1661 function Reverse_Find
1663 Item : Element_Type;
1664 Position : Cursor := No_Element) return Cursor
1666 Node : Node_Access := Position.Node;
1670 Node := Container.Last;
1673 if Position.Container /= Container'Unrestricted_Access then
1674 raise Program_Error with
1675 "Position cursor designates wrong container";
1678 pragma Assert (Vet (Position), "bad cursor in Reverse_Find");
1681 -- Per AI05-0022, the container implementation is required to detect
1682 -- element tampering by a generic actual subprogram.
1685 B : Natural renames Container'Unrestricted_Access.Busy;
1686 L : Natural renames Container'Unrestricted_Access.Lock;
1688 Result : Node_Access;
1695 while Node /= null loop
1696 if Node.Element = Item then
1707 if Result = null then
1710 return Cursor'(Container'Unrestricted_Access, Result);
1720 ---------------------
1721 -- Reverse_Iterate --
1722 ---------------------
1724 procedure Reverse_Iterate
1726 Process : not null access procedure (Position : Cursor))
1728 C : List renames Container'Unrestricted_Access.all;
1729 B : Natural renames C.Busy;
1731 Node : Node_Access := Container.Last;
1737 while Node /= null loop
1738 Process (Cursor'(Container'Unrestricted_Access, Node));
1749 end Reverse_Iterate;
1756 (Target : in out List;
1758 Source : in out List)
1761 if Before.Container /= null then
1762 if Before.Container /= Target'Unrestricted_Access then
1763 raise Program_Error with
1764 "Before cursor designates wrong container";
1767 pragma Assert (Vet (Before), "bad cursor in Splice");
1770 if Target'Address = Source'Address
1771 or else Source.Length = 0
1776 if Target.Length > Count_Type'Last - Source.Length then
1777 raise Constraint_Error with "new length exceeds maximum";
1780 if Target.Busy > 0 then
1781 raise Program_Error with
1782 "attempt to tamper with cursors of Target (list is busy)";
1785 if Source.Busy > 0 then
1786 raise Program_Error with
1787 "attempt to tamper with cursors of Source (list is busy)";
1790 Splice_Internal (Target, Before.Node, Source);
1794 (Container : in out List;
1799 if Before.Container /= null then
1800 if Before.Container /= Container'Unchecked_Access then
1801 raise Program_Error with
1802 "Before cursor designates wrong container";
1805 pragma Assert (Vet (Before), "bad Before cursor in Splice");
1808 if Position.Node = null then
1809 raise Constraint_Error with "Position cursor has no element";
1812 if Position.Container /= Container'Unrestricted_Access then
1813 raise Program_Error with
1814 "Position cursor designates wrong container";
1817 pragma Assert (Vet (Position), "bad Position cursor in Splice");
1819 if Position.Node = Before.Node
1820 or else Position.Node.Next = Before.Node
1825 pragma Assert (Container.Length >= 2);
1827 if Container.Busy > 0 then
1828 raise Program_Error with
1829 "attempt to tamper with cursors (list is busy)";
1832 if Before.Node = null then
1833 pragma Assert (Position.Node /= Container.Last);
1835 if Position.Node = Container.First then
1836 Container.First := Position.Node.Next;
1837 Container.First.Prev := null;
1839 Position.Node.Prev.Next := Position.Node.Next;
1840 Position.Node.Next.Prev := Position.Node.Prev;
1843 Container.Last.Next := Position.Node;
1844 Position.Node.Prev := Container.Last;
1846 Container.Last := Position.Node;
1847 Container.Last.Next := null;
1852 if Before.Node = Container.First then
1853 pragma Assert (Position.Node /= Container.First);
1855 if Position.Node = Container.Last then
1856 Container.Last := Position.Node.Prev;
1857 Container.Last.Next := null;
1859 Position.Node.Prev.Next := Position.Node.Next;
1860 Position.Node.Next.Prev := Position.Node.Prev;
1863 Container.First.Prev := Position.Node;
1864 Position.Node.Next := Container.First;
1866 Container.First := Position.Node;
1867 Container.First.Prev := null;
1872 if Position.Node = Container.First then
1873 Container.First := Position.Node.Next;
1874 Container.First.Prev := null;
1876 elsif Position.Node = Container.Last then
1877 Container.Last := Position.Node.Prev;
1878 Container.Last.Next := null;
1881 Position.Node.Prev.Next := Position.Node.Next;
1882 Position.Node.Next.Prev := Position.Node.Prev;
1885 Before.Node.Prev.Next := Position.Node;
1886 Position.Node.Prev := Before.Node.Prev;
1888 Before.Node.Prev := Position.Node;
1889 Position.Node.Next := Before.Node;
1891 pragma Assert (Container.First.Prev = null);
1892 pragma Assert (Container.Last.Next = null);
1896 (Target : in out List;
1898 Source : in out List;
1899 Position : in out Cursor)
1902 if Target'Address = Source'Address then
1903 Splice (Target, Before, Position);
1907 if Before.Container /= null then
1908 if Before.Container /= Target'Unrestricted_Access then
1909 raise Program_Error with
1910 "Before cursor designates wrong container";
1913 pragma Assert (Vet (Before), "bad Before cursor in Splice");
1916 if Position.Node = null then
1917 raise Constraint_Error with "Position cursor has no element";
1920 if Position.Container /= Source'Unrestricted_Access then
1921 raise Program_Error with
1922 "Position cursor designates wrong container";
1925 pragma Assert (Vet (Position), "bad Position cursor in Splice");
1927 if Target.Length = Count_Type'Last then
1928 raise Constraint_Error with "Target is full";
1931 if Target.Busy > 0 then
1932 raise Program_Error with
1933 "attempt to tamper with cursors of Target (list is busy)";
1936 if Source.Busy > 0 then
1937 raise Program_Error with
1938 "attempt to tamper with cursors of Source (list is busy)";
1941 Splice_Internal (Target, Before.Node, Source, Position.Node);
1942 Position.Container := Target'Unchecked_Access;
1945 ---------------------
1946 -- Splice_Internal --
1947 ---------------------
1949 procedure Splice_Internal
1950 (Target : in out List;
1951 Before : Node_Access;
1952 Source : in out List)
1955 -- This implements the corresponding Splice operation, after the
1956 -- parameters have been vetted, and corner-cases disposed of.
1958 pragma Assert (Target'Address /= Source'Address);
1959 pragma Assert (Source.Length > 0);
1960 pragma Assert (Source.First /= null);
1961 pragma Assert (Source.First.Prev = null);
1962 pragma Assert (Source.Last /= null);
1963 pragma Assert (Source.Last.Next = null);
1964 pragma Assert (Target.Length <= Count_Type'Last - Source.Length);
1966 if Target.Length = 0 then
1967 pragma Assert (Target.First = null);
1968 pragma Assert (Target.Last = null);
1969 pragma Assert (Before = null);
1971 Target.First := Source.First;
1972 Target.Last := Source.Last;
1974 elsif Before = null then
1975 pragma Assert (Target.Last.Next = null);
1977 Target.Last.Next := Source.First;
1978 Source.First.Prev := Target.Last;
1980 Target.Last := Source.Last;
1982 elsif Before = Target.First then
1983 pragma Assert (Target.First.Prev = null);
1985 Source.Last.Next := Target.First;
1986 Target.First.Prev := Source.Last;
1988 Target.First := Source.First;
1991 pragma Assert (Target.Length >= 2);
1993 Before.Prev.Next := Source.First;
1994 Source.First.Prev := Before.Prev;
1996 Before.Prev := Source.Last;
1997 Source.Last.Next := Before;
2000 Source.First := null;
2001 Source.Last := null;
2003 Target.Length := Target.Length + Source.Length;
2005 end Splice_Internal;
2007 procedure Splice_Internal
2008 (Target : in out List;
2009 Before : Node_Access; -- node of Target
2010 Source : in out List;
2011 Position : Node_Access) -- node of Source
2014 -- This implements the corresponding Splice operation, after the
2015 -- parameters have been vetted.
2017 pragma Assert (Target'Address /= Source'Address);
2018 pragma Assert (Target.Length < Count_Type'Last);
2019 pragma Assert (Source.Length > 0);
2020 pragma Assert (Source.First /= null);
2021 pragma Assert (Source.First.Prev = null);
2022 pragma Assert (Source.Last /= null);
2023 pragma Assert (Source.Last.Next = null);
2024 pragma Assert (Position /= null);
2026 if Position = Source.First then
2027 Source.First := Position.Next;
2029 if Position = Source.Last then
2030 pragma Assert (Source.First = null);
2031 pragma Assert (Source.Length = 1);
2032 Source.Last := null;
2035 Source.First.Prev := null;
2038 elsif Position = Source.Last then
2039 pragma Assert (Source.Length >= 2);
2040 Source.Last := Position.Prev;
2041 Source.Last.Next := null;
2044 pragma Assert (Source.Length >= 3);
2045 Position.Prev.Next := Position.Next;
2046 Position.Next.Prev := Position.Prev;
2049 if Target.Length = 0 then
2050 pragma Assert (Target.First = null);
2051 pragma Assert (Target.Last = null);
2052 pragma Assert (Before = null);
2054 Target.First := Position;
2055 Target.Last := Position;
2057 Target.First.Prev := null;
2058 Target.Last.Next := null;
2060 elsif Before = null then
2061 pragma Assert (Target.Last.Next = null);
2062 Target.Last.Next := Position;
2063 Position.Prev := Target.Last;
2065 Target.Last := Position;
2066 Target.Last.Next := null;
2068 elsif Before = Target.First then
2069 pragma Assert (Target.First.Prev = null);
2070 Target.First.Prev := Position;
2071 Position.Next := Target.First;
2073 Target.First := Position;
2074 Target.First.Prev := null;
2077 pragma Assert (Target.Length >= 2);
2078 Before.Prev.Next := Position;
2079 Position.Prev := Before.Prev;
2081 Before.Prev := Position;
2082 Position.Next := Before;
2085 Target.Length := Target.Length + 1;
2086 Source.Length := Source.Length - 1;
2087 end Splice_Internal;
2094 (Container : in out List;
2098 if I.Node = null then
2099 raise Constraint_Error with "I cursor has no element";
2102 if J.Node = null then
2103 raise Constraint_Error with "J cursor has no element";
2106 if I.Container /= Container'Unchecked_Access then
2107 raise Program_Error with "I cursor designates wrong container";
2110 if J.Container /= Container'Unchecked_Access then
2111 raise Program_Error with "J cursor designates wrong container";
2114 if I.Node = J.Node then
2118 if Container.Lock > 0 then
2119 raise Program_Error with
2120 "attempt to tamper with elements (list is locked)";
2123 pragma Assert (Vet (I), "bad I cursor in Swap");
2124 pragma Assert (Vet (J), "bad J cursor in Swap");
2127 EI : Element_Type renames I.Node.Element;
2128 EJ : Element_Type renames J.Node.Element;
2130 EI_Copy : constant Element_Type := EI;
2142 procedure Swap_Links
2143 (Container : in out List;
2147 if I.Node = null then
2148 raise Constraint_Error with "I cursor has no element";
2151 if J.Node = null then
2152 raise Constraint_Error with "J cursor has no element";
2155 if I.Container /= Container'Unrestricted_Access then
2156 raise Program_Error with "I cursor designates wrong container";
2159 if J.Container /= Container'Unrestricted_Access then
2160 raise Program_Error with "J cursor designates wrong container";
2163 if I.Node = J.Node then
2167 if Container.Busy > 0 then
2168 raise Program_Error with
2169 "attempt to tamper with cursors (list is busy)";
2172 pragma Assert (Vet (I), "bad I cursor in Swap_Links");
2173 pragma Assert (Vet (J), "bad J cursor in Swap_Links");
2176 I_Next : constant Cursor := Next (I);
2180 Splice (Container, Before => I, Position => J);
2184 J_Next : constant Cursor := Next (J);
2188 Splice (Container, Before => J, Position => I);
2191 pragma Assert (Container.Length >= 3);
2193 Splice (Container, Before => I_Next, Position => J);
2194 Splice (Container, Before => J_Next, Position => I);
2201 --------------------
2202 -- Update_Element --
2203 --------------------
2205 procedure Update_Element
2206 (Container : in out List;
2208 Process : not null access procedure (Element : in out Element_Type))
2211 if Position.Node = null then
2212 raise Constraint_Error with "Position cursor has no element";
2215 if Position.Container /= Container'Unchecked_Access then
2216 raise Program_Error with
2217 "Position cursor designates wrong container";
2220 pragma Assert (Vet (Position), "bad cursor in Update_Element");
2223 B : Natural renames Container.Busy;
2224 L : Natural renames Container.Lock;
2231 Process (Position.Node.Element);
2248 function Vet (Position : Cursor) return Boolean is
2250 if Position.Node = null then
2251 return Position.Container = null;
2254 if Position.Container = null then
2258 -- An invariant of a node is that its Previous and Next components can
2259 -- be null, or designate a different node. Operation Free sets the
2260 -- access value components of the node to designate the node itself
2261 -- before actually deallocating the node, thus deliberately violating
2262 -- the node invariant. This gives us a simple way to detect a dangling
2263 -- reference to a node.
2265 if Position.Node.Next = Position.Node then
2269 if Position.Node.Prev = Position.Node then
2273 -- In practice the tests above will detect most instances of a dangling
2274 -- reference. If we get here, it means that the invariants of the
2275 -- designated node are satisfied (they at least appear to be satisfied),
2276 -- so we perform some more tests, to determine whether invariants of the
2277 -- designated list are satisfied too.
2280 L : List renames Position.Container.all;
2283 if L.Length = 0 then
2287 if L.First = null then
2291 if L.Last = null then
2295 if L.First.Prev /= null then
2299 if L.Last.Next /= null then
2303 if Position.Node.Prev = null and then Position.Node /= L.First then
2308 (Position.Node.Prev /= null
2309 or else Position.Node = L.First);
2311 if Position.Node.Next = null and then Position.Node /= L.Last then
2316 (Position.Node.Next /= null
2317 or else Position.Node = L.Last);
2319 if L.Length = 1 then
2320 return L.First = L.Last;
2323 if L.First = L.Last then
2327 if L.First.Next = null then
2331 if L.Last.Prev = null then
2335 if L.First.Next.Prev /= L.First then
2339 if L.Last.Prev.Next /= L.Last then
2343 if L.Length = 2 then
2344 if L.First.Next /= L.Last then
2346 elsif L.Last.Prev /= L.First then
2353 if L.First.Next = L.Last then
2357 if L.Last.Prev = L.First then
2361 -- Eliminate earlier possibility
2363 if Position.Node = L.First then
2367 pragma Assert (Position.Node.Prev /= null);
2369 -- Eliminate earlier possibility
2371 if Position.Node = L.Last then
2375 pragma Assert (Position.Node.Next /= null);
2377 if Position.Node.Next.Prev /= Position.Node then
2381 if Position.Node.Prev.Next /= Position.Node then
2385 if L.Length = 3 then
2386 if L.First.Next /= Position.Node then
2388 elsif L.Last.Prev /= Position.Node then
2402 (Stream : not null access Root_Stream_Type'Class;
2408 Count_Type'Base'Write (Stream, Item.Length);
2411 while Node /= null loop
2412 Element_Type'Write (Stream, Node.Element);
2418 (Stream : not null access Root_Stream_Type'Class;
2422 raise Program_Error with "attempt to stream list cursor";
2426 (Stream : not null access Root_Stream_Type'Class;
2427 Item : Reference_Type)
2430 raise Program_Error with "attempt to stream reference";
2434 (Stream : not null access Root_Stream_Type'Class;
2435 Item : Constant_Reference_Type)
2438 raise Program_Error with "attempt to stream reference";
2441 end Ada.Containers.Doubly_Linked_Lists;