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-2011, 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 function Vet (Position : Cursor) return Boolean;
73 function "=" (Left, Right : List) return Boolean is
74 L : Node_Access := Left.First;
75 R : Node_Access := Right.First;
78 if Left'Address = Right'Address then
82 if Left.Length /= Right.Length then
86 for J in 1 .. Left.Length loop
87 if L.Element /= R.Element then
102 procedure Adjust (Container : in out List) is
103 Src : Node_Access := Container.First;
107 pragma Assert (Container.Last = null);
108 pragma Assert (Container.Length = 0);
109 pragma Assert (Container.Busy = 0);
110 pragma Assert (Container.Lock = 0);
114 pragma Assert (Container.First.Prev = null);
115 pragma Assert (Container.Last.Next = null);
116 pragma Assert (Container.Length > 0);
118 Container.First := null;
119 Container.Last := null;
120 Container.Length := 0;
124 Container.First := new Node_Type'(Src.Element, null, null);
125 Container.Last := Container.First;
126 Container.Length := 1;
129 while Src /= null loop
130 Container.Last.Next := new Node_Type'(Element => Src.Element,
131 Prev => Container.Last,
133 Container.Last := Container.Last.Next;
134 Container.Length := Container.Length + 1;
145 (Container : in out List;
146 New_Item : Element_Type;
147 Count : Count_Type := 1)
150 Insert (Container, No_Element, New_Item, Count);
157 procedure Assign (Target : in out List; Source : List) is
161 if Target'Address = Source'Address then
167 Node := Source.First;
168 while Node /= null loop
169 Target.Append (Node.Element);
178 procedure Clear (Container : in out List) is
182 if Container.Length = 0 then
183 pragma Assert (Container.First = null);
184 pragma Assert (Container.Last = null);
185 pragma Assert (Container.Busy = 0);
186 pragma Assert (Container.Lock = 0);
190 pragma Assert (Container.First.Prev = null);
191 pragma Assert (Container.Last.Next = null);
193 if Container.Busy > 0 then
194 raise Program_Error with
195 "attempt to tamper with cursors (list is busy)";
198 while Container.Length > 1 loop
199 X := Container.First;
200 pragma Assert (X.Next.Prev = Container.First);
202 Container.First := X.Next;
203 Container.First.Prev := null;
205 Container.Length := Container.Length - 1;
210 X := Container.First;
211 pragma Assert (X = Container.Last);
213 Container.First := null;
214 Container.Last := null;
215 Container.Length := 0;
217 pragma Warnings (Off);
219 pragma Warnings (On);
222 ------------------------
223 -- Constant_Reference --
224 ------------------------
226 function Constant_Reference
227 (Container : aliased List;
228 Position : Cursor) return Constant_Reference_Type
231 if Position.Container = null then
232 raise Constraint_Error with "Position cursor has no element";
235 if Position.Container /= Container'Unrestricted_Access then
236 raise Program_Error with
237 "Position cursor designates wrong container";
240 pragma Assert (Vet (Position), "bad cursor in Constant_Reference");
242 return (Element => Position.Node.Element'Access);
243 end Constant_Reference;
251 Item : Element_Type) return Boolean
254 return Find (Container, Item) /= No_Element;
261 function Copy (Source : List) return List is
263 return Target : List do
264 Target.Assign (Source);
273 (Container : in out List;
274 Position : in out Cursor;
275 Count : Count_Type := 1)
280 if Position.Node = null then
281 raise Constraint_Error with
282 "Position cursor has no element";
285 if Position.Container /= Container'Unrestricted_Access then
286 raise Program_Error with
287 "Position cursor designates wrong container";
290 pragma Assert (Vet (Position), "bad cursor in Delete");
292 if Position.Node = Container.First then
293 Delete_First (Container, Count);
294 Position := No_Element; -- Post-York behavior
299 Position := No_Element; -- Post-York behavior
303 if Container.Busy > 0 then
304 raise Program_Error with
305 "attempt to tamper with cursors (list is busy)";
308 for Index in 1 .. Count loop
310 Container.Length := Container.Length - 1;
312 if X = Container.Last then
313 Position := No_Element;
315 Container.Last := X.Prev;
316 Container.Last.Next := null;
322 Position.Node := X.Next;
324 X.Next.Prev := X.Prev;
325 X.Prev.Next := X.Next;
330 Position := No_Element; -- Post-York behavior
337 procedure Delete_First
338 (Container : in out List;
339 Count : Count_Type := 1)
344 if Count >= Container.Length then
353 if Container.Busy > 0 then
354 raise Program_Error with
355 "attempt to tamper with cursors (list is busy)";
358 for I in 1 .. Count loop
359 X := Container.First;
360 pragma Assert (X.Next.Prev = Container.First);
362 Container.First := X.Next;
363 Container.First.Prev := null;
365 Container.Length := Container.Length - 1;
375 procedure Delete_Last
376 (Container : in out List;
377 Count : Count_Type := 1)
382 if Count >= Container.Length then
391 if Container.Busy > 0 then
392 raise Program_Error with
393 "attempt to tamper with cursors (list is busy)";
396 for I in 1 .. Count loop
398 pragma Assert (X.Prev.Next = Container.Last);
400 Container.Last := X.Prev;
401 Container.Last.Next := null;
403 Container.Length := Container.Length - 1;
413 function Element (Position : Cursor) return Element_Type is
415 if Position.Node = null then
416 raise Constraint_Error with
417 "Position cursor has no element";
420 pragma Assert (Vet (Position), "bad cursor in Element");
422 return Position.Node.Element;
429 procedure Finalize (Object : in out Iterator) is
431 if Object.Container /= null then
433 B : Natural renames Object.Container.all.Busy;
447 Position : Cursor := No_Element) return Cursor
449 Node : Node_Access := Position.Node;
453 Node := Container.First;
456 if Position.Container /= Container'Unrestricted_Access then
457 raise Program_Error with
458 "Position cursor designates wrong container";
461 pragma Assert (Vet (Position), "bad cursor in Find");
464 while Node /= null loop
465 if Node.Element = Item then
466 return Cursor'(Container'Unrestricted_Access, Node);
479 function First (Container : List) return Cursor is
481 if Container.First = null then
485 return Cursor'(Container'Unrestricted_Access, Container.First);
488 function First (Object : Iterator) return Cursor is
490 -- The value of the iterator object's Node component influences the
491 -- behavior of the First (and Last) selector function.
493 -- When the Node component is null, this means the iterator object was
494 -- constructed without a start expression, in which case the (forward)
495 -- iteration starts from the (logical) beginning of the entire sequence
496 -- of items (corresponding to Container.First, for a forward iterator).
498 -- Otherwise, this is iteration over a partial sequence of items. When
499 -- the Node component is non-null, the iterator object was constructed
500 -- with a start expression, that specifies the position from which the
501 -- (forward) partial iteration begins.
503 if Object.Node = null then
504 return Doubly_Linked_Lists.First (Object.Container.all);
506 return Cursor'(Object.Container, Object.Node);
514 function First_Element (Container : List) return Element_Type is
516 if Container.First = null then
517 raise Constraint_Error with "list is empty";
520 return Container.First.Element;
527 procedure Free (X : in out Node_Access) is
528 procedure Deallocate is
529 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
536 ---------------------
537 -- Generic_Sorting --
538 ---------------------
540 package body Generic_Sorting is
546 function Is_Sorted (Container : List) return Boolean is
547 Node : Node_Access := Container.First;
550 for I in 2 .. Container.Length loop
551 if Node.Next.Element < Node.Element then
566 (Target : in out List;
567 Source : in out List)
573 -- The semantics of Merge changed slightly per AI05-0021. It was
574 -- originally the case that if Target and Source denoted the same
575 -- container object, then the GNAT implementation of Merge did
576 -- nothing. However, it was argued that RM05 did not precisely
577 -- specify the semantics for this corner case. The decision of the
578 -- ARG was that if Target and Source denote the same non-empty
579 -- container object, then Program_Error is raised.
581 if Source.Is_Empty then
585 if Target'Address = Source'Address then
586 raise Program_Error with
587 "Target and Source denote same non-empty container";
590 if Target.Busy > 0 then
591 raise Program_Error with
592 "attempt to tamper with cursors of Target (list is busy)";
595 if Source.Busy > 0 then
596 raise Program_Error with
597 "attempt to tamper with cursors of Source (list is busy)";
600 LI := First (Target);
601 RI := First (Source);
602 while RI.Node /= null loop
603 pragma Assert (RI.Node.Next = null
604 or else not (RI.Node.Next.Element <
607 if LI.Node = null then
608 Splice (Target, No_Element, Source);
612 pragma Assert (LI.Node.Next = null
613 or else not (LI.Node.Next.Element <
616 if RI.Node.Element < LI.Node.Element then
619 pragma Warnings (Off, RJ);
621 RI.Node := RI.Node.Next;
622 Splice (Target, LI, Source, RJ);
626 LI.Node := LI.Node.Next;
635 procedure Sort (Container : in out List) is
637 procedure Partition (Pivot : Node_Access; Back : Node_Access);
639 procedure Sort (Front, Back : Node_Access);
645 procedure Partition (Pivot : Node_Access; Back : Node_Access) is
646 Node : Node_Access := Pivot.Next;
649 while Node /= Back loop
650 if Node.Element < Pivot.Element then
652 Prev : constant Node_Access := Node.Prev;
653 Next : constant Node_Access := Node.Next;
659 Container.Last := Prev;
665 Node.Prev := Pivot.Prev;
669 if Node.Prev = null then
670 Container.First := Node;
672 Node.Prev.Next := Node;
688 procedure Sort (Front, Back : Node_Access) is
689 Pivot : constant Node_Access :=
690 (if Front = null then Container.First else Front.Next);
692 if Pivot /= Back then
693 Partition (Pivot, Back);
699 -- Start of processing for Sort
702 if Container.Length <= 1 then
706 pragma Assert (Container.First.Prev = null);
707 pragma Assert (Container.Last.Next = null);
709 if Container.Busy > 0 then
710 raise Program_Error with
711 "attempt to tamper with cursors (list is busy)";
714 Sort (Front => null, Back => null);
716 pragma Assert (Container.First.Prev = null);
717 pragma Assert (Container.Last.Next = null);
726 function Has_Element (Position : Cursor) return Boolean is
728 pragma Assert (Vet (Position), "bad cursor in Has_Element");
729 return Position.Node /= null;
737 (Container : in out List;
739 New_Item : Element_Type;
740 Position : out Cursor;
741 Count : Count_Type := 1)
743 New_Node : Node_Access;
746 if Before.Container /= null then
747 if Before.Container /= Container'Unrestricted_Access then
748 raise Program_Error with
749 "Before cursor designates wrong list";
752 pragma Assert (Vet (Before), "bad cursor in Insert");
760 if Container.Length > Count_Type'Last - Count then
761 raise Constraint_Error with "new length exceeds maximum";
764 if Container.Busy > 0 then
765 raise Program_Error with
766 "attempt to tamper with cursors (list is busy)";
769 New_Node := new Node_Type'(New_Item, null, null);
770 Insert_Internal (Container, Before.Node, New_Node);
772 Position := Cursor'(Container'Unchecked_Access, New_Node);
774 for J in Count_Type'(2) .. Count loop
775 New_Node := new Node_Type'(New_Item, null, null);
776 Insert_Internal (Container, Before.Node, New_Node);
781 (Container : in out List;
783 New_Item : Element_Type;
784 Count : Count_Type := 1)
787 pragma Unreferenced (Position);
789 Insert (Container, Before, New_Item, Position, Count);
793 (Container : in out List;
795 Position : out Cursor;
796 Count : Count_Type := 1)
798 New_Node : Node_Access;
801 if Before.Container /= null then
802 if Before.Container /= Container'Unrestricted_Access then
803 raise Program_Error with
804 "Before cursor designates wrong list";
807 pragma Assert (Vet (Before), "bad cursor in Insert");
815 if Container.Length > Count_Type'Last - Count then
816 raise Constraint_Error with "new length exceeds maximum";
819 if Container.Busy > 0 then
820 raise Program_Error with
821 "attempt to tamper with cursors (list is busy)";
824 New_Node := new Node_Type;
825 Insert_Internal (Container, Before.Node, New_Node);
827 Position := Cursor'(Container'Unchecked_Access, New_Node);
829 for J in Count_Type'(2) .. Count loop
830 New_Node := new Node_Type;
831 Insert_Internal (Container, Before.Node, New_Node);
835 ---------------------
836 -- Insert_Internal --
837 ---------------------
839 procedure Insert_Internal
840 (Container : in out List;
841 Before : Node_Access;
842 New_Node : Node_Access)
845 if Container.Length = 0 then
846 pragma Assert (Before = null);
847 pragma Assert (Container.First = null);
848 pragma Assert (Container.Last = null);
850 Container.First := New_Node;
851 Container.Last := New_Node;
853 elsif Before = null then
854 pragma Assert (Container.Last.Next = null);
856 Container.Last.Next := New_Node;
857 New_Node.Prev := Container.Last;
859 Container.Last := New_Node;
861 elsif Before = Container.First then
862 pragma Assert (Container.First.Prev = null);
864 Container.First.Prev := New_Node;
865 New_Node.Next := Container.First;
867 Container.First := New_Node;
870 pragma Assert (Container.First.Prev = null);
871 pragma Assert (Container.Last.Next = null);
873 New_Node.Next := Before;
874 New_Node.Prev := Before.Prev;
876 Before.Prev.Next := New_Node;
877 Before.Prev := New_Node;
880 Container.Length := Container.Length + 1;
887 function Is_Empty (Container : List) return Boolean is
889 return Container.Length = 0;
898 Process : not null access procedure (Position : Cursor))
900 B : Natural renames Container'Unrestricted_Access.all.Busy;
901 Node : Node_Access := Container.First;
907 while Node /= null loop
908 Process (Cursor'(Container'Unrestricted_Access, Node));
920 function Iterate (Container : List)
921 return List_Iterator_Interfaces.Reversible_Iterator'Class
923 B : Natural renames Container'Unrestricted_Access.all.Busy;
926 -- The value of the Node component influences the behavior of the First
927 -- and Last selector functions of the iterator object. When the Node
928 -- component is null (as is the case here), this means the iterator
929 -- object was constructed without a start expression. This is a
930 -- complete iterator, meaning that the iteration starts from the
931 -- (logical) beginning of the sequence of items.
933 -- Note: For a forward iterator, Container.First is the beginning, and
934 -- for a reverse iterator, Container.Last is the beginning.
936 return It : constant Iterator :=
937 Iterator'(Limited_Controlled with
938 Container => Container'Unrestricted_Access,
945 function Iterate (Container : List; Start : Cursor)
946 return List_Iterator_Interfaces.Reversible_Iterator'Class
948 B : Natural renames Container'Unrestricted_Access.all.Busy;
951 -- It was formerly the case that when Start = No_Element, the partial
952 -- iterator was defined to behave the same as for a complete iterator,
953 -- and iterate over the entire sequence of items. However, those
954 -- semantics were unintuitive and arguably error-prone (it is too easy
955 -- to accidentally create an endless loop), and so they were changed,
956 -- per the ARG meeting in Denver on 2011/11. However, there was no
957 -- consensus about what positive meaning this corner case should have,
958 -- and so it was decided to simply raise an exception. This does imply,
959 -- however, that it is not possible to use a partial iterator to specify
960 -- an empty sequence of items.
962 if Start = No_Element then
963 raise Constraint_Error with
964 "Start position for iterator equals No_Element";
967 if Start.Container /= Container'Unrestricted_Access then
968 raise Program_Error with
969 "Start cursor of Iterate designates wrong list";
972 pragma Assert (Vet (Start), "Start cursor of Iterate is bad");
974 -- The value of the Node component influences the behavior of the First
975 -- and Last selector functions of the iterator object. When the Node
976 -- component is non-null (as is the case here), it means that this
977 -- is a partial iteration, over a subset of the complete sequence of
978 -- items. The iterator object was constructed with a start expression,
979 -- indicating the position from which the iteration begins. Note that
980 -- the start position has the same value irrespective of whether this
981 -- is a forward or reverse iteration.
983 return It : constant Iterator :=
984 Iterator'(Limited_Controlled with
985 Container => Container'Unrestricted_Access,
996 function Last (Container : List) return Cursor is
998 if Container.Last = null then
1002 return Cursor'(Container'Unrestricted_Access, Container.Last);
1005 function Last (Object : Iterator) return Cursor is
1007 -- The value of the iterator object's Node component influences the
1008 -- behavior of the Last (and First) selector function.
1010 -- When the Node component is null, this means the iterator object was
1011 -- constructed without a start expression, in which case the (reverse)
1012 -- iteration starts from the (logical) beginning of the entire sequence
1013 -- (corresponding to Container.Last, for a reverse iterator).
1015 -- Otherwise, this is iteration over a partial sequence of items. When
1016 -- the Node component is non-null, the iterator object was constructed
1017 -- with a start expression, that specifies the position from which the
1018 -- (reverse) partial iteration begins.
1020 if Object.Node = null then
1021 return Doubly_Linked_Lists.Last (Object.Container.all);
1023 return Cursor'(Object.Container, Object.Node);
1031 function Last_Element (Container : List) return Element_Type is
1033 if Container.Last = null then
1034 raise Constraint_Error with "list is empty";
1037 return Container.Last.Element;
1044 function Length (Container : List) return Count_Type is
1046 return Container.Length;
1054 (Target : in out List;
1055 Source : in out List)
1058 if Target'Address = Source'Address then
1062 if Source.Busy > 0 then
1063 raise Program_Error with
1064 "attempt to tamper with cursors of Source (list is busy)";
1069 Target.First := Source.First;
1070 Source.First := null;
1072 Target.Last := Source.Last;
1073 Source.Last := null;
1075 Target.Length := Source.Length;
1083 procedure Next (Position : in out Cursor) is
1085 Position := Next (Position);
1088 function Next (Position : Cursor) return Cursor is
1090 if Position.Node = null then
1094 pragma Assert (Vet (Position), "bad cursor in Next");
1097 Next_Node : constant Node_Access := Position.Node.Next;
1100 if Next_Node = null then
1104 return Cursor'(Position.Container, Next_Node);
1110 Position : Cursor) return Cursor
1113 if Position.Container = null then
1117 if Position.Container /= Object.Container then
1118 raise Program_Error with
1119 "Position cursor of Next designates wrong list";
1122 return Next (Position);
1130 (Container : in out List;
1131 New_Item : Element_Type;
1132 Count : Count_Type := 1)
1135 Insert (Container, First (Container), New_Item, Count);
1142 procedure Previous (Position : in out Cursor) is
1144 Position := Previous (Position);
1147 function Previous (Position : Cursor) return Cursor is
1149 if Position.Node = null then
1153 pragma Assert (Vet (Position), "bad cursor in Previous");
1156 Prev_Node : constant Node_Access := Position.Node.Prev;
1159 if Prev_Node = null then
1163 return Cursor'(Position.Container, Prev_Node);
1169 Position : Cursor) return Cursor
1172 if Position.Container = null then
1176 if Position.Container /= Object.Container then
1177 raise Program_Error with
1178 "Position cursor of Previous designates wrong list";
1181 return Previous (Position);
1188 procedure Query_Element
1190 Process : not null access procedure (Element : Element_Type))
1193 if Position.Node = null then
1194 raise Constraint_Error with
1195 "Position cursor has no element";
1198 pragma Assert (Vet (Position), "bad cursor in Query_Element");
1201 C : List renames Position.Container.all'Unrestricted_Access.all;
1202 B : Natural renames C.Busy;
1203 L : Natural renames C.Lock;
1210 Process (Position.Node.Element);
1228 (Stream : not null access Root_Stream_Type'Class;
1231 N : Count_Type'Base;
1236 Count_Type'Base'Read (Stream, N);
1245 Element_Type'Read (Stream, X.Element);
1256 Item.Length := Item.Length + 1;
1257 exit when Item.Length = N;
1262 Element_Type'Read (Stream, X.Element);
1269 X.Prev := Item.Last;
1270 Item.Last.Next := X;
1276 (Stream : not null access Root_Stream_Type'Class;
1280 raise Program_Error with "attempt to stream list cursor";
1284 (Stream : not null access Root_Stream_Type'Class;
1285 Item : out Reference_Type)
1288 raise Program_Error with "attempt to stream reference";
1292 (Stream : not null access Root_Stream_Type'Class;
1293 Item : out Constant_Reference_Type)
1296 raise Program_Error with "attempt to stream reference";
1304 (Container : aliased in out List;
1305 Position : Cursor) return Reference_Type
1308 if Position.Container = null then
1309 raise Constraint_Error with "Position cursor has no element";
1312 if Position.Container /= Container'Unchecked_Access then
1313 raise Program_Error with
1314 "Position cursor designates wrong container";
1317 pragma Assert (Vet (Position), "bad cursor in function Reference");
1319 return (Element => Position.Node.Element'Access);
1322 ---------------------
1323 -- Replace_Element --
1324 ---------------------
1326 procedure Replace_Element
1327 (Container : in out List;
1329 New_Item : Element_Type)
1332 if Position.Container = null then
1333 raise Constraint_Error with "Position cursor has no element";
1336 if Position.Container /= Container'Unchecked_Access then
1337 raise Program_Error with
1338 "Position cursor designates wrong container";
1341 if Container.Lock > 0 then
1342 raise Program_Error with
1343 "attempt to tamper with elements (list is locked)";
1346 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
1348 Position.Node.Element := New_Item;
1349 end Replace_Element;
1351 ----------------------
1352 -- Reverse_Elements --
1353 ----------------------
1355 procedure Reverse_Elements (Container : in out List) is
1356 I : Node_Access := Container.First;
1357 J : Node_Access := Container.Last;
1359 procedure Swap (L, R : Node_Access);
1365 procedure Swap (L, R : Node_Access) is
1366 LN : constant Node_Access := L.Next;
1367 LP : constant Node_Access := L.Prev;
1369 RN : constant Node_Access := R.Next;
1370 RP : constant Node_Access := R.Prev;
1385 pragma Assert (RP = L);
1399 -- Start of processing for Reverse_Elements
1402 if Container.Length <= 1 then
1406 pragma Assert (Container.First.Prev = null);
1407 pragma Assert (Container.Last.Next = null);
1409 if Container.Busy > 0 then
1410 raise Program_Error with
1411 "attempt to tamper with cursors (list is busy)";
1414 Container.First := J;
1415 Container.Last := I;
1417 Swap (L => I, R => J);
1425 Swap (L => J, R => I);
1434 pragma Assert (Container.First.Prev = null);
1435 pragma Assert (Container.Last.Next = null);
1436 end Reverse_Elements;
1442 function Reverse_Find
1444 Item : Element_Type;
1445 Position : Cursor := No_Element) return Cursor
1447 Node : Node_Access := Position.Node;
1451 Node := Container.Last;
1454 if Position.Container /= Container'Unrestricted_Access then
1455 raise Program_Error with
1456 "Position cursor designates wrong container";
1459 pragma Assert (Vet (Position), "bad cursor in Reverse_Find");
1462 while Node /= null loop
1463 if Node.Element = Item then
1464 return Cursor'(Container'Unrestricted_Access, Node);
1473 ---------------------
1474 -- Reverse_Iterate --
1475 ---------------------
1477 procedure Reverse_Iterate
1479 Process : not null access procedure (Position : Cursor))
1481 C : List renames Container'Unrestricted_Access.all;
1482 B : Natural renames C.Busy;
1484 Node : Node_Access := Container.Last;
1490 while Node /= null loop
1491 Process (Cursor'(Container'Unrestricted_Access, Node));
1502 end Reverse_Iterate;
1509 (Target : in out List;
1511 Source : in out List)
1514 if Before.Container /= null then
1515 if Before.Container /= Target'Unrestricted_Access then
1516 raise Program_Error with
1517 "Before cursor designates wrong container";
1520 pragma Assert (Vet (Before), "bad cursor in Splice");
1523 if Target'Address = Source'Address
1524 or else Source.Length = 0
1529 pragma Assert (Source.First.Prev = null);
1530 pragma Assert (Source.Last.Next = null);
1532 if Target.Length > Count_Type'Last - Source.Length then
1533 raise Constraint_Error with "new length exceeds maximum";
1536 if Target.Busy > 0 then
1537 raise Program_Error with
1538 "attempt to tamper with cursors of Target (list is busy)";
1541 if Source.Busy > 0 then
1542 raise Program_Error with
1543 "attempt to tamper with cursors of Source (list is busy)";
1546 if Target.Length = 0 then
1547 pragma Assert (Target.First = null);
1548 pragma Assert (Target.Last = null);
1549 pragma Assert (Before = No_Element);
1551 Target.First := Source.First;
1552 Target.Last := Source.Last;
1554 elsif Before.Node = null then
1555 pragma Assert (Target.Last.Next = null);
1557 Target.Last.Next := Source.First;
1558 Source.First.Prev := Target.Last;
1560 Target.Last := Source.Last;
1562 elsif Before.Node = Target.First then
1563 pragma Assert (Target.First.Prev = null);
1565 Source.Last.Next := Target.First;
1566 Target.First.Prev := Source.Last;
1568 Target.First := Source.First;
1571 pragma Assert (Target.Length >= 2);
1573 Before.Node.Prev.Next := Source.First;
1574 Source.First.Prev := Before.Node.Prev;
1576 Before.Node.Prev := Source.Last;
1577 Source.Last.Next := Before.Node;
1580 Source.First := null;
1581 Source.Last := null;
1583 Target.Length := Target.Length + Source.Length;
1588 (Container : in out List;
1593 if Before.Container /= null then
1594 if Before.Container /= Container'Unchecked_Access then
1595 raise Program_Error with
1596 "Before cursor designates wrong container";
1599 pragma Assert (Vet (Before), "bad Before cursor in Splice");
1602 if Position.Node = null then
1603 raise Constraint_Error with "Position cursor has no element";
1606 if Position.Container /= Container'Unrestricted_Access then
1607 raise Program_Error with
1608 "Position cursor designates wrong container";
1611 pragma Assert (Vet (Position), "bad Position cursor in Splice");
1613 if Position.Node = Before.Node
1614 or else Position.Node.Next = Before.Node
1619 pragma Assert (Container.Length >= 2);
1621 if Container.Busy > 0 then
1622 raise Program_Error with
1623 "attempt to tamper with cursors (list is busy)";
1626 if Before.Node = null then
1627 pragma Assert (Position.Node /= Container.Last);
1629 if Position.Node = Container.First then
1630 Container.First := Position.Node.Next;
1631 Container.First.Prev := null;
1633 Position.Node.Prev.Next := Position.Node.Next;
1634 Position.Node.Next.Prev := Position.Node.Prev;
1637 Container.Last.Next := Position.Node;
1638 Position.Node.Prev := Container.Last;
1640 Container.Last := Position.Node;
1641 Container.Last.Next := null;
1646 if Before.Node = Container.First then
1647 pragma Assert (Position.Node /= Container.First);
1649 if Position.Node = Container.Last then
1650 Container.Last := Position.Node.Prev;
1651 Container.Last.Next := null;
1653 Position.Node.Prev.Next := Position.Node.Next;
1654 Position.Node.Next.Prev := Position.Node.Prev;
1657 Container.First.Prev := Position.Node;
1658 Position.Node.Next := Container.First;
1660 Container.First := Position.Node;
1661 Container.First.Prev := null;
1666 if Position.Node = Container.First then
1667 Container.First := Position.Node.Next;
1668 Container.First.Prev := null;
1670 elsif Position.Node = Container.Last then
1671 Container.Last := Position.Node.Prev;
1672 Container.Last.Next := null;
1675 Position.Node.Prev.Next := Position.Node.Next;
1676 Position.Node.Next.Prev := Position.Node.Prev;
1679 Before.Node.Prev.Next := Position.Node;
1680 Position.Node.Prev := Before.Node.Prev;
1682 Before.Node.Prev := Position.Node;
1683 Position.Node.Next := Before.Node;
1685 pragma Assert (Container.First.Prev = null);
1686 pragma Assert (Container.Last.Next = null);
1690 (Target : in out List;
1692 Source : in out List;
1693 Position : in out Cursor)
1696 if Target'Address = Source'Address then
1697 Splice (Target, Before, Position);
1701 if Before.Container /= null then
1702 if Before.Container /= Target'Unrestricted_Access then
1703 raise Program_Error with
1704 "Before cursor designates wrong container";
1707 pragma Assert (Vet (Before), "bad Before cursor in Splice");
1710 if Position.Node = null then
1711 raise Constraint_Error with "Position cursor has no element";
1714 if Position.Container /= Source'Unrestricted_Access then
1715 raise Program_Error with
1716 "Position cursor designates wrong container";
1719 pragma Assert (Vet (Position), "bad Position cursor in Splice");
1721 if Target.Length = Count_Type'Last then
1722 raise Constraint_Error with "Target is full";
1725 if Target.Busy > 0 then
1726 raise Program_Error with
1727 "attempt to tamper with cursors of Target (list is busy)";
1730 if Source.Busy > 0 then
1731 raise Program_Error with
1732 "attempt to tamper with cursors of Source (list is busy)";
1735 if Position.Node = Source.First then
1736 Source.First := Position.Node.Next;
1738 if Position.Node = Source.Last then
1739 pragma Assert (Source.First = null);
1740 pragma Assert (Source.Length = 1);
1741 Source.Last := null;
1744 Source.First.Prev := null;
1747 elsif Position.Node = Source.Last then
1748 pragma Assert (Source.Length >= 2);
1749 Source.Last := Position.Node.Prev;
1750 Source.Last.Next := null;
1753 pragma Assert (Source.Length >= 3);
1754 Position.Node.Prev.Next := Position.Node.Next;
1755 Position.Node.Next.Prev := Position.Node.Prev;
1758 if Target.Length = 0 then
1759 pragma Assert (Target.First = null);
1760 pragma Assert (Target.Last = null);
1761 pragma Assert (Before = No_Element);
1763 Target.First := Position.Node;
1764 Target.Last := Position.Node;
1766 Target.First.Prev := null;
1767 Target.Last.Next := null;
1769 elsif Before.Node = null then
1770 pragma Assert (Target.Last.Next = null);
1771 Target.Last.Next := Position.Node;
1772 Position.Node.Prev := Target.Last;
1774 Target.Last := Position.Node;
1775 Target.Last.Next := null;
1777 elsif Before.Node = Target.First then
1778 pragma Assert (Target.First.Prev = null);
1779 Target.First.Prev := Position.Node;
1780 Position.Node.Next := Target.First;
1782 Target.First := Position.Node;
1783 Target.First.Prev := null;
1786 pragma Assert (Target.Length >= 2);
1787 Before.Node.Prev.Next := Position.Node;
1788 Position.Node.Prev := Before.Node.Prev;
1790 Before.Node.Prev := Position.Node;
1791 Position.Node.Next := Before.Node;
1794 Target.Length := Target.Length + 1;
1795 Source.Length := Source.Length - 1;
1797 Position.Container := Target'Unchecked_Access;
1805 (Container : in out List;
1809 if I.Node = null then
1810 raise Constraint_Error with "I cursor has no element";
1813 if J.Node = null then
1814 raise Constraint_Error with "J cursor has no element";
1817 if I.Container /= Container'Unchecked_Access then
1818 raise Program_Error with "I cursor designates wrong container";
1821 if J.Container /= Container'Unchecked_Access then
1822 raise Program_Error with "J cursor designates wrong container";
1825 if I.Node = J.Node then
1829 if Container.Lock > 0 then
1830 raise Program_Error with
1831 "attempt to tamper with elements (list is locked)";
1834 pragma Assert (Vet (I), "bad I cursor in Swap");
1835 pragma Assert (Vet (J), "bad J cursor in Swap");
1838 EI : Element_Type renames I.Node.Element;
1839 EJ : Element_Type renames J.Node.Element;
1841 EI_Copy : constant Element_Type := EI;
1853 procedure Swap_Links
1854 (Container : in out List;
1858 if I.Node = null then
1859 raise Constraint_Error with "I cursor has no element";
1862 if J.Node = null then
1863 raise Constraint_Error with "J cursor has no element";
1866 if I.Container /= Container'Unrestricted_Access then
1867 raise Program_Error with "I cursor designates wrong container";
1870 if J.Container /= Container'Unrestricted_Access then
1871 raise Program_Error with "J cursor designates wrong container";
1874 if I.Node = J.Node then
1878 if Container.Busy > 0 then
1879 raise Program_Error with
1880 "attempt to tamper with cursors (list is busy)";
1883 pragma Assert (Vet (I), "bad I cursor in Swap_Links");
1884 pragma Assert (Vet (J), "bad J cursor in Swap_Links");
1887 I_Next : constant Cursor := Next (I);
1891 Splice (Container, Before => I, Position => J);
1895 J_Next : constant Cursor := Next (J);
1899 Splice (Container, Before => J, Position => I);
1902 pragma Assert (Container.Length >= 3);
1904 Splice (Container, Before => I_Next, Position => J);
1905 Splice (Container, Before => J_Next, Position => I);
1912 --------------------
1913 -- Update_Element --
1914 --------------------
1916 procedure Update_Element
1917 (Container : in out List;
1919 Process : not null access procedure (Element : in out Element_Type))
1922 if Position.Node = null then
1923 raise Constraint_Error with "Position cursor has no element";
1926 if Position.Container /= Container'Unchecked_Access then
1927 raise Program_Error with
1928 "Position cursor designates wrong container";
1931 pragma Assert (Vet (Position), "bad cursor in Update_Element");
1934 B : Natural renames Container.Busy;
1935 L : Natural renames Container.Lock;
1942 Process (Position.Node.Element);
1959 function Vet (Position : Cursor) return Boolean is
1961 if Position.Node = null then
1962 return Position.Container = null;
1965 if Position.Container = null then
1969 if Position.Node.Next = Position.Node then
1973 if Position.Node.Prev = Position.Node then
1978 L : List renames Position.Container.all;
1980 if L.Length = 0 then
1984 if L.First = null then
1988 if L.Last = null then
1992 if L.First.Prev /= null then
1996 if L.Last.Next /= null then
2000 if Position.Node.Prev = null
2001 and then Position.Node /= L.First
2006 -- If we get here, we know that this disjunction is true:
2007 -- Position.Node.Prev /= null or else Position.Node = L.First
2009 if Position.Node.Next = null
2010 and then Position.Node /= L.Last
2015 -- If we get here, we know that this disjunction is true:
2016 -- Position.Node.Next /= null or else Position.Node = L.Last
2018 if L.Length = 1 then
2019 return L.First = L.Last;
2022 if L.First = L.Last then
2026 if L.First.Next = null then
2030 if L.Last.Prev = null then
2034 if L.First.Next.Prev /= L.First then
2038 if L.Last.Prev.Next /= L.Last then
2042 if L.Length = 2 then
2043 if L.First.Next /= L.Last then
2047 if L.Last.Prev /= L.First then
2054 if L.First.Next = L.Last then
2058 if L.Last.Prev = L.First then
2062 -- Eliminate earlier disjunct
2064 if Position.Node = L.First then
2068 -- If we get here, we know (disjunctive syllogism) that this
2069 -- predicate is true: Position.Node.Prev /= null
2071 -- Eliminate earlier disjunct
2073 if Position.Node = L.Last then
2077 -- If we get here, we know (disjunctive syllogism) that this
2078 -- predicate is true: Position.Node.Next /= null
2080 if Position.Node.Next.Prev /= Position.Node then
2084 if Position.Node.Prev.Next /= Position.Node then
2088 if L.Length = 3 then
2089 if L.First.Next /= Position.Node then
2093 if L.Last.Prev /= Position.Node then
2107 (Stream : not null access Root_Stream_Type'Class;
2110 Node : Node_Access := Item.First;
2113 Count_Type'Base'Write (Stream, Item.Length);
2115 while Node /= null loop
2116 Element_Type'Write (Stream, Node.Element);
2122 (Stream : not null access Root_Stream_Type'Class;
2126 raise Program_Error with "attempt to stream list cursor";
2130 (Stream : not null access Root_Stream_Type'Class;
2131 Item : Reference_Type)
2134 raise Program_Error with "attempt to stream reference";
2138 (Stream : not null access Root_Stream_Type'Class;
2139 Item : Constant_Reference_Type)
2142 raise Program_Error with "attempt to stream reference";
2145 end Ada.Containers.Doubly_Linked_Lists;