|
Calling Sequence
|
|
Iterator:-command(arguments)
command(arguments)
|
|
Description
|
|
•
|
The Iterator package exports constructors of efficient iterators over discrete structures.
|
•
|
To reduce memory usage the iterators use a mutable data structure, a one-dimensional Array, as the output.
|
•
|
All constructors provide a compile option that is true by default. When true, the returned iterator is compiled.
|
•
|
Because these iterators use the state of the object, using the same iterator in independent loops is not allowed; an error is raised if that occurs. To loop over the the same discrete structure in independent loops, copy the object (use Object).
|
|
All Commands
|
|
The following commands are available.
|
|
Subpackages
|
|
The following subpackages are available.
Inversion
|
procedures for converting between permutations and inversion tables
|
MixedRadix
|
procedures for operating on mixed-radix tuples
|
Trees
|
procedure for converting between tree representations
|
|
|
|
|
Combinations
|
|
|
|
Compositions
|
|
|
|
Counters
|
|
|
|
Conversions
|
|
Inversion
|
subpackage for converting between permutations and inversion tables
|
Trees
|
subpackage for converting between tree representations
|
|
|
|
|
Gray Codes
|
|
|
|
Necklaces
|
|
|
|
Parentheses
|
|
|
|
Partitions
|
|
|
|
Permutations
|
|
|
|
Products
|
|
|
|
Set Partitions
|
|
|
|
Support
|
|
SplitRanks
|
compute the starting ranks and iterations suitable for parallelizing an iterator
|
|
|
|
|
Trees
|
|
BinaryTrees
|
generate binary trees of a given size
|
OrientedForests
|
generate oriented forests of a given size
|
Trees
|
subpackage for converting between tree representations
|
|
|
|
|
The Twelve-Fold Way
|
|
•
|
Richard Stanley, in Enumerative Combinatorics, categorizes common combinatorial selections using the cardinality of unrestricted, injective, and surjective functions between discrete domains in a tableau called "The Twelve-fold Way." The following table reproduces this categorization, using the enumeration of ways to place balls into urns, and links to the appropriate iterator.
|
balls per urn
|
unrestricted
|
at most 1
|
at least 1
|
labeled balls, labeled urns
|
MixedRadixTuples([m$n])
|
Permute(m,n)
|
[1]
|
unlabeled balls, labeled urns
|
Multicombination([n$m],n)
|
Combination(m,n)
|
Composition(n,parts=m)
|
labeled balls, unlabeled urns
|
SetPartitions(n,maxparts=m)
|
[2]
|
SetPartitions(n,parts=m)
|
unlabeled balls, unlabeled urns
|
PartitionFixedSize(n,maxparts=m)
|
[2]
|
PartitionFixedSize(n,parts=m)
|
|
|
•
|
[1] Partitions of distinct objects into ordered parts.
|
•
|
[2] If there is one arrangement that satisfies this, otherwise none.
|
|
|
Example Index
|
|
•
|
Index of interesting examples. The link goes to the help page; look in its Examples section for the example.
|
|
|
|
Examples
|
|
Use Permute to construct an iterator over all permutations of the list [1,2,2,3].
>
|
|
Use a for-loop to iterate over the permutations.
>
|
|
1 2 2 3
1 2 3 2
1 3 2 2
2 1 2 3
2 1 3 2
2 2 1 3
2 2 3 1
2 3 1 2
2 3 2 1
3 1 2 2
3 2 1 2
3 2 2 1
| |
The same output is more conveniently generated with the Print method. Here the number of iterations is limited and showrank option is used to display the rank.
>
|
|
1: 1 2 2 3
2: 1 2 3 2
3: 1 3 2 2
4: 2 1 2 3
5: 2 1 3 2
6: 2 2 1 3
7: 2 2 3 1
8: 2 3 1 2
9: 2 3 2 1
10: 3 1 2 2
| |
Use a seq command to create the entire sequence.
| (1) |
Note the use of the square brackets, [], to instantiate the Vector that is assigned to p. Without them, all values in the final expression sequence equal the last value because the p' evaluates to the Vector rather than its content. Here is what happens when the square brackets are omitted.
| (2) |
|
Using hasNext and getNext
|
|
•
|
Use Combination to generate all triplets of the integers 0 to 4. Extract the two procedures, hasNext and getNext, from the ModuleIterator method of the iterator object and use them in a while-loop.
|
>
|
|
>
|
|
>
|
|
|
|
Concurrent iterators
|
|
Construct an iterator over the 2-permutations of the list , use Object to create an identical, but independent, second iterator, and use both iterators in a dual-loop.
>
|
|
>
|
|
|
|
|
Compatibility
|
|
•
|
The Iterator package was introduced in Maple 2016.
|
|
|
|