Implementato interamente un tda sequenze usando array (ArrayIndexList). Aggiunto reverse di sequenze in global.
This commit is contained in:
@@ -16,6 +16,11 @@ import sequence.utility.DNodeIndex;
|
||||
*/
|
||||
public class ArraySequence<E> extends ArrayIndexList<E> implements Sequence<E> {
|
||||
|
||||
|
||||
public ArraySequence (int in){
|
||||
super(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
public E getFirst() throws EmptySequenceException {
|
||||
try {
|
||||
@@ -64,76 +69,131 @@ public class ArraySequence<E> extends ArrayIndexList<E> implements Sequence<E> {
|
||||
public Position<E> atIndex(int in_index) throws BoundaryViolationException {
|
||||
try {
|
||||
checkIndex(in_index, size());
|
||||
|
||||
/////////////
|
||||
|
||||
|
||||
return new DNodeIndex<E>(get(in_index),in_index);
|
||||
}
|
||||
catch (IndexOutOfBoundsException err){
|
||||
throw new BoundaryViolationException();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int indexOf(Position<E> position) throws InvalidPositionException {
|
||||
return 0;
|
||||
checkPosition(position);
|
||||
return ((DNodeIndex<E>) position).getIndex();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position<E> first() throws EmptyListException {
|
||||
return null;
|
||||
try{
|
||||
return new DNodeIndex<E>(get(0),0);
|
||||
}
|
||||
catch (IndexOutOfBoundsException err){
|
||||
throw new EmptyListException("Empty Array");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position<E> last() throws EmptyListException {
|
||||
return null;
|
||||
try{
|
||||
return new DNodeIndex<E>(get(size() - 1),size() - 1);
|
||||
}
|
||||
catch (IndexOutOfBoundsException err){
|
||||
throw new EmptyListException("Empty Array");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position<E> next(Position<E> p) throws InvalidPositionException, BoundaryViolationException {
|
||||
return null;
|
||||
DNodeIndex<E> t = checkPosition(p);
|
||||
if (t.getIndex()< size() -1){
|
||||
return new DNodeIndex<E>(get(t.getIndex() + 1),t.getIndex() + 1);
|
||||
}else{
|
||||
throw new BoundaryViolationException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position<E> prev(Position<E> p) throws InvalidPositionException, BoundaryViolationException {
|
||||
return null;
|
||||
DNodeIndex<E> t = checkPosition(p);
|
||||
if (t.getIndex()> 0){
|
||||
return new DNodeIndex<E>(get(t.getIndex() - 1),t.getIndex() - 1);
|
||||
}else{
|
||||
throw new BoundaryViolationException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addFirst(E e) {
|
||||
|
||||
add (0,e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addLast(E e) {
|
||||
|
||||
add (size(),e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addAfter(Position<E> p, E e) throws InvalidPositionException {
|
||||
|
||||
try {
|
||||
DNodeIndex<E> t = checkPosition(p);
|
||||
add(t.getIndex() + 1, e);
|
||||
}
|
||||
catch (IndexOutOfBoundsException err){
|
||||
throw new InvalidPositionException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addBefore(Position<E> p, E e) throws InvalidPositionException {
|
||||
|
||||
try {
|
||||
DNodeIndex<E> t = checkPosition(p);
|
||||
add(t.getIndex() - 1,e);
|
||||
}
|
||||
catch (IndexOutOfBoundsException err){
|
||||
throw new InvalidPositionException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public E remove(Position<E> p) throws InvalidPositionException {
|
||||
return null;
|
||||
try {
|
||||
DNodeIndex<E> t = checkPosition(p);
|
||||
return remove(t.getIndex());
|
||||
}
|
||||
catch (IndexOutOfBoundsException err){
|
||||
throw new InvalidPositionException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public E set(Position<E> p, E e) throws InvalidPositionException {
|
||||
return null;
|
||||
try {
|
||||
DNodeIndex<E> t = checkPosition(p);
|
||||
return set (t.getIndex(),e);
|
||||
}
|
||||
catch (IndexOutOfBoundsException err){
|
||||
throw new InvalidPositionException();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.toString();
|
||||
String to_return = "";
|
||||
to_return = to_return + "[";
|
||||
|
||||
for ( int i = 0; i< size() ; i++){
|
||||
|
||||
if ( i== size() -1){
|
||||
to_return+=(get(i).toString());
|
||||
}else{
|
||||
to_return+=(get(i).toString() + ",");
|
||||
}
|
||||
}
|
||||
to_return = to_return + "]";
|
||||
|
||||
return to_return;
|
||||
}
|
||||
|
||||
protected DNodeIndex<E> checkPosition(Position<E> p) throws InvalidPositionException {
|
||||
@@ -143,7 +203,7 @@ public class ArraySequence<E> extends ArrayIndexList<E> implements Sequence<E> {
|
||||
|
||||
try {
|
||||
DNodeIndex<E> temp = (DNodeIndex<E>) p;
|
||||
if ((temp.getPrev() == null) || (temp.getNext() == null) || (temp.getIndex() == -1))
|
||||
if ( temp.getIndex() >= size() || temp.getIndex() < 0 )
|
||||
throw new InvalidPositionException
|
||||
("Position does not belong to a valid value");
|
||||
return temp;
|
||||
@@ -152,4 +212,7 @@ public class ArraySequence<E> extends ArrayIndexList<E> implements Sequence<E> {
|
||||
("Position is of wrong type for this array");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -13,35 +13,27 @@ import position.Position;
|
||||
//Additional modifications and methods by xgiovio
|
||||
|
||||
public class DNodeIndex<E> implements Position<E> {
|
||||
private DNodeIndex<E> prev = null;
|
||||
private DNodeIndex<E> next = null;
|
||||
private E element = null;
|
||||
private int index = -1;
|
||||
|
||||
|
||||
public DNodeIndex(DNodeIndex<E> newPrev, DNodeIndex<E> newNext, E elem, int in_idex) {
|
||||
prev = newPrev;
|
||||
next = newNext;
|
||||
public DNodeIndex( E elem, int in_idex) {
|
||||
|
||||
element = elem;
|
||||
index = in_idex;
|
||||
}
|
||||
public DNodeIndex(DNodeIndex<E> newPrev, DNodeIndex<E> newNext, E elem) {
|
||||
prev = newPrev;
|
||||
next = newNext;
|
||||
public DNodeIndex( E elem) {
|
||||
|
||||
element = elem;
|
||||
}
|
||||
}
|
||||
|
||||
public E element() throws InvalidPositionException {
|
||||
if ((prev == null) && (next == null))
|
||||
if (index == -1)
|
||||
throw new InvalidPositionException("Position is not in a list!");
|
||||
return element;
|
||||
}
|
||||
|
||||
public DNodeIndex<E> getNext() { return next; }
|
||||
public DNodeIndex<E> getPrev() { return prev; }
|
||||
|
||||
public void setNext(DNodeIndex<E> newNext) { next = newNext; }
|
||||
public void setPrev(DNodeIndex<E> newPrev) { prev = newPrev; }
|
||||
public void setElement(E newElement) { element = newElement; }
|
||||
|
||||
public int getIndex() {
|
||||
|
||||
Reference in New Issue
Block a user