Created
October 11, 2011 08:53
-
-
Save salzig/1277607 to your computer and use it in GitHub Desktop.
Permutation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
public interface Permutation extends Iterable<Integer>{ | |
// Build a new Permutation by composition of this and other | |
public Permutation compose(Permutation other); | |
// Get element of Permutation at index | |
public Integer sigma(Integer index); | |
// Get number of elements | |
public Integer getSize(); | |
// Get list of elements | |
public ArrayList<Integer> getElements(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Iterator; | |
public class PermutationImpl implements Permutation { | |
public PermutationImpl(ArrayList<Integer> resultList) { | |
// TODO Auto-generated constructor stub | |
} | |
/* (non-Javadoc) | |
* @see Permutation#compose(Permutation) | |
*/ | |
@Override | |
public Permutation compose(Permutation other) { | |
// Checks: | |
// Same cardinality | |
// Same range (1...n) | |
// -> Both are Permutation objects, should be valid | |
// Example: | |
// [2,4,5,1,3] this | |
// [3,5,1,4,2] other | |
// [5,4,2,3,1] composite | |
ArrayList<Integer> resultList = new ArrayList<Integer>(); | |
for(Integer element: this) { | |
resultList.add(other.sigma(element)); | |
} | |
Permutation result = new PermutationImpl(resultList); | |
return result; | |
} | |
public int hashCode() { | |
// Delegate HashCode to element list | |
return getElements().hashCode(); | |
} | |
public boolean equals(Object other) { | |
boolean result = false; | |
// Reference Test | |
if (this == other) { | |
result = true; | |
} | |
// Type test (instanceof) | |
else if (other instanceof Permutation) { | |
// Attribute test | |
if (this.getSize() == ((Permutation)other).getSize() | |
&& this.getElements().equals(((Permutation)other).getElements())) { | |
result = true; | |
} | |
} | |
return result; | |
} | |
@Override | |
public Iterator<Integer> iterator() { | |
return getElements().iterator(); | |
} | |
@Override | |
public Integer getSize() { | |
return getElements().size(); | |
} | |
@Override | |
public Integer sigma(Integer index) { | |
// TODO Auto-generated method stub | |
return null; | |
} | |
@Override | |
public ArrayList<Integer> getElements() { | |
// TODO Auto-generated method stub | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment