Debian Codegraph @

gcc/ada/libgnat/g-lists.ads

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- G N A T . L I S T S --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 2018-2025, 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 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. --
17 -- --
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. --
21 -- --
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/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 -- Note: this unit is used during bootstrap, see ADA_GENERATED_FILES in
33 -- gcc-interface/Make-lang.in for details on the constraints.
34
35 package GNAT.Lists is
36
37 ------------------------
38 -- Doubly_Linked_List --
39 ------------------------
40
41 -- The following package offers a doubly linked list abstraction with the
42 -- following characteristics:
43 --
44 -- * Creation of multiple instances, of different sizes
45 -- * Iterable elements
46 --
47 -- The following use pattern must be employed with this list:
48 --
49 -- List : Doubly_Linked_List := Create;
50 --
51 -- <various operations>
52 --
53 -- Destroy (List);
54 --
55 -- The destruction of the list reclaims all storage occupied by it.
56
57 generic
58 type Element_Type is private;
59
60 with function "="
61 (Left : Element_Type;
62 Right : Element_Type) return Boolean;
63
64 with procedure Destroy_Element (Elem : in out Element_Type);
65 -- Element destructor
66
67 Check_Tampering : Boolean := True;
68
69 package Doubly_Linked_Lists is
70
71 ---------------------
72 -- List operations --
73 ---------------------
74
75 type Doubly_Linked_List is private;
76 Nil : constant Doubly_Linked_List;
77
78 -- The following exception is raised when the list is empty, and an
79 -- attempt is made to delete an element from it.
80
81 List_Empty : exception;
82
83 procedure Append
84 (L : Doubly_Linked_List;
85 Elem : Element_Type);
86 -- Insert element Elem at the end of list L. This action will raise
87 -- Iterated if the list has outstanding iterators.
88
89 function Contains
90 (L : Doubly_Linked_List;
91 Elem : Element_Type) return Boolean;
92 -- Determine whether list L contains element Elem
93
94 function Create return Doubly_Linked_List;
95 -- Create a new list
96
97 procedure Delete
98 (L : Doubly_Linked_List;
99 Elem : Element_Type);
100 -- Delete element Elem from list L. The routine has no effect if Elem is
101 -- not present. This action will raise
102 --
103 -- * List_Empty if the list is empty.
104 -- * Iterated if the list has outstanding iterators.
105
106 procedure Delete_First (L : Doubly_Linked_List);
107 -- Delete an element from the start of list L. This action will raise
108 --
109 -- * List_Empty if the list is empty.
110 -- * Iterated if the list has outstanding iterators.
111
112 procedure Delete_Last (L : Doubly_Linked_List);
113 -- Delete an element from the end of list L. This action will raise
114 --
115 -- * List_Empty if the list is empty.
116 -- * Iterated if the list has outstanding iterators.
117
118 procedure Destroy (L : in out Doubly_Linked_List);
119 -- Destroy the contents of list L. This routine must be called at the
120 -- end of a list's lifetime. This action will raise Iterated if the
121 -- list has outstanding iterators.
122
123 function Equal
124 (Left : Doubly_Linked_List;
125 Right : Doubly_Linked_List) return Boolean;
126 -- Determine whether lists Left and Right have the same characteristics
127 -- and contain the same elements.
128
129 function First (L : Doubly_Linked_List) return Element_Type;
130 -- Obtain an element from the start of list L. This action will raise
131 -- List_Empty if the list is empty.
132
133 procedure Insert_After
134 (L : Doubly_Linked_List;
135 After : Element_Type;
136 Elem : Element_Type);
137 -- Insert new element Elem after element After in list L. The routine
138 -- has no effect if After is not present. This action will raise
139 -- Iterated if the list has outstanding iterators.
140
141 procedure Insert_Before
142 (L : Doubly_Linked_List;
143 Before : Element_Type;
144 Elem : Element_Type);
145 -- Insert new element Elem before element Before in list L. The routine
146 -- has no effect if After is not present. This action will raise
147 -- Iterated if the list has outstanding iterators.
148
149 function Is_Empty (L : Doubly_Linked_List) return Boolean;
150 -- Determine whether list L is empty
151
152 function Last (L : Doubly_Linked_List) return Element_Type;
153 -- Obtain an element from the end of list L. This action will raise
154 -- List_Empty if the list is empty.
155
156 procedure Prepend
157 (L : Doubly_Linked_List;
158 Elem : Element_Type);
159 -- Insert element Elem at the start of list L. This action will raise
160 -- Iterated if the list has outstanding iterators.
161
162 function Present (L : Doubly_Linked_List) return Boolean;
163 -- Determine whether list L exists
164
165 procedure Replace
166 (L : Doubly_Linked_List;
167 Old_Elem : Element_Type;
168 New_Elem : Element_Type);
169 -- Replace old element Old_Elem with new element New_Elem in list L. The
170 -- routine has no effect if Old_Elem is not present. This action will
171 -- raise Iterated if the list has outstanding iterators.
172
173 function Size (L : Doubly_Linked_List) return Natural;
174 -- Obtain the number of elements in list L
175
176 -------------------------
177 -- Iterator operations --
178 -------------------------
179
180 -- The following type represents an element iterator. An iterator locks
181 -- all mutation operations, and ulocks them once it is exhausted. The
182 -- iterator must be used with the following pattern:
183 --
184 -- Iter := Iterate (My_List);
185 -- while Has_Next (Iter) loop
186 -- Next (Iter, Element);
187 -- end loop;
188 --
189 -- It is possible to advance the iterator by using Next only, however
190 -- this risks raising Iterator_Exhausted.
191
192 type Iterator is private;
193
194 function Has_Next (Iter : Iterator) return Boolean;
195 -- Determine whether iterator Iter has more elements to examine. If the
196 -- iterator has been exhausted, restore all mutation functionality of
197 -- the associated list.
198
199 function Iterate (L : Doubly_Linked_List) return Iterator;
200 -- Obtain an iterator over the elements of list L. This action locks all
201 -- mutation functionality of the associated list.
202
203 procedure Next
204 (Iter : in out Iterator;
205 Elem : out Element_Type);
206 -- Return the current element referenced by iterator Iter and advance
207 -- to the next available element. If the iterator has been exhausted
208 -- and further attempts are made to advance it, this routine restores
209 -- mutation functionality of the associated list, and then raises
210 -- Iterator_Exhausted.
211
212 private
213 -- The following type represents a list node
214
215 type Node;
216 type Node_Ptr is access all Node;
217 type Node is record
218 Elem : Element_Type;
219
220 Next : Node_Ptr := null;
221 Prev : Node_Ptr := null;
222 end record;
223
224 -- The following type represents a list
225
226 type Doubly_Linked_List_Attributes is record
227 Elements : Natural := 0;
228 -- The number of elements in the list
229
230 Iterators : Natural := 0;
231 -- Number of outstanding iterators
232
233 Nodes : aliased Node;
234 -- The dummy head of the list
235 end record;
236
237 type Doubly_Linked_List is access all Doubly_Linked_List_Attributes;
238 Nil : constant Doubly_Linked_List := null;
239
240 -- The following type represents an element iterator
241
242 type Iterator is record
243 Curr_Nod : Node_Ptr := null;
244 -- Reference to the current node being examined. The invariant of the
245 -- iterator requires that this field always points to a valid node. A
246 -- value of null indicates that the iterator is exhausted.
247
248 List : Doubly_Linked_List := null;
249 -- Reference to the associated list
250 end record;
251 end Doubly_Linked_Lists;
252
253 end GNAT.Lists;