OSDN Git Service

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