109 lines
2.5 KiB
Java
109 lines
2.5 KiB
Java
package partition;
|
|
|
|
import map.HashTableMap;
|
|
import position.NodePositionList;
|
|
import position.PositionList;
|
|
import set.OrderedListSet;
|
|
import set.Set;
|
|
import utility.DefaultComparator;
|
|
|
|
import java.security.InvalidKeyException;
|
|
import java.util.Iterator;
|
|
|
|
/**
|
|
* Created with xgiovio.macbookair.
|
|
* User: xgiovio
|
|
* Date: 19/05/14
|
|
* Time: 15:02
|
|
*/
|
|
public class ListPartition <E> implements Partition<E> {
|
|
|
|
private HashTableMap<E,Set<E>> elementi;
|
|
private PositionList<Set<E>> partizione;
|
|
|
|
public ListPartition () {
|
|
elementi = new HashTableMap<E, Set<E>>();
|
|
partizione = new NodePositionList<Set<E>>();
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
public int size() {
|
|
return elementi.size();
|
|
}
|
|
|
|
@Override
|
|
public boolean isEmpty() {
|
|
return (size() == 0 );
|
|
}
|
|
|
|
@Override
|
|
public Set<E> makeSet(E x) {
|
|
try {
|
|
OrderedListSet<E> o = new OrderedListSet<E>(x, new DefaultComparator<E>());
|
|
elementi.put(x, o);
|
|
partizione.addLast(o);
|
|
o.setLocation(partizione.last());
|
|
return o;
|
|
}
|
|
catch (InvalidKeyException e){
|
|
|
|
}
|
|
return null;
|
|
|
|
}
|
|
|
|
@Override
|
|
public Set<E> union(Set<E> A, Set<E> B) {
|
|
try {
|
|
if (A.size() > B.size()){
|
|
OrderedListSet<E> AA = (OrderedListSet<E>) A;
|
|
OrderedListSet<E> BB = (OrderedListSet<E>) B;
|
|
AA.fastUnion(BB);
|
|
partizione.remove(BB.location());
|
|
BB.setLocation(null);
|
|
Iterator<E> it = BB.list().iterator();
|
|
for (;it.hasNext();)
|
|
elementi.put(it.next(),AA);
|
|
return AA;
|
|
|
|
} else {
|
|
|
|
OrderedListSet<E> AA = (OrderedListSet<E>) B;
|
|
OrderedListSet<E> BB = (OrderedListSet<E>) A;
|
|
AA.fastUnion(BB);
|
|
partizione.remove(BB.location());
|
|
BB.setLocation(null);
|
|
Iterator<E> it = BB.list().iterator();
|
|
for (;it.hasNext();)
|
|
elementi.put(it.next(),AA);
|
|
return AA;
|
|
|
|
|
|
}
|
|
}
|
|
catch (InvalidKeyException e){
|
|
|
|
}
|
|
return null;
|
|
|
|
}
|
|
|
|
@Override
|
|
public Set<E> find(E x) {
|
|
try {
|
|
return elementi.get(x);
|
|
}
|
|
catch (InvalidKeyException e){
|
|
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return partizione.toString();
|
|
}
|
|
}
|