101 lines
2.3 KiB
Java
101 lines
2.3 KiB
Java
package utility.merge;
|
|
|
|
import exceptions.BoundaryViolationException;
|
|
import exceptions.EmptyListException;
|
|
import exceptions.InvalidPositionException;
|
|
import position.NodePositionList;
|
|
import position.Position;
|
|
import position.PositionList;
|
|
import utility.DefaultComparator;
|
|
|
|
import java.util.Comparator;
|
|
import java.util.Iterator;
|
|
|
|
/**
|
|
* Created with xgiovio.macbookair.
|
|
* User: xgiovio
|
|
* Date: 14/05/14
|
|
* Time: 14:48
|
|
*/
|
|
public abstract class MergeTemplate<E> {
|
|
|
|
protected PositionList<E> s = new NodePositionList<E>();
|
|
protected Comparator<E> c = null;
|
|
|
|
public MergeTemplate (PositionList<E> A, PositionList <E> B){
|
|
this(A,B,new DefaultComparator<E>());
|
|
}
|
|
|
|
public MergeTemplate (PositionList<E> A, PositionList <E> B, Comparator<E> in_c){
|
|
|
|
c = in_c;
|
|
E a = null ,b = null;
|
|
Iterator<E> Ait, Bit;
|
|
Ait = A.iterator();
|
|
Bit = B.iterator();
|
|
|
|
|
|
if ( Ait.hasNext() && Bit.hasNext() ){
|
|
a = Ait.next();
|
|
b = Bit.next();
|
|
for (; true; ) {
|
|
|
|
if (c.compare(a, b) < 0) {
|
|
aIsLess(a);
|
|
if (Ait.hasNext())
|
|
a = Ait.next();
|
|
else
|
|
break;
|
|
} else {
|
|
if (c.compare(a, b) > 0) {
|
|
bIsLess(b);
|
|
if (Bit.hasNext())
|
|
b = Bit.next();
|
|
else
|
|
break;
|
|
|
|
} else {// se b = a
|
|
bothAreEqual(a, b);
|
|
if (Ait.hasNext() && Bit.hasNext()) {
|
|
a = Ait.next();
|
|
b = Bit.next();
|
|
} else
|
|
break;
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
while ( Ait.hasNext() ) {
|
|
a = Ait.next();
|
|
aIsLess(a);
|
|
}
|
|
while ( Bit.hasNext()) {
|
|
b = Bit.next();
|
|
bIsLess(b);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
abstract protected void aIsLess (E a);
|
|
|
|
abstract protected void bIsLess (E b);
|
|
|
|
abstract protected void bothAreEqual (E a, E b);
|
|
|
|
|
|
public PositionList<E> getResult () {
|
|
return s;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|