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-2005, 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
503 (Pivot
: in Node_Access
;
504 Back
: in Node_Access
);
506 procedure Sort
(Front
, Back
: Node_Access
);
513 (Pivot
: Node_Access
;
516 Node
: Node_Access
:= Pivot
.Next
;
519 while Node
/= Back
loop
520 if Node
.Element
< Pivot
.Element
then
522 Prev
: constant Node_Access
:= Node
.Prev
;
523 Next
: constant Node_Access
:= Node
.Next
;
529 Container
.Last
:= Prev
;
535 Node
.Prev
:= Pivot
.Prev
;
539 if Node
.Prev
= null then
540 Container
.First
:= Node
;
542 Node
.Prev
.Next
:= Node
;
558 procedure Sort
(Front
, Back
: Node_Access
) is
563 Pivot
:= Container
.First
;
568 if Pivot
/= Back
then
569 Partition
(Pivot
, Back
);
575 -- Start of processing for Sort
578 if Container
.Length
<= 1 then
582 pragma Assert
(Container
.First
.Prev
= null);
583 pragma Assert
(Container
.Last
.Next
= null);
585 if Container
.Busy
> 0 then
589 Sort
(Front
=> null, Back
=> null);
591 pragma Assert
(Container
.First
.Prev
= null);
592 pragma Assert
(Container
.Last
.Next
= null);
601 function Has_Element
(Position
: Cursor
) return Boolean is
603 pragma Assert
(Vet
(Position
), "bad cursor in Has_Element");
604 return Position
.Node
/= null;
612 (Container
: in out List
;
614 New_Item
: Element_Type
;
615 Position
: out Cursor
;
616 Count
: Count_Type
:= 1)
618 New_Node
: Node_Access
;
621 if Before
.Container
/= null then
622 if Before
.Container
/= Container
'Unrestricted_Access then
626 pragma Assert
(Vet
(Before
), "bad cursor in Insert");
634 if Container
.Length
> Count_Type
'Last - Count
then
635 raise Constraint_Error
;
638 if Container
.Busy
> 0 then
642 New_Node
:= new Node_Type
'(New_Item, null, null);
643 Insert_Internal (Container, Before.Node, New_Node);
645 Position := Cursor'(Container
'Unchecked_Access, New_Node
);
647 for J
in Count_Type
'(2) .. Count loop
648 New_Node := new Node_Type'(New_Item
, null, null);
649 Insert_Internal
(Container
, Before
.Node
, New_Node
);
654 (Container
: in out List
;
656 New_Item
: Element_Type
;
657 Count
: Count_Type
:= 1)
661 Insert
(Container
, Before
, New_Item
, Position
, Count
);
665 (Container
: in out List
;
667 Position
: out Cursor
;
668 Count
: Count_Type
:= 1)
670 New_Node
: Node_Access
;
673 if Before
.Container
/= null then
674 if Before
.Container
/= Container
'Unrestricted_Access then
678 pragma Assert
(Vet
(Before
), "bad cursor in Insert");
686 if Container
.Length
> Count_Type
'Last - Count
then
687 raise Constraint_Error
;
690 if Container
.Busy
> 0 then
694 New_Node
:= new Node_Type
;
695 Insert_Internal
(Container
, Before
.Node
, New_Node
);
697 Position
:= Cursor
'(Container'Unchecked_Access, New_Node);
699 for J in Count_Type'(2) .. Count
loop
700 New_Node
:= new Node_Type
;
701 Insert_Internal
(Container
, Before
.Node
, New_Node
);
705 ---------------------
706 -- Insert_Internal --
707 ---------------------
709 procedure Insert_Internal
710 (Container
: in out List
;
711 Before
: Node_Access
;
712 New_Node
: Node_Access
)
715 if Container
.Length
= 0 then
716 pragma Assert
(Before
= null);
717 pragma Assert
(Container
.First
= null);
718 pragma Assert
(Container
.Last
= null);
720 Container
.First
:= New_Node
;
721 Container
.Last
:= New_Node
;
723 elsif Before
= null then
724 pragma Assert
(Container
.Last
.Next
= null);
726 Container
.Last
.Next
:= New_Node
;
727 New_Node
.Prev
:= Container
.Last
;
729 Container
.Last
:= New_Node
;
731 elsif Before
= Container
.First
then
732 pragma Assert
(Container
.First
.Prev
= null);
734 Container
.First
.Prev
:= New_Node
;
735 New_Node
.Next
:= Container
.First
;
737 Container
.First
:= New_Node
;
740 pragma Assert
(Container
.First
.Prev
= null);
741 pragma Assert
(Container
.Last
.Next
= null);
743 New_Node
.Next
:= Before
;
744 New_Node
.Prev
:= Before
.Prev
;
746 Before
.Prev
.Next
:= New_Node
;
747 Before
.Prev
:= New_Node
;
750 Container
.Length
:= Container
.Length
+ 1;
757 function Is_Empty
(Container
: List
) return Boolean is
759 return Container
.Length
= 0;
768 Process
: not null access procedure (Position
: Cursor
))
770 C
: List
renames Container
'Unrestricted_Access.all;
771 B
: Natural renames C
.Busy
;
773 Node
: Node_Access
:= Container
.First
;
779 while Node
/= null loop
780 Process
(Cursor
'(Container'Unchecked_Access, Node));
796 function Last (Container : List) return Cursor is
798 if Container.Last = null then
802 return Cursor'(Container
'Unchecked_Access, Container
.Last
);
809 function Last_Element
(Container
: List
) return Element_Type
is
811 if Container
.Last
= null then
812 raise Constraint_Error
;
815 return Container
.Last
.Element
;
822 function Length
(Container
: List
) return Count_Type
is
824 return Container
.Length
;
832 (Target
: in out List
;
833 Source
: in out List
)
836 if Target
'Address = Source
'Address then
840 if Source
.Busy
> 0 then
846 Target
.First
:= Source
.First
;
847 Source
.First
:= null;
849 Target
.Last
:= Source
.Last
;
852 Target
.Length
:= Source
.Length
;
860 procedure Next
(Position
: in out Cursor
) is
862 pragma Assert
(Vet
(Position
), "bad cursor in procedure Next");
864 if Position
.Node
= null then
868 Position
.Node
:= Position
.Node
.Next
;
870 if Position
.Node
= null then
871 Position
.Container
:= null;
875 function Next
(Position
: Cursor
) return Cursor
is
877 pragma Assert
(Vet
(Position
), "bad cursor in function Next");
879 if Position
.Node
= null then
884 Next_Node
: constant Node_Access
:= Position
.Node
.Next
;
886 if Next_Node
= null then
890 return Cursor
'(Position.Container, Next_Node);
899 (Container : in out List;
900 New_Item : Element_Type;
901 Count : Count_Type := 1)
904 Insert (Container, First (Container), New_Item, Count);
911 procedure Previous (Position : in out Cursor) is
913 pragma Assert (Vet (Position), "bad cursor in procedure Previous");
915 if Position.Node = null then
919 Position.Node := Position.Node.Prev;
921 if Position.Node = null then
922 Position.Container := null;
926 function Previous (Position : Cursor) return Cursor is
928 pragma Assert (Vet (Position), "bad cursor in function Previous");
930 if Position.Node = null then
935 Prev_Node : constant Node_Access := Position.Node.Prev;
937 if Prev_Node = null then
941 return Cursor'(Position
.Container
, Prev_Node
);
949 procedure Query_Element
951 Process
: not null access procedure (Element
: in Element_Type
))
954 if Position
.Node
= null then
955 raise Constraint_Error
;
958 pragma Assert
(Vet
(Position
), "bad cursor in Query_Element");
961 C
: List
renames Position
.Container
.all'Unrestricted_Access.all;
962 B
: Natural renames C
.Busy
;
963 L
: Natural renames C
.Lock
;
970 Process
(Position
.Node
.Element
);
988 (Stream
: access Root_Stream_Type
'Class;
996 Count_Type
'Base'Read (Stream, N);
1005 Element_Type'Read (Stream, X.Element);
1016 Item.Length := Item.Length + 1;
1017 exit when Item.Length = N;
1022 Element_Type'Read (Stream, X.Element);
1029 X.Prev := Item.Last;
1030 Item.Last.Next := X;
1036 (Stream : access Root_Stream_Type'Class;
1040 raise Program_Error;
1043 ---------------------
1044 -- Replace_Element --
1045 ---------------------
1047 procedure Replace_Element
1048 (Container : in out List;
1050 New_Item : Element_Type)
1053 if Position.Container = null then
1054 raise Constraint_Error;
1057 if Position.Container /= Container'Unchecked_Access then
1058 raise Program_Error;
1061 if Container.Lock > 0 then
1062 raise Program_Error;
1065 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
1067 Position.Node.Element := New_Item;
1068 end Replace_Element;
1070 ----------------------
1071 -- Reverse_Elements --
1072 ----------------------
1074 procedure Reverse_Elements (Container : in out List) is
1075 I : Node_Access := Container.First;
1076 J : Node_Access := Container.Last;
1078 procedure Swap (L, R : Node_Access);
1084 procedure Swap (L, R : Node_Access) is
1085 LN : constant Node_Access := L.Next;
1086 LP : constant Node_Access := L.Prev;
1088 RN : constant Node_Access := R.Next;
1089 RP : constant Node_Access := R.Prev;
1104 pragma Assert (RP = L);
1118 -- Start of processing for Reverse_Elements
1121 if Container.Length <= 1 then
1125 pragma Assert (Container.First.Prev = null);
1126 pragma Assert (Container.Last.Next = null);
1128 if Container.Busy > 0 then
1129 raise Program_Error;
1132 Container.First := J;
1133 Container.Last := I;
1135 Swap (L => I, R => J);
1143 Swap (L => J, R => I);
1152 pragma Assert (Container.First.Prev = null);
1153 pragma Assert (Container.Last.Next = null);
1154 end Reverse_Elements;
1160 function Reverse_Find
1162 Item : Element_Type;
1163 Position : Cursor := No_Element) return Cursor
1165 Node : Node_Access := Position.Node;
1169 Node := Container.Last;
1172 if Position.Container /= Container'Unrestricted_Access then
1173 raise Program_Error;
1176 pragma Assert (Vet (Position), "bad cursor in Reverse_Find");
1179 while Node /= null loop
1180 if Node.Element = Item then
1181 return Cursor'(Container
'Unchecked_Access, Node
);
1190 ---------------------
1191 -- Reverse_Iterate --
1192 ---------------------
1194 procedure Reverse_Iterate
1196 Process
: not null access procedure (Position
: Cursor
))
1198 C
: List
renames Container
'Unrestricted_Access.all;
1199 B
: Natural renames C
.Busy
;
1201 Node
: Node_Access
:= Container
.Last
;
1207 while Node
/= null loop
1208 Process
(Cursor
'(Container'Unchecked_Access, Node));
1219 end Reverse_Iterate;
1226 (Target : in out List;
1228 Source : in out List)
1231 if Before.Container /= null then
1232 if Before.Container /= Target'Unrestricted_Access then
1233 raise Program_Error;
1236 pragma Assert (Vet (Before), "bad cursor in Splice");
1239 if Target'Address = Source'Address
1240 or else Source.Length = 0
1245 pragma Assert (Source.First.Prev = null);
1246 pragma Assert (Source.Last.Next = null);
1248 if Target.Length > Count_Type'Last - Source.Length then
1249 raise Constraint_Error;
1253 or else Source.Busy > 0
1255 raise Program_Error;
1258 if Target.Length = 0 then
1259 pragma Assert (Target.First = null);
1260 pragma Assert (Target.Last = null);
1261 pragma Assert (Before = No_Element);
1263 Target.First := Source.First;
1264 Target.Last := Source.Last;
1266 elsif Before.Node = null then
1267 pragma Assert (Target.Last.Next = null);
1269 Target.Last.Next := Source.First;
1270 Source.First.Prev := Target.Last;
1272 Target.Last := Source.Last;
1274 elsif Before.Node = Target.First then
1275 pragma Assert (Target.First.Prev = null);
1277 Source.Last.Next := Target.First;
1278 Target.First.Prev := Source.Last;
1280 Target.First := Source.First;
1283 pragma Assert (Target.Length >= 2);
1285 Before.Node.Prev.Next := Source.First;
1286 Source.First.Prev := Before.Node.Prev;
1288 Before.Node.Prev := Source.Last;
1289 Source.Last.Next := Before.Node;
1292 Source.First := null;
1293 Source.Last := null;
1295 Target.Length := Target.Length + Source.Length;
1300 (Container : in out List;
1302 Position : in out Cursor)
1305 if Before.Container /= null then
1306 if Before.Container /= Container'Unchecked_Access then
1307 raise Program_Error;
1310 pragma Assert (Vet (Before), "bad Before cursor in Splice");
1313 if Position.Node = null then
1314 raise Constraint_Error;
1317 if Position.Container /= Container'Unrestricted_Access then
1318 raise Program_Error;
1321 pragma Assert (Vet (Position), "bad Position cursor in Splice");
1323 if Position.Node = Before.Node
1324 or else Position.Node.Next = Before.Node
1329 pragma Assert (Container.Length >= 2);
1331 if Container.Busy > 0 then
1332 raise Program_Error;
1335 if Before.Node = null then
1336 pragma Assert (Position.Node /= Container.Last);
1338 if Position.Node = Container.First then
1339 Container.First := Position.Node.Next;
1340 Container.First.Prev := null;
1342 Position.Node.Prev.Next := Position.Node.Next;
1343 Position.Node.Next.Prev := Position.Node.Prev;
1346 Container.Last.Next := Position.Node;
1347 Position.Node.Prev := Container.Last;
1349 Container.Last := Position.Node;
1350 Container.Last.Next := null;
1355 if Before.Node = Container.First then
1356 pragma Assert (Position.Node /= Container.First);
1358 if Position.Node = Container.Last then
1359 Container.Last := Position.Node.Prev;
1360 Container.Last.Next := null;
1362 Position.Node.Prev.Next := Position.Node.Next;
1363 Position.Node.Next.Prev := Position.Node.Prev;
1366 Container.First.Prev := Position.Node;
1367 Position.Node.Next := Container.First;
1369 Container.First := Position.Node;
1370 Container.First.Prev := null;
1375 if Position.Node = Container.First then
1376 Container.First := Position.Node.Next;
1377 Container.First.Prev := null;
1379 elsif Position.Node = Container.Last then
1380 Container.Last := Position.Node.Prev;
1381 Container.Last.Next := null;
1384 Position.Node.Prev.Next := Position.Node.Next;
1385 Position.Node.Next.Prev := Position.Node.Prev;
1388 Before.Node.Prev.Next := Position.Node;
1389 Position.Node.Prev := Before.Node.Prev;
1391 Before.Node.Prev := Position.Node;
1392 Position.Node.Next := Before.Node;
1394 pragma Assert (Container.First.Prev = null);
1395 pragma Assert (Container.Last.Next = null);
1399 (Target : in out List;
1401 Source : in out List;
1402 Position : in out Cursor)
1405 if Target'Address = Source'Address then
1406 Splice (Target, Before, Position);
1410 if Before.Container /= null then
1411 if Before.Container /= Target'Unrestricted_Access then
1412 raise Program_Error;
1415 pragma Assert (Vet (Before), "bad Before cursor in Splice");
1418 if Position.Node = null then
1419 raise Constraint_Error;
1422 if Position.Container /= Source'Unrestricted_Access then
1423 raise Program_Error;
1426 pragma Assert (Vet (Position), "bad Position cursor in Splice");
1428 if Target.Length = Count_Type'Last then
1429 raise Constraint_Error;
1433 or else Source.Busy > 0
1435 raise Program_Error;
1438 if Position.Node = Source.First then
1439 Source.First := Position.Node.Next;
1441 if Position.Node = Source.Last then
1442 pragma Assert (Source.First = null);
1443 pragma Assert (Source.Length = 1);
1444 Source.Last := null;
1447 Source.First.Prev := null;
1450 elsif Position.Node = Source.Last then
1451 pragma Assert (Source.Length >= 2);
1452 Source.Last := Position.Node.Prev;
1453 Source.Last.Next := null;
1456 pragma Assert (Source.Length >= 3);
1457 Position.Node.Prev.Next := Position.Node.Next;
1458 Position.Node.Next.Prev := Position.Node.Prev;
1461 if Target.Length = 0 then
1462 pragma Assert (Target.First = null);
1463 pragma Assert (Target.Last = null);
1464 pragma Assert (Before = No_Element);
1466 Target.First := Position.Node;
1467 Target.Last := Position.Node;
1469 Target.First.Prev := null;
1470 Target.Last.Next := null;
1472 elsif Before.Node = null then
1473 pragma Assert (Target.Last.Next = null);
1474 Target.Last.Next := Position.Node;
1475 Position.Node.Prev := Target.Last;
1477 Target.Last := Position.Node;
1478 Target.Last.Next := null;
1480 elsif Before.Node = Target.First then
1481 pragma Assert (Target.First.Prev = null);
1482 Target.First.Prev := Position.Node;
1483 Position.Node.Next := Target.First;
1485 Target.First := Position.Node;
1486 Target.First.Prev := null;
1489 pragma Assert (Target.Length >= 2);
1490 Before.Node.Prev.Next := Position.Node;
1491 Position.Node.Prev := Before.Node.Prev;
1493 Before.Node.Prev := Position.Node;
1494 Position.Node.Next := Before.Node;
1497 Target.Length := Target.Length + 1;
1498 Source.Length := Source.Length - 1;
1500 Position.Container := Target'Unchecked_Access;
1508 (Container : in out List;
1513 or else J.Node = null
1515 raise Constraint_Error;
1518 if I.Container /= Container'Unchecked_Access
1519 or else J.Container /= Container'Unchecked_Access
1521 raise Program_Error;
1524 if I.Node = J.Node then
1528 if Container.Lock > 0 then
1529 raise Program_Error;
1532 pragma Assert (Vet (I), "bad I cursor in Swap");
1533 pragma Assert (Vet (J), "bad J cursor in Swap");
1536 EI : Element_Type renames I.Node.Element;
1537 EJ : Element_Type renames J.Node.Element;
1539 EI_Copy : constant Element_Type := EI;
1551 procedure Swap_Links
1552 (Container : in out List;
1557 or else J.Node = null
1559 raise Constraint_Error;
1562 if I.Container /= Container'Unrestricted_Access
1563 or else I.Container /= J.Container
1565 raise Program_Error;
1568 if I.Node = J.Node then
1572 if Container.Busy > 0 then
1573 raise Program_Error;
1576 pragma Assert (Vet (I), "bad I cursor in Swap_Links");
1577 pragma Assert (Vet (J), "bad J cursor in Swap_Links");
1580 I_Next : constant Cursor := Next (I);
1581 J_Copy : Cursor := J;
1585 Splice (Container, Before => I, Position => J_Copy);
1589 J_Next : constant Cursor := Next (J);
1590 I_Copy : Cursor := I;
1594 Splice (Container, Before => J, Position => I_Copy);
1597 pragma Assert (Container.Length >= 3);
1599 Splice (Container, Before => I_Next, Position => J_Copy);
1600 Splice (Container, Before => J_Next, Position => I_Copy);
1607 --------------------
1608 -- Update_Element --
1609 --------------------
1611 procedure Update_Element
1612 (Container : in out List;
1614 Process : not null access procedure (Element : in out Element_Type))
1617 if Position.Node = null then
1618 raise Constraint_Error;
1621 if Position.Container /= Container'Unchecked_Access then
1622 raise Program_Error;
1625 pragma Assert (Vet (Position), "bad cursor in Update_Element");
1628 B : Natural renames Container.Busy;
1629 L : Natural renames Container.Lock;
1636 Process (Position.Node.Element);
1653 function Vet (Position : Cursor) return Boolean is
1655 if Position.Node = null then
1656 return Position.Container = null;
1659 if Position.Container = null then
1663 if Position.Node.Next = Position.Node then
1667 if Position.Node.Prev = Position.Node then
1672 L : List renames Position.Container.all;
1674 if L.Length = 0 then
1678 if L.First = null then
1682 if L.Last = null then
1686 if L.First.Prev /= null then
1690 if L.Last.Next /= null then
1694 if Position.Node.Prev = null
1695 and then Position.Node /= L.First
1700 if Position.Node.Next = null
1701 and then Position.Node /= L.Last
1706 if L.Length = 1 then
1707 return L.First = L.Last;
1710 if L.First = L.Last then
1714 if L.First.Next = null then
1718 if L.Last.Prev = null then
1722 if L.First.Next.Prev /= L.First then
1726 if L.Last.Prev.Next /= L.Last then
1730 if L.Length = 2 then
1731 if L.First.Next /= L.Last then
1735 if L.Last.Prev /= L.First then
1742 if L.First.Next = L.Last then
1746 if L.Last.Prev = L.First then
1750 if Position.Node = L.First then
1754 if Position.Node = L.Last then
1758 if Position.Node.Next = null then
1762 if Position.Node.Prev = null then
1766 if Position.Node.Next.Prev /= Position.Node then
1770 if Position.Node.Prev.Next /= Position.Node then
1774 if L.Length = 3 then
1775 if L.First.Next /= Position.Node then
1779 if L.Last.Prev /= Position.Node then
1793 (Stream : access Root_Stream_Type'Class;
1796 Node : Node_Access := Item.First;
1799 Count_Type'Base'Write
(Stream
, Item
.Length
);
1801 while Node
/= null loop
1802 Element_Type
'Write (Stream
, Node
.Element
);
1808 (Stream
: access Root_Stream_Type
'Class;
1812 raise Program_Error
;
1815 end Ada
.Containers
.Doubly_Linked_Lists
;