1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.FORMAL_DOUBLY_LINKED_LISTS --
9 -- Copyright (C) 2010-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/>. --
26 ------------------------------------------------------------------------------
28 with System; use type System.Address;
30 package body Ada.Containers.Formal_Doubly_Linked_Lists is
32 -----------------------
33 -- Local Subprograms --
34 -----------------------
37 (Container : in out List;
38 New_Item : Element_Type;
39 New_Node : out Count_Type);
42 (Container : in out List;
43 New_Node : out Count_Type);
46 (Container : in out List;
49 procedure Insert_Internal
50 (Container : in out List;
52 New_Node : Count_Type);
54 function Vet (L : List; Position : Cursor) return Boolean;
60 function "=" (Left, Right : List) return Boolean is
64 if Left'Address = Right'Address then
68 if Left.Length /= Right.Length then
75 if Left.Nodes (LI).Element /= Right.Nodes (LI).Element then
79 LI := Left.Nodes (LI).Next;
80 RI := Right.Nodes (RI).Next;
91 (Container : in out List;
92 New_Item : Element_Type;
93 New_Node : out Count_Type)
95 N : Node_Array renames Container.Nodes;
98 if Container.Free >= 0 then
99 New_Node := Container.Free;
100 N (New_Node).Element := New_Item;
101 Container.Free := N (New_Node).Next;
104 New_Node := abs Container.Free;
105 N (New_Node).Element := New_Item;
106 Container.Free := Container.Free - 1;
111 (Container : in out List;
112 New_Node : out Count_Type)
114 N : Node_Array renames Container.Nodes;
117 if Container.Free >= 0 then
118 New_Node := Container.Free;
119 Container.Free := N (New_Node).Next;
122 New_Node := abs Container.Free;
123 Container.Free := Container.Free - 1;
132 (Container : in out List;
133 New_Item : Element_Type;
134 Count : Count_Type := 1)
137 Insert (Container, No_Element, New_Item, Count);
144 procedure Assign (Target : in out List; Source : List) is
145 N : Node_Array renames Source.Nodes;
149 if Target'Address = Source'Address then
153 if Target.Capacity < Source.Length then
154 raise Constraint_Error with -- ???
155 "Source length exceeds Target capacity";
162 Append (Target, N (J).Element);
171 procedure Clear (Container : in out List) is
172 N : Node_Array renames Container.Nodes;
176 if Container.Length = 0 then
177 pragma Assert (Container.First = 0);
178 pragma Assert (Container.Last = 0);
179 pragma Assert (Container.Busy = 0);
180 pragma Assert (Container.Lock = 0);
184 pragma Assert (Container.First >= 1);
185 pragma Assert (Container.Last >= 1);
186 pragma Assert (N (Container.First).Prev = 0);
187 pragma Assert (N (Container.Last).Next = 0);
189 if Container.Busy > 0 then
190 raise Program_Error with
191 "attempt to tamper with elements (list is busy)";
194 while Container.Length > 1 loop
195 X := Container.First;
197 Container.First := N (X).Next;
198 N (Container.First).Prev := 0;
200 Container.Length := Container.Length - 1;
205 X := Container.First;
207 Container.First := 0;
209 Container.Length := 0;
220 Item : Element_Type) return Boolean
223 return Find (Container, Item) /= No_Element;
232 Capacity : Count_Type := 0) return List
234 C : constant Count_Type := Count_Type'Max (Source.Capacity, Capacity);
239 while N <= Source.Capacity loop
240 P.Nodes (N).Prev := Source.Nodes (N).Prev;
241 P.Nodes (N).Next := Source.Nodes (N).Next;
242 P.Nodes (N).Element := Source.Nodes (N).Element;
246 P.Free := Source.Free;
247 P.Length := Source.Length;
248 P.First := Source.First;
249 P.Last := Source.Last;
252 N := Source.Capacity + 1;
267 (Container : in out List;
268 Position : in out Cursor;
269 Count : Count_Type := 1)
271 N : Node_Array renames Container.Nodes;
275 if not Has_Element (Container => Container,
276 Position => Position)
278 raise Constraint_Error with
279 "Position cursor has no element";
282 pragma Assert (Vet (Container, Position), "bad cursor in Delete");
283 pragma Assert (Container.First >= 1);
284 pragma Assert (Container.Last >= 1);
285 pragma Assert (N (Container.First).Prev = 0);
286 pragma Assert (N (Container.Last).Next = 0);
288 if Position.Node = Container.First then
289 Delete_First (Container, Count);
290 Position := No_Element;
295 Position := No_Element;
299 if Container.Busy > 0 then
300 raise Program_Error with
301 "attempt to tamper with elements (list is busy)";
304 for Index in 1 .. Count loop
305 pragma Assert (Container.Length >= 2);
308 Container.Length := Container.Length - 1;
310 if X = Container.Last then
311 Position := No_Element;
313 Container.Last := N (X).Prev;
314 N (Container.Last).Next := 0;
320 Position.Node := N (X).Next;
321 pragma Assert (N (Position.Node).Prev >= 0);
323 N (N (X).Next).Prev := N (X).Prev;
324 N (N (X).Prev).Next := N (X).Next;
328 Position := No_Element;
335 procedure Delete_First
336 (Container : in out List;
337 Count : Count_Type := 1)
339 N : Node_Array renames Container.Nodes;
343 if Count >= Container.Length then
352 if Container.Busy > 0 then
353 raise Program_Error with
354 "attempt to tamper with elements (list is busy)";
357 for J in 1 .. Count loop
358 X := Container.First;
359 pragma Assert (N (N (X).Next).Prev = Container.First);
361 Container.First := N (X).Next;
362 N (Container.First).Prev := 0;
364 Container.Length := Container.Length - 1;
374 procedure Delete_Last
375 (Container : in out List;
376 Count : Count_Type := 1)
378 N : Node_Array renames Container.Nodes;
382 if Count >= Container.Length then
391 if Container.Busy > 0 then
392 raise Program_Error with
393 "attempt to tamper with elements (list is busy)";
396 for J in 1 .. Count loop
398 pragma Assert (N (N (X).Prev).Next = Container.Last);
400 Container.Last := N (X).Prev;
401 N (Container.Last).Next := 0;
403 Container.Length := Container.Length - 1;
415 Position : Cursor) return Element_Type
418 if not Has_Element (Container => Container, Position => Position) then
419 raise Constraint_Error with
420 "Position cursor has no element";
423 return Container.Nodes (Position.Node).Element;
433 Position : Cursor := No_Element) return Cursor
435 From : Count_Type := Position.Node;
438 if From = 0 and Container.Length = 0 then
443 From := Container.First;
446 if Position.Node /= 0 and then
447 not Has_Element (Container, Position)
449 raise Constraint_Error with
450 "Position cursor has no element";
454 if Container.Nodes (From).Element = Item then
455 return (Node => From);
458 From := Container.Nodes (From).Next;
468 function First (Container : List) return Cursor is
470 if Container.First = 0 then
474 return (Node => Container.First);
481 function First_Element (Container : List) return Element_Type is
482 F : constant Count_Type := Container.First;
485 raise Constraint_Error with "list is empty";
487 return Container.Nodes (F).Element;
496 (Container : in out List;
499 pragma Assert (X > 0);
500 pragma Assert (X <= Container.Capacity);
502 N : Node_Array renames Container.Nodes;
505 N (X).Prev := -1; -- Node is deallocated (not on active list)
507 if Container.Free >= 0 then
508 N (X).Next := Container.Free;
511 elsif X + 1 = abs Container.Free then
512 N (X).Next := 0; -- Not strictly necessary, but marginally safer
513 Container.Free := Container.Free + 1;
516 Container.Free := abs Container.Free;
518 if Container.Free > Container.Capacity then
522 for J in Container.Free .. Container.Capacity - 1 loop
526 N (Container.Capacity).Next := 0;
529 N (X).Next := Container.Free;
534 ---------------------
535 -- Generic_Sorting --
536 ---------------------
538 package body Generic_Sorting is
544 function Is_Sorted (Container : List) return Boolean is
545 Nodes : Node_Array renames Container.Nodes;
546 Node : Count_Type := Container.First;
549 for I in 2 .. Container.Length loop
550 if Nodes (Nodes (Node).Next).Element < Nodes (Node).Element then
554 Node := Nodes (Node).Next;
565 (Target : in out List;
566 Source : in out List)
568 LN : Node_Array renames Target.Nodes;
569 RN : Node_Array renames Source.Nodes;
574 if Target'Address = Source'Address then
578 if Target.Busy > 0 then
579 raise Program_Error with
580 "attempt to tamper with cursors of Target (list is busy)";
583 if Source.Busy > 0 then
584 raise Program_Error with
585 "attempt to tamper with cursors of Source (list is busy)";
588 LI := First (Target);
589 RI := First (Source);
590 while RI.Node /= 0 loop
591 pragma Assert (RN (RI.Node).Next = 0
592 or else not (RN (RN (RI.Node).Next).Element <
593 RN (RI.Node).Element));
596 Splice (Target, No_Element, Source);
600 pragma Assert (LN (LI.Node).Next = 0
601 or else not (LN (LN (LI.Node).Next).Element <
602 LN (LI.Node).Element));
604 if RN (RI.Node).Element < LN (LI.Node).Element then
607 pragma Warnings (Off, RJ);
609 RI.Node := RN (RI.Node).Next;
610 Splice (Target, LI, Source, RJ);
614 LI.Node := LN (LI.Node).Next;
623 procedure Sort (Container : in out List) is
624 N : Node_Array renames Container.Nodes;
626 procedure Partition (Pivot, Back : Count_Type);
627 procedure Sort (Front, Back : Count_Type);
633 procedure Partition (Pivot, Back : Count_Type) is
637 Node := N (Pivot).Next;
638 while Node /= Back loop
639 if N (Node).Element < N (Pivot).Element then
641 Prev : constant Count_Type := N (Node).Prev;
642 Next : constant Count_Type := N (Node).Next;
645 N (Prev).Next := Next;
648 Container.Last := Prev;
650 N (Next).Prev := Prev;
653 N (Node).Next := Pivot;
654 N (Node).Prev := N (Pivot).Prev;
656 N (Pivot).Prev := Node;
658 if N (Node).Prev = 0 then
659 Container.First := Node;
661 N (N (Node).Prev).Next := Node;
668 Node := N (Node).Next;
677 procedure Sort (Front, Back : Count_Type) is
682 Pivot := Container.First;
684 Pivot := N (Front).Next;
687 if Pivot /= Back then
688 Partition (Pivot, Back);
694 -- Start of processing for Sort
697 if Container.Length <= 1 then
701 pragma Assert (N (Container.First).Prev = 0);
702 pragma Assert (N (Container.Last).Next = 0);
704 if Container.Busy > 0 then
705 raise Program_Error with
706 "attempt to tamper with elements (list is busy)";
709 Sort (Front => 0, Back => 0);
711 pragma Assert (N (Container.First).Prev = 0);
712 pragma Assert (N (Container.Last).Next = 0);
721 function Has_Element (Container : List; Position : Cursor) return Boolean is
723 if Position.Node = 0 then
727 return Container.Nodes (Position.Node).Prev /= -1;
735 (Container : in out List;
737 New_Item : Element_Type;
738 Position : out Cursor;
739 Count : Count_Type := 1)
745 if Before.Node /= 0 then
747 pragma Assert (Vet (Container, Before), "bad cursor in Insert");
755 if Container.Length > Container.Capacity - Count then
756 raise Constraint_Error with "new length exceeds capacity";
759 if Container.Busy > 0 then
760 raise Program_Error with
761 "attempt to tamper with elements (list is busy)";
764 Allocate (Container, New_Item, New_Node => J);
765 Insert_Internal (Container, Before.Node, New_Node => J);
766 Position := (Node => J);
768 for Index in 2 .. Count loop
769 Allocate (Container, New_Item, New_Node => J);
770 Insert_Internal (Container, Before.Node, New_Node => J);
775 (Container : in out List;
777 New_Item : Element_Type;
778 Count : Count_Type := 1)
782 Insert (Container, Before, New_Item, Position, Count);
786 (Container : in out List;
788 Position : out Cursor;
789 Count : Count_Type := 1)
795 if Before.Node /= 0 then
797 pragma Assert (Vet (Container, Before), "bad cursor in Insert");
805 if Container.Length > Container.Capacity - Count then
806 raise Constraint_Error with "new length exceeds capacity";
809 if Container.Busy > 0 then
810 raise Program_Error with
811 "attempt to tamper with elements (list is busy)";
814 Allocate (Container, New_Node => J);
815 Insert_Internal (Container, Before.Node, New_Node => J);
816 Position := (Node => J);
818 for Index in 2 .. Count loop
819 Allocate (Container, New_Node => J);
820 Insert_Internal (Container, Before.Node, New_Node => J);
824 ---------------------
825 -- Insert_Internal --
826 ---------------------
828 procedure Insert_Internal
829 (Container : in out List;
831 New_Node : Count_Type)
833 N : Node_Array renames Container.Nodes;
836 if Container.Length = 0 then
837 pragma Assert (Before = 0);
838 pragma Assert (Container.First = 0);
839 pragma Assert (Container.Last = 0);
841 Container.First := New_Node;
842 Container.Last := New_Node;
844 N (Container.First).Prev := 0;
845 N (Container.Last).Next := 0;
847 elsif Before = 0 then
848 pragma Assert (N (Container.Last).Next = 0);
850 N (Container.Last).Next := New_Node;
851 N (New_Node).Prev := Container.Last;
853 Container.Last := New_Node;
854 N (Container.Last).Next := 0;
856 elsif Before = Container.First then
857 pragma Assert (N (Container.First).Prev = 0);
859 N (Container.First).Prev := New_Node;
860 N (New_Node).Next := Container.First;
862 Container.First := New_Node;
863 N (Container.First).Prev := 0;
866 pragma Assert (N (Container.First).Prev = 0);
867 pragma Assert (N (Container.Last).Next = 0);
869 N (New_Node).Next := Before;
870 N (New_Node).Prev := N (Before).Prev;
872 N (N (Before).Prev).Next := New_Node;
873 N (Before).Prev := New_Node;
876 Container.Length := Container.Length + 1;
883 function Is_Empty (Container : List) return Boolean is
885 return Length (Container) = 0;
895 not null access procedure (Container : List; Position : Cursor))
897 C : List renames Container'Unrestricted_Access.all;
898 B : Natural renames C.Busy;
900 Node : Count_Type := Container.First;
907 Process (Container, (Node => Node));
908 Node := Container.Nodes (Node).Next;
924 function Last (Container : List) return Cursor is
926 if Container.Last = 0 then
929 return (Node => Container.Last);
936 function Last_Element (Container : List) return Element_Type is
937 L : constant Count_Type := Container.Last;
940 raise Constraint_Error with "list is empty";
942 return Container.Nodes (L).Element;
950 function Left (Container : List; Position : Cursor) return List is
951 Curs : Cursor := Position;
952 C : List (Container.Capacity) := Copy (Container, Container.Capacity);
956 if Curs = No_Element then
960 if not Has_Element (Container, Curs) then
961 raise Constraint_Error;
964 while Curs.Node /= 0 loop
967 Curs := Next (Container, (Node => Node));
977 function Length (Container : List) return Count_Type is
979 return Container.Length;
987 (Target : in out List;
988 Source : in out List)
990 N : Node_Array renames Source.Nodes;
994 if Target'Address = Source'Address then
998 if Target.Capacity < Source.Length then
999 raise Constraint_Error with -- ???
1000 "Source length exceeds Target capacity";
1003 if Source.Busy > 0 then
1004 raise Program_Error with
1005 "attempt to tamper with cursors of Source (list is busy)";
1010 while Source.Length > 0 loop
1012 Append (Target, N (X).Element); -- optimize away???
1014 Source.First := N (X).Next;
1015 N (Source.First).Prev := 0;
1017 Source.Length := Source.Length - 1;
1026 procedure Next (Container : List; Position : in out Cursor) is
1028 Position := Next (Container, Position);
1031 function Next (Container : List; Position : Cursor) return Cursor is
1033 if Position.Node = 0 then
1037 if not Has_Element (Container, Position) then
1038 raise Program_Error with "Position cursor has no element";
1041 return (Node => Container.Nodes (Position.Node).Next);
1049 (Container : in out List;
1050 New_Item : Element_Type;
1051 Count : Count_Type := 1)
1054 Insert (Container, First (Container), New_Item, Count);
1061 procedure Previous (Container : List; Position : in out Cursor) is
1063 Position := Previous (Container, Position);
1066 function Previous (Container : List; Position : Cursor) return Cursor is
1068 if Position.Node = 0 then
1072 if not Has_Element (Container, Position) then
1073 raise Program_Error with "Position cursor has no element";
1076 return (Node => Container.Nodes (Position.Node).Prev);
1083 procedure Query_Element
1084 (Container : List; Position : Cursor;
1085 Process : not null access procedure (Element : Element_Type))
1087 C : List renames Container'Unrestricted_Access.all;
1088 B : Natural renames C.Busy;
1089 L : Natural renames C.Lock;
1092 if not Has_Element (Container, Position) then
1093 raise Constraint_Error with
1094 "Position cursor has no element";
1101 N : Node_Type renames C.Nodes (Position.Node);
1103 Process (N.Element);
1120 (Stream : not null access Root_Stream_Type'Class;
1123 N : Count_Type'Base;
1128 Count_Type'Base'Read (Stream, N);
1131 raise Program_Error with "bad list length";
1138 if N > Item.Capacity then
1139 raise Constraint_Error with "length exceeds capacity";
1142 for J in 1 .. N loop
1143 Item.Append (Element_Type'Input (Stream)); -- ???
1148 (Stream : not null access Root_Stream_Type'Class;
1152 raise Program_Error with "attempt to stream list cursor";
1155 ---------------------
1156 -- Replace_Element --
1157 ---------------------
1159 procedure Replace_Element
1160 (Container : in out List;
1162 New_Item : Element_Type)
1166 if not Has_Element (Container, Position) then
1167 raise Constraint_Error with "Position cursor has no element";
1170 if Container.Lock > 0 then
1171 raise Program_Error with
1172 "attempt to tamper with cursors (list is locked)";
1175 pragma Assert (Vet (Container, Position),
1176 "bad cursor in Replace_Element");
1179 N : Node_Array renames Container.Nodes;
1181 N (Position.Node).Element := New_Item;
1183 end Replace_Element;
1185 ----------------------
1186 -- Reverse_Elements --
1187 ----------------------
1189 procedure Reverse_Elements (Container : in out List) is
1190 N : Node_Array renames Container.Nodes;
1191 I : Count_Type := Container.First;
1192 J : Count_Type := Container.Last;
1194 procedure Swap (L, R : Count_Type);
1200 procedure Swap (L, R : Count_Type) is
1201 LN : constant Count_Type := N (L).Next;
1202 LP : constant Count_Type := N (L).Prev;
1204 RN : constant Count_Type := N (R).Next;
1205 RP : constant Count_Type := N (R).Prev;
1220 pragma Assert (RP = L);
1234 -- Start of processing for Reverse_Elements
1237 if Container.Length <= 1 then
1241 pragma Assert (N (Container.First).Prev = 0);
1242 pragma Assert (N (Container.Last).Next = 0);
1244 if Container.Busy > 0 then
1245 raise Program_Error with
1246 "attempt to tamper with elements (list is busy)";
1249 Container.First := J;
1250 Container.Last := I;
1252 Swap (L => I, R => J);
1260 Swap (L => J, R => I);
1269 pragma Assert (N (Container.First).Prev = 0);
1270 pragma Assert (N (Container.Last).Next = 0);
1271 end Reverse_Elements;
1277 function Reverse_Find
1279 Item : Element_Type;
1280 Position : Cursor := No_Element) return Cursor
1282 CFirst : Count_Type := Position.Node;
1285 CFirst := Container.First;
1288 if Container.Length = 0 then
1292 while CFirst /= 0 loop
1293 if Container.Nodes (CFirst).Element = Item then
1294 return (Node => CFirst);
1296 CFirst := Container.Nodes (CFirst).Prev;
1302 ---------------------
1303 -- Reverse_Iterate --
1304 ---------------------
1306 procedure Reverse_Iterate
1309 not null access procedure (Container : List; Position : Cursor))
1311 C : List renames Container'Unrestricted_Access.all;
1312 B : Natural renames C.Busy;
1314 Node : Count_Type := Container.Last;
1320 while Node /= 0 loop
1321 Process (Container, (Node => Node));
1322 Node := Container.Nodes (Node).Prev;
1332 end Reverse_Iterate;
1338 function Right (Container : List; Position : Cursor) return List is
1339 Curs : Cursor := First (Container);
1340 C : List (Container.Capacity) := Copy (Container, Container.Capacity);
1344 if Curs = No_Element then
1349 if Position /= No_Element and not Has_Element (Container, Position) then
1350 raise Constraint_Error;
1353 while Curs.Node /= Position.Node loop
1356 Curs := Next (Container, (Node => Node));
1367 (Target : in out List;
1369 Source : in out List)
1371 SN : Node_Array renames Source.Nodes;
1374 if Before.Node /= 0 then
1376 pragma Assert (Vet (Target, Before), "bad cursor in Splice");
1379 if Target'Address = Source'Address
1380 or else Source.Length = 0
1385 pragma Assert (SN (Source.First).Prev = 0);
1386 pragma Assert (SN (Source.Last).Next = 0);
1388 if Target.Length > Count_Type'Base'Last - Source.Length then
1389 raise Constraint_Error with "new length exceeds maximum";
1392 if Target.Length + Source.Length > Target.Capacity then
1393 raise Constraint_Error;
1396 if Target.Busy > 0 then
1397 raise Program_Error with
1398 "attempt to tamper with cursors of Target (list is busy)";
1401 if Source.Busy > 0 then
1402 raise Program_Error with
1403 "attempt to tamper with cursors of Source (list is busy)";
1407 Insert (Target, Before, SN (Source.Last).Element);
1408 Delete_Last (Source);
1409 exit when Is_Empty (Source);
1414 (Target : in out List;
1416 Source : in out List;
1417 Position : in out Cursor)
1419 Target_Position : Cursor;
1423 if Target'Address = Source'Address then
1424 Splice (Target, Before, Position);
1428 if Position.Node = 0 then
1429 raise Constraint_Error with "Position cursor has no element";
1432 pragma Assert (Vet (Source, Position), "bad Position cursor in Splice");
1434 if Target.Length >= Target.Capacity then
1435 raise Constraint_Error;
1438 if Target.Busy > 0 then
1439 raise Program_Error with
1440 "attempt to tamper with cursors of Target (list is busy)";
1443 if Source.Busy > 0 then
1444 raise Program_Error with
1445 "attempt to tamper with cursors of Source (list is busy)";
1449 (Container => Target,
1451 New_Item => Source.Nodes (Position.Node).Element,
1452 Position => Target_Position);
1454 Delete (Source, Position);
1455 Position := Target_Position;
1459 (Container : in out List;
1463 N : Node_Array renames Container.Nodes;
1466 if Before.Node /= 0 then
1468 pragma Assert (Vet (Container, Before),
1469 "bad Before cursor in Splice");
1472 if Position.Node = 0 then
1473 raise Constraint_Error with "Position cursor has no element";
1476 pragma Assert (Vet (Container, Position),
1477 "bad Position cursor in Splice");
1479 if Position.Node = Before.Node
1480 or else N (Position.Node).Next = Before.Node
1485 pragma Assert (Container.Length >= 2);
1487 if Container.Busy > 0 then
1488 raise Program_Error with
1489 "attempt to tamper with elements (list is busy)";
1492 if Before.Node = 0 then
1493 pragma Assert (Position.Node /= Container.Last);
1495 if Position.Node = Container.First then
1496 Container.First := N (Position.Node).Next;
1497 N (Container.First).Prev := 0;
1500 N (N (Position.Node).Prev).Next := N (Position.Node).Next;
1501 N (N (Position.Node).Next).Prev := N (Position.Node).Prev;
1504 N (Container.Last).Next := Position.Node;
1505 N (Position.Node).Prev := Container.Last;
1507 Container.Last := Position.Node;
1508 N (Container.Last).Next := 0;
1513 if Before.Node = Container.First then
1514 pragma Assert (Position.Node /= Container.First);
1516 if Position.Node = Container.Last then
1517 Container.Last := N (Position.Node).Prev;
1518 N (Container.Last).Next := 0;
1521 N (N (Position.Node).Prev).Next := N (Position.Node).Next;
1522 N (N (Position.Node).Next).Prev := N (Position.Node).Prev;
1525 N (Container.First).Prev := Position.Node;
1526 N (Position.Node).Next := Container.First;
1528 Container.First := Position.Node;
1529 N (Container.First).Prev := 0;
1534 if Position.Node = Container.First then
1535 Container.First := N (Position.Node).Next;
1536 N (Container.First).Prev := 0;
1538 elsif Position.Node = Container.Last then
1539 Container.Last := N (Position.Node).Prev;
1540 N (Container.Last).Next := 0;
1543 N (N (Position.Node).Prev).Next := N (Position.Node).Next;
1544 N (N (Position.Node).Next).Prev := N (Position.Node).Prev;
1547 N (N (Before.Node).Prev).Next := Position.Node;
1548 N (Position.Node).Prev := N (Before.Node).Prev;
1550 N (Before.Node).Prev := Position.Node;
1551 N (Position.Node).Next := Before.Node;
1553 pragma Assert (N (Container.First).Prev = 0);
1554 pragma Assert (N (Container.Last).Next = 0);
1561 function Strict_Equal (Left, Right : List) return Boolean is
1562 CL : Count_Type := Left.First;
1563 CR : Count_Type := Right.First;
1566 while CL /= 0 or CR /= 0 loop
1568 Left.Nodes (CL).Element /= Right.Nodes (CL).Element
1573 CL := Left.Nodes (CL).Next;
1574 CR := Right.Nodes (CR).Next;
1585 (Container : in out List;
1590 raise Constraint_Error with "I cursor has no element";
1594 raise Constraint_Error with "J cursor has no element";
1597 if I.Node = J.Node then
1601 if Container.Lock > 0 then
1602 raise Program_Error with
1603 "attempt to tamper with cursors (list is locked)";
1606 pragma Assert (Vet (Container, I), "bad I cursor in Swap");
1607 pragma Assert (Vet (Container, J), "bad J cursor in Swap");
1610 NN : Node_Array renames Container.Nodes;
1611 NI : Node_Type renames NN (I.Node);
1612 NJ : Node_Type renames NN (J.Node);
1614 EI_Copy : constant Element_Type := NI.Element;
1617 NI.Element := NJ.Element;
1618 NJ.Element := EI_Copy;
1626 procedure Swap_Links
1627 (Container : in out List;
1630 I_Next, J_Next : Cursor;
1634 raise Constraint_Error with "I cursor has no element";
1638 raise Constraint_Error with "J cursor has no element";
1641 if I.Node = J.Node then
1645 if Container.Busy > 0 then
1646 raise Program_Error with
1647 "attempt to tamper with elements (list is busy)";
1650 pragma Assert (Vet (Container, I), "bad I cursor in Swap_Links");
1651 pragma Assert (Vet (Container, J), "bad J cursor in Swap_Links");
1653 I_Next := Next (Container, I);
1656 Splice (Container, Before => I, Position => J);
1659 J_Next := Next (Container, J);
1662 Splice (Container, Before => J, Position => I);
1665 pragma Assert (Container.Length >= 3);
1666 Splice (Container, Before => I_Next, Position => J);
1667 Splice (Container, Before => J_Next, Position => I);
1672 --------------------
1673 -- Update_Element --
1674 --------------------
1676 procedure Update_Element
1677 (Container : in out List;
1679 Process : not null access procedure (Element : in out Element_Type))
1682 if Position.Node = 0 then
1683 raise Constraint_Error with "Position cursor has no element";
1686 pragma Assert (Vet (Container, Position),
1687 "bad cursor in Update_Element");
1690 B : Natural renames Container.Busy;
1691 L : Natural renames Container.Lock;
1698 N : Node_Type renames Container.Nodes (Position.Node);
1700 Process (N.Element);
1717 function Vet (L : List; Position : Cursor) return Boolean is
1718 N : Node_Array renames L.Nodes;
1721 if L.Length = 0 then
1733 if Position.Node > L.Capacity then
1737 if N (Position.Node).Prev < 0
1738 or else N (Position.Node).Prev > L.Capacity
1743 if N (Position.Node).Next > L.Capacity then
1747 if N (L.First).Prev /= 0 then
1751 if N (L.Last).Next /= 0 then
1755 if N (Position.Node).Prev = 0
1756 and then Position.Node /= L.First
1761 if N (Position.Node).Next = 0
1762 and then Position.Node /= L.Last
1767 if L.Length = 1 then
1768 return L.First = L.Last;
1771 if L.First = L.Last then
1775 if N (L.First).Next = 0 then
1779 if N (L.Last).Prev = 0 then
1783 if N (N (L.First).Next).Prev /= L.First then
1787 if N (N (L.Last).Prev).Next /= L.Last then
1791 if L.Length = 2 then
1792 if N (L.First).Next /= L.Last then
1796 if N (L.Last).Prev /= L.First then
1803 if N (L.First).Next = L.Last then
1807 if N (L.Last).Prev = L.First then
1811 if Position.Node = L.First then
1815 if Position.Node = L.Last then
1819 if N (Position.Node).Next = 0 then
1823 if N (Position.Node).Prev = 0 then
1827 if N (N (Position.Node).Next).Prev /= Position.Node then
1831 if N (N (Position.Node).Prev).Next /= Position.Node then
1835 if L.Length = 3 then
1836 if N (L.First).Next /= Position.Node then
1840 if N (L.Last).Prev /= Position.Node then
1853 (Stream : not null access Root_Stream_Type'Class;
1856 N : Node_Array renames Item.Nodes;
1860 Count_Type'Base'Write (Stream, Item.Length);
1863 while Node /= 0 loop
1864 Element_Type'Write (Stream, N (Node).Element);
1865 Node := N (Node).Next;
1870 (Stream : not null access Root_Stream_Type'Class;
1874 raise Program_Error with "attempt to stream list cursor";
1877 end Ada.Containers.Formal_Doubly_Linked_Lists;