OSDN Git Service

2012-01-10 Richard Guenther <rguenther@suse.de>
[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-2011, 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 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.                                     --
21 --                                                                          --
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.               --
25 --                                                                          --
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/>.                                          --
30 --                                                                          --
31 -- This unit was originally developed by Matthew J Heaney.                  --
32 ------------------------------------------------------------------------------
33
34 private with Ada.Containers.Red_Black_Trees;
35 private with Ada.Finalization;
36
37 with Ada.Streams; use Ada.Streams;
38 with Ada.Iterator_Interfaces;
39
40 generic
41    type Element_Type is private;
42
43    with function "<" (Left, Right : Element_Type) return Boolean is <>;
44    with function "=" (Left, Right : Element_Type) return Boolean is <>;
45
46 package Ada.Containers.Ordered_Sets is
47    pragma Preelaborate;
48    pragma Remote_Types;
49
50    function Equivalent_Elements (Left, Right : Element_Type) return Boolean;
51
52    type Set is tagged private
53    with Constant_Indexing => Constant_Reference,
54         Default_Iterator  => Iterate,
55         Iterator_Element  => Element_Type;
56
57    pragma Preelaborable_Initialization (Set);
58
59    type Cursor is private;
60    pragma Preelaborable_Initialization (Cursor);
61
62    function Has_Element (Position : Cursor) return Boolean;
63
64    Empty_Set : constant Set;
65
66    No_Element : constant Cursor;
67
68    package Set_Iterator_Interfaces is new
69      Ada.Iterator_Interfaces (Cursor, Has_Element);
70
71    function "=" (Left, Right : Set) return Boolean;
72
73    function Equivalent_Sets (Left, Right : Set) return Boolean;
74
75    function To_Set (New_Item : Element_Type) return Set;
76
77    function Length (Container : Set) return Count_Type;
78
79    function Is_Empty (Container : Set) return Boolean;
80
81    procedure Clear (Container : in out Set);
82
83    function Element (Position : Cursor) return Element_Type;
84
85    procedure Replace_Element
86      (Container : in out Set;
87       Position  : Cursor;
88       New_Item  : Element_Type);
89
90    procedure Query_Element
91      (Position : Cursor;
92       Process  : not null access procedure (Element : Element_Type));
93
94    type Constant_Reference_Type
95       (Element : not null access constant Element_Type) is
96    private
97    with
98       Implicit_Dereference => Element;
99
100    procedure Write
101      (Stream : not null access Root_Stream_Type'Class;
102       Item   : Constant_Reference_Type);
103
104    for Constant_Reference_Type'Write use Write;
105
106    function Constant_Reference
107      (Container : aliased Set;
108       Position  : Cursor) return Constant_Reference_Type;
109
110    procedure Read
111      (Stream : not null access Root_Stream_Type'Class;
112       Item   : out Constant_Reference_Type);
113
114    for Constant_Reference_Type'Read use Read;
115
116    procedure Assign (Target : in out Set; Source : Set);
117
118    function Copy (Source : Set) return Set;
119
120    procedure Move (Target : in out Set; Source : in out Set);
121
122    procedure Insert
123      (Container : in out Set;
124       New_Item  : Element_Type;
125       Position  : out Cursor;
126       Inserted  : out Boolean);
127
128    procedure Insert
129      (Container : in out Set;
130       New_Item  : Element_Type);
131
132    procedure Include
133      (Container : in out Set;
134       New_Item  : Element_Type);
135
136    procedure Replace
137      (Container : in out Set;
138       New_Item  : Element_Type);
139
140    procedure Exclude
141      (Container : in out Set;
142       Item      : Element_Type);
143
144    procedure Delete
145      (Container : in out Set;
146       Item      : Element_Type);
147
148    procedure Delete
149      (Container : in out Set;
150       Position  : in out Cursor);
151
152    procedure Delete_First (Container : in out Set);
153
154    procedure Delete_Last (Container : in out Set);
155
156    procedure Union (Target : in out Set; Source : Set);
157
158    function Union (Left, Right : Set) return Set;
159
160    function "or" (Left, Right : Set) return Set renames Union;
161
162    procedure Intersection (Target : in out Set; Source : Set);
163
164    function Intersection (Left, Right : Set) return Set;
165
166    function "and" (Left, Right : Set) return Set renames Intersection;
167
168    procedure Difference (Target : in out Set; Source : Set);
169
170    function Difference (Left, Right : Set) return Set;
171
172    function "-" (Left, Right : Set) return Set renames Difference;
173
174    procedure Symmetric_Difference (Target : in out Set; Source : Set);
175
176    function Symmetric_Difference (Left, Right : Set) return Set;
177
178    function "xor" (Left, Right : Set) return Set renames Symmetric_Difference;
179
180    function Overlap (Left, Right : Set) return Boolean;
181
182    function Is_Subset (Subset : Set; Of_Set : Set) return Boolean;
183
184    function First (Container : Set) return Cursor;
185
186    function First_Element (Container : Set) return Element_Type;
187
188    function Last (Container : Set) return Cursor;
189
190    function Last_Element (Container : Set) return Element_Type;
191
192    function Next (Position : Cursor) return Cursor;
193
194    procedure Next (Position : in out Cursor);
195
196    function Previous (Position : Cursor) return Cursor;
197
198    procedure Previous (Position : in out Cursor);
199
200    function Find (Container : Set; Item : Element_Type) return Cursor;
201
202    function Floor (Container : Set; Item : Element_Type) return Cursor;
203
204    function Ceiling (Container : Set; Item : Element_Type) return Cursor;
205
206    function Contains (Container : Set; Item : Element_Type) return Boolean;
207
208    function "<" (Left, Right : Cursor) return Boolean;
209
210    function ">" (Left, Right : Cursor) return Boolean;
211
212    function "<" (Left : Cursor; Right : Element_Type) return Boolean;
213
214    function ">" (Left : Cursor; Right : Element_Type) return Boolean;
215
216    function "<" (Left : Element_Type; Right : Cursor) return Boolean;
217
218    function ">" (Left : Element_Type; Right : Cursor) return Boolean;
219
220    procedure Iterate
221      (Container : Set;
222       Process   : not null access procedure (Position : Cursor));
223
224    procedure Reverse_Iterate
225      (Container : Set;
226       Process   : not null access procedure (Position : Cursor));
227
228    function Iterate
229      (Container : Set)
230       return Set_Iterator_Interfaces.Reversible_Iterator'class;
231
232    function Iterate
233      (Container : Set;
234       Start     : Cursor)
235       return Set_Iterator_Interfaces.Reversible_Iterator'class;
236
237    generic
238       type Key_Type (<>) is private;
239
240       with function Key (Element : Element_Type) return Key_Type;
241
242       with function "<" (Left, Right : Key_Type) return Boolean is <>;
243
244    package Generic_Keys is
245
246       function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
247
248       function Key (Position : Cursor) return Key_Type;
249
250       function Element (Container : Set; Key : Key_Type) return Element_Type;
251
252       procedure Replace
253         (Container : in out Set;
254          Key       : Key_Type;
255          New_Item  : Element_Type);
256
257       procedure Exclude (Container : in out Set; Key : Key_Type);
258
259       procedure Delete (Container : in out Set; Key : Key_Type);
260
261       function Find (Container : Set; Key : Key_Type) return Cursor;
262
263       function Floor (Container : Set; Key : Key_Type) return Cursor;
264
265       function Ceiling (Container : Set; Key : Key_Type) return Cursor;
266
267       function Contains (Container : Set; Key : Key_Type) return Boolean;
268
269       procedure Update_Element_Preserving_Key
270         (Container : in out Set;
271          Position  : Cursor;
272          Process   : not null access
273                        procedure (Element : in out Element_Type));
274
275       type Reference_Type (Element : not null access Element_Type) is private
276       with
277          Implicit_Dereference => Element;
278
279       function Reference_Preserving_Key
280         (Container : aliased in out Set;
281          Position  : Cursor) return Reference_Type;
282
283       function Constant_Reference
284         (Container : aliased Set;
285          Key       : Key_Type) return Constant_Reference_Type;
286
287       function Reference_Preserving_Key
288         (Container : aliased in out Set;
289          Key       : Key_Type) return Reference_Type;
290
291    private
292       type Reference_Type
293          (Element : not null access Element_Type) is null record;
294
295       procedure Write
296         (Stream : not null access Root_Stream_Type'Class;
297          Item   : Reference_Type);
298
299       for Reference_Type'Write use Write;
300
301       procedure Read
302         (Stream : not null access Root_Stream_Type'Class;
303          Item   : out Reference_Type);
304
305       for Reference_Type'Read use Read;
306    end Generic_Keys;
307
308 private
309
310    pragma Inline (Next);
311    pragma Inline (Previous);
312
313    type Node_Type;
314    type Node_Access is access Node_Type;
315
316    type Node_Type is limited record
317       Parent  : Node_Access;
318       Left    : Node_Access;
319       Right   : Node_Access;
320       Color   : Red_Black_Trees.Color_Type := Red_Black_Trees.Red;
321       Element : aliased Element_Type;
322    end record;
323
324    package Tree_Types is
325      new Red_Black_Trees.Generic_Tree_Types (Node_Type, Node_Access);
326
327    type Set is new Ada.Finalization.Controlled with record
328       Tree : Tree_Types.Tree_Type;
329    end record;
330
331    overriding procedure Adjust (Container : in out Set);
332
333    overriding procedure Finalize (Container : in out Set) renames Clear;
334
335    use Red_Black_Trees;
336    use Tree_Types;
337    use Ada.Finalization;
338
339    type Set_Access is access all Set;
340    for Set_Access'Storage_Size use 0;
341
342    type Cursor is record
343       Container : Set_Access;
344       Node      : Node_Access;
345    end record;
346
347    procedure Write
348      (Stream : not null access Root_Stream_Type'Class;
349       Item   : Cursor);
350
351    for Cursor'Write use Write;
352
353    procedure Read
354      (Stream : not null access Root_Stream_Type'Class;
355       Item   : out Cursor);
356
357    for Cursor'Read use Read;
358
359    No_Element : constant Cursor := Cursor'(null, null);
360
361    procedure Write
362      (Stream    : not null access Root_Stream_Type'Class;
363       Container : Set);
364
365    for Set'Write use Write;
366
367    procedure Read
368      (Stream    : not null access Root_Stream_Type'Class;
369       Container : out Set);
370
371    for Set'Read use Read;
372
373    type Constant_Reference_Type
374       (Element : not null access constant Element_Type) is null record;
375
376    Empty_Set : constant Set :=
377                  (Controlled with Tree => (First  => null,
378                                            Last   => null,
379                                            Root   => null,
380                                            Length => 0,
381                                            Busy   => 0,
382                                            Lock   => 0));
383
384 end Ada.Containers.Ordered_Sets;