diff --git a/com/xgiovio/SortedListAdaptablePriorityQueueTest.java b/com/xgiovio/SortedListAdaptablePriorityQueueTest.java new file mode 100644 index 0000000..a603c18 --- /dev/null +++ b/com/xgiovio/SortedListAdaptablePriorityQueueTest.java @@ -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 a = new SortedListAdaptablePriorityQueue(); + 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 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); + + + + } + +} diff --git a/exceptions/InvalidEntryException.java b/exceptions/InvalidEntryException.java new file mode 100644 index 0000000..47416fd --- /dev/null +++ b/exceptions/InvalidEntryException.java @@ -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"); + + } + +} + diff --git a/priorityqueue/AdaptablePriorityQueue.java b/priorityqueue/AdaptablePriorityQueue.java new file mode 100644 index 0000000..46069a5 --- /dev/null +++ b/priorityqueue/AdaptablePriorityQueue.java @@ -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 extends PriorityQueue { + + public Entry remove(Entry e); + public K replaceKey(Entry e, K key) throws InvalidKeyException; + public V replaceValue(Entry e, V value); + +} diff --git a/priorityqueue/MyEntry.java b/priorityqueue/MyEntry.java index f70ff48..4db4d98 100644 --- a/priorityqueue/MyEntry.java +++ b/priorityqueue/MyEntry.java @@ -31,7 +31,7 @@ public class MyEntry implements Entry { return ("(" + key + " - " + value + ")"); } - private K key; - private V value; + protected K key; + protected V value; } diff --git a/priorityqueue/SortedListAdaptablePriorityQueue.java b/priorityqueue/SortedListAdaptablePriorityQueue.java new file mode 100644 index 0000000..dcdfd19 --- /dev/null +++ b/priorityqueue/SortedListAdaptablePriorityQueue.java @@ -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 extends SortedListPriorityQueue implements AdaptablePriorityQueue { + + + public SortedListAdaptablePriorityQueue () { + super(); + } + + public SortedListAdaptablePriorityQueue (Comparator comp) { + super(comp); + } + + + //override from upper class + public Entry insert(K key, V value) throws InvalidKeyException { + try { + LocationAwareEntry t = new LocationAwareEntry(key, value); + Position> pret; + if (data.size() == 0) { + data.addFirst(t); + pret = data.first(); + t.setLocation(pret); + return t; + } else { + Iterator>> itp = data.positions().iterator(); + int status; + Position> 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 remove(Entry e) { + LocationAwareEntry t = checkEntry(e); + return data.remove(t.location()); + } + + @Override + public K replaceKey(Entry e, K key) throws InvalidKeyException { + checkKey(key); + LocationAwareEntry t = checkEntry(e); + K oldKey = t.setKey(key); + movebefore(t.location()); + moveafter(t.location()); + return oldKey; + + } + + @Override + public V replaceValue(Entry e, V value) { + LocationAwareEntry t = checkEntry(e); + return t.setValue(value); + } + + + // inner class + protected static class LocationAwareEntry extends MyEntry { + protected Position> 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> location() { + return loc; + } + protected Position < Entry >setLocation(Position< Entry> pos) { + Position > 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 checkEntry(Entry 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> in){ + + try { + for (Position> p = data.prev(in);true; p = data.prev(p)) { + if (c.compare(p.element().getKey(), in.element().getKey()) > 0) { + + LocationAwareEntry t = (LocationAwareEntry) ((DNode>) in).element(); + ((DNode>) in).setElement((LocationAwareEntry) ((DNode>) p).element()); + ((DNode>) p).setElement(t); + ((LocationAwareEntry )(p.element())).setLocation(p); + ((LocationAwareEntry )(in.element())).setLocation(in); + in = p; + + + } else { + break; + } + } + + + } + catch (BoundaryViolationException e){ + + } + } + + void moveafter (Position> in ){ + try { + for (Position> p = data.next(in);true; p = data.next(p)) { + if (c.compare(p.element().getKey(), in.element().getKey()) < 0) { + + LocationAwareEntry t = (LocationAwareEntry) ((DNode>) in).element(); + ((DNode>) in).setElement((LocationAwareEntry) ((DNode>) p).element()); + ((DNode>) p).setElement(t); + ((LocationAwareEntry )(p.element())).setLocation(p); + ((LocationAwareEntry )(in.element())).setLocation(in); + in = p; + + + } else { + break; + } + } + + + } + catch (BoundaryViolationException e){ + + } + + + + } + + + + + + +} diff --git a/priorityqueue/SortedListPriorityQueue.java b/priorityqueue/SortedListPriorityQueue.java index 12dae67..a512fb3 100644 --- a/priorityqueue/SortedListPriorityQueue.java +++ b/priorityqueue/SortedListPriorityQueue.java @@ -17,8 +17,8 @@ import java.util.Iterator; */ public class SortedListPriorityQueue implements PriorityQueue { - private NodePositionList> data = new NodePositionList>(); - private Comparator c; + protected NodePositionList> data = new NodePositionList>(); + protected Comparator c; public SortedListPriorityQueue(){ c = new DefaultComparator(); @@ -55,9 +55,9 @@ public class SortedListPriorityQueue implements PriorityQueue { data.addFirst(t); return t; } else { - Iterator>> itp = data.positions().iterator(); + Iterator>> itp = data.positions().iterator(); int status; - Position> temp_pos = null; + Position> 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 implements PriorityQueue { @Override public String toString() { - Iterator> it = data.iterator(); + Iterator> it = data.iterator(); String to_return = ""; to_return = to_return + "[ "; for (;it.hasNext();){ - MyEntry t = it.next(); + Entry t = it.next(); if (it.hasNext()) { - to_return+=(t.toString() + " , "); + to_return+=( t.toString() + " , " ); } else { to_return+=(t.toString()); diff --git a/priorityqueue/heap/HeapAdaptablePriorityQueue.java b/priorityqueue/heap/HeapAdaptablePriorityQueue.java new file mode 100644 index 0000000..7cc7336 --- /dev/null +++ b/priorityqueue/heap/HeapAdaptablePriorityQueue.java @@ -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 extends HeapPriorityQueue implements AdaptablePriorityQueue { + + public HeapAdaptablePriorityQueue() { + super(); + } + public HeapAdaptablePriorityQueue(Comparator comp) { + super(comp); + } + + //override from parent class + public Entry insert (K k, V v) throws InvalidKeyException { + checkKey(k); + LocationAwareEntry entry = new LocationAwareEntry(k,v); + Position > z = heap.add(entry); + entry.setLocation(z); + upHeap(z); + return entry; + } + + protected void swap(Position> u,Position> v) { + super.swap(u,v); + getEntry(u).setLocation(u); + getEntry(v).setLocation(v); + } + + //end override from parent class + + + + + + public Entry remove(Entry entry) throws InvalidEntryException { + LocationAwareEntry ee = checkEntry(entry); + Position < Entry> p = ee.location(); + if(size() == 1) + return (Entry) heap.remove(); + replaceEntry(p,(LocationAwareEntry)heap.remove()); + upHeap(p); + downHeap(p); + ee.setLocation(null); + return ee; + } + + + + + public K replaceKey(Entry entry, K k) throws InvalidEntryException,InvalidKeyException + { + checkKey(k); + LocationAwareEntry ee = checkEntry(entry); + K oldKey = ee.setKey(k); + upHeap(ee.location()); + downHeap(ee.location()); + return oldKey; + } + + public V replaceValue(Entry e, V value) throws InvalidEntryException + { + LocationAwareEntry ee = checkEntry(e); + return ee.setValue(value); + } + + + //auxiliar + + protected Position< Entry> replaceEntry(Position> v, LocationAwareEntry e) { + heap.replace(v,e); + return e.setLocation(v); + } + + protected LocationAwareEntry getEntry(Position >p) { + return (LocationAwareEntry) p.element(); + } + + protected LocationAwareEntry checkEntry(Entry 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 extends MyEntry { + protected Position> 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> location() { + return loc; + } + protected Position < Entry >setLocation(Position< Entry> pos) { + Position > 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(); + } + + +} diff --git a/priorityqueue/heap/HeapPriorityQueue.java b/priorityqueue/heap/HeapPriorityQueue.java index 3038628..f516e72 100644 --- a/priorityqueue/heap/HeapPriorityQueue.java +++ b/priorityqueue/heap/HeapPriorityQueue.java @@ -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,17 +18,7 @@ public class HeapPriorityQueue implements PriorityQueue { protected Comparator comp; - protected static class MyEntry implements Entry { - 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() { + public HeapPriorityQueue() { heap = new ArrayListCompleteBinaryTree>(); comp = new DefaultComparator(); }