OSDN Git Service

2005-06-14 Matthew Heaney <heaney@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-coorse.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT LIBRARY COMPONENTS                          --
4 --                                                                          --
5 --           A D A . C O N T A I N E R S . O R D E R E D _ S E T S          --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --          Copyright (C) 2004-2005 Free Software Foundation, Inc.          --
10 --                                                                          --
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. --
14 --                                                                          --
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 2,  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.  See the GNU General Public License --
21 -- for  more details.  You should have  received  a copy of the GNU General --
22 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
23 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
24 -- MA 02111-1307, USA.                                                      --
25 --                                                                          --
26 -- As a special exception,  if other files  instantiate  generics from this --
27 -- unit, or you link  this unit with other files  to produce an executable, --
28 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
29 -- covered  by the  GNU  General  Public  License.  This exception does not --
30 -- however invalidate  any other reasons why  the executable file  might be --
31 -- covered by the  GNU Public License.                                      --
32 --                                                                          --
33 -- This unit was originally developed by Matthew J Heaney.                  --
34 ------------------------------------------------------------------------------
35
36 with Ada.Containers.Red_Black_Trees;
37 with Ada.Finalization;
38 with Ada.Streams;
39
40 generic
41
42    type Element_Type is private;
43
44    with function "<" (Left, Right : Element_Type) return Boolean is <>;
45    with function "=" (Left, Right : Element_Type) return Boolean is <>;
46
47 package Ada.Containers.Ordered_Sets is
48 pragma Preelaborate (Ordered_Sets);
49
50    type Set is tagged private;
51
52    type Cursor is private;
53
54    Empty_Set : constant Set;
55
56    No_Element : constant Cursor;
57
58    function "=" (Left, Right : Set) return Boolean;
59
60    function Equivalent_Sets (Left, Right : Set) return Boolean;
61
62    function Length (Container : Set) return Count_Type;
63
64    function Is_Empty (Container : Set) return Boolean;
65
66    procedure Clear (Container : in out Set);
67
68    function Element (Position : Cursor) return Element_Type;
69
70    procedure Query_Element
71      (Position : Cursor;
72       Process  : not null access procedure (Element : Element_Type));
73
74    procedure Replace_Element
75      (Container : Set;  --  TODO: need ARG ruling
76       Position  : Cursor;
77       By        : Element_Type);
78
79    procedure Move
80      (Target : in out Set;
81       Source : in out Set);
82
83    procedure Insert
84      (Container : in out Set;
85       New_Item  : Element_Type;
86       Position  : out Cursor;
87       Inserted  : out Boolean);
88
89    procedure Insert
90      (Container : in out Set;
91       New_Item  : Element_Type);
92
93    procedure Include
94      (Container : in out Set;
95       New_Item  : Element_Type);
96
97    procedure Replace
98      (Container : in out Set;  --  TODO: need ARG ruling
99       New_Item  : Element_Type);
100
101    procedure Delete
102      (Container : in out Set;
103       Item      : Element_Type);
104
105    procedure Delete
106      (Container : in out Set;
107       Position  : in out Cursor);
108
109    procedure Delete_First (Container : in out Set);
110
111    procedure Delete_Last (Container : in out Set);
112
113    procedure Exclude
114      (Container : in out Set;
115       Item      : Element_Type);
116
117    procedure Union (Target : in out Set; Source : Set);
118
119    function Union (Left, Right : Set) return Set;
120
121    function "or" (Left, Right : Set) return Set renames Union;
122
123    procedure Intersection (Target : in out Set; Source : Set);
124
125    function Intersection (Left, Right : Set) return Set;
126
127    function "and" (Left, Right : Set) return Set renames Intersection;
128
129    procedure Difference (Target : in out Set;
130                          Source : Set);
131
132    function Difference (Left, Right : Set) return Set;
133
134    function "-" (Left, Right : Set) return Set renames Difference;
135
136    procedure Symmetric_Difference (Target : in out Set; Source : Set);
137
138    function Symmetric_Difference (Left, Right : Set) return Set;
139
140    function "xor" (Left, Right : Set) return Set renames Symmetric_Difference;
141
142    function Overlap (Left, Right : Set) return Boolean;
143
144    function Is_Subset (Subset : Set; Of_Set : Set) return Boolean;
145
146    function Contains (Container : Set; Item : Element_Type) return Boolean;
147
148    function Find (Container : Set; Item : Element_Type) return Cursor;
149
150    function Floor (Container : Set; Item : Element_Type) return Cursor;
151
152    function Ceiling (Container : Set; Item : Element_Type) return Cursor;
153
154    function First (Container : Set) return Cursor;
155
156    function First_Element (Container : Set) return Element_Type;
157
158    function Last (Container : Set) return Cursor;
159
160    function Last_Element (Container : Set) return Element_Type;
161
162    function Next (Position : Cursor) return Cursor;
163
164    procedure Next (Position : in out Cursor);
165
166    function Previous (Position : Cursor) return Cursor;
167
168    procedure Previous (Position : in out Cursor);
169
170    function Has_Element (Position : Cursor) return Boolean;
171
172    function "<" (Left, Right : Cursor) return Boolean;
173
174    function ">" (Left, Right : Cursor) return Boolean;
175
176    function "<" (Left : Cursor; Right : Element_Type) return Boolean;
177
178    function ">" (Left : Cursor; Right : Element_Type) return Boolean;
179
180    function "<" (Left : Element_Type; Right : Cursor) return Boolean;
181
182    function ">" (Left : Element_Type; Right : Cursor) return Boolean;
183
184    procedure Iterate
185      (Container : Set;
186       Process   : not null access procedure (Position : Cursor));
187
188    procedure Reverse_Iterate
189      (Container : Set;
190       Process   : not null access procedure (Position : Cursor));
191
192    generic
193       type Key_Type (<>) is limited private;
194
195       with function Key (Element : Element_Type) return Key_Type;
196
197       with function "<"
198         (Left  : Key_Type;
199          Right : Element_Type) return Boolean is <>;
200
201       with function ">"
202         (Left  : Key_Type;
203          Right : Element_Type) return Boolean is <>;
204
205    package Generic_Keys is
206
207       function Contains (Container : Set; Key : Key_Type) return Boolean;
208
209       function Find (Container : Set; Key : Key_Type) return Cursor;
210
211       function Floor (Container : Set; Key : Key_Type) return Cursor;
212
213       function Ceiling (Container : Set; Key : Key_Type) return Cursor;
214
215       function Key (Position : Cursor) return Key_Type;
216
217       function Element (Container : Set; Key : Key_Type) return Element_Type;
218
219       procedure Replace
220         (Container : in out Set;  --  TODO: need ARG ruling
221          Key       : Key_Type;
222          New_Item  : Element_Type);
223
224       procedure Delete (Container : in out Set; Key : Key_Type);
225
226       procedure Exclude (Container : in out Set; Key : Key_Type);
227
228       function "<" (Left : Cursor; Right : Key_Type) return Boolean;
229
230       function ">" (Left : Cursor; Right : Key_Type) return Boolean;
231
232       function "<" (Left : Key_Type; Right : Cursor) return Boolean;
233
234       function ">" (Left : Key_Type; Right : Cursor) return Boolean;
235
236       procedure Update_Element_Preserving_Key
237         (Container : in out Set;
238          Position  : Cursor;
239          Process   : not null access
240                        procedure (Element : in out Element_Type));
241
242    end Generic_Keys;
243
244 private
245
246    type Node_Type;
247    type Node_Access is access Node_Type;
248
249    type Node_Type is limited record
250       Parent  : Node_Access;
251       Left    : Node_Access;
252       Right   : Node_Access;
253       Color   : Red_Black_Trees.Color_Type := Red_Black_Trees.Red;
254       Element : Element_Type;
255    end record;
256
257    package Tree_Types is new Red_Black_Trees.Generic_Tree_Types
258      (Node_Type,
259       Node_Access);
260
261    type Set is new Ada.Finalization.Controlled with record
262       Tree : Tree_Types.Tree_Type;
263    end record;
264
265    procedure Adjust (Container : in out Set);
266
267    procedure Finalize (Container : in out Set) renames Clear;
268
269    use Red_Black_Trees;
270    use Tree_Types;
271    use Ada.Finalization;
272
273    type Set_Access is access all Set;
274    for Set_Access'Storage_Size use 0;
275
276    type Cursor is record
277       Container : Set_Access;
278       Node      : Node_Access;
279    end record;
280
281    No_Element : constant Cursor := Cursor'(null, null);
282
283    use Ada.Streams;
284
285    procedure Write
286      (Stream    : access Root_Stream_Type'Class;
287       Container : Set);
288
289    for Set'Write use Write;
290
291    procedure Read
292      (Stream    : access Root_Stream_Type'Class;
293       Container : out Set);
294
295    for Set'Read use Read;
296
297    Empty_Set : constant Set :=
298                  (Controlled with Tree => (First  => null,
299                                            Last   => null,
300                                            Root   => null,
301                                            Length => 0,
302                                            Busy   => 0,
303                                            Lock   => 0));
304
305 end Ada.Containers.Ordered_Sets;