2015-06-23 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / ada / a-ciormu.ads
blob7524cf7be3c8358ce8e2b2d1fd875d4825812cb9
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- ADA.CONTAINERS.INDEFINITE_ORDERED_MULTISETS --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 2004-2015, 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 ------------------------------------------------------------------------------
30 -- The indefinite ordered multiset container is similar to the indefinite
31 -- ordered set, but with the difference that multiple equivalent elements are
32 -- allowed. It also provides additional operations, to iterate over items that
33 -- are equivalent.
35 private with Ada.Containers.Red_Black_Trees;
36 private with Ada.Finalization;
37 private with Ada.Streams;
38 with Ada.Iterator_Interfaces;
40 generic
41 type Element_Type (<>) is private;
43 with function "<" (Left, Right : Element_Type) return Boolean is <>;
44 with function "=" (Left, Right : Element_Type) return Boolean is <>;
46 package Ada.Containers.Indefinite_Ordered_Multisets is
47 pragma Preelaborate;
48 pragma Remote_Types;
50 function Equivalent_Elements (Left, Right : Element_Type) return Boolean;
51 -- Returns False if Left is less than Right, or Right is less than Left;
52 -- otherwise, it returns True.
54 type Set is tagged private
55 with Constant_Indexing => Constant_Reference,
56 Default_Iterator => Iterate,
57 Iterator_Element => Element_Type;
59 pragma Preelaborable_Initialization (Set);
61 type Cursor is private;
62 pragma Preelaborable_Initialization (Cursor);
64 Empty_Set : constant Set;
65 -- The default value for set objects declared without an explicit
66 -- initialization expression.
68 No_Element : constant Cursor;
69 -- The default value for cursor objects declared without an explicit
70 -- initialization expression.
72 function Has_Element (Position : Cursor) return Boolean;
73 -- Equivalent to Position /= No_Element
75 package Set_Iterator_Interfaces is new
76 Ada.Iterator_Interfaces (Cursor, Has_Element);
78 function "=" (Left, Right : Set) return Boolean;
79 -- If Left denotes the same set object as Right, then equality returns
80 -- True. If the length of Left is different from the length of Right, then
81 -- it returns False. Otherwise, set equality iterates over Left and Right,
82 -- comparing the element of Left to the element of Right using the equality
83 -- operator for elements. If the elements compare False, then the iteration
84 -- terminates and set equality returns False. Otherwise, if all elements
85 -- compare True, then set equality returns True.
87 function Equivalent_Sets (Left, Right : Set) return Boolean;
88 -- Similar to set equality, but with the difference that elements are
89 -- compared for equivalence instead of equality.
91 function To_Set (New_Item : Element_Type) return Set;
92 -- Constructs a set object with New_Item as its single element
94 function Length (Container : Set) return Count_Type;
95 -- Returns the total number of elements in Container
97 function Is_Empty (Container : Set) return Boolean;
98 -- Returns True if Container.Length is 0
100 procedure Clear (Container : in out Set);
101 -- Deletes all elements from Container
103 function Element (Position : Cursor) return Element_Type;
104 -- If Position equals No_Element, then Constraint_Error is raised.
105 -- Otherwise, function Element returns the element designed by Position.
107 procedure Replace_Element
108 (Container : in out Set;
109 Position : Cursor;
110 New_Item : Element_Type);
111 -- If Position equals No_Element, then Constraint_Error is raised. If
112 -- Position is associated with a set different from Container, then
113 -- Program_Error is raised. If New_Item is equivalent to the element
114 -- designated by Position, then if Container is locked (element tampering
115 -- has been attempted), Program_Error is raised; otherwise, the element
116 -- designated by Position is assigned the value of New_Item. If New_Item is
117 -- not equivalent to the element designated by Position, then if the
118 -- container is busy (cursor tampering has been attempted), Program_Error
119 -- is raised; otherwise, the element designed by Position is assigned the
120 -- value of New_Item, and the node is moved to its new position (in
121 -- canonical insertion order).
123 procedure Query_Element
124 (Position : Cursor;
125 Process : not null access procedure (Element : Element_Type));
126 -- If Position equals No_Element, then Constraint_Error is
127 -- raised. Otherwise, it calls Process with the element designated by
128 -- Position as the parameter. This call locks the container, so attempts to
129 -- change the value of the element while Process is executing (to "tamper
130 -- with elements") will raise Program_Error.
132 type Constant_Reference_Type
133 (Element : not null access constant Element_Type) is private
134 with Implicit_Dereference => Element;
136 function Constant_Reference
137 (Container : aliased Set;
138 Position : Cursor) return Constant_Reference_Type;
139 pragma Inline (Constant_Reference);
141 procedure Assign (Target : in out Set; Source : Set);
143 function Copy (Source : Set) return Set;
145 procedure Move (Target : in out Set; Source : in out Set);
146 -- If Target denotes the same object as Source, the operation does
147 -- nothing. If either Target or Source is busy (cursor tampering is
148 -- attempted), then it raises Program_Error. Otherwise, Target is cleared,
149 -- and the nodes from Source are moved (not copied) to Target (so Source
150 -- becomes empty).
152 procedure Insert
153 (Container : in out Set;
154 New_Item : Element_Type;
155 Position : out Cursor);
156 -- Insert adds New_Item to Container, and returns cursor Position
157 -- designating the newly inserted node. The node is inserted after any
158 -- existing elements less than or equivalent to New_Item (and before any
159 -- elements greater than New_Item). Note that the issue of where the new
160 -- node is inserted relative to equivalent elements does not arise for
161 -- unique-key containers, since in that case the insertion would simply
162 -- fail. For a multiple-key container (the case here), insertion always
163 -- succeeds, and is defined such that the new item is positioned after any
164 -- equivalent elements already in the container.
166 procedure Insert (Container : in out Set; New_Item : Element_Type);
167 -- Inserts New_Item in Container, but does not return a cursor designating
168 -- the newly-inserted node.
170 -- TODO: include Replace too???
172 -- procedure Replace
173 -- (Container : in out Set;
174 -- New_Item : Element_Type);
176 procedure Exclude (Container : in out Set; Item : Element_Type);
177 -- Deletes from Container all of the elements equivalent to Item
179 procedure Delete (Container : in out Set; Item : Element_Type);
180 -- Deletes from Container all of the elements equivalent to Item. If there
181 -- are no elements equivalent to Item, then it raises Constraint_Error.
183 procedure Delete (Container : in out Set; Position : in out Cursor);
184 -- If Position equals No_Element, then Constraint_Error is raised. If
185 -- Position is associated with a set different from Container, then
186 -- Program_Error is raised. Otherwise, the node designated by Position is
187 -- removed from Container, and Position is set to No_Element.
189 procedure Delete_First (Container : in out Set);
190 -- Removes the first node from Container
192 procedure Delete_Last (Container : in out Set);
193 -- Removes the last node from Container
195 procedure Union (Target : in out Set; Source : Set);
196 -- If Target is busy (cursor tampering is attempted), then Program_Error is
197 -- raised. Otherwise, it inserts each element of Source into Target.
198 -- Elements are inserted in the canonical order for multisets, such that
199 -- the elements from Source are inserted after equivalent elements already
200 -- in Target.
202 function Union (Left, Right : Set) return Set;
203 -- Returns a set comprising the all elements from Left and all of the
204 -- elements from Right. The elements from Right follow the equivalent
205 -- elements from Left.
207 function "or" (Left, Right : Set) return Set renames Union;
209 procedure Intersection (Target : in out Set; Source : Set);
210 -- If Target denotes the same object as Source, the operation does
211 -- nothing. If Target is busy (cursor tampering is attempted),
212 -- Program_Error is raised. Otherwise, the elements in Target having no
213 -- equivalent element in Source are deleted from Target.
215 function Intersection (Left, Right : Set) return Set;
216 -- If Left denotes the same object as Right, then the function returns a
217 -- copy of Left. Otherwise, it returns a set comprising the equivalent
218 -- elements from both Left and Right. Items are inserted in the result set
219 -- in canonical order, such that the elements from Left precede the
220 -- equivalent elements from Right.
222 function "and" (Left, Right : Set) return Set renames Intersection;
224 procedure Difference (Target : in out Set; Source : Set);
225 -- If Target is busy (cursor tampering is attempted), then Program_Error is
226 -- raised. Otherwise, the elements in Target that are equivalent to
227 -- elements in Source are deleted from Target.
229 function Difference (Left, Right : Set) return Set;
230 -- Returns a set comprising the elements from Left that have no equivalent
231 -- element in Right.
233 function "-" (Left, Right : Set) return Set renames Difference;
235 procedure Symmetric_Difference (Target : in out Set; Source : Set);
236 -- If Target is busy, then Program_Error is raised. Otherwise, the elements
237 -- in Target equivalent to elements in Source are deleted from Target, and
238 -- the elements in Source not equivalent to elements in Target are inserted
239 -- into Target.
241 function Symmetric_Difference (Left, Right : Set) return Set;
242 -- Returns a set comprising the union of the elements from Target having no
243 -- equivalent in Source, and the elements of Source having no equivalent in
244 -- Target.
246 function "xor" (Left, Right : Set) return Set renames Symmetric_Difference;
248 function Overlap (Left, Right : Set) return Boolean;
249 -- Returns True if Left contains an element equivalent to an element of
250 -- Right.
252 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean;
253 -- Returns True if every element in Subset has an equivalent element in
254 -- Of_Set.
256 function First (Container : Set) return Cursor;
257 -- If Container is empty, the function returns No_Element. Otherwise, it
258 -- returns a cursor designating the smallest element.
260 function First_Element (Container : Set) return Element_Type;
261 -- Equivalent to Element (First (Container))
263 function Last (Container : Set) return Cursor;
264 -- If Container is empty, the function returns No_Element. Otherwise, it
265 -- returns a cursor designating the largest element.
267 function Last_Element (Container : Set) return Element_Type;
268 -- Equivalent to Element (Last (Container))
270 function Next (Position : Cursor) return Cursor;
271 -- If Position equals No_Element or Last (Container), the function returns
272 -- No_Element. Otherwise, it returns a cursor designating the node that
273 -- immediately follows (as per the insertion order) the node designated by
274 -- Position.
276 procedure Next (Position : in out Cursor);
277 -- Equivalent to Position := Next (Position)
279 function Previous (Position : Cursor) return Cursor;
280 -- If Position equals No_Element or First (Container), the function returns
281 -- No_Element. Otherwise, it returns a cursor designating the node that
282 -- immediately precedes (as per the insertion order) the node designated by
283 -- Position.
285 procedure Previous (Position : in out Cursor);
286 -- Equivalent to Position := Previous (Position)
288 function Find (Container : Set; Item : Element_Type) return Cursor;
289 -- Returns a cursor designating the first element in Container equivalent
290 -- to Item. If there is no equivalent element, it returns No_Element.
292 function Floor (Container : Set; Item : Element_Type) return Cursor;
293 -- If Container is empty, the function returns No_Element. If Item is
294 -- equivalent to elements in Container, it returns a cursor designating the
295 -- first equivalent element. Otherwise, it returns a cursor designating the
296 -- largest element less than Item, or No_Element if all elements are
297 -- greater than Item.
299 function Ceiling (Container : Set; Item : Element_Type) return Cursor;
300 -- If Container is empty, the function returns No_Element. If Item is
301 -- equivalent to elements of Container, it returns a cursor designating the
302 -- last equivalent element. Otherwise, it returns a cursor designating the
303 -- smallest element greater than Item, or No_Element if all elements are
304 -- less than Item.
306 function Contains (Container : Set; Item : Element_Type) return Boolean;
307 -- Equivalent to Container.Find (Item) /= No_Element
309 function "<" (Left, Right : Cursor) return Boolean;
310 -- Equivalent to Element (Left) < Element (Right)
312 function ">" (Left, Right : Cursor) return Boolean;
313 -- Equivalent to Element (Right) < Element (Left)
315 function "<" (Left : Cursor; Right : Element_Type) return Boolean;
316 -- Equivalent to Element (Left) < Right
318 function ">" (Left : Cursor; Right : Element_Type) return Boolean;
319 -- Equivalent to Right < Element (Left)
321 function "<" (Left : Element_Type; Right : Cursor) return Boolean;
322 -- Equivalent to Left < Element (Right)
324 function ">" (Left : Element_Type; Right : Cursor) return Boolean;
325 -- Equivalent to Element (Right) < Left
327 procedure Iterate
328 (Container : Set;
329 Process : not null access procedure (Position : Cursor));
330 -- Calls Process with a cursor designating each element of Container, in
331 -- order from Container.First to Container.Last.
333 procedure Reverse_Iterate
334 (Container : Set;
335 Process : not null access procedure (Position : Cursor));
336 -- Calls Process with a cursor designating each element of Container, in
337 -- order from Container.Last to Container.First.
339 procedure Iterate
340 (Container : Set;
341 Item : Element_Type;
342 Process : not null access procedure (Position : Cursor));
343 -- Call Process with a cursor designating each element equivalent to Item,
344 -- in order from Container.Floor (Item) to Container.Ceiling (Item).
346 procedure Reverse_Iterate
347 (Container : Set;
348 Item : Element_Type;
349 Process : not null access procedure (Position : Cursor));
350 -- Call Process with a cursor designating each element equivalent to Item,
351 -- in order from Container.Ceiling (Item) to Container.Floor (Item).
353 function Iterate
354 (Container : Set)
355 return Set_Iterator_Interfaces.Reversible_Iterator'class;
357 function Iterate
358 (Container : Set;
359 Start : Cursor)
360 return Set_Iterator_Interfaces.Reversible_Iterator'class;
362 generic
363 type Key_Type (<>) is private;
365 with function Key (Element : Element_Type) return Key_Type;
367 with function "<" (Left, Right : Key_Type) return Boolean is <>;
369 package Generic_Keys is
371 function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
372 -- Returns False if Left is less than Right, or Right is less than Left;
373 -- otherwise, it returns True.
375 function Key (Position : Cursor) return Key_Type;
376 -- Equivalent to Key (Element (Position))
378 function Element (Container : Set; Key : Key_Type) return Element_Type;
379 -- Equivalent to Element (Find (Container, Key))
381 procedure Exclude (Container : in out Set; Key : Key_Type);
382 -- Deletes from Container any elements whose key is equivalent to Key
384 procedure Delete (Container : in out Set; Key : Key_Type);
385 -- Deletes from Container any elements whose key is equivalent to
386 -- Key. If there are no such elements, then it raises Constraint_Error.
388 function Find (Container : Set; Key : Key_Type) return Cursor;
389 -- Returns a cursor designating the first element in Container whose key
390 -- is equivalent to Key. If there is no equivalent element, it returns
391 -- No_Element.
393 function Floor (Container : Set; Key : Key_Type) return Cursor;
394 -- If Container is empty, the function returns No_Element. If Item is
395 -- equivalent to the keys of elements in Container, it returns a cursor
396 -- designating the first such element. Otherwise, it returns a cursor
397 -- designating the largest element whose key is less than Item, or
398 -- No_Element if all keys are greater than Item.
400 function Ceiling (Container : Set; Key : Key_Type) return Cursor;
401 -- If Container is empty, the function returns No_Element. If Item is
402 -- equivalent to the keys of elements of Container, it returns a cursor
403 -- designating the last such element. Otherwise, it returns a cursor
404 -- designating the smallest element whose key is greater than Item, or
405 -- No_Element if all keys are less than Item.
407 function Contains (Container : Set; Key : Key_Type) return Boolean;
408 -- Equivalent to Find (Container, Key) /= No_Element
410 procedure Update_Element -- Update_Element_Preserving_Key ???
411 (Container : in out Set;
412 Position : Cursor;
413 Process : not null access
414 procedure (Element : in out Element_Type));
415 -- If Position equals No_Element, then Constraint_Error is raised. If
416 -- Position is associated with a set object different from Container,
417 -- then Program_Error is raised. Otherwise, it makes a copy of the key
418 -- of the element designated by Position, and then calls Process with
419 -- the element as the parameter. Update_Element then compares the key
420 -- value obtained before calling Process to the key value obtained from
421 -- the element after calling Process. If the keys are equivalent then
422 -- the operation terminates. If Container is busy (cursor tampering has
423 -- been attempted), then Program_Error is raised. Otherwise, the node
424 -- is moved to its new position (in canonical order).
426 procedure Iterate
427 (Container : Set;
428 Key : Key_Type;
429 Process : not null access procedure (Position : Cursor));
430 -- Call Process with a cursor designating each element equivalent to
431 -- Key, in order from Floor (Container, Key) to
432 -- Ceiling (Container, Key).
434 procedure Reverse_Iterate
435 (Container : Set;
436 Key : Key_Type;
437 Process : not null access procedure (Position : Cursor));
438 -- Call Process with a cursor designating each element equivalent to
439 -- Key, in order from Ceiling (Container, Key) to
440 -- Floor (Container, Key).
442 end Generic_Keys;
444 private
446 pragma Inline (Next);
447 pragma Inline (Previous);
449 type Node_Type;
450 type Node_Access is access Node_Type;
452 type Element_Access is access Element_Type;
454 type Node_Type is limited record
455 Parent : Node_Access;
456 Left : Node_Access;
457 Right : Node_Access;
458 Color : Red_Black_Trees.Color_Type := Red_Black_Trees.Red;
459 Element : Element_Access;
460 end record;
462 package Tree_Types is new Red_Black_Trees.Generic_Tree_Types
463 (Node_Type,
464 Node_Access);
466 type Set is new Ada.Finalization.Controlled with record
467 Tree : Tree_Types.Tree_Type;
468 end record;
470 overriding procedure Adjust (Container : in out Set);
472 overriding procedure Finalize (Container : in out Set) renames Clear;
474 use Red_Black_Trees;
475 use Tree_Types;
476 use Ada.Finalization;
477 use Ada.Streams;
479 type Set_Access is access all Set;
480 for Set_Access'Storage_Size use 0;
482 -- In all predefined libraries the following type is controlled, for proper
483 -- management of tampering checks. For performance reason we omit this
484 -- machinery for multisets, which are used in a number of our tools.
486 type Reference_Control_Type is record
487 Container : Set_Access;
488 end record;
490 type Constant_Reference_Type
491 (Element : not null access constant Element_Type) is record
492 Control : Reference_Control_Type :=
493 raise Program_Error with "uninitialized reference";
494 -- The RM says, "The default initialization of an object of
495 -- type Constant_Reference_Type or Reference_Type propagates
496 -- Program_Error."
497 end record;
499 type Cursor is record
500 Container : Set_Access;
501 Node : Node_Access;
502 end record;
504 procedure Write
505 (Stream : not null access Root_Stream_Type'Class;
506 Item : Cursor);
508 for Cursor'Write use Write;
510 procedure Read
511 (Stream : not null access Root_Stream_Type'Class;
512 Item : out Cursor);
514 for Cursor'Read use Read;
516 No_Element : constant Cursor := Cursor'(null, null);
518 procedure Write
519 (Stream : not null access Root_Stream_Type'Class;
520 Container : Set);
522 for Set'Write use Write;
524 procedure Read
525 (Stream : not null access Root_Stream_Type'Class;
526 Container : out Set);
528 for Set'Read use Read;
530 procedure Read
531 (Stream : not null access Root_Stream_Type'Class;
532 Item : out Constant_Reference_Type);
534 for Constant_Reference_Type'Read use Read;
536 procedure Write
537 (Stream : not null access Root_Stream_Type'Class;
538 Item : Constant_Reference_Type);
540 for Constant_Reference_Type'Write use Write;
542 Empty_Set : constant Set :=
543 (Controlled with Tree => (First => null,
544 Last => null,
545 Root => null,
546 Length => 0,
547 Busy => 0,
548 Lock => 0));
550 type Iterator is new Limited_Controlled and
551 Set_Iterator_Interfaces.Reversible_Iterator with
552 record
553 Container : Set_Access;
554 Node : Node_Access;
555 end record;
557 overriding procedure Finalize (Object : in out Iterator);
559 overriding function First (Object : Iterator) return Cursor;
560 overriding function Last (Object : Iterator) return Cursor;
562 overriding function Next
563 (Object : Iterator;
564 Position : Cursor) return Cursor;
566 overriding function Previous
567 (Object : Iterator;
568 Position : Cursor) return Cursor;
570 end Ada.Containers.Indefinite_Ordered_Multisets;