1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
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 --
9 -- Copyright (C) 2004-2015, Free Software Foundation, Inc. --
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. --
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. --
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/>. --
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
;
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
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 Constant_Indexing
=> Constant_Reference
,
55 Default_Iterator
=> Iterate
,
56 Iterator_Element
=> Element_Type
;
58 pragma Preelaborable_Initialization
(Set
);
60 type Cursor
is private;
61 pragma Preelaborable_Initialization
(Cursor
);
63 Empty_Set
: constant Set
;
64 -- The default value for set objects declared without an explicit
65 -- initialization expression.
67 No_Element
: constant Cursor
;
68 -- The default value for cursor objects declared without an explicit
69 -- initialization expression.
71 function Has_Element
(Position
: Cursor
) return Boolean;
72 -- Equivalent to Position /= No_Element
74 package Set_Iterator_Interfaces
is new
75 Ada
.Iterator_Interfaces
(Cursor
, Has_Element
);
77 function "=" (Left
, Right
: Set
) return Boolean;
78 -- If Left denotes the same set object as Right, then equality returns
79 -- True. If the length of Left is different from the length of Right, then
80 -- it returns False. Otherwise, set equality iterates over Left and Right,
81 -- comparing the element of Left to the element of Right using the equality
82 -- operator for elements. If the elements compare False, then the iteration
83 -- terminates and set equality returns False. Otherwise, if all elements
84 -- compare True, then set equality returns True.
86 function Equivalent_Sets
(Left
, Right
: Set
) return Boolean;
87 -- Similar to set equality, but with the difference that elements are
88 -- compared for equivalence instead of equality.
90 function To_Set
(New_Item
: Element_Type
) return Set
;
91 -- Constructs a set object with New_Item as its single element
93 function Length
(Container
: Set
) return Count_Type
;
94 -- Returns the total number of elements in Container
96 function Is_Empty
(Container
: Set
) return Boolean;
97 -- Returns True if Container.Length is 0
99 procedure Clear
(Container
: in out Set
);
100 -- Deletes all elements from Container
102 function Element
(Position
: Cursor
) return Element_Type
;
103 -- If Position equals No_Element, then Constraint_Error is raised.
104 -- Otherwise, function Element returns the element designed by Position.
106 procedure Replace_Element
107 (Container
: in out Set
;
109 New_Item
: Element_Type
);
110 -- If Position equals No_Element, then Constraint_Error is raised. If
111 -- Position is associated with a set different from Container, then
112 -- Program_Error is raised. If New_Item is equivalent to the element
113 -- designated by Position, then if Container is locked (element tampering
114 -- has been attempted), Program_Error is raised; otherwise, the element
115 -- designated by Position is assigned the value of New_Item. If New_Item is
116 -- not equivalent to the element designated by Position, then if the
117 -- container is busy (cursor tampering has been attempted), Program_Error
118 -- is raised; otherwise, the element designed by Position is assigned the
119 -- value of New_Item, and the node is moved to its new position (in
120 -- canonical insertion order).
122 procedure Query_Element
124 Process
: not null access procedure (Element
: Element_Type
));
125 -- If Position equals No_Element, then Constraint_Error is
126 -- raised. Otherwise, it calls Process with the element designated by
127 -- Position as the parameter. This call locks the container, so attempts to
128 -- change the value of the element while Process is executing (to "tamper
129 -- with elements") will raise Program_Error.
131 type Constant_Reference_Type
132 (Element
: not null access constant Element_Type
) is private
133 with Implicit_Dereference
=> Element
;
135 function Constant_Reference
136 (Container
: aliased Set
;
137 Position
: Cursor
) return Constant_Reference_Type
;
138 pragma Inline
(Constant_Reference
);
140 procedure Assign
(Target
: in out Set
; Source
: Set
);
142 function Copy
(Source
: Set
) return Set
;
144 procedure Move
(Target
: in out Set
; Source
: in out Set
);
145 -- If Target denotes the same object as Source, the operation does
146 -- nothing. If either Target or Source is busy (cursor tampering is
147 -- attempted), then it raises Program_Error. Otherwise, Target is cleared,
148 -- and the nodes from Source are moved (not copied) to Target (so Source
152 (Container
: in out Set
;
153 New_Item
: Element_Type
;
154 Position
: out Cursor
);
155 -- Insert adds New_Item to Container, and returns cursor Position
156 -- designating the newly inserted node. The node is inserted after any
157 -- existing elements less than or equivalent to New_Item (and before any
158 -- elements greater than New_Item). Note that the issue of where the new
159 -- node is inserted relative to equivalent elements does not arise for
160 -- unique-key containers, since in that case the insertion would simply
161 -- fail. For a multiple-key container (the case here), insertion always
162 -- succeeds, and is defined such that the new item is positioned after any
163 -- equivalent elements already in the container.
166 (Container
: in out Set
;
167 New_Item
: Element_Type
);
168 -- Inserts New_Item in Container, but does not return a cursor designating
169 -- the newly-inserted node.
171 -- TODO: include Replace too???
174 -- (Container : in out Set;
175 -- New_Item : Element_Type);
178 (Container
: in out Set
;
179 Item
: Element_Type
);
180 -- Deletes from Container all of the elements equivalent to Item
183 (Container
: in out Set
;
184 Item
: Element_Type
);
185 -- Deletes from Container all of the elements equivalent to Item. If there
186 -- are no elements equivalent to Item, then it raises Constraint_Error.
189 (Container
: in out Set
;
190 Position
: in out Cursor
);
191 -- If Position equals No_Element, then Constraint_Error is raised. If
192 -- Position is associated with a set different from Container, then
193 -- Program_Error is raised. Otherwise, the node designated by Position is
194 -- removed from Container, and Position is set to No_Element.
196 procedure Delete_First
(Container
: in out Set
);
197 -- Removes the first node from Container
199 procedure Delete_Last
(Container
: in out Set
);
200 -- Removes the last node from Container
202 procedure Union
(Target
: in out Set
; Source
: Set
);
203 -- If Target is busy (cursor tampering is attempted), the Program_Error is
204 -- raised. Otherwise, it inserts each element of Source into
205 -- Target. Elements are inserted in the canonical order for multisets, such
206 -- that the elements from Source are inserted after equivalent elements
207 -- already in Target.
209 function Union
(Left
, Right
: Set
) return Set
;
210 -- Returns a set comprising the all elements from Left and all of the
211 -- elements from Right. The elements from Right follow the equivalent
212 -- elements from Left.
214 function "or" (Left
, Right
: Set
) return Set
renames Union
;
216 procedure Intersection
(Target
: in out Set
; Source
: Set
);
217 -- If Target denotes the same object as Source, the operation does
218 -- nothing. If Target is busy (cursor tampering is attempted),
219 -- Program_Error is raised. Otherwise, the elements in Target having no
220 -- equivalent element in Source are deleted from Target.
222 function Intersection
(Left
, Right
: Set
) return Set
;
223 -- If Left denotes the same object as Right, then the function returns a
224 -- copy of Left. Otherwise, it returns a set comprising the equivalent
225 -- elements from both Left and Right. Items are inserted in the result set
226 -- in canonical order, such that the elements from Left precede the
227 -- equivalent elements from Right.
229 function "and" (Left
, Right
: Set
) return Set
renames Intersection
;
231 procedure Difference
(Target
: in out Set
; Source
: Set
);
232 -- If Target is busy (cursor tampering is attempted), then Program_Error is
233 -- raised. Otherwise, the elements in Target that are equivalent to
234 -- elements in Source are deleted from Target.
236 function Difference
(Left
, Right
: Set
) return Set
;
237 -- Returns a set comprising the elements from Left that have no equivalent
240 function "-" (Left
, Right
: Set
) return Set
renames Difference
;
242 procedure Symmetric_Difference
(Target
: in out Set
; Source
: Set
);
243 -- If Target is busy, then Program_Error is raised. Otherwise, the elements
244 -- in Target equivalent to elements in Source are deleted from Target, and
245 -- the elements in Source not equivalent to elements in Target are inserted
248 function Symmetric_Difference
(Left
, Right
: Set
) return Set
;
249 -- Returns a set comprising the union of the elements from Target having no
250 -- equivalent in Source, and the elements of Source having no equivalent in
253 function "xor" (Left
, Right
: Set
) return Set
renames Symmetric_Difference
;
255 function Overlap
(Left
, Right
: Set
) return Boolean;
256 -- Returns True if Left contains an element equivalent to an element of
259 function Is_Subset
(Subset
: Set
; Of_Set
: Set
) return Boolean;
260 -- Returns True if every element in Subset has an equivalent element in
263 function First
(Container
: Set
) return Cursor
;
264 -- If Container is empty, the function returns No_Element. Otherwise, it
265 -- returns a cursor designating the smallest element.
267 function First_Element
(Container
: Set
) return Element_Type
;
268 -- Equivalent to Element (First (Container))
270 function Last
(Container
: Set
) return Cursor
;
271 -- If Container is empty, the function returns No_Element. Otherwise, it
272 -- returns a cursor designating the largest element.
274 function Last_Element
(Container
: Set
) return Element_Type
;
275 -- Equivalent to Element (Last (Container))
277 function Next
(Position
: Cursor
) return Cursor
;
278 -- If Position equals No_Element or Last (Container), the function returns
279 -- No_Element. Otherwise, it returns a cursor designating the node that
280 -- immediately follows (as per the insertion order) the node designated by
283 procedure Next
(Position
: in out Cursor
);
284 -- Equivalent to Position := Next (Position)
286 function Previous
(Position
: Cursor
) return Cursor
;
287 -- If Position equals No_Element or First (Container), the function returns
288 -- No_Element. Otherwise, it returns a cursor designating the node that
289 -- immediately precedes (as per the insertion order) the node designated by
292 procedure Previous
(Position
: in out Cursor
);
293 -- Equivalent to Position := Previous (Position)
295 function Find
(Container
: Set
; Item
: Element_Type
) return Cursor
;
296 -- Returns a cursor designating the first element in Container equivalent
297 -- to Item. If there is no equivalent element, it returns No_Element.
299 function Floor
(Container
: Set
; Item
: Element_Type
) return Cursor
;
300 -- If Container is empty, the function returns No_Element. If Item is
301 -- equivalent to elements in Container, it returns a cursor designating the
302 -- first equivalent element. Otherwise, it returns a cursor designating the
303 -- largest element less than Item, or No_Element if all elements are
304 -- greater than Item.
306 function Ceiling
(Container
: Set
; Item
: Element_Type
) return Cursor
;
307 -- If Container is empty, the function returns No_Element. If Item is
308 -- equivalent to elements of Container, it returns a cursor designating the
309 -- last equivalent element. Otherwise, it returns a cursor designating the
310 -- smallest element greater than Item, or No_Element if all elements are
313 function Contains
(Container
: Set
; Item
: Element_Type
) return Boolean;
314 -- Equivalent to Container.Find (Item) /= No_Element
316 function "<" (Left
, Right
: Cursor
) return Boolean;
317 -- Equivalent to Element (Left) < Element (Right)
319 function ">" (Left
, Right
: Cursor
) return Boolean;
320 -- Equivalent to Element (Right) < Element (Left)
322 function "<" (Left
: Cursor
; Right
: Element_Type
) return Boolean;
323 -- Equivalent to Element (Left) < Right
325 function ">" (Left
: Cursor
; Right
: Element_Type
) return Boolean;
326 -- Equivalent to Right < Element (Left)
328 function "<" (Left
: Element_Type
; Right
: Cursor
) return Boolean;
329 -- Equivalent to Left < Element (Right)
331 function ">" (Left
: Element_Type
; Right
: Cursor
) return Boolean;
332 -- Equivalent to Element (Right) < Left
336 Process
: not null access procedure (Position
: Cursor
));
337 -- Calls Process with a cursor designating each element of Container, in
338 -- order from Container.First to Container.Last.
340 procedure Reverse_Iterate
342 Process
: not null access procedure (Position
: Cursor
));
343 -- Calls Process with a cursor designating each element of Container, in
344 -- order from Container.Last to Container.First.
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.Floor (Item) to Container.Ceiling (Item).
353 procedure Reverse_Iterate
356 Process
: not null access procedure (Position
: Cursor
));
357 -- Call Process with a cursor designating each element equivalent to Item,
358 -- in order from Container.Ceiling (Item) to Container.Floor (Item).
362 return Set_Iterator_Interfaces
.Reversible_Iterator
'class;
367 return Set_Iterator_Interfaces
.Reversible_Iterator
'class;
370 type Key_Type
(<>) is private;
372 with function Key
(Element
: Element_Type
) return Key_Type
;
374 with function "<" (Left
, Right
: Key_Type
) return Boolean is <>;
376 package Generic_Keys
is
378 function Equivalent_Keys
(Left
, Right
: Key_Type
) return Boolean;
379 -- Returns False if Left is less than Right, or Right is less than Left;
380 -- otherwise, it returns True.
382 function Key
(Position
: Cursor
) return Key_Type
;
383 -- Equivalent to Key (Element (Position))
385 function Element
(Container
: Set
; Key
: Key_Type
) return Element_Type
;
386 -- Equivalent to Element (Find (Container, Key))
388 procedure Exclude
(Container
: in out Set
; Key
: Key_Type
);
389 -- Deletes from Container any elements whose key is equivalent to Key
391 procedure Delete
(Container
: in out Set
; Key
: Key_Type
);
392 -- Deletes from Container any elements whose key is equivalent to
393 -- Key. If there are no such elements, then it raises Constraint_Error.
395 function Find
(Container
: Set
; Key
: Key_Type
) return Cursor
;
396 -- Returns a cursor designating the first element in Container whose key
397 -- is equivalent to Key. If there is no equivalent element, it returns
400 function Floor
(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 in Container, it returns a cursor
403 -- designating the first such element. Otherwise, it returns a cursor
404 -- designating the largest element whose key is less than Item, or
405 -- No_Element if all keys are greater than Item.
407 function Ceiling
(Container
: Set
; Key
: Key_Type
) return Cursor
;
408 -- If Container is empty, the function returns No_Element. If Item is
409 -- equivalent to the keys of elements of Container, it returns a cursor
410 -- designating the last such element. Otherwise, it returns a cursor
411 -- designating the smallest element whose key is greater than Item, or
412 -- No_Element if all keys are less than Item.
414 function Contains
(Container
: Set
; Key
: Key_Type
) return Boolean;
415 -- Equivalent to Find (Container, Key) /= No_Element
417 procedure Update_Element
-- Update_Element_Preserving_Key ???
418 (Container
: in out Set
;
420 Process
: not null access
421 procedure (Element
: in out Element_Type
));
422 -- If Position equals No_Element, then Constraint_Error is raised. If
423 -- Position is associated with a set object different from Container,
424 -- then Program_Error is raised. Otherwise, it makes a copy of the key
425 -- of the element designated by Position, and then calls Process with
426 -- the element as the parameter. Update_Element then compares the key
427 -- value obtained before calling Process to the key value obtained from
428 -- the element after calling Process. If the keys are equivalent then
429 -- the operation terminates. If Container is busy (cursor tampering has
430 -- been attempted), then Program_Error is raised. Otherwise, the node
431 -- is moved to its new position (in canonical order).
436 Process
: not null access procedure (Position
: Cursor
));
437 -- Call Process with a cursor designating each element equivalent to
438 -- Key, in order from Floor (Container, Key) to
439 -- Ceiling (Container, Key).
441 procedure Reverse_Iterate
444 Process
: not null access procedure (Position
: Cursor
));
445 -- Call Process with a cursor designating each element equivalent to
446 -- Key, in order from Ceiling (Container, Key) to
447 -- Floor (Container, Key).
453 pragma Inline
(Next
);
454 pragma Inline
(Previous
);
457 type Node_Access
is access Node_Type
;
459 type Node_Type
is limited record
460 Parent
: Node_Access
;
463 Color
: Red_Black_Trees
.Color_Type
:= Red_Black_Trees
.Red
;
464 Element
: Element_Type
;
467 package Tree_Types
is
468 new Red_Black_Trees
.Generic_Tree_Types
(Node_Type
, Node_Access
);
470 type Set
is new Ada
.Finalization
.Controlled
with record
471 Tree
: Tree_Types
.Tree_Type
;
474 overriding
procedure Adjust
(Container
: in out Set
);
476 overriding
procedure Finalize
(Container
: in out Set
) renames Clear
;
480 use Ada
.Finalization
;
483 type Set_Access
is access all Set
;
484 for Set_Access
'Storage_Size use 0;
486 -- In all predefined libraries the following type is controlled, for proper
487 -- management of tampering checks. For performance reason we omit this
488 -- machinery for multisets, which are used in a number of our tools.
490 type Reference_Control_Type
is record
491 Container
: Set_Access
;
494 type Constant_Reference_Type
495 (Element
: not null access constant Element_Type
) is record
496 Control
: Reference_Control_Type
:=
497 raise Program_Error
with "uninitialized reference";
498 -- The RM says, "The default initialization of an object of
499 -- type Constant_Reference_Type or Reference_Type propagates
503 type Cursor
is record
504 Container
: Set_Access
;
509 (Stream
: not null access Root_Stream_Type
'Class;
512 for Cursor
'Write use Write
;
515 (Stream
: not null access Root_Stream_Type
'Class;
518 for Cursor
'Read use Read
;
520 No_Element
: constant Cursor
:= Cursor
'(null, null);
523 (Stream : not null access Root_Stream_Type'Class;
526 for Set'Write use Write;
529 (Stream : not null access Root_Stream_Type'Class;
530 Container : out Set);
532 for Set'Read use Read;
535 (Stream : not null access Root_Stream_Type'Class;
536 Item : out Constant_Reference_Type);
538 for Constant_Reference_Type'Read use Read;
541 (Stream : not null access Root_Stream_Type'Class;
542 Item : Constant_Reference_Type);
544 for Constant_Reference_Type'Write use Write;
546 Empty_Set : constant Set :=
547 (Controlled with Tree => (First => null,
554 type Iterator is new Limited_Controlled and
555 Set_Iterator_Interfaces.Reversible_Iterator with
557 Container : Set_Access;
561 overriding procedure Finalize (Object : in out Iterator);
563 overriding function First (Object : Iterator) return Cursor;
564 overriding function Last (Object : Iterator) return Cursor;
566 overriding function Next
568 Position : Cursor) return Cursor;
570 overriding function Previous
572 Position : Cursor) return Cursor;
574 end Ada.Containers.Ordered_Multisets;