1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . D O U B L Y _ L I N K E D _ L I S T S --
9 -- Copyright (C) 2004-2011, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- This unit was originally developed by Matthew J Heaney. --
28 ------------------------------------------------------------------------------
30 with Ada.Unchecked_Deallocation;
32 with System; use type System.Address;
34 package body Ada.Containers.Doubly_Linked_Lists is
36 type Iterator is new Limited_Controlled and
37 List_Iterator_Interfaces.Reversible_Iterator with
39 Container : List_Access;
43 overriding procedure Finalize (Object : in out Iterator);
45 overriding function First (Object : Iterator) return Cursor;
46 overriding function Last (Object : Iterator) return Cursor;
48 overriding function Next
50 Position : Cursor) return Cursor;
52 overriding function Previous
54 Position : Cursor) return Cursor;
56 -----------------------
57 -- Local Subprograms --
58 -----------------------
60 procedure Free (X : in out Node_Access);
62 procedure Insert_Internal
63 (Container : in out List;
65 New_Node : Node_Access);
67 function Vet (Position : Cursor) return Boolean;
73 function "=" (Left, Right : List) return Boolean is
74 L : Node_Access := Left.First;
75 R : Node_Access := Right.First;
78 if Left'Address = Right'Address then
82 if Left.Length /= Right.Length then
86 for J in 1 .. Left.Length loop
87 if L.Element /= R.Element then
102 procedure Adjust (Container : in out List) is
103 Src : Node_Access := Container.First;
107 pragma Assert (Container.Last = null);
108 pragma Assert (Container.Length = 0);
109 pragma Assert (Container.Busy = 0);
110 pragma Assert (Container.Lock = 0);
114 pragma Assert (Container.First.Prev = null);
115 pragma Assert (Container.Last.Next = null);
116 pragma Assert (Container.Length > 0);
118 Container.First := null;
119 Container.Last := null;
120 Container.Length := 0;
124 Container.First := new Node_Type'(Src.Element, null, null);
125 Container.Last := Container.First;
126 Container.Length := 1;
129 while Src /= null loop
130 Container.Last.Next := new Node_Type'(Element => Src.Element,
131 Prev => Container.Last,
133 Container.Last := Container.Last.Next;
134 Container.Length := Container.Length + 1;
145 (Container : in out List;
146 New_Item : Element_Type;
147 Count : Count_Type := 1)
150 Insert (Container, No_Element, New_Item, Count);
157 procedure Assign (Target : in out List; Source : List) is
161 if Target'Address = Source'Address then
167 Node := Source.First;
168 while Node /= null loop
169 Target.Append (Node.Element);
178 procedure Clear (Container : in out List) is
182 if Container.Length = 0 then
183 pragma Assert (Container.First = null);
184 pragma Assert (Container.Last = null);
185 pragma Assert (Container.Busy = 0);
186 pragma Assert (Container.Lock = 0);
190 pragma Assert (Container.First.Prev = null);
191 pragma Assert (Container.Last.Next = null);
193 if Container.Busy > 0 then
194 raise Program_Error with
195 "attempt to tamper with cursors (list is busy)";
198 while Container.Length > 1 loop
199 X := Container.First;
200 pragma Assert (X.Next.Prev = Container.First);
202 Container.First := X.Next;
203 Container.First.Prev := null;
205 Container.Length := Container.Length - 1;
210 X := Container.First;
211 pragma Assert (X = Container.Last);
213 Container.First := null;
214 Container.Last := null;
215 Container.Length := 0;
217 pragma Warnings (Off);
219 pragma Warnings (On);
228 Item : Element_Type) return Boolean
231 return Find (Container, Item) /= No_Element;
238 function Copy (Source : List) return List is
240 return Target : List do
241 Target.Assign (Source);
250 (Container : in out List;
251 Position : in out Cursor;
252 Count : Count_Type := 1)
257 if Position.Node = null then
258 raise Constraint_Error with
259 "Position cursor has no element";
262 if Position.Container /= Container'Unrestricted_Access then
263 raise Program_Error with
264 "Position cursor designates wrong container";
267 pragma Assert (Vet (Position), "bad cursor in Delete");
269 if Position.Node = Container.First then
270 Delete_First (Container, Count);
271 Position := No_Element; -- Post-York behavior
276 Position := No_Element; -- Post-York behavior
280 if Container.Busy > 0 then
281 raise Program_Error with
282 "attempt to tamper with cursors (list is busy)";
285 for Index in 1 .. Count loop
287 Container.Length := Container.Length - 1;
289 if X = Container.Last then
290 Position := No_Element;
292 Container.Last := X.Prev;
293 Container.Last.Next := null;
299 Position.Node := X.Next;
301 X.Next.Prev := X.Prev;
302 X.Prev.Next := X.Next;
307 Position := No_Element; -- Post-York behavior
314 procedure Delete_First
315 (Container : in out List;
316 Count : Count_Type := 1)
321 if Count >= Container.Length then
330 if Container.Busy > 0 then
331 raise Program_Error with
332 "attempt to tamper with cursors (list is busy)";
335 for I in 1 .. Count loop
336 X := Container.First;
337 pragma Assert (X.Next.Prev = Container.First);
339 Container.First := X.Next;
340 Container.First.Prev := null;
342 Container.Length := Container.Length - 1;
352 procedure Delete_Last
353 (Container : in out List;
354 Count : Count_Type := 1)
359 if Count >= Container.Length then
368 if Container.Busy > 0 then
369 raise Program_Error with
370 "attempt to tamper with cursors (list is busy)";
373 for I in 1 .. Count loop
375 pragma Assert (X.Prev.Next = Container.Last);
377 Container.Last := X.Prev;
378 Container.Last.Next := null;
380 Container.Length := Container.Length - 1;
390 function Element (Position : Cursor) return Element_Type is
392 if Position.Node = null then
393 raise Constraint_Error with
394 "Position cursor has no element";
397 pragma Assert (Vet (Position), "bad cursor in Element");
399 return Position.Node.Element;
406 procedure Finalize (Object : in out Iterator) is
408 if Object.Container /= null then
410 B : Natural renames Object.Container.all.Busy;
424 Position : Cursor := No_Element) return Cursor
426 Node : Node_Access := Position.Node;
430 Node := Container.First;
433 if Position.Container /= Container'Unrestricted_Access then
434 raise Program_Error with
435 "Position cursor designates wrong container";
438 pragma Assert (Vet (Position), "bad cursor in Find");
441 while Node /= null loop
442 if Node.Element = Item then
443 return Cursor'(Container'Unrestricted_Access, Node);
456 function First (Container : List) return Cursor is
458 if Container.First = null then
462 return Cursor'(Container'Unrestricted_Access, Container.First);
465 function First (Object : Iterator) return Cursor is
467 -- The value of the iterator object's Node component influences the
468 -- behavior of the First (and Last) selector function.
470 -- When the Node component is null, this means the iterator object was
471 -- constructed without a start expression, in which case the (forward)
472 -- iteration starts from the (logical) beginning of the entire sequence
473 -- of items (corresponding to Container.First, for a forward iterator).
475 -- Otherwise, this is iteration over a partial sequence of items. When
476 -- the Node component is non-null, the iterator object was constructed
477 -- with a start expression, that specifies the position from which the
478 -- (forward) partial iteration begins.
480 if Object.Node = null then
481 return Doubly_Linked_Lists.First (Object.Container.all);
483 return Cursor'(Object.Container, Object.Node);
491 function First_Element (Container : List) return Element_Type is
493 if Container.First = null then
494 raise Constraint_Error with "list is empty";
497 return Container.First.Element;
504 procedure Free (X : in out Node_Access) is
505 procedure Deallocate is
506 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
513 ---------------------
514 -- Generic_Sorting --
515 ---------------------
517 package body Generic_Sorting is
523 function Is_Sorted (Container : List) return Boolean is
524 Node : Node_Access := Container.First;
527 for I in 2 .. Container.Length loop
528 if Node.Next.Element < Node.Element then
543 (Target : in out List;
544 Source : in out List)
550 -- The semantics of Merge changed slightly per AI05-0021. It was
551 -- originally the case that if Target and Source denoted the same
552 -- container object, then the GNAT implementation of Merge did
553 -- nothing. However, it was argued that RM05 did not precisely
554 -- specify the semantics for this corner case. The decision of the
555 -- ARG was that if Target and Source denote the same non-empty
556 -- container object, then Program_Error is raised.
558 if Source.Is_Empty then
562 if Target'Address = Source'Address then
563 raise Program_Error with
564 "Target and Source denote same non-empty container";
567 if Target.Busy > 0 then
568 raise Program_Error with
569 "attempt to tamper with cursors of Target (list is busy)";
572 if Source.Busy > 0 then
573 raise Program_Error with
574 "attempt to tamper with cursors of Source (list is busy)";
577 LI := First (Target);
578 RI := First (Source);
579 while RI.Node /= null loop
580 pragma Assert (RI.Node.Next = null
581 or else not (RI.Node.Next.Element <
584 if LI.Node = null then
585 Splice (Target, No_Element, Source);
589 pragma Assert (LI.Node.Next = null
590 or else not (LI.Node.Next.Element <
593 if RI.Node.Element < LI.Node.Element then
596 pragma Warnings (Off, RJ);
598 RI.Node := RI.Node.Next;
599 Splice (Target, LI, Source, RJ);
603 LI.Node := LI.Node.Next;
612 procedure Sort (Container : in out List) is
614 procedure Partition (Pivot : Node_Access; Back : Node_Access);
616 procedure Sort (Front, Back : Node_Access);
622 procedure Partition (Pivot : Node_Access; Back : Node_Access) is
623 Node : Node_Access := Pivot.Next;
626 while Node /= Back loop
627 if Node.Element < Pivot.Element then
629 Prev : constant Node_Access := Node.Prev;
630 Next : constant Node_Access := Node.Next;
636 Container.Last := Prev;
642 Node.Prev := Pivot.Prev;
646 if Node.Prev = null then
647 Container.First := Node;
649 Node.Prev.Next := Node;
665 procedure Sort (Front, Back : Node_Access) is
666 Pivot : constant Node_Access :=
667 (if Front = null then Container.First else Front.Next);
669 if Pivot /= Back then
670 Partition (Pivot, Back);
676 -- Start of processing for Sort
679 if Container.Length <= 1 then
683 pragma Assert (Container.First.Prev = null);
684 pragma Assert (Container.Last.Next = null);
686 if Container.Busy > 0 then
687 raise Program_Error with
688 "attempt to tamper with cursors (list is busy)";
691 Sort (Front => null, Back => null);
693 pragma Assert (Container.First.Prev = null);
694 pragma Assert (Container.Last.Next = null);
703 function Has_Element (Position : Cursor) return Boolean is
705 pragma Assert (Vet (Position), "bad cursor in Has_Element");
706 return Position.Node /= null;
714 (Container : in out List;
716 New_Item : Element_Type;
717 Position : out Cursor;
718 Count : Count_Type := 1)
720 New_Node : Node_Access;
723 if Before.Container /= null then
724 if Before.Container /= Container'Unrestricted_Access then
725 raise Program_Error with
726 "Before cursor designates wrong list";
729 pragma Assert (Vet (Before), "bad cursor in Insert");
737 if Container.Length > Count_Type'Last - Count then
738 raise Constraint_Error with "new length exceeds maximum";
741 if Container.Busy > 0 then
742 raise Program_Error with
743 "attempt to tamper with cursors (list is busy)";
746 New_Node := new Node_Type'(New_Item, null, null);
747 Insert_Internal (Container, Before.Node, New_Node);
749 Position := Cursor'(Container'Unchecked_Access, New_Node);
751 for J in Count_Type'(2) .. Count loop
752 New_Node := new Node_Type'(New_Item, null, null);
753 Insert_Internal (Container, Before.Node, New_Node);
758 (Container : in out List;
760 New_Item : Element_Type;
761 Count : Count_Type := 1)
764 pragma Unreferenced (Position);
766 Insert (Container, Before, New_Item, Position, Count);
770 (Container : in out List;
772 Position : out Cursor;
773 Count : Count_Type := 1)
775 New_Node : Node_Access;
778 if Before.Container /= null then
779 if Before.Container /= Container'Unrestricted_Access then
780 raise Program_Error with
781 "Before cursor designates wrong list";
784 pragma Assert (Vet (Before), "bad cursor in Insert");
792 if Container.Length > Count_Type'Last - Count then
793 raise Constraint_Error with "new length exceeds maximum";
796 if Container.Busy > 0 then
797 raise Program_Error with
798 "attempt to tamper with cursors (list is busy)";
801 New_Node := new Node_Type;
802 Insert_Internal (Container, Before.Node, New_Node);
804 Position := Cursor'(Container'Unchecked_Access, New_Node);
806 for J in Count_Type'(2) .. Count loop
807 New_Node := new Node_Type;
808 Insert_Internal (Container, Before.Node, New_Node);
812 ---------------------
813 -- Insert_Internal --
814 ---------------------
816 procedure Insert_Internal
817 (Container : in out List;
818 Before : Node_Access;
819 New_Node : Node_Access)
822 if Container.Length = 0 then
823 pragma Assert (Before = null);
824 pragma Assert (Container.First = null);
825 pragma Assert (Container.Last = null);
827 Container.First := New_Node;
828 Container.Last := New_Node;
830 elsif Before = null then
831 pragma Assert (Container.Last.Next = null);
833 Container.Last.Next := New_Node;
834 New_Node.Prev := Container.Last;
836 Container.Last := New_Node;
838 elsif Before = Container.First then
839 pragma Assert (Container.First.Prev = null);
841 Container.First.Prev := New_Node;
842 New_Node.Next := Container.First;
844 Container.First := New_Node;
847 pragma Assert (Container.First.Prev = null);
848 pragma Assert (Container.Last.Next = null);
850 New_Node.Next := Before;
851 New_Node.Prev := Before.Prev;
853 Before.Prev.Next := New_Node;
854 Before.Prev := New_Node;
857 Container.Length := Container.Length + 1;
864 function Is_Empty (Container : List) return Boolean is
866 return Container.Length = 0;
875 Process : not null access procedure (Position : Cursor))
877 B : Natural renames Container'Unrestricted_Access.all.Busy;
878 Node : Node_Access := Container.First;
884 while Node /= null loop
885 Process (Cursor'(Container'Unrestricted_Access, Node));
897 function Iterate (Container : List)
898 return List_Iterator_Interfaces.Reversible_Iterator'Class
900 B : Natural renames Container'Unrestricted_Access.all.Busy;
903 -- The value of the Node component influences the behavior of the First
904 -- and Last selector functions of the iterator object. When the Node
905 -- component is null (as is the case here), this means the iterator
906 -- object was constructed without a start expression. This is a
907 -- complete iterator, meaning that the iteration starts from the
908 -- (logical) beginning of the sequence of items.
910 -- Note: For a forward iterator, Container.First is the beginning, and
911 -- for a reverse iterator, Container.Last is the beginning.
913 return It : constant Iterator :=
914 Iterator'(Limited_Controlled with
915 Container => Container'Unrestricted_Access,
922 function Iterate (Container : List; Start : Cursor)
923 return List_Iterator_Interfaces.Reversible_Iterator'Class
925 B : Natural renames Container'Unrestricted_Access.all.Busy;
928 -- It was formerly the case that when Start = No_Element, the partial
929 -- iterator was defined to behave the same as for a complete iterator,
930 -- and iterate over the entire sequence of items. However, those
931 -- semantics were unintuitive and arguably error-prone (it is too easy
932 -- to accidentally create an endless loop), and so they were changed,
933 -- per the ARG meeting in Denver on 2011/11. However, there was no
934 -- consensus about what positive meaning this corner case should have,
935 -- and so it was decided to simply raise an exception. This does imply,
936 -- however, that it is not possible to use a partial iterator to specify
937 -- an empty sequence of items.
939 if Start = No_Element then
940 raise Constraint_Error with
941 "Start position for iterator equals No_Element";
944 if Start.Container /= Container'Unrestricted_Access then
945 raise Program_Error with
946 "Start cursor of Iterate designates wrong list";
949 pragma Assert (Vet (Start), "Start cursor of Iterate is bad");
951 -- The value of the Node component influences the behavior of the First
952 -- and Last selector functions of the iterator object. When the Node
953 -- component is non-null (as is the case here), it means that this
954 -- is a partial iteration, over a subset of the complete sequence of
955 -- items. The iterator object was constructed with a start expression,
956 -- indicating the position from which the iteration begins. Note that
957 -- the start position has the same value irrespective of whether this
958 -- is a forward or reverse iteration.
960 return It : constant Iterator :=
961 Iterator'(Limited_Controlled with
962 Container => Container'Unrestricted_Access,
973 function Last (Container : List) return Cursor is
975 if Container.Last = null then
979 return Cursor'(Container'Unrestricted_Access, Container.Last);
982 function Last (Object : Iterator) return Cursor is
984 -- The value of the iterator object's Node component influences the
985 -- behavior of the Last (and First) selector function.
987 -- When the Node component is null, this means the iterator object was
988 -- constructed without a start expression, in which case the (reverse)
989 -- iteration starts from the (logical) beginning of the entire sequence
990 -- (corresponding to Container.Last, for a reverse iterator).
992 -- Otherwise, this is iteration over a partial sequence of items. When
993 -- the Node component is non-null, the iterator object was constructed
994 -- with a start expression, that specifies the position from which the
995 -- (reverse) partial iteration begins.
997 if Object.Node = null then
998 return Doubly_Linked_Lists.Last (Object.Container.all);
1000 return Cursor'(Object.Container, Object.Node);
1008 function Last_Element (Container : List) return Element_Type is
1010 if Container.Last = null then
1011 raise Constraint_Error with "list is empty";
1014 return Container.Last.Element;
1021 function Length (Container : List) return Count_Type is
1023 return Container.Length;
1031 (Target : in out List;
1032 Source : in out List)
1035 if Target'Address = Source'Address then
1039 if Source.Busy > 0 then
1040 raise Program_Error with
1041 "attempt to tamper with cursors of Source (list is busy)";
1046 Target.First := Source.First;
1047 Source.First := null;
1049 Target.Last := Source.Last;
1050 Source.Last := null;
1052 Target.Length := Source.Length;
1060 procedure Next (Position : in out Cursor) is
1062 Position := Next (Position);
1065 function Next (Position : Cursor) return Cursor is
1067 if Position.Node = null then
1071 pragma Assert (Vet (Position), "bad cursor in Next");
1074 Next_Node : constant Node_Access := Position.Node.Next;
1077 if Next_Node = null then
1081 return Cursor'(Position.Container, Next_Node);
1087 Position : Cursor) return Cursor
1090 if Position.Container = null then
1094 if Position.Container /= Object.Container then
1095 raise Program_Error with
1096 "Position cursor of Next designates wrong list";
1099 return Next (Position);
1107 (Container : in out List;
1108 New_Item : Element_Type;
1109 Count : Count_Type := 1)
1112 Insert (Container, First (Container), New_Item, Count);
1119 procedure Previous (Position : in out Cursor) is
1121 Position := Previous (Position);
1124 function Previous (Position : Cursor) return Cursor is
1126 if Position.Node = null then
1130 pragma Assert (Vet (Position), "bad cursor in Previous");
1133 Prev_Node : constant Node_Access := Position.Node.Prev;
1136 if Prev_Node = null then
1140 return Cursor'(Position.Container, Prev_Node);
1146 Position : Cursor) return Cursor
1149 if Position.Container = null then
1153 if Position.Container /= Object.Container then
1154 raise Program_Error with
1155 "Position cursor of Previous designates wrong list";
1158 return Previous (Position);
1165 procedure Query_Element
1167 Process : not null access procedure (Element : Element_Type))
1170 if Position.Node = null then
1171 raise Constraint_Error with
1172 "Position cursor has no element";
1175 pragma Assert (Vet (Position), "bad cursor in Query_Element");
1178 C : List renames Position.Container.all'Unrestricted_Access.all;
1179 B : Natural renames C.Busy;
1180 L : Natural renames C.Lock;
1187 Process (Position.Node.Element);
1205 (Stream : not null access Root_Stream_Type'Class;
1208 N : Count_Type'Base;
1213 Count_Type'Base'Read (Stream, N);
1222 Element_Type'Read (Stream, X.Element);
1233 Item.Length := Item.Length + 1;
1234 exit when Item.Length = N;
1239 Element_Type'Read (Stream, X.Element);
1246 X.Prev := Item.Last;
1247 Item.Last.Next := X;
1253 (Stream : not null access Root_Stream_Type'Class;
1257 raise Program_Error with "attempt to stream list cursor";
1261 (Stream : not null access Root_Stream_Type'Class;
1262 Item : out Reference_Type)
1265 raise Program_Error with "attempt to stream reference";
1269 (Stream : not null access Root_Stream_Type'Class;
1270 Item : out Constant_Reference_Type)
1273 raise Program_Error with "attempt to stream reference";
1280 function Constant_Reference
1282 Position : Cursor) return Constant_Reference_Type
1285 pragma Unreferenced (Container);
1287 if Position.Container = null then
1288 raise Constraint_Error with "Position cursor has no element";
1291 return (Element => Position.Node.Element'Access);
1292 end Constant_Reference;
1296 Position : Cursor) return Reference_Type
1299 pragma Unreferenced (Container);
1301 if Position.Container = null then
1302 raise Constraint_Error with "Position cursor has no element";
1305 return (Element => Position.Node.Element'Access);
1308 ---------------------
1309 -- Replace_Element --
1310 ---------------------
1312 procedure Replace_Element
1313 (Container : in out List;
1315 New_Item : Element_Type)
1318 if Position.Container = null then
1319 raise Constraint_Error with "Position cursor has no element";
1322 if Position.Container /= Container'Unchecked_Access then
1323 raise Program_Error with
1324 "Position cursor designates wrong container";
1327 if Container.Lock > 0 then
1328 raise Program_Error with
1329 "attempt to tamper with elements (list is locked)";
1332 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
1334 Position.Node.Element := New_Item;
1335 end Replace_Element;
1337 ----------------------
1338 -- Reverse_Elements --
1339 ----------------------
1341 procedure Reverse_Elements (Container : in out List) is
1342 I : Node_Access := Container.First;
1343 J : Node_Access := Container.Last;
1345 procedure Swap (L, R : Node_Access);
1351 procedure Swap (L, R : Node_Access) is
1352 LN : constant Node_Access := L.Next;
1353 LP : constant Node_Access := L.Prev;
1355 RN : constant Node_Access := R.Next;
1356 RP : constant Node_Access := R.Prev;
1371 pragma Assert (RP = L);
1385 -- Start of processing for Reverse_Elements
1388 if Container.Length <= 1 then
1392 pragma Assert (Container.First.Prev = null);
1393 pragma Assert (Container.Last.Next = null);
1395 if Container.Busy > 0 then
1396 raise Program_Error with
1397 "attempt to tamper with cursors (list is busy)";
1400 Container.First := J;
1401 Container.Last := I;
1403 Swap (L => I, R => J);
1411 Swap (L => J, R => I);
1420 pragma Assert (Container.First.Prev = null);
1421 pragma Assert (Container.Last.Next = null);
1422 end Reverse_Elements;
1428 function Reverse_Find
1430 Item : Element_Type;
1431 Position : Cursor := No_Element) return Cursor
1433 Node : Node_Access := Position.Node;
1437 Node := Container.Last;
1440 if Position.Container /= Container'Unrestricted_Access then
1441 raise Program_Error with
1442 "Position cursor designates wrong container";
1445 pragma Assert (Vet (Position), "bad cursor in Reverse_Find");
1448 while Node /= null loop
1449 if Node.Element = Item then
1450 return Cursor'(Container'Unrestricted_Access, Node);
1459 ---------------------
1460 -- Reverse_Iterate --
1461 ---------------------
1463 procedure Reverse_Iterate
1465 Process : not null access procedure (Position : Cursor))
1467 C : List renames Container'Unrestricted_Access.all;
1468 B : Natural renames C.Busy;
1470 Node : Node_Access := Container.Last;
1476 while Node /= null loop
1477 Process (Cursor'(Container'Unrestricted_Access, Node));
1488 end Reverse_Iterate;
1495 (Target : in out List;
1497 Source : in out List)
1500 if Before.Container /= null then
1501 if Before.Container /= Target'Unrestricted_Access then
1502 raise Program_Error with
1503 "Before cursor designates wrong container";
1506 pragma Assert (Vet (Before), "bad cursor in Splice");
1509 if Target'Address = Source'Address
1510 or else Source.Length = 0
1515 pragma Assert (Source.First.Prev = null);
1516 pragma Assert (Source.Last.Next = null);
1518 if Target.Length > Count_Type'Last - Source.Length then
1519 raise Constraint_Error with "new length exceeds maximum";
1522 if Target.Busy > 0 then
1523 raise Program_Error with
1524 "attempt to tamper with cursors of Target (list is busy)";
1527 if Source.Busy > 0 then
1528 raise Program_Error with
1529 "attempt to tamper with cursors of Source (list is busy)";
1532 if Target.Length = 0 then
1533 pragma Assert (Target.First = null);
1534 pragma Assert (Target.Last = null);
1535 pragma Assert (Before = No_Element);
1537 Target.First := Source.First;
1538 Target.Last := Source.Last;
1540 elsif Before.Node = null then
1541 pragma Assert (Target.Last.Next = null);
1543 Target.Last.Next := Source.First;
1544 Source.First.Prev := Target.Last;
1546 Target.Last := Source.Last;
1548 elsif Before.Node = Target.First then
1549 pragma Assert (Target.First.Prev = null);
1551 Source.Last.Next := Target.First;
1552 Target.First.Prev := Source.Last;
1554 Target.First := Source.First;
1557 pragma Assert (Target.Length >= 2);
1559 Before.Node.Prev.Next := Source.First;
1560 Source.First.Prev := Before.Node.Prev;
1562 Before.Node.Prev := Source.Last;
1563 Source.Last.Next := Before.Node;
1566 Source.First := null;
1567 Source.Last := null;
1569 Target.Length := Target.Length + Source.Length;
1574 (Container : in out List;
1579 if Before.Container /= null then
1580 if Before.Container /= Container'Unchecked_Access then
1581 raise Program_Error with
1582 "Before cursor designates wrong container";
1585 pragma Assert (Vet (Before), "bad Before cursor in Splice");
1588 if Position.Node = null then
1589 raise Constraint_Error with "Position cursor has no element";
1592 if Position.Container /= Container'Unrestricted_Access then
1593 raise Program_Error with
1594 "Position cursor designates wrong container";
1597 pragma Assert (Vet (Position), "bad Position cursor in Splice");
1599 if Position.Node = Before.Node
1600 or else Position.Node.Next = Before.Node
1605 pragma Assert (Container.Length >= 2);
1607 if Container.Busy > 0 then
1608 raise Program_Error with
1609 "attempt to tamper with cursors (list is busy)";
1612 if Before.Node = null then
1613 pragma Assert (Position.Node /= Container.Last);
1615 if Position.Node = Container.First then
1616 Container.First := Position.Node.Next;
1617 Container.First.Prev := null;
1619 Position.Node.Prev.Next := Position.Node.Next;
1620 Position.Node.Next.Prev := Position.Node.Prev;
1623 Container.Last.Next := Position.Node;
1624 Position.Node.Prev := Container.Last;
1626 Container.Last := Position.Node;
1627 Container.Last.Next := null;
1632 if Before.Node = Container.First then
1633 pragma Assert (Position.Node /= Container.First);
1635 if Position.Node = Container.Last then
1636 Container.Last := Position.Node.Prev;
1637 Container.Last.Next := null;
1639 Position.Node.Prev.Next := Position.Node.Next;
1640 Position.Node.Next.Prev := Position.Node.Prev;
1643 Container.First.Prev := Position.Node;
1644 Position.Node.Next := Container.First;
1646 Container.First := Position.Node;
1647 Container.First.Prev := null;
1652 if Position.Node = Container.First then
1653 Container.First := Position.Node.Next;
1654 Container.First.Prev := null;
1656 elsif Position.Node = Container.Last then
1657 Container.Last := Position.Node.Prev;
1658 Container.Last.Next := null;
1661 Position.Node.Prev.Next := Position.Node.Next;
1662 Position.Node.Next.Prev := Position.Node.Prev;
1665 Before.Node.Prev.Next := Position.Node;
1666 Position.Node.Prev := Before.Node.Prev;
1668 Before.Node.Prev := Position.Node;
1669 Position.Node.Next := Before.Node;
1671 pragma Assert (Container.First.Prev = null);
1672 pragma Assert (Container.Last.Next = null);
1676 (Target : in out List;
1678 Source : in out List;
1679 Position : in out Cursor)
1682 if Target'Address = Source'Address then
1683 Splice (Target, Before, Position);
1687 if Before.Container /= null then
1688 if Before.Container /= Target'Unrestricted_Access then
1689 raise Program_Error with
1690 "Before cursor designates wrong container";
1693 pragma Assert (Vet (Before), "bad Before cursor in Splice");
1696 if Position.Node = null then
1697 raise Constraint_Error with "Position cursor has no element";
1700 if Position.Container /= Source'Unrestricted_Access then
1701 raise Program_Error with
1702 "Position cursor designates wrong container";
1705 pragma Assert (Vet (Position), "bad Position cursor in Splice");
1707 if Target.Length = Count_Type'Last then
1708 raise Constraint_Error with "Target is full";
1711 if Target.Busy > 0 then
1712 raise Program_Error with
1713 "attempt to tamper with cursors of Target (list is busy)";
1716 if Source.Busy > 0 then
1717 raise Program_Error with
1718 "attempt to tamper with cursors of Source (list is busy)";
1721 if Position.Node = Source.First then
1722 Source.First := Position.Node.Next;
1724 if Position.Node = Source.Last then
1725 pragma Assert (Source.First = null);
1726 pragma Assert (Source.Length = 1);
1727 Source.Last := null;
1730 Source.First.Prev := null;
1733 elsif Position.Node = Source.Last then
1734 pragma Assert (Source.Length >= 2);
1735 Source.Last := Position.Node.Prev;
1736 Source.Last.Next := null;
1739 pragma Assert (Source.Length >= 3);
1740 Position.Node.Prev.Next := Position.Node.Next;
1741 Position.Node.Next.Prev := Position.Node.Prev;
1744 if Target.Length = 0 then
1745 pragma Assert (Target.First = null);
1746 pragma Assert (Target.Last = null);
1747 pragma Assert (Before = No_Element);
1749 Target.First := Position.Node;
1750 Target.Last := Position.Node;
1752 Target.First.Prev := null;
1753 Target.Last.Next := null;
1755 elsif Before.Node = null then
1756 pragma Assert (Target.Last.Next = null);
1757 Target.Last.Next := Position.Node;
1758 Position.Node.Prev := Target.Last;
1760 Target.Last := Position.Node;
1761 Target.Last.Next := null;
1763 elsif Before.Node = Target.First then
1764 pragma Assert (Target.First.Prev = null);
1765 Target.First.Prev := Position.Node;
1766 Position.Node.Next := Target.First;
1768 Target.First := Position.Node;
1769 Target.First.Prev := null;
1772 pragma Assert (Target.Length >= 2);
1773 Before.Node.Prev.Next := Position.Node;
1774 Position.Node.Prev := Before.Node.Prev;
1776 Before.Node.Prev := Position.Node;
1777 Position.Node.Next := Before.Node;
1780 Target.Length := Target.Length + 1;
1781 Source.Length := Source.Length - 1;
1783 Position.Container := Target'Unchecked_Access;
1791 (Container : in out List;
1795 if I.Node = null then
1796 raise Constraint_Error with "I cursor has no element";
1799 if J.Node = null then
1800 raise Constraint_Error with "J cursor has no element";
1803 if I.Container /= Container'Unchecked_Access then
1804 raise Program_Error with "I cursor designates wrong container";
1807 if J.Container /= Container'Unchecked_Access then
1808 raise Program_Error with "J cursor designates wrong container";
1811 if I.Node = J.Node then
1815 if Container.Lock > 0 then
1816 raise Program_Error with
1817 "attempt to tamper with elements (list is locked)";
1820 pragma Assert (Vet (I), "bad I cursor in Swap");
1821 pragma Assert (Vet (J), "bad J cursor in Swap");
1824 EI : Element_Type renames I.Node.Element;
1825 EJ : Element_Type renames J.Node.Element;
1827 EI_Copy : constant Element_Type := EI;
1839 procedure Swap_Links
1840 (Container : in out List;
1844 if I.Node = null then
1845 raise Constraint_Error with "I cursor has no element";
1848 if J.Node = null then
1849 raise Constraint_Error with "J cursor has no element";
1852 if I.Container /= Container'Unrestricted_Access then
1853 raise Program_Error with "I cursor designates wrong container";
1856 if J.Container /= Container'Unrestricted_Access then
1857 raise Program_Error with "J cursor designates wrong container";
1860 if I.Node = J.Node then
1864 if Container.Busy > 0 then
1865 raise Program_Error with
1866 "attempt to tamper with cursors (list is busy)";
1869 pragma Assert (Vet (I), "bad I cursor in Swap_Links");
1870 pragma Assert (Vet (J), "bad J cursor in Swap_Links");
1873 I_Next : constant Cursor := Next (I);
1877 Splice (Container, Before => I, Position => J);
1881 J_Next : constant Cursor := Next (J);
1885 Splice (Container, Before => J, Position => I);
1888 pragma Assert (Container.Length >= 3);
1890 Splice (Container, Before => I_Next, Position => J);
1891 Splice (Container, Before => J_Next, Position => I);
1898 --------------------
1899 -- Update_Element --
1900 --------------------
1902 procedure Update_Element
1903 (Container : in out List;
1905 Process : not null access procedure (Element : in out Element_Type))
1908 if Position.Node = null then
1909 raise Constraint_Error with "Position cursor has no element";
1912 if Position.Container /= Container'Unchecked_Access then
1913 raise Program_Error with
1914 "Position cursor designates wrong container";
1917 pragma Assert (Vet (Position), "bad cursor in Update_Element");
1920 B : Natural renames Container.Busy;
1921 L : Natural renames Container.Lock;
1928 Process (Position.Node.Element);
1945 function Vet (Position : Cursor) return Boolean is
1947 if Position.Node = null then
1948 return Position.Container = null;
1951 if Position.Container = null then
1955 if Position.Node.Next = Position.Node then
1959 if Position.Node.Prev = Position.Node then
1964 L : List renames Position.Container.all;
1966 if L.Length = 0 then
1970 if L.First = null then
1974 if L.Last = null then
1978 if L.First.Prev /= null then
1982 if L.Last.Next /= null then
1986 if Position.Node.Prev = null
1987 and then Position.Node /= L.First
1992 -- If we get here, we know that this disjunction is true:
1993 -- Position.Node.Prev /= null or else Position.Node = L.First
1995 if Position.Node.Next = null
1996 and then Position.Node /= L.Last
2001 -- If we get here, we know that this disjunction is true:
2002 -- Position.Node.Next /= null or else Position.Node = L.Last
2004 if L.Length = 1 then
2005 return L.First = L.Last;
2008 if L.First = L.Last then
2012 if L.First.Next = null then
2016 if L.Last.Prev = null then
2020 if L.First.Next.Prev /= L.First then
2024 if L.Last.Prev.Next /= L.Last then
2028 if L.Length = 2 then
2029 if L.First.Next /= L.Last then
2033 if L.Last.Prev /= L.First then
2040 if L.First.Next = L.Last then
2044 if L.Last.Prev = L.First then
2048 -- Eliminate earlier disjunct
2050 if Position.Node = L.First then
2054 -- If we get here, we know (disjunctive syllogism) that this
2055 -- predicate is true: Position.Node.Prev /= null
2057 -- Eliminate earlier disjunct
2059 if Position.Node = L.Last then
2063 -- If we get here, we know (disjunctive syllogism) that this
2064 -- predicate is true: Position.Node.Next /= null
2066 if Position.Node.Next.Prev /= Position.Node then
2070 if Position.Node.Prev.Next /= Position.Node then
2074 if L.Length = 3 then
2075 if L.First.Next /= Position.Node then
2079 if L.Last.Prev /= Position.Node then
2093 (Stream : not null access Root_Stream_Type'Class;
2096 Node : Node_Access := Item.First;
2099 Count_Type'Base'Write (Stream, Item.Length);
2101 while Node /= null loop
2102 Element_Type'Write (Stream, Node.Element);
2108 (Stream : not null access Root_Stream_Type'Class;
2112 raise Program_Error with "attempt to stream list cursor";
2116 (Stream : not null access Root_Stream_Type'Class;
2117 Item : Reference_Type)
2120 raise Program_Error with "attempt to stream reference";
2124 (Stream : not null access Root_Stream_Type'Class;
2125 Item : Constant_Reference_Type)
2128 raise Program_Error with "attempt to stream reference";
2131 end Ada.Containers.Doubly_Linked_Lists;