1
-------------------------------------------------------------------------------
3
-- <STRONG>Copyright (c) 2001-2003 by Thomas Wolf.</STRONG>
5
-- This piece of software is free software; you can redistribute it and/or
6
-- modify it under the terms of the GNU General Public License as published
7
-- by the Free Software Foundation; either version 2, or (at your option)
8
-- any later version. This unit is distributed in the hope that it will be
9
-- useful, but <EM>without any warranty</EM>; without even the implied
10
-- warranty of <EM>merchantability or fitness for a particular purpose.</EM>
11
-- See the GNU General Public License for more details. You should have
12
-- received a copy of the GNU General Public License with this distribution,
13
-- see file "<A HREF="GPL.txt">GPL.txt</A>". If not, write to the Free
14
-- Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18
-- As a special exception from the GPL, if other files instantiate generics
19
-- from this unit, or you link this unit with other files to produce an
20
-- executable, this unit does not by itself cause the resulting executable
21
-- to be covered by the GPL. This exception does not however invalidate any
22
-- other reasons why the executable file might be covered by the GPL.
26
-- Author:</STRONG><DD>
28
-- <ADDRESS><A HREF="mailto:twolf@acm.org">twolf@acm.org</A></ADDRESS></DL>
31
-- Purpose:</STRONG><DD>
32
-- Simple dynamic container, implemented as a very simple singly linked list.
33
-- Only operations are adding items, sorting, and finally traversing. It's
34
-- truly simple, but captures a very common case.
38
-- Tasking semantics:</STRONG><DD>
39
-- N/A. Not abortion-safe.</DL>
42
-- Storage semantics:</STRONG><DD>
43
-- Dynamic storage allocation in a user-supplied storage pool.</DL>
48
-- 16-JUN-2003 TW Initial version.
50
-------------------------------------------------------------------------------
52
pragma License (Modified_GPL);
54
with Ada.Unchecked_Deallocation;
57
with GAL.Support.List_Sort;
60
-- type Item is private;
62
-- with package Memory is new GAL.Storage.Memory (<>);
64
-- with function "=" (Left, Right : in Item) return Boolean is <>;
66
package body GAL.Containers.Simple is
69
(Left, Right : in out Simple_Container)
71
procedure Exchange is new GAL.Support.Swap (Link);
72
procedure Exchange is new GAL.Support.Swap (Natural);
74
Exchange (Left.C.Anchor, Right.C.Anchor);
75
Exchange (Left.C.Last, Right.C.Last);
76
Exchange (Left.C.Count, Right.C.Count);
80
(Container : in Simple_Container)
84
return Container.C.Count;
88
(Container : in Simple_Container)
92
return Container.C.Last = null;
97
To : in out True_Container)
99
New_Item : constant Link := new Node'(Data => What, Next => null);
101
if To.Last = null then
102
To.Anchor := New_Item;
104
To.Last.Next := New_Item;
107
To.Count := To.Count + 1;
112
To : in out Simple_Container)
119
(Container : in out True_Container)
121
P : Link := Container.Anchor;
123
procedure Free is new Ada.Unchecked_Deallocation (Node, Link);
128
Q : constant Link := P.Next;
133
Container.Anchor := null;
134
Container.Last := null;
135
Container.Count := 0;
139
(Container : in out Simple_Container)
146
(Container : in Simple_Container;
147
V : in out Visitor'Class)
149
P : Link := Container.C.Anchor;
150
Quit : Boolean := False;
152
while P /= null and then not Quit loop
153
Execute (V, P.Data, Quit);
159
-- with procedure Execute
160
-- (Value : in out Item;
161
-- Quit : in out Boolean);
163
(Container : in Simple_Container)
165
P : Link := Container.C.Anchor;
166
Quit : Boolean := False;
168
while P /= null and then not Quit loop
169
Execute (P.Data, Quit);
175
-- type Auxiliary (<>) is limited private;
176
-- with procedure Execute
177
-- (Value : in out Item;
178
-- Data : in out Auxiliary;
179
-- Quit : in out Boolean);
180
procedure Traverse_Aux_G
181
(Container : in Simple_Container;
182
Data : in out Auxiliary)
184
P : Link := Container.C.Anchor;
185
Quit : Boolean := False;
187
while P /= null and then not Quit loop
188
Execute (P.Data, Data, Quit);
194
-- with function "<" (Left, Right : in Item) return Boolean is <>;
196
(Container : in out Simple_Container)
198
function Smaller (L, R : in Link) return Boolean;
199
pragma Inline (Smaller);
201
function Smaller (L, R : in Link) return Boolean
204
return L.Data < R.Data;
207
function Next (L : in Link) return Link;
208
pragma Inline (Next);
210
function Next (L : in Link) return Link
216
procedure Set_Next (L, Next : in Link);
217
pragma Inline (Set_Next);
219
procedure Set_Next (L, Next : in Link)
225
procedure Post_Process (First, Last : in out Link)
227
pragma Warnings (Off, First); -- silence -gnatwa
228
pragma Warnings (Off, Last); -- silence -gnatwa
234
new GAL.Support.List_Sort
235
(Node, Link, Smaller, Next, Set_Next, Post_Process);
238
Sort (Container.C.Anchor, Container.C.Last);
241
procedure Adjust (Container : in out True_Container)
243
P : Link := Container.Anchor;
245
Container.Anchor := null;
246
Container.Last := null;
247
Container.Count := 0;
249
Add (P.Data, Container);
254
procedure Finalize (Container : in out True_Container)
261
(Stream : access Ada.Streams.Root_Stream_Type'Class;
262
Container : in Simple_Container)
264
P : Link := Container.C.Anchor;
266
Natural'Write (Stream, Container.C.Count);
268
Item'Write (Stream, P.Data);
274
(Stream : access Ada.Streams.Root_Stream_Type'Class;
275
Container : out Simple_Container)
282
Natural'Read (Stream, N);
287
Item'Read (Stream, Data);
288
Add (Data, Container);
295
end GAL.Containers.Simple;