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-2015, 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 3, 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. --
22 -- As a special exception under Section 7 of GPL version 3, you are granted --
23 -- additional permissions described in the GCC Runtime Library Exception, --
24 -- version 3.1, as published by the Free Software Foundation. --
26 -- You should have received a copy of the GNU General Public License and --
27 -- a copy of the GCC Runtime Library Exception along with this program; --
28 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
29 -- <http://www.gnu.org/licenses/>. --
31 -- This unit was originally developed by Matthew J Heaney. --
32 ------------------------------------------------------------------------------
34 with Ada
.Iterator_Interfaces
;
36 with Ada
.Containers
.Helpers
;
37 private with Ada
.Finalization
;
38 private with Ada
.Streams
;
41 type Element_Type
is private;
43 with function "=" (Left
, Right
: Element_Type
)
46 package Ada
.Containers
.Doubly_Linked_Lists
is
47 pragma Annotate
(CodePeer
, Skip_Analysis
);
51 type List
is tagged private
53 Constant_Indexing
=> Constant_Reference
,
54 Variable_Indexing
=> Reference
,
55 Default_Iterator
=> Iterate
,
56 Iterator_Element
=> Element_Type
;
58 pragma Preelaborable_Initialization
(List
);
60 type Cursor
is private;
61 pragma Preelaborable_Initialization
(Cursor
);
63 Empty_List
: constant List
;
65 No_Element
: constant Cursor
;
67 function Has_Element
(Position
: Cursor
) return Boolean;
69 package List_Iterator_Interfaces
is new
70 Ada
.Iterator_Interfaces
(Cursor
, Has_Element
);
72 function "=" (Left
, Right
: List
) return Boolean;
74 function Length
(Container
: List
) return Count_Type
;
76 function Is_Empty
(Container
: List
) return Boolean;
78 procedure Clear
(Container
: in out List
);
80 function Element
(Position
: Cursor
) return Element_Type
;
82 procedure Replace_Element
83 (Container
: in out List
;
85 New_Item
: Element_Type
);
87 procedure Query_Element
89 Process
: not null access procedure (Element
: Element_Type
));
91 procedure Update_Element
92 (Container
: in out List
;
94 Process
: not null access procedure (Element
: in out Element_Type
));
96 type Constant_Reference_Type
97 (Element
: not null access constant Element_Type
) is private
99 Implicit_Dereference
=> Element
;
102 (Element
: not null access Element_Type
) is private
104 Implicit_Dereference
=> Element
;
106 function Constant_Reference
107 (Container
: aliased List
;
108 Position
: Cursor
) return Constant_Reference_Type
;
109 pragma Inline
(Constant_Reference
);
112 (Container
: aliased in out List
;
113 Position
: Cursor
) return Reference_Type
;
114 pragma Inline
(Reference
);
116 procedure Assign
(Target
: in out List
; Source
: List
);
118 function Copy
(Source
: List
) return List
;
121 (Target
: in out List
;
122 Source
: in out List
);
125 (Container
: in out List
;
127 New_Item
: Element_Type
;
128 Count
: Count_Type
:= 1);
131 (Container
: in out List
;
133 New_Item
: Element_Type
;
134 Position
: out Cursor
;
135 Count
: Count_Type
:= 1);
138 (Container
: in out List
;
140 Position
: out Cursor
;
141 Count
: Count_Type
:= 1);
144 (Container
: in out List
;
145 New_Item
: Element_Type
;
146 Count
: Count_Type
:= 1);
149 (Container
: in out List
;
150 New_Item
: Element_Type
;
151 Count
: Count_Type
:= 1);
154 (Container
: in out List
;
155 Position
: in out Cursor
;
156 Count
: Count_Type
:= 1);
158 procedure Delete_First
159 (Container
: in out List
;
160 Count
: Count_Type
:= 1);
162 procedure Delete_Last
163 (Container
: in out List
;
164 Count
: Count_Type
:= 1);
166 procedure Reverse_Elements
(Container
: in out List
);
168 function Iterate
(Container
: List
)
169 return List_Iterator_Interfaces
.Reversible_Iterator
'Class;
171 function Iterate
(Container
: List
; Start
: Cursor
)
172 return List_Iterator_Interfaces
.Reversible_Iterator
'Class;
175 (Container
: in out List
;
179 (Container
: in out List
;
183 (Target
: in out List
;
185 Source
: in out List
);
188 (Target
: in out List
;
190 Source
: in out List
;
191 Position
: in out Cursor
);
194 (Container
: in out List
;
198 function First
(Container
: List
) return Cursor
;
200 function First_Element
(Container
: List
) return Element_Type
;
202 function Last
(Container
: List
) return Cursor
;
204 function Last_Element
(Container
: List
) return Element_Type
;
206 function Next
(Position
: Cursor
) return Cursor
;
208 procedure Next
(Position
: in out Cursor
);
210 function Previous
(Position
: Cursor
) return Cursor
;
212 procedure Previous
(Position
: in out Cursor
);
217 Position
: Cursor
:= No_Element
) return Cursor
;
219 function Reverse_Find
222 Position
: Cursor
:= No_Element
) return Cursor
;
226 Item
: Element_Type
) return Boolean;
230 Process
: not null access procedure (Position
: Cursor
));
232 procedure Reverse_Iterate
234 Process
: not null access procedure (Position
: Cursor
));
237 with function "<" (Left
, Right
: Element_Type
) return Boolean is <>;
238 package Generic_Sorting
is
240 function Is_Sorted
(Container
: List
) return Boolean;
242 procedure Sort
(Container
: in out List
);
244 procedure Merge
(Target
, Source
: in out List
);
250 pragma Inline
(Next
);
251 pragma Inline
(Previous
);
253 use Ada
.Containers
.Helpers
;
254 package Implementation
is new Generic_Implementation
;
258 type Node_Access
is access Node_Type
;
262 Element
: aliased Element_Type
;
267 use Ada
.Finalization
;
271 new Controlled
with record
272 First
: Node_Access
:= null;
273 Last
: Node_Access
:= null;
274 Length
: Count_Type
:= 0;
275 TC
: aliased Tamper_Counts
;
278 overriding
procedure Adjust
(Container
: in out List
);
280 overriding
procedure Finalize
(Container
: in out List
) renames Clear
;
283 (Stream
: not null access Root_Stream_Type
'Class;
286 for List
'Read use Read
;
289 (Stream
: not null access Root_Stream_Type
'Class;
292 for List
'Write use Write
;
294 type List_Access
is access all List
;
295 for List_Access
'Storage_Size use 0;
299 Container
: List_Access
;
304 (Stream
: not null access Root_Stream_Type
'Class;
307 for Cursor
'Read use Read
;
310 (Stream
: not null access Root_Stream_Type
'Class;
313 for Cursor
'Write use Write
;
315 subtype Reference_Control_Type
is Implementation
.Reference_Control_Type
;
316 -- It is necessary to rename this here, so that the compiler can find it
318 type Constant_Reference_Type
319 (Element
: not null access constant Element_Type
) is
321 Control
: Reference_Control_Type
:=
322 raise Program_Error
with "uninitialized reference";
323 -- The RM says, "The default initialization of an object of
324 -- type Constant_Reference_Type or Reference_Type propagates
329 (Stream
: not null access Root_Stream_Type
'Class;
330 Item
: Constant_Reference_Type
);
332 for Constant_Reference_Type
'Write use Write
;
335 (Stream
: not null access Root_Stream_Type
'Class;
336 Item
: out Constant_Reference_Type
);
338 for Constant_Reference_Type
'Read use Read
;
341 (Element
: not null access Element_Type
) is
343 Control
: Reference_Control_Type
:=
344 raise Program_Error
with "uninitialized reference";
345 -- The RM says, "The default initialization of an object of
346 -- type Constant_Reference_Type or Reference_Type propagates
351 (Stream
: not null access Root_Stream_Type
'Class;
352 Item
: Reference_Type
);
354 for Reference_Type
'Write use Write
;
357 (Stream
: not null access Root_Stream_Type
'Class;
358 Item
: out Reference_Type
);
360 for Reference_Type
'Read use Read
;
362 -- Three operations are used to optimize in the expansion of "for ... of"
363 -- loops: the Next(Cursor) procedure in the visible part, and the following
364 -- Pseudo_Reference and Get_Element_Access functions. See Sem_Ch5 for
367 function Pseudo_Reference
368 (Container
: aliased List
'Class) return Reference_Control_Type
;
369 pragma Inline
(Pseudo_Reference
);
370 -- Creates an object of type Reference_Control_Type pointing to the
371 -- container, and increments the Lock. Finalization of this object will
372 -- decrement the Lock.
374 type Element_Access
is access all Element_Type
with
377 function Get_Element_Access
378 (Position
: Cursor
) return not null Element_Access
;
379 -- Returns a pointer to the element designated by Position.
381 Empty_List
: constant List
:= (Controlled
with others => <>);
383 No_Element
: constant Cursor
:= Cursor
'(null, null);
385 type Iterator is new Limited_Controlled and
386 List_Iterator_Interfaces.Reversible_Iterator with
388 Container : List_Access;
391 with Disable_Controlled => not T_Check;
393 overriding procedure Finalize (Object : in out Iterator);
395 overriding function First (Object : Iterator) return Cursor;
396 overriding function Last (Object : Iterator) return Cursor;
398 overriding function Next
400 Position : Cursor) return Cursor;
402 overriding function Previous
404 Position : Cursor) return Cursor;
406 end Ada.Containers.Doubly_Linked_Lists;