|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.eigenbase.util.Permutation
public class Permutation
Represents a mapping which reorders elements in an array.
Field Summary | |
---|---|
private int[] |
sources
|
private int[] |
targets
|
Constructor Summary | |
---|---|
|
Permutation(int size)
Creates a permutation of a given size. |
|
Permutation(int[] targets)
Creates a permutation from an array. |
private |
Permutation(int[] targets,
int[] sources)
Creates a permuation. |
Method Summary | |
---|---|
Object |
clone()
|
boolean |
equals(Object obj)
|
MappingType |
getMappingType()
|
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. |
private void |
increment(int x,
int[] zzz)
|
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. |
private boolean |
isValid(boolean fail)
Checks whether this permutation is valid. |
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. |
private void |
resize(int newSize)
|
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)
|
private void |
setInternal(int source,
int target)
|
private void |
shuffleUp(int[] zz,
int x)
|
int |
size()
Returns the number of elements in this permutation. |
String |
toString()
Returns a string representation of this permutation. |
Methods inherited from class java.lang.Object |
---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
private int[] targets
private int[] sources
Constructor Detail |
---|
public Permutation(int size)
It is intialized to the identity permutation, such as "[0, 1, 2, 3]".
size
- Number of elements in the permutationpublic Permutation(int[] targets)
targets
- Array of targets
IllegalArgumentException
- if elements of array are not unique
ArrayIndexOutOfBoundsException
- if elements of array are not
between 0 through targets.length - 1 inclusiveprivate Permutation(int[] targets, int[] sources)
Method Detail |
---|
public Object clone()
clone
in class Object
public void identity()
public final int size()
public String toString()
For example, the mapping
source | target |
---|---|
0 | 2 |
1 | 0 |
2 | 1 |
3 | 3 |
toString
in class Object
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.TargetMapping
source
- Source positiontarget
- Target position
ArrayIndexOutOfBoundsException
- 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 capacity
ArrayIndexOutOfBoundsException
- 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)
source | 0 | 1 | 2 | 3 | 4 |
target | 3 | 0 | 4 | 2 | 1 |
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 |
x
- public void insertSource(int x)
insertTarget(int)
.
x
- private void increment(int x, int[] zzz)
private void shuffleUp(int[] zz, int x)
private void resize(int newSize)
private void setInternal(int source, int target)
public Permutation inverse()
inverse
in interface Mappings.SourceMapping
inverse
in interface Mappings.TargetMapping
public boolean isIdentity()
isIdentity
in interface Mapping
isIdentity
in interface Mappings.SourceMapping
public int getTarget(int source)
source
is mapped to.
getTarget
in interface Mappings.FunctionMapping
getTarget
in interface Mappings.TargetMapping
source
- source
public int getSource(int target)
target
.
getSource
in interface Mappings.SourceMapping
private boolean isValid(boolean fail)
fail
- Whether to assert if invalid
public int hashCode()
hashCode
in class Object
public boolean equals(Object obj)
equals
in class Object
public Iterator<IntPair> iterator()
Mapping
This method is optional; implementations may throw UnsupportedOperationException
.
iterator
in interface Iterable<IntPair>
iterator
in interface Mapping
public int getSourceCount()
Mapping
getSourceCount
in interface Mapping
getSourceCount
in interface Mappings.FunctionMapping
getSourceCount
in interface Mappings.SourceMapping
getSourceCount
in interface Mappings.TargetMapping
public int getTargetCount()
Mapping
getTargetCount
in interface Mapping
getTargetCount
in interface Mappings.SourceMapping
getTargetCount
in interface Mappings.TargetMapping
public MappingType getMappingType()
getMappingType
in interface Mapping
getMappingType
in interface Mappings.FunctionMapping
getMappingType
in interface Mappings.SourceMapping
getMappingType
in interface Mappings.TargetMapping
public int getTargetOpt(int source)
Mappings.FunctionMapping
getTargetOpt
in interface Mappings.FunctionMapping
getTargetOpt
in interface Mappings.SourceMapping
getTargetOpt
in interface Mappings.TargetMapping
public int getSourceOpt(int target)
getSourceOpt
in interface Mappings.SourceMapping
getSourceOpt
in interface Mappings.TargetMapping
public void setAll(Mapping mapping)
public Permutation product(Permutation permutation)
permutation
.
For example, perm.product(perm.inverse()) yields the identity.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |