PR preprocessor/63831
[official-gcc.git] / gcc / ada / a-coormu.ads
blob8d684741e9452b270751b6d07a5f2d3877c85a33
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-2013, 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 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.
34 private with Ada.Containers.Red_Black_Trees;
35 private with Ada.Finalization;
36 private with Ada.Streams;
37 with Ada.Iterator_Interfaces;
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.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 with Default_Iterator => Iterate,
55 Iterator_Element => Element_Type;
57 pragma Preelaborable_Initialization (Set);
59 type Cursor is private;
60 pragma Preelaborable_Initialization (Cursor);
62 Empty_Set : constant Set;
63 -- The default value for set objects declared without an explicit
64 -- initialization expression.
66 No_Element : constant Cursor;
67 -- The default value for cursor objects declared without an explicit
68 -- initialization expression.
70 function Has_Element (Position : Cursor) return Boolean;
71 -- Equivalent to Position /= No_Element
73 package Set_Iterator_Interfaces is new
74 Ada.Iterator_Interfaces (Cursor, Has_Element);
76 function "=" (Left, Right : Set) return Boolean;
77 -- If Left denotes the same set object as Right, then equality returns
78 -- True. If the length of Left is different from the length of Right, then
79 -- it returns False. Otherwise, set equality iterates over Left and Right,
80 -- comparing the element of Left to the element of Right using the equality
81 -- operator for elements. If the elements compare False, then the iteration
82 -- terminates and set equality returns False. Otherwise, if all elements
83 -- compare True, then set equality returns True.
85 function Equivalent_Sets (Left, Right : Set) return Boolean;
86 -- Similar to set equality, but with the difference that elements are
87 -- compared for equivalence instead of equality.
89 function To_Set (New_Item : Element_Type) return Set;
90 -- Constructs a set object with New_Item as its single element
92 function Length (Container : Set) return Count_Type;
93 -- Returns the total number of elements in Container
95 function Is_Empty (Container : Set) return Boolean;
96 -- Returns True if Container.Length is 0
98 procedure Clear (Container : in out Set);
99 -- Deletes all elements from Container
101 function Element (Position : Cursor) return Element_Type;
102 -- If Position equals No_Element, then Constraint_Error is raised.
103 -- Otherwise, function Element returns the element designed by Position.
105 procedure Replace_Element
106 (Container : in out Set;
107 Position : Cursor;
108 New_Item : Element_Type);
109 -- If Position equals No_Element, then Constraint_Error is raised. If
110 -- Position is associated with a set different from Container, then
111 -- Program_Error is raised. If New_Item is equivalent to the element
112 -- designated by Position, then if Container is locked (element tampering
113 -- has been attempted), Program_Error is raised; otherwise, the element
114 -- designated by Position is assigned the value of New_Item. If New_Item is
115 -- not equivalent to the element designated by Position, then if the
116 -- container is busy (cursor tampering has been attempted), Program_Error
117 -- is raised; otherwise, the element designed by Position is assigned the
118 -- value of New_Item, and the node is moved to its new position (in
119 -- canonical insertion order).
121 procedure Query_Element
122 (Position : Cursor;
123 Process : not null access procedure (Element : Element_Type));
124 -- If Position equals No_Element, then Constraint_Error is
125 -- raised. Otherwise, it calls Process with the element designated by
126 -- Position as the parameter. This call locks the container, so attempts to
127 -- change the value of the element while Process is executing (to "tamper
128 -- with elements") will raise Program_Error.
130 procedure Assign (Target : in out Set; Source : Set);
132 function Copy (Source : Set) return Set;
134 procedure Move (Target : in out Set; Source : in out Set);
135 -- If Target denotes the same object as Source, the operation does
136 -- nothing. If either Target or Source is busy (cursor tampering is
137 -- attempted), then it raises Program_Error. Otherwise, Target is cleared,
138 -- and the nodes from Source are moved (not copied) to Target (so Source
139 -- becomes empty).
141 procedure Insert
142 (Container : in out Set;
143 New_Item : Element_Type;
144 Position : out Cursor);
145 -- Insert adds New_Item to Container, and returns cursor Position
146 -- designating the newly inserted node. The node is inserted after any
147 -- existing elements less than or equivalent to New_Item (and before any
148 -- elements greater than New_Item). Note that the issue of where the new
149 -- node is inserted relative to equivalent elements does not arise for
150 -- unique-key containers, since in that case the insertion would simply
151 -- fail. For a multiple-key container (the case here), insertion always
152 -- succeeds, and is defined such that the new item is positioned after any
153 -- equivalent elements already in the container.
155 procedure Insert
156 (Container : in out Set;
157 New_Item : Element_Type);
158 -- Inserts New_Item in Container, but does not return a cursor designating
159 -- the newly-inserted node.
161 -- TODO: include Replace too???
163 -- procedure Replace
164 -- (Container : in out Set;
165 -- New_Item : Element_Type);
167 procedure Exclude
168 (Container : in out Set;
169 Item : Element_Type);
170 -- Deletes from Container all of the elements equivalent to Item
172 procedure Delete
173 (Container : in out Set;
174 Item : Element_Type);
175 -- Deletes from Container all of the elements equivalent to Item. If there
176 -- are no elements equivalent to Item, then it raises Constraint_Error.
178 procedure Delete
179 (Container : in out Set;
180 Position : in out Cursor);
181 -- If Position equals No_Element, then Constraint_Error is raised. If
182 -- Position is associated with a set different from Container, then
183 -- Program_Error is raised. Otherwise, the node designated by Position is
184 -- removed from Container, and Position is set to No_Element.
186 procedure Delete_First (Container : in out Set);
187 -- Removes the first node from Container
189 procedure Delete_Last (Container : in out Set);
190 -- Removes the last node from Container
192 procedure Union (Target : in out Set; Source : Set);
193 -- If Target is busy (cursor tampering is attempted), the Program_Error is
194 -- raised. Otherwise, it inserts each element of Source into
195 -- Target. Elements are inserted in the canonical order for multisets, such
196 -- that the elements from Source are inserted after equivalent elements
197 -- already in Target.
199 function Union (Left, Right : Set) return Set;
200 -- Returns a set comprising the all elements from Left and all of the
201 -- elements from Right. The elements from Right follow the equivalent
202 -- elements from Left.
204 function "or" (Left, Right : Set) return Set renames Union;
206 procedure Intersection (Target : in out Set; Source : Set);
207 -- If Target denotes the same object as Source, the operation does
208 -- nothing. If Target is busy (cursor tampering is attempted),
209 -- Program_Error is raised. Otherwise, the elements in Target having no
210 -- equivalent element in Source are deleted from Target.
212 function Intersection (Left, Right : Set) return Set;
213 -- If Left denotes the same object as Right, then the function returns a
214 -- copy of Left. Otherwise, it returns a set comprising the equivalent
215 -- elements from both Left and Right. Items are inserted in the result set
216 -- in canonical order, such that the elements from Left precede the
217 -- equivalent elements from Right.
219 function "and" (Left, Right : Set) return Set renames Intersection;
221 procedure Difference (Target : in out Set; Source : Set);
222 -- If Target is busy (cursor tampering is attempted), then Program_Error is
223 -- raised. Otherwise, the elements in Target that are equivalent to
224 -- elements in Source are deleted from Target.
226 function Difference (Left, Right : Set) return Set;
227 -- Returns a set comprising the elements from Left that have no equivalent
228 -- element in Right.
230 function "-" (Left, Right : Set) return Set renames Difference;
232 procedure Symmetric_Difference (Target : in out Set; Source : Set);
233 -- If Target is busy, then Program_Error is raised. Otherwise, the elements
234 -- in Target equivalent to elements in Source are deleted from Target, and
235 -- the elements in Source not equivalent to elements in Target are inserted
236 -- into Target.
238 function Symmetric_Difference (Left, Right : Set) return Set;
239 -- Returns a set comprising the union of the elements from Target having no
240 -- equivalent in Source, and the elements of Source having no equivalent in
241 -- Target.
243 function "xor" (Left, Right : Set) return Set renames Symmetric_Difference;
245 function Overlap (Left, Right : Set) return Boolean;
246 -- Returns True if Left contains an element equivalent to an element of
247 -- Right.
249 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean;
250 -- Returns True if every element in Subset has an equivalent element in
251 -- Of_Set.
253 function First (Container : Set) return Cursor;
254 -- If Container is empty, the function returns No_Element. Otherwise, it
255 -- returns a cursor designating the smallest element.
257 function First_Element (Container : Set) return Element_Type;
258 -- Equivalent to Element (First (Container))
260 function Last (Container : Set) return Cursor;
261 -- If Container is empty, the function returns No_Element. Otherwise, it
262 -- returns a cursor designating the largest element.
264 function Last_Element (Container : Set) return Element_Type;
265 -- Equivalent to Element (Last (Container))
267 function Next (Position : Cursor) return Cursor;
268 -- If Position equals No_Element or Last (Container), the function returns
269 -- No_Element. Otherwise, it returns a cursor designating the node that
270 -- immediately follows (as per the insertion order) the node designated by
271 -- Position.
273 procedure Next (Position : in out Cursor);
274 -- Equivalent to Position := Next (Position)
276 function Previous (Position : Cursor) return Cursor;
277 -- If Position equals No_Element or First (Container), the function returns
278 -- No_Element. Otherwise, it returns a cursor designating the node that
279 -- immediately precedes (as per the insertion order) the node designated by
280 -- Position.
282 procedure Previous (Position : in out Cursor);
283 -- Equivalent to Position := Previous (Position)
285 function Find (Container : Set; Item : Element_Type) return Cursor;
286 -- Returns a cursor designating the first element in Container equivalent
287 -- to Item. If there is no equivalent element, it returns No_Element.
289 function Floor (Container : Set; Item : Element_Type) return Cursor;
290 -- If Container is empty, the function returns No_Element. If Item is
291 -- equivalent to elements in Container, it returns a cursor designating the
292 -- first equivalent element. Otherwise, it returns a cursor designating the
293 -- largest element less than Item, or No_Element if all elements are
294 -- greater than Item.
296 function Ceiling (Container : Set; Item : Element_Type) return Cursor;
297 -- If Container is empty, the function returns No_Element. If Item is
298 -- equivalent to elements of Container, it returns a cursor designating the
299 -- last equivalent element. Otherwise, it returns a cursor designating the
300 -- smallest element greater than Item, or No_Element if all elements are
301 -- less than Item.
303 function Contains (Container : Set; Item : Element_Type) return Boolean;
304 -- Equivalent to Container.Find (Item) /= No_Element
306 function "<" (Left, Right : Cursor) return Boolean;
307 -- Equivalent to Element (Left) < Element (Right)
309 function ">" (Left, Right : Cursor) return Boolean;
310 -- Equivalent to Element (Right) < Element (Left)
312 function "<" (Left : Cursor; Right : Element_Type) return Boolean;
313 -- Equivalent to Element (Left) < Right
315 function ">" (Left : Cursor; Right : Element_Type) return Boolean;
316 -- Equivalent to Right < Element (Left)
318 function "<" (Left : Element_Type; Right : Cursor) return Boolean;
319 -- Equivalent to Left < Element (Right)
321 function ">" (Left : Element_Type; Right : Cursor) return Boolean;
322 -- Equivalent to Element (Right) < Left
324 procedure Iterate
325 (Container : Set;
326 Process : not null access procedure (Position : Cursor));
327 -- Calls Process with a cursor designating each element of Container, in
328 -- order from Container.First to Container.Last.
330 procedure Reverse_Iterate
331 (Container : Set;
332 Process : not null access procedure (Position : Cursor));
333 -- Calls Process with a cursor designating each element of Container, in
334 -- order from Container.Last to Container.First.
336 procedure Iterate
337 (Container : Set;
338 Item : Element_Type;
339 Process : not null access procedure (Position : Cursor));
340 -- Call Process with a cursor designating each element equivalent to Item,
341 -- in order from Container.Floor (Item) to Container.Ceiling (Item).
343 procedure Reverse_Iterate
344 (Container : Set;
345 Item : Element_Type;
346 Process : not null access procedure (Position : Cursor));
347 -- Call Process with a cursor designating each element equivalent to Item,
348 -- in order from Container.Ceiling (Item) to Container.Floor (Item).
350 function Iterate
351 (Container : Set)
352 return Set_Iterator_Interfaces.Reversible_Iterator'class;
354 function Iterate
355 (Container : Set;
356 Start : Cursor)
357 return Set_Iterator_Interfaces.Reversible_Iterator'class;
359 generic
360 type Key_Type (<>) is private;
362 with function Key (Element : Element_Type) return Key_Type;
364 with function "<" (Left, Right : Key_Type) return Boolean is <>;
366 package Generic_Keys is
368 function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
369 -- Returns False if Left is less than Right, or Right is less than Left;
370 -- otherwise, it returns True.
372 function Key (Position : Cursor) return Key_Type;
373 -- Equivalent to Key (Element (Position))
375 function Element (Container : Set; Key : Key_Type) return Element_Type;
376 -- Equivalent to Element (Find (Container, Key))
378 procedure Exclude (Container : in out Set; Key : Key_Type);
379 -- Deletes from Container any elements whose key is equivalent to Key
381 procedure Delete (Container : in out Set; Key : Key_Type);
382 -- Deletes from Container any elements whose key is equivalent to
383 -- Key. If there are no such elements, then it raises Constraint_Error.
385 function Find (Container : Set; Key : Key_Type) return Cursor;
386 -- Returns a cursor designating the first element in Container whose key
387 -- is equivalent to Key. If there is no equivalent element, it returns
388 -- No_Element.
390 function Floor (Container : Set; Key : Key_Type) return Cursor;
391 -- If Container is empty, the function returns No_Element. If Item is
392 -- equivalent to the keys of elements in Container, it returns a cursor
393 -- designating the first such element. Otherwise, it returns a cursor
394 -- designating the largest element whose key is less than Item, or
395 -- No_Element if all keys are greater than Item.
397 function Ceiling (Container : Set; Key : Key_Type) return Cursor;
398 -- If Container is empty, the function returns No_Element. If Item is
399 -- equivalent to the keys of elements of Container, it returns a cursor
400 -- designating the last such element. Otherwise, it returns a cursor
401 -- designating the smallest element whose key is greater than Item, or
402 -- No_Element if all keys are less than Item.
404 function Contains (Container : Set; Key : Key_Type) return Boolean;
405 -- Equivalent to Find (Container, Key) /= No_Element
407 procedure Update_Element -- Update_Element_Preserving_Key ???
408 (Container : in out Set;
409 Position : Cursor;
410 Process : not null access
411 procedure (Element : in out Element_Type));
412 -- If Position equals No_Element, then Constraint_Error is raised. If
413 -- Position is associated with a set object different from Container,
414 -- then Program_Error is raised. Otherwise, it makes a copy of the key
415 -- of the element designated by Position, and then calls Process with
416 -- the element as the parameter. Update_Element then compares the key
417 -- value obtained before calling Process to the key value obtained from
418 -- the element after calling Process. If the keys are equivalent then
419 -- the operation terminates. If Container is busy (cursor tampering has
420 -- been attempted), then Program_Error is raised. Otherwise, the node
421 -- is moved to its new position (in canonical order).
423 procedure Iterate
424 (Container : Set;
425 Key : Key_Type;
426 Process : not null access procedure (Position : Cursor));
427 -- Call Process with a cursor designating each element equivalent to
428 -- Key, in order from Floor (Container, Key) to
429 -- Ceiling (Container, Key).
431 procedure Reverse_Iterate
432 (Container : Set;
433 Key : Key_Type;
434 Process : not null access procedure (Position : Cursor));
435 -- Call Process with a cursor designating each element equivalent to
436 -- Key, in order from Ceiling (Container, Key) to
437 -- Floor (Container, Key).
439 end Generic_Keys;
441 private
443 pragma Inline (Next);
444 pragma Inline (Previous);
446 type Node_Type;
447 type Node_Access is access Node_Type;
449 type Node_Type is limited record
450 Parent : Node_Access;
451 Left : Node_Access;
452 Right : Node_Access;
453 Color : Red_Black_Trees.Color_Type := Red_Black_Trees.Red;
454 Element : Element_Type;
455 end record;
457 package Tree_Types is
458 new Red_Black_Trees.Generic_Tree_Types (Node_Type, Node_Access);
460 type Set is new Ada.Finalization.Controlled with record
461 Tree : Tree_Types.Tree_Type;
462 end record;
464 overriding procedure Adjust (Container : in out Set);
466 overriding procedure Finalize (Container : in out Set) renames Clear;
468 use Red_Black_Trees;
469 use Tree_Types;
470 use Ada.Finalization;
471 use Ada.Streams;
473 type Set_Access is access all Set;
474 for Set_Access'Storage_Size use 0;
476 type Cursor is record
477 Container : Set_Access;
478 Node : Node_Access;
479 end record;
481 procedure Write
482 (Stream : not null access Root_Stream_Type'Class;
483 Item : Cursor);
485 for Cursor'Write use Write;
487 procedure Read
488 (Stream : not null access Root_Stream_Type'Class;
489 Item : out Cursor);
491 for Cursor'Read use Read;
493 No_Element : constant Cursor := Cursor'(null, null);
495 procedure Write
496 (Stream : not null access Root_Stream_Type'Class;
497 Container : Set);
499 for Set'Write use Write;
501 procedure Read
502 (Stream : not null access Root_Stream_Type'Class;
503 Container : out Set);
505 for Set'Read use Read;
507 Empty_Set : constant Set :=
508 (Controlled with Tree => (First => null,
509 Last => null,
510 Root => null,
511 Length => 0,
512 Busy => 0,
513 Lock => 0));
515 type Iterator is new Limited_Controlled and
516 Set_Iterator_Interfaces.Reversible_Iterator with
517 record
518 Container : Set_Access;
519 Node : Node_Access;
520 end record;
522 overriding procedure Finalize (Object : in out Iterator);
524 overriding function First (Object : Iterator) return Cursor;
525 overriding function Last (Object : Iterator) return Cursor;
527 overriding function Next
528 (Object : Iterator;
529 Position : Cursor) return Cursor;
531 overriding function Previous
532 (Object : Iterator;
533 Position : Cursor) return Cursor;
535 end Ada.Containers.Ordered_Multisets;