Implementato HeapAdaptablePriorityQueue mediante heap. Implementato SortedListAdaptablePriorityQueue estendendo SortedListPriorityQueue utilizzando l'interfacciaAdaptablePriorityQueue . L'implementazione di entrambi una una classe LocationAwareEntry estensione di Entry.

This commit is contained in:
2014-05-11 17:54:47 +02:00
parent dc7774bef6
commit 597626b34c
8 changed files with 466 additions and 20 deletions

View File

@@ -0,0 +1,60 @@
package com.xgiovio;
import priorityqueue.Entry;
import priorityqueue.SortedListAdaptablePriorityQueue;
import priorityqueue.SortedListPriorityQueue;
import java.security.InvalidKeyException;
/**
* Created with MONSTER.
* User: xgiovio
* Date: 02/04/2014
* Time: 00:10
*/
public class SortedListAdaptablePriorityQueueTest {
public static void main(String[] args) throws InvalidKeyException{
SortedListAdaptablePriorityQueue<Integer,String> a = new SortedListAdaptablePriorityQueue<Integer, String>();
a.insert(5,"hello");
System.out.print(a.size());
System.out.print(a);
a.insert(11,"sdas");
System.out.print(a.size());
a.insert(21,"sdas");
a.insert(1,"sdas");
a.insert(-10,"slkjldas");
System.out.print(a);
System.out.print(a.size());
a.insert(21,"sdas");
a.insert(1,"sdas");
a.insert(-10,"slkjldas");
System.out.print(a);
System.out.println(a.size());
a.insert(3,"ale");
System.out.println(a);
Entry<Integer,String> t = a.insert(20, "ale");
System.out.println(a);
a.replaceKey(t,-5);
System.out.println(a);
a.replaceKey(t,-20);
System.out.println(a);
a.replaceKey(t,30);
System.out.println(a);
a.replaceValue(t,"yoo");
System.out.println(a);
}
}

View File

@@ -0,0 +1,21 @@
package exceptions;
/**
* Thrown when a position is determined to be invalid.
* @author Roberto Tamassia, Michael Goodrich
*/
//Copyright (c) 2003 Brown University, Providence, RI
//Additional modifications and methods by xgiovio
public class InvalidEntryException extends RuntimeException {
public InvalidEntryException(String err) {
super(err);
}
public InvalidEntryException() {
super("Invalid Entry");
}
}

View File

@@ -0,0 +1,20 @@
package priorityqueue;
import priorityqueue.Entry;
import priorityqueue.PriorityQueue;
import java.security.InvalidKeyException;
/**
* Created with MONSTER.
* User: xgiovio
* Date: 11/05/2014
* Time: 14:51
*/
public interface AdaptablePriorityQueue<K,V> extends PriorityQueue<K,V> {
public Entry<K,V> remove(Entry<K,V> e);
public K replaceKey(Entry<K,V> e, K key) throws InvalidKeyException;
public V replaceValue(Entry<K,V> e, V value);
}

View File

@@ -31,7 +31,7 @@ public class MyEntry<K,V> implements Entry<K,V> {
return ("(" + key + " - " + value + ")");
}
private K key;
private V value;
protected K key;
protected V value;
}

View File

