Class Permutation
- All Implemented Interfaces:
Iterable<IntPair>,Mapping,Mappings.CoreMapping,Mappings.FunctionMapping,Mappings.SourceMapping,Mappings.TargetMapping
-
Constructor Summary
ConstructorsConstructorDescriptionPermutation(int size) Creates a permutation of a given size.Permutation(int[] targets) Creates a permutation from an array. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all elements in the mapping.clone()booleanReturns the mapping type.intgetSource(int target) Returns the position which maps totarget.intReturns the number of sources.intgetSourceOpt(int target) Returns the source that a target maps to, or -1 if it is not mapped.intgetTarget(int source) Returns the position thatsourceis mapped to.intReturns the number of targets.intgetTargetOpt(int source) Returns the target that a source maps to, or -1 if it is not mapped.inthashCode()voididentity()Initializes this permutation to the identity permutation.voidinsertSource(int x) Inserts into the sources.voidinsertTarget(int x) Inserts into the targets.inverse()Returns the inverse permutation.booleanReturns whether this is the identity permutation.iterator()Returns an iterator over the elements in this mapping.product(Permutation permutation) Returns the product of this Permutation with a given Permutation.voidset(int source, int target) Maps source position to target position.voidset(int source, int target, boolean allowResize) Maps source position to target position, automatically resizing if source or target is out of bounds.voidfinal intsize()Returns the number of elements in this permutation.toString()Returns a string representation of this permutation.Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
Permutation
public Permutation(int size) Creates a permutation of a given size.It is initialized to the identity permutation, such as "[0, 1, 2, 3]".
- Parameters:
size- Number of elements in the permutation
-
Permutation
public Permutation(int[] targets) Creates a permutation from an array.- Parameters:
targets- Array of targets- Throws:
IllegalArgumentException- if elements of array are not uniqueArrayIndexOutOfBoundsException- if elements of array are not between 0 through targets.length - 1 inclusive
-
-
Method Details
-
clone
-
identity
public void identity()Initializes this permutation to the identity permutation. -
size
public final int size()Returns the number of elements in this permutation.- Specified by:
sizein interfaceMapping- Specified by:
sizein interfaceMappings.CoreMapping
-
clear
public void clear()Description copied from interface:MappingRemoves all elements in the mapping. -
toString
Returns a string representation of this permutation.For example, the mapping
Example mapping source target 0 2 1 0 2 1 3 3 is represented by the string "[2, 0, 1, 3]".
-
set
public void set(int source, int target) Maps source position to target position.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].
- Specified by:
setin interfaceMappings.TargetMapping- Parameters:
source- Source positiontarget- Target position- Throws:
ArrayIndexOutOfBoundsException- if source or target is negative or greater than or equal to the size of the permuation
-
set
public void set(int source, int target, boolean allowResize) Maps source position to target position, automatically resizing if source or target is out of bounds.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].
- Parameters:
source- Source positiontarget- Target positionallowResize- Whether to resize the permutation if the source or target is greater than the current capacity- Throws:
ArrayIndexOutOfBoundsException- if source or target is negative, or greater than or equal to the size of the permutation, andallowResizeis false
-
insertTarget
public void insertTarget(int x) Inserts into the targets.For example, consider the permutation
Example 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.Mapping after applying insertTarget(2) 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.
- Parameters:
x- Ordinal of position to add to target
-
insertSource
public void insertSource(int x) Inserts into the sources.Behavior is analogous to
insertTarget(int).- Parameters:
x- Ordinal of position to add to source
-
inverse
Returns the inverse permutation.- Specified by:
inversein interfaceMappings.SourceMapping- Specified by:
inversein interfaceMappings.TargetMapping
-
isIdentity
public boolean isIdentity()Returns whether this is the identity permutation.- Specified by:
isIdentityin interfaceMapping- Specified by:
isIdentityin interfaceMappings.SourceMapping
-
getTarget
public int getTarget(int source) Returns the position thatsourceis mapped to.- Specified by:
getTargetin interfaceMappings.FunctionMapping- Specified by:
getTargetin interfaceMappings.TargetMapping- Parameters:
source- source- Returns:
- target
-
getSource
public int getSource(int target) Returns the position which maps totarget.- Specified by:
getSourcein interfaceMappings.SourceMapping- Parameters:
target- target- Returns:
- source
-
hashCode
public int hashCode() -
equals
-
iterator
Description copied from interface:MappingReturns an iterator over the elements in this mapping.This method is optional; implementations may throw
UnsupportedOperationException. -
getSourceCount
public int getSourceCount()Description copied from interface:MappingReturns the number of sources. Valid sources will be in the range 0 .. sourceCount.- Specified by:
getSourceCountin interfaceMapping- Specified by:
getSourceCountin interfaceMappings.FunctionMapping- Specified by:
getSourceCountin interfaceMappings.SourceMapping- Specified by:
getSourceCountin interfaceMappings.TargetMapping
-
getTargetCount
public int getTargetCount()Description copied from interface:MappingReturns the number of targets. Valid targets will be in the range 0 .. targetCount.- Specified by:
getTargetCountin interfaceMapping- Specified by:
getTargetCountin interfaceMappings.SourceMapping- Specified by:
getTargetCountin interfaceMappings.TargetMapping
-
getMappingType
Description copied from interface:Mappings.CoreMappingReturns the mapping type.- Specified by:
getMappingTypein interfaceMapping- Specified by:
getMappingTypein interfaceMappings.CoreMapping- Specified by:
getMappingTypein interfaceMappings.FunctionMapping- Specified by:
getMappingTypein interfaceMappings.SourceMapping- Returns:
- Mapping type
-
getTargetOpt
public int getTargetOpt(int source) Description copied from interface:Mappings.FunctionMappingReturns the target that a source maps to, or -1 if it is not mapped.- Specified by:
getTargetOptin interfaceMappings.FunctionMapping- Specified by:
getTargetOptin interfaceMappings.SourceMapping- Specified by:
getTargetOptin interfaceMappings.TargetMapping
-
getSourceOpt
public int getSourceOpt(int target) Description copied from interface:Mappings.SourceMappingReturns the source that a target maps to, or -1 if it is not mapped.- Specified by:
getSourceOptin interfaceMappings.SourceMapping- Specified by:
getSourceOptin interfaceMappings.TargetMapping
-
setAll
-
product
Returns the product of this Permutation with a given Permutation. Does not modify this Permutation orpermutation.For example, perm.product(perm.inverse()) yields the identity.
-