OSDN Git Service

2009-04-08 Thomas Quinot <quinot@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-ciormu.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT LIBRARY COMPONENTS                          --
4 --                                                                          --
5 --               ADA.CONTAINERS.INDEFINITE_ORDERED_MULTISETS                --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --          Copyright (C) 2004-2008, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- This unit was originally developed by Matthew J Heaney.                  --
30 ------------------------------------------------------------------------------
31
32 --  The indefinite ordered multiset container is similar to the indefinite
33 --  ordered set, but with the difference that multiple equivalent elements are
34 --  allowed. It also provides additional operations, to iterate over items that
35 --  are equivalent.
36
37 private with Ada.Containers.Red_Black_Trees;
38 private with Ada.Finalization;
39 private with Ada.Streams;
40
41 generic
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.Indefinite_Ordered_Multisets is
48    pragma Preelaborate;
49    pragma Remote_Types;
50
51    function Equivalent_Elements (Left, Right : Element_Type) return Boolean;
52    --  Returns False if Left is less than Right, or Right is less than Left;
53    --  otherwise, it returns True.
54
55    type Set is tagged private;
56    pragma Preelaborable_Initialization (Set);
57
58    type Cursor is private;
59    pragma Preelaborable_Initialization (Cursor);
60
61    Empty_Set : constant Set;
62    --  The default value for set objects declared without an explicit
63    --  initialization expression.
64
65    No_Element : constant Cursor;
66    --  The default value for cursor objects declared without an explicit
67    --  initialization expression.
68
69    function "=" (Left, Right : Set) return Boolean;
70    --  If Left denotes the same set object as Right, then equality returns
71    --  True. If the length of Left is different from the length of Right, then
72    --  it returns False. Otherwise, set equality iterates over Left and Right,
73    --  comparing the element of Left to the element of Right using the equality
74    --  operator for elements. If the elements compare False, then the iteration
75    --  terminates and set equality returns False. Otherwise, if all elements
76    --  compare True, then set equality returns True.
77
78    function Equivalent_Sets (Left, Right : Set) return Boolean;
79    --  Similar to set equality, but with the difference that elements are
80    --  compared for equivalence instead of equality.
81
82    function To_Set (New_Item : Element_Type) return Set;
83    --  Constructs a set object with New_Item as its single element
84
85    function Length (Container : Set) return Count_Type;
86    --  Returns the total number of elements in Container
87
88    function Is_Empty (Container : Set) return Boolean;
89    --  Returns True if Container.Length is 0
90
91    procedure Clear (Container : in out Set);
92    --  Deletes all elements from Container
93
94    function Element (Position : Cursor) return Element_Type;
95    --  If Position equals No_Element, then Constraint_Error is raised.
96    --  Otherwise, function Element returns the element designed by Position.
97
98    procedure Replace_Element
99      (Container : in out Set;
100       Position  : Cursor;
101       New_Item  : Element_Type);
102    --  If Position equals No_Element, then Constraint_Error is raised. If
103    --  Position is associated with a set different from Container, then
104    --  Program_Error is raised. If New_Item is equivalent to the element
105    --  designated by Position, then if Container is locked (element tampering
106    --  has been attempted), Program_Error is raised; otherwise, the element
107    --  designated by Position is assigned the value of New_Item. If New_Item is
108    --  not equivalent to the element designated by Position, then if the
109    --  container is busy (cursor tampering has been attempted), Program_Error
110    --  is raised; otherwise, the element designed by Position is assigned the
111    --  value of New_Item, and the node is moved to its new position (in
112    --  canonical insertion order).
113
114    procedure Query_Element
115      (Position : Cursor;
116       Process  : not null access procedure (Element : Element_Type));
117    --  If Position equals No_Element, then Constraint_Error is
118    --  raised. Otherwise, it calls Process with the element designated by
119    --  Position as the parameter. This call locks the container, so attempts to
120    --  change the value of the element while Process is executing (to "tamper
121    --  with elements") will raise Program_Error.
122
123    procedure Move (Target : in out Set; Source : in out Set);
124    --  If Target denotes the same object as Source, the operation does
125    --  nothing. If either Target or Source is busy (cursor tampering is
126    --  attempted), then it raises Program_Error. Otherwise, Target is cleared,
127    --  and the nodes from Source are moved (not copied) to Target (so Source
128    --  becomes empty).
129
130    procedure Insert
131      (Container : in out Set;
132       New_Item  : Element_Type;
133       Position  : out Cursor);
134    --  Insert adds New_Item to Container, and returns cursor Position
135    --  designating the newly inserted node. The node is inserted after any
136    --  existing elements less than or equivalent to New_Item (and before any
137    --  elements greater than New_Item). Note that the issue of where the new
138    --  node is inserted relative to equivalent elements does not arise for
139    --  unique-key containers, since in that case the insertion would simply
140    --  fail. For a multiple-key container (the case here), insertion always
141    --  succeeds, and is defined such that the new item is positioned after any
142    --  equivalent elements already in the container.
143
144    procedure Insert (Container : in out Set; New_Item : Element_Type);
145    --  Inserts New_Item in Container, but does not return a cursor designating
146    --  the newly-inserted node.
147
148 --  TODO: include Replace too???
149 --
150 --     procedure Replace
151 --       (Container : in out Set;
152 --        New_Item  : Element_Type);
153
154    procedure Exclude (Container : in out Set; Item : Element_Type);
155    --  Deletes from Container all of the elements equivalent to Item
156
157    procedure Delete (Container : in out Set; Item : Element_Type);
158    --  Deletes from Container all of the elements equivalent to Item. If there
159    --  are no elements equivalent to Item, then it raises Constraint_Error.
160
161    procedure Delete (Container : in out Set; Position : in out Cursor);
162    --  If Position equals No_Element, then Constraint_Error is raised. If
163    --  Position is associated with a set different from Container, then
164    --  Program_Error is raised. Otherwise, the node designated by Position is
165    --  removed from Container, and Position is set to No_Element.
166
167    procedure Delete_First (Container : in out Set);
168    --  Removes the first node from Container
169
170    procedure Delete_Last (Container : in out Set);
171    --  Removes the last node from Container
172
173    procedure Union (Target : in out Set; Source : Set);
174    --  If Target is busy (cursor tampering is attempted), then Program_Error is
175    --  raised. Otherwise, it inserts each element of Source into Target.
176    --  Elements are inserted in the canonical order for multisets, such that
177    --  the elements from Source are inserted after equivalent elements already
178    --  in Target.
179
180    function Union (Left, Right : Set) return Set;
181    --  Returns a set comprising the all elements from Left and all of the
182    --  elements from Right. The elements from Right follow the equivalent
183    --  elements from Left.
184
185    function "or" (Left, Right : Set) return Set renames Union;
186
187    procedure Intersection (Target : in out Set; Source : Set);
188    --  If Target denotes the same object as Source, the operation does
189    --  nothing. If Target is busy (cursor tampering is attempted),
190    --  Program_Error is raised. Otherwise, the elements in Target having no
191    --  equivalent element in Source are deleted from Target.
192
193    function Intersection (Left, Right : Set) return Set;
194    --  If Left denotes the same object as Right, then the function returns a
195    --  copy of Left. Otherwise, it returns a set comprising the equivalent
196    --  elements from both Left and Right. Items are inserted in the result set
197    --  in canonical order, such that the elements from Left precede the
198    --  equivalent elements from Right.
199
200    function "and" (Left, Right : Set) return Set renames Intersection;
201
202    procedure Difference (Target : in out Set; Source : Set);
203    --  If Target is busy (cursor tampering is attempted), then Program_Error is
204    --  raised. Otherwise, the elements in Target that are equivalent to
205    --  elements in Source are deleted from Target.
206
207    function Difference (Left, Right : Set) return Set;
208    --  Returns a set comprising the elements from Left that have no equivalent
209    --  element in Right.
210
211    function "-" (Left, Right : Set) return Set renames Difference;
212
213    procedure Symmetric_Difference (Target : in out Set; Source : Set);
214    --  If Target is busy, then Program_Error is raised. Otherwise, the elements
215    --  in Target equivalent to elements in Source are deleted from Target, and
216    --  the elements in Source not equivalent to elements in Target are inserted
217    --  into Target.
218
219    function Symmetric_Difference (Left, Right : Set) return Set;
220    --  Returns a set comprising the union of the elements from Target having no
221    --  equivalent in Source, and the elements of Source having no equivalent in
222    --  Target.
223
224    function "xor" (Left, Right : Set) return Set renames Symmetric_Difference;
225
226    function Overlap (Left, Right : Set) return Boolean;
227    --  Returns True if Left contains an element equivalent to an element of
228    --  Right.
229
230    function Is_Subset (Subset : Set; Of_Set : Set) return Boolean;
231    --  Returns True if every element in Subset has an equivalent element in
232    --  Of_Set.
233
234    function First (Container : Set) return Cursor;
235    --  If Container is empty, the function returns No_Element. Otherwise, it
236    --  returns a cursor designating the smallest element.
237
238    function First_Element (Container : Set) return Element_Type;
239    --  Equivalent to Element (First (Container))
240
241    function Last (Container : Set) return Cursor;
242    --  If Container is empty, the function returns No_Element. Otherwise, it
243    --  returns a cursor designating the largest element.
244
245    function Last_Element (Container : Set) return Element_Type;
246    --  Equivalent to Element (Last (Container))
247
248    function Next (Position : Cursor) return Cursor;
249    --  If Position equals No_Element or Last (Container), the function returns
250    --  No_Element. Otherwise, it returns a cursor designating the node that
251    --  immediately follows (as per the insertion order) the node designated by
252    --  Position.
253
254    procedure Next (Position : in out Cursor);
255    --  Equivalent to Position := Next (Position)
256
257    function Previous (Position : Cursor) return Cursor;
258    --  If Position equals No_Element or First (Container), the function returns
259    --  No_Element. Otherwise, it returns a cursor designating the node that
260    --  immediately precedes (as per the insertion order) the node designated by
261    --  Position.
262
263    procedure Previous (Position : in out Cursor);
264    --  Equivalent to Position := Previous (Position)
265
266    function Find (Container : Set; Item : Element_Type) return Cursor;
267    --  Returns a cursor designating the first element in Container equivalent
268    --  to Item. If there is no equivalent element, it returns No_Element.
269
270    function Floor (Container : Set; Item : Element_Type) return Cursor;
271    --  If Container is empty, the function returns No_Element. If Item is
272    --  equivalent to elements in Container, it returns a cursor designating the
273    --  first equivalent element. Otherwise, it returns a cursor designating the
274    --  largest element less than Item, or No_Element if all elements are
275    --  greater than Item.
276
277    function Ceiling (Container : Set; Item : Element_Type) return Cursor;
278    --  If Container is empty, the function returns No_Element. If Item is
279    --  equivalent to elements of Container, it returns a cursor designating the
280    --  last equivalent element. Otherwise, it returns a cursor designating the
281    --  smallest element greater than Item, or No_Element if all elements are
282    --  less than Item.
283
284    function Contains (Container : Set; Item : Element_Type) return Boolean;
285    --  Equivalent to Container.Find (Item) /= No_Element
286
287    function Has_Element (Position : Cursor) return Boolean;
288    --  Equivalent to Position /= No_Element
289
290    function "<" (Left, Right : Cursor) return Boolean;
291    --  Equivalent to Element (Left) < Element (Right)
292
293    function ">" (Left, Right : Cursor) return Boolean;
294    --  Equivalent to Element (Right) < Element (Left)
295
296    function "<" (Left : Cursor; Right : Element_Type) return Boolean;
297    --  Equivalent to Element (Left) < Right
298
299    function ">" (Left : Cursor; Right : Element_Type) return Boolean;
300    --  Equivalent to Right < Element (Left)
301
302    function "<" (Left : Element_Type; Right : Cursor) return Boolean;
303    --  Equivalent to Left < Element (Right)
304
305    function ">" (Left : Element_Type; Right : Cursor) return Boolean;
306    --  Equivalent to Element (Right) < Left
307
308    procedure Iterate
309      (Container : Set;
310       Process   : not null access procedure (Position : Cursor));
311    --  Calls Process with a cursor designating each element of Container, in
312    --  order from Container.First to Container.Last.
313
314    procedure Reverse_Iterate
315      (Container : Set;
316       Process   : not null access procedure (Position : Cursor));
317    --  Calls Process with a cursor designating each element of Container, in
318    --  order from Container.Last to Container.First.
319
320    procedure Iterate
321      (Container : Set;
322       Item      : Element_Type;
323       Process   : not null access procedure (Position : Cursor));
324    --  Call Process with a cursor designating each element equivalent to Item,
325    --  in order from Container.Floor (Item) to Container.Ceiling (Item).
326
327    procedure Reverse_Iterate
328      (Container : Set;
329       Item      : Element_Type;
330       Process   : not null access procedure (Position : Cursor));
331    --  Call Process with a cursor designating each element equivalent to Item,
332    --  in order from Container.Ceiling (Item) to Container.Floor (Item).
333
334    generic
335       type Key_Type (<>) is private;
336
337       with function Key (Element : Element_Type) return Key_Type;
338
339       with function "<" (Left, Right : Key_Type) return Boolean is <>;
340
341    package Generic_Keys is
342
343       function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
344       --  Returns False if Left is less than Right, or Right is less than Left;
345       --  otherwise, it returns True.
346
347       function Key (Position : Cursor) return Key_Type;
348       --  Equivalent to Key (Element (Position))
349
350       function Element (Container : Set; Key : Key_Type) return Element_Type;
351       --  Equivalent to Element (Find (Container, Key))
352
353       procedure Exclude (Container : in out Set; Key : Key_Type);
354       --  Deletes from Container any elements whose key is equivalent to Key
355
356       procedure Delete (Container : in out Set; Key : Key_Type);
357       --  Deletes from Container any elements whose key is equivalent to
358       --  Key. If there are no such elements, then it raises Constraint_Error.
359
360       function Find (Container : Set; Key : Key_Type) return Cursor;
361       --  Returns a cursor designating the first element in Container whose key
362       --  is equivalent to Key. If there is no equivalent element, it returns
363       --  No_Element.
364
365       function Floor (Container : Set; Key : Key_Type) return Cursor;
366       --  If Container is empty, the function returns No_Element. If Item is
367       --  equivalent to the keys of elements in Container, it returns a cursor
368       --  designating the first such element. Otherwise, it returns a cursor
369       --  designating the largest element whose key is less than Item, or
370       --  No_Element if all keys are greater than Item.
371
372       function Ceiling (Container : Set; Key : Key_Type) return Cursor;
373       --  If Container is empty, the function returns No_Element. If Item is
374       --  equivalent to the keys of elements of Container, it returns a cursor
375       --  designating the last such element. Otherwise, it returns a cursor
376       --  designating the smallest element whose key is greater than Item, or
377       --  No_Element if all keys are less than Item.
378
379       function Contains (Container : Set; Key : Key_Type) return Boolean;
380       --  Equivalent to Find (Container, Key) /= No_Element
381
382       procedure Update_Element  -- Update_Element_Preserving_Key ???
383         (Container : in out Set;
384          Position  : Cursor;
385          Process   : not null access
386                        procedure (Element : in out Element_Type));
387       --  If Position equals No_Element, then Constraint_Error is raised. If
388       --  Position is associated with a set object different from Container,
389       --  then Program_Error is raised. Otherwise, it makes a copy of the key
390       --  of the element designated by Position, and then calls Process with
391       --  the element as the parameter. Update_Element then compares the key
392       --  value obtained before calling Process to the key value obtained from
393       --  the element after calling Process. If the keys are equivalent then
394       --  the operation terminates. If Container is busy (cursor tampering has
395       --  been attempted), then Program_Error is raised. Otherwise, the node
396       --  is moved to its new position (in canonical order).
397
398       procedure Iterate
399         (Container : Set;
400          Key       : Key_Type;
401          Process   : not null access procedure (Position : Cursor));
402       --  Call Process with a cursor designating each element equivalent to
403       --  Key, in order from Floor (Container, Key) to
404       --  Ceiling (Container, Key).
405
406       procedure Reverse_Iterate
407         (Container : Set;
408          Key       : Key_Type;
409          Process   : not null access procedure (Position : Cursor));
410       --  Call Process with a cursor designating each element equivalent to
411       --  Key, in order from Ceiling (Container, Key) to
412       --  Floor (Container, Key).
413
414    end Generic_Keys;
415
416 private
417
418    pragma Inline (Next);
419    pragma Inline (Previous);
420
421    type Node_Type;
422    type Node_Access is access Node_Type;
423
424    type Element_Access is access Element_Type;
425
426    type Node_Type is limited record
427       Parent  : Node_Access;
428       Left    : Node_Access;
429       Right   : Node_Access;
430       Color   : Red_Black_Trees.Color_Type := Red_Black_Trees.Red;
431       Element : Element_Access;
432    end record;
433
434    package Tree_Types is new Red_Black_Trees.Generic_Tree_Types
435      (Node_Type,
436       Node_Access);
437
438    type Set is new Ada.Finalization.Controlled with record
439       Tree : Tree_Types.Tree_Type;
440    end record;
441
442    overriding
443    procedure Adjust (Container : in out Set);
444
445    overriding
446    procedure Finalize (Container : in out Set) renames Clear;
447
448    use Red_Black_Trees;
449    use Tree_Types;
450    use Ada.Finalization;
451    use Ada.Streams;
452
453    type Set_Access is access all Set;
454    for Set_Access'Storage_Size use 0;
455
456    type Cursor is record
457       Container : Set_Access;
458       Node      : Node_Access;
459    end record;
460
461    procedure Write
462      (Stream : not null access Root_Stream_Type'Class;
463       Item   : Cursor);
464
465    for Cursor'Write use Write;
466
467    procedure Read
468      (Stream : not null access Root_Stream_Type'Class;
469       Item   : out Cursor);
470
471    for Cursor'Read use Read;
472
473    No_Element : constant Cursor := Cursor'(null, null);
474
475    procedure Write
476      (Stream    : not null access Root_Stream_Type'Class;
477       Container : Set);
478
479    for Set'Write use Write;
480
481    procedure Read
482      (Stream    : not null access Root_Stream_Type'Class;
483       Container : out Set);
484
485    for Set'Read use Read;
486
487    Empty_Set : constant Set :=
488                  (Controlled with Tree => (First  => null,
489                                            Last   => null,
490                                            Root   => null,
491                                            Length => 0,
492                                            Busy   => 0,
493                                            Lock   => 0));
494
495 end Ada.Containers.Indefinite_Ordered_Multisets;