@@ -0,0 +1,210 @@
package priorityqueue;
import deque.utility.DLNode;
import exceptions.BoundaryViolationException;
import exceptions.InvalidEntryException;
import position.Position;
import position.utility.DNode;
import java.security.InvalidKeyException;
import java.util.Comparator;
import java.util.Iterator;
/**
* Created with MONSTER.
* User: xgiovio
* Date: 11/05/2014
* Time: 16:15
*/
public class SortedListAdaptablePriorityQueue <K,V> extends SortedListPriorityQueue<K,V> implements AdaptablePriorityQueue<K,V> {
public SortedListAdaptablePriorityQueue () {
super();
}
public SortedListAdaptablePriorityQueue (Comparator<K> comp) {
super(comp);
}
//override from upper class
public Entry<K, V> insert(K key, V value) throws InvalidKeyException {
try {
LocationAwareEntry<K, V> t = new LocationAwareEntry<K, V>(key, value);
Position<Entry<K,V>> pret;
if (data.size() == 0) {
data.addFirst(t);
pret = data.first();
t.setLocation(pret);
return t;
} else {
Iterator<Position<Entry<K, V>>> itp = data.positions().iterator();
int status;
Position<Entry<K, V>> temp_pos = null;
for (; itp.hasNext(); ) {
temp_pos = itp.next();
status = c.compare(temp_pos.element().getKey(), key);
if (status > 0) {
pret = data.addBefore(temp_pos, t);
t.setLocation(pret);
return t;
}
}
data.addLast(t);
return t;
}
}
catch (ClassCastException err){
throw new InvalidKeyException();
}
}
// end override from upper class
@Override
public Entry<K, V> remove(Entry<K, V> e) {
LocationAwareEntry<K,V> t = checkEntry(e);
return data.remove(t.location());
}
@Override
public K replaceKey(Entry<K, V> e, K key) throws InvalidKeyException {
checkKey(key);
LocationAwareEntry<K,V> t = checkEntry(e);
K oldKey = t.setKey(key);
movebefore(t.location());
moveafter(t.location());
return oldKey;
}
@Override
public V replaceValue(Entry<K, V> e, V value) {
LocationAwareEntry<K,V> t = checkEntry(e);
return t.setValue(value);
}
// inner class
protected static class LocationAwareEntry<K,V> extends MyEntry<K,V> {
protected Position<Entry<K, V>> loc = null;
public LocationAwareEntry(K k, V v) {
super(k, v);
}
public LocationAwareEntry(K k, V v, Position pos) {
super(k, v);
loc = pos;
}
protected Position < Entry<K,V>> location() {
return loc;
}
protected Position < Entry<K,V> >setLocation(Position< Entry<K,V>> pos) {
Position <Entry<K,V>> oldPosition = location();
loc = pos;
return oldPosition;
}
protected K setKey(K k) {
K oldKey = getKey();
key = k;
return oldKey;
}
protected V setValue(V v) {
V oldValue = getValue();
value = v;
return oldValue;
}
public String toString() { return "(" + key + "," + value + ")"; }
}
// auxiliar
protected LocationAwareEntry<K,V> checkEntry(Entry<K,V> e) throws InvalidEntryException
{
if(e == null || !(e instanceof LocationAwareEntry) || isEmpty() )
throw new InvalidEntryException("entrata non valida");
return (LocationAwareEntry) e;
}
protected void checkKey(K key) throws InvalidKeyException {
try {
c.compare(key,key);
}
catch(Exception e) {
throw new InvalidKeyException("Invalid key");
}
}
void movebefore (Position<Entry<K,V>> in){
try {
for (Position<Entry<K, V>> p = data.prev(in);true; p = data.prev(p)) {
if (c.compare(p.element().getKey(), in.element().getKey()) > 0) {
LocationAwareEntry<K, V> t = (LocationAwareEntry<K, V>) ((DNode<Entry<K, V>>) in).element();
((DNode<Entry<K, V>>) in).setElement((LocationAwareEntry<K, V>) ((DNode<Entry<K, V>>) p).element());
((DNode<Entry<K, V>>) p).setElement(t);
((LocationAwareEntry<K, V> )(p.element())).setLocation(p);
((LocationAwareEntry<K, V> )(in.element())).setLocation(in);
in = p;
} else {
break;
}
}
}
catch (BoundaryViolationException e){
}
}
void moveafter (Position<Entry<K,V>> in ){
try {
for (Position<Entry<K, V>> p = data.next(in);true; p = data.next(p)) {
if (c.compare(p.element().getKey(), in.element().getKey()) < 0) {
LocationAwareEntry<K, V> t = (LocationAwareEntry<K, V>) ((DNode<Entry<K, V>>) in).element();
((DNode<Entry<K, V>>) in).setElement((LocationAwareEntry<K, V>) ((DNode<Entry<K, V>>) p).element());
((DNode<Entry<K, V>>) p).setElement(t);
((LocationAwareEntry<K, V> )(p.element())).setLocation(p);
((LocationAwareEntry<K, V> )(in.element())).setLocation(in);
in = p;
} else {
break;
}
}
}
catch (BoundaryViolationException e){
}
}
}

View File

