1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347 | -- Abstract :
--
-- see spec
--
-- Copyright (C) 2017 - 2022 Free Software Foundation, Inc.
--
-- This library is free software; you can redistribute it and/or
-- modify it under terms of the GNU General Public License as
-- published by the Free Software Foundation; either version 3, or (at
-- your option) any later version. This library is distributed in the
-- hope that it will be useful, but WITHOUT ANY WARRANTY; without even
-- the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
-- PURPOSE. See the GNU General Public License for more details. You
-- should have received a copy of the GNU General Public License
-- distributed with this program; see file COPYING. If not, write to
-- the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
-- MA 02111-1307, USA.
--
-- As a special exception under Section 7 of GPL version 3, you are granted
-- additional permissions described in the GCC Runtime Library Exception,
-- version 3.1, as published by the Free Software Foundation.
pragma License (Modified_GPL);
package body SAL.Gen_Definite_Doubly_Linked_Lists_Ref_Count is
---------
-- Public operations, declaration order.
overriding
procedure Adjust (Container : in out List)
is
Next_Source : Node_Access := Container.Head;
New_Node : Node_Access;
begin
if Next_Source = null then
return;
end if;
Container.Tail := null;
loop
New_Node := new Node_Type'
(Element => Next_Source.Element,
Next => null,
Prev => Container.Tail,
Ref_Count => 0);
if Container.Tail = null then
Container.Head := New_Node;
Container.Tail := New_Node;
else
Container.Tail.Next := New_Node;
Container.Tail := New_Node;
end if;
Next_Source := Next_Source.Next;
exit when Next_Source = null;
end loop;
end Adjust;
overriding
procedure Finalize (Container : in out List)
is
Next : Node_Access := Container.Head;
begin
loop
exit when Next = null;
Next := Container.Head.Next;
-- We raise an exception here, even though Finalize never should,
-- because Finalize is also renamed to Clear, and called as a
-- normal procedure.
if Container.Enable_Checks and Container.Head.Ref_Count /= 0 then
raise Invalid_Operation with "ref_count " & Container.Head.Ref_Count'Image;
end if;
Free (Container.Head);
Container.Head := Next;
end loop;
Container.Tail := null;
Container.Count := 0;
end Finalize;
procedure Enable_Ref_Count_Check (Container : in out List; Enable : in Boolean)
is begin
Container.Enable_Checks := Enable;
end Enable_Ref_Count_Check;
procedure Check_Ref_Counts (Container : in out List)
is
Next : Node_Access := Container.Head;
begin
loop
exit when Next = null;
if Container.Enable_Checks and Next.Ref_Count /= 0 then
raise Invalid_Operation with "ref_count " & Next.Ref_Count'Image;
end if;
Next := Next.Next;
end loop;
end Check_Ref_Counts;
function Length (Container : in List) return Ada.Containers.Count_Type
is begin
return Container.Count;
end Length;
procedure Append (Container : in out List; Element : in Element_Type)
is
use all type Ada.Containers.Count_Type;
New_Node : constant Node_Access := new Node_Type'
(Element => Element,
Prev => Container.Tail,
Next => null,
Ref_Count => 0);
begin
if Container.Tail = null then
Container.Head := New_Node;
Container.Tail := New_Node;
else
Container.Tail.Next := New_Node;
Container.Tail := New_Node;
end if;
Container.Count := Container.Count + 1;
end Append;
procedure Prepend (Container : in out List; Element : in Element_Type)
is
use all type Ada.Containers.Count_Type;
New_Node : constant Node_Access := new Node_Type'
(Element => Element,
Prev => null,
Next => Container.Head,
Ref_Count => 0);
begin
if Container.Tail = null then
Container.Head := New_Node;
Container.Tail := New_Node;
else
Container.Head.Prev := New_Node;
Container.Head := New_Node;
end if;
Container.Count := Container.Count + 1;
end Prepend;
function To_List (Element : in Element_Type) return List
is begin
return Result : List do
Result.Append (Element);
end return;
end To_List;
function Has_Element (Position : in Cursor) return Boolean
is begin
return Position.Ptr /= null;
end Has_Element;
function First (Container : in List'Class) return Cursor
is begin
if Container.Head = null then
return (Ada.Finalization.Controlled with Ptr => null);
else
Container.Head.Ref_Count := @ + 1;
return (Ada.Finalization.Controlled with Ptr => Container.Head);
end if;
end First;
function Last (Container : in List'Class) return Cursor
is begin
if Container.Tail = null then
return (Ada.Finalization.Controlled with Ptr => null);
else
Container.Tail.Ref_Count := @ + 1;
return (Ada.Finalization.Controlled with Ptr => Container.Tail);
end if;
end Last;
procedure Next (Position : in out Cursor)
is begin
Position.Ptr.Ref_Count := @ - 1;
if Position.Ptr.Next /= null then
Position.Ptr.Next.Ref_Count := @ + 1;
end if;
Position.Ptr := Position.Ptr.Next;
end Next;
function Next (Position : in Cursor) return Cursor
is begin
if Position.Ptr.Next /= null then
Position.Ptr.Next.Ref_Count := @ + 1;
end if;
return (Ada.Finalization.Controlled with Ptr => Position.Ptr.Next);
end Next;
procedure Previous (Position : in out Cursor)
is begin
Position.Ptr.Ref_Count := @ - 1;
if Position.Ptr.Prev /= null then
Position.Ptr.Prev.Ref_Count := @ + 1;
end if;
Position.Ptr := Position.Ptr.Prev;
end Previous;
function Previous (Position : in Cursor) return Cursor
is begin
return Result : constant Cursor := (Ada.Finalization.Controlled with Ptr => Position.Ptr.Prev) do
if Result.Ptr /= null then
Result.Ptr.Ref_Count := @ + 1;
end if;
end return;
end Previous;
function Element (Position : in Cursor) return Element_Type
is begin
return Position.Ptr.Element;
end Element;
procedure Delete (Container : in out List; Position : in out Cursor'Class)
is
use all type Ada.Containers.Count_Type;
Node : Node_Access renames Position.Ptr;
begin
if Container.Enable_Checks and Node.Ref_Count /= 1 then
raise Invalid_Operation with "ref_count " & Node.Ref_Count'Image;
end if;
if Node.Next = null then
Container.Tail := Node.Prev;
else
Node.Next.Prev := Node.Prev;
end if;
if Node.Prev = null then
Container.Head := Node.Next;
else
Node.Prev.Next := Node.Next;
end if;
Free (Node);
Container.Count := Container.Count - 1;
end Delete;
procedure Replace_Element
(Position : in Cursor'Class;
New_Element : in Element_Type)
is begin
Position.Ptr.Element := New_Element;
end Replace_Element;
function Append (Container : in out List'Class; Element : in Element_Type) return Cursor
is begin
Append (Container, Element);
Container.Tail.Ref_Count := @ + 1;
return (Ada.Finalization.Controlled with Ptr => Container.Tail);
end Append;
function Insert
(Container : in out List'Class;
Before : in Cursor'Class;
Element : in Element_Type)
return Cursor
is
use all type Ada.Containers.Count_Type;
begin
if Before.Ptr = null then
return Container.Append (Element);
else
return Result : Cursor do
if Before.Ptr = Container.Head then
declare
-- old list: before ...
-- newlist: new before ...
New_Node : constant Node_Access := new Node_Type'
(Element => Element,
Prev => null,
Next => Before.Ptr,
Ref_Count => 1);
begin
Before.Ptr.Prev := New_Node;
Container.Head := New_Node;
Result.Ptr := New_Node;
end;
else
declare
-- old list: ... prev before ...
-- newlist: ... prev new before ...
New_Node : constant Node_Access := new Node_Type'
(Element => Element,
Prev => Before.Ptr.Prev,
Next => Before.Ptr,
Ref_Count => 1);
begin
Before.Ptr.Prev.Next := New_Node;
Before.Ptr.Prev := New_Node;
Result.Ptr := New_Node;
end;
end if;
Container.Count := Container.Count + 1;
end return;
end if;
end Insert;
procedure Insert
(Container : in out List;
Before : in Cursor'Class;
Element : in Element_Type)
is
Junk : Cursor := Insert (Container, Before, Element);
pragma Unreferenced (Junk);
begin
null;
end Insert;
function Contains
(Container : in List;
Item : in Cursor'Class)
return Boolean
is
Node : Node_Access := Container.Head;
begin
loop
exit when Node = null;
if Node = Item.Ptr then
return True;
end if;
Node := Node.Next;
end loop;
return False;
end Contains;
----------
-- Private operations, declaration order
overriding procedure Finalize (Object : in out Cursor)
is begin
if Object.Ptr /= null then
Object.Ptr.Ref_Count := @ - 1;
end if;
end Finalize;
overriding procedure Adjust (Object : in out Cursor)
is begin
if Object.Ptr /= null then
Object.Ptr.Ref_Count := @ + 1;
end if;
end Adjust;
end SAL.Gen_Definite_Doubly_Linked_Lists_Ref_Count;
|