OSDN Git Service

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