@@ -17,8 +17,8 @@ import java.util.Iterator;
*/
public class SortedListPriorityQueue<K,V> implements PriorityQueue<K,V> {
private NodePositionList<MyEntry<K,V>> data = new NodePositionList<MyEntry<K, V>>();
private Comparator<K> c;
protected NodePositionList<Entry<K,V>> data = new NodePositionList<Entry<K, V>>();
protected Comparator<K> c;
public SortedListPriorityQueue(){
c = new DefaultComparator<K>();
@@ -55,9 +55,9 @@ public class SortedListPriorityQueue<K,V> implements PriorityQueue<K,V> {
data.addFirst(t);
return t;
} else {
Iterator<Position<MyEntry<K, V>>> itp = data.positions().iterator();
Iterator<Position<Entry<K, V>>> itp = data.positions().iterator();
int status;
Position<MyEntry<K, V>> temp_pos = null;
Position<Entry<K, V>> temp_pos = null;
for (; itp.hasNext(); ) {
temp_pos = itp.next();
status = c.compare(temp_pos.element().getKey(), key);
@@ -89,16 +89,16 @@ public class SortedListPriorityQueue<K,V> implements PriorityQueue<K,V> {
@Override
public String toString() {
Iterator<MyEntry<K, V>> it = data.iterator();
Iterator<Entry<K, V>> it = data.iterator();
String to_return = "";
to_return = to_return + "[ ";
for (;it.hasNext();){
MyEntry<K, V> t = it.next();
Entry<K, V> t = it.next();
if (it.hasNext()) {
to_return+=(t.toString() + " , ");
to_return+=( t.toString() + " , " );
} else {
to_return+=(t.toString());

View File

@@ -0,0 +1,144 @@
package priorityqueue.heap;
import exceptions.InvalidEntryException;
import position.Position;
import priorityqueue.AdaptablePriorityQueue;
import priorityqueue.Entry;
import priorityqueue.MyEntry;
import java.security.InvalidKeyException;
import java.util.Comparator;
/**
* Created with MONSTER.
* User: xgiovio
* Date: 11/05/2014
* Time: 14:55
*/
public class HeapAdaptablePriorityQueue<K,V> extends HeapPriorityQueue<K,V> implements AdaptablePriorityQueue<K,V> {
public HeapAdaptablePriorityQueue() {
super();
}
public HeapAdaptablePriorityQueue(Comparator comp) {
super(comp);
}
//override from parent class
public Entry<K,V> insert (K k, V v) throws InvalidKeyException {
checkKey(k);
LocationAwareEntry<K,V> entry = new LocationAwareEntry<K,V>(k,v);
Position <Entry<K,V>> z = heap.add(entry);
entry.setLocation(z);
upHeap(z);
return entry;
}
protected void swap(Position<Entry<K,V>> u,Position<Entry<K,V>> v) {
super.swap(u,v);
getEntry(u).setLocation(u);
getEntry(v).setLocation(v);
}
//end override from parent class
public Entry<K,V> remove(Entry<K,V> entry) throws InvalidEntryException {
LocationAwareEntry<K,V> ee = checkEntry(entry);
Position < Entry<K,V>> p = ee.location();
if(size() == 1)
return (Entry<K,V>) heap.remove();
replaceEntry(p,(LocationAwareEntry<K,V>)heap.remove());
upHeap(p);
downHeap(p);
ee.setLocation(null);
return ee;
}
public K replaceKey(Entry<K,V> entry, K k) throws InvalidEntryException,InvalidKeyException
{
checkKey(k);
LocationAwareEntry<K,V> ee = checkEntry(entry);
K oldKey = ee.setKey(k);
upHeap(ee.location());
downHeap(ee.location());
return oldKey;
}
public V replaceValue(Entry<K,V> e, V value) throws InvalidEntryException
{
LocationAwareEntry<K,V> ee = checkEntry(e);
return ee.setValue(value);
}
//auxiliar
protected Position< Entry<K,V>> replaceEntry(Position<Entry<K,V>> v, LocationAwareEntry<K,V> e) {
heap.replace(v,e);
return e.setLocation(v);
}
protected LocationAwareEntry<K,V> getEntry(Position <Entry<K,V> >p) {
return (LocationAwareEntry<K,V>) p.element();
}
protected LocationAwareEntry<K,V> checkEntry(Entry<K,V> e) throws InvalidEntryException
{
if(e == null || !(e instanceof LocationAwareEntry) || isEmpty() )
throw new InvalidEntryException("entrata non valida");
return (LocationAwareEntry) e;
}
// inner class
protected static class LocationAwareEntry<K,V> extends MyEntry<K,V> {
protected Position<Entry<K, V>> loc = null;
public LocationAwareEntry(K k, V v) {
super(k, v);
}
public LocationAwareEntry(K k, V v, Position pos) {
super(k, v);
loc = pos;
}
protected Position < Entry<K,V>> location() {
return loc;
}
protected Position < Entry<K,V> >setLocation(Position< Entry<K,V>> pos) {
Position <Entry<K,V>> oldPosition = location();
loc = pos;
return oldPosition;
}
protected K setKey(K k) {
K oldKey = getKey();
key = k;
return oldKey;
}
protected V setValue(V v) {
V oldValue = getValue();
value = v;
return oldValue;
}
public String toString() { return "(" + key + "," + value + ")"; }
}
public String toString() {
return super.toString();
}
}

View File

@@ -2,6 +2,7 @@ package priorityqueue.heap;
import exceptions.EmptyPriorityQueueException;
import position.Position;
import priorityqueue.Entry;
import priorityqueue.MyEntry;
import priorityqueue.PriorityQueue;
import tree.binarytree.heap.ArrayListCompleteBinaryTree;
import tree.binarytree.heap.CompleteBinaryTree;
@@ -17,16 +18,6 @@ public class HeapPriorityQueue<K,V> implements PriorityQueue<K,V> {
protected Comparator<K> comp;
protected static class MyEntry<K,V> implements Entry<K,V> {
protected K key;
protected V value;
public MyEntry(K k, V v) { key = k; value = v; }
public K getKey() { return key; }
public V getValue() { return value; }
public String toString() { return "(" + key + "," + value + ")"; }
}
public HeapPriorityQueue() {
heap = new ArrayListCompleteBinaryTree<Entry<K,V>>();
comp = new DefaultComparator<K>();