205 lines
4.0 KiB
Java
205 lines
4.0 KiB
Java
package set;
|
|
|
|
import position.NodePositionList;
|
|
import position.Position;
|
|
import position.PositionList;
|
|
import utility.DefaultComparator;
|
|
import utility.merge.MergeTemplate;
|
|
|
|
import java.util.Comparator;
|
|
import java.util.Iterator;
|
|
|
|
/**
|
|
* Created with xgiovio.macbookair.
|
|
* User: xgiovio
|
|
* Date: 14/05/14
|
|
* Time: 14:23
|
|
*/
|
|
|
|
public class OrderedListSet <E> implements Set<E> {
|
|
private Comparator<E> c;
|
|
private PositionList<E> L;
|
|
private Position<Set<E>> loc;
|
|
|
|
|
|
|
|
public OrderedListSet() {
|
|
L = new NodePositionList<E>();
|
|
c = new DefaultComparator<E>();
|
|
}
|
|
|
|
public OrderedListSet(Comparator<E> in_c) {
|
|
L = new NodePositionList<E>();
|
|
c = in_c;
|
|
}
|
|
|
|
public OrderedListSet(PositionList<E> in_l){
|
|
L = in_l;
|
|
c = new DefaultComparator<E>();
|
|
}
|
|
|
|
public OrderedListSet(PositionList<E> in_l,Comparator<E> in_c ){
|
|
L = in_l;
|
|
c = in_c;
|
|
}
|
|
|
|
public OrderedListSet(E in_e,Comparator<E> in_c ){
|
|
L = new NodePositionList<E>();
|
|
L.addLast(in_e);
|
|
c = in_c;
|
|
}
|
|
|
|
public OrderedListSet(E in_e){
|
|
L = new NodePositionList<E>();
|
|
L.addLast(in_e);
|
|
c = new DefaultComparator<E>();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
public int size() {
|
|
return L.size();
|
|
}
|
|
|
|
@Override
|
|
public boolean isEmpty() {
|
|
return L.isEmpty();
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
public Set<E> union(Set<E> B) {
|
|
MergeUnion<E> ret = new MergeUnion<E>(L, ((OrderedListSet<E>)B).L , c );
|
|
L = ret.getResult();
|
|
return this;
|
|
|
|
}
|
|
|
|
@Override
|
|
public Set<E> intersect(Set<E> B) {
|
|
MergeIntersect<E> ret = new MergeIntersect<E>(L, ((OrderedListSet<E>)B).L , c );
|
|
L = ret.getResult();
|
|
return this;
|
|
}
|
|
|
|
@Override
|
|
public Set<E> subtract(Set<E> B) {
|
|
MergeSubtract<E> ret = new MergeSubtract<E>(L, ((OrderedListSet<E>)B).L , c );
|
|
L = ret.getResult();
|
|
return this;
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return L.toString();
|
|
}
|
|
|
|
|
|
|
|
public Set<E> fastUnion(Set<E> B){
|
|
OrderedListSet<E> BB = (OrderedListSet<E>) B;
|
|
Iterator<E> it = BB.L.iterator();
|
|
for (;it.hasNext();)
|
|
L.addLast(it.next());
|
|
return this;
|
|
}
|
|
|
|
|
|
public E fastInsert(E x) {
|
|
L.addLast(x);
|
|
return x;
|
|
}
|
|
|
|
|
|
public Position<Set<E>> location (){
|
|
return loc;
|
|
}
|
|
|
|
public void setLocation (Position<Set<E>> in){
|
|
loc = in;
|
|
}
|
|
|
|
public PositionList<E> list (){
|
|
return L;
|
|
}
|
|
|
|
|
|
//////////////////////// inner class override methods from merge template /////
|
|
|
|
protected class MergeUnion<E> extends MergeTemplate<E>{
|
|
|
|
public MergeUnion (PositionList<E> A , PositionList<E> B, Comparator<E> c){
|
|
super(A,B,c);
|
|
}
|
|
|
|
@Override
|
|
protected void aIsLess(E a) {
|
|
s.addLast(a);
|
|
}
|
|
|
|
@Override
|
|
protected void bIsLess(E b) {
|
|
s.addLast(b);
|
|
}
|
|
|
|
@Override
|
|
protected void bothAreEqual(E a, E b) {
|
|
s.addLast(a);
|
|
}
|
|
}
|
|
|
|
protected class MergeIntersect<E> extends MergeTemplate<E>{
|
|
|
|
public MergeIntersect (PositionList<E> A , PositionList<E> B, Comparator<E> c){
|
|
super(A,B,c);
|
|
}
|
|
|
|
@Override
|
|
protected void aIsLess(E a) {
|
|
|
|
}
|
|
|
|
@Override
|
|
protected void bIsLess(E b) {
|
|
|
|
}
|
|
|
|
@Override
|
|
protected void bothAreEqual(E a, E b) {
|
|
s.addLast(a);
|
|
}
|
|
}
|
|
|
|
protected class MergeSubtract<E> extends MergeTemplate<E>{
|
|
|
|
public MergeSubtract (PositionList<E> A , PositionList<E> B, Comparator<E> c){
|
|
super(A,B,c);
|
|
}
|
|
|
|
@Override
|
|
protected void aIsLess(E a) {
|
|
s.addLast(a);
|
|
}
|
|
|
|
@Override
|
|
protected void bIsLess(E b) {
|
|
|
|
}
|
|
|
|
@Override
|
|
protected void bothAreEqual(E a, E b) {
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} |