Module permutator::copy
source · Expand description
A module that perform everything in copy fashion. There is no heap_permutation callback family function in here because that function family already return a slice of value.
The additional constraint here is T
must implement copy.
The benefit of using this module is the input and output
of each functionality will always in the same format.
So there will be no &[&&&T] like in parent module
when you perform a cartesian_product then combination
then heap_permutation function.
The downside is that if T is expensive to copy then
there will be performance cost to pay.
General rule is that if type T
in slice like &[T]
is primitive type, this module will likely have no
performance different, thus easier to use because
there’ll be no such ***x[0]
syntax but if type T
is
a complex type like struct then it cannot be directly
used in this module. There’ll be some situation that required
usage of this module. For example, in some case, a common Vec
to store result from both combination result and those that
is result from sequence of operation like combination then
permutation. This cannot be done with the non-copy version
because the result of single operation will be &[&T] but
two operations will be &[&&T] which has different type.
To avoid such case, create another Vec that hold every T
then create another Vec that hold usize pointed to each
element in prior Vec. Now we have a Vec that store primitive
type thust can be used in this module.
Another way to do the same thing but doesn’t have an indirection
like actual_data[pointers[i]]
to access data is to create
a Vec<&T>
and every element in this Vec is just a ref to
another element in Vecref_vec[i]
.
The only restriction with later approach is T
must be sized.
Note: All Iterator that return an owned item
still return the same owned item, e.g. Vec
Structs
Iterator
trait so it can be used as Iterator
.
The struct provide into_iter() function
that return itself.Iterator
trait so it can be used in Iterator
style. The struct provide into_iter() function
that return itself.Iterator
trait so it can be used as Iterator
.
The struct provide into_iter() function
that return itself.Iterator
trait.k
must be
less than n
.
It’ll attempt to permute given data by pick k
elements
out of n
data. It use Gosper algorithm to pick the elements.
It then use Heap’s algorithm to permute those k
elements
and return each permutation back to caller by given
Rc<RefCell<&mut [&T]>> parameter to
new method of KPermutationCellIter.k
elements
out of data. It use Gosper algorithm to pick the elements.
It then use Heap’s algorithm to permute those k
elements
and return each permutation back to caller.k
must be
less than n
and store result into mutable pointer.
It’ll attempt to permute given data by pick k
elements
out of n
data. It use Gosper algorithm to pick the elements.
It then use Heap’s algorithm to permute those k
elements
and return each permutation back to caller by given
*mut [&T]>> parameter to
new method of KPermutationRefIter.Iterator
trait so it can be used in Iterator
style. The struct provide into_iter() function
that return itself.Iterator
trait so it can be used in Iterator
style. The struct provide into_iter() function
that return itself.Iterator
trait so it can be used in Iterator
style. The struct provide into_iter() function
that return itself.Traits
T
.
For example,T
Normally, it take a [T]
or Vec<T>
to create a combination.Rc<RefCell<&mut [T]>>
.Functions
T
. objects
is [1, 2, 3] and degree is 2 then
all possible result is [1, 1], [1, 2], [1, 3],
[2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]objects
is [1, 2, 3, 4] and degree
is 2 then
all possible permutation will be [1, 2], [1, 3],
[1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2],
[3, 4], [4, 1], [4, 2], [4, 3].d
. For example: d = &[1, 2, 3]; k = 2.
The result will be [1, 2], [2, 1], [1, 3], [3, 1], [2, 3], [3, 2]T
. Type Definitions
Permutation
trait
to create k-permutation iterator from it.Permutation
trait
to create k-permutation ref iterator from it.Permutation
trait
to create k-permutation iterator from it.