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-2006, Free Software Foundation, Inc. --
11 -- This specification is derived from the Ada Reference Manual for use with --
12 -- GNAT. The copyright notice above, and the license provisions that follow --
13 -- apply solely to the contents of the part following the private keyword. --
15 -- GNAT is free software; you can redistribute it and/or modify it under --
16 -- terms of the GNU General Public License as published by the Free Soft- --
17 -- ware Foundation; either version 2, or (at your option) any later ver- --
18 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
19 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
20 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
21 -- for more details. You should have received a copy of the GNU General --
22 -- Public License distributed with GNAT; see file COPYING. If not, write --
23 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
24 -- Boston, MA 02110-1301, USA. --
26 -- As a special exception, if other files instantiate generics from this --
27 -- unit, or you link this unit with other files to produce an executable, --
28 -- this unit does not by itself cause the resulting executable to be --
29 -- covered by the GNU General Public License. This exception does not --
30 -- however invalidate any other reasons why the executable file might be --
31 -- covered by the GNU Public License. --
33 -- This unit was originally developed by Matthew J Heaney. --
34 ------------------------------------------------------------------------------
36 with System
; use type System
.Address
;
38 with Ada
.Unchecked_Deallocation
;
40 package body Ada
.Containers
.Doubly_Linked_Lists
is
42 -----------------------
43 -- Local Subprograms --
44 -----------------------
46 procedure Free
(X
: in out Node_Access
);
48 procedure Insert_Internal
49 (Container
: in out List
;
51 New_Node
: Node_Access
);
53 function Vet
(Position
: Cursor
) return Boolean;
59 function "=" (Left
, Right
: List
) return Boolean is
60 L
: Node_Access
:= Left
.First
;
61 R
: Node_Access
:= Right
.First
;
64 if Left
'Address = Right
'Address then
68 if Left
.Length
/= Right
.Length
then
72 for J
in 1 .. Left
.Length
loop
73 if L
.Element
/= R
.Element
then
88 procedure Adjust
(Container
: in out List
) is
89 Src
: Node_Access
:= Container
.First
;
93 pragma Assert
(Container
.Last
= null);
94 pragma Assert
(Container
.Length
= 0);
95 pragma Assert
(Container
.Busy
= 0);
96 pragma Assert
(Container
.Lock
= 0);
100 pragma Assert
(Container
.First
.Prev
= null);
101 pragma Assert
(Container
.Last
.Next
= null);
102 pragma Assert
(Container
.Length
> 0);
104 Container
.First
:= null;
105 Container
.Last
:= null;
106 Container
.Length
:= 0;
110 Container
.First
:= new Node_Type
'(Src.Element, null, null);
111 Container.Last := Container.First;
112 Container.Length := 1;
115 while Src /= null loop
116 Container.Last.Next := new Node_Type'(Element
=> Src
.Element
,
117 Prev
=> Container
.Last
,
119 Container
.Last
:= Container
.Last
.Next
;
120 Container
.Length
:= Container
.Length
+ 1;
131 (Container
: in out List
;
132 New_Item
: Element_Type
;
133 Count
: Count_Type
:= 1)
136 Insert
(Container
, No_Element
, New_Item
, Count
);
143 procedure Clear
(Container
: in out List
) is
147 if Container
.Length
= 0 then
148 pragma Assert
(Container
.First
= null);
149 pragma Assert
(Container
.Last
= null);
150 pragma Assert
(Container
.Busy
= 0);
151 pragma Assert
(Container
.Lock
= 0);
155 pragma Assert
(Container
.First
.Prev
= null);
156 pragma Assert
(Container
.Last
.Next
= null);
158 if Container
.Busy
> 0 then
162 while Container
.Length
> 1 loop
163 X
:= Container
.First
;
164 pragma Assert
(X
.Next
.Prev
= Container
.First
);
166 Container
.First
:= X
.Next
;
167 Container
.First
.Prev
:= null;
169 Container
.Length
:= Container
.Length
- 1;
174 X
:= Container
.First
;
175 pragma Assert
(X
= Container
.Last
);
177 Container
.First
:= null;
178 Container
.Last
:= null;
179 Container
.Length
:= 0;
190 Item
: Element_Type
) return Boolean
193 return Find
(Container
, Item
) /= No_Element
;
201 (Container
: in out List
;
202 Position
: in out Cursor
;
203 Count
: Count_Type
:= 1)
208 if Position
.Node
= null then
209 raise Constraint_Error
;
212 if Position
.Container
/= Container
'Unrestricted_Access then
216 pragma Assert
(Vet
(Position
), "bad cursor in Delete");
218 if Position
.Node
= Container
.First
then
219 Delete_First
(Container
, Count
);
220 Position
:= No_Element
; -- Post-York behavior
225 Position
:= No_Element
; -- Post-York behavior
229 if Container
.Busy
> 0 then
233 for Index
in 1 .. Count
loop
235 Container
.Length
:= Container
.Length
- 1;
237 if X
= Container
.Last
then
238 Position
:= No_Element
;
240 Container
.Last
:= X
.Prev
;
241 Container
.Last
.Next
:= null;
247 Position
.Node
:= X
.Next
;
249 X
.Next
.Prev
:= X
.Prev
;
250 X
.Prev
.Next
:= X
.Next
;
255 Position
:= No_Element
; -- Post-York behavior
262 procedure Delete_First
263 (Container
: in out List
;
264 Count
: Count_Type
:= 1)
269 if Count
>= Container
.Length
then
278 if Container
.Busy
> 0 then
282 for I
in 1 .. Count
loop
283 X
:= Container
.First
;
284 pragma Assert
(X
.Next
.Prev
= Container
.First
);
286 Container
.First
:= X
.Next
;
287 Container
.First
.Prev
:= null;
289 Container
.Length
:= Container
.Length
- 1;
299 procedure Delete_Last
300 (Container
: in out List
;
301 Count
: Count_Type
:= 1)
306 if Count
>= Container
.Length
then
315 if Container
.Busy
> 0 then
319 for I
in 1 .. Count
loop
321 pragma Assert
(X
.Prev
.Next
= Container
.Last
);
323 Container
.Last
:= X
.Prev
;
324 Container
.Last
.Next
:= null;
326 Container
.Length
:= Container
.Length
- 1;
336 function Element
(Position
: Cursor
) return Element_Type
is
338 if Position
.Node
= null then
339 raise Constraint_Error
;
342 pragma Assert
(Vet
(Position
), "bad cursor in Element");
344 return Position
.Node
.Element
;
354 Position
: Cursor
:= No_Element
) return Cursor
356 Node
: Node_Access
:= Position
.Node
;
360 Node
:= Container
.First
;
363 if Position
.Container
/= Container
'Unrestricted_Access then
367 pragma Assert
(Vet
(Position
), "bad cursor in Find");
370 while Node
/= null loop
371 if Node
.Element
= Item
then
372 return Cursor
'(Container'Unchecked_Access, Node);
385 function First (Container : List) return Cursor is
387 if Container.First = null then
391 return Cursor'(Container
'Unchecked_Access, Container
.First
);
398 function First_Element
(Container
: List
) return Element_Type
is
400 if Container
.First
= null then
401 raise Constraint_Error
;
404 return Container
.First
.Element
;
411 procedure Free
(X
: in out Node_Access
) is
412 procedure Deallocate
is
413 new Ada
.Unchecked_Deallocation
(Node_Type
, Node_Access
);
421 ---------------------
422 -- Generic_Sorting --
423 ---------------------
425 package body Generic_Sorting
is
431 function Is_Sorted
(Container
: List
) return Boolean is
432 Node
: Node_Access
:= Container
.First
;
435 for I
in 2 .. Container
.Length
loop
436 if Node
.Next
.Element
< Node
.Element
then
451 (Target
: in out List
;
452 Source
: in out List
)
454 LI
: Cursor
:= First
(Target
);
455 RI
: Cursor
:= First
(Source
);
458 if Target
'Address = Source
'Address then
463 or else Source
.Busy
> 0
468 while RI
.Node
/= null loop
469 pragma Assert
(RI
.Node
.Next
= null
470 or else not (RI
.Node
.Next
.Element
<
473 if LI
.Node
= null then
474 Splice
(Target
, No_Element
, Source
);
478 pragma Assert
(LI
.Node
.Next
= null
479 or else not (LI
.Node
.Next
.Element
<
482 if RI
.Node
.Element
< LI
.Node
.Element
then
486 RI
.Node
:= RI
.Node
.Next
;
487 Splice
(Target
, LI
, Source
, RJ
);
491 LI
.Node
:= LI
.Node
.Next
;
500 procedure Sort
(Container
: in out List
) is
502 procedure Partition
(Pivot
: Node_Access
; Back
: Node_Access
);
504 procedure Sort
(Front
, Back
: Node_Access
);
510 procedure Partition
(Pivot
: Node_Access
; Back
: Node_Access
) is
511 Node
: Node_Access
:= Pivot
.Next
;
514 while Node
/= Back
loop
515 if Node
.Element
< Pivot
.Element
then
517 Prev
: constant Node_Access
:= Node
.Prev
;
518 Next
: constant Node_Access
:= Node
.Next
;
524 Container
.Last
:= Prev
;
530 Node
.Prev
:= Pivot
.Prev
;
534 if Node
.Prev
= null then
535 Container
.First
:= Node
;
537 Node
.Prev
.Next
:= Node
;
553 procedure Sort
(Front
, Back
: Node_Access
) is
558 Pivot
:= Container
.First
;
563 if Pivot
/= Back
then
564 Partition
(Pivot
, Back
);
570 -- Start of processing for Sort
573 if Container
.Length
<= 1 then
577 pragma Assert
(Container
.First
.Prev
= null);
578 pragma Assert
(Container
.Last
.Next
= null);
580 if Container
.Busy
> 0 then
584 Sort
(Front
=> null, Back
=> null);
586 pragma Assert
(Container
.First
.Prev
= null);
587 pragma Assert
(Container
.Last
.Next
= null);
596 function Has_Element
(Position
: Cursor
) return Boolean is
598 pragma Assert
(Vet
(Position
), "bad cursor in Has_Element");
599 return Position
.Node
/= null;
607 (Container
: in out List
;
609 New_Item
: Element_Type
;
610 Position
: out Cursor
;
611 Count
: Count_Type
:= 1)
613 New_Node
: Node_Access
;
616 if Before
.Container
/= null then
617 if Before
.Container
/= Container
'Unrestricted_Access then
621 pragma Assert
(Vet
(Before
), "bad cursor in Insert");
629 if Container
.Length
> Count_Type
'Last - Count
then
630 raise Constraint_Error
;
633 if Container
.Busy
> 0 then
637 New_Node
:= new Node_Type
'(New_Item, null, null);
638 Insert_Internal (Container, Before.Node, New_Node);
640 Position := Cursor'(Container
'Unchecked_Access, New_Node
);
642 for J
in Count_Type
'(2) .. Count loop
643 New_Node := new Node_Type'(New_Item
, null, null);
644 Insert_Internal
(Container
, Before
.Node
, New_Node
);
649 (Container
: in out List
;
651 New_Item
: Element_Type
;
652 Count
: Count_Type
:= 1)
656 Insert
(Container
, Before
, New_Item
, Position
, Count
);
660 (Container
: in out List
;
662 Position
: out Cursor
;
663 Count
: Count_Type
:= 1)
665 New_Node
: Node_Access
;
668 if Before
.Container
/= null then
669 if Before
.Container
/= Container
'Unrestricted_Access then
673 pragma Assert
(Vet
(Before
), "bad cursor in Insert");
681 if Container
.Length
> Count_Type
'Last - Count
then
682 raise Constraint_Error
;
685 if Container
.Busy
> 0 then
689 New_Node
:= new Node_Type
;
690 Insert_Internal
(Container
, Before
.Node
, New_Node
);
692 Position
:= Cursor
'(Container'Unchecked_Access, New_Node);
694 for J in Count_Type'(2) .. Count
loop
695 New_Node
:= new Node_Type
;
696 Insert_Internal
(Container
, Before
.Node
, New_Node
);
700 ---------------------
701 -- Insert_Internal --
702 ---------------------
704 procedure Insert_Internal
705 (Container
: in out List
;
706 Before
: Node_Access
;
707 New_Node
: Node_Access
)
710 if Container
.Length
= 0 then
711 pragma Assert
(Before
= null);
712 pragma Assert
(Container
.First
= null);
713 pragma Assert
(Container
.Last
= null);
715 Container
.First
:= New_Node
;
716 Container
.Last
:= New_Node
;
718 elsif Before
= null then
719 pragma Assert
(Container
.Last
.Next
= null);
721 Container
.Last
.Next
:= New_Node
;
722 New_Node
.Prev
:= Container
.Last
;
724 Container
.Last
:= New_Node
;
726 elsif Before
= Container
.First
then
727 pragma Assert
(Container
.First
.Prev
= null);
729 Container
.First
.Prev
:= New_Node
;
730 New_Node
.Next
:= Container
.First
;
732 Container
.First
:= New_Node
;
735 pragma Assert
(Container
.First
.Prev
= null);
736 pragma Assert
(Container
.Last
.Next
= null);
738 New_Node
.Next
:= Before
;
739 New_Node
.Prev
:= Before
.Prev
;
741 Before
.Prev
.Next
:= New_Node
;
742 Before
.Prev
:= New_Node
;
745 Container
.Length
:= Container
.Length
+ 1;
752 function Is_Empty
(Container
: List
) return Boolean is
754 return Container
.Length
= 0;
763 Process
: not null access procedure (Position
: Cursor
))
765 C
: List
renames Container
'Unrestricted_Access.all;
766 B
: Natural renames C
.Busy
;
768 Node
: Node_Access
:= Container
.First
;
774 while Node
/= null loop
775 Process
(Cursor
'(Container'Unchecked_Access, Node));
791 function Last (Container : List) return Cursor is
793 if Container.Last = null then
797 return Cursor'(Container
'Unchecked_Access, Container
.Last
);
804 function Last_Element
(Container
: List
) return Element_Type
is
806 if Container
.Last
= null then
807 raise Constraint_Error
;
810 return Container
.Last
.Element
;
817 function Length
(Container
: List
) return Count_Type
is
819 return Container
.Length
;
827 (Target
: in out List
;
828 Source
: in out List
)
831 if Target
'Address = Source
'Address then
835 if Source
.Busy
> 0 then
841 Target
.First
:= Source
.First
;
842 Source
.First
:= null;
844 Target
.Last
:= Source
.Last
;
847 Target
.Length
:= Source
.Length
;
855 procedure Next
(Position
: in out Cursor
) is
857 pragma Assert
(Vet
(Position
), "bad cursor in procedure Next");
859 if Position
.Node
= null then
863 Position
.Node
:= Position
.Node
.Next
;
865 if Position
.Node
= null then
866 Position
.Container
:= null;
870 function Next
(Position
: Cursor
) return Cursor
is
872 pragma Assert
(Vet
(Position
), "bad cursor in function Next");
874 if Position
.Node
= null then
879 Next_Node
: constant Node_Access
:= Position
.Node
.Next
;
881 if Next_Node
= null then
885 return Cursor
'(Position.Container, Next_Node);
894 (Container : in out List;
895 New_Item : Element_Type;
896 Count : Count_Type := 1)
899 Insert (Container, First (Container), New_Item, Count);
906 procedure Previous (Position : in out Cursor) is
908 pragma Assert (Vet (Position), "bad cursor in procedure Previous");
910 if Position.Node = null then
914 Position.Node := Position.Node.Prev;
916 if Position.Node = null then
917 Position.Container := null;
921 function Previous (Position : Cursor) return Cursor is
923 pragma Assert (Vet (Position), "bad cursor in function Previous");
925 if Position.Node = null then
930 Prev_Node : constant Node_Access := Position.Node.Prev;
932 if Prev_Node = null then
936 return Cursor'(Position
.Container
, Prev_Node
);
944 procedure Query_Element
946 Process
: not null access procedure (Element
: Element_Type
))
949 if Position
.Node
= null then
950 raise Constraint_Error
;
953 pragma Assert
(Vet
(Position
), "bad cursor in Query_Element");
956 C
: List
renames Position
.Container
.all'Unrestricted_Access.all;
957 B
: Natural renames C
.Busy
;
958 L
: Natural renames C
.Lock
;
965 Process
(Position
.Node
.Element
);
983 (Stream
: access Root_Stream_Type
'Class;
991 Count_Type
'Base'Read (Stream, N);
1000 Element_Type'Read (Stream, X.Element);
1011 Item.Length := Item.Length + 1;
1012 exit when Item.Length = N;
1017 Element_Type'Read (Stream, X.Element);
1024 X.Prev := Item.Last;
1025 Item.Last.Next := X;
1031 (Stream : access Root_Stream_Type'Class;
1035 raise Program_Error;
1038 ---------------------
1039 -- Replace_Element --
1040 ---------------------
1042 procedure Replace_Element
1043 (Container : in out List;
1045 New_Item : Element_Type)
1048 if Position.Container = null then
1049 raise Constraint_Error;
1052 if Position.Container /= Container'Unchecked_Access then
1053 raise Program_Error;
1056 if Container.Lock > 0 then
1057 raise Program_Error;
1060 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
1062 Position.Node.Element := New_Item;
1063 end Replace_Element;
1065 ----------------------
1066 -- Reverse_Elements --
1067 ----------------------
1069 procedure Reverse_Elements (Container : in out List) is
1070 I : Node_Access := Container.First;
1071 J : Node_Access := Container.Last;
1073 procedure Swap (L, R : Node_Access);
1079 procedure Swap (L, R : Node_Access) is
1080 LN : constant Node_Access := L.Next;
1081 LP : constant Node_Access := L.Prev;
1083 RN : constant Node_Access := R.Next;
1084 RP : constant Node_Access := R.Prev;
1099 pragma Assert (RP = L);
1113 -- Start of processing for Reverse_Elements
1116 if Container.Length <= 1 then
1120 pragma Assert (Container.First.Prev = null);
1121 pragma Assert (Container.Last.Next = null);
1123 if Container.Busy > 0 then
1124 raise Program_Error;
1127 Container.First := J;
1128 Container.Last := I;
1130 Swap (L => I, R => J);
1138 Swap (L => J, R => I);
1147 pragma Assert (Container.First.Prev = null);
1148 pragma Assert (Container.Last.Next = null);
1149 end Reverse_Elements;
1155 function Reverse_Find
1157 Item : Element_Type;
1158 Position : Cursor := No_Element) return Cursor
1160 Node : Node_Access := Position.Node;
1164 Node := Container.Last;
1167 if Position.Container /= Container'Unrestricted_Access then
1168 raise Program_Error;
1171 pragma Assert (Vet (Position), "bad cursor in Reverse_Find");
1174 while Node /= null loop
1175 if Node.Element = Item then
1176 return Cursor'(Container
'Unchecked_Access, Node
);
1185 ---------------------
1186 -- Reverse_Iterate --
1187 ---------------------
1189 procedure Reverse_Iterate
1191 Process
: not null access procedure (Position
: Cursor
))
1193 C
: List
renames Container
'Unrestricted_Access.all;
1194 B
: Natural renames C
.Busy
;
1196 Node
: Node_Access
:= Container
.Last
;
1202 while Node
/= null loop
1203 Process
(Cursor
'(Container'Unchecked_Access, Node));
1214 end Reverse_Iterate;
1221 (Target : in out List;
1223 Source : in out List)
1226 if Before.Container /= null then
1227 if Before.Container /= Target'Unrestricted_Access then
1228 raise Program_Error;
1231 pragma Assert (Vet (Before), "bad cursor in Splice");
1234 if Target'Address = Source'Address
1235 or else Source.Length = 0
1240 pragma Assert (Source.First.Prev = null);
1241 pragma Assert (Source.Last.Next = null);
1243 if Target.Length > Count_Type'Last - Source.Length then
1244 raise Constraint_Error;
1248 or else Source.Busy > 0
1250 raise Program_Error;
1253 if Target.Length = 0 then
1254 pragma Assert (Target.First = null);
1255 pragma Assert (Target.Last = null);
1256 pragma Assert (Before = No_Element);
1258 Target.First := Source.First;
1259 Target.Last := Source.Last;
1261 elsif Before.Node = null then
1262 pragma Assert (Target.Last.Next = null);
1264 Target.Last.Next := Source.First;
1265 Source.First.Prev := Target.Last;
1267 Target.Last := Source.Last;
1269 elsif Before.Node = Target.First then
1270 pragma Assert (Target.First.Prev = null);
1272 Source.Last.Next := Target.First;
1273 Target.First.Prev := Source.Last;
1275 Target.First := Source.First;
1278 pragma Assert (Target.Length >= 2);
1280 Before.Node.Prev.Next := Source.First;
1281 Source.First.Prev := Before.Node.Prev;
1283 Before.Node.Prev := Source.Last;
1284 Source.Last.Next := Before.Node;
1287 Source.First := null;
1288 Source.Last := null;
1290 Target.Length := Target.Length + Source.Length;
1295 (Container : in out List;
1297 Position : in out Cursor)
1300 if Before.Container /= null then
1301 if Before.Container /= Container'Unchecked_Access then
1302 raise Program_Error;
1305 pragma Assert (Vet (Before), "bad Before cursor in Splice");
1308 if Position.Node = null then
1309 raise Constraint_Error;
1312 if Position.Container /= Container'Unrestricted_Access then
1313 raise Program_Error;
1316 pragma Assert (Vet (Position), "bad Position cursor in Splice");
1318 if Position.Node = Before.Node
1319 or else Position.Node.Next = Before.Node
1324 pragma Assert (Container.Length >= 2);
1326 if Container.Busy > 0 then
1327 raise Program_Error;
1330 if Before.Node = null then
1331 pragma Assert (Position.Node /= Container.Last);
1333 if Position.Node = Container.First then
1334 Container.First := Position.Node.Next;
1335 Container.First.Prev := null;
1337 Position.Node.Prev.Next := Position.Node.Next;
1338 Position.Node.Next.Prev := Position.Node.Prev;
1341 Container.Last.Next := Position.Node;
1342 Position.Node.Prev := Container.Last;
1344 Container.Last := Position.Node;
1345 Container.Last.Next := null;
1350 if Before.Node = Container.First then
1351 pragma Assert (Position.Node /= Container.First);
1353 if Position.Node = Container.Last then
1354 Container.Last := Position.Node.Prev;
1355 Container.Last.Next := null;
1357 Position.Node.Prev.Next := Position.Node.Next;
1358 Position.Node.Next.Prev := Position.Node.Prev;
1361 Container.First.Prev := Position.Node;
1362 Position.Node.Next := Container.First;
1364 Container.First := Position.Node;
1365 Container.First.Prev := null;
1370 if Position.Node = Container.First then
1371 Container.First := Position.Node.Next;
1372 Container.First.Prev := null;
1374 elsif Position.Node = Container.Last then
1375 Container.Last := Position.Node.Prev;
1376 Container.Last.Next := null;
1379 Position.Node.Prev.Next := Position.Node.Next;
1380 Position.Node.Next.Prev := Position.Node.Prev;
1383 Before.Node.Prev.Next := Position.Node;
1384 Position.Node.Prev := Before.Node.Prev;
1386 Before.Node.Prev := Position.Node;
1387 Position.Node.Next := Before.Node;
1389 pragma Assert (Container.First.Prev = null);
1390 pragma Assert (Container.Last.Next = null);
1394 (Target : in out List;
1396 Source : in out List;
1397 Position : in out Cursor)
1400 if Target'Address = Source'Address then
1401 Splice (Target, Before, Position);
1405 if Before.Container /= null then
1406 if Before.Container /= Target'Unrestricted_Access then
1407 raise Program_Error;
1410 pragma Assert (Vet (Before), "bad Before cursor in Splice");
1413 if Position.Node = null then
1414 raise Constraint_Error;
1417 if Position.Container /= Source'Unrestricted_Access then
1418 raise Program_Error;
1421 pragma Assert (Vet (Position), "bad Position cursor in Splice");
1423 if Target.Length = Count_Type'Last then
1424 raise Constraint_Error;
1428 or else Source.Busy > 0
1430 raise Program_Error;
1433 if Position.Node = Source.First then
1434 Source.First := Position.Node.Next;
1436 if Position.Node = Source.Last then
1437 pragma Assert (Source.First = null);
1438 pragma Assert (Source.Length = 1);
1439 Source.Last := null;
1442 Source.First.Prev := null;
1445 elsif Position.Node = Source.Last then
1446 pragma Assert (Source.Length >= 2);
1447 Source.Last := Position.Node.Prev;
1448 Source.Last.Next := null;
1451 pragma Assert (Source.Length >= 3);
1452 Position.Node.Prev.Next := Position.Node.Next;
1453 Position.Node.Next.Prev := Position.Node.Prev;
1456 if Target.Length = 0 then
1457 pragma Assert (Target.First = null);
1458 pragma Assert (Target.Last = null);
1459 pragma Assert (Before = No_Element);
1461 Target.First := Position.Node;
1462 Target.Last := Position.Node;
1464 Target.First.Prev := null;
1465 Target.Last.Next := null;
1467 elsif Before.Node = null then
1468 pragma Assert (Target.Last.Next = null);
1469 Target.Last.Next := Position.Node;
1470 Position.Node.Prev := Target.Last;
1472 Target.Last := Position.Node;
1473 Target.Last.Next := null;
1475 elsif Before.Node = Target.First then
1476 pragma Assert (Target.First.Prev = null);
1477 Target.First.Prev := Position.Node;
1478 Position.Node.Next := Target.First;
1480 Target.First := Position.Node;
1481 Target.First.Prev := null;
1484 pragma Assert (Target.Length >= 2);
1485 Before.Node.Prev.Next := Position.Node;
1486 Position.Node.Prev := Before.Node.Prev;
1488 Before.Node.Prev := Position.Node;
1489 Position.Node.Next := Before.Node;
1492 Target.Length := Target.Length + 1;
1493 Source.Length := Source.Length - 1;
1495 Position.Container := Target'Unchecked_Access;
1503 (Container : in out List;
1508 or else J.Node = null
1510 raise Constraint_Error;
1513 if I.Container /= Container'Unchecked_Access
1514 or else J.Container /= Container'Unchecked_Access
1516 raise Program_Error;
1519 if I.Node = J.Node then
1523 if Container.Lock > 0 then
1524 raise Program_Error;
1527 pragma Assert (Vet (I), "bad I cursor in Swap");
1528 pragma Assert (Vet (J), "bad J cursor in Swap");
1531 EI : Element_Type renames I.Node.Element;
1532 EJ : Element_Type renames J.Node.Element;
1534 EI_Copy : constant Element_Type := EI;
1546 procedure Swap_Links
1547 (Container : in out List;
1552 or else J.Node = null
1554 raise Constraint_Error;
1557 if I.Container /= Container'Unrestricted_Access
1558 or else I.Container /= J.Container
1560 raise Program_Error;
1563 if I.Node = J.Node then
1567 if Container.Busy > 0 then
1568 raise Program_Error;
1571 pragma Assert (Vet (I), "bad I cursor in Swap_Links");
1572 pragma Assert (Vet (J), "bad J cursor in Swap_Links");
1575 I_Next : constant Cursor := Next (I);
1576 J_Copy : Cursor := J;
1580 Splice (Container, Before => I, Position => J_Copy);
1584 J_Next : constant Cursor := Next (J);
1585 I_Copy : Cursor := I;
1589 Splice (Container, Before => J, Position => I_Copy);
1592 pragma Assert (Container.Length >= 3);
1594 Splice (Container, Before => I_Next, Position => J_Copy);
1595 Splice (Container, Before => J_Next, Position => I_Copy);
1602 --------------------
1603 -- Update_Element --
1604 --------------------
1606 procedure Update_Element
1607 (Container : in out List;
1609 Process : not null access procedure (Element : in out Element_Type))
1612 if Position.Node = null then
1613 raise Constraint_Error;
1616 if Position.Container /= Container'Unchecked_Access then
1617 raise Program_Error;
1620 pragma Assert (Vet (Position), "bad cursor in Update_Element");
1623 B : Natural renames Container.Busy;
1624 L : Natural renames Container.Lock;
1631 Process (Position.Node.Element);
1648 function Vet (Position : Cursor) return Boolean is
1650 if Position.Node = null then
1651 return Position.Container = null;
1654 if Position.Container = null then
1658 if Position.Node.Next = Position.Node then
1662 if Position.Node.Prev = Position.Node then
1667 L : List renames Position.Container.all;
1669 if L.Length = 0 then
1673 if L.First = null then
1677 if L.Last = null then
1681 if L.First.Prev /= null then
1685 if L.Last.Next /= null then
1689 if Position.Node.Prev = null
1690 and then Position.Node /= L.First
1695 if Position.Node.Next = null
1696 and then Position.Node /= L.Last
1701 if L.Length = 1 then
1702 return L.First = L.Last;
1705 if L.First = L.Last then
1709 if L.First.Next = null then
1713 if L.Last.Prev = null then
1717 if L.First.Next.Prev /= L.First then
1721 if L.Last.Prev.Next /= L.Last then
1725 if L.Length = 2 then
1726 if L.First.Next /= L.Last then
1730 if L.Last.Prev /= L.First then
1737 if L.First.Next = L.Last then
1741 if L.Last.Prev = L.First then
1745 if Position.Node = L.First then
1749 if Position.Node = L.Last then
1753 if Position.Node.Next = null then
1757 if Position.Node.Prev = null then
1761 if Position.Node.Next.Prev /= Position.Node then
1765 if Position.Node.Prev.Next /= Position.Node then
1769 if L.Length = 3 then
1770 if L.First.Next /= Position.Node then
1774 if L.Last.Prev /= Position.Node then
1788 (Stream : access Root_Stream_Type'Class;
1791 Node : Node_Access := Item.First;
1794 Count_Type'Base'Write
(Stream
, Item
.Length
);
1796 while Node
/= null loop
1797 Element_Type
'Write (Stream
, Node
.Element
);
1803 (Stream
: access Root_Stream_Type
'Class;
1807 raise Program_Error
;
1810 end Ada
.Containers
.Doubly_Linked_Lists
;