public class Permutation extends Object implements Mapping, Mappings.TargetMapping
| Constructor and Description |
|---|
Permutation(int size)
Creates a permutation of a given size.
|
Permutation(int[] targets)
Creates a permutation from an array.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Removes all elements in the mapping.
|
Object |
clone() |
boolean |
equals(Object obj) |
MappingType |
getMappingType()
Returns the mapping type.
|
int |
getSource(int target)
Returns the position which maps to
target. |
int |
getSourceCount()
Returns the number of sources.
|
int |
getSourceOpt(int target) |
int |
getTarget(int source)
Returns the position that
source is mapped to. |
int |
getTargetCount()
Returns the number of targets.
|
int |
getTargetOpt(int source)
Returns the target that a source maps to, or -1 if it is not mapped.
|
int |
hashCode() |
void |
identity()
Initializes this permutation to the identity permutation.
|
void |
insertSource(int x)
Inserts into the sources.
|
void |
insertTarget(int x)
Inserts into the targets.
|
Permutation |
inverse()
Returns the inverse permutation.
|
boolean |
isIdentity()
Returns whether this is the identity permutation.
|
Iterator<IntPair> |
iterator()
Returns an iterator over the elements in this mapping.
|
Permutation |
product(Permutation permutation)
Returns the product of this Permutation with a given Permutation.
|
void |
set(int source,
int target)
Maps source position to target position.
|
void |
set(int source,
int target,
boolean allowResize)
Maps source position to target position, automatically resizing if source
or target is out of bounds.
|
void |
setAll(Mapping mapping) |
int |
size()
Returns the number of elements in this permutation.
|
String |
toString()
Returns a string representation of this permutation.
|
public Permutation(int size)
It is initialized to the identity permutation, such as "[0, 1, 2, 3]".
size - Number of elements in the permutationpublic Permutation(int[] targets)
targets - Array of targetsIllegalArgumentException - if elements of array are not uniqueArrayIndexOutOfBoundsException - if elements of array are not
between 0 through targets.length - 1
inclusivepublic void identity()
public final int size()
size in interface Mappingsize in interface Mappings.CoreMappingpublic void clear()
Mappingpublic String toString()
For example, the mapping
| source | target |
|---|---|
| 0 | 2 |
| 1 | 0 |
| 2 | 1 |
| 3 | 3 |
public void set(int source,
int target)
To preserve the 1:1 nature of the permutation, the previous target of source becomes the new target of the previous source.
For example, given the permutation
[3, 2, 0, 1]suppose we map position 2 to target 1. Position 2 currently has target 0, and the source of position 1 is position 3. We preserve the permutation property by mapping the previous source 3 to the previous target 0. The new permutation is
[3, 2, 1, 0].
Another example. Again starting from
[3, 2, 0, 1]suppose we map position 2 to target 3. We map the previous source 0 to the previous target 0, which gives
[0, 2, 3, 1].
set in interface Mappings.TargetMappingsource - Source positiontarget - Target positionArrayIndexOutOfBoundsException - if source or target is negative or
greater than or equal to the size of
the permuationpublic void set(int source,
int target,
boolean allowResize)
To preserve the 1:1 nature of the permutation, the previous target of source becomes the new target of the previous source.
For example, given the permutation
[3, 2, 0, 1]suppose we map position 2 to target 1. Position 2 currently has target 0, and the source of position 1 is position 3. We preserve the permutation property by mapping the previous source 3 to the previous target 0. The new permutation is
[3, 2, 1, 0].
Another example. Again starting from
[3, 2, 0, 1]suppose we map position 2 to target 3. We map the previous source 0 to the previous target 0, which gives
[0, 2, 3, 1].
source - Source positiontarget - Target positionallowResize - Whether to resize the permutation if the source or
target is greater than the current capacityArrayIndexOutOfBoundsException - if source or target is negative,
or greater than or equal to the size
of the permutation, and
allowResize is falsepublic void insertTarget(int x)
For example, consider the permutation
| source | 0 | 1 | 2 | 3 | 4 |
| target | 3 | 0 | 4 | 2 | 1 |
After applying insertTarget(2) every target 2 or higher is
shifted up one.
| source | 0 | 1 | 2 | 3 | 4 | 5 |
| target | 4 | 0 | 5 | 3 | 1 | 2 |
Note that the array has been extended to accommodate the new target, and the previously unmapped source 5 is mapped to the unused target slot 2.
x - Ordinal of position to add to targetpublic void insertSource(int x)
Behavior is analogous to insertTarget(int).
x - Ordinal of position to add to sourcepublic Permutation inverse()
inverse in interface Mappings.SourceMappinginverse in interface Mappings.TargetMappingpublic boolean isIdentity()
isIdentity in interface MappingisIdentity in interface Mappings.SourceMappingpublic int getTarget(int source)
source is mapped to.getTarget in interface Mappings.FunctionMappinggetTarget in interface Mappings.TargetMappingsource - sourcepublic int getSource(int target)
target.getSource in interface Mappings.SourceMappingpublic Iterator<IntPair> iterator()
MappingThis method is optional; implementations may throw
UnsupportedOperationException.
public int getSourceCount()
MappinggetSourceCount in interface MappinggetSourceCount in interface Mappings.FunctionMappinggetSourceCount in interface Mappings.SourceMappinggetSourceCount in interface Mappings.TargetMappingpublic int getTargetCount()
MappinggetTargetCount in interface MappinggetTargetCount in interface Mappings.SourceMappinggetTargetCount in interface Mappings.TargetMappingpublic MappingType getMappingType()
Mappings.CoreMappinggetMappingType in interface MappinggetMappingType in interface Mappings.CoreMappinggetMappingType in interface Mappings.FunctionMappinggetMappingType in interface Mappings.SourceMappingpublic int getTargetOpt(int source)
Mappings.FunctionMappinggetTargetOpt in interface Mappings.FunctionMappinggetTargetOpt in interface Mappings.SourceMappinggetTargetOpt in interface Mappings.TargetMappingpublic int getSourceOpt(int target)
getSourceOpt in interface Mappings.SourceMappinggetSourceOpt in interface Mappings.TargetMappingpublic void setAll(Mapping mapping)
public Permutation product(Permutation permutation)
permutation.
For example, perm.product(perm.inverse()) yields the identity.
Copyright © 2012–2015 The Apache Software Foundation. All rights reserved.