1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.INDEFINITE_DOUBLY_LINKED_LISTS --
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.Indefinite_Doubly_Linked_Lists is
37 new Ada.Unchecked_Deallocation (Element_Type, Element_Access);
39 type Iterator is new Limited_Controlled and
40 List_Iterator_Interfaces.Reversible_Iterator with
42 Container : List_Access;
46 overriding procedure Finalize (Object : in out Iterator);
48 overriding function First (Object : Iterator) return Cursor;
49 overriding function Last (Object : Iterator) return Cursor;
51 overriding function Next
53 Position : Cursor) return Cursor;
55 overriding function Previous
57 Position : Cursor) return Cursor;
59 -----------------------
60 -- Local Subprograms --
61 -----------------------
63 procedure Free (X : in out Node_Access);
65 procedure Insert_Internal
66 (Container : in out List;
68 New_Node : Node_Access);
70 procedure Splice_Internal
71 (Target : in out List;
73 Source : in out List);
75 procedure Splice_Internal
76 (Target : in out List;
79 Position : Node_Access);
81 function Vet (Position : Cursor) return Boolean;
82 -- Checks invariants of the cursor and its designated container, as a
83 -- simple way of detecting dangling references (see operation Free for a
84 -- description of the detection mechanism), returning True if all checks
85 -- pass. Invocations of Vet are used here as the argument of pragma Assert,
86 -- so the checks are performed only when assertions are enabled.
92 function "=" (Left, Right : List) return Boolean is
93 BL : Natural renames Left'Unrestricted_Access.Busy;
94 LL : Natural renames Left'Unrestricted_Access.Lock;
96 BR : Natural renames Right'Unrestricted_Access.Busy;
97 LR : Natural renames Right'Unrestricted_Access.Lock;
104 if Left'Address = Right'Address then
108 if Left.Length /= Right.Length then
112 -- Per AI05-0022, the container implementation is required to detect
113 -- element tampering by a generic actual subprogram.
124 for J in 1 .. Left.Length loop
125 if L.Element.all /= R.Element.all then
157 procedure Adjust (Container : in out List) is
158 Src : Node_Access := Container.First;
163 pragma Assert (Container.Last = null);
164 pragma Assert (Container.Length = 0);
165 pragma Assert (Container.Busy = 0);
166 pragma Assert (Container.Lock = 0);
170 pragma Assert (Container.First.Prev = null);
171 pragma Assert (Container.Last.Next = null);
172 pragma Assert (Container.Length > 0);
174 Container.First := null;
175 Container.Last := null;
176 Container.Length := 0;
181 Element : Element_Access := new Element_Type'(Src.Element.all);
183 Dst := new Node_Type'(Element, null, null);
190 Container.First := Dst;
191 Container.Last := Dst;
192 Container.Length := 1;
195 while Src /= null loop
197 Element : Element_Access := new Element_Type'(Src.Element.all);
199 Dst := new Node_Type'(Element, null, Prev => Container.Last);
206 Container.Last.Next := Dst;
207 Container.Last := Dst;
208 Container.Length := Container.Length + 1;
214 procedure Adjust (Control : in out Reference_Control_Type) is
216 if Control.Container /= null then
218 C : List renames Control.Container.all;
219 B : Natural renames C.Busy;
220 L : Natural renames C.Lock;
233 (Container : in out List;
234 New_Item : Element_Type;
235 Count : Count_Type := 1)
238 Insert (Container, No_Element, New_Item, Count);
245 procedure Assign (Target : in out List; Source : List) is
249 if Target'Address = Source'Address then
255 Node := Source.First;
256 while Node /= null loop
257 Target.Append (Node.Element.all);
267 procedure Clear (Container : in out List) is
269 pragma Warnings (Off, X);
272 if Container.Length = 0 then
273 pragma Assert (Container.First = null);
274 pragma Assert (Container.Last = null);
275 pragma Assert (Container.Busy = 0);
276 pragma Assert (Container.Lock = 0);
280 pragma Assert (Container.First.Prev = null);
281 pragma Assert (Container.Last.Next = null);
283 if Container.Busy > 0 then
284 raise Program_Error with
285 "attempt to tamper with cursors (list is busy)";
288 while Container.Length > 1 loop
289 X := Container.First;
290 pragma Assert (X.Next.Prev = Container.First);
292 Container.First := X.Next;
293 Container.First.Prev := null;
295 Container.Length := Container.Length - 1;
300 X := Container.First;
301 pragma Assert (X = Container.Last);
303 Container.First := null;
304 Container.Last := null;
305 Container.Length := 0;
310 ------------------------
311 -- Constant_Reference --
312 ------------------------
314 function Constant_Reference
315 (Container : aliased List;
316 Position : Cursor) return Constant_Reference_Type
319 if Position.Container = null then
320 raise Constraint_Error with "Position cursor has no element";
322 elsif Position.Container /= Container'Unrestricted_Access then
323 raise Program_Error with
324 "Position cursor designates wrong container";
325 elsif Position.Node.Element = null then
326 raise Program_Error with "Node has no element";
329 pragma Assert (Vet (Position), "bad cursor in Constant_Reference");
332 C : List renames Position.Container.all;
333 B : Natural renames C.Busy;
334 L : Natural renames C.Lock;
336 return R : constant Constant_Reference_Type :=
337 (Element => Position.Node.Element.all'Access,
338 Control => (Controlled with Position.Container))
345 end Constant_Reference;
353 Item : Element_Type) return Boolean
356 return Find (Container, Item) /= No_Element;
363 function Copy (Source : List) return List is
365 return Target : List do
366 Target.Assign (Source);
375 (Container : in out List;
376 Position : in out Cursor;
377 Count : Count_Type := 1)
382 if Position.Node = null then
383 raise Constraint_Error with
384 "Position cursor has no element";
387 if Position.Node.Element = null then
388 raise Program_Error with
389 "Position cursor has no element";
392 if Position.Container /= Container'Unrestricted_Access then
393 raise Program_Error with
394 "Position cursor designates wrong container";
397 pragma Assert (Vet (Position), "bad cursor in Delete");
399 if Position.Node = Container.First then
400 Delete_First (Container, Count);
401 Position := No_Element; -- Post-York behavior
406 Position := No_Element; -- Post-York behavior
410 if Container.Busy > 0 then
411 raise Program_Error with
412 "attempt to tamper with cursors (list is busy)";
415 for Index in 1 .. Count loop
417 Container.Length := Container.Length - 1;
419 if X = Container.Last then
420 Position := No_Element;
422 Container.Last := X.Prev;
423 Container.Last.Next := null;
429 Position.Node := X.Next;
431 X.Next.Prev := X.Prev;
432 X.Prev.Next := X.Next;
437 -- Fix this junk comment ???
439 Position := No_Element; -- Post-York behavior
446 procedure Delete_First
447 (Container : in out List;
448 Count : Count_Type := 1)
453 if Count >= Container.Length then
460 elsif Container.Busy > 0 then
461 raise Program_Error with
462 "attempt to tamper with cursors (list is busy)";
465 for J in 1 .. Count loop
466 X := Container.First;
467 pragma Assert (X.Next.Prev = Container.First);
469 Container.First := X.Next;
470 Container.First.Prev := null;
472 Container.Length := Container.Length - 1;
483 procedure Delete_Last
484 (Container : in out List;
485 Count : Count_Type := 1)
490 if Count >= Container.Length then
497 elsif Container.Busy > 0 then
498 raise Program_Error with
499 "attempt to tamper with cursors (list is busy)";
502 for J in 1 .. Count loop
504 pragma Assert (X.Prev.Next = Container.Last);
506 Container.Last := X.Prev;
507 Container.Last.Next := null;
509 Container.Length := Container.Length - 1;
520 function Element (Position : Cursor) return Element_Type is
522 if Position.Node = null then
523 raise Constraint_Error with
524 "Position cursor has no element";
526 elsif Position.Node.Element = null then
527 raise Program_Error with
528 "Position cursor has no element";
531 pragma Assert (Vet (Position), "bad cursor in Element");
533 return Position.Node.Element.all;
541 procedure Finalize (Object : in out Iterator) is
543 if Object.Container /= null then
545 B : Natural renames Object.Container.all.Busy;
552 procedure Finalize (Control : in out Reference_Control_Type) is
554 if Control.Container /= null then
556 C : List renames Control.Container.all;
557 B : Natural renames C.Busy;
558 L : Natural renames C.Lock;
564 Control.Container := null;
575 Position : Cursor := No_Element) return Cursor
577 Node : Node_Access := Position.Node;
581 Node := Container.First;
584 if Node.Element = null then
587 elsif Position.Container /= Container'Unrestricted_Access then
588 raise Program_Error with
589 "Position cursor designates wrong container";
592 pragma Assert (Vet (Position), "bad cursor in Find");
596 -- Per AI05-0022, the container implementation is required to detect
597 -- element tampering by a generic actual subprogram.
600 B : Natural renames Container'Unrestricted_Access.Busy;
601 L : Natural renames Container'Unrestricted_Access.Lock;
603 Result : Node_Access;
610 while Node /= null loop
611 if Node.Element.all = Item then
622 if Result = null then
625 return Cursor'(Container'Unrestricted_Access, Result);
640 function First (Container : List) return Cursor is
642 if Container.First = null then
645 return Cursor'(Container'Unrestricted_Access, Container.First);
649 function First (Object : Iterator) return Cursor is
651 -- The value of the iterator object's Node component influences the
652 -- behavior of the First (and Last) selector function.
654 -- When the Node component is null, this means the iterator object was
655 -- constructed without a start expression, in which case the (forward)
656 -- iteration starts from the (logical) beginning of the entire sequence
657 -- of items (corresponding to Container.First, for a forward iterator).
659 -- Otherwise, this is iteration over a partial sequence of items. When
660 -- the Node component is non-null, the iterator object was constructed
661 -- with a start expression, that specifies the position from which the
662 -- (forward) partial iteration begins.
664 if Object.Node = null then
665 return Indefinite_Doubly_Linked_Lists.First (Object.Container.all);
667 return Cursor'(Object.Container, Object.Node);
675 function First_Element (Container : List) return Element_Type is
677 if Container.First = null then
678 raise Constraint_Error with "list is empty";
680 return Container.First.Element.all;
688 procedure Free (X : in out Node_Access) is
689 procedure Deallocate is
690 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
693 -- While a node is in use, as an active link in a list, its Previous and
694 -- Next components must be null, or designate a different node; this is
695 -- a node invariant. For this indefinite list, there is an additional
696 -- invariant: that the element access value be non-null. Before actually
697 -- deallocating the node, we set the node access value components of the
698 -- node to point to the node itself, and set the element access value to
699 -- null (by deallocating the node's element), thus falsifying the node
700 -- invariant. Subprogram Vet inspects the value of the node components
701 -- when interrogating the node, in order to detect whether the cursor's
702 -- node access value is dangling.
704 -- Note that we have no guarantee that the storage for the node isn't
705 -- modified when it is deallocated, but there are other tests that Vet
706 -- does if node invariants appear to be satisifed. However, in practice
707 -- this simple test works well enough, detecting dangling references
708 -- immediately, without needing further interrogation.
725 ---------------------
726 -- Generic_Sorting --
727 ---------------------
729 package body Generic_Sorting is
735 function Is_Sorted (Container : List) return Boolean is
736 B : Natural renames Container'Unrestricted_Access.Busy;
737 L : Natural renames Container'Unrestricted_Access.Lock;
743 -- Per AI05-0022, the container implementation is required to detect
744 -- element tampering by a generic actual subprogram.
749 Node := Container.First;
751 for J in 2 .. Container.Length loop
752 if Node.Next.Element.all < Node.Element.all then
777 (Target : in out List;
778 Source : in out List)
781 -- The semantics of Merge changed slightly per AI05-0021. It was
782 -- originally the case that if Target and Source denoted the same
783 -- container object, then the GNAT implementation of Merge did
784 -- nothing. However, it was argued that RM05 did not precisely
785 -- specify the semantics for this corner case. The decision of the
786 -- ARG was that if Target and Source denote the same non-empty
787 -- container object, then Program_Error is raised.
789 if Source.Is_Empty then
792 elsif Target'Address = Source'Address then
793 raise Program_Error with
794 "Target and Source denote same non-empty container";
796 elsif Target.Length > Count_Type'Last - Source.Length then
797 raise Constraint_Error with "new length exceeds maximum";
799 elsif Target.Busy > 0 then
800 raise Program_Error with
801 "attempt to tamper with cursors of Target (list is busy)";
803 elsif Source.Busy > 0 then
804 raise Program_Error with
805 "attempt to tamper with cursors of Source (list is busy)";
809 TB : Natural renames Target.Busy;
810 TL : Natural renames Target.Lock;
812 SB : Natural renames Source.Busy;
813 SL : Natural renames Source.Lock;
815 LI, RI, RJ : Node_Access;
826 while RI /= null loop
827 pragma Assert (RI.Next = null
828 or else not (RI.Next.Element.all <
832 Splice_Internal (Target, null, Source);
836 pragma Assert (LI.Next = null
837 or else not (LI.Next.Element.all <
840 if RI.Element.all < LI.Element.all then
843 Splice_Internal (Target, LI, Source, RJ);
872 procedure Sort (Container : in out List) is
873 procedure Partition (Pivot : Node_Access; Back : Node_Access);
876 procedure Sort (Front, Back : Node_Access);
877 -- Comment??? Confusing name??? change name???
883 procedure Partition (Pivot : Node_Access; Back : Node_Access) is
888 while Node /= Back loop
889 if Node.Element.all < Pivot.Element.all then
891 Prev : constant Node_Access := Node.Prev;
892 Next : constant Node_Access := Node.Next;
898 Container.Last := Prev;
904 Node.Prev := Pivot.Prev;
908 if Node.Prev = null then
909 Container.First := Node;
911 Node.Prev.Next := Node;
927 procedure Sort (Front, Back : Node_Access) is
928 Pivot : constant Node_Access :=
929 (if Front = null then Container.First else Front.Next);
931 if Pivot /= Back then
932 Partition (Pivot, Back);
938 -- Start of processing for Sort
941 if Container.Length <= 1 then
945 pragma Assert (Container.First.Prev = null);
946 pragma Assert (Container.Last.Next = null);
948 if Container.Busy > 0 then
949 raise Program_Error with
950 "attempt to tamper with cursors (list is busy)";
953 -- Per AI05-0022, the container implementation is required to detect
954 -- element tampering by a generic actual subprogram.
957 B : Natural renames Container.Busy;
958 L : Natural renames Container.Lock;
964 Sort (Front => null, Back => null);
975 pragma Assert (Container.First.Prev = null);
976 pragma Assert (Container.Last.Next = null);
985 function Has_Element (Position : Cursor) return Boolean is
987 pragma Assert (Vet (Position), "bad cursor in Has_Element");
988 return Position.Node /= null;
996 (Container : in out List;
998 New_Item : Element_Type;
999 Position : out Cursor;
1000 Count : Count_Type := 1)
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 "attempt to tamper with cursors (list is busy)";
1010 elsif Before.Node = null or else Before.Node.Element = null then
1011 raise Program_Error with
1012 "Before cursor has no element";
1015 pragma Assert (Vet (Before), "bad cursor in Insert");
1024 if Container.Length > Count_Type'Last - Count then
1025 raise Constraint_Error with "new length exceeds maximum";
1028 if Container.Busy > 0 then
1029 raise Program_Error with
1030 "attempt to tamper with cursors (list is busy)";
1034 -- The element allocator may need an accessibility check in the case
1035 -- the actual type is class-wide or has access discriminants (see
1036 -- RM 4.8(10.1) and AI12-0035). We don't unsuppress the check on the
1037 -- allocator in the loop below, because the one in this block would
1038 -- have failed already.
1040 pragma Unsuppress (Accessibility_Check);
1042 Element : Element_Access := new Element_Type'(New_Item);
1045 New_Node := new Node_Type'(Element, null, null);
1053 Insert_Internal (Container, Before.Node, New_Node);
1054 Position := Cursor'(Container'Unchecked_Access, New_Node);
1056 for J in 2 .. Count loop
1058 Element : Element_Access := new Element_Type'(New_Item);
1060 New_Node := new Node_Type'(Element, null, null);
1067 Insert_Internal (Container, Before.Node, New_Node);
1072 (Container : in out List;
1074 New_Item : Element_Type;
1075 Count : Count_Type := 1)
1078 pragma Unreferenced (Position);
1080 Insert (Container, Before, New_Item, Position, Count);
1083 ---------------------
1084 -- Insert_Internal --
1085 ---------------------
1087 procedure Insert_Internal
1088 (Container : in out List;
1089 Before : Node_Access;
1090 New_Node : Node_Access)
1093 if Container.Length = 0 then
1094 pragma Assert (Before = null);
1095 pragma Assert (Container.First = null);
1096 pragma Assert (Container.Last = null);
1098 Container.First := New_Node;
1099 Container.Last := New_Node;
1101 elsif Before = null then
1102 pragma Assert (Container.Last.Next = null);
1104 Container.Last.Next := New_Node;
1105 New_Node.Prev := Container.Last;
1107 Container.Last := New_Node;
1109 elsif Before = Container.First then
1110 pragma Assert (Container.First.Prev = null);
1112 Container.First.Prev := New_Node;
1113 New_Node.Next := Container.First;
1115 Container.First := New_Node;
1118 pragma Assert (Container.First.Prev = null);
1119 pragma Assert (Container.Last.Next = null);
1121 New_Node.Next := Before;
1122 New_Node.Prev := Before.Prev;
1124 Before.Prev.Next := New_Node;
1125 Before.Prev := New_Node;
1128 Container.Length := Container.Length + 1;
1129 end Insert_Internal;
1135 function Is_Empty (Container : List) return Boolean is
1137 return Container.Length = 0;
1146 Process : not null access procedure (Position : Cursor))
1148 B : Natural renames Container'Unrestricted_Access.all.Busy;
1149 Node : Node_Access := Container.First;
1155 while Node /= null loop
1156 Process (Cursor'(Container'Unrestricted_Access, Node));
1170 return List_Iterator_Interfaces.Reversible_Iterator'class
1172 B : Natural renames Container'Unrestricted_Access.all.Busy;
1175 -- The value of the Node component influences the behavior of the First
1176 -- and Last selector functions of the iterator object. When the Node
1177 -- component is null (as is the case here), this means the iterator
1178 -- object was constructed without a start expression. This is a
1179 -- complete iterator, meaning that the iteration starts from the
1180 -- (logical) beginning of the sequence of items.
1182 -- Note: For a forward iterator, Container.First is the beginning, and
1183 -- for a reverse iterator, Container.Last is the beginning.
1185 return It : constant Iterator :=
1186 Iterator'(Limited_Controlled with
1187 Container => Container'Unrestricted_Access,
1197 return List_Iterator_Interfaces.Reversible_Iterator'Class
1199 B : Natural renames Container'Unrestricted_Access.all.Busy;
1202 -- It was formerly the case that when Start = No_Element, the partial
1203 -- iterator was defined to behave the same as for a complete iterator,
1204 -- and iterate over the entire sequence of items. However, those
1205 -- semantics were unintuitive and arguably error-prone (it is too easy
1206 -- to accidentally create an endless loop), and so they were changed,
1207 -- per the ARG meeting in Denver on 2011/11. However, there was no
1208 -- consensus about what positive meaning this corner case should have,
1209 -- and so it was decided to simply raise an exception. This does imply,
1210 -- however, that it is not possible to use a partial iterator to specify
1211 -- an empty sequence of items.
1213 if Start = No_Element then
1214 raise Constraint_Error with
1215 "Start position for iterator equals No_Element";
1217 elsif Start.Container /= Container'Unrestricted_Access then
1218 raise Program_Error with
1219 "Start cursor of Iterate designates wrong list";
1222 pragma Assert (Vet (Start), "Start cursor of Iterate is bad");
1224 -- The value of the Node component influences the behavior of the
1225 -- First and Last selector functions of the iterator object. When
1226 -- the Node component is non-null (as is the case here), it means
1227 -- that this is a partial iteration, over a subset of the complete
1228 -- sequence of items. The iterator object was constructed with
1229 -- a start expression, indicating the position from which the
1230 -- iteration begins. Note that the start position has the same value
1231 -- irrespective of whether this is a forward or reverse iteration.
1233 return It : constant Iterator :=
1234 Iterator'(Limited_Controlled with
1235 Container => Container'Unrestricted_Access,
1247 function Last (Container : List) return Cursor is
1249 if Container.Last = null then
1252 return Cursor'(Container'Unrestricted_Access, Container.Last);
1256 function Last (Object : Iterator) return Cursor is
1258 -- The value of the iterator object's Node component influences the
1259 -- behavior of the Last (and First) selector function.
1261 -- When the Node component is null, this means the iterator object was
1262 -- constructed without a start expression, in which case the (reverse)
1263 -- iteration starts from the (logical) beginning of the entire sequence
1264 -- (corresponding to Container.Last, for a reverse iterator).
1266 -- Otherwise, this is iteration over a partial sequence of items. When
1267 -- the Node component is non-null, the iterator object was constructed
1268 -- with a start expression, that specifies the position from which the
1269 -- (reverse) partial iteration begins.
1271 if Object.Node = null then
1272 return Indefinite_Doubly_Linked_Lists.Last (Object.Container.all);
1274 return Cursor'(Object.Container, Object.Node);
1282 function Last_Element (Container : List) return Element_Type is
1284 if Container.Last = null then
1285 raise Constraint_Error with "list is empty";
1287 return Container.Last.Element.all;
1295 function Length (Container : List) return Count_Type is
1297 return Container.Length;
1304 procedure Move (Target : in out List; Source : in out List) is
1306 if Target'Address = Source'Address then
1309 elsif Source.Busy > 0 then
1310 raise Program_Error with
1311 "attempt to tamper with cursors of Source (list is busy)";
1316 Target.First := Source.First;
1317 Source.First := null;
1319 Target.Last := Source.Last;
1320 Source.Last := null;
1322 Target.Length := Source.Length;
1331 procedure Next (Position : in out Cursor) is
1333 Position := Next (Position);
1336 function Next (Position : Cursor) return Cursor is
1338 if Position.Node = null then
1342 pragma Assert (Vet (Position), "bad cursor in Next");
1345 Next_Node : constant Node_Access := Position.Node.Next;
1347 if Next_Node = null then
1350 return Cursor'(Position.Container, Next_Node);
1356 function Next (Object : Iterator; Position : Cursor) return Cursor is
1358 if Position.Container = null then
1360 elsif Position.Container /= Object.Container then
1361 raise Program_Error with
1362 "Position cursor of Next designates wrong list";
1364 return Next (Position);
1373 (Container : in out List;
1374 New_Item : Element_Type;
1375 Count : Count_Type := 1)
1378 Insert (Container, First (Container), New_Item, Count);
1385 procedure Previous (Position : in out Cursor) is
1387 Position := Previous (Position);
1390 function Previous (Position : Cursor) return Cursor is
1392 if Position.Node = null then
1396 pragma Assert (Vet (Position), "bad cursor in Previous");
1399 Prev_Node : constant Node_Access := Position.Node.Prev;
1401 if Prev_Node = null then
1404 return Cursor'(Position.Container, Prev_Node);
1410 function Previous (Object : Iterator; Position : Cursor) return Cursor is
1412 if Position.Container = null then
1414 elsif Position.Container /= Object.Container then
1415 raise Program_Error with
1416 "Position cursor of Previous designates wrong list";
1418 return Previous (Position);
1426 procedure Query_Element
1428 Process : not null access procedure (Element : Element_Type))
1431 if Position.Node = null then
1432 raise Constraint_Error with
1433 "Position cursor has no element";
1435 elsif Position.Node.Element = null then
1436 raise Program_Error with
1437 "Position cursor has no element";
1440 pragma Assert (Vet (Position), "bad cursor in Query_Element");
1443 C : List renames Position.Container.all'Unrestricted_Access.all;
1444 B : Natural renames C.Busy;
1445 L : Natural renames C.Lock;
1452 Process (Position.Node.Element.all);
1471 (Stream : not null access Root_Stream_Type'Class;
1474 N : Count_Type'Base;
1480 Count_Type'Base'Read (Stream, N);
1487 Element : Element_Access :=
1488 new Element_Type'(Element_Type'Input (Stream));
1490 Dst := new Node_Type'(Element, null, null);
1501 while Item.Length < N loop
1503 Element : Element_Access :=
1504 new Element_Type'(Element_Type'Input (Stream));
1506 Dst := new Node_Type'(Element, Next => null, Prev => Item.Last);
1513 Item.Last.Next := Dst;
1515 Item.Length := Item.Length + 1;
1520 (Stream : not null access Root_Stream_Type'Class;
1524 raise Program_Error with "attempt to stream list cursor";
1528 (Stream : not null access Root_Stream_Type'Class;
1529 Item : out Reference_Type)
1532 raise Program_Error with "attempt to stream reference";
1536 (Stream : not null access Root_Stream_Type'Class;
1537 Item : out Constant_Reference_Type)
1540 raise Program_Error with "attempt to stream reference";
1548 (Container : aliased in out List;
1549 Position : Cursor) return Reference_Type
1552 if Position.Container = null then
1553 raise Constraint_Error with "Position cursor has no element";
1555 elsif Position.Container /= Container'Unrestricted_Access then
1556 raise Program_Error with
1557 "Position cursor designates wrong container";
1559 elsif Position.Node.Element = null then
1560 raise Program_Error with "Node has no element";
1563 pragma Assert (Vet (Position), "bad cursor in function Reference");
1566 C : List renames Position.Container.all;
1567 B : Natural renames C.Busy;
1568 L : Natural renames C.Lock;
1570 return R : constant Reference_Type :=
1571 (Element => Position.Node.Element.all'Access,
1572 Control => (Controlled with Position.Container))
1581 ---------------------
1582 -- Replace_Element --
1583 ---------------------
1585 procedure Replace_Element
1586 (Container : in out List;
1588 New_Item : Element_Type)
1591 if Position.Container = null then
1592 raise Constraint_Error with "Position cursor has no element";
1594 elsif Position.Container /= Container'Unchecked_Access then
1595 raise Program_Error with
1596 "Position cursor designates wrong container";
1598 elsif Container.Lock > 0 then
1599 raise Program_Error with
1600 "attempt to tamper with elements (list is locked)";
1602 elsif Position.Node.Element = null then
1603 raise Program_Error with
1604 "Position cursor has no element";
1607 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
1610 -- The element allocator may need an accessibility check in the
1611 -- case the actual type is class-wide or has access discriminants
1612 -- (see RM 4.8(10.1) and AI12-0035).
1614 pragma Unsuppress (Accessibility_Check);
1616 X : Element_Access := Position.Node.Element;
1619 Position.Node.Element := new Element_Type'(New_Item);
1623 end Replace_Element;
1625 ----------------------
1626 -- Reverse_Elements --
1627 ----------------------
1629 procedure Reverse_Elements (Container : in out List) is
1630 I : Node_Access := Container.First;
1631 J : Node_Access := Container.Last;
1633 procedure Swap (L, R : Node_Access);
1639 procedure Swap (L, R : Node_Access) is
1640 LN : constant Node_Access := L.Next;
1641 LP : constant Node_Access := L.Prev;
1643 RN : constant Node_Access := R.Next;
1644 RP : constant Node_Access := R.Prev;
1659 pragma Assert (RP = L);
1673 -- Start of processing for Reverse_Elements
1676 if Container.Length <= 1 then
1680 pragma Assert (Container.First.Prev = null);
1681 pragma Assert (Container.Last.Next = null);
1683 if Container.Busy > 0 then
1684 raise Program_Error with
1685 "attempt to tamper with cursors (list is busy)";
1688 Container.First := J;
1689 Container.Last := I;
1691 Swap (L => I, R => J);
1699 Swap (L => J, R => I);
1708 pragma Assert (Container.First.Prev = null);
1709 pragma Assert (Container.Last.Next = null);
1710 end Reverse_Elements;
1716 function Reverse_Find
1718 Item : Element_Type;
1719 Position : Cursor := No_Element) return Cursor
1721 Node : Node_Access := Position.Node;
1725 Node := Container.Last;
1728 if Node.Element = null then
1729 raise Program_Error with "Position cursor has no element";
1731 elsif Position.Container /= Container'Unrestricted_Access then
1732 raise Program_Error with
1733 "Position cursor designates wrong container";
1736 pragma Assert (Vet (Position), "bad cursor in Reverse_Find");
1740 -- Per AI05-0022, the container implementation is required to detect
1741 -- element tampering by a generic actual subprogram.
1744 B : Natural renames Container'Unrestricted_Access.Busy;
1745 L : Natural renames Container'Unrestricted_Access.Lock;
1747 Result : Node_Access;
1754 while Node /= null loop
1755 if Node.Element.all = Item then
1766 if Result = null then
1769 return Cursor'(Container'Unrestricted_Access, Result);
1780 ---------------------
1781 -- Reverse_Iterate --
1782 ---------------------
1784 procedure Reverse_Iterate
1786 Process : not null access procedure (Position : Cursor))
1788 C : List renames Container'Unrestricted_Access.all;
1789 B : Natural renames C.Busy;
1791 Node : Node_Access := Container.Last;
1797 while Node /= null loop
1798 Process (Cursor'(Container'Unrestricted_Access, Node));
1808 end Reverse_Iterate;
1815 (Target : in out List;
1817 Source : in out List)
1820 if Before.Container /= null then
1821 if Before.Container /= Target'Unrestricted_Access then
1822 raise Program_Error with
1823 "Before cursor designates wrong container";
1825 elsif Before.Node = null or else Before.Node.Element = null then
1826 raise Program_Error with
1827 "Before cursor has no element";
1830 pragma Assert (Vet (Before), "bad cursor in Splice");
1834 if Target'Address = Source'Address or else Source.Length = 0 then
1837 elsif Target.Length > Count_Type'Last - Source.Length then
1838 raise Constraint_Error with "new length exceeds maximum";
1840 elsif Target.Busy > 0 then
1841 raise Program_Error with
1842 "attempt to tamper with cursors of Target (list is busy)";
1844 elsif Source.Busy > 0 then
1845 raise Program_Error with
1846 "attempt to tamper with cursors of Source (list is busy)";
1849 Splice_Internal (Target, Before.Node, Source);
1854 (Container : in out List;
1859 if Before.Container /= null then
1860 if Before.Container /= Container'Unchecked_Access then
1861 raise Program_Error with
1862 "Before cursor designates wrong container";
1864 elsif Before.Node = null or else Before.Node.Element = null then
1865 raise Program_Error with
1866 "Before cursor has no element";
1869 pragma Assert (Vet (Before), "bad Before cursor in Splice");
1873 if Position.Node = null then
1874 raise Constraint_Error with "Position cursor has no element";
1877 if Position.Node.Element = null then
1878 raise Program_Error with "Position cursor has no element";
1881 if Position.Container /= Container'Unrestricted_Access then
1882 raise Program_Error with
1883 "Position cursor designates wrong container";
1886 pragma Assert (Vet (Position), "bad Position cursor in Splice");
1888 if Position.Node = Before.Node
1889 or else Position.Node.Next = Before.Node
1894 pragma Assert (Container.Length >= 2);
1896 if Container.Busy > 0 then
1897 raise Program_Error with
1898 "attempt to tamper with cursors (list is busy)";
1901 if Before.Node = null then
1902 pragma Assert (Position.Node /= Container.Last);
1904 if Position.Node = Container.First then
1905 Container.First := Position.Node.Next;
1906 Container.First.Prev := null;
1908 Position.Node.Prev.Next := Position.Node.Next;
1909 Position.Node.Next.Prev := Position.Node.Prev;
1912 Container.Last.Next := Position.Node;
1913 Position.Node.Prev := Container.Last;
1915 Container.Last := Position.Node;
1916 Container.Last.Next := null;
1921 if Before.Node = Container.First then
1922 pragma Assert (Position.Node /= Container.First);
1924 if Position.Node = Container.Last then
1925 Container.Last := Position.Node.Prev;
1926 Container.Last.Next := null;
1928 Position.Node.Prev.Next := Position.Node.Next;
1929 Position.Node.Next.Prev := Position.Node.Prev;
1932 Container.First.Prev := Position.Node;
1933 Position.Node.Next := Container.First;
1935 Container.First := Position.Node;
1936 Container.First.Prev := null;
1941 if Position.Node = Container.First then
1942 Container.First := Position.Node.Next;
1943 Container.First.Prev := null;
1945 elsif Position.Node = Container.Last then
1946 Container.Last := Position.Node.Prev;
1947 Container.Last.Next := null;
1950 Position.Node.Prev.Next := Position.Node.Next;
1951 Position.Node.Next.Prev := Position.Node.Prev;
1954 Before.Node.Prev.Next := Position.Node;
1955 Position.Node.Prev := Before.Node.Prev;
1957 Before.Node.Prev := Position.Node;
1958 Position.Node.Next := Before.Node;
1960 pragma Assert (Container.First.Prev = null);
1961 pragma Assert (Container.Last.Next = null);
1965 (Target : in out List;
1967 Source : in out List;
1968 Position : in out Cursor)
1971 if Target'Address = Source'Address then
1972 Splice (Target, Before, Position);
1976 if Before.Container /= null then
1977 if Before.Container /= Target'Unrestricted_Access then
1978 raise Program_Error with
1979 "Before cursor designates wrong container";
1982 if Before.Node = null
1983 or else Before.Node.Element = null
1985 raise Program_Error with
1986 "Before cursor has no element";
1989 pragma Assert (Vet (Before), "bad Before cursor in Splice");
1992 if Position.Node = null then
1993 raise Constraint_Error with "Position cursor has no element";
1996 if Position.Node.Element = null then
1997 raise Program_Error with
1998 "Position cursor has no element";
2001 if Position.Container /= Source'Unrestricted_Access then
2002 raise Program_Error with
2003 "Position cursor designates wrong container";
2006 pragma Assert (Vet (Position), "bad Position cursor in Splice");
2008 if Target.Length = Count_Type'Last then
2009 raise Constraint_Error with "Target is full";
2012 if Target.Busy > 0 then
2013 raise Program_Error with
2014 "attempt to tamper with cursors of Target (list is busy)";
2017 if Source.Busy > 0 then
2018 raise Program_Error with
2019 "attempt to tamper with cursors of Source (list is busy)";
2022 Splice_Internal (Target, Before.Node, Source, Position.Node);
2023 Position.Container := Target'Unchecked_Access;
2026 ---------------------
2027 -- Splice_Internal --
2028 ---------------------
2030 procedure Splice_Internal
2031 (Target : in out List;
2032 Before : Node_Access;
2033 Source : in out List)
2036 -- This implements the corresponding Splice operation, after the
2037 -- parameters have been vetted, and corner-cases disposed of.
2039 pragma Assert (Target'Address /= Source'Address);
2040 pragma Assert (Source.Length > 0);
2041 pragma Assert (Source.First /= null);
2042 pragma Assert (Source.First.Prev = null);
2043 pragma Assert (Source.Last /= null);
2044 pragma Assert (Source.Last.Next = null);
2045 pragma Assert (Target.Length <= Count_Type'Last - Source.Length);
2047 if Target.Length = 0 then
2048 pragma Assert (Before = null);
2049 pragma Assert (Target.First = null);
2050 pragma Assert (Target.Last = null);
2052 Target.First := Source.First;
2053 Target.Last := Source.Last;
2055 elsif Before = null then
2056 pragma Assert (Target.Last.Next = null);
2058 Target.Last.Next := Source.First;
2059 Source.First.Prev := Target.Last;
2061 Target.Last := Source.Last;
2063 elsif Before = Target.First then
2064 pragma Assert (Target.First.Prev = null);
2066 Source.Last.Next := Target.First;
2067 Target.First.Prev := Source.Last;
2069 Target.First := Source.First;
2072 pragma Assert (Target.Length >= 2);
2073 Before.Prev.Next := Source.First;
2074 Source.First.Prev := Before.Prev;
2076 Before.Prev := Source.Last;
2077 Source.Last.Next := Before;
2080 Source.First := null;
2081 Source.Last := null;
2083 Target.Length := Target.Length + Source.Length;
2085 end Splice_Internal;
2087 procedure Splice_Internal
2088 (Target : in out List;
2089 Before : Node_Access; -- node of Target
2090 Source : in out List;
2091 Position : Node_Access) -- node of Source
2094 -- This implements the corresponding Splice operation, after the
2095 -- parameters have been vetted.
2097 pragma Assert (Target'Address /= Source'Address);
2098 pragma Assert (Target.Length < Count_Type'Last);
2099 pragma Assert (Source.Length > 0);
2100 pragma Assert (Source.First /= null);
2101 pragma Assert (Source.First.Prev = null);
2102 pragma Assert (Source.Last /= null);
2103 pragma Assert (Source.Last.Next = null);
2104 pragma Assert (Position /= null);
2106 if Position = Source.First then
2107 Source.First := Position.Next;
2109 if Position = Source.Last then
2110 pragma Assert (Source.First = null);
2111 pragma Assert (Source.Length = 1);
2112 Source.Last := null;
2115 Source.First.Prev := null;
2118 elsif Position = Source.Last then
2119 pragma Assert (Source.Length >= 2);
2120 Source.Last := Position.Prev;
2121 Source.Last.Next := null;
2124 pragma Assert (Source.Length >= 3);
2125 Position.Prev.Next := Position.Next;
2126 Position.Next.Prev := Position.Prev;
2129 if Target.Length = 0 then
2130 pragma Assert (Before = null);
2131 pragma Assert (Target.First = null);
2132 pragma Assert (Target.Last = null);
2134 Target.First := Position;
2135 Target.Last := Position;
2137 Target.First.Prev := null;
2138 Target.Last.Next := null;
2140 elsif Before = null then
2141 pragma Assert (Target.Last.Next = null);
2142 Target.Last.Next := Position;
2143 Position.Prev := Target.Last;
2145 Target.Last := Position;
2146 Target.Last.Next := null;
2148 elsif Before = Target.First then
2149 pragma Assert (Target.First.Prev = null);
2150 Target.First.Prev := Position;
2151 Position.Next := Target.First;
2153 Target.First := Position;
2154 Target.First.Prev := null;
2157 pragma Assert (Target.Length >= 2);
2158 Before.Prev.Next := Position;
2159 Position.Prev := Before.Prev;
2161 Before.Prev := Position;
2162 Position.Next := Before;
2165 Target.Length := Target.Length + 1;
2166 Source.Length := Source.Length - 1;
2167 end Splice_Internal;
2174 (Container : in out List;
2178 if I.Node = null then
2179 raise Constraint_Error with "I cursor has no element";
2182 if J.Node = null then
2183 raise Constraint_Error with "J cursor has no element";
2186 if I.Container /= Container'Unchecked_Access then
2187 raise Program_Error with "I cursor designates wrong container";
2190 if J.Container /= Container'Unchecked_Access then
2191 raise Program_Error with "J cursor designates wrong container";
2194 if I.Node = J.Node then
2198 if Container.Lock > 0 then
2199 raise Program_Error with
2200 "attempt to tamper with elements (list is locked)";
2203 pragma Assert (Vet (I), "bad I cursor in Swap");
2204 pragma Assert (Vet (J), "bad J cursor in Swap");
2207 EI_Copy : constant Element_Access := I.Node.Element;
2210 I.Node.Element := J.Node.Element;
2211 J.Node.Element := EI_Copy;
2219 procedure Swap_Links
2220 (Container : in out List;
2224 if I.Node = null then
2225 raise Constraint_Error with "I cursor has no element";
2228 if J.Node = null then
2229 raise Constraint_Error with "J cursor has no element";
2232 if I.Container /= Container'Unrestricted_Access then
2233 raise Program_Error with "I cursor designates wrong container";
2236 if J.Container /= Container'Unrestricted_Access then
2237 raise Program_Error with "J cursor designates wrong container";
2240 if I.Node = J.Node then
2244 if Container.Busy > 0 then
2245 raise Program_Error with
2246 "attempt to tamper with cursors (list is busy)";
2249 pragma Assert (Vet (I), "bad I cursor in Swap_Links");
2250 pragma Assert (Vet (J), "bad J cursor in Swap_Links");
2253 I_Next : constant Cursor := Next (I);
2257 Splice (Container, Before => I, Position => J);
2261 J_Next : constant Cursor := Next (J);
2265 Splice (Container, Before => J, Position => I);
2268 pragma Assert (Container.Length >= 3);
2270 Splice (Container, Before => I_Next, Position => J);
2271 Splice (Container, Before => J_Next, Position => I);
2277 pragma Assert (Container.First.Prev = null);
2278 pragma Assert (Container.Last.Next = null);
2281 --------------------
2282 -- Update_Element --
2283 --------------------
2285 procedure Update_Element
2286 (Container : in out List;
2288 Process : not null access procedure (Element : in out Element_Type))
2291 if Position.Node = null then
2292 raise Constraint_Error with "Position cursor has no element";
2295 if Position.Node.Element = null then
2296 raise Program_Error with
2297 "Position cursor has no element";
2300 if Position.Container /= Container'Unchecked_Access then
2301 raise Program_Error with
2302 "Position cursor designates wrong container";
2305 pragma Assert (Vet (Position), "bad cursor in Update_Element");
2308 B : Natural renames Container.Busy;
2309 L : Natural renames Container.Lock;
2316 Process (Position.Node.Element.all);
2333 function Vet (Position : Cursor) return Boolean is
2335 if Position.Node = null then
2336 return Position.Container = null;
2339 if Position.Container = null then
2343 -- An invariant of a node is that its Previous and Next components can
2344 -- be null, or designate a different node. Also, its element access
2345 -- value must be non-null. Operation Free sets the node access value
2346 -- components of the node to designate the node itself, and the element
2347 -- access value to null, before actually deallocating the node, thus
2348 -- deliberately violating the node invariant. This gives us a simple way
2349 -- to detect a dangling reference to a node.
2351 if Position.Node.Next = Position.Node then
2355 if Position.Node.Prev = Position.Node then
2359 if Position.Node.Element = null then
2363 -- In practice the tests above will detect most instances of a dangling
2364 -- reference. If we get here, it means that the invariants of the
2365 -- designated node are satisfied (they at least appear to be satisfied),
2366 -- so we perform some more tests, to determine whether invariants of the
2367 -- designated list are satisfied too.
2370 L : List renames Position.Container.all;
2373 if L.Length = 0 then
2377 if L.First = null then
2381 if L.Last = null then
2385 if L.First.Prev /= null then
2389 if L.Last.Next /= null then
2393 if Position.Node.Prev = null and then Position.Node /= L.First then
2397 if Position.Node.Next = null and then Position.Node /= L.Last then
2401 if L.Length = 1 then
2402 return L.First = L.Last;
2405 if L.First = L.Last then
2409 if L.First.Next = null then
2413 if L.Last.Prev = null then
2417 if L.First.Next.Prev /= L.First then
2421 if L.Last.Prev.Next /= L.Last then
2425 if L.Length = 2 then
2426 if L.First.Next /= L.Last then
2430 if L.Last.Prev /= L.First then
2437 if L.First.Next = L.Last then
2441 if L.Last.Prev = L.First then
2445 if Position.Node = L.First then
2449 if Position.Node = L.Last then
2453 if Position.Node.Next = null then
2457 if Position.Node.Prev = null then
2461 if Position.Node.Next.Prev /= Position.Node then
2465 if Position.Node.Prev.Next /= Position.Node then
2469 if L.Length = 3 then
2470 if L.First.Next /= Position.Node then
2474 if L.Last.Prev /= Position.Node then
2488 (Stream : not null access Root_Stream_Type'Class;
2491 Node : Node_Access := Item.First;
2494 Count_Type'Base'Write (Stream, Item.Length);
2496 while Node /= null loop
2497 Element_Type'Output (Stream, Node.Element.all);
2503 (Stream : not null access Root_Stream_Type'Class;
2507 raise Program_Error with "attempt to stream list cursor";
2511 (Stream : not null access Root_Stream_Type'Class;
2512 Item : Reference_Type)
2515 raise Program_Error with "attempt to stream reference";
2519 (Stream : not null access Root_Stream_Type'Class;
2520 Item : Constant_Reference_Type)
2523 raise Program_Error with "attempt to stream reference";
2526 end Ada.Containers.Indefinite_Doubly_Linked_Lists;