OSDN Git Service

2012-02-17 Thomas Quinot <quinot@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-2012, 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 with Ada.Iterator_Interfaces;
35
36 private with Ada.Containers.Red_Black_Trees;
37 private with Ada.Finalization;
38 private with Ada.Streams;
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    function Constant_Reference
101      (Container : aliased Set;
102       Position  : Cursor) return Constant_Reference_Type;
103
104    procedure Assign (Target : in out Set; Source : Set);
105
106    function Copy (Source : Set) return Set;
107
108    procedure Move (Target : in out Set; Source : in out Set);
109
110    procedure Insert
111      (Container : in out Set;
112       New_Item  : Element_Type;
113       Position  : out Cursor;
114       Inserted  : out Boolean);
115
116    procedure Insert
117      (Container : in out Set;
118       New_Item  : Element_Type);
119
120    procedure Include
121      (Container : in out Set;
122       New_Item  : Element_Type);
123
124    procedure Replace
125      (Container : in out Set;
126       New_Item  : Element_Type);
127
128    procedure Exclude
129      (Container : in out Set;
130       Item      : Element_Type);
131
132    procedure Delete
133      (Container : in out Set;
134       Item      : Element_Type);
135
136    procedure Delete
137      (Container : in out Set;
138       Position  : in out Cursor);
139
140    procedure Delete_First (Container : in out Set);
141
142    procedure Delete_Last (Container : in out Set);
143
144    procedure Union (Target : in out Set; Source : Set);
145
146    function Union (Left, Right : Set) return Set;
147
148    function "or" (Left, Right : Set) return Set renames Union;
149
150    procedure Intersection (Target : in out Set; Source : Set);
151
152    function Intersection (Left, Right : Set) return Set;
153
154    function "and" (Left, Right : Set) return Set renames Intersection;
155
156    procedure Difference (Target : in out Set; Source : Set);
157
158    function Difference (Left, Right : Set) return Set;
159
160    function "-" (Left, Right : Set) return Set renames Difference;
161
162    procedure Symmetric_Difference (Target : in out Set; Source : Set);
163
164    function Symmetric_Difference (Left, Right : Set) return Set;
165
166    function "xor" (Left, Right : Set) return Set renames Symmetric_Difference;
167
168    function Overlap (Left, Right : Set) return Boolean;
169
170    function Is_Subset (Subset : Set; Of_Set : Set) return Boolean;
171
172    function First (Container : Set) return Cursor;
173
174    function First_Element (Container : Set) return Element_Type;
175
176    function Last (Container : Set) return Cursor;
177
178    function Last_Element (Container : Set) return Element_Type;
179
180    function Next (Position : Cursor) return Cursor;
181
182    procedure Next (Position : in out Cursor);
183
184    function Previous (Position : Cursor) return Cursor;
185
186    procedure Previous (Position : in out Cursor);
187
188    function Find (Container : Set; Item : Element_Type) return Cursor;
189
190    function Floor (Container : Set; Item : Element_Type) return Cursor;
191
192    function Ceiling (Container : Set; Item : Element_Type) return Cursor;
193
194    function Contains (Container : Set; Item : Element_Type) return Boolean;
195
196    function "<" (Left, Right : Cursor) return Boolean;
197
198    function ">" (Left, Right : Cursor) return Boolean;
199
200    function "<" (Left : Cursor; Right : Element_Type) return Boolean;
201
202    function ">" (Left : Cursor; Right : Element_Type) return Boolean;
203
204    function "<" (Left : Element_Type; Right : Cursor) return Boolean;
205
206    function ">" (Left : Element_Type; Right : Cursor) return Boolean;
207
208    procedure Iterate
209      (Container : Set;
210       Process   : not null access procedure (Position : Cursor));
211
212    procedure Reverse_Iterate
213      (Container : Set;
214       Process   : not null access procedure (Position : Cursor));
215
216    function Iterate
217      (Container : Set)
218       return Set_Iterator_Interfaces.Reversible_Iterator'class;
219
220    function Iterate
221      (Container : Set;
222       Start     : Cursor)
223       return Set_Iterator_Interfaces.Reversible_Iterator'class;
224
225    generic
226       type Key_Type (<>) is private;
227
228       with function Key (Element : Element_Type) return Key_Type;
229
230       with function "<" (Left, Right : Key_Type) return Boolean is <>;
231
232    package Generic_Keys is
233
234       function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
235
236       function Key (Position : Cursor) return Key_Type;
237
238       function Element (Container : Set; Key : Key_Type) return Element_Type;
239
240       procedure Replace
241         (Container : in out Set;
242          Key       : Key_Type;
243          New_Item  : Element_Type);
244
245       procedure Exclude (Container : in out Set; Key : Key_Type);
246
247       procedure Delete (Container : in out Set; Key : Key_Type);
248
249       function Find (Container : Set; Key : Key_Type) return Cursor;
250
251       function Floor (Container : Set; Key : Key_Type) return Cursor;
252
253       function Ceiling (Container : Set; Key : Key_Type) return Cursor;
254
255       function Contains (Container : Set; Key : Key_Type) return Boolean;
256
257       procedure Update_Element_Preserving_Key
258         (Container : in out Set;
259          Position  : Cursor;
260          Process   : not null access
261                        procedure (Element : in out Element_Type));
262
263       type Reference_Type (Element : not null access Element_Type) is private
264       with
265          Implicit_Dereference => Element;
266
267       function Reference_Preserving_Key
268         (Container : aliased in out Set;
269          Position  : Cursor) return Reference_Type;
270
271       function Constant_Reference
272         (Container : aliased Set;
273          Key       : Key_Type) return Constant_Reference_Type;
274
275       function Reference_Preserving_Key
276         (Container : aliased in out Set;
277          Key       : Key_Type) return Reference_Type;
278
279    private
280       type Reference_Type
281          (Element : not null access Element_Type) is null record;
282
283       use Ada.Streams;
284
285       procedure Write
286         (Stream : not null access Root_Stream_Type'Class;
287          Item   : Reference_Type);
288
289       for Reference_Type'Write use Write;
290
291       procedure Read
292         (Stream : not null access Root_Stream_Type'Class;
293          Item   : out Reference_Type);
294
295       for Reference_Type'Read use Read;
296    end Generic_Keys;
297
298 private
299
300    pragma Inline (Next);
301    pragma Inline (Previous);
302
303    type Node_Type;
304    type Node_Access is access Node_Type;
305
306    type Node_Type is limited record
307       Parent  : Node_Access;
308       Left    : Node_Access;
309       Right   : Node_Access;
310       Color   : Red_Black_Trees.Color_Type := Red_Black_Trees.Red;
311       Element : aliased Element_Type;
312    end record;
313
314    package Tree_Types is
315      new Red_Black_Trees.Generic_Tree_Types (Node_Type, Node_Access);
316
317    type Set is new Ada.Finalization.Controlled with record
318       Tree : Tree_Types.Tree_Type;
319    end record;
320
321    overriding procedure Adjust (Container : in out Set);
322
323    overriding procedure Finalize (Container : in out Set) renames Clear;
324
325    use Red_Black_Trees;
326    use Tree_Types;
327    use Ada.Finalization;
328    use Ada.Streams;
329
330    procedure Write
331      (Stream    : not null access Root_Stream_Type'Class;
332       Container : Set);
333
334    for Set'Write use Write;
335
336    procedure Read
337      (Stream    : not null access Root_Stream_Type'Class;
338       Container : out Set);
339
340    for Set'Read use Read;
341
342    type Set_Access is access all Set;
343    for Set_Access'Storage_Size use 0;
344
345    type Cursor is record
346       Container : Set_Access;
347       Node      : Node_Access;
348    end record;
349
350    procedure Write
351      (Stream : not null access Root_Stream_Type'Class;
352       Item   : Cursor);
353
354    for Cursor'Write use Write;
355
356    procedure Read
357      (Stream : not null access Root_Stream_Type'Class;
358       Item   : out Cursor);
359
360    for Cursor'Read use Read;
361
362    type Constant_Reference_Type
363       (Element : not null access constant Element_Type) is null record;
364
365    procedure Write
366      (Stream : not null access Root_Stream_Type'Class;
367       Item   : Constant_Reference_Type);
368
369    for Constant_Reference_Type'Write use Write;
370
371    procedure Read
372      (Stream : not null access Root_Stream_Type'Class;
373       Item   : out Constant_Reference_Type);
374
375    for Constant_Reference_Type'Read use Read;
376
377    Empty_Set : constant Set :=
378                  (Controlled with Tree => (First  => null,
379                                            Last   => null,
380                                            Root   => null,
381                                            Length => 0,
382                                            Busy   => 0,
383                                            Lock   => 0));
384
385    No_Element : constant Cursor := Cursor'(null, null);
386
387 end Ada.Containers.Ordered_Sets;