1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.INDEFINITE_DOUBLY_LINKED_LISTS --
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 System; use type System.Address;
31 with Ada.Unchecked_Deallocation;
33 package body Ada.Containers.Indefinite_Doubly_Linked_Lists is
36 new Ada.Unchecked_Deallocation (Element_Type, Element_Access);
39 List_Iterator_Interfaces.Reversible_Iterator with record
40 Container : List_Access;
44 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
78 if Left'Address = Right'Address then
82 if Left.Length /= Right.Length then
88 for J in 1 .. Left.Length loop
89 if L.Element.all /= R.Element.all then
104 procedure Adjust (Container : in out List) is
105 Src : Node_Access := Container.First;
110 pragma Assert (Container.Last = null);
111 pragma Assert (Container.Length = 0);
112 pragma Assert (Container.Busy = 0);
113 pragma Assert (Container.Lock = 0);
117 pragma Assert (Container.First.Prev = null);
118 pragma Assert (Container.Last.Next = null);
119 pragma Assert (Container.Length > 0);
121 Container.First := null;
122 Container.Last := null;
123 Container.Length := 0;
128 Element : Element_Access := new Element_Type'(Src.Element.all);
130 Dst := new Node_Type'(Element, null, null);
137 Container.First := Dst;
138 Container.Last := Dst;
139 Container.Length := 1;
142 while Src /= null loop
144 Element : Element_Access := new Element_Type'(Src.Element.all);
146 Dst := new Node_Type'(Element, null, Prev => Container.Last);
153 Container.Last.Next := Dst;
154 Container.Last := Dst;
155 Container.Length := Container.Length + 1;
166 (Container : in out List;
167 New_Item : Element_Type;
168 Count : Count_Type := 1)
171 Insert (Container, No_Element, New_Item, Count);
178 procedure Clear (Container : in out List) is
180 pragma Warnings (Off, X);
183 if Container.Length = 0 then
184 pragma Assert (Container.First = null);
185 pragma Assert (Container.Last = null);
186 pragma Assert (Container.Busy = 0);
187 pragma Assert (Container.Lock = 0);
191 pragma Assert (Container.First.Prev = null);
192 pragma Assert (Container.Last.Next = null);
194 if Container.Busy > 0 then
195 raise Program_Error with
196 "attempt to tamper with cursors (list is busy)";
199 while Container.Length > 1 loop
200 X := Container.First;
201 pragma Assert (X.Next.Prev = Container.First);
203 Container.First := X.Next;
204 Container.First.Prev := null;
206 Container.Length := Container.Length - 1;
211 X := Container.First;
212 pragma Assert (X = Container.Last);
214 Container.First := null;
215 Container.Last := null;
216 Container.Length := 0;
227 Item : Element_Type) return Boolean
230 return Find (Container, Item) /= No_Element;
238 (Container : in out List;
239 Position : in out Cursor;
240 Count : Count_Type := 1)
245 if Position.Node = null then
246 raise Constraint_Error with
247 "Position cursor has no element";
250 if Position.Node.Element = null then
251 raise Program_Error with
252 "Position cursor has no element";
255 if Position.Container /= Container'Unrestricted_Access then
256 raise Program_Error with
257 "Position cursor designates wrong container";
260 pragma Assert (Vet (Position), "bad cursor in Delete");
262 if Position.Node = Container.First then
263 Delete_First (Container, Count);
264 Position := No_Element; -- Post-York behavior
269 Position := No_Element; -- Post-York behavior
273 if Container.Busy > 0 then
274 raise Program_Error with
275 "attempt to tamper with cursors (list is busy)";
278 for Index in 1 .. Count loop
280 Container.Length := Container.Length - 1;
282 if X = Container.Last then
283 Position := No_Element;
285 Container.Last := X.Prev;
286 Container.Last.Next := null;
292 Position.Node := X.Next;
294 X.Next.Prev := X.Prev;
295 X.Prev.Next := X.Next;
300 Position := No_Element; -- Post-York behavior
307 procedure Delete_First
308 (Container : in out List;
309 Count : Count_Type := 1)
314 if Count >= Container.Length then
323 if Container.Busy > 0 then
324 raise Program_Error with
325 "attempt to tamper with cursors (list is busy)";
328 for I in 1 .. Count loop
329 X := Container.First;
330 pragma Assert (X.Next.Prev = Container.First);
332 Container.First := X.Next;
333 Container.First.Prev := null;
335 Container.Length := Container.Length - 1;
345 procedure Delete_Last
346 (Container : in out List;
347 Count : Count_Type := 1)
352 if Count >= Container.Length then
361 if Container.Busy > 0 then
362 raise Program_Error with
363 "attempt to tamper with cursors (list is busy)";
366 for I in 1 .. Count loop
368 pragma Assert (X.Prev.Next = Container.Last);
370 Container.Last := X.Prev;
371 Container.Last.Next := null;
373 Container.Length := Container.Length - 1;
383 function Element (Position : Cursor) return Element_Type is
385 if Position.Node = null then
386 raise Constraint_Error with
387 "Position cursor has no element";
390 if Position.Node.Element = null then
391 raise Program_Error with
392 "Position cursor has no element";
395 pragma Assert (Vet (Position), "bad cursor in Element");
397 return Position.Node.Element.all;
407 Position : Cursor := No_Element) return Cursor
409 Node : Node_Access := Position.Node;
413 Node := Container.First;
416 if Node.Element = null then
420 if Position.Container /= Container'Unrestricted_Access then
421 raise Program_Error with
422 "Position cursor designates wrong container";
425 pragma Assert (Vet (Position), "bad cursor in Find");
428 while Node /= null loop
429 if Node.Element.all = Item then
430 return Cursor'(Container'Unchecked_Access, Node);
443 function First (Container : List) return Cursor is
445 if Container.First = null then
449 return Cursor'(Container'Unchecked_Access, Container.First);
452 function First (Object : Iterator) return Cursor is
454 if Object.Container = null then
457 return Cursor'(Object.Container, Object.Container.First);
465 function First_Element (Container : List) return Element_Type is
467 if Container.First = null then
468 raise Constraint_Error with "list is empty";
471 return Container.First.Element.all;
478 procedure Free (X : in out Node_Access) is
479 procedure Deallocate is
480 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
498 ---------------------
499 -- Generic_Sorting --
500 ---------------------
502 package body Generic_Sorting is
508 function Is_Sorted (Container : List) return Boolean is
509 Node : Node_Access := Container.First;
512 for I in 2 .. Container.Length loop
513 if Node.Next.Element.all < Node.Element.all then
528 (Target : in out List;
529 Source : in out List)
534 if Target'Address = Source'Address then
538 if Target.Busy > 0 then
539 raise Program_Error with
540 "attempt to tamper with cursors of Target (list is busy)";
543 if Source.Busy > 0 then
544 raise Program_Error with
545 "attempt to tamper with cursors of Source (list is busy)";
548 LI := First (Target);
549 RI := First (Source);
550 while RI.Node /= null loop
551 pragma Assert (RI.Node.Next = null
552 or else not (RI.Node.Next.Element.all <
553 RI.Node.Element.all));
555 if LI.Node = null then
556 Splice (Target, No_Element, Source);
560 pragma Assert (LI.Node.Next = null
561 or else not (LI.Node.Next.Element.all <
562 LI.Node.Element.all));
564 if RI.Node.Element.all < LI.Node.Element.all then
567 pragma Warnings (Off, RJ);
569 RI.Node := RI.Node.Next;
570 Splice (Target, LI, Source, RJ);
574 LI.Node := LI.Node.Next;
583 procedure Sort (Container : in out List) is
584 procedure Partition (Pivot : Node_Access; Back : Node_Access);
586 procedure Sort (Front, Back : Node_Access);
592 procedure Partition (Pivot : Node_Access; Back : Node_Access) is
593 Node : Node_Access := Pivot.Next;
596 while Node /= Back loop
597 if Node.Element.all < Pivot.Element.all then
599 Prev : constant Node_Access := Node.Prev;
600 Next : constant Node_Access := Node.Next;
605 Container.Last := Prev;
611 Node.Prev := Pivot.Prev;
615 if Node.Prev = null then
616 Container.First := Node;
618 Node.Prev.Next := Node;
634 procedure Sort (Front, Back : Node_Access) is
635 Pivot : constant Node_Access :=
636 (if Front = null then Container.First else Front.Next);
638 if Pivot /= Back then
639 Partition (Pivot, Back);
645 -- Start of processing for Sort
648 if Container.Length <= 1 then
652 pragma Assert (Container.First.Prev = null);
653 pragma Assert (Container.Last.Next = null);
655 if Container.Busy > 0 then
656 raise Program_Error with
657 "attempt to tamper with cursors (list is busy)";
660 Sort (Front => null, Back => null);
662 pragma Assert (Container.First.Prev = null);
663 pragma Assert (Container.Last.Next = null);
672 function Has_Element (Position : Cursor) return Boolean is
674 pragma Assert (Vet (Position), "bad cursor in Has_Element");
675 return Position.Node /= null;
683 (Container : in out List;
685 New_Item : Element_Type;
686 Position : out Cursor;
687 Count : Count_Type := 1)
689 New_Node : Node_Access;
692 if Before.Container /= null then
693 if Before.Container /= Container'Unrestricted_Access then
694 raise Program_Error with
695 "attempt to tamper with cursors (list is busy)";
698 if Before.Node = null
699 or else Before.Node.Element = null
701 raise Program_Error with
702 "Before cursor has no element";
705 pragma Assert (Vet (Before), "bad cursor in Insert");
713 if Container.Length > Count_Type'Last - Count then
714 raise Constraint_Error with "new length exceeds maximum";
717 if Container.Busy > 0 then
718 raise Program_Error with
719 "attempt to tamper with cursors (list is busy)";
723 Element : Element_Access := new Element_Type'(New_Item);
725 New_Node := new Node_Type'(Element, null, null);
732 Insert_Internal (Container, Before.Node, New_Node);
733 Position := Cursor'(Container'Unchecked_Access, New_Node);
735 for J in Count_Type'(2) .. Count loop
738 Element : Element_Access := new Element_Type'(New_Item);
740 New_Node := new Node_Type'(Element, null, null);
747 Insert_Internal (Container, Before.Node, New_Node);
752 (Container : in out List;
754 New_Item : Element_Type;
755 Count : Count_Type := 1)
758 pragma Unreferenced (Position);
760 Insert (Container, Before, New_Item, Position, Count);
763 ---------------------
764 -- Insert_Internal --
765 ---------------------
767 procedure Insert_Internal
768 (Container : in out List;
769 Before : Node_Access;
770 New_Node : Node_Access)
773 if Container.Length = 0 then
774 pragma Assert (Before = null);
775 pragma Assert (Container.First = null);
776 pragma Assert (Container.Last = null);
778 Container.First := New_Node;
779 Container.Last := New_Node;
781 elsif Before = null then
782 pragma Assert (Container.Last.Next = null);
784 Container.Last.Next := New_Node;
785 New_Node.Prev := Container.Last;
787 Container.Last := New_Node;
789 elsif Before = Container.First then
790 pragma Assert (Container.First.Prev = null);
792 Container.First.Prev := New_Node;
793 New_Node.Next := Container.First;
795 Container.First := New_Node;
798 pragma Assert (Container.First.Prev = null);
799 pragma Assert (Container.Last.Next = null);
801 New_Node.Next := Before;
802 New_Node.Prev := Before.Prev;
804 Before.Prev.Next := New_Node;
805 Before.Prev := New_Node;
808 Container.Length := Container.Length + 1;
815 function Is_Empty (Container : List) return Boolean is
817 return Container.Length = 0;
826 Process : not null access procedure (Position : Cursor))
828 C : List renames Container'Unrestricted_Access.all;
829 B : Natural renames C.Busy;
831 Node : Node_Access := Container.First;
837 while Node /= null loop
838 Process (Cursor'(Container'Unchecked_Access, Node));
852 return List_Iterator_Interfaces.Reversible_Iterator'class
855 if Container.Length = 0 then
856 return Iterator'(null, null);
858 return Iterator'(Container'Unchecked_Access, Container.First);
865 return List_Iterator_Interfaces.Reversible_Iterator'class
867 It : constant Iterator := (Container'Unchecked_Access, Start.Node);
876 function Last (Container : List) return Cursor is
878 if Container.Last = null then
882 return Cursor'(Container'Unchecked_Access, Container.Last);
885 function Last (Object : Iterator) return Cursor is
887 if Object.Container = null then
890 return Cursor'(Object.Container, Object.Container.Last);
898 function Last_Element (Container : List) return Element_Type is
900 if Container.Last = null then
901 raise Constraint_Error with "list is empty";
904 return Container.Last.Element.all;
911 function Length (Container : List) return Count_Type is
913 return Container.Length;
920 procedure Move (Target : in out List; Source : in out List) is
922 if Target'Address = Source'Address then
926 if Source.Busy > 0 then
927 raise Program_Error with
928 "attempt to tamper with cursors of Source (list is busy)";
933 Target.First := Source.First;
934 Source.First := null;
936 Target.Last := Source.Last;
939 Target.Length := Source.Length;
947 procedure Next (Position : in out Cursor) is
949 Position := Next (Position);
952 function Next (Position : Cursor) return Cursor is
954 if Position.Node = null then
958 pragma Assert (Vet (Position), "bad cursor in Next");
961 Next_Node : constant Node_Access := Position.Node.Next;
963 if Next_Node = null then
967 return Cursor'(Position.Container, Next_Node);
971 function Next (Object : Iterator; Position : Cursor) return Cursor is
973 if Position.Node = Object.Container.Last then
977 return (Object.Container, Position.Node.Next);
986 (Container : in out List;
987 New_Item : Element_Type;
988 Count : Count_Type := 1)
991 Insert (Container, First (Container), New_Item, Count);
998 procedure Previous (Position : in out Cursor) is
1000 Position := Previous (Position);
1003 function Previous (Position : Cursor) return Cursor is
1005 if Position.Node = null then
1009 pragma Assert (Vet (Position), "bad cursor in Previous");
1012 Prev_Node : constant Node_Access := Position.Node.Prev;
1014 if Prev_Node = null then
1018 return Cursor'(Position.Container, Prev_Node);
1022 function Previous (Object : Iterator; Position : Cursor) return Cursor is
1024 if Position.Node = Position.Container.First then
1027 return (Object.Container, Position.Node.Prev);
1035 procedure Query_Element
1037 Process : not null access procedure (Element : Element_Type))
1040 if Position.Node = null then
1041 raise Constraint_Error with
1042 "Position cursor has no element";
1045 if Position.Node.Element = null then
1046 raise Program_Error with
1047 "Position cursor has no element";
1050 pragma Assert (Vet (Position), "bad cursor in Query_Element");
1053 C : List renames Position.Container.all'Unrestricted_Access.all;
1054 B : Natural renames C.Busy;
1055 L : Natural renames C.Lock;
1062 Process (Position.Node.Element.all);
1080 (Stream : not null access Root_Stream_Type'Class;
1083 N : Count_Type'Base;
1089 Count_Type'Base'Read (Stream, N);
1096 Element : Element_Access :=
1097 new Element_Type'(Element_Type'Input (Stream));
1099 Dst := new Node_Type'(Element, null, null);
1110 while Item.Length < N loop
1112 Element : Element_Access :=
1113 new Element_Type'(Element_Type'Input (Stream));
1115 Dst := new Node_Type'(Element, Next => null, Prev => Item.Last);
1122 Item.Last.Next := Dst;
1124 Item.Length := Item.Length + 1;
1129 (Stream : not null access Root_Stream_Type'Class;
1133 raise Program_Error with "attempt to stream list cursor";
1137 (Stream : not null access Root_Stream_Type'Class;
1138 Item : out Reference_Type)
1141 raise Program_Error with "attempt to stream reference";
1145 (Stream : not null access Root_Stream_Type'Class;
1146 Item : out Constant_Reference_Type)
1149 raise Program_Error with "attempt to stream reference";
1156 function Constant_Reference (Container : List; Position : Cursor)
1157 return Constant_Reference_Type is
1159 pragma Unreferenced (Container);
1161 if Position.Container = null then
1162 raise Constraint_Error with "Position cursor has no element";
1165 return (Element => Position.Node.Element.all'Access);
1166 end Constant_Reference;
1168 function Reference (Container : List; Position : Cursor)
1169 return Reference_Type is
1171 pragma Unreferenced (Container);
1173 if Position.Container = null then
1174 raise Constraint_Error with "Position cursor has no element";
1177 return (Element => Position.Node.Element.all'Access);
1180 ---------------------
1181 -- Replace_Element --
1182 ---------------------
1184 procedure Replace_Element
1185 (Container : in out List;
1187 New_Item : Element_Type)
1190 if Position.Container = null then
1191 raise Constraint_Error with "Position cursor has no element";
1194 if Position.Container /= Container'Unchecked_Access then
1195 raise Program_Error with
1196 "Position cursor designates wrong container";
1199 if Container.Lock > 0 then
1200 raise Program_Error with
1201 "attempt to tamper with elements (list is locked)";
1204 if Position.Node.Element = null then
1205 raise Program_Error with
1206 "Position cursor has no element";
1209 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
1212 X : Element_Access := Position.Node.Element;
1215 Position.Node.Element := new Element_Type'(New_Item);
1218 end Replace_Element;
1220 ----------------------
1221 -- Reverse_Elements --
1222 ----------------------
1224 procedure Reverse_Elements (Container : in out List) is
1225 I : Node_Access := Container.First;
1226 J : Node_Access := Container.Last;
1228 procedure Swap (L, R : Node_Access);
1234 procedure Swap (L, R : Node_Access) is
1235 LN : constant Node_Access := L.Next;
1236 LP : constant Node_Access := L.Prev;
1238 RN : constant Node_Access := R.Next;
1239 RP : constant Node_Access := R.Prev;
1254 pragma Assert (RP = L);
1268 -- Start of processing for Reverse_Elements
1271 if Container.Length <= 1 then
1275 pragma Assert (Container.First.Prev = null);
1276 pragma Assert (Container.Last.Next = null);
1278 if Container.Busy > 0 then
1279 raise Program_Error with
1280 "attempt to tamper with cursors (list is busy)";
1283 Container.First := J;
1284 Container.Last := I;
1286 Swap (L => I, R => J);
1294 Swap (L => J, R => I);
1303 pragma Assert (Container.First.Prev = null);
1304 pragma Assert (Container.Last.Next = null);
1305 end Reverse_Elements;
1311 function Reverse_Find
1313 Item : Element_Type;
1314 Position : Cursor := No_Element) return Cursor
1316 Node : Node_Access := Position.Node;
1320 Node := Container.Last;
1323 if Node.Element = null then
1324 raise Program_Error with "Position cursor has no element";
1327 if Position.Container /= Container'Unrestricted_Access then
1328 raise Program_Error with
1329 "Position cursor designates wrong container";
1332 pragma Assert (Vet (Position), "bad cursor in Reverse_Find");
1335 while Node /= null loop
1336 if Node.Element.all = Item then
1337 return Cursor'(Container'Unchecked_Access, Node);
1346 ---------------------
1347 -- Reverse_Iterate --
1348 ---------------------
1350 procedure Reverse_Iterate
1352 Process : not null access procedure (Position : Cursor))
1354 C : List renames Container'Unrestricted_Access.all;
1355 B : Natural renames C.Busy;
1357 Node : Node_Access := Container.Last;
1363 while Node /= null loop
1364 Process (Cursor'(Container'Unchecked_Access, Node));
1374 end Reverse_Iterate;
1381 (Target : in out List;
1383 Source : in out List)
1386 if Before.Container /= null then
1387 if Before.Container /= Target'Unrestricted_Access then
1388 raise Program_Error with
1389 "Before cursor designates wrong container";
1392 if Before.Node = null
1393 or else Before.Node.Element = null
1395 raise Program_Error with
1396 "Before cursor has no element";
1399 pragma Assert (Vet (Before), "bad cursor in Splice");
1402 if Target'Address = Source'Address
1403 or else Source.Length = 0
1408 pragma Assert (Source.First.Prev = null);
1409 pragma Assert (Source.Last.Next = null);
1411 if Target.Length > Count_Type'Last - Source.Length then
1412 raise Constraint_Error with "new length exceeds maximum";
1415 if Target.Busy > 0 then
1416 raise Program_Error with
1417 "attempt to tamper with cursors of Target (list is busy)";
1420 if Source.Busy > 0 then
1421 raise Program_Error with
1422 "attempt to tamper with cursors of Source (list is busy)";
1425 if Target.Length = 0 then
1426 pragma Assert (Before = No_Element);
1427 pragma Assert (Target.First = null);
1428 pragma Assert (Target.Last = null);
1430 Target.First := Source.First;
1431 Target.Last := Source.Last;
1433 elsif Before.Node = null then
1434 pragma Assert (Target.Last.Next = null);
1436 Target.Last.Next := Source.First;
1437 Source.First.Prev := Target.Last;
1439 Target.Last := Source.Last;
1441 elsif Before.Node = Target.First then
1442 pragma Assert (Target.First.Prev = null);
1444 Source.Last.Next := Target.First;
1445 Target.First.Prev := Source.Last;
1447 Target.First := Source.First;
1450 pragma Assert (Target.Length >= 2);
1451 Before.Node.Prev.Next := Source.First;
1452 Source.First.Prev := Before.Node.Prev;
1454 Before.Node.Prev := Source.Last;
1455 Source.Last.Next := Before.Node;
1458 Source.First := null;
1459 Source.Last := null;
1461 Target.Length := Target.Length + Source.Length;
1466 (Container : in out List;
1471 if Before.Container /= null then
1472 if Before.Container /= Container'Unchecked_Access then
1473 raise Program_Error with
1474 "Before cursor designates wrong container";
1477 if Before.Node = null
1478 or else Before.Node.Element = null
1480 raise Program_Error with
1481 "Before cursor has no element";
1484 pragma Assert (Vet (Before), "bad Before cursor in Splice");
1487 if Position.Node = null then
1488 raise Constraint_Error with "Position cursor has no element";
1491 if Position.Node.Element = null then
1492 raise Program_Error with "Position cursor has no element";
1495 if Position.Container /= Container'Unrestricted_Access then
1496 raise Program_Error with
1497 "Position cursor designates wrong container";
1500 pragma Assert (Vet (Position), "bad Position cursor in Splice");
1502 if Position.Node = Before.Node
1503 or else Position.Node.Next = Before.Node
1508 pragma Assert (Container.Length >= 2);
1510 if Container.Busy > 0 then
1511 raise Program_Error with
1512 "attempt to tamper with cursors (list is busy)";
1515 if Before.Node = null then
1516 pragma Assert (Position.Node /= Container.Last);
1518 if Position.Node = Container.First then
1519 Container.First := Position.Node.Next;
1520 Container.First.Prev := null;
1522 Position.Node.Prev.Next := Position.Node.Next;
1523 Position.Node.Next.Prev := Position.Node.Prev;
1526 Container.Last.Next := Position.Node;
1527 Position.Node.Prev := Container.Last;
1529 Container.Last := Position.Node;
1530 Container.Last.Next := null;
1535 if Before.Node = Container.First then
1536 pragma Assert (Position.Node /= Container.First);
1538 if Position.Node = Container.Last then
1539 Container.Last := Position.Node.Prev;
1540 Container.Last.Next := null;
1542 Position.Node.Prev.Next := Position.Node.Next;
1543 Position.Node.Next.Prev := Position.Node.Prev;
1546 Container.First.Prev := Position.Node;
1547 Position.Node.Next := Container.First;
1549 Container.First := Position.Node;
1550 Container.First.Prev := null;
1555 if Position.Node = Container.First then
1556 Container.First := Position.Node.Next;
1557 Container.First.Prev := null;
1559 elsif Position.Node = Container.Last then
1560 Container.Last := Position.Node.Prev;
1561 Container.Last.Next := null;
1564 Position.Node.Prev.Next := Position.Node.Next;
1565 Position.Node.Next.Prev := Position.Node.Prev;
1568 Before.Node.Prev.Next := Position.Node;
1569 Position.Node.Prev := Before.Node.Prev;
1571 Before.Node.Prev := Position.Node;
1572 Position.Node.Next := Before.Node;
1574 pragma Assert (Container.First.Prev = null);
1575 pragma Assert (Container.Last.Next = null);
1579 (Target : in out List;
1581 Source : in out List;
1582 Position : in out Cursor)
1585 if Target'Address = Source'Address then
1586 Splice (Target, Before, Position);
1590 if Before.Container /= null then
1591 if Before.Container /= Target'Unrestricted_Access then
1592 raise Program_Error with
1593 "Before cursor designates wrong container";
1596 if Before.Node = null
1597 or else Before.Node.Element = null
1599 raise Program_Error with
1600 "Before cursor has no element";
1603 pragma Assert (Vet (Before), "bad Before cursor in Splice");
1606 if Position.Node = null then
1607 raise Constraint_Error with "Position cursor has no element";
1610 if Position.Node.Element = null then
1611 raise Program_Error with
1612 "Position cursor has no element";
1615 if Position.Container /= Source'Unrestricted_Access then
1616 raise Program_Error with
1617 "Position cursor designates wrong container";
1620 pragma Assert (Vet (Position), "bad Position cursor in Splice");
1622 if Target.Length = Count_Type'Last then
1623 raise Constraint_Error with "Target is full";
1626 if Target.Busy > 0 then
1627 raise Program_Error with
1628 "attempt to tamper with cursors of Target (list is busy)";
1631 if Source.Busy > 0 then
1632 raise Program_Error with
1633 "attempt to tamper with cursors of Source (list is busy)";
1636 if Position.Node = Source.First then
1637 Source.First := Position.Node.Next;
1639 if Position.Node = Source.Last then
1640 pragma Assert (Source.First = null);
1641 pragma Assert (Source.Length = 1);
1642 Source.Last := null;
1645 Source.First.Prev := null;
1648 elsif Position.Node = Source.Last then
1649 pragma Assert (Source.Length >= 2);
1650 Source.Last := Position.Node.Prev;
1651 Source.Last.Next := null;
1654 pragma Assert (Source.Length >= 3);
1655 Position.Node.Prev.Next := Position.Node.Next;
1656 Position.Node.Next.Prev := Position.Node.Prev;
1659 if Target.Length = 0 then
1660 pragma Assert (Before = No_Element);
1661 pragma Assert (Target.First = null);
1662 pragma Assert (Target.Last = null);
1664 Target.First := Position.Node;
1665 Target.Last := Position.Node;
1667 Target.First.Prev := null;
1668 Target.Last.Next := null;
1670 elsif Before.Node = null then
1671 pragma Assert (Target.Last.Next = null);
1672 Target.Last.Next := Position.Node;
1673 Position.Node.Prev := Target.Last;
1675 Target.Last := Position.Node;
1676 Target.Last.Next := null;
1678 elsif Before.Node = Target.First then
1679 pragma Assert (Target.First.Prev = null);
1680 Target.First.Prev := Position.Node;
1681 Position.Node.Next := Target.First;
1683 Target.First := Position.Node;
1684 Target.First.Prev := null;
1687 pragma Assert (Target.Length >= 2);
1688 Before.Node.Prev.Next := Position.Node;
1689 Position.Node.Prev := Before.Node.Prev;
1691 Before.Node.Prev := Position.Node;
1692 Position.Node.Next := Before.Node;
1695 Target.Length := Target.Length + 1;
1696 Source.Length := Source.Length - 1;
1698 Position.Container := Target'Unchecked_Access;
1706 (Container : in out List;
1710 if I.Node = null then
1711 raise Constraint_Error with "I cursor has no element";
1714 if J.Node = null then
1715 raise Constraint_Error with "J cursor has no element";
1718 if I.Container /= Container'Unchecked_Access then
1719 raise Program_Error with "I cursor designates wrong container";
1722 if J.Container /= Container'Unchecked_Access then
1723 raise Program_Error with "J cursor designates wrong container";
1726 if I.Node = J.Node then
1730 if Container.Lock > 0 then
1731 raise Program_Error with
1732 "attempt to tamper with elements (list is locked)";
1735 pragma Assert (Vet (I), "bad I cursor in Swap");
1736 pragma Assert (Vet (J), "bad J cursor in Swap");
1739 EI_Copy : constant Element_Access := I.Node.Element;
1742 I.Node.Element := J.Node.Element;
1743 J.Node.Element := EI_Copy;
1751 procedure Swap_Links
1752 (Container : in out List;
1756 if I.Node = null then
1757 raise Constraint_Error with "I cursor has no element";
1760 if J.Node = null then
1761 raise Constraint_Error with "J cursor has no element";
1764 if I.Container /= Container'Unrestricted_Access then
1765 raise Program_Error with "I cursor designates wrong container";
1768 if J.Container /= Container'Unrestricted_Access then
1769 raise Program_Error with "J cursor designates wrong container";
1772 if I.Node = J.Node then
1776 if Container.Busy > 0 then
1777 raise Program_Error with
1778 "attempt to tamper with cursors (list is busy)";
1781 pragma Assert (Vet (I), "bad I cursor in Swap_Links");
1782 pragma Assert (Vet (J), "bad J cursor in Swap_Links");
1785 I_Next : constant Cursor := Next (I);
1789 Splice (Container, Before => I, Position => J);
1793 J_Next : constant Cursor := Next (J);
1797 Splice (Container, Before => J, Position => I);
1800 pragma Assert (Container.Length >= 3);
1802 Splice (Container, Before => I_Next, Position => J);
1803 Splice (Container, Before => J_Next, Position => I);
1809 pragma Assert (Container.First.Prev = null);
1810 pragma Assert (Container.Last.Next = null);
1813 --------------------
1814 -- Update_Element --
1815 --------------------
1817 procedure Update_Element
1818 (Container : in out List;
1820 Process : not null access procedure (Element : in out Element_Type))
1823 if Position.Node = null then
1824 raise Constraint_Error with "Position cursor has no element";
1827 if Position.Node.Element = null then
1828 raise Program_Error with
1829 "Position cursor has no element";
1832 if Position.Container /= Container'Unchecked_Access then
1833 raise Program_Error with
1834 "Position cursor designates wrong container";
1837 pragma Assert (Vet (Position), "bad cursor in Update_Element");
1840 B : Natural renames Container.Busy;
1841 L : Natural renames Container.Lock;
1848 Process (Position.Node.Element.all);
1865 function Vet (Position : Cursor) return Boolean is
1867 if Position.Node = null then
1868 return Position.Container = null;
1871 if Position.Container = null then
1875 if Position.Node.Next = Position.Node then
1879 if Position.Node.Prev = Position.Node then
1883 if Position.Node.Element = null then
1888 L : List renames Position.Container.all;
1890 if L.Length = 0 then
1894 if L.First = null then
1898 if L.Last = null then
1902 if L.First.Prev /= null then
1906 if L.Last.Next /= null then
1910 if Position.Node.Prev = null
1911 and then Position.Node /= L.First
1916 if Position.Node.Next = null
1917 and then Position.Node /= L.Last
1922 if L.Length = 1 then
1923 return L.First = L.Last;
1926 if L.First = L.Last then
1930 if L.First.Next = null then
1934 if L.Last.Prev = null then
1938 if L.First.Next.Prev /= L.First then
1942 if L.Last.Prev.Next /= L.Last then
1946 if L.Length = 2 then
1947 if L.First.Next /= L.Last then
1951 if L.Last.Prev /= L.First then
1958 if L.First.Next = L.Last then
1962 if L.Last.Prev = L.First then
1966 if Position.Node = L.First then
1970 if Position.Node = L.Last then
1974 if Position.Node.Next = null then
1978 if Position.Node.Prev = null then
1982 if Position.Node.Next.Prev /= Position.Node then
1986 if Position.Node.Prev.Next /= Position.Node then
1990 if L.Length = 3 then
1991 if L.First.Next /= Position.Node then
1995 if L.Last.Prev /= Position.Node then
2009 (Stream : not null access Root_Stream_Type'Class;
2012 Node : Node_Access := Item.First;
2015 Count_Type'Base'Write (Stream, Item.Length);
2017 while Node /= null loop
2018 Element_Type'Output (Stream, Node.Element.all);
2024 (Stream : not null access Root_Stream_Type'Class;
2028 raise Program_Error with "attempt to stream list cursor";
2032 (Stream : not null access Root_Stream_Type'Class;
2033 Item : Reference_Type)
2036 raise Program_Error with "attempt to stream reference";
2040 (Stream : not null access Root_Stream_Type'Class;
2041 Item : Constant_Reference_Type)
2044 raise Program_Error with "attempt to stream reference";
2047 end Ada.Containers.Indefinite_Doubly_Linked_Lists;