MutableSet
An object-based mutable set
|
Description
|
|
•
|
The MutableSet appliable object provides an efficient means to construct, manipulate, and query set objects.
|
•
|
Unlike a standard Maple set, adding an element to a MutableSet object does not allocate a new container. As such, it can be efficiently used in loops to build up a set an element at a time.
|
|
Construction
|
|
•
|
The MutableSet function returns a new MutableSet object.
|
–
|
If called with a single argument that is either a Maple set or a MutableSet object, the returned object contains the elements in the argument.
|
–
|
Otherwise, the returned object contains each distinct argument as an element.
|
|
|
Modification
|
|
•
|
The following functions modify the content The ms argument denotes a MutableSet object.
|
–
|
delete(ms,expr) : Delete expr from ms.
|
–
|
insert(ms,expr) : Insert expr into ms.
|
–
|
ms ,= expr : Insert expr into ms. Unlike the insert function, the ,= operator can accept a sequence of elements on the right hand side.
|
|
|
Set Operation
|
|
•
|
The following set functions combine MutableSet objects. Those prefixed with an ampersand (&) operate inplace on the first argument. These functions can be invoked as functions or inline operators. The ms1 and ms2 arguments denote MutableSet objects.
|
–
|
ms1 intersect ms2, or , or `intersect`(ms1,ms2,...) : Return a new MutableSet object that is the intersection of the given MutableSets.
|
–
|
ms1 &intersect ms2 or `&intersect`(ms1,ms2,...) : Remove elements of ms1 that are not elements of ms2, etc., updating ms1.
|
–
|
ms1 minus ms2, or , or `minus`(ms1,ms2) : Return a new MutableSet object that contains the set difference of ms1 and ms2.
|
–
|
ms1 &minus ms2 or `&minus`(ms1,ms2) : Remove elements of ms2 from ms1, updating ms1.
|
–
|
ms1 union ms2, or , or `union`(ms1,ms2,...) : Return a new MutableSet object that is the union of the given mutable sets.
|
–
|
ms1 &union ms2 or `&union`(ms1,ms2,...) : Combine the members of ms2, etc., with ms1, updating ms1.
|
|
|
Inspection
|
|
•
|
The following functions inspect the content of a MutableSet object. The ms, ms1, and ms2 arguments denote MutableSet objects.
|
–
|
ms = other or `=`(ms,other) : When evaluated in a boolean context, returns true if ms and other are both MutableSet objects with identical content, false otherwise.
|
–
|
expr in ms or `in`(expr,ms) : Returns true if expr is a member of ms, false otherwise.
|
–
|
empty(ms) : Returns true if ms is empty, false otherwise.
|
–
|
entries(ms) : Returns an expression sequence of all the entries in ms, with each entry enclosed in a list by default.
|
–
|
has(ms,expr) : Returns true if any member of ms contains expr, false otherwise.
|
–
|
hastype(ms,typ) : Returns true if any member of ms contains an expression of the specified type, false otherwise.
|
–
|
indets(ms,typ) : Returns a Maple set of all indeterminates in ms. If the optional typ parameter is specified, then returns the expressions of type typ.
|
–
|
indices(ms) : Returns an expression sequence of all the indices of ms, with each index enclosed in a list by default.
|
–
|
lowerbound(ms) : Returns the lower index bound (always 1).
|
–
|
max(ms) : Returns the maximum element of ms. Behaves like max on sets.
|
–
|
member(expr,ms) : Returns true if expr is a member of ms, false otherwise. The three argument form of member is not supported.
|
–
|
min(ms) : Returns the minimum element of ms. Behaves like min on sets.
|
–
|
numelems(ms) : Returns the number of elements in ms.
|
–
|
numboccur(ms,expr) : Count the number of occurrences of expr in ms, either as an element or within elements.
|
–
|
`subset`(ms1,ms2), or ms1 subset ms2, or ms1 subset ms2 : Returns true if ms1 is a subset of ms2, false otherwise.
|
–
|
upperbound(ms) : Returns the upper index (same as the output of numelems).
|
•
|
Each of the above functions is already available in Maple for built-in Maple structures such as sets and Arrays. Please refer to the help page for each function for details on usage.
|
|
|
Mapping, Selection, Removal, and Substitution
|
|
•
|
The map, select, remove, selectremove, and subs functions operate on a MutableSet object. Refer to their help pages for the calling sequences. By default they create a new MutableSet object, but if called with the inplace index option, they update the MutableSet object. The ms argument denotes a MutableSet object.
|
–
|
map(fcn,ms) : Apply a procedure, fcn, to each element of ms.
|
–
|
select(fcn,ms) : Select elements of ms for which fcn returns true.
|
–
|
remove(fcn,ms) : Remove elements of ms for which fcn returns true.
|
–
|
selectremove(fcn,dq) : Produce two MutableSets, one containing selected elements and the other removed elements.
|
–
|
subs(eqns,ms) : Substitute subexpressions in the content of ms.
|
|
|
Conversion
|
|
•
|
The convert function can be used to convert a MutableSet object to and from other Maple structures.
|
–
|
convert(ms,typ) : Convert a MutableSet to the specified type
|
–
|
convert(expr,MutableSet) : Convert expr to a MutableSet object. expr must be a list, set, or rtable.
|
|
|
Indexing
|
|
•
|
Integer indices can be used to extract or reassign a single element of a MutableSet. Reassigning an element may change its index.
|
•
|
Range indices are not supported.
|
|
|
Iteration
|
|
•
|
The MutableSet object exports a ModuleIterator function, allowing a MutableSet object to be used in loops and iterations (seq, add, etc.).
|
|
|
|
Examples
|
|
|
Construction
|
|
•
|
Create a MutableSet with three elements.
|
>
|
|
| (1) |
| (2) |
•
|
Create a MutableSet from a Maple set.
|
>
|
|
| (3) |
|
|
Modification
|
|
•
|
Delete the b element from M1 and insert a d element.
|
| (5) |
•
|
Add to each element in , doing so inplace.
|
>
|
|
| (6) |
•
|
Replace all occurrences of inside with , doing so inplace.
|
>
|
|
| (7) |
|
|
Operation
|
|
•
|
Create two MutableSet objects, then form their intersection.
|
>
|
|
| (8) |
>
|
|
| (9) |
| (10) |
•
|
Use the inplace form to update with the intersection of and .
|
| (11) |
|
|
Inspection
|
|
•
|
Use member to determine whether a given element is a member of .
|
>
|
|
| (12) |
•
|
Use has to test for the occurrence of a subexpression in any of the members of the set.
|
>
|
|
|
|
Conversion
|
|
•
|
Create a MutableSet object by converting a Vector.
|
>
|
|
| (16) |
•
|
Convert M1 to a standard set.
|
| (17) |
|
|
Indexing
|
|
•
|
Use indexing to extract each element of a MutableSet.
|
>
|
|
| (18) |
>
|
|
•
|
Reassign the value at the second index. Note that in the updated MutableSet, the new value is assigned a different index.
|
>
|
|
|
|
Iteration
|
|
•
|
Iterate through each element in M1.
|
| (21) |
•
|
Add the elements of M2.
|
|
|
Usage
|
|
•
|
The MutableSet object provides an efficient way to construct a set an element at a time, in a loop. Doing so with standard Maple sets is inefficient in that each addition creates a new set. As such the operation is in time and memory, where is the number of elements. The following compares the memory and time required to create a set of ten thousand elements using standard sets and a MutableSet object.
|
>
|
MapleSets := proc(n :: posint)
local i, s;
s := {};
for i to n do
s := s union {i};
end do:
numelems(s);
end proc:
MutableSets := proc(n :: posint)
local i, s;
s := MutableSet();
for i to n do
insert(s,-i);
end do;
numelems(s);
end proc:
|
>
|
|
memory used=382.91MiB, alloc change=25.48MiB, cpu time=1.50s, real time=1.21s, gc time=595.66ms
| |
>
|
|
memory used=1.38MiB, alloc change=0 bytes, cpu time=30.00ms, real time=30.00ms, gc time=0ns
| |
|
|
|
Compatibility
|
|
•
|
The MutableSet object was introduced in Maple 2016.
|
|
|
|
|
|
|