re PR bootstrap/51346 (LTO bootstrap failed with bootstrap-profiled)
[official-gcc.git] / gcc / ada / a-ciormu.ads
blobc1d81d5b753ef0dc0f2ddac9ac188045644dede6
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- ADA.CONTAINERS.INDEFINITE_ORDERED_MULTISETS --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 2004-2011, 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;
39 generic
40 type Element_Type (<>) is private;
42 with function "<" (Left, Right : Element_Type) return Boolean is <>;
43 with function "=" (Left, Right : Element_Type) return Boolean is <>;
45 package Ada.Containers.Indefinite_Ordered_Multisets is
46 pragma Preelaborate;
47 pragma Remote_Types;
49 function Equivalent_Elements (Left, Right : Element_Type) return Boolean;
50 -- Returns False if Left is less than Right, or Right is less than Left;
51 -- otherwise, it returns True.
53 type Set is tagged private;
54 pragma Preelaborable_Initialization (Set);
56 type Cursor is private;
57 pragma Preelaborable_Initialization (Cursor);
59 Empty_Set : constant Set;
60 -- The default value for set objects declared without an explicit
61 -- initialization expression.
63 No_Element : constant Cursor;
64 -- The default value for cursor objects declared without an explicit
65 -- initialization expression.
67 function "=" (Left, Right : Set) return Boolean;
68 -- If Left denotes the same set object as Right, then equality returns
69 -- True. If the length of Left is different from the length of Right, then
70 -- it returns False. Otherwise, set equality iterates over Left and Right,
71 -- comparing the element of Left to the element of Right using the equality
72 -- operator for elements. If the elements compare False, then the iteration
73 -- terminates and set equality returns False. Otherwise, if all elements
74 -- compare True, then set equality returns True.
76 function Equivalent_Sets (Left, Right : Set) return Boolean;
77 -- Similar to set equality, but with the difference that elements are
78 -- compared for equivalence instead of equality.
80 function To_Set (New_Item : Element_Type) return Set;
81 -- Constructs a set object with New_Item as its single element
83 function Length (Container : Set) return Count_Type;
84 -- Returns the total number of elements in Container
86 function Is_Empty (Container : Set) return Boolean;
87 -- Returns True if Container.Length is 0
89 procedure Clear (Container : in out Set);
90 -- Deletes all elements from Container
92 function Element (Position : Cursor) return Element_Type;
93 -- If Position equals No_Element, then Constraint_Error is raised.
94 -- Otherwise, function Element returns the element designed by Position.
96 procedure Replace_Element
97 (Container : in out Set;
98 Position : Cursor;
99 New_Item : Element_Type);
100 -- If Position equals No_Element, then Constraint_Error is raised. If
101 -- Position is associated with a set different from Container, then
102 -- Program_Error is raised. If New_Item is equivalent to the element
103 -- designated by Position, then if Container is locked (element tampering
104 -- has been attempted), Program_Error is raised; otherwise, the element
105 -- designated by Position is assigned the value of New_Item. If New_Item is
106 -- not equivalent to the element designated by Position, then if the
107 -- container is busy (cursor tampering has been attempted), Program_Error
108 -- is raised; otherwise, the element designed by Position is assigned the
109 -- value of New_Item, and the node is moved to its new position (in
110 -- canonical insertion order).
112 procedure Query_Element
113 (Position : Cursor;
114 Process : not null access procedure (Element : Element_Type));
115 -- If Position equals No_Element, then Constraint_Error is
116 -- raised. Otherwise, it calls Process with the element designated by
117 -- Position as the parameter. This call locks the container, so attempts to
118 -- change the value of the element while Process is executing (to "tamper
119 -- with elements") will raise Program_Error.
121 procedure Assign (Target : in out Set; Source : Set);
123 function Copy (Source : Set) return Set;
125 procedure Move (Target : in out Set; Source : in out Set);
126 -- If Target denotes the same object as Source, the operation does
127 -- nothing. If either Target or Source is busy (cursor tampering is
128 -- attempted), then it raises Program_Error. Otherwise, Target is cleared,
129 -- and the nodes from Source are moved (not copied) to Target (so Source
130 -- becomes empty).
132 procedure Insert
133 (Container : in out Set;
134 New_Item : Element_Type;
135 Position : out Cursor);
136 -- Insert adds New_Item to Container, and returns cursor Position
137 -- designating the newly inserted node. The node is inserted after any
138 -- existing elements less than or equivalent to New_Item (and before any
139 -- elements greater than New_Item). Note that the issue of where the new
140 -- node is inserted relative to equivalent elements does not arise for
141 -- unique-key containers, since in that case the insertion would simply
142 -- fail. For a multiple-key container (the case here), insertion always
143 -- succeeds, and is defined such that the new item is positioned after any
144 -- equivalent elements already in the container.
146 procedure Insert (Container : in out Set; New_Item : Element_Type);
147 -- Inserts New_Item in Container, but does not return a cursor designating
148 -- the newly-inserted node.
150 -- TODO: include Replace too???
152 -- procedure Replace
153 -- (Container : in out Set;
154 -- New_Item : Element_Type);
156 procedure Exclude (Container : in out Set; Item : Element_Type);
157 -- Deletes from Container all of the elements equivalent to Item
159 procedure Delete (Container : in out Set; Item : Element_Type);
160 -- Deletes from Container all of the elements equivalent to Item. If there
161 -- are no elements equivalent to Item, then it raises Constraint_Error.
163 procedure Delete (Container : in out Set; Position : in out Cursor);
164 -- If Position equals No_Element, then Constraint_Error is raised. If
165 -- Position is associated with a set different from Container, then
166 -- Program_Error is raised. Otherwise, the node designated by Position is
167 -- removed from Container, and Position is set to No_Element.
169 procedure Delete_First (Container : in out Set);
170 -- Removes the first node from Container
172 procedure Delete_Last (Container : in out Set);
173 -- Removes the last node from Container
175 procedure Union (Target : in out Set; Source : Set);
176 -- If Target is busy (cursor tampering is attempted), then Program_Error is
177 -- raised. Otherwise, it inserts each element of Source into Target.
178 -- Elements are inserted in the canonical order for multisets, such that
179 -- the elements from Source are inserted after equivalent elements already
180 -- in Target.
182 function Union (Left, Right : Set) return Set;
183 -- Returns a set comprising the all elements from Left and all of the
184 -- elements from Right. The elements from Right follow the equivalent
185 -- elements from Left.
187 function "or" (Left, Right : Set) return Set renames Union;
189 procedure Intersection (Target : in out Set; Source : Set);
190 -- If Target denotes the same object as Source, the operation does
191 -- nothing. If Target is busy (cursor tampering is attempted),
192 -- Program_Error is raised. Otherwise, the elements in Target having no
193 -- equivalent element in Source are deleted from Target.
195 function Intersection (Left, Right : Set) return Set;
196 -- If Left denotes the same object as Right, then the function returns a
197 -- copy of Left. Otherwise, it returns a set comprising the equivalent
198 -- elements from both Left and Right. Items are inserted in the result set
199 -- in canonical order, such that the elements from Left precede the
200 -- equivalent elements from Right.
202 function "and" (Left, Right : Set) return Set renames Intersection;
204 procedure Difference (Target : in out Set; Source : Set);
205 -- If Target is busy (cursor tampering is attempted), then Program_Error is
206 -- raised. Otherwise, the elements in Target that are equivalent to
207 -- elements in Source are deleted from Target.
209 function Difference (Left, Right : Set) return Set;
210 -- Returns a set comprising the elements from Left that have no equivalent
211 -- element in Right.
213 function "-" (Left, Right : Set) return Set renames Difference;
215 procedure Symmetric_Difference (Target : in out Set; Source : Set);
216 -- If Target is busy, then Program_Error is raised. Otherwise, the elements
217 -- in Target equivalent to elements in Source are deleted from Target, and
218 -- the elements in Source not equivalent to elements in Target are inserted
219 -- into Target.
221 function Symmetric_Difference (Left, Right : Set) return Set;
222 -- Returns a set comprising the union of the elements from Target having no
223 -- equivalent in Source, and the elements of Source having no equivalent in
224 -- Target.
226 function "xor" (Left, Right : Set) return Set renames Symmetric_Difference;
228 function Overlap (Left, Right : Set) return Boolean;
229 -- Returns True if Left contains an element equivalent to an element of
230 -- Right.
232 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean;
233 -- Returns True if every element in Subset has an equivalent element in
234 -- Of_Set.
236 function First (Container : Set) return Cursor;
237 -- If Container is empty, the function returns No_Element. Otherwise, it
238 -- returns a cursor designating the smallest element.
240 function First_Element (Container : Set) return Element_Type;
241 -- Equivalent to Element (First (Container))
243 function Last (Container : Set) return Cursor;
244 -- If Container is empty, the function returns No_Element. Otherwise, it
245 -- returns a cursor designating the largest element.
247 function Last_Element (Container : Set) return Element_Type;
248 -- Equivalent to Element (Last (Container))
250 function Next (Position : Cursor) return Cursor;
251 -- If Position equals No_Element or Last (Container), the function returns
252 -- No_Element. Otherwise, it returns a cursor designating the node that
253 -- immediately follows (as per the insertion order) the node designated by
254 -- Position.
256 procedure Next (Position : in out Cursor);
257 -- Equivalent to Position := Next (Position)
259 function Previous (Position : Cursor) return Cursor;
260 -- If Position equals No_Element or First (Container), the function returns
261 -- No_Element. Otherwise, it returns a cursor designating the node that
262 -- immediately precedes (as per the insertion order) the node designated by
263 -- Position.
265 procedure Previous (Position : in out Cursor);
266 -- Equivalent to Position := Previous (Position)
268 function Find (Container : Set; Item : Element_Type) return Cursor;
269 -- Returns a cursor designating the first element in Container equivalent
270 -- to Item. If there is no equivalent element, it returns No_Element.
272 function Floor (Container : Set; Item : Element_Type) return Cursor;
273 -- If Container is empty, the function returns No_Element. If Item is
274 -- equivalent to elements in Container, it returns a cursor designating the
275 -- first equivalent element. Otherwise, it returns a cursor designating the
276 -- largest element less than Item, or No_Element if all elements are
277 -- greater than Item.
279 function Ceiling (Container : Set; Item : Element_Type) return Cursor;
280 -- If Container is empty, the function returns No_Element. If Item is
281 -- equivalent to elements of Container, it returns a cursor designating the
282 -- last equivalent element. Otherwise, it returns a cursor designating the
283 -- smallest element greater than Item, or No_Element if all elements are
284 -- less than Item.
286 function Contains (Container : Set; Item : Element_Type) return Boolean;
287 -- Equivalent to Container.Find (Item) /= No_Element
289 function Has_Element (Position : Cursor) return Boolean;
290 -- Equivalent to Position /= No_Element
292 function "<" (Left, Right : Cursor) return Boolean;
293 -- Equivalent to Element (Left) < Element (Right)
295 function ">" (Left, Right : Cursor) return Boolean;
296 -- Equivalent to Element (Right) < Element (Left)
298 function "<" (Left : Cursor; Right : Element_Type) return Boolean;
299 -- Equivalent to Element (Left) < Right
301 function ">" (Left : Cursor; Right : Element_Type) return Boolean;
302 -- Equivalent to Right < Element (Left)
304 function "<" (Left : Element_Type; Right : Cursor) return Boolean;
305 -- Equivalent to Left < Element (Right)
307 function ">" (Left : Element_Type; Right : Cursor) return Boolean;
308 -- Equivalent to Element (Right) < Left
310 procedure Iterate
311 (Container : Set;
312 Process : not null access procedure (Position : Cursor));
313 -- Calls Process with a cursor designating each element of Container, in
314 -- order from Container.First to Container.Last.
316 procedure Reverse_Iterate
317 (Container : Set;
318 Process : not null access procedure (Position : Cursor));
319 -- Calls Process with a cursor designating each element of Container, in
320 -- order from Container.Last to Container.First.
322 procedure Iterate
323 (Container : Set;
324 Item : Element_Type;
325 Process : not null access procedure (Position : Cursor));
326 -- Call Process with a cursor designating each element equivalent to Item,
327 -- in order from Container.Floor (Item) to Container.Ceiling (Item).
329 procedure Reverse_Iterate
330 (Container : Set;
331 Item : Element_Type;
332 Process : not null access procedure (Position : Cursor));
333 -- Call Process with a cursor designating each element equivalent to Item,
334 -- in order from Container.Ceiling (Item) to Container.Floor (Item).
336 generic
337 type Key_Type (<>) is private;
339 with function Key (Element : Element_Type) return Key_Type;
341 with function "<" (Left, Right : Key_Type) return Boolean is <>;
343 package Generic_Keys is
345 function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
346 -- Returns False if Left is less than Right, or Right is less than Left;
347 -- otherwise, it returns True.
349 function Key (Position : Cursor) return Key_Type;
350 -- Equivalent to Key (Element (Position))
352 function Element (Container : Set; Key : Key_Type) return Element_Type;
353 -- Equivalent to Element (Find (Container, Key))
355 procedure Exclude (Container : in out Set; Key : Key_Type);
356 -- Deletes from Container any elements whose key is equivalent to Key
358 procedure Delete (Container : in out Set; Key : Key_Type);
359 -- Deletes from Container any elements whose key is equivalent to
360 -- Key. If there are no such elements, then it raises Constraint_Error.
362 function Find (Container : Set; Key : Key_Type) return Cursor;
363 -- Returns a cursor designating the first element in Container whose key
364 -- is equivalent to Key. If there is no equivalent element, it returns
365 -- No_Element.
367 function Floor (Container : Set; Key : Key_Type) return Cursor;
368 -- If Container is empty, the function returns No_Element. If Item is
369 -- equivalent to the keys of elements in Container, it returns a cursor
370 -- designating the first such element. Otherwise, it returns a cursor
371 -- designating the largest element whose key is less than Item, or
372 -- No_Element if all keys are greater than Item.
374 function Ceiling (Container : Set; Key : Key_Type) return Cursor;
375 -- If Container is empty, the function returns No_Element. If Item is
376 -- equivalent to the keys of elements of Container, it returns a cursor
377 -- designating the last such element. Otherwise, it returns a cursor
378 -- designating the smallest element whose key is greater than Item, or
379 -- No_Element if all keys are less than Item.
381 function Contains (Container : Set; Key : Key_Type) return Boolean;
382 -- Equivalent to Find (Container, Key) /= No_Element
384 procedure Update_Element -- Update_Element_Preserving_Key ???
385 (Container : in out Set;
386 Position : Cursor;
387 Process : not null access
388 procedure (Element : in out Element_Type));
389 -- If Position equals No_Element, then Constraint_Error is raised. If
390 -- Position is associated with a set object different from Container,
391 -- then Program_Error is raised. Otherwise, it makes a copy of the key
392 -- of the element designated by Position, and then calls Process with
393 -- the element as the parameter. Update_Element then compares the key
394 -- value obtained before calling Process to the key value obtained from
395 -- the element after calling Process. If the keys are equivalent then
396 -- the operation terminates. If Container is busy (cursor tampering has
397 -- been attempted), then Program_Error is raised. Otherwise, the node
398 -- is moved to its new position (in canonical order).
400 procedure Iterate
401 (Container : Set;
402 Key : Key_Type;
403 Process : not null access procedure (Position : Cursor));
404 -- Call Process with a cursor designating each element equivalent to
405 -- Key, in order from Floor (Container, Key) to
406 -- Ceiling (Container, Key).
408 procedure Reverse_Iterate
409 (Container : Set;
410 Key : Key_Type;
411 Process : not null access procedure (Position : Cursor));
412 -- Call Process with a cursor designating each element equivalent to
413 -- Key, in order from Ceiling (Container, Key) to
414 -- Floor (Container, Key).
416 end Generic_Keys;
418 private
420 pragma Inline (Next);
421 pragma Inline (Previous);
423 type Node_Type;
424 type Node_Access is access Node_Type;
426 type Element_Access is access Element_Type;
428 type Node_Type is limited record
429 Parent : Node_Access;
430 Left : Node_Access;
431 Right : Node_Access;
432 Color : Red_Black_Trees.Color_Type := Red_Black_Trees.Red;
433 Element : Element_Access;
434 end record;
436 package Tree_Types is new Red_Black_Trees.Generic_Tree_Types
437 (Node_Type,
438 Node_Access);
440 type Set is new Ada.Finalization.Controlled with record
441 Tree : Tree_Types.Tree_Type;
442 end record;
444 overriding procedure Adjust (Container : in out Set);
446 overriding procedure Finalize (Container : in out Set) renames Clear;
448 use Red_Black_Trees;
449 use Tree_Types;
450 use Ada.Finalization;
451 use Ada.Streams;
453 type Set_Access is access all Set;
454 for Set_Access'Storage_Size use 0;
456 type Cursor is record
457 Container : Set_Access;
458 Node : Node_Access;
459 end record;
461 procedure Write
462 (Stream : not null access Root_Stream_Type'Class;
463 Item : Cursor);
465 for Cursor'Write use Write;
467 procedure Read
468 (Stream : not null access Root_Stream_Type'Class;
469 Item : out Cursor);
471 for Cursor'Read use Read;
473 No_Element : constant Cursor := Cursor'(null, null);
475 procedure Write
476 (Stream : not null access Root_Stream_Type'Class;
477 Container : Set);
479 for Set'Write use Write;
481 procedure Read
482 (Stream : not null access Root_Stream_Type'Class;
483 Container : out Set);
485 for Set'Read use Read;
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));
495 end Ada.Containers.Indefinite_Ordered_Multisets;