* gcc.c (getenv_spec_function): New function.
[official-gcc.git] / gcc / ada / a-cdlili.adb
blob611bfb09b5d5a42271ec054b9457f1ab9585411f
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
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 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2004-2006, Free Software Foundation, Inc. --
10 -- --
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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- This unit was originally developed by Matthew J Heaney. --
30 ------------------------------------------------------------------------------
32 with System; use type System.Address;
34 with Ada.Unchecked_Deallocation;
36 package body Ada.Containers.Doubly_Linked_Lists is
38 -----------------------
39 -- Local Subprograms --
40 -----------------------
42 procedure Free (X : in out Node_Access);
44 procedure Insert_Internal
45 (Container : in out List;
46 Before : Node_Access;
47 New_Node : Node_Access);
49 function Vet (Position : Cursor) return Boolean;
51 ---------
52 -- "=" --
53 ---------
55 function "=" (Left, Right : List) return Boolean is
56 L : Node_Access := Left.First;
57 R : Node_Access := Right.First;
59 begin
60 if Left'Address = Right'Address then
61 return True;
62 end if;
64 if Left.Length /= Right.Length then
65 return False;
66 end if;
68 for J in 1 .. Left.Length loop
69 if L.Element /= R.Element then
70 return False;
71 end if;
73 L := L.Next;
74 R := R.Next;
75 end loop;
77 return True;
78 end "=";
80 ------------
81 -- Adjust --
82 ------------
84 procedure Adjust (Container : in out List) is
85 Src : Node_Access := Container.First;
87 begin
88 if Src = null then
89 pragma Assert (Container.Last = null);
90 pragma Assert (Container.Length = 0);
91 pragma Assert (Container.Busy = 0);
92 pragma Assert (Container.Lock = 0);
93 return;
94 end if;
96 pragma Assert (Container.First.Prev = null);
97 pragma Assert (Container.Last.Next = null);
98 pragma Assert (Container.Length > 0);
100 Container.First := null;
101 Container.Last := null;
102 Container.Length := 0;
103 Container.Busy := 0;
104 Container.Lock := 0;
106 Container.First := new Node_Type'(Src.Element, null, null);
107 Container.Last := Container.First;
108 Container.Length := 1;
110 Src := Src.Next;
111 while Src /= null loop
112 Container.Last.Next := new Node_Type'(Element => Src.Element,
113 Prev => Container.Last,
114 Next => null);
115 Container.Last := Container.Last.Next;
116 Container.Length := Container.Length + 1;
118 Src := Src.Next;
119 end loop;
120 end Adjust;
122 ------------
123 -- Append --
124 ------------
126 procedure Append
127 (Container : in out List;
128 New_Item : Element_Type;
129 Count : Count_Type := 1)
131 begin
132 Insert (Container, No_Element, New_Item, Count);
133 end Append;
135 -----------
136 -- Clear --
137 -----------
139 procedure Clear (Container : in out List) is
140 X : Node_Access;
142 begin
143 if Container.Length = 0 then
144 pragma Assert (Container.First = null);
145 pragma Assert (Container.Last = null);
146 pragma Assert (Container.Busy = 0);
147 pragma Assert (Container.Lock = 0);
148 return;
149 end if;
151 pragma Assert (Container.First.Prev = null);
152 pragma Assert (Container.Last.Next = null);
154 if Container.Busy > 0 then
155 raise Program_Error with
156 "attempt to tamper with elements (list is busy)";
157 end if;
159 while Container.Length > 1 loop
160 X := Container.First;
161 pragma Assert (X.Next.Prev = Container.First);
163 Container.First := X.Next;
164 Container.First.Prev := null;
166 Container.Length := Container.Length - 1;
168 Free (X);
169 end loop;
171 X := Container.First;
172 pragma Assert (X = Container.Last);
174 Container.First := null;
175 Container.Last := null;
176 Container.Length := 0;
178 Free (X);
179 end Clear;
181 --------------
182 -- Contains --
183 --------------
185 function Contains
186 (Container : List;
187 Item : Element_Type) return Boolean
189 begin
190 return Find (Container, Item) /= No_Element;
191 end Contains;
193 ------------
194 -- Delete --
195 ------------
197 procedure Delete
198 (Container : in out List;
199 Position : in out Cursor;
200 Count : Count_Type := 1)
202 X : Node_Access;
204 begin
205 if Position.Node = null then
206 raise Constraint_Error with
207 "Position cursor has no element";
208 end if;
210 if Position.Container /= Container'Unrestricted_Access then
211 raise Program_Error with
212 "Position cursor designates wrong container";
213 end if;
215 pragma Assert (Vet (Position), "bad cursor in Delete");
217 if Position.Node = Container.First then
218 Delete_First (Container, Count);
219 Position := No_Element; -- Post-York behavior
220 return;
221 end if;
223 if Count = 0 then
224 Position := No_Element; -- Post-York behavior
225 return;
226 end if;
228 if Container.Busy > 0 then
229 raise Program_Error with
230 "attempt to tamper with elements (list is busy)";
231 end if;
233 for Index in 1 .. Count loop
234 X := Position.Node;
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;
243 Free (X);
244 return;
245 end if;
247 Position.Node := X.Next;
249 X.Next.Prev := X.Prev;
250 X.Prev.Next := X.Next;
252 Free (X);
253 end loop;
255 Position := No_Element; -- Post-York behavior
256 end Delete;
258 ------------------
259 -- Delete_First --
260 ------------------
262 procedure Delete_First
263 (Container : in out List;
264 Count : Count_Type := 1)
266 X : Node_Access;
268 begin
269 if Count >= Container.Length then
270 Clear (Container);
271 return;
272 end if;
274 if Count = 0 then
275 return;
276 end if;
278 if Container.Busy > 0 then
279 raise Program_Error with
280 "attempt to tamper with elements (list is busy)";
281 end if;
283 for I in 1 .. Count loop
284 X := Container.First;
285 pragma Assert (X.Next.Prev = Container.First);
287 Container.First := X.Next;
288 Container.First.Prev := null;
290 Container.Length := Container.Length - 1;
292 Free (X);
293 end loop;
294 end Delete_First;
296 -----------------
297 -- Delete_Last --
298 -----------------
300 procedure Delete_Last
301 (Container : in out List;
302 Count : Count_Type := 1)
304 X : Node_Access;
306 begin
307 if Count >= Container.Length then
308 Clear (Container);
309 return;
310 end if;
312 if Count = 0 then
313 return;
314 end if;
316 if Container.Busy > 0 then
317 raise Program_Error with
318 "attempt to tamper with elements (list is busy)";
319 end if;
321 for I in 1 .. Count loop
322 X := Container.Last;
323 pragma Assert (X.Prev.Next = Container.Last);
325 Container.Last := X.Prev;
326 Container.Last.Next := null;
328 Container.Length := Container.Length - 1;
330 Free (X);
331 end loop;
332 end Delete_Last;
334 -------------
335 -- Element --
336 -------------
338 function Element (Position : Cursor) return Element_Type is
339 begin
340 if Position.Node = null then
341 raise Constraint_Error with
342 "Position cursor has no element";
343 end if;
345 pragma Assert (Vet (Position), "bad cursor in Element");
347 return Position.Node.Element;
348 end Element;
350 ----------
351 -- Find --
352 ----------
354 function Find
355 (Container : List;
356 Item : Element_Type;
357 Position : Cursor := No_Element) return Cursor
359 Node : Node_Access := Position.Node;
361 begin
362 if Node = null then
363 Node := Container.First;
365 else
366 if Position.Container /= Container'Unrestricted_Access then
367 raise Program_Error with
368 "Position cursor designates wrong container";
369 end if;
371 pragma Assert (Vet (Position), "bad cursor in Find");
372 end if;
374 while Node /= null loop
375 if Node.Element = Item then
376 return Cursor'(Container'Unchecked_Access, Node);
377 end if;
379 Node := Node.Next;
380 end loop;
382 return No_Element;
383 end Find;
385 -----------
386 -- First --
387 -----------
389 function First (Container : List) return Cursor is
390 begin
391 if Container.First = null then
392 return No_Element;
393 end if;
395 return Cursor'(Container'Unchecked_Access, Container.First);
396 end First;
398 -------------------
399 -- First_Element --
400 -------------------
402 function First_Element (Container : List) return Element_Type is
403 begin
404 if Container.First = null then
405 raise Constraint_Error with "list is empty";
406 end if;
408 return Container.First.Element;
409 end First_Element;
411 ----------
412 -- Free --
413 ----------
415 procedure Free (X : in out Node_Access) is
416 procedure Deallocate is
417 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
419 begin
420 X.Prev := X;
421 X.Next := X;
422 Deallocate (X);
423 end Free;
425 ---------------------
426 -- Generic_Sorting --
427 ---------------------
429 package body Generic_Sorting is
431 ---------------
432 -- Is_Sorted --
433 ---------------
435 function Is_Sorted (Container : List) return Boolean is
436 Node : Node_Access := Container.First;
438 begin
439 for I in 2 .. Container.Length loop
440 if Node.Next.Element < Node.Element then
441 return False;
442 end if;
444 Node := Node.Next;
445 end loop;
447 return True;
448 end Is_Sorted;
450 -----------
451 -- Merge --
452 -----------
454 procedure Merge
455 (Target : in out List;
456 Source : in out List)
458 LI, RI : Cursor;
460 begin
461 if Target'Address = Source'Address then
462 return;
463 end if;
465 if Target.Busy > 0 then
466 raise Program_Error with
467 "attempt to tamper with elements of Target (list is busy)";
468 end if;
470 if Source.Busy > 0 then
471 raise Program_Error with
472 "attempt to tamper with elements of Source (list is busy)";
473 end if;
475 LI := First (Target);
476 RI := First (Source);
477 while RI.Node /= null loop
478 pragma Assert (RI.Node.Next = null
479 or else not (RI.Node.Next.Element <
480 RI.Node.Element));
482 if LI.Node = null then
483 Splice (Target, No_Element, Source);
484 return;
485 end if;
487 pragma Assert (LI.Node.Next = null
488 or else not (LI.Node.Next.Element <
489 LI.Node.Element));
491 if RI.Node.Element < LI.Node.Element then
492 declare
493 RJ : Cursor := RI;
494 begin
495 RI.Node := RI.Node.Next;
496 Splice (Target, LI, Source, RJ);
497 end;
499 else
500 LI.Node := LI.Node.Next;
501 end if;
502 end loop;
503 end Merge;
505 ----------
506 -- Sort --
507 ----------
509 procedure Sort (Container : in out List) is
511 procedure Partition (Pivot : Node_Access; Back : Node_Access);
513 procedure Sort (Front, Back : Node_Access);
515 ---------------
516 -- Partition --
517 ---------------
519 procedure Partition (Pivot : Node_Access; Back : Node_Access) is
520 Node : Node_Access := Pivot.Next;
522 begin
523 while Node /= Back loop
524 if Node.Element < Pivot.Element then
525 declare
526 Prev : constant Node_Access := Node.Prev;
527 Next : constant Node_Access := Node.Next;
529 begin
530 Prev.Next := Next;
532 if Next = null then
533 Container.Last := Prev;
534 else
535 Next.Prev := Prev;
536 end if;
538 Node.Next := Pivot;
539 Node.Prev := Pivot.Prev;
541 Pivot.Prev := Node;
543 if Node.Prev = null then
544 Container.First := Node;
545 else
546 Node.Prev.Next := Node;
547 end if;
549 Node := Next;
550 end;
552 else
553 Node := Node.Next;
554 end if;
555 end loop;
556 end Partition;
558 ----------
559 -- Sort --
560 ----------
562 procedure Sort (Front, Back : Node_Access) is
563 Pivot : Node_Access;
565 begin
566 if Front = null then
567 Pivot := Container.First;
568 else
569 Pivot := Front.Next;
570 end if;
572 if Pivot /= Back then
573 Partition (Pivot, Back);
574 Sort (Front, Pivot);
575 Sort (Pivot, Back);
576 end if;
577 end Sort;
579 -- Start of processing for Sort
581 begin
582 if Container.Length <= 1 then
583 return;
584 end if;
586 pragma Assert (Container.First.Prev = null);
587 pragma Assert (Container.Last.Next = null);
589 if Container.Busy > 0 then
590 raise Program_Error with
591 "attempt to tamper with elements (list is busy)";
592 end if;
594 Sort (Front => null, Back => null);
596 pragma Assert (Container.First.Prev = null);
597 pragma Assert (Container.Last.Next = null);
598 end Sort;
600 end Generic_Sorting;
602 -----------------
603 -- Has_Element --
604 -----------------
606 function Has_Element (Position : Cursor) return Boolean is
607 begin
608 pragma Assert (Vet (Position), "bad cursor in Has_Element");
609 return Position.Node /= null;
610 end Has_Element;
612 ------------
613 -- Insert --
614 ------------
616 procedure Insert
617 (Container : in out List;
618 Before : Cursor;
619 New_Item : Element_Type;
620 Position : out Cursor;
621 Count : Count_Type := 1)
623 New_Node : Node_Access;
625 begin
626 if Before.Container /= null then
627 if Before.Container /= Container'Unrestricted_Access then
628 raise Program_Error with
629 "attempt to tamper with elements (list is busy)";
630 end if;
632 pragma Assert (Vet (Before), "bad cursor in Insert");
633 end if;
635 if Count = 0 then
636 Position := Before;
637 return;
638 end if;
640 if Container.Length > Count_Type'Last - Count then
641 raise Constraint_Error with "new length exceeds maximum";
642 end if;
644 if Container.Busy > 0 then
645 raise Program_Error with
646 "attempt to tamper with elements (list is busy)";
647 end if;
649 New_Node := new Node_Type'(New_Item, null, null);
650 Insert_Internal (Container, Before.Node, New_Node);
652 Position := Cursor'(Container'Unchecked_Access, New_Node);
654 for J in Count_Type'(2) .. Count loop
655 New_Node := new Node_Type'(New_Item, null, null);
656 Insert_Internal (Container, Before.Node, New_Node);
657 end loop;
658 end Insert;
660 procedure Insert
661 (Container : in out List;
662 Before : Cursor;
663 New_Item : Element_Type;
664 Count : Count_Type := 1)
666 Position : Cursor;
667 begin
668 Insert (Container, Before, New_Item, Position, Count);
669 end Insert;
671 procedure Insert
672 (Container : in out List;
673 Before : Cursor;
674 Position : out Cursor;
675 Count : Count_Type := 1)
677 New_Node : Node_Access;
679 begin
680 if Before.Container /= null then
681 if Before.Container /= Container'Unrestricted_Access then
682 raise Program_Error with
683 "Before cursor designates wrong list";
684 end if;
686 pragma Assert (Vet (Before), "bad cursor in Insert");
687 end if;
689 if Count = 0 then
690 Position := Before;
691 return;
692 end if;
694 if Container.Length > Count_Type'Last - Count then
695 raise Constraint_Error with "new length exceeds maximum";
696 end if;
698 if Container.Busy > 0 then
699 raise Program_Error with
700 "attempt to tamper with elements (list is busy)";
701 end if;
703 New_Node := new Node_Type;
704 Insert_Internal (Container, Before.Node, New_Node);
706 Position := Cursor'(Container'Unchecked_Access, New_Node);
708 for J in Count_Type'(2) .. Count loop
709 New_Node := new Node_Type;
710 Insert_Internal (Container, Before.Node, New_Node);
711 end loop;
712 end Insert;
714 ---------------------
715 -- Insert_Internal --
716 ---------------------
718 procedure Insert_Internal
719 (Container : in out List;
720 Before : Node_Access;
721 New_Node : Node_Access)
723 begin
724 if Container.Length = 0 then
725 pragma Assert (Before = null);
726 pragma Assert (Container.First = null);
727 pragma Assert (Container.Last = null);
729 Container.First := New_Node;
730 Container.Last := New_Node;
732 elsif Before = null then
733 pragma Assert (Container.Last.Next = null);
735 Container.Last.Next := New_Node;
736 New_Node.Prev := Container.Last;
738 Container.Last := New_Node;
740 elsif Before = Container.First then
741 pragma Assert (Container.First.Prev = null);
743 Container.First.Prev := New_Node;
744 New_Node.Next := Container.First;
746 Container.First := New_Node;
748 else
749 pragma Assert (Container.First.Prev = null);
750 pragma Assert (Container.Last.Next = null);
752 New_Node.Next := Before;
753 New_Node.Prev := Before.Prev;
755 Before.Prev.Next := New_Node;
756 Before.Prev := New_Node;
757 end if;
759 Container.Length := Container.Length + 1;
760 end Insert_Internal;
762 --------------
763 -- Is_Empty --
764 --------------
766 function Is_Empty (Container : List) return Boolean is
767 begin
768 return Container.Length = 0;
769 end Is_Empty;
771 -------------
772 -- Iterate --
773 -------------
775 procedure Iterate
776 (Container : List;
777 Process : not null access procedure (Position : Cursor))
779 C : List renames Container'Unrestricted_Access.all;
780 B : Natural renames C.Busy;
782 Node : Node_Access := Container.First;
784 begin
785 B := B + 1;
787 begin
788 while Node /= null loop
789 Process (Cursor'(Container'Unchecked_Access, Node));
790 Node := Node.Next;
791 end loop;
792 exception
793 when others =>
794 B := B - 1;
795 raise;
796 end;
798 B := B - 1;
799 end Iterate;
801 ----------
802 -- Last --
803 ----------
805 function Last (Container : List) return Cursor is
806 begin
807 if Container.Last = null then
808 return No_Element;
809 end if;
811 return Cursor'(Container'Unchecked_Access, Container.Last);
812 end Last;
814 ------------------
815 -- Last_Element --
816 ------------------
818 function Last_Element (Container : List) return Element_Type is
819 begin
820 if Container.Last = null then
821 raise Constraint_Error with "list is empty";
822 end if;
824 return Container.Last.Element;
825 end Last_Element;
827 ------------
828 -- Length --
829 ------------
831 function Length (Container : List) return Count_Type is
832 begin
833 return Container.Length;
834 end Length;
836 ----------
837 -- Move --
838 ----------
840 procedure Move
841 (Target : in out List;
842 Source : in out List)
844 begin
845 if Target'Address = Source'Address then
846 return;
847 end if;
849 if Source.Busy > 0 then
850 raise Program_Error with
851 "attempt to tamper with elements of Source (list is busy)";
852 end if;
854 Clear (Target);
856 Target.First := Source.First;
857 Source.First := null;
859 Target.Last := Source.Last;
860 Source.Last := null;
862 Target.Length := Source.Length;
863 Source.Length := 0;
864 end Move;
866 ----------
867 -- Next --
868 ----------
870 procedure Next (Position : in out Cursor) is
871 begin
872 Position := Next (Position);
873 end Next;
875 function Next (Position : Cursor) return Cursor is
876 begin
877 if Position.Node = null then
878 return No_Element;
879 end if;
881 pragma Assert (Vet (Position), "bad cursor in Next");
883 declare
884 Next_Node : constant Node_Access := Position.Node.Next;
885 begin
886 if Next_Node = null then
887 return No_Element;
888 end if;
890 return Cursor'(Position.Container, Next_Node);
891 end;
892 end Next;
894 -------------
895 -- Prepend --
896 -------------
898 procedure Prepend
899 (Container : in out List;
900 New_Item : Element_Type;
901 Count : Count_Type := 1)
903 begin
904 Insert (Container, First (Container), New_Item, Count);
905 end Prepend;
907 --------------
908 -- Previous --
909 --------------
911 procedure Previous (Position : in out Cursor) is
912 begin
913 Position := Previous (Position);
914 end Previous;
916 function Previous (Position : Cursor) return Cursor is
917 begin
918 if Position.Node = null then
919 return No_Element;
920 end if;
922 pragma Assert (Vet (Position), "bad cursor in Previous");
924 declare
925 Prev_Node : constant Node_Access := Position.Node.Prev;
926 begin
927 if Prev_Node = null then
928 return No_Element;
929 end if;
931 return Cursor'(Position.Container, Prev_Node);
932 end;
933 end Previous;
935 -------------------
936 -- Query_Element --
937 -------------------
939 procedure Query_Element
940 (Position : Cursor;
941 Process : not null access procedure (Element : Element_Type))
943 begin
944 if Position.Node = null then
945 raise Constraint_Error with
946 "Position cursor has no element";
947 end if;
949 pragma Assert (Vet (Position), "bad cursor in Query_Element");
951 declare
952 C : List renames Position.Container.all'Unrestricted_Access.all;
953 B : Natural renames C.Busy;
954 L : Natural renames C.Lock;
956 begin
957 B := B + 1;
958 L := L + 1;
960 begin
961 Process (Position.Node.Element);
962 exception
963 when others =>
964 L := L - 1;
965 B := B - 1;
966 raise;
967 end;
969 L := L - 1;
970 B := B - 1;
971 end;
972 end Query_Element;
974 ----------
975 -- Read --
976 ----------
978 procedure Read
979 (Stream : not null access Root_Stream_Type'Class;
980 Item : out List)
982 N : Count_Type'Base;
983 X : Node_Access;
985 begin
986 Clear (Item);
987 Count_Type'Base'Read (Stream, N);
989 if N = 0 then
990 return;
991 end if;
993 X := new Node_Type;
995 begin
996 Element_Type'Read (Stream, X.Element);
997 exception
998 when others =>
999 Free (X);
1000 raise;
1001 end;
1003 Item.First := X;
1004 Item.Last := X;
1006 loop
1007 Item.Length := Item.Length + 1;
1008 exit when Item.Length = N;
1010 X := new Node_Type;
1012 begin
1013 Element_Type'Read (Stream, X.Element);
1014 exception
1015 when others =>
1016 Free (X);
1017 raise;
1018 end;
1020 X.Prev := Item.Last;
1021 Item.Last.Next := X;
1022 Item.Last := X;
1023 end loop;
1024 end Read;
1026 procedure Read
1027 (Stream : not null access Root_Stream_Type'Class;
1028 Item : out Cursor)
1030 begin
1031 raise Program_Error with "attempt to stream list cursor";
1032 end Read;
1034 ---------------------
1035 -- Replace_Element --
1036 ---------------------
1038 procedure Replace_Element
1039 (Container : in out List;
1040 Position : Cursor;
1041 New_Item : Element_Type)
1043 begin
1044 if Position.Container = null then
1045 raise Constraint_Error with "Position cursor has no element";
1046 end if;
1048 if Position.Container /= Container'Unchecked_Access then
1049 raise Program_Error with
1050 "Position cursor designates wrong container";
1051 end if;
1053 if Container.Lock > 0 then
1054 raise Program_Error with
1055 "attempt to tamper with cursors (list is locked)";
1056 end if;
1058 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
1060 Position.Node.Element := New_Item;
1061 end Replace_Element;
1063 ----------------------
1064 -- Reverse_Elements --
1065 ----------------------
1067 procedure Reverse_Elements (Container : in out List) is
1068 I : Node_Access := Container.First;
1069 J : Node_Access := Container.Last;
1071 procedure Swap (L, R : Node_Access);
1073 ----------
1074 -- Swap --
1075 ----------
1077 procedure Swap (L, R : Node_Access) is
1078 LN : constant Node_Access := L.Next;
1079 LP : constant Node_Access := L.Prev;
1081 RN : constant Node_Access := R.Next;
1082 RP : constant Node_Access := R.Prev;
1084 begin
1085 if LP /= null then
1086 LP.Next := R;
1087 end if;
1089 if RN /= null then
1090 RN.Prev := L;
1091 end if;
1093 L.Next := RN;
1094 R.Prev := LP;
1096 if LN = R then
1097 pragma Assert (RP = L);
1099 L.Prev := R;
1100 R.Next := L;
1102 else
1103 L.Prev := RP;
1104 RP.Next := L;
1106 R.Next := LN;
1107 LN.Prev := R;
1108 end if;
1109 end Swap;
1111 -- Start of processing for Reverse_Elements
1113 begin
1114 if Container.Length <= 1 then
1115 return;
1116 end if;
1118 pragma Assert (Container.First.Prev = null);
1119 pragma Assert (Container.Last.Next = null);
1121 if Container.Busy > 0 then
1122 raise Program_Error with
1123 "attempt to tamper with elements (list is busy)";
1124 end if;
1126 Container.First := J;
1127 Container.Last := I;
1128 loop
1129 Swap (L => I, R => J);
1131 J := J.Next;
1132 exit when I = J;
1134 I := I.Prev;
1135 exit when I = J;
1137 Swap (L => J, R => I);
1139 I := I.Next;
1140 exit when I = J;
1142 J := J.Prev;
1143 exit when I = J;
1144 end loop;
1146 pragma Assert (Container.First.Prev = null);
1147 pragma Assert (Container.Last.Next = null);
1148 end Reverse_Elements;
1150 ------------------
1151 -- Reverse_Find --
1152 ------------------
1154 function Reverse_Find
1155 (Container : List;
1156 Item : Element_Type;
1157 Position : Cursor := No_Element) return Cursor
1159 Node : Node_Access := Position.Node;
1161 begin
1162 if Node = null then
1163 Node := Container.Last;
1165 else
1166 if Position.Container /= Container'Unrestricted_Access then
1167 raise Program_Error with
1168 "Position cursor designates wrong container";
1169 end if;
1171 pragma Assert (Vet (Position), "bad cursor in Reverse_Find");
1172 end if;
1174 while Node /= null loop
1175 if Node.Element = Item then
1176 return Cursor'(Container'Unchecked_Access, Node);
1177 end if;
1179 Node := Node.Prev;
1180 end loop;
1182 return No_Element;
1183 end Reverse_Find;
1185 ---------------------
1186 -- Reverse_Iterate --
1187 ---------------------
1189 procedure Reverse_Iterate
1190 (Container : List;
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;
1198 begin
1199 B := B + 1;
1201 begin
1202 while Node /= null loop
1203 Process (Cursor'(Container'Unchecked_Access, Node));
1204 Node := Node.Prev;
1205 end loop;
1207 exception
1208 when others =>
1209 B := B - 1;
1210 raise;
1211 end;
1213 B := B - 1;
1214 end Reverse_Iterate;
1216 ------------
1217 -- Splice --
1218 ------------
1220 procedure Splice
1221 (Target : in out List;
1222 Before : Cursor;
1223 Source : in out List)
1225 begin
1226 if Before.Container /= null then
1227 if Before.Container /= Target'Unrestricted_Access then
1228 raise Program_Error with
1229 "Before cursor designates wrong container";
1230 end if;
1232 pragma Assert (Vet (Before), "bad cursor in Splice");
1233 end if;
1235 if Target'Address = Source'Address
1236 or else Source.Length = 0
1237 then
1238 return;
1239 end if;
1241 pragma Assert (Source.First.Prev = null);
1242 pragma Assert (Source.Last.Next = null);
1244 if Target.Length > Count_Type'Last - Source.Length then
1245 raise Constraint_Error with "new length exceeds maximum";
1246 end if;
1248 if Target.Busy > 0 then
1249 raise Program_Error with
1250 "attempt to tamper with elements of Target (list is busy)";
1251 end if;
1253 if Source.Busy > 0 then
1254 raise Program_Error with
1255 "attempt to tamper with elements of Source (list is busy)";
1256 end if;
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;
1282 else
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;
1290 end if;
1292 Source.First := null;
1293 Source.Last := null;
1295 Target.Length := Target.Length + Source.Length;
1296 Source.Length := 0;
1297 end Splice;
1299 procedure Splice
1300 (Container : in out List;
1301 Before : Cursor;
1302 Position : Cursor)
1304 begin
1305 if Before.Container /= null then
1306 if Before.Container /= Container'Unchecked_Access then
1307 raise Program_Error with
1308 "Before cursor designates wrong container";
1309 end if;
1311 pragma Assert (Vet (Before), "bad Before cursor in Splice");
1312 end if;
1314 if Position.Node = null then
1315 raise Constraint_Error with "Position cursor has no element";
1316 end if;
1318 if Position.Container /= Container'Unrestricted_Access then
1319 raise Program_Error with
1320 "Position cursor designates wrong container";
1321 end if;
1323 pragma Assert (Vet (Position), "bad Position cursor in Splice");
1325 if Position.Node = Before.Node
1326 or else Position.Node.Next = Before.Node
1327 then
1328 return;
1329 end if;
1331 pragma Assert (Container.Length >= 2);
1333 if Container.Busy > 0 then
1334 raise Program_Error with
1335 "attempt to tamper with elements (list is busy)";
1336 end if;
1338 if Before.Node = null then
1339 pragma Assert (Position.Node /= Container.Last);
1341 if Position.Node = Container.First then
1342 Container.First := Position.Node.Next;
1343 Container.First.Prev := null;
1344 else
1345 Position.Node.Prev.Next := Position.Node.Next;
1346 Position.Node.Next.Prev := Position.Node.Prev;
1347 end if;
1349 Container.Last.Next := Position.Node;
1350 Position.Node.Prev := Container.Last;
1352 Container.Last := Position.Node;
1353 Container.Last.Next := null;
1355 return;
1356 end if;
1358 if Before.Node = Container.First then
1359 pragma Assert (Position.Node /= Container.First);
1361 if Position.Node = Container.Last then
1362 Container.Last := Position.Node.Prev;
1363 Container.Last.Next := null;
1364 else
1365 Position.Node.Prev.Next := Position.Node.Next;
1366 Position.Node.Next.Prev := Position.Node.Prev;
1367 end if;
1369 Container.First.Prev := Position.Node;
1370 Position.Node.Next := Container.First;
1372 Container.First := Position.Node;
1373 Container.First.Prev := null;
1375 return;
1376 end if;
1378 if Position.Node = Container.First then
1379 Container.First := Position.Node.Next;
1380 Container.First.Prev := null;
1382 elsif Position.Node = Container.Last then
1383 Container.Last := Position.Node.Prev;
1384 Container.Last.Next := null;
1386 else
1387 Position.Node.Prev.Next := Position.Node.Next;
1388 Position.Node.Next.Prev := Position.Node.Prev;
1389 end if;
1391 Before.Node.Prev.Next := Position.Node;
1392 Position.Node.Prev := Before.Node.Prev;
1394 Before.Node.Prev := Position.Node;
1395 Position.Node.Next := Before.Node;
1397 pragma Assert (Container.First.Prev = null);
1398 pragma Assert (Container.Last.Next = null);
1399 end Splice;
1401 procedure Splice
1402 (Target : in out List;
1403 Before : Cursor;
1404 Source : in out List;
1405 Position : in out Cursor)
1407 begin
1408 if Target'Address = Source'Address then
1409 Splice (Target, Before, Position);
1410 return;
1411 end if;
1413 if Before.Container /= null then
1414 if Before.Container /= Target'Unrestricted_Access then
1415 raise Program_Error with
1416 "Before cursor designates wrong container";
1417 end if;
1419 pragma Assert (Vet (Before), "bad Before cursor in Splice");
1420 end if;
1422 if Position.Node = null then
1423 raise Constraint_Error with "Position cursor has no element";
1424 end if;
1426 if Position.Container /= Source'Unrestricted_Access then
1427 raise Program_Error with
1428 "Position cursor designates wrong container";
1429 end if;
1431 pragma Assert (Vet (Position), "bad Position cursor in Splice");
1433 if Target.Length = Count_Type'Last then
1434 raise Constraint_Error with "Target is full";
1435 end if;
1437 if Target.Busy > 0 then
1438 raise Program_Error with
1439 "attempt to tamper with elements of Target (list is busy)";
1440 end if;
1442 if Source.Busy > 0 then
1443 raise Program_Error with
1444 "attempt to tamper with elements of Source (list is busy)";
1445 end if;
1447 if Position.Node = Source.First then
1448 Source.First := Position.Node.Next;
1450 if Position.Node = Source.Last then
1451 pragma Assert (Source.First = null);
1452 pragma Assert (Source.Length = 1);
1453 Source.Last := null;
1455 else
1456 Source.First.Prev := null;
1457 end if;
1459 elsif Position.Node = Source.Last then
1460 pragma Assert (Source.Length >= 2);
1461 Source.Last := Position.Node.Prev;
1462 Source.Last.Next := null;
1464 else
1465 pragma Assert (Source.Length >= 3);
1466 Position.Node.Prev.Next := Position.Node.Next;
1467 Position.Node.Next.Prev := Position.Node.Prev;
1468 end if;
1470 if Target.Length = 0 then
1471 pragma Assert (Target.First = null);
1472 pragma Assert (Target.Last = null);
1473 pragma Assert (Before = No_Element);
1475 Target.First := Position.Node;
1476 Target.Last := Position.Node;
1478 Target.First.Prev := null;
1479 Target.Last.Next := null;
1481 elsif Before.Node = null then
1482 pragma Assert (Target.Last.Next = null);
1483 Target.Last.Next := Position.Node;
1484 Position.Node.Prev := Target.Last;
1486 Target.Last := Position.Node;
1487 Target.Last.Next := null;
1489 elsif Before.Node = Target.First then
1490 pragma Assert (Target.First.Prev = null);
1491 Target.First.Prev := Position.Node;
1492 Position.Node.Next := Target.First;
1494 Target.First := Position.Node;
1495 Target.First.Prev := null;
1497 else
1498 pragma Assert (Target.Length >= 2);
1499 Before.Node.Prev.Next := Position.Node;
1500 Position.Node.Prev := Before.Node.Prev;
1502 Before.Node.Prev := Position.Node;
1503 Position.Node.Next := Before.Node;
1504 end if;
1506 Target.Length := Target.Length + 1;
1507 Source.Length := Source.Length - 1;
1509 Position.Container := Target'Unchecked_Access;
1510 end Splice;
1512 ----------
1513 -- Swap --
1514 ----------
1516 procedure Swap
1517 (Container : in out List;
1518 I, J : Cursor)
1520 begin
1521 if I.Node = null then
1522 raise Constraint_Error with "I cursor has no element";
1523 end if;
1525 if J.Node = null then
1526 raise Constraint_Error with "J cursor has no element";
1527 end if;
1529 if I.Container /= Container'Unchecked_Access then
1530 raise Program_Error with "I cursor designates wrong container";
1531 end if;
1533 if J.Container /= Container'Unchecked_Access then
1534 raise Program_Error with "J cursor designates wrong container";
1535 end if;
1537 if I.Node = J.Node then
1538 return;
1539 end if;
1541 if Container.Lock > 0 then
1542 raise Program_Error with
1543 "attempt to tamper with cursors (list is locked)";
1544 end if;
1546 pragma Assert (Vet (I), "bad I cursor in Swap");
1547 pragma Assert (Vet (J), "bad J cursor in Swap");
1549 declare
1550 EI : Element_Type renames I.Node.Element;
1551 EJ : Element_Type renames J.Node.Element;
1553 EI_Copy : constant Element_Type := EI;
1555 begin
1556 EI := EJ;
1557 EJ := EI_Copy;
1558 end;
1559 end Swap;
1561 ----------------
1562 -- Swap_Links --
1563 ----------------
1565 procedure Swap_Links
1566 (Container : in out List;
1567 I, J : Cursor)
1569 begin
1570 if I.Node = null then
1571 raise Constraint_Error with "I cursor has no element";
1572 end if;
1574 if J.Node = null then
1575 raise Constraint_Error with "J cursor has no element";
1576 end if;
1578 if I.Container /= Container'Unrestricted_Access then
1579 raise Program_Error with "I cursor designates wrong container";
1580 end if;
1582 if J.Container /= Container'Unrestricted_Access then
1583 raise Program_Error with "J cursor designates wrong container";
1584 end if;
1586 if I.Node = J.Node then
1587 return;
1588 end if;
1590 if Container.Busy > 0 then
1591 raise Program_Error with
1592 "attempt to tamper with elements (list is busy)";
1593 end if;
1595 pragma Assert (Vet (I), "bad I cursor in Swap_Links");
1596 pragma Assert (Vet (J), "bad J cursor in Swap_Links");
1598 declare
1599 I_Next : constant Cursor := Next (I);
1601 begin
1602 if I_Next = J then
1603 Splice (Container, Before => I, Position => J);
1605 else
1606 declare
1607 J_Next : constant Cursor := Next (J);
1609 begin
1610 if J_Next = I then
1611 Splice (Container, Before => J, Position => I);
1613 else
1614 pragma Assert (Container.Length >= 3);
1616 Splice (Container, Before => I_Next, Position => J);
1617 Splice (Container, Before => J_Next, Position => I);
1618 end if;
1619 end;
1620 end if;
1621 end;
1622 end Swap_Links;
1624 --------------------
1625 -- Update_Element --
1626 --------------------
1628 procedure Update_Element
1629 (Container : in out List;
1630 Position : Cursor;
1631 Process : not null access procedure (Element : in out Element_Type))
1633 begin
1634 if Position.Node = null then
1635 raise Constraint_Error with "Position cursor has no element";
1636 end if;
1638 if Position.Container /= Container'Unchecked_Access then
1639 raise Program_Error with
1640 "Position cursor designates wrong container";
1641 end if;
1643 pragma Assert (Vet (Position), "bad cursor in Update_Element");
1645 declare
1646 B : Natural renames Container.Busy;
1647 L : Natural renames Container.Lock;
1649 begin
1650 B := B + 1;
1651 L := L + 1;
1653 begin
1654 Process (Position.Node.Element);
1655 exception
1656 when others =>
1657 L := L - 1;
1658 B := B - 1;
1659 raise;
1660 end;
1662 L := L - 1;
1663 B := B - 1;
1664 end;
1665 end Update_Element;
1667 ---------
1668 -- Vet --
1669 ---------
1671 function Vet (Position : Cursor) return Boolean is
1672 begin
1673 if Position.Node = null then
1674 return Position.Container = null;
1675 end if;
1677 if Position.Container = null then
1678 return False;
1679 end if;
1681 if Position.Node.Next = Position.Node then
1682 return False;
1683 end if;
1685 if Position.Node.Prev = Position.Node then
1686 return False;
1687 end if;
1689 declare
1690 L : List renames Position.Container.all;
1691 begin
1692 if L.Length = 0 then
1693 return False;
1694 end if;
1696 if L.First = null then
1697 return False;
1698 end if;
1700 if L.Last = null then
1701 return False;
1702 end if;
1704 if L.First.Prev /= null then
1705 return False;
1706 end if;
1708 if L.Last.Next /= null then
1709 return False;
1710 end if;
1712 if Position.Node.Prev = null
1713 and then Position.Node /= L.First
1714 then
1715 return False;
1716 end if;
1718 if Position.Node.Next = null
1719 and then Position.Node /= L.Last
1720 then
1721 return False;
1722 end if;
1724 if L.Length = 1 then
1725 return L.First = L.Last;
1726 end if;
1728 if L.First = L.Last then
1729 return False;
1730 end if;
1732 if L.First.Next = null then
1733 return False;
1734 end if;
1736 if L.Last.Prev = null then
1737 return False;
1738 end if;
1740 if L.First.Next.Prev /= L.First then
1741 return False;
1742 end if;
1744 if L.Last.Prev.Next /= L.Last then
1745 return False;
1746 end if;
1748 if L.Length = 2 then
1749 if L.First.Next /= L.Last then
1750 return False;
1751 end if;
1753 if L.Last.Prev /= L.First then
1754 return False;
1755 end if;
1757 return True;
1758 end if;
1760 if L.First.Next = L.Last then
1761 return False;
1762 end if;
1764 if L.Last.Prev = L.First then
1765 return False;
1766 end if;
1768 if Position.Node = L.First then
1769 return True;
1770 end if;
1772 if Position.Node = L.Last then
1773 return True;
1774 end if;
1776 if Position.Node.Next = null then
1777 return False;
1778 end if;
1780 if Position.Node.Prev = null then
1781 return False;
1782 end if;
1784 if Position.Node.Next.Prev /= Position.Node then
1785 return False;
1786 end if;
1788 if Position.Node.Prev.Next /= Position.Node then
1789 return False;
1790 end if;
1792 if L.Length = 3 then
1793 if L.First.Next /= Position.Node then
1794 return False;
1795 end if;
1797 if L.Last.Prev /= Position.Node then
1798 return False;
1799 end if;
1800 end if;
1802 return True;
1803 end;
1804 end Vet;
1806 -----------
1807 -- Write --
1808 -----------
1810 procedure Write
1811 (Stream : not null access Root_Stream_Type'Class;
1812 Item : List)
1814 Node : Node_Access := Item.First;
1816 begin
1817 Count_Type'Base'Write (Stream, Item.Length);
1819 while Node /= null loop
1820 Element_Type'Write (Stream, Node.Element);
1821 Node := Node.Next;
1822 end loop;
1823 end Write;
1825 procedure Write
1826 (Stream : not null access Root_Stream_Type'Class;
1827 Item : Cursor)
1829 begin
1830 raise Program_Error with "attempt to stream list cursor";
1831 end Write;
1833 end Ada.Containers.Doubly_Linked_Lists;